annotate pngdec.c @ 12380:e402b74c4b62 libavcodec

Fix handling of truncated files. Should fix random FATE breakages.
author vitor
date Mon, 09 Aug 2010 07:17:01 +0000
parents 914f484bb476
children ffb3668ff7af
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
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 7236
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard
2342
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 */
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 12129
diff changeset
21 #include "libavcore/imgutils.h"
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
22 #include "avcodec.h"
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
23 #include "bytestream.h"
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
24 #include "png.h"
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
25 #include "dsputil.h"
2342
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 /* TODO:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
28 * - add 2, 4 and 16 bit depth support
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
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
31 #include <zlib.h>
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 //#define DEBUG
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
34
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
35 typedef struct PNGDecContext {
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
36 DSPContext dsp;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
37
6245
michael
parents: 5355
diff changeset
38 const uint8_t *bytestream;
michael
parents: 5355
diff changeset
39 const uint8_t *bytestream_start;
michael
parents: 5355
diff changeset
40 const uint8_t *bytestream_end;
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
41 AVFrame picture1, picture2;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
42 AVFrame *current_picture, *last_picture;
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
43
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
44 int state;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
45 int width, height;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
46 int bit_depth;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
47 int color_type;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
48 int compression_type;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
49 int interlace_type;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
50 int filter_type;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
51 int channels;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
52 int bits_per_pixel;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
53 int bpp;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
54
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
55 uint8_t *image_buf;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
56 int image_linesize;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
57 uint32_t palette[256];
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
58 uint8_t *crow_buf;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
59 uint8_t *last_row;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
60 uint8_t *tmp_row;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
61 int pass;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
62 int crow_size; /* compressed row size (include filter type) */
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
63 int row_size; /* decompressed row size */
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
64 int pass_row_size; /* decompress row size of the current pass */
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
65 int y;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
66 z_stream zstream;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
67 } PNGDecContext;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
68
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
69 /* 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
70 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
71 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
72 };
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
73
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
74 /* Mask to determine which pixels to overwrite while displaying */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
75 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
76 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
77 };
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
78
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
79 /* NOTE: we try to construct a good looking image at each pass. width
5355
45d083bbbbe7 typo fixes
diego
parents: 5339
diff changeset
80 is the original image width. We also do pixel format conversion at
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
81 this stage */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
82 static void png_put_interlaced_row(uint8_t *dst, int width,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
83 int bits_per_pixel, int pass,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
84 int color_type, const uint8_t *src)
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 int x, mask, dsp_mask, j, src_x, b, bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
87 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
88 const uint8_t *s;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
89
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
90 mask = ff_png_pass_mask[pass];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
91 dsp_mask = png_pass_dsp_mask[pass];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
92 switch(bits_per_pixel) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
93 case 1:
5097
e9a0c447dc73 spelling
diego
parents: 5089
diff changeset
94 /* 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
95 if (pass == 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
96 memset(dst, 0, (width + 7) >> 3);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
97 src_x = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
98 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
99 j = (x & 7);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
100 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
101 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
102 dst[x >> 3] |= b << (7 - j);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
103 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
104 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
105 src_x++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
106 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
107 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
108 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
109 bpp = bits_per_pixel >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
110 d = dst;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
111 s = src;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
112 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
113 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
114 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
115 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
116 *(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
117 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
118 d += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
119 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
120 s += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
121 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
122 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
123 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
124 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
125 if ((dsp_mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
126 memcpy(d, s, bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
127 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
128 d += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
129 if ((mask << j) & 0x80)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
130 s += bpp;
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
133 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
134 }
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
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
137 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
138 {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
139 int i;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
140 for(i = 0; i < w; i++) {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
141 int a, b, c, p, pa, pb, pc;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
142
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
143 a = dst[i - bpp];
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
144 b = top[i];
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
145 c = top[i - bpp];
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
146
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
147 p = b - c;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
148 pc = a - c;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
149
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
150 pa = abs(p);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
151 pb = abs(pc);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
152 pc = abs(p + pc);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
153
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
154 if (pa <= pb && pa <= pc)
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
155 p = a;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
156 else if (pb <= pc)
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
157 p = b;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
158 else
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
159 p = c;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
160 dst[i] = p + src[i];
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
161 }
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
162 }
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
163
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
164 #define UNROLL1(bpp, op) {\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
165 r = dst[0];\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
166 if(bpp >= 2) g = dst[1];\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
167 if(bpp >= 3) b = dst[2];\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
168 if(bpp >= 4) a = dst[3];\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
169 for(; i < size; i+=bpp) {\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
170 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
171 if(bpp == 1) continue;\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
172 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
173 if(bpp == 2) continue;\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
174 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
175 if(bpp == 3) continue;\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
176 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
177 }\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
178 }
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
179
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
180 #define UNROLL_FILTER(op)\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
181 if(bpp == 1) UNROLL1(1, op)\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
182 else if(bpp == 2) UNROLL1(2, op)\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
183 else if(bpp == 3) UNROLL1(3, op)\
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
184 else if(bpp == 4) UNROLL1(4, op)\
9895
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
185 else {\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
186 for (; i < size; i += bpp) {\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
187 int j;\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
188 for (j = 0; j < bpp; j++)\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
189 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
190 }\
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
191 }
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
192
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
193 /* NOTE: 'dst' can be equal to 'last' */
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
194 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
195 uint8_t *src, uint8_t *last, int size, int bpp)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
196 {
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
197 int i, p, r, g, b, a;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
198
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
199 switch(filter_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
200 case PNG_FILTER_VALUE_NONE:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
201 memcpy(dst, src, size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
202 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
203 case PNG_FILTER_VALUE_SUB:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
204 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
205 dst[i] = src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
206 }
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
207 if(bpp == 4) {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
208 p = *(int*)dst;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
209 for(; i < size; i+=bpp) {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
210 int s = *(int*)(src+i);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
211 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
212 *(int*)(dst+i) = p;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
213 }
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
214 } else {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
215 #define OP_SUB(x,s,l) x+s
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
216 UNROLL_FILTER(OP_SUB);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
217 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
218 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
219 case PNG_FILTER_VALUE_UP:
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
220 dsp->add_bytes_l2(dst, src, last, size);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
221 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
222 case PNG_FILTER_VALUE_AVG:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
223 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
224 p = (last[i] >> 1);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
225 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
226 }
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
227 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
228 UNROLL_FILTER(OP_AVG);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
229 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
230 case PNG_FILTER_VALUE_PAETH:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
231 for(i = 0; i < bpp; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
232 p = last[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
233 dst[i] = p + src[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
234 }
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
235 if(bpp > 1 && size > 4) {
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
236 // would write off the end of the array if we let it process the last pixel with bpp=3
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
237 int w = bpp==4 ? size : size-3;
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
238 dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
239 i = w;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
240 }
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
241 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
242 break;
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
245
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
246 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
2342
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 int j;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
249 unsigned int r, g, b, a;
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 for(j = 0;j < width; j++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
252 r = src[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
253 g = src[1];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
254 b = src[2];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
255 a = src[3];
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
256 if(loco) {
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
257 r = (r+g)&0xff;
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
258 b = (b+g)&0xff;
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
259 }
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
260 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
261 dst += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
262 src += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
263 }
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
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
266 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
267 {
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
268 if(loco)
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
269 convert_to_rgb32_loco(dst, src, width, 1);
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
270 else
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
271 convert_to_rgb32_loco(dst, src, width, 0);
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
272 }
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
273
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
274 static void deloco_rgb24(uint8_t *dst, int size)
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
275 {
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
276 int i;
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
277 for(i=0; i<size; i+=3) {
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
278 int g = dst[i+1];
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
279 dst[i+0] += g;
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
280 dst[i+2] += g;
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
281 }
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
282 }
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
283
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
284 /* process exactly one decompressed row */
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
285 static void png_handle_row(PNGDecContext *s)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
286 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
287 uint8_t *ptr, *last_row;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
288 int got_line;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
289
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
290 if (!s->interlace_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
291 ptr = s->image_buf + s->image_linesize * s->y;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
292 /* need to swap bytes correctly for RGB_ALPHA */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
293 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
294 png_filter_row(&s->dsp, 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
295 s->last_row, s->row_size, s->bpp);
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
296 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
6394
1a9af4a496f2 remove a memcpy
lorenm
parents: 6384
diff changeset
297 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
298 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
299 /* in normal case, we avoid one copy */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
300 if (s->y == 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
301 last_row = s->last_row;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
302 else
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
303 last_row = ptr - s->image_linesize;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
304
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
305 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
306 last_row, s->row_size, s->bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
307 }
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
308 /* loco lags by 1 row so that it doesn't interfere with top prediction */
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
309 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
310 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
311 deloco_rgb24(ptr - s->image_linesize, s->row_size);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
312 s->y++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
313 if (s->y == s->height) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
314 s->state |= PNG_ALLIMAGE;
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
315 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
316 s->color_type == PNG_COLOR_TYPE_RGB)
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
317 deloco_rgb24(ptr, s->row_size);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
318 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
319 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
320 got_line = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
321 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
322 ptr = s->image_buf + s->image_linesize * s->y;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
323 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
324 /* 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
325 wait for the next one */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
326 if (got_line)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
327 break;
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
328 png_filter_row(&s->dsp, 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
329 s->last_row, s->pass_row_size, s->bpp);
6394
1a9af4a496f2 remove a memcpy
lorenm
parents: 6384
diff changeset
330 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
331 got_line = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
332 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
333 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
334 /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
335 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
336 s->color_type, s->last_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
337 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
338 s->y++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
339 if (s->y == s->height) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
340 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
341 if (s->pass == NB_PASSES - 1) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
342 s->state |= PNG_ALLIMAGE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
343 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
344 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
345 s->pass++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
346 s->y = 0;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
347 s->pass_row_size = ff_png_pass_row_size(s->pass,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
348 s->bits_per_pixel,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
349 s->width);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
350 s->crow_size = s->pass_row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
351 if (s->pass_row_size != 0)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
352 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
353 /* skip pass if empty row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
354 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
355 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
356 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
357 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
358 the_end: ;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
359 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
360 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
361
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
362 static int png_decode_idat(PNGDecContext *s, int length)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
363 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
364 int ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
365 s->zstream.avail_in = length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
366 s->zstream.next_in = s->bytestream;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
367 s->bytestream += length;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
368
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
369 if(s->bytestream > s->bytestream_end)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
370 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
371
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
372 /* decode one line if possible */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
373 while (s->zstream.avail_in > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
374 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
375 if (ret != Z_OK && ret != Z_STREAM_END) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
376 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
377 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
378 if (s->zstream.avail_out == 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
379 if (!(s->state & PNG_ALLIMAGE)) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
380 png_handle_row(s);
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 s->zstream.avail_out = s->crow_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
383 s->zstream.next_out = s->crow_buf;
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
386 return 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
387 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
388
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
389 static int decode_frame(AVCodecContext *avctx,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
390 void *data, int *data_size,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
391 AVPacket *avpkt)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
392 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
393 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
394 int buf_size = avpkt->size;
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
395 PNGDecContext * const s = avctx->priv_data;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
396 AVFrame *picture = data;
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
397 AVFrame *p;
9703
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
398 uint8_t *crow_buf_base = NULL;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
399 uint32_t tag, length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
400 int ret, crc;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
401
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
402 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
403 avctx->coded_frame= s->current_picture;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
404 p = s->current_picture;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
405
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
406 s->bytestream_start=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
407 s->bytestream= buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
408 s->bytestream_end= buf + buf_size;
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 /* check signature */
6406
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
411 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
49dcfab1bda3 decode mng color decorrelation
lorenm
parents: 6394
diff changeset
412 memcmp(s->bytestream, ff_mngsig, 8) != 0)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
413 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
414 s->bytestream+= 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
415 s->y=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
416 s->state=0;
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
417 // memset(s, 0, sizeof(PNGDecContext));
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
418 /* init the zlib */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
419 s->zstream.zalloc = ff_png_zalloc;
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
420 s->zstream.zfree = ff_png_zfree;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
421 s->zstream.opaque = NULL;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
422 ret = inflateInit(&s->zstream);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
423 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
424 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
425 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
426 int tag32;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
427 if (s->bytestream >= s->bytestream_end)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
428 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
429 length = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
430 if (length > 0x7fffffff)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
431 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
432 tag32 = bytestream_get_be32(&s->bytestream);
12129
8b28e74de2c0 Add av_ prefix to bswap macros
mru
parents: 12108
diff changeset
433 tag = av_bswap32(tag32);
9999
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
434 dprintf(avctx, "png: tag=%c%c%c%c length=%u\n",
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
435 (tag & 0xff),
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
436 ((tag >> 8) & 0xff),
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
437 ((tag >> 16) & 0xff),
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
438 ((tag >> 24) & 0xff), length);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
439 switch(tag) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
440 case MKTAG('I', 'H', 'D', 'R'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
441 if (length != 13)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
442 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
443 s->width = bytestream_get_be32(&s->bytestream);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
444 s->height = bytestream_get_be32(&s->bytestream);
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 12129
diff changeset
445 if(av_check_image_size(s->width, s->height, 0, avctx)){
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
446 s->width= s->height= 0;
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
447 goto fail;
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
448 }
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
449 s->bit_depth = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
450 s->color_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
451 s->compression_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
452 s->filter_type = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
453 s->interlace_type = *s->bytestream++;
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 s->state |= PNG_IHDR;
9999
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
456 dprintf(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
457 s->width, s->height, s->bit_depth, s->color_type,
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
458 s->compression_type, s->filter_type, s->interlace_type);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
459 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
460 case MKTAG('I', 'D', 'A', 'T'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
461 if (!(s->state & PNG_IHDR))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
462 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
463 if (!(s->state & PNG_IDAT)) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
464 /* init image info */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
465 avctx->width = s->width;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
466 avctx->height = s->height;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
467
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
468 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
469 s->bits_per_pixel = s->bit_depth * s->channels;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
470 s->bpp = (s->bits_per_pixel + 7) >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
471 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
472
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
473 if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
474 s->color_type == PNG_COLOR_TYPE_RGB) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
475 avctx->pix_fmt = PIX_FMT_RGB24;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
476 } else if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
477 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
4494
ce643a22f049 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 4379
diff changeset
478 avctx->pix_fmt = PIX_FMT_RGB32;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
479 } else if (s->bit_depth == 8 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
480 s->color_type == PNG_COLOR_TYPE_GRAY) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
481 avctx->pix_fmt = PIX_FMT_GRAY8;
4067
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
482 } else if (s->bit_depth == 16 &&
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
483 s->color_type == PNG_COLOR_TYPE_GRAY) {
8ab58b7bc06b PNG 16-bit gray decoding support
kostya
parents: 4018
diff changeset
484 avctx->pix_fmt = PIX_FMT_GRAY16BE;
9895
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
485 } else if (s->bit_depth == 16 &&
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
486 s->color_type == PNG_COLOR_TYPE_RGB) {
06e12f9b93d7 Support RGB48 PNG decoding
kostya
parents: 9803
diff changeset
487 avctx->pix_fmt = PIX_FMT_RGB48BE;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
488 } else if (s->bit_depth == 1 &&
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
489 s->color_type == PNG_COLOR_TYPE_GRAY) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
490 avctx->pix_fmt = PIX_FMT_MONOBLACK;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
491 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
492 avctx->pix_fmt = PIX_FMT_PAL8;
11631
aef934aa4787 pngdec: Add support for PIX_FMT_Y400A
andoma
parents: 11563
diff changeset
493 } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
aef934aa4787 pngdec: Add support for PIX_FMT_Y400A
andoma
parents: 11563
diff changeset
494 avctx->pix_fmt = PIX_FMT_Y400A;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
495 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
496 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
497 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
498 if(p->data[0])
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
499 avctx->release_buffer(avctx, p);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
500
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
501 p->reference= 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
502 if(avctx->get_buffer(avctx, p) < 0){
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
503 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
504 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
505 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
506 p->pict_type= FF_I_TYPE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
507 p->key_frame= 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
508 p->interlaced_frame = !!s->interlace_type;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
509
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
510 /* compute the compressed row size */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
511 if (!s->interlace_type) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
512 s->crow_size = s->row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
513 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
514 s->pass = 0;
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
515 s->pass_row_size = ff_png_pass_row_size(s->pass,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
516 s->bits_per_pixel,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
517 s->width);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
518 s->crow_size = s->pass_row_size + 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
519 }
9999
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
520 dprintf(avctx, "row_size=%d crow_size =%d\n",
c78fd9154378 Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents: 9895
diff changeset
521 s->row_size, s->crow_size);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
522 s->image_buf = p->data[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
523 s->image_linesize = p->linesize[0];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
524 /* copy the palette if needed */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
525 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
526 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
527 /* empty row is used if differencing to the first row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
528 s->last_row = av_mallocz(s->row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
529 if (!s->last_row)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
530 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
531 if (s->interlace_type ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
532 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
533 s->tmp_row = av_malloc(s->row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
534 if (!s->tmp_row)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
535 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
536 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
537 /* compressed row */
9703
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
538 crow_buf_base = av_malloc(s->row_size + 16);
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
539 if (!crow_buf_base)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
540 goto fail;
9703
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
541
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
542 /* we want crow_buf+1 to be 16-byte aligned */
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
543 s->crow_buf = crow_buf_base + 15;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
544 s->zstream.avail_out = s->crow_size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
545 s->zstream.next_out = s->crow_buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
546 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
547 s->state |= PNG_IDAT;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
548 if (png_decode_idat(s, length) < 0)
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 /* skip crc */
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
551 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
552 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
553 case MKTAG('P', 'L', 'T', 'E'):
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 int n, i, r, g, b;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
556
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
557 if ((length % 3) != 0 || length > 256 * 3)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
558 goto skip_tag;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
559 /* read the palette */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
560 n = length / 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
561 for(i=0;i<n;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
562 r = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
563 g = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
564 b = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
565 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
566 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
567 for(;i<256;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
568 s->palette[i] = (0xff << 24);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
569 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
570 s->state |= PNG_PLTE;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
571 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
572 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
573 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
574 case MKTAG('t', 'R', 'N', 'S'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
575 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
576 int v, i;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
577
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
578 /* read the transparency. XXX: Only palette mode supported */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
579 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
580 length > 256 ||
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
581 !(s->state & PNG_PLTE))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
582 goto skip_tag;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
583 for(i=0;i<length;i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
584 v = *s->bytestream++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
585 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
586 }
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
587 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
588 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
589 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
590 case MKTAG('I', 'E', 'N', 'D'):
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
591 if (!(s->state & PNG_ALLIMAGE))
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
592 goto fail;
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
593 crc = bytestream_get_be32(&s->bytestream);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
594 goto exit_loop;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
595 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
596 /* skip tag */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
597 skip_tag:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
598 s->bytestream += length + 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
599 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
600 }
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 exit_loop:
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
603 /* handle p-frames only if a predecessor frame is available */
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
604 if(s->last_picture->data[0] != NULL) {
11563
5111783be6ad Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents: 11560
diff changeset
605 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
606 int i, j;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
607 uint8_t *pd = s->current_picture->data[0];
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
608 uint8_t *pd_last = s->last_picture->data[0];
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
609
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
610 for(j=0; j < s->height; j++) {
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
611 for(i=0; i < s->width * s->bpp; i++) {
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
612 pd[i] += pd_last[i];
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
613 }
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
614 pd += s->image_linesize;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
615 pd_last += s->image_linesize;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
616 }
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
617 }
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
618 }
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
619
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
620 *picture= *s->current_picture;
7236
6c1244ad5620 Remove useless casts.
benoit
parents: 7040
diff changeset
621 *data_size = sizeof(AVFrame);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
622
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
623 ret = s->bytestream - s->bytestream_start;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
624 the_end:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
625 inflateEnd(&s->zstream);
9703
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
626 av_free(crow_buf_base);
05cf2547e0f0 Fix unaligned dsputil call.
vitor
parents: 9387
diff changeset
627 s->crow_buf = NULL;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
628 av_freep(&s->last_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
629 av_freep(&s->tmp_row);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
630 return ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
631 fail:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
632 ret = -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
633 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
634 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
635
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6406
diff changeset
636 static av_cold int png_dec_init(AVCodecContext *avctx){
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
637 PNGDecContext *s = avctx->priv_data;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
638
9387
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
639 s->current_picture = &s->picture1;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
640 s->last_picture = &s->picture2;
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
641 avcodec_get_frame_defaults(&s->picture1);
cd207441ca56 Add support to CorePNG P-frames.
stefano
parents: 9355
diff changeset
642 avcodec_get_frame_defaults(&s->picture2);
6384
0a403ade8c81 simd and unroll png_filter_row
lorenm
parents: 6245
diff changeset
643 dsputil_init(&s->dsp, avctx);
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
644
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
645 return 0;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
646 }
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
647
10611
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
648 static av_cold int png_dec_end(AVCodecContext *avctx)
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
649 {
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
650 PNGDecContext *s = avctx->priv_data;
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
651
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
652 if (s->picture1.data[0])
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
653 avctx->release_buffer(avctx, &s->picture1);
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
654 if (s->picture2.data[0])
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
655 avctx->release_buffer(avctx, &s->picture2);
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
656
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
657 return 0;
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
658 }
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
659
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
660 AVCodec png_decoder = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
661 "png",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10611
diff changeset
662 AVMEDIA_TYPE_VIDEO,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
663 CODEC_ID_PNG,
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
664 sizeof(PNGDecContext),
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
665 png_dec_init,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
666 NULL,
10611
eb37707704e4 pngdec.c : release allocated buffers.
jai_menon
parents: 9999
diff changeset
667 png_dec_end,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
668 decode_frame,
9803
3768b0da80b8 png decoder use get_buffer, set CODEC_CAP_DR1
bcoudurier
parents: 9703
diff changeset
669 CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
6722
6eeb19edcee3 Add long names to some AVCodec declarations.
diego
parents: 6517
diff changeset
670 NULL,
12108
c35d7bc64882 Add new decoder property max_lowres and do not init decoder if requested value is higher.
cehoyos
parents: 11631
diff changeset
671 .max_lowres = 5,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6722
diff changeset
672 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
673 };