FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pngdec.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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 //#define DEBUG
23 
24 #include "libavutil/bprint.h"
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "apng.h"
30 #include "png.h"
31 #include "pngdsp.h"
32 #include "thread.h"
33 
34 #include <zlib.h>
35 
36 typedef struct PNGDecContext {
39 
44 
45  int state;
46  int width, height;
47  int cur_w, cur_h;
50  int bit_depth;
55  int channels;
57  int bpp;
58 
59  int frame_id;
62  uint32_t palette[256];
65  unsigned int last_row_size;
67  unsigned int tmp_row_size;
70  int pass;
71  int crow_size; /* compressed row size (include filter type) */
72  int row_size; /* decompressed row size */
73  int pass_row_size; /* decompress row size of the current pass */
74  int y;
75  z_stream zstream;
77 
78 /* Mask to determine which pixels are valid in a pass */
79 static const uint8_t png_pass_mask[NB_PASSES] = {
80  0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
81 };
82 
83 /* Mask to determine which y pixels can be written in a pass */
85  0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
86 };
87 
88 /* Mask to determine which pixels to overwrite while displaying */
90  0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
91 };
92 
93 /* NOTE: we try to construct a good looking image at each pass. width
94  * is the original image width. We also do pixel format conversion at
95  * this stage */
96 static void png_put_interlaced_row(uint8_t *dst, int width,
97  int bits_per_pixel, int pass,
98  int color_type, const uint8_t *src)
99 {
100  int x, mask, dsp_mask, j, src_x, b, bpp;
101  uint8_t *d;
102  const uint8_t *s;
103 
104  mask = png_pass_mask[pass];
105  dsp_mask = png_pass_dsp_mask[pass];
106 
107  switch (bits_per_pixel) {
108  case 1:
109  src_x = 0;
110  for (x = 0; x < width; x++) {
111  j = (x & 7);
112  if ((dsp_mask << j) & 0x80) {
113  b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
114  dst[x >> 3] &= 0xFF7F>>j;
115  dst[x >> 3] |= b << (7 - j);
116  }
117  if ((mask << j) & 0x80)
118  src_x++;
119  }
120  break;
121  case 2:
122  src_x = 0;
123  for (x = 0; x < width; x++) {
124  int j2 = 2 * (x & 3);
125  j = (x & 7);
126  if ((dsp_mask << j) & 0x80) {
127  b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
128  dst[x >> 2] &= 0xFF3F>>j2;
129  dst[x >> 2] |= b << (6 - j2);
130  }
131  if ((mask << j) & 0x80)
132  src_x++;
133  }
134  break;
135  case 4:
136  src_x = 0;
137  for (x = 0; x < width; x++) {
138  int j2 = 4*(x&1);
139  j = (x & 7);
140  if ((dsp_mask << j) & 0x80) {
141  b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
142  dst[x >> 1] &= 0xFF0F>>j2;
143  dst[x >> 1] |= b << (4 - j2);
144  }
145  if ((mask << j) & 0x80)
146  src_x++;
147  }
148  break;
149  default:
150  bpp = bits_per_pixel >> 3;
151  d = dst;
152  s = src;
153  for (x = 0; x < width; x++) {
154  j = x & 7;
155  if ((dsp_mask << j) & 0x80) {
156  memcpy(d, s, bpp);
157  }
158  d += bpp;
159  if ((mask << j) & 0x80)
160  s += bpp;
161  }
162  break;
163  }
164 }
165 
167  int w, int bpp)
168 {
169  int i;
170  for (i = 0; i < w; i++) {
171  int a, b, c, p, pa, pb, pc;
172 
173  a = dst[i - bpp];
174  b = top[i];
175  c = top[i - bpp];
176 
177  p = b - c;
178  pc = a - c;
179 
180  pa = abs(p);
181  pb = abs(pc);
182  pc = abs(p + pc);
183 
184  if (pa <= pb && pa <= pc)
185  p = a;
186  else if (pb <= pc)
187  p = b;
188  else
189  p = c;
190  dst[i] = p + src[i];
191  }
192 }
193 
194 #define UNROLL1(bpp, op) \
195  { \
196  r = dst[0]; \
197  if (bpp >= 2) \
198  g = dst[1]; \
199  if (bpp >= 3) \
200  b = dst[2]; \
201  if (bpp >= 4) \
202  a = dst[3]; \
203  for (; i <= size - bpp; i += bpp) { \
204  dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
205  if (bpp == 1) \
206  continue; \
207  dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
208  if (bpp == 2) \
209  continue; \
210  dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
211  if (bpp == 3) \
212  continue; \
213  dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
214  } \
215  }
216 
217 #define UNROLL_FILTER(op) \
218  if (bpp == 1) { \
219  UNROLL1(1, op) \
220  } else if (bpp == 2) { \
221  UNROLL1(2, op) \
222  } else if (bpp == 3) { \
223  UNROLL1(3, op) \
224  } else if (bpp == 4) { \
225  UNROLL1(4, op) \
226  } \
227  for (; i < size; i++) { \
228  dst[i] = op(dst[i - bpp], src[i], last[i]); \
229  }
230 
231 /* NOTE: 'dst' can be equal to 'last' */
232 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
233  uint8_t *src, uint8_t *last, int size, int bpp)
234 {
235  int i, p, r, g, b, a;
236 
237  switch (filter_type) {
239  memcpy(dst, src, size);
240  break;
242  for (i = 0; i < bpp; i++)
243  dst[i] = src[i];
244  if (bpp == 4) {
245  p = *(int *)dst;
246  for (; i < size; i += bpp) {
247  unsigned s = *(int *)(src + i);
248  p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
249  *(int *)(dst + i) = p;
250  }
251  } else {
252 #define OP_SUB(x, s, l) ((x) + (s))
254  }
255  break;
256  case PNG_FILTER_VALUE_UP:
257  dsp->add_bytes_l2(dst, src, last, size);
258  break;
260  for (i = 0; i < bpp; i++) {
261  p = (last[i] >> 1);
262  dst[i] = p + src[i];
263  }
264 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
266  break;
268  for (i = 0; i < bpp; i++) {
269  p = last[i];
270  dst[i] = p + src[i];
271  }
272  if (bpp > 2 && size > 4) {
273  /* would write off the end of the array if we let it process
274  * the last pixel with bpp=3 */
275  int w = bpp == 4 ? size : size - 3;
276  if (w > i) {
277  dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
278  i = w;
279  }
280  }
281  ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
282  break;
283  }
284 }
285 
286 /* This used to be called "deloco" in FFmpeg
287  * and is actually an inverse reversible colorspace transformation */
288 #define YUV2RGB(NAME, TYPE) \
289 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
290 { \
291  int i; \
292  for (i = 0; i < size; i += 3 + alpha) { \
293  int g = dst [i + 1]; \
294  dst[i + 0] += g; \
295  dst[i + 2] += g; \
296  } \
297 }
298 
299 YUV2RGB(rgb8, uint8_t)
300 YUV2RGB(rgb16, uint16_t)
301 
302 /* process exactly one decompressed row */
304 {
305  uint8_t *ptr, *last_row;
306  int got_line;
307 
308  if (!s->interlace_type) {
309  ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
310  if (s->y == 0)
311  last_row = s->last_row;
312  else
313  last_row = ptr - s->image_linesize;
314 
315  png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
316  last_row, s->row_size, s->bpp);
317  /* loco lags by 1 row so that it doesn't interfere with top prediction */
318  if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
319  if (s->bit_depth == 16) {
320  deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
321  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
322  } else {
323  deloco_rgb8(ptr - s->image_linesize, s->row_size,
324  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
325  }
326  }
327  s->y++;
328  if (s->y == s->cur_h) {
329  s->state |= PNG_ALLIMAGE;
330  if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
331  if (s->bit_depth == 16) {
332  deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
333  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
334  } else {
335  deloco_rgb8(ptr, s->row_size,
336  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
337  }
338  }
339  }
340  } else {
341  got_line = 0;
342  for (;;) {
343  ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
344  if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
345  /* if we already read one row, it is time to stop to
346  * wait for the next one */
347  if (got_line)
348  break;
349  png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
350  s->last_row, s->pass_row_size, s->bpp);
351  FFSWAP(uint8_t *, s->last_row, s->tmp_row);
352  FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
353  got_line = 1;
354  }
355  if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
356  png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
357  s->color_type, s->last_row);
358  }
359  s->y++;
360  if (s->y == s->cur_h) {
361  memset(s->last_row, 0, s->row_size);
362  for (;;) {
363  if (s->pass == NB_PASSES - 1) {
364  s->state |= PNG_ALLIMAGE;
365  goto the_end;
366  } else {
367  s->pass++;
368  s->y = 0;
369  s->pass_row_size = ff_png_pass_row_size(s->pass,
370  s->bits_per_pixel,
371  s->cur_w);
372  s->crow_size = s->pass_row_size + 1;
373  if (s->pass_row_size != 0)
374  break;
375  /* skip pass if empty row */
376  }
377  }
378  }
379  }
380 the_end:;
381  }
382 }
383 
385 {
386  int ret;
387  s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
388  s->zstream.next_in = (unsigned char *)s->gb.buffer;
389  bytestream2_skip(&s->gb, length);
390 
391  /* decode one line if possible */
392  while (s->zstream.avail_in > 0) {
393  ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
394  if (ret != Z_OK && ret != Z_STREAM_END) {
395  av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
396  return AVERROR_EXTERNAL;
397  }
398  if (s->zstream.avail_out == 0) {
399  if (!(s->state & PNG_ALLIMAGE)) {
400  png_handle_row(s);
401  }
402  s->zstream.avail_out = s->crow_size;
403  s->zstream.next_out = s->crow_buf;
404  }
405  if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
406  av_log(NULL, AV_LOG_WARNING,
407  "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
408  return 0;
409  }
410  }
411  return 0;
412 }
413 
414 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
415  const uint8_t *data_end)
416 {
417  z_stream zstream;
418  unsigned char *buf;
419  unsigned buf_size;
420  int ret;
421 
422  zstream.zalloc = ff_png_zalloc;
423  zstream.zfree = ff_png_zfree;
424  zstream.opaque = NULL;
425  if (inflateInit(&zstream) != Z_OK)
426  return AVERROR_EXTERNAL;
427  zstream.next_in = (unsigned char *)data;
428  zstream.avail_in = data_end - data;
429  av_bprint_init(bp, 0, -1);
430 
431  while (zstream.avail_in > 0) {
432  av_bprint_get_buffer(bp, 1, &buf, &buf_size);
433  if (!buf_size) {
434  ret = AVERROR(ENOMEM);
435  goto fail;
436  }
437  zstream.next_out = buf;
438  zstream.avail_out = buf_size;
439  ret = inflate(&zstream, Z_PARTIAL_FLUSH);
440  if (ret != Z_OK && ret != Z_STREAM_END) {
441  ret = AVERROR_EXTERNAL;
442  goto fail;
443  }
444  bp->len += zstream.next_out - buf;
445  if (ret == Z_STREAM_END)
446  break;
447  }
448  inflateEnd(&zstream);
449  bp->str[bp->len] = 0;
450  return 0;
451 
452 fail:
453  inflateEnd(&zstream);
454  av_bprint_finalize(bp, NULL);
455  return ret;
456 }
457 
458 static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
459 {
460  size_t extra = 0, i;
461  uint8_t *out, *q;
462 
463  for (i = 0; i < size_in; i++)
464  extra += in[i] >= 0x80;
465  if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
466  return NULL;
467  q = out = av_malloc(size_in + extra + 1);
468  if (!out)
469  return NULL;
470  for (i = 0; i < size_in; i++) {
471  if (in[i] >= 0x80) {
472  *(q++) = 0xC0 | (in[i] >> 6);
473  *(q++) = 0x80 | (in[i] & 0x3F);
474  } else {
475  *(q++) = in[i];
476  }
477  }
478  *(q++) = 0;
479  return out;
480 }
481 
482 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
483  AVDictionary **dict)
484 {
485  int ret, method;
486  const uint8_t *data = s->gb.buffer;
487  const uint8_t *data_end = data + length;
488  const uint8_t *keyword = data;
489  const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
490  uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
491  unsigned text_len;
492  AVBPrint bp;
493 
494  if (!keyword_end)
495  return AVERROR_INVALIDDATA;
496  data = keyword_end + 1;
497 
498  if (compressed) {
499  if (data == data_end)
500  return AVERROR_INVALIDDATA;
501  method = *(data++);
502  if (method)
503  return AVERROR_INVALIDDATA;
504  if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
505  return ret;
506  text_len = bp.len;
507  av_bprint_finalize(&bp, (char **)&text);
508  if (!text)
509  return AVERROR(ENOMEM);
510  } else {
511  text = (uint8_t *)data;
512  text_len = data_end - text;
513  }
514 
515  kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
516  txt_utf8 = iso88591_to_utf8(text, text_len);
517  if (text != data)
518  av_free(text);
519  if (!(kw_utf8 && txt_utf8)) {
520  av_free(kw_utf8);
521  av_free(txt_utf8);
522  return AVERROR(ENOMEM);
523  }
524 
525  av_dict_set(dict, kw_utf8, txt_utf8,
527  return 0;
528 }
529 
531  uint32_t length)
532 {
533  if (length != 13)
534  return AVERROR_INVALIDDATA;
535 
536  if (s->state & PNG_IDAT) {
537  av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
538  return AVERROR_INVALIDDATA;
539  }
540 
541  s->width = s->cur_w = bytestream2_get_be32(&s->gb);
542  s->height = s->cur_h = bytestream2_get_be32(&s->gb);
543  if (av_image_check_size(s->width, s->height, 0, avctx)) {
544  s->width = s->height = 0;
545  av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
546  return AVERROR_INVALIDDATA;
547  }
548  s->bit_depth = bytestream2_get_byte(&s->gb);
549  s->color_type = bytestream2_get_byte(&s->gb);
550  s->compression_type = bytestream2_get_byte(&s->gb);
551  s->filter_type = bytestream2_get_byte(&s->gb);
552  s->interlace_type = bytestream2_get_byte(&s->gb);
553  bytestream2_skip(&s->gb, 4); /* crc */
554  s->state |= PNG_IHDR;
555  if (avctx->debug & FF_DEBUG_PICT_INFO)
556  av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
557  "compression_type=%d filter_type=%d interlace_type=%d\n",
558  s->width, s->height, s->bit_depth, s->color_type,
560 
561  return 0;
562 }
563 
565 {
566  if (s->state & PNG_IDAT) {
567  av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
568  return AVERROR_INVALIDDATA;
569  }
570  avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
571  avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
572  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
573  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
574  bytestream2_skip(&s->gb, 1); /* unit specifier */
575  bytestream2_skip(&s->gb, 4); /* crc */
576 
577  return 0;
578 }
579 
581  uint32_t length, AVFrame *p)
582 {
583  int ret;
584 
585  if (!(s->state & PNG_IHDR)) {
586  av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
587  return AVERROR_INVALIDDATA;
588  }
589  if (!(s->state & PNG_IDAT)) {
590  /* init image info */
591  avctx->width = s->width;
592  avctx->height = s->height;
593 
595  s->bits_per_pixel = s->bit_depth * s->channels;
596  s->bpp = (s->bits_per_pixel + 7) >> 3;
597  s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
598 
599  if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
601  avctx->pix_fmt = AV_PIX_FMT_RGB24;
602  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
604  avctx->pix_fmt = AV_PIX_FMT_RGBA;
605  } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
607  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
608  } else if (s->bit_depth == 16 &&
610  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
611  } else if (s->bit_depth == 16 &&
613  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
614  } else if (s->bit_depth == 16 &&
616  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
617  } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
619  avctx->pix_fmt = AV_PIX_FMT_PAL8;
620  } else if (s->bit_depth == 1 && s->bits_per_pixel == 1) {
621  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
622  } else if (s->bit_depth == 8 &&
624  avctx->pix_fmt = AV_PIX_FMT_YA8;
625  } else if (s->bit_depth == 16 &&
627  avctx->pix_fmt = AV_PIX_FMT_YA16BE;
628  } else {
629  av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
630  "and color type %d\n",
631  s->bit_depth, s->color_type);
632  return AVERROR_INVALIDDATA;
633  }
634 
635  if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
636  return ret;
637  ff_thread_finish_setup(avctx);
638 
640  p->key_frame = 1;
642 
643  /* compute the compressed row size */
644  if (!s->interlace_type) {
645  s->crow_size = s->row_size + 1;
646  } else {
647  s->pass = 0;
649  s->bits_per_pixel,
650  s->cur_w);
651  s->crow_size = s->pass_row_size + 1;
652  }
653  av_dlog(avctx, "row_size=%d crow_size =%d\n",
654  s->row_size, s->crow_size);
655  s->image_buf = p->data[0];
656  s->image_linesize = p->linesize[0];
657  /* copy the palette if needed */
658  if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
659  memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
660  /* empty row is used if differencing to the first row */
662  if (!s->last_row)
663  return AVERROR_INVALIDDATA;
664  if (s->interlace_type ||
667  if (!s->tmp_row)
668  return AVERROR_INVALIDDATA;
669  }
670  /* compressed row */
672  if (!s->buffer)
673  return AVERROR(ENOMEM);
674 
675  /* we want crow_buf+1 to be 16-byte aligned */
676  s->crow_buf = s->buffer + 15;
677  s->zstream.avail_out = s->crow_size;
678  s->zstream.next_out = s->crow_buf;
679  }
680  s->state |= PNG_IDAT;
681  if ((ret = png_decode_idat(s, length)) < 0)
682  return ret;
683  bytestream2_skip(&s->gb, 4); /* crc */
684 
685  return 0;
686 }
687 
689  uint32_t length)
690 {
691  int n, i, r, g, b;
692 
693  if ((length % 3) != 0 || length > 256 * 3)
694  return AVERROR_INVALIDDATA;
695  /* read the palette */
696  n = length / 3;
697  for (i = 0; i < n; i++) {
698  r = bytestream2_get_byte(&s->gb);
699  g = bytestream2_get_byte(&s->gb);
700  b = bytestream2_get_byte(&s->gb);
701  s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
702  }
703  for (; i < 256; i++)
704  s->palette[i] = (0xFFU << 24);
705  s->state |= PNG_PLTE;
706  bytestream2_skip(&s->gb, 4); /* crc */
707 
708  return 0;
709 }
710 
712  uint32_t length)
713 {
714  int v, i;
715 
716  /* read the transparency. XXX: Only palette mode supported */
718  length > 256 ||
719  !(s->state & PNG_PLTE))
720  return AVERROR_INVALIDDATA;
721  for (i = 0; i < length; i++) {
722  v = bytestream2_get_byte(&s->gb);
723  s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
724  }
725  bytestream2_skip(&s->gb, 4); /* crc */
726 
727  return 0;
728 }
729 
731 {
732  if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
733  int i, j, k;
734  uint8_t *pd = p->data[0];
735  for (j = 0; j < s->height; j++) {
736  i = s->width / 8;
737  for (k = 7; k >= 1; k--)
738  if ((s->width&7) >= k)
739  pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
740  for (i--; i >= 0; i--) {
741  pd[8*i + 7]= pd[i] & 1;
742  pd[8*i + 6]= (pd[i]>>1) & 1;
743  pd[8*i + 5]= (pd[i]>>2) & 1;
744  pd[8*i + 4]= (pd[i]>>3) & 1;
745  pd[8*i + 3]= (pd[i]>>4) & 1;
746  pd[8*i + 2]= (pd[i]>>5) & 1;
747  pd[8*i + 1]= (pd[i]>>6) & 1;
748  pd[8*i + 0]= pd[i]>>7;
749  }
750  pd += s->image_linesize;
751  }
752  } else if (s->bits_per_pixel == 2) {
753  int i, j;
754  uint8_t *pd = p->data[0];
755  for (j = 0; j < s->height; j++) {
756  i = s->width / 4;
758  if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
759  if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
760  if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
761  for (i--; i >= 0; i--) {
762  pd[4*i + 3]= pd[i] & 3;
763  pd[4*i + 2]= (pd[i]>>2) & 3;
764  pd[4*i + 1]= (pd[i]>>4) & 3;
765  pd[4*i + 0]= pd[i]>>6;
766  }
767  } else {
768  if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
769  if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
770  if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
771  for (i--; i >= 0; i--) {
772  pd[4*i + 3]= ( pd[i] & 3)*0x55;
773  pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
774  pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
775  pd[4*i + 0]= ( pd[i]>>6 )*0x55;
776  }
777  }
778  pd += s->image_linesize;
779  }
780  } else if (s->bits_per_pixel == 4) {
781  int i, j;
782  uint8_t *pd = p->data[0];
783  for (j = 0; j < s->height; j++) {
784  i = s->width/2;
786  if (s->width&1) pd[2*i+0]= pd[i]>>4;
787  for (i--; i >= 0; i--) {
788  pd[2*i + 1] = pd[i] & 15;
789  pd[2*i + 0] = pd[i] >> 4;
790  }
791  } else {
792  if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
793  for (i--; i >= 0; i--) {
794  pd[2*i + 1] = (pd[i] & 15) * 0x11;
795  pd[2*i + 0] = (pd[i] >> 4) * 0x11;
796  }
797  }
798  pd += s->image_linesize;
799  }
800  }
801 }
802 
804  uint32_t length)
805 {
806  uint32_t sequence_number;
807 
808  if (length != 26)
809  return AVERROR_INVALIDDATA;
810 
811  sequence_number = bytestream2_get_be32(&s->gb);
812  s->cur_w = bytestream2_get_be32(&s->gb);
813  s->cur_h = bytestream2_get_be32(&s->gb);
814  s->x_offset = bytestream2_get_be32(&s->gb);
815  s->y_offset = bytestream2_get_be32(&s->gb);
816  bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
817  s->dispose_op = bytestream2_get_byte(&s->gb);
818  s->blend_op = bytestream2_get_byte(&s->gb);
819  bytestream2_skip(&s->gb, 4); /* crc */
820 
821  if (sequence_number == 0 &&
822  (s->cur_w != s->width ||
823  s->cur_h != s->height ||
824  s->x_offset != 0 ||
825  s->y_offset != 0) ||
826  s->cur_w <= 0 || s->cur_h <= 0 ||
827  s->x_offset < 0 || s->y_offset < 0 ||
828  s->cur_w > s->width - s->x_offset|| s->cur_h > s->height - s->y_offset)
829  return AVERROR_INVALIDDATA;
830 
831  /* always (re)start with a clean frame */
832  if (sequence_number == 0) {
834  s->frame_id = 0;
835  } else {
836  s->frame_id++;
837  if (s->frame_id == 1 && s->dispose_op == APNG_DISPOSE_OP_PREVIOUS)
838  /* previous for the second frame is the first frame */
840  }
841 
842  return 0;
843 }
844 
846 {
847  int i, j;
848  uint8_t *pd = p->data[0];
849  uint8_t *pd_last = s->last_picture.f->data[0];
850  int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
851 
852  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
853  for (j = 0; j < s->height; j++) {
854  for (i = 0; i < ls; i++)
855  pd[i] += pd_last[i];
856  pd += s->image_linesize;
857  pd_last += s->image_linesize;
858  }
859 }
860 
861 // divide by 255 and round to nearest
862 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
863 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
864 
866  AVFrame *p)
867 {
868  int i, j;
869  uint8_t *pd = p->data[0];
870  uint8_t *pd_last = s->last_picture.f->data[0];
871  uint8_t *pd_last_region = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
872  s->previous_picture.f->data[0] : s->last_picture.f->data[0];
873  int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
874 
875  if (s->blend_op == APNG_BLEND_OP_OVER &&
876  avctx->pix_fmt != AV_PIX_FMT_RGBA && avctx->pix_fmt != AV_PIX_FMT_ARGB) {
877  avpriv_request_sample(avctx, "Blending with pixel format %s",
878  av_get_pix_fmt_name(avctx->pix_fmt));
879  return AVERROR_PATCHWELCOME;
880  }
881 
882  ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
885 
886  for (j = 0; j < s->y_offset; j++) {
887  for (i = 0; i < ls; i++)
888  pd[i] = pd_last[i];
889  pd += s->image_linesize;
890  pd_last += s->image_linesize;
891  }
892 
894  uint8_t ri, gi, bi, ai;
895 
896  pd_last_region += s->y_offset * s->image_linesize;
897  if (avctx->pix_fmt == AV_PIX_FMT_RGBA) {
898  ri = 0;
899  gi = 1;
900  bi = 2;
901  ai = 3;
902  } else {
903  ri = 3;
904  gi = 2;
905  bi = 1;
906  ai = 0;
907  }
908 
909  for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
910  for (i = 0; i < s->x_offset * s->bpp; i++)
911  pd[i] = pd_last[i];
912  for (; i < (s->x_offset + s->cur_w) * s->bpp; i += s->bpp) {
913  uint8_t alpha = pd[i+ai];
914 
915  /* output = alpha * foreground + (1-alpha) * background */
916  switch (alpha) {
917  case 0:
918  pd[i+ri] = pd_last_region[i+ri];
919  pd[i+gi] = pd_last_region[i+gi];
920  pd[i+bi] = pd_last_region[i+bi];
921  pd[i+ai] = 0xff;
922  break;
923  case 255:
924  break;
925  default:
926  pd[i+ri] = FAST_DIV255(alpha * pd[i+ri] + (255 - alpha) * pd_last_region[i+ri]);
927  pd[i+gi] = FAST_DIV255(alpha * pd[i+gi] + (255 - alpha) * pd_last_region[i+gi]);
928  pd[i+bi] = FAST_DIV255(alpha * pd[i+bi] + (255 - alpha) * pd_last_region[i+bi]);
929  pd[i+ai] = 0xff;
930  break;
931  }
932  }
933  for (; i < ls; i++)
934  pd[i] = pd_last[i];
935  pd += s->image_linesize;
936  pd_last += s->image_linesize;
937  pd_last_region += s->image_linesize;
938  }
939  } else {
940  for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
941  for (i = 0; i < s->x_offset * s->bpp; i++)
942  pd[i] = pd_last[i];
943  for (i = (s->x_offset + s->cur_w) * s->bpp; i < ls; i++)
944  pd[i] = pd_last[i];
945  pd += s->image_linesize;
946  pd_last += s->image_linesize;
947  }
948  }
949 
950  for (j = s->y_offset + s->cur_h; j < s->height; j++) {
951  for (i = 0; i < ls; i++)
952  pd[i] = pd_last[i];
953  pd += s->image_linesize;
954  pd_last += s->image_linesize;
955  }
956 
957  return 0;
958 }
959 
961  AVFrame *p, AVPacket *avpkt)
962 {
963  AVDictionary *metadata = NULL;
964  uint32_t tag, length;
965  int decode_next_dat = 0;
966  int ret = AVERROR_INVALIDDATA;
967  AVFrame *ref;
968 
969  for (;;) {
970  length = bytestream2_get_bytes_left(&s->gb);
971  if (length <= 0) {
972  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
973  if (!(s->state & PNG_IDAT))
974  return 0;
975  else
976  goto exit_loop;
977  }
978  av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
979  if ( s->state & PNG_ALLIMAGE
981  goto exit_loop;
982  goto fail;
983  }
984 
985  length = bytestream2_get_be32(&s->gb);
986  if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
987  av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
988  goto fail;
989  }
990  tag = bytestream2_get_le32(&s->gb);
991  if (avctx->debug & FF_DEBUG_STARTCODE)
992  av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
993  (tag & 0xff),
994  ((tag >> 8) & 0xff),
995  ((tag >> 16) & 0xff),
996  ((tag >> 24) & 0xff), length);
997  switch (tag) {
998  case MKTAG('I', 'H', 'D', 'R'):
999  if (decode_ihdr_chunk(avctx, s, length) < 0)
1000  goto fail;
1001  break;
1002  case MKTAG('p', 'H', 'Y', 's'):
1003  if (decode_phys_chunk(avctx, s) < 0)
1004  goto fail;
1005  break;
1006  case MKTAG('f', 'c', 'T', 'L'):
1007  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1008  goto skip_tag;
1009  if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1010  goto fail;
1011  decode_next_dat = 1;
1012  break;
1013  case MKTAG('f', 'd', 'A', 'T'):
1014  if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1015  goto skip_tag;
1016  if (!decode_next_dat)
1017  goto fail;
1018  bytestream2_get_be32(&s->gb);
1019  length -= 4;
1020  /* fallthrough */
1021  case MKTAG('I', 'D', 'A', 'T'):
1022  if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1023  goto skip_tag;
1024  if (decode_idat_chunk(avctx, s, length, p) < 0)
1025  goto fail;
1026  break;
1027  case MKTAG('P', 'L', 'T', 'E'):
1028  if (decode_plte_chunk(avctx, s, length) < 0)
1029  goto skip_tag;
1030  break;
1031  case MKTAG('t', 'R', 'N', 'S'):
1032  if (decode_trns_chunk(avctx, s, length) < 0)
1033  goto skip_tag;
1034  break;
1035  case MKTAG('t', 'E', 'X', 't'):
1036  if (decode_text_chunk(s, length, 0, &metadata) < 0)
1037  av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1038  bytestream2_skip(&s->gb, length + 4);
1039  break;
1040  case MKTAG('z', 'T', 'X', 't'):
1041  if (decode_text_chunk(s, length, 1, &metadata) < 0)
1042  av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1043  bytestream2_skip(&s->gb, length + 4);
1044  break;
1045  case MKTAG('I', 'E', 'N', 'D'):
1046  if (!(s->state & PNG_ALLIMAGE))
1047  av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
1048  if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1049  goto fail;
1050  }
1051  bytestream2_skip(&s->gb, 4); /* crc */
1052  goto exit_loop;
1053  default:
1054  /* skip tag */
1055 skip_tag:
1056  bytestream2_skip(&s->gb, length + 4);
1057  break;
1058  }
1059  }
1060 exit_loop:
1061 
1062  if (s->bits_per_pixel <= 4)
1063  handle_small_bpp(s, p);
1064 
1065  /* handle p-frames only if a predecessor frame is available */
1066  ref = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
1067  s->previous_picture.f : s->last_picture.f;
1068  if (ref->data[0]) {
1069  if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
1070  && ref->width == p->width
1071  && ref->height== p->height
1072  && ref->format== p->format
1073  ) {
1074  if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
1075  handle_p_frame_png(s, p);
1076  else if (CONFIG_APNG_DECODER &&
1077  avctx->codec_id == AV_CODEC_ID_APNG &&
1078  (ret = handle_p_frame_apng(avctx, s, p)) < 0)
1079  goto fail;
1080  }
1081  }
1082  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1083 
1084  av_frame_set_metadata(p, metadata);
1085  metadata = NULL;
1086  return 0;
1087 
1088 fail:
1089  av_dict_free(&metadata);
1090  ff_thread_report_progress(&s->picture, INT_MAX, 0);
1091  return ret;
1092 }
1093 
1094 #if CONFIG_PNG_DECODER
1095 static int decode_frame_png(AVCodecContext *avctx,
1096  void *data, int *got_frame,
1097  AVPacket *avpkt)
1098 {
1099  PNGDecContext *const s = avctx->priv_data;
1100  const uint8_t *buf = avpkt->data;
1101  int buf_size = avpkt->size;
1102  AVFrame *p;
1103  int64_t sig;
1104  int ret;
1105 
1108  p = s->picture.f;
1109 
1110  bytestream2_init(&s->gb, buf, buf_size);
1111 
1112  /* check signature */
1113  sig = bytestream2_get_be64(&s->gb);
1114  if (sig != PNGSIG &&
1115  sig != MNGSIG) {
1116  av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
1117  return AVERROR_INVALIDDATA;
1118  }
1119 
1120  s->y = s->state = 0;
1121 
1122  /* init the zlib */
1123  s->zstream.zalloc = ff_png_zalloc;
1124  s->zstream.zfree = ff_png_zfree;
1125  s->zstream.opaque = NULL;
1126  ret = inflateInit(&s->zstream);
1127  if (ret != Z_OK) {
1128  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1129  return AVERROR_EXTERNAL;
1130  }
1131 
1132  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1133  goto the_end;
1134 
1135  if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1136  return ret;
1137 
1138  *got_frame = 1;
1139 
1140  ret = bytestream2_tell(&s->gb);
1141 the_end:
1142  inflateEnd(&s->zstream);
1143  s->crow_buf = NULL;
1144  return ret;
1145 }
1146 #endif
1147 
1148 #if CONFIG_APNG_DECODER
1149 static int decode_frame_apng(AVCodecContext *avctx,
1150  void *data, int *got_frame,
1151  AVPacket *avpkt)
1152 {
1153  PNGDecContext *const s = avctx->priv_data;
1154  int ret;
1155  AVFrame *p;
1156  ThreadFrame tmp;
1157 
1159  tmp = s->previous_picture;
1161  s->last_picture = s->picture;
1162  s->picture = tmp;
1163  p = s->picture.f;
1164 
1165  if (!(s->state & PNG_IHDR)) {
1166  if (!avctx->extradata_size)
1167  return AVERROR_INVALIDDATA;
1168 
1169  /* only init fields, there is no zlib use in extradata */
1170  s->zstream.zalloc = ff_png_zalloc;
1171  s->zstream.zfree = ff_png_zfree;
1172 
1173  bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
1174  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1175  goto end;
1176  }
1177 
1178  /* reset state for a new frame */
1179  if ((ret = inflateInit(&s->zstream)) != Z_OK) {
1180  av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
1181  ret = AVERROR_EXTERNAL;
1182  goto end;
1183  }
1184  s->y = 0;
1185  s->state &= ~(PNG_IDAT | PNG_ALLIMAGE);
1186  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1187  if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
1188  goto end;
1189 
1190  if (!(s->state & PNG_ALLIMAGE))
1191  av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
1192  if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
1193  ret = AVERROR_INVALIDDATA;
1194  goto end;
1195  }
1196  if ((ret = av_frame_ref(data, s->picture.f)) < 0)
1197  goto end;
1198 
1199  *got_frame = 1;
1200  ret = bytestream2_tell(&s->gb);
1201 
1202 end:
1203  inflateEnd(&s->zstream);
1204  return ret;
1205 }
1206 #endif
1207 
1209 {
1210  PNGDecContext *psrc = src->priv_data;
1211  PNGDecContext *pdst = dst->priv_data;
1212  int ret;
1213 
1214  if (dst == src)
1215  return 0;
1216 
1217  pdst->frame_id = psrc->frame_id;
1218 
1219  ff_thread_release_buffer(dst, &pdst->picture);
1220  if (psrc->picture.f->data[0] &&
1221  (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
1222  return ret;
1223  if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
1225  if (psrc->last_picture.f->data[0])
1226  return ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture);
1227  }
1228 
1229  return 0;
1230 }
1231 
1233 {
1234  PNGDecContext *s = avctx->priv_data;
1235 
1236  s->avctx = avctx;
1238  s->last_picture.f = av_frame_alloc();
1239  s->picture.f = av_frame_alloc();
1240  if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
1243  av_frame_free(&s->picture.f);
1244  return AVERROR(ENOMEM);
1245  }
1246 
1247  if (!avctx->internal->is_copy) {
1248  avctx->internal->allocate_progress = 1;
1249  ff_pngdsp_init(&s->dsp);
1250  }
1251 
1252  return 0;
1253 }
1254 
1256 {
1257  PNGDecContext *s = avctx->priv_data;
1258 
1263  ff_thread_release_buffer(avctx, &s->picture);
1264  av_frame_free(&s->picture.f);
1265  av_freep(&s->buffer);
1266  s->buffer_size = 0;
1267  av_freep(&s->last_row);
1268  s->last_row_size = 0;
1269  av_freep(&s->tmp_row);
1270  s->tmp_row_size = 0;
1271 
1272  return 0;
1273 }
1274 
1275 #if CONFIG_APNG_DECODER
1276 AVCodec ff_apng_decoder = {
1277  .name = "apng",
1278  .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
1279  .type = AVMEDIA_TYPE_VIDEO,
1280  .id = AV_CODEC_ID_APNG,
1281  .priv_data_size = sizeof(PNGDecContext),
1282  .init = png_dec_init,
1283  .close = png_dec_end,
1284  .decode = decode_frame_apng,
1286  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1287  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
1288 };
1289 #endif
1290 
1291 #if CONFIG_PNG_DECODER
1292 AVCodec ff_png_decoder = {
1293  .name = "png",
1294  .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
1295  .type = AVMEDIA_TYPE_VIDEO,
1296  .id = AV_CODEC_ID_PNG,
1297  .priv_data_size = sizeof(PNGDecContext),
1298  .init = png_dec_init,
1299  .close = png_dec_end,
1300  .decode = decode_frame_png,
1302  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1303  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
1304 };
1305 #endif