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