]> git.sesse.net Git - ffmpeg/blob - libavcodec/pngdec.c
Merge commit '3dc06b6972cf389269e9c36ff0a4373f80f7149b'
[ffmpeg] / libavcodec / pngdec.c
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/imgutils.h"
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "png.h"
28 #include "pngdsp.h"
29
30 /* TODO:
31  * - add 16 bit depth support
32  */
33
34 #include <zlib.h>
35
36 //#define DEBUG
37
38 typedef struct PNGDecContext {
39     PNGDSPContext dsp;
40     AVCodecContext *avctx;
41
42     GetByteContext gb;
43     AVFrame picture1, picture2;
44     AVFrame *current_picture, *last_picture;
45
46     int state;
47     int width, height;
48     int bit_depth;
49     int color_type;
50     int compression_type;
51     int interlace_type;
52     int filter_type;
53     int channels;
54     int bits_per_pixel;
55     int bpp;
56
57     uint8_t *image_buf;
58     int image_linesize;
59     uint32_t palette[256];
60     uint8_t *crow_buf;
61     uint8_t *last_row;
62     uint8_t *tmp_row;
63     int pass;
64     int crow_size; /* compressed row size (include filter type) */
65     int row_size; /* decompressed row size */
66     int pass_row_size; /* decompress row size of the current pass */
67     int y;
68     z_stream zstream;
69 } PNGDecContext;
70
71 /* Mask to determine which pixels are valid in a pass */
72 static const uint8_t png_pass_mask[NB_PASSES] = {
73     0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
74 };
75
76 /* Mask to determine which y pixels can be written in a pass */
77 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
78     0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
79 };
80
81 /* Mask to determine which pixels to overwrite while displaying */
82 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
83     0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
84 };
85
86 /* NOTE: we try to construct a good looking image at each pass. width
87    is the original image width. We also do pixel format conversion at
88    this stage */
89 static void png_put_interlaced_row(uint8_t *dst, int width,
90                                    int bits_per_pixel, int pass,
91                                    int color_type, const uint8_t *src)
92 {
93     int x, mask, dsp_mask, j, src_x, b, bpp;
94     uint8_t *d;
95     const uint8_t *s;
96
97     mask = png_pass_mask[pass];
98     dsp_mask = png_pass_dsp_mask[pass];
99     switch(bits_per_pixel) {
100     case 1:
101         src_x = 0;
102         for(x = 0; x < width; x++) {
103             j = (x & 7);
104             if ((dsp_mask << j) & 0x80) {
105                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
106                 dst[x >> 3] &= 0xFF7F>>j;
107                 dst[x >> 3] |= b << (7 - j);
108             }
109             if ((mask << j) & 0x80)
110                 src_x++;
111         }
112         break;
113     case 2:
114         src_x = 0;
115         for(x = 0; x < width; x++) {
116             int j2 = 2*(x&3);
117             j = (x & 7);
118             if ((dsp_mask << j) & 0x80) {
119                 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
120                 dst[x >> 2] &= 0xFF3F>>j2;
121                 dst[x >> 2] |= b << (6 - j2);
122             }
123             if ((mask << j) & 0x80)
124                 src_x++;
125         }
126         break;
127     case 4:
128         src_x = 0;
129         for(x = 0; x < width; x++) {
130             int j2 = 4*(x&1);
131             j = (x & 7);
132             if ((dsp_mask << j) & 0x80) {
133                 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
134                 dst[x >> 1] &= 0xFF0F>>j2;
135                 dst[x >> 1] |= b << (4 - j2);
136             }
137             if ((mask << j) & 0x80)
138                 src_x++;
139         }
140         break;
141     default:
142         bpp = bits_per_pixel >> 3;
143         d = dst;
144         s = src;
145             for(x = 0; x < width; x++) {
146                 j = x & 7;
147                 if ((dsp_mask << j) & 0x80) {
148                     memcpy(d, s, bpp);
149                 }
150                 d += bpp;
151                 if ((mask << j) & 0x80)
152                     s += bpp;
153             }
154         break;
155     }
156 }
157
158 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
159 {
160     int i;
161     for(i = 0; i < w; i++) {
162         int a, b, c, p, pa, pb, pc;
163
164         a = dst[i - bpp];
165         b = top[i];
166         c = top[i - bpp];
167
168         p = b - c;
169         pc = a - c;
170
171         pa = abs(p);
172         pb = abs(pc);
173         pc = abs(p + pc);
174
175         if (pa <= pb && pa <= pc)
176             p = a;
177         else if (pb <= pc)
178             p = b;
179         else
180             p = c;
181         dst[i] = p + src[i];
182     }
183 }
184
185 #define UNROLL1(bpp, op) {\
186                  r = dst[0];\
187     if(bpp >= 2) g = dst[1];\
188     if(bpp >= 3) b = dst[2];\
189     if(bpp >= 4) a = dst[3];\
190     for(; i < size; i+=bpp) {\
191         dst[i+0] = r = op(r, src[i+0], last[i+0]);\
192         if(bpp == 1) continue;\
193         dst[i+1] = g = op(g, src[i+1], last[i+1]);\
194         if(bpp == 2) continue;\
195         dst[i+2] = b = op(b, src[i+2], last[i+2]);\
196         if(bpp == 3) continue;\
197         dst[i+3] = a = op(a, src[i+3], last[i+3]);\
198     }\
199 }
200
201 #define UNROLL_FILTER(op)\
202          if(bpp == 1) UNROLL1(1, op)\
203     else if(bpp == 2) UNROLL1(2, op)\
204     else if(bpp == 3) UNROLL1(3, op)\
205     else if(bpp == 4) UNROLL1(4, op)\
206     else {\
207         for (; i < size; i += bpp) {\
208             int j;\
209             for (j = 0; j < bpp; j++)\
210                 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
211         }\
212     }
213
214 /* NOTE: 'dst' can be equal to 'last' */
215 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
216                            uint8_t *src, uint8_t *last, int size, int bpp)
217 {
218     int i, p, r, g, b, a;
219
220     switch(filter_type) {
221     case PNG_FILTER_VALUE_NONE:
222         memcpy(dst, src, size);
223         break;
224     case PNG_FILTER_VALUE_SUB:
225         for(i = 0; i < bpp; i++) {
226             dst[i] = src[i];
227         }
228         if(bpp == 4) {
229             p = *(int*)dst;
230             for(; i < size; i+=bpp) {
231                 int s = *(int*)(src+i);
232                 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
233                 *(int*)(dst+i) = p;
234             }
235         } else {
236 #define OP_SUB(x,s,l) x+s
237             UNROLL_FILTER(OP_SUB);
238         }
239         break;
240     case PNG_FILTER_VALUE_UP:
241         dsp->add_bytes_l2(dst, src, last, size);
242         break;
243     case PNG_FILTER_VALUE_AVG:
244         for(i = 0; i < bpp; i++) {
245             p = (last[i] >> 1);
246             dst[i] = p + src[i];
247         }
248 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
249         UNROLL_FILTER(OP_AVG);
250         break;
251     case PNG_FILTER_VALUE_PAETH:
252         for(i = 0; i < bpp; i++) {
253             p = last[i];
254             dst[i] = p + src[i];
255         }
256         if(bpp > 2 && size > 4) {
257             // would write off the end of the array if we let it process the last pixel with bpp=3
258             int w = bpp==4 ? size : size-3;
259             dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
260             i = w;
261         }
262         ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
263         break;
264     }
265 }
266
267 /* This used to be called "deloco" in FFmpeg
268  * and is actually an inverse reversible colorspace transformation */
269 #define YUV2RGB(NAME, TYPE) \
270 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
271 { \
272     int i; \
273     for (i = 0; i < size; i += 3 + alpha) { \
274         int g = dst [i+1]; \
275         dst[i+0] += g; \
276         dst[i+2] += g; \
277     } \
278 }
279
280 YUV2RGB(rgb8, uint8_t)
281 YUV2RGB(rgb16, uint16_t)
282
283 /* process exactly one decompressed row */
284 static void png_handle_row(PNGDecContext *s)
285 {
286     uint8_t *ptr, *last_row;
287     int got_line;
288
289     if (!s->interlace_type) {
290         ptr = s->image_buf + s->image_linesize * s->y;
291             if (s->y == 0)
292                 last_row = s->last_row;
293             else
294                 last_row = ptr - s->image_linesize;
295
296             png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
297                            last_row, s->row_size, s->bpp);
298         /* loco lags by 1 row so that it doesn't interfere with top prediction */
299         if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
300             if (s->bit_depth == 16) {
301                 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
302                              s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
303             } else {
304                 deloco_rgb8(ptr - s->image_linesize, s->row_size,
305                             s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
306             }
307         }
308         s->y++;
309         if (s->y == s->height) {
310             s->state |= PNG_ALLIMAGE;
311             if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
312                 if (s->bit_depth == 16) {
313                     deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
314                                  s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
315                 } else {
316                     deloco_rgb8(ptr, s->row_size,
317                                 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
318                 }
319             }
320         }
321     } else {
322         got_line = 0;
323         for(;;) {
324             ptr = s->image_buf + s->image_linesize * s->y;
325             if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
326                 /* if we already read one row, it is time to stop to
327                    wait for the next one */
328                 if (got_line)
329                     break;
330                 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
331                                s->last_row, s->pass_row_size, s->bpp);
332                 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
333                 got_line = 1;
334             }
335             if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
336                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
337                                        s->color_type, s->last_row);
338             }
339             s->y++;
340             if (s->y == s->height) {
341                 memset(s->last_row, 0, s->row_size);
342                 for(;;) {
343                     if (s->pass == NB_PASSES - 1) {
344                         s->state |= PNG_ALLIMAGE;
345                         goto the_end;
346                     } else {
347                         s->pass++;
348                         s->y = 0;
349                         s->pass_row_size = ff_png_pass_row_size(s->pass,
350                                                              s->bits_per_pixel,
351                                                              s->width);
352                         s->crow_size = s->pass_row_size + 1;
353                         if (s->pass_row_size != 0)
354                             break;
355                         /* skip pass if empty row */
356                     }
357                 }
358             }
359         }
360     the_end: ;
361     }
362 }
363
364 static int png_decode_idat(PNGDecContext *s, int length)
365 {
366     int ret;
367     s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
368     s->zstream.next_in = (unsigned char *)s->gb.buffer;
369     bytestream2_skip(&s->gb, length);
370
371     /* decode one line if possible */
372     while (s->zstream.avail_in > 0) {
373         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
374         if (ret != Z_OK && ret != Z_STREAM_END) {
375             av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
376             return -1;
377         }
378         if (s->zstream.avail_out == 0) {
379             if (!(s->state & PNG_ALLIMAGE)) {
380                 png_handle_row(s);
381             }
382             s->zstream.avail_out = s->crow_size;
383             s->zstream.next_out = s->crow_buf;
384         }
385     }
386     return 0;
387 }
388
389 static int decode_frame(AVCodecContext *avctx,
390                         void *data, int *data_size,
391                         AVPacket *avpkt)
392 {
393     const uint8_t *buf = avpkt->data;
394     int buf_size = avpkt->size;
395     PNGDecContext * const s = avctx->priv_data;
396     AVFrame *picture = data;
397     AVFrame *p;
398     uint8_t *crow_buf_base = NULL;
399     uint32_t tag, length;
400     int64_t sig;
401     int ret;
402
403     FFSWAP(AVFrame *, s->current_picture, s->last_picture);
404     avctx->coded_frame= s->current_picture;
405     p = s->current_picture;
406
407     bytestream2_init(&s->gb, buf, buf_size);
408
409     /* check signature */
410     sig = bytestream2_get_be64(&s->gb);
411     if (sig != PNGSIG &&
412         sig != MNGSIG) {
413         av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
414         return -1;
415     }
416
417     s->y=
418     s->state=0;
419 //    memset(s, 0, sizeof(PNGDecContext));
420     /* init the zlib */
421     s->zstream.zalloc = ff_png_zalloc;
422     s->zstream.zfree = ff_png_zfree;
423     s->zstream.opaque = NULL;
424     ret = inflateInit(&s->zstream);
425     if (ret != Z_OK) {
426         av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
427         return -1;
428     }
429     for(;;) {
430         if (bytestream2_get_bytes_left(&s->gb) <= 0) {
431             av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
432             goto fail;
433         }
434
435         length = bytestream2_get_be32(&s->gb);
436         if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb))  {
437             av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
438             goto fail;
439         }
440         tag = bytestream2_get_le32(&s->gb);
441         if (avctx->debug & FF_DEBUG_STARTCODE)
442             av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
443                 (tag & 0xff),
444                 ((tag >> 8) & 0xff),
445                 ((tag >> 16) & 0xff),
446                 ((tag >> 24) & 0xff), length);
447         switch(tag) {
448         case MKTAG('I', 'H', 'D', 'R'):
449             if (length != 13)
450                 goto fail;
451             s->width  = bytestream2_get_be32(&s->gb);
452             s->height = bytestream2_get_be32(&s->gb);
453             if(av_image_check_size(s->width, s->height, 0, avctx)){
454                 s->width= s->height= 0;
455                 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
456                 goto fail;
457             }
458             s->bit_depth        = bytestream2_get_byte(&s->gb);
459             s->color_type       = bytestream2_get_byte(&s->gb);
460             s->compression_type = bytestream2_get_byte(&s->gb);
461             s->filter_type      = bytestream2_get_byte(&s->gb);
462             s->interlace_type   = bytestream2_get_byte(&s->gb);
463             bytestream2_skip(&s->gb, 4); /* crc */
464             s->state |= PNG_IHDR;
465             if (avctx->debug & FF_DEBUG_PICT_INFO)
466                 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
467                     s->width, s->height, s->bit_depth, s->color_type,
468                     s->compression_type, s->filter_type, s->interlace_type);
469             break;
470         case MKTAG('p', 'H', 'Y', 's'):
471             if (s->state & PNG_IDAT) {
472                 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
473                 goto fail;
474             }
475             avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
476             avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
477             if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
478                 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
479             bytestream2_skip(&s->gb, 1); /* unit specifier */
480             bytestream2_skip(&s->gb, 4); /* crc */
481             break;
482         case MKTAG('I', 'D', 'A', 'T'):
483             if (!(s->state & PNG_IHDR)) {
484                 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
485                 goto fail;
486             }
487             if (!(s->state & PNG_IDAT)) {
488                 /* init image info */
489                 avctx->width = s->width;
490                 avctx->height = s->height;
491
492                 s->channels = ff_png_get_nb_channels(s->color_type);
493                 s->bits_per_pixel = s->bit_depth * s->channels;
494                 s->bpp = (s->bits_per_pixel + 7) >> 3;
495                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
496
497                 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
498                     s->color_type == PNG_COLOR_TYPE_RGB) {
499                     avctx->pix_fmt = AV_PIX_FMT_RGB24;
500                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
501                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
502                     avctx->pix_fmt = AV_PIX_FMT_RGBA;
503                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
504                            s->color_type == PNG_COLOR_TYPE_GRAY) {
505                     avctx->pix_fmt = AV_PIX_FMT_GRAY8;
506                 } else if (s->bit_depth == 16 &&
507                            s->color_type == PNG_COLOR_TYPE_GRAY) {
508                     avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
509                 } else if (s->bit_depth == 16 &&
510                            s->color_type == PNG_COLOR_TYPE_RGB) {
511                     avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
512                 } else if (s->bit_depth == 16 &&
513                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
514                     avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
515                 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
516                            s->color_type == PNG_COLOR_TYPE_PALETTE) {
517                     avctx->pix_fmt = AV_PIX_FMT_PAL8;
518                 } else if (s->bit_depth == 1) {
519                     avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
520                 } else if (s->bit_depth == 8 &&
521                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
522                     avctx->pix_fmt = AV_PIX_FMT_Y400A;
523                 } else {
524                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
525                                                 "and color type %d\n",
526                                                  s->bit_depth, s->color_type);
527                     goto fail;
528                 }
529                 if(p->data[0])
530                     avctx->release_buffer(avctx, p);
531
532                 p->reference= 3;
533                 if(avctx->get_buffer(avctx, p) < 0){
534                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
535                     goto fail;
536                 }
537                 p->pict_type= AV_PICTURE_TYPE_I;
538                 p->key_frame= 1;
539                 p->interlaced_frame = !!s->interlace_type;
540
541                 /* compute the compressed row size */
542                 if (!s->interlace_type) {
543                     s->crow_size = s->row_size + 1;
544                 } else {
545                     s->pass = 0;
546                     s->pass_row_size = ff_png_pass_row_size(s->pass,
547                                                          s->bits_per_pixel,
548                                                          s->width);
549                     s->crow_size = s->pass_row_size + 1;
550                 }
551                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
552                         s->row_size, s->crow_size);
553                 s->image_buf = p->data[0];
554                 s->image_linesize = p->linesize[0];
555                 /* copy the palette if needed */
556                 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
557                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
558                 /* empty row is used if differencing to the first row */
559                 s->last_row = av_mallocz(s->row_size);
560                 if (!s->last_row)
561                     goto fail;
562                 if (s->interlace_type ||
563                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
564                     s->tmp_row = av_malloc(s->row_size);
565                     if (!s->tmp_row)
566                         goto fail;
567                 }
568                 /* compressed row */
569                 crow_buf_base = av_malloc(s->row_size + 16);
570                 if (!crow_buf_base)
571                     goto fail;
572
573                 /* we want crow_buf+1 to be 16-byte aligned */
574                 s->crow_buf = crow_buf_base + 15;
575                 s->zstream.avail_out = s->crow_size;
576                 s->zstream.next_out = s->crow_buf;
577             }
578             s->state |= PNG_IDAT;
579             if (png_decode_idat(s, length) < 0)
580                 goto fail;
581             bytestream2_skip(&s->gb, 4); /* crc */
582             break;
583         case MKTAG('P', 'L', 'T', 'E'):
584             {
585                 int n, i, r, g, b;
586
587                 if ((length % 3) != 0 || length > 256 * 3)
588                     goto skip_tag;
589                 /* read the palette */
590                 n = length / 3;
591                 for(i=0;i<n;i++) {
592                     r = bytestream2_get_byte(&s->gb);
593                     g = bytestream2_get_byte(&s->gb);
594                     b = bytestream2_get_byte(&s->gb);
595                     s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
596                 }
597                 for(;i<256;i++) {
598                     s->palette[i] = (0xFFU << 24);
599                 }
600                 s->state |= PNG_PLTE;
601                 bytestream2_skip(&s->gb, 4); /* crc */
602             }
603             break;
604         case MKTAG('t', 'R', 'N', 'S'):
605             {
606                 int v, i;
607
608                 /* read the transparency. XXX: Only palette mode supported */
609                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
610                     length > 256 ||
611                     !(s->state & PNG_PLTE))
612                     goto skip_tag;
613                 for(i=0;i<length;i++) {
614                     v = bytestream2_get_byte(&s->gb);
615                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
616                 }
617                 bytestream2_skip(&s->gb, 4); /* crc */
618             }
619             break;
620         case MKTAG('I', 'E', 'N', 'D'):
621             if (!(s->state & PNG_ALLIMAGE))
622                 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
623             if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
624                 goto fail;
625             }
626             bytestream2_skip(&s->gb, 4); /* crc */
627             goto exit_loop;
628         default:
629             /* skip tag */
630         skip_tag:
631             bytestream2_skip(&s->gb, length + 4);
632             break;
633         }
634     }
635  exit_loop:
636
637     if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
638         int i, j;
639         uint8_t *pd = s->current_picture->data[0];
640         for(j=0; j < s->height; j++) {
641             for(i=s->width/8-1; i>=0; i--) {
642                 pd[8*i+7]=  pd[i]    &1;
643                 pd[8*i+6]= (pd[i]>>1)&1;
644                 pd[8*i+5]= (pd[i]>>2)&1;
645                 pd[8*i+4]= (pd[i]>>3)&1;
646                 pd[8*i+3]= (pd[i]>>4)&1;
647                 pd[8*i+2]= (pd[i]>>5)&1;
648                 pd[8*i+1]= (pd[i]>>6)&1;
649                 pd[8*i+0]=  pd[i]>>7;
650             }
651             pd += s->image_linesize;
652         }
653     }
654     if(s->bits_per_pixel == 2){
655         int i, j;
656         uint8_t *pd = s->current_picture->data[0];
657         for(j=0; j < s->height; j++) {
658             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
659             for(i=s->width/4-1; i>=0; i--) {
660                 pd[4*i+3]=  pd[i]    &3;
661                 pd[4*i+2]= (pd[i]>>2)&3;
662                 pd[4*i+1]= (pd[i]>>4)&3;
663                 pd[4*i+0]=  pd[i]>>6;
664             }
665             } else {
666                 for(i=s->width/4-1; i>=0; i--) {
667                     pd[4*i+3]= ( pd[i]    &3)*0x55;
668                     pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
669                     pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
670                     pd[4*i+0]= ( pd[i]>>6   )*0x55;
671                 }
672             }
673             pd += s->image_linesize;
674         }
675     }
676     if(s->bits_per_pixel == 4){
677         int i, j;
678         uint8_t *pd = s->current_picture->data[0];
679         for(j=0; j < s->height; j++) {
680             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
681             for(i=s->width/2-1; i>=0; i--) {
682                 pd[2*i+1]= pd[i]&15;
683                 pd[2*i+0]= pd[i]>>4;
684             }
685             } else {
686                 for(i=s->width/2-1; i>=0; i--) {
687                     pd[2*i+1]= (pd[i]&15)*0x11;
688                     pd[2*i+0]= (pd[i]>>4)*0x11;
689                 }
690             }
691             pd += s->image_linesize;
692         }
693     }
694
695      /* handle p-frames only if a predecessor frame is available */
696      if(s->last_picture->data[0] != NULL) {
697          if(   !(avpkt->flags & AV_PKT_FLAG_KEY)
698             && s->last_picture->width == s->current_picture->width
699             && s->last_picture->height== s->current_picture->height
700          ) {
701             int i, j;
702             uint8_t *pd = s->current_picture->data[0];
703             uint8_t *pd_last = s->last_picture->data[0];
704
705             for(j=0; j < s->height; j++) {
706                 for(i=0; i < s->width * s->bpp; i++) {
707                     pd[i] += pd_last[i];
708                 }
709                 pd += s->image_linesize;
710                 pd_last += s->image_linesize;
711             }
712         }
713     }
714
715     *picture= *s->current_picture;
716     *data_size = sizeof(AVFrame);
717
718     ret = bytestream2_tell(&s->gb);
719  the_end:
720     inflateEnd(&s->zstream);
721     av_free(crow_buf_base);
722     s->crow_buf = NULL;
723     av_freep(&s->last_row);
724     av_freep(&s->tmp_row);
725     return ret;
726  fail:
727     ret = -1;
728     goto the_end;
729 }
730
731 static av_cold int png_dec_init(AVCodecContext *avctx)
732 {
733     PNGDecContext *s = avctx->priv_data;
734
735     s->current_picture = &s->picture1;
736     s->last_picture = &s->picture2;
737     avcodec_get_frame_defaults(&s->picture1);
738     avcodec_get_frame_defaults(&s->picture2);
739
740     ff_pngdsp_init(&s->dsp);
741
742     s->avctx = avctx;
743
744     return 0;
745 }
746
747 static av_cold int png_dec_end(AVCodecContext *avctx)
748 {
749     PNGDecContext *s = avctx->priv_data;
750
751     if (s->picture1.data[0])
752         avctx->release_buffer(avctx, &s->picture1);
753     if (s->picture2.data[0])
754         avctx->release_buffer(avctx, &s->picture2);
755
756     return 0;
757 }
758
759 AVCodec ff_png_decoder = {
760     .name           = "png",
761     .type           = AVMEDIA_TYPE_VIDEO,
762     .id             = AV_CODEC_ID_PNG,
763     .priv_data_size = sizeof(PNGDecContext),
764     .init           = png_dec_init,
765     .close          = png_dec_end,
766     .decode         = decode_frame,
767     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
768     .long_name      = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
769 };