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