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 "internal.h"
00035 #include "mpeg12.h"
00036 #include "mpegvideo.h"
00037
00038 typedef struct TqiContext {
00039 MpegEncContext s;
00040 AVFrame frame;
00041 void *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 ff_dsputil_init(&s->dsp, avctx);
00052 ff_init_scantable_permutation(s->dsp.idct_permutation, FF_NO_IDCT_PERM);
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 = AV_PIX_FMT_YUV420P;
00057 ff_mpeg12_init_vlcs();
00058 return 0;
00059 }
00060
00061 static int 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 if (ff_mpeg1_decode_block_intra(s, block[n], n) < 0)
00067 return -1;
00068
00069 return 0;
00070 }
00071
00072 static inline void tqi_idct_put(TqiContext *t, DCTELEM (*block)[64])
00073 {
00074 MpegEncContext *s = &t->s;
00075 int linesize= t->frame.linesize[0];
00076 uint8_t *dest_y = t->frame.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16;
00077 uint8_t *dest_cb = t->frame.data[1] + (s->mb_y * 8 * t->frame.linesize[1]) + s->mb_x * 8;
00078 uint8_t *dest_cr = t->frame.data[2] + (s->mb_y * 8 * t->frame.linesize[2]) + s->mb_x * 8;
00079
00080 ff_ea_idct_put_c(dest_y , linesize, block[0]);
00081 ff_ea_idct_put_c(dest_y + 8, linesize, block[1]);
00082 ff_ea_idct_put_c(dest_y + 8*linesize , linesize, block[2]);
00083 ff_ea_idct_put_c(dest_y + 8*linesize + 8, linesize, block[3]);
00084 if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
00085 ff_ea_idct_put_c(dest_cb, t->frame.linesize[1], block[4]);
00086 ff_ea_idct_put_c(dest_cr, t->frame.linesize[2], block[5]);
00087 }
00088 }
00089
00090 static void tqi_calculate_qtable(MpegEncContext *s, int quant)
00091 {
00092 const int qscale = (215 - 2*quant)*5;
00093 int i;
00094 s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0])>>11;
00095 for(i=1; i<64; i++)
00096 s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32)>>14;
00097 }
00098
00099 static int tqi_decode_frame(AVCodecContext *avctx,
00100 void *data, int *got_frame,
00101 AVPacket *avpkt)
00102 {
00103 const uint8_t *buf = avpkt->data;
00104 int buf_size = avpkt->size;
00105 const uint8_t *buf_end = buf+buf_size;
00106 TqiContext *t = avctx->priv_data;
00107 MpegEncContext *s = &t->s;
00108
00109 s->width = AV_RL16(&buf[0]);
00110 s->height = AV_RL16(&buf[2]);
00111 tqi_calculate_qtable(s, buf[4]);
00112 buf += 8;
00113
00114 if (t->frame.data[0])
00115 avctx->release_buffer(avctx, &t->frame);
00116
00117 if (s->avctx->width!=s->width || s->avctx->height!=s->height)
00118 avcodec_set_dimensions(s->avctx, s->width, s->height);
00119
00120 if(ff_get_buffer(avctx, &t->frame) < 0) {
00121 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00122 return -1;
00123 }
00124
00125 av_fast_padded_malloc(&t->bitstream_buf, &t->bitstream_buf_size,
00126 buf_end - buf);
00127 if (!t->bitstream_buf)
00128 return AVERROR(ENOMEM);
00129 s->dsp.bswap_buf(t->bitstream_buf, (const uint32_t*)buf, (buf_end-buf)/4);
00130 init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
00131
00132 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 0;
00133 for (s->mb_y=0; s->mb_y<(avctx->height+15)/16; s->mb_y++)
00134 for (s->mb_x=0; s->mb_x<(avctx->width+15)/16; s->mb_x++)
00135 {
00136 if (tqi_decode_mb(s, t->block) < 0)
00137 goto end;
00138 tqi_idct_put(t, t->block);
00139 }
00140 end:
00141
00142 *got_frame = 1;
00143 *(AVFrame*)data = t->frame;
00144 return buf_size;
00145 }
00146
00147 static av_cold int tqi_decode_end(AVCodecContext *avctx)
00148 {
00149 TqiContext *t = avctx->priv_data;
00150 if(t->frame.data[0])
00151 avctx->release_buffer(avctx, &t->frame);
00152 av_free(t->bitstream_buf);
00153 return 0;
00154 }
00155
00156 AVCodec ff_eatqi_decoder = {
00157 .name = "eatqi",
00158 .type = AVMEDIA_TYPE_VIDEO,
00159 .id = AV_CODEC_ID_TQI,
00160 .priv_data_size = sizeof(TqiContext),
00161 .init = tqi_decode_init,
00162 .close = tqi_decode_end,
00163 .decode = tqi_decode_frame,
00164 .capabilities = CODEC_CAP_DR1,
00165 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TQI Video"),
00166 };