brotli_bit_stream.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /* Copyright 2014 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. /* Brotli bit stream functions to support the low level format. There are no
  6. compression algorithms here, just the right ordering of bits to match the
  7. specs. */
  8. #include "brotli_bit_stream.h"
  9. #include <string.h> /* memcpy, memset */
  10. #include <brotli/types.h>
  11. #include "../common/constants.h"
  12. #include "../common/context.h"
  13. #include "../common/platform.h"
  14. #include "entropy_encode.h"
  15. #include "entropy_encode_static.h"
  16. #include "fast_log.h"
  17. #include "histogram.h"
  18. #include "memory.h"
  19. #include "write_bits.h"
  20. #if defined(__cplusplus) || defined(c_plusplus)
  21. extern "C" {
  22. #endif
  23. #define MAX_HUFFMAN_TREE_SIZE (2 * BROTLI_NUM_COMMAND_SYMBOLS + 1)
  24. /* The maximum size of Huffman dictionary for distances assuming that
  25. NPOSTFIX = 0 and NDIRECT = 0. */
  26. #define MAX_SIMPLE_DISTANCE_ALPHABET_SIZE \
  27. BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_LARGE_MAX_DISTANCE_BITS)
  28. /* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */
  29. static BROTLI_INLINE uint32_t BlockLengthPrefixCode(uint32_t len) {
  30. uint32_t code = (len >= 177) ? (len >= 753 ? 20 : 14) : (len >= 41 ? 7 : 0);
  31. while (code < (BROTLI_NUM_BLOCK_LEN_SYMBOLS - 1) &&
  32. len >= _kBrotliPrefixCodeRanges[code + 1].offset) ++code;
  33. return code;
  34. }
  35. static BROTLI_INLINE void GetBlockLengthPrefixCode(uint32_t len, size_t* code,
  36. uint32_t* n_extra, uint32_t* extra) {
  37. *code = BlockLengthPrefixCode(len);
  38. *n_extra = _kBrotliPrefixCodeRanges[*code].nbits;
  39. *extra = len - _kBrotliPrefixCodeRanges[*code].offset;
  40. }
  41. typedef struct BlockTypeCodeCalculator {
  42. size_t last_type;
  43. size_t second_last_type;
  44. } BlockTypeCodeCalculator;
  45. static void InitBlockTypeCodeCalculator(BlockTypeCodeCalculator* self) {
  46. self->last_type = 1;
  47. self->second_last_type = 0;
  48. }
  49. static BROTLI_INLINE size_t NextBlockTypeCode(
  50. BlockTypeCodeCalculator* calculator, uint8_t type) {
  51. size_t type_code = (type == calculator->last_type + 1) ? 1u :
  52. (type == calculator->second_last_type) ? 0u : type + 2u;
  53. calculator->second_last_type = calculator->last_type;
  54. calculator->last_type = type;
  55. return type_code;
  56. }
  57. /* |nibblesbits| represents the 2 bits to encode MNIBBLES (0-3)
  58. REQUIRES: length > 0
  59. REQUIRES: length <= (1 << 24) */
  60. static void BrotliEncodeMlen(size_t length, uint64_t* bits,
  61. size_t* numbits, uint64_t* nibblesbits) {
  62. size_t lg = (length == 1) ? 1 : Log2FloorNonZero((uint32_t)(length - 1)) + 1;
  63. size_t mnibbles = (lg < 16 ? 16 : (lg + 3)) / 4;
  64. BROTLI_DCHECK(length > 0);
  65. BROTLI_DCHECK(length <= (1 << 24));
  66. BROTLI_DCHECK(lg <= 24);
  67. *nibblesbits = mnibbles - 4;
  68. *numbits = mnibbles * 4;
  69. *bits = length - 1;
  70. }
  71. static BROTLI_INLINE void StoreCommandExtra(
  72. const Command* cmd, size_t* storage_ix, uint8_t* storage) {
  73. uint32_t copylen_code = CommandCopyLenCode(cmd);
  74. uint16_t inscode = GetInsertLengthCode(cmd->insert_len_);
  75. uint16_t copycode = GetCopyLengthCode(copylen_code);
  76. uint32_t insnumextra = GetInsertExtra(inscode);
  77. uint64_t insextraval = cmd->insert_len_ - GetInsertBase(inscode);
  78. uint64_t copyextraval = copylen_code - GetCopyBase(copycode);
  79. uint64_t bits = (copyextraval << insnumextra) | insextraval;
  80. BrotliWriteBits(
  81. insnumextra + GetCopyExtra(copycode), bits, storage_ix, storage);
  82. }
  83. /* Data structure that stores almost everything that is needed to encode each
  84. block switch command. */
  85. typedef struct BlockSplitCode {
  86. BlockTypeCodeCalculator type_code_calculator;
  87. uint8_t type_depths[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
  88. uint16_t type_bits[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
  89. uint8_t length_depths[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
  90. uint16_t length_bits[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
  91. } BlockSplitCode;
  92. /* Stores a number between 0 and 255. */
  93. static void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage) {
  94. if (n == 0) {
  95. BrotliWriteBits(1, 0, storage_ix, storage);
  96. } else {
  97. size_t nbits = Log2FloorNonZero(n);
  98. BrotliWriteBits(1, 1, storage_ix, storage);
  99. BrotliWriteBits(3, nbits, storage_ix, storage);
  100. BrotliWriteBits(nbits, n - ((size_t)1 << nbits), storage_ix, storage);
  101. }
  102. }
  103. /* Stores the compressed meta-block header.
  104. REQUIRES: length > 0
  105. REQUIRES: length <= (1 << 24) */
  106. static void StoreCompressedMetaBlockHeader(BROTLI_BOOL is_final_block,
  107. size_t length,
  108. size_t* storage_ix,
  109. uint8_t* storage) {
  110. uint64_t lenbits;
  111. size_t nlenbits;
  112. uint64_t nibblesbits;
  113. /* Write ISLAST bit. */
  114. BrotliWriteBits(1, (uint64_t)is_final_block, storage_ix, storage);
  115. /* Write ISEMPTY bit. */
  116. if (is_final_block) {
  117. BrotliWriteBits(1, 0, storage_ix, storage);
  118. }
  119. BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits);
  120. BrotliWriteBits(2, nibblesbits, storage_ix, storage);
  121. BrotliWriteBits(nlenbits, lenbits, storage_ix, storage);
  122. if (!is_final_block) {
  123. /* Write ISUNCOMPRESSED bit. */
  124. BrotliWriteBits(1, 0, storage_ix, storage);
  125. }
  126. }
  127. /* Stores the uncompressed meta-block header.
  128. REQUIRES: length > 0
  129. REQUIRES: length <= (1 << 24) */
  130. static void BrotliStoreUncompressedMetaBlockHeader(size_t length,
  131. size_t* storage_ix,
  132. uint8_t* storage) {
  133. uint64_t lenbits;
  134. size_t nlenbits;
  135. uint64_t nibblesbits;
  136. /* Write ISLAST bit.
  137. Uncompressed block cannot be the last one, so set to 0. */
  138. BrotliWriteBits(1, 0, storage_ix, storage);
  139. BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits);
  140. BrotliWriteBits(2, nibblesbits, storage_ix, storage);
  141. BrotliWriteBits(nlenbits, lenbits, storage_ix, storage);
  142. /* Write ISUNCOMPRESSED bit. */
  143. BrotliWriteBits(1, 1, storage_ix, storage);
  144. }
  145. static void BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(
  146. const int num_codes, const uint8_t* code_length_bitdepth,
  147. size_t* storage_ix, uint8_t* storage) {
  148. static const uint8_t kStorageOrder[BROTLI_CODE_LENGTH_CODES] = {
  149. 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15
  150. };
  151. /* The bit lengths of the Huffman code over the code length alphabet
  152. are compressed with the following static Huffman code:
  153. Symbol Code
  154. ------ ----
  155. 0 00
  156. 1 1110
  157. 2 110
  158. 3 01
  159. 4 10
  160. 5 1111 */
  161. static const uint8_t kHuffmanBitLengthHuffmanCodeSymbols[6] = {
  162. 0, 7, 3, 2, 1, 15
  163. };
  164. static const uint8_t kHuffmanBitLengthHuffmanCodeBitLengths[6] = {
  165. 2, 4, 3, 2, 2, 4
  166. };
  167. size_t skip_some = 0; /* skips none. */
  168. /* Throw away trailing zeros: */
  169. size_t codes_to_store = BROTLI_CODE_LENGTH_CODES;
  170. if (num_codes > 1) {
  171. for (; codes_to_store > 0; --codes_to_store) {
  172. if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
  173. break;
  174. }
  175. }
  176. }
  177. if (code_length_bitdepth[kStorageOrder[0]] == 0 &&
  178. code_length_bitdepth[kStorageOrder[1]] == 0) {
  179. skip_some = 2; /* skips two. */
  180. if (code_length_bitdepth[kStorageOrder[2]] == 0) {
  181. skip_some = 3; /* skips three. */
  182. }
  183. }
  184. BrotliWriteBits(2, skip_some, storage_ix, storage);
  185. {
  186. size_t i;
  187. for (i = skip_some; i < codes_to_store; ++i) {
  188. size_t l = code_length_bitdepth[kStorageOrder[i]];
  189. BrotliWriteBits(kHuffmanBitLengthHuffmanCodeBitLengths[l],
  190. kHuffmanBitLengthHuffmanCodeSymbols[l], storage_ix, storage);
  191. }
  192. }
  193. }
  194. static void BrotliStoreHuffmanTreeToBitMask(
  195. const size_t huffman_tree_size, const uint8_t* huffman_tree,
  196. const uint8_t* huffman_tree_extra_bits, const uint8_t* code_length_bitdepth,
  197. const uint16_t* code_length_bitdepth_symbols,
  198. size_t* BROTLI_RESTRICT storage_ix, uint8_t* BROTLI_RESTRICT storage) {
  199. size_t i;
  200. for (i = 0; i < huffman_tree_size; ++i) {
  201. size_t ix = huffman_tree[i];
  202. BrotliWriteBits(code_length_bitdepth[ix], code_length_bitdepth_symbols[ix],
  203. storage_ix, storage);
  204. /* Extra bits */
  205. switch (ix) {
  206. case BROTLI_REPEAT_PREVIOUS_CODE_LENGTH:
  207. BrotliWriteBits(2, huffman_tree_extra_bits[i], storage_ix, storage);
  208. break;
  209. case BROTLI_REPEAT_ZERO_CODE_LENGTH:
  210. BrotliWriteBits(3, huffman_tree_extra_bits[i], storage_ix, storage);
  211. break;
  212. }
  213. }
  214. }
  215. static void StoreSimpleHuffmanTree(const uint8_t* depths,
  216. size_t symbols[4],
  217. size_t num_symbols,
  218. size_t max_bits,
  219. size_t* storage_ix, uint8_t* storage) {
  220. /* value of 1 indicates a simple Huffman code */
  221. BrotliWriteBits(2, 1, storage_ix, storage);
  222. BrotliWriteBits(2, num_symbols - 1, storage_ix, storage); /* NSYM - 1 */
  223. {
  224. /* Sort */
  225. size_t i;
  226. for (i = 0; i < num_symbols; i++) {
  227. size_t j;
  228. for (j = i + 1; j < num_symbols; j++) {
  229. if (depths[symbols[j]] < depths[symbols[i]]) {
  230. BROTLI_SWAP(size_t, symbols, j, i);
  231. }
  232. }
  233. }
  234. }
  235. if (num_symbols == 2) {
  236. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  237. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  238. } else if (num_symbols == 3) {
  239. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  240. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  241. BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
  242. } else {
  243. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  244. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  245. BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
  246. BrotliWriteBits(max_bits, symbols[3], storage_ix, storage);
  247. /* tree-select */
  248. BrotliWriteBits(1, depths[symbols[0]] == 1 ? 1 : 0, storage_ix, storage);
  249. }
  250. }
  251. /* num = alphabet size
  252. depths = symbol depths */
  253. void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
  254. HuffmanTree* tree,
  255. size_t* storage_ix, uint8_t* storage) {
  256. /* Write the Huffman tree into the brotli-representation.
  257. The command alphabet is the largest, so this allocation will fit all
  258. alphabets. */
  259. /* TODO(eustas): fix me */
  260. uint8_t huffman_tree[BROTLI_NUM_COMMAND_SYMBOLS];
  261. uint8_t huffman_tree_extra_bits[BROTLI_NUM_COMMAND_SYMBOLS];
  262. size_t huffman_tree_size = 0;
  263. uint8_t code_length_bitdepth[BROTLI_CODE_LENGTH_CODES] = { 0 };
  264. uint16_t code_length_bitdepth_symbols[BROTLI_CODE_LENGTH_CODES];
  265. uint32_t huffman_tree_histogram[BROTLI_CODE_LENGTH_CODES] = { 0 };
  266. size_t i;
  267. int num_codes = 0;
  268. size_t code = 0;
  269. BROTLI_DCHECK(num <= BROTLI_NUM_COMMAND_SYMBOLS);
  270. BrotliWriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree,
  271. huffman_tree_extra_bits);
  272. /* Calculate the statistics of the Huffman tree in brotli-representation. */
  273. for (i = 0; i < huffman_tree_size; ++i) {
  274. ++huffman_tree_histogram[huffman_tree[i]];
  275. }
  276. for (i = 0; i < BROTLI_CODE_LENGTH_CODES; ++i) {
  277. if (huffman_tree_histogram[i]) {
  278. if (num_codes == 0) {
  279. code = i;
  280. num_codes = 1;
  281. } else if (num_codes == 1) {
  282. num_codes = 2;
  283. break;
  284. }
  285. }
  286. }
  287. /* Calculate another Huffman tree to use for compressing both the
  288. earlier Huffman tree with. */
  289. BrotliCreateHuffmanTree(huffman_tree_histogram, BROTLI_CODE_LENGTH_CODES,
  290. 5, tree, code_length_bitdepth);
  291. BrotliConvertBitDepthsToSymbols(code_length_bitdepth,
  292. BROTLI_CODE_LENGTH_CODES,
  293. code_length_bitdepth_symbols);
  294. /* Now, we have all the data, let's start storing it */
  295. BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth,
  296. storage_ix, storage);
  297. if (num_codes == 1) {
  298. code_length_bitdepth[code] = 0;
  299. }
  300. /* Store the real Huffman tree now. */
  301. BrotliStoreHuffmanTreeToBitMask(huffman_tree_size,
  302. huffman_tree,
  303. huffman_tree_extra_bits,
  304. code_length_bitdepth,
  305. code_length_bitdepth_symbols,
  306. storage_ix, storage);
  307. }
  308. /* Builds a Huffman tree from histogram[0:length] into depth[0:length] and
  309. bits[0:length] and stores the encoded tree to the bit stream. */
  310. static void BuildAndStoreHuffmanTree(const uint32_t* histogram,
  311. const size_t histogram_length,
  312. const size_t alphabet_size,
  313. HuffmanTree* tree,
  314. uint8_t* depth,
  315. uint16_t* bits,
  316. size_t* storage_ix,
  317. uint8_t* storage) {
  318. size_t count = 0;
  319. size_t s4[4] = { 0 };
  320. size_t i;
  321. size_t max_bits = 0;
  322. for (i = 0; i < histogram_length; i++) {
  323. if (histogram[i]) {
  324. if (count < 4) {
  325. s4[count] = i;
  326. } else if (count > 4) {
  327. break;
  328. }
  329. count++;
  330. }
  331. }
  332. {
  333. size_t max_bits_counter = alphabet_size - 1;
  334. while (max_bits_counter) {
  335. max_bits_counter >>= 1;
  336. ++max_bits;
  337. }
  338. }
  339. if (count <= 1) {
  340. BrotliWriteBits(4, 1, storage_ix, storage);
  341. BrotliWriteBits(max_bits, s4[0], storage_ix, storage);
  342. depth[s4[0]] = 0;
  343. bits[s4[0]] = 0;
  344. return;
  345. }
  346. memset(depth, 0, histogram_length * sizeof(depth[0]));
  347. BrotliCreateHuffmanTree(histogram, histogram_length, 15, tree, depth);
  348. BrotliConvertBitDepthsToSymbols(depth, histogram_length, bits);
  349. if (count <= 4) {
  350. StoreSimpleHuffmanTree(depth, s4, count, max_bits, storage_ix, storage);
  351. } else {
  352. BrotliStoreHuffmanTree(depth, histogram_length, tree, storage_ix, storage);
  353. }
  354. }
  355. static BROTLI_INLINE BROTLI_BOOL SortHuffmanTree(
  356. const HuffmanTree* v0, const HuffmanTree* v1) {
  357. return TO_BROTLI_BOOL(v0->total_count_ < v1->total_count_);
  358. }
  359. void BrotliBuildAndStoreHuffmanTreeFast(HuffmanTree* tree,
  360. const uint32_t* histogram,
  361. const size_t histogram_total,
  362. const size_t max_bits,
  363. uint8_t* depth, uint16_t* bits,
  364. size_t* storage_ix,
  365. uint8_t* storage) {
  366. size_t count = 0;
  367. size_t symbols[4] = { 0 };
  368. size_t length = 0;
  369. size_t total = histogram_total;
  370. while (total != 0) {
  371. if (histogram[length]) {
  372. if (count < 4) {
  373. symbols[count] = length;
  374. }
  375. ++count;
  376. total -= histogram[length];
  377. }
  378. ++length;
  379. }
  380. if (count <= 1) {
  381. BrotliWriteBits(4, 1, storage_ix, storage);
  382. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  383. depth[symbols[0]] = 0;
  384. bits[symbols[0]] = 0;
  385. return;
  386. }
  387. memset(depth, 0, length * sizeof(depth[0]));
  388. {
  389. uint32_t count_limit;
  390. for (count_limit = 1; ; count_limit *= 2) {
  391. HuffmanTree* node = tree;
  392. size_t l;
  393. for (l = length; l != 0;) {
  394. --l;
  395. if (histogram[l]) {
  396. if (BROTLI_PREDICT_TRUE(histogram[l] >= count_limit)) {
  397. InitHuffmanTree(node, histogram[l], -1, (int16_t)l);
  398. } else {
  399. InitHuffmanTree(node, count_limit, -1, (int16_t)l);
  400. }
  401. ++node;
  402. }
  403. }
  404. {
  405. const int n = (int)(node - tree);
  406. HuffmanTree sentinel;
  407. int i = 0; /* Points to the next leaf node. */
  408. int j = n + 1; /* Points to the next non-leaf node. */
  409. int k;
  410. SortHuffmanTreeItems(tree, (size_t)n, SortHuffmanTree);
  411. /* The nodes are:
  412. [0, n): the sorted leaf nodes that we start with.
  413. [n]: we add a sentinel here.
  414. [n + 1, 2n): new parent nodes are added here, starting from
  415. (n+1). These are naturally in ascending order.
  416. [2n]: we add a sentinel at the end as well.
  417. There will be (2n+1) elements at the end. */
  418. InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1);
  419. *node++ = sentinel;
  420. *node++ = sentinel;
  421. for (k = n - 1; k > 0; --k) {
  422. int left, right;
  423. if (tree[i].total_count_ <= tree[j].total_count_) {
  424. left = i;
  425. ++i;
  426. } else {
  427. left = j;
  428. ++j;
  429. }
  430. if (tree[i].total_count_ <= tree[j].total_count_) {
  431. right = i;
  432. ++i;
  433. } else {
  434. right = j;
  435. ++j;
  436. }
  437. /* The sentinel node becomes the parent node. */
  438. node[-1].total_count_ =
  439. tree[left].total_count_ + tree[right].total_count_;
  440. node[-1].index_left_ = (int16_t)left;
  441. node[-1].index_right_or_value_ = (int16_t)right;
  442. /* Add back the last sentinel node. */
  443. *node++ = sentinel;
  444. }
  445. if (BrotliSetDepth(2 * n - 1, tree, depth, 14)) {
  446. /* We need to pack the Huffman tree in 14 bits. If this was not
  447. successful, add fake entities to the lowest values and retry. */
  448. break;
  449. }
  450. }
  451. }
  452. }
  453. BrotliConvertBitDepthsToSymbols(depth, length, bits);
  454. if (count <= 4) {
  455. size_t i;
  456. /* value of 1 indicates a simple Huffman code */
  457. BrotliWriteBits(2, 1, storage_ix, storage);
  458. BrotliWriteBits(2, count - 1, storage_ix, storage); /* NSYM - 1 */
  459. /* Sort */
  460. for (i = 0; i < count; i++) {
  461. size_t j;
  462. for (j = i + 1; j < count; j++) {
  463. if (depth[symbols[j]] < depth[symbols[i]]) {
  464. BROTLI_SWAP(size_t, symbols, j, i);
  465. }
  466. }
  467. }
  468. if (count == 2) {
  469. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  470. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  471. } else if (count == 3) {
  472. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  473. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  474. BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
  475. } else {
  476. BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
  477. BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
  478. BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
  479. BrotliWriteBits(max_bits, symbols[3], storage_ix, storage);
  480. /* tree-select */
  481. BrotliWriteBits(1, depth[symbols[0]] == 1 ? 1 : 0, storage_ix, storage);
  482. }
  483. } else {
  484. uint8_t previous_value = 8;
  485. size_t i;
  486. /* Complex Huffman Tree */
  487. StoreStaticCodeLengthCode(storage_ix, storage);
  488. /* Actual RLE coding. */
  489. for (i = 0; i < length;) {
  490. const uint8_t value = depth[i];
  491. size_t reps = 1;
  492. size_t k;
  493. for (k = i + 1; k < length && depth[k] == value; ++k) {
  494. ++reps;
  495. }
  496. i += reps;
  497. if (value == 0) {
  498. BrotliWriteBits(kZeroRepsDepth[reps], kZeroRepsBits[reps],
  499. storage_ix, storage);
  500. } else {
  501. if (previous_value != value) {
  502. BrotliWriteBits(kCodeLengthDepth[value], kCodeLengthBits[value],
  503. storage_ix, storage);
  504. --reps;
  505. }
  506. if (reps < 3) {
  507. while (reps != 0) {
  508. reps--;
  509. BrotliWriteBits(kCodeLengthDepth[value], kCodeLengthBits[value],
  510. storage_ix, storage);
  511. }
  512. } else {
  513. reps -= 3;
  514. BrotliWriteBits(kNonZeroRepsDepth[reps], kNonZeroRepsBits[reps],
  515. storage_ix, storage);
  516. }
  517. previous_value = value;
  518. }
  519. }
  520. }
  521. }
  522. static size_t IndexOf(const uint8_t* v, size_t v_size, uint8_t value) {
  523. size_t i = 0;
  524. for (; i < v_size; ++i) {
  525. if (v[i] == value) return i;
  526. }
  527. return i;
  528. }
  529. static void MoveToFront(uint8_t* v, size_t index) {
  530. uint8_t value = v[index];
  531. size_t i;
  532. for (i = index; i != 0; --i) {
  533. v[i] = v[i - 1];
  534. }
  535. v[0] = value;
  536. }
  537. static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in,
  538. const size_t v_size,
  539. uint32_t* v_out) {
  540. size_t i;
  541. uint8_t mtf[256];
  542. uint32_t max_value;
  543. if (v_size == 0) {
  544. return;
  545. }
  546. max_value = v_in[0];
  547. for (i = 1; i < v_size; ++i) {
  548. if (v_in[i] > max_value) max_value = v_in[i];
  549. }
  550. BROTLI_DCHECK(max_value < 256u);
  551. for (i = 0; i <= max_value; ++i) {
  552. mtf[i] = (uint8_t)i;
  553. }
  554. {
  555. size_t mtf_size = max_value + 1;
  556. for (i = 0; i < v_size; ++i) {
  557. size_t index = IndexOf(mtf, mtf_size, (uint8_t)v_in[i]);
  558. BROTLI_DCHECK(index < mtf_size);
  559. v_out[i] = (uint32_t)index;
  560. MoveToFront(mtf, index);
  561. }
  562. }
  563. }
  564. /* Finds runs of zeros in v[0..in_size) and replaces them with a prefix code of
  565. the run length plus extra bits (lower 9 bits is the prefix code and the rest
  566. are the extra bits). Non-zero values in v[] are shifted by
  567. *max_length_prefix. Will not create prefix codes bigger than the initial
  568. value of *max_run_length_prefix. The prefix code of run length L is simply
  569. Log2Floor(L) and the number of extra bits is the same as the prefix code. */
  570. static void RunLengthCodeZeros(const size_t in_size,
  571. uint32_t* BROTLI_RESTRICT v, size_t* BROTLI_RESTRICT out_size,
  572. uint32_t* BROTLI_RESTRICT max_run_length_prefix) {
  573. uint32_t max_reps = 0;
  574. size_t i;
  575. uint32_t max_prefix;
  576. for (i = 0; i < in_size;) {
  577. uint32_t reps = 0;
  578. for (; i < in_size && v[i] != 0; ++i) ;
  579. for (; i < in_size && v[i] == 0; ++i) {
  580. ++reps;
  581. }
  582. max_reps = BROTLI_MAX(uint32_t, reps, max_reps);
  583. }
  584. max_prefix = max_reps > 0 ? Log2FloorNonZero(max_reps) : 0;
  585. max_prefix = BROTLI_MIN(uint32_t, max_prefix, *max_run_length_prefix);
  586. *max_run_length_prefix = max_prefix;
  587. *out_size = 0;
  588. for (i = 0; i < in_size;) {
  589. BROTLI_DCHECK(*out_size <= i);
  590. if (v[i] != 0) {
  591. v[*out_size] = v[i] + *max_run_length_prefix;
  592. ++i;
  593. ++(*out_size);
  594. } else {
  595. uint32_t reps = 1;
  596. size_t k;
  597. for (k = i + 1; k < in_size && v[k] == 0; ++k) {
  598. ++reps;
  599. }
  600. i += reps;
  601. while (reps != 0) {
  602. if (reps < (2u << max_prefix)) {
  603. uint32_t run_length_prefix = Log2FloorNonZero(reps);
  604. const uint32_t extra_bits = reps - (1u << run_length_prefix);
  605. v[*out_size] = run_length_prefix + (extra_bits << 9);
  606. ++(*out_size);
  607. break;
  608. } else {
  609. const uint32_t extra_bits = (1u << max_prefix) - 1u;
  610. v[*out_size] = max_prefix + (extra_bits << 9);
  611. reps -= (2u << max_prefix) - 1u;
  612. ++(*out_size);
  613. }
  614. }
  615. }
  616. }
  617. }
  618. #define SYMBOL_BITS 9
  619. typedef struct EncodeContextMapArena {
  620. uint32_t histogram[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
  621. uint8_t depths[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
  622. uint16_t bits[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
  623. } EncodeContextMapArena;
  624. static void EncodeContextMap(MemoryManager* m,
  625. EncodeContextMapArena* arena,
  626. const uint32_t* context_map,
  627. size_t context_map_size,
  628. size_t num_clusters,
  629. HuffmanTree* tree,
  630. size_t* storage_ix, uint8_t* storage) {
  631. size_t i;
  632. uint32_t* rle_symbols;
  633. uint32_t max_run_length_prefix = 6;
  634. size_t num_rle_symbols = 0;
  635. uint32_t* BROTLI_RESTRICT const histogram = arena->histogram;
  636. static const uint32_t kSymbolMask = (1u << SYMBOL_BITS) - 1u;
  637. uint8_t* BROTLI_RESTRICT const depths = arena->depths;
  638. uint16_t* BROTLI_RESTRICT const bits = arena->bits;
  639. StoreVarLenUint8(num_clusters - 1, storage_ix, storage);
  640. if (num_clusters == 1) {
  641. return;
  642. }
  643. rle_symbols = BROTLI_ALLOC(m, uint32_t, context_map_size);
  644. if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(rle_symbols)) return;
  645. MoveToFrontTransform(context_map, context_map_size, rle_symbols);
  646. RunLengthCodeZeros(context_map_size, rle_symbols,
  647. &num_rle_symbols, &max_run_length_prefix);
  648. memset(histogram, 0, sizeof(arena->histogram));
  649. for (i = 0; i < num_rle_symbols; ++i) {
  650. ++histogram[rle_symbols[i] & kSymbolMask];
  651. }
  652. {
  653. BROTLI_BOOL use_rle = TO_BROTLI_BOOL(max_run_length_prefix > 0);
  654. BrotliWriteBits(1, (uint64_t)use_rle, storage_ix, storage);
  655. if (use_rle) {
  656. BrotliWriteBits(4, max_run_length_prefix - 1, storage_ix, storage);
  657. }
  658. }
  659. BuildAndStoreHuffmanTree(histogram, num_clusters + max_run_length_prefix,
  660. num_clusters + max_run_length_prefix,
  661. tree, depths, bits, storage_ix, storage);
  662. for (i = 0; i < num_rle_symbols; ++i) {
  663. const uint32_t rle_symbol = rle_symbols[i] & kSymbolMask;
  664. const uint32_t extra_bits_val = rle_symbols[i] >> SYMBOL_BITS;
  665. BrotliWriteBits(depths[rle_symbol], bits[rle_symbol], storage_ix, storage);
  666. if (rle_symbol > 0 && rle_symbol <= max_run_length_prefix) {
  667. BrotliWriteBits(rle_symbol, extra_bits_val, storage_ix, storage);
  668. }
  669. }
  670. BrotliWriteBits(1, 1, storage_ix, storage); /* use move-to-front */
  671. BROTLI_FREE(m, rle_symbols);
  672. }
  673. /* Stores the block switch command with index block_ix to the bit stream. */
  674. static BROTLI_INLINE void StoreBlockSwitch(BlockSplitCode* code,
  675. const uint32_t block_len,
  676. const uint8_t block_type,
  677. BROTLI_BOOL is_first_block,
  678. size_t* storage_ix,
  679. uint8_t* storage) {
  680. size_t typecode = NextBlockTypeCode(&code->type_code_calculator, block_type);
  681. size_t lencode;
  682. uint32_t len_nextra;
  683. uint32_t len_extra;
  684. if (!is_first_block) {
  685. BrotliWriteBits(code->type_depths[typecode], code->type_bits[typecode],
  686. storage_ix, storage);
  687. }
  688. GetBlockLengthPrefixCode(block_len, &lencode, &len_nextra, &len_extra);
  689. BrotliWriteBits(code->length_depths[lencode], code->length_bits[lencode],
  690. storage_ix, storage);
  691. BrotliWriteBits(len_nextra, len_extra, storage_ix, storage);
  692. }
  693. /* Builds a BlockSplitCode data structure from the block split given by the
  694. vector of block types and block lengths and stores it to the bit stream. */
  695. static void BuildAndStoreBlockSplitCode(const uint8_t* types,
  696. const uint32_t* lengths,
  697. const size_t num_blocks,
  698. const size_t num_types,
  699. HuffmanTree* tree,
  700. BlockSplitCode* code,
  701. size_t* storage_ix,
  702. uint8_t* storage) {
  703. uint32_t type_histo[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
  704. uint32_t length_histo[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
  705. size_t i;
  706. BlockTypeCodeCalculator type_code_calculator;
  707. memset(type_histo, 0, (num_types + 2) * sizeof(type_histo[0]));
  708. memset(length_histo, 0, sizeof(length_histo));
  709. InitBlockTypeCodeCalculator(&type_code_calculator);
  710. for (i = 0; i < num_blocks; ++i) {
  711. size_t type_code = NextBlockTypeCode(&type_code_calculator, types[i]);
  712. if (i != 0) ++type_histo[type_code];
  713. ++length_histo[BlockLengthPrefixCode(lengths[i])];
  714. }
  715. StoreVarLenUint8(num_types - 1, storage_ix, storage);
  716. if (num_types > 1) { /* TODO(eustas): else? could StoreBlockSwitch occur? */
  717. BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, num_types + 2, tree,
  718. &code->type_depths[0], &code->type_bits[0],
  719. storage_ix, storage);
  720. BuildAndStoreHuffmanTree(&length_histo[0], BROTLI_NUM_BLOCK_LEN_SYMBOLS,
  721. BROTLI_NUM_BLOCK_LEN_SYMBOLS,
  722. tree, &code->length_depths[0],
  723. &code->length_bits[0], storage_ix, storage);
  724. StoreBlockSwitch(code, lengths[0], types[0], 1, storage_ix, storage);
  725. }
  726. }
  727. /* Stores a context map where the histogram type is always the block type. */
  728. static void StoreTrivialContextMap(EncodeContextMapArena* arena,
  729. size_t num_types,
  730. size_t context_bits,
  731. HuffmanTree* tree,
  732. size_t* storage_ix,
  733. uint8_t* storage) {
  734. StoreVarLenUint8(num_types - 1, storage_ix, storage);
  735. if (num_types > 1) {
  736. size_t repeat_code = context_bits - 1u;
  737. size_t repeat_bits = (1u << repeat_code) - 1u;
  738. size_t alphabet_size = num_types + repeat_code;
  739. uint32_t* BROTLI_RESTRICT const histogram = arena->histogram;
  740. uint8_t* BROTLI_RESTRICT const depths = arena->depths;
  741. uint16_t* BROTLI_RESTRICT const bits = arena->bits;
  742. size_t i;
  743. memset(histogram, 0, alphabet_size * sizeof(histogram[0]));
  744. /* Write RLEMAX. */
  745. BrotliWriteBits(1, 1, storage_ix, storage);
  746. BrotliWriteBits(4, repeat_code - 1, storage_ix, storage);
  747. histogram[repeat_code] = (uint32_t)num_types;
  748. histogram[0] = 1;
  749. for (i = context_bits; i < alphabet_size; ++i) {
  750. histogram[i] = 1;
  751. }
  752. BuildAndStoreHuffmanTree(histogram, alphabet_size, alphabet_size,
  753. tree, depths, bits, storage_ix, storage);
  754. for (i = 0; i < num_types; ++i) {
  755. size_t code = (i == 0 ? 0 : i + context_bits - 1);
  756. BrotliWriteBits(depths[code], bits[code], storage_ix, storage);
  757. BrotliWriteBits(
  758. depths[repeat_code], bits[repeat_code], storage_ix, storage);
  759. BrotliWriteBits(repeat_code, repeat_bits, storage_ix, storage);
  760. }
  761. /* Write IMTF (inverse-move-to-front) bit. */
  762. BrotliWriteBits(1, 1, storage_ix, storage);
  763. }
  764. }
  765. /* Manages the encoding of one block category (literal, command or distance). */
  766. typedef struct BlockEncoder {
  767. size_t histogram_length_;
  768. size_t num_block_types_;
  769. const uint8_t* block_types_; /* Not owned. */
  770. const uint32_t* block_lengths_; /* Not owned. */
  771. size_t num_blocks_;
  772. BlockSplitCode block_split_code_;
  773. size_t block_ix_;
  774. size_t block_len_;
  775. size_t entropy_ix_;
  776. uint8_t* depths_;
  777. uint16_t* bits_;
  778. } BlockEncoder;
  779. static void InitBlockEncoder(BlockEncoder* self, size_t histogram_length,
  780. size_t num_block_types, const uint8_t* block_types,
  781. const uint32_t* block_lengths, const size_t num_blocks) {
  782. self->histogram_length_ = histogram_length;
  783. self->num_block_types_ = num_block_types;
  784. self->block_types_ = block_types;
  785. self->block_lengths_ = block_lengths;
  786. self->num_blocks_ = num_blocks;
  787. InitBlockTypeCodeCalculator(&self->block_split_code_.type_code_calculator);
  788. self->block_ix_ = 0;
  789. self->block_len_ = num_blocks == 0 ? 0 : block_lengths[0];
  790. self->entropy_ix_ = 0;
  791. self->depths_ = 0;
  792. self->bits_ = 0;
  793. }
  794. static void CleanupBlockEncoder(MemoryManager* m, BlockEncoder* self) {
  795. BROTLI_FREE(m, self->depths_);
  796. BROTLI_FREE(m, self->bits_);
  797. }
  798. /* Creates entropy codes of block lengths and block types and stores them
  799. to the bit stream. */
  800. static void BuildAndStoreBlockSwitchEntropyCodes(BlockEncoder* self,
  801. HuffmanTree* tree, size_t* storage_ix, uint8_t* storage) {
  802. BuildAndStoreBlockSplitCode(self->block_types_, self->block_lengths_,
  803. self->num_blocks_, self->num_block_types_, tree, &self->block_split_code_,
  804. storage_ix, storage);
  805. }
  806. /* Stores the next symbol with the entropy code of the current block type.
  807. Updates the block type and block length at block boundaries. */
  808. static void StoreSymbol(BlockEncoder* self, size_t symbol, size_t* storage_ix,
  809. uint8_t* storage) {
  810. if (self->block_len_ == 0) {
  811. size_t block_ix = ++self->block_ix_;
  812. uint32_t block_len = self->block_lengths_[block_ix];
  813. uint8_t block_type = self->block_types_[block_ix];
  814. self->block_len_ = block_len;
  815. self->entropy_ix_ = block_type * self->histogram_length_;
  816. StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0,
  817. storage_ix, storage);
  818. }
  819. --self->block_len_;
  820. {
  821. size_t ix = self->entropy_ix_ + symbol;
  822. BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage);
  823. }
  824. }
  825. /* Stores the next symbol with the entropy code of the current block type and
  826. context value.
  827. Updates the block type and block length at block boundaries. */
  828. static void StoreSymbolWithContext(BlockEncoder* self, size_t symbol,
  829. size_t context, const uint32_t* context_map, size_t* storage_ix,
  830. uint8_t* storage, const size_t context_bits) {
  831. if (self->block_len_ == 0) {
  832. size_t block_ix = ++self->block_ix_;
  833. uint32_t block_len = self->block_lengths_[block_ix];
  834. uint8_t block_type = self->block_types_[block_ix];
  835. self->block_len_ = block_len;
  836. self->entropy_ix_ = (size_t)block_type << context_bits;
  837. StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0,
  838. storage_ix, storage);
  839. }
  840. --self->block_len_;
  841. {
  842. size_t histo_ix = context_map[self->entropy_ix_ + context];
  843. size_t ix = histo_ix * self->histogram_length_ + symbol;
  844. BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage);
  845. }
  846. }
  847. #define FN(X) X ## Literal
  848. /* NOLINTNEXTLINE(build/include) */
  849. #include "block_encoder_inc.h"
  850. #undef FN
  851. #define FN(X) X ## Command
  852. /* NOLINTNEXTLINE(build/include) */
  853. #include "block_encoder_inc.h"
  854. #undef FN
  855. #define FN(X) X ## Distance
  856. /* NOLINTNEXTLINE(build/include) */
  857. #include "block_encoder_inc.h"
  858. #undef FN
  859. static void JumpToByteBoundary(size_t* storage_ix, uint8_t* storage) {
  860. *storage_ix = (*storage_ix + 7u) & ~7u;
  861. storage[*storage_ix >> 3] = 0;
  862. }
  863. typedef struct StoreMetablockArena {
  864. BlockEncoder literal_enc;
  865. BlockEncoder command_enc;
  866. BlockEncoder distance_enc;
  867. EncodeContextMapArena context_map_arena;
  868. } StoreMetablockArena;
  869. void BrotliStoreMetaBlock(MemoryManager* m,
  870. const uint8_t* input, size_t start_pos, size_t length, size_t mask,
  871. uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last,
  872. const BrotliEncoderParams* params, ContextType literal_context_mode,
  873. const Command* commands, size_t n_commands, const MetaBlockSplit* mb,
  874. size_t* storage_ix, uint8_t* storage) {
  875. size_t pos = start_pos;
  876. size_t i;
  877. uint32_t num_distance_symbols = params->dist.alphabet_size_max;
  878. uint32_t num_effective_distance_symbols = params->dist.alphabet_size_limit;
  879. HuffmanTree* tree;
  880. ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode);
  881. StoreMetablockArena* arena = NULL;
  882. BlockEncoder* literal_enc = NULL;
  883. BlockEncoder* command_enc = NULL;
  884. BlockEncoder* distance_enc = NULL;
  885. const BrotliDistanceParams* dist = &params->dist;
  886. BROTLI_DCHECK(
  887. num_effective_distance_symbols <= BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS);
  888. StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
  889. tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE);
  890. arena = BROTLI_ALLOC(m, StoreMetablockArena, 1);
  891. if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(tree) || BROTLI_IS_NULL(arena)) return;
  892. literal_enc = &arena->literal_enc;
  893. command_enc = &arena->command_enc;
  894. distance_enc = &arena->distance_enc;
  895. InitBlockEncoder(literal_enc, BROTLI_NUM_LITERAL_SYMBOLS,
  896. mb->literal_split.num_types, mb->literal_split.types,
  897. mb->literal_split.lengths, mb->literal_split.num_blocks);
  898. InitBlockEncoder(command_enc, BROTLI_NUM_COMMAND_SYMBOLS,
  899. mb->command_split.num_types, mb->command_split.types,
  900. mb->command_split.lengths, mb->command_split.num_blocks);
  901. InitBlockEncoder(distance_enc, num_effective_distance_symbols,
  902. mb->distance_split.num_types, mb->distance_split.types,
  903. mb->distance_split.lengths, mb->distance_split.num_blocks);
  904. BuildAndStoreBlockSwitchEntropyCodes(literal_enc, tree, storage_ix, storage);
  905. BuildAndStoreBlockSwitchEntropyCodes(command_enc, tree, storage_ix, storage);
  906. BuildAndStoreBlockSwitchEntropyCodes(distance_enc, tree, storage_ix, storage);
  907. BrotliWriteBits(2, dist->distance_postfix_bits, storage_ix, storage);
  908. BrotliWriteBits(
  909. 4, dist->num_direct_distance_codes >> dist->distance_postfix_bits,
  910. storage_ix, storage);
  911. for (i = 0; i < mb->literal_split.num_types; ++i) {
  912. BrotliWriteBits(2, literal_context_mode, storage_ix, storage);
  913. }
  914. if (mb->literal_context_map_size == 0) {
  915. StoreTrivialContextMap(
  916. &arena->context_map_arena, mb->literal_histograms_size,
  917. BROTLI_LITERAL_CONTEXT_BITS, tree, storage_ix, storage);
  918. } else {
  919. EncodeContextMap(m, &arena->context_map_arena,
  920. mb->literal_context_map, mb->literal_context_map_size,
  921. mb->literal_histograms_size, tree, storage_ix, storage);
  922. if (BROTLI_IS_OOM(m)) return;
  923. }
  924. if (mb->distance_context_map_size == 0) {
  925. StoreTrivialContextMap(
  926. &arena->context_map_arena, mb->distance_histograms_size,
  927. BROTLI_DISTANCE_CONTEXT_BITS, tree, storage_ix, storage);
  928. } else {
  929. EncodeContextMap(m, &arena->context_map_arena,
  930. mb->distance_context_map, mb->distance_context_map_size,
  931. mb->distance_histograms_size, tree, storage_ix, storage);
  932. if (BROTLI_IS_OOM(m)) return;
  933. }
  934. BuildAndStoreEntropyCodesLiteral(m, literal_enc, mb->literal_histograms,
  935. mb->literal_histograms_size, BROTLI_NUM_LITERAL_SYMBOLS, tree,
  936. storage_ix, storage);
  937. if (BROTLI_IS_OOM(m)) return;
  938. BuildAndStoreEntropyCodesCommand(m, command_enc, mb->command_histograms,
  939. mb->command_histograms_size, BROTLI_NUM_COMMAND_SYMBOLS, tree,
  940. storage_ix, storage);
  941. if (BROTLI_IS_OOM(m)) return;
  942. BuildAndStoreEntropyCodesDistance(m, distance_enc, mb->distance_histograms,
  943. mb->distance_histograms_size, num_distance_symbols, tree,
  944. storage_ix, storage);
  945. if (BROTLI_IS_OOM(m)) return;
  946. BROTLI_FREE(m, tree);
  947. for (i = 0; i < n_commands; ++i) {
  948. const Command cmd = commands[i];
  949. size_t cmd_code = cmd.cmd_prefix_;
  950. StoreSymbol(command_enc, cmd_code, storage_ix, storage);
  951. StoreCommandExtra(&cmd, storage_ix, storage);
  952. if (mb->literal_context_map_size == 0) {
  953. size_t j;
  954. for (j = cmd.insert_len_; j != 0; --j) {
  955. StoreSymbol(literal_enc, input[pos & mask], storage_ix, storage);
  956. ++pos;
  957. }
  958. } else {
  959. size_t j;
  960. for (j = cmd.insert_len_; j != 0; --j) {
  961. size_t context =
  962. BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut);
  963. uint8_t literal = input[pos & mask];
  964. StoreSymbolWithContext(literal_enc, literal, context,
  965. mb->literal_context_map, storage_ix, storage,
  966. BROTLI_LITERAL_CONTEXT_BITS);
  967. prev_byte2 = prev_byte;
  968. prev_byte = literal;
  969. ++pos;
  970. }
  971. }
  972. pos += CommandCopyLen(&cmd);
  973. if (CommandCopyLen(&cmd)) {
  974. prev_byte2 = input[(pos - 2) & mask];
  975. prev_byte = input[(pos - 1) & mask];
  976. if (cmd.cmd_prefix_ >= 128) {
  977. size_t dist_code = cmd.dist_prefix_ & 0x3FF;
  978. uint32_t distnumextra = cmd.dist_prefix_ >> 10;
  979. uint64_t distextra = cmd.dist_extra_;
  980. if (mb->distance_context_map_size == 0) {
  981. StoreSymbol(distance_enc, dist_code, storage_ix, storage);
  982. } else {
  983. size_t context = CommandDistanceContext(&cmd);
  984. StoreSymbolWithContext(distance_enc, dist_code, context,
  985. mb->distance_context_map, storage_ix, storage,
  986. BROTLI_DISTANCE_CONTEXT_BITS);
  987. }
  988. BrotliWriteBits(distnumextra, distextra, storage_ix, storage);
  989. }
  990. }
  991. }
  992. CleanupBlockEncoder(m, distance_enc);
  993. CleanupBlockEncoder(m, command_enc);
  994. CleanupBlockEncoder(m, literal_enc);
  995. BROTLI_FREE(m, arena);
  996. if (is_last) {
  997. JumpToByteBoundary(storage_ix, storage);
  998. }
  999. }
  1000. static void BuildHistograms(const uint8_t* input,
  1001. size_t start_pos,
  1002. size_t mask,
  1003. const Command* commands,
  1004. size_t n_commands,
  1005. HistogramLiteral* lit_histo,
  1006. HistogramCommand* cmd_histo,
  1007. HistogramDistance* dist_histo) {
  1008. size_t pos = start_pos;
  1009. size_t i;
  1010. for (i = 0; i < n_commands; ++i) {
  1011. const Command cmd = commands[i];
  1012. size_t j;
  1013. HistogramAddCommand(cmd_histo, cmd.cmd_prefix_);
  1014. for (j = cmd.insert_len_; j != 0; --j) {
  1015. HistogramAddLiteral(lit_histo, input[pos & mask]);
  1016. ++pos;
  1017. }
  1018. pos += CommandCopyLen(&cmd);
  1019. if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
  1020. HistogramAddDistance(dist_histo, cmd.dist_prefix_ & 0x3FF);
  1021. }
  1022. }
  1023. }
  1024. static void StoreDataWithHuffmanCodes(const uint8_t* input,
  1025. size_t start_pos,
  1026. size_t mask,
  1027. const Command* commands,
  1028. size_t n_commands,
  1029. const uint8_t* lit_depth,
  1030. const uint16_t* lit_bits,
  1031. const uint8_t* cmd_depth,
  1032. const uint16_t* cmd_bits,
  1033. const uint8_t* dist_depth,
  1034. const uint16_t* dist_bits,
  1035. size_t* storage_ix,
  1036. uint8_t* storage) {
  1037. size_t pos = start_pos;
  1038. size_t i;
  1039. for (i = 0; i < n_commands; ++i) {
  1040. const Command cmd = commands[i];
  1041. const size_t cmd_code = cmd.cmd_prefix_;
  1042. size_t j;
  1043. BrotliWriteBits(
  1044. cmd_depth[cmd_code], cmd_bits[cmd_code], storage_ix, storage);
  1045. StoreCommandExtra(&cmd, storage_ix, storage);
  1046. for (j = cmd.insert_len_; j != 0; --j) {
  1047. const uint8_t literal = input[pos & mask];
  1048. BrotliWriteBits(
  1049. lit_depth[literal], lit_bits[literal], storage_ix, storage);
  1050. ++pos;
  1051. }
  1052. pos += CommandCopyLen(&cmd);
  1053. if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
  1054. const size_t dist_code = cmd.dist_prefix_ & 0x3FF;
  1055. const uint32_t distnumextra = cmd.dist_prefix_ >> 10;
  1056. const uint32_t distextra = cmd.dist_extra_;
  1057. BrotliWriteBits(dist_depth[dist_code], dist_bits[dist_code],
  1058. storage_ix, storage);
  1059. BrotliWriteBits(distnumextra, distextra, storage_ix, storage);
  1060. }
  1061. }
  1062. }
  1063. /* TODO(eustas): pull alloc/dealloc to caller? */
  1064. typedef struct MetablockArena {
  1065. HistogramLiteral lit_histo;
  1066. HistogramCommand cmd_histo;
  1067. HistogramDistance dist_histo;
  1068. /* TODO(eustas): merge bits and depth? */
  1069. uint8_t lit_depth[BROTLI_NUM_LITERAL_SYMBOLS];
  1070. uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
  1071. uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS];
  1072. uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS];
  1073. uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
  1074. uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
  1075. HuffmanTree tree[MAX_HUFFMAN_TREE_SIZE];
  1076. } MetablockArena;
  1077. void BrotliStoreMetaBlockTrivial(MemoryManager* m,
  1078. const uint8_t* input, size_t start_pos, size_t length, size_t mask,
  1079. BROTLI_BOOL is_last, const BrotliEncoderParams* params,
  1080. const Command* commands, size_t n_commands,
  1081. size_t* storage_ix, uint8_t* storage) {
  1082. MetablockArena* arena = BROTLI_ALLOC(m, MetablockArena, 1);
  1083. uint32_t num_distance_symbols = params->dist.alphabet_size_max;
  1084. if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(arena)) return;
  1085. StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
  1086. HistogramClearLiteral(&arena->lit_histo);
  1087. HistogramClearCommand(&arena->cmd_histo);
  1088. HistogramClearDistance(&arena->dist_histo);
  1089. BuildHistograms(input, start_pos, mask, commands, n_commands,
  1090. &arena->lit_histo, &arena->cmd_histo, &arena->dist_histo);
  1091. BrotliWriteBits(13, 0, storage_ix, storage);
  1092. BuildAndStoreHuffmanTree(arena->lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS,
  1093. BROTLI_NUM_LITERAL_SYMBOLS, arena->tree,
  1094. arena->lit_depth, arena->lit_bits,
  1095. storage_ix, storage);
  1096. BuildAndStoreHuffmanTree(arena->cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS,
  1097. BROTLI_NUM_COMMAND_SYMBOLS, arena->tree,
  1098. arena->cmd_depth, arena->cmd_bits,
  1099. storage_ix, storage);
  1100. BuildAndStoreHuffmanTree(arena->dist_histo.data_,
  1101. MAX_SIMPLE_DISTANCE_ALPHABET_SIZE,
  1102. num_distance_symbols, arena->tree,
  1103. arena->dist_depth, arena->dist_bits,
  1104. storage_ix, storage);
  1105. StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
  1106. n_commands, arena->lit_depth, arena->lit_bits,
  1107. arena->cmd_depth, arena->cmd_bits,
  1108. arena->dist_depth, arena->dist_bits,
  1109. storage_ix, storage);
  1110. BROTLI_FREE(m, arena);
  1111. if (is_last) {
  1112. JumpToByteBoundary(storage_ix, storage);
  1113. }
  1114. }
  1115. void BrotliStoreMetaBlockFast(MemoryManager* m,
  1116. const uint8_t* input, size_t start_pos, size_t length, size_t mask,
  1117. BROTLI_BOOL is_last, const BrotliEncoderParams* params,
  1118. const Command* commands, size_t n_commands,
  1119. size_t* storage_ix, uint8_t* storage) {
  1120. MetablockArena* arena = BROTLI_ALLOC(m, MetablockArena, 1);
  1121. uint32_t num_distance_symbols = params->dist.alphabet_size_max;
  1122. uint32_t distance_alphabet_bits =
  1123. Log2FloorNonZero(num_distance_symbols - 1) + 1;
  1124. if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(arena)) return;
  1125. StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
  1126. BrotliWriteBits(13, 0, storage_ix, storage);
  1127. if (n_commands <= 128) {
  1128. uint32_t histogram[BROTLI_NUM_LITERAL_SYMBOLS] = { 0 };
  1129. size_t pos = start_pos;
  1130. size_t num_literals = 0;
  1131. size_t i;
  1132. for (i = 0; i < n_commands; ++i) {
  1133. const Command cmd = commands[i];
  1134. size_t j;
  1135. for (j = cmd.insert_len_; j != 0; --j) {
  1136. ++histogram[input[pos & mask]];
  1137. ++pos;
  1138. }
  1139. num_literals += cmd.insert_len_;
  1140. pos += CommandCopyLen(&cmd);
  1141. }
  1142. BrotliBuildAndStoreHuffmanTreeFast(arena->tree, histogram, num_literals,
  1143. /* max_bits = */ 8,
  1144. arena->lit_depth, arena->lit_bits,
  1145. storage_ix, storage);
  1146. StoreStaticCommandHuffmanTree(storage_ix, storage);
  1147. StoreStaticDistanceHuffmanTree(storage_ix, storage);
  1148. StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
  1149. n_commands, arena->lit_depth, arena->lit_bits,
  1150. kStaticCommandCodeDepth,
  1151. kStaticCommandCodeBits,
  1152. kStaticDistanceCodeDepth,
  1153. kStaticDistanceCodeBits,
  1154. storage_ix, storage);
  1155. } else {
  1156. HistogramClearLiteral(&arena->lit_histo);
  1157. HistogramClearCommand(&arena->cmd_histo);
  1158. HistogramClearDistance(&arena->dist_histo);
  1159. BuildHistograms(input, start_pos, mask, commands, n_commands,
  1160. &arena->lit_histo, &arena->cmd_histo, &arena->dist_histo);
  1161. BrotliBuildAndStoreHuffmanTreeFast(arena->tree, arena->lit_histo.data_,
  1162. arena->lit_histo.total_count_,
  1163. /* max_bits = */ 8,
  1164. arena->lit_depth, arena->lit_bits,
  1165. storage_ix, storage);
  1166. BrotliBuildAndStoreHuffmanTreeFast(arena->tree, arena->cmd_histo.data_,
  1167. arena->cmd_histo.total_count_,
  1168. /* max_bits = */ 10,
  1169. arena->cmd_depth, arena->cmd_bits,
  1170. storage_ix, storage);
  1171. BrotliBuildAndStoreHuffmanTreeFast(arena->tree, arena->dist_histo.data_,
  1172. arena->dist_histo.total_count_,
  1173. /* max_bits = */
  1174. distance_alphabet_bits,
  1175. arena->dist_depth, arena->dist_bits,
  1176. storage_ix, storage);
  1177. StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
  1178. n_commands, arena->lit_depth, arena->lit_bits,
  1179. arena->cmd_depth, arena->cmd_bits,
  1180. arena->dist_depth, arena->dist_bits,
  1181. storage_ix, storage);
  1182. }
  1183. BROTLI_FREE(m, arena);
  1184. if (is_last) {
  1185. JumpToByteBoundary(storage_ix, storage);
  1186. }
  1187. }
  1188. /* This is for storing uncompressed blocks (simple raw storage of
  1189. bytes-as-bytes). */
  1190. void BrotliStoreUncompressedMetaBlock(BROTLI_BOOL is_final_block,
  1191. const uint8_t* BROTLI_RESTRICT input,
  1192. size_t position, size_t mask,
  1193. size_t len,
  1194. size_t* BROTLI_RESTRICT storage_ix,
  1195. uint8_t* BROTLI_RESTRICT storage) {
  1196. size_t masked_pos = position & mask;
  1197. BrotliStoreUncompressedMetaBlockHeader(len, storage_ix, storage);
  1198. JumpToByteBoundary(storage_ix, storage);
  1199. if (masked_pos + len > mask + 1) {
  1200. size_t len1 = mask + 1 - masked_pos;
  1201. memcpy(&storage[*storage_ix >> 3], &input[masked_pos], len1);
  1202. *storage_ix += len1 << 3;
  1203. len -= len1;
  1204. masked_pos = 0;
  1205. }
  1206. memcpy(&storage[*storage_ix >> 3], &input[masked_pos], len);
  1207. *storage_ix += len << 3;
  1208. /* We need to clear the next 4 bytes to continue to be
  1209. compatible with BrotliWriteBits. */
  1210. BrotliWriteBitsPrepareStorage(*storage_ix, storage);
  1211. /* Since the uncompressed block itself may not be the final block, add an
  1212. empty one after this. */
  1213. if (is_final_block) {
  1214. BrotliWriteBits(1, 1, storage_ix, storage); /* islast */
  1215. BrotliWriteBits(1, 1, storage_ix, storage); /* isempty */
  1216. JumpToByteBoundary(storage_ix, storage);
  1217. }
  1218. }
  1219. #if defined(BROTLI_TEST)
  1220. void GetBlockLengthPrefixCodeForTest(uint32_t len, size_t* code,
  1221. uint32_t* n_extra, uint32_t* extra) {
  1222. GetBlockLengthPrefixCode(len, code, n_extra, extra);
  1223. }
  1224. #endif
  1225. #if defined(__cplusplus) || defined(c_plusplus)
  1226. } /* extern "C" */
  1227. #endif