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
32 #define IOBUF_SIZE 4096
34 typedef struct PNGEncContext {
38 uint8_t *bytestream_start;
39 uint8_t *bytestream_end;
45 uint8_t buf[IOBUF_SIZE];
48 static void png_get_interlaced_row(uint8_t *dst, int row_size,
49 int bits_per_pixel, int pass,
50 const uint8_t *src, int width)
52 int x, mask, dst_x, j, b, bpp;
56 mask = ff_png_pass_mask[pass];
57 switch(bits_per_pixel) {
59 memset(dst, 0, row_size);
61 for(x = 0; x < width; x++) {
63 if ((mask << j) & 0x80) {
64 b = (src[x >> 3] >> (7 - j)) & 1;
65 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
71 bpp = bits_per_pixel >> 3;
74 for(x = 0; x < width; x++) {
76 if ((mask << j) & 0x80) {
86 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
89 for(i = 0; i < w; i++) {
90 int a, b, c, p, pa, pb, pc;
103 if (pa <= pb && pa <= pc)
113 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
114 uint8_t *src, uint8_t *top, int size, int bpp)
118 switch(filter_type) {
119 case PNG_FILTER_VALUE_NONE:
120 memcpy(dst, src, size);
122 case PNG_FILTER_VALUE_SUB:
123 dsp->diff_bytes(dst, src, src-bpp, size);
124 memcpy(dst, src, bpp);
126 case PNG_FILTER_VALUE_UP:
127 dsp->diff_bytes(dst, src, top, size);
129 case PNG_FILTER_VALUE_AVG:
130 for(i = 0; i < bpp; i++)
131 dst[i] = src[i] - (top[i] >> 1);
133 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
135 case PNG_FILTER_VALUE_PAETH:
136 for(i = 0; i < bpp; i++)
137 dst[i] = src[i] - top[i];
138 sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
143 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
144 uint8_t *src, uint8_t *top, int size, int bpp)
146 int pred = s->filter_type;
147 assert(bpp || !pred);
149 pred = PNG_FILTER_VALUE_SUB;
150 if(pred == PNG_FILTER_VALUE_MIXED) {
152 int cost, bcost = INT_MAX;
153 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
154 for(pred=0; pred<5; pred++) {
155 png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
158 for(i=0; i<=size; i++)
159 cost += abs((int8_t)buf1[i]);
162 FFSWAP(uint8_t*, buf1, buf2);
167 png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
173 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
180 for(j = 0; j < width; j++) {
181 v = ((const uint32_t *)src)[j];
190 static void png_write_chunk(uint8_t **f, uint32_t tag,
191 const uint8_t *buf, int length)
196 bytestream_put_be32(f, length);
197 crc = crc32(0, Z_NULL, 0);
198 AV_WL32(tagbuf, tag);
199 crc = crc32(crc, tagbuf, 4);
200 bytestream_put_be32(f, av_bswap32(tag));
202 crc = crc32(crc, buf, length);
203 memcpy(*f, buf, length);
206 bytestream_put_be32(f, crc);
209 /* XXX: do filtering */
210 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
214 s->zstream.avail_in = size;
215 s->zstream.next_in = (uint8_t *)data;
216 while (s->zstream.avail_in > 0) {
217 ret = deflate(&s->zstream, Z_NO_FLUSH);
220 if (s->zstream.avail_out == 0) {
221 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
222 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
223 s->zstream.avail_out = IOBUF_SIZE;
224 s->zstream.next_out = s->buf;
230 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
231 const AVFrame *pict, int *got_packet)
233 PNGEncContext *s = avctx->priv_data;
234 AVFrame * const p= &s->picture;
235 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
236 int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
237 int compression_level;
239 uint8_t *crow_base = NULL, *crow_buf, *crow;
240 uint8_t *progressive_buf = NULL;
241 uint8_t *rgba_buf = NULL;
242 uint8_t *top_buf = NULL;
245 p->pict_type= AV_PICTURE_TYPE_I;
248 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
249 switch(avctx->pix_fmt) {
250 case AV_PIX_FMT_RGB32:
252 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
254 case AV_PIX_FMT_RGB24:
256 color_type = PNG_COLOR_TYPE_RGB;
258 case AV_PIX_FMT_GRAY8:
260 color_type = PNG_COLOR_TYPE_GRAY;
262 case AV_PIX_FMT_MONOBLACK:
264 color_type = PNG_COLOR_TYPE_GRAY;
266 case AV_PIX_FMT_PAL8:
268 color_type = PNG_COLOR_TYPE_PALETTE;
273 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
274 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
276 s->zstream.zalloc = ff_png_zalloc;
277 s->zstream.zfree = ff_png_zfree;
278 s->zstream.opaque = NULL;
279 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
280 Z_DEFAULT_COMPRESSION :
281 av_clip(avctx->compression_level, 0, 9);
282 ret = deflateInit2(&s->zstream, compression_level,
283 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
287 enc_row_size = deflateBound(&s->zstream, row_size);
288 max_packet_size = avctx->height * (enc_row_size +
289 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
290 + FF_MIN_BUFFER_SIZE;
292 (ret = av_new_packet(pkt, max_packet_size)) < 0) {
293 av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
298 s->bytestream_start =
299 s->bytestream = pkt->data;
300 s->bytestream_end = pkt->data + pkt->size;
302 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
305 crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
306 if (is_progressive) {
307 progressive_buf = av_malloc(row_size + 1);
308 if (!progressive_buf)
311 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
312 rgba_buf = av_malloc(row_size + 1);
316 if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
317 top_buf = av_malloc(row_size + 1);
322 /* write png header */
323 memcpy(s->bytestream, ff_pngsig, 8);
326 AV_WB32(s->buf, avctx->width);
327 AV_WB32(s->buf + 4, avctx->height);
328 s->buf[8] = bit_depth;
329 s->buf[9] = color_type;
330 s->buf[10] = 0; /* compression type */
331 s->buf[11] = 0; /* filter type */
332 s->buf[12] = is_progressive; /* interlace type */
334 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
336 /* put the palette if needed */
337 if (color_type == PNG_COLOR_TYPE_PALETTE) {
338 int has_alpha, alpha, i;
343 palette = (uint32_t *)p->data[1];
345 alpha_ptr = s->buf + 256 * 3;
347 for(i = 0; i < 256; i++) {
350 if (alpha && alpha != 0xff)
352 *alpha_ptr++ = alpha;
353 bytestream_put_be24(&ptr, v);
355 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
357 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
361 /* now put each row */
362 s->zstream.avail_out = IOBUF_SIZE;
363 s->zstream.next_out = s->buf;
364 if (is_progressive) {
367 for(pass = 0; pass < NB_PASSES; pass++) {
368 /* NOTE: a pass is completely omitted if no pixels would be
370 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
371 if (pass_row_size > 0) {
373 for(y = 0; y < avctx->height; y++) {
374 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
375 ptr = p->data[0] + y * p->linesize[0];
376 FFSWAP(uint8_t*, progressive_buf, top_buf);
377 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
378 convert_from_rgb32(rgba_buf, ptr, avctx->width);
381 png_get_interlaced_row(progressive_buf, pass_row_size,
382 bits_per_pixel, pass,
384 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
385 png_write_row(s, crow, pass_row_size + 1);
386 top = progressive_buf;
393 for(y = 0; y < avctx->height; y++) {
394 ptr = p->data[0] + y * p->linesize[0];
395 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
396 FFSWAP(uint8_t*, rgba_buf, top_buf);
397 convert_from_rgb32(rgba_buf, ptr, avctx->width);
400 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
401 png_write_row(s, crow, row_size + 1);
405 /* compress last bytes */
407 ret = deflate(&s->zstream, Z_FINISH);
408 if (ret == Z_OK || ret == Z_STREAM_END) {
409 len = IOBUF_SIZE - s->zstream.avail_out;
410 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
411 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
413 s->zstream.avail_out = IOBUF_SIZE;
414 s->zstream.next_out = s->buf;
415 if (ret == Z_STREAM_END)
421 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
423 pkt->size = s->bytestream - s->bytestream_start;
424 pkt->flags |= AV_PKT_FLAG_KEY;
430 av_free(progressive_buf);
433 deflateEnd(&s->zstream);
440 static av_cold int png_enc_init(AVCodecContext *avctx){
441 PNGEncContext *s = avctx->priv_data;
443 avcodec_get_frame_defaults(&s->picture);
444 avctx->coded_frame= &s->picture;
445 ff_dsputil_init(&s->dsp, avctx);
447 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
448 if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
449 s->filter_type = PNG_FILTER_VALUE_NONE;
454 AVCodec ff_png_encoder = {
456 .type = AVMEDIA_TYPE_VIDEO,
457 .id = AV_CODEC_ID_PNG,
458 .priv_data_size = sizeof(PNGEncContext),
459 .init = png_enc_init,
460 .encode2 = encode_frame,
461 .pix_fmts = (const enum AVPixelFormat[]){
462 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
463 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
465 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),