37 #define IOBUF_SIZE 4096
79 int bits_per_pixel,
int pass,
82 int x,
mask, dst_x, j,
b, bpp;
85 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
88 switch (bits_per_pixel) {
90 memset(dst, 0, row_size);
92 for (x = 0; x <
width; x++) {
94 if ((mask << j) & 0x80) {
95 b = (src[x >> 3] >> (7 - j)) & 1;
96 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
102 bpp = bits_per_pixel >> 3;
105 for (x = 0; x <
width; x++) {
107 if ((mask << j) & 0x80) {
121 for (i = 0; i < w; i++) {
122 int a,
b,
c, p, pa, pb, pc;
135 if (pa <= pb && pa <= pc)
151 memcpy(dst, src, bpp);
154 unaligned_w =
FFMIN(32 - bpp, size);
155 for (x = 0; x < unaligned_w; x++)
156 *dst++ = *src1++ - *src2++;
166 switch (filter_type) {
168 memcpy(dst, src, size);
177 for (i = 0; i < bpp; i++)
178 dst[i] = src[i] - (top[i] >> 1);
179 for (; i <
size; i++)
180 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
183 for (i = 0; i < bpp; i++)
184 dst[i] = src[i] - top[i];
199 int cost, bcost = INT_MAX;
200 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
201 for (pred = 0; pred < 5; pred++) {
205 for (i = 0; i <=
size; i++)
206 cost += abs((int8_t) buf1[i]);
227 bytestream_put_be32(f, length);
229 crc =
av_crc(crc_table, crc, tagbuf, 4);
232 crc =
av_crc(crc_table, crc, buf, length);
233 memcpy(*f, buf, length);
236 bytestream_put_be32(f, ~crc);
251 bytestream_put_be32(&s->
bytestream, length + 4);
257 crc =
av_crc(crc_table, crc, buf, length);
274 while (s->
zstream.avail_in > 0) {
278 if (s->
zstream.avail_out == 0) {
288 #define AV_WB32_PNG(buf, n) AV_WB32(buf, lrint((n) * 100000))
291 double rx, ry, gx, gy, bx, by, wx = 0.3127, wy = 0.3290;
294 rx = 0.640; ry = 0.330;
295 gx = 0.300; gy = 0.600;
296 bx = 0.150; by = 0.060;
299 rx = 0.670; ry = 0.330;
300 gx = 0.210; gy = 0.710;
301 bx = 0.140; by = 0.080;
302 wx = 0.310; wy = 0.316;
305 rx = 0.640; ry = 0.330;
306 gx = 0.290; gy = 0.600;
307 bx = 0.150; by = 0.060;
311 rx = 0.630; ry = 0.340;
312 gx = 0.310; gy = 0.595;
313 bx = 0.155; by = 0.070;
316 rx = 0.708; ry = 0.292;
317 gx = 0.170; gy = 0.797;
318 bx = 0.131; by = 0.046;
381 int has_alpha,
alpha, i;
386 palette = (uint32_t *)pict->
data[1];
388 alpha_ptr = s->
buf + 256 * 3;
390 for (i = 0; i < 256; i++) {
395 *alpha_ptr++ =
alpha;
396 bytestream_put_be24(&ptr, v);
399 MKTAG(
'P',
'L',
'T',
'E'), s->
buf, 256 * 3);
402 MKTAG(
't',
'R',
'N',
'S'), s->
buf + 256 * 3, 256);
415 uint8_t *ptr, *top, *crow_buf, *crow;
428 crow_buf = crow_base + 15;
430 progressive_buf =
av_malloc(row_size + 1);
432 if (!progressive_buf || !top_buf) {
444 for (pass = 0; pass <
NB_PASSES; pass++) {
448 if (pass_row_size > 0) {
450 for (y = 0; y < pict->
height; y++)
460 top = progressive_buf;
466 for (y = 0; y < pict->
height; y++) {
477 if (ret == Z_OK || ret == Z_STREAM_END) {
484 if (ret == Z_STREAM_END)
503 const AVFrame *pict,
int *got_packet)
508 size_t max_packet_size;
517 if (max_packet_size > INT_MAX)
554 unsigned int leftmost_x = input->
width;
555 unsigned int rightmost_x = 0;
556 unsigned int topmost_y = input->
height;
557 unsigned int bottommost_y = 0;
560 ptrdiff_t input_linesize = input->
linesize[0];
561 ptrdiff_t output_linesize = output->
linesize[0];
564 for (y = 0; y < input->
height; ++
y) {
565 for (x = 0; x < input->
width; ++x) {
566 if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp))
571 if (x >= rightmost_x)
575 if (y >= bottommost_y)
576 bottommost_y = y + 1;
579 input_data += input_linesize;
580 output_data += output_linesize;
583 if (leftmost_x == input->
width && rightmost_x == 0) {
586 leftmost_x = topmost_y = 0;
587 rightmost_x = bottommost_y = 1;
592 output_data = output->
data[0];
593 for (y = topmost_y; y < bottommost_y; ++
y) {
595 input->
data[0] + input_linesize * y + bpp * leftmost_x,
596 bpp * (rightmost_x - leftmost_x));
597 output_data += output_linesize;
600 size_t transparent_palette_index;
611 palette = (uint32_t*)input->
data[1];
612 for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index)
613 if (palette[transparent_palette_index] >> 24 == 0)
622 for (y = topmost_y; y < bottommost_y; ++
y) {
623 uint8_t *foreground = input->
data[0] + input_linesize * y + bpp * leftmost_x;
624 uint8_t *background = output->
data[0] + output_linesize * y + bpp * leftmost_x;
625 output_data = output->
data[0] + output_linesize * (y - topmost_y);
626 for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) {
627 if (!memcmp(foreground, background, bpp)) {
629 if (transparent_palette_index == 256) {
634 *output_data = transparent_palette_index;
636 memset(output_data, 0, bpp);
647 if (((uint16_t*)foreground)[3] == 0xffff ||
648 ((uint16_t*)background)[3] == 0)
653 if (((uint16_t*)foreground)[1] == 0xffff ||
654 ((uint16_t*)background)[1] == 0)
659 if (foreground[3] == 0xff || background[3] == 0)
664 if (foreground[1] == 0xff || background[1] == 0)
669 if (palette[*foreground] >> 24 == 0xff ||
670 palette[*background] >> 24 == 0)
675 memmove(output_data, foreground, bpp);
680 output->
width = rightmost_x - leftmost_x;
681 output->
height = bottommost_y - topmost_y;
698 uint8_t *original_bytestream, *original_bytestream_end;
699 uint8_t *temp_bytestream = 0, *temp_bytestream_end;
700 uint32_t best_sequence_number;
702 size_t best_bytestream_size = SIZE_MAX;
728 temp_bytestream =
av_malloc(original_bytestream_end - original_bytestream);
729 temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream);
730 if (!temp_bytestream) {
744 uint32_t original_sequence_number = s->
sequence_number, sequence_number;
746 size_t bytestream_size;
756 memset(diffFrame->
data[0] + row_start, 0, bpp * last_fctl_chunk.
width);
775 bytestream_size = s->
bytestream - bytestream_start;
780 if (bytestream_size < best_bytestream_size) {
781 *best_fctl_chunk = fctl_chunk;
782 *best_last_fctl_chunk = last_fctl_chunk;
784 best_sequence_number = sequence_number;
786 best_bytestream_size = bytestream_size;
788 if (best_bytestream == original_bytestream) {
800 s->
bytestream = original_bytestream + best_bytestream_size;
802 if (best_bytestream != original_bytestream)
803 memcpy(original_bytestream, best_bytestream, best_bytestream_size);
814 const AVFrame *pict,
int *got_packet)
819 size_t max_packet_size;
829 "Input contains more than one unique palette. APNG does not support multiple palettes.\n");
841 if (max_packet_size > INT_MAX)
955 int compression_level;
974 #if FF_API_CODED_FRAME
993 s->
dpm = s->
dpi * 10000 / 254;
1047 ? Z_DEFAULT_COMPRESSION
1049 if (deflateInit2(&s->
zstream, compression_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
1066 #define OFFSET(x) offsetof(PNGEncContext, x)
1067 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1070 {
"dpm",
"Set image resolution (in dots per meter)",
OFFSET(dpm),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000,
VE},
static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *top, int size, int bpp)
#define PNG_FILTER_VALUE_AVG
APNGFctlChunk last_frame_fctl
#define FF_COMPRESSION_DEFAULT
This structure describes decoded (raw) audio or video data.
BYTE int const BYTE int int row_size
ptrdiff_t const GLvoid * data
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, uint8_t *src, uint8_t *top, int size, int bpp)
#define LIBAVUTIL_VERSION_INT
packed RGB 8:8:8, 24bpp, RGBRGB...
static av_cold int init(AVCodecContext *avctx)
static const AVClass apngenc_class
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int encode_png(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
#define PNG_COLOR_TYPE_RGB
static const AVClass pngenc_class
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_COLOR_TYPE_PALETTE
AVColorTransferCharacteristic
Color Transfer Characteristic.
av_cold void ff_huffyuvencdsp_init(HuffYUVEncDSPContext *c)
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
size_t last_frame_packet_size
#define PNG_FILTER_VALUE_PAETH
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
double avpriv_get_gamma_from_trc(enum AVColorTransferCharacteristic trc)
Determine a suitable 'gamma' value to match the supplied AVColorTransferCharacteristic.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define PNG_COLOR_TYPE_RGB_ALPHA
8 bit with AV_PIX_FMT_RGB32 palette
HuffYUVEncDSPContext hdsp
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
static void deflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord)
static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict, APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk)
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
AVColorPrimaries
Chromaticity coordinates of the source primaries.
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
#define PNG_FILTER_VALUE_MIXED
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
uint32_t palette_checksum
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
static double alpha(void *priv, double x, double y)
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
16bit gray, 16bit alpha (big-endian)
static void png_get_interlaced_row(uint8_t *dst, int row_size, int bits_per_pixel, int pass, const uint8_t *src, int width)
static const uint16_t mask[17]
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
void(* diff_bytes)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int dpm
Physical pixel density, in dots per meter, if set.
int ff_png_get_nb_channels(int color_type)
#define AV_WB32_PNG(buf, n)
int flags
AV_CODEC_FLAG_*.
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
simple assert() macros that are a bit more flexible than ISO C assert().
static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
const char * name
Name of the codec implementation.
Libavcodec external API header.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
int flags
A combination of AV_PKT_FLAG values.
uint8_t * bytestream_start
enum AVPictureType pict_type
Picture type of the frame.
#define PNG_FILTER_VALUE_SUB
static void png_write_chunk(uint8_t **f, uint32_t tag, const uint8_t *buf, int length)
#define PNG_COLOR_TYPE_GRAY
int width
picture width / height.
static const AVOption options[]
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
uint8_t * last_frame_packet
static int encode_apng(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
static const float pred[4]
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
functionally identical to above
#define PNG_FILTER_VALUE_UP
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static void png_write_image_data(AVCodecContext *avctx, const uint8_t *buf, int length)
main external API structure.
const uint8_t ff_png_pass_ymask[NB_PASSES]
Replacements for frequently missing libm functions.
Describe the class of an AVClass context structure.
int dpi
Physical pixel density, in dots per inch, if set.
static av_cold int png_enc_init(AVCodecContext *avctx)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
static enum AVPixelFormat pix_fmts[]
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
IEC 61966-2-1 (sRGB or sYCC)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
#define PNG_FILTER_VALUE_NONE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
int prediction_method
prediction method (needed for huffyuv)
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
void ff_png_zfree(void *opaque, void *ptr)
#define MKBETAG(a, b, c, d)
static int output_data(MLPDecodeContext *m, unsigned int substr, AVFrame *frame, int *got_frame_ptr)
Write the audio data into the output buffer.
#define FF_ENABLE_DEPRECATION_WARNINGS
int key_frame
1 -> keyframe, 0-> not
static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
enum AVColorPrimaries color_primaries
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
int frame_number
Frame counter, set by libavcodec.
enum AVColorTransferCharacteristic color_trc
static int apng_do_inverse_blend(AVFrame *output, const AVFrame *input, APNGFctlChunk *fctl_chunk, uint8_t bpp)
static av_cold int png_enc_close(AVCodecContext *avctx)
#define FFSWAP(type, a, b)
#define MKTAG(a, b, c, d)
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
AVPixelFormat
Pixel format.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define FF_MIN_BUFFER_SIZE
static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)