24#include "config_components.h"
139 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
144 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
149 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
156 int bits_per_pixel,
int pass,
157 int color_type,
const uint8_t *
src)
159 int x,
mask, dsp_mask, j, src_x,
b, bpp;
166 switch (bits_per_pixel) {
169 for (x = 0; x <
width; x++) {
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);
176 if ((
mask << j) & 0x80)
182 for (x = 0; x <
width; x++) {
183 int j2 = 2 * (x & 3);
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);
190 if ((
mask << j) & 0x80)
196 for (x = 0; x <
width; x++) {
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);
204 if ((
mask << j) & 0x80)
209 bpp = bits_per_pixel >> 3;
212 for (x = 0; x <
width; x++) {
214 if ((dsp_mask << j) & 0x80) {
218 if ((
mask << j) & 0x80)
225#define UNROLL1(bpp, op) \
234 for (; i <= size - bpp; i += bpp) { \
235 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
238 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
241 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
244 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
248#define UNROLL_FILTER(op) \
251 } else if (bpp == 2) { \
253 } else if (bpp == 3) { \
255 } else if (bpp == 4) { \
258 for (; i < size; i++) { \
259 dst[i] = op(dst[i - bpp], src[i], last[i]); \
264 const uint8_t *
src,
const uint8_t *last,
int size,
int bpp)
266 int i, p,
r,
g,
b,
a;
268 switch (filter_type) {
273 for (
i = 0;
i < bpp;
i++)
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;
283#define OP_SUB(x, s, l) ((x) + (s))
291 for (
i = 0;
i < bpp;
i++) {
295#define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
299 for (
i = 0;
i < bpp;
i++) {
303 if (bpp > 2 &&
size > 4) {
320#define YUV2RGB(NAME, TYPE) \
321static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
324 for (i = 0; i < size - 2; i += 3 + alpha) { \
325 int g = dst [i + 1]; \
336 if (
s->interlace_type) {
337 return 100 - 100 * s->pass / (NB_PASSES - 1);
339 return 100 - 100 * s->y / s->cur_h;
346 uint8_t *ptr, *last_row;
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);
353 last_row =
s->last_row;
355 last_row = ptr - dst_stride;
358 last_row,
s->row_size,
s->bpp);
361 if (
s->bit_depth == 16) {
362 deloco_rgb16((uint16_t *)(ptr - dst_stride),
s->row_size / 2,
365 deloco_rgb8(ptr - dst_stride,
s->row_size,
370 if (
s->y ==
s->cur_h) {
373 if (
s->bit_depth == 16) {
374 deloco_rgb16((uint16_t *)ptr,
s->row_size / 2,
377 deloco_rgb8(ptr,
s->row_size,
385 ptr =
dst + dst_stride * (
s->y +
s->y_offset) +
s->x_offset *
s->bpp;
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);
399 s->color_type,
s->last_row);
402 if (
s->y ==
s->cur_h) {
403 memset(
s->last_row, 0,
s->row_size);
414 s->crow_size =
s->pass_row_size + 1;
415 if (
s->pass_row_size != 0)
427 uint8_t *
dst, ptrdiff_t dst_stride)
429 z_stream *
const zstream = &
s->zstream.zstream;
432 zstream->next_in = gb->
buffer;
435 while (zstream->avail_in > 0) {
437 if (
ret != Z_OK &&
ret != Z_STREAM_END) {
441 if (zstream->avail_out == 0) {
445 zstream->avail_out =
s->crow_size;
446 zstream->next_out =
s->crow_buf;
448 if (
ret == Z_STREAM_END && zstream->avail_in > 0) {
450 "%d undecompressed bytes left in buffer\n", zstream->avail_in);
458#define PNG_ZBUF_MAX_DECOMPRESSED (16 * 1024 * 1024)
461 const uint8_t *data_end,
void *logctx)
464 z_stream *
const zstream = &z.
zstream;
471 zstream->next_in =
data;
472 zstream->avail_in = data_end -
data;
475 while (zstream->avail_in > 0) {
478 "Compressed PNG chunk expands beyond %d bytes, aborting\n",
488 zstream->next_out = buf;
489 zstream->avail_out = buf_size - 1;
491 if (
ret != Z_OK &&
ret != Z_STREAM_END) {
495 bp->len += zstream->next_out - buf;
496 if (
ret == Z_STREAM_END)
500 bp->str[bp->len] = 0;
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)
521 for (
i = 0;
i < size_in;
i++) {
523 *(q++) = 0xC0 | ((in[
i] >> 6) & 0x3);
524 *(q++) = 0x80 | (in[
i] & 0x3F);
535 size_t len = strlen(txt_utf8);
536 const char *ptr = txt_utf8;
537 const char *end = txt_utf8 +
len;
540 const uint8_t *exif_end;
543 while (*ptr++ !=
'\n') {
549 if (end - ptr < 4 || strncmp(
"exif", ptr, 4))
561 size_t nlen = exif_len * 10 + (*ptr -
'0');
576 if (exif_len > SIZE_MAX / 2 || end - ptr < 2 * exif_len)
591 ptr += strlen(
"Exif ") * 2 - 1;
593 exif_ptr =
s->exif_data->data;
594 exif_end = exif_ptr +
s->exif_data->size;
596 while (exif_ptr < exif_end) {
597 while (++ptr < end) {
598 if (*ptr >=
'0' && *ptr <=
'9') {
599 *exif_ptr = (*ptr -
'0') << 4;
602 if (*ptr >=
'a' && *ptr <=
'f') {
603 *exif_ptr = (*ptr -
'a' + 10) << 4;
607 while (++ptr < end) {
608 if (*ptr >=
'0' && *ptr <=
'9') {
609 *exif_ptr += *ptr -
'0';
612 if (*ptr >=
'a' && *ptr <=
'f') {
613 *exif_ptr += *ptr -
'a' + 10;
630 const char *keyword =
data;
631 const char *keyword_end = memchr(keyword, 0, data_end -
data);
632 char *kw_utf8 =
NULL, *txt_utf8 =
NULL;
639 data = keyword_end + 1;
642 if (
data == data_end)
653 text_len = data_end -
data;
667 if (!strcmp(kw_utf8,
"Raw profile type exif")) {
699 s->width =
s->cur_w = bytestream2_get_be32(gb);
700 s->height =
s->cur_h = bytestream2_get_be32(gb);
702 s->cur_w =
s->cur_h =
s->width =
s->height = 0;
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) {
712 s->color_type = bytestream2_get_byte(gb);
713 s->compression_type = bytestream2_get_byte(gb);
714 if (
s->compression_type) {
718 s->filter_type = bytestream2_get_byte(gb);
719 s->interlace_type = bytestream2_get_byte(gb);
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);
729 s->cur_w =
s->cur_h =
s->width =
s->height = 0;
786 if (
s->cicp_range == 0) {
789 }
else if (
s->cicp_range != 1) {
793 }
else if (
s->iccp_data) {
796 s->iccp_data_len, &sd);
800 memcpy(sd->
data,
s->iccp_data,
s->iccp_data_len);
803 }
else if (
s->have_srgb) {
806 }
else if (
s->have_chrm) {
825 if (
s->iccp_data ||
s->have_srgb ||
s->have_cicp) {
827 }
else if (
s->gamma) {
837 if (
s->gamma > 45355 &&
s->gamma < 45555)
839 else if (
s->gamma > 35614 &&
s->gamma < 35814)
841 else if (
s->gamma > 38362 &&
s->gamma < 38562)
843 else if (
s->gamma > 99900 &&
s->gamma < 100100)
849 if (!
s->have_cicp ||
s->cicp_range == 1)
857 if (!
s->has_trns &&
s->significant_bits > 0)
872 clli->
MaxCLL =
s->clli_max / 10000;
873 clli->
MaxFALL =
s->clli_avg / 10000;
886 for (
int i = 0;
i < 3;
i++) {
905 size_t byte_depth =
s->bit_depth > 8 ? 2 : 1;
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;
926 s->subframe_packed = CONFIG_APNG_DECODER &&
928 s->bits_per_pixel <= 4 && !
s->interlace_type;
930 if ((
s->bit_depth == 2 ||
s->bit_depth == 4 ||
s->bit_depth == 8) &&
933 }
else if ((
s->bit_depth == 2 ||
s->bit_depth == 4 ||
s->bit_depth == 8) &&
936 }
else if ((
s->bit_depth == 2 ||
s->bit_depth == 4 ||
s->bit_depth == 8) &&
939 }
else if (
s->bit_depth == 16 &&
942 }
else if (
s->bit_depth == 16 &&
945 }
else if (
s->bit_depth == 16 &&
948 }
else if ((
s->bits_per_pixel == 1 ||
s->bits_per_pixel == 2 ||
s->bits_per_pixel == 4 ||
s->bits_per_pixel == 8) &&
953 }
else if (
s->bit_depth == 8 &&
956 }
else if (
s->bit_depth == 16 &&
961 "Bit depth %d color type %d",
962 s->bit_depth,
s->color_type);
986 "and color type %d with TRNS",
987 s->bit_depth,
s->color_type);
991 s->bpp += byte_depth;
1035 if (!
s->interlace_type) {
1036 s->crow_size =
s->row_size + 1;
1042 s->crow_size =
s->pass_row_size + 1;
1044 ff_dlog(avctx,
"row_size=%d crow_size =%d\n",
1045 s->row_size,
s->crow_size);
1049 memcpy(p->data[1],
s->palette, 256 *
sizeof(uint32_t));
1054 if (
s->interlace_type ||
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;
1075 s->bpp -= byte_depth;
1080 s->bpp += byte_depth;
1094 if ((length % 3) != 0 || length > 256 * 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;
1104 for (;
i < 256;
i++)
1105 s->palette[
i] = (0xFFU << 24);
1128 if (length > 256 || !(
s->hdr_state &
PNG_PLTE))
1131 for (
i = 0;
i < length;
i++) {
1132 unsigned v = bytestream2_get_byte(gb);
1133 s->palette[
i] = (
s->palette[
i] & 0x00ffffff) | (v << 24);
1141 for (
i = 0;
i < length / 2;
i++) {
1145 if (
s->bit_depth > 8)
1146 AV_WB16(&
s->transparent_color_be[2 *
i], v);
1148 s->transparent_color_be[
i] = v;
1164 while ((
s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
1171 if (bytestream2_get_byte(gb) != 0) {
1184 s->iccp_data_len = bp.len;
1188 s->iccp_name[0] = 0;
1219 int b = bytestream2_get_byteu(gb);
1227 s->significant_bits =
bits;
1239 for (k = 7; k >= 1; 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;
1252 }
else if (
s->bits_per_pixel == 2) {
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;
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;
1277 }
else if (
s->bits_per_pixel == 4) {
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;
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;
1299 uint8_t *pd = p->data[0];
1301 for (
int j = 0; j <
s->height; j++) {
1303 pd += p->linesize[0];
1314 const int bpp =
s->channels;
1315 uint8_t *pd = p->data[0] +
s->y_offset * p->linesize[0];
1317 for (
int j = 0; j <
s->cur_h; j++) {
1320 memmove(pd +
s->x_offset * bpp, pd,
s->cur_w * bpp);
1321 pd += p->linesize[0];
1328 uint32_t sequence_number;
1329 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
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);
1350 dispose_op = bytestream2_get_byte(gb);
1351 blend_op = bytestream2_get_byte(gb);
1353 if (sequence_number == 0 &&
1354 (cur_w !=
s->width ||
1355 cur_h !=
s->height ||
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)
1368 if ((sequence_number == 0 || !
s->last_picture.f) &&
1388 s->x_offset = x_offset;
1389 s->y_offset = y_offset;
1390 s->dispose_op = dispose_op;
1391 s->blend_op = blend_op;
1399 uint8_t *pd = p->data[0];
1400 uint8_t *pd_last =
s->last_picture.f->data[0];
1403 ls =
FFMIN(ls,
s->width *
s->bpp);
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];
1416#define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
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];
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);
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);
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) {
1458 uint8_t foreground_alpha, background_alpha, output_alpha;
1467 foreground_alpha = foreground[3];
1468 background_alpha = background[3];
1472 foreground_alpha = foreground[1];
1473 background_alpha = background[1];
1477 if (foreground_alpha == 255)
1480 if (foreground_alpha == 0) {
1481 memcpy(foreground, background, bpp);
1485 output_alpha = foreground_alpha +
FAST_DIV255((255 - foreground_alpha) * background_alpha);
1489 for (
b = 0;
b < bpp - 1; ++
b) {
1490 if (output_alpha == 0) {
1492 }
else if (background_alpha == 255) {
1493 output[
b] =
FAST_DIV255(foreground_alpha * foreground[
b] + (255 - foreground_alpha) * background[
b]);
1495 output[
b] = (255 * foreground_alpha * foreground[
b] + (255 - foreground_alpha) * background_alpha * background[
b]) / (255 * output_alpha);
1498 output[
b] = output_alpha;
1499 memcpy(foreground, output, 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;
1517 for (
size_t y = 0; y <
s->cur_h; y++) {
1518 memset(
dst, 0, bpp *
s->cur_w);
1527 uint32_t
tag, length;
1528 int decode_next_dat = 0;
1556 length = bytestream2_get_be32(&
s->gb);
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) {
1577 tag = bytestream2_get_le32(&
s->gb);
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'):
1604 case MKTAG(
'I',
'H',
'D',
'R'):
1608 case MKTAG(
'p',
'H',
'Y',
's'):
1612 case MKTAG(
'f',
'c',
'T',
'L'):
1617 decode_next_dat = 1;
1619 case MKTAG(
'f',
'd',
'A',
'T'):
1626 bytestream2_get_be32(&gb_chunk);
1628 case MKTAG(
'I',
'D',
'A',
'T'):
1634 case MKTAG(
'P',
'L',
'T',
'E'):
1637 case MKTAG(
't',
'R',
'N',
'S'):
1640 case MKTAG(
't',
'E',
'X',
't'):
1644 case MKTAG(
'z',
'T',
'X',
't'):
1648 case MKTAG(
's',
'T',
'E',
'R'): {
1649 int mode = bytestream2_get_byte(&gb_chunk);
1652 s->stereo_mode =
mode;
1655 "Unknown value in sTER chunk (%d)\n",
mode);
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)
1664 s->cicp_range = bytestream2_get_byte(&gb_chunk);
1665 if (
s->cicp_range != 0 &&
s->cicp_range != 1)
1669 case MKTAG(
's',
'R',
'G',
'B'):
1674 case MKTAG(
'i',
'C',
'C',
'P'): {
1679 case MKTAG(
'c',
'H',
'R',
'M'): {
1682 s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1683 s->white_point[1] = bytestream2_get_be32(&gb_chunk);
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);
1693 case MKTAG(
's',
'B',
'I',
'T'):
1697 case MKTAG(
'g',
'A',
'M',
'A'): {
1700 s->gamma = bytestream2_get_be32(&gb_chunk);
1712 case MKTAG(
'c',
'L',
'L',
'i'):
1713 case MKTAG(
'c',
'L',
'L',
'I'):
1719 s->clli_max = bytestream2_get_be32u(&gb_chunk);
1720 s->clli_avg = bytestream2_get_be32u(&gb_chunk);
1722 case MKTAG(
'm',
'D',
'C',
'v'):
1723 case MKTAG(
'm',
'D',
'C',
'V'):
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);
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);
1738 case MKTAG(
'e',
'X',
'I',
'f'):
1743 case MKTAG(
'I',
'E',
'N',
'D'):
1768 if (
s->bits_per_pixel <= 4) {
1769 if (CONFIG_APNG_DECODER &&
s->subframe_packed)
1787 for (
int y = 0; y <
s->height; y++) {
1788 uint8_t *row = &p->data[0][p->linesize[0] * y];
1790 for (
int x =
s->width - 1; x >= 0; x--) {
1791 const uint8_t idx = row[x];
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;
1803 size_t byte_depth =
s->bit_depth > 8 ? 2 : 1;
1804 size_t raw_bpp =
s->bpp - byte_depth;
1809 for (y = 0; y <
s->height; ++y) {
1810 uint8_t *row = &p->data[0][p->linesize[0] * y];
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;
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) {
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);
1836 if (!memcmp(
pixel,
s->transparent_color_be, raw_bpp)) {
1837 memset(&
pixel[raw_bpp], 0, byte_depth);
1839 memset(&
pixel[raw_bpp], 0xff, byte_depth);
1847 if (
s->last_picture.f) {
1849 &&
s->last_picture.f->width == p->width
1850 &&
s->last_picture.f->height== p->height
1851 &&
s->last_picture.f->format== p->format
1855 else if (CONFIG_APNG_DECODER &&
1875 s->iccp_data_len = 0;
1876 s->iccp_name[0] = 0;
1878 s->stereo_mode = -1;
1891 if (
s->stereo_mode >= 0) {
1914#if CONFIG_PNG_DECODER
1919 const uint8_t *buf = avpkt->
data;
1920 int buf_size = avpkt->
size;
1929 sig = bytestream2_get_be64(&
s->gb);
1936 s->y =
s->has_trns = 0;
1941 ret = inflateReset(&
s->zstream.zstream);
1972#if CONFIG_APNG_DECODER
1985 if ((
ret = inflateReset(&
s->zstream.zstream)) != Z_OK)
1993 if ((
ret = inflateReset(&
s->zstream.zstream)) != Z_OK)
2077 s->last_row_size = 0;
2079 s->tmp_row_size = 0;
2089#if CONFIG_APNG_DECODER
2107#if CONFIG_PNG_DECODER
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFCodec ff_png_decoder
const FFCodec ff_apng_decoder
#define APNG_FCTL_CHUNK_SIZE
@ APNG_DISPOSE_OP_BACKGROUND
@ APNG_DISPOSE_OP_PREVIOUS
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.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_DEBUG_STARTCODE
#define FF_THREAD_FRAME
Decode more than one frame at once.
#define FF_DEBUG_PICT_INFO
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_UNLIMITED
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_tell(const GetByteContext *g)
#define i(width, name, range_min, range_max)
#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...
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 ...
void ff_progress_frame_await(const ProgressFrame *f, int n)
Wait for earlier decoding threads to finish reference frames.
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
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...
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...
int ff_decode_exif_attach_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferRef **pbuf, enum AVExifHeaderMode header_mode)
Attach the data buffer to the frame.
int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
Wrapper around ff_progress_frame_alloc() and ff_thread_get_buffer().
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.
void ff_progress_frame_unref(ProgressFrame *f)
Give up a reference to the underlying frame contained in a ProgressFrame and reset the ProgressFrame,...
int ff_set_dimensions(AVCodecContext *s, int width, int height)
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
#define AV_EF_IGNORE_ERR
ignore errors and continue
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define FF_COMPLIANCE_NORMAL
int(* init)(AVBSFContext *ctx)
@ AV_EXIF_TIFF_HEADER
The TIFF header starts with 0x49492a00, or 0x4d4d002a.
EXIF metadata parser - internal functions.
static const uint8_t bits[8]
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
@ AVDISCARD_ALL
discard all
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...
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.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void av_bprint_get_buffer(AVBPrint *buf, unsigned size, unsigned char **mem, unsigned *actual_size)
Allocate bytes in the buffer for external use.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
Detects which enum AVColorPrimaries constant corresponds to the given complete gamut description.
static AVRational av_make_q(int num, int den)
Create an AVRational.
#define av_fourcc2str(fourcc)
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...
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.
@ AV_PICTURE_TYPE_I
Intra.
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
static av_const int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
const uint8_t ff_png_pass_ymask[NB_PASSES]
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
int ff_png_get_nb_channels(int color_type)
Multithreading API for decoders.
Macro definitions for various function/variable attributes.
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static const uint16_t mask[17]
#define FFSWAP(type, a, b)
#define MKTAG(a, b, c, d)
Memory handling functions.
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.
AVColorRange
Visual content value range.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_JPEG
Full range content.
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ 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...
@ 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...
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
AVColorPrimaries
Chromaticity coordinates of the source primaries.
@ AVCOL_PRI_NB
Not part of ABI.
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
AVColorTransferCharacteristic
Color Transfer Characteristic.
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
@ AVCOL_TRC_NB
Not part of ABI.
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_RGB_ALPHA
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_FILTER_VALUE_AVG
#define PNG_COLOR_MASK_PALETTE
#define PNG_FILTER_VALUE_UP
#define PNG_COLOR_TYPE_GRAY
#define PNG_COLOR_TYPE_PALETTE
#define PNG_FILTER_VALUE_PAETH
#define PNG_FILTER_VALUE_NONE
#define PNG_FILTER_TYPE_LOCO
#define PNG_FILTER_VALUE_SUB
static void apng_reset_background(PNGDecContext *s, const AVFrame *p)
static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb, AVFrame *p)
static int png_decode_idat(PNGDecContext *s, GetByteContext *gb, uint8_t *dst, ptrdiff_t dst_stride)
#define PNG_ZBUF_MAX_DECOMPRESSED
static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p, const AVPacket *avpkt)
static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
#define UNROLL_FILTER(op)
static int decode_zbuf(AVBPrint *bp, const uint8_t *data, const uint8_t *data_end, void *logctx)
static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static const uint8_t png_pass_mask[NB_PASSES]
static void unpack_small_row(PNGDecContext *s, uint8_t *pd, int width)
static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame)
static int decode_text_to_exif(PNGDecContext *s, const char *txt_utf8)
static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
static av_cold int png_dec_end(AVCodecContext *avctx)
static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
static const uint8_t png_pass_dsp_ymask[NB_PASSES]
static void png_put_interlaced_row(uint8_t *dst, int width, int bits_per_pixel, int pass, int color_type, const uint8_t *src)
static av_cold int png_dec_init(AVCodecContext *avctx)
static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static int percent_missing(PNGDecContext *s)
static int output_frame(PNGDecContext *s, AVFrame *f)
static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static void clear_frame_metadata(PNGDecContext *s)
static void handle_small_bpp_apng(PNGDecContext *s, AVFrame *p)
#define YUV2RGB(NAME, TYPE)
static const uint8_t png_pass_dsp_mask[NB_PASSES]
static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static int decode_exif_chunk(AVCodecContext *avctx, PNGDecContext *s, GetByteContext *gb)
static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb)
static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, AVFrame *p)
static char * iso88591_to_utf8(const char *in, size_t size_in)
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)
void ff_png_add_paeth_prediction(uint8_t *dst, const uint8_t *src, const uint8_t *top, int w, int bpp)
av_cold void ff_pngdsp_init(PNGDSPContext *dsp)
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
A reference to a data buffer.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
int active_thread_type
Which multithreading methods are in use by the codec.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
enum AVColorSpace colorspace
YUV colorspace type.
int discard_damaged_percentage
The percentage of damaged samples to discard a frame.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
enum AVDiscard skip_frame
Skip decoding for selected frames.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Struct that contains both white point location and primaries location, providing the complete descrip...
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.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
Rational number (pair of numerator and denominator).
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
enum AVStereo3DType type
How views are packed within the video.
int flags
Additional information about the frame packing.
const uint8_t * buffer_end
void(* add_paeth_prediction)(uint8_t *dst, const uint8_t *src, const uint8_t *top, int w, int bpp)
void(* add_bytes_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
uint16_t mdcv_white_point[2]
uint16_t mdcv_primaries[3][2]
AVDictionary * frame_metadata
uint8_t transparent_color_be[6]
unsigned int tmp_row_size
ProgressFrame last_picture
enum PNGImageState pic_state
uint32_t display_primaries[3][2]
enum AVColorTransferCharacteristic cicp_trc
enum AVColorRange cicp_range
enum AVColorPrimaries cicp_primaries
enum PNGHeaderState hdr_state
unsigned int last_row_size
The ProgressFrame structure.
#define avpriv_request_sample(...)
static void error(const char *err)
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().