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