| 1 | /* NOLINT(build/header_guard) */cxx . . $ `<file>/c/enc/block_splitter_inc.h`/ |
| 2 | /* Copyright 2013 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | Distributed under MIT license. |
| 5 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 6 | */ |
| 7 | |
| 8 | /* template parameters: FN, DataType */ |
| 9 | |
| 10 | #define HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`! FNcxx . . $ `c/enc/block_splitter.c:88:9`!(Histogram) |
| 11 | |
| 12 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(InitialEntropyCodes)(const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, size_t length, |
| 13 | size_t stride, |
| 14 | size_t num_histograms, |
| 15 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms) { |
| 16 | uint32_tcxx . . $ uint32_t# seed = 7; |
| 17 | size_t block_length = length / num_histograms; |
| 18 | size_t i; |
| 19 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(ClearHistograms)(histograms, num_histograms); |
| 20 | for (i = 0; i < num_histograms; ++i) { |
| 21 | size_t pos = length * i / num_histograms; |
| 22 | if (i != 0) { |
| 23 | pos += MyRandcxx . . $ MyRand(c22c494133c14864).(&seed) % block_length; |
| 24 | } |
| 25 | if (pos + stride >= length) { |
| 26 | pos = length - stride - 1; |
| 27 | } |
| 28 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAddVector)(&histograms[i], data + pos, stride); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RandomSample)(uint32_tcxx . . $ uint32_t#* seed, |
| 33 | const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, |
| 34 | size_t length, |
| 35 | size_t stride, |
| 36 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* sample) { |
| 37 | size_t pos = 0; |
| 38 | if (stride >= length) { |
| 39 | stride = length; |
| 40 | } else { |
| 41 | pos = MyRandcxx . . $ MyRand(c22c494133c14864).(seed) % (length - stride + 1); |
| 42 | } |
| 43 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAddVector)(sample, data + pos, stride); |
| 44 | } |
| 45 | |
| 46 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RefineEntropyCodes)(const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, size_t length, |
| 47 | size_t stride, |
| 48 | size_t num_histograms, |
| 49 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms, |
| 50 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* tmp) { |
| 51 | size_t iters = |
| 52 | kIterMulForRefiningcxx . . $ kIterMulForRefining. * length / stride + kMinItersForRefiningcxx . . $ kMinItersForRefining.; |
| 53 | uint32_tcxx . . $ uint32_t# seed = 7; |
| 54 | size_t iter; |
| 55 | iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms; |
| 56 | for (iter = 0; iter < iters; ++iter) { |
| 57 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramClear)(tmp); |
| 58 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RandomSample)(&seed, data, length, stride, tmp); |
| 59 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAddHistogram)(&histograms[iter % num_histograms], tmp); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* Assigns a block id from the range [0, num_histograms) to each data element |
| 64 | in data[0..length) and fills in block_id[0..length) with the assigned values. |
| 65 | Returns the number of blocks, i.e. one plus the number of block switches. */ |
| 66 | static size_t FNcxx . . $ `c/enc/block_splitter.c:88:9`!(FindBlocks)(const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, const size_t length, |
| 67 | const double block_switch_bitcost, |
| 68 | const size_t num_histograms, |
| 69 | const HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms, |
| 70 | double* insert_cost, |
| 71 | double* cost, |
| 72 | uint8_tcxx . . $ uint8_t#* switch_signal, |
| 73 | uint8_tcxx . . $ uint8_t#* block_id) { |
| 74 | const size_t alphabet_size = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramDataSize)(); |
| 75 | const size_t bitmap_len = (num_histograms + 7) >> 3; |
| 76 | size_t num_blocks = 1; |
| 77 | size_t byte_ix; |
| 78 | size_t i; |
| 79 | size_t j; |
| 80 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(num_histograms <= 256); |
| 81 | |
| 82 | /* Trivial case: single histogram -> single block type. */ |
| 83 | if (num_histograms <= 1) { |
| 84 | for (i = 0; i < length; ++i) { |
| 85 | block_id[i] = 0; |
| 86 | } |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /* Fill bitcost for each symbol of all histograms. |
| 91 | * Non-existing symbol cost: 2 + log2(total_count). |
| 92 | * Regular symbol cost: -log2(symbol_count / total_count). */ |
| 93 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(insert_cost, 0, |
| 94 | sizeof(insert_cost[0]) * alphabet_size * num_histograms); |
| 95 | for (i = 0; i < num_histograms; ++i) { |
| 96 | insert_cost[i] = FastLog2cxx . . $ FastLog2(78051de35c49feff).((uint32_tcxx . . $ uint32_t#)histograms[i].total_count_); |
| 97 | } |
| 98 | for (i = alphabet_size; i != 0;) { |
| 99 | /* Reverse order to use the 0-th row as a temporary storage. */ |
| 100 | --i; |
| 101 | for (j = 0; j < num_histograms; ++j) { |
| 102 | insert_cost[i * num_histograms + j] = |
| 103 | insert_cost[j] - BitCostcxx . . $ BitCost(78051de35c49feff).(histograms[j].data_[i]); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* After each iteration of this loop, cost[k] will contain the difference |
| 108 | between the minimum cost of arriving at the current byte position using |
| 109 | entropy code k, and the minimum cost of arriving at the current byte |
| 110 | position. This difference is capped at the block switch cost, and if it |
| 111 | reaches block switch cost, it means that when we trace back from the last |
| 112 | position, we need to switch here. */ |
| 113 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(cost, 0, sizeof(cost[0]) * num_histograms); |
| 114 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(switch_signal, 0, sizeof(switch_signal[0]) * length * bitmap_len); |
| 115 | for (byte_ix = 0; byte_ix < length; ++byte_ix) { |
| 116 | size_t ix = byte_ix * bitmap_len; |
| 117 | size_t symbol = data[byte_ix]; |
| 118 | size_t insert_cost_ix = symbol * num_histograms; |
| 119 | double min_cost = 1e99; |
| 120 | double block_switch_cost = block_switch_bitcost; |
| 121 | static const size_t prologue_length = 2000; |
| 122 | static const double multiplier = 0.07 / 2000; |
| 123 | size_t k; |
| 124 | for (k = 0; k < num_histograms; ++k) { |
| 125 | /* We are coding the symbol with entropy code k. */ |
| 126 | cost[k] += insert_cost[insert_cost_ix + k]; |
| 127 | if (cost[k] < min_cost) { |
| 128 | min_cost = cost[k]; |
| 129 | block_id[byte_ix] = (uint8_tcxx . . $ uint8_t#)k; |
| 130 | } |
| 131 | } |
| 132 | /* More blocks for the beginning. */ |
| 133 | if (byte_ix < prologue_length) { |
| 134 | block_switch_cost *= 0.77 + multiplier * (double)byte_ix; |
| 135 | } |
| 136 | for (k = 0; k < num_histograms; ++k) { |
| 137 | cost[k] -= min_cost; |
| 138 | if (cost[k] >= block_switch_cost) { |
| 139 | const uint8_tcxx . . $ uint8_t# mask = (uint8_tcxx . . $ uint8_t#)(1u << (k & 7)); |
| 140 | cost[k] = block_switch_cost; |
| 141 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!((k >> 3) < bitmap_len); |
| 142 | switch_signal[ix + (k >> 3)] |= mask; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | byte_ix = length - 1; |
| 148 | { /* Trace back from the last position and switch at the marked places. */ |
| 149 | size_t ix = byte_ix * bitmap_len; |
| 150 | uint8_tcxx . . $ uint8_t# cur_id = block_id[byte_ix]; |
| 151 | while (byte_ix > 0) { |
| 152 | const uint8_tcxx . . $ uint8_t# mask = (uint8_tcxx . . $ uint8_t#)(1u << (cur_id & 7)); |
| 153 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(((size_t)cur_id >> 3) < bitmap_len); |
| 154 | --byte_ix; |
| 155 | ix -= bitmap_len; |
| 156 | if (switch_signal[ix + (cur_id >> 3)] & mask) { |
| 157 | if (cur_id != block_id[byte_ix]) { |
| 158 | cur_id = block_id[byte_ix]; |
| 159 | ++num_blocks; |
| 160 | } |
| 161 | } |
| 162 | block_id[byte_ix] = cur_id; |
| 163 | } |
| 164 | } |
| 165 | return num_blocks; |
| 166 | } |
| 167 | |
| 168 | static size_t FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RemapBlockIds)(uint8_tcxx . . $ uint8_t#* block_ids, const size_t length, |
| 169 | uint16_tcxx . . $ uint16_t#* new_id, const size_t num_histograms) { |
| 170 | static const uint16_tcxx . . $ uint16_t# kInvalidId = 256; |
| 171 | uint16_tcxx . . $ uint16_t# next_id = 0; |
| 172 | size_t i; |
| 173 | for (i = 0; i < num_histograms; ++i) { |
| 174 | new_id[i] = kInvalidId; |
| 175 | } |
| 176 | for (i = 0; i < length; ++i) { |
| 177 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(block_ids[i] < num_histograms); |
| 178 | if (new_id[block_ids[i]] == kInvalidId) { |
| 179 | new_id[block_ids[i]] = next_id++; |
| 180 | } |
| 181 | } |
| 182 | for (i = 0; i < length; ++i) { |
| 183 | block_ids[i] = (uint8_tcxx . . $ uint8_t#)new_id[block_ids[i]]; |
| 184 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(block_ids[i] < num_histograms); |
| 185 | } |
| 186 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(next_id <= num_histograms); |
| 187 | return next_id; |
| 188 | } |
| 189 | |
| 190 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BuildBlockHistograms)(const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, const size_t length, |
| 191 | const uint8_tcxx . . $ uint8_t#* block_ids, |
| 192 | const size_t num_histograms, |
| 193 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms) { |
| 194 | size_t i; |
| 195 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(ClearHistograms)(histograms, num_histograms); |
| 196 | for (i = 0; i < length; ++i) { |
| 197 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAdd)(&histograms[block_ids[i]], data[i]); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* Given the initial partitioning build partitioning with limited number |
| 202 | * of histograms (and block types). */ |
| 203 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(ClusterBlocks)(MemoryManagercxx . . $ MemoryManager#* m, |
| 204 | const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, const size_t length, |
| 205 | const size_t num_blocks, |
| 206 | uint8_tcxx . . $ uint8_t#* block_ids, |
| 207 | BlockSplitcxx . . $ BlockSplit#* split) { |
| 208 | uint32_tcxx . . $ uint32_t#* histogram_symbols = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint32_t, num_blocks); |
| 209 | uint32_tcxx . . $ uint32_t#* u32 = |
| 210 | BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint32_t, num_blocks + 4 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!); |
| 211 | const size_t expected_num_clusters = CLUSTERS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:86:9`! * |
| 212 | (num_blocks + HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`! - 1) / HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!; |
| 213 | size_t all_histograms_size = 0; |
| 214 | size_t all_histograms_capacity = expected_num_clusters; |
| 215 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* all_histograms = |
| 216 | BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!, all_histograms_capacity); |
| 217 | size_t cluster_size_size = 0; |
| 218 | size_t cluster_size_capacity = expected_num_clusters; |
| 219 | uint32_tcxx . . $ uint32_t#* cluster_size = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint32_t, cluster_size_capacity); |
| 220 | size_t num_clusters = 0; |
| 221 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!, |
| 222 | BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(size_t, num_blocks, HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!)); |
| 223 | size_t max_num_pairs = |
| 224 | HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`! * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`! / 2; |
| 225 | size_t pairs_capacity = max_num_pairs + 1; |
| 226 | HistogramPaircxx . . $ HistogramPair#* pairs = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramPair, pairs_capacity); |
| 227 | size_t pos = 0; |
| 228 | uint32_tcxx . . $ uint32_t#* clusters; |
| 229 | size_t num_final_clusters; |
| 230 | static const uint32_tcxx . . $ uint32_t# kInvalidIndex = BROTLI_UINT32_MAXcxx . . $ `c/include/brotli/types.h:59:9`!; |
| 231 | uint32_tcxx . . $ uint32_t#* new_index; |
| 232 | size_t i; |
| 233 | uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! const sizes = |
| 234 | u32 ? (u32 + 0 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 235 | uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! const new_clusters = |
| 236 | u32 ? (u32 + 1 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 237 | uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! const symbols = |
| 238 | u32 ? (u32 + 2 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 239 | uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! const remap = |
| 240 | u32 ? (u32 + 3 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 241 | uint32_tcxx . . $ uint32_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! const block_lengths = |
| 242 | u32 ? (u32 + 4 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 243 | /* TODO(eustas): move to arena? */ |
| 244 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* tmp = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!, 2); |
| 245 | |
| 246 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(histogram_symbols) || |
| 247 | BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(u32) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(all_histograms) || |
| 248 | BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(cluster_size) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(histograms) || |
| 249 | BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(pairs) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(tmp)) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(u32, 0, (num_blocks + 4 * HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) * sizeof(uint32_tcxx . . $ uint32_t#)); |
| 254 | |
| 255 | /* Calculate block lengths (convert repeating values -> series length). */ |
| 256 | { |
| 257 | size_t block_idx = 0; |
| 258 | for (i = 0; i < length; ++i) { |
| 259 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(block_idx < num_blocks); |
| 260 | ++block_lengths[block_idx]; |
| 261 | if (i + 1 == length || block_ids[i] != block_ids[i + 1]) { |
| 262 | ++block_idx; |
| 263 | } |
| 264 | } |
| 265 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(block_idx == num_blocks); |
| 266 | } |
| 267 | |
| 268 | /* Pre-cluster blocks (cluster batches). */ |
| 269 | for (i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!) { |
| 270 | const size_t num_to_combine = |
| 271 | BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(size_t, num_blocks - i, HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!); |
| 272 | size_t num_new_clusters; |
| 273 | size_t j; |
| 274 | for (j = 0; j < num_to_combine; ++j) { |
| 275 | size_t k; |
| 276 | size_t block_length = block_lengths[i + j]; |
| 277 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramClear)(&histograms[j]); |
| 278 | for (k = 0; k < block_length; ++k) { |
| 279 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAdd)(&histograms[j], data[pos++]); |
| 280 | } |
| 281 | histograms[j].bit_cost_ = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BrotliPopulationCost)(&histograms[j]); |
| 282 | new_clusters[j] = (uint32_tcxx . . $ uint32_t#)j; |
| 283 | symbols[j] = (uint32_tcxx . . $ uint32_t#)j; |
| 284 | sizes[j] = 1; |
| 285 | } |
| 286 | num_new_clusters = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BrotliHistogramCombine)( |
| 287 | histograms, tmp, sizes, symbols, new_clusters, pairs, num_to_combine, |
| 288 | num_to_combine, HISTOGRAMS_PER_BATCHcxx . . $ `c/enc/block_splitter.c:85:9`!, max_num_pairs); |
| 289 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!(m, HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!, all_histograms, |
| 290 | all_histograms_capacity, all_histograms_size + num_new_clusters); |
| 291 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!(m, uint32_t, cluster_size, |
| 292 | cluster_size_capacity, cluster_size_size + num_new_clusters); |
| 293 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m)) return; |
| 294 | for (j = 0; j < num_new_clusters; ++j) { |
| 295 | all_histograms[all_histograms_size++] = histograms[new_clusters[j]]; |
| 296 | cluster_size[cluster_size_size++] = sizes[new_clusters[j]]; |
| 297 | remap[new_clusters[j]] = (uint32_tcxx . . $ uint32_t#)j; |
| 298 | } |
| 299 | for (j = 0; j < num_to_combine; ++j) { |
| 300 | histogram_symbols[i + j] = (uint32_tcxx . . $ uint32_t#)num_clusters + remap[symbols[j]]; |
| 301 | } |
| 302 | num_clusters += num_new_clusters; |
| 303 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(num_clusters == cluster_size_size); |
| 304 | BROTLI_DCHECKcxx . . $ `c/common/platform.h:473:9`!(num_clusters == all_histograms_size); |
| 305 | } |
| 306 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, histograms); |
| 307 | |
| 308 | /* Final clustering. */ |
| 309 | max_num_pairs = |
| 310 | BROTLI_MINcxx . . $ `c/common/platform.h:522:9`!(size_t, 64 * num_clusters, (num_clusters / 2) * num_clusters); |
| 311 | if (pairs_capacity < max_num_pairs + 1) { |
| 312 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, pairs); |
| 313 | pairs = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramPair, max_num_pairs + 1); |
| 314 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(pairs)) return; |
| 315 | } |
| 316 | clusters = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint32_t, num_clusters); |
| 317 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(clusters)) return; |
| 318 | for (i = 0; i < num_clusters; ++i) { |
| 319 | clusters[i] = (uint32_tcxx . . $ uint32_t#)i; |
| 320 | } |
| 321 | num_final_clusters = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BrotliHistogramCombine)( |
| 322 | all_histograms, tmp, cluster_size, histogram_symbols, clusters, pairs, |
| 323 | num_clusters, num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPEScxx . . $ `c/common/constants.h:21:9`!, |
| 324 | max_num_pairs); |
| 325 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, pairs); |
| 326 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, cluster_size); |
| 327 | |
| 328 | /* Assign blocks to final histograms. */ |
| 329 | new_index = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint32_t, num_clusters); |
| 330 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(new_index)) return; |
| 331 | for (i = 0; i < num_clusters; ++i) new_index[i] = kInvalidIndex; |
| 332 | pos = 0; |
| 333 | { |
| 334 | uint32_tcxx . . $ uint32_t# next_index = 0; |
| 335 | for (i = 0; i < num_blocks; ++i) { |
| 336 | size_t j; |
| 337 | uint32_tcxx . . $ uint32_t# best_out; |
| 338 | double best_bits; |
| 339 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramClear)(tmp); |
| 340 | for (j = 0; j < block_lengths[i]; ++j) { |
| 341 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramAdd)(tmp, data[pos++]); |
| 342 | } |
| 343 | /* Among equally good histograms prefer last used. */ |
| 344 | /* TODO(eustas): should we give a block-switch discount here? */ |
| 345 | best_out = (i == 0) ? histogram_symbols[0] : histogram_symbols[i - 1]; |
| 346 | best_bits = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BrotliHistogramBitCostDistance)( |
| 347 | tmp, &all_histograms[best_out], tmp + 1); |
| 348 | for (j = 0; j < num_final_clusters; ++j) { |
| 349 | const double cur_bits = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BrotliHistogramBitCostDistance)( |
| 350 | tmp, &all_histograms[clusters[j]], tmp + 1); |
| 351 | if (cur_bits < best_bits) { |
| 352 | best_bits = cur_bits; |
| 353 | best_out = clusters[j]; |
| 354 | } |
| 355 | } |
| 356 | histogram_symbols[i] = best_out; |
| 357 | if (new_index[best_out] == kInvalidIndex) { |
| 358 | new_index[best_out] = next_index++; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, tmp); |
| 363 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, clusters); |
| 364 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, all_histograms); |
| 365 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!( |
| 366 | m, uint8_t, split->types, split->types_alloc_size, num_blocks); |
| 367 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!( |
| 368 | m, uint32_t, split->lengths, split->lengths_alloc_size, num_blocks); |
| 369 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m)) return; |
| 370 | |
| 371 | /* Rewrite final assignment to block-split. There might be less blocks |
| 372 | * than |num_blocks| due to clustering. */ |
| 373 | { |
| 374 | uint32_tcxx . . $ uint32_t# cur_length = 0; |
| 375 | size_t block_idx = 0; |
| 376 | uint8_tcxx . . $ uint8_t# max_type = 0; |
| 377 | for (i = 0; i < num_blocks; ++i) { |
| 378 | cur_length += block_lengths[i]; |
| 379 | if (i + 1 == num_blocks || |
| 380 | histogram_symbols[i] != histogram_symbols[i + 1]) { |
| 381 | const uint8_tcxx . . $ uint8_t# id = (uint8_tcxx . . $ uint8_t#)new_index[histogram_symbols[i]]; |
| 382 | split->typescxx . . $ BlockSplit#types.[block_idx] = id; |
| 383 | split->lengthscxx . . $ BlockSplit#lengths.[block_idx] = cur_length; |
| 384 | max_type = BROTLI_MAXcxx . . $ `c/common/platform.h:523:9`!(uint8_t, max_type, id); |
| 385 | cur_length = 0; |
| 386 | ++block_idx; |
| 387 | } |
| 388 | } |
| 389 | split->num_blockscxx . . $ BlockSplit#num_blocks. = block_idx; |
| 390 | split->num_typescxx . . $ BlockSplit#num_types. = (size_t)max_type + 1; |
| 391 | } |
| 392 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, new_index); |
| 393 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, u32); |
| 394 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, histogram_symbols); |
| 395 | } |
| 396 | |
| 397 | /* Create BlockSplit (partitioning) given the limits, estimates and "effort" |
| 398 | * parameters. |
| 399 | * |
| 400 | * NB: max_histograms is often less than number of histograms allowed by format; |
| 401 | * this is done intentionally, to save some "space" for context-aware |
| 402 | * clustering (here entropy is estimated for context-free symbols). */ |
| 403 | static void FNcxx . . $ `c/enc/block_splitter.c:88:9`!(SplitByteVector)(MemoryManagercxx . . $ MemoryManager#* m, |
| 404 | const DataTypecxx . . $ `c/enc/block_splitter.c:89:9`!* data, const size_t length, |
| 405 | const size_t symbols_per_histogram, |
| 406 | const size_t max_histograms, |
| 407 | const size_t sampling_stride_length, |
| 408 | const double block_switch_cost, |
| 409 | const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params, |
| 410 | BlockSplitcxx . . $ BlockSplit#* split) { |
| 411 | const size_t data_size = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(HistogramDataSize)(); |
| 412 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* histograms; |
| 413 | HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!* tmp; |
| 414 | /* Calculate number of histograms; initial estimate is one histogram per |
| 415 | * specified amount of symbols; however, this value is capped. */ |
| 416 | size_t num_histograms = length / symbols_per_histogram + 1; |
| 417 | if (num_histograms > max_histograms) { |
| 418 | num_histograms = max_histograms; |
| 419 | } |
| 420 | |
| 421 | /* Corner case: no input. */ |
| 422 | if (length == 0) { |
| 423 | split->num_typescxx . . $ BlockSplit#num_types. = 1; |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | if (length < kMinLengthForBlockSplittingcxx . . $ kMinLengthForBlockSplitting.) { |
| 428 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!(m, uint8_t, |
| 429 | split->types, split->types_alloc_size, split->num_blocks + 1); |
| 430 | BROTLI_ENSURE_CAPACITYcxx . . $ `c/enc/memory.h:88:9`!(m, uint32_t, |
| 431 | split->lengths, split->lengths_alloc_size, split->num_blocks + 1); |
| 432 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m)) return; |
| 433 | split->num_typescxx . . $ BlockSplit#num_types. = 1; |
| 434 | split->typescxx . . $ BlockSplit#types.[split->num_blockscxx . . $ BlockSplit#num_blocks.] = 0; |
| 435 | split->lengthscxx . . $ BlockSplit#lengths.[split->num_blockscxx . . $ BlockSplit#num_blocks.] = (uint32_tcxx . . $ uint32_t#)length; |
| 436 | split->num_blockscxx . . $ BlockSplit#num_blocks.++; |
| 437 | return; |
| 438 | } |
| 439 | histograms = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`!, num_histograms + 1); |
| 440 | tmp = histograms + num_histograms; |
| 441 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(histograms)) return; |
| 442 | /* Find good entropy codes. */ |
| 443 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(InitialEntropyCodes)(data, length, |
| 444 | sampling_stride_length, |
| 445 | num_histograms, histograms); |
| 446 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RefineEntropyCodes)(data, length, |
| 447 | sampling_stride_length, |
| 448 | num_histograms, histograms, tmp); |
| 449 | { |
| 450 | /* Find a good path through literals with the good entropy codes. */ |
| 451 | uint8_tcxx . . $ uint8_t#* block_ids = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint8_t, length); |
| 452 | size_t num_blocks = 0; |
| 453 | const size_t bitmaplen = (num_histograms + 7) >> 3; |
| 454 | double* insert_cost = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, double, data_size * num_histograms); |
| 455 | double* cost = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, double, num_histograms); |
| 456 | uint8_tcxx . . $ uint8_t#* switch_signal = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint8_t, length * bitmaplen); |
| 457 | uint16_tcxx . . $ uint16_t#* new_id = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint16_t, num_histograms); |
| 458 | const size_t iters = params->qualitycxx . . $ BrotliEncoderParams#quality. < HQ_ZOPFLIFICATION_QUALITYcxx . . $ `c/enc/quality.h:20:9`! ? 3 : 10; |
| 459 | size_t i; |
| 460 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(block_ids) || |
| 461 | BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(insert_cost) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(cost) || |
| 462 | BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(switch_signal) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(new_id)) { |
| 463 | return; |
| 464 | } |
| 465 | for (i = 0; i < iters; ++i) { |
| 466 | num_blocks = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(FindBlocks)(data, length, |
| 467 | block_switch_cost, |
| 468 | num_histograms, histograms, |
| 469 | insert_cost, cost, switch_signal, |
| 470 | block_ids); |
| 471 | num_histograms = FNcxx . . $ `c/enc/block_splitter.c:88:9`!(RemapBlockIds)(block_ids, length, |
| 472 | new_id, num_histograms); |
| 473 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(BuildBlockHistograms)(data, length, block_ids, |
| 474 | num_histograms, histograms); |
| 475 | } |
| 476 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, insert_cost); |
| 477 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, cost); |
| 478 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, switch_signal); |
| 479 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, new_id); |
| 480 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, histograms); |
| 481 | FNcxx . . $ `c/enc/block_splitter.c:88:9`!(ClusterBlocks)(m, data, length, num_blocks, block_ids, split); |
| 482 | if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m)) return; |
| 483 | BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, block_ids); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #undef HistogramTypecxx . . $ `c/enc/block_splitter_inc.h:10:9`! |