• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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 // Author: Jyrki Alakuijala (jyrki@google.com)
11 //
12 #ifdef HAVE_CONFIG_H
13 #include "src/webp/config.h"
14 #endif
15 
16 #include <math.h>
17 
18 #include "src/enc/backward_references_enc.h"
19 #include "src/enc/histogram_enc.h"
20 #include "src/dsp/lossless.h"
21 #include "src/dsp/lossless_common.h"
22 #include "src/utils/utils.h"
23 
24 #define MAX_COST 1.e38
25 
26 // Number of partitions for the three dominant (literal, red and blue) symbol
27 // costs.
28 #define NUM_PARTITIONS 4
29 // The size of the bin-hash corresponding to the three dominant costs.
30 #define BIN_SIZE (NUM_PARTITIONS * NUM_PARTITIONS * NUM_PARTITIONS)
31 // Maximum number of histograms allowed in greedy combining algorithm.
32 #define MAX_HISTO_GREEDY 100
33 
HistogramClear(VP8LHistogram * const p)34 static void HistogramClear(VP8LHistogram* const p) {
35   uint32_t* const literal = p->literal_;
36   const int cache_bits = p->palette_code_bits_;
37   const int histo_size = VP8LGetHistogramSize(cache_bits);
38   memset(p, 0, histo_size);
39   p->palette_code_bits_ = cache_bits;
40   p->literal_ = literal;
41 }
42 
43 // Swap two histogram pointers.
HistogramSwap(VP8LHistogram ** const A,VP8LHistogram ** const B)44 static void HistogramSwap(VP8LHistogram** const A, VP8LHistogram** const B) {
45   VP8LHistogram* const tmp = *A;
46   *A = *B;
47   *B = tmp;
48 }
49 
HistogramCopy(const VP8LHistogram * const src,VP8LHistogram * const dst)50 static void HistogramCopy(const VP8LHistogram* const src,
51                           VP8LHistogram* const dst) {
52   uint32_t* const dst_literal = dst->literal_;
53   const int dst_cache_bits = dst->palette_code_bits_;
54   const int literal_size = VP8LHistogramNumCodes(dst_cache_bits);
55   const int histo_size = VP8LGetHistogramSize(dst_cache_bits);
56   assert(src->palette_code_bits_ == dst_cache_bits);
57   memcpy(dst, src, histo_size);
58   dst->literal_ = dst_literal;
59   memcpy(dst->literal_, src->literal_, literal_size * sizeof(*dst->literal_));
60 }
61 
VP8LGetHistogramSize(int cache_bits)62 int VP8LGetHistogramSize(int cache_bits) {
63   const int literal_size = VP8LHistogramNumCodes(cache_bits);
64   const size_t total_size = sizeof(VP8LHistogram) + sizeof(int) * literal_size;
65   assert(total_size <= (size_t)0x7fffffff);
66   return (int)total_size;
67 }
68 
VP8LFreeHistogram(VP8LHistogram * const histo)69 void VP8LFreeHistogram(VP8LHistogram* const histo) {
70   WebPSafeFree(histo);
71 }
72 
VP8LFreeHistogramSet(VP8LHistogramSet * const histo)73 void VP8LFreeHistogramSet(VP8LHistogramSet* const histo) {
74   WebPSafeFree(histo);
75 }
76 
VP8LHistogramStoreRefs(const VP8LBackwardRefs * const refs,VP8LHistogram * const histo)77 void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs,
78                             VP8LHistogram* const histo) {
79   VP8LRefsCursor c = VP8LRefsCursorInit(refs);
80   while (VP8LRefsCursorOk(&c)) {
81     VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos, NULL, 0);
82     VP8LRefsCursorNext(&c);
83   }
84 }
85 
VP8LHistogramCreate(VP8LHistogram * const p,const VP8LBackwardRefs * const refs,int palette_code_bits)86 void VP8LHistogramCreate(VP8LHistogram* const p,
87                          const VP8LBackwardRefs* const refs,
88                          int palette_code_bits) {
89   if (palette_code_bits >= 0) {
90     p->palette_code_bits_ = palette_code_bits;
91   }
92   HistogramClear(p);
93   VP8LHistogramStoreRefs(refs, p);
94 }
95 
VP8LHistogramInit(VP8LHistogram * const p,int palette_code_bits,int init_arrays)96 void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits,
97                        int init_arrays) {
98   p->palette_code_bits_ = palette_code_bits;
99   if (init_arrays) {
100     HistogramClear(p);
101   } else {
102     p->trivial_symbol_ = 0;
103     p->bit_cost_ = 0.;
104     p->literal_cost_ = 0.;
105     p->red_cost_ = 0.;
106     p->blue_cost_ = 0.;
107     memset(p->is_used_, 0, sizeof(p->is_used_));
108   }
109 }
110 
VP8LAllocateHistogram(int cache_bits)111 VP8LHistogram* VP8LAllocateHistogram(int cache_bits) {
112   VP8LHistogram* histo = NULL;
113   const int total_size = VP8LGetHistogramSize(cache_bits);
114   uint8_t* const memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory));
115   if (memory == NULL) return NULL;
116   histo = (VP8LHistogram*)memory;
117   // literal_ won't necessary be aligned.
118   histo->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram));
119   VP8LHistogramInit(histo, cache_bits, /*init_arrays=*/ 0);
120   return histo;
121 }
122 
123 // Resets the pointers of the histograms to point to the bit buffer in the set.
HistogramSetResetPointers(VP8LHistogramSet * const set,int cache_bits)124 static void HistogramSetResetPointers(VP8LHistogramSet* const set,
125                                       int cache_bits) {
126   int i;
127   const int histo_size = VP8LGetHistogramSize(cache_bits);
128   uint8_t* memory = (uint8_t*) (set->histograms);
129   memory += set->max_size * sizeof(*set->histograms);
130   for (i = 0; i < set->max_size; ++i) {
131     memory = (uint8_t*) WEBP_ALIGN(memory);
132     set->histograms[i] = (VP8LHistogram*) memory;
133     // literal_ won't necessary be aligned.
134     set->histograms[i]->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram));
135     memory += histo_size;
136   }
137 }
138 
139 // Returns the total size of the VP8LHistogramSet.
HistogramSetTotalSize(int size,int cache_bits)140 static size_t HistogramSetTotalSize(int size, int cache_bits) {
141   const int histo_size = VP8LGetHistogramSize(cache_bits);
142   return (sizeof(VP8LHistogramSet) + size * (sizeof(VP8LHistogram*) +
143           histo_size + WEBP_ALIGN_CST));
144 }
145 
VP8LAllocateHistogramSet(int size,int cache_bits)146 VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) {
147   int i;
148   VP8LHistogramSet* set;
149   const size_t total_size = HistogramSetTotalSize(size, cache_bits);
150   uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory));
151   if (memory == NULL) return NULL;
152 
153   set = (VP8LHistogramSet*)memory;
154   memory += sizeof(*set);
155   set->histograms = (VP8LHistogram**)memory;
156   set->max_size = size;
157   set->size = size;
158   HistogramSetResetPointers(set, cache_bits);
159   for (i = 0; i < size; ++i) {
160     VP8LHistogramInit(set->histograms[i], cache_bits, /*init_arrays=*/ 0);
161   }
162   return set;
163 }
164 
VP8LHistogramSetClear(VP8LHistogramSet * const set)165 void VP8LHistogramSetClear(VP8LHistogramSet* const set) {
166   int i;
167   const int cache_bits = set->histograms[0]->palette_code_bits_;
168   const int size = set->max_size;
169   const size_t total_size = HistogramSetTotalSize(size, cache_bits);
170   uint8_t* memory = (uint8_t*)set;
171 
172   memset(memory, 0, total_size);
173   memory += sizeof(*set);
174   set->histograms = (VP8LHistogram**)memory;
175   set->max_size = size;
176   set->size = size;
177   HistogramSetResetPointers(set, cache_bits);
178   for (i = 0; i < size; ++i) {
179     set->histograms[i]->palette_code_bits_ = cache_bits;
180   }
181 }
182 
183 // Removes the histogram 'i' from 'set' by setting it to NULL.
HistogramSetRemoveHistogram(VP8LHistogramSet * const set,int i,int * const num_used)184 static void HistogramSetRemoveHistogram(VP8LHistogramSet* const set, int i,
185                                         int* const num_used) {
186   assert(set->histograms[i] != NULL);
187   set->histograms[i] = NULL;
188   --*num_used;
189   // If we remove the last valid one, shrink until the next valid one.
190   if (i == set->size - 1) {
191     while (set->size >= 1 && set->histograms[set->size - 1] == NULL) {
192       --set->size;
193     }
194   }
195 }
196 
197 // -----------------------------------------------------------------------------
198 
VP8LHistogramAddSinglePixOrCopy(VP8LHistogram * const histo,const PixOrCopy * const v,int (* const distance_modifier)(int,int),int distance_modifier_arg0)199 void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo,
200                                      const PixOrCopy* const v,
201                                      int (*const distance_modifier)(int, int),
202                                      int distance_modifier_arg0) {
203   if (PixOrCopyIsLiteral(v)) {
204     ++histo->alpha_[PixOrCopyLiteral(v, 3)];
205     ++histo->red_[PixOrCopyLiteral(v, 2)];
206     ++histo->literal_[PixOrCopyLiteral(v, 1)];
207     ++histo->blue_[PixOrCopyLiteral(v, 0)];
208   } else if (PixOrCopyIsCacheIdx(v)) {
209     const int literal_ix =
210         NUM_LITERAL_CODES + NUM_LENGTH_CODES + PixOrCopyCacheIdx(v);
211     ++histo->literal_[literal_ix];
212   } else {
213     int code, extra_bits;
214     VP8LPrefixEncodeBits(PixOrCopyLength(v), &code, &extra_bits);
215     ++histo->literal_[NUM_LITERAL_CODES + code];
216     if (distance_modifier == NULL) {
217       VP8LPrefixEncodeBits(PixOrCopyDistance(v), &code, &extra_bits);
218     } else {
219       VP8LPrefixEncodeBits(
220           distance_modifier(distance_modifier_arg0, PixOrCopyDistance(v)),
221           &code, &extra_bits);
222     }
223     ++histo->distance_[code];
224   }
225 }
226 
227 // -----------------------------------------------------------------------------
228 // Entropy-related functions.
229 
BitsEntropyRefine(const VP8LBitEntropy * entropy)230 static WEBP_INLINE double BitsEntropyRefine(const VP8LBitEntropy* entropy) {
231   double mix;
232   if (entropy->nonzeros < 5) {
233     if (entropy->nonzeros <= 1) {
234       return 0;
235     }
236     // Two symbols, they will be 0 and 1 in a Huffman code.
237     // Let's mix in a bit of entropy to favor good clustering when
238     // distributions of these are combined.
239     if (entropy->nonzeros == 2) {
240       return 0.99 * entropy->sum + 0.01 * entropy->entropy;
241     }
242     // No matter what the entropy says, we cannot be better than min_limit
243     // with Huffman coding. I am mixing a bit of entropy into the
244     // min_limit since it produces much better (~0.5 %) compression results
245     // perhaps because of better entropy clustering.
246     if (entropy->nonzeros == 3) {
247       mix = 0.95;
248     } else {
249       mix = 0.7;  // nonzeros == 4.
250     }
251   } else {
252     mix = 0.627;
253   }
254 
255   {
256     double min_limit = 2 * entropy->sum - entropy->max_val;
257     min_limit = mix * min_limit + (1.0 - mix) * entropy->entropy;
258     return (entropy->entropy < min_limit) ? min_limit : entropy->entropy;
259   }
260 }
261 
VP8LBitsEntropy(const uint32_t * const array,int n)262 double VP8LBitsEntropy(const uint32_t* const array, int n) {
263   VP8LBitEntropy entropy;
264   VP8LBitsEntropyUnrefined(array, n, &entropy);
265 
266   return BitsEntropyRefine(&entropy);
267 }
268 
InitialHuffmanCost(void)269 static double InitialHuffmanCost(void) {
270   // Small bias because Huffman code length is typically not stored in
271   // full length.
272   static const int kHuffmanCodeOfHuffmanCodeSize = CODE_LENGTH_CODES * 3;
273   static const double kSmallBias = 9.1;
274   return kHuffmanCodeOfHuffmanCodeSize - kSmallBias;
275 }
276 
277 // Finalize the Huffman cost based on streak numbers and length type (<3 or >=3)
FinalHuffmanCost(const VP8LStreaks * const stats)278 static double FinalHuffmanCost(const VP8LStreaks* const stats) {
279   // The constants in this function are experimental and got rounded from
280   // their original values in 1/8 when switched to 1/1024.
281   double retval = InitialHuffmanCost();
282   // Second coefficient: Many zeros in the histogram are covered efficiently
283   // by a run-length encode. Originally 2/8.
284   retval += stats->counts[0] * 1.5625 + 0.234375 * stats->streaks[0][1];
285   // Second coefficient: Constant values are encoded less efficiently, but still
286   // RLE'ed. Originally 6/8.
287   retval += stats->counts[1] * 2.578125 + 0.703125 * stats->streaks[1][1];
288   // 0s are usually encoded more efficiently than non-0s.
289   // Originally 15/8.
290   retval += 1.796875 * stats->streaks[0][0];
291   // Originally 26/8.
292   retval += 3.28125 * stats->streaks[1][0];
293   return retval;
294 }
295 
296 // Get the symbol entropy for the distribution 'population'.
297 // Set 'trivial_sym', if there's only one symbol present in the distribution.
PopulationCost(const uint32_t * const population,int length,uint32_t * const trivial_sym,uint8_t * const is_used)298 static double PopulationCost(const uint32_t* const population, int length,
299                              uint32_t* const trivial_sym,
300                              uint8_t* const is_used) {
301   VP8LBitEntropy bit_entropy;
302   VP8LStreaks stats;
303   VP8LGetEntropyUnrefined(population, length, &bit_entropy, &stats);
304   if (trivial_sym != NULL) {
305     *trivial_sym = (bit_entropy.nonzeros == 1) ? bit_entropy.nonzero_code
306                                                : VP8L_NON_TRIVIAL_SYM;
307   }
308   // The histogram is used if there is at least one non-zero streak.
309   *is_used = (stats.streaks[1][0] != 0 || stats.streaks[1][1] != 0);
310 
311   return BitsEntropyRefine(&bit_entropy) + FinalHuffmanCost(&stats);
312 }
313 
314 // trivial_at_end is 1 if the two histograms only have one element that is
315 // non-zero: both the zero-th one, or both the last one.
GetCombinedEntropy(const uint32_t * const X,const uint32_t * const Y,int length,int is_X_used,int is_Y_used,int trivial_at_end)316 static WEBP_INLINE double GetCombinedEntropy(const uint32_t* const X,
317                                              const uint32_t* const Y,
318                                              int length, int is_X_used,
319                                              int is_Y_used,
320                                              int trivial_at_end) {
321   VP8LStreaks stats;
322   if (trivial_at_end) {
323     // This configuration is due to palettization that transforms an indexed
324     // pixel into 0xff000000 | (pixel << 8) in VP8LBundleColorMap.
325     // BitsEntropyRefine is 0 for histograms with only one non-zero value.
326     // Only FinalHuffmanCost needs to be evaluated.
327     memset(&stats, 0, sizeof(stats));
328     // Deal with the non-zero value at index 0 or length-1.
329     stats.streaks[1][0] = 1;
330     // Deal with the following/previous zero streak.
331     stats.counts[0] = 1;
332     stats.streaks[0][1] = length - 1;
333     return FinalHuffmanCost(&stats);
334   } else {
335     VP8LBitEntropy bit_entropy;
336     if (is_X_used) {
337       if (is_Y_used) {
338         VP8LGetCombinedEntropyUnrefined(X, Y, length, &bit_entropy, &stats);
339       } else {
340         VP8LGetEntropyUnrefined(X, length, &bit_entropy, &stats);
341       }
342     } else {
343       if (is_Y_used) {
344         VP8LGetEntropyUnrefined(Y, length, &bit_entropy, &stats);
345       } else {
346         memset(&stats, 0, sizeof(stats));
347         stats.counts[0] = 1;
348         stats.streaks[0][length > 3] = length;
349         VP8LBitEntropyInit(&bit_entropy);
350       }
351     }
352 
353     return BitsEntropyRefine(&bit_entropy) + FinalHuffmanCost(&stats);
354   }
355 }
356 
357 // Estimates the Entropy + Huffman + other block overhead size cost.
VP8LHistogramEstimateBits(VP8LHistogram * const p)358 double VP8LHistogramEstimateBits(VP8LHistogram* const p) {
359   return
360       PopulationCost(p->literal_, VP8LHistogramNumCodes(p->palette_code_bits_),
361                      NULL, &p->is_used_[0])
362       + PopulationCost(p->red_, NUM_LITERAL_CODES, NULL, &p->is_used_[1])
363       + PopulationCost(p->blue_, NUM_LITERAL_CODES, NULL, &p->is_used_[2])
364       + PopulationCost(p->alpha_, NUM_LITERAL_CODES, NULL, &p->is_used_[3])
365       + PopulationCost(p->distance_, NUM_DISTANCE_CODES, NULL, &p->is_used_[4])
366       + VP8LExtraCost(p->literal_ + NUM_LITERAL_CODES, NUM_LENGTH_CODES)
367       + VP8LExtraCost(p->distance_, NUM_DISTANCE_CODES);
368 }
369 
370 // -----------------------------------------------------------------------------
371 // Various histogram combine/cost-eval functions
372 
GetCombinedHistogramEntropy(const VP8LHistogram * const a,const VP8LHistogram * const b,double cost_threshold,double * cost)373 static int GetCombinedHistogramEntropy(const VP8LHistogram* const a,
374                                        const VP8LHistogram* const b,
375                                        double cost_threshold,
376                                        double* cost) {
377   const int palette_code_bits = a->palette_code_bits_;
378   int trivial_at_end = 0;
379   assert(a->palette_code_bits_ == b->palette_code_bits_);
380   *cost += GetCombinedEntropy(a->literal_, b->literal_,
381                               VP8LHistogramNumCodes(palette_code_bits),
382                               a->is_used_[0], b->is_used_[0], 0);
383   *cost += VP8LExtraCostCombined(a->literal_ + NUM_LITERAL_CODES,
384                                  b->literal_ + NUM_LITERAL_CODES,
385                                  NUM_LENGTH_CODES);
386   if (*cost > cost_threshold) return 0;
387 
388   if (a->trivial_symbol_ != VP8L_NON_TRIVIAL_SYM &&
389       a->trivial_symbol_ == b->trivial_symbol_) {
390     // A, R and B are all 0 or 0xff.
391     const uint32_t color_a = (a->trivial_symbol_ >> 24) & 0xff;
392     const uint32_t color_r = (a->trivial_symbol_ >> 16) & 0xff;
393     const uint32_t color_b = (a->trivial_symbol_ >> 0) & 0xff;
394     if ((color_a == 0 || color_a == 0xff) &&
395         (color_r == 0 || color_r == 0xff) &&
396         (color_b == 0 || color_b == 0xff)) {
397       trivial_at_end = 1;
398     }
399   }
400 
401   *cost +=
402       GetCombinedEntropy(a->red_, b->red_, NUM_LITERAL_CODES, a->is_used_[1],
403                          b->is_used_[1], trivial_at_end);
404   if (*cost > cost_threshold) return 0;
405 
406   *cost +=
407       GetCombinedEntropy(a->blue_, b->blue_, NUM_LITERAL_CODES, a->is_used_[2],
408                          b->is_used_[2], trivial_at_end);
409   if (*cost > cost_threshold) return 0;
410 
411   *cost +=
412       GetCombinedEntropy(a->alpha_, b->alpha_, NUM_LITERAL_CODES,
413                          a->is_used_[3], b->is_used_[3], trivial_at_end);
414   if (*cost > cost_threshold) return 0;
415 
416   *cost +=
417       GetCombinedEntropy(a->distance_, b->distance_, NUM_DISTANCE_CODES,
418                          a->is_used_[4], b->is_used_[4], 0);
419   *cost +=
420       VP8LExtraCostCombined(a->distance_, b->distance_, NUM_DISTANCE_CODES);
421   if (*cost > cost_threshold) return 0;
422 
423   return 1;
424 }
425 
HistogramAdd(const VP8LHistogram * const a,const VP8LHistogram * const b,VP8LHistogram * const out)426 static WEBP_INLINE void HistogramAdd(const VP8LHistogram* const a,
427                                      const VP8LHistogram* const b,
428                                      VP8LHistogram* const out) {
429   VP8LHistogramAdd(a, b, out);
430   out->trivial_symbol_ = (a->trivial_symbol_ == b->trivial_symbol_)
431                        ? a->trivial_symbol_
432                        : VP8L_NON_TRIVIAL_SYM;
433 }
434 
435 // Performs out = a + b, computing the cost C(a+b) - C(a) - C(b) while comparing
436 // to the threshold value 'cost_threshold'. The score returned is
437 //  Score = C(a+b) - C(a) - C(b), where C(a) + C(b) is known and fixed.
438 // Since the previous score passed is 'cost_threshold', we only need to compare
439 // the partial cost against 'cost_threshold + C(a) + C(b)' to possibly bail-out
440 // early.
HistogramAddEval(const VP8LHistogram * const a,const VP8LHistogram * const b,VP8LHistogram * const out,double cost_threshold)441 static double HistogramAddEval(const VP8LHistogram* const a,
442                                const VP8LHistogram* const b,
443                                VP8LHistogram* const out,
444                                double cost_threshold) {
445   double cost = 0;
446   const double sum_cost = a->bit_cost_ + b->bit_cost_;
447   cost_threshold += sum_cost;
448 
449   if (GetCombinedHistogramEntropy(a, b, cost_threshold, &cost)) {
450     HistogramAdd(a, b, out);
451     out->bit_cost_ = cost;
452     out->palette_code_bits_ = a->palette_code_bits_;
453   }
454 
455   return cost - sum_cost;
456 }
457 
458 // Same as HistogramAddEval(), except that the resulting histogram
459 // is not stored. Only the cost C(a+b) - C(a) is evaluated. We omit
460 // the term C(b) which is constant over all the evaluations.
HistogramAddThresh(const VP8LHistogram * const a,const VP8LHistogram * const b,double cost_threshold)461 static double HistogramAddThresh(const VP8LHistogram* const a,
462                                  const VP8LHistogram* const b,
463                                  double cost_threshold) {
464   double cost;
465   assert(a != NULL && b != NULL);
466   cost = -a->bit_cost_;
467   GetCombinedHistogramEntropy(a, b, cost_threshold, &cost);
468   return cost;
469 }
470 
471 // -----------------------------------------------------------------------------
472 
473 // The structure to keep track of cost range for the three dominant entropy
474 // symbols.
475 // TODO(skal): Evaluate if float can be used here instead of double for
476 // representing the entropy costs.
477 typedef struct {
478   double literal_max_;
479   double literal_min_;
480   double red_max_;
481   double red_min_;
482   double blue_max_;
483   double blue_min_;
484 } DominantCostRange;
485 
DominantCostRangeInit(DominantCostRange * const c)486 static void DominantCostRangeInit(DominantCostRange* const c) {
487   c->literal_max_ = 0.;
488   c->literal_min_ = MAX_COST;
489   c->red_max_ = 0.;
490   c->red_min_ = MAX_COST;
491   c->blue_max_ = 0.;
492   c->blue_min_ = MAX_COST;
493 }
494 
UpdateDominantCostRange(const VP8LHistogram * const h,DominantCostRange * const c)495 static void UpdateDominantCostRange(
496     const VP8LHistogram* const h, DominantCostRange* const c) {
497   if (c->literal_max_ < h->literal_cost_) c->literal_max_ = h->literal_cost_;
498   if (c->literal_min_ > h->literal_cost_) c->literal_min_ = h->literal_cost_;
499   if (c->red_max_ < h->red_cost_) c->red_max_ = h->red_cost_;
500   if (c->red_min_ > h->red_cost_) c->red_min_ = h->red_cost_;
501   if (c->blue_max_ < h->blue_cost_) c->blue_max_ = h->blue_cost_;
502   if (c->blue_min_ > h->blue_cost_) c->blue_min_ = h->blue_cost_;
503 }
504 
UpdateHistogramCost(VP8LHistogram * const h)505 static void UpdateHistogramCost(VP8LHistogram* const h) {
506   uint32_t alpha_sym, red_sym, blue_sym;
507   const double alpha_cost =
508       PopulationCost(h->alpha_, NUM_LITERAL_CODES, &alpha_sym,
509                      &h->is_used_[3]);
510   const double distance_cost =
511       PopulationCost(h->distance_, NUM_DISTANCE_CODES, NULL, &h->is_used_[4]) +
512       VP8LExtraCost(h->distance_, NUM_DISTANCE_CODES);
513   const int num_codes = VP8LHistogramNumCodes(h->palette_code_bits_);
514   h->literal_cost_ =
515       PopulationCost(h->literal_, num_codes, NULL, &h->is_used_[0]) +
516           VP8LExtraCost(h->literal_ + NUM_LITERAL_CODES, NUM_LENGTH_CODES);
517   h->red_cost_ =
518       PopulationCost(h->red_, NUM_LITERAL_CODES, &red_sym, &h->is_used_[1]);
519   h->blue_cost_ =
520       PopulationCost(h->blue_, NUM_LITERAL_CODES, &blue_sym, &h->is_used_[2]);
521   h->bit_cost_ = h->literal_cost_ + h->red_cost_ + h->blue_cost_ +
522                  alpha_cost + distance_cost;
523   if ((alpha_sym | red_sym | blue_sym) == VP8L_NON_TRIVIAL_SYM) {
524     h->trivial_symbol_ = VP8L_NON_TRIVIAL_SYM;
525   } else {
526     h->trivial_symbol_ =
527         ((uint32_t)alpha_sym << 24) | (red_sym << 16) | (blue_sym << 0);
528   }
529 }
530 
GetBinIdForEntropy(double min,double max,double val)531 static int GetBinIdForEntropy(double min, double max, double val) {
532   const double range = max - min;
533   if (range > 0.) {
534     const double delta = val - min;
535     return (int)((NUM_PARTITIONS - 1e-6) * delta / range);
536   } else {
537     return 0;
538   }
539 }
540 
GetHistoBinIndex(const VP8LHistogram * const h,const DominantCostRange * const c,int low_effort)541 static int GetHistoBinIndex(const VP8LHistogram* const h,
542                             const DominantCostRange* const c, int low_effort) {
543   int bin_id = GetBinIdForEntropy(c->literal_min_, c->literal_max_,
544                                   h->literal_cost_);
545   assert(bin_id < NUM_PARTITIONS);
546   if (!low_effort) {
547     bin_id = bin_id * NUM_PARTITIONS
548            + GetBinIdForEntropy(c->red_min_, c->red_max_, h->red_cost_);
549     bin_id = bin_id * NUM_PARTITIONS
550            + GetBinIdForEntropy(c->blue_min_, c->blue_max_, h->blue_cost_);
551     assert(bin_id < BIN_SIZE);
552   }
553   return bin_id;
554 }
555 
556 // Construct the histograms from backward references.
HistogramBuild(int xsize,int histo_bits,const VP8LBackwardRefs * const backward_refs,VP8LHistogramSet * const image_histo)557 static void HistogramBuild(
558     int xsize, int histo_bits, const VP8LBackwardRefs* const backward_refs,
559     VP8LHistogramSet* const image_histo) {
560   int x = 0, y = 0;
561   const int histo_xsize = VP8LSubSampleSize(xsize, histo_bits);
562   VP8LHistogram** const histograms = image_histo->histograms;
563   VP8LRefsCursor c = VP8LRefsCursorInit(backward_refs);
564   assert(histo_bits > 0);
565   VP8LHistogramSetClear(image_histo);
566   while (VP8LRefsCursorOk(&c)) {
567     const PixOrCopy* const v = c.cur_pos;
568     const int ix = (y >> histo_bits) * histo_xsize + (x >> histo_bits);
569     VP8LHistogramAddSinglePixOrCopy(histograms[ix], v, NULL, 0);
570     x += PixOrCopyLength(v);
571     while (x >= xsize) {
572       x -= xsize;
573       ++y;
574     }
575     VP8LRefsCursorNext(&c);
576   }
577 }
578 
579 // Copies the histograms and computes its bit_cost.
580 static const uint16_t kInvalidHistogramSymbol = (uint16_t)(-1);
HistogramCopyAndAnalyze(VP8LHistogramSet * const orig_histo,VP8LHistogramSet * const image_histo,int * const num_used,uint16_t * const histogram_symbols)581 static void HistogramCopyAndAnalyze(VP8LHistogramSet* const orig_histo,
582                                     VP8LHistogramSet* const image_histo,
583                                     int* const num_used,
584                                     uint16_t* const histogram_symbols) {
585   int i, cluster_id;
586   int num_used_orig = *num_used;
587   VP8LHistogram** const orig_histograms = orig_histo->histograms;
588   VP8LHistogram** const histograms = image_histo->histograms;
589   assert(image_histo->max_size == orig_histo->max_size);
590   for (cluster_id = 0, i = 0; i < orig_histo->max_size; ++i) {
591     VP8LHistogram* const histo = orig_histograms[i];
592     UpdateHistogramCost(histo);
593 
594     // Skip the histogram if it is completely empty, which can happen for tiles
595     // with no information (when they are skipped because of LZ77).
596     if (!histo->is_used_[0] && !histo->is_used_[1] && !histo->is_used_[2]
597         && !histo->is_used_[3] && !histo->is_used_[4]) {
598       // The first histogram is always used. If an histogram is empty, we set
599       // its id to be the same as the previous one: this will improve
600       // compressibility for later LZ77.
601       assert(i > 0);
602       HistogramSetRemoveHistogram(image_histo, i, num_used);
603       HistogramSetRemoveHistogram(orig_histo, i, &num_used_orig);
604       histogram_symbols[i] = kInvalidHistogramSymbol;
605     } else {
606       // Copy histograms from orig_histo[] to image_histo[].
607       HistogramCopy(histo, histograms[i]);
608       histogram_symbols[i] = cluster_id++;
609       assert(cluster_id <= image_histo->max_size);
610     }
611   }
612 }
613 
614 // Partition histograms to different entropy bins for three dominant (literal,
615 // red and blue) symbol costs and compute the histogram aggregate bit_cost.
HistogramAnalyzeEntropyBin(VP8LHistogramSet * const image_histo,uint16_t * const bin_map,int low_effort)616 static void HistogramAnalyzeEntropyBin(VP8LHistogramSet* const image_histo,
617                                        uint16_t* const bin_map,
618                                        int low_effort) {
619   int i;
620   VP8LHistogram** const histograms = image_histo->histograms;
621   const int histo_size = image_histo->size;
622   DominantCostRange cost_range;
623   DominantCostRangeInit(&cost_range);
624 
625   // Analyze the dominant (literal, red and blue) entropy costs.
626   for (i = 0; i < histo_size; ++i) {
627     if (histograms[i] == NULL) continue;
628     UpdateDominantCostRange(histograms[i], &cost_range);
629   }
630 
631   // bin-hash histograms on three of the dominant (literal, red and blue)
632   // symbol costs and store the resulting bin_id for each histogram.
633   for (i = 0; i < histo_size; ++i) {
634     // bin_map[i] is not set to a special value as its use will later be guarded
635     // by another (histograms[i] == NULL).
636     if (histograms[i] == NULL) continue;
637     bin_map[i] = GetHistoBinIndex(histograms[i], &cost_range, low_effort);
638   }
639 }
640 
641 // Merges some histograms with same bin_id together if it's advantageous.
642 // Sets the remaining histograms to NULL.
HistogramCombineEntropyBin(VP8LHistogramSet * const image_histo,int * num_used,const uint16_t * const clusters,uint16_t * const cluster_mappings,VP8LHistogram * cur_combo,const uint16_t * const bin_map,int num_bins,double combine_cost_factor,int low_effort)643 static void HistogramCombineEntropyBin(VP8LHistogramSet* const image_histo,
644                                        int *num_used,
645                                        const uint16_t* const clusters,
646                                        uint16_t* const cluster_mappings,
647                                        VP8LHistogram* cur_combo,
648                                        const uint16_t* const bin_map,
649                                        int num_bins,
650                                        double combine_cost_factor,
651                                        int low_effort) {
652   VP8LHistogram** const histograms = image_histo->histograms;
653   int idx;
654   struct {
655     int16_t first;    // position of the histogram that accumulates all
656                       // histograms with the same bin_id
657     uint16_t num_combine_failures;   // number of combine failures per bin_id
658   } bin_info[BIN_SIZE];
659 
660   assert(num_bins <= BIN_SIZE);
661   for (idx = 0; idx < num_bins; ++idx) {
662     bin_info[idx].first = -1;
663     bin_info[idx].num_combine_failures = 0;
664   }
665 
666   // By default, a cluster matches itself.
667   for (idx = 0; idx < *num_used; ++idx) cluster_mappings[idx] = idx;
668   for (idx = 0; idx < image_histo->size; ++idx) {
669     int bin_id, first;
670     if (histograms[idx] == NULL) continue;
671     bin_id = bin_map[idx];
672     first = bin_info[bin_id].first;
673     if (first == -1) {
674       bin_info[bin_id].first = idx;
675     } else if (low_effort) {
676       HistogramAdd(histograms[idx], histograms[first], histograms[first]);
677       HistogramSetRemoveHistogram(image_histo, idx, num_used);
678       cluster_mappings[clusters[idx]] = clusters[first];
679     } else {
680       // try to merge #idx into #first (both share the same bin_id)
681       const double bit_cost = histograms[idx]->bit_cost_;
682       const double bit_cost_thresh = -bit_cost * combine_cost_factor;
683       const double curr_cost_diff =
684           HistogramAddEval(histograms[first], histograms[idx],
685                            cur_combo, bit_cost_thresh);
686       if (curr_cost_diff < bit_cost_thresh) {
687         // Try to merge two histograms only if the combo is a trivial one or
688         // the two candidate histograms are already non-trivial.
689         // For some images, 'try_combine' turns out to be false for a lot of
690         // histogram pairs. In that case, we fallback to combining
691         // histograms as usual to avoid increasing the header size.
692         const int try_combine =
693             (cur_combo->trivial_symbol_ != VP8L_NON_TRIVIAL_SYM) ||
694             ((histograms[idx]->trivial_symbol_ == VP8L_NON_TRIVIAL_SYM) &&
695              (histograms[first]->trivial_symbol_ == VP8L_NON_TRIVIAL_SYM));
696         const int max_combine_failures = 32;
697         if (try_combine ||
698             bin_info[bin_id].num_combine_failures >= max_combine_failures) {
699           // move the (better) merged histogram to its final slot
700           HistogramSwap(&cur_combo, &histograms[first]);
701           HistogramSetRemoveHistogram(image_histo, idx, num_used);
702           cluster_mappings[clusters[idx]] = clusters[first];
703         } else {
704           ++bin_info[bin_id].num_combine_failures;
705         }
706       }
707     }
708   }
709   if (low_effort) {
710     // for low_effort case, update the final cost when everything is merged
711     for (idx = 0; idx < image_histo->size; ++idx) {
712       if (histograms[idx] == NULL) continue;
713       UpdateHistogramCost(histograms[idx]);
714     }
715   }
716 }
717 
718 // Implement a Lehmer random number generator with a multiplicative constant of
719 // 48271 and a modulo constant of 2^31 - 1.
MyRand(uint32_t * const seed)720 static uint32_t MyRand(uint32_t* const seed) {
721   *seed = (uint32_t)(((uint64_t)(*seed) * 48271u) % 2147483647u);
722   assert(*seed > 0);
723   return *seed;
724 }
725 
726 // -----------------------------------------------------------------------------
727 // Histogram pairs priority queue
728 
729 // Pair of histograms. Negative idx1 value means that pair is out-of-date.
730 typedef struct {
731   int idx1;
732   int idx2;
733   double cost_diff;
734   double cost_combo;
735 } HistogramPair;
736 
737 typedef struct {
738   HistogramPair* queue;
739   int size;
740   int max_size;
741 } HistoQueue;
742 
HistoQueueInit(HistoQueue * const histo_queue,const int max_size)743 static int HistoQueueInit(HistoQueue* const histo_queue, const int max_size) {
744   histo_queue->size = 0;
745   histo_queue->max_size = max_size;
746   // We allocate max_size + 1 because the last element at index "size" is
747   // used as temporary data (and it could be up to max_size).
748   histo_queue->queue = (HistogramPair*)WebPSafeMalloc(
749       histo_queue->max_size + 1, sizeof(*histo_queue->queue));
750   return histo_queue->queue != NULL;
751 }
752 
HistoQueueClear(HistoQueue * const histo_queue)753 static void HistoQueueClear(HistoQueue* const histo_queue) {
754   assert(histo_queue != NULL);
755   WebPSafeFree(histo_queue->queue);
756   histo_queue->size = 0;
757   histo_queue->max_size = 0;
758 }
759 
760 // Pop a specific pair in the queue by replacing it with the last one
761 // and shrinking the queue.
HistoQueuePopPair(HistoQueue * const histo_queue,HistogramPair * const pair)762 static void HistoQueuePopPair(HistoQueue* const histo_queue,
763                               HistogramPair* const pair) {
764   assert(pair >= histo_queue->queue &&
765          pair < (histo_queue->queue + histo_queue->size));
766   assert(histo_queue->size > 0);
767   *pair = histo_queue->queue[histo_queue->size - 1];
768   --histo_queue->size;
769 }
770 
771 // Check whether a pair in the queue should be updated as head or not.
HistoQueueUpdateHead(HistoQueue * const histo_queue,HistogramPair * const pair)772 static void HistoQueueUpdateHead(HistoQueue* const histo_queue,
773                                  HistogramPair* const pair) {
774   assert(pair->cost_diff < 0.);
775   assert(pair >= histo_queue->queue &&
776          pair < (histo_queue->queue + histo_queue->size));
777   assert(histo_queue->size > 0);
778   if (pair->cost_diff < histo_queue->queue[0].cost_diff) {
779     // Replace the best pair.
780     const HistogramPair tmp = histo_queue->queue[0];
781     histo_queue->queue[0] = *pair;
782     *pair = tmp;
783   }
784 }
785 
786 // Update the cost diff and combo of a pair of histograms. This needs to be
787 // called when the the histograms have been merged with a third one.
HistoQueueUpdatePair(const VP8LHistogram * const h1,const VP8LHistogram * const h2,double threshold,HistogramPair * const pair)788 static void HistoQueueUpdatePair(const VP8LHistogram* const h1,
789                                  const VP8LHistogram* const h2,
790                                  double threshold,
791                                  HistogramPair* const pair) {
792   const double sum_cost = h1->bit_cost_ + h2->bit_cost_;
793   pair->cost_combo = 0.;
794   GetCombinedHistogramEntropy(h1, h2, sum_cost + threshold, &pair->cost_combo);
795   pair->cost_diff = pair->cost_combo - sum_cost;
796 }
797 
798 // Create a pair from indices "idx1" and "idx2" provided its cost
799 // is inferior to "threshold", a negative entropy.
800 // It returns the cost of the pair, or 0. if it superior to threshold.
HistoQueuePush(HistoQueue * const histo_queue,VP8LHistogram ** const histograms,int idx1,int idx2,double threshold)801 static double HistoQueuePush(HistoQueue* const histo_queue,
802                              VP8LHistogram** const histograms, int idx1,
803                              int idx2, double threshold) {
804   const VP8LHistogram* h1;
805   const VP8LHistogram* h2;
806   HistogramPair pair;
807 
808   // Stop here if the queue is full.
809   if (histo_queue->size == histo_queue->max_size) return 0.;
810   assert(threshold <= 0.);
811   if (idx1 > idx2) {
812     const int tmp = idx2;
813     idx2 = idx1;
814     idx1 = tmp;
815   }
816   pair.idx1 = idx1;
817   pair.idx2 = idx2;
818   h1 = histograms[idx1];
819   h2 = histograms[idx2];
820 
821   HistoQueueUpdatePair(h1, h2, threshold, &pair);
822 
823   // Do not even consider the pair if it does not improve the entropy.
824   if (pair.cost_diff >= threshold) return 0.;
825 
826   histo_queue->queue[histo_queue->size++] = pair;
827   HistoQueueUpdateHead(histo_queue, &histo_queue->queue[histo_queue->size - 1]);
828 
829   return pair.cost_diff;
830 }
831 
832 // -----------------------------------------------------------------------------
833 
834 // Combines histograms by continuously choosing the one with the highest cost
835 // reduction.
HistogramCombineGreedy(VP8LHistogramSet * const image_histo,int * const num_used)836 static int HistogramCombineGreedy(VP8LHistogramSet* const image_histo,
837                                   int* const num_used) {
838   int ok = 0;
839   const int image_histo_size = image_histo->size;
840   int i, j;
841   VP8LHistogram** const histograms = image_histo->histograms;
842   // Priority queue of histogram pairs.
843   HistoQueue histo_queue;
844 
845   // image_histo_size^2 for the queue size is safe. If you look at
846   // HistogramCombineGreedy, and imagine that UpdateQueueFront always pushes
847   // data to the queue, you insert at most:
848   // - image_histo_size*(image_histo_size-1)/2 (the first two for loops)
849   // - image_histo_size - 1 in the last for loop at the first iteration of
850   //   the while loop, image_histo_size - 2 at the second iteration ...
851   //   therefore image_histo_size*(image_histo_size-1)/2 overall too
852   if (!HistoQueueInit(&histo_queue, image_histo_size * image_histo_size)) {
853     goto End;
854   }
855 
856   for (i = 0; i < image_histo_size; ++i) {
857     if (image_histo->histograms[i] == NULL) continue;
858     for (j = i + 1; j < image_histo_size; ++j) {
859       // Initialize queue.
860       if (image_histo->histograms[j] == NULL) continue;
861       HistoQueuePush(&histo_queue, histograms, i, j, 0.);
862     }
863   }
864 
865   while (histo_queue.size > 0) {
866     const int idx1 = histo_queue.queue[0].idx1;
867     const int idx2 = histo_queue.queue[0].idx2;
868     HistogramAdd(histograms[idx2], histograms[idx1], histograms[idx1]);
869     histograms[idx1]->bit_cost_ = histo_queue.queue[0].cost_combo;
870 
871     // Remove merged histogram.
872     HistogramSetRemoveHistogram(image_histo, idx2, num_used);
873 
874     // Remove pairs intersecting the just combined best pair.
875     for (i = 0; i < histo_queue.size;) {
876       HistogramPair* const p = histo_queue.queue + i;
877       if (p->idx1 == idx1 || p->idx2 == idx1 ||
878           p->idx1 == idx2 || p->idx2 == idx2) {
879         HistoQueuePopPair(&histo_queue, p);
880       } else {
881         HistoQueueUpdateHead(&histo_queue, p);
882         ++i;
883       }
884     }
885 
886     // Push new pairs formed with combined histogram to the queue.
887     for (i = 0; i < image_histo->size; ++i) {
888       if (i == idx1 || image_histo->histograms[i] == NULL) continue;
889       HistoQueuePush(&histo_queue, image_histo->histograms, idx1, i, 0.);
890     }
891   }
892 
893   ok = 1;
894 
895  End:
896   HistoQueueClear(&histo_queue);
897   return ok;
898 }
899 
900 // Perform histogram aggregation using a stochastic approach.
901 // 'do_greedy' is set to 1 if a greedy approach needs to be performed
902 // afterwards, 0 otherwise.
PairComparison(const void * idx1,const void * idx2)903 static int PairComparison(const void* idx1, const void* idx2) {
904   // To be used with bsearch: <0 when *idx1<*idx2, >0 if >, 0 when ==.
905   return (*(int*) idx1 - *(int*) idx2);
906 }
HistogramCombineStochastic(VP8LHistogramSet * const image_histo,int * const num_used,int min_cluster_size,int * const do_greedy)907 static int HistogramCombineStochastic(VP8LHistogramSet* const image_histo,
908                                       int* const num_used, int min_cluster_size,
909                                       int* const do_greedy) {
910   int j, iter;
911   uint32_t seed = 1;
912   int tries_with_no_success = 0;
913   const int outer_iters = *num_used;
914   const int num_tries_no_success = outer_iters / 2;
915   VP8LHistogram** const histograms = image_histo->histograms;
916   // Priority queue of histogram pairs. Its size of 'kHistoQueueSize'
917   // impacts the quality of the compression and the speed: the smaller the
918   // faster but the worse for the compression.
919   HistoQueue histo_queue;
920   const int kHistoQueueSize = 9;
921   int ok = 0;
922   // mapping from an index in image_histo with no NULL histogram to the full
923   // blown image_histo.
924   int* mappings;
925 
926   if (*num_used < min_cluster_size) {
927     *do_greedy = 1;
928     return 1;
929   }
930 
931   mappings = (int*) WebPSafeMalloc(*num_used, sizeof(*mappings));
932   if (mappings == NULL) return 0;
933   if (!HistoQueueInit(&histo_queue, kHistoQueueSize)) goto End;
934   // Fill the initial mapping.
935   for (j = 0, iter = 0; iter < image_histo->size; ++iter) {
936     if (histograms[iter] == NULL) continue;
937     mappings[j++] = iter;
938   }
939   assert(j == *num_used);
940 
941   // Collapse similar histograms in 'image_histo'.
942   for (iter = 0;
943        iter < outer_iters && *num_used >= min_cluster_size &&
944            ++tries_with_no_success < num_tries_no_success;
945        ++iter) {
946     int* mapping_index;
947     double best_cost =
948         (histo_queue.size == 0) ? 0. : histo_queue.queue[0].cost_diff;
949     int best_idx1 = -1, best_idx2 = 1;
950     const uint32_t rand_range = (*num_used - 1) * (*num_used);
951     // (*num_used) / 2 was chosen empirically. Less means faster but worse
952     // compression.
953     const int num_tries = (*num_used) / 2;
954 
955     // Pick random samples.
956     for (j = 0; *num_used >= 2 && j < num_tries; ++j) {
957       double curr_cost;
958       // Choose two different histograms at random and try to combine them.
959       const uint32_t tmp = MyRand(&seed) % rand_range;
960       uint32_t idx1 = tmp / (*num_used - 1);
961       uint32_t idx2 = tmp % (*num_used - 1);
962       if (idx2 >= idx1) ++idx2;
963       idx1 = mappings[idx1];
964       idx2 = mappings[idx2];
965 
966       // Calculate cost reduction on combination.
967       curr_cost =
968           HistoQueuePush(&histo_queue, histograms, idx1, idx2, best_cost);
969       if (curr_cost < 0) {  // found a better pair?
970         best_cost = curr_cost;
971         // Empty the queue if we reached full capacity.
972         if (histo_queue.size == histo_queue.max_size) break;
973       }
974     }
975     if (histo_queue.size == 0) continue;
976 
977     // Get the best histograms.
978     best_idx1 = histo_queue.queue[0].idx1;
979     best_idx2 = histo_queue.queue[0].idx2;
980     assert(best_idx1 < best_idx2);
981     // Pop best_idx2 from mappings.
982     mapping_index = (int*) bsearch(&best_idx2, mappings, *num_used,
983                                    sizeof(best_idx2), &PairComparison);
984     assert(mapping_index != NULL);
985     memmove(mapping_index, mapping_index + 1, sizeof(*mapping_index) *
986         ((*num_used) - (mapping_index - mappings) - 1));
987     // Merge the histograms and remove best_idx2 from the queue.
988     HistogramAdd(histograms[best_idx2], histograms[best_idx1],
989                  histograms[best_idx1]);
990     histograms[best_idx1]->bit_cost_ = histo_queue.queue[0].cost_combo;
991     HistogramSetRemoveHistogram(image_histo, best_idx2, num_used);
992     // Parse the queue and update each pair that deals with best_idx1,
993     // best_idx2 or image_histo_size.
994     for (j = 0; j < histo_queue.size;) {
995       HistogramPair* const p = histo_queue.queue + j;
996       const int is_idx1_best = p->idx1 == best_idx1 || p->idx1 == best_idx2;
997       const int is_idx2_best = p->idx2 == best_idx1 || p->idx2 == best_idx2;
998       int do_eval = 0;
999       // The front pair could have been duplicated by a random pick so
1000       // check for it all the time nevertheless.
1001       if (is_idx1_best && is_idx2_best) {
1002         HistoQueuePopPair(&histo_queue, p);
1003         continue;
1004       }
1005       // Any pair containing one of the two best indices should only refer to
1006       // best_idx1. Its cost should also be updated.
1007       if (is_idx1_best) {
1008         p->idx1 = best_idx1;
1009         do_eval = 1;
1010       } else if (is_idx2_best) {
1011         p->idx2 = best_idx1;
1012         do_eval = 1;
1013       }
1014       // Make sure the index order is respected.
1015       if (p->idx1 > p->idx2) {
1016         const int tmp = p->idx2;
1017         p->idx2 = p->idx1;
1018         p->idx1 = tmp;
1019       }
1020       if (do_eval) {
1021         // Re-evaluate the cost of an updated pair.
1022         HistoQueueUpdatePair(histograms[p->idx1], histograms[p->idx2], 0., p);
1023         if (p->cost_diff >= 0.) {
1024           HistoQueuePopPair(&histo_queue, p);
1025           continue;
1026         }
1027       }
1028       HistoQueueUpdateHead(&histo_queue, p);
1029       ++j;
1030     }
1031     tries_with_no_success = 0;
1032   }
1033   *do_greedy = (*num_used <= min_cluster_size);
1034   ok = 1;
1035 
1036 End:
1037   HistoQueueClear(&histo_queue);
1038   WebPSafeFree(mappings);
1039   return ok;
1040 }
1041 
1042 // -----------------------------------------------------------------------------
1043 // Histogram refinement
1044 
1045 // Find the best 'out' histogram for each of the 'in' histograms.
1046 // At call-time, 'out' contains the histograms of the clusters.
1047 // Note: we assume that out[]->bit_cost_ is already up-to-date.
HistogramRemap(const VP8LHistogramSet * const in,VP8LHistogramSet * const out,uint16_t * const symbols)1048 static void HistogramRemap(const VP8LHistogramSet* const in,
1049                            VP8LHistogramSet* const out,
1050                            uint16_t* const symbols) {
1051   int i;
1052   VP8LHistogram** const in_histo = in->histograms;
1053   VP8LHistogram** const out_histo = out->histograms;
1054   const int in_size = out->max_size;
1055   const int out_size = out->size;
1056   if (out_size > 1) {
1057     for (i = 0; i < in_size; ++i) {
1058       int best_out = 0;
1059       double best_bits = MAX_COST;
1060       int k;
1061       if (in_histo[i] == NULL) {
1062         // Arbitrarily set to the previous value if unused to help future LZ77.
1063         symbols[i] = symbols[i - 1];
1064         continue;
1065       }
1066       for (k = 0; k < out_size; ++k) {
1067         double cur_bits;
1068         cur_bits = HistogramAddThresh(out_histo[k], in_histo[i], best_bits);
1069         if (k == 0 || cur_bits < best_bits) {
1070           best_bits = cur_bits;
1071           best_out = k;
1072         }
1073       }
1074       symbols[i] = best_out;
1075     }
1076   } else {
1077     assert(out_size == 1);
1078     for (i = 0; i < in_size; ++i) {
1079       symbols[i] = 0;
1080     }
1081   }
1082 
1083   // Recompute each out based on raw and symbols.
1084   VP8LHistogramSetClear(out);
1085   out->size = out_size;
1086 
1087   for (i = 0; i < in_size; ++i) {
1088     int idx;
1089     if (in_histo[i] == NULL) continue;
1090     idx = symbols[i];
1091     HistogramAdd(in_histo[i], out_histo[idx], out_histo[idx]);
1092   }
1093 }
1094 
GetCombineCostFactor(int histo_size,int quality)1095 static double GetCombineCostFactor(int histo_size, int quality) {
1096   double combine_cost_factor = 0.16;
1097   if (quality < 90) {
1098     if (histo_size > 256) combine_cost_factor /= 2.;
1099     if (histo_size > 512) combine_cost_factor /= 2.;
1100     if (histo_size > 1024) combine_cost_factor /= 2.;
1101     if (quality <= 50) combine_cost_factor /= 2.;
1102   }
1103   return combine_cost_factor;
1104 }
1105 
1106 // Given a HistogramSet 'set', the mapping of clusters 'cluster_mapping' and the
1107 // current assignment of the cells in 'symbols', merge the clusters and
1108 // assign the smallest possible clusters values.
OptimizeHistogramSymbols(const VP8LHistogramSet * const set,uint16_t * const cluster_mappings,int num_clusters,uint16_t * const cluster_mappings_tmp,uint16_t * const symbols)1109 static void OptimizeHistogramSymbols(const VP8LHistogramSet* const set,
1110                                      uint16_t* const cluster_mappings,
1111                                      int num_clusters,
1112                                      uint16_t* const cluster_mappings_tmp,
1113                                      uint16_t* const symbols) {
1114   int i, cluster_max;
1115   int do_continue = 1;
1116   // First, assign the lowest cluster to each pixel.
1117   while (do_continue) {
1118     do_continue = 0;
1119     for (i = 0; i < num_clusters; ++i) {
1120       int k;
1121       k = cluster_mappings[i];
1122       while (k != cluster_mappings[k]) {
1123         cluster_mappings[k] = cluster_mappings[cluster_mappings[k]];
1124         k = cluster_mappings[k];
1125       }
1126       if (k != cluster_mappings[i]) {
1127         do_continue = 1;
1128         cluster_mappings[i] = k;
1129       }
1130     }
1131   }
1132   // Create a mapping from a cluster id to its minimal version.
1133   cluster_max = 0;
1134   memset(cluster_mappings_tmp, 0,
1135          set->max_size * sizeof(*cluster_mappings_tmp));
1136   assert(cluster_mappings[0] == 0);
1137   // Re-map the ids.
1138   for (i = 0; i < set->max_size; ++i) {
1139     int cluster;
1140     if (symbols[i] == kInvalidHistogramSymbol) continue;
1141     cluster = cluster_mappings[symbols[i]];
1142     assert(symbols[i] < num_clusters);
1143     if (cluster > 0 && cluster_mappings_tmp[cluster] == 0) {
1144       ++cluster_max;
1145       cluster_mappings_tmp[cluster] = cluster_max;
1146     }
1147     symbols[i] = cluster_mappings_tmp[cluster];
1148   }
1149 
1150   // Make sure all cluster values are used.
1151   cluster_max = 0;
1152   for (i = 0; i < set->max_size; ++i) {
1153     if (symbols[i] == kInvalidHistogramSymbol) continue;
1154     if (symbols[i] <= cluster_max) continue;
1155     ++cluster_max;
1156     assert(symbols[i] == cluster_max);
1157   }
1158 }
1159 
RemoveEmptyHistograms(VP8LHistogramSet * const image_histo)1160 static void RemoveEmptyHistograms(VP8LHistogramSet* const image_histo) {
1161   uint32_t size;
1162   int i;
1163   for (i = 0, size = 0; i < image_histo->size; ++i) {
1164     if (image_histo->histograms[i] == NULL) continue;
1165     image_histo->histograms[size++] = image_histo->histograms[i];
1166   }
1167   image_histo->size = size;
1168 }
1169 
VP8LGetHistoImageSymbols(int xsize,int ysize,const VP8LBackwardRefs * const refs,int quality,int low_effort,int histo_bits,int cache_bits,VP8LHistogramSet * const image_histo,VP8LHistogram * const tmp_histo,uint16_t * const histogram_symbols)1170 int VP8LGetHistoImageSymbols(int xsize, int ysize,
1171                              const VP8LBackwardRefs* const refs,
1172                              int quality, int low_effort,
1173                              int histo_bits, int cache_bits,
1174                              VP8LHistogramSet* const image_histo,
1175                              VP8LHistogram* const tmp_histo,
1176                              uint16_t* const histogram_symbols) {
1177   int ok = 0;
1178   const int histo_xsize = histo_bits ? VP8LSubSampleSize(xsize, histo_bits) : 1;
1179   const int histo_ysize = histo_bits ? VP8LSubSampleSize(ysize, histo_bits) : 1;
1180   const int image_histo_raw_size = histo_xsize * histo_ysize;
1181   VP8LHistogramSet* const orig_histo =
1182       VP8LAllocateHistogramSet(image_histo_raw_size, cache_bits);
1183   // Don't attempt linear bin-partition heuristic for
1184   // histograms of small sizes (as bin_map will be very sparse) and
1185   // maximum quality q==100 (to preserve the compression gains at that level).
1186   const int entropy_combine_num_bins = low_effort ? NUM_PARTITIONS : BIN_SIZE;
1187   int entropy_combine;
1188   uint16_t* const map_tmp =
1189       WebPSafeMalloc(2 * image_histo_raw_size, sizeof(map_tmp));
1190   uint16_t* const cluster_mappings = map_tmp + image_histo_raw_size;
1191   int num_used = image_histo_raw_size;
1192   if (orig_histo == NULL || map_tmp == NULL) goto Error;
1193 
1194   // Construct the histograms from backward references.
1195   HistogramBuild(xsize, histo_bits, refs, orig_histo);
1196   // Copies the histograms and computes its bit_cost.
1197   // histogram_symbols is optimized
1198   HistogramCopyAndAnalyze(orig_histo, image_histo, &num_used,
1199                           histogram_symbols);
1200 
1201   entropy_combine =
1202       (num_used > entropy_combine_num_bins * 2) && (quality < 100);
1203 
1204   if (entropy_combine) {
1205     uint16_t* const bin_map = map_tmp;
1206     const double combine_cost_factor =
1207         GetCombineCostFactor(image_histo_raw_size, quality);
1208     const uint32_t num_clusters = num_used;
1209 
1210     HistogramAnalyzeEntropyBin(image_histo, bin_map, low_effort);
1211     // Collapse histograms with similar entropy.
1212     HistogramCombineEntropyBin(image_histo, &num_used, histogram_symbols,
1213                                cluster_mappings, tmp_histo, bin_map,
1214                                entropy_combine_num_bins, combine_cost_factor,
1215                                low_effort);
1216     OptimizeHistogramSymbols(image_histo, cluster_mappings, num_clusters,
1217                              map_tmp, histogram_symbols);
1218   }
1219 
1220   // Don't combine the histograms using stochastic and greedy heuristics for
1221   // low-effort compression mode.
1222   if (!low_effort || !entropy_combine) {
1223     const float x = quality / 100.f;
1224     // cubic ramp between 1 and MAX_HISTO_GREEDY:
1225     const int threshold_size = (int)(1 + (x * x * x) * (MAX_HISTO_GREEDY - 1));
1226     int do_greedy;
1227     if (!HistogramCombineStochastic(image_histo, &num_used, threshold_size,
1228                                     &do_greedy)) {
1229       goto Error;
1230     }
1231     if (do_greedy) {
1232       RemoveEmptyHistograms(image_histo);
1233       if (!HistogramCombineGreedy(image_histo, &num_used)) {
1234         goto Error;
1235       }
1236     }
1237   }
1238 
1239   // Find the optimal map from original histograms to the final ones.
1240   RemoveEmptyHistograms(image_histo);
1241   HistogramRemap(orig_histo, image_histo, histogram_symbols);
1242 
1243   ok = 1;
1244 
1245  Error:
1246   VP8LFreeHistogramSet(orig_histo);
1247   WebPSafeFree(map_tmp);
1248   return ok;
1249 }
1250