Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: third_party/libwebp/enc/vp8l.c

Issue 2651883004: libwebp-0.6.0-rc1 (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libwebp/enc/vp8i_enc.h ('k') | third_party/libwebp/enc/vp8l_enc.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "./backward_references.h"
19 #include "./histogram.h"
20 #include "./vp8enci.h"
21 #include "./vp8li.h"
22 #include "../dsp/lossless.h"
23 #include "../utils/bit_writer.h"
24 #include "../utils/huffman_encode.h"
25 #include "../utils/utils.h"
26 #include "../webp/format_constants.h"
27
28 #include "./delta_palettization.h"
29
30 #define PALETTE_KEY_RIGHT_SHIFT 22 // Key for 1K buffer.
31 // Maximum number of histogram images (sub-blocks).
32 #define MAX_HUFF_IMAGE_SIZE 2600
33
34 // Palette reordering for smaller sum of deltas (and for smaller storage).
35
36 static int PaletteCompareColorsForQsort(const void* p1, const void* p2) {
37 const uint32_t a = WebPMemToUint32((uint8_t*)p1);
38 const uint32_t b = WebPMemToUint32((uint8_t*)p2);
39 assert(a != b);
40 return (a < b) ? -1 : 1;
41 }
42
43 static WEBP_INLINE uint32_t PaletteComponentDistance(uint32_t v) {
44 return (v <= 128) ? v : (256 - v);
45 }
46
47 // Computes a value that is related to the entropy created by the
48 // palette entry diff.
49 //
50 // Note that the last & 0xff is a no-operation in the next statement, but
51 // removed by most compilers and is here only for regularity of the code.
52 static WEBP_INLINE uint32_t PaletteColorDistance(uint32_t col1, uint32_t col2) {
53 const uint32_t diff = VP8LSubPixels(col1, col2);
54 const int kMoreWeightForRGBThanForAlpha = 9;
55 uint32_t score;
56 score = PaletteComponentDistance((diff >> 0) & 0xff);
57 score += PaletteComponentDistance((diff >> 8) & 0xff);
58 score += PaletteComponentDistance((diff >> 16) & 0xff);
59 score *= kMoreWeightForRGBThanForAlpha;
60 score += PaletteComponentDistance((diff >> 24) & 0xff);
61 return score;
62 }
63
64 static WEBP_INLINE void SwapColor(uint32_t* const col1, uint32_t* const col2) {
65 const uint32_t tmp = *col1;
66 *col1 = *col2;
67 *col2 = tmp;
68 }
69
70 static void GreedyMinimizeDeltas(uint32_t palette[], int num_colors) {
71 // Find greedily always the closest color of the predicted color to minimize
72 // deltas in the palette. This reduces storage needs since the
73 // palette is stored with delta encoding.
74 uint32_t predict = 0x00000000;
75 int i, k;
76 for (i = 0; i < num_colors; ++i) {
77 int best_ix = i;
78 uint32_t best_score = ~0U;
79 for (k = i; k < num_colors; ++k) {
80 const uint32_t cur_score = PaletteColorDistance(palette[k], predict);
81 if (best_score > cur_score) {
82 best_score = cur_score;
83 best_ix = k;
84 }
85 }
86 SwapColor(&palette[best_ix], &palette[i]);
87 predict = palette[i];
88 }
89 }
90
91 // The palette has been sorted by alpha. This function checks if the other
92 // components of the palette have a monotonic development with regards to
93 // position in the palette. If all have monotonic development, there is
94 // no benefit to re-organize them greedily. A monotonic development
95 // would be spotted in green-only situations (like lossy alpha) or gray-scale
96 // images.
97 static int PaletteHasNonMonotonousDeltas(uint32_t palette[], int num_colors) {
98 uint32_t predict = 0x000000;
99 int i;
100 uint8_t sign_found = 0x00;
101 for (i = 0; i < num_colors; ++i) {
102 const uint32_t diff = VP8LSubPixels(palette[i], predict);
103 const uint8_t rd = (diff >> 16) & 0xff;
104 const uint8_t gd = (diff >> 8) & 0xff;
105 const uint8_t bd = (diff >> 0) & 0xff;
106 if (rd != 0x00) {
107 sign_found |= (rd < 0x80) ? 1 : 2;
108 }
109 if (gd != 0x00) {
110 sign_found |= (gd < 0x80) ? 8 : 16;
111 }
112 if (bd != 0x00) {
113 sign_found |= (bd < 0x80) ? 64 : 128;
114 }
115 predict = palette[i];
116 }
117 return (sign_found & (sign_found << 1)) != 0; // two consequent signs.
118 }
119
120 // -----------------------------------------------------------------------------
121 // Palette
122
123 // If number of colors in the image is less than or equal to MAX_PALETTE_SIZE,
124 // creates a palette and returns true, else returns false.
125 static int AnalyzeAndCreatePalette(const WebPPicture* const pic,
126 int low_effort,
127 uint32_t palette[MAX_PALETTE_SIZE],
128 int* const palette_size) {
129 const int num_colors = WebPGetColorPalette(pic, palette);
130 if (num_colors > MAX_PALETTE_SIZE) return 0;
131 *palette_size = num_colors;
132 qsort(palette, num_colors, sizeof(*palette), PaletteCompareColorsForQsort);
133 if (!low_effort && PaletteHasNonMonotonousDeltas(palette, num_colors)) {
134 GreedyMinimizeDeltas(palette, num_colors);
135 }
136 return 1;
137 }
138
139 // These five modes are evaluated and their respective entropy is computed.
140 typedef enum {
141 kDirect = 0,
142 kSpatial = 1,
143 kSubGreen = 2,
144 kSpatialSubGreen = 3,
145 kPalette = 4,
146 kNumEntropyIx = 5
147 } EntropyIx;
148
149 typedef enum {
150 kHistoAlpha = 0,
151 kHistoAlphaPred,
152 kHistoGreen,
153 kHistoGreenPred,
154 kHistoRed,
155 kHistoRedPred,
156 kHistoBlue,
157 kHistoBluePred,
158 kHistoRedSubGreen,
159 kHistoRedPredSubGreen,
160 kHistoBlueSubGreen,
161 kHistoBluePredSubGreen,
162 kHistoPalette,
163 kHistoTotal // Must be last.
164 } HistoIx;
165
166 static void AddSingleSubGreen(uint32_t p, uint32_t* r, uint32_t* b) {
167 const uint32_t green = p >> 8; // The upper bits are masked away later.
168 ++r[((p >> 16) - green) & 0xff];
169 ++b[(p - green) & 0xff];
170 }
171
172 static void AddSingle(uint32_t p,
173 uint32_t* a, uint32_t* r, uint32_t* g, uint32_t* b) {
174 ++a[p >> 24];
175 ++r[(p >> 16) & 0xff];
176 ++g[(p >> 8) & 0xff];
177 ++b[(p & 0xff)];
178 }
179
180 static int AnalyzeEntropy(const uint32_t* argb,
181 int width, int height, int argb_stride,
182 int use_palette,
183 EntropyIx* const min_entropy_ix,
184 int* const red_and_blue_always_zero) {
185 // Allocate histogram set with cache_bits = 0.
186 uint32_t* const histo =
187 (uint32_t*)WebPSafeCalloc(kHistoTotal, sizeof(*histo) * 256);
188 if (histo != NULL) {
189 int i, x, y;
190 const uint32_t* prev_row = argb;
191 const uint32_t* curr_row = argb + argb_stride;
192 for (y = 1; y < height; ++y) {
193 uint32_t prev_pix = curr_row[0];
194 for (x = 1; x < width; ++x) {
195 const uint32_t pix = curr_row[x];
196 const uint32_t pix_diff = VP8LSubPixels(pix, prev_pix);
197 if ((pix_diff == 0) || (pix == prev_row[x])) continue;
198 prev_pix = pix;
199 AddSingle(pix,
200 &histo[kHistoAlpha * 256],
201 &histo[kHistoRed * 256],
202 &histo[kHistoGreen * 256],
203 &histo[kHistoBlue * 256]);
204 AddSingle(pix_diff,
205 &histo[kHistoAlphaPred * 256],
206 &histo[kHistoRedPred * 256],
207 &histo[kHistoGreenPred * 256],
208 &histo[kHistoBluePred * 256]);
209 AddSingleSubGreen(pix,
210 &histo[kHistoRedSubGreen * 256],
211 &histo[kHistoBlueSubGreen * 256]);
212 AddSingleSubGreen(pix_diff,
213 &histo[kHistoRedPredSubGreen * 256],
214 &histo[kHistoBluePredSubGreen * 256]);
215 {
216 // Approximate the palette by the entropy of the multiplicative hash.
217 const int hash = ((pix + (pix >> 19)) * 0x39c5fba7) >> 24;
218 ++histo[kHistoPalette * 256 + (hash & 0xff)];
219 }
220 }
221 prev_row = curr_row;
222 curr_row += argb_stride;
223 }
224 {
225 double entropy_comp[kHistoTotal];
226 double entropy[kNumEntropyIx];
227 int k;
228 int last_mode_to_analyze = use_palette ? kPalette : kSpatialSubGreen;
229 int j;
230 // Let's add one zero to the predicted histograms. The zeros are removed
231 // too efficiently by the pix_diff == 0 comparison, at least one of the
232 // zeros is likely to exist.
233 ++histo[kHistoRedPredSubGreen * 256];
234 ++histo[kHistoBluePredSubGreen * 256];
235 ++histo[kHistoRedPred * 256];
236 ++histo[kHistoGreenPred * 256];
237 ++histo[kHistoBluePred * 256];
238 ++histo[kHistoAlphaPred * 256];
239
240 for (j = 0; j < kHistoTotal; ++j) {
241 entropy_comp[j] = VP8LBitsEntropy(&histo[j * 256], 256, NULL);
242 }
243 entropy[kDirect] = entropy_comp[kHistoAlpha] +
244 entropy_comp[kHistoRed] +
245 entropy_comp[kHistoGreen] +
246 entropy_comp[kHistoBlue];
247 entropy[kSpatial] = entropy_comp[kHistoAlphaPred] +
248 entropy_comp[kHistoRedPred] +
249 entropy_comp[kHistoGreenPred] +
250 entropy_comp[kHistoBluePred];
251 entropy[kSubGreen] = entropy_comp[kHistoAlpha] +
252 entropy_comp[kHistoRedSubGreen] +
253 entropy_comp[kHistoGreen] +
254 entropy_comp[kHistoBlueSubGreen];
255 entropy[kSpatialSubGreen] = entropy_comp[kHistoAlphaPred] +
256 entropy_comp[kHistoRedPredSubGreen] +
257 entropy_comp[kHistoGreenPred] +
258 entropy_comp[kHistoBluePredSubGreen];
259 // Palette mode seems more efficient in a breakeven case. Bias with 1.0.
260 entropy[kPalette] = entropy_comp[kHistoPalette] - 1.0;
261
262 *min_entropy_ix = kDirect;
263 for (k = kDirect + 1; k <= last_mode_to_analyze; ++k) {
264 if (entropy[*min_entropy_ix] > entropy[k]) {
265 *min_entropy_ix = (EntropyIx)k;
266 }
267 }
268 *red_and_blue_always_zero = 1;
269 // Let's check if the histogram of the chosen entropy mode has
270 // non-zero red and blue values. If all are zero, we can later skip
271 // the cross color optimization.
272 {
273 static const uint8_t kHistoPairs[5][2] = {
274 { kHistoRed, kHistoBlue },
275 { kHistoRedPred, kHistoBluePred },
276 { kHistoRedSubGreen, kHistoBlueSubGreen },
277 { kHistoRedPredSubGreen, kHistoBluePredSubGreen },
278 { kHistoRed, kHistoBlue }
279 };
280 const uint32_t* const red_histo =
281 &histo[256 * kHistoPairs[*min_entropy_ix][0]];
282 const uint32_t* const blue_histo =
283 &histo[256 * kHistoPairs[*min_entropy_ix][1]];
284 for (i = 1; i < 256; ++i) {
285 if ((red_histo[i] | blue_histo[i]) != 0) {
286 *red_and_blue_always_zero = 0;
287 break;
288 }
289 }
290 }
291 }
292 WebPSafeFree(histo);
293 return 1;
294 } else {
295 return 0;
296 }
297 }
298
299 static int GetHistoBits(int method, int use_palette, int width, int height) {
300 // Make tile size a function of encoding method (Range: 0 to 6).
301 int histo_bits = (use_palette ? 9 : 7) - method;
302 while (1) {
303 const int huff_image_size = VP8LSubSampleSize(width, histo_bits) *
304 VP8LSubSampleSize(height, histo_bits);
305 if (huff_image_size <= MAX_HUFF_IMAGE_SIZE) break;
306 ++histo_bits;
307 }
308 return (histo_bits < MIN_HUFFMAN_BITS) ? MIN_HUFFMAN_BITS :
309 (histo_bits > MAX_HUFFMAN_BITS) ? MAX_HUFFMAN_BITS : histo_bits;
310 }
311
312 static int GetTransformBits(int method, int histo_bits) {
313 const int max_transform_bits = (method < 4) ? 6 : (method > 4) ? 4 : 5;
314 return (histo_bits > max_transform_bits) ? max_transform_bits : histo_bits;
315 }
316
317 static int AnalyzeAndInit(VP8LEncoder* const enc) {
318 const WebPPicture* const pic = enc->pic_;
319 const int width = pic->width;
320 const int height = pic->height;
321 const int pix_cnt = width * height;
322 const WebPConfig* const config = enc->config_;
323 const int method = config->method;
324 const int low_effort = (config->method == 0);
325 // we round the block size up, so we're guaranteed to have
326 // at max MAX_REFS_BLOCK_PER_IMAGE blocks used:
327 int refs_block_size = (pix_cnt - 1) / MAX_REFS_BLOCK_PER_IMAGE + 1;
328 assert(pic != NULL && pic->argb != NULL);
329
330 enc->use_cross_color_ = 0;
331 enc->use_predict_ = 0;
332 enc->use_subtract_green_ = 0;
333 enc->use_palette_ =
334 AnalyzeAndCreatePalette(pic, low_effort,
335 enc->palette_, &enc->palette_size_);
336
337 // TODO(jyrki): replace the decision to be based on an actual estimate
338 // of entropy, or even spatial variance of entropy.
339 enc->histo_bits_ = GetHistoBits(method, enc->use_palette_,
340 pic->width, pic->height);
341 enc->transform_bits_ = GetTransformBits(method, enc->histo_bits_);
342
343 if (low_effort) {
344 // AnalyzeEntropy is somewhat slow.
345 enc->use_predict_ = !enc->use_palette_;
346 enc->use_subtract_green_ = !enc->use_palette_;
347 enc->use_cross_color_ = 0;
348 } else {
349 int red_and_blue_always_zero;
350 EntropyIx min_entropy_ix;
351 if (!AnalyzeEntropy(pic->argb, width, height, pic->argb_stride,
352 enc->use_palette_, &min_entropy_ix,
353 &red_and_blue_always_zero)) {
354 return 0;
355 }
356 enc->use_palette_ = (min_entropy_ix == kPalette);
357 enc->use_subtract_green_ =
358 (min_entropy_ix == kSubGreen) || (min_entropy_ix == kSpatialSubGreen);
359 enc->use_predict_ =
360 (min_entropy_ix == kSpatial) || (min_entropy_ix == kSpatialSubGreen);
361 enc->use_cross_color_ = red_and_blue_always_zero ? 0 : enc->use_predict_;
362 }
363
364 if (!VP8LHashChainInit(&enc->hash_chain_, pix_cnt)) return 0;
365
366 // palette-friendly input typically uses less literals
367 // -> reduce block size a bit
368 if (enc->use_palette_) refs_block_size /= 2;
369 VP8LBackwardRefsInit(&enc->refs_[0], refs_block_size);
370 VP8LBackwardRefsInit(&enc->refs_[1], refs_block_size);
371
372 return 1;
373 }
374
375 // Returns false in case of memory error.
376 static int GetHuffBitLengthsAndCodes(
377 const VP8LHistogramSet* const histogram_image,
378 HuffmanTreeCode* const huffman_codes) {
379 int i, k;
380 int ok = 0;
381 uint64_t total_length_size = 0;
382 uint8_t* mem_buf = NULL;
383 const int histogram_image_size = histogram_image->size;
384 int max_num_symbols = 0;
385 uint8_t* buf_rle = NULL;
386 HuffmanTree* huff_tree = NULL;
387
388 // Iterate over all histograms and get the aggregate number of codes used.
389 for (i = 0; i < histogram_image_size; ++i) {
390 const VP8LHistogram* const histo = histogram_image->histograms[i];
391 HuffmanTreeCode* const codes = &huffman_codes[5 * i];
392 for (k = 0; k < 5; ++k) {
393 const int num_symbols =
394 (k == 0) ? VP8LHistogramNumCodes(histo->palette_code_bits_) :
395 (k == 4) ? NUM_DISTANCE_CODES : 256;
396 codes[k].num_symbols = num_symbols;
397 total_length_size += num_symbols;
398 }
399 }
400
401 // Allocate and Set Huffman codes.
402 {
403 uint16_t* codes;
404 uint8_t* lengths;
405 mem_buf = (uint8_t*)WebPSafeCalloc(total_length_size,
406 sizeof(*lengths) + sizeof(*codes));
407 if (mem_buf == NULL) goto End;
408
409 codes = (uint16_t*)mem_buf;
410 lengths = (uint8_t*)&codes[total_length_size];
411 for (i = 0; i < 5 * histogram_image_size; ++i) {
412 const int bit_length = huffman_codes[i].num_symbols;
413 huffman_codes[i].codes = codes;
414 huffman_codes[i].code_lengths = lengths;
415 codes += bit_length;
416 lengths += bit_length;
417 if (max_num_symbols < bit_length) {
418 max_num_symbols = bit_length;
419 }
420 }
421 }
422
423 buf_rle = (uint8_t*)WebPSafeMalloc(1ULL, max_num_symbols);
424 huff_tree = (HuffmanTree*)WebPSafeMalloc(3ULL * max_num_symbols,
425 sizeof(*huff_tree));
426 if (buf_rle == NULL || huff_tree == NULL) goto End;
427
428 // Create Huffman trees.
429 for (i = 0; i < histogram_image_size; ++i) {
430 HuffmanTreeCode* const codes = &huffman_codes[5 * i];
431 VP8LHistogram* const histo = histogram_image->histograms[i];
432 VP8LCreateHuffmanTree(histo->literal_, 15, buf_rle, huff_tree, codes + 0);
433 VP8LCreateHuffmanTree(histo->red_, 15, buf_rle, huff_tree, codes + 1);
434 VP8LCreateHuffmanTree(histo->blue_, 15, buf_rle, huff_tree, codes + 2);
435 VP8LCreateHuffmanTree(histo->alpha_, 15, buf_rle, huff_tree, codes + 3);
436 VP8LCreateHuffmanTree(histo->distance_, 15, buf_rle, huff_tree, codes + 4);
437 }
438 ok = 1;
439 End:
440 WebPSafeFree(huff_tree);
441 WebPSafeFree(buf_rle);
442 if (!ok) {
443 WebPSafeFree(mem_buf);
444 memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes));
445 }
446 return ok;
447 }
448
449 static void StoreHuffmanTreeOfHuffmanTreeToBitMask(
450 VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) {
451 // RFC 1951 will calm you down if you are worried about this funny sequence.
452 // This sequence is tuned from that, but more weighted for lower symbol count,
453 // and more spiking histograms.
454 static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = {
455 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
456 };
457 int i;
458 // Throw away trailing zeros:
459 int codes_to_store = CODE_LENGTH_CODES;
460 for (; codes_to_store > 4; --codes_to_store) {
461 if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
462 break;
463 }
464 }
465 VP8LPutBits(bw, codes_to_store - 4, 4);
466 for (i = 0; i < codes_to_store; ++i) {
467 VP8LPutBits(bw, code_length_bitdepth[kStorageOrder[i]], 3);
468 }
469 }
470
471 static void ClearHuffmanTreeIfOnlyOneSymbol(
472 HuffmanTreeCode* const huffman_code) {
473 int k;
474 int count = 0;
475 for (k = 0; k < huffman_code->num_symbols; ++k) {
476 if (huffman_code->code_lengths[k] != 0) {
477 ++count;
478 if (count > 1) return;
479 }
480 }
481 for (k = 0; k < huffman_code->num_symbols; ++k) {
482 huffman_code->code_lengths[k] = 0;
483 huffman_code->codes[k] = 0;
484 }
485 }
486
487 static void StoreHuffmanTreeToBitMask(
488 VP8LBitWriter* const bw,
489 const HuffmanTreeToken* const tokens, const int num_tokens,
490 const HuffmanTreeCode* const huffman_code) {
491 int i;
492 for (i = 0; i < num_tokens; ++i) {
493 const int ix = tokens[i].code;
494 const int extra_bits = tokens[i].extra_bits;
495 VP8LPutBits(bw, huffman_code->codes[ix], huffman_code->code_lengths[ix]);
496 switch (ix) {
497 case 16:
498 VP8LPutBits(bw, extra_bits, 2);
499 break;
500 case 17:
501 VP8LPutBits(bw, extra_bits, 3);
502 break;
503 case 18:
504 VP8LPutBits(bw, extra_bits, 7);
505 break;
506 }
507 }
508 }
509
510 // 'huff_tree' and 'tokens' are pre-alloacted buffers.
511 static void StoreFullHuffmanCode(VP8LBitWriter* const bw,
512 HuffmanTree* const huff_tree,
513 HuffmanTreeToken* const tokens,
514 const HuffmanTreeCode* const tree) {
515 uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 };
516 uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 };
517 const int max_tokens = tree->num_symbols;
518 int num_tokens;
519 HuffmanTreeCode huffman_code;
520 huffman_code.num_symbols = CODE_LENGTH_CODES;
521 huffman_code.code_lengths = code_length_bitdepth;
522 huffman_code.codes = code_length_bitdepth_symbols;
523
524 VP8LPutBits(bw, 0, 1);
525 num_tokens = VP8LCreateCompressedHuffmanTree(tree, tokens, max_tokens);
526 {
527 uint32_t histogram[CODE_LENGTH_CODES] = { 0 };
528 uint8_t buf_rle[CODE_LENGTH_CODES] = { 0 };
529 int i;
530 for (i = 0; i < num_tokens; ++i) {
531 ++histogram[tokens[i].code];
532 }
533
534 VP8LCreateHuffmanTree(histogram, 7, buf_rle, huff_tree, &huffman_code);
535 }
536
537 StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth);
538 ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code);
539 {
540 int trailing_zero_bits = 0;
541 int trimmed_length = num_tokens;
542 int write_trimmed_length;
543 int length;
544 int i = num_tokens;
545 while (i-- > 0) {
546 const int ix = tokens[i].code;
547 if (ix == 0 || ix == 17 || ix == 18) {
548 --trimmed_length; // discount trailing zeros
549 trailing_zero_bits += code_length_bitdepth[ix];
550 if (ix == 17) {
551 trailing_zero_bits += 3;
552 } else if (ix == 18) {
553 trailing_zero_bits += 7;
554 }
555 } else {
556 break;
557 }
558 }
559 write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12);
560 length = write_trimmed_length ? trimmed_length : num_tokens;
561 VP8LPutBits(bw, write_trimmed_length, 1);
562 if (write_trimmed_length) {
563 const int nbits = VP8LBitsLog2Ceiling(trimmed_length - 1);
564 const int nbitpairs = (nbits == 0) ? 1 : (nbits + 1) / 2;
565 VP8LPutBits(bw, nbitpairs - 1, 3);
566 assert(trimmed_length >= 2);
567 VP8LPutBits(bw, trimmed_length - 2, nbitpairs * 2);
568 }
569 StoreHuffmanTreeToBitMask(bw, tokens, length, &huffman_code);
570 }
571 }
572
573 // 'huff_tree' and 'tokens' are pre-alloacted buffers.
574 static void StoreHuffmanCode(VP8LBitWriter* const bw,
575 HuffmanTree* const huff_tree,
576 HuffmanTreeToken* const tokens,
577 const HuffmanTreeCode* const huffman_code) {
578 int i;
579 int count = 0;
580 int symbols[2] = { 0, 0 };
581 const int kMaxBits = 8;
582 const int kMaxSymbol = 1 << kMaxBits;
583
584 // Check whether it's a small tree.
585 for (i = 0; i < huffman_code->num_symbols && count < 3; ++i) {
586 if (huffman_code->code_lengths[i] != 0) {
587 if (count < 2) symbols[count] = i;
588 ++count;
589 }
590 }
591
592 if (count == 0) { // emit minimal tree for empty cases
593 // bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0
594 VP8LPutBits(bw, 0x01, 4);
595 } else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) {
596 VP8LPutBits(bw, 1, 1); // Small tree marker to encode 1 or 2 symbols.
597 VP8LPutBits(bw, count - 1, 1);
598 if (symbols[0] <= 1) {
599 VP8LPutBits(bw, 0, 1); // Code bit for small (1 bit) symbol value.
600 VP8LPutBits(bw, symbols[0], 1);
601 } else {
602 VP8LPutBits(bw, 1, 1);
603 VP8LPutBits(bw, symbols[0], 8);
604 }
605 if (count == 2) {
606 VP8LPutBits(bw, symbols[1], 8);
607 }
608 } else {
609 StoreFullHuffmanCode(bw, huff_tree, tokens, huffman_code);
610 }
611 }
612
613 static WEBP_INLINE void WriteHuffmanCode(VP8LBitWriter* const bw,
614 const HuffmanTreeCode* const code,
615 int code_index) {
616 const int depth = code->code_lengths[code_index];
617 const int symbol = code->codes[code_index];
618 VP8LPutBits(bw, symbol, depth);
619 }
620
621 static WEBP_INLINE void WriteHuffmanCodeWithExtraBits(
622 VP8LBitWriter* const bw,
623 const HuffmanTreeCode* const code,
624 int code_index,
625 int bits,
626 int n_bits) {
627 const int depth = code->code_lengths[code_index];
628 const int symbol = code->codes[code_index];
629 VP8LPutBits(bw, (bits << depth) | symbol, depth + n_bits);
630 }
631
632 static WebPEncodingError StoreImageToBitMask(
633 VP8LBitWriter* const bw, int width, int histo_bits,
634 VP8LBackwardRefs* const refs,
635 const uint16_t* histogram_symbols,
636 const HuffmanTreeCode* const huffman_codes) {
637 const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1;
638 const int tile_mask = (histo_bits == 0) ? 0 : -(1 << histo_bits);
639 // x and y trace the position in the image.
640 int x = 0;
641 int y = 0;
642 int tile_x = x & tile_mask;
643 int tile_y = y & tile_mask;
644 int histogram_ix = histogram_symbols[0];
645 const HuffmanTreeCode* codes = huffman_codes + 5 * histogram_ix;
646 VP8LRefsCursor c = VP8LRefsCursorInit(refs);
647 while (VP8LRefsCursorOk(&c)) {
648 const PixOrCopy* const v = c.cur_pos;
649 if ((tile_x != (x & tile_mask)) || (tile_y != (y & tile_mask))) {
650 tile_x = x & tile_mask;
651 tile_y = y & tile_mask;
652 histogram_ix = histogram_symbols[(y >> histo_bits) * histo_xsize +
653 (x >> histo_bits)];
654 codes = huffman_codes + 5 * histogram_ix;
655 }
656 if (PixOrCopyIsLiteral(v)) {
657 static const int order[] = { 1, 2, 0, 3 };
658 int k;
659 for (k = 0; k < 4; ++k) {
660 const int code = PixOrCopyLiteral(v, order[k]);
661 WriteHuffmanCode(bw, codes + k, code);
662 }
663 } else if (PixOrCopyIsCacheIdx(v)) {
664 const int code = PixOrCopyCacheIdx(v);
665 const int literal_ix = 256 + NUM_LENGTH_CODES + code;
666 WriteHuffmanCode(bw, codes, literal_ix);
667 } else {
668 int bits, n_bits;
669 int code;
670
671 const int distance = PixOrCopyDistance(v);
672 VP8LPrefixEncode(v->len, &code, &n_bits, &bits);
673 WriteHuffmanCodeWithExtraBits(bw, codes, 256 + code, bits, n_bits);
674
675 // Don't write the distance with the extra bits code since
676 // the distance can be up to 18 bits of extra bits, and the prefix
677 // 15 bits, totaling to 33, and our PutBits only supports up to 32 bits.
678 // TODO(jyrki): optimize this further.
679 VP8LPrefixEncode(distance, &code, &n_bits, &bits);
680 WriteHuffmanCode(bw, codes + 4, code);
681 VP8LPutBits(bw, bits, n_bits);
682 }
683 x += PixOrCopyLength(v);
684 while (x >= width) {
685 x -= width;
686 ++y;
687 }
688 VP8LRefsCursorNext(&c);
689 }
690 return bw->error_ ? VP8_ENC_ERROR_OUT_OF_MEMORY : VP8_ENC_OK;
691 }
692
693 // Special case of EncodeImageInternal() for cache-bits=0, histo_bits=31
694 static WebPEncodingError EncodeImageNoHuffman(VP8LBitWriter* const bw,
695 const uint32_t* const argb,
696 VP8LHashChain* const hash_chain,
697 VP8LBackwardRefs refs_array[2],
698 int width, int height,
699 int quality) {
700 int i;
701 int max_tokens = 0;
702 WebPEncodingError err = VP8_ENC_OK;
703 VP8LBackwardRefs* refs;
704 HuffmanTreeToken* tokens = NULL;
705 HuffmanTreeCode huffman_codes[5] = { { 0, NULL, NULL } };
706 const uint16_t histogram_symbols[1] = { 0 }; // only one tree, one symbol
707 int cache_bits = 0;
708 VP8LHistogramSet* histogram_image = NULL;
709 HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc(
710 3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree));
711 if (huff_tree == NULL) {
712 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
713 goto Error;
714 }
715
716 // Calculate backward references from ARGB image.
717 if (VP8LHashChainFill(hash_chain, quality, argb, width, height) == 0) {
718 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
719 goto Error;
720 }
721 refs = VP8LGetBackwardReferences(width, height, argb, quality, 0, &cache_bits,
722 hash_chain, refs_array);
723 if (refs == NULL) {
724 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
725 goto Error;
726 }
727 histogram_image = VP8LAllocateHistogramSet(1, cache_bits);
728 if (histogram_image == NULL) {
729 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
730 goto Error;
731 }
732
733 // Build histogram image and symbols from backward references.
734 VP8LHistogramStoreRefs(refs, histogram_image->histograms[0]);
735
736 // Create Huffman bit lengths and codes for each histogram image.
737 assert(histogram_image->size == 1);
738 if (!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
739 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
740 goto Error;
741 }
742
743 // No color cache, no Huffman image.
744 VP8LPutBits(bw, 0, 1);
745
746 // Find maximum number of symbols for the huffman tree-set.
747 for (i = 0; i < 5; ++i) {
748 HuffmanTreeCode* const codes = &huffman_codes[i];
749 if (max_tokens < codes->num_symbols) {
750 max_tokens = codes->num_symbols;
751 }
752 }
753
754 tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens));
755 if (tokens == NULL) {
756 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
757 goto Error;
758 }
759
760 // Store Huffman codes.
761 for (i = 0; i < 5; ++i) {
762 HuffmanTreeCode* const codes = &huffman_codes[i];
763 StoreHuffmanCode(bw, huff_tree, tokens, codes);
764 ClearHuffmanTreeIfOnlyOneSymbol(codes);
765 }
766
767 // Store actual literals.
768 err = StoreImageToBitMask(bw, width, 0, refs, histogram_symbols,
769 huffman_codes);
770
771 Error:
772 WebPSafeFree(tokens);
773 WebPSafeFree(huff_tree);
774 VP8LFreeHistogramSet(histogram_image);
775 WebPSafeFree(huffman_codes[0].codes);
776 return err;
777 }
778
779 static WebPEncodingError EncodeImageInternal(VP8LBitWriter* const bw,
780 const uint32_t* const argb,
781 VP8LHashChain* const hash_chain,
782 VP8LBackwardRefs refs_array[2],
783 int width, int height, int quality,
784 int low_effort,
785 int use_cache, int* cache_bits,
786 int histogram_bits,
787 size_t init_byte_position,
788 int* const hdr_size,
789 int* const data_size) {
790 WebPEncodingError err = VP8_ENC_OK;
791 const uint32_t histogram_image_xysize =
792 VP8LSubSampleSize(width, histogram_bits) *
793 VP8LSubSampleSize(height, histogram_bits);
794 VP8LHistogramSet* histogram_image = NULL;
795 VP8LHistogramSet* tmp_histos = NULL;
796 int histogram_image_size = 0;
797 size_t bit_array_size = 0;
798 HuffmanTree* huff_tree = NULL;
799 HuffmanTreeToken* tokens = NULL;
800 HuffmanTreeCode* huffman_codes = NULL;
801 VP8LBackwardRefs refs;
802 VP8LBackwardRefs* best_refs;
803 uint16_t* const histogram_symbols =
804 (uint16_t*)WebPSafeMalloc(histogram_image_xysize,
805 sizeof(*histogram_symbols));
806 assert(histogram_bits >= MIN_HUFFMAN_BITS);
807 assert(histogram_bits <= MAX_HUFFMAN_BITS);
808 assert(hdr_size != NULL);
809 assert(data_size != NULL);
810
811 VP8LBackwardRefsInit(&refs, refs_array[0].block_size_);
812 if (histogram_symbols == NULL) {
813 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
814 goto Error;
815 }
816
817 *cache_bits = use_cache ? MAX_COLOR_CACHE_BITS : 0;
818 // 'best_refs' is the reference to the best backward refs and points to one
819 // of refs_array[0] or refs_array[1].
820 // Calculate backward references from ARGB image.
821 if (VP8LHashChainFill(hash_chain, quality, argb, width, height) == 0) {
822 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
823 goto Error;
824 }
825 best_refs = VP8LGetBackwardReferences(width, height, argb, quality,
826 low_effort, cache_bits, hash_chain,
827 refs_array);
828 if (best_refs == NULL || !VP8LBackwardRefsCopy(best_refs, &refs)) {
829 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
830 goto Error;
831 }
832 histogram_image =
833 VP8LAllocateHistogramSet(histogram_image_xysize, *cache_bits);
834 tmp_histos = VP8LAllocateHistogramSet(2, *cache_bits);
835 if (histogram_image == NULL || tmp_histos == NULL) {
836 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
837 goto Error;
838 }
839
840 // Build histogram image and symbols from backward references.
841 if (!VP8LGetHistoImageSymbols(width, height, &refs, quality, low_effort,
842 histogram_bits, *cache_bits, histogram_image,
843 tmp_histos, histogram_symbols)) {
844 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
845 goto Error;
846 }
847 // Create Huffman bit lengths and codes for each histogram image.
848 histogram_image_size = histogram_image->size;
849 bit_array_size = 5 * histogram_image_size;
850 huffman_codes = (HuffmanTreeCode*)WebPSafeCalloc(bit_array_size,
851 sizeof(*huffman_codes));
852 // Note: some histogram_image entries may point to tmp_histos[], so the latter
853 // need to outlive the following call to GetHuffBitLengthsAndCodes().
854 if (huffman_codes == NULL ||
855 !GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
856 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
857 goto Error;
858 }
859 // Free combined histograms.
860 VP8LFreeHistogramSet(histogram_image);
861 histogram_image = NULL;
862
863 // Free scratch histograms.
864 VP8LFreeHistogramSet(tmp_histos);
865 tmp_histos = NULL;
866
867 // Color Cache parameters.
868 if (*cache_bits > 0) {
869 VP8LPutBits(bw, 1, 1);
870 VP8LPutBits(bw, *cache_bits, 4);
871 } else {
872 VP8LPutBits(bw, 0, 1);
873 }
874
875 // Huffman image + meta huffman.
876 {
877 const int write_histogram_image = (histogram_image_size > 1);
878 VP8LPutBits(bw, write_histogram_image, 1);
879 if (write_histogram_image) {
880 uint32_t* const histogram_argb =
881 (uint32_t*)WebPSafeMalloc(histogram_image_xysize,
882 sizeof(*histogram_argb));
883 int max_index = 0;
884 uint32_t i;
885 if (histogram_argb == NULL) {
886 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
887 goto Error;
888 }
889 for (i = 0; i < histogram_image_xysize; ++i) {
890 const int symbol_index = histogram_symbols[i] & 0xffff;
891 histogram_argb[i] = (symbol_index << 8);
892 if (symbol_index >= max_index) {
893 max_index = symbol_index + 1;
894 }
895 }
896 histogram_image_size = max_index;
897
898 VP8LPutBits(bw, histogram_bits - 2, 3);
899 err = EncodeImageNoHuffman(bw, histogram_argb, hash_chain, refs_array,
900 VP8LSubSampleSize(width, histogram_bits),
901 VP8LSubSampleSize(height, histogram_bits),
902 quality);
903 WebPSafeFree(histogram_argb);
904 if (err != VP8_ENC_OK) goto Error;
905 }
906 }
907
908 // Store Huffman codes.
909 {
910 int i;
911 int max_tokens = 0;
912 huff_tree = (HuffmanTree*)WebPSafeMalloc(3ULL * CODE_LENGTH_CODES,
913 sizeof(*huff_tree));
914 if (huff_tree == NULL) {
915 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
916 goto Error;
917 }
918 // Find maximum number of symbols for the huffman tree-set.
919 for (i = 0; i < 5 * histogram_image_size; ++i) {
920 HuffmanTreeCode* const codes = &huffman_codes[i];
921 if (max_tokens < codes->num_symbols) {
922 max_tokens = codes->num_symbols;
923 }
924 }
925 tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens,
926 sizeof(*tokens));
927 if (tokens == NULL) {
928 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
929 goto Error;
930 }
931 for (i = 0; i < 5 * histogram_image_size; ++i) {
932 HuffmanTreeCode* const codes = &huffman_codes[i];
933 StoreHuffmanCode(bw, huff_tree, tokens, codes);
934 ClearHuffmanTreeIfOnlyOneSymbol(codes);
935 }
936 }
937
938 *hdr_size = (int)(VP8LBitWriterNumBytes(bw) - init_byte_position);
939 // Store actual literals.
940 err = StoreImageToBitMask(bw, width, histogram_bits, &refs,
941 histogram_symbols, huffman_codes);
942 *data_size =
943 (int)(VP8LBitWriterNumBytes(bw) - init_byte_position - *hdr_size);
944
945 Error:
946 WebPSafeFree(tokens);
947 WebPSafeFree(huff_tree);
948 VP8LFreeHistogramSet(histogram_image);
949 VP8LFreeHistogramSet(tmp_histos);
950 VP8LBackwardRefsClear(&refs);
951 if (huffman_codes != NULL) {
952 WebPSafeFree(huffman_codes->codes);
953 WebPSafeFree(huffman_codes);
954 }
955 WebPSafeFree(histogram_symbols);
956 return err;
957 }
958
959 // -----------------------------------------------------------------------------
960 // Transforms
961
962 static void ApplySubtractGreen(VP8LEncoder* const enc, int width, int height,
963 VP8LBitWriter* const bw) {
964 VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
965 VP8LPutBits(bw, SUBTRACT_GREEN, 2);
966 VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height);
967 }
968
969 static WebPEncodingError ApplyPredictFilter(const VP8LEncoder* const enc,
970 int width, int height,
971 int quality, int low_effort,
972 int used_subtract_green,
973 VP8LBitWriter* const bw) {
974 const int pred_bits = enc->transform_bits_;
975 const int transform_width = VP8LSubSampleSize(width, pred_bits);
976 const int transform_height = VP8LSubSampleSize(height, pred_bits);
977 // we disable near-lossless quantization if palette is used.
978 const int near_lossless_strength = enc->use_palette_ ? 100
979 : enc->config_->near_lossless;
980
981 VP8LResidualImage(width, height, pred_bits, low_effort, enc->argb_,
982 enc->argb_scratch_, enc->transform_data_,
983 near_lossless_strength, enc->config_->exact,
984 used_subtract_green);
985 VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
986 VP8LPutBits(bw, PREDICTOR_TRANSFORM, 2);
987 assert(pred_bits >= 2);
988 VP8LPutBits(bw, pred_bits - 2, 3);
989 return EncodeImageNoHuffman(bw, enc->transform_data_,
990 (VP8LHashChain*)&enc->hash_chain_,
991 (VP8LBackwardRefs*)enc->refs_, // cast const away
992 transform_width, transform_height,
993 quality);
994 }
995
996 static WebPEncodingError ApplyCrossColorFilter(const VP8LEncoder* const enc,
997 int width, int height,
998 int quality,
999 VP8LBitWriter* const bw) {
1000 const int ccolor_transform_bits = enc->transform_bits_;
1001 const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits);
1002 const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits);
1003
1004 VP8LColorSpaceTransform(width, height, ccolor_transform_bits, quality,
1005 enc->argb_, enc->transform_data_);
1006 VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1007 VP8LPutBits(bw, CROSS_COLOR_TRANSFORM, 2);
1008 assert(ccolor_transform_bits >= 2);
1009 VP8LPutBits(bw, ccolor_transform_bits - 2, 3);
1010 return EncodeImageNoHuffman(bw, enc->transform_data_,
1011 (VP8LHashChain*)&enc->hash_chain_,
1012 (VP8LBackwardRefs*)enc->refs_, // cast const away
1013 transform_width, transform_height,
1014 quality);
1015 }
1016
1017 // -----------------------------------------------------------------------------
1018
1019 static WebPEncodingError WriteRiffHeader(const WebPPicture* const pic,
1020 size_t riff_size, size_t vp8l_size) {
1021 uint8_t riff[RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + VP8L_SIGNATURE_SIZE] = {
1022 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P',
1023 'V', 'P', '8', 'L', 0, 0, 0, 0, VP8L_MAGIC_BYTE,
1024 };
1025 PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
1026 PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size);
1027 if (!pic->writer(riff, sizeof(riff), pic)) {
1028 return VP8_ENC_ERROR_BAD_WRITE;
1029 }
1030 return VP8_ENC_OK;
1031 }
1032
1033 static int WriteImageSize(const WebPPicture* const pic,
1034 VP8LBitWriter* const bw) {
1035 const int width = pic->width - 1;
1036 const int height = pic->height - 1;
1037 assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
1038
1039 VP8LPutBits(bw, width, VP8L_IMAGE_SIZE_BITS);
1040 VP8LPutBits(bw, height, VP8L_IMAGE_SIZE_BITS);
1041 return !bw->error_;
1042 }
1043
1044 static int WriteRealAlphaAndVersion(VP8LBitWriter* const bw, int has_alpha) {
1045 VP8LPutBits(bw, has_alpha, 1);
1046 VP8LPutBits(bw, VP8L_VERSION, VP8L_VERSION_BITS);
1047 return !bw->error_;
1048 }
1049
1050 static WebPEncodingError WriteImage(const WebPPicture* const pic,
1051 VP8LBitWriter* const bw,
1052 size_t* const coded_size) {
1053 WebPEncodingError err = VP8_ENC_OK;
1054 const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
1055 const size_t webpll_size = VP8LBitWriterNumBytes(bw);
1056 const size_t vp8l_size = VP8L_SIGNATURE_SIZE + webpll_size;
1057 const size_t pad = vp8l_size & 1;
1058 const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad;
1059
1060 err = WriteRiffHeader(pic, riff_size, vp8l_size);
1061 if (err != VP8_ENC_OK) goto Error;
1062
1063 if (!pic->writer(webpll_data, webpll_size, pic)) {
1064 err = VP8_ENC_ERROR_BAD_WRITE;
1065 goto Error;
1066 }
1067
1068 if (pad) {
1069 const uint8_t pad_byte[1] = { 0 };
1070 if (!pic->writer(pad_byte, 1, pic)) {
1071 err = VP8_ENC_ERROR_BAD_WRITE;
1072 goto Error;
1073 }
1074 }
1075 *coded_size = CHUNK_HEADER_SIZE + riff_size;
1076 return VP8_ENC_OK;
1077
1078 Error:
1079 return err;
1080 }
1081
1082 // -----------------------------------------------------------------------------
1083
1084 static void ClearTransformBuffer(VP8LEncoder* const enc) {
1085 WebPSafeFree(enc->transform_mem_);
1086 enc->transform_mem_ = NULL;
1087 enc->transform_mem_size_ = 0;
1088 }
1089
1090 // Allocates the memory for argb (W x H) buffer, 2 rows of context for
1091 // prediction and transform data.
1092 // Flags influencing the memory allocated:
1093 // enc->transform_bits_
1094 // enc->use_predict_, enc->use_cross_color_
1095 static WebPEncodingError AllocateTransformBuffer(VP8LEncoder* const enc,
1096 int width, int height) {
1097 WebPEncodingError err = VP8_ENC_OK;
1098 const uint64_t image_size = width * height;
1099 // VP8LResidualImage needs room for 2 scanlines of uint32 pixels with an extra
1100 // pixel in each, plus 2 regular scanlines of bytes.
1101 // TODO(skal): Clean up by using arithmetic in bytes instead of words.
1102 const uint64_t argb_scratch_size =
1103 enc->use_predict_
1104 ? (width + 1) * 2 +
1105 (width * 2 + sizeof(uint32_t) - 1) / sizeof(uint32_t)
1106 : 0;
1107 const uint64_t transform_data_size =
1108 (enc->use_predict_ || enc->use_cross_color_)
1109 ? VP8LSubSampleSize(width, enc->transform_bits_) *
1110 VP8LSubSampleSize(height, enc->transform_bits_)
1111 : 0;
1112 const uint64_t max_alignment_in_words =
1113 (WEBP_ALIGN_CST + sizeof(uint32_t) - 1) / sizeof(uint32_t);
1114 const uint64_t mem_size =
1115 image_size + max_alignment_in_words +
1116 argb_scratch_size + max_alignment_in_words +
1117 transform_data_size;
1118 uint32_t* mem = enc->transform_mem_;
1119 if (mem == NULL || mem_size > enc->transform_mem_size_) {
1120 ClearTransformBuffer(enc);
1121 mem = (uint32_t*)WebPSafeMalloc(mem_size, sizeof(*mem));
1122 if (mem == NULL) {
1123 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1124 goto Error;
1125 }
1126 enc->transform_mem_ = mem;
1127 enc->transform_mem_size_ = (size_t)mem_size;
1128 }
1129 enc->argb_ = mem;
1130 mem = (uint32_t*)WEBP_ALIGN(mem + image_size);
1131 enc->argb_scratch_ = mem;
1132 mem = (uint32_t*)WEBP_ALIGN(mem + argb_scratch_size);
1133 enc->transform_data_ = mem;
1134
1135 enc->current_width_ = width;
1136 Error:
1137 return err;
1138 }
1139
1140 static WebPEncodingError MakeInputImageCopy(VP8LEncoder* const enc) {
1141 WebPEncodingError err = VP8_ENC_OK;
1142 const WebPPicture* const picture = enc->pic_;
1143 const int width = picture->width;
1144 const int height = picture->height;
1145 int y;
1146 err = AllocateTransformBuffer(enc, width, height);
1147 if (err != VP8_ENC_OK) return err;
1148 for (y = 0; y < height; ++y) {
1149 memcpy(enc->argb_ + y * width,
1150 picture->argb + y * picture->argb_stride,
1151 width * sizeof(*enc->argb_));
1152 }
1153 assert(enc->current_width_ == width);
1154 return VP8_ENC_OK;
1155 }
1156
1157 // -----------------------------------------------------------------------------
1158
1159 static int SearchColor(const uint32_t sorted[], uint32_t color, int hi) {
1160 int low = 0;
1161 if (sorted[low] == color) return low; // loop invariant: sorted[low] != color
1162 while (1) {
1163 const int mid = (low + hi) >> 1;
1164 if (sorted[mid] == color) {
1165 return mid;
1166 } else if (sorted[mid] < color) {
1167 low = mid;
1168 } else {
1169 hi = mid;
1170 }
1171 }
1172 }
1173
1174 // Sort palette in increasing order and prepare an inverse mapping array.
1175 static void PrepareMapToPalette(const uint32_t palette[], int num_colors,
1176 uint32_t sorted[], int idx_map[]) {
1177 int i;
1178 memcpy(sorted, palette, num_colors * sizeof(*sorted));
1179 qsort(sorted, num_colors, sizeof(*sorted), PaletteCompareColorsForQsort);
1180 for (i = 0; i < num_colors; ++i) {
1181 idx_map[SearchColor(sorted, palette[i], num_colors)] = i;
1182 }
1183 }
1184
1185 static void MapToPalette(const uint32_t sorted_palette[], int num_colors,
1186 uint32_t* const last_pix, int* const last_idx,
1187 const int idx_map[],
1188 const uint32_t* src, uint8_t* dst, int width) {
1189 int x;
1190 int prev_idx = *last_idx;
1191 uint32_t prev_pix = *last_pix;
1192 for (x = 0; x < width; ++x) {
1193 const uint32_t pix = src[x];
1194 if (pix != prev_pix) {
1195 prev_idx = idx_map[SearchColor(sorted_palette, pix, num_colors)];
1196 prev_pix = pix;
1197 }
1198 dst[x] = prev_idx;
1199 }
1200 *last_idx = prev_idx;
1201 *last_pix = prev_pix;
1202 }
1203
1204 // Remap argb values in src[] to packed palettes entries in dst[]
1205 // using 'row' as a temporary buffer of size 'width'.
1206 // We assume that all src[] values have a corresponding entry in the palette.
1207 // Note: src[] can be the same as dst[]
1208 static WebPEncodingError ApplyPalette(const uint32_t* src, uint32_t src_stride,
1209 uint32_t* dst, uint32_t dst_stride,
1210 const uint32_t* palette, int palette_size,
1211 int width, int height, int xbits) {
1212 // TODO(skal): this tmp buffer is not needed if VP8LBundleColorMap() can be
1213 // made to work in-place.
1214 uint8_t* const tmp_row = (uint8_t*)WebPSafeMalloc(width, sizeof(*tmp_row));
1215 int i, x, y;
1216 int use_LUT = 1;
1217
1218 if (tmp_row == NULL) return VP8_ENC_ERROR_OUT_OF_MEMORY;
1219 for (i = 0; i < palette_size; ++i) {
1220 if ((palette[i] & 0xffff00ffu) != 0) {
1221 use_LUT = 0;
1222 break;
1223 }
1224 }
1225
1226 if (use_LUT) {
1227 uint8_t inv_palette[MAX_PALETTE_SIZE] = { 0 };
1228 for (i = 0; i < palette_size; ++i) {
1229 const int color = (palette[i] >> 8) & 0xff;
1230 inv_palette[color] = i;
1231 }
1232 for (y = 0; y < height; ++y) {
1233 for (x = 0; x < width; ++x) {
1234 const int color = (src[x] >> 8) & 0xff;
1235 tmp_row[x] = inv_palette[color];
1236 }
1237 VP8LBundleColorMap(tmp_row, width, xbits, dst);
1238 src += src_stride;
1239 dst += dst_stride;
1240 }
1241 } else {
1242 // Use 1 pixel cache for ARGB pixels.
1243 uint32_t last_pix;
1244 int last_idx;
1245 uint32_t sorted[MAX_PALETTE_SIZE];
1246 int idx_map[MAX_PALETTE_SIZE];
1247 PrepareMapToPalette(palette, palette_size, sorted, idx_map);
1248 last_pix = palette[0];
1249 last_idx = 0;
1250 for (y = 0; y < height; ++y) {
1251 MapToPalette(sorted, palette_size, &last_pix, &last_idx,
1252 idx_map, src, tmp_row, width);
1253 VP8LBundleColorMap(tmp_row, width, xbits, dst);
1254 src += src_stride;
1255 dst += dst_stride;
1256 }
1257 }
1258 WebPSafeFree(tmp_row);
1259 return VP8_ENC_OK;
1260 }
1261
1262 // Note: Expects "enc->palette_" to be set properly.
1263 static WebPEncodingError MapImageFromPalette(VP8LEncoder* const enc,
1264 int in_place) {
1265 WebPEncodingError err = VP8_ENC_OK;
1266 const WebPPicture* const pic = enc->pic_;
1267 const int width = pic->width;
1268 const int height = pic->height;
1269 const uint32_t* const palette = enc->palette_;
1270 const uint32_t* src = in_place ? enc->argb_ : pic->argb;
1271 const int src_stride = in_place ? enc->current_width_ : pic->argb_stride;
1272 const int palette_size = enc->palette_size_;
1273 int xbits;
1274
1275 // Replace each input pixel by corresponding palette index.
1276 // This is done line by line.
1277 if (palette_size <= 4) {
1278 xbits = (palette_size <= 2) ? 3 : 2;
1279 } else {
1280 xbits = (palette_size <= 16) ? 1 : 0;
1281 }
1282
1283 err = AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height);
1284 if (err != VP8_ENC_OK) return err;
1285
1286 err = ApplyPalette(src, src_stride,
1287 enc->argb_, enc->current_width_,
1288 palette, palette_size, width, height, xbits);
1289 return err;
1290 }
1291
1292 // Save palette_[] to bitstream.
1293 static WebPEncodingError EncodePalette(VP8LBitWriter* const bw,
1294 VP8LEncoder* const enc) {
1295 int i;
1296 uint32_t tmp_palette[MAX_PALETTE_SIZE];
1297 const int palette_size = enc->palette_size_;
1298 const uint32_t* const palette = enc->palette_;
1299 VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1300 VP8LPutBits(bw, COLOR_INDEXING_TRANSFORM, 2);
1301 assert(palette_size >= 1 && palette_size <= MAX_PALETTE_SIZE);
1302 VP8LPutBits(bw, palette_size - 1, 8);
1303 for (i = palette_size - 1; i >= 1; --i) {
1304 tmp_palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
1305 }
1306 tmp_palette[0] = palette[0];
1307 return EncodeImageNoHuffman(bw, tmp_palette, &enc->hash_chain_, enc->refs_,
1308 palette_size, 1, 20 /* quality */);
1309 }
1310
1311 #ifdef WEBP_EXPERIMENTAL_FEATURES
1312
1313 static WebPEncodingError EncodeDeltaPalettePredictorImage(
1314 VP8LBitWriter* const bw, VP8LEncoder* const enc, int quality) {
1315 const WebPPicture* const pic = enc->pic_;
1316 const int width = pic->width;
1317 const int height = pic->height;
1318
1319 const int pred_bits = 5;
1320 const int transform_width = VP8LSubSampleSize(width, pred_bits);
1321 const int transform_height = VP8LSubSampleSize(height, pred_bits);
1322 const int pred = 7; // default is Predictor7 (Top/Left Average)
1323 const int tiles_per_row = VP8LSubSampleSize(width, pred_bits);
1324 const int tiles_per_col = VP8LSubSampleSize(height, pred_bits);
1325 uint32_t* predictors;
1326 int tile_x, tile_y;
1327 WebPEncodingError err = VP8_ENC_OK;
1328
1329 predictors = (uint32_t*)WebPSafeMalloc(tiles_per_col * tiles_per_row,
1330 sizeof(*predictors));
1331 if (predictors == NULL) return VP8_ENC_ERROR_OUT_OF_MEMORY;
1332
1333 for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) {
1334 for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) {
1335 predictors[tile_y * tiles_per_row + tile_x] = 0xff000000u | (pred << 8);
1336 }
1337 }
1338
1339 VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1340 VP8LPutBits(bw, PREDICTOR_TRANSFORM, 2);
1341 VP8LPutBits(bw, pred_bits - 2, 3);
1342 err = EncodeImageNoHuffman(bw, predictors, &enc->hash_chain_,
1343 (VP8LBackwardRefs*)enc->refs_, // cast const away
1344 transform_width, transform_height,
1345 quality);
1346 WebPSafeFree(predictors);
1347 return err;
1348 }
1349
1350 #endif // WEBP_EXPERIMENTAL_FEATURES
1351
1352 // -----------------------------------------------------------------------------
1353 // VP8LEncoder
1354
1355 static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
1356 const WebPPicture* const picture) {
1357 VP8LEncoder* const enc = (VP8LEncoder*)WebPSafeCalloc(1ULL, sizeof(*enc));
1358 if (enc == NULL) {
1359 WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1360 return NULL;
1361 }
1362 enc->config_ = config;
1363 enc->pic_ = picture;
1364
1365 VP8LEncDspInit();
1366
1367 return enc;
1368 }
1369
1370 static void VP8LEncoderDelete(VP8LEncoder* enc) {
1371 if (enc != NULL) {
1372 VP8LHashChainClear(&enc->hash_chain_);
1373 VP8LBackwardRefsClear(&enc->refs_[0]);
1374 VP8LBackwardRefsClear(&enc->refs_[1]);
1375 ClearTransformBuffer(enc);
1376 WebPSafeFree(enc);
1377 }
1378 }
1379
1380 // -----------------------------------------------------------------------------
1381 // Main call
1382
1383 WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
1384 const WebPPicture* const picture,
1385 VP8LBitWriter* const bw, int use_cache) {
1386 WebPEncodingError err = VP8_ENC_OK;
1387 const int quality = (int)config->quality;
1388 const int low_effort = (config->method == 0);
1389 const int width = picture->width;
1390 const int height = picture->height;
1391 VP8LEncoder* const enc = VP8LEncoderNew(config, picture);
1392 const size_t byte_position = VP8LBitWriterNumBytes(bw);
1393 int use_near_lossless = 0;
1394 int hdr_size = 0;
1395 int data_size = 0;
1396 int use_delta_palettization = 0;
1397
1398 if (enc == NULL) {
1399 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1400 goto Error;
1401 }
1402
1403 // ---------------------------------------------------------------------------
1404 // Analyze image (entropy, num_palettes etc)
1405
1406 if (!AnalyzeAndInit(enc)) {
1407 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1408 goto Error;
1409 }
1410
1411 // Apply near-lossless preprocessing.
1412 use_near_lossless =
1413 (config->near_lossless < 100) && !enc->use_palette_ && !enc->use_predict_;
1414 if (use_near_lossless) {
1415 if (!VP8ApplyNearLossless(width, height, picture->argb,
1416 config->near_lossless)) {
1417 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1418 goto Error;
1419 }
1420 }
1421
1422 #ifdef WEBP_EXPERIMENTAL_FEATURES
1423 if (config->delta_palettization) {
1424 enc->use_predict_ = 1;
1425 enc->use_cross_color_ = 0;
1426 enc->use_subtract_green_ = 0;
1427 enc->use_palette_ = 1;
1428 err = MakeInputImageCopy(enc);
1429 if (err != VP8_ENC_OK) goto Error;
1430 err = WebPSearchOptimalDeltaPalette(enc);
1431 if (err != VP8_ENC_OK) goto Error;
1432 if (enc->use_palette_) {
1433 err = AllocateTransformBuffer(enc, width, height);
1434 if (err != VP8_ENC_OK) goto Error;
1435 err = EncodeDeltaPalettePredictorImage(bw, enc, quality);
1436 if (err != VP8_ENC_OK) goto Error;
1437 use_delta_palettization = 1;
1438 }
1439 }
1440 #endif // WEBP_EXPERIMENTAL_FEATURES
1441
1442 // Encode palette
1443 if (enc->use_palette_) {
1444 err = EncodePalette(bw, enc);
1445 if (err != VP8_ENC_OK) goto Error;
1446 err = MapImageFromPalette(enc, use_delta_palettization);
1447 if (err != VP8_ENC_OK) goto Error;
1448 }
1449 if (!use_delta_palettization) {
1450 // In case image is not packed.
1451 if (enc->argb_ == NULL) {
1452 err = MakeInputImageCopy(enc);
1453 if (err != VP8_ENC_OK) goto Error;
1454 }
1455
1456 // -------------------------------------------------------------------------
1457 // Apply transforms and write transform data.
1458
1459 if (enc->use_subtract_green_) {
1460 ApplySubtractGreen(enc, enc->current_width_, height, bw);
1461 }
1462
1463 if (enc->use_predict_) {
1464 err = ApplyPredictFilter(enc, enc->current_width_, height, quality,
1465 low_effort, enc->use_subtract_green_, bw);
1466 if (err != VP8_ENC_OK) goto Error;
1467 }
1468
1469 if (enc->use_cross_color_) {
1470 err = ApplyCrossColorFilter(enc, enc->current_width_,
1471 height, quality, bw);
1472 if (err != VP8_ENC_OK) goto Error;
1473 }
1474 }
1475
1476 VP8LPutBits(bw, !TRANSFORM_PRESENT, 1); // No more transforms.
1477
1478 // ---------------------------------------------------------------------------
1479 // Encode and write the transformed image.
1480 err = EncodeImageInternal(bw, enc->argb_, &enc->hash_chain_, enc->refs_,
1481 enc->current_width_, height, quality, low_effort,
1482 use_cache, &enc->cache_bits_, enc->histo_bits_,
1483 byte_position, &hdr_size, &data_size);
1484 if (err != VP8_ENC_OK) goto Error;
1485
1486 if (picture->stats != NULL) {
1487 WebPAuxStats* const stats = picture->stats;
1488 stats->lossless_features = 0;
1489 if (enc->use_predict_) stats->lossless_features |= 1;
1490 if (enc->use_cross_color_) stats->lossless_features |= 2;
1491 if (enc->use_subtract_green_) stats->lossless_features |= 4;
1492 if (enc->use_palette_) stats->lossless_features |= 8;
1493 stats->histogram_bits = enc->histo_bits_;
1494 stats->transform_bits = enc->transform_bits_;
1495 stats->cache_bits = enc->cache_bits_;
1496 stats->palette_size = enc->palette_size_;
1497 stats->lossless_size = (int)(VP8LBitWriterNumBytes(bw) - byte_position);
1498 stats->lossless_hdr_size = hdr_size;
1499 stats->lossless_data_size = data_size;
1500 }
1501
1502 Error:
1503 VP8LEncoderDelete(enc);
1504 return err;
1505 }
1506
1507 int VP8LEncodeImage(const WebPConfig* const config,
1508 const WebPPicture* const picture) {
1509 int width, height;
1510 int has_alpha;
1511 size_t coded_size;
1512 int percent = 0;
1513 int initial_size;
1514 WebPEncodingError err = VP8_ENC_OK;
1515 VP8LBitWriter bw;
1516
1517 if (picture == NULL) return 0;
1518
1519 if (config == NULL || picture->argb == NULL) {
1520 err = VP8_ENC_ERROR_NULL_PARAMETER;
1521 WebPEncodingSetError(picture, err);
1522 return 0;
1523 }
1524
1525 width = picture->width;
1526 height = picture->height;
1527 // Initialize BitWriter with size corresponding to 16 bpp to photo images and
1528 // 8 bpp for graphical images.
1529 initial_size = (config->image_hint == WEBP_HINT_GRAPH) ?
1530 width * height : width * height * 2;
1531 if (!VP8LBitWriterInit(&bw, initial_size)) {
1532 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1533 goto Error;
1534 }
1535
1536 if (!WebPReportProgress(picture, 1, &percent)) {
1537 UserAbort:
1538 err = VP8_ENC_ERROR_USER_ABORT;
1539 goto Error;
1540 }
1541 // Reset stats (for pure lossless coding)
1542 if (picture->stats != NULL) {
1543 WebPAuxStats* const stats = picture->stats;
1544 memset(stats, 0, sizeof(*stats));
1545 stats->PSNR[0] = 99.f;
1546 stats->PSNR[1] = 99.f;
1547 stats->PSNR[2] = 99.f;
1548 stats->PSNR[3] = 99.f;
1549 stats->PSNR[4] = 99.f;
1550 }
1551
1552 // Write image size.
1553 if (!WriteImageSize(picture, &bw)) {
1554 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1555 goto Error;
1556 }
1557
1558 has_alpha = WebPPictureHasTransparency(picture);
1559 // Write the non-trivial Alpha flag and lossless version.
1560 if (!WriteRealAlphaAndVersion(&bw, has_alpha)) {
1561 err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1562 goto Error;
1563 }
1564
1565 if (!WebPReportProgress(picture, 5, &percent)) goto UserAbort;
1566
1567 // Encode main image stream.
1568 err = VP8LEncodeStream(config, picture, &bw, 1 /*use_cache*/);
1569 if (err != VP8_ENC_OK) goto Error;
1570
1571 // TODO(skal): have a fine-grained progress report in VP8LEncodeStream().
1572 if (!WebPReportProgress(picture, 90, &percent)) goto UserAbort;
1573
1574 // Finish the RIFF chunk.
1575 err = WriteImage(picture, &bw, &coded_size);
1576 if (err != VP8_ENC_OK) goto Error;
1577
1578 if (!WebPReportProgress(picture, 100, &percent)) goto UserAbort;
1579
1580 // Save size.
1581 if (picture->stats != NULL) {
1582 picture->stats->coded_size += (int)coded_size;
1583 picture->stats->lossless_size = (int)coded_size;
1584 }
1585
1586 if (picture->extra_info != NULL) {
1587 const int mb_w = (width + 15) >> 4;
1588 const int mb_h = (height + 15) >> 4;
1589 memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info));
1590 }
1591
1592 Error:
1593 if (bw.error_) err = VP8_ENC_ERROR_OUT_OF_MEMORY;
1594 VP8LBitWriterWipeOut(&bw);
1595 if (err != VP8_ENC_OK) {
1596 WebPEncodingSetError(picture, err);
1597 return 0;
1598 }
1599 return 1;
1600 }
1601
1602 //------------------------------------------------------------------------------
OLDNEW
« no previous file with comments | « third_party/libwebp/enc/vp8i_enc.h ('k') | third_party/libwebp/enc/vp8l_enc.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698