• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/eamad.c

Go to the documentation of this file.
00001 /*
00002  * Electronic Arts Madcow Video Decoder
00003  * Copyright (c) 2007-2009 Peter Ross
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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')    /* MAD i-frame */
00041 #define MADm_TAG MKTAG('M', 'A', 'D', 'm')    /* MAD p-frame */
00042 #define MADe_TAG MKTAG('M', 'A', 'D', 'e')    /* MAD lqp-frame */
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         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     /* The RL decoder is derived from mpeg1_decode_block_intra;
00133        Escaped level and run values a decoded differently */
00134     i = 0;
00135     {
00136         OPEN_READER(re, &s->gb);
00137         /* now quantify & encode AC coefficients */
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                 /* escape */
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         } else {
00208             mv_map = 0;
00209         }
00210     }
00211 
00212     for (j=0; j<6; j++) {
00213         if (mv_map & (1<<j)) {  // mv_x and mv_y are guarded by mv_map
00214             int add = 2*decode_motion(&s->gb);
00215             if (t->last_frame.data[0])
00216                 comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
00217         } else {
00218             s->dsp.clear_block(t->block);
00219             if(decode_block_intra(t, t->block) < 0)
00220                 return -1;
00221             idct_put(t, t->block, s->mb_x, s->mb_y, j);
00222         }
00223     }
00224     return 0;
00225 }
00226 
00227 static void calc_intra_matrix(MadContext *t, int qscale)
00228 {
00229     MpegEncContext *s = &t->s;
00230     int i;
00231 
00232     if (s->avctx->idct_algo == FF_IDCT_EA) {
00233         s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
00234         for (i=1; i<64; i++)
00235             s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
00236     } else {
00237         s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
00238         for (i=1; i<64; i++)
00239             s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
00240     }
00241 }
00242 
00243 static int decode_frame(AVCodecContext *avctx,
00244                         void *data, int *data_size,
00245                         AVPacket *avpkt)
00246 {
00247     const uint8_t *buf = avpkt->data;
00248     int buf_size       = avpkt->size;
00249     const uint8_t *buf_end = buf+buf_size;
00250     MadContext *t     = avctx->priv_data;
00251     MpegEncContext *s = &t->s;
00252     int chunk_type;
00253     int inter;
00254 
00255     if (buf_size < 17) {
00256         av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
00257         *data_size = 0;
00258         return -1;
00259     }
00260 
00261     chunk_type = AV_RL32(&buf[0]);
00262     inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
00263     buf += 8;
00264 
00265     av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00266               AV_RL16(&buf[6]), 1000, 1<<30);
00267 
00268     s->width  = AV_RL16(&buf[8]);
00269     s->height = AV_RL16(&buf[10]);
00270     calc_intra_matrix(t, buf[13]);
00271     buf += 16;
00272 
00273     if (avctx->width != s->width || avctx->height != s->height) {
00274         if((s->width * s->height)/2048*7 > buf_end-buf)
00275             return -1;
00276         if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
00277             return -1;
00278         avcodec_set_dimensions(avctx, s->width, s->height);
00279         if (t->frame.data[0])
00280             avctx->release_buffer(avctx, &t->frame);
00281         if (t->last_frame.data[0])
00282             avctx->release_buffer(avctx, &t->last_frame);
00283     }
00284 
00285     t->frame.reference = 3;
00286     if (!t->frame.data[0]) {
00287         if (avctx->get_buffer(avctx, &t->frame) < 0) {
00288             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00289             return -1;
00290         }
00291     }
00292 
00293     av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
00294     if (!t->bitstream_buf)
00295         return AVERROR(ENOMEM);
00296     bswap16_buf(t->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
00297     memset((uint8_t*)t->bitstream_buf + (buf_end-buf), 0, FF_INPUT_BUFFER_PADDING_SIZE);
00298     init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
00299 
00300     for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
00301         for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
00302             if(decode_mb(t, inter) < 0)
00303                 return -1;
00304 
00305     *data_size = sizeof(AVFrame);
00306     *(AVFrame*)data = t->frame;
00307 
00308     if (chunk_type != MADe_TAG)
00309         FFSWAP(AVFrame, t->frame, t->last_frame);
00310 
00311     return buf_size;
00312 }
00313 
00314 static av_cold int decode_end(AVCodecContext *avctx)
00315 {
00316     MadContext *t = avctx->priv_data;
00317     if (t->frame.data[0])
00318         avctx->release_buffer(avctx, &t->frame);
00319     if (t->last_frame.data[0])
00320         avctx->release_buffer(avctx, &t->last_frame);
00321     av_free(t->bitstream_buf);
00322     return 0;
00323 }
00324 
00325 AVCodec ff_eamad_decoder = {
00326     .name           = "eamad",
00327     .type           = AVMEDIA_TYPE_VIDEO,
00328     .id             = CODEC_ID_MAD,
00329     .priv_data_size = sizeof(MadContext),
00330     .init           = decode_init,
00331     .close          = decode_end,
00332     .decode         = decode_frame,
00333     .capabilities   = CODEC_CAP_DR1,
00334     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
00335 };
Generated on Fri Feb 1 2013 14:34:33 for FFmpeg by doxygen 1.7.1