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

libavcodec/eatqi.c

Go to the documentation of this file.
00001 /*
00002  * Electronic Arts TQI Video Decoder
00003  * Copyright (c) 2007-2009 Peter Ross <pross@xvid.org>
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 "bitstream.h"
00033 #include "dsputil.h"
00034 #include "aandcttab.h"
00035 #include "mpeg12.h"
00036 #include "mpegvideo.h"
00037 
00038 typedef struct TqiContext {
00039     MpegEncContext s;
00040     AVFrame frame;
00041     uint8_t *bitstream_buf;
00042     unsigned int bitstream_buf_size;
00043     DECLARE_ALIGNED_16(DCTELEM, block[6][64]);
00044 } TqiContext;
00045 
00046 static av_cold int tqi_decode_init(AVCodecContext *avctx)
00047 {
00048     TqiContext *t = avctx->priv_data;
00049     MpegEncContext *s = &t->s;
00050     s->avctx = avctx;
00051     if(avctx->idct_algo==FF_IDCT_AUTO)
00052         avctx->idct_algo=FF_IDCT_EA;
00053     dsputil_init(&s->dsp, avctx);
00054     ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
00055     s->qscale = 1;
00056     avctx->time_base = (AVRational){1, 15};
00057     avctx->pix_fmt = PIX_FMT_YUV420P;
00058     ff_mpeg12_init_vlcs();
00059     return 0;
00060 }
00061 
00062 static int tqi_decode_mb(MpegEncContext *s, DCTELEM (*block)[64])
00063 {
00064     int n;
00065     s->dsp.clear_blocks(block[0]);
00066     for (n=0; n<6; n++)
00067         if (ff_mpeg1_decode_block_intra(s, block[n], n) < 0)
00068             return -1;
00069 
00070     return 0;
00071 }
00072 
00073 static inline void tqi_idct_put(TqiContext *t, DCTELEM (*block)[64])
00074 {
00075     MpegEncContext *s = &t->s;
00076     int linesize= t->frame.linesize[0];
00077     uint8_t *dest_y  = t->frame.data[0] + (s->mb_y * 16* linesize            ) + s->mb_x * 16;
00078     uint8_t *dest_cb = t->frame.data[1] + (s->mb_y * 8 * t->frame.linesize[1]) + s->mb_x * 8;
00079     uint8_t *dest_cr = t->frame.data[2] + (s->mb_y * 8 * t->frame.linesize[2]) + s->mb_x * 8;
00080 
00081     s->dsp.idct_put(dest_y                 , linesize, block[0]);
00082     s->dsp.idct_put(dest_y              + 8, linesize, block[1]);
00083     s->dsp.idct_put(dest_y + 8*linesize    , linesize, block[2]);
00084     s->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00085     if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
00086         s->dsp.idct_put(dest_cb, t->frame.linesize[1], block[4]);
00087         s->dsp.idct_put(dest_cr, t->frame.linesize[2], block[5]);
00088     }
00089 }
00090 
00091 static void tqi_calculate_qtable(MpegEncContext *s, int quant)
00092 {
00093     const int qscale = (215 - 2*quant)*5;
00094     int i;
00095     if (s->avctx->idct_algo==FF_IDCT_EA) {
00096         s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0])>>11;
00097         for(i=1; i<64; i++)
00098             s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32)>>14;
00099     }else{
00100         s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
00101         for(i=1; i<64; i++)
00102             s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale + 32)>>3;
00103     }
00104 }
00105 
00106 static int tqi_decode_frame(AVCodecContext *avctx,
00107                             void *data, int *data_size,
00108                             const uint8_t *buf, int buf_size)
00109 {
00110     const uint8_t *buf_end = buf+buf_size;
00111     TqiContext *t = avctx->priv_data;
00112     MpegEncContext *s = &t->s;
00113 
00114     s->width  = AV_RL16(&buf[0]);
00115     s->height = AV_RL16(&buf[2]);
00116     tqi_calculate_qtable(s, buf[4]);
00117     buf += 8;
00118 
00119     if (t->frame.data[0])
00120         avctx->release_buffer(avctx, &t->frame);
00121 
00122     if (s->avctx->width!=s->width || s->avctx->height!=s->height)
00123         avcodec_set_dimensions(s->avctx, s->width, s->height);
00124 
00125     if(avctx->get_buffer(avctx, &t->frame) < 0) {
00126         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00127         return -1;
00128     }
00129 
00130     t->bitstream_buf = av_fast_realloc(t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
00131     if (!t->bitstream_buf)
00132         return -1;
00133     s->dsp.bswap_buf((uint32_t*)t->bitstream_buf, (const uint32_t*)buf, (buf_end-buf)/4);
00134     init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
00135 
00136     s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 0;
00137     for (s->mb_y=0; s->mb_y<(avctx->height+15)/16; s->mb_y++)
00138     for (s->mb_x=0; s->mb_x<(avctx->width+15)/16; s->mb_x++)
00139     {
00140         if (tqi_decode_mb(s, t->block) < 0)
00141             break;
00142         tqi_idct_put(t, t->block);
00143     }
00144 
00145     *data_size = sizeof(AVFrame);
00146     *(AVFrame*)data = t->frame;
00147     return buf_size;
00148 }
00149 
00150 static av_cold int tqi_decode_end(AVCodecContext *avctx)
00151 {
00152     TqiContext *t = avctx->priv_data;
00153     if(t->frame.data[0])
00154         avctx->release_buffer(avctx, &t->frame);
00155     av_free(t->bitstream_buf);
00156     return 0;
00157 }
00158 
00159 AVCodec eatqi_decoder = {
00160     "eatqi",
00161     CODEC_TYPE_VIDEO,
00162     CODEC_ID_TQI,
00163     sizeof(TqiContext),
00164     tqi_decode_init,
00165     NULL,
00166     tqi_decode_end,
00167     tqi_decode_frame,
00168     CODEC_CAP_DR1,
00169     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TQI Video"),
00170 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1