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
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mem.h"
34 #define BITSTREAM_READER_LE
39 #define EA_PREAMBLE_SIZE 8
40 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
42 typedef struct TgvContext {
43 AVCodecContext *avctx;
45 uint8_t *frame_buffer;
47 uint32_t palette[AVPALETTE_COUNT];
49 int (*mv_codebook)[2];
50 uint8_t (*block_codebook)[16];
51 int num_mvs; ///< current length of mv_codebook
52 int num_blocks_packed; ///< current length of block_codebook
55 static av_cold int tgv_decode_init(AVCodecContext *avctx)
57 TgvContext *s = avctx->priv_data;
59 avctx->framerate = (AVRational){ 15, 1 };
60 avctx->pix_fmt = AV_PIX_FMT_PAL8;
62 s->last_frame = av_frame_alloc();
64 return AVERROR(ENOMEM);
71 * @return 0 on success, -1 on critical buffer underflow
73 static int unpack(const uint8_t *src, const uint8_t *src_end,
74 uint8_t *dst, int width, int height)
76 uint8_t *dst_end = dst + width*height;
77 int size, size1, size2, offset, run;
78 uint8_t *dst_start = dst;
85 if (src_end - src < 3)
86 return AVERROR_INVALIDDATA;
90 while (size > 0 && src < src_end) {
92 /* determine size1 and size2 */
94 if (src[0] & 0x80) { // 1
95 if (src[0] & 0x40 ) { // 11
96 if (src[0] & 0x20) { // 111
97 if (src[0] < 0xFC) // !(111111)
98 size1 = (((src[0] & 31) + 1) << 2);
102 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
103 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
107 size1 = ((src[1] & 0xC0) >> 6);
108 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
109 size2 = (src[0] & 0x3F) + 4;
113 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
114 size2 = ((src[0] & 0x1C) >> 2) + 3;
119 /* fetch strip from src */
120 if (size1 > src_end - src)
125 run = FFMIN(size1, dst_end - dst);
126 memcpy(dst, src, run);
132 if (dst - dst_start < offset)
135 run = FFMIN(size2, dst_end - dst);
136 av_memcpy_backptr(dst, offset, run);
146 * @return 0 on success, -1 on critical buffer underflow
148 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
149 const uint8_t *buf, const uint8_t *buf_end)
153 int num_blocks_packed;
158 const uint8_t *blocks_raw;
160 if(buf_end - buf < 12)
161 return AVERROR_INVALIDDATA;
163 num_mvs = AV_RL16(&buf[0]);
164 num_blocks_raw = AV_RL16(&buf[2]);
165 num_blocks_packed = AV_RL16(&buf[4]);
166 vector_bits = AV_RL16(&buf[6]);
169 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
170 av_log(s->avctx, AV_LOG_ERROR,
171 "Invalid value for motion vector bits: %d\n", vector_bits);
172 return AVERROR_INVALIDDATA;
175 /* allocate codebook buffers as necessary */
176 if (num_mvs > s->num_mvs) {
177 int err = av_reallocp_array(&s->mv_codebook, num_mvs, sizeof(*s->mv_codebook));
182 s->num_mvs = num_mvs;
185 if (num_blocks_packed > s->num_blocks_packed) {
187 if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
188 s->num_blocks_packed = 0;
191 s->num_blocks_packed = num_blocks_packed;
194 /* read motion vectors */
195 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
197 if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
198 return AVERROR_INVALIDDATA;
200 init_get_bits(&gb, buf, mvbits);
201 for (i = 0; i < num_mvs; i++) {
202 s->mv_codebook[i][0] = get_sbits(&gb, 10);
203 s->mv_codebook[i][1] = get_sbits(&gb, 10);
207 /* note ptr to uncompressed blocks */
209 buf += num_blocks_raw * 16;
211 /* read compressed blocks */
212 init_get_bits(&gb, buf, (buf_end - buf) << 3);
213 for (i = 0; i < num_blocks_packed; i++) {
215 for (j = 0; j < 4; j++)
216 tmp[j] = get_bits(&gb, 8);
217 for (j = 0; j < 16; j++)
218 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
221 if (get_bits_left(&gb) < vector_bits *
222 (s->avctx->height / 4) * (s->avctx->width / 4))
223 return AVERROR_INVALIDDATA;
225 /* read vectors and build frame */
226 for (y = 0; y < s->avctx->height / 4; y++)
227 for (x = 0; x < s->avctx->width / 4; x++) {
228 unsigned int vector = get_bits(&gb, vector_bits);
230 ptrdiff_t src_stride;
232 if (vector < num_mvs) {
233 int mx = x * 4 + s->mv_codebook[vector][0];
234 int my = y * 4 + s->mv_codebook[vector][1];
236 if (mx < 0 || mx + 4 > s->avctx->width ||
237 my < 0 || my + 4 > s->avctx->height) {
238 av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
242 src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
243 src_stride = s->last_frame->linesize[0];
245 int offset = vector - num_mvs;
246 if (offset < num_blocks_raw)
247 src = blocks_raw + 16*offset;
248 else if (offset - num_blocks_raw < num_blocks_packed)
249 src = s->block_codebook[offset - num_blocks_raw];
255 for (j = 0; j < 4; j++)
256 for (i = 0; i < 4; i++)
257 frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
258 src[j * src_stride + i];
264 static int tgv_decode_frame(AVCodecContext *avctx,
265 void *data, int *got_frame,
268 const uint8_t *buf = avpkt->data;
269 int buf_size = avpkt->size;
270 TgvContext *s = avctx->priv_data;
271 const uint8_t *buf_end = buf + buf_size;
272 AVFrame *frame = data;
275 if (buf_end - buf < EA_PREAMBLE_SIZE)
276 return AVERROR_INVALIDDATA;
278 chunk_type = AV_RL32(&buf[0]);
279 buf += EA_PREAMBLE_SIZE;
281 if (chunk_type == kVGT_TAG) {
283 if(buf_end - buf < 12) {
284 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
285 return AVERROR_INVALIDDATA;
288 s->width = AV_RL16(&buf[0]);
289 s->height = AV_RL16(&buf[2]);
290 if (s->avctx->width != s->width || s->avctx->height != s->height) {
291 av_freep(&s->frame_buffer);
292 av_frame_unref(s->last_frame);
293 if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
297 pal_count = AV_RL16(&buf[6]);
299 for(i = 0; i < pal_count && i < AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
300 s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
305 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
308 memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
310 if (chunk_type == kVGT_TAG) {
312 frame->key_frame = 1;
313 frame->pict_type = AV_PICTURE_TYPE_I;
315 if (!s->frame_buffer &&
316 !(s->frame_buffer = av_mallocz(s->width * s->height)))
317 return AVERROR(ENOMEM);
319 if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
320 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
321 return AVERROR_INVALIDDATA;
323 for (y = 0; y < s->height; y++)
324 memcpy(frame->data[0] + y * frame->linesize[0],
325 s->frame_buffer + y * s->width,
328 if (!s->last_frame->data[0]) {
329 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
332 frame->key_frame = 0;
333 frame->pict_type = AV_PICTURE_TYPE_P;
334 if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
335 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
336 return AVERROR_INVALIDDATA;
340 av_frame_unref(s->last_frame);
341 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
349 static av_cold int tgv_decode_end(AVCodecContext *avctx)
351 TgvContext *s = avctx->priv_data;
352 av_frame_free(&s->last_frame);
353 av_freep(&s->frame_buffer);
354 av_freep(&s->mv_codebook);
355 av_freep(&s->block_codebook);
359 AVCodec ff_eatgv_decoder = {
361 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
362 .type = AVMEDIA_TYPE_VIDEO,
363 .id = AV_CODEC_ID_TGV,
364 .priv_data_size = sizeof(TgvContext),
365 .init = tgv_decode_init,
366 .close = tgv_decode_end,
367 .decode = tgv_decode_frame,
368 .capabilities = AV_CODEC_CAP_DR1,