xref: /aosp_15_r20/external/webp/src/enc/alpha_enc.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 ([email protected])
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->cache_bits = best.stats.cache_bits;
280       stats->palette_size = best.stats.palette_size;
281       stats->lossless_size = best.stats.lossless_size;
282       stats->lossless_hdr_size = best.stats.lossless_hdr_size;
283       stats->lossless_data_size = best.stats.lossless_data_size;
284     }
285 #else
286     (void)stats;
287 #endif
288     *output_size = VP8BitWriterSize(&best.bw);
289     *output = VP8BitWriterBuf(&best.bw);
290   } else {
291     VP8BitWriterWipeOut(&best.bw);
292   }
293   return ok;
294 }
295 
EncodeAlpha(VP8Encoder * const enc,int quality,int method,int filter,int effort_level,uint8_t ** const output,size_t * const output_size)296 static int EncodeAlpha(VP8Encoder* const enc,
297                        int quality, int method, int filter,
298                        int effort_level,
299                        uint8_t** const output, size_t* const output_size) {
300   const WebPPicture* const pic = enc->pic_;
301   const int width = pic->width;
302   const int height = pic->height;
303 
304   uint8_t* quant_alpha = NULL;
305   const size_t data_size = width * height;
306   uint64_t sse = 0;
307   int ok = 1;
308   const int reduce_levels = (quality < 100);
309 
310   // quick correctness checks
311   assert((uint64_t)data_size == (uint64_t)width * height);  // as per spec
312   assert(enc != NULL && pic != NULL && pic->a != NULL);
313   assert(output != NULL && output_size != NULL);
314   assert(width > 0 && height > 0);
315   assert(pic->a_stride >= width);
316   assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST);
317 
318   if (quality < 0 || quality > 100) {
319     return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
320   }
321 
322   if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) {
323     return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
324   }
325 
326   if (method == ALPHA_NO_COMPRESSION) {
327     // Don't filter, as filtering will make no impact on compressed size.
328     filter = WEBP_FILTER_NONE;
329   }
330 
331   quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size);
332   if (quant_alpha == NULL) {
333     return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
334   }
335 
336   // Extract alpha data (width x height) from raw_data (stride x height).
337   WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height);
338 
339   if (reduce_levels) {  // No Quantization required for 'quality = 100'.
340     // 16 alpha levels gives quite a low MSE w.r.t original alpha plane hence
341     // mapped to moderate quality 70. Hence Quality:[0, 70] -> Levels:[2, 16]
342     // and Quality:]70, 100] -> Levels:]16, 256].
343     const int alpha_levels = (quality <= 70) ? (2 + quality / 5)
344                                              : (16 + (quality - 70) * 8);
345     ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse);
346   }
347 
348   if (ok) {
349     VP8FiltersInit();
350     ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method,
351                                filter, reduce_levels, effort_level, output,
352                                output_size, pic->stats);
353     if (!ok) {
354       WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);  // imprecise
355     }
356 #if !defined(WEBP_DISABLE_STATS)
357     if (pic->stats != NULL) {  // need stats?
358       pic->stats->coded_size += (int)(*output_size);
359       enc->sse_[3] = sse;
360     }
361 #endif
362   }
363 
364   WebPSafeFree(quant_alpha);
365   return ok;
366 }
367 
368 //------------------------------------------------------------------------------
369 // Main calls
370 
CompressAlphaJob(void * arg1,void * unused)371 static int CompressAlphaJob(void* arg1, void* unused) {
372   VP8Encoder* const enc = (VP8Encoder*)arg1;
373   const WebPConfig* config = enc->config_;
374   uint8_t* alpha_data = NULL;
375   size_t alpha_size = 0;
376   const int effort_level = config->method;  // maps to [0..6]
377   const WEBP_FILTER_TYPE filter =
378       (config->alpha_filtering == 0) ? WEBP_FILTER_NONE :
379       (config->alpha_filtering == 1) ? WEBP_FILTER_FAST :
380                                        WEBP_FILTER_BEST;
381   if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression,
382                    filter, effort_level, &alpha_data, &alpha_size)) {
383     return 0;
384   }
385   if (alpha_size != (uint32_t)alpha_size) {  // Soundness check.
386     WebPSafeFree(alpha_data);
387     return 0;
388   }
389   enc->alpha_data_size_ = (uint32_t)alpha_size;
390   enc->alpha_data_ = alpha_data;
391   (void)unused;
392   return 1;
393 }
394 
VP8EncInitAlpha(VP8Encoder * const enc)395 void VP8EncInitAlpha(VP8Encoder* const enc) {
396   WebPInitAlphaProcessing();
397   enc->has_alpha_ = WebPPictureHasTransparency(enc->pic_);
398   enc->alpha_data_ = NULL;
399   enc->alpha_data_size_ = 0;
400   if (enc->thread_level_ > 0) {
401     WebPWorker* const worker = &enc->alpha_worker_;
402     WebPGetWorkerInterface()->Init(worker);
403     worker->data1 = enc;
404     worker->data2 = NULL;
405     worker->hook = CompressAlphaJob;
406   }
407 }
408 
VP8EncStartAlpha(VP8Encoder * const enc)409 int VP8EncStartAlpha(VP8Encoder* const enc) {
410   if (enc->has_alpha_) {
411     if (enc->thread_level_ > 0) {
412       WebPWorker* const worker = &enc->alpha_worker_;
413       // Makes sure worker is good to go.
414       if (!WebPGetWorkerInterface()->Reset(worker)) {
415         return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
416       }
417       WebPGetWorkerInterface()->Launch(worker);
418       return 1;
419     } else {
420       return CompressAlphaJob(enc, NULL);   // just do the job right away
421     }
422   }
423   return 1;
424 }
425 
VP8EncFinishAlpha(VP8Encoder * const enc)426 int VP8EncFinishAlpha(VP8Encoder* const enc) {
427   if (enc->has_alpha_) {
428     if (enc->thread_level_ > 0) {
429       WebPWorker* const worker = &enc->alpha_worker_;
430       if (!WebPGetWorkerInterface()->Sync(worker)) return 0;  // error
431     }
432   }
433   return WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_);
434 }
435 
VP8EncDeleteAlpha(VP8Encoder * const enc)436 int VP8EncDeleteAlpha(VP8Encoder* const enc) {
437   int ok = 1;
438   if (enc->thread_level_ > 0) {
439     WebPWorker* const worker = &enc->alpha_worker_;
440     // finish anything left in flight
441     ok = WebPGetWorkerInterface()->Sync(worker);
442     // still need to end the worker, even if !ok
443     WebPGetWorkerInterface()->End(worker);
444   }
445   WebPSafeFree(enc->alpha_data_);
446   enc->alpha_data_ = NULL;
447   enc->alpha_data_size_ = 0;
448   enc->has_alpha_ = 0;
449   return ok;
450 }
451