792eaf708e87076d820db9d883211bd6eef017d6
[libav.git] / libavcodec / eatgv.c
1 /*
2 * Electronic Arts TGV Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * Electronic Arts TGV Video Decoder
25 * by Peter Ross (pross@xvid.org)
26 *
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
29 */
30
31 #include "avcodec.h"
32 #define BITSTREAM_READER_LE
33 #include "get_bits.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36
37 #define EA_PREAMBLE_SIZE 8
38 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
39
40 typedef struct TgvContext {
41 AVCodecContext *avctx;
42 AVFrame frame;
43 AVFrame last_frame;
44 int width,height;
45 uint32_t palette[AVPALETTE_COUNT];
46
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
51 } TgvContext;
52
53 static av_cold int tgv_decode_init(AVCodecContext *avctx)
54 {
55 TgvContext *s = avctx->priv_data;
56 s->avctx = avctx;
57 avctx->time_base = (AVRational){1, 15};
58 avctx->pix_fmt = AV_PIX_FMT_PAL8;
59 return 0;
60 }
61
62 /**
63 * Unpack buffer
64 * @return 0 on success, -1 on critical buffer underflow
65 */
66 static int unpack(const uint8_t *src, const uint8_t *src_end,
67 uint8_t *dst, int width, int height)
68 {
69 uint8_t *dst_end = dst + width*height;
70 int size, size1, size2, offset, run;
71 uint8_t *dst_start = dst;
72
73 if (src[0] & 0x01)
74 src += 5;
75 else
76 src += 2;
77
78 if (src + 3 > src_end)
79 return AVERROR_INVALIDDATA;
80 size = AV_RB24(src);
81 src += 3;
82
83 while (size > 0 && src < src_end) {
84
85 /* determine size1 and size2 */
86 size1 = (src[0] & 3);
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);
92 src++;
93 size2 = 0;
94 } else { // 110
95 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
96 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
97 src += 4;
98 }
99 } else { // 10
100 size1 = ((src[1] & 0xC0) >> 6);
101 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
102 size2 = (src[0] & 0x3F) + 4;
103 src += 3;
104 }
105 } else { // 0
106 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
107 size2 = ((src[0] & 0x1C) >> 2) + 3;
108 src += 2;
109 }
110
111
112 /* fetch strip from src */
113 if (size1 > src_end - src)
114 break;
115
116 if (size1 > 0) {
117 size -= size1;
118 run = FFMIN(size1, dst_end - dst);
119 memcpy(dst, src, run);
120 dst += run;
121 src += run;
122 }
123
124 if (size2 > 0) {
125 if (dst - dst_start < offset)
126 return 0;
127 size -= size2;
128 run = FFMIN(size2, dst_end - dst);
129 av_memcpy_backptr(dst, offset, run);
130 dst += run;
131 }
132 }
133
134 return 0;
135 }
136
137 /**
138 * Decode inter-frame
139 * @return 0 on success, -1 on critical buffer underflow
140 */
141 static int tgv_decode_inter(TgvContext *s, const uint8_t *buf,
142 const uint8_t *buf_end)
143 {
144 int num_mvs;
145 int num_blocks_raw;
146 int num_blocks_packed;
147 int vector_bits;
148 int i,j,x,y;
149 GetBitContext gb;
150 int mvbits;
151 const uint8_t *blocks_raw;
152
153 if (buf + 12 > buf_end)
154 return AVERROR_INVALIDDATA;
155
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]);
160 buf += 12;
161
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;
166 }
167
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;
172 }
173
174 if (num_blocks_packed > s->num_blocks_packed) {
175 s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16);
176 s->num_blocks_packed = num_blocks_packed;
177 }
178
179 /* read motion vectors */
180 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
181
182 if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
183 return AVERROR_INVALIDDATA;
184
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);
189 }
190 buf += mvbits >> 3;
191
192 /* note ptr to uncompressed blocks */
193 blocks_raw = buf;
194 buf += num_blocks_raw * 16;
195
196 /* read compressed blocks */
197 init_get_bits(&gb, buf, (buf_end - buf) << 3);
198 for (i = 0; i < num_blocks_packed; i++) {
199 int tmp[4];
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)];
204 }
205
206 if (get_bits_left(&gb) < vector_bits *
207 (s->avctx->height / 4) * (s->avctx->width / 4))
208 return AVERROR_INVALIDDATA;
209
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 uint8_t *src;
215 int src_stride;
216
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];
220
221 if (mx < 0 || mx + 4 > s->avctx->width ||
222 my < 0 || my + 4 > s->avctx->height)
223 continue;
224
225 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
226 src_stride = s->last_frame.linesize[0];
227 } else {
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];
233 else
234 continue;
235 src_stride = 4;
236 }
237
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];
242 }
243
244 return 0;
245 }
246
247 /** release AVFrame buffers if allocated */
248 static void cond_release_buffer(AVFrame *pic)
249 {
250 if (pic->data[0]) {
251 av_freep(&pic->data[0]);
252 av_free(pic->data[1]);
253 }
254 }
255
256 static int tgv_decode_frame(AVCodecContext *avctx,
257 void *data, int *got_frame,
258 AVPacket *avpkt)
259 {
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;
264 int chunk_type, ret;
265
266 chunk_type = AV_RL32(&buf[0]);
267 buf += EA_PREAMBLE_SIZE;
268
269 if (chunk_type == kVGT_TAG) {
270 int pal_count, i;
271 if (buf + 12 > buf_end) {
272 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
273 return AVERROR_INVALIDDATA;
274 }
275
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);
282 }
283
284 pal_count = AV_RL16(&buf[6]);
285 buf += 12;
286 for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
287 s->palette[i] = AV_RB24(buf);
288 buf += 3;
289 }
290 }
291
292 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
293 return ret;
294
295 /* shuffle */
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;
301
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);
309 }
310 }
311 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
312
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;
319 }
320 } else {
321 if (!s->last_frame.data[0]) {
322 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
323 return buf_size;
324 }
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;
330 }
331 }
332
333 *got_frame = 1;
334 *(AVFrame*)data = s->frame;
335
336 return buf_size;
337 }
338
339 static av_cold int tgv_decode_end(AVCodecContext *avctx)
340 {
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);
346 return 0;
347 }
348
349 AVCodec ff_eatgv_decoder = {
350 .name = "eatgv",
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"),
358 };