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 "libavutil/opt.h"
23 #include "libavutil/stereo3d.h"
26 #include "bytestream.h"
27 #include "huffyuvencdsp.h"
31 * - add 2, 4 and 16 bit depth support
36 #define IOBUF_SIZE 4096
38 typedef struct PNGEncContext {
40 HuffYUVEncDSPContext hdsp;
43 uint8_t *bytestream_start;
44 uint8_t *bytestream_end;
49 uint8_t buf[IOBUF_SIZE];
52 static void png_get_interlaced_row(uint8_t *dst, int row_size,
53 int bits_per_pixel, int pass,
54 const uint8_t *src, int width)
56 int x, mask, dst_x, j, b, bpp;
60 mask = ff_png_pass_mask[pass];
61 switch (bits_per_pixel) {
63 memset(dst, 0, row_size);
65 for (x = 0; x < width; x++) {
67 if ((mask << j) & 0x80) {
68 b = (src[x >> 3] >> (7 - j)) & 1;
69 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
75 bpp = bits_per_pixel >> 3;
78 for (x = 0; x < width; x++) {
80 if ((mask << j) & 0x80) {
90 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
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(PNGEncContext *c, 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 c->hdsp.diff_bytes(dst, src, src - bpp, size);
129 memcpy(dst, src, bpp);
131 case PNG_FILTER_VALUE_UP:
132 c->hdsp.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);
137 for (; i < size; i++)
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 assert(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, 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, dst + 1, pred, src, top, size, bpp);
178 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
185 for (j = 0; j < width; j++) {
186 v = ((const uint32_t *) src)[j];
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 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
237 const AVFrame *pict, int *got_packet)
239 PNGEncContext *s = avctx->priv_data;
240 AVFrameSideData *side_data;
241 const AVFrame *const p = pict;
242 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
243 int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
244 int compression_level;
245 uint8_t *ptr, *top, *crow_buf, *crow;
246 uint8_t *crow_base = NULL;
247 uint8_t *progressive_buf = NULL;
248 uint8_t *rgba_buf = NULL;
249 uint8_t *top_buf = NULL;
251 is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
252 switch (avctx->pix_fmt) {
253 case AV_PIX_FMT_RGBA64BE:
255 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
257 case AV_PIX_FMT_RGB48BE:
259 color_type = PNG_COLOR_TYPE_RGB;
261 case AV_PIX_FMT_RGB32:
263 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
265 case AV_PIX_FMT_RGB24:
267 color_type = PNG_COLOR_TYPE_RGB;
269 case AV_PIX_FMT_GRAY16BE:
271 color_type = PNG_COLOR_TYPE_GRAY;
273 case AV_PIX_FMT_GRAY8:
275 color_type = PNG_COLOR_TYPE_GRAY;
277 case AV_PIX_FMT_MONOBLACK:
279 color_type = PNG_COLOR_TYPE_GRAY;
281 case AV_PIX_FMT_PAL8:
283 color_type = PNG_COLOR_TYPE_PALETTE;
288 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
289 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
291 s->zstream.zalloc = ff_png_zalloc;
292 s->zstream.zfree = ff_png_zfree;
293 s->zstream.opaque = NULL;
294 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
295 ? Z_DEFAULT_COMPRESSION
296 : av_clip(avctx->compression_level, 0, 9);
297 ret = deflateInit2(&s->zstream, compression_level,
298 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
302 enc_row_size = deflateBound(&s->zstream, row_size);
303 max_packet_size = avctx->height * (enc_row_size +
304 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
305 + AV_INPUT_BUFFER_MIN_SIZE;
307 (ret = av_new_packet(pkt, max_packet_size)) < 0) {
308 av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
313 s->bytestream_start =
314 s->bytestream = pkt->data;
315 s->bytestream_end = pkt->data + pkt->size;
317 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
320 // pixel data should be aligned, but there's a control byte before it
321 crow_buf = crow_base + 15;
322 if (is_progressive) {
323 progressive_buf = av_malloc(row_size + 1);
324 if (!progressive_buf)
327 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
328 rgba_buf = av_malloc(row_size + 1);
332 if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
333 top_buf = av_malloc(row_size + 1);
338 /* write png header */
339 memcpy(s->bytestream, ff_pngsig, 8);
342 AV_WB32(s->buf, avctx->width);
343 AV_WB32(s->buf + 4, avctx->height);
344 s->buf[8] = bit_depth;
345 s->buf[9] = color_type;
346 s->buf[10] = 0; /* compression type */
347 s->buf[11] = 0; /* filter type */
348 s->buf[12] = is_progressive; /* interlace type */
350 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
352 /* put the palette if needed */
353 if (color_type == PNG_COLOR_TYPE_PALETTE) {
354 int has_alpha, alpha, i;
359 palette = (uint32_t *)p->data[1];
361 alpha_ptr = s->buf + 256 * 3;
363 for (i = 0; i < 256; i++) {
366 if (alpha && alpha != 0xff)
368 *alpha_ptr++ = alpha;
369 bytestream_put_be24(&ptr, v);
371 png_write_chunk(&s->bytestream,
372 MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
374 png_write_chunk(&s->bytestream,
375 MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
379 /* write stereoscopic information */
380 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_STEREO3D);
382 AVStereo3D *stereo3d = (AVStereo3D *)side_data->data;
384 switch (stereo3d->type) {
385 case AV_STEREO3D_SIDEBYSIDE:
386 sm = !(stereo3d->flags & AV_STEREO3D_FLAG_INVERT);
387 png_write_chunk(&s->bytestream, MKTAG('s', 'T', 'E', 'R'), &sm, 1);
392 av_log(avctx, AV_LOG_WARNING,
393 "Only side-by-side stereo3d flag can be defined within sTER chunk\n");
398 /* now put each row */
399 s->zstream.avail_out = IOBUF_SIZE;
400 s->zstream.next_out = s->buf;
401 if (is_progressive) {
404 for (pass = 0; pass < NB_PASSES; pass++) {
405 /* NOTE: a pass is completely omitted if no pixels would be
407 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
408 if (pass_row_size > 0) {
410 for (y = 0; y < avctx->height; y++)
411 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
412 ptr = p->data[0] + y * p->linesize[0];
413 FFSWAP(uint8_t *, progressive_buf, top_buf);
414 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
415 convert_from_rgb32(rgba_buf, ptr, avctx->width);
418 png_get_interlaced_row(progressive_buf, pass_row_size,
419 bits_per_pixel, pass,
421 crow = png_choose_filter(s, crow_buf, progressive_buf,
422 top, pass_row_size, bits_per_pixel >> 3);
423 png_write_row(s, crow, pass_row_size + 1);
424 top = progressive_buf;
430 for (y = 0; y < avctx->height; y++) {
431 ptr = p->data[0] + y * p->linesize[0];
432 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
433 FFSWAP(uint8_t *, rgba_buf, top_buf);
434 convert_from_rgb32(rgba_buf, ptr, avctx->width);
437 crow = png_choose_filter(s, crow_buf, ptr, top,
438 row_size, bits_per_pixel >> 3);
439 png_write_row(s, crow, row_size + 1);
443 /* compress last bytes */
445 ret = deflate(&s->zstream, Z_FINISH);
446 if (ret == Z_OK || ret == Z_STREAM_END) {
447 len = IOBUF_SIZE - s->zstream.avail_out;
448 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
449 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
451 s->zstream.avail_out = IOBUF_SIZE;
452 s->zstream.next_out = s->buf;
453 if (ret == Z_STREAM_END)
459 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
461 pkt->size = s->bytestream - s->bytestream_start;
462 pkt->flags |= AV_PKT_FLAG_KEY;
468 av_free(progressive_buf);
471 deflateEnd(&s->zstream);
478 static av_cold int png_enc_init(AVCodecContext *avctx)
480 PNGEncContext *s = avctx->priv_data;
482 #if FF_API_CODED_FRAME
483 FF_DISABLE_DEPRECATION_WARNINGS
484 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
485 avctx->coded_frame->key_frame = 1;
486 FF_ENABLE_DEPRECATION_WARNINGS
489 ff_huffyuvencdsp_init(&s->hdsp);
491 #if FF_API_PRIVATE_OPT
492 FF_DISABLE_DEPRECATION_WARNINGS
493 if (avctx->prediction_method)
494 s->filter_type = av_clip(avctx->prediction_method,
495 PNG_FILTER_VALUE_NONE,
496 PNG_FILTER_VALUE_MIXED);
497 FF_ENABLE_DEPRECATION_WARNINGS
500 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
501 s->filter_type = PNG_FILTER_VALUE_NONE;
506 #define OFFSET(x) offsetof(PNGEncContext, x)
507 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
508 static const AVOption options[] = {
509 { "pred", "Prediction method", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = PNG_FILTER_VALUE_NONE }, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED, VE, "pred" },
510 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_NONE }, INT_MIN, INT_MAX, VE, "pred" },
511 { "sub", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_SUB }, INT_MIN, INT_MAX, VE, "pred" },
512 { "up", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_UP }, INT_MIN, INT_MAX, VE, "pred" },
513 { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_AVG }, INT_MIN, INT_MAX, VE, "pred" },
514 { "paeth", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_PAETH }, INT_MIN, INT_MAX, VE, "pred" },
515 { "mixed", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_MIXED }, INT_MIN, INT_MAX, VE, "pred" },
520 static const AVClass png_class = {
522 .item_name = av_default_item_name,
524 .version = LIBAVUTIL_VERSION_INT,
526 AVCodec ff_png_encoder = {
528 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
529 .type = AVMEDIA_TYPE_VIDEO,
530 .id = AV_CODEC_ID_PNG,
531 .priv_data_size = sizeof(PNGEncContext),
532 .priv_class = &png_class,
533 .init = png_enc_init,
534 .encode2 = encode_frame,
535 .pix_fmts = (const enum AVPixelFormat[]) {
536 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
537 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_GRAY16BE,
538 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE