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