33 #define IOBUF_SIZE 4096
52 int bits_per_pixel,
int pass,
55 int x,
mask, dst_x, j,
b, bpp;
58 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
61 switch (bits_per_pixel) {
63 memset(dst, 0, row_size);
65 for (x = 0; x <
width; x++) {
67 if ((mask << j) & 0x80) {
68 b = (src[x >> 3] >> (7 - j)) & 1;
69 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
75 bpp = bits_per_pixel >> 3;
78 for (x = 0; x <
width; x++) {
80 if ((mask << j) & 0x80) {
94 for (i = 0; i < w; i++) {
95 int a,
b,
c, p, pa, pb, pc;
108 if (pa <= pb && pa <= pc)
120 const uint8_t *src1 = src + bpp;
124 memcpy(dst, src, bpp);
127 unaligned_w =
FFMIN(32 - bpp, size);
128 for (x = 0; x < unaligned_w; x++)
129 *dst++ = *src1++ - *src2++;
139 switch (filter_type) {
141 memcpy(dst, src, size);
150 for (i = 0; i < bpp; i++)
151 dst[i] = src[i] - (top[i] >> 1);
152 for (; i <
size; i++)
153 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
156 for (i = 0; i < bpp; i++)
157 dst[i] = src[i] - top[i];
172 int cost, bcost = INT_MAX;
173 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
174 for (pred = 0; pred < 5; pred++) {
178 for (i = 0; i <=
size; i++)
179 cost += abs((int8_t) buf1[i]);
199 bytestream_put_be32(f, length);
200 crc =
crc32(0, Z_NULL, 0);
202 crc =
crc32(crc, tagbuf, 4);
205 crc =
crc32(crc, buf, length);
206 memcpy(*f, buf, length);
209 bytestream_put_be32(f, crc);
219 while (s->
zstream.avail_in > 0) {
220 ret = deflate(&s->
zstream, Z_NO_FLUSH);
223 if (s->
zstream.avail_out == 0) {
235 const AVFrame *pict,
int *got_packet)
240 int bits_per_pixel, pass_row_size, enc_row_size;
241 int64_t max_packet_size;
242 int compression_level;
243 uint8_t *ptr, *top, *crow_buf, *crow;
245 uint8_t *progressive_buf = NULL;
290 row_size = (avctx->
width * bits_per_pixel + 7) >> 3;
296 ? Z_DEFAULT_COMPRESSION
298 ret = deflateInit2(&s->
zstream, compression_level,
299 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
303 enc_row_size = deflateBound(&s->
zstream, row_size);
304 max_packet_size = avctx->
height * (int64_t)(enc_row_size +
307 if (max_packet_size > INT_MAX)
320 crow_buf = crow_base + 15;
321 if (is_progressive) {
322 progressive_buf =
av_malloc(row_size + 1);
323 if (!progressive_buf)
326 if (is_progressive) {
338 s->
buf[8] = bit_depth;
339 s->
buf[9] = color_type;
342 s->
buf[12] = is_progressive;
359 int has_alpha,
alpha, i;
364 palette = (uint32_t *)p->
data[1];
366 alpha_ptr = s->
buf + 256 * 3;
368 for (i = 0; i < 256; i++) {
373 *alpha_ptr++ =
alpha;
374 bytestream_put_be24(&ptr, v);
377 MKTAG(
'P',
'L',
'T',
'E'), s->
buf, 256 * 3);
380 MKTAG(
't',
'R',
'N',
'S'), s->
buf + 256 * 3, 256);
387 if (is_progressive) {
390 for (pass = 0; pass <
NB_PASSES; pass++) {
394 if (pass_row_size > 0) {
396 for (y = 0; y < avctx->
height; y++)
401 bits_per_pixel, pass,
404 top, pass_row_size, bits_per_pixel >> 3);
406 top = progressive_buf;
412 for (y = 0; y < avctx->
height; y++) {
415 row_size, bits_per_pixel >> 3);
422 ret = deflate(&s->
zstream, Z_FINISH);
423 if (ret == Z_OK || ret == Z_STREAM_END) {
430 if (ret == Z_STREAM_END)
494 s->
dpm = s->
dpi * 10000 / 254;
506 #define OFFSET(x) offsetof(PNGEncContext, x)
507 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM