• 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 // main entry for the lossless encoder.
11 //
12 // Author: Vikas Arora (vikaas.arora@gmail.com)
13 //
14 
15 #include <assert.h>
16 #include <stdlib.h>
17 
18 #include "src/enc/backward_references_enc.h"
19 #include "src/enc/histogram_enc.h"
20 #include "src/enc/vp8i_enc.h"
21 #include "src/enc/vp8li_enc.h"
22 #include "src/dsp/lossless.h"
23 #include "src/dsp/lossless_common.h"
24 #include "src/utils/bit_writer_utils.h"
25 #include "src/utils/huffman_encode_utils.h"
26 #include "src/utils/utils.h"
27 #include "src/webp/format_constants.h"
28 
29 // Maximum number of histogram images (sub-blocks).
30 #define MAX_HUFF_IMAGE_SIZE       2600
31 
32 // Palette reordering for smaller sum of deltas (and for smaller storage).
33 
PaletteCompareColorsForQsort(const void * p1,const void * p2)34 static int PaletteCompareColorsForQsort(const void* p1, const void* p2) {
35   const uint32_t a = WebPMemToUint32((uint8_t*)p1);
36   const uint32_t b = WebPMemToUint32((uint8_t*)p2);
37   assert(a != b);
38   return (a < b) ? -1 : 1;
39 }
40 
PaletteComponentDistance(uint32_t v)41 static WEBP_INLINE uint32_t PaletteComponentDistance(uint32_t v) {
42   return (v <= 128) ? v : (256 - v);
43 }
44 
45 // Computes a value that is related to the entropy created by the
46 // palette entry diff.
47 //
48 // Note that the last & 0xff is a no-operation in the next statement, but
49 // removed by most compilers and is here only for regularity of the code.
PaletteColorDistance(uint32_t col1,uint32_t col2)50 static WEBP_INLINE uint32_t PaletteColorDistance(uint32_t col1, uint32_t col2) {
51   const uint32_t diff = VP8LSubPixels(col1, col2);
52   const int kMoreWeightForRGBThanForAlpha = 9;
53   uint32_t score;
54   score =  PaletteComponentDistance((diff >>  0) & 0xff);
55   score += PaletteComponentDistance((diff >>  8) & 0xff);
56   score += PaletteComponentDistance((diff >> 16) & 0xff);
57   score *= kMoreWeightForRGBThanForAlpha;
58   score += PaletteComponentDistance((diff >> 24) & 0xff);
59   return score;
60 }
61 
SwapColor(uint32_t * const col1,uint32_t * const col2)62 static WEBP_INLINE void SwapColor(uint32_t* const col1, uint32_t* const col2) {
63   const uint32_t tmp = *col1;
64   *col1 = *col2;
65   *col2 = tmp;
66 }
67 
GreedyMinimizeDeltas(uint32_t palette[],int num_colors)68 static void GreedyMinimizeDeltas(uint32_t palette[], int num_colors) {
69   // Find greedily always the closest color of the predicted color to minimize
70   // deltas in the palette. This reduces storage needs since the
71   // palette is stored with delta encoding.
72   uint32_t predict = 0x00000000;
73   int i, k;
74   for (i = 0; i < num_colors; ++i) {
75     int best_ix = i;
76     uint32_t best_score = ~0U;
77     for (k = i; k < num_colors; ++k) {
78       const uint32_t cur_score = PaletteColorDistance(palette[k], predict);
79       if (best_score > cur_score) {
80         best_score = cur_score;
81         best_ix = k;
82       }
83     }
84     SwapColor(&palette[best_ix], &palette[i]);
85     predict = palette[i];
86   }
87 }
88 
89 // The palette has been sorted by alpha. This function checks if the other
90 // components of the palette have a monotonic development with regards to
91 // position in the palette. If all have monotonic development, there is
92 // no benefit to re-organize them greedily. A monotonic development
93 // would be spotted in green-only situations (like lossy alpha) or gray-scale
94 // images.
PaletteHasNonMonotonousDeltas(uint32_t palette[],int num_colors)95 static int PaletteHasNonMonotonousDeltas(uint32_t palette[], int num_colors) {
96   uint32_t predict = 0x000000;
97   int i;
98   uint8_t sign_found = 0x00;
99   for (i = 0; i < num_colors; ++i) {
100     const uint32_t diff = VP8LSubPixels(palette[i], predict);
101     const uint8_t rd = (diff >> 16) & 0xff;
102     const uint8_t gd = (diff >>  8) & 0xff;
103     const uint8_t bd = (diff >>  0) & 0xff;
104     if (rd != 0x00) {
105       sign_found |= (rd < 0x80) ? 1 : 2;
106     }
107     if (gd != 0x00) {
108       sign_found |= (gd < 0x80) ? 8 : 16;
109     }
110     if (bd != 0x00) {
111       sign_found |= (bd < 0x80) ? 64 : 128;
112     }
113     predict = palette[i];
114   }
115   return (sign_found & (sign_found << 1)) != 0;  // two consequent signs.
116 }
117 
118 // -----------------------------------------------------------------------------
119 // Palette
120 
121 // If number of colors in the image is less than or equal to MAX_PALETTE_SIZE,
122 // creates a palette and returns true, else returns false.
AnalyzeAndCreatePalette(const WebPPicture * const pic,int low_effort,uint32_t palette[MAX_PALETTE_SIZE],int * const palette_size)123 static int AnalyzeAndCreatePalette(const WebPPicture* const pic,
124                                    int low_effort,
125                                    uint32_t palette[MAX_PALETTE_SIZE],
126                                    int* const palette_size) {
127   const int num_colors = WebPGetColorPalette(pic, palette);
128   if (num_colors > MAX_PALETTE_SIZE) {
129     *palette_size = 0;
130     return 0;
131   }
132   *palette_size = num_colors;
133   qsort(palette, num_colors, sizeof(*palette), PaletteCompareColorsForQsort);
134   if (!low_effort && PaletteHasNonMonotonousDeltas(palette, num_colors)) {
135     GreedyMinimizeDeltas(palette, num_colors);
136   }
137   return 1;
138 }
139 
140 // These five modes are evaluated and their respective entropy is computed.
141 typedef enum {
142   kDirect = 0,
143   kSpatial = 1,
144   kSubGreen = 2,
145   kSpatialSubGreen = 3,
146   kPalette = 4,
147   kPaletteAndSpatial = 5,
148   kNumEntropyIx = 6
149 } EntropyIx;
150 
151 typedef enum {
152   kHistoAlpha = 0,
153   kHistoAlphaPred,
154   kHistoGreen,
155   kHistoGreenPred,
156   kHistoRed,
157   kHistoRedPred,
158   kHistoBlue,
159   kHistoBluePred,
160   kHistoRedSubGreen,
161   kHistoRedPredSubGreen,
162   kHistoBlueSubGreen,
163   kHistoBluePredSubGreen,
164   kHistoPalette,
165   kHistoTotal  // Must be last.
166 } HistoIx;
167 
AddSingleSubGreen(int p,uint32_t * const r,uint32_t * const b)168 static void AddSingleSubGreen(int p, uint32_t* const r, uint32_t* const b) {
169   const int green = p >> 8;  // The upper bits are masked away later.
170   ++r[((p >> 16) - green) & 0xff];
171   ++b[((p >>  0) - green) & 0xff];
172 }
173 
AddSingle(uint32_t p,uint32_t * const a,uint32_t * const r,uint32_t * const g,uint32_t * const b)174 static void AddSingle(uint32_t p,
175                       uint32_t* const a, uint32_t* const r,
176                       uint32_t* const g, uint32_t* const b) {
177   ++a[(p >> 24) & 0xff];
178   ++r[(p >> 16) & 0xff];
179   ++g[(p >>  8) & 0xff];
180   ++b[(p >>  0) & 0xff];
181 }
182 
HashPix(uint32_t pix)183 static WEBP_INLINE uint32_t HashPix(uint32_t pix) {
184   // Note that masking with 0xffffffffu is for preventing an
185   // 'unsigned int overflow' warning. Doesn't impact the compiled code.
186   return ((((uint64_t)pix + (pix >> 19)) * 0x39c5fba7ull) & 0xffffffffu) >> 24;
187 }
188 
AnalyzeEntropy(const uint32_t * argb,int width,int height,int argb_stride,int use_palette,int palette_size,int transform_bits,EntropyIx * const min_entropy_ix,int * const red_and_blue_always_zero)189 static int AnalyzeEntropy(const uint32_t* argb,
190                           int width, int height, int argb_stride,
191                           int use_palette,
192                           int palette_size, int transform_bits,
193                           EntropyIx* const min_entropy_ix,
194                           int* const red_and_blue_always_zero) {
195   // Allocate histogram set with cache_bits = 0.
196   uint32_t* histo;
197 
198   if (use_palette && palette_size <= 16) {
199     // In the case of small palettes, we pack 2, 4 or 8 pixels together. In
200     // practice, small palettes are better than any other transform.
201     *min_entropy_ix = kPalette;
202     *red_and_blue_always_zero = 1;
203     return 1;
204   }
205   histo = (uint32_t*)WebPSafeCalloc(kHistoTotal, sizeof(*histo) * 256);
206   if (histo != NULL) {
207     int i, x, y;
208     const uint32_t* prev_row = NULL;
209     const uint32_t* curr_row = argb;
210     uint32_t pix_prev = argb[0];  // Skip the first pixel.
211     for (y = 0; y < height; ++y) {
212       for (x = 0; x < width; ++x) {
213         const uint32_t pix = curr_row[x];
214         const uint32_t pix_diff = VP8LSubPixels(pix, pix_prev);
215         pix_prev = pix;
216         if ((pix_diff == 0) || (prev_row != NULL && pix == prev_row[x])) {
217           continue;
218         }
219         AddSingle(pix,
220                   &histo[kHistoAlpha * 256],
221                   &histo[kHistoRed * 256],
222                   &histo[kHistoGreen * 256],
223                   &histo[kHistoBlue * 256]);
224         AddSingle(pix_diff,
225                   &histo[kHistoAlphaPred * 256],
226                   &histo[kHistoRedPred * 256],
227                   &histo[kHistoGreenPred * 256],
228                   &histo[kHistoBluePred * 256]);
229         AddSingleSubGreen(pix,
230                           &histo[kHistoRedSubGreen * 256],
231                           &histo[kHistoBlueSubGreen * 256]);
232         AddSingleSubGreen(pix_diff,
233                           &histo[kHistoRedPredSubGreen * 256],
234                           &histo[kHistoBluePredSubGreen * 256]);
235         {
236           // Approximate the palette by the entropy of the multiplicative hash.
237           const uint32_t hash = HashPix(pix);
238           ++histo[kHistoPalette * 256 + hash];
239         }
240       }
241       prev_row = curr_row;
242       curr_row += argb_stride;
243     }
244     {
245       double entropy_comp[kHistoTotal];
246       double entropy[kNumEntropyIx];
247       int k;
248       int last_mode_to_analyze = use_palette ? kPalette : kSpatialSubGreen;
249       int j;
250       // Let's add one zero to the predicted histograms. The zeros are removed
251       // too efficiently by the pix_diff == 0 comparison, at least one of the
252       // zeros is likely to exist.
253       ++histo[kHistoRedPredSubGreen * 256];
254       ++histo[kHistoBluePredSubGreen * 256];
255       ++histo[kHistoRedPred * 256];
256       ++histo[kHistoGreenPred * 256];
257       ++histo[kHistoBluePred * 256];
258       ++histo[kHistoAlphaPred * 256];
259 
260       for (j = 0; j < kHistoTotal; ++j) {
261         entropy_comp[j] = VP8LBitsEntropy(&histo[j * 256], 256);
262       }
263       entropy[kDirect] = entropy_comp[kHistoAlpha] +
264           entropy_comp[kHistoRed] +
265           entropy_comp[kHistoGreen] +
266           entropy_comp[kHistoBlue];
267       entropy[kSpatial] = entropy_comp[kHistoAlphaPred] +
268           entropy_comp[kHistoRedPred] +
269           entropy_comp[kHistoGreenPred] +
270           entropy_comp[kHistoBluePred];
271       entropy[kSubGreen] = entropy_comp[kHistoAlpha] +
272           entropy_comp[kHistoRedSubGreen] +
273           entropy_comp[kHistoGreen] +
274           entropy_comp[kHistoBlueSubGreen];
275       entropy[kSpatialSubGreen] = entropy_comp[kHistoAlphaPred] +
276           entropy_comp[kHistoRedPredSubGreen] +
277           entropy_comp[kHistoGreenPred] +
278           entropy_comp[kHistoBluePredSubGreen];
279       entropy[kPalette] = entropy_comp[kHistoPalette];
280 
281       // When including transforms, there is an overhead in bits from
282       // storing them. This overhead is small but matters for small images.
283       // For spatial, there are 14 transformations.
284       entropy[kSpatial] += VP8LSubSampleSize(width, transform_bits) *
285                            VP8LSubSampleSize(height, transform_bits) *
286                            VP8LFastLog2(14);
287       // For color transforms: 24 as only 3 channels are considered in a
288       // ColorTransformElement.
289       entropy[kSpatialSubGreen] += VP8LSubSampleSize(width, transform_bits) *
290                                    VP8LSubSampleSize(height, transform_bits) *
291                                    VP8LFastLog2(24);
292       // For palettes, add the cost of storing the palette.
293       // We empirically estimate the cost of a compressed entry as 8 bits.
294       // The palette is differential-coded when compressed hence a much
295       // lower cost than sizeof(uint32_t)*8.
296       entropy[kPalette] += palette_size * 8;
297 
298       *min_entropy_ix = kDirect;
299       for (k = kDirect + 1; k <= last_mode_to_analyze; ++k) {
300         if (entropy[*min_entropy_ix] > entropy[k]) {
301           *min_entropy_ix = (EntropyIx)k;
302         }
303       }
304       assert((int)*min_entropy_ix <= last_mode_to_analyze);
305       *red_and_blue_always_zero = 1;
306       // Let's check if the histogram of the chosen entropy mode has
307       // non-zero red and blue values. If all are zero, we can later skip
308       // the cross color optimization.
309       {
310         static const uint8_t kHistoPairs[5][2] = {
311           { kHistoRed, kHistoBlue },
312           { kHistoRedPred, kHistoBluePred },
313           { kHistoRedSubGreen, kHistoBlueSubGreen },
314           { kHistoRedPredSubGreen, kHistoBluePredSubGreen },
315           { kHistoRed, kHistoBlue }
316         };
317         const uint32_t* const red_histo =
318             &histo[256 * kHistoPairs[*min_entropy_ix][0]];
319         const uint32_t* const blue_histo =
320             &histo[256 * kHistoPairs[*min_entropy_ix][1]];
321         for (i = 1; i < 256; ++i) {
322           if ((red_histo[i] | blue_histo[i]) != 0) {
323             *red_and_blue_always_zero = 0;
324             break;
325           }
326         }
327       }
328     }
329     WebPSafeFree(histo);
330     return 1;
331   } else {
332     return 0;
333   }
334 }
335 
GetHistoBits(int method,int use_palette,int width,int height)336 static int GetHistoBits(int method, int use_palette, int width, int height) {
337   // Make tile size a function of encoding method (Range: 0 to 6).
338   int histo_bits = (use_palette ? 9 : 7) - method;
339   while (1) {
340     const int huff_image_size = VP8LSubSampleSize(width, histo_bits) *
341                                 VP8LSubSampleSize(height, histo_bits);
342     if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break;
343     ++histo_bits;
344   }
345   return (histo_bits < MIN_HUFFMAN_BITS) ? MIN_HUFFMAN_BITS :
346          (histo_bits > MAX_HUFFMAN_BITS) ? MAX_HUFFMAN_BITS : histo_bits;
347 }
348 
GetTransformBits(int method,int histo_bits)349 static int GetTransformBits(int method, int histo_bits) {
350   const int max_transform_bits = (method < 4) ? 6 : (method > 4) ? 4 : 5;
351   const int res =
352       (histo_bits > max_transform_bits) ? max_transform_bits : histo_bits;
353   assert(res <= MAX_TRANSFORM_BITS);
354   return res;
355 }
356 
357 // Set of parameters to be used in each iteration of the cruncher.
358 #define CRUNCH_SUBCONFIGS_MAX 2
359 typedef struct {
360   int lz77_;
361   int do_no_cache_;
362 } CrunchSubConfig;
363 typedef struct {
364   int entropy_idx_;
365   CrunchSubConfig sub_configs_[CRUNCH_SUBCONFIGS_MAX];
366   int sub_configs_size_;
367 } CrunchConfig;
368 
369 #define CRUNCH_CONFIGS_MAX kNumEntropyIx
370 
EncoderAnalyze(VP8LEncoder * const enc,CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX],int * const crunch_configs_size,int * const red_and_blue_always_zero)371 static int EncoderAnalyze(VP8LEncoder* const enc,
372                           CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX],
373                           int* const crunch_configs_size,
374                           int* const red_and_blue_always_zero) {
375   const WebPPicture* const pic = enc->pic_;
376   const int width = pic->width;
377   const int height = pic->height;
378   const WebPConfig* const config = enc->config_;
379   const int method = config->method;
380   const int low_effort = (config->method == 0);
381   int i;
382   int use_palette;
383   int n_lz77s;
384   // If set to 0, analyze the cache with the computed cache value. If 1, also
385   // analyze with no-cache.
386   int do_no_cache = 0;
387   assert(pic != NULL && pic->argb != NULL);
388 
389   use_palette =
390       AnalyzeAndCreatePalette(pic, low_effort,
391                               enc->palette_, &enc->palette_size_);
392 
393   // Empirical bit sizes.
394   enc->histo_bits_ = GetHistoBits(method, use_palette,
395                                   pic->width, pic->height);
396   enc->transform_bits_ = GetTransformBits(method, enc->histo_bits_);
397 
398   if (low_effort) {
399     // AnalyzeEntropy is somewhat slow.
400     crunch_configs[0].entropy_idx_ = use_palette ? kPalette : kSpatialSubGreen;
401     n_lz77s = 1;
402     *crunch_configs_size = 1;
403   } else {
404     EntropyIx min_entropy_ix;
405     // Try out multiple LZ77 on images with few colors.
406     n_lz77s = (enc->palette_size_ > 0 && enc->palette_size_ <= 16) ? 2 : 1;
407     if (!AnalyzeEntropy(pic->argb, width, height, pic->argb_stride, use_palette,
408                         enc->palette_size_, enc->transform_bits_,
409                         &min_entropy_ix, red_and_blue_always_zero)) {
410       return 0;
411     }
412     if (method == 6 && config->quality == 100) {
413       do_no_cache = 1;
414       // Go brute force on all transforms.
415       *crunch_configs_size = 0;
416       for (i = 0; i < kNumEntropyIx; ++i) {
417         // We can only apply kPalette or kPaletteAndSpatial if we can indeed use
418         // a palette.
419         if ((i != kPalette && i != kPaletteAndSpatial) || use_palette) {
420           assert(*crunch_configs_size < CRUNCH_CONFIGS_MAX);
421           crunch_configs[(*crunch_configs_size)++].entropy_idx_ = i;
422         }
423       }
424     } else {
425       // Only choose the guessed best transform.
426       *crunch_configs_size = 1;
427       crunch_configs[0].entropy_idx_ = min_entropy_ix;
428       if (config->quality >= 75 && method == 5) {
429         // Test with and without color cache.
430         do_no_cache = 1;
431         // If we have a palette, also check in combination with spatial.
432         if (min_entropy_ix == kPalette) {
433           *crunch_configs_size = 2;
434           crunch_configs[1].entropy_idx_ = kPaletteAndSpatial;
435         }
436       }
437     }
438   }
439   // Fill in the different LZ77s.
440   assert(n_lz77s <= CRUNCH_SUBCONFIGS_MAX);
441   for (i = 0; i < *crunch_configs_size; ++i) {
442     int j;
443     for (j = 0; j < n_lz77s; ++j) {
444       assert(j < CRUNCH_SUBCONFIGS_MAX);
445       crunch_configs[i].sub_configs_[j].lz77_ =
446           (j == 0) ? kLZ77Standard | kLZ77RLE : kLZ77Box;
447       crunch_configs[i].sub_configs_[j].do_no_cache_ = do_no_cache;
448     }
449     crunch_configs[i].sub_configs_size_ = n_lz77s;
450   }
451   return 1;
452 }
453 
EncoderInit(VP8LEncoder * const enc)454 static int EncoderInit(VP8LEncoder* const enc) {
455   const WebPPicture* const pic = enc->pic_;
456   const int width = pic->width;
457   const int height = pic->height;
458   const int pix_cnt = width * height;
459   // we round the block size up, so we're guaranteed to have
460   // at most MAX_REFS_BLOCK_PER_IMAGE blocks used:
461   const int refs_block_size = (pix_cnt - 1) / MAX_REFS_BLOCK_PER_IMAGE + 1;
462   int i;
463   if (!VP8LHashChainInit(&enc->hash_chain_, pix_cnt)) return 0;
464 
465   for (i = 0; i < 4; ++i) VP8LBackwardRefsInit(&enc->refs_[i], refs_block_size);
466 
467   return 1;
468 }
469 
470 // Returns false in case of memory error.
GetHuffBitLengthsAndCodes(const VP8LHistogramSet * const histogram_image,HuffmanTreeCode * const huffman_codes)471 static int GetHuffBitLengthsAndCodes(
472     const VP8LHistogramSet* const histogram_image,
473     HuffmanTreeCode* const huffman_codes) {
474   int i, k;
475   int ok = 0;
476   uint64_t total_length_size = 0;
477   uint8_t* mem_buf = NULL;
478   const int histogram_image_size = histogram_image->size;
479   int max_num_symbols = 0;
480   uint8_t* buf_rle = NULL;
481   HuffmanTree* huff_tree = NULL;
482 
483   // Iterate over all histograms and get the aggregate number of codes used.
484   for (i = 0; i < histogram_image_size; ++i) {
485     const VP8LHistogram* const histo = histogram_image->histograms[i];
486     HuffmanTreeCode* const codes = &huffman_codes[5 * i];
487     assert(histo != NULL);
488     for (k = 0; k < 5; ++k) {
489       const int num_symbols =
490           (k == 0) ? VP8LHistogramNumCodes(histo->palette_code_bits_) :
491           (k == 4) ? NUM_DISTANCE_CODES : 256;
492       codes[k].num_symbols = num_symbols;
493       total_length_size += num_symbols;
494     }
495   }
496 
497   // Allocate and Set Huffman codes.
498   {
499     uint16_t* codes;
500     uint8_t* lengths;
501     mem_buf = (uint8_t*)WebPSafeCalloc(total_length_size,
502                                        sizeof(*lengths) + sizeof(*codes));
503     if (mem_buf == NULL) goto End;
504 
505     codes = (uint16_t*)mem_buf;
506     lengths = (uint8_t*)&codes[total_length_size];
507     for (i = 0; i < 5 * histogram_image_size; ++i) {
508       const int bit_length = huffman_codes[i].num_symbols;
509       huffman_codes[i].codes = codes;
510       huffman_codes[i].code_lengths = lengths;
511       codes += bit_length;
512       lengths += bit_length;
513       if (max_num_symbols < bit_length) {
514         max_num_symbols = bit_length;
515       }
516     }
517   }
518 
519   buf_rle = (uint8_t*)WebPSafeMalloc(1ULL, max_num_symbols);
520   huff_tree = (HuffmanTree*)WebPSafeMalloc(3ULL * max_num_symbols,
521                                            sizeof(*huff_tree));
522   if (buf_rle == NULL || huff_tree == NULL) goto End;
523 
524   // Create Huffman trees.
525   for (i = 0; i < histogram_image_size; ++i) {
526     HuffmanTreeCode* const codes = &huffman_codes[5 * i];
527     VP8LHistogram* const histo = histogram_image->histograms[i];
528     VP8LCreateHuffmanTree(histo->literal_, 15, buf_rle, huff_tree, codes + 0);
529     VP8LCreateHuffmanTree(histo->red_, 15, buf_rle, huff_tree, codes + 1);
530     VP8LCreateHuffmanTree(histo->blue_, 15, buf_rle, huff_tree, codes + 2);
531     VP8LCreateHuffmanTree(histo->alpha_, 15, buf_rle, huff_tree, codes + 3);
532     VP8LCreateHuffmanTree(histo->distance_, 15, buf_rle, huff_tree, codes + 4);
533   }
534   ok = 1;
535  End:
536   WebPSafeFree(huff_tree);
537   WebPSafeFree(buf_rle);
538   if (!ok) {
539     WebPSafeFree(mem_buf);
540     memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes));
541   }
542   return ok;
543 }
544 
StoreHuffmanTreeOfHuffmanTreeToBitMask(VP8LBitWriter * const bw,const uint8_t * code_length_bitdepth)545 static void StoreHuffmanTreeOfHuffmanTreeToBitMask(
546     VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) {
547   // RFC 1951 will calm you down if you are worried about this funny sequence.
548   // This sequence is tuned from that, but more weighted for lower symbol count,
549   // and more spiking histograms.
550   static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = {
551     17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
552   };
553   int i;
554   // Throw away trailing zeros:
555   int codes_to_store = CODE_LENGTH_CODES;
556   for (; codes_to_store > 4; --codes_to_store) {
557     if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
558       break;
559     }
560   }
561   VP8LPutBits(bw, codes_to_store - 4, 4);
562   for (i = 0; i < codes_to_store; ++i) {
563     VP8LPutBits(bw, code_length_bitdepth[kStorageOrder[i]], 3);
564   }
565 }
566 
ClearHuffmanTreeIfOnlyOneSymbol(HuffmanTreeCode * const huffman_code)567 static void ClearHuffmanTreeIfOnlyOneSymbol(
568     HuffmanTreeCode* const huffman_code) {
569   int k;
570   int count = 0;
571   for (k = 0; k < huffman_code->num_symbols; ++k) {
572     if (huffman_code->code_lengths[k] != 0) {
573       ++count;
574       if (count > 1) return;
575     }
576   }
577   for (k = 0; k < huffman_code->num_symbols; ++k) {
578     huffman_code->code_lengths[k] = 0;
579     huffman_code->codes[k] = 0;
580   }
581 }
582 
StoreHuffmanTreeToBitMask(VP8LBitWriter * const bw,const HuffmanTreeToken * const tokens,const int num_tokens,const HuffmanTreeCode * const huffman_code)583 static void StoreHuffmanTreeToBitMask(
584     VP8LBitWriter* const bw,
585     const HuffmanTreeToken* const tokens, const int num_tokens,
586     const HuffmanTreeCode* const huffman_code) {
587   int i;
588   for (i = 0; i < num_tokens; ++i) {
589     const int ix = tokens[i].code;
590     const int extra_bits = tokens[i].extra_bits;
591     VP8LPutBits(bw, huffman_code->codes[ix], huffman_code->code_lengths[ix]);
592     switch (ix) {
593       case 16:
594         VP8LPutBits(bw, extra_bits, 2);
595         break;
596       case 17:
597         VP8LPutBits(bw, extra_bits, 3);
598         break;
599       case 18:
600         VP8LPutBits(bw, extra_bits, 7);
601         break;
602     }
603   }
604 }
605 
606 // 'huff_tree' and 'tokens' are pre-alloacted buffers.
StoreFullHuffmanCode(VP8LBitWriter * const bw,HuffmanTree * const huff_tree,HuffmanTreeToken * const tokens,const HuffmanTreeCode * const tree)607 static void StoreFullHuffmanCode(VP8LBitWriter* const bw,
608                                  HuffmanTree* const huff_tree,
609                                  HuffmanTreeToken* const tokens,
610                                  const HuffmanTreeCode* const tree) {
611   uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 };
612   uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 };
613   const int max_tokens = tree->num_symbols;
614   int num_tokens;
615   HuffmanTreeCode huffman_code;
616   huffman_code.num_symbols = CODE_LENGTH_CODES;
617   huffman_code.code_lengths = code_length_bitdepth;
618   huffman_code.codes = code_length_bitdepth_symbols;
619 
620   VP8LPutBits(bw, 0, 1);
621   num_tokens = VP8LCreateCompressedHuffmanTree(tree, tokens, max_tokens);
622   {
623     uint32_t histogram[CODE_LENGTH_CODES] = { 0 };
624     uint8_t buf_rle[CODE_LENGTH_CODES] = { 0 };
625     int i;
626     for (i = 0; i < num_tokens; ++i) {
627       ++histogram[tokens[i].code];
628     }
629 
630     VP8LCreateHuffmanTree(histogram, 7, buf_rle, huff_tree, &huffman_code);
631   }
632 
633   StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth);
634   ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code);
635   {
636     int trailing_zero_bits = 0;
637     int trimmed_length = num_tokens;
638     int write_trimmed_length;
639     int length;
640     int i = num_tokens;
641     while (i-- > 0) {
642       const int ix = tokens[i].code;
643       if (ix == 0 || ix == 17 || ix == 18) {
644         --trimmed_length;   // discount trailing zeros
645         trailing_zero_bits += code_length_bitdepth[ix];
646         if (ix == 17) {
647           trailing_zero_bits += 3;
648         } else if (ix == 18) {
649           trailing_zero_bits += 7;
650         }
651       } else {
652         break;
653       }
654     }
655     write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12);
656     length = write_trimmed_length ? trimmed_length : num_tokens;
657     VP8LPutBits(bw, write_trimmed_length, 1);
658     if (write_trimmed_length) {
659       if (trimmed_length == 2) {
660         VP8LPutBits(bw, 0, 3 + 2);     // nbitpairs=1, trimmed_length=2
661       } else {
662         const int nbits = BitsLog2Floor(trimmed_length - 2);
663         const int nbitpairs = nbits / 2 + 1;
664         assert(trimmed_length > 2);
665         assert(nbitpairs - 1 < 8);
666         VP8LPutBits(bw, nbitpairs - 1, 3);
667         VP8LPutBits(bw, trimmed_length - 2, nbitpairs * 2);
668       }
669     }
670     StoreHuffmanTreeToBitMask(bw, tokens, length, &huffman_code);
671   }
672 }
673 
674 // 'huff_tree' and 'tokens' are pre-alloacted buffers.
StoreHuffmanCode(VP8LBitWriter * const bw,HuffmanTree * const huff_tree,HuffmanTreeToken * const tokens,const HuffmanTreeCode * const huffman_code)675 static void StoreHuffmanCode(VP8LBitWriter* const bw,
676                              HuffmanTree* const huff_tree,
677                              HuffmanTreeToken* const tokens,
678                              const HuffmanTreeCode* const huffman_code) {
679   int i;
680   int count = 0;
681   int symbols[2] = { 0, 0 };
682   const int kMaxBits = 8;
683   const int kMaxSymbol = 1 << kMaxBits;
684 
685   // Check whether it's a small tree.
686   for (i = 0; i < huffman_code->num_symbols && count < 3; ++i) {
687     if (huffman_code->code_lengths[i] != 0) {
688       if (count < 2) symbols[count] = i;
689       ++count;
690     }
691   }
692 
693   if (count == 0) {   // emit minimal tree for empty cases
694     // bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0
695     VP8LPutBits(bw, 0x01, 4);
696   } else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) {
697     VP8LPutBits(bw, 1, 1);  // Small tree marker to encode 1 or 2 symbols.
698     VP8LPutBits(bw, count - 1, 1);
699     if (symbols[0] <= 1) {
700       VP8LPutBits(bw, 0, 1);  // Code bit for small (1 bit) symbol value.
701       VP8LPutBits(bw, symbols[0], 1);
702     } else {
703       VP8LPutBits(bw, 1, 1);
704       VP8LPutBits(bw, symbols[0], 8);
705     }
706     if (count == 2) {
707       VP8LPutBits(bw, symbols[1], 8);
708     }
709   } else {
710     StoreFullHuffmanCode(bw, huff_tree, tokens, huffman_code);
711   }
712 }
713 
WriteHuffmanCode(VP8LBitWriter * const bw,const HuffmanTreeCode * const code,int code_index)714 static WEBP_INLINE void WriteHuffmanCode(VP8LBitWriter* const bw,
715                              const HuffmanTreeCode* const code,
716                              int code_index) {
717   const int depth = code->code_lengths[code_index];
718   const int symbol = code->codes[code_index];
719   VP8LPutBits(bw, symbol, depth);
720 }
721 
WriteHuffmanCodeWithExtraBits(VP8LBitWriter * const bw,const HuffmanTreeCode * const code,int code_index,int bits,int n_bits)722 static WEBP_INLINE void WriteHuffmanCodeWithExtraBits(
723     VP8LBitWriter* const bw,
724     const HuffmanTreeCode* const code,
725     int code_index,
726     int bits,
727     int n_bits) {
728   const int depth = code->code_lengths[code_index];
729   const int symbol = code->codes[code_index];
730   VP8LPutBits(bw, (bits << depth) | symbol, depth + n_bits);
731 }
732 
StoreImageToBitMask(VP8LBitWriter * const bw,int width,int histo_bits,const VP8LBackwardRefs * const refs,const uint16_t * histogram_symbols,const HuffmanTreeCode * const huffman_codes)733 static WebPEncodingError StoreImageToBitMask(
734     VP8LBitWriter* const bw, int width, int histo_bits,
735     const VP8LBackwardRefs* const refs,
736     const uint16_t* histogram_symbols,
737     const HuffmanTreeCode* const huffman_codes) {
738   const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1;
739   const int tile_mask = (histo_bits == 0) ? 0 : -(1 << histo_bits);
740   // x and y trace the position in the image.
741   int x = 0;
742   int y = 0;
743   int tile_x = x & tile_mask;
744   int tile_y = y & tile_mask;
745   int histogram_ix = histogram_symbols[0];
746   const HuffmanTreeCode* codes = huffman_codes + 5 * histogram_ix;
747   VP8LRefsCursor c = VP8LRefsCursorInit(refs);
748   while (VP8LRefsCursorOk(&c)) {
749     const PixOrCopy* const v = c.cur_pos;
750     if ((tile_x != (x & tile_mask)) || (tile_y != (y & tile_mask))) {
751       tile_x = x & tile_mask;
752       tile_y = y & tile_mask;
753       histogram_ix = histogram_symbols[(y >> histo_bits) * histo_xsize +
754                                        (x >> histo_bits)];
755       codes = huffman_codes + 5 * histogram_ix;
756     }
757     if (PixOrCopyIsLiteral(v)) {
758       static const uint8_t order[] = { 1, 2, 0, 3 };
759       int k;
760       for (k = 0; k < 4; ++k) {
761         const int code = PixOrCopyLiteral(v, order[k]);
762         WriteHuffmanCode(bw, codes + k, code);
763       }
764     } else if (PixOrCopyIsCacheIdx(v)) {
765       const int code = PixOrCopyCacheIdx(v);
766       const int literal_ix = 256 + NUM_LENGTH_CODES + code;
767       WriteHuffmanCode(bw, codes, literal_ix);
768     } else {
769       int bits, n_bits;
770       int code;
771 
772       const int distance = PixOrCopyDistance(v);
773       VP8LPrefixEncode(v->len, &code, &n_bits, &bits);
774       WriteHuffmanCodeWithExtraBits(bw, codes, 256 + code, bits, n_bits);
775 
776       // Don't write the distance with the extra bits code since
777       // the distance can be up to 18 bits of extra bits, and the prefix
778       // 15 bits, totaling to 33, and our PutBits only supports up to 32 bits.
779       VP8LPrefixEncode(distance, &code, &n_bits, &bits);
780       WriteHuffmanCode(bw, codes + 4, code);
781       VP8LPutBits(bw, bits, n_bits);
782     }
783     x += PixOrCopyLength(v);
784     while (x >= width) {
785       x -= width;
786       ++y;
787     }
788     VP8LRefsCursorNext(&c);
789   }
790   return bw->error_ ? VP8_ENC_ERROR_OUT_OF_MEMORY : VP8_ENC_OK;
791 }
792 
793 // Special case of EncodeImageInternal() for cache-bits=0, histo_bits=31
EncodeImageNoHuffman(VP8LBitWriter * const bw,const uint32_t * const argb,VP8LHashChain * const hash_chain,VP8LBackwardRefs * const refs_array,int width,int height,int quality,int low_effort)794 static WebPEncodingError EncodeImageNoHuffman(
795     VP8LBitWriter* const bw, const uint32_t* const argb,
796     VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs_array,
797     int width, int height, int quality, int low_effort) {
798   int i;
799   int max_tokens = 0;
800   WebPEncodingError err = VP8_ENC_OK;
801   VP8LBackwardRefs* refs;
802   HuffmanTreeToken* tokens = NULL;
803   HuffmanTreeCode huffman_codes[5] = { { 0, NULL, NULL } };
804   const uint16_t histogram_symbols[1] = { 0 };    // only one tree, one symbol
805   int cache_bits = 0;
806   VP8LHistogramSet* histogram_image = NULL;
807   HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc(
808         3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree));
809   if (huff_tree == NULL) {
810     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
811     goto Error;
812   }
813 
814   // Calculate backward references from ARGB image.
815   if (!VP8LHashChainFill(hash_chain, quality, argb, width, height,
816                          low_effort)) {
817     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
818     goto Error;
819   }
820   err = VP8LGetBackwardReferences(
821       width, height, argb, quality, /*low_effort=*/0, kLZ77Standard | kLZ77RLE,
822       cache_bits, /*do_no_cache=*/0, hash_chain, refs_array, &cache_bits);
823   if (err != VP8_ENC_OK) goto Error;
824   refs = &refs_array[0];
825   histogram_image = VP8LAllocateHistogramSet(1, cache_bits);
826   if (histogram_image == NULL) {
827     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
828     goto Error;
829   }
830   VP8LHistogramSetClear(histogram_image);
831 
832   // Build histogram image and symbols from backward references.
833   VP8LHistogramStoreRefs(refs, histogram_image->histograms[0]);
834 
835   // Create Huffman bit lengths and codes for each histogram image.
836   assert(histogram_image->size == 1);
837   if (!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
838     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
839     goto Error;
840   }
841 
842   // No color cache, no Huffman image.
843   VP8LPutBits(bw, 0, 1);
844 
845   // Find maximum number of symbols for the huffman tree-set.
846   for (i = 0; i < 5; ++i) {
847     HuffmanTreeCode* const codes = &huffman_codes[i];
848     if (max_tokens < codes->num_symbols) {
849       max_tokens = codes->num_symbols;
850     }
851   }
852 
853   tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens));
854   if (tokens == NULL) {
855     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
856     goto Error;
857   }
858 
859   // Store Huffman codes.
860   for (i = 0; i < 5; ++i) {
861     HuffmanTreeCode* const codes = &huffman_codes[i];
862     StoreHuffmanCode(bw, huff_tree, tokens, codes);
863     ClearHuffmanTreeIfOnlyOneSymbol(codes);
864   }
865 
866   // Store actual literals.
867   err = StoreImageToBitMask(bw, width, 0, refs, histogram_symbols,
868                             huffman_codes);
869 
870  Error:
871   WebPSafeFree(tokens);
872   WebPSafeFree(huff_tree);
873   VP8LFreeHistogramSet(histogram_image);
874   WebPSafeFree(huffman_codes[0].codes);
875   return err;
876 }
877 
EncodeImageInternal(VP8LBitWriter * const bw,const uint32_t * const argb,VP8LHashChain * const hash_chain,VP8LBackwardRefs refs_array[4],int width,int height,int quality,int low_effort,int use_cache,const CrunchConfig * const config,int * cache_bits,int histogram_bits,size_t init_byte_position,int * const hdr_size,int * const data_size)878 static WebPEncodingError EncodeImageInternal(
879     VP8LBitWriter* const bw, const uint32_t* const argb,
880     VP8LHashChain* const hash_chain, VP8LBackwardRefs refs_array[4], int width,
881     int height, int quality, int low_effort, int use_cache,
882     const CrunchConfig* const config, int* cache_bits, int histogram_bits,
883     size_t init_byte_position, int* const hdr_size, int* const data_size) {
884   WebPEncodingError err = VP8_ENC_ERROR_OUT_OF_MEMORY;
885   const uint32_t histogram_image_xysize =
886       VP8LSubSampleSize(width, histogram_bits) *
887       VP8LSubSampleSize(height, histogram_bits);
888   VP8LHistogramSet* histogram_image = NULL;
889   VP8LHistogram* tmp_histo = NULL;
890   int histogram_image_size = 0;
891   size_t bit_array_size = 0;
892   HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc(
893       3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree));
894   HuffmanTreeToken* tokens = NULL;
895   HuffmanTreeCode* huffman_codes = NULL;
896   uint16_t* const histogram_symbols =
897       (uint16_t*)WebPSafeMalloc(histogram_image_xysize,
898                                 sizeof(*histogram_symbols));
899   int sub_configs_idx;
900   int cache_bits_init, write_histogram_image;
901   VP8LBitWriter bw_init = *bw, bw_best;
902   int hdr_size_tmp;
903   VP8LHashChain hash_chain_histogram;  // histogram image hash chain
904   size_t bw_size_best = ~(size_t)0;
905   assert(histogram_bits >= MIN_HUFFMAN_BITS);
906   assert(histogram_bits <= MAX_HUFFMAN_BITS);
907   assert(hdr_size != NULL);
908   assert(data_size != NULL);
909 
910   // Make sure we can allocate the different objects.
911   memset(&hash_chain_histogram, 0, sizeof(hash_chain_histogram));
912   if (huff_tree == NULL || histogram_symbols == NULL ||
913       !VP8LHashChainInit(&hash_chain_histogram, histogram_image_xysize) ||
914       !VP8LHashChainFill(hash_chain, quality, argb, width, height,
915                          low_effort)) {
916     goto Error;
917   }
918   if (use_cache) {
919     // If the value is different from zero, it has been set during the
920     // palette analysis.
921     cache_bits_init = (*cache_bits == 0) ? MAX_COLOR_CACHE_BITS : *cache_bits;
922   } else {
923     cache_bits_init = 0;
924   }
925   // If several iterations will happen, clone into bw_best.
926   if (!VP8LBitWriterInit(&bw_best, 0) ||
927       ((config->sub_configs_size_ > 1 ||
928         config->sub_configs_[0].do_no_cache_) &&
929        !VP8LBitWriterClone(bw, &bw_best))) {
930     goto Error;
931   }
932   for (sub_configs_idx = 0; sub_configs_idx < config->sub_configs_size_;
933        ++sub_configs_idx) {
934     const CrunchSubConfig* const sub_config =
935         &config->sub_configs_[sub_configs_idx];
936     int cache_bits_best, i_cache;
937     err = VP8LGetBackwardReferences(width, height, argb, quality, low_effort,
938                                     sub_config->lz77_, cache_bits_init,
939                                     sub_config->do_no_cache_, hash_chain,
940                                     &refs_array[0], &cache_bits_best);
941     if (err != VP8_ENC_OK) goto Error;
942 
943     for (i_cache = 0; i_cache < (sub_config->do_no_cache_ ? 2 : 1); ++i_cache) {
944       const int cache_bits_tmp = (i_cache == 0) ? cache_bits_best : 0;
945       // Speed-up: no need to study the no-cache case if it was already studied
946       // in i_cache == 0.
947       if (i_cache == 1 && cache_bits_best == 0) break;
948 
949       // Reset the bit writer for this iteration.
950       VP8LBitWriterReset(&bw_init, bw);
951 
952       // Build histogram image and symbols from backward references.
953       histogram_image =
954           VP8LAllocateHistogramSet(histogram_image_xysize, cache_bits_tmp);
955       tmp_histo = VP8LAllocateHistogram(cache_bits_tmp);
956       if (histogram_image == NULL || tmp_histo == NULL ||
957           !VP8LGetHistoImageSymbols(width, height, &refs_array[i_cache],
958                                     quality, low_effort, histogram_bits,
959                                     cache_bits_tmp, histogram_image, tmp_histo,
960                                     histogram_symbols)) {
961         goto Error;
962       }
963       // Create Huffman bit lengths and codes for each histogram image.
964       histogram_image_size = histogram_image->size;
965       bit_array_size = 5 * histogram_image_size;
966       huffman_codes = (HuffmanTreeCode*)WebPSafeCalloc(bit_array_size,
967                                                        sizeof(*huffman_codes));
968       // Note: some histogram_image entries may point to tmp_histos[], so the
969       // latter need to outlive the following call to
970       // GetHuffBitLengthsAndCodes().
971       if (huffman_codes == NULL ||
972           !GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
973         goto Error;
974       }
975       // Free combined histograms.
976       VP8LFreeHistogramSet(histogram_image);
977       histogram_image = NULL;
978 
979       // Free scratch histograms.
980       VP8LFreeHistogram(tmp_histo);
981       tmp_histo = NULL;
982 
983       // Color Cache parameters.
984       if (cache_bits_tmp > 0) {
985         VP8LPutBits(bw, 1, 1);
986         VP8LPutBits(bw, cache_bits_tmp, 4);
987       } else {
988         VP8LPutBits(bw, 0, 1);
989       }
990 
991       // Huffman image + meta huffman.
992       write_histogram_image = (histogram_image_size > 1);
993       VP8LPutBits(bw, write_histogram_image, 1);
994       if (write_histogram_image) {
995         uint32_t* const histogram_argb =
996             (uint32_t*)WebPSafeMalloc(histogram_image_xysize,
997                                       sizeof(*histogram_argb));
998         int max_index = 0;
999         uint32_t i;
1000         if (histogram_argb == NULL) goto Error;
1001         for (i = 0; i < histogram_image_xysize; ++i) {
1002           const int symbol_index = histogram_symbols[i] & 0xffff;
1003           histogram_argb[i] = (symbol_index << 8);
1004           if (symbol_index >= max_index) {
1005             max_index = symbol_index + 1;
1006           }
1007         }
1008         histogram_image_size = max_index;
1009 
1010         VP8LPutBits(bw, histogram_bits - 2, 3);
1011         err = EncodeImageNoHuffman(
1012             bw, histogram_argb, &hash_chain_histogram, &refs_array[2],
1013             VP8LSubSampleSize(width, histogram_bits),
1014             VP8LSubSampleSize(height, histogram_bits), quality, low_effort);
1015         WebPSafeFree(histogram_argb);
1016         if (err != VP8_ENC_OK) goto Error;
1017       }
1018 
1019       // Store Huffman codes.
1020       {
1021         int i;
1022         int max_tokens = 0;
1023         // Find maximum number of symbols for the huffman tree-set.
1024         for (i = 0; i < 5 * histogram_image_size; ++i) {
1025           HuffmanTreeCode* const codes = &huffman_codes[i];
1026           if (max_tokens < codes->num_symbols) {
1027             max_tokens = codes->num_symbols;
1028           }
1029         }
1030         tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens));
1031         if (tokens == NULL) goto Error;
1032         for (i = 0; i < 5 * histogram_image_size; ++i) {
1033           HuffmanTreeCode* const codes = &huffman_codes[i];
1034           StoreHuffmanCode(bw, huff_tree, tokens, codes);
1035           ClearHuffmanTreeIfOnlyOneSymbol(codes);
1036         }
1037       }
1038       // Store actual literals.
1039       hdr_size_tmp = (int)(VP8LBitWriterNumBytes(bw) - init_byte_position);
1040       err = StoreImageToBitMask(bw, width, histogram_bits, &refs_array[i_cache],
1041                                 histogram_symbols, huffman_codes);
1042       if (err != VP8_ENC_OK) goto Error;
1043       // Keep track of the smallest image so far.
1044       if (VP8LBitWriterNumBytes(bw) < bw_size_best) {
1045         bw_size_best = VP8LBitWriterNumBytes(bw);
1046         *cache_bits = cache_bits_tmp;
1047         *hdr_size = hdr_size_tmp;
1048         *data_size =
1049             (int)(VP8LBitWriterNumBytes(bw) - init_byte_position - *hdr_size);
1050         VP8LBitWriterSwap(bw, &bw_best);
1051       }
1052       WebPSafeFree(tokens);
1053       tokens = NULL;
1054       if (huffman_codes != NULL) {
1055         WebPSafeFree(huffman_codes->codes);
1056         WebPSafeFree(huffman_codes);
1057         huffman_codes = NULL;
1058       }
1059     }
1060   }
1061   VP8LBitWriterSwap(bw, &bw_best);
1062   err = VP8_ENC_OK;
1063 
1064  Error:
1065   WebPSafeFree(tokens);
1066   WebPSafeFree(huff_tree);
1067   VP8LFreeHistogramSet(histogram_image);
1068   VP8LFreeHistogram(tmp_histo);
1069   VP8LHashChainClear(&hash_chain_histogram);
1070   if (huffman_codes != NULL) {
1071     WebPSafeFree(huffman_codes->codes);
1072     WebPSafeFree(huffman_codes);
1073   }
1074   WebPSafeFree(histogram_symbols);
1075   VP8LBitWriterWipeOut(&bw_best);
1076   return err;
1077 }
1078 
1079 // -----------------------------------------------------------------------------
1080 // Transforms
1081 
ApplySubtractGreen(VP8LEncoder * const enc,int width,int height,VP8LBitWriter * const bw)1082 static void ApplySubtractGreen(VP8LEncoder* const enc, int width, int height,
1083                                VP8LBitWriter* const bw) {
1084   VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1085   VP8LPutBits(bw, SUBTRACT_GREEN, 2);
1086   VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height);
1087 }
1088 
ApplyPredictFilter(const VP8LEncoder * const enc,int width,int height,int quality,int low_effort,int used_subtract_green,VP8LBitWriter * const bw)1089 static WebPEncodingError ApplyPredictFilter(const VP8LEncoder* const enc,
1090                                             int width, int height,
1091                                             int quality, int low_effort,
1092                                             int used_subtract_green,
1093                                             VP8LBitWriter* const bw) {
1094   const int pred_bits = enc->transform_bits_;
1095   const int transform_width = VP8LSubSampleSize(width, pred_bits);
1096   const int transform_height = VP8LSubSampleSize(height, pred_bits);
1097   // we disable near-lossless quantization if palette is used.
1098   const int near_lossless_strength = enc->use_palette_ ? 100
1099                                    : enc->config_->near_lossless;
1100 
1101   VP8LResidualImage(width, height, pred_bits, low_effort, enc->argb_,
1102                     enc->argb_scratch_, enc->transform_data_,
1103                     near_lossless_strength, enc->config_->exact,
1104                     used_subtract_green);
1105   VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1106   VP8LPutBits(bw, PREDICTOR_TRANSFORM, 2);
1107   assert(pred_bits >= 2);
1108   VP8LPutBits(bw, pred_bits - 2, 3);
1109   return EncodeImageNoHuffman(
1110       bw, enc->transform_data_, (VP8LHashChain*)&enc->hash_chain_,
1111       (VP8LBackwardRefs*)&enc->refs_[0], transform_width, transform_height,
1112       quality, low_effort);
1113 }
1114 
ApplyCrossColorFilter(const VP8LEncoder * const enc,int width,int height,int quality,int low_effort,VP8LBitWriter * const bw)1115 static WebPEncodingError ApplyCrossColorFilter(const VP8LEncoder* const enc,
1116                                                int width, int height,
1117                                                int quality, int low_effort,
1118                                                VP8LBitWriter* const bw) {
1119   const int ccolor_transform_bits = enc->transform_bits_;
1120   const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits);
1121   const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits);
1122 
1123   VP8LColorSpaceTransform(width, height, ccolor_transform_bits, quality,
1124                           enc->argb_, enc->transform_data_);
1125   VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1126   VP8LPutBits(bw, CROSS_COLOR_TRANSFORM, 2);
1127   assert(ccolor_transform_bits >= 2);
1128   VP8LPutBits(bw, ccolor_transform_bits - 2, 3);
1129   return EncodeImageNoHuffman(
1130       bw, enc->transform_data_, (VP8LHashChain*)&enc->hash_chain_,
1131       (VP8LBackwardRefs*)&enc->refs_[0], transform_width, transform_height,
1132       quality, low_effort);
1133 }
1134 
1135 // -----------------------------------------------------------------------------
1136 
WriteRiffHeader(const WebPPicture * const pic,size_t riff_size,size_t vp8l_size)1137 static WebPEncodingError WriteRiffHeader(const WebPPicture* const pic,
1138                                          size_t riff_size, size_t vp8l_size) {
1139   uint8_t riff[RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + VP8L_SIGNATURE_SIZE] = {
1140     'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P',
1141     'V', 'P', '8', 'L', 0, 0, 0, 0, VP8L_MAGIC_BYTE,
1142   };
1143   PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
1144   PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size);
1145   if (!pic->writer(riff, sizeof(riff), pic)) {
1146     return VP8_ENC_ERROR_BAD_WRITE;
1147   }
1148   return VP8_ENC_OK;
1149 }
1150 
WriteImageSize(const WebPPicture * const pic,VP8LBitWriter * const bw)1151 static int WriteImageSize(const WebPPicture* const pic,
1152                           VP8LBitWriter* const bw) {
1153   const int width = pic->width - 1;
1154   const int height = pic->height - 1;
1155   assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
1156 
1157   VP8LPutBits(bw, width, VP8L_IMAGE_SIZE_BITS);
1158   VP8LPutBits(bw, height, VP8L_IMAGE_SIZE_BITS);
1159   return !bw->error_;
1160 }
1161 
WriteRealAlphaAndVersion(VP8LBitWriter * const bw,int has_alpha)1162 static int WriteRealAlphaAndVersion(VP8LBitWriter* const bw, int has_alpha) {
1163   VP8LPutBits(bw, has_alpha, 1);
1164   VP8LPutBits(bw, VP8L_VERSION, VP8L_VERSION_BITS);
1165   return !bw->error_;
1166 }
1167 
WriteImage(const WebPPicture * const pic,VP8LBitWriter * const bw,size_t * const coded_size)1168 static WebPEncodingError WriteImage(const WebPPicture* const pic,
1169                                     VP8LBitWriter* const bw,
1170                                     size_t* const coded_size) {
1171   WebPEncodingError err = VP8_ENC_OK;
1172   const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
1173   const size_t webpll_size = VP8LBitWriterNumBytes(bw);
1174   const size_t vp8l_size = VP8L_SIGNATURE_SIZE + webpll_size;
1175   const size_t pad = vp8l_size & 1;
1176   const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad;
1177 
1178   err = WriteRiffHeader(pic, riff_size, vp8l_size);
1179   if (err != VP8_ENC_OK) goto Error;
1180 
1181   if (!pic->writer(webpll_data, webpll_size, pic)) {
1182     err = VP8_ENC_ERROR_BAD_WRITE;
1183     goto Error;
1184   }
1185 
1186   if (pad) {
1187     const uint8_t pad_byte[1] = { 0 };
1188     if (!pic->writer(pad_byte, 1, pic)) {
1189       err = VP8_ENC_ERROR_BAD_WRITE;
1190       goto Error;
1191     }
1192   }
1193   *coded_size = CHUNK_HEADER_SIZE + riff_size;
1194   return VP8_ENC_OK;
1195 
1196  Error:
1197   return err;
1198 }
1199 
1200 // -----------------------------------------------------------------------------
1201 
ClearTransformBuffer(VP8LEncoder * const enc)1202 static void ClearTransformBuffer(VP8LEncoder* const enc) {
1203   WebPSafeFree(enc->transform_mem_);
1204   enc->transform_mem_ = NULL;
1205   enc->transform_mem_size_ = 0;
1206 }
1207 
1208 // Allocates the memory for argb (W x H) buffer, 2 rows of context for
1209 // prediction and transform data.
1210 // Flags influencing the memory allocated:
1211 //  enc->transform_bits_
1212 //  enc->use_predict_, enc->use_cross_color_
AllocateTransformBuffer(VP8LEncoder * const enc,int width,int height)1213 static WebPEncodingError AllocateTransformBuffer(VP8LEncoder* const enc,
1214                                                  int width, int height) {
1215   WebPEncodingError err = VP8_ENC_OK;
1216   const uint64_t image_size = width * height;
1217   // VP8LResidualImage needs room for 2 scanlines of uint32 pixels with an extra
1218   // pixel in each, plus 2 regular scanlines of bytes.
1219   // TODO(skal): Clean up by using arithmetic in bytes instead of words.
1220   const uint64_t argb_scratch_size =
1221       enc->use_predict_
1222           ? (width + 1) * 2 +
1223             (width * 2 + sizeof(uint32_t) - 1) / sizeof(uint32_t)
1224           : 0;
1225   const uint64_t transform_data_size =
1226       (enc->use_predict_ || enc->use_cross_color_)
1227           ? VP8LSubSampleSize(width, enc->transform_bits_) *
1228                 VP8LSubSampleSize(height, enc->transform_bits_)
1229           : 0;
1230   const uint64_t max_alignment_in_words =
1231       (WEBP_ALIGN_CST + sizeof(uint32_t) - 1) / sizeof(uint32_t);
1232   const uint64_t mem_size =
1233       image_size + max_alignment_in_words +
1234       argb_scratch_size + max_alignment_in_words +
1235       transform_data_size;
1236   uint32_t* mem = enc->transform_mem_;
1237   if (mem == NULL || mem_size > enc->transform_mem_size_) {
1238     ClearTransformBuffer(enc);
1239     mem = (uint32_t*)WebPSafeMalloc(mem_size, sizeof(*mem));
1240     if (mem == NULL) {
1241       err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1242       goto Error;
1243     }
1244     enc->transform_mem_ = mem;
1245     enc->transform_mem_size_ = (size_t)mem_size;
1246     enc->argb_content_ = kEncoderNone;
1247   }
1248   enc->argb_ = mem;
1249   mem = (uint32_t*)WEBP_ALIGN(mem + image_size);
1250   enc->argb_scratch_ = mem;
1251   mem = (uint32_t*)WEBP_ALIGN(mem + argb_scratch_size);
1252   enc->transform_data_ = mem;
1253 
1254   enc->current_width_ = width;
1255  Error:
1256   return err;
1257 }
1258 
MakeInputImageCopy(VP8LEncoder * const enc)1259 static WebPEncodingError MakeInputImageCopy(VP8LEncoder* const enc) {
1260   WebPEncodingError err = VP8_ENC_OK;
1261   const WebPPicture* const picture = enc->pic_;
1262   const int width = picture->width;
1263   const int height = picture->height;
1264 
1265   err = AllocateTransformBuffer(enc, width, height);
1266   if (err != VP8_ENC_OK) return err;
1267   if (enc->argb_content_ == kEncoderARGB) return VP8_ENC_OK;
1268 
1269   {
1270     uint32_t* dst = enc->argb_;
1271     const uint32_t* src = picture->argb;
1272     int y;
1273     for (y = 0; y < height; ++y) {
1274       memcpy(dst, src, width * sizeof(*dst));
1275       dst += width;
1276       src += picture->argb_stride;
1277     }
1278   }
1279   enc->argb_content_ = kEncoderARGB;
1280   assert(enc->current_width_ == width);
1281   return VP8_ENC_OK;
1282 }
1283 
1284 // -----------------------------------------------------------------------------
1285 
SearchColorNoIdx(const uint32_t sorted[],uint32_t color,int hi)1286 static WEBP_INLINE int SearchColorNoIdx(const uint32_t sorted[], uint32_t color,
1287                                         int hi) {
1288   int low = 0;
1289   if (sorted[low] == color) return low;  // loop invariant: sorted[low] != color
1290   while (1) {
1291     const int mid = (low + hi) >> 1;
1292     if (sorted[mid] == color) {
1293       return mid;
1294     } else if (sorted[mid] < color) {
1295       low = mid;
1296     } else {
1297       hi = mid;
1298     }
1299   }
1300 }
1301 
1302 #define APPLY_PALETTE_GREEDY_MAX 4
1303 
SearchColorGreedy(const uint32_t palette[],int palette_size,uint32_t color)1304 static WEBP_INLINE uint32_t SearchColorGreedy(const uint32_t palette[],
1305                                               int palette_size,
1306                                               uint32_t color) {
1307   (void)palette_size;
1308   assert(palette_size < APPLY_PALETTE_GREEDY_MAX);
1309   assert(3 == APPLY_PALETTE_GREEDY_MAX - 1);
1310   if (color == palette[0]) return 0;
1311   if (color == palette[1]) return 1;
1312   if (color == palette[2]) return 2;
1313   return 3;
1314 }
1315 
ApplyPaletteHash0(uint32_t color)1316 static WEBP_INLINE uint32_t ApplyPaletteHash0(uint32_t color) {
1317   // Focus on the green color.
1318   return (color >> 8) & 0xff;
1319 }
1320 
1321 #define PALETTE_INV_SIZE_BITS 11
1322 #define PALETTE_INV_SIZE (1 << PALETTE_INV_SIZE_BITS)
1323 
ApplyPaletteHash1(uint32_t color)1324 static WEBP_INLINE uint32_t ApplyPaletteHash1(uint32_t color) {
1325   // Forget about alpha.
1326   return ((uint32_t)((color & 0x00ffffffu) * 4222244071ull)) >>
1327          (32 - PALETTE_INV_SIZE_BITS);
1328 }
1329 
ApplyPaletteHash2(uint32_t color)1330 static WEBP_INLINE uint32_t ApplyPaletteHash2(uint32_t color) {
1331   // Forget about alpha.
1332   return ((uint32_t)((color & 0x00ffffffu) * ((1ull << 31) - 1))) >>
1333          (32 - PALETTE_INV_SIZE_BITS);
1334 }
1335 
1336 // Sort palette in increasing order and prepare an inverse mapping array.
PrepareMapToPalette(const uint32_t palette[],int num_colors,uint32_t sorted[],uint32_t idx_map[])1337 static void PrepareMapToPalette(const uint32_t palette[], int num_colors,
1338                                 uint32_t sorted[], uint32_t idx_map[]) {
1339   int i;
1340   memcpy(sorted, palette, num_colors * sizeof(*sorted));
1341   qsort(sorted, num_colors, sizeof(*sorted), PaletteCompareColorsForQsort);
1342   for (i = 0; i < num_colors; ++i) {
1343     idx_map[SearchColorNoIdx(sorted, palette[i], num_colors)] = i;
1344   }
1345 }
1346 
1347 // Use 1 pixel cache for ARGB pixels.
1348 #define APPLY_PALETTE_FOR(COLOR_INDEX) do {         \
1349   uint32_t prev_pix = palette[0];                   \
1350   uint32_t prev_idx = 0;                            \
1351   for (y = 0; y < height; ++y) {                    \
1352     for (x = 0; x < width; ++x) {                   \
1353       const uint32_t pix = src[x];                  \
1354       if (pix != prev_pix) {                        \
1355         prev_idx = COLOR_INDEX;                     \
1356         prev_pix = pix;                             \
1357       }                                             \
1358       tmp_row[x] = prev_idx;                        \
1359     }                                               \
1360     VP8LBundleColorMap(tmp_row, width, xbits, dst); \
1361     src += src_stride;                              \
1362     dst += dst_stride;                              \
1363   }                                                 \
1364 } while (0)
1365 
1366 // Remap argb values in src[] to packed palettes entries in dst[]
1367 // using 'row' as a temporary buffer of size 'width'.
1368 // We assume that all src[] values have a corresponding entry in the palette.
1369 // Note: src[] can be the same as dst[]
ApplyPalette(const uint32_t * src,uint32_t src_stride,uint32_t * dst,uint32_t dst_stride,const uint32_t * palette,int palette_size,int width,int height,int xbits)1370 static WebPEncodingError ApplyPalette(const uint32_t* src, uint32_t src_stride,
1371                                       uint32_t* dst, uint32_t dst_stride,
1372                                       const uint32_t* palette, int palette_size,
1373                                       int width, int height, int xbits) {
1374   // TODO(skal): this tmp buffer is not needed if VP8LBundleColorMap() can be
1375   // made to work in-place.
1376   uint8_t* const tmp_row = (uint8_t*)WebPSafeMalloc(width, sizeof(*tmp_row));
1377   int x, y;
1378 
1379   if (tmp_row == NULL) return VP8_ENC_ERROR_OUT_OF_MEMORY;
1380 
1381   if (palette_size < APPLY_PALETTE_GREEDY_MAX) {
1382     APPLY_PALETTE_FOR(SearchColorGreedy(palette, palette_size, pix));
1383   } else {
1384     int i, j;
1385     uint16_t buffer[PALETTE_INV_SIZE];
1386     uint32_t (*const hash_functions[])(uint32_t) = {
1387         ApplyPaletteHash0, ApplyPaletteHash1, ApplyPaletteHash2
1388     };
1389 
1390     // Try to find a perfect hash function able to go from a color to an index
1391     // within 1 << PALETTE_INV_SIZE_BITS in order to build a hash map to go
1392     // from color to index in palette.
1393     for (i = 0; i < 3; ++i) {
1394       int use_LUT = 1;
1395       // Set each element in buffer to max uint16_t.
1396       memset(buffer, 0xff, sizeof(buffer));
1397       for (j = 0; j < palette_size; ++j) {
1398         const uint32_t ind = hash_functions[i](palette[j]);
1399         if (buffer[ind] != 0xffffu) {
1400           use_LUT = 0;
1401           break;
1402         } else {
1403           buffer[ind] = j;
1404         }
1405       }
1406       if (use_LUT) break;
1407     }
1408 
1409     if (i == 0) {
1410       APPLY_PALETTE_FOR(buffer[ApplyPaletteHash0(pix)]);
1411     } else if (i == 1) {
1412       APPLY_PALETTE_FOR(buffer[ApplyPaletteHash1(pix)]);
1413     } else if (i == 2) {
1414       APPLY_PALETTE_FOR(buffer[ApplyPaletteHash2(pix)]);
1415     } else {
1416       uint32_t idx_map[MAX_PALETTE_SIZE];
1417       uint32_t palette_sorted[MAX_PALETTE_SIZE];
1418       PrepareMapToPalette(palette, palette_size, palette_sorted, idx_map);
1419       APPLY_PALETTE_FOR(
1420           idx_map[SearchColorNoIdx(palette_sorted, pix, palette_size)]);
1421     }
1422   }
1423   WebPSafeFree(tmp_row);
1424   return VP8_ENC_OK;
1425 }
1426 #undef APPLY_PALETTE_FOR
1427 #undef PALETTE_INV_SIZE_BITS
1428 #undef PALETTE_INV_SIZE
1429 #undef APPLY_PALETTE_GREEDY_MAX
1430 
1431 // Note: Expects "enc->palette_" to be set properly.
MapImageFromPalette(VP8LEncoder * const enc,int in_place)1432 static WebPEncodingError MapImageFromPalette(VP8LEncoder* const enc,
1433                                              int in_place) {
1434   WebPEncodingError err = VP8_ENC_OK;
1435   const WebPPicture* const pic = enc->pic_;
1436   const int width = pic->width;
1437   const int height = pic->height;
1438   const uint32_t* const palette = enc->palette_;
1439   const uint32_t* src = in_place ? enc->argb_ : pic->argb;
1440   const int src_stride = in_place ? enc->current_width_ : pic->argb_stride;
1441   const int palette_size = enc->palette_size_;
1442   int xbits;
1443 
1444   // Replace each input pixel by corresponding palette index.
1445   // This is done line by line.
1446   if (palette_size <= 4) {
1447     xbits = (palette_size <= 2) ? 3 : 2;
1448   } else {
1449     xbits = (palette_size <= 16) ? 1 : 0;
1450   }
1451 
1452   err = AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height);
1453   if (err != VP8_ENC_OK) return err;
1454 
1455   err = ApplyPalette(src, src_stride,
1456                      enc->argb_, enc->current_width_,
1457                      palette, palette_size, width, height, xbits);
1458   enc->argb_content_ = kEncoderPalette;
1459   return err;
1460 }
1461 
1462 // Save palette_[] to bitstream.
EncodePalette(VP8LBitWriter * const bw,int low_effort,VP8LEncoder * const enc)1463 static WebPEncodingError EncodePalette(VP8LBitWriter* const bw, int low_effort,
1464                                        VP8LEncoder* const enc) {
1465   int i;
1466   uint32_t tmp_palette[MAX_PALETTE_SIZE];
1467   const int palette_size = enc->palette_size_;
1468   const uint32_t* const palette = enc->palette_;
1469   VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1470   VP8LPutBits(bw, COLOR_INDEXING_TRANSFORM, 2);
1471   assert(palette_size >= 1 && palette_size <= MAX_PALETTE_SIZE);
1472   VP8LPutBits(bw, palette_size - 1, 8);
1473   for (i = palette_size - 1; i >= 1; --i) {
1474     tmp_palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
1475   }
1476   tmp_palette[0] = palette[0];
1477   return EncodeImageNoHuffman(bw, tmp_palette, &enc->hash_chain_,
1478                               &enc->refs_[0], palette_size, 1, /*quality=*/20,
1479                               low_effort);
1480 }
1481 
1482 // -----------------------------------------------------------------------------
1483 // VP8LEncoder
1484 
VP8LEncoderNew(const WebPConfig * const config,const WebPPicture * const picture)1485 static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
1486                                    const WebPPicture* const picture) {
1487   VP8LEncoder* const enc = (VP8LEncoder*)WebPSafeCalloc(1ULL, sizeof(*enc));
1488   if (enc == NULL) {
1489     WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1490     return NULL;
1491   }
1492   enc->config_ = config;
1493   enc->pic_ = picture;
1494   enc->argb_content_ = kEncoderNone;
1495 
1496   VP8LEncDspInit();
1497 
1498   return enc;
1499 }
1500 
VP8LEncoderDelete(VP8LEncoder * enc)1501 static void VP8LEncoderDelete(VP8LEncoder* enc) {
1502   if (enc != NULL) {
1503     int i;
1504     VP8LHashChainClear(&enc->hash_chain_);
1505     for (i = 0; i < 4; ++i) VP8LBackwardRefsClear(&enc->refs_[i]);
1506     ClearTransformBuffer(enc);
1507     WebPSafeFree(enc);
1508   }
1509 }
1510 
1511 // -----------------------------------------------------------------------------
1512 // Main call
1513 
1514 typedef struct {
1515   const WebPConfig* config_;
1516   const WebPPicture* picture_;
1517   VP8LBitWriter* bw_;
1518   VP8LEncoder* enc_;
1519   int use_cache_;
1520   CrunchConfig crunch_configs_[CRUNCH_CONFIGS_MAX];
1521   int num_crunch_configs_;
1522   int red_and_blue_always_zero_;
1523   WebPEncodingError err_;
1524   WebPAuxStats* stats_;
1525 } StreamEncodeContext;
1526 
EncodeStreamHook(void * input,void * data2)1527 static int EncodeStreamHook(void* input, void* data2) {
1528   StreamEncodeContext* const params = (StreamEncodeContext*)input;
1529   const WebPConfig* const config = params->config_;
1530   const WebPPicture* const picture = params->picture_;
1531   VP8LBitWriter* const bw = params->bw_;
1532   VP8LEncoder* const enc = params->enc_;
1533   const int use_cache = params->use_cache_;
1534   const CrunchConfig* const crunch_configs = params->crunch_configs_;
1535   const int num_crunch_configs = params->num_crunch_configs_;
1536   const int red_and_blue_always_zero = params->red_and_blue_always_zero_;
1537 #if !defined(WEBP_DISABLE_STATS)
1538   WebPAuxStats* const stats = params->stats_;
1539 #endif
1540   WebPEncodingError err = VP8_ENC_OK;
1541   const int quality = (int)config->quality;
1542   const int low_effort = (config->method == 0);
1543 #if (WEBP_NEAR_LOSSLESS == 1)
1544   const int width = picture->width;
1545 #endif
1546   const int height = picture->height;
1547   const size_t byte_position = VP8LBitWriterNumBytes(bw);
1548 #if (WEBP_NEAR_LOSSLESS == 1)
1549   int use_near_lossless = 0;
1550 #endif
1551   int hdr_size = 0;
1552   int data_size = 0;
1553   int use_delta_palette = 0;
1554   int idx;
1555   size_t best_size = ~(size_t)0;
1556   VP8LBitWriter bw_init = *bw, bw_best;
1557   (void)data2;
1558 
1559   if (!VP8LBitWriterInit(&bw_best, 0) ||
1560       (num_crunch_configs > 1 && !VP8LBitWriterClone(bw, &bw_best))) {
1561     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1562     goto Error;
1563   }
1564 
1565   for (idx = 0; idx < num_crunch_configs; ++idx) {
1566     const int entropy_idx = crunch_configs[idx].entropy_idx_;
1567     enc->use_palette_ =
1568         (entropy_idx == kPalette) || (entropy_idx == kPaletteAndSpatial);
1569     enc->use_subtract_green_ =
1570         (entropy_idx == kSubGreen) || (entropy_idx == kSpatialSubGreen);
1571     enc->use_predict_ = (entropy_idx == kSpatial) ||
1572                         (entropy_idx == kSpatialSubGreen) ||
1573                         (entropy_idx == kPaletteAndSpatial);
1574     if (low_effort) {
1575       enc->use_cross_color_ = 0;
1576     } else {
1577       enc->use_cross_color_ = red_and_blue_always_zero ? 0 : enc->use_predict_;
1578     }
1579     // Reset any parameter in the encoder that is set in the previous iteration.
1580     enc->cache_bits_ = 0;
1581     VP8LBackwardRefsClear(&enc->refs_[0]);
1582     VP8LBackwardRefsClear(&enc->refs_[1]);
1583 
1584 #if (WEBP_NEAR_LOSSLESS == 1)
1585     // Apply near-lossless preprocessing.
1586     use_near_lossless = (config->near_lossless < 100) && !enc->use_palette_ &&
1587                         !enc->use_predict_;
1588     if (use_near_lossless) {
1589       err = AllocateTransformBuffer(enc, width, height);
1590       if (err != VP8_ENC_OK) goto Error;
1591       if ((enc->argb_content_ != kEncoderNearLossless) &&
1592           !VP8ApplyNearLossless(picture, config->near_lossless, enc->argb_)) {
1593         err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1594         goto Error;
1595       }
1596       enc->argb_content_ = kEncoderNearLossless;
1597     } else {
1598       enc->argb_content_ = kEncoderNone;
1599     }
1600 #else
1601     enc->argb_content_ = kEncoderNone;
1602 #endif
1603 
1604     // Encode palette
1605     if (enc->use_palette_) {
1606       err = EncodePalette(bw, low_effort, enc);
1607       if (err != VP8_ENC_OK) goto Error;
1608       err = MapImageFromPalette(enc, use_delta_palette);
1609       if (err != VP8_ENC_OK) goto Error;
1610       // If using a color cache, do not have it bigger than the number of
1611       // colors.
1612       if (use_cache && enc->palette_size_ < (1 << MAX_COLOR_CACHE_BITS)) {
1613         enc->cache_bits_ = BitsLog2Floor(enc->palette_size_) + 1;
1614       }
1615     }
1616     if (!use_delta_palette) {
1617       // In case image is not packed.
1618       if (enc->argb_content_ != kEncoderNearLossless &&
1619           enc->argb_content_ != kEncoderPalette) {
1620         err = MakeInputImageCopy(enc);
1621         if (err != VP8_ENC_OK) goto Error;
1622       }
1623 
1624       // -----------------------------------------------------------------------
1625       // Apply transforms and write transform data.
1626 
1627       if (enc->use_subtract_green_) {
1628         ApplySubtractGreen(enc, enc->current_width_, height, bw);
1629       }
1630 
1631       if (enc->use_predict_) {
1632         err = ApplyPredictFilter(enc, enc->current_width_, height, quality,
1633                                  low_effort, enc->use_subtract_green_, bw);
1634         if (err != VP8_ENC_OK) goto Error;
1635       }
1636 
1637       if (enc->use_cross_color_) {
1638         err = ApplyCrossColorFilter(enc, enc->current_width_, height, quality,
1639                                     low_effort, bw);
1640         if (err != VP8_ENC_OK) goto Error;
1641       }
1642     }
1643 
1644     VP8LPutBits(bw, !TRANSFORM_PRESENT, 1);  // No more transforms.
1645 
1646     // -------------------------------------------------------------------------
1647     // Encode and write the transformed image.
1648     err = EncodeImageInternal(bw, enc->argb_, &enc->hash_chain_, enc->refs_,
1649                               enc->current_width_, height, quality, low_effort,
1650                               use_cache, &crunch_configs[idx],
1651                               &enc->cache_bits_, enc->histo_bits_,
1652                               byte_position, &hdr_size, &data_size);
1653     if (err != VP8_ENC_OK) goto Error;
1654 
1655     // If we are better than what we already have.
1656     if (VP8LBitWriterNumBytes(bw) < best_size) {
1657       best_size = VP8LBitWriterNumBytes(bw);
1658       // Store the BitWriter.
1659       VP8LBitWriterSwap(bw, &bw_best);
1660 #if !defined(WEBP_DISABLE_STATS)
1661       // Update the stats.
1662       if (stats != NULL) {
1663         stats->lossless_features = 0;
1664         if (enc->use_predict_) stats->lossless_features |= 1;
1665         if (enc->use_cross_color_) stats->lossless_features |= 2;
1666         if (enc->use_subtract_green_) stats->lossless_features |= 4;
1667         if (enc->use_palette_) stats->lossless_features |= 8;
1668         stats->histogram_bits = enc->histo_bits_;
1669         stats->transform_bits = enc->transform_bits_;
1670         stats->cache_bits = enc->cache_bits_;
1671         stats->palette_size = enc->palette_size_;
1672         stats->lossless_size = (int)(best_size - byte_position);
1673         stats->lossless_hdr_size = hdr_size;
1674         stats->lossless_data_size = data_size;
1675       }
1676 #endif
1677     }
1678     // Reset the bit writer for the following iteration if any.
1679     if (num_crunch_configs > 1) VP8LBitWriterReset(&bw_init, bw);
1680   }
1681   VP8LBitWriterSwap(&bw_best, bw);
1682 
1683 Error:
1684   VP8LBitWriterWipeOut(&bw_best);
1685   params->err_ = err;
1686   // The hook should return false in case of error.
1687   return (err == VP8_ENC_OK);
1688 }
1689 
VP8LEncodeStream(const WebPConfig * const config,const WebPPicture * const picture,VP8LBitWriter * const bw_main,int use_cache)1690 WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
1691                                    const WebPPicture* const picture,
1692                                    VP8LBitWriter* const bw_main,
1693                                    int use_cache) {
1694   WebPEncodingError err = VP8_ENC_OK;
1695   VP8LEncoder* const enc_main = VP8LEncoderNew(config, picture);
1696   VP8LEncoder* enc_side = NULL;
1697   CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX];
1698   int num_crunch_configs_main, num_crunch_configs_side = 0;
1699   int idx;
1700   int red_and_blue_always_zero = 0;
1701   WebPWorker worker_main, worker_side;
1702   StreamEncodeContext params_main, params_side;
1703   // The main thread uses picture->stats, the side thread uses stats_side.
1704   WebPAuxStats stats_side;
1705   VP8LBitWriter bw_side;
1706   const WebPWorkerInterface* const worker_interface = WebPGetWorkerInterface();
1707   int ok_main;
1708 
1709   // Analyze image (entropy, num_palettes etc)
1710   if (enc_main == NULL ||
1711       !EncoderAnalyze(enc_main, crunch_configs, &num_crunch_configs_main,
1712                       &red_and_blue_always_zero) ||
1713       !EncoderInit(enc_main) || !VP8LBitWriterInit(&bw_side, 0)) {
1714     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1715     goto Error;
1716   }
1717 
1718   // Split the configs between the main and side threads (if any).
1719   if (config->thread_level > 0) {
1720     num_crunch_configs_side = num_crunch_configs_main / 2;
1721     for (idx = 0; idx < num_crunch_configs_side; ++idx) {
1722       params_side.crunch_configs_[idx] =
1723           crunch_configs[num_crunch_configs_main - num_crunch_configs_side +
1724                          idx];
1725     }
1726     params_side.num_crunch_configs_ = num_crunch_configs_side;
1727   }
1728   num_crunch_configs_main -= num_crunch_configs_side;
1729   for (idx = 0; idx < num_crunch_configs_main; ++idx) {
1730     params_main.crunch_configs_[idx] = crunch_configs[idx];
1731   }
1732   params_main.num_crunch_configs_ = num_crunch_configs_main;
1733 
1734   // Fill in the parameters for the thread workers.
1735   {
1736     const int params_size = (num_crunch_configs_side > 0) ? 2 : 1;
1737     for (idx = 0; idx < params_size; ++idx) {
1738       // Create the parameters for each worker.
1739       WebPWorker* const worker = (idx == 0) ? &worker_main : &worker_side;
1740       StreamEncodeContext* const param =
1741           (idx == 0) ? &params_main : &params_side;
1742       param->config_ = config;
1743       param->picture_ = picture;
1744       param->use_cache_ = use_cache;
1745       param->red_and_blue_always_zero_ = red_and_blue_always_zero;
1746       if (idx == 0) {
1747         param->stats_ = picture->stats;
1748         param->bw_ = bw_main;
1749         param->enc_ = enc_main;
1750       } else {
1751         param->stats_ = (picture->stats == NULL) ? NULL : &stats_side;
1752         // Create a side bit writer.
1753         if (!VP8LBitWriterClone(bw_main, &bw_side)) {
1754           err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1755           goto Error;
1756         }
1757         param->bw_ = &bw_side;
1758         // Create a side encoder.
1759         enc_side = VP8LEncoderNew(config, picture);
1760         if (enc_side == NULL || !EncoderInit(enc_side)) {
1761           err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1762           goto Error;
1763         }
1764         // Copy the values that were computed for the main encoder.
1765         enc_side->histo_bits_ = enc_main->histo_bits_;
1766         enc_side->transform_bits_ = enc_main->transform_bits_;
1767         enc_side->palette_size_ = enc_main->palette_size_;
1768         memcpy(enc_side->palette_, enc_main->palette_,
1769                sizeof(enc_main->palette_));
1770         param->enc_ = enc_side;
1771       }
1772       // Create the workers.
1773       worker_interface->Init(worker);
1774       worker->data1 = param;
1775       worker->data2 = NULL;
1776       worker->hook = EncodeStreamHook;
1777     }
1778   }
1779 
1780   // Start the second thread if needed.
1781   if (num_crunch_configs_side != 0) {
1782     if (!worker_interface->Reset(&worker_side)) {
1783       err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1784       goto Error;
1785     }
1786 #if !defined(WEBP_DISABLE_STATS)
1787     // This line is here and not in the param initialization above to remove a
1788     // Clang static analyzer warning.
1789     if (picture->stats != NULL) {
1790       memcpy(&stats_side, picture->stats, sizeof(stats_side));
1791     }
1792 #endif
1793     // This line is only useful to remove a Clang static analyzer warning.
1794     params_side.err_ = VP8_ENC_OK;
1795     worker_interface->Launch(&worker_side);
1796   }
1797   // Execute the main thread.
1798   worker_interface->Execute(&worker_main);
1799   ok_main = worker_interface->Sync(&worker_main);
1800   worker_interface->End(&worker_main);
1801   if (num_crunch_configs_side != 0) {
1802     // Wait for the second thread.
1803     const int ok_side = worker_interface->Sync(&worker_side);
1804     worker_interface->End(&worker_side);
1805     if (!ok_main || !ok_side) {
1806       err = ok_main ? params_side.err_ : params_main.err_;
1807       goto Error;
1808     }
1809     if (VP8LBitWriterNumBytes(&bw_side) < VP8LBitWriterNumBytes(bw_main)) {
1810       VP8LBitWriterSwap(bw_main, &bw_side);
1811 #if !defined(WEBP_DISABLE_STATS)
1812       if (picture->stats != NULL) {
1813         memcpy(picture->stats, &stats_side, sizeof(*picture->stats));
1814       }
1815 #endif
1816     }
1817   } else {
1818     if (!ok_main) {
1819       err = params_main.err_;
1820       goto Error;
1821     }
1822   }
1823 
1824 Error:
1825   VP8LBitWriterWipeOut(&bw_side);
1826   VP8LEncoderDelete(enc_main);
1827   VP8LEncoderDelete(enc_side);
1828   return err;
1829 }
1830 
1831 #undef CRUNCH_CONFIGS_MAX
1832 #undef CRUNCH_SUBCONFIGS_MAX
1833 
VP8LEncodeImage(const WebPConfig * const config,const WebPPicture * const picture)1834 int VP8LEncodeImage(const WebPConfig* const config,
1835                     const WebPPicture* const picture) {
1836   int width, height;
1837   int has_alpha;
1838   size_t coded_size;
1839   int percent = 0;
1840   int initial_size;
1841   WebPEncodingError err = VP8_ENC_OK;
1842   VP8LBitWriter bw;
1843 
1844   if (picture == NULL) return 0;
1845 
1846   if (config == NULL || picture->argb == NULL) {
1847     err = VP8_ENC_ERROR_NULL_PARAMETER;
1848     WebPEncodingSetError(picture, err);
1849     return 0;
1850   }
1851 
1852   width = picture->width;
1853   height = picture->height;
1854   // Initialize BitWriter with size corresponding to 16 bpp to photo images and
1855   // 8 bpp for graphical images.
1856   initial_size = (config->image_hint == WEBP_HINT_GRAPH) ?
1857       width * height : width * height * 2;
1858   if (!VP8LBitWriterInit(&bw, initial_size)) {
1859     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1860     goto Error;
1861   }
1862 
1863   if (!WebPReportProgress(picture, 1, &percent)) {
1864  UserAbort:
1865     err = VP8_ENC_ERROR_USER_ABORT;
1866     goto Error;
1867   }
1868   // Reset stats (for pure lossless coding)
1869   if (picture->stats != NULL) {
1870     WebPAuxStats* const stats = picture->stats;
1871     memset(stats, 0, sizeof(*stats));
1872     stats->PSNR[0] = 99.f;
1873     stats->PSNR[1] = 99.f;
1874     stats->PSNR[2] = 99.f;
1875     stats->PSNR[3] = 99.f;
1876     stats->PSNR[4] = 99.f;
1877   }
1878 
1879   // Write image size.
1880   if (!WriteImageSize(picture, &bw)) {
1881     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1882     goto Error;
1883   }
1884 
1885   has_alpha = WebPPictureHasTransparency(picture);
1886   // Write the non-trivial Alpha flag and lossless version.
1887   if (!WriteRealAlphaAndVersion(&bw, has_alpha)) {
1888     err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1889     goto Error;
1890   }
1891 
1892   if (!WebPReportProgress(picture, 5, &percent)) goto UserAbort;
1893 
1894   // Encode main image stream.
1895   err = VP8LEncodeStream(config, picture, &bw, 1 /*use_cache*/);
1896   if (err != VP8_ENC_OK) goto Error;
1897 
1898   if (!WebPReportProgress(picture, 90, &percent)) goto UserAbort;
1899 
1900   // Finish the RIFF chunk.
1901   err = WriteImage(picture, &bw, &coded_size);
1902   if (err != VP8_ENC_OK) goto Error;
1903 
1904   if (!WebPReportProgress(picture, 100, &percent)) goto UserAbort;
1905 
1906 #if !defined(WEBP_DISABLE_STATS)
1907   // Save size.
1908   if (picture->stats != NULL) {
1909     picture->stats->coded_size += (int)coded_size;
1910     picture->stats->lossless_size = (int)coded_size;
1911   }
1912 #endif
1913 
1914   if (picture->extra_info != NULL) {
1915     const int mb_w = (width + 15) >> 4;
1916     const int mb_h = (height + 15) >> 4;
1917     memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info));
1918   }
1919 
1920  Error:
1921   if (bw.error_) err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1922   VP8LBitWriterWipeOut(&bw);
1923   if (err != VP8_ENC_OK) {
1924     WebPEncodingSetError(picture, err);
1925     return 0;
1926   }
1927   return 1;
1928 }
1929 
1930 //------------------------------------------------------------------------------
1931