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 };