../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
154 / 183
84.2%
Branches:
84 / 114
73.7%
Functions:
5 / 5
100.0%

libavcodec/eatgv.c
Line Branch Exec Source
1 /*
2 * Electronic Arts TGV Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
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 "libavutil/mem.h"
32
33 #define BITSTREAM_READER_LE
34 #include "avcodec.h"
35 #include "get_bits.h"
36 #include "codec_internal.h"
37 #include "decode.h"
38
39 #define EA_PREAMBLE_SIZE 8
40 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
41
42 typedef struct TgvContext {
43 AVCodecContext *avctx;
44 AVFrame *last_frame;
45 uint8_t *frame_buffer;
46 int width,height;
47 uint32_t palette[AVPALETTE_COUNT];
48
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
53 } TgvContext;
54
55 6 static av_cold int tgv_decode_init(AVCodecContext *avctx)
56 {
57 6 TgvContext *s = avctx->priv_data;
58 6 s->avctx = avctx;
59 6 avctx->framerate = (AVRational){ 15, 1 };
60 6 avctx->pix_fmt = AV_PIX_FMT_PAL8;
61
62 6 s->last_frame = av_frame_alloc();
63
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 6 times.
6 if (!s->last_frame)
64 ✗ return AVERROR(ENOMEM);
65
66 6 return 0;
67 }
68
69 /**
70 * Unpack buffer
71 * @return 0 on success, -1 on critical buffer underflow
72 */
73 11 static int unpack(const uint8_t *src, const uint8_t *src_end,
74 uint8_t *dst, int width, int height)
75 {
76 11 uint8_t *dst_end = dst + width*height;
77 int size, size1, size2, offset, run;
78 11 uint8_t *dst_start = dst;
79
80
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 11 times.
11 if (src[0] & 0x01)
81 ✗ src += 5;
82 else
83 11 src += 2;
84
85
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 11 times.
11 if (src_end - src < 3)
86 ✗ return AVERROR_INVALIDDATA;
87 11 size = AV_RB24(src);
88 11 src += 3;
89
90
3/4
✓ Branch 27 → 28 taken 102018 times.
✓ Branch 27 → 29 taken 11 times.
✓ Branch 28 → 8 taken 102018 times.
✗ Branch 28 → 29 not taken.
102029 while (size > 0 && src < src_end) {
91
92 /* determine size1 and size2 */
93 102018 size1 = (src[0] & 3);
94
2/2
✓ Branch 8 → 9 taken 49446 times.
✓ Branch 8 → 16 taken 52572 times.
102018 if (src[0] & 0x80) { // 1
95
2/2
✓ Branch 9 → 10 taken 15234 times.
✓ Branch 9 → 15 taken 34212 times.
49446 if (src[0] & 0x40 ) { // 11
96
2/2
✓ Branch 10 → 11 taken 10712 times.
✓ Branch 10 → 14 taken 4522 times.
15234 if (src[0] & 0x20) { // 111
97
2/2
✓ Branch 11 → 12 taken 10704 times.
✓ Branch 11 → 13 taken 8 times.
10712 if (src[0] < 0xFC) // !(111111)
98 10704 size1 = (((src[0] & 31) + 1) << 2);
99 10712 src++;
100 10712 size2 = 0;
101 } else { // 110
102 4522 offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
103 4522 size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
104 4522 src += 4;
105 }
106 } else { // 10
107 34212 size1 = ((src[1] & 0xC0) >> 6);
108 34212 offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
109 34212 size2 = (src[0] & 0x3F) + 4;
110 34212 src += 3;
111 }
112 } else { // 0
113 52572 offset = ((src[0] & 0x60) << 3) + src[1] + 1;
114 52572 size2 = ((src[0] & 0x1C) >> 2) + 3;
115 52572 src += 2;
116 }
117
118
119 /* fetch strip from src */
120
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 102018 times.
102018 if (size1 > src_end - src)
121 ✗ break;
122
123
2/2
✓ Branch 19 → 20 taken 35301 times.
✓ Branch 19 → 21 taken 66717 times.
102018 if (size1 > 0) {
124 35301 size -= size1;
125 35301 run = FFMIN(size1, dst_end - dst);
126 35301 memcpy(dst, src, run);
127 35301 dst += run;
128 35301 src += run;
129 }
130
131
2/2
✓ Branch 21 → 22 taken 91306 times.
✓ Branch 21 → 26 taken 10712 times.
102018 if (size2 > 0) {
132
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 91306 times.
91306 if (dst - dst_start < offset)
133 ✗ return 0;
134 91306 size -= size2;
135 91306 run = FFMIN(size2, dst_end - dst);
136 91306 av_memcpy_backptr(dst, offset, run);
137 91306 dst += run;
138 }
139 }
140
141 11 return 0;
142 }
143
144 /**
145 * Decode inter-frame
146 * @return 0 on success, -1 on critical buffer underflow
147 */
148 79 static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
149 const uint8_t *buf, const uint8_t *buf_end)
150 {
151 int num_mvs;
152 int num_blocks_raw;
153 int num_blocks_packed;
154 int vector_bits;
155 int i,j,x,y;
156 GetBitContext gb;
157 int mvbits;
158 const uint8_t *blocks_raw;
159
160
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 79 times.
79 if(buf_end - buf < 12)
161 ✗ return AVERROR_INVALIDDATA;
162
163 79 num_mvs = AV_RL16(&buf[0]);
164 79 num_blocks_raw = AV_RL16(&buf[2]);
165 79 num_blocks_packed = AV_RL16(&buf[4]);
166 79 vector_bits = AV_RL16(&buf[6]);
167 79 buf += 12;
168
169
2/4
✓ Branch 4 → 5 taken 79 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 79 times.
79 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;
173 }
174
175 /* allocate codebook buffers as necessary */
176
2/2
✓ Branch 8 → 9 taken 8 times.
✓ Branch 8 → 13 taken 71 times.
79 if (num_mvs > s->num_mvs) {
177 8 int err = av_reallocp_array(&s->mv_codebook, num_mvs, sizeof(*s->mv_codebook));
178
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 8 times.
8 if (err < 0) {
179 ✗ s->num_mvs = 0;
180 ✗ return err;
181 }
182 8 s->num_mvs = num_mvs;
183 }
184
185
2/2
✓ Branch 13 → 14 taken 7 times.
✓ Branch 13 → 18 taken 72 times.
79 if (num_blocks_packed > s->num_blocks_packed) {
186 int err;
187
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 7 times.
7 if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
188 ✗ s->num_blocks_packed = 0;
189 ✗ return err;
190 }
191 7 s->num_blocks_packed = num_blocks_packed;
192 }
193
194 /* read motion vectors */
195 79 mvbits = (num_mvs * 2 * 10 + 31) & ~31;
196
197
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 79 times.
79 if (buf_end - buf < (mvbits>>3) + 16*num_blocks_raw + 8*num_blocks_packed)
198 ✗ return AVERROR_INVALIDDATA;
199
200 79 init_get_bits(&gb, buf, mvbits);
201
2/2
✓ Branch 25 → 22 taken 13374 times.
✓ Branch 25 → 26 taken 79 times.
13453 for (i = 0; i < num_mvs; i++) {
202 13374 s->mv_codebook[i][0] = get_sbits(&gb, 10);
203 13374 s->mv_codebook[i][1] = get_sbits(&gb, 10);
204 }
205 79 buf += mvbits >> 3;
206
207 /* note ptr to uncompressed blocks */
208 79 blocks_raw = buf;
209 79 buf += num_blocks_raw * 16;
210
211 /* read compressed blocks */
212 79 int ret = init_get_bits8(&gb, buf, buf_end - buf);
213
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 79 times.
79 if (ret < 0)
214 ✗ return ret;
215
2/2
✓ Branch 39 → 30 taken 31423 times.
✓ Branch 39 → 40 taken 79 times.
31502 for (i = 0; i < num_blocks_packed; i++) {
216 int tmp[4];
217
2/2
✓ Branch 33 → 31 taken 125692 times.
✓ Branch 33 → 34 taken 31423 times.
157115 for (j = 0; j < 4; j++)
218 125692 tmp[j] = get_bits(&gb, 8);
219
2/2
✓ Branch 37 → 35 taken 502768 times.
✓ Branch 37 → 38 taken 31423 times.
534191 for (j = 0; j < 16; j++)
220 502768 s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
221 }
222
223 79 if (get_bits_left(&gb) < vector_bits *
224
2/2
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 43 taken 78 times.
79 (s->avctx->height / 4) * (s->avctx->width / 4))
225 1 return AVERROR_INVALIDDATA;
226
227 /* read vectors and build frame */
228
2/2
✓ Branch 69 → 44 taken 4340 times.
✓ Branch 69 → 70 taken 78 times.
4418 for (y = 0; y < s->avctx->height / 4; y++)
229
2/2
✓ Branch 67 → 45 taken 347200 times.
✓ Branch 67 → 68 taken 4340 times.
351540 for (x = 0; x < s->avctx->width / 4; x++) {
230 347200 unsigned int vector = get_bits(&gb, vector_bits);
231 const uint8_t *src;
232 ptrdiff_t src_stride;
233
234
2/2
✓ Branch 46 → 47 taken 293708 times.
✓ Branch 46 → 54 taken 53492 times.
347200 if (vector < num_mvs) {
235 293708 int mx = x * 4 + s->mv_codebook[vector][0];
236 293708 int my = y * 4 + s->mv_codebook[vector][1];
237
238
3/6
✓ Branch 47 → 48 taken 293708 times.
✗ Branch 47 → 51 not taken.
✓ Branch 48 → 49 taken 293708 times.
✗ Branch 48 → 51 not taken.
✓ Branch 49 → 50 taken 293708 times.
✗ Branch 49 → 51 not taken.
293708 if (mx < 0 || mx + 4 > s->avctx->width ||
239
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 293708 times.
293708 my < 0 || my + 4 > s->avctx->height) {
240 ✗ av_log(s->avctx, AV_LOG_ERROR, "MV %d %d out of picture\n", mx, my);
241 ✗ continue;
242 }
243
244 293708 src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
245 293708 src_stride = s->last_frame->linesize[0];
246 } else {
247 53492 int offset = vector - num_mvs;
248
2/2
✓ Branch 54 → 55 taken 5411 times.
✓ Branch 54 → 56 taken 48081 times.
53492 if (offset < num_blocks_raw)
249 5411 src = blocks_raw + 16*offset;
250
1/2
✓ Branch 56 → 57 taken 48081 times.
✗ Branch 56 → 58 not taken.
48081 else if (offset - num_blocks_raw < num_blocks_packed)
251 48081 src = s->block_codebook[offset - num_blocks_raw];
252 else
253 ✗ continue;
254 53492 src_stride = 4;
255 }
256
257
2/2
✓ Branch 65 → 61 taken 1388800 times.
✓ Branch 65 → 66 taken 347200 times.
1736000 for (j = 0; j < 4; j++)
258
2/2
✓ Branch 63 → 62 taken 5555200 times.
✓ Branch 63 → 64 taken 1388800 times.
6944000 for (i = 0; i < 4; i++)
259 5555200 frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
260 5555200 src[j * src_stride + i];
261 }
262
263 78 return 0;
264 }
265
266 90 static int tgv_decode_frame(AVCodecContext *avctx, AVFrame *frame,
267 int *got_frame, AVPacket *avpkt)
268 {
269 90 const uint8_t *buf = avpkt->data;
270 90 int buf_size = avpkt->size;
271 90 TgvContext *s = avctx->priv_data;
272 90 const uint8_t *buf_end = buf + buf_size;
273 int chunk_type, ret;
274
275
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 90 times.
90 if (buf_end - buf < EA_PREAMBLE_SIZE)
276 ✗ return AVERROR_INVALIDDATA;
277
278 90 chunk_type = AV_RL32(&buf[0]);
279 90 buf += EA_PREAMBLE_SIZE;
280
281
2/2
✓ Branch 4 → 5 taken 11 times.
✓ Branch 4 → 22 taken 79 times.
90 if (chunk_type == kVGT_TAG) {
282 int pal_count, i;
283
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 11 times.
11 if(buf_end - buf < 12) {
284 ✗ av_log(avctx, AV_LOG_WARNING, "truncated header\n");
285 ✗ return AVERROR_INVALIDDATA;
286 }
287
288 11 s->width = AV_RL16(&buf[0]);
289 11 s->height = AV_RL16(&buf[2]);
290
3/4
✓ Branch 8 → 9 taken 7 times.
✓ Branch 8 → 10 taken 4 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 15 taken 7 times.
11 if (s->avctx->width != s->width || s->avctx->height != s->height) {
291 4 av_freep(&s->frame_buffer);
292 4 av_frame_unref(s->last_frame);
293
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 4 times.
4 if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
294 ✗ return ret;
295 }
296
297 11 pal_count = AV_RL16(&buf[6]);
298 11 buf += 12;
299
4/6
✓ Branch 17 → 18 taken 2816 times.
✓ Branch 17 → 20 taken 11 times.
✓ Branch 18 → 19 taken 2816 times.
✗ Branch 18 → 20 not taken.
✓ Branch 19 → 16 taken 2816 times.
✗ Branch 19 → 20 not taken.
2827 for(i = 0; i < pal_count && i < AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
300 2816 s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
301 2816 buf += 3;
302 }
303
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 11 times.
11 if (buf_end - buf < 5) {
304 ✗ return AVERROR_INVALIDDATA;
305 }
306 }
307
308
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 90 times.
90 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
309 ✗ return ret;
310
311 90 memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
312
313
2/2
✓ Branch 25 → 26 taken 11 times.
✓ Branch 25 → 37 taken 79 times.
90 if (chunk_type == kVGT_TAG) {
314 int y;
315 11 frame->flags |= AV_FRAME_FLAG_KEY;
316 11 frame->pict_type = AV_PICTURE_TYPE_I;
317
318
2/2
✓ Branch 26 → 27 taken 6 times.
✓ Branch 26 → 30 taken 5 times.
11 if (!s->frame_buffer &&
319
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 6 times.
6 !(s->frame_buffer = av_mallocz(s->width * s->height)))
320 ✗ return AVERROR(ENOMEM);
321
322
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 34 taken 11 times.
11 if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
323 ✗ av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
324 ✗ return AVERROR_INVALIDDATA;
325 }
326
2/2
✓ Branch 36 → 35 taken 2400 times.
✓ Branch 36 → 44 taken 11 times.
2411 for (y = 0; y < s->height; y++)
327 2400 memcpy(frame->data[0] + y * frame->linesize[0],
328 2400 s->frame_buffer + y * s->width,
329 2400 s->width);
330 } else {
331
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 79 times.
79 if (!s->last_frame->data[0]) {
332 ✗ av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
333 ✗ return buf_size;
334 }
335 79 frame->flags &= ~AV_FRAME_FLAG_KEY;
336 79 frame->pict_type = AV_PICTURE_TYPE_P;
337
2/2
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 44 taken 78 times.
79 if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
338 1 av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
339 1 return AVERROR_INVALIDDATA;
340 }
341 }
342
343
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 89 times.
89 if ((ret = av_frame_replace(s->last_frame, frame)) < 0)
344 ✗ return ret;
345
346 89 *got_frame = 1;
347
348 89 return buf_size;
349 }
350
351 6 static av_cold int tgv_decode_end(AVCodecContext *avctx)
352 {
353 6 TgvContext *s = avctx->priv_data;
354 6 av_frame_free(&s->last_frame);
355 6 av_freep(&s->frame_buffer);
356 6 av_freep(&s->mv_codebook);
357 6 av_freep(&s->block_codebook);
358 6 return 0;
359 }
360
361 const FFCodec ff_eatgv_decoder = {
362 .p.name = "eatgv",
363 CODEC_LONG_NAME("Electronic Arts TGV video"),
364 .p.type = AVMEDIA_TYPE_VIDEO,
365 .p.id = AV_CODEC_ID_TGV,
366 .priv_data_size = sizeof(TgvContext),
367 .init = tgv_decode_init,
368 .close = tgv_decode_end,
369 FF_CODEC_DECODE_CB(tgv_decode_frame),
370 .p.capabilities = AV_CODEC_CAP_DR1,
371 };
372