| OLD | NEW |
| (Empty) | |
| 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 // Block split point selection utilities. |
| 8 |
| 9 #include "./block_splitter.h" |
| 10 |
| 11 #include <assert.h> |
| 12 #include <math.h> |
| 13 |
| 14 #include <algorithm> |
| 15 #include <cstring> |
| 16 #include <vector> |
| 17 |
| 18 #include "./cluster.h" |
| 19 #include "./command.h" |
| 20 #include "./fast_log.h" |
| 21 #include "./histogram.h" |
| 22 |
| 23 namespace brotli { |
| 24 |
| 25 static const size_t kMaxLiteralHistograms = 100; |
| 26 static const size_t kMaxCommandHistograms = 50; |
| 27 static const double kLiteralBlockSwitchCost = 28.1; |
| 28 static const double kCommandBlockSwitchCost = 13.5; |
| 29 static const double kDistanceBlockSwitchCost = 14.6; |
| 30 static const size_t kLiteralStrideLength = 70; |
| 31 static const size_t kCommandStrideLength = 40; |
| 32 static const size_t kSymbolsPerLiteralHistogram = 544; |
| 33 static const size_t kSymbolsPerCommandHistogram = 530; |
| 34 static const size_t kSymbolsPerDistanceHistogram = 544; |
| 35 static const size_t kMinLengthForBlockSplitting = 128; |
| 36 static const size_t kIterMulForRefining = 2; |
| 37 static const size_t kMinItersForRefining = 100; |
| 38 |
| 39 void CopyLiteralsToByteArray(const Command* cmds, |
| 40 const size_t num_commands, |
| 41 const uint8_t* data, |
| 42 const size_t offset, |
| 43 const size_t mask, |
| 44 std::vector<uint8_t>* literals) { |
| 45 // Count how many we have. |
| 46 size_t total_length = 0; |
| 47 for (size_t i = 0; i < num_commands; ++i) { |
| 48 total_length += cmds[i].insert_len_; |
| 49 } |
| 50 if (total_length == 0) { |
| 51 return; |
| 52 } |
| 53 |
| 54 // Allocate. |
| 55 literals->resize(total_length); |
| 56 |
| 57 // Loop again, and copy this time. |
| 58 size_t pos = 0; |
| 59 size_t from_pos = offset & mask; |
| 60 for (size_t i = 0; i < num_commands && pos < total_length; ++i) { |
| 61 size_t insert_len = cmds[i].insert_len_; |
| 62 if (from_pos + insert_len > mask) { |
| 63 size_t head_size = mask + 1 - from_pos; |
| 64 memcpy(&(*literals)[pos], data + from_pos, head_size); |
| 65 from_pos = 0; |
| 66 pos += head_size; |
| 67 insert_len -= head_size; |
| 68 } |
| 69 if (insert_len > 0) { |
| 70 memcpy(&(*literals)[pos], data + from_pos, insert_len); |
| 71 pos += insert_len; |
| 72 } |
| 73 from_pos = (from_pos + insert_len + cmds[i].copy_len()) & mask; |
| 74 } |
| 75 } |
| 76 |
| 77 inline static unsigned int MyRand(unsigned int* seed) { |
| 78 *seed *= 16807U; |
| 79 if (*seed == 0) { |
| 80 *seed = 1; |
| 81 } |
| 82 return *seed; |
| 83 } |
| 84 |
| 85 template<typename HistogramType, typename DataType> |
| 86 void InitialEntropyCodes(const DataType* data, size_t length, |
| 87 size_t stride, |
| 88 size_t num_histograms, |
| 89 HistogramType* histograms) { |
| 90 for (size_t i = 0; i < num_histograms; ++i) { |
| 91 histograms[i].Clear(); |
| 92 } |
| 93 unsigned int seed = 7; |
| 94 size_t block_length = length / num_histograms; |
| 95 for (size_t i = 0; i < num_histograms; ++i) { |
| 96 size_t pos = length * i / num_histograms; |
| 97 if (i != 0) { |
| 98 pos += MyRand(&seed) % block_length; |
| 99 } |
| 100 if (pos + stride >= length) { |
| 101 pos = length - stride - 1; |
| 102 } |
| 103 histograms[i].Add(data + pos, stride); |
| 104 } |
| 105 } |
| 106 |
| 107 template<typename HistogramType, typename DataType> |
| 108 void RandomSample(unsigned int* seed, |
| 109 const DataType* data, |
| 110 size_t length, |
| 111 size_t stride, |
| 112 HistogramType* sample) { |
| 113 size_t pos = 0; |
| 114 if (stride >= length) { |
| 115 pos = 0; |
| 116 stride = length; |
| 117 } else { |
| 118 pos = MyRand(seed) % (length - stride + 1); |
| 119 } |
| 120 sample->Add(data + pos, stride); |
| 121 } |
| 122 |
| 123 template<typename HistogramType, typename DataType> |
| 124 void RefineEntropyCodes(const DataType* data, size_t length, |
| 125 size_t stride, |
| 126 size_t num_histograms, |
| 127 HistogramType* histograms) { |
| 128 size_t iters = |
| 129 kIterMulForRefining * length / stride + kMinItersForRefining; |
| 130 unsigned int seed = 7; |
| 131 iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms; |
| 132 for (size_t iter = 0; iter < iters; ++iter) { |
| 133 HistogramType sample; |
| 134 RandomSample(&seed, data, length, stride, &sample); |
| 135 size_t ix = iter % num_histograms; |
| 136 histograms[ix].AddHistogram(sample); |
| 137 } |
| 138 } |
| 139 |
| 140 inline static double BitCost(size_t count) { |
| 141 return count == 0 ? -2.0 : FastLog2(count); |
| 142 } |
| 143 |
| 144 // Assigns a block id from the range [0, vec.size()) to each data element |
| 145 // in data[0..length) and fills in block_id[0..length) with the assigned values. |
| 146 // Returns the number of blocks, i.e. one plus the number of block switches. |
| 147 template<typename DataType, int kSize> |
| 148 size_t FindBlocks(const DataType* data, const size_t length, |
| 149 const double block_switch_bitcost, |
| 150 const size_t num_histograms, |
| 151 const Histogram<kSize>* histograms, |
| 152 double* insert_cost, |
| 153 double* cost, |
| 154 uint8_t* switch_signal, |
| 155 uint8_t *block_id) { |
| 156 if (num_histograms <= 1) { |
| 157 for (size_t i = 0; i < length; ++i) { |
| 158 block_id[i] = 0; |
| 159 } |
| 160 return 1; |
| 161 } |
| 162 const size_t bitmaplen = (num_histograms + 7) >> 3; |
| 163 assert(num_histograms <= 256); |
| 164 memset(insert_cost, 0, sizeof(insert_cost[0]) * kSize * num_histograms); |
| 165 for (size_t j = 0; j < num_histograms; ++j) { |
| 166 insert_cost[j] = FastLog2(static_cast<uint32_t>( |
| 167 histograms[j].total_count_)); |
| 168 } |
| 169 for (size_t i = kSize; i != 0;) { |
| 170 --i; |
| 171 for (size_t j = 0; j < num_histograms; ++j) { |
| 172 insert_cost[i * num_histograms + j] = |
| 173 insert_cost[j] - BitCost(histograms[j].data_[i]); |
| 174 } |
| 175 } |
| 176 memset(cost, 0, sizeof(cost[0]) * num_histograms); |
| 177 memset(switch_signal, 0, sizeof(switch_signal[0]) * length * bitmaplen); |
| 178 // After each iteration of this loop, cost[k] will contain the difference |
| 179 // between the minimum cost of arriving at the current byte position using |
| 180 // entropy code k, and the minimum cost of arriving at the current byte |
| 181 // position. This difference is capped at the block switch cost, and if it |
| 182 // reaches block switch cost, it means that when we trace back from the last |
| 183 // position, we need to switch here. |
| 184 for (size_t byte_ix = 0; byte_ix < length; ++byte_ix) { |
| 185 size_t ix = byte_ix * bitmaplen; |
| 186 size_t insert_cost_ix = data[byte_ix] * num_histograms; |
| 187 double min_cost = 1e99; |
| 188 for (size_t k = 0; k < num_histograms; ++k) { |
| 189 // We are coding the symbol in data[byte_ix] with entropy code k. |
| 190 cost[k] += insert_cost[insert_cost_ix + k]; |
| 191 if (cost[k] < min_cost) { |
| 192 min_cost = cost[k]; |
| 193 block_id[byte_ix] = static_cast<uint8_t>(k); |
| 194 } |
| 195 } |
| 196 double block_switch_cost = block_switch_bitcost; |
| 197 // More blocks for the beginning. |
| 198 if (byte_ix < 2000) { |
| 199 block_switch_cost *= 0.77 + 0.07 * static_cast<double>(byte_ix) / 2000; |
| 200 } |
| 201 for (size_t k = 0; k < num_histograms; ++k) { |
| 202 cost[k] -= min_cost; |
| 203 if (cost[k] >= block_switch_cost) { |
| 204 cost[k] = block_switch_cost; |
| 205 const uint8_t mask = static_cast<uint8_t>(1u << (k & 7)); |
| 206 assert((k >> 3) < bitmaplen); |
| 207 switch_signal[ix + (k >> 3)] |= mask; |
| 208 } |
| 209 } |
| 210 } |
| 211 // Now trace back from the last position and switch at the marked places. |
| 212 size_t byte_ix = length - 1; |
| 213 size_t ix = byte_ix * bitmaplen; |
| 214 uint8_t cur_id = block_id[byte_ix]; |
| 215 size_t num_blocks = 1; |
| 216 while (byte_ix > 0) { |
| 217 --byte_ix; |
| 218 ix -= bitmaplen; |
| 219 const uint8_t mask = static_cast<uint8_t>(1u << (cur_id & 7)); |
| 220 assert((static_cast<size_t>(cur_id) >> 3) < bitmaplen); |
| 221 if (switch_signal[ix + (cur_id >> 3)] & mask) { |
| 222 if (cur_id != block_id[byte_ix]) { |
| 223 cur_id = block_id[byte_ix]; |
| 224 ++num_blocks; |
| 225 } |
| 226 } |
| 227 block_id[byte_ix] = cur_id; |
| 228 } |
| 229 return num_blocks; |
| 230 } |
| 231 |
| 232 static size_t RemapBlockIds(uint8_t* block_ids, const size_t length, |
| 233 uint16_t* new_id, const size_t num_histograms) { |
| 234 static const uint16_t kInvalidId = 256; |
| 235 for (size_t i = 0; i < num_histograms; ++i) { |
| 236 new_id[i] = kInvalidId; |
| 237 } |
| 238 uint16_t next_id = 0; |
| 239 for (size_t i = 0; i < length; ++i) { |
| 240 assert(block_ids[i] < num_histograms); |
| 241 if (new_id[block_ids[i]] == kInvalidId) { |
| 242 new_id[block_ids[i]] = next_id++; |
| 243 } |
| 244 } |
| 245 for (size_t i = 0; i < length; ++i) { |
| 246 block_ids[i] = static_cast<uint8_t>(new_id[block_ids[i]]); |
| 247 assert(block_ids[i] < num_histograms); |
| 248 } |
| 249 assert(next_id <= num_histograms); |
| 250 return next_id; |
| 251 } |
| 252 |
| 253 template<typename HistogramType, typename DataType> |
| 254 void BuildBlockHistograms(const DataType* data, const size_t length, |
| 255 const uint8_t* block_ids, |
| 256 const size_t num_histograms, |
| 257 HistogramType* histograms) { |
| 258 for (size_t i = 0; i < num_histograms; ++i) { |
| 259 histograms[i].Clear(); |
| 260 } |
| 261 for (size_t i = 0; i < length; ++i) { |
| 262 histograms[block_ids[i]].Add(data[i]); |
| 263 } |
| 264 } |
| 265 |
| 266 template<typename HistogramType, typename DataType> |
| 267 void ClusterBlocks(const DataType* data, const size_t length, |
| 268 const size_t num_blocks, |
| 269 uint8_t* block_ids, |
| 270 BlockSplit* split) { |
| 271 static const size_t kMaxNumberOfBlockTypes = 256; |
| 272 static const size_t kHistogramsPerBatch = 64; |
| 273 static const size_t kClustersPerBatch = 16; |
| 274 std::vector<uint32_t> histogram_symbols(num_blocks); |
| 275 std::vector<uint32_t> block_lengths(num_blocks); |
| 276 |
| 277 size_t block_idx = 0; |
| 278 for (size_t i = 0; i < length; ++i) { |
| 279 assert(block_idx < num_blocks); |
| 280 ++block_lengths[block_idx]; |
| 281 if (i + 1 == length || block_ids[i] != block_ids[i + 1]) { |
| 282 ++block_idx; |
| 283 } |
| 284 } |
| 285 assert(block_idx == num_blocks); |
| 286 |
| 287 const size_t expected_num_clusters = |
| 288 kClustersPerBatch * |
| 289 (num_blocks + kHistogramsPerBatch - 1) / kHistogramsPerBatch; |
| 290 std::vector<HistogramType> all_histograms; |
| 291 std::vector<uint32_t> cluster_size; |
| 292 all_histograms.reserve(expected_num_clusters); |
| 293 cluster_size.reserve(expected_num_clusters); |
| 294 size_t num_clusters = 0; |
| 295 std::vector<HistogramType> histograms( |
| 296 std::min(num_blocks, kHistogramsPerBatch)); |
| 297 size_t max_num_pairs = kHistogramsPerBatch * kHistogramsPerBatch / 2; |
| 298 std::vector<HistogramPair> pairs(max_num_pairs + 1); |
| 299 size_t pos = 0; |
| 300 for (size_t i = 0; i < num_blocks; i += kHistogramsPerBatch) { |
| 301 const size_t num_to_combine = std::min(num_blocks - i, kHistogramsPerBatch); |
| 302 uint32_t sizes[kHistogramsPerBatch]; |
| 303 uint32_t clusters[kHistogramsPerBatch]; |
| 304 uint32_t symbols[kHistogramsPerBatch]; |
| 305 uint32_t remap[kHistogramsPerBatch]; |
| 306 for (size_t j = 0; j < num_to_combine; ++j) { |
| 307 histograms[j].Clear(); |
| 308 for (size_t k = 0; k < block_lengths[i + j]; ++k) { |
| 309 histograms[j].Add(data[pos++]); |
| 310 } |
| 311 histograms[j].bit_cost_ = PopulationCost(histograms[j]); |
| 312 symbols[j] = clusters[j] = static_cast<uint32_t>(j); |
| 313 sizes[j] = 1; |
| 314 } |
| 315 size_t num_new_clusters = HistogramCombine( |
| 316 &histograms[0], sizes, symbols, clusters, &pairs[0], num_to_combine, |
| 317 num_to_combine, kHistogramsPerBatch, max_num_pairs); |
| 318 for (size_t j = 0; j < num_new_clusters; ++j) { |
| 319 all_histograms.push_back(histograms[clusters[j]]); |
| 320 cluster_size.push_back(sizes[clusters[j]]); |
| 321 remap[clusters[j]] = static_cast<uint32_t>(j); |
| 322 } |
| 323 for (size_t j = 0; j < num_to_combine; ++j) { |
| 324 histogram_symbols[i + j] = |
| 325 static_cast<uint32_t>(num_clusters) + remap[symbols[j]]; |
| 326 } |
| 327 num_clusters += num_new_clusters; |
| 328 assert(num_clusters == cluster_size.size()); |
| 329 assert(num_clusters == all_histograms.size()); |
| 330 } |
| 331 |
| 332 max_num_pairs = |
| 333 std::min(64 * num_clusters, (num_clusters / 2) * num_clusters); |
| 334 pairs.resize(max_num_pairs + 1); |
| 335 |
| 336 std::vector<uint32_t> clusters(num_clusters); |
| 337 for (size_t i = 0; i < num_clusters; ++i) { |
| 338 clusters[i] = static_cast<uint32_t>(i); |
| 339 } |
| 340 size_t num_final_clusters = |
| 341 HistogramCombine(&all_histograms[0], &cluster_size[0], |
| 342 &histogram_symbols[0], |
| 343 &clusters[0], &pairs[0], num_clusters, |
| 344 num_blocks, kMaxNumberOfBlockTypes, max_num_pairs); |
| 345 |
| 346 static const uint32_t kInvalidIndex = std::numeric_limits<uint32_t>::max(); |
| 347 std::vector<uint32_t> new_index(num_clusters, kInvalidIndex); |
| 348 uint32_t next_index = 0; |
| 349 pos = 0; |
| 350 for (size_t i = 0; i < num_blocks; ++i) { |
| 351 HistogramType histo; |
| 352 for (size_t j = 0; j < block_lengths[i]; ++j) { |
| 353 histo.Add(data[pos++]); |
| 354 } |
| 355 uint32_t best_out = |
| 356 i == 0 ? histogram_symbols[0] : histogram_symbols[i - 1]; |
| 357 double best_bits = HistogramBitCostDistance( |
| 358 histo, all_histograms[best_out]); |
| 359 for (size_t j = 0; j < num_final_clusters; ++j) { |
| 360 const double cur_bits = HistogramBitCostDistance( |
| 361 histo, all_histograms[clusters[j]]); |
| 362 if (cur_bits < best_bits) { |
| 363 best_bits = cur_bits; |
| 364 best_out = clusters[j]; |
| 365 } |
| 366 } |
| 367 histogram_symbols[i] = best_out; |
| 368 if (new_index[best_out] == kInvalidIndex) { |
| 369 new_index[best_out] = next_index++; |
| 370 } |
| 371 } |
| 372 uint8_t max_type = 0; |
| 373 uint32_t cur_length = 0; |
| 374 block_idx = 0; |
| 375 split->types.resize(num_blocks); |
| 376 split->lengths.resize(num_blocks); |
| 377 for (size_t 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_t id = static_cast<uint8_t>(new_index[histogram_symbols[i]]); |
| 382 split->types[block_idx] = id; |
| 383 split->lengths[block_idx] = cur_length; |
| 384 max_type = std::max(max_type, id); |
| 385 cur_length = 0; |
| 386 ++block_idx; |
| 387 } |
| 388 } |
| 389 split->types.resize(block_idx); |
| 390 split->lengths.resize(block_idx); |
| 391 split->num_types = static_cast<size_t>(max_type) + 1; |
| 392 } |
| 393 |
| 394 template<int kSize, typename DataType> |
| 395 void SplitByteVector(const std::vector<DataType>& data, |
| 396 const size_t literals_per_histogram, |
| 397 const size_t max_histograms, |
| 398 const size_t sampling_stride_length, |
| 399 const double block_switch_cost, |
| 400 BlockSplit* split) { |
| 401 if (data.empty()) { |
| 402 split->num_types = 1; |
| 403 return; |
| 404 } else if (data.size() < kMinLengthForBlockSplitting) { |
| 405 split->num_types = 1; |
| 406 split->types.push_back(0); |
| 407 split->lengths.push_back(static_cast<uint32_t>(data.size())); |
| 408 return; |
| 409 } |
| 410 size_t num_histograms = data.size() / literals_per_histogram + 1; |
| 411 if (num_histograms > max_histograms) { |
| 412 num_histograms = max_histograms; |
| 413 } |
| 414 Histogram<kSize>* histograms = new Histogram<kSize>[num_histograms]; |
| 415 // Find good entropy codes. |
| 416 InitialEntropyCodes(&data[0], data.size(), |
| 417 sampling_stride_length, |
| 418 num_histograms, histograms); |
| 419 RefineEntropyCodes(&data[0], data.size(), |
| 420 sampling_stride_length, |
| 421 num_histograms, histograms); |
| 422 // Find a good path through literals with the good entropy codes. |
| 423 std::vector<uint8_t> block_ids(data.size()); |
| 424 size_t num_blocks; |
| 425 const size_t bitmaplen = (num_histograms + 7) >> 3; |
| 426 double* insert_cost = new double[kSize * num_histograms]; |
| 427 double *cost = new double[num_histograms]; |
| 428 uint8_t* switch_signal = new uint8_t[data.size() * bitmaplen]; |
| 429 uint16_t* new_id = new uint16_t[num_histograms]; |
| 430 for (size_t i = 0; i < 10; ++i) { |
| 431 num_blocks = FindBlocks(&data[0], data.size(), |
| 432 block_switch_cost, |
| 433 num_histograms, histograms, |
| 434 insert_cost, cost, switch_signal, |
| 435 &block_ids[0]); |
| 436 num_histograms = RemapBlockIds(&block_ids[0], data.size(), |
| 437 new_id, num_histograms); |
| 438 BuildBlockHistograms(&data[0], data.size(), &block_ids[0], |
| 439 num_histograms, histograms); |
| 440 } |
| 441 delete[] insert_cost; |
| 442 delete[] cost; |
| 443 delete[] switch_signal; |
| 444 delete[] new_id; |
| 445 delete[] histograms; |
| 446 ClusterBlocks<Histogram<kSize> >(&data[0], data.size(), num_blocks, |
| 447 &block_ids[0], split); |
| 448 } |
| 449 |
| 450 void SplitBlock(const Command* cmds, |
| 451 const size_t num_commands, |
| 452 const uint8_t* data, |
| 453 const size_t pos, |
| 454 const size_t mask, |
| 455 BlockSplit* literal_split, |
| 456 BlockSplit* insert_and_copy_split, |
| 457 BlockSplit* dist_split) { |
| 458 { |
| 459 // Create a continuous array of literals. |
| 460 std::vector<uint8_t> literals; |
| 461 CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, &literals); |
| 462 // Create the block split on the array of literals. |
| 463 // Literal histograms have alphabet size 256. |
| 464 SplitByteVector<256>( |
| 465 literals, |
| 466 kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, |
| 467 kLiteralStrideLength, kLiteralBlockSwitchCost, |
| 468 literal_split); |
| 469 } |
| 470 |
| 471 { |
| 472 // Compute prefix codes for commands. |
| 473 std::vector<uint16_t> insert_and_copy_codes(num_commands); |
| 474 for (size_t i = 0; i < num_commands; ++i) { |
| 475 insert_and_copy_codes[i] = cmds[i].cmd_prefix_; |
| 476 } |
| 477 // Create the block split on the array of command prefixes. |
| 478 SplitByteVector<kNumCommandPrefixes>( |
| 479 insert_and_copy_codes, |
| 480 kSymbolsPerCommandHistogram, kMaxCommandHistograms, |
| 481 kCommandStrideLength, kCommandBlockSwitchCost, |
| 482 insert_and_copy_split); |
| 483 } |
| 484 |
| 485 { |
| 486 // Create a continuous array of distance prefixes. |
| 487 std::vector<uint16_t> distance_prefixes(num_commands); |
| 488 size_t pos = 0; |
| 489 for (size_t i = 0; i < num_commands; ++i) { |
| 490 const Command& cmd = cmds[i]; |
| 491 if (cmd.copy_len() && cmd.cmd_prefix_ >= 128) { |
| 492 distance_prefixes[pos++] = cmd.dist_prefix_; |
| 493 } |
| 494 } |
| 495 distance_prefixes.resize(pos); |
| 496 // Create the block split on the array of distance prefixes. |
| 497 SplitByteVector<kNumDistancePrefixes>( |
| 498 distance_prefixes, |
| 499 kSymbolsPerDistanceHistogram, kMaxCommandHistograms, |
| 500 kCommandStrideLength, kDistanceBlockSwitchCost, |
| 501 dist_split); |
| 502 } |
| 503 } |
| 504 |
| 505 } // namespace brotli |
| OLD | NEW |