/src/libwebp/src/enc/picture_enc.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // WebPPicture class basis |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | #include <limits.h> |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include "src/enc/vp8i_enc.h" |
20 | | #include "src/utils/utils.h" |
21 | | #include "src/webp/encode.h" |
22 | | #include "src/webp/types.h" |
23 | | |
24 | | //------------------------------------------------------------------------------ |
25 | | // WebPPicture |
26 | | //------------------------------------------------------------------------------ |
27 | | |
28 | | static int DummyWriter(const uint8_t* data, size_t data_size, |
29 | 0 | const WebPPicture* const picture) { |
30 | | // The following are to prevent 'unused variable' error message. |
31 | 0 | (void)data; |
32 | 0 | (void)data_size; |
33 | 0 | (void)picture; |
34 | 0 | return 1; |
35 | 0 | } |
36 | | |
37 | 0 | int WebPPictureInitInternal(WebPPicture* picture, int version) { |
38 | 0 | if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) { |
39 | 0 | return 0; // caller/system version mismatch! |
40 | 0 | } |
41 | 0 | if (picture != NULL) { |
42 | 0 | memset(picture, 0, sizeof(*picture)); |
43 | 0 | picture->writer = DummyWriter; |
44 | 0 | WebPEncodingSetError(picture, VP8_ENC_OK); |
45 | 0 | } |
46 | 0 | return 1; |
47 | 0 | } |
48 | | |
49 | | //------------------------------------------------------------------------------ |
50 | | |
51 | 0 | int WebPValidatePicture(const WebPPicture* const picture) { |
52 | 0 | if (picture == NULL) return 0; |
53 | 0 | if (picture->width <= 0 || picture->height <= 0) { |
54 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION); |
55 | 0 | } |
56 | 0 | if (picture->width <= 0 || picture->width / 4 > INT_MAX / 4 || |
57 | 0 | picture->height <= 0 || picture->height / 4 > INT_MAX / 4) { |
58 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION); |
59 | 0 | } |
60 | 0 | if (picture->colorspace != WEBP_YUV420 && |
61 | 0 | picture->colorspace != WEBP_YUV420A) { |
62 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
63 | 0 | } |
64 | 0 | return 1; |
65 | 0 | } |
66 | | |
67 | 0 | static void WebPPictureResetBufferARGB(WebPPicture* const picture) { |
68 | 0 | picture->memory_argb_ = NULL; |
69 | 0 | picture->argb = NULL; |
70 | 0 | picture->argb_stride = 0; |
71 | 0 | } |
72 | | |
73 | 0 | static void WebPPictureResetBufferYUVA(WebPPicture* const picture) { |
74 | 0 | picture->memory_ = NULL; |
75 | 0 | picture->y = picture->u = picture->v = picture->a = NULL; |
76 | 0 | picture->y_stride = picture->uv_stride = 0; |
77 | 0 | picture->a_stride = 0; |
78 | 0 | } |
79 | | |
80 | 0 | void WebPPictureResetBuffers(WebPPicture* const picture) { |
81 | 0 | WebPPictureResetBufferARGB(picture); |
82 | 0 | WebPPictureResetBufferYUVA(picture); |
83 | 0 | } |
84 | | |
85 | 0 | int WebPPictureAllocARGB(WebPPicture* const picture) { |
86 | 0 | void* memory; |
87 | 0 | const int width = picture->width; |
88 | 0 | const int height = picture->height; |
89 | 0 | const uint64_t argb_size = (uint64_t)width * height; |
90 | |
|
91 | 0 | if (!WebPValidatePicture(picture)) return 0; |
92 | | |
93 | 0 | WebPSafeFree(picture->memory_argb_); |
94 | 0 | WebPPictureResetBufferARGB(picture); |
95 | | |
96 | | // allocate a new buffer. |
97 | 0 | memory = WebPSafeMalloc(argb_size + WEBP_ALIGN_CST, sizeof(*picture->argb)); |
98 | 0 | if (memory == NULL) { |
99 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
100 | 0 | } |
101 | 0 | picture->memory_argb_ = memory; |
102 | 0 | picture->argb = (uint32_t*)WEBP_ALIGN(memory); |
103 | 0 | picture->argb_stride = width; |
104 | 0 | return 1; |
105 | 0 | } |
106 | | |
107 | 0 | int WebPPictureAllocYUVA(WebPPicture* const picture) { |
108 | 0 | const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT; |
109 | 0 | const int width = picture->width; |
110 | 0 | const int height = picture->height; |
111 | 0 | const int y_stride = width; |
112 | 0 | const int uv_width = (int)(((int64_t)width + 1) >> 1); |
113 | 0 | const int uv_height = (int)(((int64_t)height + 1) >> 1); |
114 | 0 | const int uv_stride = uv_width; |
115 | 0 | int a_width, a_stride; |
116 | 0 | uint64_t y_size, uv_size, a_size, total_size; |
117 | 0 | uint8_t* mem; |
118 | |
|
119 | 0 | if (!WebPValidatePicture(picture)) return 0; |
120 | | |
121 | 0 | WebPSafeFree(picture->memory_); |
122 | 0 | WebPPictureResetBufferYUVA(picture); |
123 | | |
124 | | // alpha |
125 | 0 | a_width = has_alpha ? width : 0; |
126 | 0 | a_stride = a_width; |
127 | 0 | y_size = (uint64_t)y_stride * height; |
128 | 0 | uv_size = (uint64_t)uv_stride * uv_height; |
129 | 0 | a_size = (uint64_t)a_stride * height; |
130 | |
|
131 | 0 | total_size = y_size + a_size + 2 * uv_size; |
132 | | |
133 | | // Security and validation checks |
134 | 0 | if (width <= 0 || height <= 0 || // luma/alpha param error |
135 | 0 | uv_width <= 0 || uv_height <= 0) { // u/v param error |
136 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION); |
137 | 0 | } |
138 | | // allocate a new buffer. |
139 | 0 | mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem)); |
140 | 0 | if (mem == NULL) { |
141 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
142 | 0 | } |
143 | | |
144 | | // From now on, we're in the clear, we can no longer fail... |
145 | 0 | picture->memory_ = (void*)mem; |
146 | 0 | picture->y_stride = y_stride; |
147 | 0 | picture->uv_stride = uv_stride; |
148 | 0 | picture->a_stride = a_stride; |
149 | | |
150 | | // TODO(skal): we could align the y/u/v planes and adjust stride. |
151 | 0 | picture->y = mem; |
152 | 0 | mem += y_size; |
153 | |
|
154 | 0 | picture->u = mem; |
155 | 0 | mem += uv_size; |
156 | 0 | picture->v = mem; |
157 | 0 | mem += uv_size; |
158 | |
|
159 | 0 | if (a_size > 0) { |
160 | 0 | picture->a = mem; |
161 | 0 | mem += a_size; |
162 | 0 | } |
163 | 0 | (void)mem; // makes the static analyzer happy |
164 | 0 | return 1; |
165 | 0 | } |
166 | | |
167 | 0 | int WebPPictureAlloc(WebPPicture* picture) { |
168 | 0 | if (picture != NULL) { |
169 | 0 | WebPPictureFree(picture); // erase previous buffer |
170 | |
|
171 | 0 | if (!picture->use_argb) { |
172 | 0 | return WebPPictureAllocYUVA(picture); |
173 | 0 | } else { |
174 | 0 | return WebPPictureAllocARGB(picture); |
175 | 0 | } |
176 | 0 | } |
177 | 0 | return 1; |
178 | 0 | } |
179 | | |
180 | 0 | void WebPPictureFree(WebPPicture* picture) { |
181 | 0 | if (picture != NULL) { |
182 | 0 | WebPSafeFree(picture->memory_); |
183 | 0 | WebPSafeFree(picture->memory_argb_); |
184 | 0 | WebPPictureResetBuffers(picture); |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | //------------------------------------------------------------------------------ |
189 | | // WebPMemoryWriter: Write-to-memory |
190 | | |
191 | 0 | void WebPMemoryWriterInit(WebPMemoryWriter* writer) { |
192 | 0 | writer->mem = NULL; |
193 | 0 | writer->size = 0; |
194 | 0 | writer->max_size = 0; |
195 | 0 | } |
196 | | |
197 | | int WebPMemoryWrite(const uint8_t* data, size_t data_size, |
198 | 0 | const WebPPicture* picture) { |
199 | 0 | WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr; |
200 | 0 | uint64_t next_size; |
201 | 0 | if (w == NULL) { |
202 | 0 | return 1; |
203 | 0 | } |
204 | 0 | next_size = (uint64_t)w->size + data_size; |
205 | 0 | if (next_size > w->max_size) { |
206 | 0 | uint8_t* new_mem; |
207 | 0 | uint64_t next_max_size = 2ULL * w->max_size; |
208 | 0 | if (next_max_size < next_size) next_max_size = next_size; |
209 | 0 | if (next_max_size < 8192ULL) next_max_size = 8192ULL; |
210 | 0 | new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1); |
211 | 0 | if (new_mem == NULL) { |
212 | 0 | return 0; |
213 | 0 | } |
214 | 0 | if (w->size > 0) { |
215 | 0 | memcpy(new_mem, w->mem, w->size); |
216 | 0 | } |
217 | 0 | WebPSafeFree(w->mem); |
218 | 0 | w->mem = new_mem; |
219 | | // down-cast is ok, thanks to WebPSafeMalloc |
220 | 0 | w->max_size = (size_t)next_max_size; |
221 | 0 | } |
222 | 0 | if (data_size > 0) { |
223 | 0 | memcpy(w->mem + w->size, data, data_size); |
224 | 0 | w->size += data_size; |
225 | 0 | } |
226 | 0 | return 1; |
227 | 0 | } |
228 | | |
229 | 0 | void WebPMemoryWriterClear(WebPMemoryWriter* writer) { |
230 | 0 | if (writer != NULL) { |
231 | 0 | WebPSafeFree(writer->mem); |
232 | 0 | WebPMemoryWriterInit(writer); |
233 | 0 | } |
234 | 0 | } |
235 | | |
236 | | //------------------------------------------------------------------------------ |
237 | | // Simplest high-level calls: |
238 | | |
239 | | typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int); |
240 | | |
241 | | static size_t Encode(const uint8_t* rgba, int width, int height, int stride, |
242 | | Importer import, float quality_factor, int lossless, |
243 | 0 | uint8_t** output) { |
244 | 0 | WebPPicture pic; |
245 | 0 | WebPConfig config; |
246 | 0 | WebPMemoryWriter wrt; |
247 | 0 | int ok; |
248 | |
|
249 | 0 | if (output == NULL) return 0; |
250 | | |
251 | 0 | if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) || |
252 | 0 | !WebPPictureInit(&pic)) { |
253 | 0 | return 0; // shouldn't happen, except if system installation is broken |
254 | 0 | } |
255 | | |
256 | 0 | config.lossless = !!lossless; |
257 | 0 | pic.use_argb = !!lossless; |
258 | 0 | pic.width = width; |
259 | 0 | pic.height = height; |
260 | 0 | pic.writer = WebPMemoryWrite; |
261 | 0 | pic.custom_ptr = &wrt; |
262 | 0 | WebPMemoryWriterInit(&wrt); |
263 | |
|
264 | 0 | ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic); |
265 | 0 | WebPPictureFree(&pic); |
266 | 0 | if (!ok) { |
267 | 0 | WebPMemoryWriterClear(&wrt); |
268 | 0 | *output = NULL; |
269 | 0 | return 0; |
270 | 0 | } |
271 | 0 | *output = wrt.mem; |
272 | 0 | return wrt.size; |
273 | 0 | } |
274 | | |
275 | | #define ENCODE_FUNC(NAME, IMPORTER) \ |
276 | | size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \ |
277 | 0 | uint8_t** out) { \ |
278 | 0 | return Encode(in, w, h, bps, IMPORTER, q, 0, out); \ |
279 | 0 | } Unexecuted instantiation: WebPEncodeRGB Unexecuted instantiation: WebPEncodeRGBA Unexecuted instantiation: WebPEncodeBGR Unexecuted instantiation: WebPEncodeBGRA |
280 | | |
281 | | ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB) |
282 | | ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA) |
283 | | #if !defined(WEBP_REDUCE_CSP) |
284 | | ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR) |
285 | | ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA) |
286 | | #endif // WEBP_REDUCE_CSP |
287 | | |
288 | | #undef ENCODE_FUNC |
289 | | |
290 | 0 | #define LOSSLESS_DEFAULT_QUALITY 70. |
291 | | #define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER) \ |
292 | 0 | size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) { \ |
293 | 0 | return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out); \ |
294 | 0 | } Unexecuted instantiation: WebPEncodeLosslessRGB Unexecuted instantiation: WebPEncodeLosslessRGBA Unexecuted instantiation: WebPEncodeLosslessBGR Unexecuted instantiation: WebPEncodeLosslessBGRA |
295 | | |
296 | | LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB) |
297 | | LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA) |
298 | | #if !defined(WEBP_REDUCE_CSP) |
299 | | LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR) |
300 | | LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA) |
301 | | #endif // WEBP_REDUCE_CSP |
302 | | |
303 | | #undef LOSSLESS_ENCODE_FUNC |
304 | | |
305 | | //------------------------------------------------------------------------------ |