3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bytestream.h"
27 * - add 2, 4 and 16 bit depth support
34 #define IOBUF_SIZE 4096
36 typedef struct PNGEncContext
{
40 uint8_t *bytestream_start
;
41 uint8_t *bytestream_end
;
47 uint8_t buf
[IOBUF_SIZE
];
50 static void png_get_interlaced_row(uint8_t *dst
, int row_size
,
51 int bits_per_pixel
, int pass
,
52 const uint8_t *src
, int width
)
54 int x
, mask
, dst_x
, j
, b
, bpp
;
58 mask
= ff_png_pass_mask
[pass
];
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) {
88 static void sub_png_paeth_prediction(uint8_t *dst
, uint8_t *src
, uint8_t *top
, int w
, int bpp
)
91 for(i
= 0; i
< w
; i
++) {
92 int a
, b
, c
, p
, pa
, pb
, pc
;
105 if (pa
<= pb
&& pa
<= pc
)
115 static void png_filter_row(DSPContext
*dsp
, uint8_t *dst
, int filter_type
,
116 uint8_t *src
, uint8_t *top
, int size
, int bpp
)
120 switch(filter_type
) {
121 case PNG_FILTER_VALUE_NONE
:
122 memcpy(dst
, src
, size
);
124 case PNG_FILTER_VALUE_SUB
:
125 dsp
->diff_bytes(dst
, src
, src
-bpp
, size
);
126 memcpy(dst
, src
, bpp
);
128 case PNG_FILTER_VALUE_UP
:
129 dsp
->diff_bytes(dst
, src
, top
, size
);
131 case PNG_FILTER_VALUE_AVG
:
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);
137 case PNG_FILTER_VALUE_PAETH
:
138 for(i
= 0; i
< bpp
; i
++)
139 dst
[i
] = src
[i
] - top
[i
];
140 sub_png_paeth_prediction(dst
+i
, src
+i
, top
+i
, size
-i
, bpp
);
145 static uint8_t *png_choose_filter(PNGEncContext
*s
, uint8_t *dst
,
146 uint8_t *src
, uint8_t *top
, int size
, int bpp
)
148 int pred
= s
->filter_type
;
149 assert(bpp
|| !pred
);
151 pred
= PNG_FILTER_VALUE_SUB
;
152 if(pred
== PNG_FILTER_VALUE_MIXED
) {
154 int cost
, bcost
= INT_MAX
;
155 uint8_t *buf1
= dst
, *buf2
= dst
+ size
+ 16;
156 for(pred
=0; pred
<5; pred
++) {
157 png_filter_row(&s
->dsp
, buf1
+1, pred
, src
, top
, size
, bpp
);
160 for(i
=0; i
<=size
; i
++)
161 cost
+= abs((int8_t)buf1
[i
]);
164 FFSWAP(uint8_t*, buf1
, buf2
);
169 png_filter_row(&s
->dsp
, dst
+1, pred
, src
, top
, size
, bpp
);
175 static void convert_from_rgb32(uint8_t *dst
, const uint8_t *src
, int width
)
182 for(j
= 0; j
< width
; j
++) {
183 v
= ((const uint32_t *)src
)[j
];
192 static void png_write_chunk(uint8_t **f
, uint32_t tag
,
193 const uint8_t *buf
, int length
)
198 bytestream_put_be32(f
, length
);
199 crc
= crc32(0, Z_NULL
, 0);
200 AV_WL32(tagbuf
, tag
);
201 crc
= crc32(crc
, tagbuf
, 4);
202 bytestream_put_be32(f
, av_bswap32(tag
));
204 crc
= crc32(crc
, buf
, length
);
205 memcpy(*f
, buf
, length
);
208 bytestream_put_be32(f
, crc
);
211 /* XXX: do filtering */
212 static int png_write_row(PNGEncContext
*s
, const uint8_t *data
, int size
)
216 s
->zstream
.avail_in
= size
;
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) {
223 if(s
->bytestream_end
- s
->bytestream
> IOBUF_SIZE
+ 100)
224 png_write_chunk(&s
->bytestream
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, IOBUF_SIZE
);
225 s
->zstream
.avail_out
= IOBUF_SIZE
;
226 s
->zstream
.next_out
= s
->buf
;
232 static int encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
233 const AVFrame
*pict
, int *got_packet
)
235 PNGEncContext
*s
= avctx
->priv_data
;
236 AVFrame
* const p
= &s
->picture
;
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
;
241 uint8_t *crow_base
= NULL
, *crow_buf
, *crow
;
242 uint8_t *progressive_buf
= NULL
;
243 uint8_t *rgba_buf
= NULL
;
244 uint8_t *top_buf
= NULL
;
247 p
->pict_type
= AV_PICTURE_TYPE_I
;
250 is_progressive
= !!(avctx
->flags
& CODEC_FLAG_INTERLACED_DCT
);
251 switch(avctx
->pix_fmt
) {
252 case AV_PIX_FMT_RGB32
:
254 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
256 case AV_PIX_FMT_RGB24
:
258 color_type
= PNG_COLOR_TYPE_RGB
;
260 case AV_PIX_FMT_GRAY8
:
262 color_type
= PNG_COLOR_TYPE_GRAY
;
264 case AV_PIX_FMT_MONOBLACK
:
266 color_type
= PNG_COLOR_TYPE_GRAY
;
268 case AV_PIX_FMT_PAL8
:
270 color_type
= PNG_COLOR_TYPE_PALETTE
;
275 bits_per_pixel
= ff_png_get_nb_channels(color_type
) * bit_depth
;
276 row_size
= (avctx
->width
* bits_per_pixel
+ 7) >> 3;
278 s
->zstream
.zalloc
= ff_png_zalloc
;
279 s
->zstream
.zfree
= ff_png_zfree
;
280 s
->zstream
.opaque
= NULL
;
281 compression_level
= avctx
->compression_level
== FF_COMPRESSION_DEFAULT
?
282 Z_DEFAULT_COMPRESSION
:
283 av_clip(avctx
->compression_level
, 0, 9);
284 ret
= deflateInit2(&s
->zstream
, compression_level
,
285 Z_DEFLATED
, 15, 8, Z_DEFAULT_STRATEGY
);
289 enc_row_size
= deflateBound(&s
->zstream
, row_size
);
290 max_packet_size
= avctx
->height
* (enc_row_size
+
291 ((enc_row_size
+ IOBUF_SIZE
- 1) / IOBUF_SIZE
) * 12)
292 + FF_MIN_BUFFER_SIZE
;
294 (ret
= av_new_packet(pkt
, max_packet_size
)) < 0) {
295 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate output packet of size %d.\n",
300 s
->bytestream_start
=
301 s
->bytestream
= pkt
->data
;
302 s
->bytestream_end
= pkt
->data
+ pkt
->size
;
304 crow_base
= av_malloc((row_size
+ 32) << (s
->filter_type
== PNG_FILTER_VALUE_MIXED
));
307 crow_buf
= crow_base
+ 15; // pixel data should be aligned, but there's a control byte before it
308 if (is_progressive
) {
309 progressive_buf
= av_malloc(row_size
+ 1);
310 if (!progressive_buf
)
313 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
314 rgba_buf
= av_malloc(row_size
+ 1);
318 if (is_progressive
|| color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
319 top_buf
= av_malloc(row_size
+ 1);
324 /* write png header */
325 memcpy(s
->bytestream
, ff_pngsig
, 8);
328 AV_WB32(s
->buf
, avctx
->width
);
329 AV_WB32(s
->buf
+ 4, avctx
->height
);
330 s
->buf
[8] = bit_depth
;
331 s
->buf
[9] = color_type
;
332 s
->buf
[10] = 0; /* compression type */
333 s
->buf
[11] = 0; /* filter type */
334 s
->buf
[12] = is_progressive
; /* interlace type */
336 png_write_chunk(&s
->bytestream
, MKTAG('I', 'H', 'D', 'R'), s
->buf
, 13);
338 /* put the palette if needed */
339 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
340 int has_alpha
, alpha
, i
;
345 palette
= (uint32_t *)p
->data
[1];
347 alpha_ptr
= s
->buf
+ 256 * 3;
349 for(i
= 0; i
< 256; i
++) {
352 if (alpha
&& alpha
!= 0xff)
354 *alpha_ptr
++ = alpha
;
355 bytestream_put_be24(&ptr
, v
);
357 png_write_chunk(&s
->bytestream
, MKTAG('P', 'L', 'T', 'E'), s
->buf
, 256 * 3);
359 png_write_chunk(&s
->bytestream
, MKTAG('t', 'R', 'N', 'S'), s
->buf
+ 256 * 3, 256);
363 /* now put each row */
364 s
->zstream
.avail_out
= IOBUF_SIZE
;
365 s
->zstream
.next_out
= s
->buf
;
366 if (is_progressive
) {
369 for(pass
= 0; pass
< NB_PASSES
; pass
++) {
370 /* NOTE: a pass is completely omitted if no pixels would be
372 pass_row_size
= ff_png_pass_row_size(pass
, bits_per_pixel
, avctx
->width
);
373 if (pass_row_size
> 0) {
375 for(y
= 0; y
< avctx
->height
; y
++) {
376 if ((ff_png_pass_ymask
[pass
] << (y
& 7)) & 0x80) {
377 ptr
= p
->data
[0] + y
* p
->linesize
[0];
378 FFSWAP(uint8_t*, progressive_buf
, top_buf
);
379 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
380 convert_from_rgb32(rgba_buf
, ptr
, avctx
->width
);
383 png_get_interlaced_row(progressive_buf
, pass_row_size
,
384 bits_per_pixel
, pass
,
386 crow
= png_choose_filter(s
, crow_buf
, progressive_buf
, top
, pass_row_size
, bits_per_pixel
>>3);
387 png_write_row(s
, crow
, pass_row_size
+ 1);
388 top
= progressive_buf
;
395 for(y
= 0; y
< avctx
->height
; y
++) {
396 ptr
= p
->data
[0] + y
* p
->linesize
[0];
397 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
398 FFSWAP(uint8_t*, rgba_buf
, top_buf
);
399 convert_from_rgb32(rgba_buf
, ptr
, avctx
->width
);
402 crow
= png_choose_filter(s
, crow_buf
, ptr
, top
, row_size
, bits_per_pixel
>>3);
403 png_write_row(s
, crow
, row_size
+ 1);
407 /* compress last bytes */
409 ret
= deflate(&s
->zstream
, Z_FINISH
);
410 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
411 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
412 if (len
> 0 && s
->bytestream_end
- s
->bytestream
> len
+ 100) {
413 png_write_chunk(&s
->bytestream
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, len
);
415 s
->zstream
.avail_out
= IOBUF_SIZE
;
416 s
->zstream
.next_out
= s
->buf
;
417 if (ret
== Z_STREAM_END
)
423 png_write_chunk(&s
->bytestream
, MKTAG('I', 'E', 'N', 'D'), NULL
, 0);
425 pkt
->size
= s
->bytestream
- s
->bytestream_start
;
426 pkt
->flags
|= AV_PKT_FLAG_KEY
;
432 av_free(progressive_buf
);
435 deflateEnd(&s
->zstream
);
442 static av_cold
int png_enc_init(AVCodecContext
*avctx
){
443 PNGEncContext
*s
= avctx
->priv_data
;
445 avcodec_get_frame_defaults(&s
->picture
);
446 avctx
->coded_frame
= &s
->picture
;
447 ff_dsputil_init(&s
->dsp
, avctx
);
449 s
->filter_type
= av_clip(avctx
->prediction_method
, PNG_FILTER_VALUE_NONE
, PNG_FILTER_VALUE_MIXED
);
450 if(avctx
->pix_fmt
== AV_PIX_FMT_MONOBLACK
)
451 s
->filter_type
= PNG_FILTER_VALUE_NONE
;
456 AVCodec ff_png_encoder
= {
458 .type
= AVMEDIA_TYPE_VIDEO
,
459 .id
= AV_CODEC_ID_PNG
,
460 .priv_data_size
= sizeof(PNGEncContext
),
461 .init
= png_enc_init
,
462 .encode2
= encode_frame
,
463 .pix_fmts
= (const enum AVPixelFormat
[]){
464 AV_PIX_FMT_RGB24
, AV_PIX_FMT_RGB32
, AV_PIX_FMT_PAL8
, AV_PIX_FMT_GRAY8
,
465 AV_PIX_FMT_MONOBLACK
, AV_PIX_FMT_NONE
467 .long_name
= NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),