Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(662)

Side by Side Diff: third_party/brotli/enc/brotli_bit_stream.c

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

Powered by Google App Engine
This is Rietveld 408576698