1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Author: Jyrki Alakuijala (jyrki@google.com)
11 //
12
13 #include "src/enc/backward_references_enc.h"
14
15 #include <assert.h>
16
17 #include "src/dsp/dsp.h"
18 #include "src/dsp/lossless.h"
19 #include "src/dsp/lossless_common.h"
20 #include "src/enc/histogram_enc.h"
21 #include "src/enc/vp8i_enc.h"
22 #include "src/utils/color_cache_utils.h"
23 #include "src/utils/utils.h"
24 #include "src/webp/encode.h"
25
26 #define MIN_BLOCK_SIZE 256 // minimum block size for backward references
27
28 // 1M window (4M bytes) minus 120 special codes for short distances.
29 #define WINDOW_SIZE ((1 << WINDOW_SIZE_BITS) - 120)
30
31 // Minimum number of pixels for which it is cheaper to encode a
32 // distance + length instead of each pixel as a literal.
33 #define MIN_LENGTH 4
34
35 // -----------------------------------------------------------------------------
36
37 static const uint8_t plane_to_code_lut[128] = {
38 96, 73, 55, 39, 23, 13, 5, 1, 255, 255, 255, 255, 255, 255, 255, 255,
39 101, 78, 58, 42, 26, 16, 8, 2, 0, 3, 9, 17, 27, 43, 59, 79,
40 102, 86, 62, 46, 32, 20, 10, 6, 4, 7, 11, 21, 33, 47, 63, 87,
41 105, 90, 70, 52, 37, 28, 18, 14, 12, 15, 19, 29, 38, 53, 71, 91,
42 110, 99, 82, 66, 48, 35, 30, 24, 22, 25, 31, 36, 49, 67, 83, 100,
43 115, 108, 94, 76, 64, 50, 44, 40, 34, 41, 45, 51, 65, 77, 95, 109,
44 118, 113, 103, 92, 80, 68, 60, 56, 54, 57, 61, 69, 81, 93, 104, 114,
45 119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117
46 };
47
48 extern int VP8LDistanceToPlaneCode(int xsize, int dist);
VP8LDistanceToPlaneCode(int xsize,int dist)49 int VP8LDistanceToPlaneCode(int xsize, int dist) {
50 const int yoffset = dist / xsize;
51 const int xoffset = dist - yoffset * xsize;
52 if (xoffset <= 8 && yoffset < 8) {
53 return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1;
54 } else if (xoffset > xsize - 8 && yoffset < 7) {
55 return plane_to_code_lut[(yoffset + 1) * 16 + 8 + (xsize - xoffset)] + 1;
56 }
57 return dist + 120;
58 }
59
60 // Returns the exact index where array1 and array2 are different. For an index
61 // inferior or equal to best_len_match, the return value just has to be strictly
62 // inferior to best_len_match. The current behavior is to return 0 if this index
63 // is best_len_match, and the index itself otherwise.
64 // If no two elements are the same, it returns max_limit.
FindMatchLength(const uint32_t * const array1,const uint32_t * const array2,int best_len_match,int max_limit)65 static WEBP_INLINE int FindMatchLength(const uint32_t* const array1,
66 const uint32_t* const array2,
67 int best_len_match, int max_limit) {
68 // Before 'expensive' linear match, check if the two arrays match at the
69 // current best length index.
70 if (array1[best_len_match] != array2[best_len_match]) return 0;
71
72 return VP8LVectorMismatch(array1, array2, max_limit);
73 }
74
75 // -----------------------------------------------------------------------------
76 // VP8LBackwardRefs
77
78 struct PixOrCopyBlock {
79 PixOrCopyBlock* next_; // next block (or NULL)
80 PixOrCopy* start_; // data start
81 int size_; // currently used size
82 };
83
84 extern void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs);
VP8LClearBackwardRefs(VP8LBackwardRefs * const refs)85 void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs) {
86 assert(refs != NULL);
87 if (refs->tail_ != NULL) {
88 *refs->tail_ = refs->free_blocks_; // recycle all blocks at once
89 }
90 refs->free_blocks_ = refs->refs_;
91 refs->tail_ = &refs->refs_;
92 refs->last_block_ = NULL;
93 refs->refs_ = NULL;
94 }
95
VP8LBackwardRefsClear(VP8LBackwardRefs * const refs)96 void VP8LBackwardRefsClear(VP8LBackwardRefs* const refs) {
97 assert(refs != NULL);
98 VP8LClearBackwardRefs(refs);
99 while (refs->free_blocks_ != NULL) {
100 PixOrCopyBlock* const next = refs->free_blocks_->next_;
101 WebPSafeFree(refs->free_blocks_);
102 refs->free_blocks_ = next;
103 }
104 }
105
106 // Swaps the content of two VP8LBackwardRefs.
BackwardRefsSwap(VP8LBackwardRefs * const refs1,VP8LBackwardRefs * const refs2)107 static void BackwardRefsSwap(VP8LBackwardRefs* const refs1,
108 VP8LBackwardRefs* const refs2) {
109 const int point_to_refs1 =
110 (refs1->tail_ != NULL && refs1->tail_ == &refs1->refs_);
111 const int point_to_refs2 =
112 (refs2->tail_ != NULL && refs2->tail_ == &refs2->refs_);
113 const VP8LBackwardRefs tmp = *refs1;
114 *refs1 = *refs2;
115 *refs2 = tmp;
116 if (point_to_refs2) refs1->tail_ = &refs1->refs_;
117 if (point_to_refs1) refs2->tail_ = &refs2->refs_;
118 }
119
VP8LBackwardRefsInit(VP8LBackwardRefs * const refs,int block_size)120 void VP8LBackwardRefsInit(VP8LBackwardRefs* const refs, int block_size) {
121 assert(refs != NULL);
122 memset(refs, 0, sizeof(*refs));
123 refs->tail_ = &refs->refs_;
124 refs->block_size_ =
125 (block_size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : block_size;
126 }
127
VP8LRefsCursorInit(const VP8LBackwardRefs * const refs)128 VP8LRefsCursor VP8LRefsCursorInit(const VP8LBackwardRefs* const refs) {
129 VP8LRefsCursor c;
130 c.cur_block_ = refs->refs_;
131 if (refs->refs_ != NULL) {
132 c.cur_pos = c.cur_block_->start_;
133 c.last_pos_ = c.cur_pos + c.cur_block_->size_;
134 } else {
135 c.cur_pos = NULL;
136 c.last_pos_ = NULL;
137 }
138 return c;
139 }
140
VP8LRefsCursorNextBlock(VP8LRefsCursor * const c)141 void VP8LRefsCursorNextBlock(VP8LRefsCursor* const c) {
142 PixOrCopyBlock* const b = c->cur_block_->next_;
143 c->cur_pos = (b == NULL) ? NULL : b->start_;
144 c->last_pos_ = (b == NULL) ? NULL : b->start_ + b->size_;
145 c->cur_block_ = b;
146 }
147
148 // Create a new block, either from the free list or allocated
BackwardRefsNewBlock(VP8LBackwardRefs * const refs)149 static PixOrCopyBlock* BackwardRefsNewBlock(VP8LBackwardRefs* const refs) {
150 PixOrCopyBlock* b = refs->free_blocks_;
151 if (b == NULL) { // allocate new memory chunk
152 const size_t total_size =
153 sizeof(*b) + refs->block_size_ * sizeof(*b->start_);
154 b = (PixOrCopyBlock*)WebPSafeMalloc(1ULL, total_size);
155 if (b == NULL) {
156 refs->error_ |= 1;
157 return NULL;
158 }
159 b->start_ = (PixOrCopy*)((uint8_t*)b + sizeof(*b)); // not always aligned
160 } else { // recycle from free-list
161 refs->free_blocks_ = b->next_;
162 }
163 *refs->tail_ = b;
164 refs->tail_ = &b->next_;
165 refs->last_block_ = b;
166 b->next_ = NULL;
167 b->size_ = 0;
168 return b;
169 }
170
171 // Return 1 on success, 0 on error.
BackwardRefsClone(const VP8LBackwardRefs * const from,VP8LBackwardRefs * const to)172 static int BackwardRefsClone(const VP8LBackwardRefs* const from,
173 VP8LBackwardRefs* const to) {
174 const PixOrCopyBlock* block_from = from->refs_;
175 VP8LClearBackwardRefs(to);
176 while (block_from != NULL) {
177 PixOrCopyBlock* const block_to = BackwardRefsNewBlock(to);
178 if (block_to == NULL) return 0;
179 memcpy(block_to->start_, block_from->start_,
180 block_from->size_ * sizeof(PixOrCopy));
181 block_to->size_ = block_from->size_;
182 block_from = block_from->next_;
183 }
184 return 1;
185 }
186
187 extern void VP8LBackwardRefsCursorAdd(VP8LBackwardRefs* const refs,
188 const PixOrCopy v);
VP8LBackwardRefsCursorAdd(VP8LBackwardRefs * const refs,const PixOrCopy v)189 void VP8LBackwardRefsCursorAdd(VP8LBackwardRefs* const refs,
190 const PixOrCopy v) {
191 PixOrCopyBlock* b = refs->last_block_;
192 if (b == NULL || b->size_ == refs->block_size_) {
193 b = BackwardRefsNewBlock(refs);
194 if (b == NULL) return; // refs->error_ is set
195 }
196 b->start_[b->size_++] = v;
197 }
198
199 // -----------------------------------------------------------------------------
200 // Hash chains
201
VP8LHashChainInit(VP8LHashChain * const p,int size)202 int VP8LHashChainInit(VP8LHashChain* const p, int size) {
203 assert(p->size_ == 0);
204 assert(p->offset_length_ == NULL);
205 assert(size > 0);
206 p->offset_length_ =
207 (uint32_t*)WebPSafeMalloc(size, sizeof(*p->offset_length_));
208 if (p->offset_length_ == NULL) return 0;
209 p->size_ = size;
210
211 return 1;
212 }
213
VP8LHashChainClear(VP8LHashChain * const p)214 void VP8LHashChainClear(VP8LHashChain* const p) {
215 assert(p != NULL);
216 WebPSafeFree(p->offset_length_);
217
218 p->size_ = 0;
219 p->offset_length_ = NULL;
220 }
221
222 // -----------------------------------------------------------------------------
223
224 static const uint32_t kHashMultiplierHi = 0xc6a4a793u;
225 static const uint32_t kHashMultiplierLo = 0x5bd1e996u;
226
227 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
GetPixPairHash64(const uint32_t * const argb)228 uint32_t GetPixPairHash64(const uint32_t* const argb) {
229 uint32_t key;
230 key = argb[1] * kHashMultiplierHi;
231 key += argb[0] * kHashMultiplierLo;
232 key = key >> (32 - HASH_BITS);
233 return key;
234 }
235
236 // Returns the maximum number of hash chain lookups to do for a
237 // given compression quality. Return value in range [8, 86].
GetMaxItersForQuality(int quality)238 static int GetMaxItersForQuality(int quality) {
239 return 8 + (quality * quality) / 128;
240 }
241
GetWindowSizeForHashChain(int quality,int xsize)242 static int GetWindowSizeForHashChain(int quality, int xsize) {
243 const int max_window_size = (quality > 75) ? WINDOW_SIZE
244 : (quality > 50) ? (xsize << 8)
245 : (quality > 25) ? (xsize << 6)
246 : (xsize << 4);
247 assert(xsize > 0);
248 return (max_window_size > WINDOW_SIZE) ? WINDOW_SIZE : max_window_size;
249 }
250
MaxFindCopyLength(int len)251 static WEBP_INLINE int MaxFindCopyLength(int len) {
252 return (len < MAX_LENGTH) ? len : MAX_LENGTH;
253 }
254
VP8LHashChainFill(VP8LHashChain * const p,int quality,const uint32_t * const argb,int xsize,int ysize,int low_effort,const WebPPicture * const pic,int percent_range,int * const percent)255 int VP8LHashChainFill(VP8LHashChain* const p, int quality,
256 const uint32_t* const argb, int xsize, int ysize,
257 int low_effort, const WebPPicture* const pic,
258 int percent_range, int* const percent) {
259 const int size = xsize * ysize;
260 const int iter_max = GetMaxItersForQuality(quality);
261 const uint32_t window_size = GetWindowSizeForHashChain(quality, xsize);
262 int remaining_percent = percent_range;
263 int percent_start = *percent;
264 int pos;
265 int argb_comp;
266 uint32_t base_position;
267 int32_t* hash_to_first_index;
268 // Temporarily use the p->offset_length_ as a hash chain.
269 int32_t* chain = (int32_t*)p->offset_length_;
270 assert(size > 0);
271 assert(p->size_ != 0);
272 assert(p->offset_length_ != NULL);
273
274 if (size <= 2) {
275 p->offset_length_[0] = p->offset_length_[size - 1] = 0;
276 return 1;
277 }
278
279 hash_to_first_index =
280 (int32_t*)WebPSafeMalloc(HASH_SIZE, sizeof(*hash_to_first_index));
281 if (hash_to_first_index == NULL) {
282 return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
283 }
284
285 percent_range = remaining_percent / 2;
286 remaining_percent -= percent_range;
287
288 // Set the int32_t array to -1.
289 memset(hash_to_first_index, 0xff, HASH_SIZE * sizeof(*hash_to_first_index));
290 // Fill the chain linking pixels with the same hash.
291 argb_comp = (argb[0] == argb[1]);
292 for (pos = 0; pos < size - 2;) {
293 uint32_t hash_code;
294 const int argb_comp_next = (argb[pos + 1] == argb[pos + 2]);
295 if (argb_comp && argb_comp_next) {
296 // Consecutive pixels with the same color will share the same hash.
297 // We therefore use a different hash: the color and its repetition
298 // length.
299 uint32_t tmp[2];
300 uint32_t len = 1;
301 tmp[0] = argb[pos];
302 // Figure out how far the pixels are the same.
303 // The last pixel has a different 64 bit hash, as its next pixel does
304 // not have the same color, so we just need to get to the last pixel equal
305 // to its follower.
306 while (pos + (int)len + 2 < size && argb[pos + len + 2] == argb[pos]) {
307 ++len;
308 }
309 if (len > MAX_LENGTH) {
310 // Skip the pixels that match for distance=1 and length>MAX_LENGTH
311 // because they are linked to their predecessor and we automatically
312 // check that in the main for loop below. Skipping means setting no
313 // predecessor in the chain, hence -1.
314 memset(chain + pos, 0xff, (len - MAX_LENGTH) * sizeof(*chain));
315 pos += len - MAX_LENGTH;
316 len = MAX_LENGTH;
317 }
318 // Process the rest of the hash chain.
319 while (len) {
320 tmp[1] = len--;
321 hash_code = GetPixPairHash64(tmp);
322 chain[pos] = hash_to_first_index[hash_code];
323 hash_to_first_index[hash_code] = pos++;
324 }
325 argb_comp = 0;
326 } else {
327 // Just move one pixel forward.
328 hash_code = GetPixPairHash64(argb + pos);
329 chain[pos] = hash_to_first_index[hash_code];
330 hash_to_first_index[hash_code] = pos++;
331 argb_comp = argb_comp_next;
332 }
333
334 if (!WebPReportProgress(
335 pic, percent_start + percent_range * pos / (size - 2), percent)) {
336 WebPSafeFree(hash_to_first_index);
337 return 0;
338 }
339 }
340 // Process the penultimate pixel.
341 chain[pos] = hash_to_first_index[GetPixPairHash64(argb + pos)];
342
343 WebPSafeFree(hash_to_first_index);
344
345 percent_start += percent_range;
346 if (!WebPReportProgress(pic, percent_start, percent)) return 0;
347 percent_range = remaining_percent;
348
349 // Find the best match interval at each pixel, defined by an offset to the
350 // pixel and a length. The right-most pixel cannot match anything to the right
351 // (hence a best length of 0) and the left-most pixel nothing to the left
352 // (hence an offset of 0).
353 assert(size > 2);
354 p->offset_length_[0] = p->offset_length_[size - 1] = 0;
355 for (base_position = size - 2; base_position > 0;) {
356 const int max_len = MaxFindCopyLength(size - 1 - base_position);
357 const uint32_t* const argb_start = argb + base_position;
358 int iter = iter_max;
359 int best_length = 0;
360 uint32_t best_distance = 0;
361 uint32_t best_argb;
362 const int min_pos =
363 (base_position > window_size) ? base_position - window_size : 0;
364 const int length_max = (max_len < 256) ? max_len : 256;
365 uint32_t max_base_position;
366
367 pos = chain[base_position];
368 if (!low_effort) {
369 int curr_length;
370 // Heuristic: use the comparison with the above line as an initialization.
371 if (base_position >= (uint32_t)xsize) {
372 curr_length = FindMatchLength(argb_start - xsize, argb_start,
373 best_length, max_len);
374 if (curr_length > best_length) {
375 best_length = curr_length;
376 best_distance = xsize;
377 }
378 --iter;
379 }
380 // Heuristic: compare to the previous pixel.
381 curr_length =
382 FindMatchLength(argb_start - 1, argb_start, best_length, max_len);
383 if (curr_length > best_length) {
384 best_length = curr_length;
385 best_distance = 1;
386 }
387 --iter;
388 // Skip the for loop if we already have the maximum.
389 if (best_length == MAX_LENGTH) pos = min_pos - 1;
390 }
391 best_argb = argb_start[best_length];
392
393 for (; pos >= min_pos && --iter; pos = chain[pos]) {
394 int curr_length;
395 assert(base_position > (uint32_t)pos);
396
397 if (argb[pos + best_length] != best_argb) continue;
398
399 curr_length = VP8LVectorMismatch(argb + pos, argb_start, max_len);
400 if (best_length < curr_length) {
401 best_length = curr_length;
402 best_distance = base_position - pos;
403 best_argb = argb_start[best_length];
404 // Stop if we have reached a good enough length.
405 if (best_length >= length_max) break;
406 }
407 }
408 // We have the best match but in case the two intervals continue matching
409 // to the left, we have the best matches for the left-extended pixels.
410 max_base_position = base_position;
411 while (1) {
412 assert(best_length <= MAX_LENGTH);
413 assert(best_distance <= WINDOW_SIZE);
414 p->offset_length_[base_position] =
415 (best_distance << MAX_LENGTH_BITS) | (uint32_t)best_length;
416 --base_position;
417 // Stop if we don't have a match or if we are out of bounds.
418 if (best_distance == 0 || base_position == 0) break;
419 // Stop if we cannot extend the matching intervals to the left.
420 if (base_position < best_distance ||
421 argb[base_position - best_distance] != argb[base_position]) {
422 break;
423 }
424 // Stop if we are matching at its limit because there could be a closer
425 // matching interval with the same maximum length. Then again, if the
426 // matching interval is as close as possible (best_distance == 1), we will
427 // never find anything better so let's continue.
428 if (best_length == MAX_LENGTH && best_distance != 1 &&
429 base_position + MAX_LENGTH < max_base_position) {
430 break;
431 }
432 if (best_length < MAX_LENGTH) {
433 ++best_length;
434 max_base_position = base_position;
435 }
436 }
437
438 if (!WebPReportProgress(pic,
439 percent_start + percent_range *
440 (size - 2 - base_position) /
441 (size - 2),
442 percent)) {
443 return 0;
444 }
445 }
446
447 return WebPReportProgress(pic, percent_start + percent_range, percent);
448 }
449
AddSingleLiteral(uint32_t pixel,int use_color_cache,VP8LColorCache * const hashers,VP8LBackwardRefs * const refs)450 static WEBP_INLINE void AddSingleLiteral(uint32_t pixel, int use_color_cache,
451 VP8LColorCache* const hashers,
452 VP8LBackwardRefs* const refs) {
453 PixOrCopy v;
454 if (use_color_cache) {
455 const uint32_t key = VP8LColorCacheGetIndex(hashers, pixel);
456 if (VP8LColorCacheLookup(hashers, key) == pixel) {
457 v = PixOrCopyCreateCacheIdx(key);
458 } else {
459 v = PixOrCopyCreateLiteral(pixel);
460 VP8LColorCacheSet(hashers, key, pixel);
461 }
462 } else {
463 v = PixOrCopyCreateLiteral(pixel);
464 }
465 VP8LBackwardRefsCursorAdd(refs, v);
466 }
467
BackwardReferencesRle(int xsize,int ysize,const uint32_t * const argb,int cache_bits,VP8LBackwardRefs * const refs)468 static int BackwardReferencesRle(int xsize, int ysize,
469 const uint32_t* const argb,
470 int cache_bits, VP8LBackwardRefs* const refs) {
471 const int pix_count = xsize * ysize;
472 int i, k;
473 const int use_color_cache = (cache_bits > 0);
474 VP8LColorCache hashers;
475
476 if (use_color_cache && !VP8LColorCacheInit(&hashers, cache_bits)) {
477 return 0;
478 }
479 VP8LClearBackwardRefs(refs);
480 // Add first pixel as literal.
481 AddSingleLiteral(argb[0], use_color_cache, &hashers, refs);
482 i = 1;
483 while (i < pix_count) {
484 const int max_len = MaxFindCopyLength(pix_count - i);
485 const int rle_len = FindMatchLength(argb + i, argb + i - 1, 0, max_len);
486 const int prev_row_len = (i < xsize) ? 0 :
487 FindMatchLength(argb + i, argb + i - xsize, 0, max_len);
488 if (rle_len >= prev_row_len && rle_len >= MIN_LENGTH) {
489 VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(1, rle_len));
490 // We don't need to update the color cache here since it is always the
491 // same pixel being copied, and that does not change the color cache
492 // state.
493 i += rle_len;
494 } else if (prev_row_len >= MIN_LENGTH) {
495 VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(xsize, prev_row_len));
496 if (use_color_cache) {
497 for (k = 0; k < prev_row_len; ++k) {
498 VP8LColorCacheInsert(&hashers, argb[i + k]);
499 }
500 }
501 i += prev_row_len;
502 } else {
503 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
504 i++;
505 }
506 }
507 if (use_color_cache) VP8LColorCacheClear(&hashers);
508 return !refs->error_;
509 }
510
BackwardReferencesLz77(int xsize,int ysize,const uint32_t * const argb,int cache_bits,const VP8LHashChain * const hash_chain,VP8LBackwardRefs * const refs)511 static int BackwardReferencesLz77(int xsize, int ysize,
512 const uint32_t* const argb, int cache_bits,
513 const VP8LHashChain* const hash_chain,
514 VP8LBackwardRefs* const refs) {
515 int i;
516 int i_last_check = -1;
517 int ok = 0;
518 int cc_init = 0;
519 const int use_color_cache = (cache_bits > 0);
520 const int pix_count = xsize * ysize;
521 VP8LColorCache hashers;
522
523 if (use_color_cache) {
524 cc_init = VP8LColorCacheInit(&hashers, cache_bits);
525 if (!cc_init) goto Error;
526 }
527 VP8LClearBackwardRefs(refs);
528 for (i = 0; i < pix_count;) {
529 // Alternative#1: Code the pixels starting at 'i' using backward reference.
530 int offset = 0;
531 int len = 0;
532 int j;
533 VP8LHashChainFindCopy(hash_chain, i, &offset, &len);
534 if (len >= MIN_LENGTH) {
535 const int len_ini = len;
536 int max_reach = 0;
537 const int j_max =
538 (i + len_ini >= pix_count) ? pix_count - 1 : i + len_ini;
539 // Only start from what we have not checked already.
540 i_last_check = (i > i_last_check) ? i : i_last_check;
541 // We know the best match for the current pixel but we try to find the
542 // best matches for the current pixel AND the next one combined.
543 // The naive method would use the intervals:
544 // [i,i+len) + [i+len, length of best match at i+len)
545 // while we check if we can use:
546 // [i,j) (where j<=i+len) + [j, length of best match at j)
547 for (j = i_last_check + 1; j <= j_max; ++j) {
548 const int len_j = VP8LHashChainFindLength(hash_chain, j);
549 const int reach =
550 j + (len_j >= MIN_LENGTH ? len_j : 1); // 1 for single literal.
551 if (reach > max_reach) {
552 len = j - i;
553 max_reach = reach;
554 if (max_reach >= pix_count) break;
555 }
556 }
557 } else {
558 len = 1;
559 }
560 // Go with literal or backward reference.
561 assert(len > 0);
562 if (len == 1) {
563 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
564 } else {
565 VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len));
566 if (use_color_cache) {
567 for (j = i; j < i + len; ++j) VP8LColorCacheInsert(&hashers, argb[j]);
568 }
569 }
570 i += len;
571 }
572
573 ok = !refs->error_;
574 Error:
575 if (cc_init) VP8LColorCacheClear(&hashers);
576 return ok;
577 }
578
579 // Compute an LZ77 by forcing matches to happen within a given distance cost.
580 // We therefore limit the algorithm to the lowest 32 values in the PlaneCode
581 // definition.
582 #define WINDOW_OFFSETS_SIZE_MAX 32
BackwardReferencesLz77Box(int xsize,int ysize,const uint32_t * const argb,int cache_bits,const VP8LHashChain * const hash_chain_best,VP8LHashChain * hash_chain,VP8LBackwardRefs * const refs)583 static int BackwardReferencesLz77Box(int xsize, int ysize,
584 const uint32_t* const argb, int cache_bits,
585 const VP8LHashChain* const hash_chain_best,
586 VP8LHashChain* hash_chain,
587 VP8LBackwardRefs* const refs) {
588 int i;
589 const int pix_count = xsize * ysize;
590 uint16_t* counts;
591 int window_offsets[WINDOW_OFFSETS_SIZE_MAX] = {0};
592 int window_offsets_new[WINDOW_OFFSETS_SIZE_MAX] = {0};
593 int window_offsets_size = 0;
594 int window_offsets_new_size = 0;
595 uint16_t* const counts_ini =
596 (uint16_t*)WebPSafeMalloc(xsize * ysize, sizeof(*counts_ini));
597 int best_offset_prev = -1, best_length_prev = -1;
598 if (counts_ini == NULL) return 0;
599
600 // counts[i] counts how many times a pixel is repeated starting at position i.
601 i = pix_count - 2;
602 counts = counts_ini + i;
603 counts[1] = 1;
604 for (; i >= 0; --i, --counts) {
605 if (argb[i] == argb[i + 1]) {
606 // Max out the counts to MAX_LENGTH.
607 counts[0] = counts[1] + (counts[1] != MAX_LENGTH);
608 } else {
609 counts[0] = 1;
610 }
611 }
612
613 // Figure out the window offsets around a pixel. They are stored in a
614 // spiraling order around the pixel as defined by VP8LDistanceToPlaneCode.
615 {
616 int x, y;
617 for (y = 0; y <= 6; ++y) {
618 for (x = -6; x <= 6; ++x) {
619 const int offset = y * xsize + x;
620 int plane_code;
621 // Ignore offsets that bring us after the pixel.
622 if (offset <= 0) continue;
623 plane_code = VP8LDistanceToPlaneCode(xsize, offset) - 1;
624 if (plane_code >= WINDOW_OFFSETS_SIZE_MAX) continue;
625 window_offsets[plane_code] = offset;
626 }
627 }
628 // For narrow images, not all plane codes are reached, so remove those.
629 for (i = 0; i < WINDOW_OFFSETS_SIZE_MAX; ++i) {
630 if (window_offsets[i] == 0) continue;
631 window_offsets[window_offsets_size++] = window_offsets[i];
632 }
633 // Given a pixel P, find the offsets that reach pixels unreachable from P-1
634 // with any of the offsets in window_offsets[].
635 for (i = 0; i < window_offsets_size; ++i) {
636 int j;
637 int is_reachable = 0;
638 for (j = 0; j < window_offsets_size && !is_reachable; ++j) {
639 is_reachable |= (window_offsets[i] == window_offsets[j] + 1);
640 }
641 if (!is_reachable) {
642 window_offsets_new[window_offsets_new_size] = window_offsets[i];
643 ++window_offsets_new_size;
644 }
645 }
646 }
647
648 hash_chain->offset_length_[0] = 0;
649 for (i = 1; i < pix_count; ++i) {
650 int ind;
651 int best_length = VP8LHashChainFindLength(hash_chain_best, i);
652 int best_offset;
653 int do_compute = 1;
654
655 if (best_length >= MAX_LENGTH) {
656 // Do not recompute the best match if we already have a maximal one in the
657 // window.
658 best_offset = VP8LHashChainFindOffset(hash_chain_best, i);
659 for (ind = 0; ind < window_offsets_size; ++ind) {
660 if (best_offset == window_offsets[ind]) {
661 do_compute = 0;
662 break;
663 }
664 }
665 }
666 if (do_compute) {
667 // Figure out if we should use the offset/length from the previous pixel
668 // as an initial guess and therefore only inspect the offsets in
669 // window_offsets_new[].
670 const int use_prev =
671 (best_length_prev > 1) && (best_length_prev < MAX_LENGTH);
672 const int num_ind =
673 use_prev ? window_offsets_new_size : window_offsets_size;
674 best_length = use_prev ? best_length_prev - 1 : 0;
675 best_offset = use_prev ? best_offset_prev : 0;
676 // Find the longest match in a window around the pixel.
677 for (ind = 0; ind < num_ind; ++ind) {
678 int curr_length = 0;
679 int j = i;
680 int j_offset =
681 use_prev ? i - window_offsets_new[ind] : i - window_offsets[ind];
682 if (j_offset < 0 || argb[j_offset] != argb[i]) continue;
683 // The longest match is the sum of how many times each pixel is
684 // repeated.
685 do {
686 const int counts_j_offset = counts_ini[j_offset];
687 const int counts_j = counts_ini[j];
688 if (counts_j_offset != counts_j) {
689 curr_length +=
690 (counts_j_offset < counts_j) ? counts_j_offset : counts_j;
691 break;
692 }
693 // The same color is repeated counts_pos times at j_offset and j.
694 curr_length += counts_j_offset;
695 j_offset += counts_j_offset;
696 j += counts_j_offset;
697 } while (curr_length <= MAX_LENGTH && j < pix_count &&
698 argb[j_offset] == argb[j]);
699 if (best_length < curr_length) {
700 best_offset =
701 use_prev ? window_offsets_new[ind] : window_offsets[ind];
702 if (curr_length >= MAX_LENGTH) {
703 best_length = MAX_LENGTH;
704 break;
705 } else {
706 best_length = curr_length;
707 }
708 }
709 }
710 }
711
712 assert(i + best_length <= pix_count);
713 assert(best_length <= MAX_LENGTH);
714 if (best_length <= MIN_LENGTH) {
715 hash_chain->offset_length_[i] = 0;
716 best_offset_prev = 0;
717 best_length_prev = 0;
718 } else {
719 hash_chain->offset_length_[i] =
720 (best_offset << MAX_LENGTH_BITS) | (uint32_t)best_length;
721 best_offset_prev = best_offset;
722 best_length_prev = best_length;
723 }
724 }
725 hash_chain->offset_length_[0] = 0;
726 WebPSafeFree(counts_ini);
727
728 return BackwardReferencesLz77(xsize, ysize, argb, cache_bits, hash_chain,
729 refs);
730 }
731
732 // -----------------------------------------------------------------------------
733
BackwardReferences2DLocality(int xsize,const VP8LBackwardRefs * const refs)734 static void BackwardReferences2DLocality(int xsize,
735 const VP8LBackwardRefs* const refs) {
736 VP8LRefsCursor c = VP8LRefsCursorInit(refs);
737 while (VP8LRefsCursorOk(&c)) {
738 if (PixOrCopyIsCopy(c.cur_pos)) {
739 const int dist = c.cur_pos->argb_or_distance;
740 const int transformed_dist = VP8LDistanceToPlaneCode(xsize, dist);
741 c.cur_pos->argb_or_distance = transformed_dist;
742 }
743 VP8LRefsCursorNext(&c);
744 }
745 }
746
747 // Evaluate optimal cache bits for the local color cache.
748 // The input *best_cache_bits sets the maximum cache bits to use (passing 0
749 // implies disabling the local color cache). The local color cache is also
750 // disabled for the lower (<= 25) quality.
751 // Returns 0 in case of memory error.
CalculateBestCacheSize(const uint32_t * argb,int quality,const VP8LBackwardRefs * const refs,int * const best_cache_bits)752 static int CalculateBestCacheSize(const uint32_t* argb, int quality,
753 const VP8LBackwardRefs* const refs,
754 int* const best_cache_bits) {
755 int i;
756 const int cache_bits_max = (quality <= 25) ? 0 : *best_cache_bits;
757 uint64_t entropy_min = WEBP_UINT64_MAX;
758 int cc_init[MAX_COLOR_CACHE_BITS + 1] = { 0 };
759 VP8LColorCache hashers[MAX_COLOR_CACHE_BITS + 1];
760 VP8LRefsCursor c = VP8LRefsCursorInit(refs);
761 VP8LHistogram* histos[MAX_COLOR_CACHE_BITS + 1] = { NULL };
762 int ok = 0;
763
764 assert(cache_bits_max >= 0 && cache_bits_max <= MAX_COLOR_CACHE_BITS);
765
766 if (cache_bits_max == 0) {
767 *best_cache_bits = 0;
768 // Local color cache is disabled.
769 return 1;
770 }
771
772 // Allocate data.
773 for (i = 0; i <= cache_bits_max; ++i) {
774 histos[i] = VP8LAllocateHistogram(i);
775 if (histos[i] == NULL) goto Error;
776 VP8LHistogramInit(histos[i], i, /*init_arrays=*/ 1);
777 if (i == 0) continue;
778 cc_init[i] = VP8LColorCacheInit(&hashers[i], i);
779 if (!cc_init[i]) goto Error;
780 }
781
782 // Find the cache_bits giving the lowest entropy. The search is done in a
783 // brute-force way as the function (entropy w.r.t cache_bits) can be
784 // anything in practice.
785 while (VP8LRefsCursorOk(&c)) {
786 const PixOrCopy* const v = c.cur_pos;
787 if (PixOrCopyIsLiteral(v)) {
788 const uint32_t pix = *argb++;
789 const uint32_t a = (pix >> 24) & 0xff;
790 const uint32_t r = (pix >> 16) & 0xff;
791 const uint32_t g = (pix >> 8) & 0xff;
792 const uint32_t b = (pix >> 0) & 0xff;
793 // The keys of the caches can be derived from the longest one.
794 int key = VP8LHashPix(pix, 32 - cache_bits_max);
795 // Do not use the color cache for cache_bits = 0.
796 ++histos[0]->blue_[b];
797 ++histos[0]->literal_[g];
798 ++histos[0]->red_[r];
799 ++histos[0]->alpha_[a];
800 // Deal with cache_bits > 0.
801 for (i = cache_bits_max; i >= 1; --i, key >>= 1) {
802 if (VP8LColorCacheLookup(&hashers[i], key) == pix) {
803 ++histos[i]->literal_[NUM_LITERAL_CODES + NUM_LENGTH_CODES + key];
804 } else {
805 VP8LColorCacheSet(&hashers[i], key, pix);
806 ++histos[i]->blue_[b];
807 ++histos[i]->literal_[g];
808 ++histos[i]->red_[r];
809 ++histos[i]->alpha_[a];
810 }
811 }
812 } else {
813 int code, extra_bits, extra_bits_value;
814 // We should compute the contribution of the (distance,length)
815 // histograms but those are the same independently from the cache size.
816 // As those constant contributions are in the end added to the other
817 // histogram contributions, we can ignore them, except for the length
818 // prefix that is part of the literal_ histogram.
819 int len = PixOrCopyLength(v);
820 uint32_t argb_prev = *argb ^ 0xffffffffu;
821 VP8LPrefixEncode(len, &code, &extra_bits, &extra_bits_value);
822 for (i = 0; i <= cache_bits_max; ++i) {
823 ++histos[i]->literal_[NUM_LITERAL_CODES + code];
824 }
825 // Update the color caches.
826 do {
827 if (*argb != argb_prev) {
828 // Efficiency: insert only if the color changes.
829 int key = VP8LHashPix(*argb, 32 - cache_bits_max);
830 for (i = cache_bits_max; i >= 1; --i, key >>= 1) {
831 hashers[i].colors_[key] = *argb;
832 }
833 argb_prev = *argb;
834 }
835 argb++;
836 } while (--len != 0);
837 }
838 VP8LRefsCursorNext(&c);
839 }
840
841 for (i = 0; i <= cache_bits_max; ++i) {
842 const uint64_t entropy = VP8LHistogramEstimateBits(histos[i]);
843 if (i == 0 || entropy < entropy_min) {
844 entropy_min = entropy;
845 *best_cache_bits = i;
846 }
847 }
848 ok = 1;
849 Error:
850 for (i = 0; i <= cache_bits_max; ++i) {
851 if (cc_init[i]) VP8LColorCacheClear(&hashers[i]);
852 VP8LFreeHistogram(histos[i]);
853 }
854 return ok;
855 }
856
857 // Update (in-place) backward references for specified cache_bits.
BackwardRefsWithLocalCache(const uint32_t * const argb,int cache_bits,VP8LBackwardRefs * const refs)858 static int BackwardRefsWithLocalCache(const uint32_t* const argb,
859 int cache_bits,
860 VP8LBackwardRefs* const refs) {
861 int pixel_index = 0;
862 VP8LColorCache hashers;
863 VP8LRefsCursor c = VP8LRefsCursorInit(refs);
864 if (!VP8LColorCacheInit(&hashers, cache_bits)) return 0;
865
866 while (VP8LRefsCursorOk(&c)) {
867 PixOrCopy* const v = c.cur_pos;
868 if (PixOrCopyIsLiteral(v)) {
869 const uint32_t argb_literal = v->argb_or_distance;
870 const int ix = VP8LColorCacheContains(&hashers, argb_literal);
871 if (ix >= 0) {
872 // hashers contains argb_literal
873 *v = PixOrCopyCreateCacheIdx(ix);
874 } else {
875 VP8LColorCacheInsert(&hashers, argb_literal);
876 }
877 ++pixel_index;
878 } else {
879 // refs was created without local cache, so it can not have cache indexes.
880 int k;
881 assert(PixOrCopyIsCopy(v));
882 for (k = 0; k < v->len; ++k) {
883 VP8LColorCacheInsert(&hashers, argb[pixel_index++]);
884 }
885 }
886 VP8LRefsCursorNext(&c);
887 }
888 VP8LColorCacheClear(&hashers);
889 return 1;
890 }
891
GetBackwardReferencesLowEffort(int width,int height,const uint32_t * const argb,int * const cache_bits,const VP8LHashChain * const hash_chain,VP8LBackwardRefs * const refs_lz77)892 static VP8LBackwardRefs* GetBackwardReferencesLowEffort(
893 int width, int height, const uint32_t* const argb,
894 int* const cache_bits, const VP8LHashChain* const hash_chain,
895 VP8LBackwardRefs* const refs_lz77) {
896 *cache_bits = 0;
897 if (!BackwardReferencesLz77(width, height, argb, 0, hash_chain, refs_lz77)) {
898 return NULL;
899 }
900 BackwardReferences2DLocality(width, refs_lz77);
901 return refs_lz77;
902 }
903
904 extern int VP8LBackwardReferencesTraceBackwards(
905 int xsize, int ysize, const uint32_t* const argb, int cache_bits,
906 const VP8LHashChain* const hash_chain,
907 const VP8LBackwardRefs* const refs_src, VP8LBackwardRefs* const refs_dst);
GetBackwardReferences(int width,int height,const uint32_t * const argb,int quality,int lz77_types_to_try,int cache_bits_max,int do_no_cache,const VP8LHashChain * const hash_chain,VP8LBackwardRefs * const refs,int * const cache_bits_best)908 static int GetBackwardReferences(int width, int height,
909 const uint32_t* const argb, int quality,
910 int lz77_types_to_try, int cache_bits_max,
911 int do_no_cache,
912 const VP8LHashChain* const hash_chain,
913 VP8LBackwardRefs* const refs,
914 int* const cache_bits_best) {
915 VP8LHistogram* histo = NULL;
916 int i, lz77_type;
917 // Index 0 is for a color cache, index 1 for no cache (if needed).
918 int lz77_types_best[2] = {0, 0};
919 uint64_t bit_costs_best[2] = {WEBP_UINT64_MAX, WEBP_UINT64_MAX};
920 VP8LHashChain hash_chain_box;
921 VP8LBackwardRefs* const refs_tmp = &refs[do_no_cache ? 2 : 1];
922 int status = 0;
923 memset(&hash_chain_box, 0, sizeof(hash_chain_box));
924
925 histo = VP8LAllocateHistogram(MAX_COLOR_CACHE_BITS);
926 if (histo == NULL) goto Error;
927
928 for (lz77_type = 1; lz77_types_to_try;
929 lz77_types_to_try &= ~lz77_type, lz77_type <<= 1) {
930 int res = 0;
931 uint64_t bit_cost = 0u;
932 if ((lz77_types_to_try & lz77_type) == 0) continue;
933 switch (lz77_type) {
934 case kLZ77RLE:
935 res = BackwardReferencesRle(width, height, argb, 0, refs_tmp);
936 break;
937 case kLZ77Standard:
938 // Compute LZ77 with no cache (0 bits), as the ideal LZ77 with a color
939 // cache is not that different in practice.
940 res = BackwardReferencesLz77(width, height, argb, 0, hash_chain,
941 refs_tmp);
942 break;
943 case kLZ77Box:
944 if (!VP8LHashChainInit(&hash_chain_box, width * height)) goto Error;
945 res = BackwardReferencesLz77Box(width, height, argb, 0, hash_chain,
946 &hash_chain_box, refs_tmp);
947 break;
948 default:
949 assert(0);
950 }
951 if (!res) goto Error;
952
953 // Start with the no color cache case.
954 for (i = 1; i >= 0; --i) {
955 int cache_bits = (i == 1) ? 0 : cache_bits_max;
956
957 if (i == 1 && !do_no_cache) continue;
958
959 if (i == 0) {
960 // Try with a color cache.
961 if (!CalculateBestCacheSize(argb, quality, refs_tmp, &cache_bits)) {
962 goto Error;
963 }
964 if (cache_bits > 0) {
965 if (!BackwardRefsWithLocalCache(argb, cache_bits, refs_tmp)) {
966 goto Error;
967 }
968 }
969 }
970
971 if (i == 0 && do_no_cache && cache_bits == 0) {
972 // No need to re-compute bit_cost as it was computed at i == 1.
973 } else {
974 VP8LHistogramCreate(histo, refs_tmp, cache_bits);
975 bit_cost = VP8LHistogramEstimateBits(histo);
976 }
977
978 if (bit_cost < bit_costs_best[i]) {
979 if (i == 1) {
980 // Do not swap as the full cache analysis would have the wrong
981 // VP8LBackwardRefs to start with.
982 if (!BackwardRefsClone(refs_tmp, &refs[1])) goto Error;
983 } else {
984 BackwardRefsSwap(refs_tmp, &refs[0]);
985 }
986 bit_costs_best[i] = bit_cost;
987 lz77_types_best[i] = lz77_type;
988 if (i == 0) *cache_bits_best = cache_bits;
989 }
990 }
991 }
992 assert(lz77_types_best[0] > 0);
993 assert(!do_no_cache || lz77_types_best[1] > 0);
994
995 // Improve on simple LZ77 but only for high quality (TraceBackwards is
996 // costly).
997 for (i = 1; i >= 0; --i) {
998 if (i == 1 && !do_no_cache) continue;
999 if ((lz77_types_best[i] == kLZ77Standard ||
1000 lz77_types_best[i] == kLZ77Box) &&
1001 quality >= 25) {
1002 const VP8LHashChain* const hash_chain_tmp =
1003 (lz77_types_best[i] == kLZ77Standard) ? hash_chain : &hash_chain_box;
1004 const int cache_bits = (i == 1) ? 0 : *cache_bits_best;
1005 uint64_t bit_cost_trace;
1006 if (!VP8LBackwardReferencesTraceBackwards(width, height, argb, cache_bits,
1007 hash_chain_tmp, &refs[i],
1008 refs_tmp)) {
1009 goto Error;
1010 }
1011 VP8LHistogramCreate(histo, refs_tmp, cache_bits);
1012 bit_cost_trace = VP8LHistogramEstimateBits(histo);
1013 if (bit_cost_trace < bit_costs_best[i]) {
1014 BackwardRefsSwap(refs_tmp, &refs[i]);
1015 }
1016 }
1017
1018 BackwardReferences2DLocality(width, &refs[i]);
1019
1020 if (i == 1 && lz77_types_best[0] == lz77_types_best[1] &&
1021 *cache_bits_best == 0) {
1022 // If the best cache size is 0 and we have the same best LZ77, just copy
1023 // the data over and stop here.
1024 if (!BackwardRefsClone(&refs[1], &refs[0])) goto Error;
1025 break;
1026 }
1027 }
1028 status = 1;
1029
1030 Error:
1031 VP8LHashChainClear(&hash_chain_box);
1032 VP8LFreeHistogram(histo);
1033 return status;
1034 }
1035
VP8LGetBackwardReferences(int width,int height,const uint32_t * const argb,int quality,int low_effort,int lz77_types_to_try,int cache_bits_max,int do_no_cache,const VP8LHashChain * const hash_chain,VP8LBackwardRefs * const refs,int * const cache_bits_best,const WebPPicture * const pic,int percent_range,int * const percent)1036 int VP8LGetBackwardReferences(
1037 int width, int height, const uint32_t* const argb, int quality,
1038 int low_effort, int lz77_types_to_try, int cache_bits_max, int do_no_cache,
1039 const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs,
1040 int* const cache_bits_best, const WebPPicture* const pic, int percent_range,
1041 int* const percent) {
1042 if (low_effort) {
1043 VP8LBackwardRefs* refs_best;
1044 *cache_bits_best = cache_bits_max;
1045 refs_best = GetBackwardReferencesLowEffort(
1046 width, height, argb, cache_bits_best, hash_chain, refs);
1047 if (refs_best == NULL) {
1048 return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1049 }
1050 // Set it in first position.
1051 BackwardRefsSwap(refs_best, &refs[0]);
1052 } else {
1053 if (!GetBackwardReferences(width, height, argb, quality, lz77_types_to_try,
1054 cache_bits_max, do_no_cache, hash_chain, refs,
1055 cache_bits_best)) {
1056 return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1057 }
1058 }
1059
1060 return WebPReportProgress(pic, *percent + percent_range, percent);
1061 }
1062