• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 void 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;
00174             }
00175 
00176             block[j] = level;
00177         }
00178         CLOSE_READER(re, &s->gb);
00179     }
00180 }
00181 
00182 static int decode_motion(GetBitContext *gb)
00183 {
00184     int value = 0;
00185     if (get_bits1(gb)) {
00186         if (get_bits1(gb))
00187             value = -17;
00188         value += get_bits(gb, 4) + 1;
00189     }
00190     return value;
00191 }
00192 
00193 static void decode_mb(MadContext *t, int inter)
00194 {
00195     MpegEncContext *s = &t->s;
00196     int mv_map = 0;
00197     int mv_x, mv_y;
00198     int j;
00199 
00200     if (inter) {
00201         int v = decode210(&s->gb);
00202         if (v < 2) {
00203             mv_map = v ? get_bits(&s->gb, 6) : 63;
00204             mv_x = decode_motion(&s->gb);
00205             mv_y = decode_motion(&s->gb);
00206         } else {
00207             mv_map = 0;
00208         }
00209     }
00210 
00211     for (j=0; j<6; j++) {
00212         if (mv_map & (1<<j)) {  // mv_x and mv_y are guarded by mv_map
00213             int add = 2*decode_motion(&s->gb);
00214             if (t->last_frame.data[0])
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     const uint8_t *buf_end = buf+buf_size;
00247     MadContext *t     = avctx->priv_data;
00248     MpegEncContext *s = &t->s;
00249     int chunk_type;
00250     int inter;
00251 
00252     if (buf_size < 17) {
00253         av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
00254         *data_size = 0;
00255         return -1;
00256     }
00257 
00258     chunk_type = AV_RL32(&buf[0]);
00259     inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
00260     buf += 8;
00261 
00262     av_reduce(&avctx->time_base.num, &avctx->time_base.den,
00263               AV_RL16(&buf[6]), 1000, 1<<30);
00264 
00265     s->width  = AV_RL16(&buf[8]);
00266     s->height = AV_RL16(&buf[10]);
00267     calc_intra_matrix(t, buf[13]);
00268     buf += 16;
00269 
00270     if (avctx->width != s->width || avctx->height != s->height) {
00271         if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
00272             return -1;
00273         avcodec_set_dimensions(avctx, s->width, s->height);
00274         if (t->frame.data[0])
00275             avctx->release_buffer(avctx, &t->frame);
00276         if (t->last_frame.data[0])
00277             avctx->release_buffer(avctx, &t->last_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, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
00289     if (!t->bitstream_buf)
00290         return AVERROR(ENOMEM);
00291     bswap16_buf(t->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
00292     memset((uint8_t*)t->bitstream_buf + (buf_end-buf), 0, FF_INPUT_BUFFER_PADDING_SIZE);
00293     init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
00294 
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     "eamad",
00321     AVMEDIA_TYPE_VIDEO,
00322     CODEC_ID_MAD,
00323     sizeof(MadContext),
00324     decode_init,
00325     NULL,
00326     decode_end,
00327     decode_frame,
00328     CODEC_CAP_DR1,
00329     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
00330 };

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1