]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/png.c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
[frescor/ffmpeg.git] / libavcodec / png.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
23 /* TODO:
24  * - add 2, 4 and 16 bit depth support
25  * - use filters when generating a png (better compression)
26  */
27
28 #ifdef CONFIG_ZLIB
29 #include <zlib.h>
30
31 //#define DEBUG
32
33 #define PNG_COLOR_MASK_PALETTE    1
34 #define PNG_COLOR_MASK_COLOR      2
35 #define PNG_COLOR_MASK_ALPHA      4
36
37 #define PNG_COLOR_TYPE_GRAY 0
38 #define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
39 #define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
40 #define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
41 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
42
43 #define PNG_FILTER_VALUE_NONE  0
44 #define PNG_FILTER_VALUE_SUB   1
45 #define PNG_FILTER_VALUE_UP    2
46 #define PNG_FILTER_VALUE_AVG   3
47 #define PNG_FILTER_VALUE_PAETH 4
48
49 #define PNG_IHDR      0x0001
50 #define PNG_IDAT      0x0002
51 #define PNG_ALLIMAGE  0x0004
52 #define PNG_PLTE      0x0008
53
54 #define NB_PASSES 7
55
56 #define IOBUF_SIZE 4096
57
58 typedef struct PNGContext {
59     uint8_t *bytestream;
60     uint8_t *bytestream_start;
61     uint8_t *bytestream_end;
62     AVFrame picture;
63
64     int state;
65     int width, height;
66     int bit_depth;
67     int color_type;
68     int compression_type;
69     int interlace_type;
70     int filter_type;
71     int channels;
72     int bits_per_pixel;
73     int bpp;
74
75     uint8_t *image_buf;
76     int image_linesize;
77     uint32_t palette[256];
78     uint8_t *crow_buf;
79     uint8_t *last_row;
80     uint8_t *tmp_row;
81     int pass;
82     int crow_size; /* compressed row size (include filter type) */
83     int row_size; /* decompressed row size */
84     int pass_row_size; /* decompress row size of the current pass */
85     int y;
86     z_stream zstream;
87     uint8_t buf[IOBUF_SIZE];
88 } PNGContext;
89
90 static unsigned int get32(uint8_t **b){
91     (*b) += 4;
92     return ((*b)[-4]<<24) + ((*b)[-3]<<16) + ((*b)[-2]<<8) + (*b)[-1];
93 }
94
95 #ifdef CONFIG_ENCODERS
96 static void put32(uint8_t **b, unsigned int v){
97     *(*b)++= v>>24;
98     *(*b)++= v>>16;
99     *(*b)++= v>>8;
100     *(*b)++= v;
101 }
102 #endif
103
104 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
105
106 /* Mask to determine which y pixels are valid in a pass */
107 static const uint8_t png_pass_ymask[NB_PASSES] = {
108     0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
109 };
110
111 /* Mask to determine which y pixels can be written in a pass */
112 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
113     0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
114 };
115
116 /* minimum x value */
117 static const uint8_t png_pass_xmin[NB_PASSES] = {
118     0, 4, 0, 2, 0, 1, 0
119 };
120
121 /* x shift to get row width */
122 static const uint8_t png_pass_xshift[NB_PASSES] = {
123     3, 3, 2, 2, 1, 1, 0
124 };
125
126 /* Mask to determine which pixels are valid in a pass */
127 static const uint8_t png_pass_mask[NB_PASSES] = {
128     0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
129 };
130
131 /* Mask to determine which pixels to overwrite while displaying */
132 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
133     0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
134 };
135 #if 0
136 static int png_probe(AVProbeData *pd)
137 {
138     if (pd->buf_size >= 8 &&
139         memcmp(pd->buf, pngsig, 8) == 0)
140         return AVPROBE_SCORE_MAX;
141     else
142         return 0;
143 }
144 #endif
145 static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
146 {
147     if(items >= UINT_MAX / size)
148         return NULL;
149     return av_malloc(items * size);
150 }
151
152 static void png_zfree(void *opaque, void *ptr)
153 {
154     av_free(ptr);
155 }
156
157 static int png_get_nb_channels(int color_type)
158 {
159     int channels;
160     channels = 1;
161     if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
162         PNG_COLOR_MASK_COLOR)
163         channels = 3;
164     if (color_type & PNG_COLOR_MASK_ALPHA)
165         channels++;
166     return channels;
167 }
168
169 /* compute the row size of an interleaved pass */
170 static int png_pass_row_size(int pass, int bits_per_pixel, int width)
171 {
172     int shift, xmin, pass_width;
173
174     xmin = png_pass_xmin[pass];
175     if (width <= xmin)
176         return 0;
177     shift = png_pass_xshift[pass];
178     pass_width = (width - xmin + (1 << shift) - 1) >> shift;
179     return (pass_width * bits_per_pixel + 7) >> 3;
180 }
181
182 /* NOTE: we try to construct a good looking image at each pass. width
183    is the original image width. We also do pixel format convertion at
184    this stage */
185 static void png_put_interlaced_row(uint8_t *dst, int width,
186                                    int bits_per_pixel, int pass,
187                                    int color_type, const uint8_t *src)
188 {
189     int x, mask, dsp_mask, j, src_x, b, bpp;
190     uint8_t *d;
191     const uint8_t *s;
192
193     mask = png_pass_mask[pass];
194     dsp_mask = png_pass_dsp_mask[pass];
195     switch(bits_per_pixel) {
196     case 1:
197         /* we must intialize the line to zero before writing to it */
198         if (pass == 0)
199             memset(dst, 0, (width + 7) >> 3);
200         src_x = 0;
201         for(x = 0; x < width; x++) {
202             j = (x & 7);
203             if ((dsp_mask << j) & 0x80) {
204                 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
205                 dst[x >> 3] |= b << (7 - j);
206             }
207             if ((mask << j) & 0x80)
208                 src_x++;
209         }
210         break;
211     default:
212         bpp = bits_per_pixel >> 3;
213         d = dst;
214         s = src;
215         if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
216             for(x = 0; x < width; x++) {
217                 j = x & 7;
218                 if ((dsp_mask << j) & 0x80) {
219                     *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
220                 }
221                 d += bpp;
222                 if ((mask << j) & 0x80)
223                     s += bpp;
224             }
225         } else {
226             for(x = 0; x < width; x++) {
227                 j = x & 7;
228                 if ((dsp_mask << j) & 0x80) {
229                     memcpy(d, s, bpp);
230                 }
231                 d += bpp;
232                 if ((mask << j) & 0x80)
233                     s += bpp;
234             }
235         }
236         break;
237     }
238 }
239
240 #ifdef CONFIG_ENCODERS
241 static void png_get_interlaced_row(uint8_t *dst, int row_size,
242                                    int bits_per_pixel, int pass,
243                                    const uint8_t *src, int width)
244 {
245     int x, mask, dst_x, j, b, bpp;
246     uint8_t *d;
247     const uint8_t *s;
248
249     mask = png_pass_mask[pass];
250     switch(bits_per_pixel) {
251     case 1:
252         memset(dst, 0, row_size);
253         dst_x = 0;
254         for(x = 0; x < width; x++) {
255             j = (x & 7);
256             if ((mask << j) & 0x80) {
257                 b = (src[x >> 3] >> (7 - j)) & 1;
258                 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
259                 dst_x++;
260             }
261         }
262         break;
263     default:
264         bpp = bits_per_pixel >> 3;
265         d = dst;
266         s = src;
267         for(x = 0; x < width; x++) {
268             j = x & 7;
269             if ((mask << j) & 0x80) {
270                 memcpy(d, s, bpp);
271                 d += bpp;
272             }
273             s += bpp;
274         }
275         break;
276     }
277 }
278 #endif
279
280 /* XXX: optimize */
281 /* NOTE: 'dst' can be equal to 'last' */
282 static void png_filter_row(uint8_t *dst, int filter_type,
283                            uint8_t *src, uint8_t *last, int size, int bpp)
284 {
285     int i, p;
286
287     switch(filter_type) {
288     case PNG_FILTER_VALUE_NONE:
289         memcpy(dst, src, size);
290         break;
291     case PNG_FILTER_VALUE_SUB:
292         for(i = 0; i < bpp; i++) {
293             dst[i] = src[i];
294         }
295         for(i = bpp; i < size; i++) {
296             p = dst[i - bpp];
297             dst[i] = p + src[i];
298         }
299         break;
300     case PNG_FILTER_VALUE_UP:
301         for(i = 0; i < size; i++) {
302             p = last[i];
303             dst[i] = p + src[i];
304         }
305         break;
306     case PNG_FILTER_VALUE_AVG:
307         for(i = 0; i < bpp; i++) {
308             p = (last[i] >> 1);
309             dst[i] = p + src[i];
310         }
311         for(i = bpp; i < size; i++) {
312             p = ((dst[i - bpp] + last[i]) >> 1);
313             dst[i] = p + src[i];
314         }
315         break;
316     case PNG_FILTER_VALUE_PAETH:
317         for(i = 0; i < bpp; i++) {
318             p = last[i];
319             dst[i] = p + src[i];
320         }
321         for(i = bpp; i < size; i++) {
322             int a, b, c, pa, pb, pc;
323
324             a = dst[i - bpp];
325             b = last[i];
326             c = last[i - bpp];
327
328             p = b - c;
329             pc = a - c;
330
331             pa = abs(p);
332             pb = abs(pc);
333             pc = abs(p + pc);
334
335             if (pa <= pb && pa <= pc)
336                 p = a;
337             else if (pb <= pc)
338                 p = b;
339             else
340                 p = c;
341             dst[i] = p + src[i];
342         }
343         break;
344     }
345 }
346
347 #ifdef CONFIG_ENCODERS
348 static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
349 {
350     uint8_t *d;
351     int j;
352     unsigned int v;
353
354     d = dst;
355     for(j = 0; j < width; j++) {
356         v = ((const uint32_t *)src)[j];
357         d[0] = v >> 16;
358         d[1] = v >> 8;
359         d[2] = v;
360         d[3] = v >> 24;
361         d += 4;
362     }
363 }
364 #endif
365
366 #ifdef CONFIG_DECODERS
367 static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
368 {
369     int j;
370     unsigned int r, g, b, a;
371
372     for(j = 0;j < width; j++) {
373         r = src[0];
374         g = src[1];
375         b = src[2];
376         a = src[3];
377         *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
378         dst += 4;
379         src += 4;
380     }
381 }
382
383 /* process exactly one decompressed row */
384 static void png_handle_row(PNGContext *s)
385 {
386     uint8_t *ptr, *last_row;
387     int got_line;
388
389     if (!s->interlace_type) {
390         ptr = s->image_buf + s->image_linesize * s->y;
391         /* need to swap bytes correctly for RGB_ALPHA */
392         if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
393             png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
394                            s->last_row, s->row_size, s->bpp);
395             memcpy(s->last_row, s->tmp_row, s->row_size);
396             convert_to_rgba32(ptr, s->tmp_row, s->width);
397         } else {
398             /* in normal case, we avoid one copy */
399             if (s->y == 0)
400                 last_row = s->last_row;
401             else
402                 last_row = ptr - s->image_linesize;
403
404             png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
405                            last_row, s->row_size, s->bpp);
406         }
407         s->y++;
408         if (s->y == s->height) {
409             s->state |= PNG_ALLIMAGE;
410         }
411     } else {
412         got_line = 0;
413         for(;;) {
414             ptr = s->image_buf + s->image_linesize * s->y;
415             if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
416                 /* if we already read one row, it is time to stop to
417                    wait for the next one */
418                 if (got_line)
419                     break;
420                 png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
421                                s->last_row, s->pass_row_size, s->bpp);
422                 memcpy(s->last_row, s->tmp_row, s->pass_row_size);
423                 got_line = 1;
424             }
425             if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
426                 /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
427                 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
428                                        s->color_type, s->last_row);
429             }
430             s->y++;
431             if (s->y == s->height) {
432                 for(;;) {
433                     if (s->pass == NB_PASSES - 1) {
434                         s->state |= PNG_ALLIMAGE;
435                         goto the_end;
436                     } else {
437                         s->pass++;
438                         s->y = 0;
439                         s->pass_row_size = png_pass_row_size(s->pass,
440                                                              s->bits_per_pixel,
441                                                              s->width);
442                         s->crow_size = s->pass_row_size + 1;
443                         if (s->pass_row_size != 0)
444                             break;
445                         /* skip pass if empty row */
446                     }
447                 }
448             }
449         }
450     the_end: ;
451     }
452 }
453
454 static int png_decode_idat(PNGContext *s, int length)
455 {
456     int ret;
457     s->zstream.avail_in = length;
458     s->zstream.next_in = s->bytestream;
459     s->bytestream += length;
460
461     if(s->bytestream > s->bytestream_end)
462         return -1;
463
464     /* decode one line if possible */
465     while (s->zstream.avail_in > 0) {
466         ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
467         if (ret != Z_OK && ret != Z_STREAM_END) {
468             return -1;
469         }
470         if (s->zstream.avail_out == 0) {
471             if (!(s->state & PNG_ALLIMAGE)) {
472                 png_handle_row(s);
473             }
474             s->zstream.avail_out = s->crow_size;
475             s->zstream.next_out = s->crow_buf;
476         }
477     }
478     return 0;
479 }
480
481 static int decode_frame(AVCodecContext *avctx,
482                         void *data, int *data_size,
483                         uint8_t *buf, int buf_size)
484 {
485     PNGContext * const s = avctx->priv_data;
486     AVFrame *picture = data;
487     AVFrame * const p= (AVFrame*)&s->picture;
488     uint32_t tag, length;
489     int ret, crc;
490
491     s->bytestream_start=
492     s->bytestream= buf;
493     s->bytestream_end= buf + buf_size;
494
495     /* check signature */
496     if (memcmp(s->bytestream, pngsig, 8) != 0)
497         return -1;
498     s->bytestream+= 8;
499     s->y=
500     s->state=0;
501 //    memset(s, 0, sizeof(PNGContext));
502     /* init the zlib */
503     s->zstream.zalloc = png_zalloc;
504     s->zstream.zfree = png_zfree;
505     s->zstream.opaque = NULL;
506     ret = inflateInit(&s->zstream);
507     if (ret != Z_OK)
508         return -1;
509     for(;;) {
510         int tag32;
511         if (s->bytestream >= s->bytestream_end)
512             goto fail;
513         length = get32(&s->bytestream);
514         if (length > 0x7fffffff)
515             goto fail;
516         tag32 = get32(&s->bytestream);
517         tag = bswap_32(tag32);
518 #ifdef DEBUG
519         av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
520                (tag & 0xff),
521                ((tag >> 8) & 0xff),
522                ((tag >> 16) & 0xff),
523                ((tag >> 24) & 0xff), length);
524 #endif
525         switch(tag) {
526         case MKTAG('I', 'H', 'D', 'R'):
527             if (length != 13)
528                 goto fail;
529             s->width = get32(&s->bytestream);
530             s->height = get32(&s->bytestream);
531             if(avcodec_check_dimensions(avctx, s->width, s->height)){
532                 s->width= s->height= 0;
533                 goto fail;
534             }
535             s->bit_depth = *s->bytestream++;
536             s->color_type = *s->bytestream++;
537             s->compression_type = *s->bytestream++;
538             s->filter_type = *s->bytestream++;
539             s->interlace_type = *s->bytestream++;
540             crc = get32(&s->bytestream);
541             s->state |= PNG_IHDR;
542 #ifdef DEBUG
543             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",
544                    s->width, s->height, s->bit_depth, s->color_type,
545                    s->compression_type, s->filter_type, s->interlace_type);
546 #endif
547             break;
548         case MKTAG('I', 'D', 'A', 'T'):
549             if (!(s->state & PNG_IHDR))
550                 goto fail;
551             if (!(s->state & PNG_IDAT)) {
552                 /* init image info */
553                 avctx->width = s->width;
554                 avctx->height = s->height;
555
556                 s->channels = png_get_nb_channels(s->color_type);
557                 s->bits_per_pixel = s->bit_depth * s->channels;
558                 s->bpp = (s->bits_per_pixel + 7) >> 3;
559                 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
560
561                 if (s->bit_depth == 8 &&
562                     s->color_type == PNG_COLOR_TYPE_RGB) {
563                     avctx->pix_fmt = PIX_FMT_RGB24;
564                 } else if (s->bit_depth == 8 &&
565                            s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
566                     avctx->pix_fmt = PIX_FMT_RGBA32;
567                 } else if (s->bit_depth == 8 &&
568                            s->color_type == PNG_COLOR_TYPE_GRAY) {
569                     avctx->pix_fmt = PIX_FMT_GRAY8;
570                 } else if (s->bit_depth == 1 &&
571                            s->color_type == PNG_COLOR_TYPE_GRAY) {
572                     avctx->pix_fmt = PIX_FMT_MONOBLACK;
573                 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
574                     avctx->pix_fmt = PIX_FMT_PAL8;
575                 } else {
576                     goto fail;
577                 }
578                 if(p->data[0])
579                     avctx->release_buffer(avctx, p);
580
581                 p->reference= 0;
582                 if(avctx->get_buffer(avctx, p) < 0){
583                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
584                     goto fail;
585                 }
586                 p->pict_type= FF_I_TYPE;
587                 p->key_frame= 1;
588                 p->interlaced_frame = !!s->interlace_type;
589
590                 /* compute the compressed row size */
591                 if (!s->interlace_type) {
592                     s->crow_size = s->row_size + 1;
593                 } else {
594                     s->pass = 0;
595                     s->pass_row_size = png_pass_row_size(s->pass,
596                                                          s->bits_per_pixel,
597                                                          s->width);
598                     s->crow_size = s->pass_row_size + 1;
599                 }
600 #ifdef DEBUG
601                 av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
602                        s->row_size, s->crow_size);
603 #endif
604                 s->image_buf = p->data[0];
605                 s->image_linesize = p->linesize[0];
606                 /* copy the palette if needed */
607                 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
608                     memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
609                 /* empty row is used if differencing to the first row */
610                 s->last_row = av_mallocz(s->row_size);
611                 if (!s->last_row)
612                     goto fail;
613                 if (s->interlace_type ||
614                     s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
615                     s->tmp_row = av_malloc(s->row_size);
616                     if (!s->tmp_row)
617                         goto fail;
618                 }
619                 /* compressed row */
620                 s->crow_buf = av_malloc(s->row_size + 1);
621                 if (!s->crow_buf)
622                     goto fail;
623                 s->zstream.avail_out = s->crow_size;
624                 s->zstream.next_out = s->crow_buf;
625             }
626             s->state |= PNG_IDAT;
627             if (png_decode_idat(s, length) < 0)
628                 goto fail;
629             /* skip crc */
630             crc = get32(&s->bytestream);
631             break;
632         case MKTAG('P', 'L', 'T', 'E'):
633             {
634                 int n, i, r, g, b;
635
636                 if ((length % 3) != 0 || length > 256 * 3)
637                     goto skip_tag;
638                 /* read the palette */
639                 n = length / 3;
640                 for(i=0;i<n;i++) {
641                     r = *s->bytestream++;
642                     g = *s->bytestream++;
643                     b = *s->bytestream++;
644                     s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
645                 }
646                 for(;i<256;i++) {
647                     s->palette[i] = (0xff << 24);
648                 }
649                 s->state |= PNG_PLTE;
650                 crc = get32(&s->bytestream);
651             }
652             break;
653         case MKTAG('t', 'R', 'N', 'S'):
654             {
655                 int v, i;
656
657                 /* read the transparency. XXX: Only palette mode supported */
658                 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
659                     length > 256 ||
660                     !(s->state & PNG_PLTE))
661                     goto skip_tag;
662                 for(i=0;i<length;i++) {
663                     v = *s->bytestream++;
664                     s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
665                 }
666                 crc = get32(&s->bytestream);
667             }
668             break;
669         case MKTAG('I', 'E', 'N', 'D'):
670             if (!(s->state & PNG_ALLIMAGE))
671                 goto fail;
672             crc = get32(&s->bytestream);
673             goto exit_loop;
674         default:
675             /* skip tag */
676         skip_tag:
677             s->bytestream += length + 4;
678             break;
679         }
680     }
681  exit_loop:
682     *picture= *(AVFrame*)&s->picture;
683     *data_size = sizeof(AVPicture);
684
685     ret = s->bytestream - s->bytestream_start;
686  the_end:
687     inflateEnd(&s->zstream);
688     av_freep(&s->crow_buf);
689     av_freep(&s->last_row);
690     av_freep(&s->tmp_row);
691     return ret;
692  fail:
693     ret = -1;
694     goto the_end;
695 }
696 #endif
697
698 #ifdef CONFIG_ENCODERS
699 static void png_write_chunk(uint8_t **f, uint32_t tag,
700                             const uint8_t *buf, int length)
701 {
702     uint32_t crc;
703     uint8_t tagbuf[4];
704
705     put32(f, length);
706     crc = crc32(0, Z_NULL, 0);
707     tagbuf[0] = tag;
708     tagbuf[1] = tag >> 8;
709     tagbuf[2] = tag >> 16;
710     tagbuf[3] = tag >> 24;
711     crc = crc32(crc, tagbuf, 4);
712     put32(f, bswap_32(tag));
713     if (length > 0) {
714         crc = crc32(crc, buf, length);
715         memcpy(*f, buf, length);
716         *f += length;
717     }
718     put32(f, crc);
719 }
720
721 /* XXX: use avcodec generic function ? */
722 static void to_be32(uint8_t *p, uint32_t v)
723 {
724     p[0] = v >> 24;
725     p[1] = v >> 16;
726     p[2] = v >> 8;
727     p[3] = v;
728 }
729
730 /* XXX: do filtering */
731 static int png_write_row(PNGContext *s, const uint8_t *data, int size)
732 {
733     int ret;
734
735     s->zstream.avail_in = size;
736     s->zstream.next_in = (uint8_t *)data;
737     while (s->zstream.avail_in > 0) {
738         ret = deflate(&s->zstream, Z_NO_FLUSH);
739         if (ret != Z_OK)
740             return -1;
741         if (s->zstream.avail_out == 0) {
742             if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
743                 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
744             s->zstream.avail_out = IOBUF_SIZE;
745             s->zstream.next_out = s->buf;
746         }
747     }
748     return 0;
749 }
750 #endif /* CONFIG_ENCODERS */
751
752 static int common_init(AVCodecContext *avctx){
753     PNGContext *s = avctx->priv_data;
754
755     avcodec_get_frame_defaults((AVFrame*)&s->picture);
756     avctx->coded_frame= (AVFrame*)&s->picture;
757 //    s->avctx= avctx;
758
759     return 0;
760 }
761
762 #ifdef CONFIG_ENCODERS
763 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
764     PNGContext *s = avctx->priv_data;
765     AVFrame *pict = data;
766     AVFrame * const p= (AVFrame*)&s->picture;
767     int bit_depth, color_type, y, len, row_size, ret, is_progressive;
768     int bits_per_pixel, pass_row_size;
769     uint8_t *ptr;
770     uint8_t *crow_buf = NULL;
771     uint8_t *tmp_buf = NULL;
772
773     *p = *pict;
774     p->pict_type= FF_I_TYPE;
775     p->key_frame= 1;
776
777     s->bytestream_start=
778     s->bytestream= buf;
779     s->bytestream_end= buf+buf_size;
780
781     is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
782     switch(avctx->pix_fmt) {
783     case PIX_FMT_RGBA32:
784         bit_depth = 8;
785         color_type = PNG_COLOR_TYPE_RGB_ALPHA;
786         break;
787     case PIX_FMT_RGB24:
788         bit_depth = 8;
789         color_type = PNG_COLOR_TYPE_RGB;
790         break;
791     case PIX_FMT_GRAY8:
792         bit_depth = 8;
793         color_type = PNG_COLOR_TYPE_GRAY;
794         break;
795     case PIX_FMT_MONOBLACK:
796         bit_depth = 1;
797         color_type = PNG_COLOR_TYPE_GRAY;
798         break;
799     case PIX_FMT_PAL8:
800         bit_depth = 8;
801         color_type = PNG_COLOR_TYPE_PALETTE;
802         break;
803     default:
804         return -1;
805     }
806     bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
807     row_size = (avctx->width * bits_per_pixel + 7) >> 3;
808
809     s->zstream.zalloc = png_zalloc;
810     s->zstream.zfree = png_zfree;
811     s->zstream.opaque = NULL;
812     ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
813                        Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
814     if (ret != Z_OK)
815         return -1;
816     crow_buf = av_malloc(row_size + 1);
817     if (!crow_buf)
818         goto fail;
819     if (is_progressive) {
820         tmp_buf = av_malloc(row_size + 1);
821         if (!tmp_buf)
822             goto fail;
823     }
824
825     /* write png header */
826     memcpy(s->bytestream, pngsig, 8);
827     s->bytestream += 8;
828
829     to_be32(s->buf, avctx->width);
830     to_be32(s->buf + 4, avctx->height);
831     s->buf[8] = bit_depth;
832     s->buf[9] = color_type;
833     s->buf[10] = 0; /* compression type */
834     s->buf[11] = 0; /* filter type */
835     s->buf[12] = is_progressive; /* interlace type */
836
837     png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
838
839     /* put the palette if needed */
840     if (color_type == PNG_COLOR_TYPE_PALETTE) {
841         int has_alpha, alpha, i;
842         unsigned int v;
843         uint32_t *palette;
844         uint8_t *alpha_ptr;
845
846         palette = (uint32_t *)p->data[1];
847         ptr = s->buf;
848         alpha_ptr = s->buf + 256 * 3;
849         has_alpha = 0;
850         for(i = 0; i < 256; i++) {
851             v = palette[i];
852             alpha = v >> 24;
853             if (alpha != 0xff)
854                 has_alpha = 1;
855             *alpha_ptr++ = alpha;
856             ptr[0] = v >> 16;
857             ptr[1] = v >> 8;
858             ptr[2] = v;
859             ptr += 3;
860         }
861         png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
862         if (has_alpha) {
863             png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
864         }
865     }
866
867     /* now put each row */
868     s->zstream.avail_out = IOBUF_SIZE;
869     s->zstream.next_out = s->buf;
870     if (is_progressive) {
871         uint8_t *ptr1;
872         int pass;
873
874         for(pass = 0; pass < NB_PASSES; pass++) {
875             /* NOTE: a pass is completely omited if no pixels would be
876                output */
877             pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
878             if (pass_row_size > 0) {
879                 for(y = 0; y < avctx->height; y++) {
880                     if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
881                         ptr = p->data[0] + y * p->linesize[0];
882                         if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
883                             convert_from_rgba32(tmp_buf, ptr, avctx->width);
884                             ptr1 = tmp_buf;
885                         } else {
886                             ptr1 = ptr;
887                         }
888                         png_get_interlaced_row(crow_buf + 1, pass_row_size,
889                                                bits_per_pixel, pass,
890                                                ptr1, avctx->width);
891                         crow_buf[0] = PNG_FILTER_VALUE_NONE;
892                         png_write_row(s, crow_buf, pass_row_size + 1);
893                     }
894                 }
895             }
896         }
897     } else {
898         for(y = 0; y < avctx->height; y++) {
899             ptr = p->data[0] + y * p->linesize[0];
900             if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
901                 convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
902             else
903                 memcpy(crow_buf + 1, ptr, row_size);
904             crow_buf[0] = PNG_FILTER_VALUE_NONE;
905             png_write_row(s, crow_buf, row_size + 1);
906         }
907     }
908     /* compress last bytes */
909     for(;;) {
910         ret = deflate(&s->zstream, Z_FINISH);
911         if (ret == Z_OK || ret == Z_STREAM_END) {
912             len = IOBUF_SIZE - s->zstream.avail_out;
913             if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
914                 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
915             }
916             s->zstream.avail_out = IOBUF_SIZE;
917             s->zstream.next_out = s->buf;
918             if (ret == Z_STREAM_END)
919                 break;
920         } else {
921             goto fail;
922         }
923     }
924     png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
925
926     ret = s->bytestream - s->bytestream_start;
927  the_end:
928     av_free(crow_buf);
929     av_free(tmp_buf);
930     deflateEnd(&s->zstream);
931     return ret;
932  fail:
933     ret = -1;
934     goto the_end;
935 }
936 #endif
937
938 #ifdef CONFIG_PNG_DECODER
939 AVCodec png_decoder = {
940     "png",
941     CODEC_TYPE_VIDEO,
942     CODEC_ID_PNG,
943     sizeof(PNGContext),
944     common_init,
945     NULL,
946     NULL, //decode_end,
947     decode_frame,
948     0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
949     NULL
950 };
951 #endif
952
953 #ifdef CONFIG_PNG_ENCODER
954 AVCodec png_encoder = {
955     "png",
956     CODEC_TYPE_VIDEO,
957     CODEC_ID_PNG,
958     sizeof(PNGContext),
959     common_init,
960     encode_frame,
961     NULL, //encode_end,
962     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
963 };
964 #endif // CONFIG_PNG_ENCODER
965 #endif