38 #define IOBUF_SIZE 4096
83 int bits_per_pixel,
int pass,
86 int x,
mask, dst_x, j,
b, bpp;
89 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
92 switch (bits_per_pixel) {
94 memset(dst, 0, row_size);
96 for (x = 0; x <
width; x++) {
98 if ((mask << j) & 0x80) {
99 b = (src[x >> 3] >> (7 - j)) & 1;
100 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
106 bpp = bits_per_pixel >> 3;
109 for (x = 0; x <
width; x++) {
111 if ((mask << j) & 0x80) {
125 for (i = 0; i <
w; i++) {
126 int a,
b,
c, p, pa, pb, pc;
139 if (pa <= pb && pa <= pc)
155 memcpy(dst, src, bpp);
158 unaligned_w =
FFMIN(32 - bpp, size);
159 for (x = 0; x < unaligned_w; x++)
160 *dst++ = *src1++ - *src2++;
170 switch (filter_type) {
172 memcpy(dst, src, size);
181 for (i = 0; i < bpp; i++)
182 dst[i] = src[i] - (top[i] >> 1);
183 for (; i <
size; i++)
184 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
187 for (i = 0; i < bpp; i++)
188 dst[i] = src[i] - top[i];
203 int cost, bcost = INT_MAX;
204 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
205 for (pred = 0; pred < 5; pred++) {
209 for (i = 0; i <=
size; i++)
210 cost += abs((int8_t) buf1[i]);
231 bytestream_put_be32(f, length);
233 crc =
av_crc(crc_table, crc, tagbuf, 4);
236 crc =
av_crc(crc_table, crc, buf, length);
237 memcpy(*f, buf, length);
240 bytestream_put_be32(f, ~crc);
255 bytestream_put_be32(&s->
bytestream, length + 4);
261 crc =
av_crc(crc_table, crc, buf, length);
278 while (s->
zstream.avail_in > 0) {
282 if (s->
zstream.avail_out == 0) {
292 #define AV_WB32_PNG(buf, n) AV_WB32(buf, lrint((n) * 100000))
295 double rx, ry, gx, gy, bx, by, wx = 0.3127, wy = 0.3290;
298 rx = 0.640; ry = 0.330;
299 gx = 0.300; gy = 0.600;
300 bx = 0.150; by = 0.060;
303 rx = 0.670; ry = 0.330;
304 gx = 0.210; gy = 0.710;
305 bx = 0.140; by = 0.080;
306 wx = 0.310; wy = 0.316;
309 rx = 0.640; ry = 0.330;
310 gx = 0.290; gy = 0.600;
311 bx = 0.150; by = 0.060;
315 rx = 0.630; ry = 0.340;
316 gx = 0.310; gy = 0.595;
317 bx = 0.155; by = 0.070;
320 rx = 0.708; ry = 0.292;
321 gx = 0.170; gy = 0.797;
322 bx = 0.131; by = 0.046;
376 switch (stereo3d->
type) {
384 av_log(avctx,
AV_LOG_WARNING,
"Only side-by-side stereo3d flag can be defined within sTER chunk\n");
403 int has_alpha,
alpha, i;
408 palette = (uint32_t *)pict->
data[1];
410 alpha_ptr = s->
buf + 256 * 3;
412 for (i = 0; i < 256; i++) {
417 *alpha_ptr++ =
alpha;
418 bytestream_put_be24(&ptr, v);
421 MKTAG(
'P',
'L',
'T',
'E'), s->
buf, 256 * 3);
424 MKTAG(
't',
'R',
'N',
'S'), s->
buf + 256 * 3, 256);
437 uint8_t *ptr, *top, *crow_buf, *crow;
450 crow_buf = crow_base + 15;
452 progressive_buf =
av_malloc(row_size + 1);
454 if (!progressive_buf || !top_buf) {
466 for (pass = 0; pass <
NB_PASSES; pass++) {
470 if (pass_row_size > 0) {
472 for (y = 0; y < pict->
height; y++)
482 top = progressive_buf;
488 for (y = 0; y < pict->
height; y++) {
499 if (ret == Z_OK || ret == Z_STREAM_END) {
506 if (ret == Z_STREAM_END)
525 const AVFrame *pict,
int *got_packet)
530 size_t max_packet_size;
539 if (max_packet_size > INT_MAX)
576 unsigned int leftmost_x = input->
width;
577 unsigned int rightmost_x = 0;
578 unsigned int topmost_y = input->
height;
579 unsigned int bottommost_y = 0;
582 ptrdiff_t input_linesize = input->
linesize[0];
583 ptrdiff_t output_linesize = output->
linesize[0];
586 for (y = 0; y < input->
height; ++y) {
587 for (x = 0; x < input->
width; ++x) {
588 if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp))
593 if (x >= rightmost_x)
597 if (y >= bottommost_y)
598 bottommost_y = y + 1;
601 input_data += input_linesize;
602 output_data += output_linesize;
605 if (leftmost_x == input->
width && rightmost_x == 0) {
608 leftmost_x = topmost_y = 0;
609 rightmost_x = bottommost_y = 1;
614 output_data = output->
data[0];
615 for (y = topmost_y; y < bottommost_y; ++y) {
617 input->
data[0] + input_linesize * y + bpp * leftmost_x,
618 bpp * (rightmost_x - leftmost_x));
619 output_data += output_linesize;
622 size_t transparent_palette_index;
633 palette = (uint32_t*)input->
data[1];
634 for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index)
635 if (palette[transparent_palette_index] >> 24 == 0)
644 for (y = topmost_y; y < bottommost_y; ++y) {
645 uint8_t *foreground = input->
data[0] + input_linesize * y + bpp * leftmost_x;
646 uint8_t *background = output->
data[0] + output_linesize * y + bpp * leftmost_x;
647 output_data = output->
data[0] + output_linesize * (y - topmost_y);
648 for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) {
649 if (!memcmp(foreground, background, bpp)) {
651 if (transparent_palette_index == 256) {
656 *output_data = transparent_palette_index;
658 memset(output_data, 0, bpp);
669 if (((uint16_t*)foreground)[3] == 0xffff ||
670 ((uint16_t*)background)[3] == 0)
675 if (((uint16_t*)foreground)[1] == 0xffff ||
676 ((uint16_t*)background)[1] == 0)
681 if (foreground[3] == 0xff || background[3] == 0)
686 if (foreground[1] == 0xff || background[1] == 0)
691 if (palette[*foreground] >> 24 == 0xff ||
692 palette[*background] >> 24 == 0)
697 memmove(output_data, foreground, bpp);
702 output->
width = rightmost_x - leftmost_x;
703 output->
height = bottommost_y - topmost_y;
720 uint8_t *original_bytestream, *original_bytestream_end;
721 uint8_t *temp_bytestream = 0, *temp_bytestream_end;
722 uint32_t best_sequence_number;
724 size_t best_bytestream_size = SIZE_MAX;
750 temp_bytestream =
av_malloc(original_bytestream_end - original_bytestream);
751 temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream);
752 if (!temp_bytestream) {
766 uint32_t original_sequence_number = s->
sequence_number, sequence_number;
768 size_t bytestream_size;
780 size_t row_start = diffFrame->
linesize[0] * y + bpp * last_fctl_chunk.
x_offset;
781 memset(diffFrame->
data[0] + row_start, 0, bpp * last_fctl_chunk.
width);
803 bytestream_size = s->
bytestream - bytestream_start;
808 if (bytestream_size < best_bytestream_size) {
809 *best_fctl_chunk = fctl_chunk;
810 *best_last_fctl_chunk = last_fctl_chunk;
812 best_sequence_number = sequence_number;
814 best_bytestream_size = bytestream_size;
816 if (best_bytestream == original_bytestream) {
828 s->
bytestream = original_bytestream + best_bytestream_size;
830 if (best_bytestream != original_bytestream)
831 memcpy(original_bytestream, best_bytestream, best_bytestream_size);
842 const AVFrame *pict,
int *got_packet)
847 size_t max_packet_size;
857 "Input contains more than one unique palette. APNG does not support multiple palettes.\n");
869 if (max_packet_size > INT_MAX)
992 int compression_level;
1011 #if FF_API_CODED_FRAME
1020 #if FF_API_PRIVATE_OPT
1035 }
else if (s->
dpi) {
1036 s->
dpm = s->
dpi * 10000 / 254;
1090 ? Z_DEFAULT_COMPRESSION
1092 if (deflateInit2(&s->
zstream, compression_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
1111 #define OFFSET(x) offsetof(PNGEncContext, x)
1112 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1115 {
"dpm",
"Set image resolution (in dots per meter)",
OFFSET(dpm),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000,
VE},
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
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 AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
packed RGB 8:8:8, 24bpp, RGBRGB...
static av_cold int init(AVCodecContext *avctx)
static const AVClass apngenc_class
const char * av_default_item_name(void *ptr)
Return the context name.
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)
Views are next to each other.
#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.
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
size_t last_frame_packet_size
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
#define PNG_FILTER_VALUE_PAETH
av_cold void ff_llvidencdsp_init(LLVidEncDSPContext *c)
void(* diff_bytes)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, intptr_t w)
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_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#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.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define PNG_COLOR_TYPE_RGB_ALPHA
8 bits with AV_PIX_FMT_RGB32 palette
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
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).
Public header for CRC hash function implementation.
Structure to hold side data for an AVFrame.
static void input_data(MLPEncodeContext *ctx, void *samples)
Wrapper function for inputting data in two different bit-depths.
Video is not stereoscopic (and metadata has to be there).
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)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int flags
Additional information about the frame packing.
16 bits gray, 16 bits 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]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#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.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
int flags
A combination of AV_PKT_FLAG values.
uint8_t * bytestream_start
enum AVPictureType pict_type
Picture type of the frame.
static int output_data(MLPDecodeContext *m, unsigned int substr, AVFrame *frame, int *got_frame_ptr)
Write the audio data into the output buffer.
#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 volatile int checksum
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)
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
enum AVStereo3DType type
How views are packed within the video.
Libavcodec external API header.
functionally identical to above
attribute_deprecated int prediction_method
#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)
static const int16_t alpha[]
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)
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...
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
void ff_png_zfree(void *opaque, void *ptr)
#define MKBETAG(a, b, c, d)
#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)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
#define MKTAG(a, b, c, d)
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
Stereoscopic 3d metadata.
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...
LLVidEncDSPContext llvidencdsp
static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)