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