FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/eatgv.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 154 182 84.6%
Branches: 83 112 74.1%

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