metablock.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* Copyright 2015 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Algorithms for distributing the literals and commands of a metablock between
  6. block types and contexts. */
  7. #include "./metablock.h"
  8. #include "../common/constants.h"
  9. #include "../common/context.h"
  10. #include "../common/platform.h"
  11. #include <brotli/types.h>
  12. #include "./bit_cost.h"
  13. #include "./block_splitter.h"
  14. #include "./cluster.h"
  15. #include "./entropy_encode.h"
  16. #include "./histogram.h"
  17. #include "./memory.h"
  18. #include "./quality.h"
  19. #if defined(__cplusplus) || defined(c_plusplus)
  20. extern "C" {
  21. #endif
  22. void BrotliInitDistanceParams(BrotliEncoderParams* params,
  23. uint32_t npostfix, uint32_t ndirect) {
  24. BrotliDistanceParams* dist_params = &params->dist;
  25. uint32_t alphabet_size, max_distance;
  26. dist_params->distance_postfix_bits = npostfix;
  27. dist_params->num_direct_distance_codes = ndirect;
  28. alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE(
  29. npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS);
  30. max_distance = ndirect + (1U << (BROTLI_MAX_DISTANCE_BITS + npostfix + 2)) -
  31. (1U << (npostfix + 2));
  32. if (params->large_window) {
  33. static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28};
  34. uint32_t postfix = 1U << npostfix;
  35. alphabet_size = BROTLI_DISTANCE_ALPHABET_SIZE(
  36. npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS);
  37. /* The maximum distance is set so that no distance symbol used can encode
  38. a distance larger than BROTLI_MAX_ALLOWED_DISTANCE with all
  39. its extra bits set. */
  40. if (ndirect < bound[npostfix]) {
  41. max_distance = BROTLI_MAX_ALLOWED_DISTANCE - (bound[npostfix] - ndirect);
  42. } else if (ndirect >= bound[npostfix] + postfix) {
  43. max_distance = (3U << 29) - 4 + (ndirect - bound[npostfix]);
  44. } else {
  45. max_distance = BROTLI_MAX_ALLOWED_DISTANCE;
  46. }
  47. }
  48. dist_params->alphabet_size = alphabet_size;
  49. dist_params->max_distance = max_distance;
  50. }
  51. static void RecomputeDistancePrefixes(Command* cmds,
  52. size_t num_commands,
  53. const BrotliDistanceParams* orig_params,
  54. const BrotliDistanceParams* new_params) {
  55. size_t i;
  56. if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits &&
  57. orig_params->num_direct_distance_codes ==
  58. new_params->num_direct_distance_codes) {
  59. return;
  60. }
  61. for (i = 0; i < num_commands; ++i) {
  62. Command* cmd = &cmds[i];
  63. if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
  64. PrefixEncodeCopyDistance(CommandRestoreDistanceCode(cmd, orig_params),
  65. new_params->num_direct_distance_codes,
  66. new_params->distance_postfix_bits,
  67. &cmd->dist_prefix_,
  68. &cmd->dist_extra_);
  69. }
  70. }
  71. }
  72. static BROTLI_BOOL ComputeDistanceCost(const Command* cmds,
  73. size_t num_commands,
  74. const BrotliDistanceParams* orig_params,
  75. const BrotliDistanceParams* new_params,
  76. double* cost) {
  77. size_t i;
  78. BROTLI_BOOL equal_params = BROTLI_FALSE;
  79. uint16_t dist_prefix;
  80. uint32_t dist_extra;
  81. double extra_bits = 0.0;
  82. HistogramDistance histo;
  83. HistogramClearDistance(&histo);
  84. if (orig_params->distance_postfix_bits == new_params->distance_postfix_bits &&
  85. orig_params->num_direct_distance_codes ==
  86. new_params->num_direct_distance_codes) {
  87. equal_params = BROTLI_TRUE;
  88. }
  89. for (i = 0; i < num_commands; i++) {
  90. const Command* cmd = &cmds[i];
  91. if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
  92. if (equal_params) {
  93. dist_prefix = cmd->dist_prefix_;
  94. } else {
  95. uint32_t distance = CommandRestoreDistanceCode(cmd, orig_params);
  96. if (distance > new_params->max_distance) {
  97. return BROTLI_FALSE;
  98. }
  99. PrefixEncodeCopyDistance(distance,
  100. new_params->num_direct_distance_codes,
  101. new_params->distance_postfix_bits,
  102. &dist_prefix,
  103. &dist_extra);
  104. }
  105. HistogramAddDistance(&histo, dist_prefix & 0x3FF);
  106. extra_bits += dist_prefix >> 10;
  107. }
  108. }
  109. *cost = BrotliPopulationCostDistance(&histo) + extra_bits;
  110. return BROTLI_TRUE;
  111. }
  112. void BrotliBuildMetaBlock(MemoryManager* m,
  113. const uint8_t* ringbuffer,
  114. const size_t pos,
  115. const size_t mask,
  116. BrotliEncoderParams* params,
  117. uint8_t prev_byte,
  118. uint8_t prev_byte2,
  119. Command* cmds,
  120. size_t num_commands,
  121. ContextType literal_context_mode,
  122. MetaBlockSplit* mb) {
  123. /* Histogram ids need to fit in one byte. */
  124. static const size_t kMaxNumberOfHistograms = 256;
  125. HistogramDistance* distance_histograms;
  126. HistogramLiteral* literal_histograms;
  127. ContextType* literal_context_modes = NULL;
  128. size_t literal_histograms_size;
  129. size_t distance_histograms_size;
  130. size_t i;
  131. size_t literal_context_multiplier = 1;
  132. uint32_t npostfix;
  133. uint32_t ndirect_msb = 0;
  134. BROTLI_BOOL check_orig = BROTLI_TRUE;
  135. double best_dist_cost = 1e99;
  136. BrotliEncoderParams orig_params = *params;
  137. BrotliEncoderParams new_params = *params;
  138. for (npostfix = 0; npostfix <= BROTLI_MAX_NPOSTFIX; npostfix++) {
  139. for (; ndirect_msb < 16; ndirect_msb++) {
  140. uint32_t ndirect = ndirect_msb << npostfix;
  141. BROTLI_BOOL skip;
  142. double dist_cost;
  143. BrotliInitDistanceParams(&new_params, npostfix, ndirect);
  144. if (npostfix == orig_params.dist.distance_postfix_bits &&
  145. ndirect == orig_params.dist.num_direct_distance_codes) {
  146. check_orig = BROTLI_FALSE;
  147. }
  148. skip = !ComputeDistanceCost(
  149. cmds, num_commands,
  150. &orig_params.dist, &new_params.dist, &dist_cost);
  151. if (skip || (dist_cost > best_dist_cost)) {
  152. break;
  153. }
  154. best_dist_cost = dist_cost;
  155. params->dist = new_params.dist;
  156. }
  157. if (ndirect_msb > 0) ndirect_msb--;
  158. ndirect_msb /= 2;
  159. }
  160. if (check_orig) {
  161. double dist_cost;
  162. ComputeDistanceCost(cmds, num_commands,
  163. &orig_params.dist, &orig_params.dist, &dist_cost);
  164. if (dist_cost < best_dist_cost) {
  165. /* NB: currently unused; uncomment when more param tuning is added. */
  166. /* best_dist_cost = dist_cost; */
  167. params->dist = orig_params.dist;
  168. }
  169. }
  170. RecomputeDistancePrefixes(cmds, num_commands,
  171. &orig_params.dist, &params->dist);
  172. BrotliSplitBlock(m, cmds, num_commands,
  173. ringbuffer, pos, mask, params,
  174. &mb->literal_split,
  175. &mb->command_split,
  176. &mb->distance_split);
  177. if (BROTLI_IS_OOM(m)) return;
  178. if (!params->disable_literal_context_modeling) {
  179. literal_context_multiplier = 1 << BROTLI_LITERAL_CONTEXT_BITS;
  180. literal_context_modes =
  181. BROTLI_ALLOC(m, ContextType, mb->literal_split.num_types);
  182. if (BROTLI_IS_OOM(m)) return;
  183. for (i = 0; i < mb->literal_split.num_types; ++i) {
  184. literal_context_modes[i] = literal_context_mode;
  185. }
  186. }
  187. literal_histograms_size =
  188. mb->literal_split.num_types * literal_context_multiplier;
  189. literal_histograms =
  190. BROTLI_ALLOC(m, HistogramLiteral, literal_histograms_size);
  191. if (BROTLI_IS_OOM(m)) return;
  192. ClearHistogramsLiteral(literal_histograms, literal_histograms_size);
  193. distance_histograms_size =
  194. mb->distance_split.num_types << BROTLI_DISTANCE_CONTEXT_BITS;
  195. distance_histograms =
  196. BROTLI_ALLOC(m, HistogramDistance, distance_histograms_size);
  197. if (BROTLI_IS_OOM(m)) return;
  198. ClearHistogramsDistance(distance_histograms, distance_histograms_size);
  199. BROTLI_DCHECK(mb->command_histograms == 0);
  200. mb->command_histograms_size = mb->command_split.num_types;
  201. mb->command_histograms =
  202. BROTLI_ALLOC(m, HistogramCommand, mb->command_histograms_size);
  203. if (BROTLI_IS_OOM(m)) return;
  204. ClearHistogramsCommand(mb->command_histograms, mb->command_histograms_size);
  205. BrotliBuildHistogramsWithContext(cmds, num_commands,
  206. &mb->literal_split, &mb->command_split, &mb->distance_split,
  207. ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_modes,
  208. literal_histograms, mb->command_histograms, distance_histograms);
  209. BROTLI_FREE(m, literal_context_modes);
  210. BROTLI_DCHECK(mb->literal_context_map == 0);
  211. mb->literal_context_map_size =
  212. mb->literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS;
  213. mb->literal_context_map =
  214. BROTLI_ALLOC(m, uint32_t, mb->literal_context_map_size);
  215. if (BROTLI_IS_OOM(m)) return;
  216. BROTLI_DCHECK(mb->literal_histograms == 0);
  217. mb->literal_histograms_size = mb->literal_context_map_size;
  218. mb->literal_histograms =
  219. BROTLI_ALLOC(m, HistogramLiteral, mb->literal_histograms_size);
  220. if (BROTLI_IS_OOM(m)) return;
  221. BrotliClusterHistogramsLiteral(m, literal_histograms, literal_histograms_size,
  222. kMaxNumberOfHistograms, mb->literal_histograms,
  223. &mb->literal_histograms_size, mb->literal_context_map);
  224. if (BROTLI_IS_OOM(m)) return;
  225. BROTLI_FREE(m, literal_histograms);
  226. if (params->disable_literal_context_modeling) {
  227. /* Distribute assignment to all contexts. */
  228. for (i = mb->literal_split.num_types; i != 0;) {
  229. size_t j = 0;
  230. i--;
  231. for (; j < (1 << BROTLI_LITERAL_CONTEXT_BITS); j++) {
  232. mb->literal_context_map[(i << BROTLI_LITERAL_CONTEXT_BITS) + j] =
  233. mb->literal_context_map[i];
  234. }
  235. }
  236. }
  237. BROTLI_DCHECK(mb->distance_context_map == 0);
  238. mb->distance_context_map_size =
  239. mb->distance_split.num_types << BROTLI_DISTANCE_CONTEXT_BITS;
  240. mb->distance_context_map =
  241. BROTLI_ALLOC(m, uint32_t, mb->distance_context_map_size);
  242. if (BROTLI_IS_OOM(m)) return;
  243. BROTLI_DCHECK(mb->distance_histograms == 0);
  244. mb->distance_histograms_size = mb->distance_context_map_size;
  245. mb->distance_histograms =
  246. BROTLI_ALLOC(m, HistogramDistance, mb->distance_histograms_size);
  247. if (BROTLI_IS_OOM(m)) return;
  248. BrotliClusterHistogramsDistance(m, distance_histograms,
  249. mb->distance_context_map_size,
  250. kMaxNumberOfHistograms,
  251. mb->distance_histograms,
  252. &mb->distance_histograms_size,
  253. mb->distance_context_map);
  254. if (BROTLI_IS_OOM(m)) return;
  255. BROTLI_FREE(m, distance_histograms);
  256. }
  257. #define FN(X) X ## Literal
  258. #include "./metablock_inc.h" /* NOLINT(build/include) */
  259. #undef FN
  260. #define FN(X) X ## Command
  261. #include "./metablock_inc.h" /* NOLINT(build/include) */
  262. #undef FN
  263. #define FN(X) X ## Distance
  264. #include "./metablock_inc.h" /* NOLINT(build/include) */
  265. #undef FN
  266. #define BROTLI_MAX_STATIC_CONTEXTS 13
  267. /* Greedy block splitter for one block category (literal, command or distance).
  268. Gathers histograms for all context buckets. */
  269. typedef struct ContextBlockSplitter {
  270. /* Alphabet size of particular block category. */
  271. size_t alphabet_size_;
  272. size_t num_contexts_;
  273. size_t max_block_types_;
  274. /* We collect at least this many symbols for each block. */
  275. size_t min_block_size_;
  276. /* We merge histograms A and B if
  277. entropy(A+B) < entropy(A) + entropy(B) + split_threshold_,
  278. where A is the current histogram and B is the histogram of the last or the
  279. second last block type. */
  280. double split_threshold_;
  281. size_t num_blocks_;
  282. BlockSplit* split_; /* not owned */
  283. HistogramLiteral* histograms_; /* not owned */
  284. size_t* histograms_size_; /* not owned */
  285. /* The number of symbols that we want to collect before deciding on whether
  286. or not to merge the block with a previous one or emit a new block. */
  287. size_t target_block_size_;
  288. /* The number of symbols in the current histogram. */
  289. size_t block_size_;
  290. /* Offset of the current histogram. */
  291. size_t curr_histogram_ix_;
  292. /* Offset of the histograms of the previous two block types. */
  293. size_t last_histogram_ix_[2];
  294. /* Entropy of the previous two block types. */
  295. double last_entropy_[2 * BROTLI_MAX_STATIC_CONTEXTS];
  296. /* The number of times we merged the current block with the last one. */
  297. size_t merge_last_count_;
  298. } ContextBlockSplitter;
  299. static void InitContextBlockSplitter(
  300. MemoryManager* m, ContextBlockSplitter* self, size_t alphabet_size,
  301. size_t num_contexts, size_t min_block_size, double split_threshold,
  302. size_t num_symbols, BlockSplit* split, HistogramLiteral** histograms,
  303. size_t* histograms_size) {
  304. size_t max_num_blocks = num_symbols / min_block_size + 1;
  305. size_t max_num_types;
  306. BROTLI_DCHECK(num_contexts <= BROTLI_MAX_STATIC_CONTEXTS);
  307. self->alphabet_size_ = alphabet_size;
  308. self->num_contexts_ = num_contexts;
  309. self->max_block_types_ = BROTLI_MAX_NUMBER_OF_BLOCK_TYPES / num_contexts;
  310. self->min_block_size_ = min_block_size;
  311. self->split_threshold_ = split_threshold;
  312. self->num_blocks_ = 0;
  313. self->split_ = split;
  314. self->histograms_size_ = histograms_size;
  315. self->target_block_size_ = min_block_size;
  316. self->block_size_ = 0;
  317. self->curr_histogram_ix_ = 0;
  318. self->merge_last_count_ = 0;
  319. /* We have to allocate one more histogram than the maximum number of block
  320. types for the current histogram when the meta-block is too big. */
  321. max_num_types =
  322. BROTLI_MIN(size_t, max_num_blocks, self->max_block_types_ + 1);
  323. BROTLI_ENSURE_CAPACITY(m, uint8_t,
  324. split->types, split->types_alloc_size, max_num_blocks);
  325. BROTLI_ENSURE_CAPACITY(m, uint32_t,
  326. split->lengths, split->lengths_alloc_size, max_num_blocks);
  327. if (BROTLI_IS_OOM(m)) return;
  328. split->num_blocks = max_num_blocks;
  329. if (BROTLI_IS_OOM(m)) return;
  330. BROTLI_DCHECK(*histograms == 0);
  331. *histograms_size = max_num_types * num_contexts;
  332. *histograms = BROTLI_ALLOC(m, HistogramLiteral, *histograms_size);
  333. self->histograms_ = *histograms;
  334. if (BROTLI_IS_OOM(m)) return;
  335. /* Clear only current histogram. */
  336. ClearHistogramsLiteral(&self->histograms_[0], num_contexts);
  337. self->last_histogram_ix_[0] = self->last_histogram_ix_[1] = 0;
  338. }
  339. /* Does either of three things:
  340. (1) emits the current block with a new block type;
  341. (2) emits the current block with the type of the second last block;
  342. (3) merges the current block with the last block. */
  343. static void ContextBlockSplitterFinishBlock(
  344. ContextBlockSplitter* self, MemoryManager* m, BROTLI_BOOL is_final) {
  345. BlockSplit* split = self->split_;
  346. const size_t num_contexts = self->num_contexts_;
  347. double* last_entropy = self->last_entropy_;
  348. HistogramLiteral* histograms = self->histograms_;
  349. if (self->block_size_ < self->min_block_size_) {
  350. self->block_size_ = self->min_block_size_;
  351. }
  352. if (self->num_blocks_ == 0) {
  353. size_t i;
  354. /* Create first block. */
  355. split->lengths[0] = (uint32_t)self->block_size_;
  356. split->types[0] = 0;
  357. for (i = 0; i < num_contexts; ++i) {
  358. last_entropy[i] =
  359. BitsEntropy(histograms[i].data_, self->alphabet_size_);
  360. last_entropy[num_contexts + i] = last_entropy[i];
  361. }
  362. ++self->num_blocks_;
  363. ++split->num_types;
  364. self->curr_histogram_ix_ += num_contexts;
  365. if (self->curr_histogram_ix_ < *self->histograms_size_) {
  366. ClearHistogramsLiteral(
  367. &self->histograms_[self->curr_histogram_ix_], self->num_contexts_);
  368. }
  369. self->block_size_ = 0;
  370. } else if (self->block_size_ > 0) {
  371. /* Try merging the set of histograms for the current block type with the
  372. respective set of histograms for the last and second last block types.
  373. Decide over the split based on the total reduction of entropy across
  374. all contexts. */
  375. double entropy[BROTLI_MAX_STATIC_CONTEXTS];
  376. HistogramLiteral* combined_histo =
  377. BROTLI_ALLOC(m, HistogramLiteral, 2 * num_contexts);
  378. double combined_entropy[2 * BROTLI_MAX_STATIC_CONTEXTS];
  379. double diff[2] = { 0.0 };
  380. size_t i;
  381. if (BROTLI_IS_OOM(m)) return;
  382. for (i = 0; i < num_contexts; ++i) {
  383. size_t curr_histo_ix = self->curr_histogram_ix_ + i;
  384. size_t j;
  385. entropy[i] = BitsEntropy(histograms[curr_histo_ix].data_,
  386. self->alphabet_size_);
  387. for (j = 0; j < 2; ++j) {
  388. size_t jx = j * num_contexts + i;
  389. size_t last_histogram_ix = self->last_histogram_ix_[j] + i;
  390. combined_histo[jx] = histograms[curr_histo_ix];
  391. HistogramAddHistogramLiteral(&combined_histo[jx],
  392. &histograms[last_histogram_ix]);
  393. combined_entropy[jx] = BitsEntropy(
  394. &combined_histo[jx].data_[0], self->alphabet_size_);
  395. diff[j] += combined_entropy[jx] - entropy[i] - last_entropy[jx];
  396. }
  397. }
  398. if (split->num_types < self->max_block_types_ &&
  399. diff[0] > self->split_threshold_ &&
  400. diff[1] > self->split_threshold_) {
  401. /* Create new block. */
  402. split->lengths[self->num_blocks_] = (uint32_t)self->block_size_;
  403. split->types[self->num_blocks_] = (uint8_t)split->num_types;
  404. self->last_histogram_ix_[1] = self->last_histogram_ix_[0];
  405. self->last_histogram_ix_[0] = split->num_types * num_contexts;
  406. for (i = 0; i < num_contexts; ++i) {
  407. last_entropy[num_contexts + i] = last_entropy[i];
  408. last_entropy[i] = entropy[i];
  409. }
  410. ++self->num_blocks_;
  411. ++split->num_types;
  412. self->curr_histogram_ix_ += num_contexts;
  413. if (self->curr_histogram_ix_ < *self->histograms_size_) {
  414. ClearHistogramsLiteral(
  415. &self->histograms_[self->curr_histogram_ix_], self->num_contexts_);
  416. }
  417. self->block_size_ = 0;
  418. self->merge_last_count_ = 0;
  419. self->target_block_size_ = self->min_block_size_;
  420. } else if (diff[1] < diff[0] - 20.0) {
  421. /* Combine this block with second last block. */
  422. split->lengths[self->num_blocks_] = (uint32_t)self->block_size_;
  423. split->types[self->num_blocks_] = split->types[self->num_blocks_ - 2];
  424. BROTLI_SWAP(size_t, self->last_histogram_ix_, 0, 1);
  425. for (i = 0; i < num_contexts; ++i) {
  426. histograms[self->last_histogram_ix_[0] + i] =
  427. combined_histo[num_contexts + i];
  428. last_entropy[num_contexts + i] = last_entropy[i];
  429. last_entropy[i] = combined_entropy[num_contexts + i];
  430. HistogramClearLiteral(&histograms[self->curr_histogram_ix_ + i]);
  431. }
  432. ++self->num_blocks_;
  433. self->block_size_ = 0;
  434. self->merge_last_count_ = 0;
  435. self->target_block_size_ = self->min_block_size_;
  436. } else {
  437. /* Combine this block with last block. */
  438. split->lengths[self->num_blocks_ - 1] += (uint32_t)self->block_size_;
  439. for (i = 0; i < num_contexts; ++i) {
  440. histograms[self->last_histogram_ix_[0] + i] = combined_histo[i];
  441. last_entropy[i] = combined_entropy[i];
  442. if (split->num_types == 1) {
  443. last_entropy[num_contexts + i] = last_entropy[i];
  444. }
  445. HistogramClearLiteral(&histograms[self->curr_histogram_ix_ + i]);
  446. }
  447. self->block_size_ = 0;
  448. if (++self->merge_last_count_ > 1) {
  449. self->target_block_size_ += self->min_block_size_;
  450. }
  451. }
  452. BROTLI_FREE(m, combined_histo);
  453. }
  454. if (is_final) {
  455. *self->histograms_size_ = split->num_types * num_contexts;
  456. split->num_blocks = self->num_blocks_;
  457. }
  458. }
  459. /* Adds the next symbol to the current block type and context. When the
  460. current block reaches the target size, decides on merging the block. */
  461. static void ContextBlockSplitterAddSymbol(
  462. ContextBlockSplitter* self, MemoryManager* m,
  463. size_t symbol, size_t context) {
  464. HistogramAddLiteral(&self->histograms_[self->curr_histogram_ix_ + context],
  465. symbol);
  466. ++self->block_size_;
  467. if (self->block_size_ == self->target_block_size_) {
  468. ContextBlockSplitterFinishBlock(self, m, /* is_final = */ BROTLI_FALSE);
  469. if (BROTLI_IS_OOM(m)) return;
  470. }
  471. }
  472. static void MapStaticContexts(MemoryManager* m,
  473. size_t num_contexts,
  474. const uint32_t* static_context_map,
  475. MetaBlockSplit* mb) {
  476. size_t i;
  477. BROTLI_DCHECK(mb->literal_context_map == 0);
  478. mb->literal_context_map_size =
  479. mb->literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS;
  480. mb->literal_context_map =
  481. BROTLI_ALLOC(m, uint32_t, mb->literal_context_map_size);
  482. if (BROTLI_IS_OOM(m)) return;
  483. for (i = 0; i < mb->literal_split.num_types; ++i) {
  484. uint32_t offset = (uint32_t)(i * num_contexts);
  485. size_t j;
  486. for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS); ++j) {
  487. mb->literal_context_map[(i << BROTLI_LITERAL_CONTEXT_BITS) + j] =
  488. offset + static_context_map[j];
  489. }
  490. }
  491. }
  492. static BROTLI_INLINE void BrotliBuildMetaBlockGreedyInternal(
  493. MemoryManager* m, const uint8_t* ringbuffer, size_t pos, size_t mask,
  494. uint8_t prev_byte, uint8_t prev_byte2, ContextLut literal_context_lut,
  495. const size_t num_contexts, const uint32_t* static_context_map,
  496. const Command* commands, size_t n_commands, MetaBlockSplit* mb) {
  497. union {
  498. BlockSplitterLiteral plain;
  499. ContextBlockSplitter ctx;
  500. } lit_blocks;
  501. BlockSplitterCommand cmd_blocks;
  502. BlockSplitterDistance dist_blocks;
  503. size_t num_literals = 0;
  504. size_t i;
  505. for (i = 0; i < n_commands; ++i) {
  506. num_literals += commands[i].insert_len_;
  507. }
  508. if (num_contexts == 1) {
  509. InitBlockSplitterLiteral(m, &lit_blocks.plain, 256, 512, 400.0,
  510. num_literals, &mb->literal_split, &mb->literal_histograms,
  511. &mb->literal_histograms_size);
  512. } else {
  513. InitContextBlockSplitter(m, &lit_blocks.ctx, 256, num_contexts, 512, 400.0,
  514. num_literals, &mb->literal_split, &mb->literal_histograms,
  515. &mb->literal_histograms_size);
  516. }
  517. if (BROTLI_IS_OOM(m)) return;
  518. InitBlockSplitterCommand(m, &cmd_blocks, BROTLI_NUM_COMMAND_SYMBOLS, 1024,
  519. 500.0, n_commands, &mb->command_split, &mb->command_histograms,
  520. &mb->command_histograms_size);
  521. if (BROTLI_IS_OOM(m)) return;
  522. InitBlockSplitterDistance(m, &dist_blocks, 64, 512, 100.0, n_commands,
  523. &mb->distance_split, &mb->distance_histograms,
  524. &mb->distance_histograms_size);
  525. if (BROTLI_IS_OOM(m)) return;
  526. for (i = 0; i < n_commands; ++i) {
  527. const Command cmd = commands[i];
  528. size_t j;
  529. BlockSplitterAddSymbolCommand(&cmd_blocks, cmd.cmd_prefix_);
  530. for (j = cmd.insert_len_; j != 0; --j) {
  531. uint8_t literal = ringbuffer[pos & mask];
  532. if (num_contexts == 1) {
  533. BlockSplitterAddSymbolLiteral(&lit_blocks.plain, literal);
  534. } else {
  535. size_t context =
  536. BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut);
  537. ContextBlockSplitterAddSymbol(&lit_blocks.ctx, m, literal,
  538. static_context_map[context]);
  539. if (BROTLI_IS_OOM(m)) return;
  540. }
  541. prev_byte2 = prev_byte;
  542. prev_byte = literal;
  543. ++pos;
  544. }
  545. pos += CommandCopyLen(&cmd);
  546. if (CommandCopyLen(&cmd)) {
  547. prev_byte2 = ringbuffer[(pos - 2) & mask];
  548. prev_byte = ringbuffer[(pos - 1) & mask];
  549. if (cmd.cmd_prefix_ >= 128) {
  550. BlockSplitterAddSymbolDistance(&dist_blocks, cmd.dist_prefix_ & 0x3FF);
  551. }
  552. }
  553. }
  554. if (num_contexts == 1) {
  555. BlockSplitterFinishBlockLiteral(
  556. &lit_blocks.plain, /* is_final = */ BROTLI_TRUE);
  557. } else {
  558. ContextBlockSplitterFinishBlock(
  559. &lit_blocks.ctx, m, /* is_final = */ BROTLI_TRUE);
  560. if (BROTLI_IS_OOM(m)) return;
  561. }
  562. BlockSplitterFinishBlockCommand(&cmd_blocks, /* is_final = */ BROTLI_TRUE);
  563. BlockSplitterFinishBlockDistance(&dist_blocks, /* is_final = */ BROTLI_TRUE);
  564. if (num_contexts > 1) {
  565. MapStaticContexts(m, num_contexts, static_context_map, mb);
  566. }
  567. }
  568. void BrotliBuildMetaBlockGreedy(MemoryManager* m,
  569. const uint8_t* ringbuffer,
  570. size_t pos,
  571. size_t mask,
  572. uint8_t prev_byte,
  573. uint8_t prev_byte2,
  574. ContextLut literal_context_lut,
  575. size_t num_contexts,
  576. const uint32_t* static_context_map,
  577. const Command* commands,
  578. size_t n_commands,
  579. MetaBlockSplit* mb) {
  580. if (num_contexts == 1) {
  581. BrotliBuildMetaBlockGreedyInternal(m, ringbuffer, pos, mask, prev_byte,
  582. prev_byte2, literal_context_lut, 1, NULL, commands, n_commands, mb);
  583. } else {
  584. BrotliBuildMetaBlockGreedyInternal(m, ringbuffer, pos, mask, prev_byte,
  585. prev_byte2, literal_context_lut, num_contexts, static_context_map,
  586. commands, n_commands, mb);
  587. }
  588. }
  589. void BrotliOptimizeHistograms(uint32_t num_distance_codes,
  590. MetaBlockSplit* mb) {
  591. uint8_t good_for_rle[BROTLI_NUM_COMMAND_SYMBOLS];
  592. size_t i;
  593. for (i = 0; i < mb->literal_histograms_size; ++i) {
  594. BrotliOptimizeHuffmanCountsForRle(256, mb->literal_histograms[i].data_,
  595. good_for_rle);
  596. }
  597. for (i = 0; i < mb->command_histograms_size; ++i) {
  598. BrotliOptimizeHuffmanCountsForRle(BROTLI_NUM_COMMAND_SYMBOLS,
  599. mb->command_histograms[i].data_,
  600. good_for_rle);
  601. }
  602. for (i = 0; i < mb->distance_histograms_size; ++i) {
  603. BrotliOptimizeHuffmanCountsForRle(num_distance_codes,
  604. mb->distance_histograms[i].data_,
  605. good_for_rle);
  606. }
  607. }
  608. #if defined(__cplusplus) || defined(c_plusplus)
  609. } /* extern "C" */
  610. #endif