Libav
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 IOBUF_SIZE 4096
33 
34 typedef struct PNGEncContext {
36 
40 
42 
43  z_stream zstream;
46 
47 static void png_get_interlaced_row(uint8_t *dst, int row_size,
48  int bits_per_pixel, int pass,
49  const uint8_t *src, int width)
50 {
51  int x, mask, dst_x, j, b, bpp;
52  uint8_t *d;
53  const uint8_t *s;
54 
55  mask = ff_png_pass_mask[pass];
56  switch(bits_per_pixel) {
57  case 1:
58  memset(dst, 0, row_size);
59  dst_x = 0;
60  for(x = 0; x < width; x++) {
61  j = (x & 7);
62  if ((mask << j) & 0x80) {
63  b = (src[x >> 3] >> (7 - j)) & 1;
64  dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
65  dst_x++;
66  }
67  }
68  break;
69  default:
70  bpp = bits_per_pixel >> 3;
71  d = dst;
72  s = src;
73  for(x = 0; x < width; x++) {
74  j = x & 7;
75  if ((mask << j) & 0x80) {
76  memcpy(d, s, bpp);
77  d += bpp;
78  }
79  s += bpp;
80  }
81  break;
82  }
83 }
84 
85 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
86 {
87  int i;
88  for(i = 0; i < w; i++) {
89  int a, b, c, p, pa, pb, pc;
90 
91  a = src[i - bpp];
92  b = top[i];
93  c = top[i - bpp];
94 
95  p = b - c;
96  pc = a - c;
97 
98  pa = abs(p);
99  pb = abs(pc);
100  pc = abs(p + pc);
101 
102  if (pa <= pb && pa <= pc)
103  p = a;
104  else if (pb <= pc)
105  p = b;
106  else
107  p = c;
108  dst[i] = src[i] - p;
109  }
110 }
111 
112 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
113  uint8_t *src, uint8_t *top, int size, int bpp)
114 {
115  int i;
116 
117  switch(filter_type) {
119  memcpy(dst, src, size);
120  break;
122  dsp->diff_bytes(dst, src, src-bpp, size);
123  memcpy(dst, src, bpp);
124  break;
125  case PNG_FILTER_VALUE_UP:
126  dsp->diff_bytes(dst, src, top, size);
127  break;
129  for(i = 0; i < bpp; i++)
130  dst[i] = src[i] - (top[i] >> 1);
131  for(; i < size; i++)
132  dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
133  break;
135  for(i = 0; i < bpp; i++)
136  dst[i] = src[i] - top[i];
137  sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
138  break;
139  }
140 }
141 
143  uint8_t *src, uint8_t *top, int size, int bpp)
144 {
145  int pred = s->filter_type;
146  assert(bpp || !pred);
147  if(!top && pred)
148  pred = PNG_FILTER_VALUE_SUB;
149  if(pred == PNG_FILTER_VALUE_MIXED) {
150  int i;
151  int cost, bcost = INT_MAX;
152  uint8_t *buf1 = dst, *buf2 = dst + size + 16;
153  for(pred=0; pred<5; pred++) {
154  png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
155  buf1[0] = pred;
156  cost = 0;
157  for(i=0; i<=size; i++)
158  cost += abs((int8_t)buf1[i]);
159  if(cost < bcost) {
160  bcost = cost;
161  FFSWAP(uint8_t*, buf1, buf2);
162  }
163  }
164  return buf2;
165  } else {
166  png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
167  dst[0] = pred;
168  return dst;
169  }
170 }
171 
172 static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
173 {
174  uint8_t *d;
175  int j;
176  unsigned int v;
177 
178  d = dst;
179  for(j = 0; j < width; j++) {
180  v = ((const uint32_t *)src)[j];
181  d[0] = v >> 16;
182  d[1] = v >> 8;
183  d[2] = v;
184  d[3] = v >> 24;
185  d += 4;
186  }
187 }
188 
189 static void png_write_chunk(uint8_t **f, uint32_t tag,
190  const uint8_t *buf, int length)
191 {
192  uint32_t crc;
193  uint8_t tagbuf[4];
194 
195  bytestream_put_be32(f, length);
196  crc = crc32(0, Z_NULL, 0);
197  AV_WL32(tagbuf, tag);
198  crc = crc32(crc, tagbuf, 4);
199  bytestream_put_be32(f, av_bswap32(tag));
200  if (length > 0) {
201  crc = crc32(crc, buf, length);
202  memcpy(*f, buf, length);
203  *f += length;
204  }
205  bytestream_put_be32(f, crc);
206 }
207 
208 /* XXX: do filtering */
209 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
210 {
211  int ret;
212 
213  s->zstream.avail_in = size;
214  s->zstream.next_in = (uint8_t *)data;
215  while (s->zstream.avail_in > 0) {
216  ret = deflate(&s->zstream, Z_NO_FLUSH);
217  if (ret != Z_OK)
218  return -1;
219  if (s->zstream.avail_out == 0) {
220  if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
221  png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
222  s->zstream.avail_out = IOBUF_SIZE;
223  s->zstream.next_out = s->buf;
224  }
225  }
226  return 0;
227 }
228 
229 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
230  const AVFrame *pict, int *got_packet)
231 {
232  PNGEncContext *s = avctx->priv_data;
233  const AVFrame * const p = pict;
234  int bit_depth, color_type, y, len, row_size, ret, is_progressive;
235  int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
236  int compression_level;
237  uint8_t *ptr, *top;
238  uint8_t *crow_base = NULL, *crow_buf, *crow;
239  uint8_t *progressive_buf = NULL;
240  uint8_t *rgba_buf = NULL;
241  uint8_t *top_buf = NULL;
242 
243  is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
244  switch(avctx->pix_fmt) {
245  case AV_PIX_FMT_RGB32:
246  bit_depth = 8;
247  color_type = PNG_COLOR_TYPE_RGB_ALPHA;
248  break;
249  case AV_PIX_FMT_RGB24:
250  bit_depth = 8;
251  color_type = PNG_COLOR_TYPE_RGB;
252  break;
253  case AV_PIX_FMT_GRAY16BE:
254  bit_depth = 16;
255  color_type = PNG_COLOR_TYPE_GRAY;
256  break;
257  case AV_PIX_FMT_GRAY8:
258  bit_depth = 8;
259  color_type = PNG_COLOR_TYPE_GRAY;
260  break;
262  bit_depth = 1;
263  color_type = PNG_COLOR_TYPE_GRAY;
264  break;
265  case AV_PIX_FMT_PAL8:
266  bit_depth = 8;
267  color_type = PNG_COLOR_TYPE_PALETTE;
268  break;
269  default:
270  return -1;
271  }
272  bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
273  row_size = (avctx->width * bits_per_pixel + 7) >> 3;
274 
275  s->zstream.zalloc = ff_png_zalloc;
276  s->zstream.zfree = ff_png_zfree;
277  s->zstream.opaque = NULL;
278  compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
279  Z_DEFAULT_COMPRESSION :
280  av_clip(avctx->compression_level, 0, 9);
281  ret = deflateInit2(&s->zstream, compression_level,
282  Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
283  if (ret != Z_OK)
284  return -1;
285 
286  enc_row_size = deflateBound(&s->zstream, row_size);
287  max_packet_size = avctx->height * (enc_row_size +
288  ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
290  if (!pkt->data &&
291  (ret = av_new_packet(pkt, max_packet_size)) < 0) {
292  av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
293  max_packet_size);
294  return ret;
295  }
296 
297  s->bytestream_start =
298  s->bytestream = pkt->data;
299  s->bytestream_end = pkt->data + pkt->size;
300 
301  crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
302  if (!crow_base)
303  goto fail;
304  crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
305  if (is_progressive) {
306  progressive_buf = av_malloc(row_size + 1);
307  if (!progressive_buf)
308  goto fail;
309  }
310  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
311  rgba_buf = av_malloc(row_size + 1);
312  if (!rgba_buf)
313  goto fail;
314  }
315  if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
316  top_buf = av_malloc(row_size + 1);
317  if (!top_buf)
318  goto fail;
319  }
320 
321  /* write png header */
322  memcpy(s->bytestream, ff_pngsig, 8);
323  s->bytestream += 8;
324 
325  AV_WB32(s->buf, avctx->width);
326  AV_WB32(s->buf + 4, avctx->height);
327  s->buf[8] = bit_depth;
328  s->buf[9] = color_type;
329  s->buf[10] = 0; /* compression type */
330  s->buf[11] = 0; /* filter type */
331  s->buf[12] = is_progressive; /* interlace type */
332 
333  png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
334 
335  /* put the palette if needed */
336  if (color_type == PNG_COLOR_TYPE_PALETTE) {
337  int has_alpha, alpha, i;
338  unsigned int v;
339  uint32_t *palette;
340  uint8_t *alpha_ptr;
341 
342  palette = (uint32_t *)p->data[1];
343  ptr = s->buf;
344  alpha_ptr = s->buf + 256 * 3;
345  has_alpha = 0;
346  for(i = 0; i < 256; i++) {
347  v = palette[i];
348  alpha = v >> 24;
349  if (alpha && alpha != 0xff)
350  has_alpha = 1;
351  *alpha_ptr++ = alpha;
352  bytestream_put_be24(&ptr, v);
353  }
354  png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
355  if (has_alpha) {
356  png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
357  }
358  }
359 
360  /* now put each row */
361  s->zstream.avail_out = IOBUF_SIZE;
362  s->zstream.next_out = s->buf;
363  if (is_progressive) {
364  int pass;
365 
366  for(pass = 0; pass < NB_PASSES; pass++) {
367  /* NOTE: a pass is completely omitted if no pixels would be
368  output */
369  pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
370  if (pass_row_size > 0) {
371  top = NULL;
372  for(y = 0; y < avctx->height; y++) {
373  if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
374  ptr = p->data[0] + y * p->linesize[0];
375  FFSWAP(uint8_t*, progressive_buf, top_buf);
376  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
377  convert_from_rgb32(rgba_buf, ptr, avctx->width);
378  ptr = rgba_buf;
379  }
380  png_get_interlaced_row(progressive_buf, pass_row_size,
381  bits_per_pixel, pass,
382  ptr, avctx->width);
383  crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
384  png_write_row(s, crow, pass_row_size + 1);
385  top = progressive_buf;
386  }
387  }
388  }
389  }
390  } else {
391  top = NULL;
392  for(y = 0; y < avctx->height; y++) {
393  ptr = p->data[0] + y * p->linesize[0];
394  if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
395  FFSWAP(uint8_t*, rgba_buf, top_buf);
396  convert_from_rgb32(rgba_buf, ptr, avctx->width);
397  ptr = rgba_buf;
398  }
399  crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
400  png_write_row(s, crow, row_size + 1);
401  top = ptr;
402  }
403  }
404  /* compress last bytes */
405  for(;;) {
406  ret = deflate(&s->zstream, Z_FINISH);
407  if (ret == Z_OK || ret == Z_STREAM_END) {
408  len = IOBUF_SIZE - s->zstream.avail_out;
409  if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
410  png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
411  }
412  s->zstream.avail_out = IOBUF_SIZE;
413  s->zstream.next_out = s->buf;
414  if (ret == Z_STREAM_END)
415  break;
416  } else {
417  goto fail;
418  }
419  }
420  png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
421 
422  pkt->size = s->bytestream - s->bytestream_start;
423  pkt->flags |= AV_PKT_FLAG_KEY;
424  *got_packet = 1;
425  ret = 0;
426 
427  the_end:
428  av_free(crow_base);
429  av_free(progressive_buf);
430  av_free(rgba_buf);
431  av_free(top_buf);
432  deflateEnd(&s->zstream);
433  return ret;
434  fail:
435  ret = -1;
436  goto the_end;
437 }
438 
440  PNGEncContext *s = avctx->priv_data;
441 
442  avctx->coded_frame = av_frame_alloc();
443  if (!avctx->coded_frame)
444  return AVERROR(ENOMEM);
445 
447  avctx->coded_frame->key_frame = 1;
448 
449  ff_dsputil_init(&s->dsp, avctx);
450 
452  if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
454 
455  return 0;
456 }
457 
459 {
460  av_frame_free(&avctx->coded_frame);
461  return 0;
462 }
463 
465  .name = "png",
466  .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
467  .type = AVMEDIA_TYPE_VIDEO,
468  .id = AV_CODEC_ID_PNG,
469  .priv_data_size = sizeof(PNGEncContext),
470  .init = png_enc_init,
471  .close = png_enc_close,
472  .encode2 = encode_frame,
473  .pix_fmts = (const enum AVPixelFormat[]){
477  },
478 };