brotli_bit_stream.c 48 KB

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