brotli_bit_stream.c 49 KB

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