• 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 // 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 
EncodeLossless(const uint8_t * const data,int width,int height,int effort_level,int use_quality_100,VP8LBitWriter * const bw,WebPAuxStats * const stats)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                           WebPAuxStats* const stats) {
55   int ok = 0;
56   WebPConfig config;
57   WebPPicture picture;
58 
59   if (!WebPPictureInit(&picture)) return 0;
60   picture.width = width;
61   picture.height = height;
62   picture.use_argb = 1;
63   picture.stats = stats;
64   if (!WebPPictureAlloc(&picture)) return 0;
65 
66   // Transfer the alpha values to the green channel.
67   WebPDispatchAlphaToGreen(data, width, picture.width, picture.height,
68                            picture.argb, picture.argb_stride);
69 
70   if (!WebPConfigInit(&config)) return 0;
71   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   config.exact = 1;
77   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   config.quality =
84       (use_quality_100 && effort_level == 6) ? 100 : 8.f * effort_level;
85   assert(config.quality >= 0 && config.quality <= 100.f);
86 
87   ok = VP8LEncodeStream(&config, &picture, bw);
88   WebPPictureFree(&picture);
89   ok = ok && !bw->error_;
90   if (!ok) {
91     VP8LBitWriterWipeOut(bw);
92     return 0;
93   }
94   return 1;
95 }
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.
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)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                                FilterTrial* result) {
112   int ok = 0;
113   const uint8_t* alpha_src;
114   WebPFilterFunc filter_func;
115   uint8_t header;
116   const size_t data_size = width * height;
117   const uint8_t* output = NULL;
118   size_t output_size = 0;
119   VP8LBitWriter tmp_bw;
120 
121   assert((uint64_t)data_size == (uint64_t)width * height);  // as per spec
122   assert(filter >= 0 && filter < WEBP_FILTER_LAST);
123   assert(method >= ALPHA_NO_COMPRESSION);
124   assert(method <= ALPHA_LOSSLESS_COMPRESSION);
125   assert(sizeof(header) == ALPHA_HEADER_LEN);
126 
127   filter_func = WebPFilters[filter];
128   if (filter_func != NULL) {
129     filter_func(data, width, height, width, tmp_alpha);
130     alpha_src = tmp_alpha;
131   }  else {
132     alpha_src = data;
133   }
134 
135   if (method != ALPHA_NO_COMPRESSION) {
136     ok = VP8LBitWriterInit(&tmp_bw, data_size >> 3);
137     ok = ok && EncodeLossless(alpha_src, width, height, effort_level,
138                               !reduce_levels, &tmp_bw, &result->stats);
139     if (ok) {
140       output = VP8LBitWriterFinish(&tmp_bw);
141       if (tmp_bw.error_) {
142         VP8LBitWriterWipeOut(&tmp_bw);
143         memset(&result->bw, 0, sizeof(result->bw));
144         return 0;
145       }
146       output_size = VP8LBitWriterNumBytes(&tmp_bw);
147       if (output_size > data_size) {
148         // compressed size is larger than source! Revert to uncompressed mode.
149         method = ALPHA_NO_COMPRESSION;
150         VP8LBitWriterWipeOut(&tmp_bw);
151       }
152     } else {
153       VP8LBitWriterWipeOut(&tmp_bw);
154       memset(&result->bw, 0, sizeof(result->bw));
155       return 0;
156     }
157   }
158 
159   if (method == ALPHA_NO_COMPRESSION) {
160     output = alpha_src;
161     output_size = data_size;
162     ok = 1;
163   }
164 
165   // Emit final result.
166   header = method | (filter << 2);
167   if (reduce_levels) header |= ALPHA_PREPROCESSED_LEVELS << 4;
168 
169   if (!VP8BitWriterInit(&result->bw, ALPHA_HEADER_LEN + output_size)) ok = 0;
170   ok = ok && VP8BitWriterAppend(&result->bw, &header, ALPHA_HEADER_LEN);
171   ok = ok && VP8BitWriterAppend(&result->bw, output, output_size);
172 
173   if (method != ALPHA_NO_COMPRESSION) {
174     VP8LBitWriterWipeOut(&tmp_bw);
175   }
176   ok = ok && !result->bw.error_;
177   result->score = VP8BitWriterSize(&result->bw);
178   return ok;
179 }
180 
181 // -----------------------------------------------------------------------------
182 
GetNumColors(const uint8_t * data,int width,int height,int stride)183 static int GetNumColors(const uint8_t* data, int width, int height,
184                         int stride) {
185   int j;
186   int colors = 0;
187   uint8_t color[256] = { 0 };
188 
189   for (j = 0; j < height; ++j) {
190     int i;
191     const uint8_t* const p = data + j * stride;
192     for (i = 0; i < width; ++i) {
193       color[p[i]] = 1;
194     }
195   }
196   for (j = 0; j < 256; ++j) {
197     if (color[j] > 0) ++colors;
198   }
199   return colors;
200 }
201 
202 #define FILTER_TRY_NONE (1 << WEBP_FILTER_NONE)
203 #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.
GetFilterMap(const uint8_t * alpha,int width,int height,int filter,int effort_level)206 static uint32_t GetFilterMap(const uint8_t* alpha, int width, int height,
207                              int filter, int effort_level) {
208   uint32_t bit_map = 0U;
209   if (filter == WEBP_FILTER_FAST) {
210     // Quick estimate of the best candidate.
211     int try_filter_none = (effort_level > 3);
212     const int kMinColorsForFilterNone = 16;
213     const int kMaxColorsForFilterNone = 192;
214     const int num_colors = GetNumColors(alpha, width, height, width);
215     // For low number of colors, NONE yields better compression.
216     filter = (num_colors <= kMinColorsForFilterNone)
217         ? WEBP_FILTER_NONE
218         : WebPEstimateBestFilter(alpha, width, height, width);
219     bit_map |= 1 << filter;
220     // For large number of colors, try FILTER_NONE in addition to the best
221     // filter as well.
222     if (try_filter_none || num_colors > kMaxColorsForFilterNone) {
223       bit_map |= FILTER_TRY_NONE;
224     }
225   } else if (filter == WEBP_FILTER_NONE) {
226     bit_map = FILTER_TRY_NONE;
227   } else {  // WEBP_FILTER_BEST -> try all
228     bit_map = FILTER_TRY_ALL;
229   }
230   return bit_map;
231 }
232 
InitFilterTrial(FilterTrial * const score)233 static void InitFilterTrial(FilterTrial* const score) {
234   score->score = (size_t)~0U;
235   VP8BitWriterInit(&score->bw, 0);
236 }
237 
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)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                                  WebPAuxStats* const stats) {
244   int ok = 1;
245   FilterTrial best;
246   uint32_t try_map =
247       GetFilterMap(alpha, width, height, filter, effort_level);
248   InitFilterTrial(&best);
249 
250   if (try_map != FILTER_TRY_NONE) {
251     uint8_t* filtered_alpha =  (uint8_t*)WebPSafeMalloc(1ULL, data_size);
252     if (filtered_alpha == NULL) return 0;
253 
254     for (filter = WEBP_FILTER_NONE; ok && try_map; ++filter, try_map >>= 1) {
255       if (try_map & 1) {
256         FilterTrial trial;
257         ok = EncodeAlphaInternal(alpha, width, height, method, filter,
258                                  reduce_levels, effort_level, filtered_alpha,
259                                  &trial);
260         if (ok && trial.score < best.score) {
261           VP8BitWriterWipeOut(&best.bw);
262           best = trial;
263         } else {
264           VP8BitWriterWipeOut(&trial.bw);
265         }
266       }
267     }
268     WebPSafeFree(filtered_alpha);
269   } else {
270     ok = EncodeAlphaInternal(alpha, width, height, method, WEBP_FILTER_NONE,
271                              reduce_levels, effort_level, NULL, &best);
272   }
273   if (ok) {
274 #if !defined(WEBP_DISABLE_STATS)
275     if (stats != NULL) {
276       stats->lossless_features = best.stats.lossless_features;
277       stats->histogram_bits = best.stats.histogram_bits;
278       stats->transform_bits = best.stats.transform_bits;
279       stats->cross_color_transform_bits = best.stats.cross_color_transform_bits;
280       stats->cache_bits = best.stats.cache_bits;
281       stats->palette_size = best.stats.palette_size;
282       stats->lossless_size = best.stats.lossless_size;
283       stats->lossless_hdr_size = best.stats.lossless_hdr_size;
284       stats->lossless_data_size = best.stats.lossless_data_size;
285     }
286 #else
287     (void)stats;
288 #endif
289     *output_size = VP8BitWriterSize(&best.bw);
290     *output = VP8BitWriterBuf(&best.bw);
291   } else {
292     VP8BitWriterWipeOut(&best.bw);
293   }
294   return ok;
295 }
296 
EncodeAlpha(VP8Encoder * const enc,int quality,int method,int filter,int effort_level,uint8_t ** const output,size_t * const output_size)297 static int EncodeAlpha(VP8Encoder* const enc,
298                        int quality, int method, int filter,
299                        int effort_level,
300                        uint8_t** const output, size_t* const output_size) {
301   const WebPPicture* const pic = enc->pic_;
302   const int width = pic->width;
303   const int height = pic->height;
304 
305   uint8_t* quant_alpha = NULL;
306   const size_t data_size = width * height;
307   uint64_t sse = 0;
308   int ok = 1;
309   const int reduce_levels = (quality < 100);
310 
311   // quick correctness checks
312   assert((uint64_t)data_size == (uint64_t)width * height);  // as per spec
313   assert(enc != NULL && pic != NULL && pic->a != NULL);
314   assert(output != NULL && output_size != NULL);
315   assert(width > 0 && height > 0);
316   assert(pic->a_stride >= width);
317   assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST);
318 
319   if (quality < 0 || quality > 100) {
320     return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
321   }
322 
323   if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) {
324     return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
325   }
326 
327   if (method == ALPHA_NO_COMPRESSION) {
328     // Don't filter, as filtering will make no impact on compressed size.
329     filter = WEBP_FILTER_NONE;
330   }
331 
332   quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size);
333   if (quant_alpha == NULL) {
334     return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
335   }
336 
337   // Extract alpha data (width x height) from raw_data (stride x height).
338   WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height);
339 
340   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     const int alpha_levels = (quality <= 70) ? (2 + quality / 5)
345                                              : (16 + (quality - 70) * 8);
346     ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse);
347   }
348 
349   if (ok) {
350     VP8FiltersInit();
351     ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method,
352                                filter, reduce_levels, effort_level, output,
353                                output_size, pic->stats);
354     if (!ok) {
355       WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);  // imprecise
356     }
357 #if !defined(WEBP_DISABLE_STATS)
358     if (pic->stats != NULL) {  // need stats?
359       pic->stats->coded_size += (int)(*output_size);
360       enc->sse_[3] = sse;
361     }
362 #endif
363   }
364 
365   WebPSafeFree(quant_alpha);
366   return ok;
367 }
368 
369 //------------------------------------------------------------------------------
370 // Main calls
371 
CompressAlphaJob(void * arg1,void * unused)372 static int CompressAlphaJob(void* arg1, void* unused) {
373   VP8Encoder* const enc = (VP8Encoder*)arg1;
374   const WebPConfig* config = enc->config_;
375   uint8_t* alpha_data = NULL;
376   size_t alpha_size = 0;
377   const int effort_level = config->method;  // maps to [0..6]
378   const WEBP_FILTER_TYPE filter =
379       (config->alpha_filtering == 0) ? WEBP_FILTER_NONE :
380       (config->alpha_filtering == 1) ? WEBP_FILTER_FAST :
381                                        WEBP_FILTER_BEST;
382   if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression,
383                    filter, effort_level, &alpha_data, &alpha_size)) {
384     return 0;
385   }
386   if (alpha_size != (uint32_t)alpha_size) {  // Soundness check.
387     WebPSafeFree(alpha_data);
388     return 0;
389   }
390   enc->alpha_data_size_ = (uint32_t)alpha_size;
391   enc->alpha_data_ = alpha_data;
392   (void)unused;
393   return 1;
394 }
395 
VP8EncInitAlpha(VP8Encoder * const enc)396 void VP8EncInitAlpha(VP8Encoder* const enc) {
397   WebPInitAlphaProcessing();
398   enc->has_alpha_ = WebPPictureHasTransparency(enc->pic_);
399   enc->alpha_data_ = NULL;
400   enc->alpha_data_size_ = 0;
401   if (enc->thread_level_ > 0) {
402     WebPWorker* const worker = &enc->alpha_worker_;
403     WebPGetWorkerInterface()->Init(worker);
404     worker->data1 = enc;
405     worker->data2 = NULL;
406     worker->hook = CompressAlphaJob;
407   }
408 }
409 
VP8EncStartAlpha(VP8Encoder * const enc)410 int VP8EncStartAlpha(VP8Encoder* const enc) {
411   if (enc->has_alpha_) {
412     if (enc->thread_level_ > 0) {
413       WebPWorker* const worker = &enc->alpha_worker_;
414       // Makes sure worker is good to go.
415       if (!WebPGetWorkerInterface()->Reset(worker)) {
416         return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
417       }
418       WebPGetWorkerInterface()->Launch(worker);
419       return 1;
420     } else {
421       return CompressAlphaJob(enc, NULL);   // just do the job right away
422     }
423   }
424   return 1;
425 }
426 
VP8EncFinishAlpha(VP8Encoder * const enc)427 int VP8EncFinishAlpha(VP8Encoder* const enc) {
428   if (enc->has_alpha_) {
429     if (enc->thread_level_ > 0) {
430       WebPWorker* const worker = &enc->alpha_worker_;
431       if (!WebPGetWorkerInterface()->Sync(worker)) return 0;  // error
432     }
433   }
434   return WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_);
435 }
436 
VP8EncDeleteAlpha(VP8Encoder * const enc)437 int VP8EncDeleteAlpha(VP8Encoder* const enc) {
438   int ok = 1;
439   if (enc->thread_level_ > 0) {
440     WebPWorker* const worker = &enc->alpha_worker_;
441     // finish anything left in flight
442     ok = WebPGetWorkerInterface()->Sync(worker);
443     // still need to end the worker, even if !ok
444     WebPGetWorkerInterface()->End(worker);
445   }
446   WebPSafeFree(enc->alpha_data_);
447   enc->alpha_data_ = NULL;
448   enc->alpha_data_size_ = 0;
449   enc->has_alpha_ = 0;
450   return ok;
451 }
452