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