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