FFmpeg coverage


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