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 "eaidct.h"
37 #include "internal.h"
38 #include "mpeg12.h"
39 #include "mpeg12data.h"
40 #include "libavutil/imgutils.h"
41 
42 #define EA_PREAMBLE_SIZE 8
43 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
44 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
45 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
46 
47 typedef struct MadContext {
54  unsigned int bitstream_buf_size;
57  uint16_t quant_matrix[64];
58  int mb_x;
59  int mb_y;
60 } MadContext;
61 
63 {
64  MadContext *s = avctx->priv_data;
65  s->avctx = avctx;
66  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
67  ff_dsputil_init(&s->dsp, avctx);
71  return 0;
72 }
73 
74 static inline void comp(unsigned char *dst, int dst_stride,
75  unsigned char *src, int src_stride, int add)
76 {
77  int j, i;
78  for (j=0; j<8; j++)
79  for (i=0; i<8; i++)
80  dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
81 }
82 
83 static inline void comp_block(MadContext *t, int mb_x, int mb_y,
84  int j, int mv_x, int mv_y, int add)
85 {
86  if (j < 4) {
87  comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
88  t->frame.linesize[0],
89  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,
90  t->last_frame.linesize[0], add);
91  } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
92  int index = j - 3;
93  comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
94  t->frame.linesize[index],
95  t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
96  t->last_frame.linesize[index], add);
97  }
98 }
99 
100 static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
101 {
102  if (j < 4) {
104  t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
105  t->frame.linesize[0], block);
106  } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
107  int index = j - 3;
109  t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
110  t->frame.linesize[index], block);
111  }
112 }
113 
114 static inline void decode_block_intra(MadContext *s, DCTELEM * block)
115 {
116  int level, i, j, run;
117  RLTable *rl = &ff_rl_mpeg1;
118  const uint8_t *scantable = s->scantable.permutated;
119  int16_t *quant_matrix = s->quant_matrix;
120 
121  block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
122 
123  /* The RL decoder is derived from mpeg1_decode_block_intra;
124  Escaped level and run values a decoded differently */
125  i = 0;
126  {
127  OPEN_READER(re, &s->gb);
128  /* now quantify & encode AC coefficients */
129  for (;;) {
130  UPDATE_CACHE(re, &s->gb);
131  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
132 
133  if (level == 127) {
134  break;
135  } else if (level != 0) {
136  i += run;
137  if (i > 63) {
139  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
140  return;
141  }
142  j = scantable[i];
143  level = (level*quant_matrix[j]) >> 4;
144  level = (level-1)|1;
145  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
146  LAST_SKIP_BITS(re, &s->gb, 1);
147  } else {
148  /* escape */
149  UPDATE_CACHE(re, &s->gb);
150  level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
151 
152  UPDATE_CACHE(re, &s->gb);
153  run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
154 
155  i += run;
156  if (i > 63) {
158  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
159  return;
160  }
161  j = scantable[i];
162  if (level < 0) {
163  level = -level;
164  level = (level*quant_matrix[j]) >> 4;
165  level = (level-1)|1;
166  level = -level;
167  } else {
168  level = (level*quant_matrix[j]) >> 4;
169  level = (level-1)|1;
170  }
171  }
172 
173  block[j] = level;
174  }
175  CLOSE_READER(re, &s->gb);
176  }
177 }
178 
180 {
181  int value = 0;
182  if (get_bits1(gb)) {
183  if (get_bits1(gb))
184  value = -17;
185  value += get_bits(gb, 4) + 1;
186  }
187  return value;
188 }
189 
190 static void decode_mb(MadContext *s, int inter)
191 {
192  int mv_map = 0;
193  int mv_x, mv_y;
194  int j;
195 
196  if (inter) {
197  int v = decode210(&s->gb);
198  if (v < 2) {
199  mv_map = v ? get_bits(&s->gb, 6) : 63;
200  mv_x = decode_motion(&s->gb);
201  mv_y = decode_motion(&s->gb);
202  } else {
203  mv_map = 0;
204  }
205  }
206 
207  for (j=0; j<6; j++) {
208  if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
209  int add = 2*decode_motion(&s->gb);
210  comp_block(s, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
211  } else {
212  s->dsp.clear_block(s->block);
213  decode_block_intra(s, s->block);
214  idct_put(s, s->block, s->mb_x, s->mb_y, j);
215  }
216  }
217 }
218 
219 static void calc_quant_matrix(MadContext *s, int qscale)
220 {
221  int i;
222 
224  for (i=1; i<64; i++)
225  s->quant_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
226 }
227 
228 static int decode_frame(AVCodecContext *avctx,
229  void *data, int *got_frame,
230  AVPacket *avpkt)
231 {
232  const uint8_t *buf = avpkt->data;
233  int buf_size = avpkt->size;
234  MadContext *s = avctx->priv_data;
235  GetByteContext gb;
236  int width, height;
237  int chunk_type;
238  int inter;
239 
240  bytestream2_init(&gb, buf, buf_size);
241 
242  chunk_type = bytestream2_get_le32(&gb);
243  inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
244  bytestream2_skip(&gb, 10);
245 
246  av_reduce(&avctx->time_base.num, &avctx->time_base.den,
247  bytestream2_get_le16(&gb), 1000, 1<<30);
248 
249  width = bytestream2_get_le16(&gb);
250  height = bytestream2_get_le16(&gb);
251  bytestream2_skip(&gb, 1);
252  calc_quant_matrix(s, bytestream2_get_byte(&gb));
253  bytestream2_skip(&gb, 2);
254 
255  if (bytestream2_get_bytes_left(&gb) < 2) {
256  av_log(avctx, AV_LOG_ERROR, "Input data too small\n");
257  return AVERROR_INVALIDDATA;
258  }
259 
260  if (avctx->width != width || avctx->height != height) {
261  if (av_image_check_size(width, height, 0, avctx) < 0)
262  return -1;
263  avcodec_set_dimensions(avctx, width, height);
264  if (s->frame.data[0])
265  avctx->release_buffer(avctx, &s->frame);
266  }
267 
268  s->frame.reference = 1;
269  if (!s->frame.data[0]) {
270  if (ff_get_buffer(avctx, &s->frame) < 0) {
271  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
272  return -1;
273  }
274  }
275 
276  if (inter && !s->last_frame.data[0]) {
277  int ret;
278  av_log(avctx, AV_LOG_WARNING, "Missing reference frame.\n");
279  s->last_frame.reference = 1;
280  ret = ff_get_buffer(avctx, &s->last_frame);
281  if (ret < 0)
282  return ret;
283  memset(s->last_frame.data[0], 0, s->last_frame.height *
284  s->last_frame.linesize[0]);
285  memset(s->last_frame.data[1], 0x80, s->last_frame.height / 2 *
286  s->last_frame.linesize[1]);
287  memset(s->last_frame.data[2], 0x80, s->last_frame.height / 2 *
288  s->last_frame.linesize[2]);
289  }
290 
293  if (!s->bitstream_buf)
294  return AVERROR(ENOMEM);
295  s->dsp.bswap16_buf(s->bitstream_buf, (const uint16_t *)(buf + bytestream2_tell(&gb)),
296  bytestream2_get_bytes_left(&gb) / 2);
298  for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
299  for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
300  decode_mb(s, inter);
301 
302  *got_frame = 1;
303  *(AVFrame*)data = s->frame;
304 
305  if (chunk_type != MADe_TAG)
306  FFSWAP(AVFrame, s->frame, s->last_frame);
307 
308  return buf_size;
309 }
310 
312 {
313  MadContext *t = avctx->priv_data;
314  if (t->frame.data[0])
315  avctx->release_buffer(avctx, &t->frame);
316  if (t->last_frame.data[0])
317  avctx->release_buffer(avctx, &t->last_frame);
319  return 0;
320 }
321 
323  .name = "eamad",
324  .type = AVMEDIA_TYPE_VIDEO,
325  .id = AV_CODEC_ID_MAD,
326  .priv_data_size = sizeof(MadContext),
327  .init = decode_init,
328  .close = decode_end,
329  .decode = decode_frame,
330  .capabilities = CODEC_CAP_DR1,
331  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
332 };
static void decode_mb(MadContext *s, int inter)
Definition: eamad.c:190
DSPContext dsp
Definition: eamad.c:49
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:114
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:322
DCTELEM block[64]
Definition: eamad.c:55
uint16_t quant_matrix[64]
Definition: eamad.c:57
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:677
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
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
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:132
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:100
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:219
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
AVCodecContext * avctx
Definition: eamad.c:48
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
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
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:311
#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:56
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:228
#define FF_NO_IDCT_PERM
Definition: dsputil.h:427
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:180
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
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
GetBitContext gb
Definition: eamad.c:52
AVFrame frame
Definition: eamad.c:50
#define MADe_TAG
Definition: eamad.c:45
static int width
Definition: utils.c:156
AVFrame last_frame
Definition: eamad.c:51
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:83
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:179
int mb_y
Definition: eamad.c:59
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:44
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:133
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:53
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:58
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:74
unsigned int bitstream_buf_size
Definition: eamad.c:54
This structure stores compressed data.
Definition: avcodec.h:898
static av_cold int decode_init(AVCodecContext *avctx)
Definition: eamad.c:62
DSPContext.
Definition: dsputil.h:194
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)