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