brotli 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +11 -3
  4. data/Gemfile +2 -0
  5. data/ext/brotli/brotli.c +279 -0
  6. data/ext/brotli/brotli.h +2 -0
  7. data/ext/brotli/buffer.c +95 -0
  8. data/ext/brotli/buffer.h +19 -0
  9. data/ext/brotli/extconf.rb +21 -81
  10. data/lib/brotli/version.rb +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +5 -5
  12. data/vendor/brotli/dec/bit_reader.h +15 -15
  13. data/vendor/brotli/dec/context.h +1 -1
  14. data/vendor/brotli/dec/decode.c +433 -348
  15. data/vendor/brotli/dec/decode.h +74 -48
  16. data/vendor/brotli/dec/huffman.c +5 -4
  17. data/vendor/brotli/dec/huffman.h +4 -4
  18. data/vendor/brotli/dec/port.h +2 -95
  19. data/vendor/brotli/dec/prefix.h +5 -3
  20. data/vendor/brotli/dec/state.c +15 -27
  21. data/vendor/brotli/dec/state.h +21 -17
  22. data/vendor/brotli/dec/transform.h +1 -1
  23. data/vendor/brotli/enc/backward_references.c +892 -0
  24. data/vendor/brotli/enc/backward_references.h +85 -102
  25. data/vendor/brotli/enc/backward_references_inc.h +147 -0
  26. data/vendor/brotli/enc/bit_cost.c +35 -0
  27. data/vendor/brotli/enc/bit_cost.h +23 -121
  28. data/vendor/brotli/enc/bit_cost_inc.h +127 -0
  29. data/vendor/brotli/enc/block_encoder_inc.h +33 -0
  30. data/vendor/brotli/enc/block_splitter.c +197 -0
  31. data/vendor/brotli/enc/block_splitter.h +40 -50
  32. data/vendor/brotli/enc/block_splitter_inc.h +432 -0
  33. data/vendor/brotli/enc/brotli_bit_stream.c +1334 -0
  34. data/vendor/brotli/enc/brotli_bit_stream.h +95 -167
  35. data/vendor/brotli/enc/cluster.c +56 -0
  36. data/vendor/brotli/enc/cluster.h +23 -305
  37. data/vendor/brotli/enc/cluster_inc.h +315 -0
  38. data/vendor/brotli/enc/command.h +83 -76
  39. data/vendor/brotli/enc/compress_fragment.c +747 -0
  40. data/vendor/brotli/enc/compress_fragment.h +48 -37
  41. data/vendor/brotli/enc/compress_fragment_two_pass.c +557 -0
  42. data/vendor/brotli/enc/compress_fragment_two_pass.h +37 -26
  43. data/vendor/brotli/enc/compressor.cc +139 -0
  44. data/vendor/brotli/enc/compressor.h +146 -0
  45. data/vendor/brotli/enc/context.h +102 -96
  46. data/vendor/brotli/enc/dictionary_hash.h +9 -5
  47. data/vendor/brotli/enc/encode.c +1562 -0
  48. data/vendor/brotli/enc/encode.h +211 -199
  49. data/vendor/brotli/enc/encode_parallel.cc +161 -151
  50. data/vendor/brotli/enc/encode_parallel.h +7 -8
  51. data/vendor/brotli/enc/entropy_encode.c +501 -0
  52. data/vendor/brotli/enc/entropy_encode.h +107 -89
  53. data/vendor/brotli/enc/entropy_encode_static.h +29 -62
  54. data/vendor/brotli/enc/fast_log.h +26 -20
  55. data/vendor/brotli/enc/find_match_length.h +23 -20
  56. data/vendor/brotli/enc/hash.h +614 -871
  57. data/vendor/brotli/enc/hash_forgetful_chain_inc.h +249 -0
  58. data/vendor/brotli/enc/hash_longest_match_inc.h +241 -0
  59. data/vendor/brotli/enc/hash_longest_match_quickly_inc.h +230 -0
  60. data/vendor/brotli/enc/histogram.c +95 -0
  61. data/vendor/brotli/enc/histogram.h +49 -83
  62. data/vendor/brotli/enc/histogram_inc.h +51 -0
  63. data/vendor/brotli/enc/literal_cost.c +178 -0
  64. data/vendor/brotli/enc/literal_cost.h +16 -10
  65. data/vendor/brotli/enc/memory.c +181 -0
  66. data/vendor/brotli/enc/memory.h +62 -0
  67. data/vendor/brotli/enc/metablock.c +515 -0
  68. data/vendor/brotli/enc/metablock.h +87 -57
  69. data/vendor/brotli/enc/metablock_inc.h +183 -0
  70. data/vendor/brotli/enc/port.h +73 -47
  71. data/vendor/brotli/enc/prefix.h +34 -61
  72. data/vendor/brotli/enc/quality.h +130 -0
  73. data/vendor/brotli/enc/ringbuffer.h +137 -122
  74. data/vendor/brotli/enc/{static_dict.cc → static_dict.c} +162 -139
  75. data/vendor/brotli/enc/static_dict.h +23 -18
  76. data/vendor/brotli/enc/static_dict_lut.h +11223 -12037
  77. data/vendor/brotli/enc/streams.cc +7 -7
  78. data/vendor/brotli/enc/streams.h +32 -32
  79. data/vendor/brotli/enc/{utf8_util.cc → utf8_util.c} +22 -20
  80. data/vendor/brotli/enc/utf8_util.h +16 -9
  81. data/vendor/brotli/enc/write_bits.h +49 -43
  82. metadata +34 -25
  83. data/ext/brotli/brotli.cc +0 -181
  84. data/vendor/brotli/dec/Makefile +0 -12
  85. data/vendor/brotli/dec/dictionary.c +0 -9466
  86. data/vendor/brotli/dec/dictionary.h +0 -38
  87. data/vendor/brotli/dec/types.h +0 -38
  88. data/vendor/brotli/enc/Makefile +0 -14
  89. data/vendor/brotli/enc/backward_references.cc +0 -858
  90. data/vendor/brotli/enc/block_splitter.cc +0 -505
  91. data/vendor/brotli/enc/brotli_bit_stream.cc +0 -1181
  92. data/vendor/brotli/enc/compress_fragment.cc +0 -701
  93. data/vendor/brotli/enc/compress_fragment_two_pass.cc +0 -524
  94. data/vendor/brotli/enc/dictionary.cc +0 -9466
  95. data/vendor/brotli/enc/dictionary.h +0 -41
  96. data/vendor/brotli/enc/encode.cc +0 -1180
  97. data/vendor/brotli/enc/entropy_encode.cc +0 -480
  98. data/vendor/brotli/enc/histogram.cc +0 -67
  99. data/vendor/brotli/enc/literal_cost.cc +0 -165
  100. data/vendor/brotli/enc/metablock.cc +0 -539
  101. data/vendor/brotli/enc/transform.h +0 -248
  102. data/vendor/brotli/enc/types.h +0 -29
@@ -1,505 +0,0 @@
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