annotate pngdec.c @ 5337:26f4095e35d2 libavcodec

separate en/decoder specific parts from png.c
author mru
date Sun, 15 Jul 2007 18:24:26 +0000
parents png.c@e9a0c447dc73
children a8c48a070cff
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"
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
22 #include "bytestream.h"
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
23 #include "png.h"
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
24
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
25 /* TODO:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
26 * - add 2, 4 and 16 bit depth support
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
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
29 #include <zlib.h>
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
30
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
31 //#define DEBUG
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
32
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
33 /* 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
34 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
35 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
36 };
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
37
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
38 /* Mask to determine which pixels to overwrite while displaying */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
39 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
40 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
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
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
43 /* 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
44 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
45 this stage */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
46 static void png_put_interlaced_row(uint8_t *dst, int width,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
47 int bits_per_pixel, int pass,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
48 int color_type, const uint8_t *src)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
49 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
50 int x, mask, dsp_mask, j, src_x, b, bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
51 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
52 const uint8_t *s;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
53
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
54 mask = ff_png_pass_mask[pass];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
55 dsp_mask = png_pass_dsp_mask[pass];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
56 switch(bits_per_pixel) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
57 case 1:
5097
e9a0c447dc73 spelling
diego
parents: 5089
diff changeset
58 /* we must initialize the line to zero before writing to it */
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
59 if (pass == 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
60 memset(dst, 0, (width + 7) >> 3);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
61 src_x = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
62 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
63 j = (x & 7);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
64 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
65 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
66 dst[x >> 3] |= b << (7 - j);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
67 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
68 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
69 src_x++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
70 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
71 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
72 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
73 bpp = bits_per_pixel >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
74 d = dst;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
75 s = src;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
76 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
77 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
78 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
79 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
80 *(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
81 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
82 d += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
83 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
84 s += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
85 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
86 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
87 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
88 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
89 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
90 memcpy(d, s, bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
91 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
92 d += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
93 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
94 s += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
95 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
96 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
97 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
98 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
99 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
100
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
101 /* XXX: optimize */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
102 /* NOTE: 'dst' can be equal to 'last' */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
103 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
104 uint8_t *src, uint8_t *last, int size, int bpp)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
105 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
106 int i, p;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
107
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
108 switch(filter_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
109 case PNG_FILTER_VALUE_NONE:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
110 memcpy(dst, src, size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
111 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
112 case PNG_FILTER_VALUE_SUB:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
113 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
114 dst[i] = src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
115 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
116 for(i = bpp; i < size; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
117 p = dst[i - bpp];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
118 dst[i] = p + src[i];
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 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
121 case PNG_FILTER_VALUE_UP:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
122 for(i = 0; i < size; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
123 p = last[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
124 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
125 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
126 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
127 case PNG_FILTER_VALUE_AVG:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
128 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
129 p = (last[i] >> 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
130 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
131 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
132 for(i = bpp; i < size; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
133 p = ((dst[i - bpp] + last[i]) >> 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
134 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
135 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
136 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
137 case PNG_FILTER_VALUE_PAETH:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
138 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
139 p = last[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
140 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
141 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
142 for(i = bpp; i < size; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
143 int a, b, c, pa, pb, pc;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
144
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
145 a = dst[i - bpp];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
146 b = last[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
147 c = last[i - bpp];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
148
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
149 p = b - c;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
150 pc = a - c;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
151
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
152 pa = abs(p);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
153 pb = abs(pc);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
154 pc = abs(p + pc);
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 if (pa <= pb && pa <= pc)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
157 p = a;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
158 else if (pb <= pc)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
159 p = b;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
160 else
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
161 p = c;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
162 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
163 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
164 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
165 }
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
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
168 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
169 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
170 int j;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
171 unsigned int r, g, b, a;
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 for(j = 0;j < width; j++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
174 r = src[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
175 g = src[1];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
176 b = src[2];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
177 a = src[3];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
178 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
179 dst += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
180 src += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
181 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
182 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
183
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
184 /* process exactly one decompressed row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
185 static void png_handle_row(PNGContext *s)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
186 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
187 uint8_t *ptr, *last_row;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
188 int got_line;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
189
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
190 if (!s->interlace_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
191 ptr = s->image_buf + s->image_linesize * s->y;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
192 /* need to swap bytes correctly for RGB_ALPHA */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
193 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
194 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
195 s->last_row, s->row_size, s->bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
196 memcpy(s->last_row, s->tmp_row, s->row_size);
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
197 convert_to_rgb32(ptr, s->tmp_row, s->width);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
198 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
199 /* in normal case, we avoid one copy */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
200 if (s->y == 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
201 last_row = s->last_row;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
202 else
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
203 last_row = ptr - s->image_linesize;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
204
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
205 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
206 last_row, s->row_size, s->bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
207 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
208 s->y++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
209 if (s->y == s->height) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
210 s->state |= PNG_ALLIMAGE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
211 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
212 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
213 got_line = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
214 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
215 ptr = s->image_buf + s->image_linesize * s->y;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
216 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
217 /* 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
218 wait for the next one */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
219 if (got_line)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
220 break;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
221 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
222 s->last_row, s->pass_row_size, s->bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
223 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
224 got_line = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
225 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
226 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
227 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
228 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
229 s->color_type, s->last_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
230 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
231 s->y++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
232 if (s->y == s->height) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
233 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
234 if (s->pass == NB_PASSES - 1) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
235 s->state |= PNG_ALLIMAGE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
236 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
237 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
238 s->pass++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
239 s->y = 0;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
240 s->pass_row_size = ff_png_pass_row_size(s->pass,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
241 s->bits_per_pixel,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
242 s->width);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
243 s->crow_size = s->pass_row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
244 if (s->pass_row_size != 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
245 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
246 /* skip pass if empty row */
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
249 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
250 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
251 the_end: ;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
252 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
253 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
254
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
255 static int png_decode_idat(PNGContext *s, int length)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
256 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
257 int ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
258 s->zstream.avail_in = length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
259 s->zstream.next_in = s->bytestream;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
260 s->bytestream += length;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
261
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
262 if(s->bytestream > s->bytestream_end)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
263 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
264
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
265 /* decode one line if possible */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
266 while (s->zstream.avail_in > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
267 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
268 if (ret != Z_OK && ret != Z_STREAM_END) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
269 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
270 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
271 if (s->zstream.avail_out == 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
272 if (!(s->state & PNG_ALLIMAGE)) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
273 png_handle_row(s);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
274 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
275 s->zstream.avail_out = s->crow_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
276 s->zstream.next_out = s->crow_buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
277 }
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 return 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
280 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
281
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
282 static int decode_frame(AVCodecContext *avctx,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
283 void *data, int *data_size,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
284 uint8_t *buf, int buf_size)
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 PNGContext * const s = avctx->priv_data;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
287 AVFrame *picture = data;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
288 AVFrame * const p= (AVFrame*)&s->picture;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
289 uint32_t tag, length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
290 int ret, crc;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
291
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
292 s->bytestream_start=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
293 s->bytestream= buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
294 s->bytestream_end= buf + buf_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
295
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
296 /* check signature */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
297 if (memcmp(s->bytestream, ff_pngsig, 8) != 0)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
298 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
299 s->bytestream+= 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
300 s->y=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
301 s->state=0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
302 // memset(s, 0, sizeof(PNGContext));
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
303 /* init the zlib */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
304 s->zstream.zalloc = ff_png_zalloc;
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
305 s->zstream.zfree = ff_png_zfree;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
306 s->zstream.opaque = NULL;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
307 ret = inflateInit(&s->zstream);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
308 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
309 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
310 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
311 int tag32;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
312 if (s->bytestream >= s->bytestream_end)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
313 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
314 length = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
315 if (length > 0x7fffffff)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
316 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
317 tag32 = bytestream_get_be32(&s->bytestream);
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
318 tag = bswap_32(tag32);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
319 #ifdef DEBUG
3177
8d1b2cc2a75b (f)printf --> av_log conversion
diego
parents: 3036
diff changeset
320 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
321 (tag & 0xff),
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
322 ((tag >> 8) & 0xff),
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
323 ((tag >> 16) & 0xff),
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
324 ((tag >> 24) & 0xff), length);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
325 #endif
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
326 switch(tag) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
327 case MKTAG('I', 'H', 'D', 'R'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
328 if (length != 13)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
329 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
330 s->width = bytestream_get_be32(&s->bytestream);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
331 s->height = bytestream_get_be32(&s->bytestream);
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
332 if(avcodec_check_dimensions(avctx, s->width, s->height)){
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
333 s->width= s->height= 0;
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
334 goto fail;
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
335 }
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
336 s->bit_depth = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
337 s->color_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
338 s->compression_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
339 s->filter_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
340 s->interlace_type = *s->bytestream++;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
341 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
342 s->state |= PNG_IHDR;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
343 #ifdef DEBUG
3177
8d1b2cc2a75b (f)printf --> av_log conversion
diego
parents: 3036
diff changeset
344 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
345 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
346 s->compression_type, s->filter_type, s->interlace_type);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
347 #endif
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
348 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
349 case MKTAG('I', 'D', 'A', 'T'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
350 if (!(s->state & PNG_IHDR))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
351 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
352 if (!(s->state & PNG_IDAT)) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
353 /* init image info */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
354 avctx->width = s->width;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
355 avctx->height = s->height;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
356
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
357 s->channels = ff_png_get_nb_channels(s->color_type);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
358 s->bits_per_pixel = s->bit_depth * s->channels;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
359 s->bpp = (s->bits_per_pixel + 7) >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
360 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
361
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
362 if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
363 s->color_type == PNG_COLOR_TYPE_RGB) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
364 avctx->pix_fmt = PIX_FMT_RGB24;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
365 } else if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
366 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
4494
ce643a22f049 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 4379
diff changeset
367 avctx->pix_fmt = PIX_FMT_RGB32;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
368 } else if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
369 s->color_type == PNG_COLOR_TYPE_GRAY) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
370 avctx->pix_fmt = PIX_FMT_GRAY8;
4067
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
371 } else if (s->bit_depth == 16 &&
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
372 s->color_type == PNG_COLOR_TYPE_GRAY) {
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
373 avctx->pix_fmt = PIX_FMT_GRAY16BE;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
374 } else if (s->bit_depth == 1 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
375 s->color_type == PNG_COLOR_TYPE_GRAY) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
376 avctx->pix_fmt = PIX_FMT_MONOBLACK;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
377 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
378 avctx->pix_fmt = PIX_FMT_PAL8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
379 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
380 goto fail;
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 if(p->data[0])
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
383 avctx->release_buffer(avctx, p);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
384
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
385 p->reference= 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
386 if(avctx->get_buffer(avctx, p) < 0){
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
387 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
388 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
389 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
390 p->pict_type= FF_I_TYPE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
391 p->key_frame= 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
392 p->interlaced_frame = !!s->interlace_type;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
393
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
394 /* compute the compressed row size */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
395 if (!s->interlace_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
396 s->crow_size = s->row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
397 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
398 s->pass = 0;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
399 s->pass_row_size = ff_png_pass_row_size(s->pass,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
400 s->bits_per_pixel,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
401 s->width);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
402 s->crow_size = s->pass_row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
403 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
404 #ifdef DEBUG
3177
8d1b2cc2a75b (f)printf --> av_log conversion
diego
parents: 3036
diff changeset
405 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
406 s->row_size, s->crow_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
407 #endif
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
408 s->image_buf = p->data[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
409 s->image_linesize = p->linesize[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
410 /* copy the palette if needed */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
411 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
412 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
413 /* empty row is used if differencing to the first row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
414 s->last_row = av_mallocz(s->row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
415 if (!s->last_row)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
416 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
417 if (s->interlace_type ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
418 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
419 s->tmp_row = av_malloc(s->row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
420 if (!s->tmp_row)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
421 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
422 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
423 /* compressed row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
424 s->crow_buf = av_malloc(s->row_size + 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
425 if (!s->crow_buf)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
426 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
427 s->zstream.avail_out = s->crow_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
428 s->zstream.next_out = s->crow_buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
429 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
430 s->state |= PNG_IDAT;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
431 if (png_decode_idat(s, length) < 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
432 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
433 /* skip crc */
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
434 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
435 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
436 case MKTAG('P', 'L', 'T', 'E'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
437 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
438 int n, i, r, g, b;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
439
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
440 if ((length % 3) != 0 || length > 256 * 3)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
441 goto skip_tag;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
442 /* read the palette */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
443 n = length / 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
444 for(i=0;i<n;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
445 r = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
446 g = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
447 b = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
448 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
449 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
450 for(;i<256;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
451 s->palette[i] = (0xff << 24);
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 s->state |= PNG_PLTE;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
454 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
455 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
456 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
457 case MKTAG('t', 'R', 'N', 'S'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
458 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
459 int v, i;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
460
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
461 /* read the transparency. XXX: Only palette mode supported */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
462 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
463 length > 256 ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
464 !(s->state & PNG_PLTE))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
465 goto skip_tag;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
466 for(i=0;i<length;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
467 v = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
468 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
469 }
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
470 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
471 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
472 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
473 case MKTAG('I', 'E', 'N', 'D'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
474 if (!(s->state & PNG_ALLIMAGE))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
475 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
476 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
477 goto exit_loop;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
478 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
479 /* skip tag */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
480 skip_tag:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
481 s->bytestream += length + 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
482 break;
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
485 exit_loop:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
486 *picture= *(AVFrame*)&s->picture;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
487 *data_size = sizeof(AVPicture);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
488
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
489 ret = s->bytestream - s->bytestream_start;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
490 the_end:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
491 inflateEnd(&s->zstream);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
492 av_freep(&s->crow_buf);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
493 av_freep(&s->last_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
494 av_freep(&s->tmp_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
495 return ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
496 fail:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
497 ret = -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
498 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
499 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
500
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
501 AVCodec png_decoder = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
502 "png",
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
503 CODEC_TYPE_VIDEO,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
504 CODEC_ID_PNG,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
505 sizeof(PNGContext),
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
506 ff_png_common_init,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
507 NULL,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
508 NULL, //decode_end,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
509 decode_frame,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
510 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
511 NULL
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
512 };