00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "dsputil.h"
00032 #include "aandcttab.h"
00033 #include "eaidct.h"
00034 #include "mpeg12.h"
00035 #include "mpegvideo.h"
00036
00037 typedef struct TqiContext {
00038 MpegEncContext s;
00039 AVFrame frame;
00040 void *bitstream_buf;
00041 unsigned int bitstream_buf_size;
00042 DECLARE_ALIGNED(16, DCTELEM, block)[6][64];
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 ff_dsputil_init(&s->dsp, avctx);
00051 ff_init_scantable_permutation(s->dsp.idct_permutation, FF_NO_IDCT_PERM);
00052 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
00053 s->qscale = 1;
00054 avctx->time_base = (AVRational){1, 15};
00055 avctx->pix_fmt = PIX_FMT_YUV420P;
00056 ff_mpeg12_init_vlcs();
00057 return 0;
00058 }
00059
00060 static int tqi_decode_mb(MpegEncContext *s, DCTELEM (*block)[64])
00061 {
00062 int n;
00063 s->dsp.clear_blocks(block[0]);
00064 for (n=0; n<6; n++)
00065 if (ff_mpeg1_decode_block_intra(s, block[n], n) < 0)
00066 return -1;
00067
00068 return 0;
00069 }
00070
00071 static inline void tqi_idct_put(TqiContext *t, DCTELEM (*block)[64])
00072 {
00073 MpegEncContext *s = &t->s;
00074 int linesize= t->frame.linesize[0];
00075 uint8_t *dest_y = t->frame.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16;
00076 uint8_t *dest_cb = t->frame.data[1] + (s->mb_y * 8 * t->frame.linesize[1]) + s->mb_x * 8;
00077 uint8_t *dest_cr = t->frame.data[2] + (s->mb_y * 8 * t->frame.linesize[2]) + s->mb_x * 8;
00078
00079 ff_ea_idct_put_c(dest_y , linesize, block[0]);
00080 ff_ea_idct_put_c(dest_y + 8, linesize, block[1]);
00081 ff_ea_idct_put_c(dest_y + 8*linesize , linesize, block[2]);
00082 ff_ea_idct_put_c(dest_y + 8*linesize + 8, linesize, block[3]);
00083 if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
00084 ff_ea_idct_put_c(dest_cb, t->frame.linesize[1], block[4]);
00085 ff_ea_idct_put_c(dest_cr, t->frame.linesize[2], block[5]);
00086 }
00087 }
00088
00089 static void tqi_calculate_qtable(MpegEncContext *s, int quant)
00090 {
00091 const int qscale = (215 - 2*quant)*5;
00092 int i;
00093 s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0])>>11;
00094 for(i=1; i<64; i++)
00095 s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32)>>14;
00096 }
00097
00098 static int tqi_decode_frame(AVCodecContext *avctx,
00099 void *data, int *data_size,
00100 AVPacket *avpkt)
00101 {
00102 const uint8_t *buf = avpkt->data;
00103 int buf_size = avpkt->size;
00104 const uint8_t *buf_end = buf+buf_size;
00105 TqiContext *t = avctx->priv_data;
00106 MpegEncContext *s = &t->s;
00107
00108 s->width = AV_RL16(&buf[0]);
00109 s->height = AV_RL16(&buf[2]);
00110 tqi_calculate_qtable(s, buf[4]);
00111 buf += 8;
00112
00113 if (t->frame.data[0])
00114 avctx->release_buffer(avctx, &t->frame);
00115
00116 if (s->avctx->width!=s->width || s->avctx->height!=s->height)
00117 avcodec_set_dimensions(s->avctx, s->width, s->height);
00118
00119 if(avctx->get_buffer(avctx, &t->frame) < 0) {
00120 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00121 return -1;
00122 }
00123
00124 av_fast_padded_malloc(&t->bitstream_buf, &t->bitstream_buf_size,
00125 buf_end - buf);
00126 if (!t->bitstream_buf)
00127 return AVERROR(ENOMEM);
00128 s->dsp.bswap_buf(t->bitstream_buf, (const uint32_t*)buf, (buf_end-buf)/4);
00129 init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
00130
00131 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 0;
00132 for (s->mb_y=0; s->mb_y<(avctx->height+15)/16; s->mb_y++)
00133 for (s->mb_x=0; s->mb_x<(avctx->width+15)/16; s->mb_x++)
00134 {
00135 if (tqi_decode_mb(s, t->block) < 0)
00136 goto end;
00137 tqi_idct_put(t, t->block);
00138 }
00139 end:
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 ff_eatqi_decoder = {
00156 .name = "eatqi",
00157 .type = AVMEDIA_TYPE_VIDEO,
00158 .id = AV_CODEC_ID_TQI,
00159 .priv_data_size = sizeof(TqiContext),
00160 .init = tqi_decode_init,
00161 .close = tqi_decode_end,
00162 .decode = tqi_decode_frame,
00163 .capabilities = CODEC_CAP_DR1,
00164 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TQI Video"),
00165 };