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 "bytestream.h"
00033 #include "get_bits.h"
00034 #include "dsputil.h"
00035 #include "aandcttab.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 MpegEncContext s;
00047 AVFrame frame;
00048 AVFrame last_frame;
00049 void *bitstream_buf;
00050 unsigned int bitstream_buf_size;
00051 DECLARE_ALIGNED(16, DCTELEM, block)[64];
00052 } MadContext;
00053
00054 static void bswap16_buf(uint16_t *dst, const uint16_t *src, int count)
00055 {
00056 int i;
00057 for (i=0; i<count; i++)
00058 dst[i] = av_bswap16(src[i]);
00059 }
00060
00061 static av_cold int decode_init(AVCodecContext *avctx)
00062 {
00063 MadContext *t = avctx->priv_data;
00064 MpegEncContext *s = &t->s;
00065 s->avctx = avctx;
00066 avctx->pix_fmt = PIX_FMT_YUV420P;
00067 if (avctx->idct_algo == FF_IDCT_AUTO)
00068 avctx->idct_algo = FF_IDCT_EA;
00069 dsputil_init(&s->dsp, avctx);
00070 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
00071 ff_mpeg12_init_vlcs();
00072 return 0;
00073 }
00074
00075 static inline void comp(unsigned char *dst, int dst_stride,
00076 unsigned char *src, int src_stride, int add)
00077 {
00078 int j, i;
00079 for (j=0; j<8; j++)
00080 for (i=0; i<8; i++)
00081 dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
00082 }
00083
00084 static inline void comp_block(MadContext *t, int mb_x, int mb_y,
00085 int j, int mv_x, int mv_y, int add)
00086 {
00087 MpegEncContext *s = &t->s;
00088 if (j < 4) {
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] + (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x,
00092 t->last_frame.linesize[0], add);
00093 } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
00094 int index = j - 3;
00095 comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
00096 t->frame.linesize[index],
00097 t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
00098 t->last_frame.linesize[index], add);
00099 }
00100 }
00101
00102 static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
00103 {
00104 MpegEncContext *s = &t->s;
00105 if (j < 4) {
00106 s->dsp.idct_put(
00107 t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
00108 t->frame.linesize[0], block);
00109 } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
00110 int index = j - 3;
00111 s->dsp.idct_put(
00112 t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
00113 t->frame.linesize[index], block);
00114 }
00115 }
00116
00117 static inline void decode_block_intra(MadContext * t, DCTELEM * block)
00118 {
00119 MpegEncContext *s = &t->s;
00120 int level, i, j, run;
00121 RLTable *rl = &ff_rl_mpeg1;
00122 const uint8_t *scantable = s->intra_scantable.permutated;
00123 int16_t *quant_matrix = s->intra_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 if (i > 63) {
00142 av_log(s->avctx, AV_LOG_ERROR,
00143 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00144 return;
00145 }
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 if (i > 63) {
00161 av_log(s->avctx, AV_LOG_ERROR,
00162 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00163 return;
00164 }
00165 j = scantable[i];
00166 if (level < 0) {
00167 level = -level;
00168 level = (level*quant_matrix[j]) >> 4;
00169 level = (level-1)|1;
00170 level = -level;
00171 } else {
00172 level = (level*quant_matrix[j]) >> 4;
00173 level = (level-1)|1;
00174 }
00175 }
00176
00177 block[j] = level;
00178 }
00179 CLOSE_READER(re, &s->gb);
00180 }
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 void 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 } else {
00208 mv_map = 0;
00209 }
00210 }
00211
00212 for (j=0; j<6; j++) {
00213 if (mv_map & (1<<j)) {
00214 int add = 2*decode_motion(&s->gb);
00215 comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
00216 } else {
00217 s->dsp.clear_block(t->block);
00218 decode_block_intra(t, t->block);
00219 idct_put(t, t->block, s->mb_x, s->mb_y, j);
00220 }
00221 }
00222 }
00223
00224 static void calc_intra_matrix(MadContext *t, int qscale)
00225 {
00226 MpegEncContext *s = &t->s;
00227 int i;
00228
00229 if (s->avctx->idct_algo == FF_IDCT_EA) {
00230 s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
00231 for (i=1; i<64; i++)
00232 s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
00233 } else {
00234 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
00235 for (i=1; i<64; i++)
00236 s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
00237 }
00238 }
00239
00240 static int decode_frame(AVCodecContext *avctx,
00241 void *data, int *data_size,
00242 AVPacket *avpkt)
00243 {
00244 const uint8_t *buf = avpkt->data;
00245 int buf_size = avpkt->size;
00246 MadContext *t = avctx->priv_data;
00247 GetByteContext gb;
00248 MpegEncContext *s = &t->s;
00249 int chunk_type;
00250 int inter;
00251
00252 bytestream2_init(&gb, buf, buf_size);
00253
00254 chunk_type = bytestream2_get_le32(&gb);
00255 inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
00256 bytestream2_skip(&gb, 10);
00257
00258 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00259 bytestream2_get_le16(&gb), 1000, 1<<30);
00260
00261 s->width = bytestream2_get_le16(&gb);
00262 s->height = bytestream2_get_le16(&gb);
00263 bytestream2_skip(&gb, 1);
00264 calc_intra_matrix(t, bytestream2_get_byte(&gb));
00265 bytestream2_skip(&gb, 2);
00266
00267 if (bytestream2_get_bytes_left(&gb) < 2) {
00268 av_log(avctx, AV_LOG_ERROR, "Input data too small\n");
00269 return AVERROR_INVALIDDATA;
00270 }
00271
00272 if (avctx->width != s->width || avctx->height != s->height) {
00273 if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
00274 return -1;
00275 avcodec_set_dimensions(avctx, s->width, s->height);
00276 if (t->frame.data[0])
00277 avctx->release_buffer(avctx, &t->frame);
00278 }
00279
00280 t->frame.reference = 1;
00281 if (!t->frame.data[0]) {
00282 if (avctx->get_buffer(avctx, &t->frame) < 0) {
00283 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00284 return -1;
00285 }
00286 }
00287
00288 av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size,
00289 bytestream2_get_bytes_left(&gb) + FF_INPUT_BUFFER_PADDING_SIZE);
00290 if (!t->bitstream_buf)
00291 return AVERROR(ENOMEM);
00292 bswap16_buf(t->bitstream_buf, (const uint16_t *)(buf + bytestream2_tell(&gb)),
00293 bytestream2_get_bytes_left(&gb) / 2);
00294 init_get_bits(&s->gb, t->bitstream_buf, 8*(bytestream2_get_bytes_left(&gb)));
00295 for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
00296 for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
00297 decode_mb(t, inter);
00298
00299 *data_size = sizeof(AVFrame);
00300 *(AVFrame*)data = t->frame;
00301
00302 if (chunk_type != MADe_TAG)
00303 FFSWAP(AVFrame, t->frame, t->last_frame);
00304
00305 return buf_size;
00306 }
00307
00308 static av_cold int decode_end(AVCodecContext *avctx)
00309 {
00310 MadContext *t = avctx->priv_data;
00311 if (t->frame.data[0])
00312 avctx->release_buffer(avctx, &t->frame);
00313 if (t->last_frame.data[0])
00314 avctx->release_buffer(avctx, &t->last_frame);
00315 av_free(t->bitstream_buf);
00316 return 0;
00317 }
00318
00319 AVCodec ff_eamad_decoder = {
00320 .name = "eamad",
00321 .type = AVMEDIA_TYPE_VIDEO,
00322 .id = CODEC_ID_MAD,
00323 .priv_data_size = sizeof(MadContext),
00324 .init = decode_init,
00325 .close = decode_end,
00326 .decode = decode_frame,
00327 .capabilities = CODEC_CAP_DR1,
00328 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
00329 };