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