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