00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "avcodec.h"
00032 #include "get_bits.h"
00033 #include "dsputil.h"
00034 #include "aandcttab.h"
00035 #include "eaidct.h"
00036 #include "mpeg12.h"
00037 #include "mpeg12data.h"
00038 #include "libavutil/imgutils.h"
00039
00040 #define EA_PREAMBLE_SIZE 8
00041 #define MADk_TAG MKTAG('M', 'A', 'D', 'k')
00042 #define MADm_TAG MKTAG('M', 'A', 'D', 'm')
00043 #define MADe_TAG MKTAG('M', 'A', 'D', 'e')
00044
00045 typedef struct MadContext {
00046 AVCodecContext *avctx;
00047 DSPContext dsp;
00048 AVFrame frame;
00049 AVFrame last_frame;
00050 GetBitContext gb;
00051 void *bitstream_buf;
00052 unsigned int bitstream_buf_size;
00053 DECLARE_ALIGNED(16, DCTELEM, block)[64];
00054 ScanTable scantable;
00055 uint16_t quant_matrix[64];
00056 int mb_x;
00057 int mb_y;
00058 } MadContext;
00059
00060 static av_cold int decode_init(AVCodecContext *avctx)
00061 {
00062 MadContext *s = avctx->priv_data;
00063 s->avctx = avctx;
00064 avctx->pix_fmt = PIX_FMT_YUV420P;
00065 ff_dsputil_init(&s->dsp, avctx);
00066 ff_init_scantable_permutation(s->dsp.idct_permutation, FF_NO_IDCT_PERM);
00067 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
00068 ff_mpeg12_init_vlcs();
00069 return 0;
00070 }
00071
00072 static inline void comp(unsigned char *dst, int dst_stride,
00073 unsigned char *src, int src_stride, int add)
00074 {
00075 int j, i;
00076 for (j=0; j<8; j++)
00077 for (i=0; i<8; i++)
00078 dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
00079 }
00080
00081 static inline void comp_block(MadContext *t, int mb_x, int mb_y,
00082 int j, int mv_x, int mv_y, int add)
00083 {
00084 if (j < 4) {
00085 unsigned offset = (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x;
00086 if (offset >= (t->avctx->height - 7) * t->last_frame.linesize[0] - 7)
00087 return;
00088 comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
00089 t->frame.linesize[0],
00090 t->last_frame.data[0] + offset,
00091 t->last_frame.linesize[0], add);
00092 } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
00093 int index = j - 3;
00094 unsigned offset = (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2);
00095 if (offset >= (t->avctx->height/2 - 7) * t->last_frame.linesize[index] - 7)
00096 return;
00097 comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
00098 t->frame.linesize[index],
00099 t->last_frame.data[index] + offset,
00100 t->last_frame.linesize[index], add);
00101 }
00102 }
00103
00104 static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
00105 {
00106 if (j < 4) {
00107 ff_ea_idct_put_c(
00108 t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
00109 t->frame.linesize[0], block);
00110 } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
00111 int index = j - 3;
00112 ff_ea_idct_put_c(
00113 t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
00114 t->frame.linesize[index], block);
00115 }
00116 }
00117
00118 static inline int decode_block_intra(MadContext *s, DCTELEM * block)
00119 {
00120 int level, i, j, run;
00121 RLTable *rl = &ff_rl_mpeg1;
00122 const uint8_t *scantable = s->scantable.permutated;
00123 int16_t *quant_matrix = s->quant_matrix;
00124
00125 block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
00126
00127
00128
00129 i = 0;
00130 {
00131 OPEN_READER(re, &s->gb);
00132
00133 for (;;) {
00134 UPDATE_CACHE(re, &s->gb);
00135 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00136
00137 if (level == 127) {
00138 break;
00139 } else if (level != 0) {
00140 i += run;
00141 j = scantable[i];
00142 level = (level*quant_matrix[j]) >> 4;
00143 level = (level-1)|1;
00144 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00145 LAST_SKIP_BITS(re, &s->gb, 1);
00146 } else {
00147
00148 UPDATE_CACHE(re, &s->gb);
00149 level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
00150
00151 UPDATE_CACHE(re, &s->gb);
00152 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00153
00154 i += run;
00155 j = scantable[i];
00156 if (level < 0) {
00157 level = -level;
00158 level = (level*quant_matrix[j]) >> 4;
00159 level = (level-1)|1;
00160 level = -level;
00161 } else {
00162 level = (level*quant_matrix[j]) >> 4;
00163 level = (level-1)|1;
00164 }
00165 }
00166 if (i > 63) {
00167 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00168 return -1;
00169 }
00170
00171 block[j] = level;
00172 }
00173 CLOSE_READER(re, &s->gb);
00174 }
00175 return 0;
00176 }
00177
00178 static int decode_motion(GetBitContext *gb)
00179 {
00180 int value = 0;
00181 if (get_bits1(gb)) {
00182 if (get_bits1(gb))
00183 value = -17;
00184 value += get_bits(gb, 4) + 1;
00185 }
00186 return value;
00187 }
00188
00189 static int decode_mb(MadContext *s, int inter)
00190 {
00191 int mv_map = 0;
00192 int mv_x, mv_y;
00193 int j;
00194
00195 if (inter) {
00196 int v = decode210(&s->gb);
00197 if (v < 2) {
00198 mv_map = v ? get_bits(&s->gb, 6) : 63;
00199 mv_x = decode_motion(&s->gb);
00200 mv_y = decode_motion(&s->gb);
00201 }
00202 }
00203
00204 for (j=0; j<6; j++) {
00205 if (mv_map & (1<<j)) {
00206 int add = 2*decode_motion(&s->gb);
00207 if (s->last_frame.data[0])
00208 comp_block(s, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
00209 } else {
00210 s->dsp.clear_block(s->block);
00211 if(decode_block_intra(s, s->block) < 0)
00212 return -1;
00213 idct_put(s, s->block, s->mb_x, s->mb_y, j);
00214 }
00215 }
00216 return 0;
00217 }
00218
00219 static void calc_quant_matrix(MadContext *s, int qscale)
00220 {
00221 int i;
00222
00223 s->quant_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
00224 for (i=1; i<64; i++)
00225 s->quant_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
00226 }
00227
00228 static int decode_frame(AVCodecContext *avctx,
00229 void *data, int *data_size,
00230 AVPacket *avpkt)
00231 {
00232 const uint8_t *buf = avpkt->data;
00233 int buf_size = avpkt->size;
00234 const uint8_t *buf_end = buf+buf_size;
00235 MadContext *s = avctx->priv_data;
00236 int width, height;
00237 int chunk_type;
00238 int inter;
00239
00240 if (buf_size < 17) {
00241 av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
00242 *data_size = 0;
00243 return -1;
00244 }
00245
00246 chunk_type = AV_RL32(&buf[0]);
00247 inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
00248 buf += 8;
00249
00250 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00251 AV_RL16(&buf[6]), 1000, 1<<30);
00252
00253 width = AV_RL16(&buf[8]);
00254 height = AV_RL16(&buf[10]);
00255 calc_quant_matrix(s, buf[13]);
00256 buf += 16;
00257
00258 if (avctx->width != width || avctx->height != height) {
00259 if((width * height)/2048*7 > buf_end-buf)
00260 return -1;
00261 if (av_image_check_size(width, height, 0, avctx) < 0)
00262 return -1;
00263 avcodec_set_dimensions(avctx, width, height);
00264 if (s->frame.data[0])
00265 avctx->release_buffer(avctx, &s->frame);
00266 if (s->last_frame.data[0])
00267 avctx->release_buffer(avctx, &s->last_frame);
00268 }
00269
00270 s->frame.reference = 3;
00271 if (!s->frame.data[0]) {
00272 if (avctx->get_buffer(avctx, &s->frame) < 0) {
00273 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00274 return -1;
00275 }
00276 }
00277
00278 av_fast_malloc(&s->bitstream_buf, &s->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
00279 if (!s->bitstream_buf)
00280 return AVERROR(ENOMEM);
00281 s->dsp.bswap16_buf(s->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
00282 memset((uint8_t*)s->bitstream_buf + (buf_end-buf), 0, FF_INPUT_BUFFER_PADDING_SIZE);
00283 init_get_bits(&s->gb, s->bitstream_buf, 8*(buf_end-buf));
00284
00285 for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
00286 for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
00287 if(decode_mb(s, inter) < 0)
00288 return -1;
00289
00290 *data_size = sizeof(AVFrame);
00291 *(AVFrame*)data = s->frame;
00292
00293 if (chunk_type != MADe_TAG)
00294 FFSWAP(AVFrame, s->frame, s->last_frame);
00295
00296 return buf_size;
00297 }
00298
00299 static av_cold int decode_end(AVCodecContext *avctx)
00300 {
00301 MadContext *t = avctx->priv_data;
00302 if (t->frame.data[0])
00303 avctx->release_buffer(avctx, &t->frame);
00304 if (t->last_frame.data[0])
00305 avctx->release_buffer(avctx, &t->last_frame);
00306 av_free(t->bitstream_buf);
00307 return 0;
00308 }
00309
00310 AVCodec ff_eamad_decoder = {
00311 .name = "eamad",
00312 .type = AVMEDIA_TYPE_VIDEO,
00313 .id = AV_CODEC_ID_MAD,
00314 .priv_data_size = sizeof(MadContext),
00315 .init = decode_init,
00316 .close = decode_end,
00317 .decode = decode_frame,
00318 .capabilities = CODEC_CAP_DR1,
00319 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
00320 };