xref: /aosp_15_r20/external/brotli/c/enc/metablock_inc.h (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker /* NOLINT(build/header_guard) */
2*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2015 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 */
9*f4ee7fbaSAndroid Build Coastguard Worker 
10*f4ee7fbaSAndroid Build Coastguard Worker #define HistogramType FN(Histogram)
11*f4ee7fbaSAndroid Build Coastguard Worker 
12*f4ee7fbaSAndroid Build Coastguard Worker /* Greedy block splitter for one block category (literal, command or distance).
13*f4ee7fbaSAndroid Build Coastguard Worker */
14*f4ee7fbaSAndroid Build Coastguard Worker typedef struct FN(BlockSplitter) {
15*f4ee7fbaSAndroid Build Coastguard Worker   /* Alphabet size of particular block category. */
16*f4ee7fbaSAndroid Build Coastguard Worker   size_t alphabet_size_;
17*f4ee7fbaSAndroid Build Coastguard Worker   /* We collect at least this many symbols for each block. */
18*f4ee7fbaSAndroid Build Coastguard Worker   size_t min_block_size_;
19*f4ee7fbaSAndroid Build Coastguard Worker   /* We merge histograms A and B if
20*f4ee7fbaSAndroid Build Coastguard Worker        entropy(A+B) < entropy(A) + entropy(B) + split_threshold_,
21*f4ee7fbaSAndroid Build Coastguard Worker      where A is the current histogram and B is the histogram of the last or the
22*f4ee7fbaSAndroid Build Coastguard Worker      second last block type. */
23*f4ee7fbaSAndroid Build Coastguard Worker   double split_threshold_;
24*f4ee7fbaSAndroid Build Coastguard Worker 
25*f4ee7fbaSAndroid Build Coastguard Worker   size_t num_blocks_;
26*f4ee7fbaSAndroid Build Coastguard Worker   BlockSplit* split_;  /* not owned */
27*f4ee7fbaSAndroid Build Coastguard Worker   HistogramType* histograms_;  /* not owned */
28*f4ee7fbaSAndroid Build Coastguard Worker   size_t* histograms_size_;  /* not owned */
29*f4ee7fbaSAndroid Build Coastguard Worker 
30*f4ee7fbaSAndroid Build Coastguard Worker   /* The number of symbols that we want to collect before deciding on whether
31*f4ee7fbaSAndroid Build Coastguard Worker      or not to merge the block with a previous one or emit a new block. */
32*f4ee7fbaSAndroid Build Coastguard Worker   size_t target_block_size_;
33*f4ee7fbaSAndroid Build Coastguard Worker   /* The number of symbols in the current histogram. */
34*f4ee7fbaSAndroid Build Coastguard Worker   size_t block_size_;
35*f4ee7fbaSAndroid Build Coastguard Worker   /* Offset of the current histogram. */
36*f4ee7fbaSAndroid Build Coastguard Worker   size_t curr_histogram_ix_;
37*f4ee7fbaSAndroid Build Coastguard Worker   /* Offset of the histograms of the previous two block types. */
38*f4ee7fbaSAndroid Build Coastguard Worker   size_t last_histogram_ix_[2];
39*f4ee7fbaSAndroid Build Coastguard Worker   /* Entropy of the previous two block types. */
40*f4ee7fbaSAndroid Build Coastguard Worker   double last_entropy_[2];
41*f4ee7fbaSAndroid Build Coastguard Worker   /* The number of times we merged the current block with the last one. */
42*f4ee7fbaSAndroid Build Coastguard Worker   size_t merge_last_count_;
43*f4ee7fbaSAndroid Build Coastguard Worker } FN(BlockSplitter);
44*f4ee7fbaSAndroid Build Coastguard Worker 
FN(InitBlockSplitter)45*f4ee7fbaSAndroid Build Coastguard Worker static void FN(InitBlockSplitter)(
46*f4ee7fbaSAndroid Build Coastguard Worker     MemoryManager* m, FN(BlockSplitter)* self, size_t alphabet_size,
47*f4ee7fbaSAndroid Build Coastguard Worker     size_t min_block_size, double split_threshold, size_t num_symbols,
48*f4ee7fbaSAndroid Build Coastguard Worker     BlockSplit* split, HistogramType** histograms, size_t* histograms_size) {
49*f4ee7fbaSAndroid Build Coastguard Worker   size_t max_num_blocks = num_symbols / min_block_size + 1;
50*f4ee7fbaSAndroid Build Coastguard Worker   /* We have to allocate one more histogram than the maximum number of block
51*f4ee7fbaSAndroid Build Coastguard Worker      types for the current histogram when the meta-block is too big. */
52*f4ee7fbaSAndroid Build Coastguard Worker   size_t max_num_types =
53*f4ee7fbaSAndroid Build Coastguard Worker       BROTLI_MIN(size_t, max_num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 1);
54*f4ee7fbaSAndroid Build Coastguard Worker   self->alphabet_size_ = alphabet_size;
55*f4ee7fbaSAndroid Build Coastguard Worker   self->min_block_size_ = min_block_size;
56*f4ee7fbaSAndroid Build Coastguard Worker   self->split_threshold_ = split_threshold;
57*f4ee7fbaSAndroid Build Coastguard Worker   self->num_blocks_ = 0;
58*f4ee7fbaSAndroid Build Coastguard Worker   self->split_ = split;
59*f4ee7fbaSAndroid Build Coastguard Worker   self->histograms_size_ = histograms_size;
60*f4ee7fbaSAndroid Build Coastguard Worker   self->target_block_size_ = min_block_size;
61*f4ee7fbaSAndroid Build Coastguard Worker   self->block_size_ = 0;
62*f4ee7fbaSAndroid Build Coastguard Worker   self->curr_histogram_ix_ = 0;
63*f4ee7fbaSAndroid Build Coastguard Worker   self->merge_last_count_ = 0;
64*f4ee7fbaSAndroid Build Coastguard Worker   BROTLI_ENSURE_CAPACITY(m, uint8_t,
65*f4ee7fbaSAndroid Build Coastguard Worker       split->types, split->types_alloc_size, max_num_blocks);
66*f4ee7fbaSAndroid Build Coastguard Worker   BROTLI_ENSURE_CAPACITY(m, uint32_t,
67*f4ee7fbaSAndroid Build Coastguard Worker       split->lengths, split->lengths_alloc_size, max_num_blocks);
68*f4ee7fbaSAndroid Build Coastguard Worker   if (BROTLI_IS_OOM(m)) return;
69*f4ee7fbaSAndroid Build Coastguard Worker   self->split_->num_blocks = max_num_blocks;
70*f4ee7fbaSAndroid Build Coastguard Worker   BROTLI_DCHECK(*histograms == 0);
71*f4ee7fbaSAndroid Build Coastguard Worker   *histograms_size = max_num_types;
72*f4ee7fbaSAndroid Build Coastguard Worker   *histograms = BROTLI_ALLOC(m, HistogramType, *histograms_size);
73*f4ee7fbaSAndroid Build Coastguard Worker   self->histograms_ = *histograms;
74*f4ee7fbaSAndroid Build Coastguard Worker   if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(*histograms)) return;
75*f4ee7fbaSAndroid Build Coastguard Worker   /* Clear only current histogram. */
76*f4ee7fbaSAndroid Build Coastguard Worker   FN(HistogramClear)(&self->histograms_[0]);
77*f4ee7fbaSAndroid Build Coastguard Worker   self->last_histogram_ix_[0] = self->last_histogram_ix_[1] = 0;
78*f4ee7fbaSAndroid Build Coastguard Worker }
79*f4ee7fbaSAndroid Build Coastguard Worker 
80*f4ee7fbaSAndroid Build Coastguard Worker /* Does either of three things:
81*f4ee7fbaSAndroid Build Coastguard Worker      (1) emits the current block with a new block type;
82*f4ee7fbaSAndroid Build Coastguard Worker      (2) emits the current block with the type of the second last block;
83*f4ee7fbaSAndroid Build Coastguard Worker      (3) merges the current block with the last block. */
FN(BlockSplitterFinishBlock)84*f4ee7fbaSAndroid Build Coastguard Worker static void FN(BlockSplitterFinishBlock)(
85*f4ee7fbaSAndroid Build Coastguard Worker     FN(BlockSplitter)* self, BROTLI_BOOL is_final) {
86*f4ee7fbaSAndroid Build Coastguard Worker   BlockSplit* split = self->split_;
87*f4ee7fbaSAndroid Build Coastguard Worker   double* last_entropy = self->last_entropy_;
88*f4ee7fbaSAndroid Build Coastguard Worker   HistogramType* histograms = self->histograms_;
89*f4ee7fbaSAndroid Build Coastguard Worker   self->block_size_ =
90*f4ee7fbaSAndroid Build Coastguard Worker       BROTLI_MAX(size_t, self->block_size_, self->min_block_size_);
91*f4ee7fbaSAndroid Build Coastguard Worker   if (self->num_blocks_ == 0) {
92*f4ee7fbaSAndroid Build Coastguard Worker     /* Create first block. */
93*f4ee7fbaSAndroid Build Coastguard Worker     split->lengths[0] = (uint32_t)self->block_size_;
94*f4ee7fbaSAndroid Build Coastguard Worker     split->types[0] = 0;
95*f4ee7fbaSAndroid Build Coastguard Worker     last_entropy[0] =
96*f4ee7fbaSAndroid Build Coastguard Worker         BitsEntropy(histograms[0].data_, self->alphabet_size_);
97*f4ee7fbaSAndroid Build Coastguard Worker     last_entropy[1] = last_entropy[0];
98*f4ee7fbaSAndroid Build Coastguard Worker     ++self->num_blocks_;
99*f4ee7fbaSAndroid Build Coastguard Worker     ++split->num_types;
100*f4ee7fbaSAndroid Build Coastguard Worker     ++self->curr_histogram_ix_;
101*f4ee7fbaSAndroid Build Coastguard Worker     if (self->curr_histogram_ix_ < *self->histograms_size_)
102*f4ee7fbaSAndroid Build Coastguard Worker       FN(HistogramClear)(&histograms[self->curr_histogram_ix_]);
103*f4ee7fbaSAndroid Build Coastguard Worker     self->block_size_ = 0;
104*f4ee7fbaSAndroid Build Coastguard Worker   } else if (self->block_size_ > 0) {
105*f4ee7fbaSAndroid Build Coastguard Worker     double entropy = BitsEntropy(histograms[self->curr_histogram_ix_].data_,
106*f4ee7fbaSAndroid Build Coastguard Worker                                  self->alphabet_size_);
107*f4ee7fbaSAndroid Build Coastguard Worker     HistogramType combined_histo[2];
108*f4ee7fbaSAndroid Build Coastguard Worker     double combined_entropy[2];
109*f4ee7fbaSAndroid Build Coastguard Worker     double diff[2];
110*f4ee7fbaSAndroid Build Coastguard Worker     size_t j;
111*f4ee7fbaSAndroid Build Coastguard Worker     for (j = 0; j < 2; ++j) {
112*f4ee7fbaSAndroid Build Coastguard Worker       size_t last_histogram_ix = self->last_histogram_ix_[j];
113*f4ee7fbaSAndroid Build Coastguard Worker       combined_histo[j] = histograms[self->curr_histogram_ix_];
114*f4ee7fbaSAndroid Build Coastguard Worker       FN(HistogramAddHistogram)(&combined_histo[j],
115*f4ee7fbaSAndroid Build Coastguard Worker           &histograms[last_histogram_ix]);
116*f4ee7fbaSAndroid Build Coastguard Worker       combined_entropy[j] = BitsEntropy(
117*f4ee7fbaSAndroid Build Coastguard Worker           &combined_histo[j].data_[0], self->alphabet_size_);
118*f4ee7fbaSAndroid Build Coastguard Worker       diff[j] = combined_entropy[j] - entropy - last_entropy[j];
119*f4ee7fbaSAndroid Build Coastguard Worker     }
120*f4ee7fbaSAndroid Build Coastguard Worker 
121*f4ee7fbaSAndroid Build Coastguard Worker     if (split->num_types < BROTLI_MAX_NUMBER_OF_BLOCK_TYPES &&
122*f4ee7fbaSAndroid Build Coastguard Worker         diff[0] > self->split_threshold_ &&
123*f4ee7fbaSAndroid Build Coastguard Worker         diff[1] > self->split_threshold_) {
124*f4ee7fbaSAndroid Build Coastguard Worker       /* Create new block. */
125*f4ee7fbaSAndroid Build Coastguard Worker       split->lengths[self->num_blocks_] = (uint32_t)self->block_size_;
126*f4ee7fbaSAndroid Build Coastguard Worker       split->types[self->num_blocks_] = (uint8_t)split->num_types;
127*f4ee7fbaSAndroid Build Coastguard Worker       self->last_histogram_ix_[1] = self->last_histogram_ix_[0];
128*f4ee7fbaSAndroid Build Coastguard Worker       self->last_histogram_ix_[0] = (uint8_t)split->num_types;
129*f4ee7fbaSAndroid Build Coastguard Worker       last_entropy[1] = last_entropy[0];
130*f4ee7fbaSAndroid Build Coastguard Worker       last_entropy[0] = entropy;
131*f4ee7fbaSAndroid Build Coastguard Worker       ++self->num_blocks_;
132*f4ee7fbaSAndroid Build Coastguard Worker       ++split->num_types;
133*f4ee7fbaSAndroid Build Coastguard Worker       ++self->curr_histogram_ix_;
134*f4ee7fbaSAndroid Build Coastguard Worker       if (self->curr_histogram_ix_ < *self->histograms_size_)
135*f4ee7fbaSAndroid Build Coastguard Worker         FN(HistogramClear)(&histograms[self->curr_histogram_ix_]);
136*f4ee7fbaSAndroid Build Coastguard Worker       self->block_size_ = 0;
137*f4ee7fbaSAndroid Build Coastguard Worker       self->merge_last_count_ = 0;
138*f4ee7fbaSAndroid Build Coastguard Worker       self->target_block_size_ = self->min_block_size_;
139*f4ee7fbaSAndroid Build Coastguard Worker     } else if (diff[1] < diff[0] - 20.0) {
140*f4ee7fbaSAndroid Build Coastguard Worker       /* Combine this block with second last block. */
141*f4ee7fbaSAndroid Build Coastguard Worker       split->lengths[self->num_blocks_] = (uint32_t)self->block_size_;
142*f4ee7fbaSAndroid Build Coastguard Worker       split->types[self->num_blocks_] = split->types[self->num_blocks_ - 2];
143*f4ee7fbaSAndroid Build Coastguard Worker       BROTLI_SWAP(size_t, self->last_histogram_ix_, 0, 1);
144*f4ee7fbaSAndroid Build Coastguard Worker       histograms[self->last_histogram_ix_[0]] = combined_histo[1];
145*f4ee7fbaSAndroid Build Coastguard Worker       last_entropy[1] = last_entropy[0];
146*f4ee7fbaSAndroid Build Coastguard Worker       last_entropy[0] = combined_entropy[1];
147*f4ee7fbaSAndroid Build Coastguard Worker       ++self->num_blocks_;
148*f4ee7fbaSAndroid Build Coastguard Worker       self->block_size_ = 0;
149*f4ee7fbaSAndroid Build Coastguard Worker       FN(HistogramClear)(&histograms[self->curr_histogram_ix_]);
150*f4ee7fbaSAndroid Build Coastguard Worker       self->merge_last_count_ = 0;
151*f4ee7fbaSAndroid Build Coastguard Worker       self->target_block_size_ = self->min_block_size_;
152*f4ee7fbaSAndroid Build Coastguard Worker     } else {
153*f4ee7fbaSAndroid Build Coastguard Worker       /* Combine this block with last block. */
154*f4ee7fbaSAndroid Build Coastguard Worker       split->lengths[self->num_blocks_ - 1] += (uint32_t)self->block_size_;
155*f4ee7fbaSAndroid Build Coastguard Worker       histograms[self->last_histogram_ix_[0]] = combined_histo[0];
156*f4ee7fbaSAndroid Build Coastguard Worker       last_entropy[0] = combined_entropy[0];
157*f4ee7fbaSAndroid Build Coastguard Worker       if (split->num_types == 1) {
158*f4ee7fbaSAndroid Build Coastguard Worker         last_entropy[1] = last_entropy[0];
159*f4ee7fbaSAndroid Build Coastguard Worker       }
160*f4ee7fbaSAndroid Build Coastguard Worker       self->block_size_ = 0;
161*f4ee7fbaSAndroid Build Coastguard Worker       FN(HistogramClear)(&histograms[self->curr_histogram_ix_]);
162*f4ee7fbaSAndroid Build Coastguard Worker       if (++self->merge_last_count_ > 1) {
163*f4ee7fbaSAndroid Build Coastguard Worker         self->target_block_size_ += self->min_block_size_;
164*f4ee7fbaSAndroid Build Coastguard Worker       }
165*f4ee7fbaSAndroid Build Coastguard Worker     }
166*f4ee7fbaSAndroid Build Coastguard Worker   }
167*f4ee7fbaSAndroid Build Coastguard Worker   if (is_final) {
168*f4ee7fbaSAndroid Build Coastguard Worker     *self->histograms_size_ = split->num_types;
169*f4ee7fbaSAndroid Build Coastguard Worker     split->num_blocks = self->num_blocks_;
170*f4ee7fbaSAndroid Build Coastguard Worker   }
171*f4ee7fbaSAndroid Build Coastguard Worker }
172*f4ee7fbaSAndroid Build Coastguard Worker 
173*f4ee7fbaSAndroid Build Coastguard Worker /* Adds the next symbol to the current histogram. When the current histogram
174*f4ee7fbaSAndroid Build Coastguard Worker    reaches the target size, decides on merging the block. */
FN(BlockSplitterAddSymbol)175*f4ee7fbaSAndroid Build Coastguard Worker static void FN(BlockSplitterAddSymbol)(FN(BlockSplitter)* self, size_t symbol) {
176*f4ee7fbaSAndroid Build Coastguard Worker   FN(HistogramAdd)(&self->histograms_[self->curr_histogram_ix_], symbol);
177*f4ee7fbaSAndroid Build Coastguard Worker   ++self->block_size_;
178*f4ee7fbaSAndroid Build Coastguard Worker   if (self->block_size_ == self->target_block_size_) {
179*f4ee7fbaSAndroid Build Coastguard Worker     FN(BlockSplitterFinishBlock)(self, /* is_final = */ BROTLI_FALSE);
180*f4ee7fbaSAndroid Build Coastguard Worker   }
181*f4ee7fbaSAndroid Build Coastguard Worker }
182*f4ee7fbaSAndroid Build Coastguard Worker 
183*f4ee7fbaSAndroid Build Coastguard Worker #undef HistogramType
184