FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngdec.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 739 1194 61.9%
Functions: 27 34 79.4%
Branches: 488 907 53.8%

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 208257 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 99886367 times.
✓ Branch 1 taken 208257 times.
100094624 for (i = 0; i < w; i++) {
226 int a, b, c, p, pa, pb, pc;
227
228 99886367 a = dst[i - bpp];
229 99886367 b = top[i];
230 99886367 c = top[i - bpp];
231
232 99886367 p = b - c;
233 99886367 pc = a - c;
234
235 99886367 pa = abs(p);
236 99886367 pb = abs(pc);
237 99886367 pc = abs(p + pc);
238
239
4/4
✓ Branch 0 taken 58673587 times.
✓ Branch 1 taken 41212780 times.
✓ Branch 2 taken 51763099 times.
✓ Branch 3 taken 6910488 times.
99886367 if (pa <= pb && pa <= pc)
240 51763099 p = a;
241
2/2
✓ Branch 0 taken 37848745 times.
✓ Branch 1 taken 10274523 times.
48123268 else if (pb <= pc)
242 37848745 p = b;
243 else
244 10274523 p = c;
245 99886367 dst[i] = p + src[i];
246 }
247 208257 }
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 131568 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 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) {
293 4782 case PNG_FILTER_VALUE_NONE:
294 4782 memcpy(dst, src, size);
295 4782 break;
296 4768 case PNG_FILTER_VALUE_SUB:
297
2/2
✓ Branch 0 taken 17017 times.
✓ Branch 1 taken 4768 times.
21785 for (i = 0; i < bpp; i++)
298 17017 dst[i] = src[i];
299
2/2
✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 2113 times.
4768 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 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);
309 }
310 4768 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 108211 case PNG_FILTER_VALUE_PAETH:
323
2/2
✓ Branch 0 taken 335650 times.
✓ Branch 1 taken 108211 times.
443861 for (i = 0; i < bpp; i++) {
324 335650 p = last[i];
325 335650 dst[i] = p + src[i];
326 }
327
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) {
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 101700 times.
✓ Branch 1 taken 2423 times.
104123 int w = (bpp & 3) ? size - 3 : size;
331
332
1/2
✓ Branch 0 taken 104123 times.
✗ Branch 1 not taken.
104123 if (w > i) {
333 104123 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
334 104123 i = w;
335 }
336 }
337 108211 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
338 108211 break;
339 }
340 131568 }
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 587 static int percent_missing(PNGDecContext *s)
359 {
360
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 583 times.
587 if (s->interlace_type) {
361 4 return 100 - 100 * s->pass / (NB_PASSES - 1);
362 } else {
363 583 return 100 - 100 * s->y / s->cur_h;
364 }
365 }
366
367 /* process exactly one decompressed row */
368 127327 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 125407 times.
✓ Branch 1 taken 1920 times.
127327 if (!s->interlace_type) {
374 125407 ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
375
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 124749 times.
125407 if (s->y == 0)
376 658 last_row = s->last_row;
377 else
378 124749 last_row = ptr - dst_stride;
379
380 125407 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 125407 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
125407 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 125407 s->y++;
393
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 124749 times.
125407 if (s->y == s->cur_h) {
394 658 s->pic_state |= PNG_ALLIMAGE;
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 658 times.
658 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 127327 }
448
449 12454 static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
450 uint8_t *dst, ptrdiff_t dst_stride)
451 {
452 12454 z_stream *const zstream = &s->zstream.zstream;
453 int ret;
454 12454 zstream->avail_in = bytestream2_get_bytes_left(gb);
455 12454 zstream->next_in = gb->buffer;
456
457 /* decode one line if possible */
458
2/2
✓ Branch 0 taken 139083 times.
✓ Branch 1 taken 12454 times.
151537 while (zstream->avail_in > 0) {
459 139083 ret = inflate(zstream, Z_PARTIAL_FLUSH);
460
3/4
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 138417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
139083 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 127327 times.
✓ Branch 1 taken 11756 times.
139083 if (zstream->avail_out == 0) {
465
1/2
✓ Branch 0 taken 127327 times.
✗ Branch 1 not taken.
127327 if (!(s->pic_state & PNG_ALLIMAGE)) {
466 127327 png_handle_row(s, dst, dst_stride);
467 }
468 127327 zstream->avail_out = s->crow_size;
469 127327 zstream->next_out = s->crow_buf;
470 }
471
3/4
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 138417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
139083 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 12454 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 421 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
697 GetByteContext *gb)
698 {
699
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 if (bytestream2_get_bytes_left(gb) != 13)
700 return AVERROR_INVALIDDATA;
701
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 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 421 times.
421 if (s->hdr_state & PNG_IHDR) {
708 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
709 return AVERROR_INVALIDDATA;
710 }
711
712 421 s->width = s->cur_w = bytestream2_get_be32(gb);
713 421 s->height = s->cur_h = bytestream2_get_be32(gb);
714
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 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 421 s->bit_depth = bytestream2_get_byte(gb);
720
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 &&
721
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) {
722 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
723 goto error;
724 }
725 421 s->color_type = bytestream2_get_byte(gb);
726 421 s->compression_type = bytestream2_get_byte(gb);
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (s->compression_type) {
728 av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
729 goto error;
730 }
731 421 s->filter_type = bytestream2_get_byte(gb);
732 421 s->interlace_type = bytestream2_get_byte(gb);
733 421 s->hdr_state |= PNG_IHDR;
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 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 421 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 388 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
748 GetByteContext *gb)
749 {
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (s->pic_state & PNG_IDAT) {
751 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
752 return AVERROR_INVALIDDATA;
753 }
754 388 avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
755 388 avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
756
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)
757 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
758 388 bytestream2_skip(gb, 1); /* unit specifier */
759
760 388 return 0;
761 }
762
763 2 static int decode_exif_chunk(AVCodecContext *avctx, PNGDecContext *s,
764 GetByteContext *gb)
765 {
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(s->hdr_state & PNG_IHDR)) {
767 av_log(avctx, AV_LOG_ERROR, "eXIf before IHDR\n");
768 return AVERROR_INVALIDDATA;
769 }
770
771 2 av_buffer_unref(&s->exif_data);
772 2 s->exif_data = av_buffer_alloc(bytestream2_get_bytes_left(gb));
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->exif_data)
774 return AVERROR(ENOMEM);
775 2 bytestream2_get_buffer(gb, s->exif_data->data, s->exif_data->size);
776
777 2 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 666 static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame)
786 {
787 666 PNGDecContext *s = avctx->priv_data;
788 int ret;
789
790
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 662 times.
666 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 656 times.
662 } 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 617 times.
656 } 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 611 times.
617 } 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 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) {
839 49 av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
840
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 583 times.
617 } 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 666 avctx->colorspace = frame->colorspace = AVCOL_SPC_RGB;
862
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)
863 666 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 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)
871 avctx->bits_per_raw_sample = s->significant_bits;
872
873
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 664 times.
666 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 664 times.
666 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 666 return 0;
912 }
913
914 12454 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 10917 times.
12454 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
919
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12454 times.
12454 if (!p)
921 return AVERROR_INVALIDDATA;
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12454 times.
12454 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 666 times.
✓ Branch 1 taken 11788 times.
12454 if (!(s->pic_state & PNG_IDAT)) {
927 /* init image info */
928 666 ret = ff_set_dimensions(avctx, s->width, s->height);
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (ret < 0)
930 return ret;
931
932 666 s->channels = ff_png_get_nb_channels(s->color_type);
933 666 s->bits_per_pixel = s->bit_depth * s->channels;
934 666 s->bpp = (s->bits_per_pixel + 7) >> 3;
935 666 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
936
937
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) &&
938
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 145 times.
626 s->color_type == PNG_COLOR_TYPE_RGB) {
939 481 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 487 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 42 times.
666 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 /* PNG spec mandates independent alpha channel */
1002
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 101 times.
666 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
1003
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 561 times.
565 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1004 105 avctx->alpha_mode = AVALPHA_MODE_STRAIGHT;
1005
1006 666 ff_progress_frame_unref(&s->picture);
1007
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 665 times.
666 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1008 /* We only need a buffer for the current picture. */
1009 1 ret = ff_thread_get_buffer(avctx, p, 0);
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1011 return ret;
1012
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 653 times.
665 } else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1013 /* We need a buffer for the current picture as well as
1014 * a buffer for the reference to retain. */
1015 12 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
1016 AV_GET_BUFFER_FLAG_REF);
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
1018 return ret;
1019 12 ret = ff_thread_get_buffer(avctx, p, 0);
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
1021 return ret;
1022 } else {
1023 /* The picture output this time and the reference to retain coincide. */
1024 653 ret = ff_progress_frame_get_buffer(avctx, &s->picture,
1025 AV_GET_BUFFER_FLAG_REF);
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1027 return ret;
1028 653 ret = av_frame_ref(p, s->picture.f);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1030 return ret;
1031 }
1032
1033 666 p->pict_type = AV_PICTURE_TYPE_I;
1034 666 p->flags |= AV_FRAME_FLAG_KEY;
1035
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 658 times.
666 p->flags |= AV_FRAME_FLAG_INTERLACED * !!s->interlace_type;
1036
1037
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
666 if ((ret = populate_avctx_color_fields(avctx, p)) < 0)
1038 return ret;
1039 666 ff_thread_finish_setup(avctx);
1040
1041 /* compute the compressed row size */
1042
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 8 times.
666 if (!s->interlace_type) {
1043 658 s->crow_size = s->row_size + 1;
1044 } else {
1045 8 s->pass = 0;
1046 8 s->pass_row_size = ff_png_pass_row_size(s->pass,
1047 s->bits_per_pixel,
1048 s->cur_w);
1049 8 s->crow_size = s->pass_row_size + 1;
1050 }
1051 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
1052 s->row_size, s->crow_size);
1053
1054 /* copy the palette if needed */
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
1056 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
1057 /* empty row is used if differencing to the first row */
1058 666 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (!s->last_row)
1060 return AVERROR_INVALIDDATA;
1061
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 8 times.
666 if (s->interlace_type ||
1062
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 557 times.
658 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
1063 109 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (!s->tmp_row)
1065 return AVERROR_INVALIDDATA;
1066 }
1067 /* compressed row */
1068 666 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (!s->buffer)
1070 return AVERROR(ENOMEM);
1071
1072 /* we want crow_buf+1 to be 16-byte aligned */
1073 666 s->crow_buf = s->buffer + 15;
1074 666 s->zstream.zstream.avail_out = s->crow_size;
1075 666 s->zstream.zstream.next_out = s->crow_buf;
1076 }
1077
1078 12454 s->pic_state |= PNG_IDAT;
1079
1080 /* set image to non-transparent bpp while decompressing */
1081
4/4
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 12265 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 42 times.
12454 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
1082 147 s->bpp -= byte_depth;
1083
1084 12454 ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
1085
1086
4/4
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 12265 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 42 times.
12454 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
1087 147 s->bpp += byte_depth;
1088
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12454 times.
12454 if (ret < 0)
1090 return ret;
1091
1092 12454 return 0;
1093 }
1094
1095 3 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
1096 GetByteContext *gb)
1097 {
1098 3 int length = bytestream2_get_bytes_left(gb);
1099 int n, i, r, g, b;
1100
1101
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)
1102 return AVERROR_INVALIDDATA;
1103 /* read the palette */
1104 3 n = length / 3;
1105
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 3 times.
711 for (i = 0; i < n; i++) {
1106 708 r = bytestream2_get_byte(gb);
1107 708 g = bytestream2_get_byte(gb);
1108 708 b = bytestream2_get_byte(gb);
1109 708 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
1110 }
1111
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3 times.
63 for (; i < 256; i++)
1112 60 s->palette[i] = (0xFFU << 24);
1113 3 s->hdr_state |= PNG_PLTE;
1114
1115 3 return 0;
1116 }
1117
1118 7 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
1119 GetByteContext *gb)
1120 {
1121 7 int length = bytestream2_get_bytes_left(gb);
1122 int v, i;
1123
1124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!(s->hdr_state & PNG_IHDR)) {
1125 av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
1126 return AVERROR_INVALIDDATA;
1127 }
1128
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (s->pic_state & PNG_IDAT) {
1130 av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
1131 return AVERROR_INVALIDDATA;
1132 }
1133
1134
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1135
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))
1136 return AVERROR_INVALIDDATA;
1137
1138
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 3 times.
54 for (i = 0; i < length; i++) {
1139 51 unsigned v = bytestream2_get_byte(gb);
1140 51 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
1141 }
1142
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) {
1143
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) ||
1144
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) ||
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 s->bit_depth == 1)
1146 return AVERROR_INVALIDDATA;
1147
1148
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < length / 2; i++) {
1149 /* only use the least significant bits */
1150 12 v = av_zero_extend(bytestream2_get_be16(gb), s->bit_depth);
1151
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->bit_depth > 8)
1153 AV_WB16(&s->transparent_color_be[2 * i], v);
1154 else
1155 12 s->transparent_color_be[i] = v;
1156 }
1157 } else {
1158 return AVERROR_INVALIDDATA;
1159 }
1160
1161 7 s->has_trns = 1;
1162
1163 7 return 0;
1164 }
1165
1166 6 static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb)
1167 {
1168 6 int ret, cnt = 0;
1169 AVBPrint bp;
1170
1171
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);
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cnt > 80) {
1173 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
1174 ret = AVERROR_INVALIDDATA;
1175 goto fail;
1176 }
1177
1178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_byte(gb) != 0) {
1179 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
1180 ret = AVERROR_INVALIDDATA;
1181 goto fail;
1182 }
1183
1184
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)
1185 return ret;
1186
1187 6 av_freep(&s->iccp_data);
1188 6 ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
1189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1190 return ret;
1191 6 s->iccp_data_len = bp.len;
1192
1193 6 return 0;
1194 fail:
1195 s->iccp_name[0] = 0;
1196 return ret;
1197 }
1198
1199 static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s,
1200 GetByteContext *gb)
1201 {
1202 int bits = 0;
1203 int channels;
1204 int remainder = bytestream2_get_bytes_left(gb);
1205
1206 if (!(s->hdr_state & PNG_IHDR)) {
1207 av_log(avctx, AV_LOG_ERROR, "sBIT before IHDR\n");
1208 return AVERROR_INVALIDDATA;
1209 }
1210
1211 if (s->pic_state & PNG_IDAT) {
1212 av_log(avctx, AV_LOG_WARNING, "Ignoring illegal sBIT chunk after IDAT\n");
1213 return 0;
1214 }
1215
1216 channels = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type);
1217
1218 if (remainder != channels) {
1219 av_log(avctx, AV_LOG_WARNING, "Invalid sBIT size: %d, expected: %d\n", remainder, channels);
1220 /* not enough space left in chunk to read info */
1221 if (remainder < channels)
1222 return 0;
1223 }
1224
1225 for (int i = 0; i < channels; i++) {
1226 int b = bytestream2_get_byteu(gb);
1227 bits = FFMAX(b, bits);
1228 }
1229
1230 if (bits <= 0 || bits > (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) {
1231 av_log(avctx, AV_LOG_WARNING, "Invalid significant bits: %d\n", bits);
1232 return 0;
1233 }
1234 s->significant_bits = bits;
1235
1236 return 0;
1237 }
1238
1239 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
1240 {
1241 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
1242 int i, j, k;
1243 uint8_t *pd = p->data[0];
1244 for (j = 0; j < s->height; j++) {
1245 i = s->width / 8;
1246 for (k = 7; k >= 1; k--)
1247 if ((s->width&7) >= k)
1248 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
1249 for (i--; i >= 0; i--) {
1250 pd[8*i + 7]= pd[i] & 1;
1251 pd[8*i + 6]= (pd[i]>>1) & 1;
1252 pd[8*i + 5]= (pd[i]>>2) & 1;
1253 pd[8*i + 4]= (pd[i]>>3) & 1;
1254 pd[8*i + 3]= (pd[i]>>4) & 1;
1255 pd[8*i + 2]= (pd[i]>>5) & 1;
1256 pd[8*i + 1]= (pd[i]>>6) & 1;
1257 pd[8*i + 0]= pd[i]>>7;
1258 }
1259 pd += p->linesize[0];
1260 }
1261 } else if (s->bits_per_pixel == 2) {
1262 int i, j;
1263 uint8_t *pd = p->data[0];
1264 for (j = 0; j < s->height; j++) {
1265 i = s->width / 4;
1266 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1267 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
1268 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
1269 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
1270 for (i--; i >= 0; i--) {
1271 pd[4*i + 3]= pd[i] & 3;
1272 pd[4*i + 2]= (pd[i]>>2) & 3;
1273 pd[4*i + 1]= (pd[i]>>4) & 3;
1274 pd[4*i + 0]= pd[i]>>6;
1275 }
1276 } else {
1277 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1278 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1279 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1280 for (i--; i >= 0; i--) {
1281 pd[4*i + 3]= ( pd[i] & 3)*0x55;
1282 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
1283 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
1284 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
1285 }
1286 }
1287 pd += p->linesize[0];
1288 }
1289 } else if (s->bits_per_pixel == 4) {
1290 int i, j;
1291 uint8_t *pd = p->data[0];
1292 for (j = 0; j < s->height; j++) {
1293 i = s->width/2;
1294 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
1295 if (s->width&1) pd[2*i+0]= pd[i]>>4;
1296 for (i--; i >= 0; i--) {
1297 pd[2*i + 1] = pd[i] & 15;
1298 pd[2*i + 0] = pd[i] >> 4;
1299 }
1300 } else {
1301 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
1302 for (i--; i >= 0; i--) {
1303 pd[2*i + 1] = (pd[i] & 15) * 0x11;
1304 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
1305 }
1306 }
1307 pd += p->linesize[0];
1308 }
1309 }
1310 }
1311
1312 262 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
1313 GetByteContext *gb)
1314 {
1315 uint32_t sequence_number;
1316 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
1317
1318
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if (bytestream2_get_bytes_left(gb) != APNG_FCTL_CHUNK_SIZE)
1319 return AVERROR_INVALIDDATA;
1320
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->hdr_state & PNG_IHDR)) {
1322 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
1323 return AVERROR_INVALIDDATA;
1324 }
1325
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (s->pic_state & PNG_IDAT) {
1327 av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
1328 return AVERROR_INVALIDDATA;
1329 }
1330
1331 262 sequence_number = bytestream2_get_be32(gb);
1332 262 cur_w = bytestream2_get_be32(gb);
1333 262 cur_h = bytestream2_get_be32(gb);
1334 262 x_offset = bytestream2_get_be32(gb);
1335 262 y_offset = bytestream2_get_be32(gb);
1336 262 bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
1337 262 dispose_op = bytestream2_get_byte(gb);
1338 262 blend_op = bytestream2_get_byte(gb);
1339
1340
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 246 times.
262 if (sequence_number == 0 &&
1341
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 (cur_w != s->width ||
1342
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 ||
1343
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 x_offset != 0 ||
1344
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 y_offset != 0) ||
1345
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 ||
1346
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 x_offset < 0 || y_offset < 0 ||
1347
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)
1348 return AVERROR_INVALIDDATA;
1349
1350
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) {
1351 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1352 return AVERROR_INVALIDDATA;
1353 }
1354
1355
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) &&
1356 dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1357 // No previous frame to revert to for the first frame
1358 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1359 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1360 }
1361
1362
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 && (
1363
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1364
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1365
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1366
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1368 )) {
1369 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1370 blend_op = APNG_BLEND_OP_SOURCE;
1371 }
1372
1373 262 s->cur_w = cur_w;
1374 262 s->cur_h = cur_h;
1375 262 s->x_offset = x_offset;
1376 262 s->y_offset = y_offset;
1377 262 s->dispose_op = dispose_op;
1378 262 s->blend_op = blend_op;
1379
1380 262 return 0;
1381 }
1382
1383 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
1384 {
1385 int i, j;
1386 uint8_t *pd = p->data[0];
1387 uint8_t *pd_last = s->last_picture.f->data[0];
1388 int ls = av_image_get_linesize(p->format, s->width, 0);
1389
1390 ls = FFMIN(ls, s->width * s->bpp);
1391
1392 ff_progress_frame_await(&s->last_picture, INT_MAX);
1393 for (j = 0; j < s->height; j++) {
1394 for (i = 0; i < ls; i++)
1395 pd[i] += pd_last[i];
1396 pd += p->linesize[0];
1397 pd_last += s->last_picture.f->linesize[0];
1398 }
1399 }
1400
1401 // divide by 255 and round to nearest
1402 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1403 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1404
1405 61 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
1406 AVFrame *p)
1407 {
1408 61 uint8_t *dst = p->data[0];
1409 61 ptrdiff_t dst_stride = p->linesize[0];
1410 61 const uint8_t *src = s->last_picture.f->data[0];
1411 61 ptrdiff_t src_stride = s->last_picture.f->linesize[0];
1412
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;
1413
1414 size_t x, y;
1415
1416
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER &&
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1418 avctx->pix_fmt != AV_PIX_FMT_GRAY8A) {
1419 avpriv_request_sample(avctx, "Blending with pixel format %s",
1420 av_get_pix_fmt_name(avctx->pix_fmt));
1421 return AVERROR_PATCHWELCOME;
1422 }
1423
1424 61 ff_progress_frame_await(&s->last_picture, INT_MAX);
1425
1426 // copy unchanged rectangles from the last frame
1427
2/2
✓ Branch 0 taken 2186 times.
✓ Branch 1 taken 61 times.
2247 for (y = 0; y < s->y_offset; y++)
1428 2186 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1429
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++) {
1430 3320 memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * bpp);
1431 3320 memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * bpp,
1432 3320 src + y * src_stride + (s->x_offset + s->cur_w) * bpp,
1433 3320 (p->width - s->cur_w - s->x_offset) * bpp);
1434 }
1435
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++)
1436 2500 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
1437
1438
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER) {
1439 // Perform blending
1440
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) {
1441 1912 uint8_t *foreground = dst + dst_stride * y + bpp * s->x_offset;
1442 1912 const uint8_t *background = src + src_stride * y + bpp * s->x_offset;
1443
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) {
1444 size_t b;
1445 uint8_t foreground_alpha, background_alpha, output_alpha;
1446 uint8_t output[10];
1447
1448 // Since we might be blending alpha onto alpha, we use the following equations:
1449 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1450 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1451
1452
1/3
✓ Branch 0 taken 143265 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
143265 switch (avctx->pix_fmt) {
1453 143265 case AV_PIX_FMT_RGBA:
1454 143265 foreground_alpha = foreground[3];
1455 143265 background_alpha = background[3];
1456 143265 break;
1457
1458 case AV_PIX_FMT_GRAY8A:
1459 foreground_alpha = foreground[1];
1460 background_alpha = background[1];
1461 break;
1462 }
1463
1464
2/2
✓ Branch 0 taken 21682 times.
✓ Branch 1 taken 121583 times.
143265 if (foreground_alpha == 255)
1465 143265 continue;
1466
1467
1/2
✓ Branch 0 taken 121583 times.
✗ Branch 1 not taken.
121583 if (foreground_alpha == 0) {
1468 121583 memcpy(foreground, background, bpp);
1469 121583 continue;
1470 }
1471
1472 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1473
1474 av_assert0(bpp <= 10);
1475
1476 for (b = 0; b < bpp - 1; ++b) {
1477 if (output_alpha == 0) {
1478 output[b] = 0;
1479 } else if (background_alpha == 255) {
1480 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1481 } else {
1482 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1483 }
1484 }
1485 output[b] = output_alpha;
1486 memcpy(foreground, output, bpp);
1487 }
1488 }
1489 }
1490
1491 61 return 0;
1492 }
1493
1494 12 static void apng_reset_background(PNGDecContext *s, const AVFrame *p)
1495 {
1496 // need to reset a rectangle to black
1497 12 av_unused int ret = av_frame_copy(s->picture.f, p);
1498
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;
1499 12 const ptrdiff_t dst_stride = s->picture.f->linesize[0];
1500 12 uint8_t *dst = s->picture.f->data[0] + s->y_offset * dst_stride + bpp * s->x_offset;
1501
1502 av_assert1(ret >= 0);
1503
1504
2/2
✓ Branch 0 taken 959 times.
✓ Branch 1 taken 12 times.
971 for (size_t y = 0; y < s->cur_h; y++) {
1505 959 memset(dst, 0, bpp * s->cur_w);
1506 959 dst += dst_stride;
1507 }
1508 12 }
1509
1510 683 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1511 AVFrame *p, const AVPacket *avpkt)
1512 {
1513 683 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1514 uint32_t tag, length;
1515 683 int decode_next_dat = 0;
1516 int i, ret;
1517
1518 13993 for (;;) {
1519 GetByteContext gb_chunk;
1520
1521 14676 length = bytestream2_get_bytes_left(&s->gb);
1522
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 14319 times.
14676 if (length <= 0) {
1523
1524
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 278 times.
357 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1525
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 avctx->skip_frame == AVDISCARD_ALL) {
1526 95 return 0;
1527 }
1528
1529
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) {
1530
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 262 times.
278 if (!(s->pic_state & PNG_IDAT))
1531 16 return 0;
1532 else
1533 587 goto exit_loop;
1534 }
1535 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1536 if ( s->pic_state & PNG_ALLIMAGE
1537 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1538 goto exit_loop;
1539 ret = AVERROR_INVALIDDATA;
1540 1 goto fail;
1541 }
1542
1543 14319 length = bytestream2_get_be32(&s->gb);
1544
3/4
✓ Branch 0 taken 14319 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 14318 times.
14319 if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
1545 1 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1546 1 ret = AVERROR_INVALIDDATA;
1547 1 goto fail;
1548 }
1549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14318 times.
14318 if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1550 uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1551 uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1552 if (crc_sig ^ crc_cal) {
1553 av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1554 if (avctx->err_recognition & AV_EF_EXPLODE) {
1555 av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1556 ret = AVERROR_INVALIDDATA;
1557 goto fail;
1558 }
1559 av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1560 bytestream2_skip(&s->gb, length + 8); /* tag */
1561 143 continue;
1562 }
1563 }
1564 14318 tag = bytestream2_get_le32(&s->gb);
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14318 times.
14318 if (avctx->debug & FF_DEBUG_STARTCODE)
1566 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1567 av_fourcc2str(tag), length);
1568
1569 14318 bytestream2_init(&gb_chunk, s->gb.buffer, length);
1570 14318 bytestream2_skip(&s->gb, length + 4);
1571
1572
2/2
✓ Branch 0 taken 11749 times.
✓ Branch 1 taken 2569 times.
14318 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1573
2/2
✓ Branch 0 taken 1525 times.
✓ Branch 1 taken 10224 times.
11749 avctx->skip_frame == AVDISCARD_ALL) {
1574
2/2
✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 143 times.
1525 switch(tag) {
1575 1382 case MKTAG('I', 'H', 'D', 'R'):
1576 case MKTAG('p', 'H', 'Y', 's'):
1577 case MKTAG('t', 'E', 'X', 't'):
1578 case MKTAG('I', 'D', 'A', 'T'):
1579 case MKTAG('t', 'R', 'N', 'S'):
1580 case MKTAG('s', 'R', 'G', 'B'):
1581 case MKTAG('c', 'I', 'C', 'P'):
1582 case MKTAG('c', 'H', 'R', 'M'):
1583 case MKTAG('g', 'A', 'M', 'A'):
1584 1382 break;
1585 143 default:
1586 143 continue;
1587 }
1588 }
1589
1590
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 10400 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.
14175 switch (tag) {
1591 421 case MKTAG('I', 'H', 'D', 'R'):
1592
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
1593 goto fail;
1594 421 break;
1595 388 case MKTAG('p', 'H', 'Y', 's'):
1596
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
1597 goto fail;
1598 388 break;
1599 262 case MKTAG('f', 'c', 'T', 'L'):
1600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1601 continue;
1602
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
1603 goto fail;
1604 262 decode_next_dat = 1;
1605 262 break;
1606 2054 case MKTAG('f', 'd', 'A', 'T'):
1607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2054 times.
2054 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1608 continue;
1609
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) {
1610 ret = AVERROR_INVALIDDATA;
1611 goto fail;
1612 }
1613 2054 bytestream2_get_be32(&gb_chunk);
1614 /* fallthrough */
1615 12454 case MKTAG('I', 'D', 'A', 'T'):
1616
3/4
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 10194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2260 times.
12454 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1617 continue;
1618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12454 times.
12454 if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
1619 goto fail;
1620 12454 break;
1621 3 case MKTAG('P', 'L', 'T', 'E'):
1622 3 decode_plte_chunk(avctx, s, &gb_chunk);
1623 3 break;
1624 7 case MKTAG('t', 'R', 'N', 'S'):
1625 7 decode_trns_chunk(avctx, s, &gb_chunk);
1626 7 break;
1627 138 case MKTAG('t', 'E', 'X', 't'):
1628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
138 if (decode_text_chunk(s, &gb_chunk, 0) < 0)
1629 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1630 138 break;
1631 case MKTAG('z', 'T', 'X', 't'):
1632 if (decode_text_chunk(s, &gb_chunk, 1) < 0)
1633 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1634 break;
1635 case MKTAG('s', 'T', 'E', 'R'): {
1636 int mode = bytestream2_get_byte(&gb_chunk);
1637
1638 if (mode == 0 || mode == 1) {
1639 s->stereo_mode = mode;
1640 } else {
1641 av_log(avctx, AV_LOG_WARNING,
1642 "Unknown value in sTER chunk (%d)\n", mode);
1643 }
1644 break;
1645 }
1646 4 case MKTAG('c', 'I', 'C', 'P'):
1647 4 s->cicp_primaries = bytestream2_get_byte(&gb_chunk);
1648 4 s->cicp_trc = bytestream2_get_byte(&gb_chunk);
1649
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_byte(&gb_chunk) != 0)
1650 av_log(avctx, AV_LOG_WARNING, "nonzero cICP matrix\n");
1651 4 s->cicp_range = bytestream2_get_byte(&gb_chunk);
1652
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)
1653 av_log(avctx, AV_LOG_WARNING, "invalid cICP range: %d\n", s->cicp_range);
1654 4 s->have_cicp = 1;
1655 4 break;
1656 39 case MKTAG('s', 'R', 'G', 'B'):
1657 /* skip rendering intent byte */
1658 39 bytestream2_skip(&gb_chunk, 1);
1659 39 s->have_srgb = 1;
1660 39 break;
1661 6 case MKTAG('i', 'C', 'C', 'P'): {
1662
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = decode_iccp_chunk(s, &gb_chunk)) < 0)
1663 goto fail;
1664 6 break;
1665 }
1666 16 case MKTAG('c', 'H', 'R', 'M'): {
1667 16 s->have_chrm = 1;
1668
1669 16 s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1670 16 s->white_point[1] = bytestream2_get_be32(&gb_chunk);
1671
1672 /* RGB Primaries */
1673
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 for (i = 0; i < 3; i++) {
1674 48 s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
1675 48 s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
1676 }
1677
1678 16 break;
1679 }
1680 case MKTAG('s', 'B', 'I', 'T'):
1681 if ((ret = decode_sbit_chunk(avctx, s, &gb_chunk)) < 0)
1682 goto fail;
1683 break;
1684 34 case MKTAG('g', 'A', 'M', 'A'): {
1685 AVBPrint bp;
1686 char *gamma_str;
1687 34 s->gamma = bytestream2_get_be32(&gb_chunk);
1688
1689 34 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1690 34 av_bprintf(&bp, "%i/%i", s->gamma, 100000);
1691 34 ret = av_bprint_finalize(&bp, &gamma_str);
1692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ret < 0)
1693 return ret;
1694
1695 34 av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1696
1697 34 break;
1698 }
1699 2 case MKTAG('c', 'L', 'L', 'i'): /* legacy spelling, for backwards compat */
1700 case MKTAG('c', 'L', 'L', 'I'):
1701
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&gb_chunk) != 8) {
1702 av_log(avctx, AV_LOG_WARNING, "Invalid cLLI chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1703 break;
1704 }
1705 2 s->have_clli = 1;
1706 2 s->clli_max = bytestream2_get_be32u(&gb_chunk);
1707 2 s->clli_avg = bytestream2_get_be32u(&gb_chunk);
1708 2 break;
1709 2 case MKTAG('m', 'D', 'C', 'v'): /* legacy spelling, for backward compat */
1710 case MKTAG('m', 'D', 'C', 'V'):
1711
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&gb_chunk) != 24) {
1712 av_log(avctx, AV_LOG_WARNING, "Invalid mDCV chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk));
1713 break;
1714 }
1715 2 s->have_mdcv = 1;
1716
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++) {
1717 6 s->mdcv_primaries[i][0] = bytestream2_get_be16u(&gb_chunk);
1718 6 s->mdcv_primaries[i][1] = bytestream2_get_be16u(&gb_chunk);
1719 }
1720 2 s->mdcv_white_point[0] = bytestream2_get_be16u(&gb_chunk);
1721 2 s->mdcv_white_point[1] = bytestream2_get_be16u(&gb_chunk);
1722 2 s->mdcv_max_lum = bytestream2_get_be32u(&gb_chunk);
1723 2 s->mdcv_min_lum = bytestream2_get_be32u(&gb_chunk);
1724 2 break;
1725 2 case MKTAG('e', 'X', 'I', 'f'):
1726 2 ret = decode_exif_chunk(avctx, s, &gb_chunk);
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1728 goto fail;
1729 2 break;
1730 325 case MKTAG('I', 'E', 'N', 'D'):
1731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (!(s->pic_state & PNG_ALLIMAGE))
1732 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1734 ret = AVERROR_INVALIDDATA;
1735 goto fail;
1736 }
1737 325 goto exit_loop;
1738 }
1739 }
1740 587 exit_loop:
1741
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!p)
1743 return AVERROR_INVALIDDATA;
1744
1745
2/2
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 262 times.
587 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 avctx->skip_frame == AVDISCARD_ALL) {
1747 return 0;
1748 }
1749
1750
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 587 times.
587 if (percent_missing(s) > avctx->discard_damaged_percentage) {
1751 ret = AVERROR_INVALIDDATA;
1752 goto fail;
1753 }
1754
1755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (s->bits_per_pixel <= 4)
1756 handle_small_bpp(s, p);
1757
1758
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 585 times.
587 if (s->exif_data) {
1759 // we swap because ff_decode_exif_attach_buffer adds to p->metadata
1760 2 FFSWAP(AVDictionary *, p->metadata, s->frame_metadata);
1761 2 ret = ff_decode_exif_attach_buffer(avctx, p, &s->exif_data, AV_EXIF_TIFF_HEADER);
1762 2 FFSWAP(AVDictionary *, p->metadata, s->frame_metadata);
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1764 av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF buffer\n");
1765 return ret;
1766 }
1767 }
1768
1769
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) {
1770
2/2
✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 42 times.
6342 for (int y = 0; y < s->height; y++) {
1771 6300 uint8_t *row = &p->data[0][p->linesize[0] * y];
1772
1773
2/2
✓ Branch 0 taken 945000 times.
✓ Branch 1 taken 6300 times.
951300 for (int x = s->width - 1; x >= 0; x--) {
1774 945000 const uint8_t idx = row[x];
1775
1776 945000 row[4*x+2] = s->palette[idx] & 0xFF;
1777 945000 row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF;
1778 945000 row[4*x+0] = (s->palette[idx] >> 16) & 0xFF;
1779 945000 row[4*x+3] = s->palette[idx] >> 24;
1780 }
1781 }
1782 }
1783
1784 /* apply transparency if needed */
1785
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) {
1786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1787 136 size_t raw_bpp = s->bpp - byte_depth;
1788 ptrdiff_t x, y;
1789
1790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 av_assert0(s->bit_depth > 1);
1791
1792
2/2
✓ Branch 0 taken 17610 times.
✓ Branch 1 taken 136 times.
17746 for (y = 0; y < s->height; ++y) {
1793 17610 uint8_t *row = &p->data[0][p->linesize[0] * y];
1794
1795
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) {
1796 uint8_t *pixel = &row[2 * s->width - 1];
1797 uint8_t *rowp = &row[1 * s->width - 1];
1798 int tcolor = s->transparent_color_be[0];
1799 for (x = s->width; x > 0; --x) {
1800 *pixel-- = *rowp == tcolor ? 0 : 0xff;
1801 *pixel-- = *rowp--;
1802 }
1803
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) {
1804 17610 uint8_t *pixel = &row[4 * s->width - 1];
1805 17610 uint8_t *rowp = &row[3 * s->width - 1];
1806 17610 int tcolor = AV_RL24(s->transparent_color_be);
1807
2/2
✓ Branch 0 taken 1577700 times.
✓ Branch 1 taken 17610 times.
1595310 for (x = s->width; x > 0; --x) {
1808
2/2
✓ Branch 0 taken 82906 times.
✓ Branch 1 taken 1494794 times.
1577700 *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
1809 1577700 *pixel-- = *rowp--;
1810 1577700 *pixel-- = *rowp--;
1811 1577700 *pixel-- = *rowp--;
1812 }
1813 } else {
1814 /* since we're updating in-place, we have to go from right to left */
1815 for (x = s->width; x > 0; --x) {
1816 uint8_t *pixel = &row[s->bpp * (x - 1)];
1817 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1818
1819 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1820 memset(&pixel[raw_bpp], 0, byte_depth);
1821 } else {
1822 memset(&pixel[raw_bpp], 0xff, byte_depth);
1823 }
1824 }
1825 }
1826 }
1827 }
1828
1829 /* handle P-frames only if a predecessor frame is available */
1830
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 81 times.
587 if (s->last_picture.f) {
1831
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")
1832
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->width == p->width
1833
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->height== p->height
1834
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->format== p->format
1835 ) {
1836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1837 handle_p_frame_png(s, p);
1838 61 else if (CONFIG_APNG_DECODER &&
1839
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 &&
1840 61 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1841 goto fail;
1842 }
1843 }
1844
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 575 times.
587 if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)
1845 12 apng_reset_background(s, p);
1846
1847 587 ret = 0;
1848 588 fail:
1849
2/2
✓ Branch 0 taken 586 times.
✓ Branch 1 taken 2 times.
588 if (s->picture.f)
1850 586 ff_progress_frame_report(&s->picture, INT_MAX);
1851
1852 588 return ret;
1853 }
1854
1855 667 static void clear_frame_metadata(PNGDecContext *s)
1856 {
1857 667 av_freep(&s->iccp_data);
1858 667 s->iccp_data_len = 0;
1859 667 s->iccp_name[0] = 0;
1860
1861 667 s->stereo_mode = -1;
1862
1863 667 s->have_chrm = 0;
1864 667 s->have_srgb = 0;
1865 667 s->have_cicp = 0;
1866
1867 667 av_dict_free(&s->frame_metadata);
1868 667 }
1869
1870 587 static int output_frame(PNGDecContext *s, AVFrame *f)
1871 {
1872 int ret;
1873
1874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (s->stereo_mode >= 0) {
1875 AVStereo3D *stereo3d = av_stereo3d_create_side_data(f);
1876 if (!stereo3d) {
1877 ret = AVERROR(ENOMEM);
1878 goto fail;
1879 }
1880
1881 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1882 stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1883 }
1884
1885 587 FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
1886
1887 587 return 0;
1888 fail:
1889 av_frame_unref(f);
1890 return ret;
1891 }
1892
1893 #if CONFIG_PNG_DECODER
1894 405 static int decode_frame_png(AVCodecContext *avctx, AVFrame *p,
1895 int *got_frame, AVPacket *avpkt)
1896 {
1897 405 PNGDecContext *const s = avctx->priv_data;
1898 405 const uint8_t *buf = avpkt->data;
1899 405 int buf_size = avpkt->size;
1900 int64_t sig;
1901 int ret;
1902
1903 405 clear_frame_metadata(s);
1904
1905 405 bytestream2_init(&s->gb, buf, buf_size);
1906
1907 /* check signature */
1908 405 sig = bytestream2_get_be64(&s->gb);
1909
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
405 if (sig != PNGSIG &&
1910 sig != MNGSIG) {
1911 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1912 return AVERROR_INVALIDDATA;
1913 }
1914
1915 405 s->y = s->has_trns = 0;
1916 405 s->hdr_state = 0;
1917 405 s->pic_state = 0;
1918
1919 /* Reset z_stream */
1920 405 ret = inflateReset(&s->zstream.zstream);
1921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret != Z_OK)
1922 return AVERROR_EXTERNAL;
1923
1924
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 404 times.
405 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1925 1 goto the_end;
1926
1927
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 325 times.
404 if (avctx->skip_frame == AVDISCARD_ALL) {
1928 79 *got_frame = 0;
1929 79 ret = bytestream2_tell(&s->gb);
1930 79 goto the_end;
1931 }
1932
1933 325 ret = output_frame(s, p);
1934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 if (ret < 0)
1935 goto the_end;
1936
1937
1/2
✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
325 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1938 325 ff_progress_frame_unref(&s->last_picture);
1939 325 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1940 }
1941
1942 325 *got_frame = 1;
1943
1944 325 ret = bytestream2_tell(&s->gb);
1945 405 the_end:
1946 405 s->crow_buf = NULL;
1947 405 return ret;
1948 }
1949 #endif
1950
1951 #if CONFIG_APNG_DECODER
1952 262 static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p,
1953 int *got_frame, AVPacket *avpkt)
1954 {
1955 262 PNGDecContext *const s = avctx->priv_data;
1956 int ret;
1957
1958 262 clear_frame_metadata(s);
1959
1960
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 246 times.
262 if (!(s->hdr_state & PNG_IHDR)) {
1961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!avctx->extradata_size)
1962 return AVERROR_INVALIDDATA;
1963
1964
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1965 return AVERROR_EXTERNAL;
1966 16 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1967
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = decode_frame_common(avctx, s, NULL, avpkt)) < 0)
1968 return ret;
1969 }
1970
1971 /* reset state for a new frame */
1972
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
1973 return AVERROR_EXTERNAL;
1974 262 s->y = 0;
1975 262 s->pic_state = 0;
1976 262 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1977
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 262 times.
262 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1978 return ret;
1979
1980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->pic_state & PNG_ALLIMAGE))
1981 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT)))
1983 return AVERROR_INVALIDDATA;
1984
1985 262 ret = output_frame(s, p);
1986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if (ret < 0)
1987 return ret;
1988
1989
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1990
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 1 times.
262 if (s->dispose_op != APNG_DISPOSE_OP_PREVIOUS)
1991 261 FFSWAP(ProgressFrame, s->picture, s->last_picture);
1992 262 ff_progress_frame_unref(&s->picture);
1993 }
1994
1995 262 *got_frame = 1;
1996 262 return bytestream2_tell(&s->gb);
1997 }
1998 #endif
1999
2000 #if HAVE_THREADS
2001 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
2002 {
2003 PNGDecContext *psrc = src->priv_data;
2004 PNGDecContext *pdst = dst->priv_data;
2005 const ProgressFrame *src_frame;
2006
2007 if (dst == src)
2008 return 0;
2009
2010 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
2011
2012 pdst->width = psrc->width;
2013 pdst->height = psrc->height;
2014 pdst->bit_depth = psrc->bit_depth;
2015 pdst->color_type = psrc->color_type;
2016 pdst->compression_type = psrc->compression_type;
2017 pdst->interlace_type = psrc->interlace_type;
2018 pdst->filter_type = psrc->filter_type;
2019 pdst->has_trns = psrc->has_trns;
2020 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
2021
2022 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
2023
2024 pdst->hdr_state |= psrc->hdr_state;
2025 }
2026
2027 src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
2028 &psrc->last_picture : &psrc->picture;
2029
2030 ff_progress_frame_replace(&pdst->last_picture, src_frame);
2031
2032 return 0;
2033 }
2034 #endif
2035
2036 165 static av_cold int png_dec_init(AVCodecContext *avctx)
2037 {
2038 165 PNGDecContext *s = avctx->priv_data;
2039
2040 165 s->avctx = avctx;
2041
2042 165 ff_pngdsp_init(&s->dsp);
2043
2044 165 return ff_inflate_init(&s->zstream, avctx);
2045 }
2046
2047 165 static av_cold int png_dec_end(AVCodecContext *avctx)
2048 {
2049 165 PNGDecContext *s = avctx->priv_data;
2050
2051 165 ff_progress_frame_unref(&s->last_picture);
2052 165 ff_progress_frame_unref(&s->picture);
2053 165 av_freep(&s->buffer);
2054 165 s->buffer_size = 0;
2055 165 av_freep(&s->last_row);
2056 165 s->last_row_size = 0;
2057 165 av_freep(&s->tmp_row);
2058 165 s->tmp_row_size = 0;
2059
2060 165 av_freep(&s->iccp_data);
2061 165 av_buffer_unref(&s->exif_data);
2062 165 av_dict_free(&s->frame_metadata);
2063 165 ff_inflate_end(&s->zstream);
2064
2065 165 return 0;
2066 }
2067
2068 #if CONFIG_APNG_DECODER
2069 const FFCodec ff_apng_decoder = {
2070 .p.name = "apng",
2071 CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
2072 .p.type = AVMEDIA_TYPE_VIDEO,
2073 .p.id = AV_CODEC_ID_APNG,
2074 .priv_data_size = sizeof(PNGDecContext),
2075 .init = png_dec_init,
2076 .close = png_dec_end,
2077 FF_CODEC_DECODE_CB(decode_frame_apng),
2078 UPDATE_THREAD_CONTEXT(update_thread_context),
2079 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2080 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
2081 FF_CODEC_CAP_USES_PROGRESSFRAMES |
2082 FF_CODEC_CAP_ICC_PROFILES,
2083 };
2084 #endif
2085
2086 #if CONFIG_PNG_DECODER
2087 const FFCodec ff_png_decoder = {
2088 .p.name = "png",
2089 CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
2090 .p.type = AVMEDIA_TYPE_VIDEO,
2091 .p.id = AV_CODEC_ID_PNG,
2092 .priv_data_size = sizeof(PNGDecContext),
2093 .init = png_dec_init,
2094 .close = png_dec_end,
2095 FF_CODEC_DECODE_CB(decode_frame_png),
2096 UPDATE_THREAD_CONTEXT(update_thread_context),
2097 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2098 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
2099 FF_CODEC_CAP_INIT_CLEANUP |
2100 FF_CODEC_CAP_USES_PROGRESSFRAMES |
2101 FF_CODEC_CAP_ICC_PROFILES,
2102 };
2103 #endif
2104