lavc decoders: work with refcounted frames.
[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 "internal.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mem.h"
37
38 #define EA_PREAMBLE_SIZE 8
39 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
40
41 typedef struct TgvContext {
42 AVCodecContext *avctx;
43 AVFrame last_frame;
44 uint8_t *frame_buffer;
45 int width,height;
46 uint32_t palette[AVPALETTE_COUNT];
47
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
52 } TgvContext;
53
54 static av_cold int tgv_decode_init(AVCodecContext *avctx)
55 {
56 TgvContext *s = avctx->priv_data;
57 s->avctx = avctx;
58 avctx->time_base = (AVRational){1, 15};
59 avctx->pix_fmt = AV_PIX_FMT_PAL8;
60 return 0;
61 }
62
63 /**
64 * Unpack buffer
65 * @return 0 on success, -1 on critical buffer underflow
66 */
67 static int unpack(const uint8_t *src, const uint8_t *src_end,
68 uint8_t *dst, int width, int height)
69 {
70 uint8_t *dst_end = dst + width*height;
71 int size, size1, size2, offset, run;
72 uint8_t *dst_start = dst;
73
74 if (src[0] & 0x01)
75 src += 5;
76 else
77 src += 2;
78
79 if (src + 3 > src_end)
80 return AVERROR_INVALIDDATA;
81 size = AV_RB24(src);
82 src += 3;
83
84 while (size > 0 && src < src_end) {
85
86 /* determine size1 and size2 */
87 size1 = (src[0] & 3);
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);
93 src++;
94 size2 = 0;
95 } else { // 110
96 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
97 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
98 src += 4;
99 }
100 } else { // 10
101 size1 = ((src[1] & 0xC0) >> 6);
102 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
103 size2 = (src[0] & 0x3F) + 4;
104 src += 3;
105 }
106 } else { // 0
107 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
108 size2 = ((src[0] & 0x1C) >> 2) + 3;
109 src += 2;
110 }
111
112
113 /* fetch strip from src */
114 if (size1 > src_end - src)
115 break;
116
117 if (size1 > 0) {
118 size -= size1;
119 run = FFMIN(size1, dst_end - dst);
120 memcpy(dst, src, run);
121 dst += run;
122 src += run;
123 }
124
125 if (size2 > 0) {
126 if (dst - dst_start < offset)
127 return 0;
128 size -= size2;
129 run = FFMIN(size2, dst_end - dst);
130 av_memcpy_backptr(dst, offset, run);
131 dst += run;
132 }
133 }
134
135 return 0;
136 }
137
138 /**
139 * Decode inter-frame
140 * @return 0 on success, -1 on critical buffer underflow
141 */
142 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
143 const uint8_t *buf, const uint8_t *buf_end)
144 {
145 int num_mvs;
146 int num_blocks_raw;
147 int num_blocks_packed;
148 int vector_bits;
149 int i,j,x,y;
150 GetBitContext gb;
151 int mvbits;
152 const uint8_t *blocks_raw;
153
154 if (buf + 12 > buf_end)
155 return AVERROR_INVALIDDATA;
156
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]);
161 buf += 12;
162
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;
167 }
168
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;
173 }
174
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;
178 }
179
180 /* read motion vectors */
181 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
182
183 if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
184 return AVERROR_INVALIDDATA;
185
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);
190 }
191 buf += mvbits >> 3;
192
193 /* note ptr to uncompressed blocks */
194 blocks_raw = buf;
195 buf += num_blocks_raw * 16;
196
197 /* read compressed blocks */
198 init_get_bits(&gb, buf, (buf_end - buf) << 3);
199 for (i = 0; i < num_blocks_packed; i++) {
200 int tmp[4];
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)];
205 }
206
207 if (get_bits_left(&gb) < vector_bits *
208 (s->avctx->height / 4) * (s->avctx->width / 4))
209 return AVERROR_INVALIDDATA;
210
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);
215 const uint8_t *src;
216 int src_stride;
217
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];
221
222 if (mx < 0 || mx + 4 > s->avctx->width ||
223 my < 0 || my + 4 > s->avctx->height)
224 continue;
225
226 src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
227 src_stride = s->last_frame.linesize[0];
228 } else {
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];
234 else
235 continue;
236 src_stride = 4;
237 }
238
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];
243 }
244
245 return 0;
246 }
247
248 static int tgv_decode_frame(AVCodecContext *avctx,
249 void *data, int *got_frame,
250 AVPacket *avpkt)
251 {
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;
257 int chunk_type, ret;
258
259 chunk_type = AV_RL32(&buf[0]);
260 buf += EA_PREAMBLE_SIZE;
261
262 if (chunk_type == kVGT_TAG) {
263 int pal_count, i;
264 if (buf + 12 > buf_end) {
265 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
266 return AVERROR_INVALIDDATA;
267 }
268
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);
275 }
276
277 pal_count = AV_RL16(&buf[6]);
278 buf += 12;
279 for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
280 s->palette[i] = AV_RB24(buf);
281 buf += 3;
282 }
283 }
284
285 if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
286 return ret;
287
288 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
289 return ret;
290
291 memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
292
293 if (chunk_type == kVGT_TAG) {
294 int y;
295 frame->key_frame = 1;
296 frame->pict_type = AV_PICTURE_TYPE_I;
297
298 if (!s->frame_buffer &&
299 !(s->frame_buffer = av_malloc(s->width * s->height)))
300 return AVERROR(ENOMEM);
301
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;
305 }
306 for (y = 0; y < s->height; y++)
307 memcpy(frame->data[0] + y * frame->linesize[0],
308 s->frame_buffer + y * s->width,
309 s->width);
310 } else {
311 if (!s->last_frame.data[0]) {
312 av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
313 return buf_size;
314 }
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;
320 }
321 }
322
323 av_frame_unref(&s->last_frame);
324 if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
325 return ret;
326
327 *got_frame = 1;
328
329 return buf_size;
330 }
331
332 static av_cold int tgv_decode_end(AVCodecContext *avctx)
333 {
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);
339 return 0;
340 }
341
342 AVCodec ff_eatgv_decoder = {
343 .name = "eatgv",
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,
352 };