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