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
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mem.h"
38 #define EA_PREAMBLE_SIZE 8
39 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
41 typedef struct TgvContext {
42 AVCodecContext *avctx;
44 uint8_t *frame_buffer;
46 uint32_t palette[AVPALETTE_COUNT];
48 int (*mv_codebook)[2];
49 uint8_t (*block_codebook)[16];
50 int num_mvs; ///< current length of mv_codebook
51 int num_blocks_packed; ///< current length of block_codebook
54 static av_cold int tgv_decode_init(AVCodecContext *avctx)
56 TgvContext *s = avctx->priv_data;
58 avctx->time_base = (AVRational){1, 15};
59 avctx->pix_fmt = AV_PIX_FMT_PAL8;
65 * @return 0 on success, -1 on critical buffer underflow
67 static int unpack(const uint8_t *src, const uint8_t *src_end,
68 uint8_t *dst, int width, int height)
70 uint8_t *dst_end = dst + width*height;
71 int size, size1, size2, offset, run;
72 uint8_t *dst_start = dst;
79 if (src + 3 > src_end)
80 return AVERROR_INVALIDDATA;
84 while (size > 0 && src < src_end) {
86 /* determine size1 and size2 */
88 if (src[0] & 0x80) { // 1
89 if (src[0] & 0x40 ) { // 11
90 if (src[0] & 0x20) { // 111
91 if (src[0] < 0xFC) // !(111111)
92 size1 = (((src[0] & 31) + 1) << 2);
96 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
97 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
101 size1 = ((src[1] & 0xC0) >> 6);
102 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
103 size2 = (src[0] & 0x3F) + 4;
107 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
108 size2 = ((src[0] & 0x1C) >> 2) + 3;
113 /* fetch strip from src */
114 if (size1 > src_end - src)
119 run = FFMIN(size1, dst_end - dst);
120 memcpy(dst, src, run);
126 if (dst - dst_start < offset)
129 run = FFMIN(size2, dst_end - dst);
130 av_memcpy_backptr(dst, offset, run);
140 * @return 0 on success, -1 on critical buffer underflow
142 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
143 const uint8_t *buf, const uint8_t *buf_end)
147 int num_blocks_packed;
152 const uint8_t *blocks_raw;
154 if (buf + 12 > buf_end)
155 return AVERROR_INVALIDDATA;
157 num_mvs = AV_RL16(&buf[0]);
158 num_blocks_raw = AV_RL16(&buf[2]);
159 num_blocks_packed = AV_RL16(&buf[4]);
160 vector_bits = AV_RL16(&buf[6]);
163 if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
164 av_log(s->avctx, AV_LOG_ERROR,
165 "Invalid value for motion vector bits: %d\n", vector_bits);
166 return AVERROR_INVALIDDATA;
169 /* allocate codebook buffers as necessary */
170 if (num_mvs > s->num_mvs) {
171 s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
172 s->num_mvs = num_mvs;
175 if (num_blocks_packed > s->num_blocks_packed) {
176 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16);
177 s->num_blocks_packed = num_blocks_packed;
180 /* read motion vectors */
181 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
183 if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
184 return AVERROR_INVALIDDATA;
186 init_get_bits(&gb, buf, mvbits);
187 for (i = 0; i < num_mvs; i++) {
188 s->mv_codebook[i][0] = get_sbits(&gb, 10);
189 s->mv_codebook[i][1] = get_sbits(&gb, 10);
193 /* note ptr to uncompressed blocks */
195 buf += num_blocks_raw * 16;
197 /* read compressed blocks */
198 init_get_bits(&gb, buf, (buf_end - buf) << 3);
199 for (i = 0; i < num_blocks_packed; i++) {
201 for (j = 0; j < 4; j++)
202 tmp[j] = get_bits(&gb, 8);
203 for (j = 0; j < 16; j++)
204 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
207 if (get_bits_left(&gb) < vector_bits *
208 (s->avctx->height / 4) * (s->avctx->width / 4))
209 return AVERROR_INVALIDDATA;
211 /* read vectors and build frame */
212 for (y = 0; y < s->avctx->height / 4; y++)
213 for (x = 0; x < s->avctx->width / 4; x++) {
214 unsigned int vector = get_bits(&gb, vector_bits);
218 if (vector < num_mvs) {
219 int mx = x * 4 + s->mv_codebook[vector][0];
220 int my = y * 4 + s->mv_codebook[vector][1];
222 if (mx < 0 || mx + 4 > s->avctx->width ||
223 my < 0 || my + 4 > s->avctx->height)
226 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
227 src_stride = s->last_frame.linesize[0];
229 int offset = vector - num_mvs;
230 if (offset < num_blocks_raw)
231 src = blocks_raw + 16*offset;
232 else if (offset - num_blocks_raw < num_blocks_packed)
233 src = s->block_codebook[offset - num_blocks_raw];
239 for (j = 0; j < 4; j++)
240 for (i = 0; i < 4; i++)
241 frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
242 src[j * src_stride + i];
248 static int tgv_decode_frame(AVCodecContext *avctx,
249 void *data, int *got_frame,
252 const uint8_t *buf = avpkt->data;
253 int buf_size = avpkt->size;
254 TgvContext *s = avctx->priv_data;
255 const uint8_t *buf_end = buf + buf_size;
256 AVFrame *frame = data;
259 chunk_type = AV_RL32(&buf[0]);
260 buf += EA_PREAMBLE_SIZE;
262 if (chunk_type == kVGT_TAG) {
264 if (buf + 12 > buf_end) {
265 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
266 return AVERROR_INVALIDDATA;
269 s->width = AV_RL16(&buf[0]);
270 s->height = AV_RL16(&buf[2]);
271 if (s->avctx->width != s->width || s->avctx->height != s->height) {
272 avcodec_set_dimensions(s->avctx, s->width, s->height);
273 av_freep(&s->frame_buffer);
274 av_frame_unref(&s->last_frame);
277 pal_count = AV_RL16(&buf[6]);
279 for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
280 s->palette[i] = AV_RB24(buf);
285 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
288 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
291 memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
293 if (chunk_type == kVGT_TAG) {
295 frame->key_frame = 1;
296 frame->pict_type = AV_PICTURE_TYPE_I;
298 if (!s->frame_buffer &&
299 !(s->frame_buffer = av_malloc(s->width * s->height)))
300 return AVERROR(ENOMEM);
302 if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
303 av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
304 return AVERROR_INVALIDDATA;
306 for (y = 0; y < s->height; y++)
307 memcpy(frame->data[0] + y * frame->linesize[0],
308 s->frame_buffer + y * s->width,
311 if (!s->last_frame.data[0]) {
312 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
315 frame->key_frame = 0;
316 frame->pict_type = AV_PICTURE_TYPE_P;
317 if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
318 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
319 return AVERROR_INVALIDDATA;
323 av_frame_unref(&s->last_frame);
324 if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
332 static av_cold int tgv_decode_end(AVCodecContext *avctx)
334 TgvContext *s = avctx->priv_data;
335 av_frame_unref(&s->last_frame);
336 av_freep(&s->frame_buffer);
337 av_free(s->mv_codebook);
338 av_free(s->block_codebook);
342 AVCodec ff_eatgv_decoder = {
344 .type = AVMEDIA_TYPE_VIDEO,
345 .id = AV_CODEC_ID_TGV,
346 .priv_data_size = sizeof(TgvContext),
347 .init = tgv_decode_init,
348 .close = tgv_decode_end,
349 .decode = tgv_decode_frame,
350 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
351 .capabilities = CODEC_CAP_DR1,