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