metablock_inc.h 7.3 KB

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