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

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

Issue 2149863002: libwebp: update to v0.5.1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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/backward_references.h ('k') | third_party/libwebp/enc/filter.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 Google Inc. All Rights Reserved. 1 // Copyright 2012 Google Inc. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 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 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 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 // 9 //
10 // Author: Jyrki Alakuijala (jyrki@google.com) 10 // Author: Jyrki Alakuijala (jyrki@google.com)
11 // 11 //
12 12
13 #include <assert.h> 13 #include <assert.h>
14 #include <math.h> 14 #include <math.h>
15 15
16 #include "./backward_references.h" 16 #include "./backward_references.h"
17 #include "./histogram.h" 17 #include "./histogram.h"
18 #include "../dsp/lossless.h" 18 #include "../dsp/lossless.h"
19 #include "../dsp/dsp.h" 19 #include "../dsp/dsp.h"
20 #include "../utils/color_cache.h" 20 #include "../utils/color_cache.h"
21 #include "../utils/utils.h" 21 #include "../utils/utils.h"
22 22
23 #define VALUES_IN_BYTE 256 23 #define VALUES_IN_BYTE 256
24 24
25 #define MIN_BLOCK_SIZE 256 // minimum block size for backward references 25 #define MIN_BLOCK_SIZE 256 // minimum block size for backward references
26 26
27 #define MAX_ENTROPY (1e30f) 27 #define MAX_ENTROPY (1e30f)
28 28
29 // 1M window (4M bytes) minus 120 special codes for short distances. 29 // 1M window (4M bytes) minus 120 special codes for short distances.
30 #define WINDOW_SIZE ((1 << 20) - 120) 30 #define WINDOW_SIZE_BITS 20
31 #define WINDOW_SIZE ((1 << WINDOW_SIZE_BITS) - 120)
31 32
32 // Bounds for the match length. 33 // Bounds for the match length.
33 #define MIN_LENGTH 2 34 #define MIN_LENGTH 2
34 #define MAX_LENGTH 4096 35 // If you change this, you need MAX_LENGTH_BITS + WINDOW_SIZE_BITS <= 32 as it
36 // is used in VP8LHashChain.
37 #define MAX_LENGTH_BITS 12
38 // We want the max value to be attainable and stored in MAX_LENGTH_BITS bits.
39 #define MAX_LENGTH ((1 << MAX_LENGTH_BITS) - 1)
40 #if MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32
41 #error "MAX_LENGTH_BITS + WINDOW_SIZE_BITS > 32"
42 #endif
35 43
36 // ----------------------------------------------------------------------------- 44 // -----------------------------------------------------------------------------
37 45
38 static const uint8_t plane_to_code_lut[128] = { 46 static const uint8_t plane_to_code_lut[128] = {
39 96, 73, 55, 39, 23, 13, 5, 1, 255, 255, 255, 255, 255, 255, 255, 255, 47 96, 73, 55, 39, 23, 13, 5, 1, 255, 255, 255, 255, 255, 255, 255, 255,
40 101, 78, 58, 42, 26, 16, 8, 2, 0, 3, 9, 17, 27, 43, 59, 79, 48 101, 78, 58, 42, 26, 16, 8, 2, 0, 3, 9, 17, 27, 43, 59, 79,
41 102, 86, 62, 46, 32, 20, 10, 6, 4, 7, 11, 21, 33, 47, 63, 87, 49 102, 86, 62, 46, 32, 20, 10, 6, 4, 7, 11, 21, 33, 47, 63, 87,
42 105, 90, 70, 52, 37, 28, 18, 14, 12, 15, 19, 29, 38, 53, 71, 91, 50 105, 90, 70, 52, 37, 28, 18, 14, 12, 15, 19, 29, 38, 53, 71, 91,
43 110, 99, 82, 66, 48, 35, 30, 24, 22, 25, 31, 36, 49, 67, 83, 100, 51 110, 99, 82, 66, 48, 35, 30, 24, 22, 25, 31, 36, 49, 67, 83, 100,
44 115, 108, 94, 76, 64, 50, 44, 40, 34, 41, 45, 51, 65, 77, 95, 109, 52 115, 108, 94, 76, 64, 50, 44, 40, 34, 41, 45, 51, 65, 77, 95, 109,
45 118, 113, 103, 92, 80, 68, 60, 56, 54, 57, 61, 69, 81, 93, 104, 114, 53 118, 113, 103, 92, 80, 68, 60, 56, 54, 57, 61, 69, 81, 93, 104, 114,
46 119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117 54 119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117
47 }; 55 };
48 56
49 static int DistanceToPlaneCode(int xsize, int dist) { 57 static int DistanceToPlaneCode(int xsize, int dist) {
50 const int yoffset = dist / xsize; 58 const int yoffset = dist / xsize;
51 const int xoffset = dist - yoffset * xsize; 59 const int xoffset = dist - yoffset * xsize;
52 if (xoffset <= 8 && yoffset < 8) { 60 if (xoffset <= 8 && yoffset < 8) {
53 return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1; 61 return plane_to_code_lut[yoffset * 16 + 8 - xoffset] + 1;
54 } else if (xoffset > xsize - 8 && yoffset < 7) { 62 } else if (xoffset > xsize - 8 && yoffset < 7) {
55 return plane_to_code_lut[(yoffset + 1) * 16 + 8 + (xsize - xoffset)] + 1; 63 return plane_to_code_lut[(yoffset + 1) * 16 + 8 + (xsize - xoffset)] + 1;
56 } 64 }
57 return dist + 120; 65 return dist + 120;
58 } 66 }
59 67
60 // Returns the exact index where array1 and array2 are different if this 68 // Returns the exact index where array1 and array2 are different. For an index
61 // index is strictly superior to best_len_match. Otherwise, it returns 0. 69 // inferior or equal to best_len_match, the return value just has to be strictly
70 // inferior to best_len_match. The current behavior is to return 0 if this index
71 // is best_len_match, and the index itself otherwise.
62 // If no two elements are the same, it returns max_limit. 72 // If no two elements are the same, it returns max_limit.
63 static WEBP_INLINE int FindMatchLength(const uint32_t* const array1, 73 static WEBP_INLINE int FindMatchLength(const uint32_t* const array1,
64 const uint32_t* const array2, 74 const uint32_t* const array2,
65 int best_len_match, 75 int best_len_match, int max_limit) {
66 int max_limit) {
67 int match_len;
68
69 // Before 'expensive' linear match, check if the two arrays match at the 76 // Before 'expensive' linear match, check if the two arrays match at the
70 // current best length index. 77 // current best length index.
71 if (array1[best_len_match] != array2[best_len_match]) return 0; 78 if (array1[best_len_match] != array2[best_len_match]) return 0;
72 79
73 #if defined(WEBP_USE_SSE2) 80 return VP8LVectorMismatch(array1, array2, max_limit);
74 // Check if anything is different up to best_len_match excluded.
75 // memcmp seems to be slower on ARM so it is disabled for now.
76 if (memcmp(array1, array2, best_len_match * sizeof(*array1))) return 0;
77 match_len = best_len_match + 1;
78 #else
79 match_len = 0;
80 #endif
81
82 while (match_len < max_limit && array1[match_len] == array2[match_len]) {
83 ++match_len;
84 }
85 return match_len;
86 } 81 }
87 82
88 // ----------------------------------------------------------------------------- 83 // -----------------------------------------------------------------------------
89 // VP8LBackwardRefs 84 // VP8LBackwardRefs
90 85
91 struct PixOrCopyBlock { 86 struct PixOrCopyBlock {
92 PixOrCopyBlock* next_; // next block (or NULL) 87 PixOrCopyBlock* next_; // next block (or NULL)
93 PixOrCopy* start_; // data start 88 PixOrCopy* start_; // data start
94 int size_; // currently used size 89 int size_; // currently used size
95 }; 90 };
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 memcpy(new_b->start_, b->start_, b->size_ * sizeof(*b->start_)); 182 memcpy(new_b->start_, b->start_, b->size_ * sizeof(*b->start_));
188 new_b->size_ = b->size_; 183 new_b->size_ = b->size_;
189 b = b->next_; 184 b = b->next_;
190 } 185 }
191 return 1; 186 return 1;
192 } 187 }
193 188
194 // ----------------------------------------------------------------------------- 189 // -----------------------------------------------------------------------------
195 // Hash chains 190 // Hash chains
196 191
197 // initialize as empty
198 static void HashChainReset(VP8LHashChain* const p) {
199 assert(p != NULL);
200 // Set the int32_t arrays to -1.
201 memset(p->chain_, 0xff, p->size_ * sizeof(*p->chain_));
202 memset(p->hash_to_first_index_, 0xff,
203 HASH_SIZE * sizeof(*p->hash_to_first_index_));
204 }
205
206 int VP8LHashChainInit(VP8LHashChain* const p, int size) { 192 int VP8LHashChainInit(VP8LHashChain* const p, int size) {
207 assert(p->size_ == 0); 193 assert(p->size_ == 0);
208 assert(p->chain_ == NULL); 194 assert(p->offset_length_ == NULL);
209 assert(size > 0); 195 assert(size > 0);
210 p->chain_ = (int*)WebPSafeMalloc(size, sizeof(*p->chain_)); 196 p->offset_length_ =
211 if (p->chain_ == NULL) return 0; 197 (uint32_t*)WebPSafeMalloc(size, sizeof(*p->offset_length_));
198 if (p->offset_length_ == NULL) return 0;
212 p->size_ = size; 199 p->size_ = size;
213 HashChainReset(p); 200
214 return 1; 201 return 1;
215 } 202 }
216 203
217 void VP8LHashChainClear(VP8LHashChain* const p) { 204 void VP8LHashChainClear(VP8LHashChain* const p) {
218 assert(p != NULL); 205 assert(p != NULL);
219 WebPSafeFree(p->chain_); 206 WebPSafeFree(p->offset_length_);
207
220 p->size_ = 0; 208 p->size_ = 0;
221 p->chain_ = NULL; 209 p->offset_length_ = NULL;
222 } 210 }
223 211
224 // ----------------------------------------------------------------------------- 212 // -----------------------------------------------------------------------------
225 213
226 #define HASH_MULTIPLIER_HI (0xc6a4a793U) 214 #define HASH_MULTIPLIER_HI (0xc6a4a793U)
227 #define HASH_MULTIPLIER_LO (0x5bd1e996U) 215 #define HASH_MULTIPLIER_LO (0x5bd1e996U)
228 216
229 static WEBP_INLINE uint32_t GetPixPairHash64(const uint32_t* const argb) { 217 static WEBP_INLINE uint32_t GetPixPairHash64(const uint32_t* const argb) {
230 uint32_t key; 218 uint32_t key;
231 key = argb[1] * HASH_MULTIPLIER_HI; 219 key = argb[1] * HASH_MULTIPLIER_HI;
232 key += argb[0] * HASH_MULTIPLIER_LO; 220 key += argb[0] * HASH_MULTIPLIER_LO;
233 key = key >> (32 - HASH_BITS); 221 key = key >> (32 - HASH_BITS);
234 return key; 222 return key;
235 } 223 }
236 224
237 // Insertion of two pixels at a time.
238 static void HashChainInsert(VP8LHashChain* const p,
239 const uint32_t* const argb, int pos) {
240 const uint32_t hash_code = GetPixPairHash64(argb);
241 p->chain_[pos] = p->hash_to_first_index_[hash_code];
242 p->hash_to_first_index_[hash_code] = pos;
243 }
244
245 // Returns the maximum number of hash chain lookups to do for a 225 // Returns the maximum number of hash chain lookups to do for a
246 // given compression quality. Return value in range [6, 86]. 226 // given compression quality. Return value in range [8, 86].
247 static int GetMaxItersForQuality(int quality, int low_effort) { 227 static int GetMaxItersForQuality(int quality) {
248 return (low_effort ? 6 : 8) + (quality * quality) / 128; 228 return 8 + (quality * quality) / 128;
249 } 229 }
250 230
251 static int GetWindowSizeForHashChain(int quality, int xsize) { 231 static int GetWindowSizeForHashChain(int quality, int xsize) {
252 const int max_window_size = (quality > 75) ? WINDOW_SIZE 232 const int max_window_size = (quality > 75) ? WINDOW_SIZE
253 : (quality > 50) ? (xsize << 8) 233 : (quality > 50) ? (xsize << 8)
254 : (quality > 25) ? (xsize << 6) 234 : (quality > 25) ? (xsize << 6)
255 : (xsize << 4); 235 : (xsize << 4);
256 assert(xsize > 0); 236 assert(xsize > 0);
257 return (max_window_size > WINDOW_SIZE) ? WINDOW_SIZE : max_window_size; 237 return (max_window_size > WINDOW_SIZE) ? WINDOW_SIZE : max_window_size;
258 } 238 }
259 239
260 static WEBP_INLINE int MaxFindCopyLength(int len) { 240 static WEBP_INLINE int MaxFindCopyLength(int len) {
261 return (len < MAX_LENGTH) ? len : MAX_LENGTH; 241 return (len < MAX_LENGTH) ? len : MAX_LENGTH;
262 } 242 }
263 243
264 static void HashChainFindOffset(const VP8LHashChain* const p, int base_position, 244 int VP8LHashChainFill(VP8LHashChain* const p, int quality,
265 const uint32_t* const argb, int len, 245 const uint32_t* const argb, int xsize, int ysize) {
266 int window_size, int* const distance_ptr) { 246 const int size = xsize * ysize;
267 const uint32_t* const argb_start = argb + base_position; 247 const int iter_max = GetMaxItersForQuality(quality);
268 const int min_pos = 248 const int iter_min = iter_max - quality / 10;
269 (base_position > window_size) ? base_position - window_size : 0; 249 const uint32_t window_size = GetWindowSizeForHashChain(quality, xsize);
270 int pos; 250 int pos;
271 assert(len <= MAX_LENGTH); 251 uint32_t base_position;
272 for (pos = p->hash_to_first_index_[GetPixPairHash64(argb_start)]; 252 int32_t* hash_to_first_index;
273 pos >= min_pos; 253 // Temporarily use the p->offset_length_ as a hash chain.
274 pos = p->chain_[pos]) { 254 int32_t* chain = (int32_t*)p->offset_length_;
275 const int curr_length = 255 assert(p->size_ != 0);
276 FindMatchLength(argb + pos, argb_start, len - 1, len); 256 assert(p->offset_length_ != NULL);
277 if (curr_length == len) break; 257
258 hash_to_first_index =
259 (int32_t*)WebPSafeMalloc(HASH_SIZE, sizeof(*hash_to_first_index));
260 if (hash_to_first_index == NULL) return 0;
261
262 // Set the int32_t array to -1.
263 memset(hash_to_first_index, 0xff, HASH_SIZE * sizeof(*hash_to_first_index));
264 // Fill the chain linking pixels with the same hash.
265 for (pos = 0; pos < size - 1; ++pos) {
266 const uint32_t hash_code = GetPixPairHash64(argb + pos);
267 chain[pos] = hash_to_first_index[hash_code];
268 hash_to_first_index[hash_code] = pos;
278 } 269 }
279 *distance_ptr = base_position - pos; 270 WebPSafeFree(hash_to_first_index);
280 }
281 271
282 static int HashChainFindCopy(const VP8LHashChain* const p, 272 // Find the best match interval at each pixel, defined by an offset to the
283 int base_position, 273 // pixel and a length. The right-most pixel cannot match anything to the right
284 const uint32_t* const argb, int max_len, 274 // (hence a best length of 0) and the left-most pixel nothing to the left
285 int window_size, int iter_max, 275 // (hence an offset of 0).
286 int* const distance_ptr, 276 p->offset_length_[0] = p->offset_length_[size - 1] = 0;
287 int* const length_ptr) { 277 for (base_position = size - 2 < 0 ? 0 : size - 2; base_position > 0;) {
288 const uint32_t* const argb_start = argb + base_position; 278 const int max_len = MaxFindCopyLength(size - 1 - base_position);
289 int iter = iter_max; 279 const uint32_t* const argb_start = argb + base_position;
290 int best_length = 0; 280 int iter = iter_max;
291 int best_distance = 0; 281 int best_length = 0;
292 const int min_pos = 282 uint32_t best_distance = 0;
293 (base_position > window_size) ? base_position - window_size : 0; 283 const int min_pos =
294 int pos; 284 (base_position > window_size) ? base_position - window_size : 0;
295 int length_max = 256; 285 const int length_max = (max_len < 256) ? max_len : 256;
296 if (max_len < length_max) { 286 uint32_t max_base_position;
297 length_max = max_len; 287
298 } 288 for (pos = chain[base_position]; pos >= min_pos; pos = chain[pos]) {
299 for (pos = p->hash_to_first_index_[GetPixPairHash64(argb_start)]; 289 int curr_length;
300 pos >= min_pos; 290 if (--iter < 0) {
301 pos = p->chain_[pos]) { 291 break;
302 int curr_length; 292 }
303 int distance; 293 assert(base_position > (uint32_t)pos);
304 if (--iter < 0) { 294
305 break; 295 curr_length =
296 FindMatchLength(argb + pos, argb_start, best_length, max_len);
297 if (best_length < curr_length) {
298 best_length = curr_length;
299 best_distance = base_position - pos;
300 // Stop if we have reached the maximum length. Otherwise, make sure
301 // we have executed a minimum number of iterations depending on the
302 // quality.
303 if ((best_length == MAX_LENGTH) ||
304 (curr_length >= length_max && iter < iter_min)) {
305 break;
306 }
307 }
306 } 308 }
307 309 // We have the best match but in case the two intervals continue matching
308 curr_length = FindMatchLength(argb + pos, argb_start, best_length, max_len); 310 // to the left, we have the best matches for the left-extended pixels.
309 if (best_length < curr_length) { 311 max_base_position = base_position;
310 distance = base_position - pos; 312 while (1) {
311 best_length = curr_length; 313 assert(best_length <= MAX_LENGTH);
312 best_distance = distance; 314 assert(best_distance <= WINDOW_SIZE);
313 if (curr_length >= length_max) { 315 p->offset_length_[base_position] =
316 (best_distance << MAX_LENGTH_BITS) | (uint32_t)best_length;
317 --base_position;
318 // Stop if we don't have a match or if we are out of bounds.
319 if (best_distance == 0 || base_position == 0) break;
320 // Stop if we cannot extend the matching intervals to the left.
321 if (base_position < best_distance ||
322 argb[base_position - best_distance] != argb[base_position]) {
314 break; 323 break;
315 } 324 }
325 // Stop if we are matching at its limit because there could be a closer
326 // matching interval with the same maximum length. Then again, if the
327 // matching interval is as close as possible (best_distance == 1), we will
328 // never find anything better so let's continue.
329 if (best_length == MAX_LENGTH && best_distance != 1 &&
330 base_position + MAX_LENGTH < max_base_position) {
331 break;
332 }
333 if (best_length < MAX_LENGTH) {
334 ++best_length;
335 max_base_position = base_position;
336 }
316 } 337 }
317 } 338 }
318 *distance_ptr = best_distance; 339 return 1;
319 *length_ptr = best_length; 340 }
320 return (best_length >= MIN_LENGTH); 341
342 static WEBP_INLINE int HashChainFindOffset(const VP8LHashChain* const p,
343 const int base_position) {
344 return p->offset_length_[base_position] >> MAX_LENGTH_BITS;
345 }
346
347 static WEBP_INLINE int HashChainFindLength(const VP8LHashChain* const p,
348 const int base_position) {
349 return p->offset_length_[base_position] & ((1U << MAX_LENGTH_BITS) - 1);
350 }
351
352 static WEBP_INLINE void HashChainFindCopy(const VP8LHashChain* const p,
353 int base_position,
354 int* const offset_ptr,
355 int* const length_ptr) {
356 *offset_ptr = HashChainFindOffset(p, base_position);
357 *length_ptr = HashChainFindLength(p, base_position);
321 } 358 }
322 359
323 static WEBP_INLINE void AddSingleLiteral(uint32_t pixel, int use_color_cache, 360 static WEBP_INLINE void AddSingleLiteral(uint32_t pixel, int use_color_cache,
324 VP8LColorCache* const hashers, 361 VP8LColorCache* const hashers,
325 VP8LBackwardRefs* const refs) { 362 VP8LBackwardRefs* const refs) {
326 PixOrCopy v; 363 PixOrCopy v;
327 if (use_color_cache) { 364 if (use_color_cache) {
328 const uint32_t key = VP8LColorCacheGetIndex(hashers, pixel); 365 const uint32_t key = VP8LColorCacheGetIndex(hashers, pixel);
329 if (VP8LColorCacheLookup(hashers, key) == pixel) { 366 if (VP8LColorCacheLookup(hashers, key) == pixel) {
330 v = PixOrCopyCreateCacheIdx(key); 367 v = PixOrCopyCreateCacheIdx(key);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs); 414 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
378 i++; 415 i++;
379 } 416 }
380 } 417 }
381 if (use_color_cache) VP8LColorCacheClear(&hashers); 418 if (use_color_cache) VP8LColorCacheClear(&hashers);
382 return !refs->error_; 419 return !refs->error_;
383 } 420 }
384 421
385 static int BackwardReferencesLz77(int xsize, int ysize, 422 static int BackwardReferencesLz77(int xsize, int ysize,
386 const uint32_t* const argb, int cache_bits, 423 const uint32_t* const argb, int cache_bits,
387 int quality, int low_effort, 424 const VP8LHashChain* const hash_chain,
388 VP8LHashChain* const hash_chain,
389 VP8LBackwardRefs* const refs) { 425 VP8LBackwardRefs* const refs) {
390 int i; 426 int i;
427 int i_last_check = -1;
391 int ok = 0; 428 int ok = 0;
392 int cc_init = 0; 429 int cc_init = 0;
393 const int use_color_cache = (cache_bits > 0); 430 const int use_color_cache = (cache_bits > 0);
394 const int pix_count = xsize * ysize; 431 const int pix_count = xsize * ysize;
395 VP8LColorCache hashers; 432 VP8LColorCache hashers;
396 int iter_max = GetMaxItersForQuality(quality, low_effort);
397 const int window_size = GetWindowSizeForHashChain(quality, xsize);
398 int min_matches = 32;
399 433
400 if (use_color_cache) { 434 if (use_color_cache) {
401 cc_init = VP8LColorCacheInit(&hashers, cache_bits); 435 cc_init = VP8LColorCacheInit(&hashers, cache_bits);
402 if (!cc_init) goto Error; 436 if (!cc_init) goto Error;
403 } 437 }
404 ClearBackwardRefs(refs); 438 ClearBackwardRefs(refs);
405 HashChainReset(hash_chain); 439 for (i = 0; i < pix_count;) {
406 for (i = 0; i < pix_count - 2; ) {
407 // Alternative#1: Code the pixels starting at 'i' using backward reference. 440 // Alternative#1: Code the pixels starting at 'i' using backward reference.
408 int offset = 0; 441 int offset = 0;
409 int len = 0; 442 int len = 0;
410 const int max_len = MaxFindCopyLength(pix_count - i); 443 int j;
411 HashChainFindCopy(hash_chain, i, argb, max_len, window_size, 444 HashChainFindCopy(hash_chain, i, &offset, &len);
412 iter_max, &offset, &len); 445 if (len > MIN_LENGTH + 1) {
413 if (len > MIN_LENGTH || (len == MIN_LENGTH && offset <= 512)) { 446 const int len_ini = len;
414 int offset2 = 0; 447 int max_reach = 0;
415 int len2 = 0; 448 assert(i + len < pix_count);
416 int k; 449 // Only start from what we have not checked already.
417 min_matches = 8; 450 i_last_check = (i > i_last_check) ? i : i_last_check;
418 HashChainInsert(hash_chain, &argb[i], i); 451 // We know the best match for the current pixel but we try to find the
419 if ((len < (max_len >> 2)) && !low_effort) { 452 // best matches for the current pixel AND the next one combined.
420 // Evaluate Alternative#2: Insert the pixel at 'i' as literal, and code 453 // The naive method would use the intervals:
421 // the pixels starting at 'i + 1' using backward reference. 454 // [i,i+len) + [i+len, length of best match at i+len)
422 HashChainFindCopy(hash_chain, i + 1, argb, max_len - 1, 455 // while we check if we can use:
423 window_size, iter_max, &offset2, 456 // [i,j) (where j<=i+len) + [j, length of best match at j)
424 &len2); 457 for (j = i_last_check + 1; j <= i + len_ini; ++j) {
425 if (len2 > len + 1) { 458 const int len_j = HashChainFindLength(hash_chain, j);
426 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs); 459 const int reach =
427 i++; // Backward reference to be done for next pixel. 460 j + (len_j > MIN_LENGTH + 1 ? len_j : 1); // 1 for single literal.
428 len = len2; 461 if (reach > max_reach) {
429 offset = offset2; 462 len = j - i;
463 max_reach = reach;
430 } 464 }
431 } 465 }
466 } else {
467 len = 1;
468 }
469 // Go with literal or backward reference.
470 assert(len > 0);
471 if (len == 1) {
472 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
473 } else {
432 BackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len)); 474 BackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len));
433 if (use_color_cache) { 475 if (use_color_cache) {
434 for (k = 0; k < len; ++k) { 476 for (j = i; j < i + len; ++j) VP8LColorCacheInsert(&hashers, argb[j]);
435 VP8LColorCacheInsert(&hashers, argb[i + k]);
436 }
437 }
438 // Add to the hash_chain (but cannot add the last pixel).
439 if (offset >= 3 && offset != xsize) {
440 const int last = (len < pix_count - 1 - i) ? len : pix_count - 1 - i;
441 for (k = 2; k < last - 8; k += 2) {
442 HashChainInsert(hash_chain, &argb[i + k], i + k);
443 }
444 for (; k < last; ++k) {
445 HashChainInsert(hash_chain, &argb[i + k], i + k);
446 }
447 }
448 i += len;
449 } else {
450 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
451 HashChainInsert(hash_chain, &argb[i], i);
452 ++i;
453 --min_matches;
454 if (min_matches <= 0) {
455 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
456 HashChainInsert(hash_chain, &argb[i], i);
457 ++i;
458 } 477 }
459 } 478 }
460 } 479 i += len;
461 while (i < pix_count) {
462 // Handle the last pixel(s).
463 AddSingleLiteral(argb[i], use_color_cache, &hashers, refs);
464 ++i;
465 } 480 }
466 481
467 ok = !refs->error_; 482 ok = !refs->error_;
468 Error: 483 Error:
469 if (cc_init) VP8LColorCacheClear(&hashers); 484 if (cc_init) VP8LColorCacheClear(&hashers);
470 return ok; 485 return ok;
471 } 486 }
472 487
473 // ----------------------------------------------------------------------------- 488 // -----------------------------------------------------------------------------
474 489
475 typedef struct { 490 typedef struct {
476 double alpha_[VALUES_IN_BYTE]; 491 double alpha_[VALUES_IN_BYTE];
477 double red_[VALUES_IN_BYTE]; 492 double red_[VALUES_IN_BYTE];
478 double blue_[VALUES_IN_BYTE]; 493 double blue_[VALUES_IN_BYTE];
479 double distance_[NUM_DISTANCE_CODES]; 494 double distance_[NUM_DISTANCE_CODES];
480 double* literal_; 495 double* literal_;
481 } CostModel; 496 } CostModel;
482 497
483 static int BackwardReferencesTraceBackwards( 498 static int BackwardReferencesTraceBackwards(
484 int xsize, int ysize, const uint32_t* const argb, int quality, 499 int xsize, int ysize, const uint32_t* const argb, int quality,
485 int cache_bits, VP8LHashChain* const hash_chain, 500 int cache_bits, const VP8LHashChain* const hash_chain,
486 VP8LBackwardRefs* const refs); 501 VP8LBackwardRefs* const refs);
487 502
488 static void ConvertPopulationCountTableToBitEstimates( 503 static void ConvertPopulationCountTableToBitEstimates(
489 int num_symbols, const uint32_t population_counts[], double output[]) { 504 int num_symbols, const uint32_t population_counts[], double output[]) {
490 uint32_t sum = 0; 505 uint32_t sum = 0;
491 int nonzeros = 0; 506 int nonzeros = 0;
492 int i; 507 int i;
493 for (i = 0; i < num_symbols; ++i) { 508 for (i = 0; i < num_symbols; ++i) {
494 sum += population_counts[i]; 509 sum += population_counts[i];
495 if (population_counts[i] > 0) { 510 if (population_counts[i] > 0) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 return m->literal_[VALUES_IN_BYTE + code] + extra_bits; 566 return m->literal_[VALUES_IN_BYTE + code] + extra_bits;
552 } 567 }
553 568
554 static WEBP_INLINE double GetDistanceCost(const CostModel* const m, 569 static WEBP_INLINE double GetDistanceCost(const CostModel* const m,
555 uint32_t distance) { 570 uint32_t distance) {
556 int code, extra_bits; 571 int code, extra_bits;
557 VP8LPrefixEncodeBits(distance, &code, &extra_bits); 572 VP8LPrefixEncodeBits(distance, &code, &extra_bits);
558 return m->distance_[code] + extra_bits; 573 return m->distance_[code] + extra_bits;
559 } 574 }
560 575
561 static void AddSingleLiteralWithCostModel( 576 static void AddSingleLiteralWithCostModel(const uint32_t* const argb,
562 const uint32_t* const argb, VP8LHashChain* const hash_chain, 577 VP8LColorCache* const hashers,
563 VP8LColorCache* const hashers, const CostModel* const cost_model, int idx, 578 const CostModel* const cost_model,
564 int is_last, int use_color_cache, double prev_cost, float* const cost, 579 int idx, int use_color_cache,
565 uint16_t* const dist_array) { 580 double prev_cost, float* const cost,
581 uint16_t* const dist_array) {
566 double cost_val = prev_cost; 582 double cost_val = prev_cost;
567 const uint32_t color = argb[0]; 583 const uint32_t color = argb[0];
568 if (!is_last) {
569 HashChainInsert(hash_chain, argb, idx);
570 }
571 if (use_color_cache && VP8LColorCacheContains(hashers, color)) { 584 if (use_color_cache && VP8LColorCacheContains(hashers, color)) {
572 const double mul0 = 0.68; 585 const double mul0 = 0.68;
573 const int ix = VP8LColorCacheGetIndex(hashers, color); 586 const int ix = VP8LColorCacheGetIndex(hashers, color);
574 cost_val += GetCacheCost(cost_model, ix) * mul0; 587 cost_val += GetCacheCost(cost_model, ix) * mul0;
575 } else { 588 } else {
576 const double mul1 = 0.82; 589 const double mul1 = 0.82;
577 if (use_color_cache) VP8LColorCacheInsert(hashers, color); 590 if (use_color_cache) VP8LColorCacheInsert(hashers, color);
578 cost_val += GetLiteralCost(cost_model, color) * mul1; 591 cost_val += GetLiteralCost(cost_model, color) * mul1;
579 } 592 }
580 if (cost[idx] > cost_val) { 593 if (cost[idx] > cost_val) {
581 cost[idx] = (float)cost_val; 594 cost[idx] = (float)cost_val;
582 dist_array[idx] = 1; // only one is inserted. 595 dist_array[idx] = 1; // only one is inserted.
583 } 596 }
584 } 597 }
585 598
599 // -----------------------------------------------------------------------------
600 // CostManager and interval handling
601
602 // Empirical value to avoid high memory consumption but good for performance.
603 #define COST_CACHE_INTERVAL_SIZE_MAX 100
604
605 // To perform backward reference every pixel at index index_ is considered and
606 // the cost for the MAX_LENGTH following pixels computed. Those following pixels
607 // at index index_ + k (k from 0 to MAX_LENGTH) have a cost of:
608 // distance_cost_ at index_ + GetLengthCost(cost_model, k)
609 // (named cost) (named cached cost)
610 // and the minimum value is kept. GetLengthCost(cost_model, k) is cached in an
611 // array of size MAX_LENGTH.
612 // Instead of performing MAX_LENGTH comparisons per pixel, we keep track of the
613 // minimal values using intervals, for which lower_ and upper_ bounds are kept.
614 // An interval is defined by the index_ of the pixel that generated it and
615 // is only useful in a range of indices from start_ to end_ (exclusive), i.e.
616 // it contains the minimum value for pixels between start_ and end_.
617 // Intervals are stored in a linked list and ordered by start_. When a new
618 // interval has a better minimum, old intervals are split or removed.
619 typedef struct CostInterval CostInterval;
620 struct CostInterval {
621 double lower_;
622 double upper_;
623 int start_;
624 int end_;
625 double distance_cost_;
626 int index_;
627 CostInterval* previous_;
628 CostInterval* next_;
629 };
630
631 // The GetLengthCost(cost_model, k) part of the costs is also bounded for
632 // efficiency in a set of intervals of a different type.
633 // If those intervals are small enough, they are not used for comparison and
634 // written into the costs right away.
635 typedef struct {
636 double lower_; // Lower bound of the interval.
637 double upper_; // Upper bound of the interval.
638 int start_;
639 int end_; // Exclusive.
640 int do_write_; // If !=0, the interval is saved to cost instead of being kept
641 // for comparison.
642 } CostCacheInterval;
643
644 // This structure is in charge of managing intervals and costs.
645 // It caches the different CostCacheInterval, caches the different
646 // GetLengthCost(cost_model, k) in cost_cache_ and the CostInterval's (whose
647 // count_ is limited by COST_CACHE_INTERVAL_SIZE_MAX).
648 #define COST_MANAGER_MAX_FREE_LIST 10
649 typedef struct {
650 CostInterval* head_;
651 int count_; // The number of stored intervals.
652 CostCacheInterval* cache_intervals_;
653 size_t cache_intervals_size_;
654 double cost_cache_[MAX_LENGTH]; // Contains the GetLengthCost(cost_model, k).
655 double min_cost_cache_; // The minimum value in cost_cache_[1:].
656 double max_cost_cache_; // The maximum value in cost_cache_[1:].
657 float* costs_;
658 uint16_t* dist_array_;
659 // Most of the time, we only need few intervals -> use a free-list, to avoid
660 // fragmentation with small allocs in most common cases.
661 CostInterval intervals_[COST_MANAGER_MAX_FREE_LIST];
662 CostInterval* free_intervals_;
663 // These are regularly malloc'd remains. This list can't grow larger than than
664 // size COST_CACHE_INTERVAL_SIZE_MAX - COST_MANAGER_MAX_FREE_LIST, note.
665 CostInterval* recycled_intervals_;
666 // Buffer used in BackwardReferencesHashChainDistanceOnly to store the ends
667 // of the intervals that can have impacted the cost at a pixel.
668 int* interval_ends_;
669 int interval_ends_size_;
670 } CostManager;
671
672 static int IsCostCacheIntervalWritable(int start, int end) {
673 // 100 is the length for which we consider an interval for comparison, and not
674 // for writing.
675 // The first intervals are very small and go in increasing size. This constant
676 // helps merging them into one big interval (up to index 150/200 usually from
677 // which intervals start getting much bigger).
678 // This value is empirical.
679 return (end - start + 1 < 100);
680 }
681
682 static void CostIntervalAddToFreeList(CostManager* const manager,
683 CostInterval* const interval) {
684 interval->next_ = manager->free_intervals_;
685 manager->free_intervals_ = interval;
686 }
687
688 static int CostIntervalIsInFreeList(const CostManager* const manager,
689 const CostInterval* const interval) {
690 return (interval >= &manager->intervals_[0] &&
691 interval <= &manager->intervals_[COST_MANAGER_MAX_FREE_LIST - 1]);
692 }
693
694 static void CostManagerInitFreeList(CostManager* const manager) {
695 int i;
696 manager->free_intervals_ = NULL;
697 for (i = 0; i < COST_MANAGER_MAX_FREE_LIST; ++i) {
698 CostIntervalAddToFreeList(manager, &manager->intervals_[i]);
699 }
700 }
701
702 static void DeleteIntervalList(CostManager* const manager,
703 const CostInterval* interval) {
704 while (interval != NULL) {
705 const CostInterval* const next = interval->next_;
706 if (!CostIntervalIsInFreeList(manager, interval)) {
707 WebPSafeFree((void*)interval);
708 } // else: do nothing
709 interval = next;
710 }
711 }
712
713 static void CostManagerClear(CostManager* const manager) {
714 if (manager == NULL) return;
715
716 WebPSafeFree(manager->costs_);
717 WebPSafeFree(manager->cache_intervals_);
718 WebPSafeFree(manager->interval_ends_);
719
720 // Clear the interval lists.
721 DeleteIntervalList(manager, manager->head_);
722 manager->head_ = NULL;
723 DeleteIntervalList(manager, manager->recycled_intervals_);
724 manager->recycled_intervals_ = NULL;
725
726 // Reset pointers, count_ and cache_intervals_size_.
727 memset(manager, 0, sizeof(*manager));
728 CostManagerInitFreeList(manager);
729 }
730
731 static int CostManagerInit(CostManager* const manager,
732 uint16_t* const dist_array, int pix_count,
733 const CostModel* const cost_model) {
734 int i;
735 const int cost_cache_size = (pix_count > MAX_LENGTH) ? MAX_LENGTH : pix_count;
736 // This constant is tied to the cost_model we use.
737 // Empirically, differences between intervals is usually of more than 1.
738 const double min_cost_diff = 0.1;
739
740 manager->costs_ = NULL;
741 manager->cache_intervals_ = NULL;
742 manager->interval_ends_ = NULL;
743 manager->head_ = NULL;
744 manager->recycled_intervals_ = NULL;
745 manager->count_ = 0;
746 manager->dist_array_ = dist_array;
747 CostManagerInitFreeList(manager);
748
749 // Fill in the cost_cache_.
750 manager->cache_intervals_size_ = 1;
751 manager->cost_cache_[0] = 0;
752 for (i = 1; i < cost_cache_size; ++i) {
753 manager->cost_cache_[i] = GetLengthCost(cost_model, i);
754 // Get an approximation of the number of bound intervals.
755 if (fabs(manager->cost_cache_[i] - manager->cost_cache_[i - 1]) >
756 min_cost_diff) {
757 ++manager->cache_intervals_size_;
758 }
759 // Compute the minimum of cost_cache_.
760 if (i == 1) {
761 manager->min_cost_cache_ = manager->cost_cache_[1];
762 manager->max_cost_cache_ = manager->cost_cache_[1];
763 } else if (manager->cost_cache_[i] < manager->min_cost_cache_) {
764 manager->min_cost_cache_ = manager->cost_cache_[i];
765 } else if (manager->cost_cache_[i] > manager->max_cost_cache_) {
766 manager->max_cost_cache_ = manager->cost_cache_[i];
767 }
768 }
769
770 // With the current cost models, we have 15 intervals, so we are safe by
771 // setting a maximum of COST_CACHE_INTERVAL_SIZE_MAX.
772 if (manager->cache_intervals_size_ > COST_CACHE_INTERVAL_SIZE_MAX) {
773 manager->cache_intervals_size_ = COST_CACHE_INTERVAL_SIZE_MAX;
774 }
775 manager->cache_intervals_ = (CostCacheInterval*)WebPSafeMalloc(
776 manager->cache_intervals_size_, sizeof(*manager->cache_intervals_));
777 if (manager->cache_intervals_ == NULL) {
778 CostManagerClear(manager);
779 return 0;
780 }
781
782 // Fill in the cache_intervals_.
783 {
784 double cost_prev = -1e38f; // unprobably low initial value
785 CostCacheInterval* prev = NULL;
786 CostCacheInterval* cur = manager->cache_intervals_;
787 const CostCacheInterval* const end =
788 manager->cache_intervals_ + manager->cache_intervals_size_;
789
790 // Consecutive values in cost_cache_ are compared and if a big enough
791 // difference is found, a new interval is created and bounded.
792 for (i = 0; i < cost_cache_size; ++i) {
793 const double cost_val = manager->cost_cache_[i];
794 if (i == 0 ||
795 (fabs(cost_val - cost_prev) > min_cost_diff && cur + 1 < end)) {
796 if (i > 1) {
797 const int is_writable =
798 IsCostCacheIntervalWritable(cur->start_, cur->end_);
799 // Merge with the previous interval if both are writable.
800 if (is_writable && cur != manager->cache_intervals_ &&
801 prev->do_write_) {
802 // Update the previous interval.
803 prev->end_ = cur->end_;
804 if (cur->lower_ < prev->lower_) {
805 prev->lower_ = cur->lower_;
806 } else if (cur->upper_ > prev->upper_) {
807 prev->upper_ = cur->upper_;
808 }
809 } else {
810 cur->do_write_ = is_writable;
811 prev = cur;
812 ++cur;
813 }
814 }
815 // Initialize an interval.
816 cur->start_ = i;
817 cur->do_write_ = 0;
818 cur->lower_ = cost_val;
819 cur->upper_ = cost_val;
820 } else {
821 // Update the current interval bounds.
822 if (cost_val < cur->lower_) {
823 cur->lower_ = cost_val;
824 } else if (cost_val > cur->upper_) {
825 cur->upper_ = cost_val;
826 }
827 }
828 cur->end_ = i + 1;
829 cost_prev = cost_val;
830 }
831 manager->cache_intervals_size_ = cur + 1 - manager->cache_intervals_;
832 }
833
834 manager->costs_ = (float*)WebPSafeMalloc(pix_count, sizeof(*manager->costs_));
835 if (manager->costs_ == NULL) {
836 CostManagerClear(manager);
837 return 0;
838 }
839 // Set the initial costs_ high for every pixel as we will keep the minimum.
840 for (i = 0; i < pix_count; ++i) manager->costs_[i] = 1e38f;
841
842 // The cost at pixel is influenced by the cost intervals from previous pixels.
843 // Let us take the specific case where the offset is the same (which actually
844 // happens a lot in case of uniform regions).
845 // pixel i contributes to j>i a cost of: offset cost + cost_cache_[j-i]
846 // pixel i+1 contributes to j>i a cost of: 2*offset cost + cost_cache_[j-i-1]
847 // pixel i+2 contributes to j>i a cost of: 3*offset cost + cost_cache_[j-i-2]
848 // and so on.
849 // A pixel i influences the following length(j) < MAX_LENGTH pixels. What is
850 // the value of j such that pixel i + j cannot influence any of those pixels?
851 // This value is such that:
852 // max of cost_cache_ < j*offset cost + min of cost_cache_
853 // (pixel i + j 's cost cannot beat the worst cost given by pixel i).
854 // This value will be used to optimize the cost computation in
855 // BackwardReferencesHashChainDistanceOnly.
856 {
857 // The offset cost is computed in GetDistanceCost and has a minimum value of
858 // the minimum in cost_model->distance_. The case where the offset cost is 0
859 // will be dealt with differently later so we are only interested in the
860 // minimum non-zero offset cost.
861 double offset_cost_min = 0.;
862 int size;
863 for (i = 0; i < NUM_DISTANCE_CODES; ++i) {
864 if (cost_model->distance_[i] != 0) {
865 if (offset_cost_min == 0.) {
866 offset_cost_min = cost_model->distance_[i];
867 } else if (cost_model->distance_[i] < offset_cost_min) {
868 offset_cost_min = cost_model->distance_[i];
869 }
870 }
871 }
872 // In case all the cost_model->distance_ is 0, the next non-zero cost we
873 // can have is from the extra bit in GetDistanceCost, hence 1.
874 if (offset_cost_min < 1.) offset_cost_min = 1.;
875
876 size = 1 + (int)ceil((manager->max_cost_cache_ - manager->min_cost_cache_) /
877 offset_cost_min);
878 // Empirically, we usually end up with a value below 100.
879 if (size > MAX_LENGTH) size = MAX_LENGTH;
880
881 manager->interval_ends_ =
882 (int*)WebPSafeMalloc(size, sizeof(*manager->interval_ends_));
883 if (manager->interval_ends_ == NULL) {
884 CostManagerClear(manager);
885 return 0;
886 }
887 manager->interval_ends_size_ = size;
888 }
889
890 return 1;
891 }
892
893 // Given the distance_cost for pixel 'index', update the cost at pixel 'i' if it
894 // is smaller than the previously computed value.
895 static WEBP_INLINE void UpdateCost(CostManager* const manager, int i, int index,
896 double distance_cost) {
897 int k = i - index;
898 double cost_tmp;
899 assert(k >= 0 && k < MAX_LENGTH);
900 cost_tmp = distance_cost + manager->cost_cache_[k];
901
902 if (manager->costs_[i] > cost_tmp) {
903 manager->costs_[i] = (float)cost_tmp;
904 manager->dist_array_[i] = k + 1;
905 }
906 }
907
908 // Given the distance_cost for pixel 'index', update the cost for all the pixels
909 // between 'start' and 'end' excluded.
910 static WEBP_INLINE void UpdateCostPerInterval(CostManager* const manager,
911 int start, int end, int index,
912 double distance_cost) {
913 int i;
914 for (i = start; i < end; ++i) UpdateCost(manager, i, index, distance_cost);
915 }
916
917 // Given two intervals, make 'prev' be the previous one of 'next' in 'manager'.
918 static WEBP_INLINE void ConnectIntervals(CostManager* const manager,
919 CostInterval* const prev,
920 CostInterval* const next) {
921 if (prev != NULL) {
922 prev->next_ = next;
923 } else {
924 manager->head_ = next;
925 }
926
927 if (next != NULL) next->previous_ = prev;
928 }
929
930 // Pop an interval in the manager.
931 static WEBP_INLINE void PopInterval(CostManager* const manager,
932 CostInterval* const interval) {
933 CostInterval* const next = interval->next_;
934
935 if (interval == NULL) return;
936
937 ConnectIntervals(manager, interval->previous_, next);
938 if (CostIntervalIsInFreeList(manager, interval)) {
939 CostIntervalAddToFreeList(manager, interval);
940 } else { // recycle regularly malloc'd intervals too
941 interval->next_ = manager->recycled_intervals_;
942 manager->recycled_intervals_ = interval;
943 }
944 --manager->count_;
945 assert(manager->count_ >= 0);
946 }
947
948 // Update the cost at index i by going over all the stored intervals that
949 // overlap with i.
950 static WEBP_INLINE void UpdateCostPerIndex(CostManager* const manager, int i) {
951 CostInterval* current = manager->head_;
952
953 while (current != NULL && current->start_ <= i) {
954 if (current->end_ <= i) {
955 // We have an outdated interval, remove it.
956 CostInterval* next = current->next_;
957 PopInterval(manager, current);
958 current = next;
959 } else {
960 UpdateCost(manager, i, current->index_, current->distance_cost_);
961 current = current->next_;
962 }
963 }
964 }
965
966 // Given a current orphan interval and its previous interval, before
967 // it was orphaned (which can be NULL), set it at the right place in the list
968 // of intervals using the start_ ordering and the previous interval as a hint.
969 static WEBP_INLINE void PositionOrphanInterval(CostManager* const manager,
970 CostInterval* const current,
971 CostInterval* previous) {
972 assert(current != NULL);
973
974 if (previous == NULL) previous = manager->head_;
975 while (previous != NULL && current->start_ < previous->start_) {
976 previous = previous->previous_;
977 }
978 while (previous != NULL && previous->next_ != NULL &&
979 previous->next_->start_ < current->start_) {
980 previous = previous->next_;
981 }
982
983 if (previous != NULL) {
984 ConnectIntervals(manager, current, previous->next_);
985 } else {
986 ConnectIntervals(manager, current, manager->head_);
987 }
988 ConnectIntervals(manager, previous, current);
989 }
990
991 // Insert an interval in the list contained in the manager by starting at
992 // interval_in as a hint. The intervals are sorted by start_ value.
993 static WEBP_INLINE void InsertInterval(CostManager* const manager,
994 CostInterval* const interval_in,
995 double distance_cost, double lower,
996 double upper, int index, int start,
997 int end) {
998 CostInterval* interval_new;
999
1000 if (IsCostCacheIntervalWritable(start, end) ||
1001 manager->count_ >= COST_CACHE_INTERVAL_SIZE_MAX) {
1002 // Write down the interval if it is too small.
1003 UpdateCostPerInterval(manager, start, end, index, distance_cost);
1004 return;
1005 }
1006 if (manager->free_intervals_ != NULL) {
1007 interval_new = manager->free_intervals_;
1008 manager->free_intervals_ = interval_new->next_;
1009 } else if (manager->recycled_intervals_ != NULL) {
1010 interval_new = manager->recycled_intervals_;
1011 manager->recycled_intervals_ = interval_new->next_;
1012 } else { // malloc for good
1013 interval_new = (CostInterval*)WebPSafeMalloc(1, sizeof(*interval_new));
1014 if (interval_new == NULL) {
1015 // Write down the interval if we cannot create it.
1016 UpdateCostPerInterval(manager, start, end, index, distance_cost);
1017 return;
1018 }
1019 }
1020
1021 interval_new->distance_cost_ = distance_cost;
1022 interval_new->lower_ = lower;
1023 interval_new->upper_ = upper;
1024 interval_new->index_ = index;
1025 interval_new->start_ = start;
1026 interval_new->end_ = end;
1027 PositionOrphanInterval(manager, interval_new, interval_in);
1028
1029 ++manager->count_;
1030 }
1031
1032 // When an interval has its start_ or end_ modified, it needs to be
1033 // repositioned in the linked list.
1034 static WEBP_INLINE void RepositionInterval(CostManager* const manager,
1035 CostInterval* const interval) {
1036 if (IsCostCacheIntervalWritable(interval->start_, interval->end_)) {
1037 // Maybe interval has been resized and is small enough to be removed.
1038 UpdateCostPerInterval(manager, interval->start_, interval->end_,
1039 interval->index_, interval->distance_cost_);
1040 PopInterval(manager, interval);
1041 return;
1042 }
1043
1044 // Early exit if interval is at the right spot.
1045 if ((interval->previous_ == NULL ||
1046 interval->previous_->start_ <= interval->start_) &&
1047 (interval->next_ == NULL ||
1048 interval->start_ <= interval->next_->start_)) {
1049 return;
1050 }
1051
1052 ConnectIntervals(manager, interval->previous_, interval->next_);
1053 PositionOrphanInterval(manager, interval, interval->previous_);
1054 }
1055
1056 // Given a new cost interval defined by its start at index, its last value and
1057 // distance_cost, add its contributions to the previous intervals and costs.
1058 // If handling the interval or one of its subintervals becomes to heavy, its
1059 // contribution is added to the costs right away.
1060 static WEBP_INLINE void PushInterval(CostManager* const manager,
1061 double distance_cost, int index,
1062 int last) {
1063 size_t i;
1064 CostInterval* interval = manager->head_;
1065 CostInterval* interval_next;
1066 const CostCacheInterval* const cost_cache_intervals =
1067 manager->cache_intervals_;
1068
1069 for (i = 0; i < manager->cache_intervals_size_ &&
1070 cost_cache_intervals[i].start_ < last;
1071 ++i) {
1072 // Define the intersection of the ith interval with the new one.
1073 int start = index + cost_cache_intervals[i].start_;
1074 const int end = index + (cost_cache_intervals[i].end_ > last
1075 ? last
1076 : cost_cache_intervals[i].end_);
1077 const double lower_in = cost_cache_intervals[i].lower_;
1078 const double upper_in = cost_cache_intervals[i].upper_;
1079 const double lower_full_in = distance_cost + lower_in;
1080 const double upper_full_in = distance_cost + upper_in;
1081
1082 if (cost_cache_intervals[i].do_write_) {
1083 UpdateCostPerInterval(manager, start, end, index, distance_cost);
1084 continue;
1085 }
1086
1087 for (; interval != NULL && interval->start_ < end && start < end;
1088 interval = interval_next) {
1089 const double lower_full_interval =
1090 interval->distance_cost_ + interval->lower_;
1091 const double upper_full_interval =
1092 interval->distance_cost_ + interval->upper_;
1093
1094 interval_next = interval->next_;
1095
1096 // Make sure we have some overlap
1097 if (start >= interval->end_) continue;
1098
1099 if (lower_full_in >= upper_full_interval) {
1100 // When intervals are represented, the lower, the better.
1101 // [**********************************************************]
1102 // start end
1103 // [----------------------------------]
1104 // interval->start_ interval->end_
1105 // If we are worse than what we already have, add whatever we have so
1106 // far up to interval.
1107 const int start_new = interval->end_;
1108 InsertInterval(manager, interval, distance_cost, lower_in, upper_in,
1109 index, start, interval->start_);
1110 start = start_new;
1111 continue;
1112 }
1113
1114 // We know the two intervals intersect.
1115 if (upper_full_in >= lower_full_interval) {
1116 // There is no clear cut on which is best, so let's keep both.
1117 // [*********[*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*]***********]
1118 // start interval->start_ interval->end_ end
1119 // OR
1120 // [*********[*-*-*-*-*-*-*-*-*-*-*-]----------------------]
1121 // start interval->start_ end interval->end_
1122 const int end_new = (interval->end_ <= end) ? interval->end_ : end;
1123 InsertInterval(manager, interval, distance_cost, lower_in, upper_in,
1124 index, start, end_new);
1125 start = end_new;
1126 } else if (start <= interval->start_ && interval->end_ <= end) {
1127 // [----------------------------------]
1128 // interval->start_ interval->end_
1129 // [**************************************************************]
1130 // start end
1131 // We can safely remove the old interval as it is fully included.
1132 PopInterval(manager, interval);
1133 } else {
1134 if (interval->start_ <= start && end <= interval->end_) {
1135 // [--------------------------------------------------------------]
1136 // interval->start_ interval->end_
1137 // [*****************************]
1138 // start end
1139 // We have to split the old interval as it fully contains the new one.
1140 const int end_original = interval->end_;
1141 interval->end_ = start;
1142 InsertInterval(manager, interval, interval->distance_cost_,
1143 interval->lower_, interval->upper_, interval->index_,
1144 end, end_original);
1145 } else if (interval->start_ < start) {
1146 // [------------------------------------]
1147 // interval->start_ interval->end_
1148 // [*****************************]
1149 // start end
1150 interval->end_ = start;
1151 } else {
1152 // [------------------------------------]
1153 // interval->start_ interval->end_
1154 // [*****************************]
1155 // start end
1156 interval->start_ = end;
1157 }
1158
1159 // The interval has been modified, we need to reposition it or write it.
1160 RepositionInterval(manager, interval);
1161 }
1162 }
1163 // Insert the remaining interval from start to end.
1164 InsertInterval(manager, interval, distance_cost, lower_in, upper_in, index,
1165 start, end);
1166 }
1167 }
1168
586 static int BackwardReferencesHashChainDistanceOnly( 1169 static int BackwardReferencesHashChainDistanceOnly(
587 int xsize, int ysize, const uint32_t* const argb, 1170 int xsize, int ysize, const uint32_t* const argb, int quality,
588 int quality, int cache_bits, VP8LHashChain* const hash_chain, 1171 int cache_bits, const VP8LHashChain* const hash_chain,
589 VP8LBackwardRefs* const refs, uint16_t* const dist_array) { 1172 VP8LBackwardRefs* const refs, uint16_t* const dist_array) {
590 int i; 1173 int i;
591 int ok = 0; 1174 int ok = 0;
592 int cc_init = 0; 1175 int cc_init = 0;
593 const int pix_count = xsize * ysize; 1176 const int pix_count = xsize * ysize;
594 const int use_color_cache = (cache_bits > 0); 1177 const int use_color_cache = (cache_bits > 0);
595 float* const cost =
596 (float*)WebPSafeMalloc(pix_count, sizeof(*cost));
597 const size_t literal_array_size = sizeof(double) * 1178 const size_t literal_array_size = sizeof(double) *
598 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + 1179 (NUM_LITERAL_CODES + NUM_LENGTH_CODES +
599 ((cache_bits > 0) ? (1 << cache_bits) : 0)); 1180 ((cache_bits > 0) ? (1 << cache_bits) : 0));
600 const size_t cost_model_size = sizeof(CostModel) + literal_array_size; 1181 const size_t cost_model_size = sizeof(CostModel) + literal_array_size;
601 CostModel* const cost_model = 1182 CostModel* const cost_model =
602 (CostModel*)WebPSafeMalloc(1ULL, cost_model_size); 1183 (CostModel*)WebPSafeCalloc(1ULL, cost_model_size);
603 VP8LColorCache hashers; 1184 VP8LColorCache hashers;
604 const int skip_length = 32 + quality; 1185 const int skip_length = 32 + quality;
605 const int skip_min_distance_code = 2; 1186 const int skip_min_distance_code = 2;
606 int iter_max = GetMaxItersForQuality(quality, 0); 1187 CostManager* cost_manager =
607 const int window_size = GetWindowSizeForHashChain(quality, xsize); 1188 (CostManager*)WebPSafeMalloc(1ULL, sizeof(*cost_manager));
608 1189
609 if (cost == NULL || cost_model == NULL) goto Error; 1190 if (cost_model == NULL || cost_manager == NULL) goto Error;
610 1191
611 cost_model->literal_ = (double*)(cost_model + 1); 1192 cost_model->literal_ = (double*)(cost_model + 1);
612 if (use_color_cache) { 1193 if (use_color_cache) {
613 cc_init = VP8LColorCacheInit(&hashers, cache_bits); 1194 cc_init = VP8LColorCacheInit(&hashers, cache_bits);
614 if (!cc_init) goto Error; 1195 if (!cc_init) goto Error;
615 } 1196 }
616 1197
617 if (!CostModelBuild(cost_model, cache_bits, refs)) { 1198 if (!CostModelBuild(cost_model, cache_bits, refs)) {
618 goto Error; 1199 goto Error;
619 } 1200 }
620 1201
621 for (i = 0; i < pix_count; ++i) cost[i] = 1e38f; 1202 if (!CostManagerInit(cost_manager, dist_array, pix_count, cost_model)) {
1203 goto Error;
1204 }
622 1205
623 // We loop one pixel at a time, but store all currently best points to 1206 // We loop one pixel at a time, but store all currently best points to
624 // non-processed locations from this point. 1207 // non-processed locations from this point.
625 dist_array[0] = 0; 1208 dist_array[0] = 0;
626 HashChainReset(hash_chain);
627 // Add first pixel as literal. 1209 // Add first pixel as literal.
628 AddSingleLiteralWithCostModel(argb + 0, hash_chain, &hashers, cost_model, 0, 1210 AddSingleLiteralWithCostModel(argb + 0, &hashers, cost_model, 0,
629 0, use_color_cache, 0.0, cost, dist_array); 1211 use_color_cache, 0.0, cost_manager->costs_,
1212 dist_array);
1213
630 for (i = 1; i < pix_count - 1; ++i) { 1214 for (i = 1; i < pix_count - 1; ++i) {
631 int offset = 0; 1215 int offset = 0, len = 0;
632 int len = 0; 1216 double prev_cost = cost_manager->costs_[i - 1];
633 double prev_cost = cost[i - 1]; 1217 HashChainFindCopy(hash_chain, i, &offset, &len);
634 const int max_len = MaxFindCopyLength(pix_count - i);
635 HashChainFindCopy(hash_chain, i, argb, max_len, window_size,
636 iter_max, &offset, &len);
637 if (len >= MIN_LENGTH) { 1218 if (len >= MIN_LENGTH) {
638 const int code = DistanceToPlaneCode(xsize, offset); 1219 const int code = DistanceToPlaneCode(xsize, offset);
639 const double distance_cost = 1220 const double offset_cost = GetDistanceCost(cost_model, code);
640 prev_cost + GetDistanceCost(cost_model, code); 1221 const int first_i = i;
641 int k; 1222 int j_max = 0, interval_ends_index = 0;
642 for (k = 1; k < len; ++k) { 1223 const int is_offset_zero = (offset_cost == 0.);
643 const double cost_val = distance_cost + GetLengthCost(cost_model, k); 1224
644 if (cost[i + k] > cost_val) { 1225 if (!is_offset_zero) {
645 cost[i + k] = (float)cost_val; 1226 j_max = (int)ceil(
646 dist_array[i + k] = k + 1; 1227 (cost_manager->max_cost_cache_ - cost_manager->min_cost_cache_) /
1228 offset_cost);
1229 if (j_max < 1) {
1230 j_max = 1;
1231 } else if (j_max > cost_manager->interval_ends_size_ - 1) {
1232 // This could only happen in the case of MAX_LENGTH.
1233 j_max = cost_manager->interval_ends_size_ - 1;
647 } 1234 }
1235 } // else j_max is unused anyway.
1236
1237 // Instead of considering all contributions from a pixel i by calling:
1238 // PushInterval(cost_manager, prev_cost + offset_cost, i, len);
1239 // we optimize these contributions in case offset_cost stays the same for
1240 // consecutive pixels. This describes a set of pixels similar to a
1241 // previous set (e.g. constant color regions).
1242 for (; i < pix_count - 1; ++i) {
1243 int offset_next, len_next;
1244 prev_cost = cost_manager->costs_[i - 1];
1245
1246 if (is_offset_zero) {
1247 // No optimization can be made so we just push all of the
1248 // contributions from i.
1249 PushInterval(cost_manager, prev_cost, i, len);
1250 } else {
1251 // j_max is chosen as the smallest j such that:
1252 // max of cost_cache_ < j*offset cost + min of cost_cache_
1253 // Therefore, the pixel influenced by i-j_max, cannot be influenced
1254 // by i. Only the costs after the end of what i contributed need to be
1255 // updated. cost_manager->interval_ends_ is a circular buffer that
1256 // stores those ends.
1257 const double distance_cost = prev_cost + offset_cost;
1258 int j = cost_manager->interval_ends_[interval_ends_index];
1259 if (i - first_i <= j_max ||
1260 !IsCostCacheIntervalWritable(j, i + len)) {
1261 PushInterval(cost_manager, distance_cost, i, len);
1262 } else {
1263 for (; j < i + len; ++j) {
1264 UpdateCost(cost_manager, j, i, distance_cost);
1265 }
1266 }
1267 // Store the new end in the circular buffer.
1268 assert(interval_ends_index < cost_manager->interval_ends_size_);
1269 cost_manager->interval_ends_[interval_ends_index] = i + len;
1270 if (++interval_ends_index > j_max) interval_ends_index = 0;
1271 }
1272
1273 // Check whether i is the last pixel to consider, as it is handled
1274 // differently.
1275 if (i + 1 >= pix_count - 1) break;
1276 HashChainFindCopy(hash_chain, i + 1, &offset_next, &len_next);
1277 if (offset_next != offset) break;
1278 len = len_next;
1279 UpdateCostPerIndex(cost_manager, i);
1280 AddSingleLiteralWithCostModel(argb + i, &hashers, cost_model, i,
1281 use_color_cache, prev_cost,
1282 cost_manager->costs_, dist_array);
648 } 1283 }
1284 // Submit the last pixel.
1285 UpdateCostPerIndex(cost_manager, i + 1);
1286
649 // This if is for speedup only. It roughly doubles the speed, and 1287 // This if is for speedup only. It roughly doubles the speed, and
650 // makes compression worse by .1 %. 1288 // makes compression worse by .1 %.
651 if (len >= skip_length && code <= skip_min_distance_code) { 1289 if (len >= skip_length && code <= skip_min_distance_code) {
652 // Long copy for short distances, let's skip the middle 1290 // Long copy for short distances, let's skip the middle
653 // lookups for better copies. 1291 // lookups for better copies.
654 // 1) insert the hashes. 1292 // 1) insert the hashes.
655 if (use_color_cache) { 1293 if (use_color_cache) {
1294 int k;
656 for (k = 0; k < len; ++k) { 1295 for (k = 0; k < len; ++k) {
657 VP8LColorCacheInsert(&hashers, argb[i + k]); 1296 VP8LColorCacheInsert(&hashers, argb[i + k]);
658 } 1297 }
659 } 1298 }
660 // 2) Add to the hash_chain (but cannot add the last pixel) 1299 // 2) jump.
661 { 1300 {
662 const int last = (len + i < pix_count - 1) ? len + i 1301 const int i_next = i + len - 1; // for loop does ++i, thus -1 here.
663 : pix_count - 1; 1302 for (; i <= i_next; ++i) UpdateCostPerIndex(cost_manager, i + 1);
664 for (k = i; k < last; ++k) { 1303 i = i_next;
665 HashChainInsert(hash_chain, &argb[k], k);
666 }
667 } 1304 }
668 // 3) jump.
669 i += len - 1; // for loop does ++i, thus -1 here.
670 goto next_symbol; 1305 goto next_symbol;
671 } 1306 }
672 if (len != MIN_LENGTH) { 1307 if (len > MIN_LENGTH) {
673 int code_min_length; 1308 int code_min_length;
674 double cost_total; 1309 double cost_total;
675 HashChainFindOffset(hash_chain, i, argb, MIN_LENGTH, window_size, 1310 offset = HashChainFindOffset(hash_chain, i);
676 &offset);
677 code_min_length = DistanceToPlaneCode(xsize, offset); 1311 code_min_length = DistanceToPlaneCode(xsize, offset);
678 cost_total = prev_cost + 1312 cost_total = prev_cost +
679 GetDistanceCost(cost_model, code_min_length) + 1313 GetDistanceCost(cost_model, code_min_length) +
680 GetLengthCost(cost_model, 1); 1314 GetLengthCost(cost_model, 1);
681 if (cost[i + 1] > cost_total) { 1315 if (cost_manager->costs_[i + 1] > cost_total) {
682 cost[i + 1] = (float)cost_total; 1316 cost_manager->costs_[i + 1] = (float)cost_total;
683 dist_array[i + 1] = 2; 1317 dist_array[i + 1] = 2;
684 } 1318 }
685 } 1319 }
1320 } else { // len < MIN_LENGTH
1321 UpdateCostPerIndex(cost_manager, i + 1);
686 } 1322 }
687 AddSingleLiteralWithCostModel(argb + i, hash_chain, &hashers, cost_model, i, 1323
688 0, use_color_cache, prev_cost, cost, 1324 AddSingleLiteralWithCostModel(argb + i, &hashers, cost_model, i,
689 dist_array); 1325 use_color_cache, prev_cost,
1326 cost_manager->costs_, dist_array);
1327
690 next_symbol: ; 1328 next_symbol: ;
691 } 1329 }
692 // Handle the last pixel. 1330 // Handle the last pixel.
693 if (i == (pix_count - 1)) { 1331 if (i == (pix_count - 1)) {
694 AddSingleLiteralWithCostModel(argb + i, hash_chain, &hashers, cost_model, i, 1332 AddSingleLiteralWithCostModel(
695 1, use_color_cache, cost[pix_count - 2], cost, 1333 argb + i, &hashers, cost_model, i, use_color_cache,
696 dist_array); 1334 cost_manager->costs_[pix_count - 2], cost_manager->costs_, dist_array);
697 } 1335 }
1336
698 ok = !refs->error_; 1337 ok = !refs->error_;
699 Error: 1338 Error:
700 if (cc_init) VP8LColorCacheClear(&hashers); 1339 if (cc_init) VP8LColorCacheClear(&hashers);
1340 CostManagerClear(cost_manager);
701 WebPSafeFree(cost_model); 1341 WebPSafeFree(cost_model);
702 WebPSafeFree(cost); 1342 WebPSafeFree(cost_manager);
703 return ok; 1343 return ok;
704 } 1344 }
705 1345
706 // We pack the path at the end of *dist_array and return 1346 // We pack the path at the end of *dist_array and return
707 // a pointer to this part of the array. Example: 1347 // a pointer to this part of the array. Example:
708 // dist_array = [1x2xx3x2] => packed [1x2x1232], chosen_path = [1232] 1348 // dist_array = [1x2xx3x2] => packed [1x2x1232], chosen_path = [1232]
709 static void TraceBackwards(uint16_t* const dist_array, 1349 static void TraceBackwards(uint16_t* const dist_array,
710 int dist_array_size, 1350 int dist_array_size,
711 uint16_t** const chosen_path, 1351 uint16_t** const chosen_path,
712 int* const chosen_path_size) { 1352 int* const chosen_path_size) {
713 uint16_t* path = dist_array + dist_array_size; 1353 uint16_t* path = dist_array + dist_array_size;
714 uint16_t* cur = dist_array + dist_array_size - 1; 1354 uint16_t* cur = dist_array + dist_array_size - 1;
715 while (cur >= dist_array) { 1355 while (cur >= dist_array) {
716 const int k = *cur; 1356 const int k = *cur;
717 --path; 1357 --path;
718 *path = k; 1358 *path = k;
719 cur -= k; 1359 cur -= k;
720 } 1360 }
721 *chosen_path = path; 1361 *chosen_path = path;
722 *chosen_path_size = (int)(dist_array + dist_array_size - path); 1362 *chosen_path_size = (int)(dist_array + dist_array_size - path);
723 } 1363 }
724 1364
725 static int BackwardReferencesHashChainFollowChosenPath( 1365 static int BackwardReferencesHashChainFollowChosenPath(
726 int xsize, int ysize, const uint32_t* const argb, 1366 const uint32_t* const argb, int cache_bits,
727 int quality, int cache_bits,
728 const uint16_t* const chosen_path, int chosen_path_size, 1367 const uint16_t* const chosen_path, int chosen_path_size,
729 VP8LHashChain* const hash_chain, 1368 const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs) {
730 VP8LBackwardRefs* const refs) {
731 const int pix_count = xsize * ysize;
732 const int use_color_cache = (cache_bits > 0); 1369 const int use_color_cache = (cache_bits > 0);
733 int ix; 1370 int ix;
734 int i = 0; 1371 int i = 0;
735 int ok = 0; 1372 int ok = 0;
736 int cc_init = 0; 1373 int cc_init = 0;
737 const int window_size = GetWindowSizeForHashChain(quality, xsize);
738 VP8LColorCache hashers; 1374 VP8LColorCache hashers;
739 1375
740 if (use_color_cache) { 1376 if (use_color_cache) {
741 cc_init = VP8LColorCacheInit(&hashers, cache_bits); 1377 cc_init = VP8LColorCacheInit(&hashers, cache_bits);
742 if (!cc_init) goto Error; 1378 if (!cc_init) goto Error;
743 } 1379 }
744 1380
745 ClearBackwardRefs(refs); 1381 ClearBackwardRefs(refs);
746 HashChainReset(hash_chain);
747 for (ix = 0; ix < chosen_path_size; ++ix) { 1382 for (ix = 0; ix < chosen_path_size; ++ix) {
748 int offset = 0;
749 const int len = chosen_path[ix]; 1383 const int len = chosen_path[ix];
750 if (len != 1) { 1384 if (len != 1) {
751 int k; 1385 int k;
752 HashChainFindOffset(hash_chain, i, argb, len, window_size, &offset); 1386 const int offset = HashChainFindOffset(hash_chain, i);
753 BackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len)); 1387 BackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len));
754 if (use_color_cache) { 1388 if (use_color_cache) {
755 for (k = 0; k < len; ++k) { 1389 for (k = 0; k < len; ++k) {
756 VP8LColorCacheInsert(&hashers, argb[i + k]); 1390 VP8LColorCacheInsert(&hashers, argb[i + k]);
757 } 1391 }
758 } 1392 }
759 {
760 const int last = (len < pix_count - 1 - i) ? len : pix_count - 1 - i;
761 for (k = 0; k < last; ++k) {
762 HashChainInsert(hash_chain, &argb[i + k], i + k);
763 }
764 }
765 i += len; 1393 i += len;
766 } else { 1394 } else {
767 PixOrCopy v; 1395 PixOrCopy v;
768 if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) { 1396 if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) {
769 // push pixel as a color cache index 1397 // push pixel as a color cache index
770 const int idx = VP8LColorCacheGetIndex(&hashers, argb[i]); 1398 const int idx = VP8LColorCacheGetIndex(&hashers, argb[i]);
771 v = PixOrCopyCreateCacheIdx(idx); 1399 v = PixOrCopyCreateCacheIdx(idx);
772 } else { 1400 } else {
773 if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]); 1401 if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]);
774 v = PixOrCopyCreateLiteral(argb[i]); 1402 v = PixOrCopyCreateLiteral(argb[i]);
775 } 1403 }
776 BackwardRefsCursorAdd(refs, v); 1404 BackwardRefsCursorAdd(refs, v);
777 if (i + 1 < pix_count) {
778 HashChainInsert(hash_chain, &argb[i], i);
779 }
780 ++i; 1405 ++i;
781 } 1406 }
782 } 1407 }
783 ok = !refs->error_; 1408 ok = !refs->error_;
784 Error: 1409 Error:
785 if (cc_init) VP8LColorCacheClear(&hashers); 1410 if (cc_init) VP8LColorCacheClear(&hashers);
786 return ok; 1411 return ok;
787 } 1412 }
788 1413
789 // Returns 1 on success. 1414 // Returns 1 on success.
790 static int BackwardReferencesTraceBackwards(int xsize, int ysize, 1415 static int BackwardReferencesTraceBackwards(
791 const uint32_t* const argb, 1416 int xsize, int ysize, const uint32_t* const argb, int quality,
792 int quality, int cache_bits, 1417 int cache_bits, const VP8LHashChain* const hash_chain,
793 VP8LHashChain* const hash_chain, 1418 VP8LBackwardRefs* const refs) {
794 VP8LBackwardRefs* const refs) {
795 int ok = 0; 1419 int ok = 0;
796 const int dist_array_size = xsize * ysize; 1420 const int dist_array_size = xsize * ysize;
797 uint16_t* chosen_path = NULL; 1421 uint16_t* chosen_path = NULL;
798 int chosen_path_size = 0; 1422 int chosen_path_size = 0;
799 uint16_t* dist_array = 1423 uint16_t* dist_array =
800 (uint16_t*)WebPSafeMalloc(dist_array_size, sizeof(*dist_array)); 1424 (uint16_t*)WebPSafeMalloc(dist_array_size, sizeof(*dist_array));
801 1425
802 if (dist_array == NULL) goto Error; 1426 if (dist_array == NULL) goto Error;
803 1427
804 if (!BackwardReferencesHashChainDistanceOnly( 1428 if (!BackwardReferencesHashChainDistanceOnly(
805 xsize, ysize, argb, quality, cache_bits, hash_chain, 1429 xsize, ysize, argb, quality, cache_bits, hash_chain,
806 refs, dist_array)) { 1430 refs, dist_array)) {
807 goto Error; 1431 goto Error;
808 } 1432 }
809 TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size); 1433 TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size);
810 if (!BackwardReferencesHashChainFollowChosenPath( 1434 if (!BackwardReferencesHashChainFollowChosenPath(
811 xsize, ysize, argb, quality, cache_bits, chosen_path, chosen_path_size, 1435 argb, cache_bits, chosen_path, chosen_path_size, hash_chain, refs)) {
812 hash_chain, refs)) {
813 goto Error; 1436 goto Error;
814 } 1437 }
815 ok = 1; 1438 ok = 1;
816 Error: 1439 Error:
817 WebPSafeFree(dist_array); 1440 WebPSafeFree(dist_array);
818 return ok; 1441 return ok;
819 } 1442 }
820 1443
821 static void BackwardReferences2DLocality(int xsize, 1444 static void BackwardReferences2DLocality(int xsize,
822 const VP8LBackwardRefs* const refs) { 1445 const VP8LBackwardRefs* const refs) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 return entropy; 1513 return entropy;
891 } 1514 }
892 1515
893 // Evaluate optimal cache bits for the local color cache. 1516 // Evaluate optimal cache bits for the local color cache.
894 // The input *best_cache_bits sets the maximum cache bits to use (passing 0 1517 // The input *best_cache_bits sets the maximum cache bits to use (passing 0
895 // implies disabling the local color cache). The local color cache is also 1518 // implies disabling the local color cache). The local color cache is also
896 // disabled for the lower (<= 25) quality. 1519 // disabled for the lower (<= 25) quality.
897 // Returns 0 in case of memory error. 1520 // Returns 0 in case of memory error.
898 static int CalculateBestCacheSize(const uint32_t* const argb, 1521 static int CalculateBestCacheSize(const uint32_t* const argb,
899 int xsize, int ysize, int quality, 1522 int xsize, int ysize, int quality,
900 VP8LHashChain* const hash_chain, 1523 const VP8LHashChain* const hash_chain,
901 VP8LBackwardRefs* const refs, 1524 VP8LBackwardRefs* const refs,
902 int* const lz77_computed, 1525 int* const lz77_computed,
903 int* const best_cache_bits) { 1526 int* const best_cache_bits) {
904 int eval_low = 1; 1527 int eval_low = 1;
905 int eval_high = 1; 1528 int eval_high = 1;
906 double entropy_low = MAX_ENTROPY; 1529 double entropy_low = MAX_ENTROPY;
907 double entropy_high = MAX_ENTROPY; 1530 double entropy_high = MAX_ENTROPY;
908 const double cost_mul = 5e-4; 1531 const double cost_mul = 5e-4;
909 int cache_bits_low = 0; 1532 int cache_bits_low = 0;
910 int cache_bits_high = (quality <= 25) ? 0 : *best_cache_bits; 1533 int cache_bits_high = (quality <= 25) ? 0 : *best_cache_bits;
911 1534
912 assert(cache_bits_high <= MAX_COLOR_CACHE_BITS); 1535 assert(cache_bits_high <= MAX_COLOR_CACHE_BITS);
913 1536
914 *lz77_computed = 0; 1537 *lz77_computed = 0;
915 if (cache_bits_high == 0) { 1538 if (cache_bits_high == 0) {
916 *best_cache_bits = 0; 1539 *best_cache_bits = 0;
917 // Local color cache is disabled. 1540 // Local color cache is disabled.
918 return 1; 1541 return 1;
919 } 1542 }
920 if (!BackwardReferencesLz77(xsize, ysize, argb, cache_bits_low, quality, 0, 1543 if (!BackwardReferencesLz77(xsize, ysize, argb, cache_bits_low, hash_chain,
921 hash_chain, refs)) { 1544 refs)) {
922 return 0; 1545 return 0;
923 } 1546 }
924 // Do a binary search to find the optimal entropy for cache_bits. 1547 // Do a binary search to find the optimal entropy for cache_bits.
925 while (eval_low || eval_high) { 1548 while (eval_low || eval_high) {
926 if (eval_low) { 1549 if (eval_low) {
927 entropy_low = ComputeCacheEntropy(argb, refs, cache_bits_low); 1550 entropy_low = ComputeCacheEntropy(argb, refs, cache_bits_low);
928 entropy_low += entropy_low * cache_bits_low * cost_mul; 1551 entropy_low += entropy_low * cache_bits_low * cost_mul;
929 eval_low = 0; 1552 eval_low = 0;
930 } 1553 }
931 if (eval_high) { 1554 if (eval_high) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 VP8LColorCacheInsert(&hashers, argb[pixel_index++]); 1599 VP8LColorCacheInsert(&hashers, argb[pixel_index++]);
977 } 1600 }
978 } 1601 }
979 VP8LRefsCursorNext(&c); 1602 VP8LRefsCursorNext(&c);
980 } 1603 }
981 VP8LColorCacheClear(&hashers); 1604 VP8LColorCacheClear(&hashers);
982 return 1; 1605 return 1;
983 } 1606 }
984 1607
985 static VP8LBackwardRefs* GetBackwardReferencesLowEffort( 1608 static VP8LBackwardRefs* GetBackwardReferencesLowEffort(
986 int width, int height, const uint32_t* const argb, int quality, 1609 int width, int height, const uint32_t* const argb,
987 int* const cache_bits, VP8LHashChain* const hash_chain, 1610 int* const cache_bits, const VP8LHashChain* const hash_chain,
988 VP8LBackwardRefs refs_array[2]) { 1611 VP8LBackwardRefs refs_array[2]) {
989 VP8LBackwardRefs* refs_lz77 = &refs_array[0]; 1612 VP8LBackwardRefs* refs_lz77 = &refs_array[0];
990 *cache_bits = 0; 1613 *cache_bits = 0;
991 if (!BackwardReferencesLz77(width, height, argb, 0, quality, 1614 if (!BackwardReferencesLz77(width, height, argb, 0, hash_chain, refs_lz77)) {
992 1 /* Low effort. */, hash_chain, refs_lz77)) {
993 return NULL; 1615 return NULL;
994 } 1616 }
995 BackwardReferences2DLocality(width, refs_lz77); 1617 BackwardReferences2DLocality(width, refs_lz77);
996 return refs_lz77; 1618 return refs_lz77;
997 } 1619 }
998 1620
999 static VP8LBackwardRefs* GetBackwardReferences( 1621 static VP8LBackwardRefs* GetBackwardReferences(
1000 int width, int height, const uint32_t* const argb, int quality, 1622 int width, int height, const uint32_t* const argb, int quality,
1001 int* const cache_bits, VP8LHashChain* const hash_chain, 1623 int* const cache_bits, const VP8LHashChain* const hash_chain,
1002 VP8LBackwardRefs refs_array[2]) { 1624 VP8LBackwardRefs refs_array[2]) {
1003 int lz77_is_useful; 1625 int lz77_is_useful;
1004 int lz77_computed; 1626 int lz77_computed;
1005 double bit_cost_lz77, bit_cost_rle; 1627 double bit_cost_lz77, bit_cost_rle;
1006 VP8LBackwardRefs* best = NULL; 1628 VP8LBackwardRefs* best = NULL;
1007 VP8LBackwardRefs* refs_lz77 = &refs_array[0]; 1629 VP8LBackwardRefs* refs_lz77 = &refs_array[0];
1008 VP8LBackwardRefs* refs_rle = &refs_array[1]; 1630 VP8LBackwardRefs* refs_rle = &refs_array[1];
1009 VP8LHistogram* histo = NULL; 1631 VP8LHistogram* histo = NULL;
1010 1632
1011 if (!CalculateBestCacheSize(argb, width, height, quality, hash_chain, 1633 if (!CalculateBestCacheSize(argb, width, height, quality, hash_chain,
1012 refs_lz77, &lz77_computed, cache_bits)) { 1634 refs_lz77, &lz77_computed, cache_bits)) {
1013 goto Error; 1635 goto Error;
1014 } 1636 }
1015 1637
1016 if (lz77_computed) { 1638 if (lz77_computed) {
1017 // Transform refs_lz77 for the optimized cache_bits. 1639 // Transform refs_lz77 for the optimized cache_bits.
1018 if (*cache_bits > 0) { 1640 if (*cache_bits > 0) {
1019 if (!BackwardRefsWithLocalCache(argb, *cache_bits, refs_lz77)) { 1641 if (!BackwardRefsWithLocalCache(argb, *cache_bits, refs_lz77)) {
1020 goto Error; 1642 goto Error;
1021 } 1643 }
1022 } 1644 }
1023 } else { 1645 } else {
1024 if (!BackwardReferencesLz77(width, height, argb, *cache_bits, quality, 1646 if (!BackwardReferencesLz77(width, height, argb, *cache_bits, hash_chain,
1025 0 /* Low effort. */, hash_chain, refs_lz77)) { 1647 refs_lz77)) {
1026 goto Error; 1648 goto Error;
1027 } 1649 }
1028 } 1650 }
1029 1651
1030 if (!BackwardReferencesRle(width, height, argb, *cache_bits, refs_rle)) { 1652 if (!BackwardReferencesRle(width, height, argb, *cache_bits, refs_rle)) {
1031 goto Error; 1653 goto Error;
1032 } 1654 }
1033 1655
1034 histo = VP8LAllocateHistogram(*cache_bits); 1656 histo = VP8LAllocateHistogram(*cache_bits);
1035 if (histo == NULL) goto Error; 1657 if (histo == NULL) goto Error;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 1696
1075 BackwardReferences2DLocality(width, best); 1697 BackwardReferences2DLocality(width, best);
1076 1698
1077 Error: 1699 Error:
1078 VP8LFreeHistogram(histo); 1700 VP8LFreeHistogram(histo);
1079 return best; 1701 return best;
1080 } 1702 }
1081 1703
1082 VP8LBackwardRefs* VP8LGetBackwardReferences( 1704 VP8LBackwardRefs* VP8LGetBackwardReferences(
1083 int width, int height, const uint32_t* const argb, int quality, 1705 int width, int height, const uint32_t* const argb, int quality,
1084 int low_effort, int* const cache_bits, VP8LHashChain* const hash_chain, 1706 int low_effort, int* const cache_bits,
1085 VP8LBackwardRefs refs_array[2]) { 1707 const VP8LHashChain* const hash_chain, VP8LBackwardRefs refs_array[2]) {
1086 if (low_effort) { 1708 if (low_effort) {
1087 return GetBackwardReferencesLowEffort(width, height, argb, quality, 1709 return GetBackwardReferencesLowEffort(width, height, argb, cache_bits,
1088 cache_bits, hash_chain, refs_array); 1710 hash_chain, refs_array);
1089 } else { 1711 } else {
1090 return GetBackwardReferences(width, height, argb, quality, cache_bits, 1712 return GetBackwardReferences(width, height, argb, quality, cache_bits,
1091 hash_chain, refs_array); 1713 hash_chain, refs_array);
1092 } 1714 }
1093 } 1715 }
OLDNEW
« no previous file with comments | « third_party/libwebp/enc/backward_references.h ('k') | third_party/libwebp/enc/filter.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698