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/stereo3d.h"
25 #include "bytestream.h"
26 #include "huffyuvencdsp.h"
30 * - add 2, 4 and 16 bit depth support
35 #define IOBUF_SIZE 4096
37 typedef struct PNGEncContext {
38 HuffYUVEncDSPContext hdsp;
41 uint8_t *bytestream_start;
42 uint8_t *bytestream_end;
47 uint8_t buf[IOBUF_SIZE];
50 static void png_get_interlaced_row(uint8_t *dst, int row_size,
51 int bits_per_pixel, int pass,
52 const uint8_t *src, int width)
54 int x, mask, dst_x, j, b, bpp;
58 mask = ff_png_pass_mask[pass];
59 switch (bits_per_pixel) {
61 memset(dst, 0, row_size);
63 for (x = 0; x < width; x++) {
65 if ((mask << j) & 0x80) {
66 b = (src[x >> 3] >> (7 - j)) & 1;
67 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
73 bpp = bits_per_pixel >> 3;
76 for (x = 0; x < width; x++) {
78 if ((mask << j) & 0x80) {
88 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
92 for (i = 0; i < w; i++) {
93 int a, b, c, p, pa, pb, pc;
106 if (pa <= pb && pa <= pc)
116 static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
117 uint8_t *src, uint8_t *top, int size, int bpp)
121 switch (filter_type) {
122 case PNG_FILTER_VALUE_NONE:
123 memcpy(dst, src, size);
125 case PNG_FILTER_VALUE_SUB:
126 c->hdsp.diff_bytes(dst, src, src - bpp, size);
127 memcpy(dst, src, bpp);
129 case PNG_FILTER_VALUE_UP:
130 c->hdsp.diff_bytes(dst, src, top, size);
132 case PNG_FILTER_VALUE_AVG:
133 for (i = 0; i < bpp; i++)
134 dst[i] = src[i] - (top[i] >> 1);
135 for (; i < size; i++)
136 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
138 case PNG_FILTER_VALUE_PAETH:
139 for (i = 0; i < bpp; i++)
140 dst[i] = src[i] - top[i];
141 sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
146 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
147 uint8_t *src, uint8_t *top, int size, int bpp)
149 int pred = s->filter_type;
150 assert(bpp || !pred);
152 pred = PNG_FILTER_VALUE_SUB;
153 if (pred == PNG_FILTER_VALUE_MIXED) {
155 int cost, bcost = INT_MAX;
156 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
157 for (pred = 0; pred < 5; pred++) {
158 png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
161 for (i = 0; i <= size; i++)
162 cost += abs((int8_t) buf1[i]);
165 FFSWAP(uint8_t *, buf1, buf2);
170 png_filter_row(s, dst + 1, pred, src, top, size, bpp);
176 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
183 for (j = 0; j < width; j++) {
184 v = ((const uint32_t *) src)[j];
193 static void png_write_chunk(uint8_t **f, uint32_t tag,
194 const uint8_t *buf, int length)
199 bytestream_put_be32(f, length);
200 crc = crc32(0, Z_NULL, 0);
201 AV_WL32(tagbuf, tag);
202 crc = crc32(crc, tagbuf, 4);
203 bytestream_put_be32(f, av_bswap32(tag));
205 crc = crc32(crc, buf, length);
206 memcpy(*f, buf, length);
209 bytestream_put_be32(f, crc);
212 /* XXX: do filtering */
213 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
217 s->zstream.avail_in = size;
218 s->zstream.next_in = data;
219 while (s->zstream.avail_in > 0) {
220 ret = deflate(&s->zstream, Z_NO_FLUSH);
223 if (s->zstream.avail_out == 0) {
224 if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
225 png_write_chunk(&s->bytestream,
226 MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
227 s->zstream.avail_out = IOBUF_SIZE;
228 s->zstream.next_out = s->buf;
234 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
235 const AVFrame *pict, int *got_packet)
237 PNGEncContext *s = avctx->priv_data;
238 AVFrameSideData *side_data;
239 const AVFrame *const p = pict;
240 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
241 int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
242 int compression_level;
243 uint8_t *ptr, *top, *crow_buf, *crow;
244 uint8_t *crow_base = NULL;
245 uint8_t *progressive_buf = NULL;
246 uint8_t *rgba_buf = NULL;
247 uint8_t *top_buf = NULL;
249 is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
250 switch (avctx->pix_fmt) {
251 case AV_PIX_FMT_RGBA64BE:
253 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
255 case AV_PIX_FMT_RGB48BE:
257 color_type = PNG_COLOR_TYPE_RGB;
259 case AV_PIX_FMT_RGB32:
261 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
263 case AV_PIX_FMT_RGB24:
265 color_type = PNG_COLOR_TYPE_RGB;
267 case AV_PIX_FMT_GRAY16BE:
269 color_type = PNG_COLOR_TYPE_GRAY;
271 case AV_PIX_FMT_GRAY8:
273 color_type = PNG_COLOR_TYPE_GRAY;
275 case AV_PIX_FMT_MONOBLACK:
277 color_type = PNG_COLOR_TYPE_GRAY;
279 case AV_PIX_FMT_PAL8:
281 color_type = PNG_COLOR_TYPE_PALETTE;
286 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
287 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
289 s->zstream.zalloc = ff_png_zalloc;
290 s->zstream.zfree = ff_png_zfree;
291 s->zstream.opaque = NULL;
292 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
293 ? Z_DEFAULT_COMPRESSION
294 : av_clip(avctx->compression_level, 0, 9);
295 ret = deflateInit2(&s->zstream, compression_level,
296 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
300 enc_row_size = deflateBound(&s->zstream, row_size);
301 max_packet_size = avctx->height * (enc_row_size +
302 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
303 + AV_INPUT_BUFFER_MIN_SIZE;
305 (ret = av_new_packet(pkt, max_packet_size)) < 0) {
306 av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
311 s->bytestream_start =
312 s->bytestream = pkt->data;
313 s->bytestream_end = pkt->data + pkt->size;
315 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
318 // pixel data should be aligned, but there's a control byte before it
319 crow_buf = crow_base + 15;
320 if (is_progressive) {
321 progressive_buf = av_malloc(row_size + 1);
322 if (!progressive_buf)
325 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
326 rgba_buf = av_malloc(row_size + 1);
330 if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
331 top_buf = av_malloc(row_size + 1);
336 /* write png header */
337 memcpy(s->bytestream, ff_pngsig, 8);
340 AV_WB32(s->buf, avctx->width);
341 AV_WB32(s->buf + 4, avctx->height);
342 s->buf[8] = bit_depth;
343 s->buf[9] = color_type;
344 s->buf[10] = 0; /* compression type */
345 s->buf[11] = 0; /* filter type */
346 s->buf[12] = is_progressive; /* interlace type */
348 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
350 /* put the palette if needed */
351 if (color_type == PNG_COLOR_TYPE_PALETTE) {
352 int has_alpha, alpha, i;
357 palette = (uint32_t *)p->data[1];
359 alpha_ptr = s->buf + 256 * 3;
361 for (i = 0; i < 256; i++) {
364 if (alpha && alpha != 0xff)
366 *alpha_ptr++ = alpha;
367 bytestream_put_be24(&ptr, v);
369 png_write_chunk(&s->bytestream,
370 MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
372 png_write_chunk(&s->bytestream,
373 MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
377 /* write stereoscopic information */
378 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_STEREO3D);
380 AVStereo3D *stereo3d = (AVStereo3D *)side_data->data;
382 switch (stereo3d->type) {
383 case AV_STEREO3D_SIDEBYSIDE:
384 sm = !(stereo3d->flags & AV_STEREO3D_FLAG_INVERT);
385 png_write_chunk(&s->bytestream, MKTAG('s', 'T', 'E', 'R'), &sm, 1);
390 av_log(avctx, AV_LOG_WARNING,
391 "Only side-by-side stereo3d flag can be defined within sTER chunk\n");
396 /* now put each row */
397 s->zstream.avail_out = IOBUF_SIZE;
398 s->zstream.next_out = s->buf;
399 if (is_progressive) {
402 for (pass = 0; pass < NB_PASSES; pass++) {
403 /* NOTE: a pass is completely omitted if no pixels would be
405 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
406 if (pass_row_size > 0) {
408 for (y = 0; y < avctx->height; y++)
409 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
410 ptr = p->data[0] + y * p->linesize[0];
411 FFSWAP(uint8_t *, progressive_buf, top_buf);
412 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
413 convert_from_rgb32(rgba_buf, ptr, avctx->width);
416 png_get_interlaced_row(progressive_buf, pass_row_size,
417 bits_per_pixel, pass,
419 crow = png_choose_filter(s, crow_buf, progressive_buf,
420 top, pass_row_size, bits_per_pixel >> 3);
421 png_write_row(s, crow, pass_row_size + 1);
422 top = progressive_buf;
428 for (y = 0; y < avctx->height; y++) {
429 ptr = p->data[0] + y * p->linesize[0];
430 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
431 FFSWAP(uint8_t *, rgba_buf, top_buf);
432 convert_from_rgb32(rgba_buf, ptr, avctx->width);
435 crow = png_choose_filter(s, crow_buf, ptr, top,
436 row_size, bits_per_pixel >> 3);
437 png_write_row(s, crow, row_size + 1);
441 /* compress last bytes */
443 ret = deflate(&s->zstream, Z_FINISH);
444 if (ret == Z_OK || ret == Z_STREAM_END) {
445 len = IOBUF_SIZE - s->zstream.avail_out;
446 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
447 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
449 s->zstream.avail_out = IOBUF_SIZE;
450 s->zstream.next_out = s->buf;
451 if (ret == Z_STREAM_END)
457 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
459 pkt->size = s->bytestream - s->bytestream_start;
460 pkt->flags |= AV_PKT_FLAG_KEY;
466 av_free(progressive_buf);
469 deflateEnd(&s->zstream);
476 static av_cold int png_enc_init(AVCodecContext *avctx)
478 PNGEncContext *s = avctx->priv_data;
480 #if FF_API_CODED_FRAME
481 FF_DISABLE_DEPRECATION_WARNINGS
482 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
483 avctx->coded_frame->key_frame = 1;
484 FF_ENABLE_DEPRECATION_WARNINGS
487 ff_huffyuvencdsp_init(&s->hdsp);
489 s->filter_type = av_clip(avctx->prediction_method,
490 PNG_FILTER_VALUE_NONE,
491 PNG_FILTER_VALUE_MIXED);
492 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
493 s->filter_type = PNG_FILTER_VALUE_NONE;
498 AVCodec ff_png_encoder = {
500 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
501 .type = AVMEDIA_TYPE_VIDEO,
502 .id = AV_CODEC_ID_PNG,
503 .priv_data_size = sizeof(PNGEncContext),
504 .init = png_enc_init,
505 .encode2 = encode_frame,
506 .pix_fmts = (const enum AVPixelFormat[]) {
507 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
508 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_GRAY16BE,
509 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE