]> git.sesse.net Git - ffmpeg/blob - libavcodec/pngdec.c
Fix typo in v410 decoder.
[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     int64_t sig;
399     int ret;
400
401     FFSWAP(AVFrame *, s->current_picture, s->last_picture);
402     avctx->coded_frame= s->current_picture;
403     p = s->current_picture;
404
405     bytestream2_init(&s->gb, buf, buf_size);
406
407     /* check signature */
408     sig = bytestream2_get_be64(&s->gb);
409     if (sig != PNGSIG &&
410         sig != MNGSIG) {
411         av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
412         return -1;
413     }
414
415     s->y=
416     s->state=0;
417 //    memset(s, 0, sizeof(PNGDecContext));
418     /* init the zlib */
419     s->zstream.zalloc = ff_png_zalloc;
420     s->zstream.zfree = ff_png_zfree;
421     s->zstream.opaque = NULL;
422     ret = inflateInit(&s->zstream);
423     if (ret != Z_OK)
424         return -1;
425     for(;;) {
426         if (bytestream2_get_bytes_left(&s->gb) <= 0) {
427             av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
428             goto fail;
429         }
430
431         length = bytestream2_get_be32(&s->gb);
432         if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb))  {
433             av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
434             goto fail;
435         }
436         tag = bytestream2_get_le32(&s->gb);
437         if (avctx->debug & FF_DEBUG_STARTCODE)
438             av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
439                 (tag & 0xff),
440                 ((tag >> 8) & 0xff),
441                 ((tag >> 16) & 0xff),
442                 ((tag >> 24) & 0xff), length);
443         switch(tag) {
444         case MKTAG('I', 'H', 'D', 'R'):
445             if (length != 13)
446                 goto fail;
447             s->width  = bytestream2_get_be32(&s->gb);
448             s->height = bytestream2_get_be32(&s->gb);
449             if(av_image_check_size(s->width, s->height, 0, avctx)){
450                 s->width= s->height= 0;
451                 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
452                 goto fail;
453             }
454             s->bit_depth        = bytestream2_get_byte(&s->gb);
455             s->color_type       = bytestream2_get_byte(&s->gb);
456             s->compression_type = bytestream2_get_byte(&s->gb);
457             s->filter_type      = bytestream2_get_byte(&s->gb);
458             s->interlace_type   = bytestream2_get_byte(&s->gb);
459             bytestream2_skip(&s->gb, 4); /* crc */
460             s->state |= PNG_IHDR;
461             if (avctx->debug & FF_DEBUG_PICT_INFO)
462                 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",
463                     s->width, s->height, s->bit_depth, s->color_type,
464                     s->compression_type, s->filter_type, s->interlace_type);
465             break;
466         case MKTAG('I', 'D', 'A', 'T'):
467             if (!(s->state & PNG_IHDR)) {
468                 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
469                 goto fail;
470             }
471             if (!(s->state & PNG_IDAT)) {
472                 /* init image info */
473                 avctx->width = s->width;
474                 avctx->height = s->height;
475
476                 s->channels = ff_png_get_nb_channels(s->color_type);
477                 s->bits_per_pixel = s->bit_depth * s->channels;
478                 s->bpp = (s->bits_per_pixel + 7) >> 3;
479                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
480
481                 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
482                     s->color_type == PNG_COLOR_TYPE_RGB) {
483                     avctx->pix_fmt = PIX_FMT_RGB24;
484                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
485                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
486                     avctx->pix_fmt = PIX_FMT_RGBA;
487                 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
488                            s->color_type == PNG_COLOR_TYPE_GRAY) {
489                     avctx->pix_fmt = PIX_FMT_GRAY8;
490                 } else if (s->bit_depth == 16 &&
491                            s->color_type == PNG_COLOR_TYPE_GRAY) {
492                     avctx->pix_fmt = PIX_FMT_GRAY16BE;
493                 } else if (s->bit_depth == 16 &&
494                            s->color_type == PNG_COLOR_TYPE_RGB) {
495                     avctx->pix_fmt = PIX_FMT_RGB48BE;
496                 } else if (s->bit_depth == 16 &&
497                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
498                     avctx->pix_fmt = PIX_FMT_RGBA64BE;
499                 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
500                            s->color_type == PNG_COLOR_TYPE_PALETTE) {
501                     avctx->pix_fmt = PIX_FMT_PAL8;
502                 } else if (s->bit_depth == 1) {
503                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
504                 } else if (s->bit_depth == 8 &&
505                            s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
506                     avctx->pix_fmt = PIX_FMT_Y400A;
507                 } else {
508                     av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
509                                                 "and color type %d\n",
510                                                  s->bit_depth, s->color_type);
511                     goto fail;
512                 }
513                 if(p->data[0])
514                     avctx->release_buffer(avctx, p);
515
516                 p->reference= 3;
517                 if(avctx->get_buffer(avctx, p) < 0){
518                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
519                     goto fail;
520                 }
521                 p->pict_type= AV_PICTURE_TYPE_I;
522                 p->key_frame= 1;
523                 p->interlaced_frame = !!s->interlace_type;
524
525                 /* compute the compressed row size */
526                 if (!s->interlace_type) {
527                     s->crow_size = s->row_size + 1;
528                 } else {
529                     s->pass = 0;
530                     s->pass_row_size = ff_png_pass_row_size(s->pass,
531                                                          s->bits_per_pixel,
532                                                          s->width);
533                     s->crow_size = s->pass_row_size + 1;
534                 }
535                 av_dlog(avctx, "row_size=%d crow_size =%d\n",
536                         s->row_size, s->crow_size);
537                 s->image_buf = p->data[0];
538                 s->image_linesize = p->linesize[0];
539                 /* copy the palette if needed */
540                 if (avctx->pix_fmt == PIX_FMT_PAL8)
541                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
542                 /* empty row is used if differencing to the first row */
543                 s->last_row = av_mallocz(s->row_size);
544                 if (!s->last_row)
545                     goto fail;
546                 if (s->interlace_type ||
547                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
548                     s->tmp_row = av_malloc(s->row_size);
549                     if (!s->tmp_row)
550                         goto fail;
551                 }
552                 /* compressed row */
553                 crow_buf_base = av_malloc(s->row_size + 16);
554                 if (!crow_buf_base)
555                     goto fail;
556
557                 /* we want crow_buf+1 to be 16-byte aligned */
558                 s->crow_buf = crow_buf_base + 15;
559                 s->zstream.avail_out = s->crow_size;
560                 s->zstream.next_out = s->crow_buf;
561             }
562             s->state |= PNG_IDAT;
563             if (png_decode_idat(s, length) < 0)
564                 goto fail;
565             bytestream2_skip(&s->gb, 4); /* crc */
566             break;
567         case MKTAG('P', 'L', 'T', 'E'):
568             {
569                 int n, i, r, g, b;
570
571                 if ((length % 3) != 0 || length > 256 * 3)
572                     goto skip_tag;
573                 /* read the palette */
574                 n = length / 3;
575                 for(i=0;i<n;i++) {
576                     r = bytestream2_get_byte(&s->gb);
577                     g = bytestream2_get_byte(&s->gb);
578                     b = bytestream2_get_byte(&s->gb);
579                     s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
580                 }
581                 for(;i<256;i++) {
582                     s->palette[i] = (0xff << 24);
583                 }
584                 s->state |= PNG_PLTE;
585                 bytestream2_skip(&s->gb, 4); /* crc */
586             }
587             break;
588         case MKTAG('t', 'R', 'N', 'S'):
589             {
590                 int v, i;
591
592                 /* read the transparency. XXX: Only palette mode supported */
593                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
594                     length > 256 ||
595                     !(s->state & PNG_PLTE))
596                     goto skip_tag;
597                 for(i=0;i<length;i++) {
598                     v = bytestream2_get_byte(&s->gb);
599                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
600                 }
601                 bytestream2_skip(&s->gb, 4); /* crc */
602             }
603             break;
604         case MKTAG('I', 'E', 'N', 'D'):
605             if (!(s->state & PNG_ALLIMAGE))
606                 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
607             if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
608                 goto fail;
609             }
610             bytestream2_skip(&s->gb, 4); /* crc */
611             goto exit_loop;
612         default:
613             /* skip tag */
614         skip_tag:
615             bytestream2_skip(&s->gb, length + 4);
616             break;
617         }
618     }
619  exit_loop:
620
621     if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
622         int i, j;
623         uint8_t *pd = s->current_picture->data[0];
624         for(j=0; j < s->height; j++) {
625             for(i=s->width/8-1; i>=0; i--) {
626                 pd[8*i+7]=  pd[i]    &1;
627                 pd[8*i+6]= (pd[i]>>1)&1;
628                 pd[8*i+5]= (pd[i]>>2)&1;
629                 pd[8*i+4]= (pd[i]>>3)&1;
630                 pd[8*i+3]= (pd[i]>>4)&1;
631                 pd[8*i+2]= (pd[i]>>5)&1;
632                 pd[8*i+1]= (pd[i]>>6)&1;
633                 pd[8*i+0]=  pd[i]>>7;
634             }
635             pd += s->image_linesize;
636         }
637     }
638     if(s->bits_per_pixel == 2){
639         int i, j;
640         uint8_t *pd = s->current_picture->data[0];
641         for(j=0; j < s->height; j++) {
642             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
643             for(i=s->width/4-1; i>=0; i--) {
644                 pd[4*i+3]=  pd[i]    &3;
645                 pd[4*i+2]= (pd[i]>>2)&3;
646                 pd[4*i+1]= (pd[i]>>4)&3;
647                 pd[4*i+0]=  pd[i]>>6;
648             }
649             } else {
650                 for(i=s->width/4-1; i>=0; i--) {
651                     pd[4*i+3]= ( pd[i]    &3)*0x55;
652                     pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
653                     pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
654                     pd[4*i+0]= ( pd[i]>>6   )*0x55;
655                 }
656             }
657             pd += s->image_linesize;
658         }
659     }
660     if(s->bits_per_pixel == 4){
661         int i, j;
662         uint8_t *pd = s->current_picture->data[0];
663         for(j=0; j < s->height; j++) {
664             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
665             for(i=s->width/2-1; i>=0; i--) {
666                 pd[2*i+1]= pd[i]&15;
667                 pd[2*i+0]= pd[i]>>4;
668             }
669             } else {
670                 for(i=s->width/2-1; i>=0; i--) {
671                     pd[2*i+1]= (pd[i]&15)*0x11;
672                     pd[2*i+0]= (pd[i]>>4)*0x11;
673                 }
674             }
675             pd += s->image_linesize;
676         }
677     }
678
679      /* handle p-frames only if a predecessor frame is available */
680      if(s->last_picture->data[0] != NULL) {
681          if(   !(avpkt->flags & AV_PKT_FLAG_KEY)
682             && s->last_picture->width == s->current_picture->width
683             && s->last_picture->height== s->current_picture->height
684          ) {
685             int i, j;
686             uint8_t *pd = s->current_picture->data[0];
687             uint8_t *pd_last = s->last_picture->data[0];
688
689             for(j=0; j < s->height; j++) {
690                 for(i=0; i < s->width * s->bpp; i++) {
691                     pd[i] += pd_last[i];
692                 }
693                 pd += s->image_linesize;
694                 pd_last += s->image_linesize;
695             }
696         }
697     }
698
699     *picture= *s->current_picture;
700     *data_size = sizeof(AVFrame);
701
702     ret = bytestream2_tell(&s->gb);
703  the_end:
704     inflateEnd(&s->zstream);
705     av_free(crow_buf_base);
706     s->crow_buf = NULL;
707     av_freep(&s->last_row);
708     av_freep(&s->tmp_row);
709     return ret;
710  fail:
711     ret = -1;
712     goto the_end;
713 }
714
715 static av_cold int png_dec_init(AVCodecContext *avctx)
716 {
717     PNGDecContext *s = avctx->priv_data;
718
719     s->current_picture = &s->picture1;
720     s->last_picture = &s->picture2;
721     avcodec_get_frame_defaults(&s->picture1);
722     avcodec_get_frame_defaults(&s->picture2);
723
724     ff_pngdsp_init(&s->dsp);
725
726     return 0;
727 }
728
729 static av_cold int png_dec_end(AVCodecContext *avctx)
730 {
731     PNGDecContext *s = avctx->priv_data;
732
733     if (s->picture1.data[0])
734         avctx->release_buffer(avctx, &s->picture1);
735     if (s->picture2.data[0])
736         avctx->release_buffer(avctx, &s->picture2);
737
738     return 0;
739 }
740
741 AVCodec ff_png_decoder = {
742     .name           = "png",
743     .type           = AVMEDIA_TYPE_VIDEO,
744     .id             = CODEC_ID_PNG,
745     .priv_data_size = sizeof(PNGDecContext),
746     .init           = png_dec_init,
747     .close          = png_dec_end,
748     .decode         = decode_frame,
749     .capabilities   = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
750     .long_name      = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
751 };