34 #define IOBUF_SIZE 4096
51 int bits_per_pixel,
int pass,
52 const uint8_t *src,
int width)
54 int x,
mask, dst_x, j,
b, bpp;
59 switch(bits_per_pixel) {
61 memset(dst, 0, row_size);
63 for(x = 0; x <
width; x++) {
65 if ((mask << j) & 0x80) {
66 b = (src[x >> 3] >> (7 - j)) & 1;
67 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
73 bpp = bits_per_pixel >> 3;
76 for(x = 0; x <
width; x++) {
78 if ((mask << j) & 0x80) {
91 for(i = 0; i < w; i++) {
92 int a,
b, c, p, pa, pb, pc;
105 if (pa <= pb && pa <= pc)
116 uint8_t *src, uint8_t *top,
int size,
int bpp)
120 switch(filter_type) {
122 memcpy(dst, src, size);
126 memcpy(dst, src, bpp);
132 for(i = 0; i < bpp; i++)
133 dst[i] = src[i] - (top[i] >> 1);
135 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
138 for(i = 0; i < bpp; i++)
139 dst[i] = src[i] - top[i];
146 uint8_t *src, uint8_t *top,
int size,
int bpp)
149 assert(bpp || !pred);
154 int cost, bcost = INT_MAX;
155 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
156 for(pred=0; pred<5; pred++) {
160 for(i=0; i<=
size; i++)
161 cost += abs((int8_t)buf1[i]);
164 FFSWAP(uint8_t*, buf1, buf2);
182 for(j = 0; j <
width; j++) {
183 v = ((
const uint32_t *)src)[j];
193 const uint8_t *buf,
int length)
198 bytestream_put_be32(f, length);
199 crc = crc32(0, Z_NULL, 0);
201 crc = crc32(crc, tagbuf, 4);
204 crc = crc32(crc, buf, length);
205 memcpy(*f, buf, length);
208 bytestream_put_be32(f, crc);
217 s->
zstream.next_in = (uint8_t *)data;
218 while (s->
zstream.avail_in > 0) {
219 ret = deflate(&s->
zstream, Z_NO_FLUSH);
222 if (s->
zstream.avail_out == 0) {
236 int bit_depth, color_type, y,
len, row_size, ret, is_progressive;
237 int bits_per_pixel, pass_row_size;
238 int compression_level;
240 uint8_t *crow_base =
NULL, *crow_buf, *crow;
241 uint8_t *progressive_buf =
NULL;
242 uint8_t *rgba_buf =
NULL;
243 uint8_t *top_buf =
NULL;
279 row_size = (avctx->
width * bits_per_pixel + 7) >> 3;
285 Z_DEFAULT_COMPRESSION :
287 ret = deflateInit2(&s->
zstream, compression_level,
288 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
294 crow_buf = crow_base + 15;
295 if (is_progressive) {
296 progressive_buf =
av_malloc(row_size + 1);
297 if (!progressive_buf)
317 s->
buf[8] = bit_depth;
318 s->
buf[9] = color_type;
321 s->
buf[12] = is_progressive;
327 int has_alpha, alpha, i;
332 palette = (uint32_t *)p->
data[1];
334 alpha_ptr = s->
buf + 256 * 3;
336 for(i = 0; i < 256; i++) {
339 if (alpha && alpha != 0xff)
341 *alpha_ptr++ = alpha;
342 bytestream_put_be24(&ptr, v);
353 if (is_progressive) {
356 for(pass = 0; pass <
NB_PASSES; pass++) {
360 if (pass_row_size > 0) {
362 for(y = 0; y < avctx->
height; y++) {
365 FFSWAP(uint8_t*, progressive_buf, top_buf);
371 bits_per_pixel, pass,
373 crow =
png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
375 top = progressive_buf;
382 for(y = 0; y < avctx->
height; y++) {
385 FFSWAP(uint8_t*, rgba_buf, top_buf);
396 ret = deflate(&s->
zstream, Z_FINISH);
397 if (ret == Z_OK || ret == Z_STREAM_END) {
404 if (ret == Z_STREAM_END)
#define PNG_FILTER_VALUE_AVG
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
#define FF_COMPRESSION_DEFAULT
struct PNGEncContext PNGEncContext
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, uint8_t *src, uint8_t *top, int size, int bpp)
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
AVFrame * coded_frame
the picture in the bitstream
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_PALETTE
#define PNG_FILTER_VALUE_PAETH
#define PNG_COLOR_TYPE_RGB_ALPHA
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
#define PNG_FILTER_VALUE_MIXED
static int init(AVCodecParserContext *s)
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]
#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)
const char * name
Name of the codec implementation.
uint8_t * bytestream_start
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
#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 float pred[4]
#define PNG_FILTER_VALUE_UP
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
const uint8_t ff_png_pass_mask[NB_PASSES]
const uint8_t ff_png_pass_ymask[NB_PASSES]
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static av_cold int png_enc_init(AVCodecContext *avctx)
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
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.
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)
8 bit with PIX_FMT_RGB32 palette
const uint8_t ff_pngsig[8]
void ff_png_zfree(void *opaque, void *ptr)
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *top, int size, int bpp)
packed RGB 8:8:8, 24bpp, RGBRGB...
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)
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
#define FFSWAP(type, a, b)
#define MKTAG(a, b, c, d)
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)