2007-07-15 21:24:26 +03:00
|
|
|
/*
|
|
|
|
* PNG image format
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
2007-07-15 21:24:26 +03:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2011-04-27 19:34:10 +03:00
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2015-08-28 15:53:07 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2012-10-28 16:41:40 +03:00
|
|
|
#include "libavutil/bprint.h"
|
2019-03-07 20:15:23 +02:00
|
|
|
#include "libavutil/crc.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2019-08-18 01:28:38 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2015-10-22 22:36:52 +02:00
|
|
|
#include "libavutil/stereo3d.h"
|
2017-09-25 21:28:08 +02:00
|
|
|
#include "libavutil/mastering_display_metadata.h"
|
2015-10-27 19:27:03 +02:00
|
|
|
|
2007-07-15 21:24:26 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 22:03:27 +02:00
|
|
|
#include "decode.h"
|
2014-12-02 10:21:47 +02:00
|
|
|
#include "apng.h"
|
2007-07-15 21:24:26 +03:00
|
|
|
#include "png.h"
|
2012-01-27 18:00:36 +03:00
|
|
|
#include "pngdsp.h"
|
2013-08-12 13:56:20 +03:00
|
|
|
#include "thread.h"
|
2022-02-06 08:08:01 +02:00
|
|
|
#include "threadframe.h"
|
2022-03-15 16:05:53 +02:00
|
|
|
#include "zlib_wrapper.h"
|
2007-07-15 21:24:26 +03:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
enum PNGHeaderState {
|
|
|
|
PNG_IHDR = 1 << 0,
|
|
|
|
PNG_PLTE = 1 << 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum PNGImageState {
|
|
|
|
PNG_IDAT = 1 << 0,
|
|
|
|
PNG_ALLIMAGE = 1 << 1,
|
|
|
|
};
|
|
|
|
|
2007-07-15 22:23:55 +03:00
|
|
|
typedef struct PNGDecContext {
|
2012-01-27 18:00:36 +03:00
|
|
|
PNGDSPContext dsp;
|
2012-09-15 22:30:57 +03:00
|
|
|
AVCodecContext *avctx;
|
2008-02-21 09:10:46 +02:00
|
|
|
|
2012-03-08 03:16:20 +03:00
|
|
|
GetByteContext gb;
|
2013-08-12 13:56:20 +03:00
|
|
|
ThreadFrame last_picture;
|
|
|
|
ThreadFrame picture;
|
2007-07-15 22:23:55 +03:00
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
AVDictionary *frame_metadata;
|
|
|
|
|
|
|
|
uint8_t iccp_name[82];
|
|
|
|
uint8_t *iccp_data;
|
|
|
|
size_t iccp_data_len;
|
|
|
|
|
|
|
|
int stereo_mode;
|
|
|
|
|
|
|
|
int have_chrm;
|
|
|
|
uint32_t white_point[2];
|
|
|
|
uint32_t display_primaries[3][2];
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
enum PNGHeaderState hdr_state;
|
|
|
|
enum PNGImageState pic_state;
|
2007-07-15 22:23:55 +03:00
|
|
|
int width, height;
|
2014-11-27 16:26:27 +02:00
|
|
|
int cur_w, cur_h;
|
|
|
|
int x_offset, y_offset;
|
|
|
|
uint8_t dispose_op, blend_op;
|
2007-07-15 22:23:55 +03:00
|
|
|
int bit_depth;
|
|
|
|
int color_type;
|
|
|
|
int compression_type;
|
|
|
|
int interlace_type;
|
|
|
|
int filter_type;
|
|
|
|
int channels;
|
|
|
|
int bits_per_pixel;
|
|
|
|
int bpp;
|
2015-06-02 17:49:25 +02:00
|
|
|
int has_trns;
|
2015-08-28 15:53:07 +02:00
|
|
|
uint8_t transparent_color_be[6];
|
2007-07-15 22:23:55 +03:00
|
|
|
|
|
|
|
uint32_t palette[256];
|
|
|
|
uint8_t *crow_buf;
|
|
|
|
uint8_t *last_row;
|
2013-08-30 17:20:33 +03:00
|
|
|
unsigned int last_row_size;
|
2007-07-15 22:23:55 +03:00
|
|
|
uint8_t *tmp_row;
|
2013-08-30 17:06:05 +03:00
|
|
|
unsigned int tmp_row_size;
|
2013-08-22 20:05:53 +03:00
|
|
|
uint8_t *buffer;
|
|
|
|
int buffer_size;
|
2007-07-15 22:23:55 +03:00
|
|
|
int pass;
|
|
|
|
int crow_size; /* compressed row size (include filter type) */
|
|
|
|
int row_size; /* decompressed row size */
|
|
|
|
int pass_row_size; /* decompress row size of the current pass */
|
|
|
|
int y;
|
2022-03-15 16:05:53 +02:00
|
|
|
FFZStream zstream;
|
2007-07-15 22:23:55 +03:00
|
|
|
} PNGDecContext;
|
|
|
|
|
2012-03-23 00:52:55 +03:00
|
|
|
/* Mask to determine which pixels are valid in a pass */
|
|
|
|
static const uint8_t png_pass_mask[NB_PASSES] = {
|
|
|
|
0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
|
|
|
|
};
|
|
|
|
|
2007-07-15 21:24:26 +03:00
|
|
|
/* Mask to determine which y pixels can be written in a pass */
|
|
|
|
static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
|
2012-01-19 22:49:59 +03:00
|
|
|
0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
|
2007-07-15 21:24:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Mask to determine which pixels to overwrite while displaying */
|
|
|
|
static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
|
|
|
|
0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
|
|
|
|
};
|
|
|
|
|
|
|
|
/* NOTE: we try to construct a good looking image at each pass. width
|
2014-03-14 20:32:00 +03:00
|
|
|
* is the original image width. We also do pixel format conversion at
|
|
|
|
* this stage */
|
2007-07-15 21:24:26 +03:00
|
|
|
static void png_put_interlaced_row(uint8_t *dst, int width,
|
|
|
|
int bits_per_pixel, int pass,
|
|
|
|
int color_type, const uint8_t *src)
|
|
|
|
{
|
|
|
|
int x, mask, dsp_mask, j, src_x, b, bpp;
|
|
|
|
uint8_t *d;
|
|
|
|
const uint8_t *s;
|
|
|
|
|
2013-01-26 17:19:48 +03:00
|
|
|
mask = png_pass_mask[pass];
|
2007-07-15 21:24:26 +03:00
|
|
|
dsp_mask = png_pass_dsp_mask[pass];
|
2012-12-08 14:58:46 +03:00
|
|
|
|
|
|
|
switch (bits_per_pixel) {
|
2007-07-15 21:24:26 +03:00
|
|
|
case 1:
|
|
|
|
src_x = 0;
|
2012-12-08 14:58:46 +03:00
|
|
|
for (x = 0; x < width; x++) {
|
2007-07-15 21:24:26 +03:00
|
|
|
j = (x & 7);
|
|
|
|
if ((dsp_mask << j) & 0x80) {
|
|
|
|
b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
|
2011-09-06 16:28:36 +03:00
|
|
|
dst[x >> 3] &= 0xFF7F>>j;
|
2007-07-15 21:24:26 +03:00
|
|
|
dst[x >> 3] |= b << (7 - j);
|
|
|
|
}
|
|
|
|
if ((mask << j) & 0x80)
|
|
|
|
src_x++;
|
|
|
|
}
|
|
|
|
break;
|
2011-09-06 19:46:42 +03:00
|
|
|
case 2:
|
|
|
|
src_x = 0;
|
2013-01-26 17:19:48 +03:00
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
int j2 = 2 * (x & 3);
|
2011-09-06 19:46:42 +03:00
|
|
|
j = (x & 7);
|
|
|
|
if ((dsp_mask << j) & 0x80) {
|
|
|
|
b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
|
|
|
|
dst[x >> 2] &= 0xFF3F>>j2;
|
|
|
|
dst[x >> 2] |= b << (6 - j2);
|
|
|
|
}
|
|
|
|
if ((mask << j) & 0x80)
|
|
|
|
src_x++;
|
|
|
|
}
|
|
|
|
break;
|
2011-09-06 20:02:14 +03:00
|
|
|
case 4:
|
|
|
|
src_x = 0;
|
2013-01-26 17:19:48 +03:00
|
|
|
for (x = 0; x < width; x++) {
|
2011-09-06 20:02:14 +03:00
|
|
|
int j2 = 4*(x&1);
|
|
|
|
j = (x & 7);
|
|
|
|
if ((dsp_mask << j) & 0x80) {
|
|
|
|
b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
|
|
|
|
dst[x >> 1] &= 0xFF0F>>j2;
|
|
|
|
dst[x >> 1] |= b << (4 - j2);
|
|
|
|
}
|
|
|
|
if ((mask << j) & 0x80)
|
|
|
|
src_x++;
|
|
|
|
}
|
|
|
|
break;
|
2007-07-15 21:24:26 +03:00
|
|
|
default:
|
|
|
|
bpp = bits_per_pixel >> 3;
|
2012-12-08 14:58:46 +03:00
|
|
|
d = dst;
|
|
|
|
s = src;
|
|
|
|
for (x = 0; x < width; x++) {
|
2007-07-15 21:24:26 +03:00
|
|
|
j = x & 7;
|
|
|
|
if ((dsp_mask << j) & 0x80) {
|
|
|
|
memcpy(d, s, bpp);
|
|
|
|
}
|
|
|
|
d += bpp;
|
|
|
|
if ((mask << j) & 0x80)
|
|
|
|
s += bpp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 20:32:00 +03:00
|
|
|
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
|
|
|
|
int w, int bpp)
|
2008-02-21 09:10:46 +02:00
|
|
|
{
|
|
|
|
int i;
|
2012-12-08 14:58:46 +03:00
|
|
|
for (i = 0; i < w; i++) {
|
2008-02-21 09:10:46 +02:00
|
|
|
int a, b, c, p, pa, pb, pc;
|
|
|
|
|
|
|
|
a = dst[i - bpp];
|
|
|
|
b = top[i];
|
|
|
|
c = top[i - bpp];
|
|
|
|
|
2012-12-08 14:58:46 +03:00
|
|
|
p = b - c;
|
2008-02-21 09:10:46 +02:00
|
|
|
pc = a - c;
|
|
|
|
|
|
|
|
pa = abs(p);
|
|
|
|
pb = abs(pc);
|
|
|
|
pc = abs(p + pc);
|
|
|
|
|
|
|
|
if (pa <= pb && pa <= pc)
|
|
|
|
p = a;
|
|
|
|
else if (pb <= pc)
|
|
|
|
p = b;
|
|
|
|
else
|
|
|
|
p = c;
|
|
|
|
dst[i] = p + src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 16:25:30 +03:00
|
|
|
#define UNROLL1(bpp, op) \
|
|
|
|
{ \
|
2014-03-14 20:32:00 +03:00
|
|
|
r = dst[0]; \
|
|
|
|
if (bpp >= 2) \
|
|
|
|
g = dst[1]; \
|
|
|
|
if (bpp >= 3) \
|
|
|
|
b = dst[2]; \
|
|
|
|
if (bpp >= 4) \
|
|
|
|
a = dst[3]; \
|
2014-03-17 17:24:05 +03:00
|
|
|
for (; i <= size - bpp; i += bpp) { \
|
2014-03-14 20:32:00 +03:00
|
|
|
dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
|
|
|
|
if (bpp == 1) \
|
|
|
|
continue; \
|
|
|
|
dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
|
|
|
|
if (bpp == 2) \
|
|
|
|
continue; \
|
|
|
|
dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
|
|
|
|
if (bpp == 3) \
|
|
|
|
continue; \
|
|
|
|
dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
|
|
|
|
} \
|
2014-07-31 16:25:30 +03:00
|
|
|
}
|
2008-02-21 09:10:46 +02:00
|
|
|
|
2014-03-14 20:32:00 +03:00
|
|
|
#define UNROLL_FILTER(op) \
|
2014-07-31 16:25:30 +03:00
|
|
|
if (bpp == 1) { \
|
2014-03-14 20:32:00 +03:00
|
|
|
UNROLL1(1, op) \
|
2014-07-31 16:25:30 +03:00
|
|
|
} else if (bpp == 2) { \
|
|
|
|
UNROLL1(2, op) \
|
|
|
|
} else if (bpp == 3) { \
|
|
|
|
UNROLL1(3, op) \
|
|
|
|
} else if (bpp == 4) { \
|
|
|
|
UNROLL1(4, op) \
|
2014-08-01 17:48:09 +03:00
|
|
|
} \
|
|
|
|
for (; i < size; i++) { \
|
|
|
|
dst[i] = op(dst[i - bpp], src[i], last[i]); \
|
2014-07-31 16:25:30 +03:00
|
|
|
}
|
2008-02-21 09:10:46 +02:00
|
|
|
|
2007-07-15 21:24:26 +03:00
|
|
|
/* NOTE: 'dst' can be equal to 'last' */
|
2021-02-14 16:50:10 +02:00
|
|
|
void ff_png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
|
|
|
|
uint8_t *src, uint8_t *last, int size, int bpp)
|
2007-07-15 21:24:26 +03:00
|
|
|
{
|
2008-02-21 09:10:46 +02:00
|
|
|
int i, p, r, g, b, a;
|
2007-07-15 21:24:26 +03:00
|
|
|
|
2012-12-08 14:58:46 +03:00
|
|
|
switch (filter_type) {
|
2007-07-15 21:24:26 +03:00
|
|
|
case PNG_FILTER_VALUE_NONE:
|
|
|
|
memcpy(dst, src, size);
|
|
|
|
break;
|
|
|
|
case PNG_FILTER_VALUE_SUB:
|
2014-03-14 20:32:00 +03:00
|
|
|
for (i = 0; i < bpp; i++)
|
2007-07-15 21:24:26 +03:00
|
|
|
dst[i] = src[i];
|
2012-12-08 14:58:46 +03:00
|
|
|
if (bpp == 4) {
|
2014-03-14 20:32:00 +03:00
|
|
|
p = *(int *)dst;
|
2012-12-08 14:58:46 +03:00
|
|
|
for (; i < size; i += bpp) {
|
2014-03-17 17:24:05 +03:00
|
|
|
unsigned s = *(int *)(src + i);
|
2012-12-08 14:58:46 +03:00
|
|
|
p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
|
2014-03-14 20:32:00 +03:00
|
|
|
*(int *)(dst + i) = p;
|
2008-02-21 09:10:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-25 20:24:30 +03:00
|
|
|
#define OP_SUB(x, s, l) ((x) + (s))
|
2008-02-21 09:10:46 +02:00
|
|
|
UNROLL_FILTER(OP_SUB);
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PNG_FILTER_VALUE_UP:
|
2008-02-21 09:10:46 +02:00
|
|
|
dsp->add_bytes_l2(dst, src, last, size);
|
2007-07-15 21:24:26 +03:00
|
|
|
break;
|
|
|
|
case PNG_FILTER_VALUE_AVG:
|
2012-12-08 14:58:46 +03:00
|
|
|
for (i = 0; i < bpp; i++) {
|
2014-03-14 20:32:00 +03:00
|
|
|
p = (last[i] >> 1);
|
2007-07-15 21:24:26 +03:00
|
|
|
dst[i] = p + src[i];
|
|
|
|
}
|
2014-05-25 20:24:30 +03:00
|
|
|
#define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
|
2008-02-21 09:10:46 +02:00
|
|
|
UNROLL_FILTER(OP_AVG);
|
2007-07-15 21:24:26 +03:00
|
|
|
break;
|
|
|
|
case PNG_FILTER_VALUE_PAETH:
|
2012-12-08 14:58:46 +03:00
|
|
|
for (i = 0; i < bpp; i++) {
|
2014-03-14 20:32:00 +03:00
|
|
|
p = last[i];
|
2007-07-15 21:24:26 +03:00
|
|
|
dst[i] = p + src[i];
|
|
|
|
}
|
2013-01-26 17:19:48 +03:00
|
|
|
if (bpp > 2 && size > 4) {
|
2014-03-14 20:32:00 +03:00
|
|
|
/* would write off the end of the array if we let it process
|
|
|
|
* the last pixel with bpp=3 */
|
2014-12-07 13:36:10 +02:00
|
|
|
int w = (bpp & 3) ? size - 3 : size;
|
|
|
|
|
2014-11-26 18:00:17 +02:00
|
|
|
if (w > i) {
|
2014-12-07 13:28:37 +02:00
|
|
|
dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
|
2014-11-26 18:00:17 +02:00
|
|
|
i = w;
|
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
2012-12-08 14:58:46 +03:00
|
|
|
ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
|
2007-07-15 21:24:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:03:26 +03:00
|
|
|
/* This used to be called "deloco" in FFmpeg
|
|
|
|
* and is actually an inverse reversible colorspace transformation */
|
|
|
|
#define YUV2RGB(NAME, TYPE) \
|
|
|
|
static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
|
|
|
|
{ \
|
|
|
|
int i; \
|
|
|
|
for (i = 0; i < size; i += 3 + alpha) { \
|
2014-03-17 17:24:05 +03:00
|
|
|
int g = dst [i + 1]; \
|
|
|
|
dst[i + 0] += g; \
|
|
|
|
dst[i + 2] += g; \
|
2012-01-31 02:03:26 +03:00
|
|
|
} \
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
|
|
|
|
2012-01-31 02:03:26 +03:00
|
|
|
YUV2RGB(rgb8, uint8_t)
|
|
|
|
YUV2RGB(rgb16, uint16_t)
|
2008-02-26 12:21:33 +02:00
|
|
|
|
2019-08-17 23:55:34 +02:00
|
|
|
static int percent_missing(PNGDecContext *s)
|
|
|
|
{
|
|
|
|
if (s->interlace_type) {
|
|
|
|
return 100 - 100 * s->pass / (NB_PASSES - 1);
|
|
|
|
} else {
|
|
|
|
return 100 - 100 * s->y / s->cur_h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-15 21:24:26 +03:00
|
|
|
/* process exactly one decompressed row */
|
2021-04-02 10:45:27 +02:00
|
|
|
static void png_handle_row(PNGDecContext *s, uint8_t *dst, ptrdiff_t dst_stride)
|
2007-07-15 21:24:26 +03:00
|
|
|
{
|
|
|
|
uint8_t *ptr, *last_row;
|
|
|
|
int got_line;
|
|
|
|
|
|
|
|
if (!s->interlace_type) {
|
2021-04-02 10:45:27 +02:00
|
|
|
ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
|
2014-12-03 15:16:57 +02:00
|
|
|
if (s->y == 0)
|
|
|
|
last_row = s->last_row;
|
|
|
|
else
|
2021-04-02 10:45:27 +02:00
|
|
|
last_row = ptr - dst_stride;
|
2007-07-15 21:24:26 +03:00
|
|
|
|
2021-02-14 16:50:10 +02:00
|
|
|
ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
|
|
|
|
last_row, s->row_size, s->bpp);
|
2008-02-26 12:21:33 +02:00
|
|
|
/* loco lags by 1 row so that it doesn't interfere with top prediction */
|
2012-01-31 02:03:26 +03:00
|
|
|
if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
|
|
|
|
if (s->bit_depth == 16) {
|
2021-04-02 10:45:27 +02:00
|
|
|
deloco_rgb16((uint16_t *)(ptr - dst_stride), s->row_size / 2,
|
2012-01-31 02:03:26 +03:00
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
|
|
|
|
} else {
|
2021-04-02 10:45:27 +02:00
|
|
|
deloco_rgb8(ptr - dst_stride, s->row_size,
|
2012-01-31 02:03:26 +03:00
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
|
|
|
|
}
|
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
s->y++;
|
2014-11-27 16:26:27 +02:00
|
|
|
if (s->y == s->cur_h) {
|
2017-04-03 16:08:29 +02:00
|
|
|
s->pic_state |= PNG_ALLIMAGE;
|
2012-01-31 02:03:26 +03:00
|
|
|
if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
|
|
|
|
if (s->bit_depth == 16) {
|
|
|
|
deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
|
|
|
|
} else {
|
|
|
|
deloco_rgb8(ptr, s->row_size,
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
|
|
|
|
}
|
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
got_line = 0;
|
2012-12-08 14:58:46 +03:00
|
|
|
for (;;) {
|
2021-04-02 10:45:27 +02:00
|
|
|
ptr = dst + dst_stride * (s->y + s->y_offset) + s->x_offset * s->bpp;
|
2007-07-15 21:24:26 +03:00
|
|
|
if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
|
|
|
|
/* if we already read one row, it is time to stop to
|
2014-03-14 20:32:00 +03:00
|
|
|
* wait for the next one */
|
2007-07-15 21:24:26 +03:00
|
|
|
if (got_line)
|
|
|
|
break;
|
2021-02-14 16:50:10 +02:00
|
|
|
ff_png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
|
|
|
|
s->last_row, s->pass_row_size, s->bpp);
|
2014-03-14 20:32:00 +03:00
|
|
|
FFSWAP(uint8_t *, s->last_row, s->tmp_row);
|
2013-08-30 17:06:05 +03:00
|
|
|
FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
|
2007-07-15 21:24:26 +03:00
|
|
|
got_line = 1;
|
|
|
|
}
|
|
|
|
if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
|
2014-11-27 16:26:27 +02:00
|
|
|
png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
|
2007-07-15 21:24:26 +03:00
|
|
|
s->color_type, s->last_row);
|
|
|
|
}
|
|
|
|
s->y++;
|
2014-11-27 16:26:27 +02:00
|
|
|
if (s->y == s->cur_h) {
|
2011-09-06 17:22:38 +03:00
|
|
|
memset(s->last_row, 0, s->row_size);
|
2012-12-08 14:58:46 +03:00
|
|
|
for (;;) {
|
2007-07-15 21:24:26 +03:00
|
|
|
if (s->pass == NB_PASSES - 1) {
|
2017-04-03 16:08:29 +02:00
|
|
|
s->pic_state |= PNG_ALLIMAGE;
|
2007-07-15 21:24:26 +03:00
|
|
|
goto the_end;
|
|
|
|
} else {
|
|
|
|
s->pass++;
|
|
|
|
s->y = 0;
|
|
|
|
s->pass_row_size = ff_png_pass_row_size(s->pass,
|
2014-03-14 20:32:00 +03:00
|
|
|
s->bits_per_pixel,
|
2014-11-27 16:26:27 +02:00
|
|
|
s->cur_w);
|
2007-07-15 21:24:26 +03:00
|
|
|
s->crow_size = s->pass_row_size + 1;
|
|
|
|
if (s->pass_row_size != 0)
|
|
|
|
break;
|
|
|
|
/* skip pass if empty row */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 20:32:00 +03:00
|
|
|
the_end:;
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
|
2021-04-02 10:45:27 +02:00
|
|
|
uint8_t *dst, ptrdiff_t dst_stride)
|
2007-07-15 21:24:26 +03:00
|
|
|
{
|
2022-03-15 16:05:53 +02:00
|
|
|
z_stream *const zstream = &s->zstream.zstream;
|
2007-07-15 21:24:26 +03:00
|
|
|
int ret;
|
2022-03-15 16:05:53 +02:00
|
|
|
zstream->avail_in = bytestream2_get_bytes_left(gb);
|
|
|
|
zstream->next_in = gb->buffer;
|
2007-07-15 21:24:26 +03:00
|
|
|
|
|
|
|
/* decode one line if possible */
|
2022-03-15 16:05:53 +02:00
|
|
|
while (zstream->avail_in > 0) {
|
|
|
|
ret = inflate(zstream, Z_PARTIAL_FLUSH);
|
2007-07-15 21:24:26 +03:00
|
|
|
if (ret != Z_OK && ret != Z_STREAM_END) {
|
2013-06-25 11:02:22 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
|
|
|
|
return AVERROR_EXTERNAL;
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
if (zstream->avail_out == 0) {
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & PNG_ALLIMAGE)) {
|
2021-04-02 10:45:27 +02:00
|
|
|
png_handle_row(s, dst, dst_stride);
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
zstream->avail_out = s->crow_size;
|
|
|
|
zstream->next_out = s->crow_buf;
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
if (ret == Z_STREAM_END && zstream->avail_in > 0) {
|
2019-09-30 09:07:19 +02:00
|
|
|
av_log(s->avctx, AV_LOG_WARNING,
|
2022-03-15 16:05:53 +02:00
|
|
|
"%d undecompressed bytes left in buffer\n", zstream->avail_in);
|
2013-09-29 00:12:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:41:40 +03:00
|
|
|
static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
|
2022-03-15 16:05:53 +02:00
|
|
|
const uint8_t *data_end, void *logctx)
|
2012-10-28 16:41:40 +03:00
|
|
|
{
|
2022-03-15 16:05:53 +02:00
|
|
|
FFZStream z;
|
|
|
|
z_stream *const zstream = &z.zstream;
|
2012-10-28 16:41:40 +03:00
|
|
|
unsigned char *buf;
|
|
|
|
unsigned buf_size;
|
2022-03-15 16:05:53 +02:00
|
|
|
int ret = ff_inflate_init(&z, logctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-28 16:41:40 +03:00
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
zstream->next_in = data;
|
|
|
|
zstream->avail_in = data_end - data;
|
2018-06-10 10:00:02 +02:00
|
|
|
av_bprint_init(bp, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2012-10-28 16:41:40 +03:00
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
while (zstream->avail_in > 0) {
|
2017-01-23 02:25:27 +02:00
|
|
|
av_bprint_get_buffer(bp, 2, &buf, &buf_size);
|
|
|
|
if (buf_size < 2) {
|
2012-10-28 16:41:40 +03:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
zstream->next_out = buf;
|
|
|
|
zstream->avail_out = buf_size - 1;
|
|
|
|
ret = inflate(zstream, Z_PARTIAL_FLUSH);
|
2012-10-28 16:41:40 +03:00
|
|
|
if (ret != Z_OK && ret != Z_STREAM_END) {
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
bp->len += zstream->next_out - buf;
|
2012-10-28 16:41:40 +03:00
|
|
|
if (ret == Z_STREAM_END)
|
|
|
|
break;
|
|
|
|
}
|
2022-03-15 16:05:53 +02:00
|
|
|
ff_inflate_end(&z);
|
2012-10-28 16:41:40 +03:00
|
|
|
bp->str[bp->len] = 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2022-03-15 16:05:53 +02:00
|
|
|
ff_inflate_end(&z);
|
2012-10-28 16:41:40 +03:00
|
|
|
av_bprint_finalize(bp, NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-21 02:54:41 +02:00
|
|
|
static char *iso88591_to_utf8(const char *in, size_t size_in)
|
2012-10-28 16:41:40 +03:00
|
|
|
{
|
|
|
|
size_t extra = 0, i;
|
2022-08-21 02:54:41 +02:00
|
|
|
char *out, *q;
|
2012-10-28 16:41:40 +03:00
|
|
|
|
|
|
|
for (i = 0; i < size_in; i++)
|
2022-08-21 02:54:41 +02:00
|
|
|
extra += !!(in[i] & 0x80);
|
2012-10-28 16:41:40 +03:00
|
|
|
if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
|
|
|
|
return NULL;
|
|
|
|
q = out = av_malloc(size_in + extra + 1);
|
|
|
|
if (!out)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < size_in; i++) {
|
2022-08-21 02:54:41 +02:00
|
|
|
if (in[i] & 0x80) {
|
2012-10-28 16:41:40 +03:00
|
|
|
*(q++) = 0xC0 | (in[i] >> 6);
|
|
|
|
*(q++) = 0x80 | (in[i] & 0x3F);
|
|
|
|
} else {
|
|
|
|
*(q++) = in[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(q++) = 0;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
|
2012-10-28 16:41:40 +03:00
|
|
|
{
|
|
|
|
int ret, method;
|
2021-04-02 16:33:44 +02:00
|
|
|
const uint8_t *data = gb->buffer;
|
|
|
|
const uint8_t *data_end = gb->buffer_end;
|
2022-08-21 02:54:41 +02:00
|
|
|
const char *keyword = data;
|
|
|
|
const char *keyword_end = memchr(keyword, 0, data_end - data);
|
|
|
|
char *kw_utf8 = NULL, *txt_utf8 = NULL;
|
|
|
|
const char *text;
|
2012-10-28 16:41:40 +03:00
|
|
|
unsigned text_len;
|
|
|
|
AVBPrint bp;
|
|
|
|
|
|
|
|
if (!keyword_end)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
data = keyword_end + 1;
|
|
|
|
|
|
|
|
if (compressed) {
|
|
|
|
if (data == data_end)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
method = *(data++);
|
|
|
|
if (method)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2022-03-15 16:05:53 +02:00
|
|
|
if ((ret = decode_zbuf(&bp, data, data_end, s->avctx)) < 0)
|
2012-10-28 16:41:40 +03:00
|
|
|
return ret;
|
2021-03-17 16:20:45 +02:00
|
|
|
text = bp.str;
|
2012-10-28 16:41:40 +03:00
|
|
|
text_len = bp.len;
|
|
|
|
} else {
|
2022-08-21 02:54:41 +02:00
|
|
|
text = data;
|
|
|
|
text_len = data_end - data;
|
2012-10-28 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
txt_utf8 = iso88591_to_utf8(text, text_len);
|
2021-03-17 16:20:45 +02:00
|
|
|
if (compressed)
|
|
|
|
av_bprint_finalize(&bp, NULL);
|
2021-03-17 17:49:44 +02:00
|
|
|
if (!txt_utf8)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
|
|
|
|
if (!kw_utf8) {
|
2012-10-28 16:41:40 +03:00
|
|
|
av_free(txt_utf8);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
av_dict_set(&s->frame_metadata, kw_utf8, txt_utf8,
|
2012-10-28 16:41:40 +03:00
|
|
|
AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:31 +02:00
|
|
|
static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext *gb)
|
2014-11-14 11:17:31 +02:00
|
|
|
{
|
2021-04-02 16:33:44 +02:00
|
|
|
if (bytestream2_get_bytes_left(gb) != 13)
|
2014-11-14 11:17:31 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2014-11-26 16:45:47 +02:00
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (s->pic_state & PNG_IDAT) {
|
2014-11-26 16:45:47 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (s->hdr_state & PNG_IHDR) {
|
2015-06-29 21:08:05 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
s->width = s->cur_w = bytestream2_get_be32(gb);
|
|
|
|
s->height = s->cur_h = bytestream2_get_be32(gb);
|
2014-11-14 11:17:31 +02:00
|
|
|
if (av_image_check_size(s->width, s->height, 0, avctx)) {
|
2015-06-29 21:59:41 +02:00
|
|
|
s->cur_w = s->cur_h = s->width = s->height = 0;
|
2014-11-14 11:17:31 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2021-04-02 16:33:44 +02:00
|
|
|
s->bit_depth = bytestream2_get_byte(gb);
|
2017-02-19 20:12:25 +02:00
|
|
|
if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
|
|
|
|
s->bit_depth != 8 && s->bit_depth != 16) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2021-04-02 16:33:44 +02:00
|
|
|
s->color_type = bytestream2_get_byte(gb);
|
|
|
|
s->compression_type = bytestream2_get_byte(gb);
|
2018-11-09 04:12:45 +02:00
|
|
|
if (s->compression_type) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
|
|
|
|
goto error;
|
|
|
|
}
|
2021-04-02 16:33:44 +02:00
|
|
|
s->filter_type = bytestream2_get_byte(gb);
|
|
|
|
s->interlace_type = bytestream2_get_byte(gb);
|
2017-04-03 16:08:29 +02:00
|
|
|
s->hdr_state |= PNG_IHDR;
|
2014-11-14 11:17:31 +02:00
|
|
|
if (avctx->debug & FF_DEBUG_PICT_INFO)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
|
|
|
|
"compression_type=%d filter_type=%d interlace_type=%d\n",
|
|
|
|
s->width, s->height, s->bit_depth, s->color_type,
|
|
|
|
s->compression_type, s->filter_type, s->interlace_type);
|
2014-11-14 11:17:32 +02:00
|
|
|
|
|
|
|
return 0;
|
2017-02-19 20:12:25 +02:00
|
|
|
error:
|
|
|
|
s->cur_w = s->cur_h = s->width = s->height = 0;
|
|
|
|
s->bit_depth = 8;
|
|
|
|
return AVERROR_INVALIDDATA;
|
2014-11-14 11:17:32 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
|
|
|
GetByteContext *gb)
|
2014-11-14 11:17:32 +02:00
|
|
|
{
|
2017-04-03 16:08:29 +02:00
|
|
|
if (s->pic_state & PNG_IDAT) {
|
2014-11-14 11:17:32 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2021-04-02 16:33:44 +02:00
|
|
|
avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
|
|
|
|
avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
|
2014-11-14 11:17:32 +02:00
|
|
|
if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
|
|
|
|
avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
|
2021-04-02 16:33:44 +02:00
|
|
|
bytestream2_skip(gb, 1); /* unit specifier */
|
2014-11-14 11:17:32 +02:00
|
|
|
|
2014-11-14 11:17:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:33 +02:00
|
|
|
static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext *gb, AVFrame *p)
|
2014-11-14 11:17:33 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-08-28 15:53:07 +02:00
|
|
|
size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
|
2014-11-14 11:17:33 +02:00
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->hdr_state & PNG_IHDR)) {
|
2014-11-14 11:17:33 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & PNG_IDAT)) {
|
2014-11-14 11:17:33 +02:00
|
|
|
/* init image info */
|
2017-05-04 18:40:46 +02:00
|
|
|
ret = ff_set_dimensions(avctx, s->width, s->height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-11-14 11:17:33 +02:00
|
|
|
|
|
|
|
s->channels = ff_png_get_nb_channels(s->color_type);
|
|
|
|
s->bits_per_pixel = s->bit_depth * s->channels;
|
|
|
|
s->bpp = (s->bits_per_pixel + 7) >> 3;
|
2014-11-27 16:26:27 +02:00
|
|
|
s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
|
2014-11-14 11:17:33 +02:00
|
|
|
|
|
|
|
if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
|
|
|
} else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
|
|
|
} else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_GRAY) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY8;
|
|
|
|
} else if (s->bit_depth == 16 &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_GRAY) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
|
|
|
|
} else if (s->bit_depth == 16 &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
|
|
|
|
} else if (s->bit_depth == 16 &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
|
|
|
|
} else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_PALETTE) {
|
2022-03-05 19:36:43 +02:00
|
|
|
avctx->pix_fmt = avctx->codec_id == AV_CODEC_ID_APNG ? AV_PIX_FMT_RGBA : AV_PIX_FMT_PAL8;
|
2015-05-03 20:36:20 +02:00
|
|
|
} else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) {
|
2014-11-14 11:17:33 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
|
|
|
|
} else if (s->bit_depth == 8 &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YA8;
|
|
|
|
} else if (s->bit_depth == 16 &&
|
|
|
|
s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YA16BE;
|
|
|
|
} else {
|
2017-11-11 14:58:01 +02:00
|
|
|
avpriv_report_missing_feature(avctx,
|
|
|
|
"Bit depth %d color type %d",
|
|
|
|
s->bit_depth, s->color_type);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
2014-11-14 11:17:33 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:53:07 +02:00
|
|
|
if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
switch (avctx->pix_fmt) {
|
|
|
|
case AV_PIX_FMT_RGB24:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_PIX_FMT_RGB48BE:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_PIX_FMT_GRAY8:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YA8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_PIX_FMT_GRAY16BE:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YA16BE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-11-16 19:34:44 +02:00
|
|
|
avpriv_request_sample(avctx, "bit depth %d "
|
|
|
|
"and color type %d with TRNS",
|
|
|
|
s->bit_depth, s->color_type);
|
|
|
|
return AVERROR_INVALIDDATA;
|
2015-08-28 15:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->bpp += byte_depth;
|
|
|
|
}
|
|
|
|
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->picture);
|
2022-08-20 21:21:40 +02:00
|
|
|
if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
|
|
|
|
/* We only need a buffer for the current picture. */
|
|
|
|
ret = ff_thread_get_buffer(avctx, p, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
|
|
|
|
/* We need a buffer for the current picture as well as
|
|
|
|
* a buffer for the reference to retain. */
|
|
|
|
ret = ff_thread_get_ext_buffer(avctx, &s->picture,
|
|
|
|
AV_GET_BUFFER_FLAG_REF);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = ff_thread_get_buffer(avctx, p, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
/* The picture output this time and the reference to retain coincide. */
|
|
|
|
if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture,
|
|
|
|
AV_GET_BUFFER_FLAG_REF)) < 0)
|
|
|
|
return ret;
|
|
|
|
ret = av_frame_ref(p, s->picture.f);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2021-02-14 17:37:33 +02:00
|
|
|
|
2014-11-14 11:17:33 +02:00
|
|
|
p->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
p->key_frame = 1;
|
|
|
|
p->interlaced_frame = !!s->interlace_type;
|
|
|
|
|
2017-04-03 20:43:40 +02:00
|
|
|
ff_thread_finish_setup(avctx);
|
|
|
|
|
2014-11-14 11:17:33 +02:00
|
|
|
/* compute the compressed row size */
|
|
|
|
if (!s->interlace_type) {
|
|
|
|
s->crow_size = s->row_size + 1;
|
|
|
|
} else {
|
|
|
|
s->pass = 0;
|
|
|
|
s->pass_row_size = ff_png_pass_row_size(s->pass,
|
|
|
|
s->bits_per_pixel,
|
2014-11-27 16:26:27 +02:00
|
|
|
s->cur_w);
|
2014-11-14 11:17:33 +02:00
|
|
|
s->crow_size = s->pass_row_size + 1;
|
|
|
|
}
|
2015-04-20 03:34:22 +02:00
|
|
|
ff_dlog(avctx, "row_size=%d crow_size =%d\n",
|
2014-11-14 11:17:33 +02:00
|
|
|
s->row_size, s->crow_size);
|
2021-04-02 10:45:27 +02:00
|
|
|
|
2014-11-14 11:17:33 +02:00
|
|
|
/* copy the palette if needed */
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
|
|
|
|
memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
|
|
|
|
/* empty row is used if differencing to the first row */
|
|
|
|
av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
|
|
|
|
if (!s->last_row)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
if (s->interlace_type ||
|
|
|
|
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
|
|
|
|
av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
|
|
|
|
if (!s->tmp_row)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
/* compressed row */
|
|
|
|
av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
|
|
|
|
if (!s->buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
/* we want crow_buf+1 to be 16-byte aligned */
|
|
|
|
s->crow_buf = s->buffer + 15;
|
2022-03-15 16:05:53 +02:00
|
|
|
s->zstream.zstream.avail_out = s->crow_size;
|
|
|
|
s->zstream.zstream.next_out = s->crow_buf;
|
2014-11-14 11:17:33 +02:00
|
|
|
}
|
2015-08-28 15:53:07 +02:00
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
s->pic_state |= PNG_IDAT;
|
2015-08-28 15:53:07 +02:00
|
|
|
|
|
|
|
/* set image to non-transparent bpp while decompressing */
|
|
|
|
if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
|
|
|
|
s->bpp -= byte_depth;
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
|
2015-08-28 15:53:07 +02:00
|
|
|
|
|
|
|
if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
|
|
|
|
s->bpp += byte_depth;
|
|
|
|
|
|
|
|
if (ret < 0)
|
2014-11-14 11:17:33 +02:00
|
|
|
return ret;
|
2015-08-28 15:53:07 +02:00
|
|
|
|
2014-11-14 11:17:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:34 +02:00
|
|
|
static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext *gb)
|
2014-11-14 11:17:34 +02:00
|
|
|
{
|
2021-04-02 16:33:44 +02:00
|
|
|
int length = bytestream2_get_bytes_left(gb);
|
2014-11-14 11:17:34 +02:00
|
|
|
int n, i, r, g, b;
|
|
|
|
|
|
|
|
if ((length % 3) != 0 || length > 256 * 3)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
/* read the palette */
|
|
|
|
n = length / 3;
|
|
|
|
for (i = 0; i < n; i++) {
|
2021-04-02 16:33:44 +02:00
|
|
|
r = bytestream2_get_byte(gb);
|
|
|
|
g = bytestream2_get_byte(gb);
|
|
|
|
b = bytestream2_get_byte(gb);
|
2014-11-14 11:17:34 +02:00
|
|
|
s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
|
|
|
|
}
|
|
|
|
for (; i < 256; i++)
|
|
|
|
s->palette[i] = (0xFFU << 24);
|
2017-04-03 16:08:29 +02:00
|
|
|
s->hdr_state |= PNG_PLTE;
|
2014-11-14 11:17:34 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:35 +02:00
|
|
|
static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext *gb)
|
2014-11-14 11:17:35 +02:00
|
|
|
{
|
2021-04-02 16:33:44 +02:00
|
|
|
int length = bytestream2_get_bytes_left(gb);
|
2014-11-14 11:17:35 +02:00
|
|
|
int v, i;
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->hdr_state & PNG_IHDR)) {
|
2017-02-04 13:24:14 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (s->pic_state & PNG_IDAT) {
|
2017-02-04 13:24:14 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:53:07 +02:00
|
|
|
if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
|
2017-04-03 16:08:29 +02:00
|
|
|
if (length > 256 || !(s->hdr_state & PNG_PLTE))
|
2015-08-28 15:53:07 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
2021-04-02 16:33:44 +02:00
|
|
|
unsigned v = bytestream2_get_byte(gb);
|
2015-08-28 15:53:07 +02:00
|
|
|
s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
|
|
|
|
}
|
|
|
|
} else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
|
|
|
|
if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
|
2017-02-04 13:24:14 +02:00
|
|
|
(s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
|
|
|
|
s->bit_depth == 1)
|
2015-08-28 15:53:07 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
for (i = 0; i < length / 2; i++) {
|
|
|
|
/* only use the least significant bits */
|
2021-04-02 16:33:44 +02:00
|
|
|
v = av_mod_uintp2(bytestream2_get_be16(gb), s->bit_depth);
|
2015-08-28 15:53:07 +02:00
|
|
|
|
|
|
|
if (s->bit_depth > 8)
|
|
|
|
AV_WB16(&s->transparent_color_be[2 * i], v);
|
|
|
|
else
|
|
|
|
s->transparent_color_be[i] = v;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-14 11:17:35 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-06-02 17:49:25 +02:00
|
|
|
s->has_trns = 1;
|
|
|
|
|
2014-11-14 11:17:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb, AVFrame *f)
|
2017-07-20 22:46:22 +02:00
|
|
|
{
|
|
|
|
int ret, cnt = 0;
|
|
|
|
AVBPrint bp;
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
while ((s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
|
2017-07-20 22:46:22 +02:00
|
|
|
if (cnt > 80) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
|
2021-03-20 20:57:25 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2017-07-20 22:46:22 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
if (bytestream2_get_byte(gb) != 0) {
|
2017-07-20 22:46:22 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
|
2021-03-20 20:57:25 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2017-07-20 22:46:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx)) < 0)
|
2017-07-20 22:46:22 +02:00
|
|
|
return ret;
|
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
av_freep(&s->iccp_data);
|
|
|
|
ret = av_bprint_finalize(&bp, (char **)&s->iccp_data);
|
2017-11-09 10:21:05 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-03-20 20:57:25 +02:00
|
|
|
s->iccp_data_len = bp.len;
|
2017-07-20 22:46:22 +02:00
|
|
|
|
|
|
|
return 0;
|
2021-03-20 20:57:25 +02:00
|
|
|
fail:
|
|
|
|
s->iccp_name[0] = 0;
|
|
|
|
return ret;
|
2017-07-20 22:46:22 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:38 +02:00
|
|
|
static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
|
|
|
|
{
|
|
|
|
if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
int i, j, k;
|
|
|
|
uint8_t *pd = p->data[0];
|
|
|
|
for (j = 0; j < s->height; j++) {
|
|
|
|
i = s->width / 8;
|
|
|
|
for (k = 7; k >= 1; k--)
|
|
|
|
if ((s->width&7) >= k)
|
|
|
|
pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
|
|
|
|
for (i--; i >= 0; i--) {
|
|
|
|
pd[8*i + 7]= pd[i] & 1;
|
|
|
|
pd[8*i + 6]= (pd[i]>>1) & 1;
|
|
|
|
pd[8*i + 5]= (pd[i]>>2) & 1;
|
|
|
|
pd[8*i + 4]= (pd[i]>>3) & 1;
|
|
|
|
pd[8*i + 3]= (pd[i]>>4) & 1;
|
|
|
|
pd[8*i + 2]= (pd[i]>>5) & 1;
|
|
|
|
pd[8*i + 1]= (pd[i]>>6) & 1;
|
|
|
|
pd[8*i + 0]= pd[i]>>7;
|
|
|
|
}
|
2021-04-02 10:45:27 +02:00
|
|
|
pd += p->linesize[0];
|
2014-11-14 11:17:38 +02:00
|
|
|
}
|
|
|
|
} else if (s->bits_per_pixel == 2) {
|
|
|
|
int i, j;
|
|
|
|
uint8_t *pd = p->data[0];
|
|
|
|
for (j = 0; j < s->height; j++) {
|
|
|
|
i = s->width / 4;
|
|
|
|
if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
|
|
|
|
if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
|
|
|
|
if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
|
|
|
|
for (i--; i >= 0; i--) {
|
|
|
|
pd[4*i + 3]= pd[i] & 3;
|
|
|
|
pd[4*i + 2]= (pd[i]>>2) & 3;
|
|
|
|
pd[4*i + 1]= (pd[i]>>4) & 3;
|
|
|
|
pd[4*i + 0]= pd[i]>>6;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
|
|
|
|
if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
|
|
|
|
if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
|
|
|
|
for (i--; i >= 0; i--) {
|
|
|
|
pd[4*i + 3]= ( pd[i] & 3)*0x55;
|
|
|
|
pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
|
|
|
|
pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
|
|
|
|
pd[4*i + 0]= ( pd[i]>>6 )*0x55;
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 10:45:27 +02:00
|
|
|
pd += p->linesize[0];
|
2014-11-14 11:17:38 +02:00
|
|
|
}
|
|
|
|
} else if (s->bits_per_pixel == 4) {
|
|
|
|
int i, j;
|
|
|
|
uint8_t *pd = p->data[0];
|
|
|
|
for (j = 0; j < s->height; j++) {
|
|
|
|
i = s->width/2;
|
|
|
|
if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
if (s->width&1) pd[2*i+0]= pd[i]>>4;
|
|
|
|
for (i--; i >= 0; i--) {
|
|
|
|
pd[2*i + 1] = pd[i] & 15;
|
|
|
|
pd[2*i + 0] = pd[i] >> 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
|
|
|
|
for (i--; i >= 0; i--) {
|
|
|
|
pd[2*i + 1] = (pd[i] & 15) * 0x11;
|
|
|
|
pd[2*i + 0] = (pd[i] >> 4) * 0x11;
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 10:45:27 +02:00
|
|
|
pd += p->linesize[0];
|
2014-11-14 11:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext *gb)
|
2014-11-21 13:05:47 +02:00
|
|
|
{
|
2014-11-27 16:26:27 +02:00
|
|
|
uint32_t sequence_number;
|
2015-06-29 22:32:02 +02:00
|
|
|
int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
|
2014-11-21 13:05:47 +02:00
|
|
|
|
2022-07-04 17:13:58 +02:00
|
|
|
if (bytestream2_get_bytes_left(gb) != APNG_FCTL_CHUNK_SIZE)
|
2014-11-21 13:05:47 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->hdr_state & PNG_IHDR)) {
|
2015-06-29 21:57:12 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "fctl before IHDR\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:22:01 +02:00
|
|
|
if (s->pic_state & PNG_IDAT) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "fctl after IDAT\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
sequence_number = bytestream2_get_be32(gb);
|
|
|
|
cur_w = bytestream2_get_be32(gb);
|
|
|
|
cur_h = bytestream2_get_be32(gb);
|
|
|
|
x_offset = bytestream2_get_be32(gb);
|
|
|
|
y_offset = bytestream2_get_be32(gb);
|
|
|
|
bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
|
|
|
|
dispose_op = bytestream2_get_byte(gb);
|
|
|
|
blend_op = bytestream2_get_byte(gb);
|
2014-11-27 16:26:27 +02:00
|
|
|
|
|
|
|
if (sequence_number == 0 &&
|
2015-06-29 22:32:02 +02:00
|
|
|
(cur_w != s->width ||
|
|
|
|
cur_h != s->height ||
|
|
|
|
x_offset != 0 ||
|
|
|
|
y_offset != 0) ||
|
|
|
|
cur_w <= 0 || cur_h <= 0 ||
|
|
|
|
x_offset < 0 || y_offset < 0 ||
|
|
|
|
cur_w > s->width - x_offset|| cur_h > s->height - y_offset)
|
2014-11-21 13:05:47 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2014-11-27 16:26:27 +02:00
|
|
|
|
2015-10-11 03:46:44 +02:00
|
|
|
if (blend_op != APNG_BLEND_OP_OVER && blend_op != APNG_BLEND_OP_SOURCE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid blend_op %d\n", blend_op);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:37:33 +02:00
|
|
|
if ((sequence_number == 0 || !s->last_picture.f->data[0]) &&
|
2016-11-25 23:09:51 +02:00
|
|
|
dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
|
2015-06-07 20:12:30 +02:00
|
|
|
// No previous frame to revert to for the first frame
|
|
|
|
// Spec says to just treat it as a APNG_DISPOSE_OP_BACKGROUND
|
2015-07-19 22:20:56 +02:00
|
|
|
dispose_op = APNG_DISPOSE_OP_BACKGROUND;
|
2014-11-21 13:05:47 +02:00
|
|
|
}
|
|
|
|
|
2015-07-19 22:20:56 +02:00
|
|
|
if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && (
|
2015-06-02 17:49:25 +02:00
|
|
|
avctx->pix_fmt == AV_PIX_FMT_RGB24 ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_MONOBLACK
|
|
|
|
)) {
|
2015-07-19 22:20:56 +02:00
|
|
|
// APNG_BLEND_OP_OVER is the same as APNG_BLEND_OP_SOURCE when there is no alpha channel
|
|
|
|
blend_op = APNG_BLEND_OP_SOURCE;
|
2015-06-02 17:49:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:32:02 +02:00
|
|
|
s->cur_w = cur_w;
|
|
|
|
s->cur_h = cur_h;
|
|
|
|
s->x_offset = x_offset;
|
|
|
|
s->y_offset = y_offset;
|
|
|
|
s->dispose_op = dispose_op;
|
|
|
|
s->blend_op = blend_op;
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:26:26 +02:00
|
|
|
static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
uint8_t *pd = p->data[0];
|
|
|
|
uint8_t *pd_last = s->last_picture.f->data[0];
|
2021-06-13 01:55:40 +02:00
|
|
|
int ls = av_image_get_linesize(p->format, s->width, 0);
|
|
|
|
|
|
|
|
ls = FFMIN(ls, s->width * s->bpp);
|
2014-11-27 16:26:26 +02:00
|
|
|
|
|
|
|
ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
|
|
|
|
for (j = 0; j < s->height; j++) {
|
|
|
|
for (i = 0; i < ls; i++)
|
|
|
|
pd[i] += pd_last[i];
|
2021-04-02 10:45:27 +02:00
|
|
|
pd += p->linesize[0];
|
|
|
|
pd_last += s->last_picture.f->linesize[0];
|
2014-11-27 16:26:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:26:27 +02:00
|
|
|
// divide by 255 and round to nearest
|
|
|
|
// apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
|
|
|
|
#define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
|
|
|
|
|
|
|
|
static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
|
|
|
|
AVFrame *p)
|
|
|
|
{
|
2021-04-01 15:45:45 +02:00
|
|
|
uint8_t *dst = p->data[0];
|
|
|
|
ptrdiff_t dst_stride = p->linesize[0];
|
|
|
|
const uint8_t *src = s->last_picture.f->data[0];
|
|
|
|
ptrdiff_t src_stride = s->last_picture.f->linesize[0];
|
2022-03-05 19:36:43 +02:00
|
|
|
const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
|
2021-04-01 15:45:45 +02:00
|
|
|
|
2015-06-07 20:12:30 +02:00
|
|
|
size_t x, y;
|
2014-12-08 04:16:46 +02:00
|
|
|
|
2014-11-27 16:26:27 +02:00
|
|
|
if (s->blend_op == APNG_BLEND_OP_OVER &&
|
2015-06-02 17:49:26 +02:00
|
|
|
avctx->pix_fmt != AV_PIX_FMT_RGBA &&
|
2022-03-05 19:36:43 +02:00
|
|
|
avctx->pix_fmt != AV_PIX_FMT_GRAY8A) {
|
2014-11-27 16:26:27 +02:00
|
|
|
avpriv_request_sample(avctx, "Blending with pixel format %s",
|
|
|
|
av_get_pix_fmt_name(avctx->pix_fmt));
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:37:33 +02:00
|
|
|
ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
|
2015-10-11 13:09:56 +02:00
|
|
|
|
2021-04-01 15:45:45 +02:00
|
|
|
// copy unchanged rectangles from the last frame
|
|
|
|
for (y = 0; y < s->y_offset; y++)
|
2022-03-05 19:36:43 +02:00
|
|
|
memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
|
2021-04-01 15:45:45 +02:00
|
|
|
for (y = s->y_offset; y < s->y_offset + s->cur_h; y++) {
|
2022-03-05 19:36:43 +02:00
|
|
|
memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * bpp);
|
|
|
|
memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * bpp,
|
|
|
|
src + y * src_stride + (s->x_offset + s->cur_w) * bpp,
|
|
|
|
(p->width - s->cur_w - s->x_offset) * bpp);
|
2021-04-01 15:45:45 +02:00
|
|
|
}
|
|
|
|
for (y = s->y_offset + s->cur_h; y < p->height; y++)
|
2022-03-05 19:36:43 +02:00
|
|
|
memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp);
|
2021-02-14 17:37:33 +02:00
|
|
|
|
2021-04-01 15:45:45 +02:00
|
|
|
if (s->blend_op == APNG_BLEND_OP_OVER) {
|
|
|
|
// Perform blending
|
2015-06-07 20:12:30 +02:00
|
|
|
for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) {
|
2022-03-05 19:36:43 +02:00
|
|
|
uint8_t *foreground = dst + dst_stride * y + bpp * s->x_offset;
|
|
|
|
const uint8_t *background = src + src_stride * y + bpp * s->x_offset;
|
|
|
|
for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += bpp, background += bpp) {
|
2015-06-07 20:12:30 +02:00
|
|
|
size_t b;
|
|
|
|
uint8_t foreground_alpha, background_alpha, output_alpha;
|
2015-11-07 00:44:01 +02:00
|
|
|
uint8_t output[10];
|
2015-06-07 20:12:30 +02:00
|
|
|
|
|
|
|
// Since we might be blending alpha onto alpha, we use the following equations:
|
|
|
|
// output_alpha = foreground_alpha + (1 - foreground_alpha) * background_alpha
|
|
|
|
// output = (foreground_alpha * foreground + (1 - foreground_alpha) * background_alpha * background) / output_alpha
|
|
|
|
|
|
|
|
switch (avctx->pix_fmt) {
|
|
|
|
case AV_PIX_FMT_RGBA:
|
|
|
|
foreground_alpha = foreground[3];
|
|
|
|
background_alpha = background[3];
|
2014-11-27 16:26:27 +02:00
|
|
|
break;
|
2015-06-02 17:49:26 +02:00
|
|
|
|
|
|
|
case AV_PIX_FMT_GRAY8A:
|
|
|
|
foreground_alpha = foreground[1];
|
|
|
|
background_alpha = background[1];
|
|
|
|
break;
|
2014-11-27 16:26:27 +02:00
|
|
|
}
|
2015-06-07 20:12:30 +02:00
|
|
|
|
2021-04-01 15:45:45 +02:00
|
|
|
if (foreground_alpha == 255)
|
2015-06-07 20:12:30 +02:00
|
|
|
continue;
|
|
|
|
|
2021-04-01 15:45:45 +02:00
|
|
|
if (foreground_alpha == 0) {
|
2022-03-05 19:36:43 +02:00
|
|
|
memcpy(foreground, background, bpp);
|
2015-06-02 17:49:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-07 20:12:30 +02:00
|
|
|
output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha);
|
|
|
|
|
2022-03-05 19:36:43 +02:00
|
|
|
av_assert0(bpp <= 10);
|
2015-11-07 00:44:01 +02:00
|
|
|
|
2022-03-05 19:36:43 +02:00
|
|
|
for (b = 0; b < bpp - 1; ++b) {
|
2015-06-07 20:12:30 +02:00
|
|
|
if (output_alpha == 0) {
|
|
|
|
output[b] = 0;
|
|
|
|
} else if (background_alpha == 255) {
|
|
|
|
output[b] = FAST_DIV255(foreground_alpha * foreground[b] + (255 - foreground_alpha) * background[b]);
|
|
|
|
} else {
|
|
|
|
output[b] = (255 * foreground_alpha * foreground[b] + (255 - foreground_alpha) * background_alpha * background[b]) / (255 * output_alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output[b] = output_alpha;
|
2022-03-05 19:36:43 +02:00
|
|
|
memcpy(foreground, output, bpp);
|
2014-11-27 16:26:27 +02:00
|
|
|
}
|
2014-11-28 17:54:09 +02:00
|
|
|
}
|
2014-11-27 16:26:27 +02:00
|
|
|
}
|
2014-11-28 17:54:09 +02:00
|
|
|
|
2014-11-27 16:26:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:21:40 +02:00
|
|
|
static void apng_reset_background(PNGDecContext *s, const AVFrame *p)
|
|
|
|
{
|
|
|
|
// need to reset a rectangle to black
|
|
|
|
av_unused int ret = av_frame_copy(s->picture.f, p);
|
|
|
|
const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp;
|
|
|
|
const ptrdiff_t dst_stride = s->picture.f->linesize[0];
|
|
|
|
uint8_t *dst = s->picture.f->data[0] + s->y_offset * dst_stride + bpp * s->x_offset;
|
|
|
|
|
|
|
|
av_assert1(ret >= 0);
|
|
|
|
|
|
|
|
for (size_t y = 0; y < s->cur_h; y++) {
|
|
|
|
memset(dst, 0, bpp * s->cur_w);
|
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:17:39 +02:00
|
|
|
static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
|
2021-03-04 14:13:20 +02:00
|
|
|
AVFrame *p, const AVPacket *avpkt)
|
2007-07-15 21:24:26 +03:00
|
|
|
{
|
2019-03-07 20:15:23 +02:00
|
|
|
const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
|
2007-07-15 21:24:26 +03:00
|
|
|
uint32_t tag, length;
|
2014-11-21 13:05:47 +02:00
|
|
|
int decode_next_dat = 0;
|
2017-09-25 21:28:08 +02:00
|
|
|
int i, ret;
|
2007-07-15 21:24:26 +03:00
|
|
|
|
2012-12-08 14:58:46 +03:00
|
|
|
for (;;) {
|
2021-04-02 16:33:44 +02:00
|
|
|
GetByteContext gb_chunk;
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
length = bytestream2_get_bytes_left(&s->gb);
|
|
|
|
if (length <= 0) {
|
2015-10-09 15:14:11 +02:00
|
|
|
|
|
|
|
if (avctx->codec_id == AV_CODEC_ID_PNG &&
|
|
|
|
avctx->skip_frame == AVDISCARD_ALL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-28 15:03:08 +02:00
|
|
|
if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & PNG_IDAT))
|
2014-11-21 13:05:47 +02:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
goto exit_loop;
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
|
2017-04-03 16:08:29 +02:00
|
|
|
if ( s->pic_state & PNG_ALLIMAGE
|
2014-03-11 11:56:38 +03:00
|
|
|
&& avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
|
|
|
|
goto exit_loop;
|
2015-05-03 17:50:26 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2007-07-15 21:24:26 +03:00
|
|
|
goto fail;
|
2012-03-20 15:22:40 +03:00
|
|
|
}
|
2012-03-09 03:22:31 +03:00
|
|
|
|
2012-03-08 03:16:20 +03:00
|
|
|
length = bytestream2_get_be32(&s->gb);
|
2021-04-02 16:00:23 +02:00
|
|
|
if (length > 0x7fffffff || length + 8 > bytestream2_get_bytes_left(&s->gb)) {
|
2012-03-20 15:22:40 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
|
2015-05-03 17:50:26 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2007-07-15 21:24:26 +03:00
|
|
|
goto fail;
|
2012-03-20 15:22:40 +03:00
|
|
|
}
|
2019-03-07 20:15:23 +02:00
|
|
|
if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
|
|
|
|
uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
|
|
|
|
uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
|
|
|
|
if (crc_sig ^ crc_cal) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, ", quitting\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_ERROR, ", skipping\n");
|
2021-04-02 16:33:44 +02:00
|
|
|
bytestream2_skip(&s->gb, length + 8); /* tag */
|
2019-03-07 20:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-08 03:16:20 +03:00
|
|
|
tag = bytestream2_get_le32(&s->gb);
|
2012-01-19 20:58:24 +03:00
|
|
|
if (avctx->debug & FF_DEBUG_STARTCODE)
|
2017-03-27 09:45:11 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
|
|
|
|
av_fourcc2str(tag), length);
|
2015-10-09 15:14:11 +02:00
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
bytestream2_init(&gb_chunk, s->gb.buffer, length);
|
|
|
|
bytestream2_skip(&s->gb, length + 4);
|
|
|
|
|
2015-10-09 15:14:11 +02:00
|
|
|
if (avctx->codec_id == AV_CODEC_ID_PNG &&
|
|
|
|
avctx->skip_frame == AVDISCARD_ALL) {
|
|
|
|
switch(tag) {
|
|
|
|
case MKTAG('I', 'H', 'D', 'R'):
|
|
|
|
case MKTAG('p', 'H', 'Y', 's'):
|
|
|
|
case MKTAG('t', 'E', 'X', 't'):
|
|
|
|
case MKTAG('I', 'D', 'A', 'T'):
|
2016-04-09 00:47:10 +02:00
|
|
|
case MKTAG('t', 'R', 'N', 'S'):
|
2015-10-09 15:14:11 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-04-02 16:33:44 +02:00
|
|
|
continue;
|
2015-10-09 15:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 14:58:46 +03:00
|
|
|
switch (tag) {
|
2007-07-15 21:24:26 +03:00
|
|
|
case MKTAG('I', 'H', 'D', 'R'):
|
2021-04-02 16:33:44 +02:00
|
|
|
if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
|
2007-07-15 21:24:26 +03:00
|
|
|
goto fail;
|
|
|
|
break;
|
2012-10-13 11:38:02 +03:00
|
|
|
case MKTAG('p', 'H', 'Y', 's'):
|
2021-04-02 16:33:44 +02:00
|
|
|
if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
|
2012-10-13 11:38:02 +03:00
|
|
|
goto fail;
|
|
|
|
break;
|
2014-11-21 13:05:47 +02:00
|
|
|
case MKTAG('f', 'c', 'T', 'L'):
|
2014-11-28 15:03:08 +02:00
|
|
|
if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
|
2021-04-02 16:33:44 +02:00
|
|
|
continue;
|
|
|
|
if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
|
2014-11-21 13:05:47 +02:00
|
|
|
goto fail;
|
|
|
|
decode_next_dat = 1;
|
|
|
|
break;
|
|
|
|
case MKTAG('f', 'd', 'A', 'T'):
|
2014-11-28 15:03:08 +02:00
|
|
|
if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
|
2021-04-02 16:33:44 +02:00
|
|
|
continue;
|
|
|
|
if (!decode_next_dat || bytestream2_get_bytes_left(&gb_chunk) < 4) {
|
2015-05-03 17:50:26 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2014-11-21 13:05:47 +02:00
|
|
|
goto fail;
|
2015-05-03 17:50:26 +02:00
|
|
|
}
|
2021-04-02 16:33:44 +02:00
|
|
|
bytestream2_get_be32(&gb_chunk);
|
2014-11-21 13:05:47 +02:00
|
|
|
/* fallthrough */
|
2007-07-15 21:24:26 +03:00
|
|
|
case MKTAG('I', 'D', 'A', 'T'):
|
2014-11-28 15:03:08 +02:00
|
|
|
if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
|
2021-04-02 16:33:44 +02:00
|
|
|
continue;
|
|
|
|
if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
|
2007-07-15 21:24:26 +03:00
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
case MKTAG('P', 'L', 'T', 'E'):
|
2021-04-02 16:33:44 +02:00
|
|
|
decode_plte_chunk(avctx, s, &gb_chunk);
|
2014-11-14 11:17:36 +02:00
|
|
|
break;
|
2007-07-15 21:24:26 +03:00
|
|
|
case MKTAG('t', 'R', 'N', 'S'):
|
2021-04-02 16:33:44 +02:00
|
|
|
decode_trns_chunk(avctx, s, &gb_chunk);
|
2014-11-14 11:17:36 +02:00
|
|
|
break;
|
2012-10-28 16:41:40 +03:00
|
|
|
case MKTAG('t', 'E', 'X', 't'):
|
2021-04-02 16:33:44 +02:00
|
|
|
if (decode_text_chunk(s, &gb_chunk, 0) < 0)
|
2012-10-28 16:41:40 +03:00
|
|
|
av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
|
|
|
|
break;
|
|
|
|
case MKTAG('z', 'T', 'X', 't'):
|
2021-04-02 16:33:44 +02:00
|
|
|
if (decode_text_chunk(s, &gb_chunk, 1) < 0)
|
2012-10-28 16:41:40 +03:00
|
|
|
av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
|
|
|
|
break;
|
2015-10-22 22:36:52 +02:00
|
|
|
case MKTAG('s', 'T', 'E', 'R'): {
|
2021-04-02 16:33:44 +02:00
|
|
|
int mode = bytestream2_get_byte(&gb_chunk);
|
2015-10-27 19:27:03 +02:00
|
|
|
|
|
|
|
if (mode == 0 || mode == 1) {
|
2021-03-20 20:57:25 +02:00
|
|
|
s->stereo_mode = mode;
|
2015-10-22 22:36:52 +02:00
|
|
|
} else {
|
2015-10-27 19:27:03 +02:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Unknown value in sTER chunk (%d)\n", mode);
|
2015-10-22 22:36:52 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-07-20 22:46:22 +02:00
|
|
|
case MKTAG('i', 'C', 'C', 'P'): {
|
2021-04-02 16:33:44 +02:00
|
|
|
if ((ret = decode_iccp_chunk(s, &gb_chunk, p)) < 0)
|
2017-07-20 22:46:22 +02:00
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-25 21:28:08 +02:00
|
|
|
case MKTAG('c', 'H', 'R', 'M'): {
|
2021-03-20 20:57:25 +02:00
|
|
|
s->have_chrm = 1;
|
2017-09-25 21:28:08 +02:00
|
|
|
|
2021-04-02 16:33:44 +02:00
|
|
|
s->white_point[0] = bytestream2_get_be32(&gb_chunk);
|
|
|
|
s->white_point[1] = bytestream2_get_be32(&gb_chunk);
|
2017-09-25 21:28:08 +02:00
|
|
|
|
|
|
|
/* RGB Primaries */
|
|
|
|
for (i = 0; i < 3; i++) {
|
2021-04-02 16:33:44 +02:00
|
|
|
s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
|
|
|
|
s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
|
2017-09-25 21:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MKTAG('g', 'A', 'M', 'A'): {
|
|
|
|
AVBPrint bp;
|
|
|
|
char *gamma_str;
|
2021-04-02 16:33:44 +02:00
|
|
|
int num = bytestream2_get_be32(&gb_chunk);
|
2017-09-25 21:28:08 +02:00
|
|
|
|
2018-06-10 10:00:02 +02:00
|
|
|
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2017-09-25 21:28:08 +02:00
|
|
|
av_bprintf(&bp, "%i/%i", num, 100000);
|
2017-11-09 10:21:05 +02:00
|
|
|
ret = av_bprint_finalize(&bp, &gamma_str);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-09-25 21:28:08 +02:00
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
|
2017-09-25 21:28:08 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
case MKTAG('I', 'E', 'N', 'D'):
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & PNG_ALLIMAGE))
|
2012-03-20 15:22:40 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
|
2015-05-03 17:50:26 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2007-07-15 21:24:26 +03:00
|
|
|
goto fail;
|
2012-03-20 15:22:40 +03:00
|
|
|
}
|
2007-07-15 21:24:26 +03:00
|
|
|
goto exit_loop;
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 20:32:00 +03:00
|
|
|
exit_loop:
|
2017-02-02 22:48:05 +02:00
|
|
|
|
2015-10-09 15:14:11 +02:00
|
|
|
if (avctx->codec_id == AV_CODEC_ID_PNG &&
|
|
|
|
avctx->skip_frame == AVDISCARD_ALL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-06 14:35:15 +03:00
|
|
|
|
2019-08-17 23:55:34 +02:00
|
|
|
if (percent_missing(s) > avctx->discard_damaged_percentage)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2014-11-14 11:17:38 +02:00
|
|
|
if (s->bits_per_pixel <= 4)
|
|
|
|
handle_small_bpp(s, p);
|
2011-09-06 14:35:15 +03:00
|
|
|
|
2022-03-05 19:36:43 +02:00
|
|
|
if (s->color_type == PNG_COLOR_TYPE_PALETTE && avctx->codec_id == AV_CODEC_ID_APNG) {
|
|
|
|
for (int y = 0; y < s->height; y++) {
|
|
|
|
uint8_t *row = &p->data[0][p->linesize[0] * y];
|
|
|
|
|
|
|
|
for (int x = s->width - 1; x >= 0; x--) {
|
|
|
|
const uint8_t idx = row[x];
|
|
|
|
|
|
|
|
row[4*x+2] = s->palette[idx] & 0xFF;
|
|
|
|
row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF;
|
|
|
|
row[4*x+0] = (s->palette[idx] >> 16) & 0xFF;
|
|
|
|
row[4*x+3] = s->palette[idx] >> 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:53:07 +02:00
|
|
|
/* apply transparency if needed */
|
|
|
|
if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
|
|
|
|
size_t raw_bpp = s->bpp - byte_depth;
|
2022-09-29 01:45:23 +02:00
|
|
|
ptrdiff_t x, y;
|
2015-08-28 15:53:07 +02:00
|
|
|
|
2017-02-04 13:24:14 +02:00
|
|
|
av_assert0(s->bit_depth > 1);
|
|
|
|
|
2015-08-28 15:53:07 +02:00
|
|
|
for (y = 0; y < s->height; ++y) {
|
2021-04-02 10:45:27 +02:00
|
|
|
uint8_t *row = &p->data[0][p->linesize[0] * y];
|
2015-08-28 15:53:07 +02:00
|
|
|
|
2019-08-18 01:28:38 +02:00
|
|
|
if (s->bpp == 2 && byte_depth == 1) {
|
|
|
|
uint8_t *pixel = &row[2 * s->width - 1];
|
|
|
|
uint8_t *rowp = &row[1 * s->width - 1];
|
|
|
|
int tcolor = s->transparent_color_be[0];
|
|
|
|
for (x = s->width; x > 0; --x) {
|
|
|
|
*pixel-- = *rowp == tcolor ? 0 : 0xff;
|
|
|
|
*pixel-- = *rowp--;
|
|
|
|
}
|
|
|
|
} else if (s->bpp == 4 && byte_depth == 1) {
|
|
|
|
uint8_t *pixel = &row[4 * s->width - 1];
|
|
|
|
uint8_t *rowp = &row[3 * s->width - 1];
|
|
|
|
int tcolor = AV_RL24(s->transparent_color_be);
|
|
|
|
for (x = s->width; x > 0; --x) {
|
|
|
|
*pixel-- = AV_RL24(rowp-2) == tcolor ? 0 : 0xff;
|
|
|
|
*pixel-- = *rowp--;
|
|
|
|
*pixel-- = *rowp--;
|
|
|
|
*pixel-- = *rowp--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* since we're updating in-place, we have to go from right to left */
|
|
|
|
for (x = s->width; x > 0; --x) {
|
|
|
|
uint8_t *pixel = &row[s->bpp * (x - 1)];
|
|
|
|
memmove(pixel, &row[raw_bpp * (x - 1)], raw_bpp);
|
2015-08-28 15:53:07 +02:00
|
|
|
|
2019-08-18 01:28:38 +02:00
|
|
|
if (!memcmp(pixel, s->transparent_color_be, raw_bpp)) {
|
|
|
|
memset(&pixel[raw_bpp], 0, byte_depth);
|
|
|
|
} else {
|
|
|
|
memset(&pixel[raw_bpp], 0xff, byte_depth);
|
|
|
|
}
|
2015-08-28 15:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:55:20 +02:00
|
|
|
/* handle P-frames only if a predecessor frame is available */
|
2015-06-07 20:12:30 +02:00
|
|
|
if (s->last_picture.f->data[0]) {
|
2014-03-17 17:24:05 +03:00
|
|
|
if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
|
2015-06-07 20:12:30 +02:00
|
|
|
&& s->last_picture.f->width == p->width
|
|
|
|
&& s->last_picture.f->height== p->height
|
|
|
|
&& s->last_picture.f->format== p->format
|
2012-04-15 18:41:17 +03:00
|
|
|
) {
|
2014-11-27 16:26:27 +02:00
|
|
|
if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
|
2014-11-27 16:26:26 +02:00
|
|
|
handle_p_frame_png(s, p);
|
2014-11-27 16:26:27 +02:00
|
|
|
else if (CONFIG_APNG_DECODER &&
|
|
|
|
avctx->codec_id == AV_CODEC_ID_APNG &&
|
|
|
|
(ret = handle_p_frame_apng(avctx, s, p)) < 0)
|
|
|
|
goto fail;
|
2009-04-10 20:16:19 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 21:21:40 +02:00
|
|
|
if (CONFIG_APNG_DECODER && s->dispose_op == APNG_DISPOSE_OP_BACKGROUND)
|
|
|
|
apng_reset_background(s, p);
|
|
|
|
|
2013-08-12 13:56:20 +03:00
|
|
|
ff_thread_report_progress(&s->picture, INT_MAX, 0);
|
2009-04-10 20:16:19 +03:00
|
|
|
|
2014-11-14 11:17:39 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ff_thread_report_progress(&s->picture, INT_MAX, 0);
|
2014-11-21 13:05:47 +02:00
|
|
|
return ret;
|
2014-11-14 11:17:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
static void clear_frame_metadata(PNGDecContext *s)
|
|
|
|
{
|
|
|
|
av_freep(&s->iccp_data);
|
|
|
|
s->iccp_data_len = 0;
|
|
|
|
s->iccp_name[0] = 0;
|
|
|
|
|
|
|
|
s->stereo_mode = -1;
|
|
|
|
|
|
|
|
s->have_chrm = 0;
|
|
|
|
|
|
|
|
av_dict_free(&s->frame_metadata);
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:21:40 +02:00
|
|
|
static int output_frame(PNGDecContext *s, AVFrame *f)
|
2021-03-20 20:57:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (s->iccp_data) {
|
|
|
|
AVFrameSideData *sd = av_frame_new_side_data(f, AV_FRAME_DATA_ICC_PROFILE, s->iccp_data_len);
|
|
|
|
if (!sd) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memcpy(sd->data, s->iccp_data, s->iccp_data_len);
|
|
|
|
|
|
|
|
av_dict_set(&sd->metadata, "name", s->iccp_name, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->stereo_mode >= 0) {
|
|
|
|
AVStereo3D *stereo3d = av_stereo3d_create_side_data(f);
|
|
|
|
if (!stereo3d) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
|
|
|
|
stereo3d->flags = s->stereo_mode ? 0 : AV_STEREO3D_FLAG_INVERT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->have_chrm) {
|
|
|
|
AVMasteringDisplayMetadata *mdm = av_mastering_display_metadata_create_side_data(f);
|
|
|
|
if (!mdm) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdm->white_point[0] = av_make_q(s->white_point[0], 100000);
|
|
|
|
mdm->white_point[1] = av_make_q(s->white_point[1], 100000);
|
|
|
|
|
|
|
|
/* RGB Primaries */
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
mdm->display_primaries[i][0] = av_make_q(s->display_primaries[i][0], 100000);
|
|
|
|
mdm->display_primaries[i][1] = av_make_q(s->display_primaries[i][1], 100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
mdm->has_primaries = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FFSWAP(AVDictionary*, f->metadata, s->frame_metadata);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_frame_unref(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
#if CONFIG_PNG_DECODER
|
2022-08-20 21:21:40 +02:00
|
|
|
static int decode_frame_png(AVCodecContext *avctx, AVFrame *p,
|
2022-03-30 21:33:24 +02:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2014-11-14 11:17:39 +02:00
|
|
|
{
|
|
|
|
PNGDecContext *const s = avctx->priv_data;
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
int64_t sig;
|
|
|
|
int ret;
|
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
clear_frame_metadata(s);
|
|
|
|
|
2014-11-14 11:17:39 +02:00
|
|
|
bytestream2_init(&s->gb, buf, buf_size);
|
|
|
|
|
|
|
|
/* check signature */
|
|
|
|
sig = bytestream2_get_be64(&s->gb);
|
|
|
|
if (sig != PNGSIG &&
|
|
|
|
sig != MNGSIG) {
|
2015-08-05 12:10:08 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature 0x%08"PRIX64".\n", sig);
|
2014-11-14 11:17:39 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
s->y = s->has_trns = 0;
|
|
|
|
s->hdr_state = 0;
|
|
|
|
s->pic_state = 0;
|
2014-11-14 11:17:39 +02:00
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
/* Reset z_stream */
|
|
|
|
ret = inflateReset(&s->zstream.zstream);
|
|
|
|
if (ret != Z_OK)
|
2014-11-14 11:17:39 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
|
|
|
|
if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
|
|
|
|
goto the_end;
|
2013-03-12 05:20:18 +03:00
|
|
|
|
2015-10-09 15:14:11 +02:00
|
|
|
if (avctx->skip_frame == AVDISCARD_ALL) {
|
|
|
|
*got_frame = 0;
|
|
|
|
ret = bytestream2_tell(&s->gb);
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:21:40 +02:00
|
|
|
ret = output_frame(s, p);
|
2021-03-20 20:57:25 +02:00
|
|
|
if (ret < 0)
|
2017-09-17 02:42:11 +02:00
|
|
|
goto the_end;
|
2012-11-21 23:34:46 +03:00
|
|
|
|
2021-02-14 17:37:33 +02:00
|
|
|
if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->last_picture);
|
2021-02-14 17:37:33 +02:00
|
|
|
FFSWAP(ThreadFrame, s->picture, s->last_picture);
|
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2007-07-15 21:24:26 +03:00
|
|
|
|
2012-03-08 03:16:20 +03:00
|
|
|
ret = bytestream2_tell(&s->gb);
|
2014-03-14 20:32:00 +03:00
|
|
|
the_end:
|
2009-05-24 15:44:54 +03:00
|
|
|
s->crow_buf = NULL;
|
2007-07-15 21:24:26 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2014-11-21 13:05:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_APNG_DECODER
|
2022-08-20 21:21:40 +02:00
|
|
|
static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p,
|
2022-03-30 21:33:24 +02:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2014-11-21 13:05:47 +02:00
|
|
|
{
|
|
|
|
PNGDecContext *const s = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
clear_frame_metadata(s);
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->hdr_state & PNG_IHDR)) {
|
2016-11-18 17:08:54 +02:00
|
|
|
if (!avctx->extradata_size)
|
2014-11-21 13:05:47 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
|
|
|
|
return AVERROR_EXTERNAL;
|
2016-11-18 17:08:54 +02:00
|
|
|
bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
|
2014-11-21 13:05:47 +02:00
|
|
|
if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
|
2022-03-15 16:05:53 +02:00
|
|
|
return ret;
|
2014-11-21 13:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reset state for a new frame */
|
2022-03-15 16:05:53 +02:00
|
|
|
if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
|
|
|
|
return AVERROR_EXTERNAL;
|
2014-11-21 13:05:47 +02:00
|
|
|
s->y = 0;
|
2017-04-03 16:08:29 +02:00
|
|
|
s->pic_state = 0;
|
2014-11-21 13:05:47 +02:00
|
|
|
bytestream2_init(&s->gb, avpkt->data, avpkt->size);
|
|
|
|
if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
|
2022-03-15 16:05:53 +02:00
|
|
|
return ret;
|
2014-11-21 13:05:47 +02:00
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
if (!(s->pic_state & PNG_ALLIMAGE))
|
2014-11-21 13:05:47 +02:00
|
|
|
av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
|
2022-03-15 16:05:53 +02:00
|
|
|
if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT)))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2021-03-20 20:57:25 +02:00
|
|
|
|
2022-08-20 21:21:40 +02:00
|
|
|
ret = output_frame(s, p);
|
2021-03-20 20:57:25 +02:00
|
|
|
if (ret < 0)
|
2022-03-15 16:05:53 +02:00
|
|
|
return ret;
|
2014-11-21 13:05:47 +02:00
|
|
|
|
2021-02-14 17:37:33 +02:00
|
|
|
if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
|
|
|
|
if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->picture);
|
2021-05-08 13:02:33 +02:00
|
|
|
} else {
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->last_picture);
|
2021-02-14 17:37:33 +02:00
|
|
|
FFSWAP(ThreadFrame, s->picture, s->last_picture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
*got_frame = 1;
|
2022-03-15 16:05:53 +02:00
|
|
|
return bytestream2_tell(&s->gb);
|
2014-11-21 13:05:47 +02:00
|
|
|
}
|
|
|
|
#endif
|
2007-07-15 21:24:26 +03:00
|
|
|
|
2015-09-17 04:22:27 +02:00
|
|
|
#if HAVE_THREADS
|
2013-08-12 13:56:20 +03:00
|
|
|
static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
|
|
|
|
{
|
|
|
|
PNGDecContext *psrc = src->priv_data;
|
|
|
|
PNGDecContext *pdst = dst->priv_data;
|
2021-02-14 17:37:33 +02:00
|
|
|
ThreadFrame *src_frame = NULL;
|
2014-12-03 15:16:55 +02:00
|
|
|
int ret;
|
2013-08-12 13:56:20 +03:00
|
|
|
|
|
|
|
if (dst == src)
|
|
|
|
return 0;
|
|
|
|
|
2014-12-03 15:16:55 +02:00
|
|
|
if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
|
2021-02-14 17:37:33 +02:00
|
|
|
|
2015-06-29 21:59:41 +02:00
|
|
|
pdst->width = psrc->width;
|
|
|
|
pdst->height = psrc->height;
|
|
|
|
pdst->bit_depth = psrc->bit_depth;
|
|
|
|
pdst->color_type = psrc->color_type;
|
|
|
|
pdst->compression_type = psrc->compression_type;
|
|
|
|
pdst->interlace_type = psrc->interlace_type;
|
|
|
|
pdst->filter_type = psrc->filter_type;
|
2015-07-19 22:20:56 +02:00
|
|
|
pdst->has_trns = psrc->has_trns;
|
2015-08-28 15:53:07 +02:00
|
|
|
memcpy(pdst->transparent_color_be, psrc->transparent_color_be, sizeof(pdst->transparent_color_be));
|
2015-06-29 21:59:41 +02:00
|
|
|
|
|
|
|
memcpy(pdst->palette, psrc->palette, sizeof(pdst->palette));
|
|
|
|
|
2017-04-03 16:08:29 +02:00
|
|
|
pdst->hdr_state |= psrc->hdr_state;
|
2021-02-14 17:37:33 +02:00
|
|
|
}
|
2015-06-29 21:59:41 +02:00
|
|
|
|
2021-05-08 13:02:33 +02:00
|
|
|
src_frame = psrc->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
|
|
|
|
&psrc->last_picture : &psrc->picture;
|
2015-07-19 04:43:20 +02:00
|
|
|
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(dst, &pdst->last_picture);
|
2021-02-14 17:37:33 +02:00
|
|
|
if (src_frame && src_frame->f->data[0]) {
|
|
|
|
ret = ff_thread_ref_frame(&pdst->last_picture, src_frame);
|
|
|
|
if (ret < 0)
|
2015-07-19 04:43:20 +02:00
|
|
|
return ret;
|
2014-12-03 15:16:55 +02:00
|
|
|
}
|
2013-08-12 13:56:20 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-17 04:22:27 +02:00
|
|
|
#endif
|
2013-08-12 13:56:20 +03:00
|
|
|
|
2011-04-27 19:34:10 +03:00
|
|
|
static av_cold int png_dec_init(AVCodecContext *avctx)
|
|
|
|
{
|
2007-07-15 22:23:55 +03:00
|
|
|
PNGDecContext *s = avctx->priv_data;
|
|
|
|
|
2015-05-08 17:01:50 +02:00
|
|
|
avctx->color_range = AVCOL_RANGE_JPEG;
|
|
|
|
|
2013-08-12 13:56:20 +03:00
|
|
|
s->avctx = avctx;
|
|
|
|
s->last_picture.f = av_frame_alloc();
|
|
|
|
s->picture.f = av_frame_alloc();
|
2022-02-08 12:03:52 +02:00
|
|
|
if (!s->last_picture.f || !s->picture.f)
|
2012-11-21 23:34:46 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2011-04-27 19:34:10 +03:00
|
|
|
|
2017-01-09 19:04:42 +02:00
|
|
|
ff_pngdsp_init(&s->dsp);
|
2012-09-15 22:30:57 +03:00
|
|
|
|
2022-03-15 16:05:53 +02:00
|
|
|
return ff_inflate_init(&s->zstream, avctx);
|
2007-07-15 22:23:55 +03:00
|
|
|
}
|
|
|
|
|
2009-12-01 13:38:37 +02:00
|
|
|
static av_cold int png_dec_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
PNGDecContext *s = avctx->priv_data;
|
|
|
|
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->last_picture);
|
2013-08-12 13:56:20 +03:00
|
|
|
av_frame_free(&s->last_picture.f);
|
2022-02-06 08:44:10 +02:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->picture);
|
2013-08-12 13:56:20 +03:00
|
|
|
av_frame_free(&s->picture.f);
|
2013-08-22 20:05:53 +03:00
|
|
|
av_freep(&s->buffer);
|
|
|
|
s->buffer_size = 0;
|
|
|
|
av_freep(&s->last_row);
|
|
|
|
s->last_row_size = 0;
|
2013-08-30 17:06:05 +03:00
|
|
|
av_freep(&s->tmp_row);
|
|
|
|
s->tmp_row_size = 0;
|
2009-12-01 13:38:37 +02:00
|
|
|
|
2021-03-20 20:57:25 +02:00
|
|
|
av_freep(&s->iccp_data);
|
|
|
|
av_dict_free(&s->frame_metadata);
|
2022-03-15 16:05:53 +02:00
|
|
|
ff_inflate_end(&s->zstream);
|
2021-03-20 20:57:25 +02:00
|
|
|
|
2009-12-01 13:38:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-21 13:05:47 +02:00
|
|
|
#if CONFIG_APNG_DECODER
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_apng_decoder = {
|
|
|
|
.p.name = "apng",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_APNG,
|
2014-11-21 13:05:47 +02:00
|
|
|
.priv_data_size = sizeof(PNGDecContext),
|
|
|
|
.init = png_dec_init,
|
|
|
|
.close = png_dec_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame_apng),
|
2022-08-29 15:03:21 +02:00
|
|
|
UPDATE_THREAD_CONTEXT(update_thread_context),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
|
2022-06-28 14:25:15 +02:00
|
|
|
FF_CODEC_CAP_ALLOCATE_PROGRESS |
|
|
|
|
FF_CODEC_CAP_ICC_PROFILES,
|
2014-11-21 13:05:47 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_PNG_DECODER
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_png_decoder = {
|
|
|
|
.p.name = "png",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_PNG,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(PNGDecContext),
|
|
|
|
.init = png_dec_init,
|
|
|
|
.close = png_dec_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame_png),
|
2022-08-29 15:03:21 +02:00
|
|
|
UPDATE_THREAD_CONTEXT(update_thread_context),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
|
2022-06-28 14:25:15 +02:00
|
|
|
FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP |
|
|
|
|
FF_CODEC_CAP_ICC_PROFILES,
|
2007-07-15 21:24:26 +03:00
|
|
|
};
|
2014-11-21 13:05:47 +02:00
|
|
|
#endif
|