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"
28 * - add 2, 4 and 16 bit depth support
35 #define IOBUF_SIZE 4096
37 typedef struct PNGEncContext {
41 uint8_t *bytestream_start;
42 uint8_t *bytestream_end;
48 uint8_t buf[IOBUF_SIZE];
51 static void png_get_interlaced_row(uint8_t *dst, int row_size,
52 int bits_per_pixel, int pass,
53 const uint8_t *src, int width)
55 int x, mask, dst_x, j, b, bpp;
59 mask = (int[]){0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}[pass];
60 switch(bits_per_pixel) {
62 memset(dst, 0, row_size);
64 for(x = 0; x < width; x++) {
66 if ((mask << j) & 0x80) {
67 b = (src[x >> 3] >> (7 - j)) & 1;
68 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
74 bpp = bits_per_pixel >> 3;
77 for(x = 0; x < width; x++) {
79 if ((mask << j) & 0x80) {
89 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
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(DSPContext *dsp, 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 dsp->diff_bytes(dst, src, src-bpp, size);
127 memcpy(dst, src, bpp);
129 case PNG_FILTER_VALUE_UP:
130 dsp->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);
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->dsp, 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->dsp, dst+1, pred, src, top, size, bpp);
176 static void png_write_chunk(uint8_t **f, uint32_t tag,
177 const uint8_t *buf, int length)
182 bytestream_put_be32(f, length);
183 crc = crc32(0, Z_NULL, 0);
184 AV_WL32(tagbuf, tag);
185 crc = crc32(crc, tagbuf, 4);
186 bytestream_put_be32(f, av_bswap32(tag));
188 crc = crc32(crc, buf, length);
189 memcpy(*f, buf, length);
192 bytestream_put_be32(f, crc);
195 /* XXX: do filtering */
196 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
200 s->zstream.avail_in = size;
201 s->zstream.next_in = (uint8_t *)data;
202 while (s->zstream.avail_in > 0) {
203 ret = deflate(&s->zstream, Z_NO_FLUSH);
206 if (s->zstream.avail_out == 0) {
207 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
208 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
209 s->zstream.avail_out = IOBUF_SIZE;
210 s->zstream.next_out = s->buf;
216 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
217 const AVFrame *pict, int *got_packet)
219 PNGEncContext *s = avctx->priv_data;
220 AVFrame * const p= &s->picture;
221 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
222 int bits_per_pixel, pass_row_size, enc_row_size;
223 int64_t max_packet_size;
224 int compression_level;
226 uint8_t *crow_base = NULL, *crow_buf, *crow;
227 uint8_t *progressive_buf = NULL;
228 uint8_t *top_buf = NULL;
231 p->pict_type= AV_PICTURE_TYPE_I;
234 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
235 switch(avctx->pix_fmt) {
236 case PIX_FMT_RGBA64BE:
238 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
240 case PIX_FMT_RGB48BE:
242 color_type = PNG_COLOR_TYPE_RGB;
246 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
250 color_type = PNG_COLOR_TYPE_RGB;
252 case PIX_FMT_GRAY16BE:
254 color_type = PNG_COLOR_TYPE_GRAY;
258 color_type = PNG_COLOR_TYPE_GRAY;
262 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
264 case PIX_FMT_MONOBLACK:
266 color_type = PNG_COLOR_TYPE_GRAY;
270 color_type = PNG_COLOR_TYPE_PALETTE;
275 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
276 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
278 s->zstream.zalloc = ff_png_zalloc;
279 s->zstream.zfree = ff_png_zfree;
280 s->zstream.opaque = NULL;
281 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
282 Z_DEFAULT_COMPRESSION :
283 av_clip(avctx->compression_level, 0, 9);
284 ret = deflateInit2(&s->zstream, compression_level,
285 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
289 enc_row_size = deflateBound(&s->zstream, row_size);
290 max_packet_size = avctx->height * (int64_t)(enc_row_size +
291 ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
292 + FF_MIN_BUFFER_SIZE;
293 if (max_packet_size > INT_MAX)
294 return AVERROR(ENOMEM);
295 if ((ret = ff_alloc_packet2(avctx, pkt, max_packet_size)) < 0)
298 s->bytestream_start =
299 s->bytestream = pkt->data;
300 s->bytestream_end = pkt->data + pkt->size;
302 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
305 crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
306 if (is_progressive) {
307 progressive_buf = av_malloc(row_size + 1);
308 if (!progressive_buf)
311 if (is_progressive) {
312 top_buf = av_malloc(row_size + 1);
317 /* write png header */
318 memcpy(s->bytestream, ff_pngsig, 8);
321 AV_WB32(s->buf, avctx->width);
322 AV_WB32(s->buf + 4, avctx->height);
323 s->buf[8] = bit_depth;
324 s->buf[9] = color_type;
325 s->buf[10] = 0; /* compression type */
326 s->buf[11] = 0; /* filter type */
327 s->buf[12] = is_progressive; /* interlace type */
329 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
331 /* put the palette if needed */
332 if (color_type == PNG_COLOR_TYPE_PALETTE) {
333 int has_alpha, alpha, i;
338 palette = (uint32_t *)p->data[1];
340 alpha_ptr = s->buf + 256 * 3;
342 for(i = 0; i < 256; i++) {
347 *alpha_ptr++ = alpha;
348 bytestream_put_be24(&ptr, v);
350 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
352 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
356 /* now put each row */
357 s->zstream.avail_out = IOBUF_SIZE;
358 s->zstream.next_out = s->buf;
359 if (is_progressive) {
362 for(pass = 0; pass < NB_PASSES; pass++) {
363 /* NOTE: a pass is completely omited if no pixels would be
365 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
366 if (pass_row_size > 0) {
368 for(y = 0; y < avctx->height; y++) {
369 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
370 ptr = p->data[0] + y * p->linesize[0];
371 FFSWAP(uint8_t*, progressive_buf, top_buf);
372 png_get_interlaced_row(progressive_buf, pass_row_size,
373 bits_per_pixel, pass,
375 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
376 png_write_row(s, crow, pass_row_size + 1);
377 top = progressive_buf;
384 for(y = 0; y < avctx->height; y++) {
385 ptr = p->data[0] + y * p->linesize[0];
386 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
387 png_write_row(s, crow, row_size + 1);
391 /* compress last bytes */
393 ret = deflate(&s->zstream, Z_FINISH);
394 if (ret == Z_OK || ret == Z_STREAM_END) {
395 len = IOBUF_SIZE - s->zstream.avail_out;
396 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
397 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
399 s->zstream.avail_out = IOBUF_SIZE;
400 s->zstream.next_out = s->buf;
401 if (ret == Z_STREAM_END)
407 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
409 pkt->size = s->bytestream - s->bytestream_start;
410 pkt->flags |= AV_PKT_FLAG_KEY;
416 av_free(progressive_buf);
418 deflateEnd(&s->zstream);
425 static av_cold int png_enc_init(AVCodecContext *avctx){
426 PNGEncContext *s = avctx->priv_data;
428 avcodec_get_frame_defaults(&s->picture);
429 avctx->coded_frame= &s->picture;
430 ff_dsputil_init(&s->dsp, avctx);
432 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
433 if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
434 s->filter_type = PNG_FILTER_VALUE_NONE;
439 AVCodec ff_png_encoder = {
441 .type = AVMEDIA_TYPE_VIDEO,
443 .priv_data_size = sizeof(PNGEncContext),
444 .init = png_enc_init,
445 .encode2 = encode_frame,
446 .pix_fmts = (const enum PixelFormat[]){
447 PIX_FMT_RGB24, PIX_FMT_RGBA,
448 PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE,
450 PIX_FMT_GRAY8, PIX_FMT_GRAY8A,
452 PIX_FMT_MONOBLACK, PIX_FMT_NONE
454 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),