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