Originally committed as revision 14642 to svn://svn.ffmpeg.org/ffmpeg/trunk
Peter Ross authored on 2008/08/06 17:33:25... | ... |
@@ -229,6 +229,7 @@ following image formats are supported: |
229 | 229 |
@tab Codec originally used in Feeble Files game. |
230 | 230 |
@item Electronic Arts CMV @tab @tab X |
231 | 231 |
@tab Used in NHL 95 game. |
232 |
+@item Electronic Arts TGV @tab @tab X |
|
232 | 233 |
@item FFmpeg Video 1 @tab X @tab X |
233 | 234 |
@tab experimental lossless codec (fourcc: FFV1) |
234 | 235 |
@item Flash Screen Video @tab X @tab X |
... | ... |
@@ -62,6 +62,7 @@ OBJS-$(CONFIG_DVVIDEO_DECODER) += dv.o |
62 | 62 |
OBJS-$(CONFIG_DVVIDEO_ENCODER) += dv.o |
63 | 63 |
OBJS-$(CONFIG_DXA_DECODER) += dxa.o |
64 | 64 |
OBJS-$(CONFIG_EACMV_DECODER) += eacmv.o |
65 |
+OBJS-$(CONFIG_EATGV_DECODER) += eatgv.o |
|
65 | 66 |
OBJS-$(CONFIG_EIGHTBPS_DECODER) += 8bps.o |
66 | 67 |
OBJS-$(CONFIG_EIGHTSVX_EXP_DECODER) += 8svx.o |
67 | 68 |
OBJS-$(CONFIG_EIGHTSVX_FIB_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 (EATGV, eatgv); |
|
82 | 83 |
REGISTER_DECODER (EIGHTBPS, eightbps); |
83 | 84 |
REGISTER_DECODER (EIGHTSVX_EXP, eightsvx_exp); |
84 | 85 |
REGISTER_DECODER (EIGHTSVX_FIB, eightsvx_fib); |
... | ... |
@@ -30,7 +30,7 @@ |
30 | 30 |
#include "libavutil/avutil.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVCODEC_VERSION_MAJOR 51 |
33 |
-#define LIBAVCODEC_VERSION_MINOR 62 |
|
33 |
+#define LIBAVCODEC_VERSION_MINOR 63 |
|
34 | 34 |
#define LIBAVCODEC_VERSION_MICRO 0 |
35 | 35 |
|
36 | 36 |
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |
... | ... |
@@ -188,6 +188,7 @@ enum CodecID { |
188 | 188 |
CODEC_ID_BFI, |
189 | 189 |
CODEC_ID_CMV, |
190 | 190 |
CODEC_ID_MOTIONPIXELS, |
191 |
+ CODEC_ID_TGV, |
|
191 | 192 |
|
192 | 193 |
/* various PCM "codecs" */ |
193 | 194 |
CODEC_ID_PCM_S16LE= 0x10000, |
194 | 195 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,340 @@ |
0 |
+/* |
|
1 |
+ * Electronic Arts TGV Video Decoder |
|
2 |
+ * Copyright (c) 2007-2008 Peter Ross |
|
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 eatgv.c |
|
23 |
+ * Electronic Arts TGV Video Decoder |
|
24 |
+ * by Peter Ross (suxen_drol at hotmail dot com) |
|
25 |
+ * |
|
26 |
+ * Technical details here: |
|
27 |
+ * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+#include "avcodec.h" |
|
31 |
+#define ALT_BITSTREAM_READER_LE |
|
32 |
+#include "bitstream.h" |
|
33 |
+#include <libavutil/lzo.h> |
|
34 |
+ |
|
35 |
+#define EA_PREAMBLE_SIZE 8 |
|
36 |
+#define kVGT_TAG MKTAG('k', 'V', 'G', 'T') |
|
37 |
+ |
|
38 |
+typedef struct TgvContext { |
|
39 |
+ AVCodecContext *avctx; |
|
40 |
+ AVFrame frame; |
|
41 |
+ AVFrame last_frame; |
|
42 |
+ int width,height; |
|
43 |
+ unsigned int palette[AVPALETTE_COUNT]; |
|
44 |
+ |
|
45 |
+ int (*mv_codebook)[2]; |
|
46 |
+ unsigned char (*block_codebook)[16]; |
|
47 |
+ int num_mvs; ///< current length of mv_codebook |
|
48 |
+ int num_blocks_packed; ///< current length of block_codebook |
|
49 |
+} TgvContext; |
|
50 |
+ |
|
51 |
+static av_cold int tgv_decode_init(AVCodecContext *avctx){ |
|
52 |
+ TgvContext *s = avctx->priv_data; |
|
53 |
+ s->avctx = avctx; |
|
54 |
+ avctx->time_base = (AVRational){1, 15}; |
|
55 |
+ avctx->pix_fmt = PIX_FMT_PAL8; |
|
56 |
+ return 0; |
|
57 |
+} |
|
58 |
+ |
|
59 |
+/** |
|
60 |
+ * Unpack buffer |
|
61 |
+ * @return 0 on success, -1 on critical buffer underflow |
|
62 |
+ */ |
|
63 |
+static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) { |
|
64 |
+ unsigned char *dst_end = dst + width*height; |
|
65 |
+ int size,size1,size2,offset,run; |
|
66 |
+ unsigned char *dst_start = dst; |
|
67 |
+ |
|
68 |
+ if (src[0] & 0x01) |
|
69 |
+ src += 5; |
|
70 |
+ else |
|
71 |
+ src += 2; |
|
72 |
+ |
|
73 |
+ if (src+3>src_end) |
|
74 |
+ return -1; |
|
75 |
+ size = AV_RB24(src); |
|
76 |
+ src += 3; |
|
77 |
+ |
|
78 |
+ while(size>0 && src<src_end) { |
|
79 |
+ |
|
80 |
+ /* determine size1 and size2 */ |
|
81 |
+ size1 = (src[0] & 3); |
|
82 |
+ if ( src[0] & 0x80 ) { // 1 |
|
83 |
+ if (src[0] & 0x40 ) { // 11 |
|
84 |
+ if ( src[0] & 0x20 ) { // 111 |
|
85 |
+ if ( src[0] < 0xFC ) // !(111111) |
|
86 |
+ size1 = (((src[0] & 31) + 1) << 2); |
|
87 |
+ src++; |
|
88 |
+ size2 = 0; |
|
89 |
+ } else { // 110 |
|
90 |
+ offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1; |
|
91 |
+ size2 = ((src[0] & 0xC) << 6) + src[3] + 5; |
|
92 |
+ src += 4; |
|
93 |
+ } |
|
94 |
+ } else { // 10 |
|
95 |
+ size1 = ( ( src[1] & 0xC0) >> 6 ); |
|
96 |
+ offset = (AV_RB16(&src[1]) & 0x3FFF) + 1; |
|
97 |
+ size2 = (src[0] & 0x3F) + 4; |
|
98 |
+ src += 3; |
|
99 |
+ } |
|
100 |
+ } else { // 0 |
|
101 |
+ offset = ((src[0] & 0x60) << 3) + src[1] + 1; |
|
102 |
+ size2 = ((src[0] & 0x1C) >> 2) + 3; |
|
103 |
+ src += 2; |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ |
|
107 |
+ /* fetch strip from src */ |
|
108 |
+ if (size1>src_end-src) |
|
109 |
+ break; |
|
110 |
+ |
|
111 |
+ if (size1>0) { |
|
112 |
+ size -= size1; |
|
113 |
+ run = FFMIN(size1, dst_end-dst); |
|
114 |
+ memcpy(dst, src, run); |
|
115 |
+ dst += run; |
|
116 |
+ src += run; |
|
117 |
+ } |
|
118 |
+ |
|
119 |
+ if (size2>0) { |
|
120 |
+ if (dst-dst_start<offset) |
|
121 |
+ return 0; |
|
122 |
+ size -= size2; |
|
123 |
+ run = FFMIN(size2, dst_end-dst); |
|
124 |
+ av_memcpy_backptr(dst, offset, run); |
|
125 |
+ dst += run; |
|
126 |
+ } |
|
127 |
+ } |
|
128 |
+ |
|
129 |
+ return 0; |
|
130 |
+} |
|
131 |
+ |
|
132 |
+/** |
|
133 |
+ * Decode inter-frame |
|
134 |
+ * @return 0 on success, -1 on critical buffer underflow |
|
135 |
+ */ |
|
136 |
+static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){ |
|
137 |
+ unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0]; |
|
138 |
+ int num_mvs; |
|
139 |
+ int num_blocks_raw; |
|
140 |
+ int num_blocks_packed; |
|
141 |
+ int vector_bits; |
|
142 |
+ int i,j,x,y; |
|
143 |
+ GetBitContext gb; |
|
144 |
+ int mvbits; |
|
145 |
+ const unsigned char *blocks_raw; |
|
146 |
+ |
|
147 |
+ if(buf+12>buf_end) |
|
148 |
+ return -1; |
|
149 |
+ |
|
150 |
+ num_mvs = AV_RL16(&buf[0]); |
|
151 |
+ num_blocks_raw = AV_RL16(&buf[2]); |
|
152 |
+ num_blocks_packed = AV_RL16(&buf[4]); |
|
153 |
+ vector_bits = AV_RL16(&buf[6]); |
|
154 |
+ buf += 12; |
|
155 |
+ |
|
156 |
+ /* allocate codebook buffers as neccessary */ |
|
157 |
+ if (num_mvs > s->num_mvs) { |
|
158 |
+ s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int)); |
|
159 |
+ s->num_mvs = num_mvs; |
|
160 |
+ } |
|
161 |
+ |
|
162 |
+ if (num_blocks_packed > s->num_blocks_packed) { |
|
163 |
+ s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char)); |
|
164 |
+ s->num_blocks_packed = num_blocks_packed; |
|
165 |
+ } |
|
166 |
+ |
|
167 |
+ /* read motion vectors */ |
|
168 |
+ mvbits = (num_mvs*2*10+31) & ~31; |
|
169 |
+ |
|
170 |
+ if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end) |
|
171 |
+ return -1; |
|
172 |
+ |
|
173 |
+ init_get_bits(&gb, buf, mvbits); |
|
174 |
+ for (i=0; i<num_mvs; i++) { |
|
175 |
+ s->mv_codebook[i][0] = get_sbits(&gb, 10); |
|
176 |
+ s->mv_codebook[i][1] = get_sbits(&gb, 10); |
|
177 |
+ } |
|
178 |
+ buf += mvbits>>3; |
|
179 |
+ |
|
180 |
+ /* note ptr to uncompressed blocks */ |
|
181 |
+ blocks_raw = buf; |
|
182 |
+ buf += num_blocks_raw*16; |
|
183 |
+ |
|
184 |
+ /* read compressed blocks */ |
|
185 |
+ init_get_bits(&gb, buf, (buf_end-buf)<<3); |
|
186 |
+ for (i=0; i<num_blocks_packed; i++) { |
|
187 |
+ int tmp[4]; |
|
188 |
+ for(j=0; j<4; j++) |
|
189 |
+ tmp[j] = get_bits(&gb, 8); |
|
190 |
+ for(j=0; j<16; j++) |
|
191 |
+ s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)]; |
|
192 |
+ } |
|
193 |
+ |
|
194 |
+ /* read vectors and build frame */ |
|
195 |
+ for(y=0; y<s->avctx->height/4; y++) |
|
196 |
+ for(x=0; x<s->avctx->width/4; x++) { |
|
197 |
+ unsigned int vector = get_bits(&gb, vector_bits); |
|
198 |
+ const unsigned char *src; |
|
199 |
+ int src_stride; |
|
200 |
+ |
|
201 |
+ if (vector < num_mvs) { |
|
202 |
+ src = s->last_frame.data[0] + |
|
203 |
+ (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] + |
|
204 |
+ x*4 + s->mv_codebook[vector][0]; |
|
205 |
+ src_stride = s->last_frame.linesize[0]; |
|
206 |
+ if (src+3*src_stride+3>=frame0_end) |
|
207 |
+ continue; |
|
208 |
+ }else{ |
|
209 |
+ int offset = vector - num_mvs; |
|
210 |
+ if (offset<num_blocks_raw) |
|
211 |
+ src = blocks_raw + 16*offset; |
|
212 |
+ else if (offset-num_blocks_raw<num_blocks_packed) |
|
213 |
+ src = s->block_codebook[offset-num_blocks_raw]; |
|
214 |
+ else |
|
215 |
+ continue; |
|
216 |
+ src_stride = 4; |
|
217 |
+ } |
|
218 |
+ |
|
219 |
+ for(j=0; j<4; j++) |
|
220 |
+ for(i=0; i<4; i++) |
|
221 |
+ s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] = |
|
222 |
+ src[j*src_stride + i]; |
|
223 |
+ } |
|
224 |
+ |
|
225 |
+ return 0; |
|
226 |
+} |
|
227 |
+ |
|
228 |
+/** release AVFrame buffers if allocated */ |
|
229 |
+static void cond_release_buffer(AVFrame *pic) |
|
230 |
+{ |
|
231 |
+ if (pic->data[0]) { |
|
232 |
+ av_freep(&pic->data[0]); |
|
233 |
+ av_free(pic->data[1]); |
|
234 |
+ } |
|
235 |
+} |
|
236 |
+ |
|
237 |
+static int tgv_decode_frame(AVCodecContext *avctx, |
|
238 |
+ void *data, int *data_size, |
|
239 |
+ const uint8_t *buf, int buf_size) |
|
240 |
+{ |
|
241 |
+ TgvContext *s = avctx->priv_data; |
|
242 |
+ const uint8_t *buf_end = buf + buf_size; |
|
243 |
+ int chunk_type; |
|
244 |
+ |
|
245 |
+ chunk_type = AV_RL32(&buf[0]); |
|
246 |
+ buf += EA_PREAMBLE_SIZE; |
|
247 |
+ |
|
248 |
+ if (chunk_type==kVGT_TAG) { |
|
249 |
+ int pal_count, i; |
|
250 |
+ if(buf+12>buf_end) { |
|
251 |
+ av_log(avctx, AV_LOG_WARNING, "truncated header\n"); |
|
252 |
+ return -1; |
|
253 |
+ } |
|
254 |
+ |
|
255 |
+ s->width = AV_RL16(&buf[0]); |
|
256 |
+ s->height = AV_RL16(&buf[2]); |
|
257 |
+ if (s->avctx->width!=s->width || s->avctx->height!=s->height) { |
|
258 |
+ avcodec_set_dimensions(s->avctx, s->width, s->height); |
|
259 |
+ cond_release_buffer(&s->frame); |
|
260 |
+ cond_release_buffer(&s->last_frame); |
|
261 |
+ } |
|
262 |
+ |
|
263 |
+ pal_count = AV_RL16(&buf[6]); |
|
264 |
+ buf += 12; |
|
265 |
+ for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) { |
|
266 |
+ s->palette[i] = AV_RB24(buf); |
|
267 |
+ buf += 3; |
|
268 |
+ } |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ if (avcodec_check_dimensions(avctx, s->width, s->height)) |
|
272 |
+ return -1; |
|
273 |
+ |
|
274 |
+ /* shuffle */ |
|
275 |
+ FFSWAP(AVFrame, s->frame, s->last_frame); |
|
276 |
+ if (!s->frame.data[0]) { |
|
277 |
+ s->frame.reference = 1; |
|
278 |
+ s->frame.buffer_hints = FF_BUFFER_HINTS_VALID; |
|
279 |
+ s->frame.linesize[0] = s->width; |
|
280 |
+ |
|
281 |
+ /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */ |
|
282 |
+ s->frame.data[0] = av_malloc(s->width*s->height + 12); |
|
283 |
+ if (!s->frame.data[0]) |
|
284 |
+ return AVERROR_NOMEM; |
|
285 |
+ s->frame.data[1] = av_malloc(AVPALETTE_SIZE); |
|
286 |
+ if (!s->frame.data[1]) { |
|
287 |
+ av_freep(&s->frame.data[0]); |
|
288 |
+ return AVERROR_NOMEM; |
|
289 |
+ } |
|
290 |
+ } |
|
291 |
+ memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); |
|
292 |
+ |
|
293 |
+ if(chunk_type==kVGT_TAG) { |
|
294 |
+ s->frame.key_frame = 1; |
|
295 |
+ s->frame.pict_type = FF_I_TYPE; |
|
296 |
+ if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) { |
|
297 |
+ av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n"); |
|
298 |
+ return -1; |
|
299 |
+ } |
|
300 |
+ }else{ |
|
301 |
+ if (!s->last_frame.data[0]) { |
|
302 |
+ av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n"); |
|
303 |
+ return buf_size; |
|
304 |
+ } |
|
305 |
+ s->frame.key_frame = 0; |
|
306 |
+ s->frame.pict_type = FF_P_TYPE; |
|
307 |
+ if (tgv_decode_inter(s, buf, buf_end)<0) { |
|
308 |
+ av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n"); |
|
309 |
+ return -1; |
|
310 |
+ } |
|
311 |
+ } |
|
312 |
+ |
|
313 |
+ *data_size = sizeof(AVFrame); |
|
314 |
+ *(AVFrame*)data = s->frame; |
|
315 |
+ |
|
316 |
+ return buf_size; |
|
317 |
+} |
|
318 |
+ |
|
319 |
+static av_cold int tgv_decode_end(AVCodecContext *avctx) |
|
320 |
+{ |
|
321 |
+ TgvContext *s = avctx->priv_data; |
|
322 |
+ cond_release_buffer(&s->frame); |
|
323 |
+ cond_release_buffer(&s->last_frame); |
|
324 |
+ av_free(s->mv_codebook); |
|
325 |
+ av_free(s->block_codebook); |
|
326 |
+ return 0; |
|
327 |
+} |
|
328 |
+ |
|
329 |
+AVCodec eatgv_decoder = { |
|
330 |
+ "eatgv", |
|
331 |
+ CODEC_TYPE_VIDEO, |
|
332 |
+ CODEC_ID_TGV, |
|
333 |
+ sizeof(TgvContext), |
|
334 |
+ tgv_decode_init, |
|
335 |
+ NULL, |
|
336 |
+ tgv_decode_end, |
|
337 |
+ tgv_decode_frame, |
|
338 |
+ .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV Video"), |
|
339 |
+}; |