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