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 "get_bits.h"
33 #include "dsputil.h"
34 #include "aandcttab.h"
35 #include "eaidct.h"
36 #include "internal.h"
37 #include "mpeg12.h"
38 #include "mpeg12data.h"
39 #include "libavutil/imgutils.h"
40 
41 #define EA_PREAMBLE_SIZE 8
42 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
43 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
44 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
45 
46 typedef struct MadContext {
53  unsigned int bitstream_buf_size;
56  uint16_t quant_matrix[64];
57  int mb_x;
58  int mb_y;
59 } MadContext;
60 
62 {
63  MadContext *s = avctx->priv_data;
64  s->avctx = avctx;
65  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
66  ff_dsputil_init(&s->dsp, avctx);
70  return 0;
71 }
72 
73 static inline void comp(unsigned char *dst, int dst_stride,
74  unsigned char *src, int src_stride, int add)
75 {
76  int j, i;
77  for (j=0; j<8; j++)
78  for (i=0; i<8; i++)
79  dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
80 }
81 
82 static inline void comp_block(MadContext *t, int mb_x, int mb_y,
83  int j, int mv_x, int mv_y, int add)
84 {
85  if (j < 4) {
86  comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
87  t->frame.linesize[0],
88  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,
89  t->last_frame.linesize[0], add);
90  } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
91  int index = j - 3;
92  comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
93  t->frame.linesize[index],
94  t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
95  t->last_frame.linesize[index], add);
96  }
97 }
98 
99 static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
100 {
101  if (j < 4) {
103  t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
104  t->frame.linesize[0], block);
105  } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
106  int index = j - 3;
108  t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
109  t->frame.linesize[index], block);
110  }
111 }
112 
113 static inline void decode_block_intra(MadContext *s, DCTELEM * block)
114 {
115  int level, i, j, run;
116  RLTable *rl = &ff_rl_mpeg1;
117  const uint8_t *scantable = s->scantable.permutated;
118  int16_t *quant_matrix = s->quant_matrix;
119 
120  block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
121 
122  /* The RL decoder is derived from mpeg1_decode_block_intra;
123  Escaped level and run values a decoded differently */
124  i = 0;
125  {
126  OPEN_READER(re, &s->gb);
127  /* now quantify & encode AC coefficients */
128  for (;;) {
129  UPDATE_CACHE(re, &s->gb);
130  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
131 
132  if (level == 127) {
133  break;
134  } else if (level != 0) {
135  i += run;
136  j = scantable[i];
137  level = (level*quant_matrix[j]) >> 4;
138  level = (level-1)|1;
139  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
140  LAST_SKIP_BITS(re, &s->gb, 1);
141  } else {
142  /* escape */
143  UPDATE_CACHE(re, &s->gb);
144  level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
145 
146  UPDATE_CACHE(re, &s->gb);
147  run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
148 
149  i += run;
150  j = scantable[i];
151  if (level < 0) {
152  level = -level;
153  level = (level*quant_matrix[j]) >> 4;
154  level = (level-1)|1;
155  level = -level;
156  } else {
157  level = (level*quant_matrix[j]) >> 4;
158  level = (level-1)|1;
159  }
160  }
161  if (i > 63) {
162  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
163  return;
164  }
165 
166  block[j] = level;
167  }
168  CLOSE_READER(re, &s->gb);
169  }
170 }
171 
173 {
174  int value = 0;
175  if (get_bits1(gb)) {
176  if (get_bits1(gb))
177  value = -17;
178  value += get_bits(gb, 4) + 1;
179  }
180  return value;
181 }
182 
183 static void decode_mb(MadContext *s, int inter)
184 {
185  int mv_map = 0;
186  int mv_x, mv_y;
187  int j;
188 
189  if (inter) {
190  int v = decode210(&s->gb);
191  if (v < 2) {
192  mv_map = v ? get_bits(&s->gb, 6) : 63;
193  mv_x = decode_motion(&s->gb);
194  mv_y = decode_motion(&s->gb);
195  } else {
196  mv_map = 0;
197  }
198  }
199 
200  for (j=0; j<6; j++) {
201  if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
202  int add = 2*decode_motion(&s->gb);
203  comp_block(s, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
204  } else {
205  s->dsp.clear_block(s->block);
206  decode_block_intra(s, s->block);
207  idct_put(s, s->block, s->mb_x, s->mb_y, j);
208  }
209  }
210 }
211 
212 static void calc_quant_matrix(MadContext *s, int qscale)
213 {
214  int i;
215 
217  for (i=1; i<64; i++)
218  s->quant_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
219 }
220 
221 static int decode_frame(AVCodecContext *avctx,
222  void *data, int *got_frame,
223  AVPacket *avpkt)
224 {
225  const uint8_t *buf = avpkt->data;
226  int buf_size = avpkt->size;
227  const uint8_t *buf_end = buf+buf_size;
228  MadContext *s = avctx->priv_data;
229  int width, height;
230  int chunk_type;
231  int inter;
232 
233  if (buf_size < 17) {
234  av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
235  *got_frame = 0;
236  return -1;
237  }
238 
239  chunk_type = AV_RL32(&buf[0]);
240  inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
241  buf += 8;
242 
243  av_reduce(&avctx->time_base.num, &avctx->time_base.den,
244  AV_RL16(&buf[6]), 1000, 1<<30);
245 
246  width = AV_RL16(&buf[8]);
247  height = AV_RL16(&buf[10]);
248  calc_quant_matrix(s, buf[13]);
249  buf += 16;
250 
251  if (avctx->width != width || avctx->height != height) {
252  if (av_image_check_size(width, height, 0, avctx) < 0)
253  return -1;
254  avcodec_set_dimensions(avctx, width, height);
255  if (s->frame.data[0])
256  avctx->release_buffer(avctx, &s->frame);
257  }
258 
259  s->frame.reference = 1;
260  if (!s->frame.data[0]) {
261  if (ff_get_buffer(avctx, &s->frame) < 0) {
262  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
263  return -1;
264  }
265  }
266 
267  if (inter && !s->last_frame.data[0]) {
268  int ret;
269  av_log(avctx, AV_LOG_WARNING, "Missing reference frame.\n");
270  s->last_frame.reference = 1;
271  ret = ff_get_buffer(avctx, &s->last_frame);
272  if (ret < 0)
273  return ret;
274  memset(s->last_frame.data[0], 0, s->last_frame.height *
275  s->last_frame.linesize[0]);
276  memset(s->last_frame.data[1], 0x80, s->last_frame.height / 2 *
277  s->last_frame.linesize[1]);
278  memset(s->last_frame.data[2], 0x80, s->last_frame.height / 2 *
279  s->last_frame.linesize[2]);
280  }
281 
283  buf_end - buf);
284  if (!s->bitstream_buf)
285  return AVERROR(ENOMEM);
286  s->dsp.bswap16_buf(s->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
287  init_get_bits(&s->gb, s->bitstream_buf, 8*(buf_end-buf));
288 
289  for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
290  for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
291  decode_mb(s, inter);
292 
293  *got_frame = 1;
294  *(AVFrame*)data = s->frame;
295 
296  if (chunk_type != MADe_TAG)
297  FFSWAP(AVFrame, s->frame, s->last_frame);
298 
299  return buf_size;
300 }
301 
303 {
304  MadContext *t = avctx->priv_data;
305  if (t->frame.data[0])
306  avctx->release_buffer(avctx, &t->frame);
307  if (t->last_frame.data[0])
308  avctx->release_buffer(avctx, &t->last_frame);
310  return 0;
311 }
312 
314  .name = "eamad",
315  .type = AVMEDIA_TYPE_VIDEO,
316  .id = AV_CODEC_ID_MAD,
317  .priv_data_size = sizeof(MadContext),
318  .init = decode_init,
319  .close = decode_end,
320  .decode = decode_frame,
321  .capabilities = CODEC_CAP_DR1,
322  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
323 };
static void decode_mb(MadContext *s, int inter)
Definition: eamad.c:183
DSPContext dsp
Definition: eamad.c:48
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
static void decode_block_intra(MadContext *s, DCTELEM *block)
Definition: eamad.c:113
const uint8_t ff_zigzag_direct[64]
Definition: dsputil.c:59
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVCodec ff_eamad_decoder
Definition: eamad.c:313
DCTELEM block[64]
Definition: eamad.c:54
uint16_t quant_matrix[64]
Definition: eamad.c:56
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
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:667
Scantable.
Definition: dsputil.h:181
int num
numerator
Definition: rational.h:44
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
#define AV_RL16
Definition: intreadwrite.h:42
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:85
uint8_t permutated[64]
Definition: dsputil.h:183
uint8_t run
Definition: svq3.c:124
AVCodec.
Definition: avcodec.h:2960
RLTable.
Definition: rl.h:38
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
struct MadContext MadContext
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
static void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
Definition: eamad.c:99
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
bitstream reader API header.
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:425
static void calc_quant_matrix(MadContext *s, int qscale)
Definition: eamad.c:212
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
AVCodecContext * avctx
Definition: eamad.c:47
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:139
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: dsputil.h:344
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:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static av_cold int decode_end(AVCodecContext *avctx)
Definition: eamad.c:302
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
void(* clear_block)(DCTELEM *block)
Definition: dsputil.h:218
#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:220
static DCTELEM block[64]
Definition: dct-test.c:169
ScanTable scantable
Definition: eamad.c:55
int width
picture width / height.
Definition: avcodec.h:1508
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:481
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: eamad.c:221
#define FF_NO_IDCT_PERM
Definition: dsputil.h:427
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:180
#define AV_RL32
Definition: intreadwrite.h:146
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:539
GetBitContext gb
Definition: eamad.c:51
AVFrame frame
Definition: eamad.c:49
#define MADe_TAG
Definition: eamad.c:44
static int width
Definition: utils.c:156
AVFrame last_frame
Definition: eamad.c:50
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:82
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:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
MPEG1/2 tables.
#define OPEN_READER(name, gb)
Definition: get_bits.h:124
static int decode_motion(GetBitContext *gb)
Definition: eamad.c:172
int mb_y
Definition: eamad.c:58
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
int index
Definition: gxfenc.c:72
#define TEX_VLC_BITS
Definition: dvdata.h:100
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
void ff_init_scantable_permutation(uint8_t *idct_permutation, int idct_permutation_type)
Definition: dsputil.c:143
short DCTELEM
Definition: dsputil.h:39
#define MADm_TAG
Definition: eamad.c:43
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:125
int height
Definition: gxfenc.c:72
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:187
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
void ff_ea_idct_put_c(uint8_t *dest, int linesize, DCTELEM *block)
Definition: eaidct.c:81
void * bitstream_buf
Definition: eamad.c:52
int den
denominator
Definition: rational.h:45
DSP utils.
void * priv_data
Definition: avcodec.h:1382
float re
Definition: fft-test.c:64
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:122
int mb_x
Definition: eamad.c:57
int height
Definition: avcodec.h:1035
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:73
unsigned int bitstream_buf_size
Definition: eamad.c:53
This structure stores compressed data.
Definition: avcodec.h:898
static av_cold int decode_init(AVCodecContext *avctx)
Definition: eamad.c:61
DSPContext.
Definition: dsputil.h:194
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)