FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngdec.c
Date: 2026-05-03 08:24:11
Exec Total Coverage
Lines: 723 1178 61.4%
Functions: 26 33 78.8%
Branches: 480 901 53.3%

Line Branch Exec Source
1 /*
2 * PNG image format
3 * Copyright (c) 2003 Fabrice Bellard
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 //#define DEBUG
23
24 #include "config_components.h"
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/csp.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mastering_display_metadata.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/rational.h"
37 #include "libavutil/stereo3d.h"
38
39 #include "avcodec.h"
40 #include "bytestream.h"
41 #include "codec_internal.h"
42 #include "decode.h"
43 #include "exif_internal.h"
44 #include "apng.h"
45 #include "png.h"
46 #include "pngdsp.h"
47 #include "progressframe.h"
48 #include "thread.h"
49 #include "zlib_wrapper.h"
50
51 #include <zlib.h>
52
53 enum PNGHeaderState {
54 PNG_IHDR = 1 << 0,
55 PNG_PLTE = 1 << 1,
56 };
57
58 enum PNGImageState {
59 PNG_IDAT = 1 << 0,
60 PNG_ALLIMAGE = 1 << 1,
61 };
62
63 typedef struct PNGDecContext {
64 PNGDSPContext dsp;
65 AVCodecContext *avctx;
66
67 GetByteContext gb;
68 ProgressFrame last_picture;
69 ProgressFrame picture;
70
71 AVDictionary *frame_metadata;
72
73 uint8_t iccp_name[82];
74 uint8_t *iccp_data;
75 size_t iccp_data_len;
76
77 int stereo_mode;
78
79 int have_chrm;
80 uint32_t white_point[2];
81 uint32_t display_primaries[3][2];
82 int gamma;
83 int have_srgb;
84 int have_cicp;
85 enum AVColorPrimaries cicp_primaries;
86 enum AVColorTransferCharacteristic cicp_trc;
87 enum AVColorRange cicp_range;
88 int have_clli;
89 uint32_t clli_max;
90 uint32_t clli_avg;
91 /* Mastering Display Color Volume */
92 int have_mdcv;
93 uint16_t mdcv_primaries[3][2];
94 uint16_t mdcv_white_point[2];
95 uint32_t mdcv_max_lum;
96 uint32_t mdcv_min_lum;
97
98 enum PNGHeaderState hdr_state;
99 enum PNGImageState pic_state;
100 int width, height;
101 int cur_w, cur_h;
102 int x_offset, y_offset;
103 uint8_t dispose_op, blend_op;
104 int bit_depth;
105 int color_type;
106 int compression_type;
107 int interlace_type;
108 int filter_type;
109 int channels;
110 int bits_per_pixel;
111 int bpp;
112 int has_trns;
113 uint8_t transparent_color_be[6];
114 int significant_bits;
115
116 uint32_t palette[256];
117 uint8_t *crow_buf;
118 uint8_t *last_row;
119 unsigned int last_row_size;
120 uint8_t *tmp_row;
121 unsigned int tmp_row_size;
122 uint8_t *buffer;
123 int buffer_size;
124 int pass;
125 int crow_size; /* compressed row size (include filter type) */
126 int row_size; /* decompressed row size */
127 int pass_row_size; /* decompress row size of the current pass */
128 int y;
129 FFZStream zstream;
130
131 AVBufferRef *exif_data;
132 } PNGDecContext;
133
134 /* Mask to determine which pixels are valid in a pass */
135 static const uint8_t png_pass_mask[NB_PASSES] = {
136 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
137 };
138
139 /* Mask to determine which y pixels can be written in a pass */
140 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
141 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
142 };
143
144 /* Mask to determine which pixels to overwrite while displaying */
145 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
146 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
147 };
148
149 /* NOTE: we try to construct a good looking image at each pass. width
150 * is the original image width. We also do pixel format conversion at
151 * this stage */
152 5632 static void png_put_interlaced_row(uint8_t *dst, int width,
153 int bits_per_pixel, int pass,
154 int color_type, const uint8_t *src)
155 {
156 int x, mask, dsp_mask, j, src_x, b, bpp;
157 uint8_t *d;
158 const uint8_t *s;
159
160 5632 mask = png_pass_mask[pass];
161 5632 dsp_mask = png_pass_dsp_mask[pass];
162
163
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5632 times.
5632 switch (bits_per_pixel) {
164 case 1:
165 src_x = 0;
166 for (x = 0; x < width; x++) {
167 j = (x & 7);
168 if ((dsp_mask << j) & 0x80) {
169 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
170 dst[x >> 3] &= 0xFF7F>>j;
171 dst[x >> 3] |= b << (7 - j);
172 }
173 if ((mask << j) & 0x80)
174 src_x++;
175 }
176 break;
177 case 2:
178 src_x = 0;
179 for (x = 0; x < width; x++) {
180 int j2 = 2 * (x & 3);
181 j = (x & 7);
182 if ((dsp_mask << j) & 0x80) {
183 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
184 dst[x >> 2] &= 0xFF3F>>j2;
185 dst[x >> 2] |= b << (6 - j2);
186 }
187 if ((mask << j) & 0x80)
188 src_x++;
189 }
190 break;
191 case 4:
192 src_x = 0;
193 for (x = 0; x < width; x++) {
194 int j2 = 4*(x&1);
195 j = (x & 7);
196 if ((dsp_mask << j) & 0x80) {
197 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
198 dst[x >> 1] &= 0xFF0F>>j2;
199 dst[x >> 1] |= b << (4 - j2);
200 }
201 if ((mask << j) & 0x80)
202 src_x++;
203 }
204 break;
205 5632 default:
206 5632 bpp = bits_per_pixel >> 3;
207 5632 d = dst;
208 5632 s = src;
209
2/2
✓ Branch 0 taken 720896 times.
✓ Branch 1 taken 5632 times.
726528 for (x = 0; x < width; x++) {
210 720896 j = x & 7;
211
2/2
✓ Branch 0 taken 524288 times.
✓ Branch 1 taken 196608 times.
720896 if ((dsp_mask << j) & 0x80) {
212 524288 memcpy(d, s, bpp);
213 }
214 720896 d += bpp;
215
2/2
✓ Branch 0 taken 245760 times.
✓ Branch 1 taken 475136 times.
720896 if ((mask << j) & 0x80)
216 245760 s += bpp;
217 }
218 5632 break;
219 }
220 5632 }
221
222 #define UNROLL1(bpp, op) \
223 { \
224 r = dst[0]; \
225 if (bpp >= 2) \
226 g = dst[1]; \
227 if (bpp >= 3) \
228 b = dst[2]; \
229 if (bpp >= 4) \
230 a = dst[3]; \
231 for (; i <= size - bpp; i += bpp) { \
232 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
233 if (bpp == 1) \
234 continue; \
235 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
236 if (bpp == 2) \
237 continue; \
238 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
239 if (bpp == 3) \
240 continue; \
241 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
242 } \
243 }
244
245 #define UNROLL_FILTER(op) \
246 if (bpp == 1) { \
247 UNROLL1(1, op) \
248 } else if (bpp == 2) { \
249 UNROLL1(2, op) \
250 } else if (bpp == 3) { \
251 UNROLL1(3, op) \
252 } else if (bpp == 4) { \
253 UNROLL1(4, op) \
254 } \
255 for (; i < size; i++) { \
256 dst[i] = op(dst[i - bpp], src[i], last[i]); \
257 }
258
259 /* NOTE: 'dst' can be equal to 'last' */
260 131568 void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
261 const uint8_t *src, const uint8_t *last, int size, int bpp)
262 {
263 int i, p, r, g, b, a;
264
265
5/6
✓ Branch 0 taken 4782 times.
✓ Branch 1 taken 4768 times.
✓ Branch 2 taken 11070 times.
✓ Branch 3 taken 2737 times.
✓ Branch 4 taken 108211 times.
✗ Branch 5 not taken.
131568 switch (filter_type) {
266 4782 case PNG_FILTER_VALUE_NONE:
267 4782 memcpy(dst, src, size);
268 4782 break;
269 4768 case PNG_FILTER_VALUE_SUB:
270
2/2
✓ Branch 0 taken 17017 times.
✓ Branch 1 taken 4768 times.
21785 for (i = 0; i < bpp; i++)
271 17017 dst[i] = src[i];
272
2/2
✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 2113 times.
4768 if (bpp == 4) {
273 2655 p = *(int *)dst;
274
2/2
✓ Branch 0 taken 408525 times.
✓ Branch 1 taken 2655 times.
411180 for (; i < size; i += bpp) {
275 408525 unsigned s = *(const int *)(src + i);
276 408525 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
277 408525 *(int *)(dst + i) = p;
278 }
279 } else {
280 #define OP_SUB(x, s, l) ((x) + (s))
281
12/18
✗ Branch 0 not taken.
✓ Branch 1 taken 2113 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 2097 times.
✓ Branch 6 taken 5168 times.
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 2075 times.
✓ Branch 9 taken 22 times.
✓ Branch 10 taken 415890 times.
✓ Branch 11 taken 2075 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 22 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 36596 times.
✓ Branch 17 taken 2113 times.
459767 UNROLL_FILTER(OP_SUB);
282 }
283 4768 break;
284 11070 case PNG_FILTER_VALUE_UP:
285 11070 dsp->add_bytes_l2(dst, src, last, size);
286 11070 break;
287 2737 case PNG_FILTER_VALUE_AVG:
288
2/2
✓ Branch 0 taken 8869 times.
✓ Branch 1 taken 2737 times.
11606 for (i = 0; i < bpp; i++) {
289 8869 p = (last[i] >> 1);
290 8869 dst[i] = p + src[i];
291 }
292 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
293
15/18
✗ Branch 0 not taken.
✓ Branch 1 taken 2737 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2735 times.
✓ Branch 6 taken 254 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2131 times.
✓ Branch 9 taken 604 times.
✓ Branch 10 taken 310689 times.
✓ Branch 11 taken 2131 times.
✓ Branch 12 taken 576 times.
✓ Branch 13 taken 28 times.
✓ Branch 14 taken 48974 times.
✓ Branch 15 taken 576 times.
✓ Branch 16 taken 21336 times.
✓ Branch 17 taken 2737 times.
383990 UNROLL_FILTER(OP_AVG);
294 2737 break;
295 108211 case PNG_FILTER_VALUE_PAETH:
296
2/2
✓ Branch 0 taken 335650 times.
✓ Branch 1 taken 108211 times.
443861 for (i = 0; i < bpp; i++) {
297 335650 p = last[i];
298 335650 dst[i] = p + src[i];
299 }
300
3/4
✓ Branch 0 taken 104123 times.
✓ Branch 1 taken 4088 times.
✓ Branch 2 taken 104123 times.
✗ Branch 3 not taken.
108211 if (bpp > 2 && size > 4) {
301 /* would write off the end of the array if we let it process
302 * the last pixel with bpp=3 */
303
2/2
✓ Branch 0 taken 101700 times.
✓ Branch 1 taken 2423 times.
104123 int w = (bpp & 3) ? size - 3 : size;
304
305
1/2
✓ Branch 0 taken 104123 times.
✗ Branch 1 not taken.
104123 if (w > i) {
306 104123 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
307 104123 i = w;
308 }
309 }
310 108211 ff_png_add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
311 108211 break;
312 }
313 131568 }
314
315 /* This used to be called "deloco" in FFmpeg
316 * and is actually an inverse reversible colorspace transformation */
317 #define YUV2RGB(NAME, TYPE) \
318 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
319 { \
320 int i; \
321 for (i = 0; i < size - 2; i += 3 + alpha) { \
322 int g = dst [i + 1]; \
323 dst[i + 0] += g; \
324 dst[i + 2] += g; \
325 } \
326 }
327
328 YUV2RGB(rgb8, uint8_t)
329 YUV2RGB(rgb16, uint16_t)
330
331 587 static int percent_missing(PNGDecContext *s)
332 {
333
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 583 times.
587 if (s->interlace_type) {
334 4 return 100 - 100 * s->pass / (NB_PASSES - 1);
335 } else {
336 583 return 100 - 100 * s->y / s->cur_h;
337 }
338 }
339
340 /* process exactly one decompressed row */
341 127327 static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
342 {
343 uint8_t *ptr, *last_row;
344 int got_line;
345
346
2/2
✓ Branch 0 taken 125407 times.
✓ Branch 1 taken 1920 times.
127327 if (!s->interlace_type) {
347 125407 ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
348
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 124749 times.
125407 if (s->y == 0)
349 658 last_row = s->last_row;
350 else
351 124749 last_row = ptr - dst_stride;
352
353 125407 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
354 last_row, s->row_size, s->bpp);
355 /* loco lags by 1 row so that it doesn't interfere with top prediction */
356
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 125407 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
125407 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
357 if (s->bit_depth == 16) {
358 deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2,
359 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
360 } else {
361 deloco_rgb8(ptr - dst_stride, s->row_size,
362 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
363 }
364 }
365 125407 s->y++;
366
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 124749 times.
125407 if (s->y == s->cur_h) {
367 658 s->pic_state |= PNG_ALLIMAGE;
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 658 times.
658 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
369 if (s->bit_depth == 16) {
370 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
371 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
372 } else {
373 deloco_rgb8(ptr, s->row_size,
374 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
375 }
376 }
377 }
378 } else {
379 1920 got_line = 0;
380 for (;;) {
381 9080 ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
382
2/2
✓ Branch 0 taken 3832 times.
✓ Branch 1 taken 5248 times.
9080 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
383 /* if we already read one row, it is time to stop to
384 * wait for the next one */
385
2/2
✓ Branch 0 taken 1912 times.
✓ Branch 1 taken 1920 times.
3832 if (got_line)
386 1912 break;
387 1920 ff_png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
388 1920 s->last_row, s->pass_row_size, s->bpp);
389 1920 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
390 1920 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
391 1920 got_line = 1;
392 }
393
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 1536 times.
7168 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
394 5632 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
395 5632 s->color_type, s->last_row);
396 }
397 7168 s->y++;
398
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 7112 times.
7168 if (s->y == s->cur_h) {
399 56 memset(s->last_row, 0, s->row_size);
400 for (;;) {
401
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 48 times.
56 if (s->pass == NB_PASSES - 1) {
402 8 s->pic_state |= PNG_ALLIMAGE;
403 8 goto the_end;
404 } else {
405 48 s->pass++;
406 48 s->y = 0;
407 48 s->pass_row_size = ff_png_pass_row_size(s->pass,
408 s->bits_per_pixel,
409 s->cur_w);
410 48 s->crow_size = s->pass_row_size + 1;
411
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (s->pass_row_size != 0)
412 48 break;
413 /* skip pass if empty row */
414 }
415 }
416 }
417 }
418 1920 the_end:;
419 }
420 127327 }
421
422 12453 static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
423 uint8_t *dst, ptrdiff_t dst_stride)
424 {
425 12453 z_stream *const zstream = &s->zstream.zstream;
426 int ret;
427 12453 zstream->avail_in = bytestream2_get_bytes_left(gb);
428 12453 zstream->next_in = gb->buffer;
429
430 /* decode one line if possible */
431
2/2
✓ Branch 0 taken 139082 times.
✓ Branch 1 taken 12453 times.
151535 while (zstream->avail_in > 0) {
432 139082 ret = inflate(zstream, Z_PARTIAL_FLUSH);
433
3/4
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 138416 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
139082 if (ret != Z_OK && ret != Z_STREAM_END) {
434 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
435 return AVERROR_EXTERNAL;
436 }
437
2/2
✓ Branch 0 taken 127327 times.
✓ Branch 1 taken 11755 times.
139082 if (zstream->avail_out == 0) {
438
1/2
✓ Branch 0 taken 127327 times.
✗ Branch 1 not taken.
127327 if (!(s->pic_state & PNG_ALLIMAGE)) {
439 127327 png_handle_row(s, dst, dst_stride);
440 }
441 127327 zstream->avail_out = s->crow_size;
442 127327 zstream->next_out = s->crow_buf;
443 }
444
3/4
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 138416 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
139082 if (ret == Z_STREAM_END && zstream->avail_in > 0) {
445 av_log(s->avctx, AV_LOG_WARNING,
446 "%d undecompressed bytes left in buffer\n", zstream->avail_in);
447 return 0;
448 }
449 }
450 12453 return 0;
451 }
452
453 6 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
454 const uint8_t *data_end, void *logctx)
455 {
456 FFZStream z;
457 6 z_stream *const zstream = &z.zstream;
458 unsigned char *buf;
459 unsigned buf_size;
460 6 int ret = ff_inflate_init(&z, logctx);
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
462 return ret;
463
464 6 zstream->next_in = data;
465 6 zstream->avail_in = data_end - data;
466 6 av_bprint_init(bp, 0, AV_BPRINT_SIZE_UNLIMITED);
467
468
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 while (zstream->avail_in > 0) {
469 18 av_bprint_get_buffer(bp, 2, &buf, &buf_size);
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (buf_size < 2) {
471 ret = AVERROR(ENOMEM);
472 goto fail;
473 }
474 18 zstream->next_out = buf;
475 18 zstream->avail_out = buf_size - 1;
476 18 ret = inflate(zstream, Z_PARTIAL_FLUSH);
477
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
18 if (ret != Z_OK && ret != Z_STREAM_END) {
478 ret = AVERROR_EXTERNAL;
479 goto fail;
480 }
481 18 bp->len += zstream->next_out - buf;
482
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 if (ret == Z_STREAM_END)
483 6 break;
484 }
485 6 ff_inflate_end(&z);
486 6 bp->str[bp->len] = 0;
487 6 return 0;
488
489 fail:
490 ff_inflate_end(&z);
491 av_bprint_finalize(bp, NULL);
492 return ret;
493 }
494
495 276 static char *iso88591_to_utf8(const char *in, size_t size_in)
496 {
497 276 size_t extra = 0, i;
498 char *out, *q;
499
500
2/2
✓ Branch 0 taken 4545 times.
✓ Branch 1 taken 276 times.
4821 for (i = 0; i < size_in; i++)
501 4545 extra += !!(in[i] & 0x80);
502
2/4
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 276 times.
276 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
503 return NULL;
504 276 q = out = av_malloc(size_in + extra + 1);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 276 times.
276 if (!out)
506 return NULL;
507
2/2
✓ Branch 0 taken 4545 times.
✓ Branch 1 taken 276 times.
4821 for (i = 0; i < size_in; i++) {
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4545 times.
4545 if (in[i] & 0x80) {
509 *(q++) = 0xC0 | (in[i] >> 6);
510 *(q++) = 0x80 | (in[i] & 0x3F);
511 } else {
512 4545 *(q++) = in[i];
513 }
514 }
515 276 *(q++) = 0;
516 276 return out;
517 }
518
519 static int decode_text_to_exif(PNGDecContext *s, const char *txt_utf8)
520 {
521 size_t len = strlen(txt_utf8);
522 const char *ptr = txt_utf8;
523 const char *end = txt_utf8 + len;
524 size_t exif_len = 0;
525 uint8_t *exif_ptr;
526 const uint8_t *exif_end;
527
528 // first we find a newline
529 while (*ptr++ != '\n') {
530 if (ptr >= end)
531 return AVERROR_BUFFER_TOO_SMALL;
532 }
533
534 // we check for "exif" and skip over it
535 if (end - ptr < 4 || strncmp("exif", ptr, 4))
536 return AVERROR_INVALIDDATA;
537 ptr += 3;
538
539 // then we find the next printable non-space character
540 while (!av_isgraph(*++ptr)) {
541 if (ptr >= end)
542 return AVERROR_BUFFER_TOO_SMALL;
543 }
544
545 // parse the length
546 while (av_isdigit(*ptr)) {
547 size_t nlen = exif_len * 10 + (*ptr - '0');
548 if (nlen < exif_len) // overflow
549 return AVERROR_INVALIDDATA;
550 exif_len = nlen;
551 if (++ptr >= end)
552 return AVERROR_BUFFER_TOO_SMALL;
553 }
554
555 // then we find the next printable non-space character
556 while (!av_isgraph(*ptr)) {
557 if (++ptr >= end)
558 return AVERROR_BUFFER_TOO_SMALL;
559 }
560
561 // first condition checks for overflow in 2 * exif_len
562 if (exif_len > SIZE_MAX / 2 || end - ptr < 2 * exif_len)
563 return AVERROR_INVALIDDATA;
564 if (exif_len < 10)
565 return AVERROR_INVALIDDATA;
566
567 av_buffer_unref(&s->exif_data);
568 // the buffer starts with "Exif " which we skip over
569 // we don't use AV_EXIF_EXIF00 because that disagrees
570 // with the eXIf chunk format
571 s->exif_data = av_buffer_alloc(exif_len - 6);
572 if (!s->exif_data)
573 return AVERROR(ENOMEM);
574
575 // we subtract one because we call ++ptr later
576 // compiler will optimize out the call
577 ptr += strlen("Exif ") * 2 - 1;
578
579 exif_ptr = s->exif_data->data;
580 exif_end = exif_ptr + s->exif_data->size;
581
582 while (exif_ptr < exif_end) {
583 while (++ptr < end) {
584 if (*ptr >= '0' && *ptr <= '9') {
585 *exif_ptr = (*ptr - '0') << 4;
586 break;
587 }
588 if (*ptr >= 'a' && *ptr <= 'f') {
589 *exif_ptr = (*ptr - 'a' + 10) << 4;
590 break;
591 }
592 }
593 while (++ptr < end) {
594 if (*ptr >= '0' && *ptr <= '9') {
595 *exif_ptr += *ptr - '0';
596 break;
597 }
598 if (*ptr >= 'a' && *ptr <= 'f') {
599 *exif_ptr += *ptr - 'a' + 10;
600 break;
601 }
602 }
603 if (ptr > end)
604 return AVERROR_INVALIDDATA;
605 exif_ptr++;
606 }
607
608 return 0;
609 }
610
611 138 static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
612 {
613 int ret, method;
614 138 const uint8_t *data = gb->buffer;
615 138 const uint8_t *data_end = gb->buffer_end;
616 138 const char *keyword = data;
617 138 const char *keyword_end = memchr(keyword, 0, data_end - data);
618 138 char *kw_utf8 = NULL, *txt_utf8 = NULL;
619 const char *text;
620 unsigned text_len;
621 AVBPrint bp;
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!keyword_end)
624 return AVERROR_INVALIDDATA;
625 138 data = keyword_end + 1;
626
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (compressed) {
628 if (data == data_end)
629 return AVERROR_INVALIDDATA;
630 method = *(data++);
631 if (method)
632 return AVERROR_INVALIDDATA;
633 if ((ret = decode_zbuf(&bp, data, data_end, s->avctx)) < 0)
634 return ret;
635 text = bp.str;
636 text_len = bp.len;
637 } else {
638 138 text = data;
639 138 text_len = data_end - data;
640 }
641
642 138 txt_utf8 = iso88591_to_utf8(text, text_len);
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (compressed)
644 av_bprint_finalize(&bp, NULL);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!txt_utf8)
646 return AVERROR(ENOMEM);
647 138 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!kw_utf8) {
649 av_free(txt_utf8);
650 return AVERROR(ENOMEM);
651 }
652
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!strcmp(kw_utf8, "Raw profile type exif")) {
654 ret = decode_text_to_exif(s, txt_utf8);
655 if (ret < 0) {;
656 av_buffer_unref(&s->exif_data);
657 } else {
658 av_freep(&kw_utf8);
659 av_freep(&txt_utf8);
660 return ret;
661 }
662 }
663
664 138 av_dict_set(&s->frame_metadata, kw_utf8, txt_utf8,
665 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
666 138 return 0;
667 }
668
669 421 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
670 GetByteContext *gb)
671 {
672
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 if (bytestream2_get_bytes_left(gb) != 13)
673 return AVERROR_INVALIDDATA;
674
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (s->pic_state & PNG_IDAT) {
676 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
677 return AVERROR_INVALIDDATA;
678 }
679
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (s->hdr_state & PNG_IHDR) {
681 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
682 return AVERROR_INVALIDDATA;
683 }
684
685 421 s->width = s->cur_w = bytestream2_get_be32(gb);
686 421 s->height = s->cur_h = bytestream2_get_be32(gb);
687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 if (av_image_check_size(s->width, s->height, 0, avctx)) {
688 s->cur_w = s->cur_h = s->width = s->height = 0;
689 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
690 return AVERROR_INVALIDDATA;
691 }
692 421 s->bit_depth = bytestream2_get_byte(gb);
693
3/6
✓ Branch 0 taken 421 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 421 times.
✗ Branch 5 not taken.
421 if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
694
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 381 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
421 s->bit_depth != 8 && s->bit_depth != 16) {
695 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
696 goto error;
697 }
698 421 s->color_type = bytestream2_get_byte(gb);
699 421 s->compression_type = bytestream2_get_byte(gb);
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (s->compression_type) {
701 av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
702 goto error;
703 }
704 421 s->filter_type = bytestream2_get_byte(gb);
705 421 s->interlace_type = bytestream2_get_byte(gb);
706 421 s->hdr_state |= PNG_IHDR;
707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (avctx->debug & FF_DEBUG_PICT_INFO)
708 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
709 "compression_type=%d filter_type=%d interlace_type=%d\n",
710 s->width, s->height, s->bit_depth, s->color_type,
711 s->compression_type, s->filter_type, s->interlace_type);
712
713 421 return 0;
714 error:
715 s->cur_w = s->cur_h = s->width = s->height = 0;
716 s->bit_depth = 8;
717 return AVERROR_INVALIDDATA;
718 }
719
720 388 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
721 GetByteContext *gb)
722 {
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (s->pic_state & PNG_IDAT) {
724 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
725 return AVERROR_INVALIDDATA;
726 }
727 388 avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
728 388 avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
729
2/4
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 388 times.
388 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
730 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
731 388 bytestream2_skip(gb, 1); /* unit specifier */
732
733 388 return 0;
734 }
735
736 2 static int decode_exif_chunk(AVCodecContext *avctx, PNGDecContext *s,
737 GetByteContext *gb)
738 {
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(s->hdr_state & PNG_IHDR)) {
740 av_log(avctx, AV_LOG_ERROR, "eXIf before IHDR\n");
741 return AVERROR_INVALIDDATA;
742 }
743
744 2 av_buffer_unref(&s->exif_data);
745 2 s->exif_data = av_buffer_alloc(bytestream2_get_bytes_left(gb));
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->exif_data)
747 return AVERROR(ENOMEM);
748 2 bytestream2_get_buffer(gb, s->exif_data->data, s->exif_data->size);
749
750 2 return 0;
751 }
752
753 /*
754 * This populates AVCodecContext fields so it must be called before
755 * ff_thread_finish_setup() to avoid a race condition with respect to the
756 * generic copying of avctx fields.
757 */
758 666 static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame)
759 {
760 666 PNGDecContext *s = avctx->priv_data;
761 int ret;
762
763
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 662 times.
666 if (s->have_cicp) {
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->cicp_primaries >= AVCOL_PRI_NB)
765 av_log(avctx, AV_LOG_WARNING, "unrecognized cICP primaries\n");
766 else
767 4 avctx->color_primaries = frame->color_primaries = s->cicp_primaries;
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->cicp_trc >= AVCOL_TRC_NB)
769 av_log(avctx, AV_LOG_WARNING, "unrecognized cICP transfer\n");
770 else
771 4 avctx->color_trc = frame->color_trc = s->cicp_trc;
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->cicp_range == 0) {
773 av_log(avctx, AV_LOG_WARNING, "tv-range cICP tag found. Colors may be wrong\n");
774 avctx->color_range = frame->color_range = AVCOL_RANGE_MPEG;
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (s->cicp_range != 1) {
776 /* we already printed a warning when parsing the cICP chunk */
777 avctx->color_range = frame->color_range = AVCOL_RANGE_UNSPECIFIED;
778 }
779
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 656 times.
662 } else if (s->iccp_data) {
780 AVFrameSideData *sd;
781 6 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_ICC_PROFILE,
782 s->iccp_data_len, &sd);
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
784 return ret;
785
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (sd) {
786 6 memcpy(sd->data, s->iccp_data, s->iccp_data_len);
787 6 av_dict_set(&sd->metadata, "name", s->iccp_name, 0);
788 }
789
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 617 times.
656 } else if (s->have_srgb) {
790 39 avctx->color_primaries = frame->color_primaries = AVCOL_PRI_BT709;
791 39 avctx->color_trc = frame->color_trc = AVCOL_TRC_IEC61966_2_1;
792
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 611 times.
617 } else if (s->have_chrm) {
793 AVColorPrimariesDesc desc;
794 enum AVColorPrimaries prim;
795 6 desc.wp.x = av_make_q(s->white_point[0], 100000);
796 6 desc.wp.y = av_make_q(s->white_point[1], 100000);
797 6 desc.prim.r.x = av_make_q(s->display_primaries[0][0], 100000);
798 6 desc.prim.r.y = av_make_q(s->display_primaries[0][1], 100000);
799 6 desc.prim.g.x = av_make_q(s->display_primaries[1][0], 100000);
800 6 desc.prim.g.y = av_make_q(s->display_primaries[1][1], 100000);
801 6 desc.prim.b.x = av_make_q(s->display_primaries[2][0], 100000);
802 6 desc.prim.b.y = av_make_q(s->display_primaries[2][1], 100000);
803 6 prim = av_csp_primaries_id_from_desc(&desc);
804
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (prim != AVCOL_PRI_UNSPECIFIED)
805 6 avctx->color_primaries = frame->color_primaries = prim;
806 else
807 av_log(avctx, AV_LOG_WARNING, "unknown cHRM primaries\n");
808 }
809
810 /* these chunks override gAMA */
811
6/6
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 621 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 617 times.
666 if (s->iccp_data || s->have_srgb || s->have_cicp) {
812 49 av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
813
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 583 times.
617 } else if (s->gamma) {
814 /*
815 * These values are 100000/2.2, 100000/2.8, 100000/2.6, and
816 * 100000/1.0 respectively. 45455, 35714, and 38462, and 100000.
817 * There's a 0.001 gamma tolerance here in case of floating
818 * point issues when the PNG was written.
819 *
820 * None of the other enums have a pure gamma curve so it makes
821 * sense to leave those to sRGB and cICP.
822 */
823
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
34 if (s->gamma > 45355 && s->gamma < 45555)
824 26 avctx->color_trc = frame->color_trc = AVCOL_TRC_GAMMA22;
825
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 else if (s->gamma > 35614 && s->gamma < 35814)
826 avctx->color_trc = frame->color_trc = AVCOL_TRC_GAMMA28;
827
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 else if (s->gamma > 38362 && s->gamma < 38562)
828 avctx->color_trc = frame->color_trc = AVCOL_TRC_SMPTE428;
829
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8 else if (s->gamma > 99900 && s->gamma < 100100)
830 avctx->color_trc = frame->color_trc = AVCOL_TRC_LINEAR;
831 }
832
833 /* PNG only supports RGB */
834 666 avctx->colorspace = frame->colorspace = AVCOL_SPC_RGB;
835
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 662 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
666 if (!s->have_cicp || s->cicp_range == 1)
836 666 avctx->color_range = frame->color_range = AVCOL_RANGE_JPEG;
837
838 /*
839 * tRNS sets alpha depth to full, so we ignore sBIT if set.
840 * As a result we must wait until now to set
841 * avctx->bits_per_raw_sample in case tRNS appears after sBIT
842 */
843
3/4
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 179 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 487 times.
666 if (!s->has_trns && s->significant_bits > 0)
844 avctx->bits_per_raw_sample = s->significant_bits;
845
846
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 664 times.
666 if (s->have_clli) {
847 AVContentLightMetadata *clli;
848
849 2 ret = ff_decode_content_light_new(avctx, frame, &clli);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
851 return ret;
852
853
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (clli) {
854 /*
855 * 0.0001 divisor value
856 * see: https://www.w3.org/TR/png-3/#cLLI-chunk
857 */
858 2 clli->MaxCLL = s->clli_max / 10000;
859 2 clli->MaxFALL = s->clli_avg / 10000;
860 }
861 }
862
863
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 664 times.
666 if (s->have_mdcv) {
864 AVMasteringDisplayMetadata *mdcv;
865
866 2 ret = ff_decode_mastering_display_new(avctx, frame, &mdcv);
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
868 return ret;
869
870
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (mdcv) {
871 2 mdcv->has_primaries = 1;
872
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++) {
873 6 mdcv->display_primaries[i][0] = av_make_q(s->mdcv_primaries[i][0], 50000);
874 6 mdcv->display_primaries[i][1] = av_make_q(s->mdcv_primaries[i][1], 50000);
875 }
876 2 mdcv->white_point[0] = av_make_q(s->mdcv_white_point[0], 50000);
877 2 mdcv->white_point[1] = av_make_q(s->mdcv_white_point[1], 50000);
878 2 mdcv->has_luminance = 1;
879 2 mdcv->max_luminance = av_make_q(s->mdcv_max_lum, 10000);
880 2 mdcv->min_luminance = av_make_q(s->mdcv_min_lum, 10000);
881 }
882 }
883
884 666 return 0;
885 }
886
887 12453 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
888 GetByteContext *gb, AVFrame *p)
889 {
890 int ret;
891
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 10917 times.
12453 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
892
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12453 times.
12453 if (!p)
894 return AVERROR_INVALIDDATA;
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12453 times.
12453 if (!(s->hdr_state & PNG_IHDR)) {
896 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
897 return AVERROR_INVALIDDATA;
898 }
899
2/2
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 11787 times.
12453 if (!(s->pic_state & PNG_IDAT)) {
900 /* init image info */
901 666 ret = ff_set_dimensions(avctx, s->width, s->height);
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (ret < 0)
903 return ret;
904
905 666 s->channels = ff_png_get_nb_channels(s->color_type);
906 666 s->bits_per_pixel = s->bit_depth * s->channels;
907 666 s->bpp = (s->bits_per_pixel + 7) >> 3;
908 666 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
909
910
4/6
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 626 times.
✓ Branch 5 taken 40 times.
666 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
911
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 145 times.
626 s->color_type == PNG_COLOR_TYPE_RGB) {
912 481 avctx->pix_fmt = AV_PIX_FMT_RGB24;
913
4/6
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 145 times.
✓ Branch 5 taken 40 times.
185 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
914
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 48 times.
145 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
915 97 avctx->pix_fmt = AV_PIX_FMT_RGBA;
916
4/6
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 40 times.
88 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
917
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 44 times.
48 s->color_type == PNG_COLOR_TYPE_GRAY) {
918 4 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
919
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 44 times.
84 } else if (s->bit_depth == 16 &&
920
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 24 times.
40 s->color_type == PNG_COLOR_TYPE_GRAY) {
921 16 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
922
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 44 times.
68 } else if (s->bit_depth == 16 &&
923
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 s->color_type == PNG_COLOR_TYPE_RGB) {
924 18 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
925
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 44 times.
50 } else if (s->bit_depth == 16 &&
926
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
927 4 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
928
5/8
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 42 times.
✓ Branch 7 taken 4 times.
46 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
929
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 s->color_type == PNG_COLOR_TYPE_PALETTE) {
930
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 avctx->pix_fmt = avctx->codec_id == AV_CODEC_ID_APNG ? AV_PIX_FMT_RGBA : AV_PIX_FMT_PAL8;
931
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
932 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
933
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (s->bit_depth == 8 &&
934
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
935 2 avctx->pix_fmt = AV_PIX_FMT_YA8;
936
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (s->bit_depth == 16 &&
937
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
938 2 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
939 } else {
940 avpriv_report_missing_feature(avctx,
941 "Bit depth %d color type %d",
942 s->bit_depth, s->color_type);
943 return AVERROR_PATCHWELCOME;
944 }
945
946
4/4
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 487 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 42 times.
666 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
947
1/5
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
137 switch (avctx->pix_fmt) {
948 137 case AV_PIX_FMT_RGB24:
949 137 avctx->pix_fmt = AV_PIX_FMT_RGBA;
950 137 break;
951
952 case AV_PIX_FMT_RGB48BE:
953 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
954 break;
955
956 case AV_PIX_FMT_GRAY8:
957 avctx->pix_fmt = AV_PIX_FMT_YA8;
958 break;
959
960 case AV_PIX_FMT_GRAY16BE:
961 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
962 break;
963
964 default:
965 avpriv_request_sample(avctx, "bit depth %d "
966 "and color type %d with TRNS",
967 s->bit_depth, s->color_type);
968 return AVERROR_INVALIDDATA;
969 }
970
971 137 s->bpp += byte_depth;
972 }
973
974 /* PNG spec mandates independent alpha channel */
975
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 101 times.
666 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
976
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 561 times.
565 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
977 105 avctx->alpha_mode = AVALPHA_MODE_STRAIGHT;
978
979 666 ff_progress_frame_unref(&s->picture);
980
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 665 times.
666 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
981 /* We only need a buffer for the current picture. */
982 1 ret = ff_thread_get_buffer(avctx, p, 0);
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
984 return ret;
985
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 653 times.
665 } else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
986 /* We need a buffer for the current picture as well as
987 * a buffer for the reference to retain. */
988 12 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
989 AV_GET_BUFFER_FLAG_REF);
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
991 return ret;
992 12 ret = ff_thread_get_buffer(avctx, p, 0);
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
994 return ret;
995 } else {
996 /* The picture output this time and the reference to retain coincide. */
997 653 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
998 AV_GET_BUFFER_FLAG_REF);
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1000 return ret;
1001 653 ret = av_frame_ref(p, s->picture.f);
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1003 return ret;
1004 }
1005
1006 666 p->pict_type = AV_PICTURE_TYPE_I;
1007 666 p->flags |= AV_FRAME_FLAG_KEY;
1008
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 658 times.
666 p->flags |= AV_FRAME_FLAG_INTERLACED * !!s->interlace_type;
1009
1010
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
666 if ((ret = populate_avctx_color_fields(avctx, p)) < 0)
1011 return ret;
1012 666 ff_thread_finish_setup(avctx);
1013
1014 /* compute the compressed row size */
1015
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 8 times.
666 if (!s->interlace_type) {
1016 658 s->crow_size = s->row_size + 1;
1017 } else {
1018 8 s->pass = 0;
1019 8 s->pass_row_size = ff_png_pass_row_size(s->pass,
1020 s->bits_per_pixel,
1021 s->cur_w);
1022 8 s->crow_size = s->pass_row_size + 1;
1023 }
1024 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
1025 s->row_size, s->crow_size);
1026
1027 /* copy the palette if needed */
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
1029 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
1030 /* empty row is used if differencing to the first row */
1031 666 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (!s->last_row)
1033 return AVERROR_INVALIDDATA;
1034
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 8 times.
666 if (s->interlace_type ||
1035
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 557 times.
658 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
1036 109 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (!s->tmp_row)
1038 return AVERROR_INVALIDDATA;
1039 }
1040 /* compressed row */
1041 666 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
1042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (!s->buffer)
1043 return AVERROR(ENOMEM);
1044
1045 /* we want crow_buf+1 to be 16-byte aligned */
1046 666 s->crow_buf = s->buffer + 15;
1047 666 s->zstream.zstream.avail_out = s->crow_size;
1048 666 s->zstream.zstream.next_out = s->crow_buf;
1049 }
1050
1051 12453 s->pic_state |= PNG_IDAT;
1052
1053 /* set image to non-transparent bpp while decompressing */
1054
4/4
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 12264 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 42 times.
12453 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
1055 147 s->bpp -= byte_depth;
1056
1057 12453 ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
1058
1059
4/4
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 12264 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 42 times.
12453 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
1060 147 s->bpp += byte_depth;
1061
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12453 times.
12453 if (ret < 0)
1063 return ret;
1064
1065 12453 return 0;
1066 }
1067
1068 3 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
1069 GetByteContext *gb)
1070 {
1071 3 int length = bytestream2_get_bytes_left(gb);
1072 int n, i, r, g, b;
1073
1074
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((length % 3) != 0 || length > 256 * 3)
1075 return AVERROR_INVALIDDATA;
1076 /* read the palette */
1077 3 n = length / 3;
1078
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 3 times.
711 for (i = 0; i < n; i++) {
1079 708 r = bytestream2_get_byte(gb);
1080 708 g = bytestream2_get_byte(gb);
1081 708 b = bytestream2_get_byte(gb);
1082 708 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
1083 }
1084
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3 times.
63 for (; i < 256; i++)
1085 60 s->palette[i] = (0xFFU << 24);
1086 3 s->hdr_state |= PNG_PLTE;
1087
1088 3 return 0;
1089 }
1090
1091 7 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
1092 GetByteContext *gb)
1093 {
1094 7 int length = bytestream2_get_bytes_left(gb);
1095 int v, i;
1096
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!(s->hdr_state & PNG_IHDR)) {
1098 av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
1099 return AVERROR_INVALIDDATA;
1100 }
1101
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (s->pic_state & PNG_IDAT) {
1103 av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
1104 return AVERROR_INVALIDDATA;
1105 }
1106
1107
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1108
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (length > 256 || !(s->hdr_state & PNG_PLTE))
1109 return AVERROR_INVALIDDATA;
1110
1111
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 3 times.
54 for (i = 0; i < length; i++) {
1112 51 unsigned v = bytestream2_get_byte(gb);
1113 51 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
1114 }
1115
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
1116
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
1117
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 s->bit_depth == 1)
1119 return AVERROR_INVALIDDATA;
1120
1121
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < length / 2; i++) {
1122 /* only use the least significant bits */
1123 12 v = av_zero_extend(bytestream2_get_be16(gb), s->bit_depth);
1124
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->bit_depth > 8)
1126 AV_WB16(&s->transparent_color_be[2 * i], v);
1127 else
1128 12 s->transparent_color_be[i] = v;
1129 }
1130 } else {
1131 return AVERROR_INVALIDDATA;
1132 }
1133
1134 7 s->has_trns = 1;
1135
1136 7 return 0;
1137 }
1138
1139 6 static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb)
1140 {
1141 6 int ret, cnt = 0;
1142 AVBPrint bp;
1143
1144
3/4
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 126 times.
✗ Branch 4 not taken.
132 while ((s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cnt > 80) {
1146 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
1147 ret = AVERROR_INVALIDDATA;
1148 goto fail;
1149 }
1150
1151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_byte(gb) != 0) {
1152 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
1153 ret = AVERROR_INVALIDDATA;
1154 goto fail;
1155 }
1156
1157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx)) < 0)
1158 return ret;
1159
1160 6 av_freep(&s->iccp_data);
1161 6 ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1163 return ret;
1164 6 s->iccp_data_len = bp.len;
1165
1166 6 return 0;
1167 fail:
1168 s->iccp_name[0] = 0;
1169 return ret;
1170 }
1171
1172 static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s,
1173 GetByteContext *gb)
1174 {
1175 int bits = 0;
1176 int channels;
1177 int remainder = bytestream2_get_bytes_left(gb);
1178
1179 if (!(s->hdr_state & PNG_IHDR)) {
1180 av_log(avctx, AV_LOG_ERROR, "sBIT before IHDR\n");
1181 return AVERROR_INVALIDDATA;
1182 }
1183
1184 if (s->pic_state & PNG_IDAT) {
1185 av_log(avctx, AV_LOG_WARNING, "Ignoring illegal sBIT chunk after IDAT\n");
1186 return 0;
1187 }
1188
1189 channels = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type);
1190
1191 if (remainder != channels) {
1192 av_log(avctx, AV_LOG_WARNING, "Invalid sBIT size: %d, expected: %d\n", remainder, channels);
1193 /* not enough space left in chunk to read info */
1194 if (remainder < channels)
1195 return 0;
1196 }
1197
1198 for (int i = 0; i < channels; i++) {
1199 int b = bytestream2_get_byteu(gb);
1200 bits = FFMAX(b, bits);
1201 }
1202
1203 if (bits <= 0 || bits > (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) {
1204 av_log(avctx, AV_LOG_WARNING, "Invalid significant bits: %d\n", bits);
1205 return 0;
1206 }
1207 s->significant_bits = bits;
1208
1209 return 0;
1210 }
1211
1212 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
1213 {
1214 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
1215 int i, j, k;
1216 uint8_t *pd = p->data[0];
1217 for (j = 0; j < s->height; j++) {
1218 i = s->width / 8;
1219 for (k = 7; k >= 1; k--)
1220 if ((s->width&7) >= k)
1221 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
1222 for (i--; i >= 0; i--) {
1223 pd[8*i + 7]= pd[i] & 1;
1224 pd[8*i + 6]= (pd[i]>>1) & 1;
1225 pd[8*i + 5]= (pd[i]>>2) & 1;
1226 pd[8*i + 4]= (pd[i]>>3) & 1;
1227 pd[8*i + 3]= (pd[i]>>4) & 1;
1228 pd[8*i + 2]= (pd[i]>>5) & 1;
1229 pd[8*i + 1]= (pd[i]>>6) & 1;
1230 pd[8*i + 0]= pd[i]>>7;
1231 }
1232 pd += p->linesize[0];
1233 }
1234 } else if (s->bits_per_pixel == 2) {
1235 int i, j;
1236 uint8_t *pd = p->data[0];
1237 for (j = 0; j < s->height; j++) {
1238 i = s->width / 4;
1239 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1240 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
1241 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
1242 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
1243 for (i--; i >= 0; i--) {
1244 pd[4*i + 3]= pd[i] & 3;
1245 pd[4*i + 2]= (pd[i]>>2) & 3;
1246 pd[4*i + 1]= (pd[i]>>4) & 3;
1247 pd[4*i + 0]= pd[i]>>6;
1248 }
1249 } else {
1250 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1251 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1252 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1253 for (i--; i >= 0; i--) {
1254 pd[4*i + 3]= ( pd[i] & 3)*0x55;
1255 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1256 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1257 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1258 }
1259 }
1260 pd += p->linesize[0];
1261 }
1262 } else if (s->bits_per_pixel == 4) {
1263 int i, j;
1264 uint8_t *pd = p->data[0];
1265 for (j = 0; j < s->height; j++) {
1266 i = s->width/2;
1267 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1268 if (s->width&1) pd[2*i+0]= pd[i]>>4;
1269 for (i--; i >= 0; i--) {
1270 pd[2*i + 1] = pd[i] & 15;
1271 pd[2*i + 0] = pd[i] >> 4;
1272 }
1273 } else {
1274 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
1275 for (i--; i >= 0; i--) {
1276 pd[2*i + 1] = (pd[i] & 15) * 0x11;
1277 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
1278 }
1279 }
1280 pd += p->linesize[0];
1281 }
1282 }
1283 }
1284
1285 262 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
1286 GetByteContext *gb)
1287 {
1288 uint32_t sequence_number;
1289 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
1290
1291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if (bytestream2_get_bytes_left(gb) != APNG_FCTL_CHUNK_SIZE)
1292 return AVERROR_INVALIDDATA;
1293
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->hdr_state & PNG_IHDR)) {
1295 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
1296 return AVERROR_INVALIDDATA;
1297 }
1298
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (s->pic_state & PNG_IDAT) {
1300 av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
1301 return AVERROR_INVALIDDATA;
1302 }
1303
1304 262 sequence_number = bytestream2_get_be32(gb);
1305 262 cur_w = bytestream2_get_be32(gb);
1306 262 cur_h = bytestream2_get_be32(gb);
1307 262 x_offset = bytestream2_get_be32(gb);
1308 262 y_offset = bytestream2_get_be32(gb);
1309 262 bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
1310 262 dispose_op = bytestream2_get_byte(gb);
1311 262 blend_op = bytestream2_get_byte(gb);
1312
1313
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 246 times.
262 if (sequence_number == 0 &&
1314
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 (cur_w != s->width ||
1315
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 cur_h != s->height ||
1316
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 x_offset != 0 ||
1317
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 y_offset != 0) ||
1318
2/4
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
✗ Branch 3 not taken.
262 cur_w <= 0 || cur_h <= 0 ||
1319
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 x_offset < 0 || y_offset < 0 ||
1320
2/4
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 262 times.
262 cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
1321 return AVERROR_INVALIDDATA;
1322
1323
3/4
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 219 times.
262 if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
1324 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1325 return AVERROR_INVALIDDATA;
1326 }
1327
1328
4/6
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 246 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
262 if ((sequence_number == 0 || !s->last_picture.f) &&
1329 dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1330 // No previous frame to revert to for the first frame
1331 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1332 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1333 }
1334
1335
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 39 times.
262 if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
1336
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1337
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1338
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1339
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1341 )) {
1342 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1343 blend_op = APNG_BLEND_OP_SOURCE;
1344 }
1345
1346 262 s->cur_w = cur_w;
1347 262 s->cur_h = cur_h;
1348 262 s->x_offset = x_offset;
1349 262 s->y_offset = y_offset;
1350 262 s->dispose_op = dispose_op;
1351 262 s->blend_op = blend_op;
1352
1353 262 return 0;
1354 }
1355
1356 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
1357 {
1358 int i, j;
1359 uint8_t *pd = p->data[0];
1360 uint8_t *pd_last = s->last_picture.f->data[0];
1361 int ls = av_image_get_linesize(p->format, s->width, 0);
1362
1363 ls = FFMIN(ls, s->width * s->bpp);
1364
1365 ff_progress_frame_await(&s->last_picture, INT_MAX);
1366 for (j = 0; j < s->height; j++) {
1367 for (i = 0; i < ls; i++)
1368 pd[i] += pd_last[i];
1369 pd += p->linesize[0];
1370 pd_last += s->last_picture.f->linesize[0];
1371 }
1372 }
1373
1374 // divide by 255 and round to nearest
1375 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1376 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1377
1378 61 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
1379 AVFrame *p)
1380 {
1381 61 uint8_t *dst = p->data[0];
1382 61 ptrdiff_t dst_stride = p->linesize[0];
1383 61 const uint8_t *src = s->last_picture.f->data[0];
1384 61 ptrdiff_t src_stride = s->last_picture.f->linesize[0];
1385
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 39 times.
61 const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
1386
1387 size_t x, y;
1388
1389
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER &&
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1391 avctx->pix_fmt != AV_PIX_FMT_GRAY8A) {
1392 avpriv_request_sample(avctx, "Blending with pixel format %s",
1393 av_get_pix_fmt_name(avctx->pix_fmt));
1394 return AVERROR_PATCHWELCOME;
1395 }
1396
1397 61 ff_progress_frame_await(&s->last_picture, INT_MAX);
1398
1399 // copy unchanged rectangles from the last frame
1400
2/2
✓ Branch 0 taken 2186 times.
✓ Branch 1 taken 61 times.
2247 for (y = 0; y < s->y_offset; y++)
1401 2186 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1402
2/2
✓ Branch 0 taken 3320 times.
✓ Branch 1 taken 61 times.
3381 for (y = s->y_offset; y < s->y_offset + s->cur_h; y++) {
1403 3320 memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * bpp);
1404 3320 memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * bpp,
1405 3320 src + y * src_stride + (s->x_offset + s->cur_w) * bpp,
1406 3320 (p->width - s->cur_w - s->x_offset) * bpp);
1407 }
1408
2/2
✓ Branch 0 taken 2500 times.
✓ Branch 1 taken 61 times.
2561 for (y = s->y_offset + s->cur_h; y < p->height; y++)
1409 2500 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1410
1411
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER) {
1412 // Perform blending
1413
2/2
✓ Branch 0 taken 1912 times.
✓ Branch 1 taken 41 times.
1953 for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
1414 1912 uint8_t *foreground = dst + dst_stride * y + bpp * s->x_offset;
1415 1912 const uint8_t *background = src + src_stride * y + bpp * s->x_offset;
1416
2/2
✓ Branch 0 taken 143265 times.
✓ Branch 1 taken 1912 times.
145177 for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += bpp, background += bpp) {
1417 size_t b;
1418 uint8_t foreground_alpha, background_alpha, output_alpha;
1419 uint8_t output[10];
1420
1421 // Since we might be blending alpha onto alpha, we use the following equations:
1422 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1423 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1424
1425
1/3
✓ Branch 0 taken 143265 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
143265 switch (avctx->pix_fmt) {
1426 143265 case AV_PIX_FMT_RGBA:
1427 143265 foreground_alpha = foreground[3];
1428 143265 background_alpha = background[3];
1429 143265 break;
1430
1431 case AV_PIX_FMT_GRAY8A:
1432 foreground_alpha = foreground[1];
1433 background_alpha = background[1];
1434 break;
1435 }
1436
1437
2/2
✓ Branch 0 taken 21682 times.
✓ Branch 1 taken 121583 times.
143265 if (foreground_alpha == 255)
1438 143265 continue;
1439
1440
1/2
✓ Branch 0 taken 121583 times.
✗ Branch 1 not taken.
121583 if (foreground_alpha == 0) {
1441 121583 memcpy(foreground, background, bpp);
1442 121583 continue;
1443 }
1444
1445 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1446
1447 av_assert0(bpp <= 10);
1448
1449 for (b = 0; b < bpp - 1; ++b) {
1450 if (output_alpha == 0) {
1451 output[b] = 0;
1452 } else if (background_alpha == 255) {
1453 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1454 } else {
1455 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1456 }
1457 }
1458 output[b] = output_alpha;
1459 memcpy(foreground, output, bpp);
1460 }
1461 }
1462 }
1463
1464 61 return 0;
1465 }
1466
1467 12 static void apng_reset_background(PNGDecContext *s, const AVFrame *p)
1468 {
1469 // need to reset a rectangle to black
1470 12 av_unused int ret = av_frame_copy(s->picture.f, p);
1471
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
1472 12 const ptrdiff_t dst_stride = s->picture.f->linesize[0];
1473 12 uint8_t *dst = s->picture.f->data[0] + s->y_offset * dst_stride + bpp * s->x_offset;
1474
1475 av_assert1(ret >= 0);
1476
1477
2/2
✓ Branch 0 taken 959 times.
✓ Branch 1 taken 12 times.
971 for (size_t y = 0; y < s->cur_h; y++) {
1478 959 memset(dst, 0, bpp * s->cur_w);
1479 959 dst += dst_stride;
1480 }
1481 12 }
1482
1483 683 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1484 AVFrame *p, const AVPacket *avpkt)
1485 {
1486 683 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1487 uint32_t tag, length;
1488 683 int decode_next_dat = 0;
1489 int i, ret;
1490
1491 13992 for (;;) {
1492 GetByteContext gb_chunk;
1493
1494 14675 length = bytestream2_get_bytes_left(&s->gb);
1495
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 14318 times.
14675 if (length <= 0) {
1496
1497
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 278 times.
357 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1498
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 avctx->skip_frame == AVDISCARD_ALL) {
1499 95 return 0;
1500 }
1501
1502
2/4
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
278 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1503
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 262 times.
278 if (!(s->pic_state & PNG_IDAT))
1504 16 return 0;
1505 else
1506 587 goto exit_loop;
1507 }
1508 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1509 if ( s->pic_state & PNG_ALLIMAGE
1510 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1511 goto exit_loop;
1512 ret = AVERROR_INVALIDDATA;
1513 1 goto fail;
1514 }
1515
1516 14318 length = bytestream2_get_be32(&s->gb);
1517
3/4
✓ Branch 0 taken 14318 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 14317 times.
14318 if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
1518 1 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1519 1 ret = AVERROR_INVALIDDATA;
1520 1 goto fail;
1521 }
1522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14317 times.
14317 if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1523 uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1524 uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1525 if (crc_sig ^ crc_cal) {
1526 av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1527 if (avctx->err_recognition & AV_EF_EXPLODE) {
1528 av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1529 ret = AVERROR_INVALIDDATA;
1530 goto fail;
1531 }
1532 av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1533 bytestream2_skip(&s->gb, length + 8); /* tag */
1534 143 continue;
1535 }
1536 }
1537 14317 tag = bytestream2_get_le32(&s->gb);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14317 times.
14317 if (avctx->debug & FF_DEBUG_STARTCODE)
1539 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1540 av_fourcc2str(tag), length);
1541
1542 14317 bytestream2_init(&gb_chunk, s->gb.buffer, length);
1543 14317 bytestream2_skip(&s->gb, length + 4);
1544
1545
2/2
✓ Branch 0 taken 11748 times.
✓ Branch 1 taken 2569 times.
14317 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1546
2/2
✓ Branch 0 taken 1525 times.
✓ Branch 1 taken 10223 times.
11748 avctx->skip_frame == AVDISCARD_ALL) {
1547
2/2
✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 143 times.
1525 switch(tag) {
1548 1382 case MKTAG('I', 'H', 'D', 'R'):
1549 case MKTAG('p', 'H', 'Y', 's'):
1550 case MKTAG('t', 'E', 'X', 't'):
1551 case MKTAG('I', 'D', 'A', 'T'):
1552 case MKTAG('t', 'R', 'N', 'S'):
1553 case MKTAG('s', 'R', 'G', 'B'):
1554 case MKTAG('c', 'I', 'C', 'P'):
1555 case MKTAG('c', 'H', 'R', 'M'):
1556 case MKTAG('g', 'A', 'M', 'A'):
1557 1382 break;
1558 143 default:
1559 143 continue;
1560 }
1561 }
1562
1563
18/21
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 262 times.
✓ Branch 3 taken 2054 times.
✓ Branch 4 taken 10399 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 138 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 39 times.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 34 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 325 times.
✓ Branch 20 taken 72 times.
14174 switch (tag) {
1564 421 case MKTAG('I', 'H', 'D', 'R'):
1565
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
1566 goto fail;
1567 421 break;
1568 388 case MKTAG('p', 'H', 'Y', 's'):
1569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
1570 goto fail;
1571 388 break;
1572 262 case MKTAG('f', 'c', 'T', 'L'):
1573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1574 continue;
1575
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
1576 goto fail;
1577 262 decode_next_dat = 1;
1578 262 break;
1579 2054 case MKTAG('f', 'd', 'A', 'T'):
1580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2054 times.
2054 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1581 continue;
1582
2/4
✓ Branch 0 taken 2054 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2054 times.
2054 if (!decode_next_dat || bytestream2_get_bytes_left(&gb_chunk) < 4) {
1583 ret = AVERROR_INVALIDDATA;
1584 goto fail;
1585 }
1586 2054 bytestream2_get_be32(&gb_chunk);
1587 av_fallthrough;
1588 12453 case MKTAG('I', 'D', 'A', 'T'):
1589
3/4
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 10193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2260 times.
12453 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1590 continue;
1591
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12453 times.
12453 if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
1592 goto fail;
1593 12453 break;
1594 3 case MKTAG('P', 'L', 'T', 'E'):
1595 3 decode_plte_chunk(avctx, s, &gb_chunk);
1596 3 break;
1597 7 case MKTAG('t', 'R', 'N', 'S'):
1598 7 decode_trns_chunk(avctx, s, &gb_chunk);
1599 7 break;
1600 138 case MKTAG('t', 'E', 'X', 't'):
1601
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
138 if (decode_text_chunk(s, &gb_chunk, 0) < 0)
1602 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1603 138 break;
1604 case MKTAG('z', 'T', 'X', 't'):
1605 if (decode_text_chunk(s, &gb_chunk, 1) < 0)
1606 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1607 break;
1608 case MKTAG('s', 'T', 'E', 'R'): {
1609 int mode = bytestream2_get_byte(&gb_chunk);
1610
1611 if (mode == 0 || mode == 1) {
1612 s->stereo_mode = mode;
1613 } else {
1614 av_log(avctx, AV_LOG_WARNING,
1615 "Unknown value in sTER chunk (%d)\n", mode);
1616 }
1617 break;
1618 }
1619 4 case MKTAG('c', 'I', 'C', 'P'):
1620 4 s->cicp_primaries = bytestream2_get_byte(&gb_chunk);
1621 4 s->cicp_trc = bytestream2_get_byte(&gb_chunk);
1622
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_byte(&gb_chunk) != 0)
1623 av_log(avctx, AV_LOG_WARNING, "nonzero cICP matrix\n");
1624 4 s->cicp_range = bytestream2_get_byte(&gb_chunk);
1625
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (s->cicp_range != 0 && s->cicp_range != 1)
1626 av_log(avctx, AV_LOG_WARNING, "invalid cICP range: %d\n", s->cicp_range);
1627 4 s->have_cicp = 1;
1628 4 break;
1629 39 case MKTAG('s', 'R', 'G', 'B'):
1630 /* skip rendering intent byte */
1631 39 bytestream2_skip(&gb_chunk, 1);
1632 39 s->have_srgb = 1;
1633 39 break;
1634 6 case MKTAG('i', 'C', 'C', 'P'): {
1635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = decode_iccp_chunk(s, &gb_chunk)) < 0)
1636 goto fail;
1637 6 break;
1638 }
1639 16 case MKTAG('c', 'H', 'R', 'M'): {
1640 16 s->have_chrm = 1;
1641
1642 16 s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1643 16 s->white_point[1] = bytestream2_get_be32(&gb_chunk);
1644
1645 /* RGB Primaries */
1646
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 for (i = 0; i < 3; i++) {
1647 48 s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
1648 48 s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
1649 }
1650
1651 16 break;
1652 }
1653 case MKTAG('s', 'B', 'I', 'T'):
1654 if ((ret = decode_sbit_chunk(avctx, s, &gb_chunk)) < 0)
1655 goto fail;
1656 break;
1657 34 case MKTAG('g', 'A', 'M', 'A'): {
1658 AVBPrint bp;
1659 char *gamma_str;
1660 34 s->gamma = bytestream2_get_be32(&gb_chunk);
1661
1662 34 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1663 34 av_bprintf(&bp, "%i/%i", s->gamma, 100000);
1664 34 ret = av_bprint_finalize(&bp, &gamma_str);
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ret < 0)
1666 return ret;
1667
1668 34 av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1669
1670 34 break;
1671 }
1672 2 case MKTAG('c', 'L', 'L', 'i'): /* legacy spelling, for backwards compat */
1673 case MKTAG('c', 'L', 'L', 'I'):
1674
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&gb_chunk) != 8) {
1675 av_log(avctx, AV_LOG_WARNING, "Invalid cLLI chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1676 break;
1677 }
1678 2 s->have_clli = 1;
1679 2 s->clli_max = bytestream2_get_be32u(&gb_chunk);
1680 2 s->clli_avg = bytestream2_get_be32u(&gb_chunk);
1681 2 break;
1682 2 case MKTAG('m', 'D', 'C', 'v'): /* legacy spelling, for backward compat */
1683 case MKTAG('m', 'D', 'C', 'V'):
1684
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&gb_chunk) != 24) {
1685 av_log(avctx, AV_LOG_WARNING, "Invalid mDCV chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1686 break;
1687 }
1688 2 s->have_mdcv = 1;
1689
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++) {
1690 6 s->mdcv_primaries[i][0] = bytestream2_get_be16u(&gb_chunk);
1691 6 s->mdcv_primaries[i][1] = bytestream2_get_be16u(&gb_chunk);
1692 }
1693 2 s->mdcv_white_point[0] = bytestream2_get_be16u(&gb_chunk);
1694 2 s->mdcv_white_point[1] = bytestream2_get_be16u(&gb_chunk);
1695 2 s->mdcv_max_lum = bytestream2_get_be32u(&gb_chunk);
1696 2 s->mdcv_min_lum = bytestream2_get_be32u(&gb_chunk);
1697 2 break;
1698 2 case MKTAG('e', 'X', 'I', 'f'):
1699 2 ret = decode_exif_chunk(avctx, s, &gb_chunk);
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1701 goto fail;
1702 2 break;
1703 325 case MKTAG('I', 'E', 'N', 'D'):
1704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (!(s->pic_state & PNG_ALLIMAGE))
1705 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1707 ret = AVERROR_INVALIDDATA;
1708 goto fail;
1709 }
1710 325 goto exit_loop;
1711 }
1712 }
1713 587 exit_loop:
1714
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!p)
1716 return AVERROR_INVALIDDATA;
1717
1718
2/2
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 262 times.
587 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 avctx->skip_frame == AVDISCARD_ALL) {
1720 return 0;
1721 }
1722
1723
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 587 times.
587 if (percent_missing(s) > avctx->discard_damaged_percentage) {
1724 ret = AVERROR_INVALIDDATA;
1725 goto fail;
1726 }
1727
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (s->bits_per_pixel <= 4)
1729 handle_small_bpp(s, p);
1730
1731
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 585 times.
587 if (s->exif_data) {
1732 // we swap because ff_decode_exif_attach_buffer adds to p->metadata
1733 2 FFSWAP(AVDictionary *, p->metadata, s->frame_metadata);
1734 2 ret = ff_decode_exif_attach_buffer(avctx, p, &s->exif_data, AV_EXIF_TIFF_HEADER);
1735 2 FFSWAP(AVDictionary *, p->metadata, s->frame_metadata);
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1737 av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF buffer\n");
1738 return ret;
1739 }
1740 }
1741
1742
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 545 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
587 if (s->color_type == PNG_COLOR_TYPE_PALETTE && avctx->codec_id == AV_CODEC_ID_APNG) {
1743
2/2
✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 42 times.
6342 for (int y = 0; y < s->height; y++) {
1744 6300 uint8_t *row = &p->data[0][p->linesize[0] * y];
1745
1746
2/2
✓ Branch 0 taken 945000 times.
✓ Branch 1 taken 6300 times.
951300 for (int x = s->width - 1; x >= 0; x--) {
1747 945000 const uint8_t idx = row[x];
1748
1749 945000 row[4*x+2] = s->palette[idx] & 0xFF;
1750 945000 row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF;
1751 945000 row[4*x+0] = (s->palette[idx] >> 16) & 0xFF;
1752 945000 row[4*x+3] = s->palette[idx] >> 24;
1753 }
1754 }
1755 }
1756
1757 /* apply transparency if needed */
1758
4/4
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 409 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 42 times.
587 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1760 136 size_t raw_bpp = s->bpp - byte_depth;
1761 ptrdiff_t x, y;
1762
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 av_assert0(s->bit_depth > 1);
1764
1765
2/2
✓ Branch 0 taken 17610 times.
✓ Branch 1 taken 136 times.
17746 for (y = 0; y < s->height; ++y) {
1766 17610 uint8_t *row = &p->data[0][p->linesize[0] * y];
1767
1768
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17610 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17610 if (s->bpp == 2 && byte_depth == 1) {
1769 uint8_t *pixel = &row[2 * s->width - 1];
1770 uint8_t *rowp = &row[1 * s->width - 1];
1771 int tcolor = s->transparent_color_be[0];
1772 for (x = s->width; x > 0; --x) {
1773 *pixel-- = *rowp == tcolor ? 0 : 0xff;
1774 *pixel-- = *rowp--;
1775 }
1776
2/4
✓ Branch 0 taken 17610 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17610 times.
✗ Branch 3 not taken.
35220 } else if (s->bpp == 4 && byte_depth == 1) {
1777 17610 uint8_t *pixel = &row[4 * s->width - 1];
1778 17610 uint8_t *rowp = &row[3 * s->width - 1];
1779 17610 int tcolor = AV_RL24(s->transparent_color_be);
1780
2/2
✓ Branch 0 taken 1577700 times.
✓ Branch 1 taken 17610 times.
1595310 for (x = s->width; x > 0; --x) {
1781
2/2
✓ Branch 0 taken 82906 times.
✓ Branch 1 taken 1494794 times.
1577700 *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
1782 1577700 *pixel-- = *rowp--;
1783 1577700 *pixel-- = *rowp--;
1784 1577700 *pixel-- = *rowp--;
1785 }
1786 } else {
1787 /* since we're updating in-place, we have to go from right to left */
1788 for (x = s->width; x > 0; --x) {
1789 uint8_t *pixel = &row[s->bpp * (x - 1)];
1790 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1791
1792 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1793 memset(&pixel[raw_bpp], 0, byte_depth);
1794 } else {
1795 memset(&pixel[raw_bpp], 0xff, byte_depth);
1796 }
1797 }
1798 }
1799 }
1800 }
1801
1802 /* handle P-frames only if a predecessor frame is available */
1803
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 81 times.
587 if (s->last_picture.f) {
1804
3/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 445 times.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
506 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1805
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->width == p->width
1806
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->height== p->height
1807
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->format== p->format
1808 ) {
1809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1810 handle_p_frame_png(s, p);
1811 61 else if (CONFIG_APNG_DECODER &&
1812
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
122 avctx->codec_id == AV_CODEC_ID_APNG &&
1813 61 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1814 goto fail;
1815 }
1816 }
1817
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 575 times.
587 if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1818 12 apng_reset_background(s, p);
1819
1820 587 ret = 0;
1821 588 fail:
1822
2/2
✓ Branch 0 taken 586 times.
✓ Branch 1 taken 2 times.
588 if (s->picture.f)
1823 586 ff_progress_frame_report(&s->picture, INT_MAX);
1824
1825 588 return ret;
1826 }
1827
1828 667 static void clear_frame_metadata(PNGDecContext *s)
1829 {
1830 667 av_freep(&s->iccp_data);
1831 667 s->iccp_data_len = 0;
1832 667 s->iccp_name[0] = 0;
1833
1834 667 s->stereo_mode = -1;
1835
1836 667 s->have_chrm = 0;
1837 667 s->have_srgb = 0;
1838 667 s->have_cicp = 0;
1839
1840 667 av_dict_free(&s->frame_metadata);
1841 667 }
1842
1843 587 static int output_frame(PNGDecContext *s, AVFrame *f)
1844 {
1845 int ret;
1846
1847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (s->stereo_mode >= 0) {
1848 AVStereo3D *stereo3d = av_stereo3d_create_side_data(f);
1849 if (!stereo3d) {
1850 ret = AVERROR(ENOMEM);
1851 goto fail;
1852 }
1853
1854 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1855 stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1856 }
1857
1858 587 FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
1859
1860 587 return 0;
1861 fail:
1862 av_frame_unref(f);
1863 return ret;
1864 }
1865
1866 #if CONFIG_PNG_DECODER
1867 405 static int decode_frame_png(AVCodecContext *avctx, AVFrame *p,
1868 int *got_frame, AVPacket *avpkt)
1869 {
1870 405 PNGDecContext *const s = avctx->priv_data;
1871 405 const uint8_t *buf = avpkt->data;
1872 405 int buf_size = avpkt->size;
1873 int64_t sig;
1874 int ret;
1875
1876 405 clear_frame_metadata(s);
1877
1878 405 bytestream2_init(&s->gb, buf, buf_size);
1879
1880 /* check signature */
1881 405 sig = bytestream2_get_be64(&s->gb);
1882
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
405 if (sig != PNGSIG &&
1883 sig != MNGSIG) {
1884 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1885 return AVERROR_INVALIDDATA;
1886 }
1887
1888 405 s->y = s->has_trns = 0;
1889 405 s->hdr_state = 0;
1890 405 s->pic_state = 0;
1891
1892 /* Reset z_stream */
1893 405 ret = inflateReset(&s->zstream.zstream);
1894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret != Z_OK)
1895 return AVERROR_EXTERNAL;
1896
1897
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 404 times.
405 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1898 1 goto the_end;
1899
1900
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 325 times.
404 if (avctx->skip_frame == AVDISCARD_ALL) {
1901 79 *got_frame = 0;
1902 79 ret = bytestream2_tell(&s->gb);
1903 79 goto the_end;
1904 }
1905
1906 325 ret = output_frame(s, p);
1907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (ret < 0)
1908 goto the_end;
1909
1910
1/2
✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
325 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1911 325 ff_progress_frame_unref(&s->last_picture);
1912 325 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1913 }
1914
1915 325 *got_frame = 1;
1916
1917 325 ret = bytestream2_tell(&s->gb);
1918 405 the_end:
1919 405 s->crow_buf = NULL;
1920 405 return ret;
1921 }
1922 #endif
1923
1924 #if CONFIG_APNG_DECODER
1925 262 static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p,
1926 int *got_frame, AVPacket *avpkt)
1927 {
1928 262 PNGDecContext *const s = avctx->priv_data;
1929 int ret;
1930
1931 262 clear_frame_metadata(s);
1932
1933
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 246 times.
262 if (!(s->hdr_state & PNG_IHDR)) {
1934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!avctx->extradata_size)
1935 return AVERROR_INVALIDDATA;
1936
1937
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1938 return AVERROR_EXTERNAL;
1939 16 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0)
1941 return ret;
1942 }
1943
1944 /* reset state for a new frame */
1945
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1946 return AVERROR_EXTERNAL;
1947 262 s->y = 0;
1948 262 s->pic_state = 0;
1949 262 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1950
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1951 return ret;
1952
1953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->pic_state & PNG_ALLIMAGE))
1954 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT)))
1956 return AVERROR_INVALIDDATA;
1957
1958 262 ret = output_frame(s, p);
1959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (ret < 0)
1960 return ret;
1961
1962
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1963
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 1 times.
262 if (s->dispose_op != APNG_DISPOSE_OP_PREVIOUS)
1964 261 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1965 262 ff_progress_frame_unref(&s->picture);
1966 }
1967
1968 262 *got_frame = 1;
1969 262 return bytestream2_tell(&s->gb);
1970 }
1971 #endif
1972
1973 #if HAVE_THREADS
1974 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1975 {
1976 PNGDecContext *psrc = src->priv_data;
1977 PNGDecContext *pdst = dst->priv_data;
1978 const ProgressFrame *src_frame;
1979
1980 if (dst == src)
1981 return 0;
1982
1983 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1984
1985 pdst->width = psrc->width;
1986 pdst->height = psrc->height;
1987 pdst->bit_depth = psrc->bit_depth;
1988 pdst->color_type = psrc->color_type;
1989 pdst->compression_type = psrc->compression_type;
1990 pdst->interlace_type = psrc->interlace_type;
1991 pdst->filter_type = psrc->filter_type;
1992 pdst->has_trns = psrc->has_trns;
1993 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1994
1995 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1996
1997 pdst->hdr_state |= psrc->hdr_state;
1998 }
1999
2000 src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
2001 &psrc->last_picture : &psrc->picture;
2002
2003 ff_progress_frame_replace(&pdst->last_picture, src_frame);
2004
2005 return 0;
2006 }
2007 #endif
2008
2009 165 static av_cold int png_dec_init(AVCodecContext *avctx)
2010 {
2011 165 PNGDecContext *s = avctx->priv_data;
2012
2013 165 s->avctx = avctx;
2014
2015 165 ff_pngdsp_init(&s->dsp);
2016
2017 165 return ff_inflate_init(&s->zstream, avctx);
2018 }
2019
2020 165 static av_cold int png_dec_end(AVCodecContext *avctx)
2021 {
2022 165 PNGDecContext *s = avctx->priv_data;
2023
2024 165 ff_progress_frame_unref(&s->last_picture);
2025 165 ff_progress_frame_unref(&s->picture);
2026 165 av_freep(&s->buffer);
2027 165 s->buffer_size = 0;
2028 165 av_freep(&s->last_row);
2029 165 s->last_row_size = 0;
2030 165 av_freep(&s->tmp_row);
2031 165 s->tmp_row_size = 0;
2032
2033 165 av_freep(&s->iccp_data);
2034 165 av_buffer_unref(&s->exif_data);
2035 165 av_dict_free(&s->frame_metadata);
2036 165 ff_inflate_end(&s->zstream);
2037
2038 165 return 0;
2039 }
2040
2041 #if CONFIG_APNG_DECODER
2042 const FFCodec ff_apng_decoder = {
2043 .p.name = "apng",
2044 CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
2045 .p.type = AVMEDIA_TYPE_VIDEO,
2046 .p.id = AV_CODEC_ID_APNG,
2047 .priv_data_size = sizeof(PNGDecContext),
2048 .init = png_dec_init,
2049 .close = png_dec_end,
2050 FF_CODEC_DECODE_CB(decode_frame_apng),
2051 UPDATE_THREAD_CONTEXT(update_thread_context),
2052 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2053 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
2054 FF_CODEC_CAP_USES_PROGRESSFRAMES |
2055 FF_CODEC_CAP_ICC_PROFILES,
2056 };
2057 #endif
2058
2059 #if CONFIG_PNG_DECODER
2060 const FFCodec ff_png_decoder = {
2061 .p.name = "png",
2062 CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
2063 .p.type = AVMEDIA_TYPE_VIDEO,
2064 .p.id = AV_CODEC_ID_PNG,
2065 .priv_data_size = sizeof(PNGDecContext),
2066 .init = png_dec_init,
2067 .close = png_dec_end,
2068 FF_CODEC_DECODE_CB(decode_frame_png),
2069 UPDATE_THREAD_CONTEXT(update_thread_context),
2070 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2071 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
2072 FF_CODEC_CAP_INIT_CLEANUP |
2073 FF_CODEC_CAP_USES_PROGRESSFRAMES |
2074 FF_CODEC_CAP_ICC_PROFILES,
2075 };
2076 #endif
2077