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