Browse code

Electronic Arts TGQ video decoder

Originally committed as revision 15789 to svn://svn.ffmpeg.org/ffmpeg/trunk

Peter Ross authored on 2008/11/08 09:29:22
Showing 6 changed files
... ...
@@ -139,6 +139,7 @@ version <next>
139 139
 - ASS and SSA demuxer and muxer
140 140
 - liba52 wrapper removed
141 141
 - Speex decoding via libspeex
142
+- Electronic Arts TGQ decoder
142 143
 
143 144
 version 0.4.9-pre1:
144 145
 
... ...
@@ -230,6 +230,7 @@ following image formats are supported:
230 230
 @item Electronic Arts CMV    @tab     @tab  X
231 231
     @tab Used in NHL 95 game.
232 232
 @item Electronic Arts TGV    @tab     @tab  X
233
+@item Electronic Arts TGQ    @tab     @tab  X
233 234
 @item FFmpeg Video 1         @tab  X  @tab  X
234 235
     @tab experimental lossless codec (fourcc: FFV1)
235 236
 @item Flash Screen Video     @tab  X  @tab  X
... ...
@@ -68,6 +68,7 @@ OBJS-$(CONFIG_DVVIDEO_ENCODER)         += dv.o
68 68
 OBJS-$(CONFIG_DXA_DECODER)             += dxa.o
69 69
 OBJS-$(CONFIG_EAC3_DECODER)            += eac3dec.o ac3dec.o ac3tab.o ac3dec_data.o ac3.o
70 70
 OBJS-$(CONFIG_EACMV_DECODER)           += eacmv.o
71
+OBJS-$(CONFIG_EATGQ_DECODER)           += eatgq.o
71 72
 OBJS-$(CONFIG_EATGV_DECODER)           += eatgv.o
72 73
 OBJS-$(CONFIG_EIGHTBPS_DECODER)        += 8bps.o
73 74
 OBJS-$(CONFIG_EIGHTSVX_EXP_DECODER)    += 8svx.o
... ...
@@ -79,6 +79,7 @@ void avcodec_register_all(void)
79 79
     REGISTER_ENCDEC  (DVVIDEO, dvvideo);
80 80
     REGISTER_DECODER (DXA, dxa);
81 81
     REGISTER_DECODER (EACMV, eacmv);
82
+    REGISTER_DECODER (EATGQ, eatgq);
82 83
     REGISTER_DECODER (EATGV, eatgv);
83 84
     REGISTER_DECODER (EIGHTBPS, eightbps);
84 85
     REGISTER_DECODER (EIGHTSVX_EXP, eightsvx_exp);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR  2
33
+#define LIBAVCODEC_VERSION_MINOR  3
34 34
 #define LIBAVCODEC_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -189,6 +189,7 @@ enum CodecID {
189 189
     CODEC_ID_CMV,
190 190
     CODEC_ID_MOTIONPIXELS,
191 191
     CODEC_ID_TGV,
192
+    CODEC_ID_TGQ,
192 193
 
193 194
     /* various PCM "codecs" */
194 195
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -1388,6 +1389,7 @@ typedef struct AVCodecContext {
1388 1388
 #define FF_IDCT_SIMPLEVIS     18
1389 1389
 #define FF_IDCT_WMV2          19
1390 1390
 #define FF_IDCT_FAAN          20
1391
+#define FF_IDCT_EA            21
1391 1392
 
1392 1393
     /**
1393 1394
      * slice count
1394 1395
new file mode 100644
... ...
@@ -0,0 +1,255 @@
0
+/*
1
+ * Electronic Arts TGQ Video Decoder
2
+ * Copyright (c) 2007-2008 Peter Ross <pross@xvid.org>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
+ */
20
+
21
+/**
22
+ * @file eatgq.c
23
+ * Electronic Arts TGQ Video Decoder
24
+ * @author Peter Ross <pross@xvid.org>
25
+ *
26
+ * Technical details here:
27
+ * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGQ
28
+ */
29
+
30
+#include "avcodec.h"
31
+#define ALT_BITSTREAM_READER_LE
32
+#include "bitstream.h"
33
+#include "bytestream.h"
34
+#include "dsputil.h"
35
+extern const uint16_t ff_inv_aanscales[64]; //mpegvideo_enc.c
36
+
37
+typedef struct TgqContext {
38
+    AVCodecContext *avctx;
39
+    DSPContext dsp;
40
+    AVFrame frame;
41
+    int width,height;
42
+    ScanTable scantable;
43
+    int qtable[64];
44
+} TgqContext;
45
+
46
+static av_cold int tgq_decode_init(AVCodecContext *avctx){
47
+    TgqContext *s = avctx->priv_data;
48
+    s->avctx = avctx;
49
+    if(avctx->idct_algo==FF_IDCT_AUTO)
50
+        avctx->idct_algo=FF_IDCT_EA;
51
+    dsputil_init(&s->dsp, avctx);
52
+    ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
53
+    avctx->time_base = (AVRational){1, 15};
54
+    avctx->pix_fmt = PIX_FMT_YUV420P;
55
+    return 0;
56
+}
57
+
58
+static void tgq_decode_block(TgqContext *s, DCTELEM block[64], GetBitContext *gb){
59
+    uint8_t *perm = s->scantable.permutated;
60
+    int i,j,value;
61
+    block[0] = get_sbits(gb,8) * s->qtable[0];
62
+    for(i=1; i<64; ) {
63
+        switch(show_bits(gb,3)) {
64
+        case 4:
65
+            block[perm[i++]] = 0;
66
+        case 0:
67
+            block[perm[i++]] = 0;
68
+            skip_bits(gb,3);
69
+            break;
70
+        case 5:
71
+        case 1:
72
+            skip_bits(gb,2);
73
+            value = get_bits(gb,6);
74
+            for(j=0; j<value; j++)
75
+                block[perm[i++]] = 0;
76
+            break;
77
+        case 6:
78
+            skip_bits(gb,3);
79
+            block[perm[i]] = -s->qtable[perm[i]];
80
+            i++;
81
+            break;
82
+        case 2:
83
+            skip_bits(gb,3);
84
+            block[perm[i]] = s->qtable[perm[i]];
85
+            i++;
86
+            break;
87
+        case 7: // 111b
88
+        case 3: // 011b
89
+            skip_bits(gb,2);
90
+            if (show_bits(gb,6)==0x3F) {
91
+                skip_bits(gb, 6);
92
+                block[perm[i]] = get_sbits(gb,8)*s->qtable[perm[i]];
93
+            }else{
94
+                block[perm[i]] = get_sbits(gb,6)*s->qtable[perm[i]];
95
+            }
96
+            i++;
97
+            break;
98
+        }
99
+    }
100
+    block[0] += 128<<4;
101
+}
102
+
103
+static void tgq_idct_put_mb(TgqContext *s, DCTELEM (*block)[64], int mb_x, int mb_y){
104
+    int linesize= s->frame.linesize[0];
105
+    uint8_t *dest_y  = s->frame.data[0] + (mb_y * 16* linesize            ) + mb_x * 16;
106
+    uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
107
+    uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->frame.linesize[2]) + mb_x * 8;
108
+
109
+    s->dsp.idct_put(dest_y                 , linesize, block[0]);
110
+    s->dsp.idct_put(dest_y              + 8, linesize, block[1]);
111
+    s->dsp.idct_put(dest_y + 8*linesize    , linesize, block[2]);
112
+    s->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
113
+    if(!(s->avctx->flags&CODEC_FLAG_GRAY)){
114
+         s->dsp.idct_put(dest_cb, s->frame.linesize[1], block[4]);
115
+         s->dsp.idct_put(dest_cr, s->frame.linesize[2], block[5]);
116
+    }
117
+}
118
+
119
+static inline void tgq_dconly(TgqContext *s, unsigned char *dst, int dst_stride, int dc){
120
+    int level = av_clip_uint8((dc*s->qtable[0] + 2056)>>4);
121
+    int j;
122
+    for(j=0;j<8;j++)
123
+        memset(dst+j*dst_stride, level, 8);
124
+}
125
+
126
+static void tgq_idct_put_mb_dconly(TgqContext *s, int mb_x, int mb_y, const int8_t *dc)
127
+{
128
+    int linesize= s->frame.linesize[0];
129
+    uint8_t *dest_y  = s->frame.data[0] + (mb_y * 16* linesize            ) + mb_x * 16;
130
+    uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
131
+    uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->frame.linesize[2]) + mb_x * 8;
132
+    tgq_dconly(s,dest_y                 , linesize, dc[0]);
133
+    tgq_dconly(s,dest_y              + 8, linesize, dc[1]);
134
+    tgq_dconly(s,dest_y + 8*linesize    , linesize, dc[2]);
135
+    tgq_dconly(s,dest_y + 8*linesize + 8, linesize, dc[3]);
136
+    if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
137
+        tgq_dconly(s,dest_cb, s->frame.linesize[1], dc[4]);
138
+        tgq_dconly(s,dest_cr, s->frame.linesize[2], dc[5]);
139
+    }
140
+}
141
+
142
+static void tgq_decode_mb(TgqContext *s, int mb_y, int mb_x, const int8_t **bs, const int8_t *buf_end){
143
+    int mode;
144
+    int i;
145
+    int8_t dc[6];
146
+    DCTELEM block[6][64];
147
+
148
+    mode = bytestream_get_byte((const uint8_t**)bs);
149
+    if (mode>buf_end-*bs) {
150
+        av_log(s->avctx, AV_LOG_ERROR, "truncated macroblock\n");
151
+        return;
152
+    }
153
+
154
+    if (mode>12) {
155
+        GetBitContext gb;
156
+        init_get_bits(&gb, *bs, mode*8);
157
+        for(i=0; i<6; i++)
158
+            tgq_decode_block(s, block[i], &gb);
159
+        tgq_idct_put_mb(s, block, mb_x, mb_y);
160
+    }else{
161
+        if (mode==3) {
162
+            memset(dc, (*bs)[0], 4);
163
+            dc[4] = (*bs)[1];
164
+            dc[5] = (*bs)[2];
165
+        }else if (mode==6) {
166
+            memcpy(dc, *bs, 6);
167
+        }else if (mode==12) {
168
+            for(i=0; i<6; i++)
169
+                dc[i] = (*bs)[i*2];
170
+        }else{
171
+            av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
172
+        }
173
+        tgq_idct_put_mb_dconly(s, mb_x, mb_y, dc);
174
+    }
175
+    *bs += mode;
176
+}
177
+
178
+static void tgq_calculate_qtable(TgqContext *s, int quant){
179
+    int i,j;
180
+    const int a = (14*(100-quant))/100 + 1;
181
+    const int b = (11*(100-quant))/100 + 4;
182
+    for(j=0;j<8;j++)
183
+    for(i=0;i<8;i++)
184
+        if (s->avctx->idct_algo==FF_IDCT_EA)
185
+            s->qtable[j*8+i] = ((a*(j+i)/(7+7) + b)*ff_inv_aanscales[j*8+i])>>(14-4);
186
+        else
187
+            s->qtable[j*8+i] = (a*(j+i)/(7+7) + b)<<3;
188
+}
189
+
190
+static int tgq_decode_frame(AVCodecContext *avctx,
191
+                            void *data, int *data_size,
192
+                            const uint8_t *buf, int buf_size){
193
+    const uint8_t *buf_start = buf;
194
+    const uint8_t *buf_end = buf + buf_size;
195
+    TgqContext *s = avctx->priv_data;
196
+    int x,y;
197
+
198
+    int big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
199
+    buf += 8;
200
+
201
+    if(8>buf_end-buf) {
202
+        av_log(avctx, AV_LOG_WARNING, "truncated header\n");
203
+        return -1;
204
+    }
205
+    s->width  = big_endian ? AV_RB16(&buf[0]) : AV_RL16(&buf[0]);
206
+    s->height = big_endian ? AV_RB16(&buf[2]) : AV_RL16(&buf[2]);
207
+
208
+    if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
209
+        avcodec_set_dimensions(s->avctx, s->width, s->height);
210
+        if (s->frame.data[0])
211
+            avctx->release_buffer(avctx, &s->frame);
212
+    }
213
+    tgq_calculate_qtable(s, buf[4]);
214
+    buf += 8;
215
+
216
+    if (!s->frame.data[0]) {
217
+        s->frame.key_frame = 1;
218
+        s->frame.pict_type = FF_I_TYPE;
219
+        s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
220
+        if (avctx->get_buffer(avctx, &s->frame)) {
221
+            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
222
+            return -1;
223
+        }
224
+    }
225
+
226
+    for (y=0; y<(avctx->height+15)/16; y++)
227
+    for (x=0; x<(avctx->width+15)/16; x++)
228
+        tgq_decode_mb(s, y, x, (const int8_t**)&buf, (const int8_t*)buf_end);
229
+
230
+    *data_size = sizeof(AVFrame);
231
+    *(AVFrame*)data = s->frame;
232
+
233
+    return buf-buf_start;
234
+}
235
+
236
+static av_cold int tgq_decode_end(AVCodecContext *avctx){
237
+    TgqContext *s = avctx->priv_data;
238
+    if (s->frame.data[0])
239
+        s->avctx->release_buffer(avctx, &s->frame);
240
+    return 0;
241
+}
242
+
243
+AVCodec eatgq_decoder = {
244
+    "eatgq",
245
+    CODEC_TYPE_VIDEO,
246
+    CODEC_ID_TGQ,
247
+    sizeof(TgqContext),
248
+    tgq_decode_init,
249
+    NULL,
250
+    tgq_decode_end,
251
+    tgq_decode_frame,
252
+    CODEC_CAP_DR1,
253
+    .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ Video"),
254
+};