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