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