pngenc.c
Go to the documentation of this file.
1 /*
2  * PNG image format
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "dsputil.h"
24 #include "png.h"
25 
26 /* TODO:
27  * - add 2, 4 and 16 bit depth support
28  */
29 
30 #include <zlib.h>
31 
32 //#define DEBUG
33 
34 #define IOBUF_SIZE 4096
35 
36 typedef struct PNGEncContext {
38 
43 
45 
46  z_stream zstream;
49 
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)
53 {
54  int x, mask, dst_x, j, b, bpp;
55  uint8_t *d;
56  const uint8_t *s;
57 
58  mask = ff_png_pass_mask[pass];
59  switch(bits_per_pixel) {
60  case 1:
61  memset(dst, 0, row_size);
62  dst_x = 0;
63  for(x = 0; x < width; x++) {
64  j = (x & 7);
65  if ((mask << j) & 0x80) {
66  b = (src[x >> 3] >> (7 - j)) & 1;
67  dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
68  dst_x++;
69  }
70  }
71  break;
72  default:
73  bpp = bits_per_pixel >> 3;
74  d = dst;
75  s = src;
76  for(x = 0; x < width; x++) {
77  j = x & 7;
78  if ((mask << j) & 0x80) {
79  memcpy(d, s, bpp);
80  d += bpp;
81  }
82  s += bpp;
83  }
84  break;
85  }
86 }
87 
88 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
89 {
90  int i;
91  for(i = 0; i < w; i++) {
92  int a, b, c, p, pa, pb, pc;
93 
94  a = src[i - bpp];
95  b = top[i];
96  c = top[i - bpp];
97 
98  p = b - c;
99  pc = a - c;
100 
101  pa = abs(p);
102  pb = abs(pc);
103  pc = abs(p + pc);
104 
105  if (pa <= pb && pa <= pc)
106  p = a;
107  else if (pb <= pc)
108  p = b;
109  else
110  p = c;
111  dst[i] = src[i] - p;
112  }
113 }
114 
115 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
116  uint8_t *src, uint8_t *top, int size, int bpp)
117 {
118  int i;
119 
120  switch(filter_type) {
122  memcpy(dst, src, size);
123  break;
125  dsp->diff_bytes(dst, src, src-bpp, size);
126  memcpy(dst, src, bpp);
127  break;
128  case PNG_FILTER_VALUE_UP:
129  dsp->diff_bytes(dst, src, top, size);
130  break;
132  for(i = 0; i < bpp; i++)
133  dst[i] = src[i] - (top[i] >> 1);
134  for(; i < size; i++)
135  dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
136  break;
138  for(i = 0; i < bpp; i++)
139  dst[i] = src[i] - top[i];
140  sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
141  break;
142  }
143 }
144 
146  uint8_t *src, uint8_t *top, int size, int bpp)
147 {
148  int pred = s->filter_type;
149  assert(bpp || !pred);
150  if(!top && pred)
151  pred = PNG_FILTER_VALUE_SUB;
152  if(pred == PNG_FILTER_VALUE_MIXED) {
153  int i;
154  int cost, bcost = INT_MAX;
155  uint8_t *buf1 = dst, *buf2 = dst + size + 16;
156  for(pred=0; pred<5; pred++) {
157  png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
158  buf1[0] = pred;
159  cost = 0;
160  for(i=0; i<=size; i++)
161  cost += abs((int8_t)buf1[i]);
162  if(cost < bcost) {
163  bcost = cost;
164  FFSWAP(uint8_t*, buf1, buf2);
165  }
166  }
167  return buf2;
168  } else {
169  png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
170  dst[0] = pred;
171  return dst;
172  }
173 }
174 
175 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
176 {
177  uint8_t *d;
178  int j;
179  unsigned int v;
180 
181  d = dst;
182  for(j = 0; j < width; j++) {
183  v = ((const uint32_t *)src)[j];
184  d[0] = v >> 16;
185  d[1] = v >> 8;
186  d[2] = v;
187  d[3] = v >> 24;
188  d += 4;
189  }
190 }
191 
192 static void png_write_chunk(uint8_t **f, uint32_t tag,
193  const uint8_t *buf, int length)
194 {
195  uint32_t crc;
196  uint8_t tagbuf[4];
197 
198  bytestream_put_be32(f, length);
199  crc = crc32(0, Z_NULL, 0);
200  AV_WL32(tagbuf, tag);
201  crc = crc32(crc, tagbuf, 4);
202  bytestream_put_be32(f, av_bswap32(tag));
203  if (length > 0) {
204  crc = crc32(crc, buf, length);
205  memcpy(*f, buf, length);
206  *f += length;
207  }
208  bytestream_put_be32(f, crc);
209 }
210 
211 /* XXX: do filtering */
212 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
213 {
214  int ret;
215 
216  s->zstream.avail_in = size;
217  s->zstream.next_in = (uint8_t *)data;
218  while (s->zstream.avail_in > 0) {
219  ret = deflate(&s->zstream, Z_NO_FLUSH);
220  if (ret != Z_OK)
221  return -1;
222  if (s->zstream.avail_out == 0) {
223  if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
224  png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
225  s->zstream.avail_out = IOBUF_SIZE;
226  s->zstream.next_out = s->buf;
227  }
228  }
229  return 0;
230 }
231 
232 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
233  const AVFrame *pict, int *got_packet)
234 {
235  PNGEncContext *s = avctx->priv_data;
236  AVFrame * const p= &s->picture;
237  int bit_depth, color_type, y, len, row_size, ret, is_progressive;
238  int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
239  int compression_level;
240  uint8_t *ptr, *top;
241  uint8_t *crow_base = NULL, *crow_buf, *crow;
242  uint8_t *progressive_buf = NULL;
243  uint8_t *rgba_buf = NULL;
244  uint8_t *top_buf = NULL;
245 
246  *p = *pict;
248  p->key_frame= 1;
249 
250  is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
251  switch(avctx->pix_fmt) {
252  case AV_PIX_FMT_RGB32:
253  bit_depth = 8;
254  color_type = PNG_COLOR_TYPE_RGB_ALPHA;
255  break;
256  case AV_PIX_FMT_RGB24:
257  bit_depth = 8;
258  color_type = PNG_COLOR_TYPE_RGB;
259  break;
260  case AV_PIX_FMT_GRAY8:
261  bit_depth = 8;
262  color_type = PNG_COLOR_TYPE_GRAY;
263  break;
265  bit_depth = 1;
266  color_type = PNG_COLOR_TYPE_GRAY;
267  break;
268  case AV_PIX_FMT_PAL8:
269  bit_depth = 8;
270  color_type = PNG_COLOR_TYPE_PALETTE;
271  break;
272  default:
273  return -1;
274  }
275  bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
276  row_size = (avctx->width * bits_per_pixel + 7) >> 3;
277 
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);
286  if (ret != Z_OK)
287  return -1;
288 
289  enc_row_size = deflateBound(&s->zstream, row_size);
290  max_packet_size = avctx->height * (enc_row_size +
291  ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
293  if (!pkt->data &&
294  (ret = av_new_packet(pkt, max_packet_size)) < 0) {
295  av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
296  max_packet_size);
297  return ret;
298  }
299 
300  s->bytestream_start =
301  s->bytestream = pkt->data;
302  s->bytestream_end = pkt->data + pkt->size;
303 
304  crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
305  if (!crow_base)
306  goto fail;
307  crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
308  if (is_progressive) {
309  progressive_buf = av_malloc(row_size + 1);
310  if (!progressive_buf)
311  goto fail;
312  }
313  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
314  rgba_buf = av_malloc(row_size + 1);
315  if (!rgba_buf)
316  goto fail;
317  }
318  if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
319  top_buf = av_malloc(row_size + 1);
320  if (!top_buf)
321  goto fail;
322  }
323 
324  /* write png header */
325  memcpy(s->bytestream, ff_pngsig, 8);
326  s->bytestream += 8;
327 
328  AV_WB32(s->buf, avctx->width);
329  AV_WB32(s->buf + 4, avctx->height);
330  s->buf[8] = bit_depth;
331  s->buf[9] = color_type;
332  s->buf[10] = 0; /* compression type */
333  s->buf[11] = 0; /* filter type */
334  s->buf[12] = is_progressive; /* interlace type */
335 
336  png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
337 
338  /* put the palette if needed */
339  if (color_type == PNG_COLOR_TYPE_PALETTE) {
340  int has_alpha, alpha, i;
341  unsigned int v;
342  uint32_t *palette;
343  uint8_t *alpha_ptr;
344 
345  palette = (uint32_t *)p->data[1];
346  ptr = s->buf;
347  alpha_ptr = s->buf + 256 * 3;
348  has_alpha = 0;
349  for(i = 0; i < 256; i++) {
350  v = palette[i];
351  alpha = v >> 24;
352  if (alpha && alpha != 0xff)
353  has_alpha = 1;
354  *alpha_ptr++ = alpha;
355  bytestream_put_be24(&ptr, v);
356  }
357  png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
358  if (has_alpha) {
359  png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
360  }
361  }
362 
363  /* now put each row */
364  s->zstream.avail_out = IOBUF_SIZE;
365  s->zstream.next_out = s->buf;
366  if (is_progressive) {
367  int pass;
368 
369  for(pass = 0; pass < NB_PASSES; pass++) {
370  /* NOTE: a pass is completely omitted if no pixels would be
371  output */
372  pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
373  if (pass_row_size > 0) {
374  top = NULL;
375  for(y = 0; y < avctx->height; y++) {
376  if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
377  ptr = p->data[0] + y * p->linesize[0];
378  FFSWAP(uint8_t*, progressive_buf, top_buf);
379  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
380  convert_from_rgb32(rgba_buf, ptr, avctx->width);
381  ptr = rgba_buf;
382  }
383  png_get_interlaced_row(progressive_buf, pass_row_size,
384  bits_per_pixel, pass,
385  ptr, avctx->width);
386  crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
387  png_write_row(s, crow, pass_row_size + 1);
388  top = progressive_buf;
389  }
390  }
391  }
392  }
393  } else {
394  top = NULL;
395  for(y = 0; y < avctx->height; y++) {
396  ptr = p->data[0] + y * p->linesize[0];
397  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
398  FFSWAP(uint8_t*, rgba_buf, top_buf);
399  convert_from_rgb32(rgba_buf, ptr, avctx->width);
400  ptr = rgba_buf;
401  }
402  crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
403  png_write_row(s, crow, row_size + 1);
404  top = ptr;
405  }
406  }
407  /* compress last bytes */
408  for(;;) {
409  ret = deflate(&s->zstream, Z_FINISH);
410  if (ret == Z_OK || ret == Z_STREAM_END) {
411  len = IOBUF_SIZE - s->zstream.avail_out;
412  if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
413  png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
414  }
415  s->zstream.avail_out = IOBUF_SIZE;
416  s->zstream.next_out = s->buf;
417  if (ret == Z_STREAM_END)
418  break;
419  } else {
420  goto fail;
421  }
422  }
423  png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
424 
425  pkt->size = s->bytestream - s->bytestream_start;
426  pkt->flags |= AV_PKT_FLAG_KEY;
427  *got_packet = 1;
428  ret = 0;
429 
430  the_end:
431  av_free(crow_base);
432  av_free(progressive_buf);
433  av_free(rgba_buf);
434  av_free(top_buf);
435  deflateEnd(&s->zstream);
436  return ret;
437  fail:
438  ret = -1;
439  goto the_end;
440 }
441 
443  PNGEncContext *s = avctx->priv_data;
444 
446  avctx->coded_frame= &s->picture;
447  ff_dsputil_init(&s->dsp, avctx);
448 
450  if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
452 
453  return 0;
454 }
455 
457  .name = "png",
458  .type = AVMEDIA_TYPE_VIDEO,
459  .id = AV_CODEC_ID_PNG,
460  .priv_data_size = sizeof(PNGEncContext),
461  .init = png_enc_init,
462  .encode2 = encode_frame,
463  .pix_fmts = (const enum AVPixelFormat[]){
466  },
467  .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
468 };
#define PNG_FILTER_VALUE_AVG
Definition: png.h:41
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
struct PNGEncContext PNGEncContext
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, uint8_t *src, uint8_t *top, int size, int bpp)
Definition: pngenc.c:145
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
uint8_t * bytestream
Definition: pngenc.c:39
int size
Definition: avcodec.h:916
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pngenc.c:232
#define pass
Definition: fft.c:334
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
#define PNG_COLOR_TYPE_RGB
Definition: png.h:33
AVCodec.
Definition: avcodec.h:2960
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:32
AVFrame picture
Definition: pngenc.c:42
int filter_type
Definition: pngenc.c:44
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:42
uint8_t
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:34
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
Definition: pngenc.c:88
#define b
Definition: input.c:52
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
uint32_t tag
Definition: movenc.c:802
#define PNG_FILTER_VALUE_MIXED
Definition: png.h:43
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:56
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static void png_get_interlaced_row(uint8_t *dst, int row_size, int bits_per_pixel, int pass, const uint8_t *src, int width)
Definition: pngenc.c:50
static const uint16_t mask[17]
Definition: lzw.c:38
z_stream zstream
Definition: pngenc.c:46
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int ff_png_get_nb_channels(int color_type)
Definition: png.c:58
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
#define NB_PASSES
Definition: png.h:50
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
uint8_t * bytestream_start
Definition: pngenc.c:40
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
#define PNG_FILTER_VALUE_SUB
Definition: png.h:39
static void png_write_chunk(uint8_t **f, uint32_t tag, const uint8_t *buf, int length)
Definition: pngenc.c:192
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:31
int width
picture width / height.
Definition: avcodec.h:1508
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:52
#define IOBUF_SIZE
Definition: pngenc.c:34
static int width
Definition: utils.c:156
#define av_bswap32
Definition: bswap.h:33
AVCodec ff_png_encoder
Definition: pngenc.c:456
external API header
int compression_level
Definition: avcodec.h:1426
#define PNG_FILTER_VALUE_UP
Definition: png.h:40
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
const uint8_t ff_png_pass_mask[NB_PASSES]
Definition: png.c:44
const uint8_t ff_png_pass_ymask[NB_PASSES]
Definition: png.c:29
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
static av_cold int png_enc_init(AVCodecContext *avctx)
Definition: pngenc.c:442
uint8_t * bytestream_end
Definition: pngenc.c:41
static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
Definition: pngenc.c:175
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
uint8_t buf[IOBUF_SIZE]
Definition: pngenc.c:47
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
void(* diff_bytes)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
Definition: dsputil.h:334
Y , 8bpp.
Definition: pixfmt.h:73
#define PNG_FILTER_VALUE_NONE
Definition: png.h:38
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1705
const uint8_t ff_pngsig[8]
Definition: png.c:25
void ff_png_zfree(void *opaque, void *ptr)
Definition: png.c:53
DSP utils.
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *top, int size, int bpp)
Definition: pngenc.c:115
void * priv_data
Definition: avcodec.h:1382
int len
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
Definition: pngenc.c:212
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
Definition: png.c:71
DSPContext dsp
Definition: pngenc.c:37
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
Definition: png.c:48
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
DSPContext.
Definition: dsputil.h:194