eamad.c
Go to the documentation of this file.
1 /*
2  * Electronic Arts Madcow Video Decoder
3  * Copyright (c) 2007-2009 Peter Ross
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "get_bits.h"
34 #include "dsputil.h"
35 #include "aandcttab.h"
36 #include "mpeg12.h"
37 #include "mpeg12data.h"
38 #include "libavutil/imgutils.h"
39 
40 #define EA_PREAMBLE_SIZE 8
41 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
42 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
43 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
44 
45 typedef struct MadContext {
50  unsigned int bitstream_buf_size;
52 } MadContext;
53 
54 static void bswap16_buf(uint16_t *dst, const uint16_t *src, int count)
55 {
56  int i;
57  for (i=0; i<count; i++)
58  dst[i] = av_bswap16(src[i]);
59 }
60 
62 {
63  MadContext *t = avctx->priv_data;
64  MpegEncContext *s = &t->s;
65  s->avctx = avctx;
66  avctx->pix_fmt = PIX_FMT_YUV420P;
67  if (avctx->idct_algo == FF_IDCT_AUTO)
68  avctx->idct_algo = FF_IDCT_EA;
69  dsputil_init(&s->dsp, avctx);
72  return 0;
73 }
74 
75 static inline void comp(unsigned char *dst, int dst_stride,
76  unsigned char *src, int src_stride, int add)
77 {
78  int j, i;
79  for (j=0; j<8; j++)
80  for (i=0; i<8; i++)
81  dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
82 }
83 
84 static inline void comp_block(MadContext *t, int mb_x, int mb_y,
85  int j, int mv_x, int mv_y, int add)
86 {
87  MpegEncContext *s = &t->s;
88  if (j < 4) {
89  comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
90  t->frame.linesize[0],
91  t->last_frame.data[0] + (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x,
92  t->last_frame.linesize[0], add);
93  } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
94  int index = j - 3;
95  comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
96  t->frame.linesize[index],
97  t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
98  t->last_frame.linesize[index], add);
99  }
100 }
101 
102 static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
103 {
104  MpegEncContext *s = &t->s;
105  if (j < 4) {
106  s->dsp.idct_put(
107  t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
108  t->frame.linesize[0], block);
109  } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
110  int index = j - 3;
111  s->dsp.idct_put(
112  t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
113  t->frame.linesize[index], block);
114  }
115 }
116 
117 static inline void decode_block_intra(MadContext * t, DCTELEM * block)
118 {
119  MpegEncContext *s = &t->s;
120  int level, i, j, run;
121  RLTable *rl = &ff_rl_mpeg1;
122  const uint8_t *scantable = s->intra_scantable.permutated;
123  int16_t *quant_matrix = s->intra_matrix;
124 
125  block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
126 
127  /* The RL decoder is derived from mpeg1_decode_block_intra;
128  Escaped level and run values a decoded differently */
129  i = 0;
130  {
131  OPEN_READER(re, &s->gb);
132  /* now quantify & encode AC coefficients */
133  for (;;) {
134  UPDATE_CACHE(re, &s->gb);
135  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
136 
137  if (level == 127) {
138  break;
139  } else if (level != 0) {
140  i += run;
141  if (i > 63) {
143  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
144  return;
145  }
146  j = scantable[i];
147  level = (level*quant_matrix[j]) >> 4;
148  level = (level-1)|1;
149  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
150  LAST_SKIP_BITS(re, &s->gb, 1);
151  } else {
152  /* escape */
153  UPDATE_CACHE(re, &s->gb);
154  level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
155 
156  UPDATE_CACHE(re, &s->gb);
157  run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
158 
159  i += run;
160  if (i > 63) {
162  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
163  return;
164  }
165  j = scantable[i];
166  if (level < 0) {
167  level = -level;
168  level = (level*quant_matrix[j]) >> 4;
169  level = (level-1)|1;
170  level = -level;
171  } else {
172  level = (level*quant_matrix[j]) >> 4;
173  level = (level-1)|1;
174  }
175  }
176 
177  block[j] = level;
178  }
179  CLOSE_READER(re, &s->gb);
180  }
181 }
182 
184 {
185  int value = 0;
186  if (get_bits1(gb)) {
187  if (get_bits1(gb))
188  value = -17;
189  value += get_bits(gb, 4) + 1;
190  }
191  return value;
192 }
193 
194 static void decode_mb(MadContext *t, int inter)
195 {
196  MpegEncContext *s = &t->s;
197  int mv_map = 0;
198  int mv_x, mv_y;
199  int j;
200 
201  if (inter) {
202  int v = decode210(&s->gb);
203  if (v < 2) {
204  mv_map = v ? get_bits(&s->gb, 6) : 63;
205  mv_x = decode_motion(&s->gb);
206  mv_y = decode_motion(&s->gb);
207  } else {
208  mv_map = 0;
209  }
210  }
211 
212  for (j=0; j<6; j++) {
213  if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
214  int add = 2*decode_motion(&s->gb);
215  comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
216  } else {
217  s->dsp.clear_block(t->block);
218  decode_block_intra(t, t->block);
219  idct_put(t, t->block, s->mb_x, s->mb_y, j);
220  }
221  }
222 }
223 
224 static void calc_intra_matrix(MadContext *t, int qscale)
225 {
226  MpegEncContext *s = &t->s;
227  int i;
228 
229  if (s->avctx->idct_algo == FF_IDCT_EA) {
231  for (i=1; i<64; i++)
232  s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
233  } else {
235  for (i=1; i<64; i++)
236  s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
237  }
238 }
239 
240 static int decode_frame(AVCodecContext *avctx,
241  void *data, int *data_size,
242  AVPacket *avpkt)
243 {
244  const uint8_t *buf = avpkt->data;
245  int buf_size = avpkt->size;
246  MadContext *t = avctx->priv_data;
247  GetByteContext gb;
248  MpegEncContext *s = &t->s;
249  int chunk_type;
250  int inter;
251 
252  bytestream2_init(&gb, buf, buf_size);
253 
254  chunk_type = bytestream2_get_le32(&gb);
255  inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
256  bytestream2_skip(&gb, 10);
257 
258  av_reduce(&avctx->time_base.num, &avctx->time_base.den,
259  bytestream2_get_le16(&gb), 1000, 1<<30);
260 
261  s->width = bytestream2_get_le16(&gb);
262  s->height = bytestream2_get_le16(&gb);
263  bytestream2_skip(&gb, 1);
264  calc_intra_matrix(t, bytestream2_get_byte(&gb));
265  bytestream2_skip(&gb, 2);
266 
267  if (bytestream2_get_bytes_left(&gb) < 2) {
268  av_log(avctx, AV_LOG_ERROR, "Input data too small\n");
269  return AVERROR_INVALIDDATA;
270  }
271 
272  if (avctx->width != s->width || avctx->height != s->height) {
273  if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
274  return -1;
275  avcodec_set_dimensions(avctx, s->width, s->height);
276  if (t->frame.data[0])
277  avctx->release_buffer(avctx, &t->frame);
278  }
279 
280  t->frame.reference = 1;
281  if (!t->frame.data[0]) {
282  if (avctx->get_buffer(avctx, &t->frame) < 0) {
283  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
284  return -1;
285  }
286  }
287 
290  if (!t->bitstream_buf)
291  return AVERROR(ENOMEM);
292  bswap16_buf(t->bitstream_buf, (const uint16_t *)(buf + bytestream2_tell(&gb)),
293  bytestream2_get_bytes_left(&gb) / 2);
295  for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
296  for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
297  decode_mb(t, inter);
298 
299  *data_size = sizeof(AVFrame);
300  *(AVFrame*)data = t->frame;
301 
302  if (chunk_type != MADe_TAG)
303  FFSWAP(AVFrame, t->frame, t->last_frame);
304 
305  return buf_size;
306 }
307 
309 {
310  MadContext *t = avctx->priv_data;
311  if (t->frame.data[0])
312  avctx->release_buffer(avctx, &t->frame);
313  if (t->last_frame.data[0])
314  avctx->release_buffer(avctx, &t->last_frame);
316  return 0;
317 }
318 
320  .name = "eamad",
321  .type = AVMEDIA_TYPE_VIDEO,
322  .id = CODEC_ID_MAD,
323  .priv_data_size = sizeof(MadContext),
324  .init = decode_init,
325  .close = decode_end,
326  .decode = decode_frame,
327  .capabilities = CODEC_CAP_DR1,
328  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
329 };
static void decode_block_intra(MadContext *t, DCTELEM *block)
Definition: eamad.c:117
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
const uint8_t ff_zigzag_direct[64]
Definition: dsputil.c:61
Audio Video Frame.
Definition: avcodec.h:985
AVCodec ff_eamad_decoder
Definition: eamad.c:319
DCTELEM block[64]
Definition: eamad.c:51
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
av_cold void ff_mpeg12_init_vlcs(void)
Definition: mpeg12.c:677
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
#define av_bswap16
Definition: bswap.h:31
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:55
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: eamad.c:240
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:138
uint8_t permutated[64]
Definition: dsputil.h:198
static void decode_mb(MadContext *t, int inter)
Definition: eamad.c:194
uint8_t run
Definition: svq3.c:123
AVCodec.
Definition: avcodec.h:3189
RLTable.
Definition: rl.h:38
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:71
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
#define v(n)
Definition: regs.h:34
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
struct MadContext MadContext
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
#define FF_IDCT_EA
Definition: avcodec.h:1932
static void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
Definition: eamad.c:102
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
bitstream reader API header.
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:505
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2789
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:133
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define FF_IDCT_AUTO
Definition: avcodec.h:1911
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:640
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
const uint16_t ff_mpeg1_default_intra_matrix[64]
Definition: mpeg12data.c:30
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:167
void(* idct_put)(uint8_t *dest, int line_size, DCTELEM *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: dsputil.h:485
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:157
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
static av_cold int decode_end(AVCodecContext *avctx)
Definition: eamad.c:308
GetBitContext gb
Definition: mpegvideo.h:603
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
void(* clear_block)(DCTELEM *block)
Definition: dsputil.h:265
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:175
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:213
static DCTELEM block[64]
Definition: dct-test.c:189
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1910
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:447
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
static void bswap16_buf(uint16_t *dst, const uint16_t *src, int count)
Definition: eamad.c:54
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:180
#define TEX_VLC_BITS
Definition: dv.c:67
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:186
static int decode210(GetBitContext *gb)
Definition: get_bits.h:505
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:191
AVFrame frame
Definition: eamad.c:47
#define MADe_TAG
Definition: eamad.c:43
AVFrame last_frame
Definition: eamad.c:48
external API header
static void comp_block(MadContext *t, int mb_x, int mb_y, int j, int mv_x, int mv_y, int add)
Definition: eamad.c:84
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
const uint16_t ff_inv_aanscales[64]
Definition: aandcttab.c:38
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
ScanTable intra_scantable
Definition: mpegvideo.h:256
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:205
MPEG1/2 tables.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
#define OPEN_READER(name, gb)
Definition: get_bits.h:124
static int decode_motion(GetBitContext *gb)
Definition: eamad.c:183
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
int index
Definition: gxfenc.c:73
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:343
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
short DCTELEM
Definition: dsputil.h:39
#define MADm_TAG
Definition: eamad.c:42
AAN (Arai Agui Nakajima) (I)DCT tables.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
uint8_t level
Definition: svq3.c:123
MpegEncContext.
Definition: mpegvideo.h:201
struct AVCodecContext * avctx
Definition: mpegvideo.h:203
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:187
void * bitstream_buf
Definition: eamad.c:49
static void calc_intra_matrix(MadContext *t, int qscale)
Definition: eamad.c:224
int den
denominator
Definition: rational.h:45
MpegEncContext s
Definition: eamad.c:46
DSP utils.
void * priv_data
Definition: avcodec.h:1531
float re
Definition: fft-test.c:61
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:124
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:421
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:75
#define FFSWAP(type, a, b)
Definition: common.h:58
unsigned int bitstream_buf_size
Definition: eamad.c:50
static av_cold int decode_init(AVCodecContext *avctx)
Definition: eamad.c:61