32 #define IOBUF_SIZE 4096
48 int bits_per_pixel,
int pass,
51 int x,
mask, dst_x, j,
b, bpp;
56 switch(bits_per_pixel) {
58 memset(dst, 0, row_size);
60 for(x = 0; x <
width; x++) {
62 if ((mask << j) & 0x80) {
63 b = (src[x >> 3] >> (7 - j)) & 1;
64 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
70 bpp = bits_per_pixel >> 3;
73 for(x = 0; x <
width; x++) {
75 if ((mask << j) & 0x80) {
88 for(i = 0; i < w; i++) {
89 int a,
b, c, p, pa, pb, pc;
102 if (pa <= pb && pa <= pc)
117 switch(filter_type) {
119 memcpy(dst, src, size);
123 memcpy(dst, src, bpp);
129 for(i = 0; i < bpp; i++)
130 dst[i] = src[i] - (top[i] >> 1);
132 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
135 for(i = 0; i < bpp; i++)
136 dst[i] = src[i] - top[i];
146 assert(bpp || !pred);
151 int cost, bcost = INT_MAX;
152 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
153 for(pred=0; pred<5; pred++) {
157 for(i=0; i<=
size; i++)
158 cost += abs((int8_t)buf1[i]);
179 for(j = 0; j <
width; j++) {
180 v = ((
const uint32_t *)src)[j];
190 const uint8_t *buf,
int length)
195 bytestream_put_be32(f, length);
196 crc = crc32(0, Z_NULL, 0);
198 crc = crc32(crc, tagbuf, 4);
201 crc = crc32(crc, buf, length);
202 memcpy(*f, buf, length);
205 bytestream_put_be32(f, crc);
215 while (s->
zstream.avail_in > 0) {
216 ret = deflate(&s->
zstream, Z_NO_FLUSH);
219 if (s->
zstream.avail_out == 0) {
230 const AVFrame *pict,
int *got_packet)
233 const AVFrame *
const p = pict;
234 int bit_depth, color_type, y,
len, row_size, ret, is_progressive;
235 int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
236 int compression_level;
273 row_size = (avctx->
width * bits_per_pixel + 7) >> 3;
279 Z_DEFAULT_COMPRESSION :
281 ret = deflateInit2(&s->
zstream, compression_level,
282 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
286 enc_row_size = deflateBound(&s->
zstream, row_size);
287 max_packet_size = avctx->
height * (enc_row_size +
304 crow_buf = crow_base + 15;
305 if (is_progressive) {
306 progressive_buf =
av_malloc(row_size + 1);
307 if (!progressive_buf)
327 s->
buf[8] = bit_depth;
328 s->
buf[9] = color_type;
331 s->
buf[12] = is_progressive;
337 int has_alpha, alpha, i;
342 palette = (uint32_t *)p->
data[1];
344 alpha_ptr = s->
buf + 256 * 3;
346 for(i = 0; i < 256; i++) {
349 if (alpha && alpha != 0xff)
351 *alpha_ptr++ = alpha;
352 bytestream_put_be24(&ptr, v);
363 if (is_progressive) {
366 for(pass = 0; pass <
NB_PASSES; pass++) {
370 if (pass_row_size > 0) {
372 for(y = 0; y < avctx->
height; y++) {
381 bits_per_pixel, pass,
383 crow =
png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
385 top = progressive_buf;
392 for(y = 0; y < avctx->
height; y++) {
406 ret = deflate(&s->
zstream, Z_FINISH);
407 if (ret == Z_OK || ret == Z_STREAM_END) {
414 if (ret == Z_STREAM_END)
#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 ...
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
#define FF_COMPRESSION_DEFAULT
This structure describes decoded (raw) audio or video data.
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
#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
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
#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...
void(* diff_bytes)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
#define PNG_FILTER_VALUE_NONE
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)
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *top, int size, int bpp)
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.