FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngdec.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 721 1192 60.5%
Functions: 26 34 76.5%
Branches: 478 903 52.9%

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