literal_cost.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Literal cost model to allow backward reference replacement to be efficient.
  6. */
  7. #include "./literal_cost.h"
  8. #include "../common/platform.h"
  9. #include <brotli/types.h>
  10. #include "./fast_log.h"
  11. #include "./utf8_util.h"
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. static size_t UTF8Position(size_t last, size_t c, size_t clamp) {
  16. if (c < 128) {
  17. return 0; /* Next one is the 'Byte 1' again. */
  18. } else if (c >= 192) { /* Next one is the 'Byte 2' of utf-8 encoding. */
  19. return BROTLI_MIN(size_t, 1, clamp);
  20. } else {
  21. /* Let's decide over the last byte if this ends the sequence. */
  22. if (last < 0xE0) {
  23. return 0; /* Completed two or three byte coding. */
  24. } else { /* Next one is the 'Byte 3' of utf-8 encoding. */
  25. return BROTLI_MIN(size_t, 2, clamp);
  26. }
  27. }
  28. }
  29. static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask,
  30. const uint8_t* data) {
  31. size_t counts[3] = { 0 };
  32. size_t max_utf8 = 1; /* should be 2, but 1 compresses better. */
  33. size_t last_c = 0;
  34. size_t i;
  35. for (i = 0; i < len; ++i) {
  36. size_t c = data[(pos + i) & mask];
  37. ++counts[UTF8Position(last_c, c, 2)];
  38. last_c = c;
  39. }
  40. if (counts[2] < 500) {
  41. max_utf8 = 1;
  42. }
  43. if (counts[1] + counts[2] < 25) {
  44. max_utf8 = 0;
  45. }
  46. return max_utf8;
  47. }
  48. static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask,
  49. const uint8_t* data, float* cost) {
  50. /* max_utf8 is 0 (normal ASCII single byte modeling),
  51. 1 (for 2-byte UTF-8 modeling), or 2 (for 3-byte UTF-8 modeling). */
  52. const size_t max_utf8 = DecideMultiByteStatsLevel(pos, len, mask, data);
  53. size_t histogram[3][256] = { { 0 } };
  54. size_t window_half = 495;
  55. size_t in_window = BROTLI_MIN(size_t, window_half, len);
  56. size_t in_window_utf8[3] = { 0 };
  57. size_t i;
  58. { /* Bootstrap histograms. */
  59. size_t last_c = 0;
  60. size_t utf8_pos = 0;
  61. for (i = 0; i < in_window; ++i) {
  62. size_t c = data[(pos + i) & mask];
  63. ++histogram[utf8_pos][c];
  64. ++in_window_utf8[utf8_pos];
  65. utf8_pos = UTF8Position(last_c, c, max_utf8);
  66. last_c = c;
  67. }
  68. }
  69. /* Compute bit costs with sliding window. */
  70. for (i = 0; i < len; ++i) {
  71. if (i >= window_half) {
  72. /* Remove a byte in the past. */
  73. size_t c =
  74. i < window_half + 1 ? 0 : data[(pos + i - window_half - 1) & mask];
  75. size_t last_c =
  76. i < window_half + 2 ? 0 : data[(pos + i - window_half - 2) & mask];
  77. size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
  78. --histogram[utf8_pos2][data[(pos + i - window_half) & mask]];
  79. --in_window_utf8[utf8_pos2];
  80. }
  81. if (i + window_half < len) {
  82. /* Add a byte in the future. */
  83. size_t c = data[(pos + i + window_half - 1) & mask];
  84. size_t last_c = data[(pos + i + window_half - 2) & mask];
  85. size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
  86. ++histogram[utf8_pos2][data[(pos + i + window_half) & mask]];
  87. ++in_window_utf8[utf8_pos2];
  88. }
  89. {
  90. size_t c = i < 1 ? 0 : data[(pos + i - 1) & mask];
  91. size_t last_c = i < 2 ? 0 : data[(pos + i - 2) & mask];
  92. size_t utf8_pos = UTF8Position(last_c, c, max_utf8);
  93. size_t masked_pos = (pos + i) & mask;
  94. size_t histo = histogram[utf8_pos][data[masked_pos]];
  95. double lit_cost;
  96. if (histo == 0) {
  97. histo = 1;
  98. }
  99. lit_cost = FastLog2(in_window_utf8[utf8_pos]) - FastLog2(histo);
  100. lit_cost += 0.02905;
  101. if (lit_cost < 1.0) {
  102. lit_cost *= 0.5;
  103. lit_cost += 0.5;
  104. }
  105. /* Make the first bytes more expensive -- seems to help, not sure why.
  106. Perhaps because the entropy source is changing its properties
  107. rapidly in the beginning of the file, perhaps because the beginning
  108. of the data is a statistical "anomaly". */
  109. if (i < 2000) {
  110. lit_cost += 0.7 - ((double)(2000 - i) / 2000.0 * 0.35);
  111. }
  112. cost[i] = (float)lit_cost;
  113. }
  114. }
  115. }
  116. void BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask,
  117. const uint8_t* data, float* cost) {
  118. if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) {
  119. EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, cost);
  120. return;
  121. } else {
  122. size_t histogram[256] = { 0 };
  123. size_t window_half = 2000;
  124. size_t in_window = BROTLI_MIN(size_t, window_half, len);
  125. /* Bootstrap histogram. */
  126. size_t i;
  127. for (i = 0; i < in_window; ++i) {
  128. ++histogram[data[(pos + i) & mask]];
  129. }
  130. /* Compute bit costs with sliding window. */
  131. for (i = 0; i < len; ++i) {
  132. size_t histo;
  133. if (i >= window_half) {
  134. /* Remove a byte in the past. */
  135. --histogram[data[(pos + i - window_half) & mask]];
  136. --in_window;
  137. }
  138. if (i + window_half < len) {
  139. /* Add a byte in the future. */
  140. ++histogram[data[(pos + i + window_half) & mask]];
  141. ++in_window;
  142. }
  143. histo = histogram[data[(pos + i) & mask]];
  144. if (histo == 0) {
  145. histo = 1;
  146. }
  147. {
  148. double lit_cost = FastLog2(in_window) - FastLog2(histo);
  149. lit_cost += 0.029;
  150. if (lit_cost < 1.0) {
  151. lit_cost *= 0.5;
  152. lit_cost += 0.5;
  153. }
  154. cost[i] = (float)lit_cost;
  155. }
  156. }
  157. }
  158. }
  159. #if defined(__cplusplus) || defined(c_plusplus)
  160. } /* extern "C" */
  161. #endif