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