png: Be more informative regarding signature errors
[libav.git] / libavcodec / pngdec.c
CommitLineData
b958a7f1
MR
1/*
2 * PNG image format
406792e7 3 * Copyright (c) 2003 Fabrice Bellard
b958a7f1 4 *
2912e87a 5 * This file is part of Libav.
b958a7f1 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
b958a7f1
MR
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
b958a7f1
MR
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
b958a7f1
MR
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
4fee11ab
LB
21
22#include "libavutil/avstring.h"
737eb597 23#include "libavutil/imgutils.h"
b958a7f1
MR
24#include "avcodec.h"
25#include "bytestream.h"
594d4d5d 26#include "internal.h"
b958a7f1 27#include "png.h"
e9200351 28#include "pngdsp.h"
b958a7f1
MR
29
30/* TODO:
31 * - add 2, 4 and 16 bit depth support
32 */
33
34#include <zlib.h>
35
e0eddd12 36typedef struct PNGDecContext {
e9200351 37 PNGDSPContext dsp;
4a9ca0a2 38
4c25269c 39 GetByteContext gb;
759001c5 40 AVFrame *prev;
e0eddd12
MR
41
42 int state;
43 int width, height;
44 int bit_depth;
45 int color_type;
46 int compression_type;
47 int interlace_type;
48 int filter_type;
49 int channels;
50 int bits_per_pixel;
51 int bpp;
52
53 uint8_t *image_buf;
54 int image_linesize;
55 uint32_t palette[256];
56 uint8_t *crow_buf;
57 uint8_t *last_row;
58 uint8_t *tmp_row;
59 int pass;
60 int crow_size; /* compressed row size (include filter type) */
61 int row_size; /* decompressed row size */
62 int pass_row_size; /* decompress row size of the current pass */
63 int y;
64 z_stream zstream;
65} PNGDecContext;
66
b958a7f1
MR
67/* Mask to determine which y pixels can be written in a pass */
68static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
69 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
70};
71
72/* Mask to determine which pixels to overwrite while displaying */
73static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
74 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
75};
76
77/* NOTE: we try to construct a good looking image at each pass. width
c598b569
VG
78 * is the original image width. We also do pixel format conversion at
79 * this stage */
b958a7f1
MR
80static void png_put_interlaced_row(uint8_t *dst, int width,
81 int bits_per_pixel, int pass,
82 int color_type, const uint8_t *src)
83{
84 int x, mask, dsp_mask, j, src_x, b, bpp;
85 uint8_t *d;
86 const uint8_t *s;
87
9221c0af 88 mask = ff_png_pass_mask[pass];
b958a7f1 89 dsp_mask = png_pass_dsp_mask[pass];
9221c0af
AK
90
91 switch (bits_per_pixel) {
b958a7f1
MR
92 case 1:
93 /* we must initialize the line to zero before writing to it */
94 if (pass == 0)
95 memset(dst, 0, (width + 7) >> 3);
96 src_x = 0;
9221c0af 97 for (x = 0; x < width; x++) {
b958a7f1
MR
98 j = (x & 7);
99 if ((dsp_mask << j) & 0x80) {
100 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
101 dst[x >> 3] |= b << (7 - j);
102 }
103 if ((mask << j) & 0x80)
104 src_x++;
105 }
106 break;
107 default:
108 bpp = bits_per_pixel >> 3;
9221c0af
AK
109 d = dst;
110 s = src;
b958a7f1 111 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
9221c0af 112 for (x = 0; x < width; x++) {
b958a7f1
MR
113 j = x & 7;
114 if ((dsp_mask << j) & 0x80) {
115 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
116 }
117 d += bpp;
118 if ((mask << j) & 0x80)
119 s += bpp;
120 }
121 } else {
c598b569 122 for (x = 0; x < width; x++) {
b958a7f1
MR
123 j = x & 7;
124 if ((dsp_mask << j) & 0x80) {
125 memcpy(d, s, bpp);
126 }
127 d += bpp;
128 if ((mask << j) & 0x80)
129 s += bpp;
130 }
131 }
132 break;
133 }
134}
135
c598b569
VG
136void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
137 int w, int bpp)
4a9ca0a2
LM
138{
139 int i;
9221c0af 140 for (i = 0; i < w; i++) {
4a9ca0a2
LM
141 int a, b, c, p, pa, pb, pc;
142
143 a = dst[i - bpp];
144 b = top[i];
145 c = top[i - bpp];
146
9221c0af 147 p = b - c;
4a9ca0a2
LM
148 pc = a - c;
149
150 pa = abs(p);
151 pb = abs(pc);
152 pc = abs(p + pc);
153
154 if (pa <= pb && pa <= pc)
155 p = a;
156 else if (pb <= pc)
157 p = b;
158 else
159 p = c;
160 dst[i] = p + src[i];
161 }
162}
163
92a36a6b
VG
164#define UNROLL1(bpp, op) \
165 { \
c598b569
VG
166 r = dst[0]; \
167 if (bpp >= 2) \
168 g = dst[1]; \
169 if (bpp >= 3) \
170 b = dst[2]; \
171 if (bpp >= 4) \
172 a = dst[3]; \
173 for (; i < size; i += bpp) { \
174 dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
175 if (bpp == 1) \
176 continue; \
177 dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
178 if (bpp == 2) \
179 continue; \
180 dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
181 if (bpp == 3) \
182 continue; \
183 dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
184 } \
92a36a6b 185 }
4a9ca0a2 186
c598b569 187#define UNROLL_FILTER(op) \
92a36a6b 188 if (bpp == 1) { \
c598b569 189 UNROLL1(1, op) \
92a36a6b
VG
190 } else if (bpp == 2) { \
191 UNROLL1(2, op) \
192 } else if (bpp == 3) { \
193 UNROLL1(3, op) \
194 } else if (bpp == 4) { \
195 UNROLL1(4, op) \
196 } else { \
197 for (; i < size; i += bpp) { \
198 int j; \
199 for (j = 0; j < bpp; j++) \
200 dst[i + j] = op(dst[i + j - bpp], src[i + j], last[i + j]); \
201 } \
202 }
4a9ca0a2 203
b958a7f1 204/* NOTE: 'dst' can be equal to 'last' */
e9200351 205static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
b958a7f1
MR
206 uint8_t *src, uint8_t *last, int size, int bpp)
207{
4a9ca0a2 208 int i, p, r, g, b, a;
b958a7f1 209
9221c0af 210 switch (filter_type) {
b958a7f1
MR
211 case PNG_FILTER_VALUE_NONE:
212 memcpy(dst, src, size);
213 break;
214 case PNG_FILTER_VALUE_SUB:
c598b569 215 for (i = 0; i < bpp; i++)
b958a7f1 216 dst[i] = src[i];
9221c0af 217 if (bpp == 4) {
c598b569 218 p = *(int *)dst;
9221c0af 219 for (; i < size; i += bpp) {
c598b569 220 int s = *(int *)(src + i);
9221c0af 221 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
c598b569 222 *(int *)(dst + i) = p;
4a9ca0a2
LM
223 }
224 } else {
c598b569 225#define OP_SUB(x, s, l) x + s
4a9ca0a2 226 UNROLL_FILTER(OP_SUB);
b958a7f1
MR
227 }
228 break;
229 case PNG_FILTER_VALUE_UP:
4a9ca0a2 230 dsp->add_bytes_l2(dst, src, last, size);
b958a7f1
MR
231 break;
232 case PNG_FILTER_VALUE_AVG:
9221c0af 233 for (i = 0; i < bpp; i++) {
c598b569 234 p = (last[i] >> 1);
b958a7f1
MR
235 dst[i] = p + src[i];
236 }
c598b569 237#define OP_AVG(x, s, l) (((x + l) >> 1) + s) & 0xff
4a9ca0a2 238 UNROLL_FILTER(OP_AVG);
b958a7f1
MR
239 break;
240 case PNG_FILTER_VALUE_PAETH:
9221c0af 241 for (i = 0; i < bpp; i++) {
c598b569 242 p = last[i];
b958a7f1
MR
243 dst[i] = p + src[i];
244 }
2257165b 245 if (bpp > 2 && size > 4) {
c598b569
VG
246 /* would write off the end of the array if we let it process
247 * the last pixel with bpp=3 */
9221c0af
AK
248 int w = bpp == 4 ? size : size - 3;
249 dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
4a9ca0a2 250 i = w;
b958a7f1 251 }
9221c0af 252 ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
b958a7f1
MR
253 break;
254 }
255}
256
9221c0af
AK
257static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
258 const uint8_t *src,
259 int width, int loco)
b958a7f1
MR
260{
261 int j;
262 unsigned int r, g, b, a;
263
9221c0af 264 for (j = 0; j < width; j++) {
b958a7f1
MR
265 r = src[0];
266 g = src[1];
267 b = src[2];
268 a = src[3];
9221c0af
AK
269 if (loco) {
270 r = (r + g) & 0xff;
271 b = (b + g) & 0xff;
042e0add 272 }
c598b569 273 *(uint32_t *) dst = (a << 24) | (r << 16) | (g << 8) | b;
b958a7f1
MR
274 dst += 4;
275 src += 4;
276 }
277}
278
c598b569
VG
279static void convert_to_rgb32(uint8_t *dst, const uint8_t *src,
280 int width, int loco)
042e0add 281{
9221c0af 282 if (loco)
042e0add
LM
283 convert_to_rgb32_loco(dst, src, width, 1);
284 else
285 convert_to_rgb32_loco(dst, src, width, 0);
286}
287
288static void deloco_rgb24(uint8_t *dst, int size)
289{
290 int i;
9221c0af
AK
291 for (i = 0; i < size; i += 3) {
292 int g = dst[i + 1];
293 dst[i + 0] += g;
294 dst[i + 2] += g;
042e0add
LM
295 }
296}
297
b958a7f1 298/* process exactly one decompressed row */
e0eddd12 299static void png_handle_row(PNGDecContext *s)
b958a7f1
MR
300{
301 uint8_t *ptr, *last_row;
302 int got_line;
303
304 if (!s->interlace_type) {
305 ptr = s->image_buf + s->image_linesize * s->y;
306 /* need to swap bytes correctly for RGB_ALPHA */
307 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
4a9ca0a2 308 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
b958a7f1 309 s->last_row, s->row_size, s->bpp);
c598b569
VG
310 convert_to_rgb32(ptr, s->tmp_row, s->width,
311 s->filter_type == PNG_FILTER_TYPE_LOCO);
312 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
b958a7f1
MR
313 } else {
314 /* in normal case, we avoid one copy */
315 if (s->y == 0)
316 last_row = s->last_row;
317 else
318 last_row = ptr - s->image_linesize;
319
4a9ca0a2 320 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
b958a7f1
MR
321 last_row, s->row_size, s->bpp);
322 }
042e0add
LM
323 /* loco lags by 1 row so that it doesn't interfere with top prediction */
324 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
325 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
326 deloco_rgb24(ptr - s->image_linesize, s->row_size);
b958a7f1
MR
327 s->y++;
328 if (s->y == s->height) {
329 s->state |= PNG_ALLIMAGE;
042e0add
LM
330 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
331 s->color_type == PNG_COLOR_TYPE_RGB)
332 deloco_rgb24(ptr, s->row_size);
b958a7f1
MR
333 }
334 } else {
335 got_line = 0;
9221c0af 336 for (;;) {
b958a7f1
MR
337 ptr = s->image_buf + s->image_linesize * s->y;
338 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
339 /* if we already read one row, it is time to stop to
c598b569 340 * wait for the next one */
b958a7f1
MR
341 if (got_line)
342 break;
4a9ca0a2 343 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
b958a7f1 344 s->last_row, s->pass_row_size, s->bpp);
c598b569 345 FFSWAP(uint8_t *, s->last_row, s->tmp_row);
b958a7f1
MR
346 got_line = 1;
347 }
348 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
349 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
350 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
351 s->color_type, s->last_row);
352 }
353 s->y++;
354 if (s->y == s->height) {
9221c0af 355 for (;;) {
b958a7f1
MR
356 if (s->pass == NB_PASSES - 1) {
357 s->state |= PNG_ALLIMAGE;
358 goto the_end;
359 } else {
360 s->pass++;
361 s->y = 0;
362 s->pass_row_size = ff_png_pass_row_size(s->pass,
c598b569
VG
363 s->bits_per_pixel,
364 s->width);
b958a7f1
MR
365 s->crow_size = s->pass_row_size + 1;
366 if (s->pass_row_size != 0)
367 break;
368 /* skip pass if empty row */
369 }
370 }
371 }
372 }
c598b569 373the_end:;
b958a7f1
MR
374 }
375}
376
e0eddd12 377static int png_decode_idat(PNGDecContext *s, int length)
b958a7f1
MR
378{
379 int ret;
4c25269c 380 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
9221c0af 381 s->zstream.next_in = s->gb.buffer;
4c25269c 382 bytestream2_skip(&s->gb, length);
b958a7f1
MR
383
384 /* decode one line if possible */
385 while (s->zstream.avail_in > 0) {
386 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
387 if (ret != Z_OK && ret != Z_STREAM_END) {
388 return -1;
389 }
390 if (s->zstream.avail_out == 0) {
391 if (!(s->state & PNG_ALLIMAGE)) {
392 png_handle_row(s);
393 }
394 s->zstream.avail_out = s->crow_size;
9221c0af 395 s->zstream.next_out = s->crow_buf;
b958a7f1 396 }
a81cad8f 397 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
c598b569
VG
398 av_log(NULL, AV_LOG_WARNING,
399 "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
a81cad8f
MS
400 return 0;
401 }
b958a7f1
MR
402 }
403 return 0;
404}
405
406static int decode_frame(AVCodecContext *avctx,
df9b9567 407 void *data, int *got_frame,
7a00bbad 408 AVPacket *avpkt)
b958a7f1 409{
c598b569
VG
410 PNGDecContext *const s = avctx->priv_data;
411 const uint8_t *buf = avpkt->data;
412 int buf_size = avpkt->size;
413 AVFrame *p = data;
414 uint8_t *crow_buf_base = NULL;
b958a7f1 415 uint32_t tag, length;
e65ab9d9 416 int ret;
b958a7f1 417
b958a7f1 418 /* check signature */
4fee11ab
LB
419 if (buf_size < 8) {
420 av_log(avctx, AV_LOG_ERROR, "Not enough data %d\n",
421 buf_size);
422 return AVERROR_INVALIDDATA;
423 }
424 if (memcmp(buf, ff_pngsig, 8) != 0 &&
425 memcmp(buf, ff_mngsig, 8) != 0) {
426 char signature[5 * 8 + 1] = { 0 };
427 int i;
428 for (i = 0; i < 8; i++) {
429 av_strlcatf(signature + i * 5, sizeof(signature) - i * 5,
430 " 0x%02x", buf[i]);
431 }
432 av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature %s\n",
433 signature);
1e763454
VG
434 return AVERROR_INVALIDDATA;
435 }
4c25269c
RB
436
437 bytestream2_init(&s->gb, buf + 8, buf_size - 8);
9221c0af
AK
438 s->y = s->state = 0;
439
b958a7f1
MR
440 /* init the zlib */
441 s->zstream.zalloc = ff_png_zalloc;
9221c0af 442 s->zstream.zfree = ff_png_zfree;
b958a7f1
MR
443 s->zstream.opaque = NULL;
444 ret = inflateInit(&s->zstream);
445 if (ret != Z_OK)
446 return -1;
9221c0af 447 for (;;) {
4c25269c 448 if (bytestream2_get_bytes_left(&s->gb) <= 0)
b958a7f1 449 goto fail;
4c25269c 450 length = bytestream2_get_be32(&s->gb);
b958a7f1
MR
451 if (length > 0x7fffffff)
452 goto fail;
4c25269c 453 tag = bytestream2_get_le32(&s->gb);
6a85dfc8 454 ff_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
8b44de14
DB
455 (tag & 0xff),
456 ((tag >> 8) & 0xff),
457 ((tag >> 16) & 0xff),
458 ((tag >> 24) & 0xff), length);
9221c0af 459 switch (tag) {
b958a7f1
MR
460 case MKTAG('I', 'H', 'D', 'R'):
461 if (length != 13)
462 goto fail;
4c25269c
RB
463 s->width = bytestream2_get_be32(&s->gb);
464 s->height = bytestream2_get_be32(&s->gb);
9221c0af
AK
465 if (av_image_check_size(s->width, s->height, 0, avctx)) {
466 s->width = s->height = 0;
b958a7f1
MR
467 goto fail;
468 }
4c25269c
RB
469 s->bit_depth = bytestream2_get_byte(&s->gb);
470 s->color_type = bytestream2_get_byte(&s->gb);
471 s->compression_type = bytestream2_get_byte(&s->gb);
472 s->filter_type = bytestream2_get_byte(&s->gb);
473 s->interlace_type = bytestream2_get_byte(&s->gb);
474 bytestream2_skip(&s->gb, 4); /* crc */
b958a7f1 475 s->state |= PNG_IHDR;
6a85dfc8 476 ff_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
c598b569 477 "compression_type=%d filter_type=%d interlace_type=%d\n",
8b44de14
DB
478 s->width, s->height, s->bit_depth, s->color_type,
479 s->compression_type, s->filter_type, s->interlace_type);
b958a7f1
MR
480 break;
481 case MKTAG('I', 'D', 'A', 'T'):
482 if (!(s->state & PNG_IHDR))
483 goto fail;
484 if (!(s->state & PNG_IDAT)) {
485 /* init image info */
9221c0af 486 avctx->width = s->width;
b958a7f1
MR
487 avctx->height = s->height;
488
9221c0af 489 s->channels = ff_png_get_nb_channels(s->color_type);
b958a7f1 490 s->bits_per_pixel = s->bit_depth * s->channels;
9221c0af
AK
491 s->bpp = (s->bits_per_pixel + 7) >> 3;
492 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
b958a7f1
MR
493
494 if (s->bit_depth == 8 &&
495 s->color_type == PNG_COLOR_TYPE_RGB) {
716d413c 496 avctx->pix_fmt = AV_PIX_FMT_RGB24;
b958a7f1
MR
497 } else if (s->bit_depth == 8 &&
498 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
716d413c 499 avctx->pix_fmt = AV_PIX_FMT_RGB32;
b958a7f1
MR
500 } else if (s->bit_depth == 8 &&
501 s->color_type == PNG_COLOR_TYPE_GRAY) {
716d413c 502 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
b958a7f1
MR
503 } else if (s->bit_depth == 16 &&
504 s->color_type == PNG_COLOR_TYPE_GRAY) {
716d413c 505 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
f3ac96e4
KS
506 } else if (s->bit_depth == 16 &&
507 s->color_type == PNG_COLOR_TYPE_RGB) {
716d413c 508 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
b958a7f1
MR
509 } else if (s->bit_depth == 1 &&
510 s->color_type == PNG_COLOR_TYPE_GRAY) {
716d413c 511 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
d2205d65
RB
512 } else if (s->bit_depth == 8 &&
513 s->color_type == PNG_COLOR_TYPE_PALETTE) {
716d413c 514 avctx->pix_fmt = AV_PIX_FMT_PAL8;
d2205d65
RB
515 } else if (s->bit_depth == 8 &&
516 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
e96c3b81 517 avctx->pix_fmt = AV_PIX_FMT_YA8;
e64f0bf2
VG
518 } else if (s->bit_depth == 16 &&
519 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
520 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
b958a7f1
MR
521 } else {
522 goto fail;
523 }
b958a7f1 524
759001c5 525 if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
b958a7f1
MR
526 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
527 goto fail;
528 }
9221c0af
AK
529 p->pict_type = AV_PICTURE_TYPE_I;
530 p->key_frame = 1;
b958a7f1
MR
531 p->interlaced_frame = !!s->interlace_type;
532
533 /* compute the compressed row size */
534 if (!s->interlace_type) {
535 s->crow_size = s->row_size + 1;
536 } else {
c598b569 537 s->pass = 0;
b958a7f1 538 s->pass_row_size = ff_png_pass_row_size(s->pass,
c598b569
VG
539 s->bits_per_pixel,
540 s->width);
b958a7f1
MR
541 s->crow_size = s->pass_row_size + 1;
542 }
6a85dfc8 543 ff_dlog(avctx, "row_size=%d crow_size =%d\n",
8b44de14 544 s->row_size, s->crow_size);
9221c0af 545 s->image_buf = p->data[0];
b958a7f1
MR
546 s->image_linesize = p->linesize[0];
547 /* copy the palette if needed */
548 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
549 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
550 /* empty row is used if differencing to the first row */
551 s->last_row = av_mallocz(s->row_size);
552 if (!s->last_row)
553 goto fail;
554 if (s->interlace_type ||
555 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
556 s->tmp_row = av_malloc(s->row_size);
557 if (!s->tmp_row)
558 goto fail;
559 }
560 /* compressed row */
09d1208c
VS
561 crow_buf_base = av_malloc(s->row_size + 16);
562 if (!crow_buf_base)
b958a7f1 563 goto fail;
09d1208c
VS
564
565 /* we want crow_buf+1 to be 16-byte aligned */
9221c0af 566 s->crow_buf = crow_buf_base + 15;
b958a7f1 567 s->zstream.avail_out = s->crow_size;
9221c0af 568 s->zstream.next_out = s->crow_buf;
b958a7f1
MR
569 }
570 s->state |= PNG_IDAT;
571 if (png_decode_idat(s, length) < 0)
572 goto fail;
4c25269c 573 bytestream2_skip(&s->gb, 4); /* crc */
b958a7f1
MR
574 break;
575 case MKTAG('P', 'L', 'T', 'E'):
c598b569
VG
576 {
577 int n, i, r, g, b;
578
579 if ((length % 3) != 0 || length > 256 * 3)
580 goto skip_tag;
581 /* read the palette */
582 n = length / 3;
583 for (i = 0; i < n; i++) {
584 r = bytestream2_get_byte(&s->gb);
585 g = bytestream2_get_byte(&s->gb);
586 b = bytestream2_get_byte(&s->gb);
587 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
b958a7f1 588 }
c598b569
VG
589 for (; i < 256; i++)
590 s->palette[i] = (0xff << 24);
591 s->state |= PNG_PLTE;
592 bytestream2_skip(&s->gb, 4); /* crc */
593 }
594 break;
b958a7f1 595 case MKTAG('t', 'R', 'N', 'S'):
c598b569
VG
596 {
597 int v, i;
598
599 /* read the transparency. XXX: Only palette mode supported */
600 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
601 length > 256 ||
602 !(s->state & PNG_PLTE))
603 goto skip_tag;
604 for (i = 0; i < length; i++) {
605 v = bytestream2_get_byte(&s->gb);
606 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
b958a7f1 607 }
c598b569
VG
608 bytestream2_skip(&s->gb, 4); /* crc */
609 }
610 break;
b958a7f1
MR
611 case MKTAG('I', 'E', 'N', 'D'):
612 if (!(s->state & PNG_ALLIMAGE))
613 goto fail;
4c25269c 614 bytestream2_skip(&s->gb, 4); /* crc */
b958a7f1
MR
615 goto exit_loop;
616 default:
617 /* skip tag */
c598b569 618skip_tag:
4c25269c 619 bytestream2_skip(&s->gb, length + 4);
b958a7f1
MR
620 break;
621 }
622 }
c598b569
VG
623exit_loop:
624 /* handle p-frames only if a predecessor frame is available */
625 if (s->prev->data[0]) {
626 if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
ee30cdab 627 int i, j;
759001c5
AK
628 uint8_t *pd = p->data[0];
629 uint8_t *pd_last = s->prev->data[0];
ee30cdab 630
9221c0af 631 for (j = 0; j < s->height; j++) {
c598b569 632 for (i = 0; i < s->width * s->bpp; i++)
ee30cdab 633 pd[i] += pd_last[i];
9221c0af 634 pd += s->image_linesize;
ee30cdab
TB
635 pd_last += s->image_linesize;
636 }
637 }
638 }
639
c598b569
VG
640 av_frame_unref(s->prev);
641 if ((ret = av_frame_ref(s->prev, p)) < 0)
642 goto fail;
759001c5 643
df9b9567 644 *got_frame = 1;
b958a7f1 645
4c25269c 646 ret = bytestream2_tell(&s->gb);
c598b569 647the_end:
b958a7f1 648 inflateEnd(&s->zstream);
09d1208c
VS
649 av_free(crow_buf_base);
650 s->crow_buf = NULL;
b958a7f1
MR
651 av_freep(&s->last_row);
652 av_freep(&s->tmp_row);
653 return ret;
c598b569 654fail:
b958a7f1
MR
655 ret = -1;
656 goto the_end;
657}
658
9221c0af
AK
659static av_cold int png_dec_init(AVCodecContext *avctx)
660{
e0eddd12
MR
661 PNGDecContext *s = avctx->priv_data;
662
b9f7a677 663 avctx->color_range = AVCOL_RANGE_JPEG;
664
759001c5
AK
665 s->prev = av_frame_alloc();
666 if (!s->prev)
667 return AVERROR(ENOMEM);
668
e9200351 669 ff_pngdsp_init(&s->dsp);
e0eddd12
MR
670
671 return 0;
672}
673
de3553b0
JM
674static av_cold int png_dec_end(AVCodecContext *avctx)
675{
676 PNGDecContext *s = avctx->priv_data;
677
759001c5 678 av_frame_free(&s->prev);
de3553b0
JM
679
680 return 0;
681}
682
d36beb3f 683AVCodec ff_png_decoder = {
ec6402b7 684 .name = "png",
b2bed932 685 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
ec6402b7 686 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 687 .id = AV_CODEC_ID_PNG,
ec6402b7
AK
688 .priv_data_size = sizeof(PNGDecContext),
689 .init = png_dec_init,
690 .close = png_dec_end,
691 .decode = decode_frame,
def97856 692 .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
b958a7f1 693};