2 * Electronic Arts TGV Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Electronic Arts TGV Video Decoder
25 * by Peter Ross (pross@xvid.org)
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
32 #define BITSTREAM_READER_LE
34 #include "libavutil/lzo.h"
35 #include "libavutil/imgutils.h"
37 #define EA_PREAMBLE_SIZE 8
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
40 typedef struct TgvContext {
41 AVCodecContext *avctx;
45 unsigned int palette[AVPALETTE_COUNT];
47 int (*mv_codebook)[2];
48 unsigned char (*block_codebook)[16];
49 int num_mvs; ///< current length of mv_codebook
50 int num_blocks_packed; ///< current length of block_codebook
53 static av_cold int tgv_decode_init(AVCodecContext *avctx){
54 TgvContext *s = avctx->priv_data;
56 avctx->time_base = (AVRational){1, 15};
57 avctx->pix_fmt = PIX_FMT_PAL8;
58 avcodec_get_frame_defaults(&s->frame);
59 avcodec_get_frame_defaults(&s->last_frame);
65 * @return 0 on success, -1 on critical buffer underflow
67 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
68 unsigned char *dst_end = dst + width*height;
69 int size, size1, size2, offset, run;
70 unsigned char *dst_start = dst;
77 if (src_end - src < 3)
82 while(size>0 && src<src_end) {
84 /* determine size1 and size2 */
86 if ( src[0] & 0x80 ) { // 1
87 if (src[0] & 0x40 ) { // 11
88 if ( src[0] & 0x20 ) { // 111
89 if ( src[0] < 0xFC ) // !(111111)
90 size1 = (((src[0] & 31) + 1) << 2);
94 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
95 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
99 size1 = ( ( src[1] & 0xC0) >> 6 );
100 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
101 size2 = (src[0] & 0x3F) + 4;
105 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
106 size2 = ((src[0] & 0x1C) >> 2) + 3;
111 /* fetch strip from src */
112 if (size1>src_end-src)
117 run = FFMIN(size1, dst_end-dst);
118 memcpy(dst, src, run);
124 if (dst-dst_start<offset)
127 run = FFMIN(size2, dst_end-dst);
128 av_memcpy_backptr(dst, offset, run);
138 * @return 0 on success, -1 on critical buffer underflow
140 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
141 unsigned last_frame_size = s->avctx->height*s->last_frame.linesize[0];
144 int num_blocks_packed;
149 const unsigned char *blocks_raw;
151 if(buf_end - buf < 12)
154 num_mvs = AV_RL16(&buf[0]);
155 num_blocks_raw = AV_RL16(&buf[2]);
156 num_blocks_packed = AV_RL16(&buf[4]);
157 vector_bits = AV_RL16(&buf[6]);
160 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
161 av_log(s->avctx, AV_LOG_ERROR, "vector_bits %d invalid\n", vector_bits);
162 return AVERROR_INVALIDDATA;
165 /* allocate codebook buffers as necessary */
166 if (num_mvs > s->num_mvs) {
167 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
168 s->num_mvs = num_mvs;
171 if (num_blocks_packed > s->num_blocks_packed) {
172 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
173 s->num_blocks_packed = num_blocks_packed;
176 /* read motion vectors */
177 mvbits = (num_mvs*2*10+31) & ~31;
179 if (buf_end - buf < (mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed)
182 init_get_bits(&gb, buf, mvbits);
183 for (i=0; i<num_mvs; i++) {
184 s->mv_codebook[i][0] = get_sbits(&gb, 10);
185 s->mv_codebook[i][1] = get_sbits(&gb, 10);
189 /* note ptr to uncompressed blocks */
191 buf += num_blocks_raw*16;
193 /* read compressed blocks */
194 init_get_bits(&gb, buf, (buf_end-buf)<<3);
195 for (i=0; i<num_blocks_packed; i++) {
198 tmp[j] = get_bits(&gb, 8);
200 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
203 if (get_bits_left(&gb) < vector_bits *
204 (s->avctx->height/4) * (s->avctx->width/4))
207 /* read vectors and build frame */
208 for(y=0; y<s->avctx->height/4; y++)
209 for(x=0; x<s->avctx->width/4; x++) {
210 unsigned int vector = get_bits(&gb, vector_bits);
211 const unsigned char *src;
214 if (vector < num_mvs) {
216 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
217 x*4 + s->mv_codebook[vector][0];
219 src_stride = s->last_frame.linesize[0];
220 if (offset >= last_frame_size - (3*src_stride+3))
222 src = s->last_frame.data[0] + offset;
224 int offset = vector - num_mvs;
225 if (offset<num_blocks_raw)
226 src = blocks_raw + 16*offset;
227 else if (offset-num_blocks_raw<num_blocks_packed)
228 src = s->block_codebook[offset-num_blocks_raw];
236 s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
237 src[j*src_stride + i];
243 /** release AVFrame buffers if allocated */
244 static void cond_release_buffer(AVFrame *pic)
247 av_freep(&pic->data[0]);
248 av_free(pic->data[1]);
252 static int tgv_decode_frame(AVCodecContext *avctx,
253 void *data, int *data_size,
256 const uint8_t *buf = avpkt->data;
257 int buf_size = avpkt->size;
258 TgvContext *s = avctx->priv_data;
259 const uint8_t *buf_end = buf + buf_size;
262 if (buf_end - buf < EA_PREAMBLE_SIZE)
263 return AVERROR_INVALIDDATA;
265 chunk_type = AV_RL32(&buf[0]);
266 buf += EA_PREAMBLE_SIZE;
268 if (chunk_type==kVGT_TAG) {
270 if(buf_end - buf < 12) {
271 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
275 s->width = AV_RL16(&buf[0]);
276 s->height = AV_RL16(&buf[2]);
277 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
278 avcodec_set_dimensions(s->avctx, s->width, s->height);
279 cond_release_buffer(&s->frame);
280 cond_release_buffer(&s->last_frame);
283 pal_count = AV_RL16(&buf[6]);
285 for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
286 s->palette[i] = 0xFF << 24 | AV_RB24(buf);
291 if (av_image_check_size(s->width, s->height, 0, avctx))
295 FFSWAP(AVFrame, s->frame, s->last_frame);
296 if (!s->frame.data[0]) {
297 s->frame.reference = 3;
298 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
299 s->frame.linesize[0] = s->width;
301 /* allocate additional 12 bytes to accommodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
302 s->frame.data[0] = av_malloc(s->width*s->height + 12);
303 if (!s->frame.data[0])
304 return AVERROR(ENOMEM);
305 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
306 if (!s->frame.data[1]) {
307 av_freep(&s->frame.data[0]);
308 return AVERROR(ENOMEM);
311 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
313 if(chunk_type==kVGT_TAG) {
314 s->frame.key_frame = 1;
315 s->frame.pict_type = AV_PICTURE_TYPE_I;
316 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
317 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
321 if (!s->last_frame.data[0]) {
322 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
325 s->frame.key_frame = 0;
326 s->frame.pict_type = AV_PICTURE_TYPE_P;
327 if (tgv_decode_inter(s, buf, buf_end)<0) {
328 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
333 *data_size = sizeof(AVFrame);
334 *(AVFrame*)data = s->frame;
339 static av_cold int tgv_decode_end(AVCodecContext *avctx)
341 TgvContext *s = avctx->priv_data;
342 cond_release_buffer(&s->frame);
343 cond_release_buffer(&s->last_frame);
344 av_free(s->mv_codebook);
345 av_free(s->block_codebook);
349 AVCodec ff_eatgv_decoder = {
351 .type = AVMEDIA_TYPE_VIDEO,
353 .priv_data_size = sizeof(TgvContext),
354 .init = tgv_decode_init,
355 .close = tgv_decode_end,
356 .decode = tgv_decode_frame,
357 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),