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/imgutils.h"
35 #include "libavutil/mem.h"
37 #define EA_PREAMBLE_SIZE 8
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
40 typedef struct TgvContext {
41 AVCodecContext *avctx;
45 uint32_t palette[AVPALETTE_COUNT];
47 int (*mv_codebook)[2];
48 uint8_t (*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)
55 TgvContext *s = avctx->priv_data;
57 avctx->time_base = (AVRational){1, 15};
58 avctx->pix_fmt = AV_PIX_FMT_PAL8;
59 avcodec_get_frame_defaults(&s->frame);
60 avcodec_get_frame_defaults(&s->last_frame);
66 * @return 0 on success, -1 on critical buffer underflow
68 static int unpack(const uint8_t *src, const uint8_t *src_end,
69 uint8_t *dst, int width, int height)
71 uint8_t *dst_end = dst + width*height;
72 int size, size1, size2, offset, run;
73 uint8_t *dst_start = dst;
80 if (src_end - src < 3)
81 return AVERROR_INVALIDDATA;
85 while (size > 0 && src < src_end) {
87 /* determine size1 and size2 */
89 if (src[0] & 0x80) { // 1
90 if (src[0] & 0x40 ) { // 11
91 if (src[0] & 0x20) { // 111
92 if (src[0] < 0xFC) // !(111111)
93 size1 = (((src[0] & 31) + 1) << 2);
97 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
98 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
102 size1 = ((src[1] & 0xC0) >> 6);
103 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
104 size2 = (src[0] & 0x3F) + 4;
108 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
109 size2 = ((src[0] & 0x1C) >> 2) + 3;
114 /* fetch strip from src */
115 if (size1 > src_end - src)
120 run = FFMIN(size1, dst_end - dst);
121 memcpy(dst, src, run);
127 if (dst - dst_start < offset)
130 run = FFMIN(size2, dst_end - dst);
131 av_memcpy_backptr(dst, offset, run);
141 * @return 0 on success, -1 on critical buffer underflow
143 static int tgv_decode_inter(TgvContext *s, const uint8_t *buf,
144 const uint8_t *buf_end)
148 int num_blocks_packed;
153 const uint8_t *blocks_raw;
155 if(buf_end - buf < 12)
156 return AVERROR_INVALIDDATA;
158 num_mvs = AV_RL16(&buf[0]);
159 num_blocks_raw = AV_RL16(&buf[2]);
160 num_blocks_packed = AV_RL16(&buf[4]);
161 vector_bits = AV_RL16(&buf[6]);
164 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
165 av_log(s->avctx, AV_LOG_ERROR,
166 "Invalid value for motion vector bits: %d\n", vector_bits);
167 return AVERROR_INVALIDDATA;
170 /* allocate codebook buffers as necessary */
171 if (num_mvs > s->num_mvs) {
172 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
173 s->num_mvs = num_mvs;
176 if (num_blocks_packed > s->num_blocks_packed) {
177 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16);
178 s->num_blocks_packed = num_blocks_packed;
181 /* read motion vectors */
182 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
184 if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
185 return AVERROR_INVALIDDATA;
187 init_get_bits(&gb, buf, mvbits);
188 for (i = 0; i < num_mvs; i++) {
189 s->mv_codebook[i][0] = get_sbits(&gb, 10);
190 s->mv_codebook[i][1] = get_sbits(&gb, 10);
194 /* note ptr to uncompressed blocks */
196 buf += num_blocks_raw * 16;
198 /* read compressed blocks */
199 init_get_bits(&gb, buf, (buf_end - buf) << 3);
200 for (i = 0; i < num_blocks_packed; i++) {
202 for (j = 0; j < 4; j++)
203 tmp[j] = get_bits(&gb, 8);
204 for (j = 0; j < 16; j++)
205 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
208 if (get_bits_left(&gb) < vector_bits *
209 (s->avctx->height / 4) * (s->avctx->width / 4))
210 return AVERROR_INVALIDDATA;
212 /* read vectors and build frame */
213 for (y = 0; y < s->avctx->height / 4; y++)
214 for (x = 0; x < s->avctx->width / 4; x++) {
215 unsigned int vector = get_bits(&gb, vector_bits);
219 if (vector < num_mvs) {
220 int mx = x * 4 + s->mv_codebook[vector][0];
221 int my = y * 4 + s->mv_codebook[vector][1];
223 if (mx < 0 || mx + 4 > s->avctx->width ||
224 my < 0 || my + 4 > s->avctx->height) {
225 av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
229 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
230 src_stride = s->last_frame.linesize[0];
232 int offset = vector - num_mvs;
233 if (offset < num_blocks_raw)
234 src = blocks_raw + 16*offset;
235 else if (offset - num_blocks_raw < num_blocks_packed)
236 src = s->block_codebook[offset - num_blocks_raw];
242 for (j = 0; j < 4; j++)
243 for (i = 0; i < 4; i++)
244 s->frame.data[0][(y * 4 + j) * s->frame.linesize[0] + (x * 4 + i)] =
245 src[j * src_stride + i];
251 /** release AVFrame buffers if allocated */
252 static void cond_release_buffer(AVFrame *pic)
255 av_freep(&pic->data[0]);
256 av_free(pic->data[1]);
260 static int tgv_decode_frame(AVCodecContext *avctx,
261 void *data, int *got_frame,
264 const uint8_t *buf = avpkt->data;
265 int buf_size = avpkt->size;
266 TgvContext *s = avctx->priv_data;
267 const uint8_t *buf_end = buf + buf_size;
270 if (buf_end - buf < EA_PREAMBLE_SIZE)
271 return AVERROR_INVALIDDATA;
273 chunk_type = AV_RL32(&buf[0]);
274 buf += EA_PREAMBLE_SIZE;
276 if (chunk_type == kVGT_TAG) {
278 if(buf_end - buf < 12) {
279 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
280 return AVERROR_INVALIDDATA;
283 s->width = AV_RL16(&buf[0]);
284 s->height = AV_RL16(&buf[2]);
285 if (s->avctx->width != s->width || s->avctx->height != s->height) {
286 avcodec_set_dimensions(s->avctx, s->width, s->height);
287 cond_release_buffer(&s->frame);
288 cond_release_buffer(&s->last_frame);
291 pal_count = AV_RL16(&buf[6]);
293 for(i = 0; i < pal_count && i < AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
294 s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
299 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
303 FFSWAP(AVFrame, s->frame, s->last_frame);
304 if (!s->frame.data[0]) {
305 s->frame.reference = 3;
306 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
307 s->frame.linesize[0] = s->width;
309 s->frame.data[0] = av_malloc(s->width * s->height);
310 if (!s->frame.data[0])
311 return AVERROR(ENOMEM);
312 s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
313 if (!s->frame.data[1]) {
314 av_freep(&s->frame.data[0]);
315 return AVERROR(ENOMEM);
318 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
320 if (chunk_type == kVGT_TAG) {
321 s->frame.key_frame = 1;
322 s->frame.pict_type = AV_PICTURE_TYPE_I;
323 if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height) < 0) {
324 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
325 return AVERROR_INVALIDDATA;
328 if (!s->last_frame.data[0]) {
329 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
332 s->frame.key_frame = 0;
333 s->frame.pict_type = AV_PICTURE_TYPE_P;
334 if (tgv_decode_inter(s, buf, buf_end) < 0) {
335 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
336 return AVERROR_INVALIDDATA;
341 *(AVFrame*)data = s->frame;
346 static av_cold int tgv_decode_end(AVCodecContext *avctx)
348 TgvContext *s = avctx->priv_data;
349 cond_release_buffer(&s->frame);
350 cond_release_buffer(&s->last_frame);
351 av_free(s->mv_codebook);
352 av_free(s->block_codebook);
356 AVCodec ff_eatgv_decoder = {
358 .type = AVMEDIA_TYPE_VIDEO,
359 .id = AV_CODEC_ID_TGV,
360 .priv_data_size = sizeof(TgvContext),
361 .init = tgv_decode_init,
362 .close = tgv_decode_end,
363 .decode = tgv_decode_frame,
364 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),