annotate pngenc.c @ 12494:94eaea836bf4 libavcodec

Check avctx width/height more thoroughly (e.g. all values 0 except width would have been accepted before). Also do not fail if they are invalid but instead override them to 0. This allows decoding e.g. MPEG video when only the container values are corrupted. For encoding a value of 0,0 of course makes no sense, but was allowed through before and will be caught by an extra check in the encode function.
author reimar
date Wed, 15 Sep 2010 04:46:55 +0000
parents 8b28e74de2c0
children
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: 7235
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 */
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"
6759
6b7ba5b9af2e dsputil.h is not a system header, use "" when #including it.
diego
parents: 6722
diff changeset
23 #include "dsputil.h"
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
24 #include "png.h"
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
25
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
26 /* TODO:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
27 * - add 2, 4 and 16 bit depth support
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
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
30 #include <zlib.h>
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
31
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
32 //#define DEBUG
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
33
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
34 #define IOBUF_SIZE 4096
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
35
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
36 typedef struct PNGEncContext {
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
37 DSPContext dsp;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
38
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
39 uint8_t *bytestream;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
40 uint8_t *bytestream_start;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
41 uint8_t *bytestream_end;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
42 AVFrame picture;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
43
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
44 int filter_type;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
45
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
46 z_stream zstream;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
47 uint8_t buf[IOBUF_SIZE];
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
48 } PNGEncContext;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
49
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
50 static void png_get_interlaced_row(uint8_t *dst, int row_size,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
51 int bits_per_pixel, int pass,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
52 const uint8_t *src, int width)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
53 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
54 int x, mask, dst_x, j, b, bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
55 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
56 const uint8_t *s;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
57
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
58 mask = ff_png_pass_mask[pass];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
59 switch(bits_per_pixel) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
60 case 1:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
61 memset(dst, 0, row_size);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
62 dst_x = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
63 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
64 j = (x & 7);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
65 if ((mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
66 b = (src[x >> 3] >> (7 - j)) & 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
67 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
68 dst_x++;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
69 }
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 for(x = 0; x < width; x++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
77 j = x & 7;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
78 if ((mask << j) & 0x80) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
79 memcpy(d, s, bpp);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
80 d += bpp;
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 s += bpp;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
83 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
84 break;
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
87
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
88 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
89 {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
90 int i;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
91 for(i = 0; i < w; i++) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
92 int a, b, c, p, pa, pb, pc;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
93
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
94 a = src[i - bpp];
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
95 b = top[i];
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
96 c = top[i - bpp];
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
97
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
98 p = b - c;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
99 pc = a - c;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
100
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
101 pa = abs(p);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
102 pb = abs(pc);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
103 pc = abs(p + pc);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
104
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
105 if (pa <= pb && pa <= pc)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
106 p = a;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
107 else if (pb <= pc)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
108 p = b;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
109 else
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
110 p = c;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
111 dst[i] = src[i] - p;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
112 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
113 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
114
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
115 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
116 uint8_t *src, uint8_t *top, int size, int bpp)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
117 {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
118 int i;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
119
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
120 switch(filter_type) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
121 case PNG_FILTER_VALUE_NONE:
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
122 memcpy(dst, src, size);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
123 break;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
124 case PNG_FILTER_VALUE_SUB:
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
125 dsp->diff_bytes(dst, src, src-bpp, size);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
126 memcpy(dst, src, bpp);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
127 break;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
128 case PNG_FILTER_VALUE_UP:
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
129 dsp->diff_bytes(dst, src, top, size);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
130 break;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
131 case PNG_FILTER_VALUE_AVG:
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
132 for(i = 0; i < bpp; i++)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
133 dst[i] = src[i] - (top[i] >> 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
134 for(; i < size; i++)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
135 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
136 break;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
137 case PNG_FILTER_VALUE_PAETH:
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
138 for(i = 0; i < bpp; i++)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
139 dst[i] = src[i] - top[i];
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
140 sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
141 break;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
142 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
143 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
144
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
145 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
146 uint8_t *src, uint8_t *top, int size, int bpp)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
147 {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
148 int pred = s->filter_type;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
149 assert(bpp || !pred);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
150 if(!top && pred)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
151 pred = PNG_FILTER_VALUE_SUB;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
152 if(pred == PNG_FILTER_VALUE_MIXED) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
153 int i;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
154 int cost, bcost = INT_MAX;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
155 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
156 for(pred=0; pred<5; pred++) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
157 png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
158 buf1[0] = pred;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
159 cost = 0;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
160 for(i=0; i<=size; i++)
6404
a0a1db4738dd simplify
lorenm
parents: 6396
diff changeset
161 cost += abs((int8_t)buf1[i]);
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
162 if(cost < bcost) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
163 bcost = cost;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
164 FFSWAP(uint8_t*, buf1, buf2);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
165 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
166 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
167 return buf2;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
168 } else {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
169 png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
170 dst[0] = pred;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
171 return dst;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
172 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
173 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
174
4515
a2b14c6fccfd consistency renaming: rgba32 --> rgb32
diego
parents: 4494
diff changeset
175 static void convert_from_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
176 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
177 uint8_t *d;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
178 int j;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
179 unsigned int v;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
180
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
181 d = dst;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
182 for(j = 0; j < width; j++) {
3347
82277c821113 Add const to (mostly) char* and make some functions static, which aren't used
diego
parents: 3177
diff changeset
183 v = ((const uint32_t *)src)[j];
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
184 d[0] = v >> 16;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
185 d[1] = v >> 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
186 d[2] = v;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
187 d[3] = v >> 24;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
188 d += 4;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
189 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
190 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
191
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
192 static void png_write_chunk(uint8_t **f, uint32_t tag,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
193 const uint8_t *buf, int length)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
194 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
195 uint32_t crc;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
196 uint8_t tagbuf[4];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
197
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
198 bytestream_put_be32(f, length);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
199 crc = crc32(0, Z_NULL, 0);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5067
diff changeset
200 AV_WL32(tagbuf, tag);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
201 crc = crc32(crc, tagbuf, 4);
12129
8b28e74de2c0 Add av_ prefix to bswap macros
mru
parents: 11560
diff changeset
202 bytestream_put_be32(f, av_bswap32(tag));
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
203 if (length > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
204 crc = crc32(crc, buf, length);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
205 memcpy(*f, buf, length);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
206 *f += length;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
207 }
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
208 bytestream_put_be32(f, crc);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
209 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
210
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
211 /* XXX: do filtering */
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
212 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
213 {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
214 int ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
215
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
216 s->zstream.avail_in = size;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
217 s->zstream.next_in = (uint8_t *)data;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
218 while (s->zstream.avail_in > 0) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
219 ret = deflate(&s->zstream, Z_NO_FLUSH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
220 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
221 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
222 if (s->zstream.avail_out == 0) {
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
223 if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
224 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
225 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
226 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
227 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
228 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
229 return 0;
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
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
232 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
233 PNGEncContext *s = avctx->priv_data;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
234 AVFrame *pict = data;
7235
dd07ede98d7e Remove useless casts.
benoit
parents: 7040
diff changeset
235 AVFrame * const p= &s->picture;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
236 int bit_depth, color_type, y, len, row_size, ret, is_progressive;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
237 int bits_per_pixel, pass_row_size;
5804
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
238 int compression_level;
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
239 uint8_t *ptr, *top;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
240 uint8_t *crow_base = NULL, *crow_buf, *crow;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
241 uint8_t *progressive_buf = NULL;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
242 uint8_t *rgba_buf = NULL;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
243 uint8_t *top_buf = NULL;
2342
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 *p = *pict;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
246 p->pict_type= FF_I_TYPE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
247 p->key_frame= 1;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
248
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
249 s->bytestream_start=
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
250 s->bytestream= buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
251 s->bytestream_end= buf+buf_size;
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 is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
254 switch(avctx->pix_fmt) {
4494
ce643a22f049 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 4379
diff changeset
255 case PIX_FMT_RGB32:
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
256 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
257 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
258 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
259 case PIX_FMT_RGB24:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
260 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
261 color_type = PNG_COLOR_TYPE_RGB;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
262 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
263 case PIX_FMT_GRAY8:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
264 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
265 color_type = PNG_COLOR_TYPE_GRAY;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
266 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
267 case PIX_FMT_MONOBLACK:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
268 bit_depth = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
269 color_type = PNG_COLOR_TYPE_GRAY;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
270 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
271 case PIX_FMT_PAL8:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
272 bit_depth = 8;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
273 color_type = PNG_COLOR_TYPE_PALETTE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
274 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
275 default:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
276 return -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
277 }
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
278 bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
279 row_size = (avctx->width * bits_per_pixel + 7) >> 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
280
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
281 s->zstream.zalloc = ff_png_zalloc;
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
282 s->zstream.zfree = ff_png_zfree;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
283 s->zstream.opaque = NULL;
5804
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
284 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
285 Z_DEFAULT_COMPRESSION :
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
286 av_clip(avctx->compression_level, 0, 9);
af7e6436d8bd Allow to override zlib compression level in PNG encoder via avctx->compression_level
reimar
parents: 5339
diff changeset
287 ret = deflateInit2(&s->zstream, compression_level,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
288 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
289 if (ret != Z_OK)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
290 return -1;
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
291 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
292 if (!crow_base)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
293 goto fail;
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
294 crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
295 if (is_progressive) {
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
296 progressive_buf = av_malloc(row_size + 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
297 if (!progressive_buf)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
298 goto fail;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
299 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
300 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
301 rgba_buf = av_malloc(row_size + 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
302 if (!rgba_buf)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
303 goto fail;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
304 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
305 if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
306 top_buf = av_malloc(row_size + 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
307 if (!top_buf)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
308 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
309 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
310
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
311 /* write png header */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
312 memcpy(s->bytestream, ff_pngsig, 8);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
313 s->bytestream += 8;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
314
5067
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
315 AV_WB32(s->buf, avctx->width);
6166fbf375cc Remove duplicate bytestream functions
ramiro
parents: 4515
diff changeset
316 AV_WB32(s->buf + 4, avctx->height);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
317 s->buf[8] = bit_depth;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
318 s->buf[9] = color_type;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
319 s->buf[10] = 0; /* compression type */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
320 s->buf[11] = 0; /* filter type */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
321 s->buf[12] = is_progressive; /* interlace type */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
322
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
323 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
324
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
325 /* put the palette if needed */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
326 if (color_type == PNG_COLOR_TYPE_PALETTE) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
327 int has_alpha, alpha, i;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
328 unsigned int v;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
329 uint32_t *palette;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
330 uint8_t *alpha_ptr;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
331
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
332 palette = (uint32_t *)p->data[1];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
333 ptr = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
334 alpha_ptr = s->buf + 256 * 3;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
335 has_alpha = 0;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
336 for(i = 0; i < 256; i++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
337 v = palette[i];
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
338 alpha = v >> 24;
4018
9c8ab288ebf6 Make PNG produce correct 8-bit pictures
kostya
parents: 3947
diff changeset
339 if (alpha && alpha != 0xff)
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
340 has_alpha = 1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
341 *alpha_ptr++ = alpha;
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5067
diff changeset
342 bytestream_put_be24(&ptr, v);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
343 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
344 png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
345 if (has_alpha) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
346 png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
347 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
348 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
349
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
350 /* now put each row */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
351 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
352 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
353 if (is_progressive) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
354 int pass;
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 for(pass = 0; pass < NB_PASSES; pass++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
357 /* NOTE: a pass is completely omited if no pixels would be
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
358 output */
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
359 pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
360 if (pass_row_size > 0) {
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
361 top = NULL;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
362 for(y = 0; y < avctx->height; y++) {
5337
26f4095e35d2 separate en/decoder specific parts from png.c
mru
parents: 5097
diff changeset
363 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
364 ptr = p->data[0] + y * p->linesize[0];
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
365 FFSWAP(uint8_t*, progressive_buf, top_buf);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
366 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
367 convert_from_rgb32(rgba_buf, ptr, avctx->width);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
368 ptr = rgba_buf;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
369 }
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
370 png_get_interlaced_row(progressive_buf, pass_row_size,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2661
diff changeset
371 bits_per_pixel, pass,
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
372 ptr, avctx->width);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
373 crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
374 png_write_row(s, crow, pass_row_size + 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
375 top = progressive_buf;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
376 }
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 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
379 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
380 } else {
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
381 top = NULL;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
382 for(y = 0; y < avctx->height; y++) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
383 ptr = p->data[0] + y * p->linesize[0];
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
384 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
385 FFSWAP(uint8_t*, rgba_buf, top_buf);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
386 convert_from_rgb32(rgba_buf, ptr, avctx->width);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
387 ptr = rgba_buf;
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
388 }
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
389 crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
390 png_write_row(s, crow, row_size + 1);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
391 top = ptr;
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
392 }
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 /* compress last bytes */
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
395 for(;;) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
396 ret = deflate(&s->zstream, Z_FINISH);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
397 if (ret == Z_OK || ret == Z_STREAM_END) {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
398 len = IOBUF_SIZE - s->zstream.avail_out;
2422
18b8b2dcc037 various security fixes and precautionary checks
michael
parents: 2347
diff changeset
399 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
400 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
401 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
402 s->zstream.avail_out = IOBUF_SIZE;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
403 s->zstream.next_out = s->buf;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
404 if (ret == Z_STREAM_END)
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
405 break;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
406 } else {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
407 goto fail;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
408 }
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 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
411
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
412 ret = s->bytestream - s->bytestream_start;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
413 the_end:
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
414 av_free(crow_base);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
415 av_free(progressive_buf);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
416 av_free(rgba_buf);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
417 av_free(top_buf);
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
418 deflateEnd(&s->zstream);
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
419 return ret;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
420 fail:
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
421 ret = -1;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
422 goto the_end;
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
423 }
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
424
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6404
diff changeset
425 static av_cold int png_enc_init(AVCodecContext *avctx){
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
426 PNGEncContext *s = avctx->priv_data;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
427
7235
dd07ede98d7e Remove useless casts.
benoit
parents: 7040
diff changeset
428 avcodec_get_frame_defaults(&s->picture);
dd07ede98d7e Remove useless casts.
benoit
parents: 7040
diff changeset
429 avctx->coded_frame= &s->picture;
6396
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
430 dsputil_init(&s->dsp, avctx);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
431
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
432 s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
433 if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
2d7afa1bc573 png filters
lorenm
parents: 5804
diff changeset
434 s->filter_type = PNG_FILTER_VALUE_NONE;
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
435
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
436 return 0;
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
437 }
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
438
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
439 AVCodec png_encoder = {
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
440 "png",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10146
diff changeset
441 AVMEDIA_TYPE_VIDEO,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
442 CODEC_ID_PNG,
5339
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
443 sizeof(PNGEncContext),
a8c48a070cff hardly anything in PNGContext is shared; split it
mru
parents: 5337
diff changeset
444 png_enc_init,
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
445 encode_frame,
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
446 NULL, //encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 8629
diff changeset
447 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
448 .long_name= NULL_IF_CONFIG_SMALL("PNG image"),
2342
8b6668325ff8 porting png support from -f image to -f image2
michael
parents:
diff changeset
449 };