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