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
23 #include "bytestream.h"
27 #include "libavutil/avassert.h"
30 * - add 2, 4 and 16 bit depth support
37 #define IOBUF_SIZE 4096
39 typedef struct PNGEncContext {
43 uint8_t *bytestream_start;
44 uint8_t *bytestream_end;
50 uint8_t buf[IOBUF_SIZE];
53 static void png_get_interlaced_row(uint8_t *dst, int row_size,
54 int bits_per_pixel, int pass,
55 const uint8_t *src, int width)
57 int x, mask, dst_x, j, b, bpp;
61 mask = (int[]){0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}[pass];
62 switch(bits_per_pixel) {
64 memset(dst, 0, row_size);
66 for(x = 0; x < width; x++) {
68 if ((mask << j) & 0x80) {
69 b = (src[x >> 3] >> (7 - j)) & 1;
70 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
76 bpp = bits_per_pixel >> 3;
79 for(x = 0; x < width; x++) {
81 if ((mask << j) & 0x80) {
91 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
94 for(i = 0; i < w; i++) {
95 int a, b, c, p, pa, pb, pc;
108 if (pa <= pb && pa <= pc)
118 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
119 uint8_t *src, uint8_t *top, int size, int bpp)
123 switch(filter_type) {
124 case PNG_FILTER_VALUE_NONE:
125 memcpy(dst, src, size);
127 case PNG_FILTER_VALUE_SUB:
128 dsp->diff_bytes(dst, src, src-bpp, size);
129 memcpy(dst, src, bpp);
131 case PNG_FILTER_VALUE_UP:
132 dsp->diff_bytes(dst, src, top, size);
134 case PNG_FILTER_VALUE_AVG:
135 for(i = 0; i < bpp; i++)
136 dst[i] = src[i] - (top[i] >> 1);
138 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
140 case PNG_FILTER_VALUE_PAETH:
141 for(i = 0; i < bpp; i++)
142 dst[i] = src[i] - top[i];
143 sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
148 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
149 uint8_t *src, uint8_t *top, int size, int bpp)
151 int pred = s->filter_type;
152 av_assert0(bpp || !pred);
154 pred = PNG_FILTER_VALUE_SUB;
155 if(pred == PNG_FILTER_VALUE_MIXED) {
157 int cost, bcost = INT_MAX;
158 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
159 for(pred=0; pred<5; pred++) {
160 png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
163 for(i=0; i<=size; i++)
164 cost += abs((int8_t)buf1[i]);
167 FFSWAP(uint8_t*, buf1, buf2);
172 png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
178 static void png_write_chunk(uint8_t **f, uint32_t tag,
179 const uint8_t *buf, int length)
184 bytestream_put_be32(f, length);
185 crc = crc32(0, Z_NULL, 0);
186 AV_WL32(tagbuf, tag);
187 crc = crc32(crc, tagbuf, 4);
188 bytestream_put_be32(f, av_bswap32(tag));
190 crc = crc32(crc, buf, length);
191 memcpy(*f, buf, length);
194 bytestream_put_be32(f, crc);
197 /* XXX: do filtering */
198 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
202 s->zstream.avail_in = size;
203 s->zstream.next_in = (uint8_t *)data;
204 while (s->zstream.avail_in > 0) {
205 ret = deflate(&s->zstream, Z_NO_FLUSH);
208 if (s->zstream.avail_out == 0) {
209 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
210 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
211 s->zstream.avail_out = IOBUF_SIZE;
212 s->zstream.next_out = s->buf;
218 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
219 const AVFrame *pict, int *got_packet)
221 PNGEncContext *s = avctx->priv_data;
222 AVFrame * const p= &s->picture;
223 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
224 int bits_per_pixel, pass_row_size, enc_row_size;
225 int64_t max_packet_size;
226 int compression_level;
228 uint8_t *crow_base = NULL, *crow_buf, *crow;
229 uint8_t *progressive_buf = NULL;
230 uint8_t *top_buf = NULL;
233 p->pict_type= AV_PICTURE_TYPE_I;
236 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
237 switch(avctx->pix_fmt) {
238 case AV_PIX_FMT_RGBA64BE:
240 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
242 case AV_PIX_FMT_RGB48BE:
244 color_type = PNG_COLOR_TYPE_RGB;
246 case AV_PIX_FMT_RGBA:
248 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
250 case AV_PIX_FMT_RGB24:
252 color_type = PNG_COLOR_TYPE_RGB;
254 case AV_PIX_FMT_GRAY16BE:
256 color_type = PNG_COLOR_TYPE_GRAY;
258 case AV_PIX_FMT_GRAY8:
260 color_type = PNG_COLOR_TYPE_GRAY;
262 case AV_PIX_FMT_GRAY8A:
264 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
266 case AV_PIX_FMT_MONOBLACK:
268 color_type = PNG_COLOR_TYPE_GRAY;
270 case AV_PIX_FMT_PAL8:
272 color_type = PNG_COLOR_TYPE_PALETTE;
277 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
278 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
280 s->zstream.zalloc = ff_png_zalloc;
281 s->zstream.zfree = ff_png_zfree;
282 s->zstream.opaque = NULL;
283 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
284 Z_DEFAULT_COMPRESSION :
285 av_clip(avctx->compression_level, 0, 9);
286 ret = deflateInit2(&s->zstream, compression_level,
287 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
291 enc_row_size = deflateBound(&s->zstream, row_size);
292 max_packet_size = avctx->height * (int64_t)(enc_row_size +
293 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
294 + FF_MIN_BUFFER_SIZE;
295 if (max_packet_size > INT_MAX)
296 return AVERROR(ENOMEM);
297 if ((ret = ff_alloc_packet2(avctx, pkt, max_packet_size)) < 0)
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 (is_progressive) {
314 top_buf = av_malloc(row_size + 1);
319 /* write png header */
320 AV_WB64(s->bytestream, PNGSIG);
323 AV_WB32(s->buf, avctx->width);
324 AV_WB32(s->buf + 4, avctx->height);
325 s->buf[8] = bit_depth;
326 s->buf[9] = color_type;
327 s->buf[10] = 0; /* compression type */
328 s->buf[11] = 0; /* filter type */
329 s->buf[12] = is_progressive; /* interlace type */
331 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
333 AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
334 AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
335 s->buf[8] = 0; /* unit specifier is unknown */
336 png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
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++) {
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 png_get_interlaced_row(progressive_buf, pass_row_size,
380 bits_per_pixel, pass,
382 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
383 png_write_row(s, crow, pass_row_size + 1);
384 top = progressive_buf;
391 for(y = 0; y < avctx->height; y++) {
392 ptr = p->data[0] + y * p->linesize[0];
393 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
394 png_write_row(s, crow, row_size + 1);
398 /* compress last bytes */
400 ret = deflate(&s->zstream, Z_FINISH);
401 if (ret == Z_OK || ret == Z_STREAM_END) {
402 len = IOBUF_SIZE - s->zstream.avail_out;
403 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
404 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
406 s->zstream.avail_out = IOBUF_SIZE;
407 s->zstream.next_out = s->buf;
408 if (ret == Z_STREAM_END)
414 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
416 pkt->size = s->bytestream - s->bytestream_start;
417 pkt->flags |= AV_PKT_FLAG_KEY;
423 av_free(progressive_buf);
425 deflateEnd(&s->zstream);
432 static av_cold int png_enc_init(AVCodecContext *avctx){
433 PNGEncContext *s = avctx->priv_data;
435 switch(avctx->pix_fmt) {
436 case AV_PIX_FMT_RGBA:
437 avctx->bits_per_coded_sample = 32;
439 case AV_PIX_FMT_RGB24:
440 avctx->bits_per_coded_sample = 24;
442 case AV_PIX_FMT_GRAY8:
443 avctx->bits_per_coded_sample = 0x28;
445 case AV_PIX_FMT_MONOBLACK:
446 avctx->bits_per_coded_sample = 1;
448 case AV_PIX_FMT_PAL8:
449 avctx->bits_per_coded_sample = 8;
452 avcodec_get_frame_defaults(&s->picture);
453 avctx->coded_frame= &s->picture;
454 ff_dsputil_init(&s->dsp, avctx);
456 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
457 if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
458 s->filter_type = PNG_FILTER_VALUE_NONE;
463 AVCodec ff_png_encoder = {
465 .type = AVMEDIA_TYPE_VIDEO,
466 .id = AV_CODEC_ID_PNG,
467 .priv_data_size = sizeof(PNGEncContext),
468 .init = png_enc_init,
469 .encode2 = encode_frame,
470 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
471 .pix_fmts = (const enum AVPixelFormat[]){
472 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
473 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
475 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
477 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
479 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),