annotate png.c @ 4494:ce643a22f049 libavcodec

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