FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pafvideo.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 192 224 85.7%
Branches: 92 126 73.0%

Line Branch Exec Source
1 /*
2 * Packed Animation File video decoder
3 * Copyright (c) 2012 Paul B Mahol
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/imgutils.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "copy_block.h"
27 #include "codec_internal.h"
28 #include "internal.h"
29
30
31 static const uint8_t block_sequences[16][8] = {
32 { 0, 0, 0, 0, 0, 0, 0, 0 },
33 { 2, 0, 0, 0, 0, 0, 0, 0 },
34 { 5, 7, 0, 0, 0, 0, 0, 0 },
35 { 5, 0, 0, 0, 0, 0, 0, 0 },
36 { 6, 0, 0, 0, 0, 0, 0, 0 },
37 { 5, 7, 5, 7, 0, 0, 0, 0 },
38 { 5, 7, 5, 0, 0, 0, 0, 0 },
39 { 5, 7, 6, 0, 0, 0, 0, 0 },
40 { 5, 5, 0, 0, 0, 0, 0, 0 },
41 { 3, 0, 0, 0, 0, 0, 0, 0 },
42 { 6, 6, 0, 0, 0, 0, 0, 0 },
43 { 2, 4, 0, 0, 0, 0, 0, 0 },
44 { 2, 4, 5, 7, 0, 0, 0, 0 },
45 { 2, 4, 5, 0, 0, 0, 0, 0 },
46 { 2, 4, 6, 0, 0, 0, 0, 0 },
47 { 2, 4, 5, 7, 5, 7, 0, 0 },
48 };
49
50 typedef struct PAFVideoDecContext {
51 AVFrame *pic;
52 GetByteContext gb;
53
54 int width;
55 int height;
56
57 int current_frame;
58 uint8_t *frame[4];
59 int dirty[4];
60 int frame_size;
61 int video_size;
62
63 uint8_t *opcodes;
64 } PAFVideoDecContext;
65
66 4 static av_cold int paf_video_close(AVCodecContext *avctx)
67 {
68 4 PAFVideoDecContext *c = avctx->priv_data;
69 int i;
70
71 4 av_frame_free(&c->pic);
72
73
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < 4; i++)
74 16 av_freep(&c->frame[i]);
75
76 4 return 0;
77 }
78
79 4 static av_cold int paf_video_init(AVCodecContext *avctx)
80 {
81 4 PAFVideoDecContext *c = avctx->priv_data;
82 int i;
83 int ret;
84
85 4 c->width = avctx->width;
86 4 c->height = avctx->height;
87
88
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (avctx->height & 3 || avctx->width & 3) {
89 av_log(avctx, AV_LOG_ERROR,
90 "width %d and height %d must be multiplie of 4.\n",
91 avctx->width, avctx->height);
92 return AVERROR_INVALIDDATA;
93 }
94
95 4 avctx->pix_fmt = AV_PIX_FMT_PAL8;
96 4 ret = av_image_check_size2(avctx->width, FFALIGN(avctx->height, 256), avctx->max_pixels, avctx->pix_fmt, 0, avctx);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
98 return ret;
99
100 4 c->pic = av_frame_alloc();
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!c->pic)
102 return AVERROR(ENOMEM);
103
104 4 c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
105 4 c->video_size = avctx->width * avctx->height;
106
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < 4; i++) {
107 16 c->frame[i] = av_mallocz(c->frame_size);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!c->frame[i])
109 return AVERROR(ENOMEM);
110 }
111
112 4 return 0;
113 }
114
115 612 static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
116 {
117 int i;
118
119
2/2
✓ Branch 0 taken 2448 times.
✓ Branch 1 taken 612 times.
3060 for (i = 0; i < 4; i++) {
120 2448 bytestream2_get_buffer(&c->gb, dst, 4);
121 2448 dst += width;
122 }
123 612 }
124
125 9005 static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
126 {
127 int i;
128
129
2/2
✓ Branch 0 taken 36020 times.
✓ Branch 1 taken 9005 times.
45025 for (i = 0; i < 4; i++) {
130
2/2
✓ Branch 0 taken 7465 times.
✓ Branch 1 taken 28555 times.
36020 if (mask & (1 << 7 - i))
131 7465 dst[i] = color;
132
2/2
✓ Branch 0 taken 7596 times.
✓ Branch 1 taken 28424 times.
36020 if (mask & (1 << 3 - i))
133 7596 dst[width + i] = color;
134 }
135 9005 }
136
137 10810 static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
138 {
139 int i;
140
141
2/2
✓ Branch 0 taken 43240 times.
✓ Branch 1 taken 10810 times.
54050 for (i = 0; i < 4; i++) {
142
2/2
✓ Branch 0 taken 14148 times.
✓ Branch 1 taken 29092 times.
43240 if (mask & (1 << 7 - i))
143 14148 dst[i] = src[i];
144
2/2
✓ Branch 0 taken 14674 times.
✓ Branch 1 taken 28566 times.
43240 if (mask & (1 << 3 - i))
145 14674 dst[width + i] = src[width + i];
146 }
147 10810 }
148
149 24878 static void set_src_position(PAFVideoDecContext *c,
150 const uint8_t **p,
151 const uint8_t **pend)
152 {
153 24878 int val = bytestream2_get_be16(&c->gb);
154 24878 int page = val >> 14;
155 24878 int x = (val & 0x7F);
156 24878 int y = ((val >> 7) & 0x7F);
157
158 24878 *p = c->frame[page] + x * 2 + y * 2 * c->width;
159 24878 *pend = c->frame[page] + c->frame_size;
160 24878 }
161
162 6 static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
163 {
164 uint32_t opcode_size, offset;
165 6 uint8_t *dst, *dend, mask = 0, color = 0;
166 const uint8_t *src, *send, *opcodes;
167 6 int i, j, op = 0;
168
169 6 i = bytestream2_get_byte(&c->gb);
170
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (i) {
171
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (code & 0x10) {
172 int align;
173
174 6 align = bytestream2_tell(&c->gb) & 3;
175
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (align)
176 6 bytestream2_skip(&c->gb, 4 - align);
177 }
178 do {
179 int page, val, x, y;
180 6 val = bytestream2_get_be16(&c->gb);
181 6 page = val >> 14;
182 6 x = (val & 0x7F) * 2;
183 6 y = ((val >> 7) & 0x7F) * 2;
184 6 dst = c->frame[page] + x + y * c->width;
185 6 dend = c->frame[page] + c->frame_size;
186 6 offset = (x & 0x7F) * 2;
187 6 j = bytestream2_get_le16(&c->gb) + offset;
188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(&c->gb) < (j - offset) * 16)
189 return AVERROR_INVALIDDATA;
190 6 c->dirty[page] = 1;
191 do {
192 612 offset++;
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (dst + 3 * c->width + 4 > dend)
194 return AVERROR_INVALIDDATA;
195 612 read4x4block(c, dst, c->width);
196
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 605 times.
612 if ((offset & 0x3F) == 0)
197 7 dst += c->width * 3;
198 612 dst += 4;
199
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 6 times.
612 } while (offset < j);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } while (--i);
201 }
202
203 6 dst = c->frame[c->current_frame];
204 6 dend = c->frame[c->current_frame] + c->frame_size;
205 do {
206 18432 set_src_position(c, &src, &send);
207
1/2
✓ Branch 0 taken 18432 times.
✗ Branch 1 not taken.
18432 if ((src + 3 * c->width + 4 > send) ||
208
2/4
✓ Branch 0 taken 18432 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18432 times.
36864 (dst + 3 * c->width + 4 > dend) ||
209 18432 bytestream2_get_bytes_left(&c->gb) < 4)
210 return AVERROR_INVALIDDATA;
211 18432 copy_block4(dst, src, c->width, c->width, 4);
212 18432 i++;
213
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 18144 times.
18432 if ((i & 0x3F) == 0)
214 288 dst += c->width * 3;
215 18432 dst += 4;
216
2/2
✓ Branch 0 taken 18426 times.
✓ Branch 1 taken 6 times.
18432 } while (i < c->video_size / 16);
217
218 6 opcode_size = bytestream2_get_le16(&c->gb);
219 6 bytestream2_skip(&c->gb, 2);
220
221
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
222 return AVERROR_INVALIDDATA;
223
224 6 opcodes = pkt + bytestream2_tell(&c->gb);
225 6 bytestream2_skipu(&c->gb, opcode_size);
226
227 6 dst = c->frame[c->current_frame];
228
229
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 6 times.
294 for (i = 0; i < c->height; i += 4, dst += c->width * 3)
230
2/2
✓ Branch 0 taken 18432 times.
✓ Branch 1 taken 288 times.
18720 for (j = 0; j < c->width; j += 4, dst += 4) {
231 18432 int opcode, k = 0;
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18432 times.
18432 if (op > opcode_size)
233 return AVERROR_INVALIDDATA;
234
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 9216 times.
18432 if (j & 4) {
235 9216 opcode = opcodes[op] & 15;
236 9216 op++;
237 } else {
238 9216 opcode = opcodes[op] >> 4;
239 }
240
241
2/2
✓ Branch 0 taken 19815 times.
✓ Branch 1 taken 18432 times.
38247 while (block_sequences[opcode][k]) {
242 19815 offset = c->width * 2;
243 19815 code = block_sequences[opcode][k++];
244
245
6/7
✓ Branch 0 taken 4435 times.
✓ Branch 1 taken 1400 times.
✓ Branch 2 taken 3170 times.
✓ Branch 3 taken 5422 times.
✓ Branch 4 taken 1024 times.
✓ Branch 5 taken 4364 times.
✗ Branch 6 not taken.
19815 switch (code) {
246 4435 case 2:
247 4435 offset = 0;
248 5835 case 3:
249 5835 color = bytestream2_get_byte(&c->gb);
250 9005 case 4:
251 9005 mask = bytestream2_get_byte(&c->gb);
252 9005 copy_color_mask(dst + offset, c->width, mask, color);
253 9005 break;
254 5422 case 5:
255 5422 offset = 0;
256 6446 case 6:
257 6446 set_src_position(c, &src, &send);
258 10810 case 7:
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10810 times.
10810 if (src + offset + c->width + 4 > send)
260 return AVERROR_INVALIDDATA;
261 10810 mask = bytestream2_get_byte(&c->gb);
262 10810 copy_src_mask(dst + offset, c->width, mask, src + offset);
263 10810 break;
264 }
265 }
266 }
267
268 6 return 0;
269 }
270
271 152 static int paf_video_decode(AVCodecContext *avctx, AVFrame *rframe,
272 int *got_frame, AVPacket *pkt)
273 {
274 152 PAFVideoDecContext *c = avctx->priv_data;
275 uint8_t code, *dst, *end;
276 int i, frame, ret;
277
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 if (pkt->size < 2)
279 return AVERROR_INVALIDDATA;
280
281 152 bytestream2_init(&c->gb, pkt->data, pkt->size);
282
283 152 code = bytestream2_get_byte(&c->gb);
284
2/4
✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 152 times.
152 if ((code & 0xF) > 4 || (code & 0xF) == 3) {
285 avpriv_request_sample(avctx, "unknown/invalid code");
286 return AVERROR_INVALIDDATA;
287 }
288
289
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 146 times.
152 if ((code & 0xF) == 0 &&
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 c->video_size / 32 - (int64_t)bytestream2_get_bytes_left(&c->gb) > c->video_size / 32 * (int64_t)avctx->discard_damaged_percentage / 100)
291 return AVERROR_INVALIDDATA;
292
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
152 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
294 return ret;
295
296
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 150 times.
152 if (code & 0x20) { // frame is keyframe
297 2 memset(c->pic->data[1], 0, AVPALETTE_SIZE);
298 2 c->current_frame = 0;
299 2 c->pic->key_frame = 1;
300 2 c->pic->pict_type = AV_PICTURE_TYPE_I;
301 } else {
302 150 c->pic->key_frame = 0;
303 150 c->pic->pict_type = AV_PICTURE_TYPE_P;
304 }
305
306
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 150 times.
152 if (code & 0x40) { // palette update
307 2 uint32_t *out = (uint32_t *)c->pic->data[1];
308 int index, count;
309
310 2 index = bytestream2_get_byte(&c->gb);
311 2 count = bytestream2_get_byte(&c->gb) + 1;
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (index + count > 256)
314 return AVERROR_INVALIDDATA;
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
316 return AVERROR_INVALIDDATA;
317
318 2 out += index;
319
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < count; i++) {
320 unsigned r, g, b;
321
322 512 r = bytestream2_get_byteu(&c->gb);
323 512 r = r << 2 | r >> 4;
324 512 g = bytestream2_get_byteu(&c->gb);
325 512 g = g << 2 | g >> 4;
326 512 b = bytestream2_get_byteu(&c->gb);
327 512 b = b << 2 | b >> 4;
328 512 *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
329 }
330 2 c->pic->palette_has_changed = 1;
331 }
332
333 152 c->dirty[c->current_frame] = 1;
334
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 150 times.
152 if (code & 0x20)
335
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < 4; i++) {
336
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (c->dirty[i])
337 5 memset(c->frame[i], 0, c->frame_size);
338 8 c->dirty[i] = 0;
339 }
340
341
3/5
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 118 times.
✗ Branch 4 not taken.
152 switch (code & 0x0F) {
342 6 case 0:
343 /* Block-based motion compensation using 4x4 blocks with either
344 * horizontal or vertical vectors; might incorporate VQ as well. */
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = decode_0(c, pkt->data, code)) < 0)
346 return ret;
347 6 break;
348 case 1:
349 /* Uncompressed data. This mode specifies that (width * height) bytes
350 * should be copied directly from the encoded buffer into the output. */
351 dst = c->frame[c->current_frame];
352 // possibly chunk length data
353 bytestream2_skip(&c->gb, 2);
354 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
355 return AVERROR_INVALIDDATA;
356 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
357 break;
358 28 case 2:
359 /* Copy reference frame: Consume the next byte in the stream as the
360 * reference frame (which should be 0, 1, 2, or 3, and should not be
361 * the same as the current frame number). */
362 28 frame = bytestream2_get_byte(&c->gb);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (frame > 3)
364 return AVERROR_INVALIDDATA;
365
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (frame != c->current_frame)
366 7 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
367 28 break;
368 118 case 4:
369 /* Run length encoding.*/
370 118 dst = c->frame[c->current_frame];
371 118 end = dst + c->video_size;
372
373 118 bytestream2_skip(&c->gb, 2);
374
375
2/2
✓ Branch 0 taken 175223 times.
✓ Branch 1 taken 118 times.
175341 while (dst < end) {
376 int8_t code;
377 int count;
378
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 175223 times.
175223 if (bytestream2_get_bytes_left(&c->gb) < 2)
380 return AVERROR_INVALIDDATA;
381
382 175223 code = bytestream2_get_byteu(&c->gb);
383 175223 count = FFABS(code) + 1;
384
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175223 times.
175223 if (dst + count > end)
386 return AVERROR_INVALIDDATA;
387
2/2
✓ Branch 0 taken 116135 times.
✓ Branch 1 taken 59088 times.
175223 if (code < 0)
388 116135 memset(dst, bytestream2_get_byteu(&c->gb), count);
389 else
390 59088 bytestream2_get_buffer(&c->gb, dst, count);
391 175223 dst += count;
392 }
393 118 break;
394 default:
395 av_assert0(0);
396 }
397
398 152 av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
399 152 c->frame[c->current_frame], c->width,
400 c->width, c->height);
401
402 152 c->current_frame = (c->current_frame + 1) & 3;
403
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
152 if ((ret = av_frame_ref(rframe, c->pic)) < 0)
404 return ret;
405
406 152 *got_frame = 1;
407
408 152 return pkt->size;
409 }
410
411 const FFCodec ff_paf_video_decoder = {
412 .p.name = "paf_video",
413 .p.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
414 .p.type = AVMEDIA_TYPE_VIDEO,
415 .p.id = AV_CODEC_ID_PAF_VIDEO,
416 .priv_data_size = sizeof(PAFVideoDecContext),
417 .init = paf_video_init,
418 .close = paf_video_close,
419 FF_CODEC_DECODE_CB(paf_video_decode),
420 .p.capabilities = AV_CODEC_CAP_DR1,
421 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
422 };
423