3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bytestream.h"
26 * - add 2, 4 and 16 bit depth support
27 * - use filters when generating a png (better compression)
34 #define IOBUF_SIZE 4096
36 typedef struct PNGEncContext
{
38 uint8_t *bytestream_start
;
39 uint8_t *bytestream_end
;
43 uint8_t buf
[IOBUF_SIZE
];
46 static void png_get_interlaced_row(uint8_t *dst
, int row_size
,
47 int bits_per_pixel
, int pass
,
48 const uint8_t *src
, int width
)
50 int x
, mask
, dst_x
, j
, b
, bpp
;
54 mask
= ff_png_pass_mask
[pass
];
55 switch(bits_per_pixel
) {
57 memset(dst
, 0, row_size
);
59 for(x
= 0; x
< width
; x
++) {
61 if ((mask
<< j
) & 0x80) {
62 b
= (src
[x
>> 3] >> (7 - j
)) & 1;
63 dst
[dst_x
>> 3] |= b
<< (7 - (dst_x
& 7));
69 bpp
= bits_per_pixel
>> 3;
72 for(x
= 0; x
< width
; x
++) {
74 if ((mask
<< j
) & 0x80) {
84 static void convert_from_rgb32(uint8_t *dst
, const uint8_t *src
, int width
)
91 for(j
= 0; j
< width
; j
++) {
92 v
= ((const uint32_t *)src
)[j
];
101 static void png_write_chunk(uint8_t **f
, uint32_t tag
,
102 const uint8_t *buf
, int length
)
107 bytestream_put_be32(f
, length
);
108 crc
= crc32(0, Z_NULL
, 0);
109 AV_WL32(tagbuf
, tag
);
110 crc
= crc32(crc
, tagbuf
, 4);
111 bytestream_put_be32(f
, bswap_32(tag
));
113 crc
= crc32(crc
, buf
, length
);
114 memcpy(*f
, buf
, length
);
117 bytestream_put_be32(f
, crc
);
120 /* XXX: do filtering */
121 static int png_write_row(PNGEncContext
*s
, const uint8_t *data
, int size
)
125 s
->zstream
.avail_in
= size
;
126 s
->zstream
.next_in
= (uint8_t *)data
;
127 while (s
->zstream
.avail_in
> 0) {
128 ret
= deflate(&s
->zstream
, Z_NO_FLUSH
);
131 if (s
->zstream
.avail_out
== 0) {
132 if(s
->bytestream_end
- s
->bytestream
> IOBUF_SIZE
+ 100)
133 png_write_chunk(&s
->bytestream
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, IOBUF_SIZE
);
134 s
->zstream
.avail_out
= IOBUF_SIZE
;
135 s
->zstream
.next_out
= s
->buf
;
141 static int encode_frame(AVCodecContext
*avctx
, unsigned char *buf
, int buf_size
, void *data
){
142 PNGEncContext
*s
= avctx
->priv_data
;
143 AVFrame
*pict
= data
;
144 AVFrame
* const p
= (AVFrame
*)&s
->picture
;
145 int bit_depth
, color_type
, y
, len
, row_size
, ret
, is_progressive
;
146 int bits_per_pixel
, pass_row_size
;
147 int compression_level
;
149 uint8_t *crow_buf
= NULL
;
150 uint8_t *tmp_buf
= NULL
;
153 p
->pict_type
= FF_I_TYPE
;
158 s
->bytestream_end
= buf
+buf_size
;
160 is_progressive
= !!(avctx
->flags
& CODEC_FLAG_INTERLACED_DCT
);
161 switch(avctx
->pix_fmt
) {
164 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
168 color_type
= PNG_COLOR_TYPE_RGB
;
172 color_type
= PNG_COLOR_TYPE_GRAY
;
174 case PIX_FMT_MONOBLACK
:
176 color_type
= PNG_COLOR_TYPE_GRAY
;
180 color_type
= PNG_COLOR_TYPE_PALETTE
;
185 bits_per_pixel
= ff_png_get_nb_channels(color_type
) * bit_depth
;
186 row_size
= (avctx
->width
* bits_per_pixel
+ 7) >> 3;
188 s
->zstream
.zalloc
= ff_png_zalloc
;
189 s
->zstream
.zfree
= ff_png_zfree
;
190 s
->zstream
.opaque
= NULL
;
191 compression_level
= avctx
->compression_level
== FF_COMPRESSION_DEFAULT
?
192 Z_DEFAULT_COMPRESSION
:
193 av_clip(avctx
->compression_level
, 0, 9);
194 ret
= deflateInit2(&s
->zstream
, compression_level
,
195 Z_DEFLATED
, 15, 8, Z_DEFAULT_STRATEGY
);
198 crow_buf
= av_malloc(row_size
+ 1);
201 if (is_progressive
) {
202 tmp_buf
= av_malloc(row_size
+ 1);
207 /* write png header */
208 memcpy(s
->bytestream
, ff_pngsig
, 8);
211 AV_WB32(s
->buf
, avctx
->width
);
212 AV_WB32(s
->buf
+ 4, avctx
->height
);
213 s
->buf
[8] = bit_depth
;
214 s
->buf
[9] = color_type
;
215 s
->buf
[10] = 0; /* compression type */
216 s
->buf
[11] = 0; /* filter type */
217 s
->buf
[12] = is_progressive
; /* interlace type */
219 png_write_chunk(&s
->bytestream
, MKTAG('I', 'H', 'D', 'R'), s
->buf
, 13);
221 /* put the palette if needed */
222 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
223 int has_alpha
, alpha
, i
;
228 palette
= (uint32_t *)p
->data
[1];
230 alpha_ptr
= s
->buf
+ 256 * 3;
232 for(i
= 0; i
< 256; i
++) {
235 if (alpha
&& alpha
!= 0xff)
237 *alpha_ptr
++ = alpha
;
238 bytestream_put_be24(&ptr
, v
);
240 png_write_chunk(&s
->bytestream
, MKTAG('P', 'L', 'T', 'E'), s
->buf
, 256 * 3);
242 png_write_chunk(&s
->bytestream
, MKTAG('t', 'R', 'N', 'S'), s
->buf
+ 256 * 3, 256);
246 /* now put each row */
247 s
->zstream
.avail_out
= IOBUF_SIZE
;
248 s
->zstream
.next_out
= s
->buf
;
249 if (is_progressive
) {
253 for(pass
= 0; pass
< NB_PASSES
; pass
++) {
254 /* NOTE: a pass is completely omited if no pixels would be
256 pass_row_size
= ff_png_pass_row_size(pass
, bits_per_pixel
, avctx
->width
);
257 if (pass_row_size
> 0) {
258 for(y
= 0; y
< avctx
->height
; y
++) {
259 if ((ff_png_pass_ymask
[pass
] << (y
& 7)) & 0x80) {
260 ptr
= p
->data
[0] + y
* p
->linesize
[0];
261 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) {
262 convert_from_rgb32(tmp_buf
, ptr
, avctx
->width
);
267 png_get_interlaced_row(crow_buf
+ 1, pass_row_size
,
268 bits_per_pixel
, pass
,
270 crow_buf
[0] = PNG_FILTER_VALUE_NONE
;
271 png_write_row(s
, crow_buf
, pass_row_size
+ 1);
277 for(y
= 0; y
< avctx
->height
; y
++) {
278 ptr
= p
->data
[0] + y
* p
->linesize
[0];
279 if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
280 convert_from_rgb32(crow_buf
+ 1, ptr
, avctx
->width
);
282 memcpy(crow_buf
+ 1, ptr
, row_size
);
283 crow_buf
[0] = PNG_FILTER_VALUE_NONE
;
284 png_write_row(s
, crow_buf
, row_size
+ 1);
287 /* compress last bytes */
289 ret
= deflate(&s
->zstream
, Z_FINISH
);
290 if (ret
== Z_OK
|| ret
== Z_STREAM_END
) {
291 len
= IOBUF_SIZE
- s
->zstream
.avail_out
;
292 if (len
> 0 && s
->bytestream_end
- s
->bytestream
> len
+ 100) {
293 png_write_chunk(&s
->bytestream
, MKTAG('I', 'D', 'A', 'T'), s
->buf
, len
);
295 s
->zstream
.avail_out
= IOBUF_SIZE
;
296 s
->zstream
.next_out
= s
->buf
;
297 if (ret
== Z_STREAM_END
)
303 png_write_chunk(&s
->bytestream
, MKTAG('I', 'E', 'N', 'D'), NULL
, 0);
305 ret
= s
->bytestream
- s
->bytestream_start
;
309 deflateEnd(&s
->zstream
);
316 static int png_enc_init(AVCodecContext
*avctx
){
317 PNGEncContext
*s
= avctx
->priv_data
;
319 avcodec_get_frame_defaults((AVFrame
*)&s
->picture
);
320 avctx
->coded_frame
= (AVFrame
*)&s
->picture
;
325 AVCodec png_encoder
= {
329 sizeof(PNGEncContext
),
333 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_RGB24
, PIX_FMT_RGB32
, PIX_FMT_PAL8
, PIX_FMT_GRAY8
, PIX_FMT_MONOBLACK
, -1},