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