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 "png.h"
30 #include "pngdsp.h"
31 #include "thread.h"
32 
33 #include <zlib.h>
34 
35 #ifndef SIZE_MAX
36 #  ifdef __SIZE_MAX__
37 #    define SIZE_MAX __SIZE_MAX__
38 #  else
39 #    error no SIZE_MAX
40 #  endif
41 #endif
42 
43 typedef struct PNGDecContext {
44     PNGDSPContext dsp;
45     AVCodecContext *avctx;
46 
47     GetByteContext gb;
48     ThreadFrame last_picture;
49     ThreadFrame picture;
50 
51     int state;
52     int width, height;
53     int bit_depth;
54     int color_type;
55     int compression_type;
56     int interlace_type;
57     int filter_type;
58     int channels;
59     int bits_per_pixel;
60     int bpp;
61 
62     uint8_t *image_buf;
63     int image_linesize;
64     uint32_t palette[256];
65     uint8_t *crow_buf;
66     uint8_t *last_row;
67     unsigned int last_row_size;
68     uint8_t *tmp_row;
69     unsigned int tmp_row_size;
70     uint8_t *buffer;
71     int buffer_size;
72     int pass;
73     int crow_size; /* compressed row size (include filter type) */
74     int row_size; /* decompressed row size */
75     int pass_row_size; /* decompress row size of the current pass */
76     int y;
77     z_stream zstream;
78 } PNGDecContext;
79 
80 /* Mask to determine which pixels are valid in a pass */
81 static const uint8_t png_pass_mask[NB_PASSES] = {
82     0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
83 };
84 
85 /* Mask to determine which y pixels can be written in a pass */
86 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
87     0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
88 };
89 
90 /* Mask to determine which pixels to overwrite while displaying */
91 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
92     0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
93 };
94 
95 /* NOTE: we try to construct a good looking image at each pass. width
96  * is the original image width. We also do pixel format conversion at
97  * this stage */
png_put_interlaced_row(uint8_t * dst,int width,int bits_per_pixel,int pass,int color_type,const uint8_t * src)98 static void png_put_interlaced_row(uint8_t *dst, int width,
99                                    int bits_per_pixel, int pass,
100                                    int color_type, const uint8_t *src)
101 {
102     int x, mask, dsp_mask, j, src_x, b, bpp;
103     uint8_t *d;
104     const uint8_t *s;
105 
106     mask     = png_pass_mask[pass];
107     dsp_mask = png_pass_dsp_mask[pass];
108 
109     switch (bits_per_pixel) {
110     case 1:
111         src_x = 0;
112         for (x = 0; x < width; x++) {
113             j = (x & 7);
114             if ((dsp_mask << j) & 0x80) {
115                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
116                 dst[x >> 3] &= 0xFF7F>>j;
117                 dst[x >> 3] |= b << (7 - j);
118             }
119             if ((mask << j) & 0x80)
120                 src_x++;
121         }
122         break;
123     case 2:
124         src_x = 0;
125         for (x = 0; x < width; x++) {
126             int j2 = 2 * (x & 3);
127             j = (x & 7);
128             if ((dsp_mask << j) & 0x80) {
129                 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
130                 dst[x >> 2] &= 0xFF3F>>j2;
131                 dst[x >> 2] |= b << (6 - j2);
132             }
133             if ((mask << j) & 0x80)
134                 src_x++;
135         }
136         break;
137     case 4:
138         src_x = 0;
139         for (x = 0; x < width; x++) {
140             int j2 = 4*(x&1);
141             j = (x & 7);
142             if ((dsp_mask << j) & 0x80) {
143                 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
144                 dst[x >> 1] &= 0xFF0F>>j2;
145                 dst[x >> 1] |= b << (4 - j2);
146             }
147             if ((mask << j) & 0x80)
148                 src_x++;
149         }
150         break;
151     default:
152         bpp = bits_per_pixel >> 3;
153         d   = dst;
154         s   = src;
155             for (x = 0; x < width; x++) {
156                 j = x & 7;
157                 if ((dsp_mask << j) & 0x80) {
158                     memcpy(d, s, bpp);
159                 }
160                 d += bpp;
161                 if ((mask << j) & 0x80)
162                     s += bpp;
163             }
164         break;
165     }
166 }
167 
ff_add_png_paeth_prediction(uint8_t * dst,uint8_t * src,uint8_t * top,int w,int bpp)168 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
169                                  int w, int bpp)
170 {
171     int i;
172     for (i = 0; i < w; i++) {
173         int a, b, c, p, pa, pb, pc;
174 
175         a = dst[i - bpp];
176         b = top[i];
177         c = top[i - bpp];
178 
179         p  = b - c;
180         pc = a - c;
181 
182         pa = abs(p);
183         pb = abs(pc);
184         pc = abs(p + pc);
185 
186         if (pa <= pb && pa <= pc)
187             p = a;
188         else if (pb <= pc)
189             p = b;
190         else
191             p = c;
192         dst[i] = p + src[i];
193     }
194 }
195 
196 #define UNROLL1(bpp, op)                                                      \
197     {                                                                         \
198         r = dst[0];                                                           \
199         if (bpp >= 2)                                                         \
200             g = dst[1];                                                       \
201         if (bpp >= 3)                                                         \
202             b = dst[2];                                                       \
203         if (bpp >= 4)                                                         \
204             a = dst[3];                                                       \
205         for (; i <= size - bpp; i += bpp) {                                   \
206             dst[i + 0] = r = op(r, src[i + 0], last[i + 0]);                  \
207             if (bpp == 1)                                                     \
208                 continue;                                                     \
209             dst[i + 1] = g = op(g, src[i + 1], last[i + 1]);                  \
210             if (bpp == 2)                                                     \
211                 continue;                                                     \
212             dst[i + 2] = b = op(b, src[i + 2], last[i + 2]);                  \
213             if (bpp == 3)                                                     \
214                 continue;                                                     \
215             dst[i + 3] = a = op(a, src[i + 3], last[i + 3]);                  \
216         }                                                                     \
217     }
218 
219 #define UNROLL_FILTER(op)                                                     \
220     if (bpp == 1) {                                                           \
221         UNROLL1(1, op)                                                        \
222     } else if (bpp == 2) {                                                    \
223         UNROLL1(2, op)                                                        \
224     } else if (bpp == 3) {                                                    \
225         UNROLL1(3, op)                                                        \
226     } else if (bpp == 4) {                                                    \
227         UNROLL1(4, op)                                                        \
228     }                                                                         \
229     for (; i < size; i++) {                                                   \
230         dst[i] = op(dst[i - bpp], src[i], last[i]);                           \
231     }
232 
233 /* NOTE: 'dst' can be equal to 'last' */
png_filter_row(PNGDSPContext * dsp,uint8_t * dst,int filter_type,uint8_t * src,uint8_t * last,int size,int bpp)234 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
235                            uint8_t *src, uint8_t *last, int size, int bpp)
236 {
237     int i, p, r, g, b, a;
238 
239     switch (filter_type) {
240     case PNG_FILTER_VALUE_NONE:
241         memcpy(dst, src, size);
242         break;
243     case PNG_FILTER_VALUE_SUB:
244         for (i = 0; i < bpp; i++)
245             dst[i] = src[i];
246         if (bpp == 4) {
247             p = *(int *)dst;
248             for (; i < size; i += bpp) {
249                 unsigned s = *(int *)(src + i);
250                 p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
251                 *(int *)(dst + i) = p;
252             }
253         } else {
254 #define OP_SUB(x, s, l) ((x) + (s))
255             UNROLL_FILTER(OP_SUB);
256         }
257         break;
258     case PNG_FILTER_VALUE_UP:
259         dsp->add_bytes_l2(dst, src, last, size);
260         break;
261     case PNG_FILTER_VALUE_AVG:
262         for (i = 0; i < bpp; i++) {
263             p      = (last[i] >> 1);
264             dst[i] = p + src[i];
265         }
266 #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
267         UNROLL_FILTER(OP_AVG);
268         break;
269     case PNG_FILTER_VALUE_PAETH:
270         for (i = 0; i < bpp; i++) {
271             p      = last[i];
272             dst[i] = p + src[i];
273         }
274         if (bpp > 2 && size > 4) {
275             /* would write off the end of the array if we let it process
276              * the last pixel with bpp=3 */
277             int w = bpp == 4 ? size : size - 3;
278             dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
279             i = w;
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 
YUV2RGB(rgb8,uint8_t)299 YUV2RGB(rgb8, uint8_t)
300 YUV2RGB(rgb16, uint16_t)
301 
302 /* process exactly one decompressed row */
303 static void png_handle_row(PNGDecContext *s)
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;
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->height) {
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;
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->width, s->bits_per_pixel, s->pass,
357                                        s->color_type, s->last_row);
358             }
359             s->y++;
360             if (s->y == s->height) {
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->width);
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 
png_decode_idat(PNGDecContext * s,int length)384 static int png_decode_idat(PNGDecContext *s, int length)
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 
decode_zbuf(AVBPrint * bp,const uint8_t * data,const uint8_t * data_end)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 
iso88591_to_utf8(const uint8_t * in,size_t size_in)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 typedef union {
483     uint8_t *t_uint8_t;
484     char *t_char;
485 } u_uint8_char;
486 
decode_text_chunk(PNGDecContext * s,uint32_t length,int compressed,AVDictionary ** dict)487 static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
488                              AVDictionary **dict)
489 {
490     int ret, method;
491     const uint8_t *data        = s->gb.buffer;
492     const uint8_t *data_end    = data + length;
493     const uint8_t *keyword     = data;
494     const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
495     uint8_t *kw_utf8 = NULL, *txt_utf8 = NULL;
496     u_uint8_char text;
497     unsigned text_len;
498     AVBPrint bp;
499 
500     if (!keyword_end)
501         return AVERROR_INVALIDDATA;
502     data = keyword_end + 1;
503 
504     if (compressed) {
505         if (data == data_end)
506             return AVERROR_INVALIDDATA;
507         method = *(data++);
508         if (method)
509             return AVERROR_INVALIDDATA;
510         if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
511             return ret;
512         text_len = bp.len;
513         av_bprint_finalize(&bp, (char **)&text);
514         if (!text.t_uint8_t)
515             return AVERROR(ENOMEM);
516     } else {
517         text.t_uint8_t = (uint8_t *)data;
518         text_len = data_end - text.t_uint8_t;
519     }
520 
521     kw_utf8  = iso88591_to_utf8(keyword, keyword_end - keyword);
522     txt_utf8 = iso88591_to_utf8(text.t_uint8_t, text_len);
523     if (text.t_uint8_t != data)
524         av_free(text.t_uint8_t);
525     if (!(kw_utf8 && txt_utf8)) {
526         av_free(kw_utf8);
527         av_free(txt_utf8);
528         return AVERROR(ENOMEM);
529     }
530 
531     av_dict_set(dict, kw_utf8, txt_utf8,
532                 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
533     return 0;
534 }
535 
decode_frame(AVCodecContext * avctx,void * data,int * got_frame,AVPacket * avpkt)536 static int decode_frame(AVCodecContext *avctx,
537                         void *data, int *got_frame,
538                         AVPacket *avpkt)
539 {
540     PNGDecContext *const s = avctx->priv_data;
541     const uint8_t *buf     = avpkt->data;
542     int buf_size           = avpkt->size;
543     AVFrame *p;
544     AVDictionary *metadata  = NULL;
545     uint32_t tag, length;
546     int64_t sig;
547     int ret;
548 
549     ff_thread_release_buffer(avctx, &s->last_picture);
550     FFSWAP(ThreadFrame, s->picture, s->last_picture);
551     p = s->picture.f;
552 
553     bytestream2_init(&s->gb, buf, buf_size);
554 
555     /* check signature */
556     sig = bytestream2_get_be64(&s->gb);
557     if (sig != PNGSIG &&
558         sig != MNGSIG) {
559         av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
560         return AVERROR_INVALIDDATA;
561     }
562 
563     s->y = s->state = 0;
564 
565     /* init the zlib */
566     s->zstream.zalloc = ff_png_zalloc;
567     s->zstream.zfree  = ff_png_zfree;
568     s->zstream.opaque = NULL;
569     ret = inflateInit(&s->zstream);
570     if (ret != Z_OK) {
571         av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
572         return AVERROR_EXTERNAL;
573     }
574     for (;;) {
575         if (bytestream2_get_bytes_left(&s->gb) <= 0) {
576             av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", bytestream2_get_bytes_left(&s->gb));
577             if (   s->state & PNG_ALLIMAGE
578                 && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
579                 goto exit_loop;
580             goto fail;
581         }
582 
583         length = bytestream2_get_be32(&s->gb);
584         if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb))  {
585             av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
586             goto fail;
587         }
588         tag = bytestream2_get_le32(&s->gb);
589         if (avctx->debug & FF_DEBUG_STARTCODE)
590             av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
591                 (tag & 0xff),
592                 ((tag >> 8) & 0xff),
593                 ((tag >> 16) & 0xff),
594                 ((tag >> 24) & 0xff), length);
595         switch (tag) {
596         case MKTAG('I', 'H', 'D', 'R'):
597             if (length != 13)
598                 goto fail;
599             s->width  = bytestream2_get_be32(&s->gb);
600             s->height = bytestream2_get_be32(&s->gb);
601             if (av_image_check_size(s->width, s->height, 0, avctx)) {
602                 s->width = s->height = 0;
603                 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
604                 goto fail;
605             }
606             s->bit_depth        = bytestream2_get_byte(&s->gb);
607             s->color_type       = bytestream2_get_byte(&s->gb);
608             s->compression_type = bytestream2_get_byte(&s->gb);
609             s->filter_type      = bytestream2_get_byte(&s->gb);
610             s->interlace_type   = bytestream2_get_byte(&s->gb);
611             bytestream2_skip(&s->gb, 4); /* crc */
612             s->state |= PNG_IHDR;
613             if (avctx->debug & FF_DEBUG_PICT_INFO)
614                 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
615                            "compression_type=%d filter_type=%d interlace_type=%d\n",
616                     s->width, s->height, s->bit_depth, s->color_type,
617                     s->compression_type, s->filter_type, s->interlace_type);
618             break;
619         case MKTAG('p', 'H', 'Y', 's'):
620             if (s->state & PNG_IDAT) {
621                 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
622                 goto fail;
623             }
624             avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
625             avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
626             if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
627                 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
628             bytestream2_skip(&s->gb, 1); /* unit specifier */
629             bytestream2_skip(&s->gb, 4); /* crc */
630             break;
631         case MKTAG('I', 'D', 'A', 'T'):
632             if (!(s->state & PNG_IHDR)) {
633                 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
634                 goto fail;
635             }
636             if (!(s->state & PNG_IDAT)) {
637                 /* init image info */
638                 avctx->width  = s->width;
639                 avctx->height = s->height;
640 
641                 s->channels       = ff_png_get_nb_channels(s->color_type);
642                 s->bits_per_pixel = s->bit_depth * s->channels;
643                 s->bpp            = (s->bits_per_pixel + 7) >> 3;
644                 s->row_size       = (avctx->width * s->bits_per_pixel + 7) >> 3;
645 
646                 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
647                     s->color_type == PNG_COLOR_TYPE_RGB) {
648                     avctx->pix_fmt = AV_PIX_FMT_RGB24;
649                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
650                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
651                     avctx->pix_fmt = AV_PIX_FMT_RGBA;
652                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
653                            s->color_type == PNG_COLOR_TYPE_GRAY) {
654                     avctx->pix_fmt = AV_PIX_FMT_GRAY8;
655                 } else if (s->bit_depth == 16 &&
656                            s->color_type == PNG_COLOR_TYPE_GRAY) {
657                     avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
658                 } else if (s->bit_depth == 16 &&
659                            s->color_type == PNG_COLOR_TYPE_RGB) {
660                     avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
661                 } else if (s->bit_depth == 16 &&
662                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
663                     avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
664                 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
665                            s->color_type == PNG_COLOR_TYPE_PALETTE) {
666                     avctx->pix_fmt = AV_PIX_FMT_PAL8;
667                 } else if (s->bit_depth == 1 && s->bits_per_pixel == 1) {
668                     avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
669                 } else if (s->bit_depth == 8 &&
670                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
671                     avctx->pix_fmt = AV_PIX_FMT_YA8;
672                 } else if (s->bit_depth == 16 &&
673                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
674                     avctx->pix_fmt = AV_PIX_FMT_YA16BE;
675                 } else {
676                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
677                                                 "and color type %d\n",
678                                                  s->bit_depth, s->color_type);
679                     goto fail;
680                 }
681 
682                 if (ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF) < 0)
683                     goto fail;
684                 ff_thread_finish_setup(avctx);
685 
686                 p->pict_type        = AV_PICTURE_TYPE_I;
687                 p->key_frame        = 1;
688                 p->interlaced_frame = !!s->interlace_type;
689 
690                 /* compute the compressed row size */
691                 if (!s->interlace_type) {
692                     s->crow_size = s->row_size + 1;
693                 } else {
694                     s->pass          = 0;
695                     s->pass_row_size = ff_png_pass_row_size(s->pass,
696                                                             s->bits_per_pixel,
697                                                             s->width);
698                     s->crow_size = s->pass_row_size + 1;
699                 }
700                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
701                         s->row_size, s->crow_size);
702                 s->image_buf      = p->data[0];
703                 s->image_linesize = p->linesize[0];
704                 /* copy the palette if needed */
705                 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
706                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
707                 /* empty row is used if differencing to the first row */
708                 av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
709                 if (!s->last_row)
710                     goto fail;
711                 if (s->interlace_type ||
712                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
713                     av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
714                     if (!s->tmp_row)
715                         goto fail;
716                 }
717                 /* compressed row */
718                 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
719                 if (!s->buffer)
720                     goto fail;
721 
722                 /* we want crow_buf+1 to be 16-byte aligned */
723                 s->crow_buf          = s->buffer + 15;
724                 s->zstream.avail_out = s->crow_size;
725                 s->zstream.next_out  = s->crow_buf;
726             }
727             s->state |= PNG_IDAT;
728             if (png_decode_idat(s, length) < 0)
729                 goto fail;
730             bytestream2_skip(&s->gb, 4); /* crc */
731             break;
732         case MKTAG('P', 'L', 'T', 'E'):
733         {
734             int n, i, r, g, b;
735 
736             if ((length % 3) != 0 || length > 256 * 3)
737                 goto skip_tag;
738             /* read the palette */
739             n = length / 3;
740             for (i = 0; i < n; i++) {
741                 r = bytestream2_get_byte(&s->gb);
742                 g = bytestream2_get_byte(&s->gb);
743                 b = bytestream2_get_byte(&s->gb);
744                 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
745             }
746             for (; i < 256; i++)
747                 s->palette[i] = (0xFFU << 24);
748             s->state |= PNG_PLTE;
749             bytestream2_skip(&s->gb, 4);     /* crc */
750         }
751         break;
752         case MKTAG('t', 'R', 'N', 'S'):
753         {
754             int v, i;
755 
756             /* read the transparency. XXX: Only palette mode supported */
757             if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
758                 length > 256 ||
759                 !(s->state & PNG_PLTE))
760                 goto skip_tag;
761             for (i = 0; i < length; i++) {
762                 v = bytestream2_get_byte(&s->gb);
763                 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
764             }
765             bytestream2_skip(&s->gb, 4);     /* crc */
766         }
767         break;
768         case MKTAG('t', 'E', 'X', 't'):
769             if (decode_text_chunk(s, length, 0, &metadata) < 0)
770                 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
771             bytestream2_skip(&s->gb, length + 4);
772             break;
773         case MKTAG('z', 'T', 'X', 't'):
774             if (decode_text_chunk(s, length, 1, &metadata) < 0)
775                 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
776             bytestream2_skip(&s->gb, length + 4);
777             break;
778         case MKTAG('I', 'E', 'N', 'D'):
779             if (!(s->state & PNG_ALLIMAGE))
780                 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
781             if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
782                 goto fail;
783             }
784             bytestream2_skip(&s->gb, 4); /* crc */
785             goto exit_loop;
786         default:
787             /* skip tag */
788 skip_tag:
789             bytestream2_skip(&s->gb, length + 4);
790             break;
791         }
792     }
793 exit_loop:
794 
795     if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
796         int i, j, k;
797         uint8_t *pd = p->data[0];
798         for (j = 0; j < s->height; j++) {
799             i = s->width / 8;
800             for (k = 7; k >= 1; k--)
801                 if ((s->width&7) >= k)
802                     pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
803             for (i--; i >= 0; i--) {
804                 pd[8*i + 7]=  pd[i]     & 1;
805                 pd[8*i + 6]= (pd[i]>>1) & 1;
806                 pd[8*i + 5]= (pd[i]>>2) & 1;
807                 pd[8*i + 4]= (pd[i]>>3) & 1;
808                 pd[8*i + 3]= (pd[i]>>4) & 1;
809                 pd[8*i + 2]= (pd[i]>>5) & 1;
810                 pd[8*i + 1]= (pd[i]>>6) & 1;
811                 pd[8*i + 0]=  pd[i]>>7;
812             }
813             pd += s->image_linesize;
814         }
815     }
816     if (s->bits_per_pixel == 2){
817         int i, j;
818         uint8_t *pd = p->data[0];
819         for (j = 0; j < s->height; j++) {
820             i = s->width / 4;
821             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
822                 if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
823                 if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
824                 if ((s->width&3) >= 1) pd[4*i + 0]=  pd[i] >> 6;
825                 for (i--; i >= 0; i--) {
826                     pd[4*i + 3]=  pd[i]     & 3;
827                     pd[4*i + 2]= (pd[i]>>2) & 3;
828                     pd[4*i + 1]= (pd[i]>>4) & 3;
829                     pd[4*i + 0]=  pd[i]>>6;
830                 }
831             } else {
832                 if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
833                 if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
834                 if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6     )*0x55;
835                 for (i--; i >= 0; i--) {
836                     pd[4*i + 3]= ( pd[i]     & 3)*0x55;
837                     pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
838                     pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
839                     pd[4*i + 0]= ( pd[i]>>6     )*0x55;
840                 }
841             }
842             pd += s->image_linesize;
843         }
844     }
845     if (s->bits_per_pixel == 4){
846         int i, j;
847         uint8_t *pd = p->data[0];
848         for (j = 0; j < s->height; j++) {
849             i = s->width/2;
850             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
851                 if (s->width&1) pd[2*i+0]= pd[i]>>4;
852                 for (i--; i >= 0; i--) {
853                 pd[2*i + 1] = pd[i] & 15;
854                 pd[2*i + 0] = pd[i] >> 4;
855             }
856             } else {
857                 if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
858                 for (i--; i >= 0; i--) {
859                     pd[2*i + 1] = (pd[i] & 15) * 0x11;
860                     pd[2*i + 0] = (pd[i] >> 4) * 0x11;
861                 }
862             }
863             pd += s->image_linesize;
864         }
865     }
866 
867     /* handle p-frames only if a predecessor frame is available */
868     if (s->last_picture.f->data[0]) {
869         if (   !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
870             && s->last_picture.f->width == p->width
871             && s->last_picture.f->height== p->height
872             && s->last_picture.f->format== p->format
873          ) {
874             int i, j;
875             uint8_t *pd      = p->data[0];
876             uint8_t *pd_last = s->last_picture.f->data[0];
877             int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
878 
879             ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
880             for (j = 0; j < s->height; j++) {
881                 for (i = 0; i < ls; i++)
882                     pd[i] += pd_last[i];
883                 pd      += s->image_linesize;
884                 pd_last += s->image_linesize;
885             }
886         }
887     }
888     ff_thread_report_progress(&s->picture, INT_MAX, 0);
889 
890     av_frame_set_metadata(p, metadata);
891     metadata   = NULL;
892 
893     if ((ret = av_frame_ref(data, s->picture.f)) < 0)
894         return ret;
895 
896     *got_frame = 1;
897 
898     ret = bytestream2_tell(&s->gb);
899 the_end:
900     inflateEnd(&s->zstream);
901     s->crow_buf = NULL;
902     return ret;
903 fail:
904     av_dict_free(&metadata);
905     ff_thread_report_progress(&s->picture, INT_MAX, 0);
906     ret = AVERROR_INVALIDDATA;
907     goto the_end;
908 }
909 
update_thread_context(AVCodecContext * dst,const AVCodecContext * src)910 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
911 {
912     PNGDecContext *psrc = src->priv_data;
913     PNGDecContext *pdst = dst->priv_data;
914 
915     if (dst == src)
916         return 0;
917 
918     ff_thread_release_buffer(dst, &pdst->picture);
919     if (psrc->picture.f->data[0])
920         return ff_thread_ref_frame(&pdst->picture, &psrc->picture);
921 
922     return 0;
923 }
924 
png_dec_init(AVCodecContext * avctx)925 static av_cold int png_dec_init(AVCodecContext *avctx)
926 {
927     PNGDecContext *s = avctx->priv_data;
928 
929     s->avctx = avctx;
930     s->last_picture.f = av_frame_alloc();
931     s->picture.f = av_frame_alloc();
932     if (!s->last_picture.f || !s->picture.f)
933         return AVERROR(ENOMEM);
934 
935     if (!avctx->internal->is_copy) {
936         avctx->internal->allocate_progress = 1;
937         ff_pngdsp_init(&s->dsp);
938     }
939 
940     return 0;
941 }
942 
png_dec_end(AVCodecContext * avctx)943 static av_cold int png_dec_end(AVCodecContext *avctx)
944 {
945     PNGDecContext *s = avctx->priv_data;
946 
947     ff_thread_release_buffer(avctx, &s->last_picture);
948     av_frame_free(&s->last_picture.f);
949     ff_thread_release_buffer(avctx, &s->picture);
950     av_frame_free(&s->picture.f);
951     av_freep(&s->buffer);
952     s->buffer_size = 0;
953     av_freep(&s->last_row);
954     s->last_row_size = 0;
955     av_freep(&s->tmp_row);
956     s->tmp_row_size = 0;
957 
958     return 0;
959 }
960 
961 AVCodec ff_png_decoder = {
962     .name           = "png",
963     .long_name      = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
964     .type           = AVMEDIA_TYPE_VIDEO,
965     .id             = AV_CODEC_ID_PNG,
966     .priv_data_size = sizeof(PNGDecContext),
967     .init           = png_dec_init,
968     .close          = png_dec_end,
969     .decode         = decode_frame,
970     .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
971     .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
972     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
973 };
974