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