backward_references_enc.c 37 KB

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