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 #define ALT_BITSTREAM_READER_LE
00033 #include "bitstream.h"
00034 #include "bytestream.h"
00035 #include "dsputil.h"
00036 #include "aandcttab.h"
00037
00038 typedef struct TgqContext {
00039 AVCodecContext *avctx;
00040 DSPContext dsp;
00041 AVFrame frame;
00042 int width,height;
00043 ScanTable scantable;
00044 int qtable[64];
00045 } TgqContext;
00046
00047 static av_cold int tgq_decode_init(AVCodecContext *avctx){
00048 TgqContext *s = avctx->priv_data;
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->scantable, ff_zigzag_direct);
00054 avctx->time_base = (AVRational){1, 15};
00055 avctx->pix_fmt = PIX_FMT_YUV420P;
00056 return 0;
00057 }
00058
00059 static void tgq_decode_block(TgqContext *s, DCTELEM block[64], GetBitContext *gb){
00060 uint8_t *perm = s->scantable.permutated;
00061 int i,j,value;
00062 block[0] = get_sbits(gb,8) * s->qtable[0];
00063 for(i=1; i<64; ) {
00064 switch(show_bits(gb,3)) {
00065 case 4:
00066 block[perm[i++]] = 0;
00067 case 0:
00068 block[perm[i++]] = 0;
00069 skip_bits(gb,3);
00070 break;
00071 case 5:
00072 case 1:
00073 skip_bits(gb,2);
00074 value = get_bits(gb,6);
00075 for(j=0; j<value; j++)
00076 block[perm[i++]] = 0;
00077 break;
00078 case 6:
00079 skip_bits(gb,3);
00080 block[perm[i]] = -s->qtable[perm[i]];
00081 i++;
00082 break;
00083 case 2:
00084 skip_bits(gb,3);
00085 block[perm[i]] = s->qtable[perm[i]];
00086 i++;
00087 break;
00088 case 7:
00089 case 3:
00090 skip_bits(gb,2);
00091 if (show_bits(gb,6)==0x3F) {
00092 skip_bits(gb, 6);
00093 block[perm[i]] = get_sbits(gb,8)*s->qtable[perm[i]];
00094 }else{
00095 block[perm[i]] = get_sbits(gb,6)*s->qtable[perm[i]];
00096 }
00097 i++;
00098 break;
00099 }
00100 }
00101 block[0] += 128<<4;
00102 }
00103
00104 static void tgq_idct_put_mb(TgqContext *s, DCTELEM (*block)[64], int mb_x, int mb_y){
00105 int linesize= s->frame.linesize[0];
00106 uint8_t *dest_y = s->frame.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00107 uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
00108 uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->frame.linesize[2]) + mb_x * 8;
00109
00110 s->dsp.idct_put(dest_y , linesize, block[0]);
00111 s->dsp.idct_put(dest_y + 8, linesize, block[1]);
00112 s->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
00113 s->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00114 if(!(s->avctx->flags&CODEC_FLAG_GRAY)){
00115 s->dsp.idct_put(dest_cb, s->frame.linesize[1], block[4]);
00116 s->dsp.idct_put(dest_cr, s->frame.linesize[2], block[5]);
00117 }
00118 }
00119
00120 static inline void tgq_dconly(TgqContext *s, unsigned char *dst, int dst_stride, int dc){
00121 int level = av_clip_uint8((dc*s->qtable[0] + 2056)>>4);
00122 int j;
00123 for(j=0;j<8;j++)
00124 memset(dst+j*dst_stride, level, 8);
00125 }
00126
00127 static void tgq_idct_put_mb_dconly(TgqContext *s, int mb_x, int mb_y, const int8_t *dc)
00128 {
00129 int linesize= s->frame.linesize[0];
00130 uint8_t *dest_y = s->frame.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00131 uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
00132 uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->frame.linesize[2]) + mb_x * 8;
00133 tgq_dconly(s,dest_y , linesize, dc[0]);
00134 tgq_dconly(s,dest_y + 8, linesize, dc[1]);
00135 tgq_dconly(s,dest_y + 8*linesize , linesize, dc[2]);
00136 tgq_dconly(s,dest_y + 8*linesize + 8, linesize, dc[3]);
00137 if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
00138 tgq_dconly(s,dest_cb, s->frame.linesize[1], dc[4]);
00139 tgq_dconly(s,dest_cr, s->frame.linesize[2], dc[5]);
00140 }
00141 }
00142
00143 static void tgq_decode_mb(TgqContext *s, int mb_y, int mb_x, const int8_t **bs, const int8_t *buf_end){
00144 int mode;
00145 int i;
00146 int8_t dc[6];
00147 DCTELEM block[6][64];
00148
00149 mode = bytestream_get_byte((const uint8_t**)bs);
00150 if (mode>buf_end-*bs) {
00151 av_log(s->avctx, AV_LOG_ERROR, "truncated macroblock\n");
00152 return;
00153 }
00154
00155 if (mode>12) {
00156 GetBitContext gb;
00157 init_get_bits(&gb, *bs, mode*8);
00158 for(i=0; i<6; i++)
00159 tgq_decode_block(s, block[i], &gb);
00160 tgq_idct_put_mb(s, block, mb_x, mb_y);
00161 }else{
00162 if (mode==3) {
00163 memset(dc, (*bs)[0], 4);
00164 dc[4] = (*bs)[1];
00165 dc[5] = (*bs)[2];
00166 }else if (mode==6) {
00167 memcpy(dc, *bs, 6);
00168 }else if (mode==12) {
00169 for(i=0; i<6; i++)
00170 dc[i] = (*bs)[i*2];
00171 }else{
00172 av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
00173 }
00174 tgq_idct_put_mb_dconly(s, mb_x, mb_y, dc);
00175 }
00176 *bs += mode;
00177 }
00178
00179 static void tgq_calculate_qtable(TgqContext *s, int quant){
00180 int i,j;
00181 const int a = (14*(100-quant))/100 + 1;
00182 const int b = (11*(100-quant))/100 + 4;
00183 for(j=0;j<8;j++)
00184 for(i=0;i<8;i++)
00185 if (s->avctx->idct_algo==FF_IDCT_EA)
00186 s->qtable[j*8+i] = ((a*(j+i)/(7+7) + b)*ff_inv_aanscales[j*8+i])>>(14-4);
00187 else
00188 s->qtable[j*8+i] = (a*(j+i)/(7+7) + b)<<3;
00189 }
00190
00191 static int tgq_decode_frame(AVCodecContext *avctx,
00192 void *data, int *data_size,
00193 const uint8_t *buf, int buf_size){
00194 const uint8_t *buf_start = buf;
00195 const uint8_t *buf_end = buf + buf_size;
00196 TgqContext *s = avctx->priv_data;
00197 int x,y;
00198
00199 int big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
00200 buf += 8;
00201
00202 if(8>buf_end-buf) {
00203 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
00204 return -1;
00205 }
00206 s->width = big_endian ? AV_RB16(&buf[0]) : AV_RL16(&buf[0]);
00207 s->height = big_endian ? AV_RB16(&buf[2]) : AV_RL16(&buf[2]);
00208
00209 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
00210 avcodec_set_dimensions(s->avctx, s->width, s->height);
00211 if (s->frame.data[0])
00212 avctx->release_buffer(avctx, &s->frame);
00213 }
00214 tgq_calculate_qtable(s, buf[4]);
00215 buf += 8;
00216
00217 if (!s->frame.data[0]) {
00218 s->frame.key_frame = 1;
00219 s->frame.pict_type = FF_I_TYPE;
00220 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00221 if (avctx->get_buffer(avctx, &s->frame)) {
00222 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00223 return -1;
00224 }
00225 }
00226
00227 for (y=0; y<(avctx->height+15)/16; y++)
00228 for (x=0; x<(avctx->width+15)/16; x++)
00229 tgq_decode_mb(s, y, x, (const int8_t**)&buf, (const int8_t*)buf_end);
00230
00231 *data_size = sizeof(AVFrame);
00232 *(AVFrame*)data = s->frame;
00233
00234 return buf-buf_start;
00235 }
00236
00237 static av_cold int tgq_decode_end(AVCodecContext *avctx){
00238 TgqContext *s = avctx->priv_data;
00239 if (s->frame.data[0])
00240 s->avctx->release_buffer(avctx, &s->frame);
00241 return 0;
00242 }
00243
00244 AVCodec eatgq_decoder = {
00245 "eatgq",
00246 CODEC_TYPE_VIDEO,
00247 CODEC_ID_TGQ,
00248 sizeof(TgqContext),
00249 tgq_decode_init,
00250 NULL,
00251 tgq_decode_end,
00252 tgq_decode_frame,
00253 CODEC_CAP_DR1,
00254 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ video"),
00255 };