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 ALT_BITSTREAM_READER_LE
34 #include "libavutil/lzo.h"
36 #define EA_PREAMBLE_SIZE 8
37 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
39 typedef struct TgvContext {
40 AVCodecContext *avctx;
44 unsigned int palette[AVPALETTE_COUNT];
46 int (*mv_codebook)[2];
47 unsigned char (*block_codebook)[16];
48 int num_mvs; ///< current length of mv_codebook
49 int num_blocks_packed; ///< current length of block_codebook
52 static av_cold int tgv_decode_init(AVCodecContext *avctx){
53 TgvContext *s = avctx->priv_data;
55 avctx->time_base = (AVRational){1, 15};
56 avctx->pix_fmt = PIX_FMT_PAL8;
62 * @return 0 on success, -1 on critical buffer underflow
64 static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
65 unsigned char *dst_end = dst + width*height;
66 int size, size1, size2, av_uninit(offset), run;
67 unsigned char *dst_start = dst;
79 while(size>0 && src<src_end) {
81 /* determine size1 and size2 */
83 if ( src[0] & 0x80 ) { // 1
84 if (src[0] & 0x40 ) { // 11
85 if ( src[0] & 0x20 ) { // 111
86 if ( src[0] < 0xFC ) // !(111111)
87 size1 = (((src[0] & 31) + 1) << 2);
91 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
92 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
96 size1 = ( ( src[1] & 0xC0) >> 6 );
97 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
98 size2 = (src[0] & 0x3F) + 4;
102 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
103 size2 = ((src[0] & 0x1C) >> 2) + 3;
108 /* fetch strip from src */
109 if (size1>src_end-src)
114 run = FFMIN(size1, dst_end-dst);
115 memcpy(dst, src, run);
121 if (dst-dst_start<offset)
124 run = FFMIN(size2, dst_end-dst);
125 av_memcpy_backptr(dst, offset, run);
135 * @return 0 on success, -1 on critical buffer underflow
137 static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
138 unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];
141 int num_blocks_packed;
146 const unsigned char *blocks_raw;
151 num_mvs = AV_RL16(&buf[0]);
152 num_blocks_raw = AV_RL16(&buf[2]);
153 num_blocks_packed = AV_RL16(&buf[4]);
154 vector_bits = AV_RL16(&buf[6]);
157 /* allocate codebook buffers as neccessary */
158 if (num_mvs > s->num_mvs) {
159 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
160 s->num_mvs = num_mvs;
163 if (num_blocks_packed > s->num_blocks_packed) {
164 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
165 s->num_blocks_packed = num_blocks_packed;
168 /* read motion vectors */
169 mvbits = (num_mvs*2*10+31) & ~31;
171 if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
174 init_get_bits(&gb, buf, mvbits);
175 for (i=0; i<num_mvs; i++) {
176 s->mv_codebook[i][0] = get_sbits(&gb, 10);
177 s->mv_codebook[i][1] = get_sbits(&gb, 10);
181 /* note ptr to uncompressed blocks */
183 buf += num_blocks_raw*16;
185 /* read compressed blocks */
186 init_get_bits(&gb, buf, (buf_end-buf)<<3);
187 for (i=0; i<num_blocks_packed; i++) {
190 tmp[j] = get_bits(&gb, 8);
192 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
195 if (get_bits_left(&gb) < vector_bits *
196 (s->avctx->height/4) * (s->avctx->width/4))
199 /* read vectors and build frame */
200 for(y=0; y<s->avctx->height/4; y++)
201 for(x=0; x<s->avctx->width/4; x++) {
202 unsigned int vector = get_bits(&gb, vector_bits);
203 const unsigned char *src;
206 if (vector < num_mvs) {
207 src = s->last_frame.data[0] +
208 (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
209 x*4 + s->mv_codebook[vector][0];
210 src_stride = s->last_frame.linesize[0];
211 if (src+3*src_stride+3>=frame0_end)
214 int offset = vector - num_mvs;
215 if (offset<num_blocks_raw)
216 src = blocks_raw + 16*offset;
217 else if (offset-num_blocks_raw<num_blocks_packed)
218 src = s->block_codebook[offset-num_blocks_raw];
226 s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
227 src[j*src_stride + i];
233 /** release AVFrame buffers if allocated */
234 static void cond_release_buffer(AVFrame *pic)
237 av_freep(&pic->data[0]);
238 av_free(pic->data[1]);
242 static int tgv_decode_frame(AVCodecContext *avctx,
243 void *data, int *data_size,
246 const uint8_t *buf = avpkt->data;
247 int buf_size = avpkt->size;
248 TgvContext *s = avctx->priv_data;
249 const uint8_t *buf_end = buf + buf_size;
252 chunk_type = AV_RL32(&buf[0]);
253 buf += EA_PREAMBLE_SIZE;
255 if (chunk_type==kVGT_TAG) {
258 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
262 s->width = AV_RL16(&buf[0]);
263 s->height = AV_RL16(&buf[2]);
264 if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
265 avcodec_set_dimensions(s->avctx, s->width, s->height);
266 cond_release_buffer(&s->frame);
267 cond_release_buffer(&s->last_frame);
270 pal_count = AV_RL16(&buf[6]);
272 for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
273 s->palette[i] = AV_RB24(buf);
278 if (avcodec_check_dimensions(avctx, s->width, s->height))
282 FFSWAP(AVFrame, s->frame, s->last_frame);
283 if (!s->frame.data[0]) {
284 s->frame.reference = 1;
285 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
286 s->frame.linesize[0] = s->width;
288 /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
289 s->frame.data[0] = av_malloc(s->width*s->height + 12);
290 if (!s->frame.data[0])
291 return AVERROR(ENOMEM);
292 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
293 if (!s->frame.data[1]) {
294 av_freep(&s->frame.data[0]);
295 return AVERROR(ENOMEM);
298 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
300 if(chunk_type==kVGT_TAG) {
301 s->frame.key_frame = 1;
302 s->frame.pict_type = FF_I_TYPE;
303 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
304 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
308 if (!s->last_frame.data[0]) {
309 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
312 s->frame.key_frame = 0;
313 s->frame.pict_type = FF_P_TYPE;
314 if (tgv_decode_inter(s, buf, buf_end)<0) {
315 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
320 *data_size = sizeof(AVFrame);
321 *(AVFrame*)data = s->frame;
326 static av_cold int tgv_decode_end(AVCodecContext *avctx)
328 TgvContext *s = avctx->priv_data;
329 cond_release_buffer(&s->frame);
330 cond_release_buffer(&s->last_frame);
331 av_free(s->mv_codebook);
332 av_free(s->block_codebook);
336 AVCodec eatgv_decoder = {
345 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),