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;
60 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
63 switch(bits_per_pixel) {
65 memset(dst, 0, row_size);
67 for(x = 0; x < width; x++) {
69 if ((mask << j) & 0x80) {
70 b = (src[x >> 3] >> (7 - j)) & 1;
71 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
77 bpp = bits_per_pixel >> 3;
80 for(x = 0; x < width; x++) {
82 if ((mask << j) & 0x80) {
92 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
95 for(i = 0; i < w; i++) {
96 int a, b, c, p, pa, pb, pc;
109 if (pa <= pb && pa <= pc)
119 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
120 uint8_t *src, uint8_t *top, int size, int bpp)
124 switch(filter_type) {
125 case PNG_FILTER_VALUE_NONE:
126 memcpy(dst, src, size);
128 case PNG_FILTER_VALUE_SUB:
129 dsp->diff_bytes(dst, src, src-bpp, size);
130 memcpy(dst, src, bpp);
132 case PNG_FILTER_VALUE_UP:
133 dsp->diff_bytes(dst, src, top, size);
135 case PNG_FILTER_VALUE_AVG:
136 for(i = 0; i < bpp; i++)
137 dst[i] = src[i] - (top[i] >> 1);
139 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
141 case PNG_FILTER_VALUE_PAETH:
142 for(i = 0; i < bpp; i++)
143 dst[i] = src[i] - top[i];
144 sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
149 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
150 uint8_t *src, uint8_t *top, int size, int bpp)
152 int pred = s->filter_type;
153 av_assert0(bpp || !pred);
155 pred = PNG_FILTER_VALUE_SUB;
156 if(pred == PNG_FILTER_VALUE_MIXED) {
158 int cost, bcost = INT_MAX;
159 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
160 for(pred=0; pred<5; pred++) {
161 png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
164 for(i=0; i<=size; i++)
165 cost += abs((int8_t)buf1[i]);
168 FFSWAP(uint8_t*, buf1, buf2);
173 png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
179 static void png_write_chunk(uint8_t **f, uint32_t tag,
180 const uint8_t *buf, int length)
185 bytestream_put_be32(f, length);
186 crc = crc32(0, Z_NULL, 0);
187 AV_WL32(tagbuf, tag);
188 crc = crc32(crc, tagbuf, 4);
189 bytestream_put_be32(f, av_bswap32(tag));
191 crc = crc32(crc, buf, length);
192 memcpy(*f, buf, length);
195 bytestream_put_be32(f, crc);
198 /* XXX: do filtering */
199 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
203 s->zstream.avail_in = size;
204 s->zstream.next_in = (uint8_t *)data;
205 while (s->zstream.avail_in > 0) {
206 ret = deflate(&s->zstream, Z_NO_FLUSH);
209 if (s->zstream.avail_out == 0) {
210 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
211 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
212 s->zstream.avail_out = IOBUF_SIZE;
213 s->zstream.next_out = s->buf;
219 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
220 const AVFrame *pict, int *got_packet)
222 PNGEncContext *s = avctx->priv_data;
223 AVFrame * const p= &s->picture;
224 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
225 int bits_per_pixel, pass_row_size, enc_row_size;
226 int64_t max_packet_size;
227 int compression_level;
229 uint8_t *crow_base = NULL, *crow_buf, *crow;
230 uint8_t *progressive_buf = NULL;
231 uint8_t *top_buf = NULL;
234 p->pict_type= AV_PICTURE_TYPE_I;
237 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
238 switch(avctx->pix_fmt) {
239 case AV_PIX_FMT_RGBA64BE:
241 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
243 case AV_PIX_FMT_RGB48BE:
245 color_type = PNG_COLOR_TYPE_RGB;
247 case AV_PIX_FMT_RGBA:
249 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
251 case AV_PIX_FMT_RGB24:
253 color_type = PNG_COLOR_TYPE_RGB;
255 case AV_PIX_FMT_GRAY16BE:
257 color_type = PNG_COLOR_TYPE_GRAY;
259 case AV_PIX_FMT_GRAY8:
261 color_type = PNG_COLOR_TYPE_GRAY;
263 case AV_PIX_FMT_GRAY8A:
265 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
267 case AV_PIX_FMT_MONOBLACK:
269 color_type = PNG_COLOR_TYPE_GRAY;
271 case AV_PIX_FMT_PAL8:
273 color_type = PNG_COLOR_TYPE_PALETTE;
278 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
279 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
281 s->zstream.zalloc = ff_png_zalloc;
282 s->zstream.zfree = ff_png_zfree;
283 s->zstream.opaque = NULL;
284 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
285 Z_DEFAULT_COMPRESSION :
286 av_clip(avctx->compression_level, 0, 9);
287 ret = deflateInit2(&s->zstream, compression_level,
288 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
292 enc_row_size = deflateBound(&s->zstream, row_size);
293 max_packet_size = avctx->height * (int64_t)(enc_row_size +
294 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
295 + FF_MIN_BUFFER_SIZE;
296 if (max_packet_size > INT_MAX)
297 return AVERROR(ENOMEM);
298 if ((ret = ff_alloc_packet2(avctx, pkt, max_packet_size)) < 0)
301 s->bytestream_start =
302 s->bytestream = pkt->data;
303 s->bytestream_end = pkt->data + pkt->size;
305 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
308 crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
309 if (is_progressive) {
310 progressive_buf = av_malloc(row_size + 1);
311 if (!progressive_buf)
314 if (is_progressive) {
315 top_buf = av_malloc(row_size + 1);
320 /* write png header */
321 AV_WB64(s->bytestream, PNGSIG);
324 AV_WB32(s->buf, avctx->width);
325 AV_WB32(s->buf + 4, avctx->height);
326 s->buf[8] = bit_depth;
327 s->buf[9] = color_type;
328 s->buf[10] = 0; /* compression type */
329 s->buf[11] = 0; /* filter type */
330 s->buf[12] = is_progressive; /* interlace type */
332 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
334 AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
335 AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
336 s->buf[8] = 0; /* unit specifier is unknown */
337 png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
339 /* put the palette if needed */
340 if (color_type == PNG_COLOR_TYPE_PALETTE) {
341 int has_alpha, alpha, i;
346 palette = (uint32_t *)p->data[1];
348 alpha_ptr = s->buf + 256 * 3;
350 for(i = 0; i < 256; i++) {
355 *alpha_ptr++ = alpha;
356 bytestream_put_be24(&ptr, v);
358 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
360 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
364 /* now put each row */
365 s->zstream.avail_out = IOBUF_SIZE;
366 s->zstream.next_out = s->buf;
367 if (is_progressive) {
370 for(pass = 0; pass < NB_PASSES; pass++) {
371 /* NOTE: a pass is completely omitted if no pixels would be
373 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
374 if (pass_row_size > 0) {
376 for(y = 0; y < avctx->height; y++) {
377 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
378 ptr = p->data[0] + y * p->linesize[0];
379 FFSWAP(uint8_t*, progressive_buf, top_buf);
380 png_get_interlaced_row(progressive_buf, pass_row_size,
381 bits_per_pixel, pass,
383 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
384 png_write_row(s, crow, pass_row_size + 1);
385 top = progressive_buf;
392 for(y = 0; y < avctx->height; y++) {
393 ptr = p->data[0] + y * p->linesize[0];
394 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
395 png_write_row(s, crow, row_size + 1);
399 /* compress last bytes */
401 ret = deflate(&s->zstream, Z_FINISH);
402 if (ret == Z_OK || ret == Z_STREAM_END) {
403 len = IOBUF_SIZE - s->zstream.avail_out;
404 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
405 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
407 s->zstream.avail_out = IOBUF_SIZE;
408 s->zstream.next_out = s->buf;
409 if (ret == Z_STREAM_END)
415 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
417 pkt->size = s->bytestream - s->bytestream_start;
418 pkt->flags |= AV_PKT_FLAG_KEY;
424 av_free(progressive_buf);
426 deflateEnd(&s->zstream);
433 static av_cold int png_enc_init(AVCodecContext *avctx){
434 PNGEncContext *s = avctx->priv_data;
436 switch(avctx->pix_fmt) {
437 case AV_PIX_FMT_RGBA:
438 avctx->bits_per_coded_sample = 32;
440 case AV_PIX_FMT_RGB24:
441 avctx->bits_per_coded_sample = 24;
443 case AV_PIX_FMT_GRAY8:
444 avctx->bits_per_coded_sample = 0x28;
446 case AV_PIX_FMT_MONOBLACK:
447 avctx->bits_per_coded_sample = 1;
449 case AV_PIX_FMT_PAL8:
450 avctx->bits_per_coded_sample = 8;
453 avcodec_get_frame_defaults(&s->picture);
454 avctx->coded_frame= &s->picture;
455 ff_dsputil_init(&s->dsp, avctx);
457 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
458 if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
459 s->filter_type = PNG_FILTER_VALUE_NONE;
464 AVCodec ff_png_encoder = {
466 .type = AVMEDIA_TYPE_VIDEO,
467 .id = AV_CODEC_ID_PNG,
468 .priv_data_size = sizeof(PNGEncContext),
469 .init = png_enc_init,
470 .encode2 = encode_frame,
471 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
472 .pix_fmts = (const enum AVPixelFormat[]){
473 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
474 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
476 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
478 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
480 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),