/src/libwebp/src/enc/alpha_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 | | // Alpha-plane compression. |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | #include <stdlib.h> |
16 | | #include <string.h> |
17 | | |
18 | | #include "src/enc/vp8i_enc.h" |
19 | | #include "src/dsp/dsp.h" |
20 | | #include "src/utils/filters_utils.h" |
21 | | #include "src/utils/quant_levels_utils.h" |
22 | | #include "src/utils/utils.h" |
23 | | #include "src/webp/encode.h" |
24 | | #include "src/webp/format_constants.h" |
25 | | |
26 | | // ----------------------------------------------------------------------------- |
27 | | // Encodes the given alpha data via specified compression method 'method'. |
28 | | // The pre-processing (quantization) is performed if 'quality' is less than 100. |
29 | | // For such cases, the encoding is lossy. The valid range is [0, 100] for |
30 | | // 'quality' and [0, 1] for 'method': |
31 | | // 'method = 0' - No compression; |
32 | | // 'method = 1' - Use lossless coder on the alpha plane only |
33 | | // 'filter' values [0, 4] correspond to prediction modes none, horizontal, |
34 | | // vertical & gradient filters. The prediction mode 4 will try all the |
35 | | // prediction modes 0 to 3 and pick the best one. |
36 | | // 'effort_level': specifies how much effort must be spent to try and reduce |
37 | | // the compressed output size. In range 0 (quick) to 6 (slow). |
38 | | // |
39 | | // 'output' corresponds to the buffer containing compressed alpha data. |
40 | | // This buffer is allocated by this method and caller should call |
41 | | // WebPSafeFree(*output) when done. |
42 | | // 'output_size' corresponds to size of this compressed alpha buffer. |
43 | | // |
44 | | // Returns 1 on successfully encoding the alpha and |
45 | | // 0 if either: |
46 | | // invalid quality or method, or |
47 | | // memory allocation for the compressed data fails. |
48 | | |
49 | | #include "src/enc/vp8li_enc.h" |
50 | | |
51 | | static int EncodeLossless(const uint8_t* const data, int width, int height, |
52 | | int effort_level, // in [0..6] range |
53 | | int use_quality_100, VP8LBitWriter* const bw, |
54 | 0 | WebPAuxStats* const stats) { |
55 | 0 | int ok = 0; |
56 | 0 | WebPConfig config; |
57 | 0 | WebPPicture picture; |
58 | |
|
59 | 0 | if (!WebPPictureInit(&picture)) return 0; |
60 | 0 | picture.width = width; |
61 | 0 | picture.height = height; |
62 | 0 | picture.use_argb = 1; |
63 | 0 | picture.stats = stats; |
64 | 0 | if (!WebPPictureAlloc(&picture)) return 0; |
65 | | |
66 | | // Transfer the alpha values to the green channel. |
67 | 0 | WebPDispatchAlphaToGreen(data, width, picture.width, picture.height, |
68 | 0 | picture.argb, picture.argb_stride); |
69 | |
|
70 | 0 | if (!WebPConfigInit(&config)) return 0; |
71 | 0 | config.lossless = 1; |
72 | | // Enable exact, or it would alter RGB values of transparent alpha, which is |
73 | | // normally OK but not here since we are not encoding the input image but an |
74 | | // internal encoding-related image containing necessary exact information in |
75 | | // RGB channels. |
76 | 0 | config.exact = 1; |
77 | 0 | config.method = effort_level; // impact is very small |
78 | | // Set a low default quality for encoding alpha. Ensure that Alpha quality at |
79 | | // lower methods (3 and below) is less than the threshold for triggering |
80 | | // costly 'BackwardReferencesTraceBackwards'. |
81 | | // If the alpha quality is set to 100 and the method to 6, allow for a high |
82 | | // lossless quality to trigger the cruncher. |
83 | 0 | config.quality = |
84 | 0 | (use_quality_100 && effort_level == 6) ? 100 : 8.f * effort_level; |
85 | 0 | assert(config.quality >= 0 && config.quality <= 100.f); |
86 | | |
87 | 0 | ok = VP8LEncodeStream(&config, &picture, bw); |
88 | 0 | WebPPictureFree(&picture); |
89 | 0 | ok = ok && !bw->error_; |
90 | 0 | if (!ok) { |
91 | 0 | VP8LBitWriterWipeOut(bw); |
92 | 0 | return 0; |
93 | 0 | } |
94 | 0 | return 1; |
95 | 0 | } |
96 | | |
97 | | // ----------------------------------------------------------------------------- |
98 | | |
99 | | // Small struct to hold the result of a filter mode compression attempt. |
100 | | typedef struct { |
101 | | size_t score; |
102 | | VP8BitWriter bw; |
103 | | WebPAuxStats stats; |
104 | | } FilterTrial; |
105 | | |
106 | | // This function always returns an initialized 'bw' object, even upon error. |
107 | | static int EncodeAlphaInternal(const uint8_t* const data, int width, int height, |
108 | | int method, int filter, int reduce_levels, |
109 | | int effort_level, // in [0..6] range |
110 | | uint8_t* const tmp_alpha, |
111 | 0 | FilterTrial* result) { |
112 | 0 | int ok = 0; |
113 | 0 | const uint8_t* alpha_src; |
114 | 0 | WebPFilterFunc filter_func; |
115 | 0 | uint8_t header; |
116 | 0 | const size_t data_size = width * height; |
117 | 0 | const uint8_t* output = NULL; |
118 | 0 | size_t output_size = 0; |
119 | 0 | VP8LBitWriter tmp_bw; |
120 | |
|
121 | 0 | assert((uint64_t)data_size == (uint64_t)width * height); // as per spec |
122 | 0 | assert(filter >= 0 && filter < WEBP_FILTER_LAST); |
123 | 0 | assert(method >= ALPHA_NO_COMPRESSION); |
124 | 0 | assert(method <= ALPHA_LOSSLESS_COMPRESSION); |
125 | 0 | assert(sizeof(header) == ALPHA_HEADER_LEN); |
126 | | |
127 | 0 | filter_func = WebPFilters[filter]; |
128 | 0 | if (filter_func != NULL) { |
129 | 0 | filter_func(data, width, height, width, tmp_alpha); |
130 | 0 | alpha_src = tmp_alpha; |
131 | 0 | } else { |
132 | 0 | alpha_src = data; |
133 | 0 | } |
134 | |
|
135 | 0 | if (method != ALPHA_NO_COMPRESSION) { |
136 | 0 | ok = VP8LBitWriterInit(&tmp_bw, data_size >> 3); |
137 | 0 | ok = ok && EncodeLossless(alpha_src, width, height, effort_level, |
138 | 0 | !reduce_levels, &tmp_bw, &result->stats); |
139 | 0 | if (ok) { |
140 | 0 | output = VP8LBitWriterFinish(&tmp_bw); |
141 | 0 | if (tmp_bw.error_) { |
142 | 0 | VP8LBitWriterWipeOut(&tmp_bw); |
143 | 0 | memset(&result->bw, 0, sizeof(result->bw)); |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | output_size = VP8LBitWriterNumBytes(&tmp_bw); |
147 | 0 | if (output_size > data_size) { |
148 | | // compressed size is larger than source! Revert to uncompressed mode. |
149 | 0 | method = ALPHA_NO_COMPRESSION; |
150 | 0 | VP8LBitWriterWipeOut(&tmp_bw); |
151 | 0 | } |
152 | 0 | } else { |
153 | 0 | VP8LBitWriterWipeOut(&tmp_bw); |
154 | 0 | memset(&result->bw, 0, sizeof(result->bw)); |
155 | 0 | return 0; |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | 0 | if (method == ALPHA_NO_COMPRESSION) { |
160 | 0 | output = alpha_src; |
161 | 0 | output_size = data_size; |
162 | 0 | ok = 1; |
163 | 0 | } |
164 | | |
165 | | // Emit final result. |
166 | 0 | header = method | (filter << 2); |
167 | 0 | if (reduce_levels) header |= ALPHA_PREPROCESSED_LEVELS << 4; |
168 | |
|
169 | 0 | if (!VP8BitWriterInit(&result->bw, ALPHA_HEADER_LEN + output_size)) ok = 0; |
170 | 0 | ok = ok && VP8BitWriterAppend(&result->bw, &header, ALPHA_HEADER_LEN); |
171 | 0 | ok = ok && VP8BitWriterAppend(&result->bw, output, output_size); |
172 | |
|
173 | 0 | if (method != ALPHA_NO_COMPRESSION) { |
174 | 0 | VP8LBitWriterWipeOut(&tmp_bw); |
175 | 0 | } |
176 | 0 | ok = ok && !result->bw.error_; |
177 | 0 | result->score = VP8BitWriterSize(&result->bw); |
178 | 0 | return ok; |
179 | 0 | } |
180 | | |
181 | | // ----------------------------------------------------------------------------- |
182 | | |
183 | | static int GetNumColors(const uint8_t* data, int width, int height, |
184 | 0 | int stride) { |
185 | 0 | int j; |
186 | 0 | int colors = 0; |
187 | 0 | uint8_t color[256] = { 0 }; |
188 | |
|
189 | 0 | for (j = 0; j < height; ++j) { |
190 | 0 | int i; |
191 | 0 | const uint8_t* const p = data + j * stride; |
192 | 0 | for (i = 0; i < width; ++i) { |
193 | 0 | color[p[i]] = 1; |
194 | 0 | } |
195 | 0 | } |
196 | 0 | for (j = 0; j < 256; ++j) { |
197 | 0 | if (color[j] > 0) ++colors; |
198 | 0 | } |
199 | 0 | return colors; |
200 | 0 | } |
201 | | |
202 | 0 | #define FILTER_TRY_NONE (1 << WEBP_FILTER_NONE) |
203 | 0 | #define FILTER_TRY_ALL ((1 << WEBP_FILTER_LAST) - 1) |
204 | | |
205 | | // Given the input 'filter' option, return an OR'd bit-set of filters to try. |
206 | | static uint32_t GetFilterMap(const uint8_t* alpha, int width, int height, |
207 | 0 | int filter, int effort_level) { |
208 | 0 | uint32_t bit_map = 0U; |
209 | 0 | if (filter == WEBP_FILTER_FAST) { |
210 | | // Quick estimate of the best candidate. |
211 | 0 | int try_filter_none = (effort_level > 3); |
212 | 0 | const int kMinColorsForFilterNone = 16; |
213 | 0 | const int kMaxColorsForFilterNone = 192; |
214 | 0 | const int num_colors = GetNumColors(alpha, width, height, width); |
215 | | // For low number of colors, NONE yields better compression. |
216 | 0 | filter = (num_colors <= kMinColorsForFilterNone) |
217 | 0 | ? WEBP_FILTER_NONE |
218 | 0 | : WebPEstimateBestFilter(alpha, width, height, width); |
219 | 0 | bit_map |= 1 << filter; |
220 | | // For large number of colors, try FILTER_NONE in addition to the best |
221 | | // filter as well. |
222 | 0 | if (try_filter_none || num_colors > kMaxColorsForFilterNone) { |
223 | 0 | bit_map |= FILTER_TRY_NONE; |
224 | 0 | } |
225 | 0 | } else if (filter == WEBP_FILTER_NONE) { |
226 | 0 | bit_map = FILTER_TRY_NONE; |
227 | 0 | } else { // WEBP_FILTER_BEST -> try all |
228 | 0 | bit_map = FILTER_TRY_ALL; |
229 | 0 | } |
230 | 0 | return bit_map; |
231 | 0 | } |
232 | | |
233 | 0 | static void InitFilterTrial(FilterTrial* const score) { |
234 | 0 | score->score = (size_t)~0U; |
235 | 0 | VP8BitWriterInit(&score->bw, 0); |
236 | 0 | } |
237 | | |
238 | | static int ApplyFiltersAndEncode(const uint8_t* alpha, int width, int height, |
239 | | size_t data_size, int method, int filter, |
240 | | int reduce_levels, int effort_level, |
241 | | uint8_t** const output, |
242 | | size_t* const output_size, |
243 | 0 | WebPAuxStats* const stats) { |
244 | 0 | int ok = 1; |
245 | 0 | FilterTrial best; |
246 | 0 | uint32_t try_map = |
247 | 0 | GetFilterMap(alpha, width, height, filter, effort_level); |
248 | 0 | InitFilterTrial(&best); |
249 | |
|
250 | 0 | if (try_map != FILTER_TRY_NONE) { |
251 | 0 | uint8_t* filtered_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size); |
252 | 0 | if (filtered_alpha == NULL) return 0; |
253 | | |
254 | 0 | for (filter = WEBP_FILTER_NONE; ok && try_map; ++filter, try_map >>= 1) { |
255 | 0 | if (try_map & 1) { |
256 | 0 | FilterTrial trial; |
257 | 0 | ok = EncodeAlphaInternal(alpha, width, height, method, filter, |
258 | 0 | reduce_levels, effort_level, filtered_alpha, |
259 | 0 | &trial); |
260 | 0 | if (ok && trial.score < best.score) { |
261 | 0 | VP8BitWriterWipeOut(&best.bw); |
262 | 0 | best = trial; |
263 | 0 | } else { |
264 | 0 | VP8BitWriterWipeOut(&trial.bw); |
265 | 0 | } |
266 | 0 | } |
267 | 0 | } |
268 | 0 | WebPSafeFree(filtered_alpha); |
269 | 0 | } else { |
270 | 0 | ok = EncodeAlphaInternal(alpha, width, height, method, WEBP_FILTER_NONE, |
271 | 0 | reduce_levels, effort_level, NULL, &best); |
272 | 0 | } |
273 | 0 | if (ok) { |
274 | 0 | #if !defined(WEBP_DISABLE_STATS) |
275 | 0 | if (stats != NULL) { |
276 | 0 | stats->lossless_features = best.stats.lossless_features; |
277 | 0 | stats->histogram_bits = best.stats.histogram_bits; |
278 | 0 | stats->transform_bits = best.stats.transform_bits; |
279 | 0 | stats->cross_color_transform_bits = best.stats.cross_color_transform_bits; |
280 | 0 | stats->cache_bits = best.stats.cache_bits; |
281 | 0 | stats->palette_size = best.stats.palette_size; |
282 | 0 | stats->lossless_size = best.stats.lossless_size; |
283 | 0 | stats->lossless_hdr_size = best.stats.lossless_hdr_size; |
284 | 0 | stats->lossless_data_size = best.stats.lossless_data_size; |
285 | 0 | } |
286 | | #else |
287 | | (void)stats; |
288 | | #endif |
289 | 0 | *output_size = VP8BitWriterSize(&best.bw); |
290 | 0 | *output = VP8BitWriterBuf(&best.bw); |
291 | 0 | } else { |
292 | 0 | VP8BitWriterWipeOut(&best.bw); |
293 | 0 | } |
294 | 0 | return ok; |
295 | 0 | } |
296 | | |
297 | | static int EncodeAlpha(VP8Encoder* const enc, |
298 | | int quality, int method, int filter, |
299 | | int effort_level, |
300 | 0 | uint8_t** const output, size_t* const output_size) { |
301 | 0 | const WebPPicture* const pic = enc->pic_; |
302 | 0 | const int width = pic->width; |
303 | 0 | const int height = pic->height; |
304 | |
|
305 | 0 | uint8_t* quant_alpha = NULL; |
306 | 0 | const size_t data_size = width * height; |
307 | 0 | uint64_t sse = 0; |
308 | 0 | int ok = 1; |
309 | 0 | const int reduce_levels = (quality < 100); |
310 | | |
311 | | // quick correctness checks |
312 | 0 | assert((uint64_t)data_size == (uint64_t)width * height); // as per spec |
313 | 0 | assert(enc != NULL && pic != NULL && pic->a != NULL); |
314 | 0 | assert(output != NULL && output_size != NULL); |
315 | 0 | assert(width > 0 && height > 0); |
316 | 0 | assert(pic->a_stride >= width); |
317 | 0 | assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST); |
318 | | |
319 | 0 | if (quality < 0 || quality > 100) { |
320 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
321 | 0 | } |
322 | | |
323 | 0 | if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) { |
324 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
325 | 0 | } |
326 | | |
327 | 0 | if (method == ALPHA_NO_COMPRESSION) { |
328 | | // Don't filter, as filtering will make no impact on compressed size. |
329 | 0 | filter = WEBP_FILTER_NONE; |
330 | 0 | } |
331 | |
|
332 | 0 | quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size); |
333 | 0 | if (quant_alpha == NULL) { |
334 | 0 | return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
335 | 0 | } |
336 | | |
337 | | // Extract alpha data (width x height) from raw_data (stride x height). |
338 | 0 | WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height); |
339 | |
|
340 | 0 | if (reduce_levels) { // No Quantization required for 'quality = 100'. |
341 | | // 16 alpha levels gives quite a low MSE w.r.t original alpha plane hence |
342 | | // mapped to moderate quality 70. Hence Quality:[0, 70] -> Levels:[2, 16] |
343 | | // and Quality:]70, 100] -> Levels:]16, 256]. |
344 | 0 | const int alpha_levels = (quality <= 70) ? (2 + quality / 5) |
345 | 0 | : (16 + (quality - 70) * 8); |
346 | 0 | ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse); |
347 | 0 | } |
348 | |
|
349 | 0 | if (ok) { |
350 | 0 | VP8FiltersInit(); |
351 | 0 | ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method, |
352 | 0 | filter, reduce_levels, effort_level, output, |
353 | 0 | output_size, pic->stats); |
354 | 0 | if (!ok) { |
355 | 0 | WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); // imprecise |
356 | 0 | } |
357 | 0 | #if !defined(WEBP_DISABLE_STATS) |
358 | 0 | if (pic->stats != NULL) { // need stats? |
359 | 0 | pic->stats->coded_size += (int)(*output_size); |
360 | 0 | enc->sse_[3] = sse; |
361 | 0 | } |
362 | 0 | #endif |
363 | 0 | } |
364 | |
|
365 | 0 | WebPSafeFree(quant_alpha); |
366 | 0 | return ok; |
367 | 0 | } |
368 | | |
369 | | //------------------------------------------------------------------------------ |
370 | | // Main calls |
371 | | |
372 | 0 | static int CompressAlphaJob(void* arg1, void* unused) { |
373 | 0 | VP8Encoder* const enc = (VP8Encoder*)arg1; |
374 | 0 | const WebPConfig* config = enc->config_; |
375 | 0 | uint8_t* alpha_data = NULL; |
376 | 0 | size_t alpha_size = 0; |
377 | 0 | const int effort_level = config->method; // maps to [0..6] |
378 | 0 | const WEBP_FILTER_TYPE filter = |
379 | 0 | (config->alpha_filtering == 0) ? WEBP_FILTER_NONE : |
380 | 0 | (config->alpha_filtering == 1) ? WEBP_FILTER_FAST : |
381 | 0 | WEBP_FILTER_BEST; |
382 | 0 | if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression, |
383 | 0 | filter, effort_level, &alpha_data, &alpha_size)) { |
384 | 0 | return 0; |
385 | 0 | } |
386 | 0 | if (alpha_size != (uint32_t)alpha_size) { // Soundness check. |
387 | 0 | WebPSafeFree(alpha_data); |
388 | 0 | return 0; |
389 | 0 | } |
390 | 0 | enc->alpha_data_size_ = (uint32_t)alpha_size; |
391 | 0 | enc->alpha_data_ = alpha_data; |
392 | 0 | (void)unused; |
393 | 0 | return 1; |
394 | 0 | } |
395 | | |
396 | 0 | void VP8EncInitAlpha(VP8Encoder* const enc) { |
397 | 0 | WebPInitAlphaProcessing(); |
398 | 0 | enc->has_alpha_ = WebPPictureHasTransparency(enc->pic_); |
399 | 0 | enc->alpha_data_ = NULL; |
400 | 0 | enc->alpha_data_size_ = 0; |
401 | 0 | if (enc->thread_level_ > 0) { |
402 | 0 | WebPWorker* const worker = &enc->alpha_worker_; |
403 | 0 | WebPGetWorkerInterface()->Init(worker); |
404 | 0 | worker->data1 = enc; |
405 | 0 | worker->data2 = NULL; |
406 | 0 | worker->hook = CompressAlphaJob; |
407 | 0 | } |
408 | 0 | } |
409 | | |
410 | 0 | int VP8EncStartAlpha(VP8Encoder* const enc) { |
411 | 0 | if (enc->has_alpha_) { |
412 | 0 | if (enc->thread_level_ > 0) { |
413 | 0 | WebPWorker* const worker = &enc->alpha_worker_; |
414 | | // Makes sure worker is good to go. |
415 | 0 | if (!WebPGetWorkerInterface()->Reset(worker)) { |
416 | 0 | return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY); |
417 | 0 | } |
418 | 0 | WebPGetWorkerInterface()->Launch(worker); |
419 | 0 | return 1; |
420 | 0 | } else { |
421 | 0 | return CompressAlphaJob(enc, NULL); // just do the job right away |
422 | 0 | } |
423 | 0 | } |
424 | 0 | return 1; |
425 | 0 | } |
426 | | |
427 | 0 | int VP8EncFinishAlpha(VP8Encoder* const enc) { |
428 | 0 | if (enc->has_alpha_) { |
429 | 0 | if (enc->thread_level_ > 0) { |
430 | 0 | WebPWorker* const worker = &enc->alpha_worker_; |
431 | 0 | if (!WebPGetWorkerInterface()->Sync(worker)) return 0; // error |
432 | 0 | } |
433 | 0 | } |
434 | 0 | return WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_); |
435 | 0 | } |
436 | | |
437 | 0 | int VP8EncDeleteAlpha(VP8Encoder* const enc) { |
438 | 0 | int ok = 1; |
439 | 0 | if (enc->thread_level_ > 0) { |
440 | 0 | WebPWorker* const worker = &enc->alpha_worker_; |
441 | | // finish anything left in flight |
442 | 0 | ok = WebPGetWorkerInterface()->Sync(worker); |
443 | | // still need to end the worker, even if !ok |
444 | 0 | WebPGetWorkerInterface()->End(worker); |
445 | 0 | } |
446 | 0 | WebPSafeFree(enc->alpha_data_); |
447 | 0 | enc->alpha_data_ = NULL; |
448 | 0 | enc->alpha_data_size_ = 0; |
449 | 0 | enc->has_alpha_ = 0; |
450 | 0 | return ok; |
451 | 0 | } |