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