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