2 * Electronic Arts TGV Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 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)
55 TgvContext *s = avctx->priv_data;
57 avctx->time_base = (AVRational){1, 15};
58 avctx->pix_fmt = AV_PIX_FMT_PAL8;
64 * @return 0 on success, -1 on critical buffer underflow
66 static int unpack(const uint8_t *src, const uint8_t *src_end,
67 unsigned char *dst, int width, int height)
69 unsigned char *dst_end = dst + width*height;
70 int size, size1, size2, offset, run;
71 unsigned char *dst_start = dst;
78 if (src + 3 > src_end)
79 return AVERROR_INVALIDDATA;
83 while (size > 0 && src < src_end) {
85 /* determine size1 and size2 */
87 if (src[0] & 0x80) { // 1
88 if (src[0] & 0x40 ) { // 11
89 if (src[0] & 0x20) { // 111
90 if (src[0] < 0xFC) // !(111111)
91 size1 = (((src[0] & 31) + 1) << 2);
95 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
96 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
100 size1 = ((src[1] & 0xC0) >> 6);
101 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
102 size2 = (src[0] & 0x3F) + 4;
106 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
107 size2 = ((src[0] & 0x1C) >> 2) + 3;
112 /* fetch strip from src */
113 if (size1 > src_end - src)
118 run = FFMIN(size1, dst_end - dst);
119 memcpy(dst, src, run);
125 if (dst - dst_start < offset)
128 run = FFMIN(size2, dst_end - dst);
129 av_memcpy_backptr(dst, offset, run);
139 * @return 0 on success, -1 on critical buffer underflow
141 static int tgv_decode_inter(TgvContext *s, const uint8_t *buf,
142 const uint8_t *buf_end)
146 int num_blocks_packed;
151 const unsigned char *blocks_raw;
153 if (buf + 12 > buf_end)
154 return AVERROR_INVALIDDATA;
156 num_mvs = AV_RL16(&buf[0]);
157 num_blocks_raw = AV_RL16(&buf[2]);
158 num_blocks_packed = AV_RL16(&buf[4]);
159 vector_bits = AV_RL16(&buf[6]);
162 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
163 av_log(s->avctx, AV_LOG_ERROR,
164 "Invalid value for motion vector bits: %d\n", vector_bits);
165 return AVERROR_INVALIDDATA;
168 /* allocate codebook buffers as necessary */
169 if (num_mvs > s->num_mvs) {
170 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
171 s->num_mvs = num_mvs;
174 if (num_blocks_packed > s->num_blocks_packed) {
175 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
176 s->num_blocks_packed = num_blocks_packed;
179 /* read motion vectors */
180 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
182 if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
183 return AVERROR_INVALIDDATA;
185 init_get_bits(&gb, buf, mvbits);
186 for (i = 0; i < num_mvs; i++) {
187 s->mv_codebook[i][0] = get_sbits(&gb, 10);
188 s->mv_codebook[i][1] = get_sbits(&gb, 10);
192 /* note ptr to uncompressed blocks */
194 buf += num_blocks_raw * 16;
196 /* read compressed blocks */
197 init_get_bits(&gb, buf, (buf_end - buf) << 3);
198 for (i = 0; i < num_blocks_packed; i++) {
200 for (j = 0; j < 4; j++)
201 tmp[j] = get_bits(&gb, 8);
202 for (j = 0; j < 16; j++)
203 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
206 if (get_bits_left(&gb) < vector_bits *
207 (s->avctx->height / 4) * (s->avctx->width / 4))
208 return AVERROR_INVALIDDATA;
210 /* read vectors and build frame */
211 for (y = 0; y < s->avctx->height / 4; y++)
212 for (x = 0; x < s->avctx->width / 4; x++) {
213 unsigned int vector = get_bits(&gb, vector_bits);
214 const unsigned char *src;
217 if (vector < num_mvs) {
218 int mx = x * 4 + s->mv_codebook[vector][0];
219 int my = y * 4 + s->mv_codebook[vector][1];
221 if (mx < 0 || mx + 4 > s->avctx->width ||
222 my < 0 || my + 4 > s->avctx->height)
225 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
226 src_stride = s->last_frame.linesize[0];
228 int offset = vector - num_mvs;
229 if (offset < num_blocks_raw)
230 src = blocks_raw + 16*offset;
231 else if (offset - num_blocks_raw < num_blocks_packed)
232 src = s->block_codebook[offset - num_blocks_raw];
238 for (j = 0; j < 4; j++)
239 for (i = 0; i < 4; i++)
240 s->frame.data[0][(y * 4 + j) * s->frame.linesize[0] + (x * 4 + i)] =
241 src[j * src_stride + i];
247 /** release AVFrame buffers if allocated */
248 static void cond_release_buffer(AVFrame *pic)
251 av_freep(&pic->data[0]);
252 av_free(pic->data[1]);
256 static int tgv_decode_frame(AVCodecContext *avctx,
257 void *data, int *got_frame,
260 const uint8_t *buf = avpkt->data;
261 int buf_size = avpkt->size;
262 TgvContext *s = avctx->priv_data;
263 const uint8_t *buf_end = buf + buf_size;
266 chunk_type = AV_RL32(&buf[0]);
267 buf += EA_PREAMBLE_SIZE;
269 if (chunk_type == kVGT_TAG) {
271 if (buf + 12 > buf_end) {
272 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
273 return AVERROR_INVALIDDATA;
276 s->width = AV_RL16(&buf[0]);
277 s->height = AV_RL16(&buf[2]);
278 if (s->avctx->width != s->width || s->avctx->height != s->height) {
279 avcodec_set_dimensions(s->avctx, s->width, s->height);
280 cond_release_buffer(&s->frame);
281 cond_release_buffer(&s->last_frame);
284 pal_count = AV_RL16(&buf[6]);
286 for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
287 s->palette[i] = AV_RB24(buf);
292 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
296 FFSWAP(AVFrame, s->frame, s->last_frame);
297 if (!s->frame.data[0]) {
298 s->frame.reference = 1;
299 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
300 s->frame.linesize[0] = s->width;
302 s->frame.data[0] = av_malloc(s->width * s->height);
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");
318 return AVERROR_INVALIDDATA;
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");
329 return AVERROR_INVALIDDATA;
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,
352 .id = AV_CODEC_ID_TGV,
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"),