1*f4ee7fbaSAndroid Build Coastguard Worker /* NOLINT(build/header_guard) */
2*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2013 Google Inc. All Rights Reserved.
3*f4ee7fbaSAndroid Build Coastguard Worker
4*f4ee7fbaSAndroid Build Coastguard Worker Distributed under MIT license.
5*f4ee7fbaSAndroid Build Coastguard Worker See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6*f4ee7fbaSAndroid Build Coastguard Worker */
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Worker /* template parameters: FN, DataType */
9*f4ee7fbaSAndroid Build Coastguard Worker
10*f4ee7fbaSAndroid Build Coastguard Worker #define HistogramType FN(Histogram)
11*f4ee7fbaSAndroid Build Coastguard Worker
FN(InitialEntropyCodes)12*f4ee7fbaSAndroid Build Coastguard Worker static void FN(InitialEntropyCodes)(const DataType* data, size_t length,
13*f4ee7fbaSAndroid Build Coastguard Worker size_t stride,
14*f4ee7fbaSAndroid Build Coastguard Worker size_t num_histograms,
15*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* histograms) {
16*f4ee7fbaSAndroid Build Coastguard Worker uint32_t seed = 7;
17*f4ee7fbaSAndroid Build Coastguard Worker size_t block_length = length / num_histograms;
18*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
19*f4ee7fbaSAndroid Build Coastguard Worker FN(ClearHistograms)(histograms, num_histograms);
20*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_histograms; ++i) {
21*f4ee7fbaSAndroid Build Coastguard Worker size_t pos = length * i / num_histograms;
22*f4ee7fbaSAndroid Build Coastguard Worker if (i != 0) {
23*f4ee7fbaSAndroid Build Coastguard Worker pos += MyRand(&seed) % block_length;
24*f4ee7fbaSAndroid Build Coastguard Worker }
25*f4ee7fbaSAndroid Build Coastguard Worker if (pos + stride >= length) {
26*f4ee7fbaSAndroid Build Coastguard Worker pos = length - stride - 1;
27*f4ee7fbaSAndroid Build Coastguard Worker }
28*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAddVector)(&histograms[i], data + pos, stride);
29*f4ee7fbaSAndroid Build Coastguard Worker }
30*f4ee7fbaSAndroid Build Coastguard Worker }
31*f4ee7fbaSAndroid Build Coastguard Worker
FN(RandomSample)32*f4ee7fbaSAndroid Build Coastguard Worker static void FN(RandomSample)(uint32_t* seed,
33*f4ee7fbaSAndroid Build Coastguard Worker const DataType* data,
34*f4ee7fbaSAndroid Build Coastguard Worker size_t length,
35*f4ee7fbaSAndroid Build Coastguard Worker size_t stride,
36*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* sample) {
37*f4ee7fbaSAndroid Build Coastguard Worker size_t pos = 0;
38*f4ee7fbaSAndroid Build Coastguard Worker if (stride >= length) {
39*f4ee7fbaSAndroid Build Coastguard Worker stride = length;
40*f4ee7fbaSAndroid Build Coastguard Worker } else {
41*f4ee7fbaSAndroid Build Coastguard Worker pos = MyRand(seed) % (length - stride + 1);
42*f4ee7fbaSAndroid Build Coastguard Worker }
43*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAddVector)(sample, data + pos, stride);
44*f4ee7fbaSAndroid Build Coastguard Worker }
45*f4ee7fbaSAndroid Build Coastguard Worker
FN(RefineEntropyCodes)46*f4ee7fbaSAndroid Build Coastguard Worker static void FN(RefineEntropyCodes)(const DataType* data, size_t length,
47*f4ee7fbaSAndroid Build Coastguard Worker size_t stride,
48*f4ee7fbaSAndroid Build Coastguard Worker size_t num_histograms,
49*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* histograms) {
50*f4ee7fbaSAndroid Build Coastguard Worker size_t iters =
51*f4ee7fbaSAndroid Build Coastguard Worker kIterMulForRefining * length / stride + kMinItersForRefining;
52*f4ee7fbaSAndroid Build Coastguard Worker uint32_t seed = 7;
53*f4ee7fbaSAndroid Build Coastguard Worker size_t iter;
54*f4ee7fbaSAndroid Build Coastguard Worker iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms;
55*f4ee7fbaSAndroid Build Coastguard Worker for (iter = 0; iter < iters; ++iter) {
56*f4ee7fbaSAndroid Build Coastguard Worker HistogramType sample;
57*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramClear)(&sample);
58*f4ee7fbaSAndroid Build Coastguard Worker FN(RandomSample)(&seed, data, length, stride, &sample);
59*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAddHistogram)(&histograms[iter % num_histograms], &sample);
60*f4ee7fbaSAndroid Build Coastguard Worker }
61*f4ee7fbaSAndroid Build Coastguard Worker }
62*f4ee7fbaSAndroid Build Coastguard Worker
63*f4ee7fbaSAndroid Build Coastguard Worker /* Assigns a block id from the range [0, num_histograms) to each data element
64*f4ee7fbaSAndroid Build Coastguard Worker in data[0..length) and fills in block_id[0..length) with the assigned values.
65*f4ee7fbaSAndroid Build Coastguard Worker Returns the number of blocks, i.e. one plus the number of block switches. */
FN(FindBlocks)66*f4ee7fbaSAndroid Build Coastguard Worker static size_t FN(FindBlocks)(const DataType* data, const size_t length,
67*f4ee7fbaSAndroid Build Coastguard Worker const double block_switch_bitcost,
68*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_histograms,
69*f4ee7fbaSAndroid Build Coastguard Worker const HistogramType* histograms,
70*f4ee7fbaSAndroid Build Coastguard Worker double* insert_cost,
71*f4ee7fbaSAndroid Build Coastguard Worker double* cost,
72*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* switch_signal,
73*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* block_id) {
74*f4ee7fbaSAndroid Build Coastguard Worker const size_t data_size = FN(HistogramDataSize)();
75*f4ee7fbaSAndroid Build Coastguard Worker const size_t bitmaplen = (num_histograms + 7) >> 3;
76*f4ee7fbaSAndroid Build Coastguard Worker size_t num_blocks = 1;
77*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
78*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
79*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(num_histograms <= 256);
80*f4ee7fbaSAndroid Build Coastguard Worker if (num_histograms <= 1) {
81*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
82*f4ee7fbaSAndroid Build Coastguard Worker block_id[i] = 0;
83*f4ee7fbaSAndroid Build Coastguard Worker }
84*f4ee7fbaSAndroid Build Coastguard Worker return 1;
85*f4ee7fbaSAndroid Build Coastguard Worker }
86*f4ee7fbaSAndroid Build Coastguard Worker memset(insert_cost, 0, sizeof(insert_cost[0]) * data_size * num_histograms);
87*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_histograms; ++i) {
88*f4ee7fbaSAndroid Build Coastguard Worker insert_cost[i] = FastLog2((uint32_t)histograms[i].total_count_);
89*f4ee7fbaSAndroid Build Coastguard Worker }
90*f4ee7fbaSAndroid Build Coastguard Worker for (i = data_size; i != 0;) {
91*f4ee7fbaSAndroid Build Coastguard Worker --i;
92*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_histograms; ++j) {
93*f4ee7fbaSAndroid Build Coastguard Worker insert_cost[i * num_histograms + j] =
94*f4ee7fbaSAndroid Build Coastguard Worker insert_cost[j] - BitCost(histograms[j].data_[i]);
95*f4ee7fbaSAndroid Build Coastguard Worker }
96*f4ee7fbaSAndroid Build Coastguard Worker }
97*f4ee7fbaSAndroid Build Coastguard Worker memset(cost, 0, sizeof(cost[0]) * num_histograms);
98*f4ee7fbaSAndroid Build Coastguard Worker memset(switch_signal, 0, sizeof(switch_signal[0]) * length * bitmaplen);
99*f4ee7fbaSAndroid Build Coastguard Worker /* After each iteration of this loop, cost[k] will contain the difference
100*f4ee7fbaSAndroid Build Coastguard Worker between the minimum cost of arriving at the current byte position using
101*f4ee7fbaSAndroid Build Coastguard Worker entropy code k, and the minimum cost of arriving at the current byte
102*f4ee7fbaSAndroid Build Coastguard Worker position. This difference is capped at the block switch cost, and if it
103*f4ee7fbaSAndroid Build Coastguard Worker reaches block switch cost, it means that when we trace back from the last
104*f4ee7fbaSAndroid Build Coastguard Worker position, we need to switch here. */
105*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
106*f4ee7fbaSAndroid Build Coastguard Worker const size_t byte_ix = i;
107*f4ee7fbaSAndroid Build Coastguard Worker size_t ix = byte_ix * bitmaplen;
108*f4ee7fbaSAndroid Build Coastguard Worker size_t insert_cost_ix = data[byte_ix] * num_histograms;
109*f4ee7fbaSAndroid Build Coastguard Worker double min_cost = 1e99;
110*f4ee7fbaSAndroid Build Coastguard Worker double block_switch_cost = block_switch_bitcost;
111*f4ee7fbaSAndroid Build Coastguard Worker size_t k;
112*f4ee7fbaSAndroid Build Coastguard Worker for (k = 0; k < num_histograms; ++k) {
113*f4ee7fbaSAndroid Build Coastguard Worker /* We are coding the symbol in data[byte_ix] with entropy code k. */
114*f4ee7fbaSAndroid Build Coastguard Worker cost[k] += insert_cost[insert_cost_ix + k];
115*f4ee7fbaSAndroid Build Coastguard Worker if (cost[k] < min_cost) {
116*f4ee7fbaSAndroid Build Coastguard Worker min_cost = cost[k];
117*f4ee7fbaSAndroid Build Coastguard Worker block_id[byte_ix] = (uint8_t)k;
118*f4ee7fbaSAndroid Build Coastguard Worker }
119*f4ee7fbaSAndroid Build Coastguard Worker }
120*f4ee7fbaSAndroid Build Coastguard Worker /* More blocks for the beginning. */
121*f4ee7fbaSAndroid Build Coastguard Worker if (byte_ix < 2000) {
122*f4ee7fbaSAndroid Build Coastguard Worker block_switch_cost *= 0.77 + 0.07 * (double)byte_ix / 2000;
123*f4ee7fbaSAndroid Build Coastguard Worker }
124*f4ee7fbaSAndroid Build Coastguard Worker for (k = 0; k < num_histograms; ++k) {
125*f4ee7fbaSAndroid Build Coastguard Worker cost[k] -= min_cost;
126*f4ee7fbaSAndroid Build Coastguard Worker if (cost[k] >= block_switch_cost) {
127*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t mask = (uint8_t)(1u << (k & 7));
128*f4ee7fbaSAndroid Build Coastguard Worker cost[k] = block_switch_cost;
129*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK((k >> 3) < bitmaplen);
130*f4ee7fbaSAndroid Build Coastguard Worker switch_signal[ix + (k >> 3)] |= mask;
131*f4ee7fbaSAndroid Build Coastguard Worker }
132*f4ee7fbaSAndroid Build Coastguard Worker }
133*f4ee7fbaSAndroid Build Coastguard Worker }
134*f4ee7fbaSAndroid Build Coastguard Worker { /* Trace back from the last position and switch at the marked places. */
135*f4ee7fbaSAndroid Build Coastguard Worker size_t byte_ix = length - 1;
136*f4ee7fbaSAndroid Build Coastguard Worker size_t ix = byte_ix * bitmaplen;
137*f4ee7fbaSAndroid Build Coastguard Worker uint8_t cur_id = block_id[byte_ix];
138*f4ee7fbaSAndroid Build Coastguard Worker while (byte_ix > 0) {
139*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t mask = (uint8_t)(1u << (cur_id & 7));
140*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(((size_t)cur_id >> 3) < bitmaplen);
141*f4ee7fbaSAndroid Build Coastguard Worker --byte_ix;
142*f4ee7fbaSAndroid Build Coastguard Worker ix -= bitmaplen;
143*f4ee7fbaSAndroid Build Coastguard Worker if (switch_signal[ix + (cur_id >> 3)] & mask) {
144*f4ee7fbaSAndroid Build Coastguard Worker if (cur_id != block_id[byte_ix]) {
145*f4ee7fbaSAndroid Build Coastguard Worker cur_id = block_id[byte_ix];
146*f4ee7fbaSAndroid Build Coastguard Worker ++num_blocks;
147*f4ee7fbaSAndroid Build Coastguard Worker }
148*f4ee7fbaSAndroid Build Coastguard Worker }
149*f4ee7fbaSAndroid Build Coastguard Worker block_id[byte_ix] = cur_id;
150*f4ee7fbaSAndroid Build Coastguard Worker }
151*f4ee7fbaSAndroid Build Coastguard Worker }
152*f4ee7fbaSAndroid Build Coastguard Worker return num_blocks;
153*f4ee7fbaSAndroid Build Coastguard Worker }
154*f4ee7fbaSAndroid Build Coastguard Worker
FN(RemapBlockIds)155*f4ee7fbaSAndroid Build Coastguard Worker static size_t FN(RemapBlockIds)(uint8_t* block_ids, const size_t length,
156*f4ee7fbaSAndroid Build Coastguard Worker uint16_t* new_id, const size_t num_histograms) {
157*f4ee7fbaSAndroid Build Coastguard Worker static const uint16_t kInvalidId = 256;
158*f4ee7fbaSAndroid Build Coastguard Worker uint16_t next_id = 0;
159*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
160*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_histograms; ++i) {
161*f4ee7fbaSAndroid Build Coastguard Worker new_id[i] = kInvalidId;
162*f4ee7fbaSAndroid Build Coastguard Worker }
163*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
164*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(block_ids[i] < num_histograms);
165*f4ee7fbaSAndroid Build Coastguard Worker if (new_id[block_ids[i]] == kInvalidId) {
166*f4ee7fbaSAndroid Build Coastguard Worker new_id[block_ids[i]] = next_id++;
167*f4ee7fbaSAndroid Build Coastguard Worker }
168*f4ee7fbaSAndroid Build Coastguard Worker }
169*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
170*f4ee7fbaSAndroid Build Coastguard Worker block_ids[i] = (uint8_t)new_id[block_ids[i]];
171*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(block_ids[i] < num_histograms);
172*f4ee7fbaSAndroid Build Coastguard Worker }
173*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(next_id <= num_histograms);
174*f4ee7fbaSAndroid Build Coastguard Worker return next_id;
175*f4ee7fbaSAndroid Build Coastguard Worker }
176*f4ee7fbaSAndroid Build Coastguard Worker
FN(BuildBlockHistograms)177*f4ee7fbaSAndroid Build Coastguard Worker static void FN(BuildBlockHistograms)(const DataType* data, const size_t length,
178*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* block_ids,
179*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_histograms,
180*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* histograms) {
181*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
182*f4ee7fbaSAndroid Build Coastguard Worker FN(ClearHistograms)(histograms, num_histograms);
183*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
184*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAdd)(&histograms[block_ids[i]], data[i]);
185*f4ee7fbaSAndroid Build Coastguard Worker }
186*f4ee7fbaSAndroid Build Coastguard Worker }
187*f4ee7fbaSAndroid Build Coastguard Worker
FN(ClusterBlocks)188*f4ee7fbaSAndroid Build Coastguard Worker static void FN(ClusterBlocks)(MemoryManager* m,
189*f4ee7fbaSAndroid Build Coastguard Worker const DataType* data, const size_t length,
190*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_blocks,
191*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* block_ids,
192*f4ee7fbaSAndroid Build Coastguard Worker BlockSplit* split) {
193*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* histogram_symbols = BROTLI_ALLOC(m, uint32_t, num_blocks);
194*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* block_lengths = BROTLI_ALLOC(m, uint32_t, num_blocks);
195*f4ee7fbaSAndroid Build Coastguard Worker const size_t expected_num_clusters = CLUSTERS_PER_BATCH *
196*f4ee7fbaSAndroid Build Coastguard Worker (num_blocks + HISTOGRAMS_PER_BATCH - 1) / HISTOGRAMS_PER_BATCH;
197*f4ee7fbaSAndroid Build Coastguard Worker size_t all_histograms_size = 0;
198*f4ee7fbaSAndroid Build Coastguard Worker size_t all_histograms_capacity = expected_num_clusters;
199*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* all_histograms =
200*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ALLOC(m, HistogramType, all_histograms_capacity);
201*f4ee7fbaSAndroid Build Coastguard Worker size_t cluster_size_size = 0;
202*f4ee7fbaSAndroid Build Coastguard Worker size_t cluster_size_capacity = expected_num_clusters;
203*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* cluster_size = BROTLI_ALLOC(m, uint32_t, cluster_size_capacity);
204*f4ee7fbaSAndroid Build Coastguard Worker size_t num_clusters = 0;
205*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* histograms = BROTLI_ALLOC(m, HistogramType,
206*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_MIN(size_t, num_blocks, HISTOGRAMS_PER_BATCH));
207*f4ee7fbaSAndroid Build Coastguard Worker size_t max_num_pairs =
208*f4ee7fbaSAndroid Build Coastguard Worker HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH / 2;
209*f4ee7fbaSAndroid Build Coastguard Worker size_t pairs_capacity = max_num_pairs + 1;
210*f4ee7fbaSAndroid Build Coastguard Worker HistogramPair* pairs = BROTLI_ALLOC(m, HistogramPair, pairs_capacity);
211*f4ee7fbaSAndroid Build Coastguard Worker size_t pos = 0;
212*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* clusters;
213*f4ee7fbaSAndroid Build Coastguard Worker size_t num_final_clusters;
214*f4ee7fbaSAndroid Build Coastguard Worker static const uint32_t kInvalidIndex = BROTLI_UINT32_MAX;
215*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* new_index;
216*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
217*f4ee7fbaSAndroid Build Coastguard Worker uint32_t sizes[HISTOGRAMS_PER_BATCH] = { 0 };
218*f4ee7fbaSAndroid Build Coastguard Worker uint32_t new_clusters[HISTOGRAMS_PER_BATCH] = { 0 };
219*f4ee7fbaSAndroid Build Coastguard Worker uint32_t symbols[HISTOGRAMS_PER_BATCH] = { 0 };
220*f4ee7fbaSAndroid Build Coastguard Worker uint32_t remap[HISTOGRAMS_PER_BATCH] = { 0 };
221*f4ee7fbaSAndroid Build Coastguard Worker
222*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(histogram_symbols) ||
223*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(block_lengths) || BROTLI_IS_NULL(all_histograms) ||
224*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(cluster_size) || BROTLI_IS_NULL(histograms) ||
225*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(pairs)) {
226*f4ee7fbaSAndroid Build Coastguard Worker return;
227*f4ee7fbaSAndroid Build Coastguard Worker }
228*f4ee7fbaSAndroid Build Coastguard Worker
229*f4ee7fbaSAndroid Build Coastguard Worker memset(block_lengths, 0, num_blocks * sizeof(uint32_t));
230*f4ee7fbaSAndroid Build Coastguard Worker
231*f4ee7fbaSAndroid Build Coastguard Worker {
232*f4ee7fbaSAndroid Build Coastguard Worker size_t block_idx = 0;
233*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) {
234*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(block_idx < num_blocks);
235*f4ee7fbaSAndroid Build Coastguard Worker ++block_lengths[block_idx];
236*f4ee7fbaSAndroid Build Coastguard Worker if (i + 1 == length || block_ids[i] != block_ids[i + 1]) {
237*f4ee7fbaSAndroid Build Coastguard Worker ++block_idx;
238*f4ee7fbaSAndroid Build Coastguard Worker }
239*f4ee7fbaSAndroid Build Coastguard Worker }
240*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(block_idx == num_blocks);
241*f4ee7fbaSAndroid Build Coastguard Worker }
242*f4ee7fbaSAndroid Build Coastguard Worker
243*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH) {
244*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_to_combine =
245*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_MIN(size_t, num_blocks - i, HISTOGRAMS_PER_BATCH);
246*f4ee7fbaSAndroid Build Coastguard Worker size_t num_new_clusters;
247*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
248*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_to_combine; ++j) {
249*f4ee7fbaSAndroid Build Coastguard Worker size_t k;
250*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramClear)(&histograms[j]);
251*f4ee7fbaSAndroid Build Coastguard Worker for (k = 0; k < block_lengths[i + j]; ++k) {
252*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAdd)(&histograms[j], data[pos++]);
253*f4ee7fbaSAndroid Build Coastguard Worker }
254*f4ee7fbaSAndroid Build Coastguard Worker histograms[j].bit_cost_ = FN(BrotliPopulationCost)(&histograms[j]);
255*f4ee7fbaSAndroid Build Coastguard Worker new_clusters[j] = (uint32_t)j;
256*f4ee7fbaSAndroid Build Coastguard Worker symbols[j] = (uint32_t)j;
257*f4ee7fbaSAndroid Build Coastguard Worker sizes[j] = 1;
258*f4ee7fbaSAndroid Build Coastguard Worker }
259*f4ee7fbaSAndroid Build Coastguard Worker num_new_clusters = FN(BrotliHistogramCombine)(
260*f4ee7fbaSAndroid Build Coastguard Worker histograms, sizes, symbols, new_clusters, pairs, num_to_combine,
261*f4ee7fbaSAndroid Build Coastguard Worker num_to_combine, HISTOGRAMS_PER_BATCH, max_num_pairs);
262*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(m, HistogramType, all_histograms,
263*f4ee7fbaSAndroid Build Coastguard Worker all_histograms_capacity, all_histograms_size + num_new_clusters);
264*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(m, uint32_t, cluster_size,
265*f4ee7fbaSAndroid Build Coastguard Worker cluster_size_capacity, cluster_size_size + num_new_clusters);
266*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
267*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_new_clusters; ++j) {
268*f4ee7fbaSAndroid Build Coastguard Worker all_histograms[all_histograms_size++] = histograms[new_clusters[j]];
269*f4ee7fbaSAndroid Build Coastguard Worker cluster_size[cluster_size_size++] = sizes[new_clusters[j]];
270*f4ee7fbaSAndroid Build Coastguard Worker remap[new_clusters[j]] = (uint32_t)j;
271*f4ee7fbaSAndroid Build Coastguard Worker }
272*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_to_combine; ++j) {
273*f4ee7fbaSAndroid Build Coastguard Worker histogram_symbols[i + j] = (uint32_t)num_clusters + remap[symbols[j]];
274*f4ee7fbaSAndroid Build Coastguard Worker }
275*f4ee7fbaSAndroid Build Coastguard Worker num_clusters += num_new_clusters;
276*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(num_clusters == cluster_size_size);
277*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(num_clusters == all_histograms_size);
278*f4ee7fbaSAndroid Build Coastguard Worker }
279*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, histograms);
280*f4ee7fbaSAndroid Build Coastguard Worker
281*f4ee7fbaSAndroid Build Coastguard Worker max_num_pairs =
282*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_MIN(size_t, 64 * num_clusters, (num_clusters / 2) * num_clusters);
283*f4ee7fbaSAndroid Build Coastguard Worker if (pairs_capacity < max_num_pairs + 1) {
284*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, pairs);
285*f4ee7fbaSAndroid Build Coastguard Worker pairs = BROTLI_ALLOC(m, HistogramPair, max_num_pairs + 1);
286*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(pairs)) return;
287*f4ee7fbaSAndroid Build Coastguard Worker }
288*f4ee7fbaSAndroid Build Coastguard Worker
289*f4ee7fbaSAndroid Build Coastguard Worker clusters = BROTLI_ALLOC(m, uint32_t, num_clusters);
290*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(clusters)) return;
291*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_clusters; ++i) {
292*f4ee7fbaSAndroid Build Coastguard Worker clusters[i] = (uint32_t)i;
293*f4ee7fbaSAndroid Build Coastguard Worker }
294*f4ee7fbaSAndroid Build Coastguard Worker num_final_clusters = FN(BrotliHistogramCombine)(
295*f4ee7fbaSAndroid Build Coastguard Worker all_histograms, cluster_size, histogram_symbols, clusters, pairs,
296*f4ee7fbaSAndroid Build Coastguard Worker num_clusters, num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES,
297*f4ee7fbaSAndroid Build Coastguard Worker max_num_pairs);
298*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, pairs);
299*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, cluster_size);
300*f4ee7fbaSAndroid Build Coastguard Worker
301*f4ee7fbaSAndroid Build Coastguard Worker new_index = BROTLI_ALLOC(m, uint32_t, num_clusters);
302*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(new_index)) return;
303*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_clusters; ++i) new_index[i] = kInvalidIndex;
304*f4ee7fbaSAndroid Build Coastguard Worker pos = 0;
305*f4ee7fbaSAndroid Build Coastguard Worker {
306*f4ee7fbaSAndroid Build Coastguard Worker uint32_t next_index = 0;
307*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_blocks; ++i) {
308*f4ee7fbaSAndroid Build Coastguard Worker HistogramType histo;
309*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
310*f4ee7fbaSAndroid Build Coastguard Worker uint32_t best_out;
311*f4ee7fbaSAndroid Build Coastguard Worker double best_bits;
312*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramClear)(&histo);
313*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < block_lengths[i]; ++j) {
314*f4ee7fbaSAndroid Build Coastguard Worker FN(HistogramAdd)(&histo, data[pos++]);
315*f4ee7fbaSAndroid Build Coastguard Worker }
316*f4ee7fbaSAndroid Build Coastguard Worker best_out = (i == 0) ? histogram_symbols[0] : histogram_symbols[i - 1];
317*f4ee7fbaSAndroid Build Coastguard Worker best_bits =
318*f4ee7fbaSAndroid Build Coastguard Worker FN(BrotliHistogramBitCostDistance)(&histo, &all_histograms[best_out]);
319*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_final_clusters; ++j) {
320*f4ee7fbaSAndroid Build Coastguard Worker const double cur_bits = FN(BrotliHistogramBitCostDistance)(
321*f4ee7fbaSAndroid Build Coastguard Worker &histo, &all_histograms[clusters[j]]);
322*f4ee7fbaSAndroid Build Coastguard Worker if (cur_bits < best_bits) {
323*f4ee7fbaSAndroid Build Coastguard Worker best_bits = cur_bits;
324*f4ee7fbaSAndroid Build Coastguard Worker best_out = clusters[j];
325*f4ee7fbaSAndroid Build Coastguard Worker }
326*f4ee7fbaSAndroid Build Coastguard Worker }
327*f4ee7fbaSAndroid Build Coastguard Worker histogram_symbols[i] = best_out;
328*f4ee7fbaSAndroid Build Coastguard Worker if (new_index[best_out] == kInvalidIndex) {
329*f4ee7fbaSAndroid Build Coastguard Worker new_index[best_out] = next_index++;
330*f4ee7fbaSAndroid Build Coastguard Worker }
331*f4ee7fbaSAndroid Build Coastguard Worker }
332*f4ee7fbaSAndroid Build Coastguard Worker }
333*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, clusters);
334*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, all_histograms);
335*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(
336*f4ee7fbaSAndroid Build Coastguard Worker m, uint8_t, split->types, split->types_alloc_size, num_blocks);
337*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(
338*f4ee7fbaSAndroid Build Coastguard Worker m, uint32_t, split->lengths, split->lengths_alloc_size, num_blocks);
339*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
340*f4ee7fbaSAndroid Build Coastguard Worker {
341*f4ee7fbaSAndroid Build Coastguard Worker uint32_t cur_length = 0;
342*f4ee7fbaSAndroid Build Coastguard Worker size_t block_idx = 0;
343*f4ee7fbaSAndroid Build Coastguard Worker uint8_t max_type = 0;
344*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_blocks; ++i) {
345*f4ee7fbaSAndroid Build Coastguard Worker cur_length += block_lengths[i];
346*f4ee7fbaSAndroid Build Coastguard Worker if (i + 1 == num_blocks ||
347*f4ee7fbaSAndroid Build Coastguard Worker histogram_symbols[i] != histogram_symbols[i + 1]) {
348*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t id = (uint8_t)new_index[histogram_symbols[i]];
349*f4ee7fbaSAndroid Build Coastguard Worker split->types[block_idx] = id;
350*f4ee7fbaSAndroid Build Coastguard Worker split->lengths[block_idx] = cur_length;
351*f4ee7fbaSAndroid Build Coastguard Worker max_type = BROTLI_MAX(uint8_t, max_type, id);
352*f4ee7fbaSAndroid Build Coastguard Worker cur_length = 0;
353*f4ee7fbaSAndroid Build Coastguard Worker ++block_idx;
354*f4ee7fbaSAndroid Build Coastguard Worker }
355*f4ee7fbaSAndroid Build Coastguard Worker }
356*f4ee7fbaSAndroid Build Coastguard Worker split->num_blocks = block_idx;
357*f4ee7fbaSAndroid Build Coastguard Worker split->num_types = (size_t)max_type + 1;
358*f4ee7fbaSAndroid Build Coastguard Worker }
359*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, new_index);
360*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, block_lengths);
361*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, histogram_symbols);
362*f4ee7fbaSAndroid Build Coastguard Worker }
363*f4ee7fbaSAndroid Build Coastguard Worker
FN(SplitByteVector)364*f4ee7fbaSAndroid Build Coastguard Worker static void FN(SplitByteVector)(MemoryManager* m,
365*f4ee7fbaSAndroid Build Coastguard Worker const DataType* data, const size_t length,
366*f4ee7fbaSAndroid Build Coastguard Worker const size_t literals_per_histogram,
367*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_histograms,
368*f4ee7fbaSAndroid Build Coastguard Worker const size_t sampling_stride_length,
369*f4ee7fbaSAndroid Build Coastguard Worker const double block_switch_cost,
370*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderParams* params,
371*f4ee7fbaSAndroid Build Coastguard Worker BlockSplit* split) {
372*f4ee7fbaSAndroid Build Coastguard Worker const size_t data_size = FN(HistogramDataSize)();
373*f4ee7fbaSAndroid Build Coastguard Worker size_t num_histograms = length / literals_per_histogram + 1;
374*f4ee7fbaSAndroid Build Coastguard Worker HistogramType* histograms;
375*f4ee7fbaSAndroid Build Coastguard Worker if (num_histograms > max_histograms) {
376*f4ee7fbaSAndroid Build Coastguard Worker num_histograms = max_histograms;
377*f4ee7fbaSAndroid Build Coastguard Worker }
378*f4ee7fbaSAndroid Build Coastguard Worker if (length == 0) {
379*f4ee7fbaSAndroid Build Coastguard Worker split->num_types = 1;
380*f4ee7fbaSAndroid Build Coastguard Worker return;
381*f4ee7fbaSAndroid Build Coastguard Worker } else if (length < kMinLengthForBlockSplitting) {
382*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(m, uint8_t,
383*f4ee7fbaSAndroid Build Coastguard Worker split->types, split->types_alloc_size, split->num_blocks + 1);
384*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(m, uint32_t,
385*f4ee7fbaSAndroid Build Coastguard Worker split->lengths, split->lengths_alloc_size, split->num_blocks + 1);
386*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
387*f4ee7fbaSAndroid Build Coastguard Worker split->num_types = 1;
388*f4ee7fbaSAndroid Build Coastguard Worker split->types[split->num_blocks] = 0;
389*f4ee7fbaSAndroid Build Coastguard Worker split->lengths[split->num_blocks] = (uint32_t)length;
390*f4ee7fbaSAndroid Build Coastguard Worker split->num_blocks++;
391*f4ee7fbaSAndroid Build Coastguard Worker return;
392*f4ee7fbaSAndroid Build Coastguard Worker }
393*f4ee7fbaSAndroid Build Coastguard Worker histograms = BROTLI_ALLOC(m, HistogramType, num_histograms);
394*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(histograms)) return;
395*f4ee7fbaSAndroid Build Coastguard Worker /* Find good entropy codes. */
396*f4ee7fbaSAndroid Build Coastguard Worker FN(InitialEntropyCodes)(data, length,
397*f4ee7fbaSAndroid Build Coastguard Worker sampling_stride_length,
398*f4ee7fbaSAndroid Build Coastguard Worker num_histograms, histograms);
399*f4ee7fbaSAndroid Build Coastguard Worker FN(RefineEntropyCodes)(data, length,
400*f4ee7fbaSAndroid Build Coastguard Worker sampling_stride_length,
401*f4ee7fbaSAndroid Build Coastguard Worker num_histograms, histograms);
402*f4ee7fbaSAndroid Build Coastguard Worker {
403*f4ee7fbaSAndroid Build Coastguard Worker /* Find a good path through literals with the good entropy codes. */
404*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* block_ids = BROTLI_ALLOC(m, uint8_t, length);
405*f4ee7fbaSAndroid Build Coastguard Worker size_t num_blocks = 0;
406*f4ee7fbaSAndroid Build Coastguard Worker const size_t bitmaplen = (num_histograms + 7) >> 3;
407*f4ee7fbaSAndroid Build Coastguard Worker double* insert_cost = BROTLI_ALLOC(m, double, data_size * num_histograms);
408*f4ee7fbaSAndroid Build Coastguard Worker double* cost = BROTLI_ALLOC(m, double, num_histograms);
409*f4ee7fbaSAndroid Build Coastguard Worker uint8_t* switch_signal = BROTLI_ALLOC(m, uint8_t, length * bitmaplen);
410*f4ee7fbaSAndroid Build Coastguard Worker uint16_t* new_id = BROTLI_ALLOC(m, uint16_t, num_histograms);
411*f4ee7fbaSAndroid Build Coastguard Worker const size_t iters = params->quality < HQ_ZOPFLIFICATION_QUALITY ? 3 : 10;
412*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
413*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(block_ids) ||
414*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(insert_cost) || BROTLI_IS_NULL(cost) ||
415*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(switch_signal) || BROTLI_IS_NULL(new_id)) {
416*f4ee7fbaSAndroid Build Coastguard Worker return;
417*f4ee7fbaSAndroid Build Coastguard Worker }
418*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < iters; ++i) {
419*f4ee7fbaSAndroid Build Coastguard Worker num_blocks = FN(FindBlocks)(data, length,
420*f4ee7fbaSAndroid Build Coastguard Worker block_switch_cost,
421*f4ee7fbaSAndroid Build Coastguard Worker num_histograms, histograms,
422*f4ee7fbaSAndroid Build Coastguard Worker insert_cost, cost, switch_signal,
423*f4ee7fbaSAndroid Build Coastguard Worker block_ids);
424*f4ee7fbaSAndroid Build Coastguard Worker num_histograms = FN(RemapBlockIds)(block_ids, length,
425*f4ee7fbaSAndroid Build Coastguard Worker new_id, num_histograms);
426*f4ee7fbaSAndroid Build Coastguard Worker FN(BuildBlockHistograms)(data, length, block_ids,
427*f4ee7fbaSAndroid Build Coastguard Worker num_histograms, histograms);
428*f4ee7fbaSAndroid Build Coastguard Worker }
429*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, insert_cost);
430*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, cost);
431*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, switch_signal);
432*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, new_id);
433*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, histograms);
434*f4ee7fbaSAndroid Build Coastguard Worker FN(ClusterBlocks)(m, data, length, num_blocks, block_ids, split);
435*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
436*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, block_ids);
437*f4ee7fbaSAndroid Build Coastguard Worker }
438*f4ee7fbaSAndroid Build Coastguard Worker }
439*f4ee7fbaSAndroid Build Coastguard Worker
440*f4ee7fbaSAndroid Build Coastguard Worker #undef HistogramType
441