33 #define IOBUF_SIZE 4096
49 int bits_per_pixel,
int pass,
52 int x,
mask, dst_x, j,
b, bpp;
57 switch (bits_per_pixel) {
59 memset(dst, 0, row_size);
61 for (x = 0; x <
width; x++) {
63 if ((mask << j) & 0x80) {
64 b = (src[x >> 3] >> (7 - j)) & 1;
65 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
71 bpp = bits_per_pixel >> 3;
74 for (x = 0; x <
width; x++) {
76 if ((mask << j) & 0x80) {
90 for (i = 0; i < w; i++) {
91 int a,
b, c, p, pa, pb, pc;
104 if (pa <= pb && pa <= pc)
119 switch (filter_type) {
121 memcpy(dst, src, size);
125 memcpy(dst, src, bpp);
131 for (i = 0; i < bpp; i++)
132 dst[i] = src[i] - (top[i] >> 1);
133 for (; i <
size; i++)
134 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
137 for (i = 0; i < bpp; i++)
138 dst[i] = src[i] - top[i];
148 assert(bpp || !pred);
153 int cost, bcost = INT_MAX;
154 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
155 for (pred = 0; pred < 5; pred++) {
159 for (i = 0; i <=
size; i++)
160 cost += abs((int8_t) buf1[i]);
181 for (j = 0; j <
width; j++) {
182 v = ((
const uint32_t *) src)[j];
192 const uint8_t *buf,
int length)
197 bytestream_put_be32(f, length);
198 crc = crc32(0, Z_NULL, 0);
200 crc = crc32(crc, tagbuf, 4);
203 crc = crc32(crc, buf, length);
204 memcpy(*f, buf, length);
207 bytestream_put_be32(f, crc);
217 while (s->
zstream.avail_in > 0) {
218 ret = deflate(&s->
zstream, Z_NO_FLUSH);
221 if (s->
zstream.avail_out == 0) {
233 const AVFrame *pict,
int *got_packet)
237 int bit_depth, color_type, y,
len, row_size, ret, is_progressive;
238 int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
239 int compression_level;
240 uint8_t *ptr, *top, *crow_buf, *crow;
284 row_size = (avctx->
width * bits_per_pixel + 7) >> 3;
290 ? Z_DEFAULT_COMPRESSION
292 ret = deflateInit2(&s->
zstream, compression_level,
293 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
297 enc_row_size = deflateBound(&s->
zstream, row_size);
298 max_packet_size = avctx->
height * (enc_row_size +
316 crow_buf = crow_base + 15;
317 if (is_progressive) {
318 progressive_buf =
av_malloc(row_size + 1);
319 if (!progressive_buf)
339 s->
buf[8] = bit_depth;
340 s->
buf[9] = color_type;
343 s->
buf[12] = is_progressive;
349 int has_alpha, alpha, i;
354 palette = (uint32_t *)p->
data[1];
356 alpha_ptr = s->
buf + 256 * 3;
358 for (i = 0; i < 256; i++) {
361 if (alpha && alpha != 0xff)
363 *alpha_ptr++ = alpha;
364 bytestream_put_be24(&ptr, v);
367 MKTAG(
'P',
'L',
'T',
'E'), s->
buf, 256 * 3);
370 MKTAG(
't',
'R',
'N',
'S'), s->
buf + 256 * 3, 256);
377 if (is_progressive) {
380 for (pass = 0; pass <
NB_PASSES; pass++) {
384 if (pass_row_size > 0) {
386 for (y = 0; y < avctx->
height; y++)
395 bits_per_pixel, pass,
398 top, pass_row_size, bits_per_pixel >> 3);
400 top = progressive_buf;
406 for (y = 0; y < avctx->
height; y++) {
414 row_size, bits_per_pixel >> 3);
421 ret = deflate(&s->
zstream, Z_FINISH);
422 if (ret == Z_OK || ret == Z_STREAM_END) {
429 if (ret == Z_STREAM_END)
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
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define FF_COMPRESSION_DEFAULT
This structure describes decoded (raw) audio or video data.
void(* diff_bytes)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, uint8_t *src, uint8_t *top, int size, int bpp)
packed RGB 8:8:8, 24bpp, RGBRGB...
AVFrame * coded_frame
the picture in the bitstream
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_PALETTE
av_cold void ff_huffyuvencdsp_init(HuffYUVEncDSPContext *c)
#define PNG_FILTER_VALUE_PAETH
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define PNG_COLOR_TYPE_RGB_ALPHA
8 bit with 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)
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
#define PNG_FILTER_VALUE_MIXED
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
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 ff_png_get_nb_channels(int color_type)
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
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
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
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.
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
static const float pred[4]
Libavcodec external API header.
#define PNG_FILTER_VALUE_UP
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
static void close(AVCodecParserContext *s)
const uint8_t ff_png_pass_mask[NB_PASSES]
const uint8_t ff_png_pass_ymask[NB_PASSES]
static av_cold int png_enc_init(AVCodecContext *avctx)
static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
#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)
const uint8_t ff_pngsig[8]
void ff_png_zfree(void *opaque, void *ptr)
static av_cold int init(AVCodecParserContext *s)
int key_frame
1 -> keyframe, 0-> not
static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
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.