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