FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngdec.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 645 1005 64.2%
Branches: 410 728 56.3%

Line Branch Exec Source
1 /*
2 * PNG image format
3 * Copyright (c) 2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 //#define DEBUG
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/crc.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/stereo3d.h"
30 #include "libavutil/mastering_display_metadata.h"
31
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "apng.h"
36 #include "png.h"
37 #include "pngdsp.h"
38 #include "thread.h"
39
40 #include <zlib.h>
41
42 enum PNGHeaderState {
43 PNG_IHDR = 1 << 0,
44 PNG_PLTE = 1 << 1,
45 };
46
47 enum PNGImageState {
48 PNG_IDAT = 1 << 0,
49 PNG_ALLIMAGE = 1 << 1,
50 };
51
52 typedef struct PNGDecContext {
53 PNGDSPContext dsp;
54 AVCodecContext *avctx;
55
56 GetByteContext gb;
57 ThreadFrame last_picture;
58 ThreadFrame picture;
59
60 AVDictionary *frame_metadata;
61
62 uint8_t iccp_name[82];
63 uint8_t *iccp_data;
64 size_t iccp_data_len;
65
66 int stereo_mode;
67
68 int have_chrm;
69 uint32_t white_point[2];
70 uint32_t display_primaries[3][2];
71
72 enum PNGHeaderState hdr_state;
73 enum PNGImageState pic_state;
74 int width, height;
75 int cur_w, cur_h;
76 int last_w, last_h;
77 int x_offset, y_offset;
78 int last_x_offset, last_y_offset;
79 uint8_t dispose_op, blend_op;
80 uint8_t last_dispose_op;
81 int bit_depth;
82 int color_type;
83 int compression_type;
84 int interlace_type;
85 int filter_type;
86 int channels;
87 int bits_per_pixel;
88 int bpp;
89 int has_trns;
90 uint8_t transparent_color_be[6];
91
92 uint8_t *background_buf;
93 unsigned background_buf_allocated;
94 uint32_t palette[256];
95 uint8_t *crow_buf;
96 uint8_t *last_row;
97 unsigned int last_row_size;
98 uint8_t *tmp_row;
99 unsigned int tmp_row_size;
100 uint8_t *buffer;
101 int buffer_size;
102 int pass;
103 int crow_size; /* compressed row size (include filter type) */
104 int row_size; /* decompressed row size */
105 int pass_row_size; /* decompress row size of the current pass */
106 int y;
107 z_stream zstream;
108 } PNGDecContext;
109
110 /* Mask to determine which pixels are valid in a pass */
111 static const uint8_t png_pass_mask[NB_PASSES] = {
112 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
113 };
114
115 /* Mask to determine which y pixels can be written in a pass */
116 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
117 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
118 };
119
120 /* Mask to determine which pixels to overwrite while displaying */
121 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
122 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
123 };
124
125 /* NOTE: we try to construct a good looking image at each pass. width
126 * is the original image width. We also do pixel format conversion at
127 * this stage */
128 2816 static void png_put_interlaced_row(uint8_t *dst, int width,
129 int bits_per_pixel, int pass,
130 int color_type, const uint8_t *src)
131 {
132 int x, mask, dsp_mask, j, src_x, b, bpp;
133 uint8_t *d;
134 const uint8_t *s;
135
136 2816 mask = png_pass_mask[pass];
137 2816 dsp_mask = png_pass_dsp_mask[pass];
138
139
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2816 times.
2816 switch (bits_per_pixel) {
140 case 1:
141 src_x = 0;
142 for (x = 0; x < width; x++) {
143 j = (x & 7);
144 if ((dsp_mask << j) & 0x80) {
145 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
146 dst[x >> 3] &= 0xFF7F>>j;
147 dst[x >> 3] |= b << (7 - j);
148 }
149 if ((mask << j) & 0x80)
150 src_x++;
151 }
152 break;
153 case 2:
154 src_x = 0;
155 for (x = 0; x < width; x++) {
156 int j2 = 2 * (x & 3);
157 j = (x & 7);
158 if ((dsp_mask << j) & 0x80) {
159 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
160 dst[x >> 2] &= 0xFF3F>>j2;
161 dst[x >> 2] |= b << (6 - j2);
162 }
163 if ((mask << j) & 0x80)
164 src_x++;
165 }
166 break;
167 case 4:
168 src_x = 0;
169 for (x = 0; x < width; x++) {
170 int j2 = 4*(x&1);
171 j = (x & 7);
172 if ((dsp_mask << j) & 0x80) {
173 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
174 dst[x >> 1] &= 0xFF0F>>j2;
175 dst[x >> 1] |= b << (4 - j2);
176 }
177 if ((mask << j) & 0x80)
178 src_x++;
179 }
180 break;
181 2816 default:
182 2816 bpp = bits_per_pixel >> 3;
183 2816 d = dst;
184 2816 s = src;
185
2/2
✓ Branch 0 taken 360448 times.
✓ Branch 1 taken 2816 times.
363264 for (x = 0; x < width; x++) {
186 360448 j = x & 7;
187
2/2
✓ Branch 0 taken 262144 times.
✓ Branch 1 taken 98304 times.
360448 if ((dsp_mask << j) & 0x80) {
188 262144 memcpy(d, s, bpp);
189 }
190 360448 d += bpp;
191
2/2
✓ Branch 0 taken 122880 times.
✓ Branch 1 taken 237568 times.
360448 if ((mask << j) & 0x80)
192 122880 s += bpp;
193 }
194 2816 break;
195 }
196 2816 }
197
198 50710 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
199 int w, int bpp)
200 {
201 int i;
202
2/2
✓ Branch 0 taken 12195554 times.
✓ Branch 1 taken 50710 times.
12246264 for (i = 0; i < w; i++) {
203 int a, b, c, p, pa, pb, pc;
204
205 12195554 a = dst[i - bpp];
206 12195554 b = top[i];
207 12195554 c = top[i - bpp];
208
209 12195554 p = b - c;
210 12195554 pc = a - c;
211
212 12195554 pa = abs(p);
213 12195554 pb = abs(pc);
214 12195554 pc = abs(p + pc);
215
216
4/4
✓ Branch 0 taken 7703953 times.
✓ Branch 1 taken 4491601 times.
✓ Branch 2 taken 7039325 times.
✓ Branch 3 taken 664628 times.
12195554 if (pa <= pb && pa <= pc)
217 7039325 p = a;
218
2/2
✓ Branch 0 taken 4193004 times.
✓ Branch 1 taken 963225 times.
5156229 else if (pb <= pc)
219 4193004 p = b;
220 else
221 963225 p = c;
222 12195554 dst[i] = p + src[i];
223 }
224 50710 }
225
226 #define UNROLL1(bpp, op) \
227 { \
228 r = dst[0]; \
229 if (bpp >= 2) \
230 g = dst[1]; \
231 if (bpp >= 3) \
232 b = dst[2]; \
233 if (bpp >= 4) \
234 a = dst[3]; \
235 for (; i <= size - bpp; i += bpp) { \
236 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
237 if (bpp == 1) \
238 continue; \
239 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
240 if (bpp == 2) \
241 continue; \
242 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
243 if (bpp == 3) \
244 continue; \
245 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
246 } \
247 }
248
249 #define UNROLL_FILTER(op) \
250 if (bpp == 1) { \
251 UNROLL1(1, op) \
252 } else if (bpp == 2) { \
253 UNROLL1(2, op) \
254 } else if (bpp == 3) { \
255 UNROLL1(3, op) \
256 } else if (bpp == 4) { \
257 UNROLL1(4, op) \
258 } \
259 for (; i < size; i++) { \
260 dst[i] = op(dst[i - bpp], src[i], last[i]); \
261 }
262
263 /* NOTE: 'dst' can be equal to 'last' */
264 120458 void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
265 uint8_t *src, uint8_t *last, int size, int bpp)
266 {
267 int i, p, r, g, b, a;
268
269
5/6
✓ Branch 0 taken 80760 times.
✓ Branch 1 taken 3789 times.
✓ Branch 2 taken 8422 times.
✓ Branch 3 taken 1914 times.
✓ Branch 4 taken 25573 times.
✗ Branch 5 not taken.
120458 switch (filter_type) {
270 80760 case PNG_FILTER_VALUE_NONE:
271 80760 memcpy(dst, src, size);
272 80760 break;
273 3789 case PNG_FILTER_VALUE_SUB:
274
2/2
✓ Branch 0 taken 13632 times.
✓ Branch 1 taken 3789 times.
17421 for (i = 0; i < bpp; i++)
275 13632 dst[i] = src[i];
276
2/2
✓ Branch 0 taken 2235 times.
✓ Branch 1 taken 1554 times.
3789 if (bpp == 4) {
277 2235 p = *(int *)dst;
278
2/2
✓ Branch 0 taken 329481 times.
✓ Branch 1 taken 2235 times.
331716 for (; i < size; i += bpp) {
279 329481 unsigned s = *(int *)(src + i);
280 329481 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
281 329481 *(int *)(dst + i) = p;
282 }
283 } else {
284 #define OP_SUB(x, s, l) ((x) + (s))
285
12/18
✗ Branch 0 not taken.
✓ Branch 1 taken 1554 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1552 times.
✓ Branch 6 taken 254 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1544 times.
✓ Branch 9 taken 8 times.
✓ Branch 10 taken 284679 times.
✓ Branch 11 taken 1544 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 8 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 7112 times.
✓ Branch 17 taken 1554 times.
293599 UNROLL_FILTER(OP_SUB);
286 }
287 3789 break;
288 8422 case PNG_FILTER_VALUE_UP:
289 8422 dsp->add_bytes_l2(dst, src, last, size);
290 8422 break;
291 1914 case PNG_FILTER_VALUE_AVG:
292
2/2
✓ Branch 0 taken 6324 times.
✓ Branch 1 taken 1914 times.
8238 for (i = 0; i < bpp; i++) {
293 6324 p = (last[i] >> 1);
294 6324 dst[i] = p + src[i];
295 }
296 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
297
15/18
✗ Branch 0 not taken.
✓ Branch 1 taken 1914 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1912 times.
✓ Branch 6 taken 254 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1384 times.
✓ Branch 9 taken 528 times.
✓ Branch 10 taken 180020 times.
✓ Branch 11 taken 1384 times.
✓ Branch 12 taken 500 times.
✓ Branch 13 taken 28 times.
✓ Branch 14 taken 39070 times.
✓ Branch 15 taken 500 times.
✓ Branch 16 taken 21336 times.
✓ Branch 17 taken 1914 times.
242594 UNROLL_FILTER(OP_AVG);
298 1914 break;
299 25573 case PNG_FILTER_VALUE_PAETH:
300
2/2
✓ Branch 0 taken 79140 times.
✓ Branch 1 taken 25573 times.
104713 for (i = 0; i < bpp; i++) {
301 79140 p = last[i];
302 79140 dst[i] = p + src[i];
303 }
304
3/4
✓ Branch 0 taken 25503 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 25503 times.
✗ Branch 3 not taken.
25573 if (bpp > 2 && size > 4) {
305 /* would write off the end of the array if we let it process
306 * the last pixel with bpp=3 */
307
2/2
✓ Branch 0 taken 23640 times.
✓ Branch 1 taken 1863 times.
25503 int w = (bpp & 3) ? size - 3 : size;
308
309
1/2
✓ Branch 0 taken 25503 times.
✗ Branch 1 not taken.
25503 if (w > i) {
310 25503 dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
311 25503 i = w;
312 }
313 }
314 25573 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
315 25573 break;
316 }
317 120458 }
318
319 /* This used to be called "deloco" in FFmpeg
320 * and is actually an inverse reversible colorspace transformation */
321 #define YUV2RGB(NAME, TYPE) \
322 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
323 { \
324 int i; \
325 for (i = 0; i < size; i += 3 + alpha) { \
326 int g = dst [i + 1]; \
327 dst[i + 0] += g; \
328 dst[i + 2] += g; \
329 } \
330 }
331
332 YUV2RGB(rgb8, uint8_t)
333 YUV2RGB(rgb16, uint16_t)
334
335 565 static int percent_missing(PNGDecContext *s)
336 {
337
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 563 times.
565 if (s->interlace_type) {
338 2 return 100 - 100 * s->pass / (NB_PASSES - 1);
339 } else {
340 563 return 100 - 100 * s->y / s->cur_h;
341 }
342 }
343
344 /* process exactly one decompressed row */
345 116217 static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
346 {
347 uint8_t *ptr, *last_row;
348 int got_line;
349
350
2/2
✓ Branch 0 taken 115257 times.
✓ Branch 1 taken 960 times.
116217 if (!s->interlace_type) {
351 115257 ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
352
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 114643 times.
115257 if (s->y == 0)
353 614 last_row = s->last_row;
354 else
355 114643 last_row = ptr - dst_stride;
356
357 115257 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
358 last_row, s->row_size, s->bpp);
359 /* loco lags by 1 row so that it doesn't interfere with top prediction */
360
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 115257 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
115257 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
361 if (s->bit_depth == 16) {
362 deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2,
363 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
364 } else {
365 deloco_rgb8(ptr - dst_stride, s->row_size,
366 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
367 }
368 }
369 115257 s->y++;
370
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 114643 times.
115257 if (s->y == s->cur_h) {
371 614 s->pic_state |= PNG_ALLIMAGE;
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 614 times.
614 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
373 if (s->bit_depth == 16) {
374 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
375 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
376 } else {
377 deloco_rgb8(ptr, s->row_size,
378 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
379 }
380 }
381 }
382 } else {
383 960 got_line = 0;
384 for (;;) {
385 4540 ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
386
2/2
✓ Branch 0 taken 1916 times.
✓ Branch 1 taken 2624 times.
4540 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
387 /* if we already read one row, it is time to stop to
388 * wait for the next one */
389
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 960 times.
1916 if (got_line)
390 956 break;
391 960 ff_png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
392 s->last_row, s->pass_row_size, s->bpp);
393 960 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
394 960 FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
395 960 got_line = 1;
396 }
397
2/2
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 768 times.
3584 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
398 2816 png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
399 2816 s->color_type, s->last_row);
400 }
401 3584 s->y++;
402
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3556 times.
3584 if (s->y == s->cur_h) {
403 28 memset(s->last_row, 0, s->row_size);
404 for (;;) {
405
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (s->pass == NB_PASSES - 1) {
406 4 s->pic_state |= PNG_ALLIMAGE;
407 4 goto the_end;
408 } else {
409 24 s->pass++;
410 24 s->y = 0;
411 24 s->pass_row_size = ff_png_pass_row_size(s->pass,
412 s->bits_per_pixel,
413 s->cur_w);
414 24 s->crow_size = s->pass_row_size + 1;
415
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s->pass_row_size != 0)
416 24 break;
417 /* skip pass if empty row */
418 }
419 }
420 }
421 }
422 960 the_end:;
423 }
424 116217 }
425
426 15955 static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
427 uint8_t *dst, ptrdiff_t dst_stride)
428 {
429 int ret;
430 15955 s->zstream.avail_in = bytestream2_get_bytes_left(gb);
431 15955 s->zstream.next_in = gb->buffer;
432
433 /* decode one line if possible */
434
2/2
✓ Branch 0 taken 131529 times.
✓ Branch 1 taken 15955 times.
147484 while (s->zstream.avail_in > 0) {
435 131529 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
436
3/4
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 130911 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 618 times.
131529 if (ret != Z_OK && ret != Z_STREAM_END) {
437 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
438 return AVERROR_EXTERNAL;
439 }
440
2/2
✓ Branch 0 taken 116217 times.
✓ Branch 1 taken 15312 times.
131529 if (s->zstream.avail_out == 0) {
441
1/2
✓ Branch 0 taken 116217 times.
✗ Branch 1 not taken.
116217 if (!(s->pic_state & PNG_ALLIMAGE)) {
442 116217 png_handle_row(s, dst, dst_stride);
443 }
444 116217 s->zstream.avail_out = s->crow_size;
445 116217 s->zstream.next_out = s->crow_buf;
446 }
447
3/4
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 130911 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 618 times.
131529 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
448 av_log(s->avctx, AV_LOG_WARNING,
449 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
450 return 0;
451 }
452 }
453 15955 return 0;
454 }
455
456 2 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
457 const uint8_t *data_end)
458 {
459 z_stream zstream;
460 unsigned char *buf;
461 unsigned buf_size;
462 int ret;
463
464 2 zstream.zalloc = ff_png_zalloc;
465 2 zstream.zfree = ff_png_zfree;
466 2 zstream.opaque = NULL;
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (inflateInit(&zstream) != Z_OK)
468 return AVERROR_EXTERNAL;
469 2 zstream.next_in = data;
470 2 zstream.avail_in = data_end - data;
471 2 av_bprint_init(bp, 0, AV_BPRINT_SIZE_UNLIMITED);
472
473
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 while (zstream.avail_in > 0) {
474 6 av_bprint_get_buffer(bp, 2, &buf, &buf_size);
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (buf_size < 2) {
476 ret = AVERROR(ENOMEM);
477 goto fail;
478 }
479 6 zstream.next_out = buf;
480 6 zstream.avail_out = buf_size - 1;
481 6 ret = inflate(&zstream, Z_PARTIAL_FLUSH);
482
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
6 if (ret != Z_OK && ret != Z_STREAM_END) {
483 ret = AVERROR_EXTERNAL;
484 goto fail;
485 }
486 6 bp->len += zstream.next_out - buf;
487
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (ret == Z_STREAM_END)
488 2 break;
489 }
490 2 inflateEnd(&zstream);
491 2 bp->str[bp->len] = 0;
492 2 return 0;
493
494 fail:
495 inflateEnd(&zstream);
496 av_bprint_finalize(bp, NULL);
497 return ret;
498 }
499
500 234 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
501 {
502 234 size_t extra = 0, i;
503 uint8_t *out, *q;
504
505
2/2
✓ Branch 0 taken 3815 times.
✓ Branch 1 taken 234 times.
4049 for (i = 0; i < size_in; i++)
506 3815 extra += in[i] >= 0x80;
507
2/4
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 234 times.
234 if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
508 return NULL;
509 234 q = out = av_malloc(size_in + extra + 1);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
234 if (!out)
511 return NULL;
512
2/2
✓ Branch 0 taken 3815 times.
✓ Branch 1 taken 234 times.
4049 for (i = 0; i < size_in; i++) {
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3815 times.
3815 if (in[i] >= 0x80) {
514 *(q++) = 0xC0 | (in[i] >> 6);
515 *(q++) = 0x80 | (in[i] & 0x3F);
516 } else {
517 3815 *(q++) = in[i];
518 }
519 }
520 234 *(q++) = 0;
521 234 return out;
522 }
523
524 117 static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
525 {
526 int ret, method;
527 117 const uint8_t *data = gb->buffer;
528 117 const uint8_t *data_end = gb->buffer_end;
529 117 const uint8_t *keyword = data;
530 117 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
531 117 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
532 unsigned text_len;
533 AVBPrint bp;
534
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (!keyword_end)
536 return AVERROR_INVALIDDATA;
537 117 data = keyword_end + 1;
538
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (compressed) {
540 if (data == data_end)
541 return AVERROR_INVALIDDATA;
542 method = *(data++);
543 if (method)
544 return AVERROR_INVALIDDATA;
545 if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
546 return ret;
547 text_len = bp.len;
548 ret = av_bprint_finalize(&bp, (char **)&text);
549 if (ret < 0)
550 return ret;
551 } else {
552 117 text = (uint8_t *)data;
553 117 text_len = data_end - text;
554 }
555
556 117 kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
557 117 txt_utf8 = iso88591_to_utf8(text, text_len);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (text != data)
559 av_free(text);
560
2/4
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 117 times.
117 if (!(kw_utf8 && txt_utf8)) {
561 av_free(kw_utf8);
562 av_free(txt_utf8);
563 return AVERROR(ENOMEM);
564 }
565
566 117 av_dict_set(&s->frame_metadata, kw_utf8, txt_utf8,
567 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
568 117 return 0;
569 }
570
571 373 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
572 GetByteContext *gb)
573 {
574
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 373 times.
373 if (bytestream2_get_bytes_left(gb) != 13)
575 return AVERROR_INVALIDDATA;
576
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 373 times.
373 if (s->pic_state & PNG_IDAT) {
578 av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
579 return AVERROR_INVALIDDATA;
580 }
581
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 373 times.
373 if (s->hdr_state & PNG_IHDR) {
583 av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
584 return AVERROR_INVALIDDATA;
585 }
586
587 373 s->width = s->cur_w = bytestream2_get_be32(gb);
588 373 s->height = s->cur_h = bytestream2_get_be32(gb);
589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 373 times.
373 if (av_image_check_size(s->width, s->height, 0, avctx)) {
590 s->cur_w = s->cur_h = s->width = s->height = 0;
591 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
592 return AVERROR_INVALIDDATA;
593 }
594 373 s->bit_depth = bytestream2_get_byte(gb);
595
3/6
✓ Branch 0 taken 373 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 373 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 373 times.
✗ Branch 5 not taken.
373 if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
596
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 333 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
373 s->bit_depth != 8 && s->bit_depth != 16) {
597 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
598 goto error;
599 }
600 373 s->color_type = bytestream2_get_byte(gb);
601 373 s->compression_type = bytestream2_get_byte(gb);
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 373 times.
373 if (s->compression_type) {
603 av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
604 goto error;
605 }
606 373 s->filter_type = bytestream2_get_byte(gb);
607 373 s->interlace_type = bytestream2_get_byte(gb);
608 373 s->hdr_state |= PNG_IHDR;
609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 373 times.
373 if (avctx->debug & FF_DEBUG_PICT_INFO)
610 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
611 "compression_type=%d filter_type=%d interlace_type=%d\n",
612 s->width, s->height, s->bit_depth, s->color_type,
613 s->compression_type, s->filter_type, s->interlace_type);
614
615 373 return 0;
616 error:
617 s->cur_w = s->cur_h = s->width = s->height = 0;
618 s->bit_depth = 8;
619 return AVERROR_INVALIDDATA;
620 }
621
622 341 static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
623 GetByteContext *gb)
624 {
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 341 times.
341 if (s->pic_state & PNG_IDAT) {
626 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
627 return AVERROR_INVALIDDATA;
628 }
629 341 avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
630 341 avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
631
2/4
✓ Branch 0 taken 341 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 341 times.
341 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
632 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
633 341 bytestream2_skip(gb, 1); /* unit specifier */
634
635 341 return 0;
636 }
637
638 15955 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
639 GetByteContext *gb, AVFrame *p)
640 {
641 int ret;
642
2/2
✓ Branch 0 taken 1954 times.
✓ Branch 1 taken 14001 times.
15955 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
643
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15955 times.
15955 if (!(s->hdr_state & PNG_IHDR)) {
645 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
646 return AVERROR_INVALIDDATA;
647 }
648
2/2
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 15337 times.
15955 if (!(s->pic_state & PNG_IDAT)) {
649 /* init image info */
650 618 ret = ff_set_dimensions(avctx, s->width, s->height);
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 618 times.
618 if (ret < 0)
652 return ret;
653
654 618 s->channels = ff_png_get_nb_channels(s->color_type);
655 618 s->bits_per_pixel = s->bit_depth * s->channels;
656 618 s->bpp = (s->bits_per_pixel + 7) >> 3;
657 618 s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
658
659
4/6
✓ Branch 0 taken 618 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 618 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 578 times.
✓ Branch 5 taken 40 times.
618 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
660
2/2
✓ Branch 0 taken 452 times.
✓ Branch 1 taken 126 times.
578 s->color_type == PNG_COLOR_TYPE_RGB) {
661 452 avctx->pix_fmt = AV_PIX_FMT_RGB24;
662
4/6
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 166 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 126 times.
✓ Branch 5 taken 40 times.
166 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
663
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 47 times.
126 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
664 79 avctx->pix_fmt = AV_PIX_FMT_RGBA;
665
4/6
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47 times.
✓ Branch 5 taken 40 times.
87 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
666
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 43 times.
47 s->color_type == PNG_COLOR_TYPE_GRAY) {
667 4 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
668
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 43 times.
83 } else if (s->bit_depth == 16 &&
669
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 24 times.
40 s->color_type == PNG_COLOR_TYPE_GRAY) {
670 16 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
671
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 43 times.
67 } else if (s->bit_depth == 16 &&
672
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 s->color_type == PNG_COLOR_TYPE_RGB) {
673 18 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
674
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 43 times.
49 } else if (s->bit_depth == 16 &&
675
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
676 4 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
677
5/8
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 4 times.
45 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
678
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 s->color_type == PNG_COLOR_TYPE_PALETTE) {
679 41 avctx->pix_fmt = AV_PIX_FMT_PAL8;
680
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) {
681 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
682
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (s->bit_depth == 8 &&
683
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
684 2 avctx->pix_fmt = AV_PIX_FMT_YA8;
685
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (s->bit_depth == 16 &&
686
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
687 2 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
688 } else {
689 avpriv_report_missing_feature(avctx,
690 "Bit depth %d color type %d",
691 s->bit_depth, s->color_type);
692 return AVERROR_PATCHWELCOME;
693 }
694
695
4/4
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 440 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 41 times.
618 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
696
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) {
697 137 case AV_PIX_FMT_RGB24:
698 137 avctx->pix_fmt = AV_PIX_FMT_RGBA;
699 137 break;
700
701 case AV_PIX_FMT_RGB48BE:
702 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
703 break;
704
705 case AV_PIX_FMT_GRAY8:
706 avctx->pix_fmt = AV_PIX_FMT_YA8;
707 break;
708
709 case AV_PIX_FMT_GRAY16BE:
710 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
711 break;
712
713 default:
714 avpriv_request_sample(avctx, "bit depth %d "
715 "and color type %d with TRNS",
716 s->bit_depth, s->color_type);
717 return AVERROR_INVALIDDATA;
718 }
719
720 137 s->bpp += byte_depth;
721 }
722
723 618 ff_thread_release_buffer(avctx, &s->picture);
724
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 618 times.
618 if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
725 return ret;
726
727 618 p->pict_type = AV_PICTURE_TYPE_I;
728 618 p->key_frame = 1;
729 618 p->interlaced_frame = !!s->interlace_type;
730
731 618 ff_thread_finish_setup(avctx);
732
733 /* compute the compressed row size */
734
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 4 times.
618 if (!s->interlace_type) {
735 614 s->crow_size = s->row_size + 1;
736 } else {
737 4 s->pass = 0;
738 4 s->pass_row_size = ff_png_pass_row_size(s->pass,
739 s->bits_per_pixel,
740 s->cur_w);
741 4 s->crow_size = s->pass_row_size + 1;
742 }
743 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
744 s->row_size, s->crow_size);
745
746 /* copy the palette if needed */
747
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 577 times.
618 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
748 41 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
749 /* empty row is used if differencing to the first row */
750 618 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 618 times.
618 if (!s->last_row)
752 return AVERROR_INVALIDDATA;
753
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 4 times.
618 if (s->interlace_type ||
754
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 531 times.
614 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
755 87 av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!s->tmp_row)
757 return AVERROR_INVALIDDATA;
758 }
759 /* compressed row */
760 618 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 618 times.
618 if (!s->buffer)
762 return AVERROR(ENOMEM);
763
764 /* we want crow_buf+1 to be 16-byte aligned */
765 618 s->crow_buf = s->buffer + 15;
766 618 s->zstream.avail_out = s->crow_size;
767 618 s->zstream.next_out = s->crow_buf;
768 }
769
770 15955 s->pic_state |= PNG_IDAT;
771
772 /* set image to non-transparent bpp while decompressing */
773
4/4
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 15767 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 41 times.
15955 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
774 147 s->bpp -= byte_depth;
775
776 15955 ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
777
778
4/4
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 15767 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 41 times.
15955 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
779 147 s->bpp += byte_depth;
780
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15955 times.
15955 if (ret < 0)
782 return ret;
783
784 15955 return 0;
785 }
786
787 2 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
788 GetByteContext *gb)
789 {
790 2 int length = bytestream2_get_bytes_left(gb);
791 int n, i, r, g, b;
792
793
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((length % 3) != 0 || length > 256 * 3)
794 return AVERROR_INVALIDDATA;
795 /* read the palette */
796 2 n = length / 3;
797
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 2 times.
474 for (i = 0; i < n; i++) {
798 472 r = bytestream2_get_byte(gb);
799 472 g = bytestream2_get_byte(gb);
800 472 b = bytestream2_get_byte(gb);
801 472 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
802 }
803
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
42 for (; i < 256; i++)
804 40 s->palette[i] = (0xFFU << 24);
805 2 s->hdr_state |= PNG_PLTE;
806
807 2 return 0;
808 }
809
810 6 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
811 GetByteContext *gb)
812 {
813 6 int length = bytestream2_get_bytes_left(gb);
814 int v, i;
815
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!(s->hdr_state & PNG_IHDR)) {
817 av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
818 return AVERROR_INVALIDDATA;
819 }
820
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->pic_state & PNG_IDAT) {
822 av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
823 return AVERROR_INVALIDDATA;
824 }
825
826
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
827
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (length > 256 || !(s->hdr_state & PNG_PLTE))
828 return AVERROR_INVALIDDATA;
829
830
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2 times.
36 for (i = 0; i < length; i++) {
831 34 unsigned v = bytestream2_get_byte(gb);
832 34 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
833 }
834
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) {
835
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) ||
836
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) ||
837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 s->bit_depth == 1)
838 return AVERROR_INVALIDDATA;
839
840
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < length / 2; i++) {
841 /* only use the least significant bits */
842 12 v = av_mod_uintp2(bytestream2_get_be16(gb), s->bit_depth);
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->bit_depth > 8)
845 AV_WB16(&s->transparent_color_be[2 * i], v);
846 else
847 12 s->transparent_color_be[i] = v;
848 }
849 } else {
850 return AVERROR_INVALIDDATA;
851 }
852
853 6 s->has_trns = 1;
854
855 6 return 0;
856 }
857
858 2 static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb, AVFrame *f)
859 {
860 2 int ret, cnt = 0;
861 AVBPrint bp;
862
863
3/4
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
44 while ((s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (cnt > 80) {
865 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
866 ret = AVERROR_INVALIDDATA;
867 goto fail;
868 }
869
870
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_byte(gb) != 0) {
871 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
872 ret = AVERROR_INVALIDDATA;
873 goto fail;
874 }
875
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end)) < 0)
877 return ret;
878
879 2 av_freep(&s->iccp_data);
880 2 ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
882 return ret;
883 2 s->iccp_data_len = bp.len;
884
885 2 return 0;
886 fail:
887 s->iccp_name[0] = 0;
888 return ret;
889 }
890
891 static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
892 {
893 if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
894 int i, j, k;
895 uint8_t *pd = p->data[0];
896 for (j = 0; j < s->height; j++) {
897 i = s->width / 8;
898 for (k = 7; k >= 1; k--)
899 if ((s->width&7) >= k)
900 pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
901 for (i--; i >= 0; i--) {
902 pd[8*i + 7]= pd[i] & 1;
903 pd[8*i + 6]= (pd[i]>>1) & 1;
904 pd[8*i + 5]= (pd[i]>>2) & 1;
905 pd[8*i + 4]= (pd[i]>>3) & 1;
906 pd[8*i + 3]= (pd[i]>>4) & 1;
907 pd[8*i + 2]= (pd[i]>>5) & 1;
908 pd[8*i + 1]= (pd[i]>>6) & 1;
909 pd[8*i + 0]= pd[i]>>7;
910 }
911 pd += p->linesize[0];
912 }
913 } else if (s->bits_per_pixel == 2) {
914 int i, j;
915 uint8_t *pd = p->data[0];
916 for (j = 0; j < s->height; j++) {
917 i = s->width / 4;
918 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
919 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
920 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
921 if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
922 for (i--; i >= 0; i--) {
923 pd[4*i + 3]= pd[i] & 3;
924 pd[4*i + 2]= (pd[i]>>2) & 3;
925 pd[4*i + 1]= (pd[i]>>4) & 3;
926 pd[4*i + 0]= pd[i]>>6;
927 }
928 } else {
929 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
930 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
931 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
932 for (i--; i >= 0; i--) {
933 pd[4*i + 3]= ( pd[i] & 3)*0x55;
934 pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
935 pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
936 pd[4*i + 0]= ( pd[i]>>6 )*0x55;
937 }
938 }
939 pd += p->linesize[0];
940 }
941 } else if (s->bits_per_pixel == 4) {
942 int i, j;
943 uint8_t *pd = p->data[0];
944 for (j = 0; j < s->height; j++) {
945 i = s->width/2;
946 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
947 if (s->width&1) pd[2*i+0]= pd[i]>>4;
948 for (i--; i >= 0; i--) {
949 pd[2*i + 1] = pd[i] & 15;
950 pd[2*i + 0] = pd[i] >> 4;
951 }
952 } else {
953 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
954 for (i--; i >= 0; i--) {
955 pd[2*i + 1] = (pd[i] & 15) * 0x11;
956 pd[2*i + 0] = (pd[i] >> 4) * 0x11;
957 }
958 }
959 pd += p->linesize[0];
960 }
961 }
962 }
963
964 261 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
965 GetByteContext *gb)
966 {
967 uint32_t sequence_number;
968 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
969
970
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
261 if (bytestream2_get_bytes_left(gb) != 26)
971 return AVERROR_INVALIDDATA;
972
973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (!(s->hdr_state & PNG_IHDR)) {
974 av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
975 return AVERROR_INVALIDDATA;
976 }
977
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (s->pic_state & PNG_IDAT) {
979 av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
980 return AVERROR_INVALIDDATA;
981 }
982
983 261 s->last_w = s->cur_w;
984 261 s->last_h = s->cur_h;
985 261 s->last_x_offset = s->x_offset;
986 261 s->last_y_offset = s->y_offset;
987 261 s->last_dispose_op = s->dispose_op;
988
989 261 sequence_number = bytestream2_get_be32(gb);
990 261 cur_w = bytestream2_get_be32(gb);
991 261 cur_h = bytestream2_get_be32(gb);
992 261 x_offset = bytestream2_get_be32(gb);
993 261 y_offset = bytestream2_get_be32(gb);
994 261 bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
995 261 dispose_op = bytestream2_get_byte(gb);
996 261 blend_op = bytestream2_get_byte(gb);
997
998
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 246 times.
261 if (sequence_number == 0 &&
999
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 (cur_w != s->width ||
1000
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 cur_h != s->height ||
1001
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 x_offset != 0 ||
1002
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 y_offset != 0) ||
1003
2/4
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
✗ Branch 3 not taken.
261 cur_w <= 0 || cur_h <= 0 ||
1004
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 x_offset < 0 || y_offset < 0 ||
1005
2/4
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 261 times.
261 cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
1006 return AVERROR_INVALIDDATA;
1007
1008
3/4
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 218 times.
261 if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
1009 av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
1010 return AVERROR_INVALIDDATA;
1011 }
1012
1013
4/6
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 246 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
261 if ((sequence_number == 0 || !s->last_picture.f->data[0]) &&
1014 dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1015 // No previous frame to revert to for the first frame
1016 // Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
1017 dispose_op = APNG_DISPOSE_OP_BACKGROUND;
1018 }
1019
1020
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 39 times.
261 if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
1021
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
1022
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1023
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_PAL8 ||
1024
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
1025
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
1027 )) {
1028 // APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
1029 blend_op = APNG_BLEND_OP_SOURCE;
1030 }
1031
1032 261 s->cur_w = cur_w;
1033 261 s->cur_h = cur_h;
1034 261 s->x_offset = x_offset;
1035 261 s->y_offset = y_offset;
1036 261 s->dispose_op = dispose_op;
1037 261 s->blend_op = blend_op;
1038
1039 261 return 0;
1040 }
1041
1042 static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
1043 {
1044 int i, j;
1045 uint8_t *pd = p->data[0];
1046 uint8_t *pd_last = s->last_picture.f->data[0];
1047 int ls = av_image_get_linesize(p->format, s->width, 0);
1048
1049 ls = FFMIN(ls, s->width * s->bpp);
1050
1051 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1052 for (j = 0; j < s->height; j++) {
1053 for (i = 0; i < ls; i++)
1054 pd[i] += pd_last[i];
1055 pd += p->linesize[0];
1056 pd_last += s->last_picture.f->linesize[0];
1057 }
1058 }
1059
1060 // divide by 255 and round to nearest
1061 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
1062 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
1063
1064 61 static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
1065 AVFrame *p)
1066 {
1067 61 uint8_t *dst = p->data[0];
1068 61 ptrdiff_t dst_stride = p->linesize[0];
1069 61 const uint8_t *src = s->last_picture.f->data[0];
1070 61 ptrdiff_t src_stride = s->last_picture.f->linesize[0];
1071
1072 size_t x, y;
1073
1074
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER &&
1075
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2 times.
41 avctx->pix_fmt != AV_PIX_FMT_RGBA &&
1076
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 avctx->pix_fmt != AV_PIX_FMT_GRAY8A &&
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 avctx->pix_fmt != AV_PIX_FMT_PAL8) {
1078 avpriv_request_sample(avctx, "Blending with pixel format %s",
1079 av_get_pix_fmt_name(avctx->pix_fmt));
1080 return AVERROR_PATCHWELCOME;
1081 }
1082
1083 61 ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
1084
1085 // need to reset a rectangle to background:
1086
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 50 times.
61 if (s->last_dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1087 11 av_fast_malloc(&s->background_buf, &s->background_buf_allocated,
1088 11 src_stride * p->height);
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!s->background_buf)
1090 return AVERROR(ENOMEM);
1091
1092 11 memcpy(s->background_buf, src, src_stride * p->height);
1093
1094
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 11 times.
870 for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; y++) {
1095 859 memset(s->background_buf + src_stride * y +
1096 859 s->bpp * s->last_x_offset, 0, s->bpp * s->last_w);
1097 }
1098
1099 11 src = s->background_buf;
1100 }
1101
1102 // copy unchanged rectangles from the last frame
1103
2/2
✓ Branch 0 taken 2186 times.
✓ Branch 1 taken 61 times.
2247 for (y = 0; y < s->y_offset; y++)
1104 2186 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp);
1105
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++) {
1106 3320 memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * s->bpp);
1107 3320 memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * s->bpp,
1108 3320 src + y * src_stride + (s->x_offset + s->cur_w) * s->bpp,
1109 3320 (p->width - s->cur_w - s->x_offset) * s->bpp);
1110 }
1111
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++)
1112 2500 memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp);
1113
1114
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20 times.
61 if (s->blend_op == APNG_BLEND_OP_OVER) {
1115 // Perform blending
1116
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) {
1117 1912 uint8_t *foreground = dst + dst_stride * y + s->bpp * s->x_offset;
1118 1912 const uint8_t *background = src + src_stride * y + s->bpp * s->x_offset;
1119
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 += s->bpp, background += s->bpp) {
1120 size_t b;
1121 uint8_t foreground_alpha, background_alpha, output_alpha;
1122 uint8_t output[10];
1123
1124 // Since we might be blending alpha onto alpha, we use the following equations:
1125 // output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
1126 // output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
1127
1128
2/4
✓ Branch 0 taken 2049 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 141216 times.
✗ Branch 3 not taken.
143265 switch (avctx->pix_fmt) {
1129 2049 case AV_PIX_FMT_RGBA:
1130 2049 foreground_alpha = foreground[3];
1131 2049 background_alpha = background[3];
1132 2049 break;
1133
1134 case AV_PIX_FMT_GRAY8A:
1135 foreground_alpha = foreground[1];
1136 background_alpha = background[1];
1137 break;
1138
1139 141216 case AV_PIX_FMT_PAL8:
1140 141216 foreground_alpha = s->palette[foreground[0]] >> 24;
1141 141216 background_alpha = s->palette[background[0]] >> 24;
1142 141216 break;
1143 }
1144
1145
2/2
✓ Branch 0 taken 21682 times.
✓ Branch 1 taken 121583 times.
143265 if (foreground_alpha == 255)
1146 143265 continue;
1147
1148
1/2
✓ Branch 0 taken 121583 times.
✗ Branch 1 not taken.
121583 if (foreground_alpha == 0) {
1149 121583 memcpy(foreground, background, s->bpp);
1150 121583 continue;
1151 }
1152
1153 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1154 // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first
1155 avpriv_request_sample(avctx, "Alpha blending palette samples");
1156 continue;
1157 }
1158
1159 output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
1160
1161 av_assert0(s->bpp <= 10);
1162
1163 for (b = 0; b < s->bpp - 1; ++b) {
1164 if (output_alpha == 0) {
1165 output[b] = 0;
1166 } else if (background_alpha == 255) {
1167 output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
1168 } else {
1169 output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
1170 }
1171 }
1172 output[b] = output_alpha;
1173 memcpy(foreground, output, s->bpp);
1174 }
1175 }
1176 }
1177
1178 61 return 0;
1179 }
1180
1181 634 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
1182 AVFrame *p, const AVPacket *avpkt)
1183 {
1184 634 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1185 uint32_t tag, length;
1186 634 int decode_next_dat = 0;
1187 int i, ret;
1188
1189 17258 for (;;) {
1190 GetByteContext gb_chunk;
1191
1192 17892 length = bytestream2_get_bytes_left(&s->gb);
1193
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 17563 times.
17892 if (length <= 0) {
1194
1195
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 276 times.
329 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1196
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 avctx->skip_frame == AVDISCARD_ALL) {
1197 68 return 0;
1198 }
1199
1200
2/4
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 276 times.
✗ Branch 3 not taken.
276 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
1201
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 261 times.
276 if (!(s->pic_state & PNG_IDAT))
1202 15 return 0;
1203 else
1204 565 goto exit_loop;
1205 }
1206 av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
1207 if ( s->pic_state & PNG_ALLIMAGE
1208 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
1209 goto exit_loop;
1210 ret = AVERROR_INVALIDDATA;
1211 1 goto fail;
1212 }
1213
1214 17563 length = bytestream2_get_be32(&s->gb);
1215
3/4
✓ Branch 0 taken 17563 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 17562 times.
17563 if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
1216 1 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
1217 1 ret = AVERROR_INVALIDDATA;
1218 1 goto fail;
1219 }
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17562 times.
17562 if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1221 uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1222 uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1223 if (crc_sig ^ crc_cal) {
1224 av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1225 if (avctx->err_recognition & AV_EF_EXPLODE) {
1226 av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1227 ret = AVERROR_INVALIDDATA;
1228 goto fail;
1229 }
1230 av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1231 bytestream2_skip(&s->gb, length + 8); /* tag */
1232 }
1233 }
1234 17562 tag = bytestream2_get_le32(&s->gb);
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17562 times.
17562 if (avctx->debug & FF_DEBUG_STARTCODE)
1236 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
1237 av_fourcc2str(tag), length);
1238
1239 17562 bytestream2_init(&gb_chunk, s->gb.buffer, length);
1240 17562 bytestream2_skip(&s->gb, length + 4);
1241
1242
2/2
✓ Branch 0 taken 13823 times.
✓ Branch 1 taken 3739 times.
17562 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1243
2/2
✓ Branch 0 taken 1003 times.
✓ Branch 1 taken 12820 times.
13823 avctx->skip_frame == AVDISCARD_ALL) {
1244
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 111 times.
1003 switch(tag) {
1245 892 case MKTAG('I', 'H', 'D', 'R'):
1246 case MKTAG('p', 'H', 'Y', 's'):
1247 case MKTAG('t', 'E', 'X', 't'):
1248 case MKTAG('I', 'D', 'A', 'T'):
1249 case MKTAG('t', 'R', 'N', 'S'):
1250 892 break;
1251 111 default:
1252 111 continue;
1253 }
1254 }
1255
1256
13/15
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 341 times.
✓ Branch 2 taken 261 times.
✓ Branch 3 taken 3120 times.
✓ Branch 4 taken 12835 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 117 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 14 times.
✓ Branch 13 taken 304 times.
✓ Branch 14 taken 74 times.
17451 switch (tag) {
1257 373 case MKTAG('I', 'H', 'D', 'R'):
1258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 373 times.
373 if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
1259 goto fail;
1260 373 break;
1261 341 case MKTAG('p', 'H', 'Y', 's'):
1262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 341 times.
341 if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
1263 goto fail;
1264 341 break;
1265 261 case MKTAG('f', 'c', 'T', 'L'):
1266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1267 continue;
1268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
261 if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
1269 goto fail;
1270 261 decode_next_dat = 1;
1271 261 break;
1272 3120 case MKTAG('f', 'd', 'A', 'T'):
1273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3120 times.
3120 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1274 continue;
1275
2/4
✓ Branch 0 taken 3120 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3120 times.
3120 if (!decode_next_dat || bytestream2_get_bytes_left(&gb_chunk) < 4) {
1276 ret = AVERROR_INVALIDDATA;
1277 goto fail;
1278 }
1279 3120 bytestream2_get_be32(&gb_chunk);
1280 /* fallthrough */
1281 15955 case MKTAG('I', 'D', 'A', 'T'):
1282
3/4
✓ Branch 0 taken 3435 times.
✓ Branch 1 taken 12520 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3435 times.
15955 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1283 continue;
1284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15955 times.
15955 if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
1285 goto fail;
1286 15955 break;
1287 2 case MKTAG('P', 'L', 'T', 'E'):
1288 2 decode_plte_chunk(avctx, s, &gb_chunk);
1289 2 break;
1290 6 case MKTAG('t', 'R', 'N', 'S'):
1291 6 decode_trns_chunk(avctx, s, &gb_chunk);
1292 6 break;
1293 117 case MKTAG('t', 'E', 'X', 't'):
1294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
117 if (decode_text_chunk(s, &gb_chunk, 0) < 0)
1295 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1296 117 break;
1297 case MKTAG('z', 'T', 'X', 't'):
1298 if (decode_text_chunk(s, &gb_chunk, 1) < 0)
1299 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1300 break;
1301 case MKTAG('s', 'T', 'E', 'R'): {
1302 int mode = bytestream2_get_byte(&gb_chunk);
1303
1304 if (mode == 0 || mode == 1) {
1305 s->stereo_mode = mode;
1306 } else {
1307 av_log(avctx, AV_LOG_WARNING,
1308 "Unknown value in sTER chunk (%d)\n", mode);
1309 }
1310 break;
1311 }
1312 2 case MKTAG('i', 'C', 'C', 'P'): {
1313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = decode_iccp_chunk(s, &gb_chunk, p)) < 0)
1314 goto fail;
1315 2 break;
1316 }
1317 2 case MKTAG('c', 'H', 'R', 'M'): {
1318 2 s->have_chrm = 1;
1319
1320 2 s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1321 2 s->white_point[1] = bytestream2_get_be32(&gb_chunk);
1322
1323 /* RGB Primaries */
1324
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++) {
1325 6 s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
1326 6 s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
1327 }
1328
1329 2 break;
1330 }
1331 14 case MKTAG('g', 'A', 'M', 'A'): {
1332 AVBPrint bp;
1333 char *gamma_str;
1334 14 int num = bytestream2_get_be32(&gb_chunk);
1335
1336 14 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1337 14 av_bprintf(&bp, "%i/%i", num, 100000);
1338 14 ret = av_bprint_finalize(&bp, &gamma_str);
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
1340 return ret;
1341
1342 14 av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
1343
1344 14 break;
1345 }
1346 304 case MKTAG('I', 'E', 'N', 'D'):
1347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (!(s->pic_state & PNG_ALLIMAGE))
1348 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1350 ret = AVERROR_INVALIDDATA;
1351 goto fail;
1352 }
1353 304 goto exit_loop;
1354 }
1355 }
1356 565 exit_loop:
1357
1358
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 261 times.
565 if (avctx->codec_id == AV_CODEC_ID_PNG &&
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 avctx->skip_frame == AVDISCARD_ALL) {
1360 return 0;
1361 }
1362
1363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 565 times.
565 if (percent_missing(s) > avctx->discard_damaged_percentage)
1364 return AVERROR_INVALIDDATA;
1365
1366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 565 times.
565 if (s->bits_per_pixel <= 4)
1367 handle_small_bpp(s, p);
1368
1369 /* apply transparency if needed */
1370
4/4
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 41 times.
565 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
1371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
1372 136 size_t raw_bpp = s->bpp - byte_depth;
1373 unsigned x, y;
1374
1375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 av_assert0(s->bit_depth > 1);
1376
1377
2/2
✓ Branch 0 taken 17610 times.
✓ Branch 1 taken 136 times.
17746 for (y = 0; y < s->height; ++y) {
1378 17610 uint8_t *row = &p->data[0][p->linesize[0] * y];
1379
1380
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) {
1381 uint8_t *pixel = &row[2 * s->width - 1];
1382 uint8_t *rowp = &row[1 * s->width - 1];
1383 int tcolor = s->transparent_color_be[0];
1384 for (x = s->width; x > 0; --x) {
1385 *pixel-- = *rowp == tcolor ? 0 : 0xff;
1386 *pixel-- = *rowp--;
1387 }
1388
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) {
1389 17610 uint8_t *pixel = &row[4 * s->width - 1];
1390 17610 uint8_t *rowp = &row[3 * s->width - 1];
1391 17610 int tcolor = AV_RL24(s->transparent_color_be);
1392
2/2
✓ Branch 0 taken 1577700 times.
✓ Branch 1 taken 17610 times.
1595310 for (x = s->width; x > 0; --x) {
1393
2/2
✓ Branch 0 taken 82906 times.
✓ Branch 1 taken 1494794 times.
1577700 *pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
1394 1577700 *pixel-- = *rowp--;
1395 1577700 *pixel-- = *rowp--;
1396 1577700 *pixel-- = *rowp--;
1397 }
1398 } else {
1399 /* since we're updating in-place, we have to go from right to left */
1400 for (x = s->width; x > 0; --x) {
1401 uint8_t *pixel = &row[s->bpp * (x - 1)];
1402 memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
1403
1404 if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
1405 memset(&pixel[raw_bpp], 0, byte_depth);
1406 } else {
1407 memset(&pixel[raw_bpp], 0xff, byte_depth);
1408 }
1409 }
1410 }
1411 }
1412 }
1413
1414 /* handle P-frames only if a predecessor frame is available */
1415
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 61 times.
565 if (s->last_picture.f->data[0]) {
1416
3/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 443 times.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
504 if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1417
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->width == p->width
1418
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->height== p->height
1419
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 && s->last_picture.f->format== p->format
1420 ) {
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1422 handle_p_frame_png(s, p);
1423 61 else if (CONFIG_APNG_DECODER &&
1424
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 &&
1425 61 (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1426 goto fail;
1427 }
1428 }
1429 565 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1430
1431 565 return 0;
1432
1433 1 fail:
1434 1 ff_thread_report_progress(&s->picture, INT_MAX, 0);
1435 1 return ret;
1436 }
1437
1438 619 static void clear_frame_metadata(PNGDecContext *s)
1439 {
1440 619 av_freep(&s->iccp_data);
1441 619 s->iccp_data_len = 0;
1442 619 s->iccp_name[0] = 0;
1443
1444 619 s->stereo_mode = -1;
1445
1446 619 s->have_chrm = 0;
1447
1448 619 av_dict_free(&s->frame_metadata);
1449 619 }
1450
1451 565 static int output_frame(PNGDecContext *s, AVFrame *f,
1452 const AVFrame *src)
1453 {
1454 int ret;
1455
1456 565 ret = av_frame_ref(f, src);
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 565 times.
565 if (ret < 0)
1458 return ret;
1459
1460
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 563 times.
565 if (s->iccp_data) {
1461 2 AVFrameSideData *sd = av_frame_new_side_data(f, AV_FRAME_DATA_ICC_PROFILE, s->iccp_data_len);
1462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sd) {
1463 ret = AVERROR(ENOMEM);
1464 goto fail;
1465 }
1466 2 memcpy(sd->data, s->iccp_data, s->iccp_data_len);
1467
1468 2 av_dict_set(&sd->metadata, "name", s->iccp_name, 0);
1469 }
1470
1471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 565 times.
565 if (s->stereo_mode >= 0) {
1472 AVStereo3D *stereo3d = av_stereo3d_create_side_data(f);
1473 if (!stereo3d) {
1474 ret = AVERROR(ENOMEM);
1475 goto fail;
1476 }
1477
1478 stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1479 stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
1480 }
1481
1482
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 563 times.
565 if (s->have_chrm) {
1483 2 AVMasteringDisplayMetadata *mdm = av_mastering_display_metadata_create_side_data(f);
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!mdm) {
1485 ret = AVERROR(ENOMEM);
1486 goto fail;
1487 }
1488
1489 2 mdm->white_point[0] = av_make_q(s->white_point[0], 100000);
1490 2 mdm->white_point[1] = av_make_q(s->white_point[1], 100000);
1491
1492 /* RGB Primaries */
1493
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++) {
1494 6 mdm->display_primaries[i][0] = av_make_q(s->display_primaries[i][0], 100000);
1495 6 mdm->display_primaries[i][1] = av_make_q(s->display_primaries[i][1], 100000);
1496 }
1497
1498 2 mdm->has_primaries = 1;
1499 }
1500
1501 565 FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
1502
1503 565 return 0;
1504 fail:
1505 av_frame_unref(f);
1506 return ret;
1507 }
1508
1509 #if CONFIG_PNG_DECODER
1510 358 static int decode_frame_png(AVCodecContext *avctx,
1511 void *data, int *got_frame,
1512 AVPacket *avpkt)
1513 {
1514 358 PNGDecContext *const s = avctx->priv_data;
1515 358 const uint8_t *buf = avpkt->data;
1516 358 int buf_size = avpkt->size;
1517 358 AVFrame *dst_frame = data;
1518 358 AVFrame *p = s->picture.f;
1519 int64_t sig;
1520 int ret;
1521
1522 358 clear_frame_metadata(s);
1523
1524 358 bytestream2_init(&s->gb, buf, buf_size);
1525
1526 /* check signature */
1527 358 sig = bytestream2_get_be64(&s->gb);
1528
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 358 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
358 if (sig != PNGSIG &&
1529 sig != MNGSIG) {
1530 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
1531 return AVERROR_INVALIDDATA;
1532 }
1533
1534 358 s->y = s->has_trns = 0;
1535 358 s->hdr_state = 0;
1536 358 s->pic_state = 0;
1537
1538 /* init the zlib */
1539 358 s->zstream.zalloc = ff_png_zalloc;
1540 358 s->zstream.zfree = ff_png_zfree;
1541 358 s->zstream.opaque = NULL;
1542 358 ret = inflateInit(&s->zstream);
1543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 358 times.
358 if (ret != Z_OK) {
1544 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1545 return AVERROR_EXTERNAL;
1546 }
1547
1548
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 357 times.
358 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1549 1 goto the_end;
1550
1551
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 304 times.
357 if (avctx->skip_frame == AVDISCARD_ALL) {
1552 53 *got_frame = 0;
1553 53 ret = bytestream2_tell(&s->gb);
1554 53 goto the_end;
1555 }
1556
1557 304 ret = output_frame(s, dst_frame, s->picture.f);
1558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (ret < 0)
1559 goto the_end;
1560
1561
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 4 times.
304 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1562 300 ff_thread_release_buffer(avctx, &s->last_picture);
1563 300 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1564 }
1565
1566 304 *got_frame = 1;
1567
1568 304 ret = bytestream2_tell(&s->gb);
1569 358 the_end:
1570 358 inflateEnd(&s->zstream);
1571 358 s->crow_buf = NULL;
1572 358 return ret;
1573 }
1574 #endif
1575
1576 #if CONFIG_APNG_DECODER
1577 261 static int decode_frame_apng(AVCodecContext *avctx,
1578 void *data, int *got_frame,
1579 AVPacket *avpkt)
1580 {
1581 261 PNGDecContext *const s = avctx->priv_data;
1582 261 AVFrame *dst_frame = data;
1583 int ret;
1584 261 AVFrame *p = s->picture.f;
1585
1586 261 clear_frame_metadata(s);
1587
1588
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 246 times.
261 if (!(s->hdr_state & PNG_IHDR)) {
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!avctx->extradata_size)
1590 return AVERROR_INVALIDDATA;
1591
1592 /* only init fields, there is no zlib use in extradata */
1593 15 s->zstream.zalloc = ff_png_zalloc;
1594 15 s->zstream.zfree = ff_png_zfree;
1595
1596 15 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1597
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1598 goto end;
1599 }
1600
1601 /* reset state for a new frame */
1602
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
261 if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1603 av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1604 ret = AVERROR_EXTERNAL;
1605 goto end;
1606 }
1607 261 s->y = 0;
1608 261 s->pic_state = 0;
1609 261 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
261 if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1611 goto end;
1612
1613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (!(s->pic_state & PNG_ALLIMAGE))
1614 av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
1616 ret = AVERROR_INVALIDDATA;
1617 goto end;
1618 }
1619
1620 261 ret = output_frame(s, dst_frame, s->picture.f);
1621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (ret < 0)
1622 goto end;
1623
1624
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
1625
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 260 times.
261 if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
1626 1 ff_thread_release_buffer(avctx, &s->picture);
1627 } else {
1628 260 ff_thread_release_buffer(avctx, &s->last_picture);
1629 260 FFSWAP(ThreadFrame, s->picture, s->last_picture);
1630 }
1631 }
1632
1633 261 *got_frame = 1;
1634 261 ret = bytestream2_tell(&s->gb);
1635
1636 261 end:
1637 261 inflateEnd(&s->zstream);
1638 261 return ret;
1639 }
1640 #endif
1641
1642 #if HAVE_THREADS
1643 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1644 {
1645 PNGDecContext *psrc = src->priv_data;
1646 PNGDecContext *pdst = dst->priv_data;
1647 ThreadFrame *src_frame = NULL;
1648 int ret;
1649
1650 if (dst == src)
1651 return 0;
1652
1653 if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1654
1655 pdst->width = psrc->width;
1656 pdst->height = psrc->height;
1657 pdst->bit_depth = psrc->bit_depth;
1658 pdst->color_type = psrc->color_type;
1659 pdst->compression_type = psrc->compression_type;
1660 pdst->interlace_type = psrc->interlace_type;
1661 pdst->filter_type = psrc->filter_type;
1662 pdst->cur_w = psrc->cur_w;
1663 pdst->cur_h = psrc->cur_h;
1664 pdst->x_offset = psrc->x_offset;
1665 pdst->y_offset = psrc->y_offset;
1666 pdst->has_trns = psrc->has_trns;
1667 memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
1668
1669 pdst->dispose_op = psrc->dispose_op;
1670
1671 memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
1672
1673 pdst->hdr_state |= psrc->hdr_state;
1674 }
1675
1676 src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1677 &psrc->last_picture : &psrc->picture;
1678
1679 ff_thread_release_buffer(dst, &pdst->last_picture);
1680 if (src_frame && src_frame->f->data[0]) {
1681 ret = ff_thread_ref_frame(&pdst->last_picture, src_frame);
1682 if (ret < 0)
1683 return ret;
1684 }
1685
1686 return 0;
1687 }
1688 #endif
1689
1690 149 static av_cold int png_dec_init(AVCodecContext *avctx)
1691 {
1692 149 PNGDecContext *s = avctx->priv_data;
1693
1694 149 avctx->color_range = AVCOL_RANGE_JPEG;
1695
1696 149 s->avctx = avctx;
1697 149 s->last_picture.f = av_frame_alloc();
1698 149 s->picture.f = av_frame_alloc();
1699
2/4
✓ Branch 0 taken 149 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 149 times.
149 if (!s->last_picture.f || !s->picture.f) {
1700 av_frame_free(&s->last_picture.f);
1701 av_frame_free(&s->picture.f);
1702 return AVERROR(ENOMEM);
1703 }
1704
1705 149 ff_pngdsp_init(&s->dsp);
1706
1707 149 return 0;
1708 }
1709
1710 149 static av_cold int png_dec_end(AVCodecContext *avctx)
1711 {
1712 149 PNGDecContext *s = avctx->priv_data;
1713
1714 149 ff_thread_release_buffer(avctx, &s->last_picture);
1715 149 av_frame_free(&s->last_picture.f);
1716 149 ff_thread_release_buffer(avctx, &s->picture);
1717 149 av_frame_free(&s->picture.f);
1718 149 av_freep(&s->buffer);
1719 149 s->buffer_size = 0;
1720 149 av_freep(&s->last_row);
1721 149 s->last_row_size = 0;
1722 149 av_freep(&s->tmp_row);
1723 149 s->tmp_row_size = 0;
1724 149 av_freep(&s->background_buf);
1725
1726 149 av_freep(&s->iccp_data);
1727 149 av_dict_free(&s->frame_metadata);
1728
1729 149 return 0;
1730 }
1731
1732 #if CONFIG_APNG_DECODER
1733 const AVCodec ff_apng_decoder = {
1734 .name = "apng",
1735 .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1736 .type = AVMEDIA_TYPE_VIDEO,
1737 .id = AV_CODEC_ID_APNG,
1738 .priv_data_size = sizeof(PNGDecContext),
1739 .init = png_dec_init,
1740 .close = png_dec_end,
1741 .decode = decode_frame_apng,
1742 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1743 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1744 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1745 FF_CODEC_CAP_ALLOCATE_PROGRESS,
1746 };
1747 #endif
1748
1749 #if CONFIG_PNG_DECODER
1750 const AVCodec ff_png_decoder = {
1751 .name = "png",
1752 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1753 .type = AVMEDIA_TYPE_VIDEO,
1754 .id = AV_CODEC_ID_PNG,
1755 .priv_data_size = sizeof(PNGDecContext),
1756 .init = png_dec_init,
1757 .close = png_dec_end,
1758 .decode = decode_frame_png,
1759 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1760 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
1761 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_THREADSAFE |
1762 FF_CODEC_CAP_ALLOCATE_PROGRESS,
1763 };
1764 #endif
1765