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
24 #include "bytestream.h"
25 #include "huffyuvencdsp.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/libm.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/color_utils.h"
35 #define IOBUF_SIZE 4096
37 typedef struct PNGEncContext {
39 HuffYUVEncDSPContext hdsp;
42 uint8_t *bytestream_start;
43 uint8_t *bytestream_end;
48 uint8_t buf[IOBUF_SIZE];
49 int dpi; ///< Physical pixel density, in dots per inch, if set
50 int dpm; ///< Physical pixel density, in dots per meter, if set
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,
96 for (i = 0; i < w; i++) {
97 int a, b, c, p, pa, pb, pc;
110 if (pa <= pb && pa <= pc)
120 static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)
122 const uint8_t *src1 = src + bpp;
123 const uint8_t *src2 = src;
126 memcpy(dst, src, bpp);
129 unaligned_w = FFMIN(32 - bpp, size);
130 for (x = 0; x < unaligned_w; x++)
131 *dst++ = *src1++ - *src2++;
133 c->hdsp.diff_bytes(dst, src1, src2, size);
136 static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
137 uint8_t *src, uint8_t *top, int size, int bpp)
141 switch (filter_type) {
142 case PNG_FILTER_VALUE_NONE:
143 memcpy(dst, src, size);
145 case PNG_FILTER_VALUE_SUB:
146 sub_left_prediction(c, dst, src, bpp, size);
148 case PNG_FILTER_VALUE_UP:
149 c->hdsp.diff_bytes(dst, src, top, size);
151 case PNG_FILTER_VALUE_AVG:
152 for (i = 0; i < bpp; i++)
153 dst[i] = src[i] - (top[i] >> 1);
154 for (; i < size; i++)
155 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
157 case PNG_FILTER_VALUE_PAETH:
158 for (i = 0; i < bpp; i++)
159 dst[i] = src[i] - top[i];
160 sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
165 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
166 uint8_t *src, uint8_t *top, int size, int bpp)
168 int pred = s->filter_type;
169 av_assert0(bpp || !pred);
171 pred = PNG_FILTER_VALUE_SUB;
172 if (pred == PNG_FILTER_VALUE_MIXED) {
174 int cost, bcost = INT_MAX;
175 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
176 for (pred = 0; pred < 5; pred++) {
177 png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
180 for (i = 0; i <= size; i++)
181 cost += abs((int8_t) buf1[i]);
184 FFSWAP(uint8_t *, buf1, buf2);
189 png_filter_row(s, dst + 1, pred, src, top, size, bpp);
195 static void png_write_chunk(uint8_t **f, uint32_t tag,
196 const uint8_t *buf, int length)
201 bytestream_put_be32(f, length);
202 crc = crc32(0, Z_NULL, 0);
203 AV_WL32(tagbuf, tag);
204 crc = crc32(crc, tagbuf, 4);
205 bytestream_put_be32(f, av_bswap32(tag));
207 crc = crc32(crc, buf, length);
208 memcpy(*f, buf, length);
211 bytestream_put_be32(f, crc);
214 /* XXX: do filtering */
215 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
219 s->zstream.avail_in = size;
220 s->zstream.next_in = data;
221 while (s->zstream.avail_in > 0) {
222 ret = deflate(&s->zstream, Z_NO_FLUSH);
225 if (s->zstream.avail_out == 0) {
226 if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
227 png_write_chunk(&s->bytestream,
228 MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
229 s->zstream.avail_out = IOBUF_SIZE;
230 s->zstream.next_out = s->buf;
236 #define AV_WB32_PNG(buf, n) AV_WB32(buf, lrint((n) * 100000))
237 static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
239 double rx, ry, gx, gy, bx, by, wx = 0.3127, wy = 0.3290;
241 case AVCOL_PRI_BT709:
242 rx = 0.640; ry = 0.330;
243 gx = 0.300; gy = 0.600;
244 bx = 0.150; by = 0.060;
246 case AVCOL_PRI_BT470M:
247 rx = 0.670; ry = 0.330;
248 gx = 0.210; gy = 0.710;
249 bx = 0.140; by = 0.080;
250 wx = 0.310; wy = 0.316;
252 case AVCOL_PRI_BT470BG:
253 rx = 0.640; ry = 0.330;
254 gx = 0.290; gy = 0.600;
255 bx = 0.150; by = 0.060;
257 case AVCOL_PRI_SMPTE170M:
258 case AVCOL_PRI_SMPTE240M:
259 rx = 0.630; ry = 0.340;
260 gx = 0.310; gy = 0.595;
261 bx = 0.155; by = 0.070;
263 case AVCOL_PRI_BT2020:
264 rx = 0.708; ry = 0.292;
265 gx = 0.170; gy = 0.797;
266 bx = 0.131; by = 0.046;
272 AV_WB32_PNG(buf , wx); AV_WB32_PNG(buf + 4 , wy);
273 AV_WB32_PNG(buf + 8 , rx); AV_WB32_PNG(buf + 12, ry);
274 AV_WB32_PNG(buf + 16, gx); AV_WB32_PNG(buf + 20, gy);
275 AV_WB32_PNG(buf + 24, bx); AV_WB32_PNG(buf + 28, by);
279 static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
281 double gamma = avpriv_get_gamma_from_trc(trc);
285 AV_WB32_PNG(buf, 1.0 / gamma);
289 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
290 const AVFrame *pict, int *got_packet)
292 PNGEncContext *s = avctx->priv_data;
293 const AVFrame *const p = pict;
294 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
295 int bits_per_pixel, pass_row_size, enc_row_size;
296 int64_t max_packet_size;
297 int compression_level;
298 uint8_t *ptr, *top, *crow_buf, *crow;
299 uint8_t *crow_base = NULL;
300 uint8_t *progressive_buf = NULL;
301 uint8_t *top_buf = NULL;
303 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
304 switch (avctx->pix_fmt) {
305 case AV_PIX_FMT_RGBA64BE:
307 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
309 case AV_PIX_FMT_RGB48BE:
311 color_type = PNG_COLOR_TYPE_RGB;
313 case AV_PIX_FMT_RGBA:
315 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
317 case AV_PIX_FMT_RGB24:
319 color_type = PNG_COLOR_TYPE_RGB;
321 case AV_PIX_FMT_GRAY16BE:
323 color_type = PNG_COLOR_TYPE_GRAY;
325 case AV_PIX_FMT_GRAY8:
327 color_type = PNG_COLOR_TYPE_GRAY;
329 case AV_PIX_FMT_GRAY8A:
331 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
333 case AV_PIX_FMT_YA16BE:
335 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
337 case AV_PIX_FMT_MONOBLACK:
339 color_type = PNG_COLOR_TYPE_GRAY;
341 case AV_PIX_FMT_PAL8:
343 color_type = PNG_COLOR_TYPE_PALETTE;
348 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
349 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
351 s->zstream.zalloc = ff_png_zalloc;
352 s->zstream.zfree = ff_png_zfree;
353 s->zstream.opaque = NULL;
354 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
355 ? Z_DEFAULT_COMPRESSION
356 : av_clip(avctx->compression_level, 0, 9);
357 ret = deflateInit2(&s->zstream, compression_level,
358 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
362 enc_row_size = deflateBound(&s->zstream, row_size);
363 max_packet_size = avctx->height * (int64_t)(enc_row_size +
364 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
365 + FF_MIN_BUFFER_SIZE;
366 if (max_packet_size > INT_MAX)
367 return AVERROR(ENOMEM);
368 if ((ret = ff_alloc_packet2(avctx, pkt, max_packet_size)) < 0)
371 s->bytestream_start =
372 s->bytestream = pkt->data;
373 s->bytestream_end = pkt->data + pkt->size;
375 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
378 // pixel data should be aligned, but there's a control byte before it
379 crow_buf = crow_base + 15;
380 if (is_progressive) {
381 progressive_buf = av_malloc(row_size + 1);
382 if (!progressive_buf)
385 if (is_progressive) {
386 top_buf = av_malloc(row_size + 1);
391 /* write png header */
392 AV_WB64(s->bytestream, PNGSIG);
395 AV_WB32(s->buf, avctx->width);
396 AV_WB32(s->buf + 4, avctx->height);
397 s->buf[8] = bit_depth;
398 s->buf[9] = color_type;
399 s->buf[10] = 0; /* compression type */
400 s->buf[11] = 0; /* filter type */
401 s->buf[12] = is_progressive; /* interlace type */
403 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
406 AV_WB32(s->buf, s->dpm);
407 AV_WB32(s->buf + 4, s->dpm);
408 s->buf[8] = 1; /* unit specifier is meter */
410 AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
411 AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
412 s->buf[8] = 0; /* unit specifier is unknown */
414 png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
416 /* write colorspace information */
417 if (pict->color_primaries == AVCOL_PRI_BT709 &&
418 pict->color_trc == AVCOL_TRC_IEC61966_2_1) {
419 s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
420 png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
423 if (png_get_chrm(pict->color_primaries, s->buf))
424 png_write_chunk(&s->bytestream, MKTAG('c', 'H', 'R', 'M'), s->buf, 32);
425 if (png_get_gama(pict->color_trc, s->buf))
426 png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
428 /* put the palette if needed */
429 if (color_type == PNG_COLOR_TYPE_PALETTE) {
430 int has_alpha, alpha, i;
435 palette = (uint32_t *)p->data[1];
437 alpha_ptr = s->buf + 256 * 3;
439 for (i = 0; i < 256; i++) {
444 *alpha_ptr++ = alpha;
445 bytestream_put_be24(&ptr, v);
447 png_write_chunk(&s->bytestream,
448 MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
450 png_write_chunk(&s->bytestream,
451 MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
455 /* now put each row */
456 s->zstream.avail_out = IOBUF_SIZE;
457 s->zstream.next_out = s->buf;
458 if (is_progressive) {
461 for (pass = 0; pass < NB_PASSES; pass++) {
462 /* NOTE: a pass is completely omitted if no pixels would be
464 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
465 if (pass_row_size > 0) {
467 for (y = 0; y < avctx->height; y++)
468 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
469 ptr = p->data[0] + y * p->linesize[0];
470 FFSWAP(uint8_t *, progressive_buf, top_buf);
471 png_get_interlaced_row(progressive_buf, pass_row_size,
472 bits_per_pixel, pass,
474 crow = png_choose_filter(s, crow_buf, progressive_buf,
475 top, pass_row_size, bits_per_pixel >> 3);
476 png_write_row(s, crow, pass_row_size + 1);
477 top = progressive_buf;
483 for (y = 0; y < avctx->height; y++) {
484 ptr = p->data[0] + y * p->linesize[0];
485 crow = png_choose_filter(s, crow_buf, ptr, top,
486 row_size, bits_per_pixel >> 3);
487 png_write_row(s, crow, row_size + 1);
491 /* compress last bytes */
493 ret = deflate(&s->zstream, Z_FINISH);
494 if (ret == Z_OK || ret == Z_STREAM_END) {
495 len = IOBUF_SIZE - s->zstream.avail_out;
496 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
497 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
499 s->zstream.avail_out = IOBUF_SIZE;
500 s->zstream.next_out = s->buf;
501 if (ret == Z_STREAM_END)
507 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
509 pkt->size = s->bytestream - s->bytestream_start;
510 pkt->flags |= AV_PKT_FLAG_KEY;
516 av_free(progressive_buf);
518 deflateEnd(&s->zstream);
525 static av_cold int png_enc_init(AVCodecContext *avctx)
527 PNGEncContext *s = avctx->priv_data;
529 switch (avctx->pix_fmt) {
530 case AV_PIX_FMT_RGBA:
531 avctx->bits_per_coded_sample = 32;
533 case AV_PIX_FMT_RGB24:
534 avctx->bits_per_coded_sample = 24;
536 case AV_PIX_FMT_GRAY8:
537 avctx->bits_per_coded_sample = 0x28;
539 case AV_PIX_FMT_MONOBLACK:
540 avctx->bits_per_coded_sample = 1;
542 case AV_PIX_FMT_PAL8:
543 avctx->bits_per_coded_sample = 8;
546 avctx->coded_frame = av_frame_alloc();
547 if (!avctx->coded_frame)
548 return AVERROR(ENOMEM);
550 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
551 avctx->coded_frame->key_frame = 1;
553 ff_huffyuvencdsp_init(&s->hdsp);
555 s->filter_type = av_clip(avctx->prediction_method,
556 PNG_FILTER_VALUE_NONE,
557 PNG_FILTER_VALUE_MIXED);
558 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
559 s->filter_type = PNG_FILTER_VALUE_NONE;
561 if (s->dpi && s->dpm) {
562 av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n");
563 return AVERROR(EINVAL);
565 s->dpm = s->dpi * 10000 / 254;
571 static av_cold int png_enc_close(AVCodecContext *avctx)
573 av_frame_free(&avctx->coded_frame);
577 #define OFFSET(x) offsetof(PNGEncContext, x)
578 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
579 static const AVOption options[] = {
580 {"dpi", "Set image resolution (in dots per inch)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
581 {"dpm", "Set image resolution (in dots per meter)", OFFSET(dpm), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
585 static const AVClass pngenc_class = {
586 .class_name = "PNG encoder",
587 .item_name = av_default_item_name,
589 .version = LIBAVUTIL_VERSION_INT,
592 AVCodec ff_png_encoder = {
594 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
595 .type = AVMEDIA_TYPE_VIDEO,
596 .id = AV_CODEC_ID_PNG,
597 .priv_data_size = sizeof(PNGEncContext),
598 .init = png_enc_init,
599 .close = png_enc_close,
600 .encode2 = encode_frame,
601 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
602 .pix_fmts = (const enum AVPixelFormat[]) {
603 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
604 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
606 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
607 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE,
608 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
610 .priv_class = &pngenc_class,