OLD | NEW |
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 // Functions to convert brotli-related data structures into the | 7 /* Functions to convert brotli-related data structures into the |
8 // brotli bit stream. The functions here operate under | 8 brotli bit stream. The functions here operate under |
9 // assumption that there is enough space in the storage, i.e., there are | 9 assumption that there is enough space in the storage, i.e., there are |
10 // no out-of-range checks anywhere. | 10 no out-of-range checks anywhere. |
11 // | 11 |
12 // These functions do bit addressing into a byte array. The byte array | 12 These functions do bit addressing into a byte array. The byte array |
13 // is called "storage" and the index to the bit is called storage_ix | 13 is called "storage" and the index to the bit is called storage_ix |
14 // in function arguments. | 14 in function arguments. */ |
15 | 15 |
16 #ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_ | 16 #ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_ |
17 #define BROTLI_ENC_BROTLI_BIT_STREAM_H_ | 17 #define BROTLI_ENC_BROTLI_BIT_STREAM_H_ |
18 | 18 |
19 #include <vector> | 19 #include <brotli/types.h> |
| 20 #include "./command.h" |
| 21 #include "./context.h" |
| 22 #include "./entropy_encode.h" |
| 23 #include "./memory.h" |
| 24 #include "./metablock.h" |
| 25 #include "./port.h" |
20 | 26 |
21 #include "./entropy_encode.h" | 27 #if defined(__cplusplus) || defined(c_plusplus) |
22 #include "./metablock.h" | 28 extern "C" { |
23 #include "./types.h" | 29 #endif |
24 | 30 |
25 namespace brotli { | 31 /* All Store functions here will use a storage_ix, which is always the bit |
| 32 position for the current storage. */ |
26 | 33 |
27 // All Store functions here will use a storage_ix, which is always the bit | 34 BROTLI_INTERNAL void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num, |
28 // position for the current storage. | 35 HuffmanTree* tree, size_t *storage_ix, uint8_t *storage); |
29 | 36 |
30 // Stores a number between 0 and 255. | 37 BROTLI_INTERNAL void BrotliBuildAndStoreHuffmanTreeFast( |
31 void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage); | 38 MemoryManager* m, const uint32_t* histogram, const size_t histogram_total, |
| 39 const size_t max_bits, uint8_t* depth, uint16_t* bits, size_t* storage_ix, |
| 40 uint8_t* storage); |
32 | 41 |
33 // Stores the compressed meta-block header. | 42 /* REQUIRES: length > 0 */ |
34 // REQUIRES: length > 0 | 43 /* REQUIRES: length <= (1 << 24) */ |
35 // REQUIRES: length <= (1 << 24) | 44 BROTLI_INTERNAL void BrotliStoreMetaBlock(MemoryManager* m, |
36 void StoreCompressedMetaBlockHeader(bool final_block, | 45 const uint8_t* input, |
37 size_t length, | 46 size_t start_pos, |
38 size_t* storage_ix, | 47 size_t length, |
39 uint8_t* storage); | 48 size_t mask, |
| 49 uint8_t prev_byte, |
| 50 uint8_t prev_byte2, |
| 51 BROTLI_BOOL is_final_block, |
| 52 uint32_t num_direct_distance_codes, |
| 53 uint32_t distance_postfix_bits, |
| 54 ContextType literal_context_mode, |
| 55 const Command* commands, |
| 56 size_t n_commands, |
| 57 const MetaBlockSplit* mb, |
| 58 size_t* storage_ix, |
| 59 uint8_t* storage); |
40 | 60 |
41 // Stores the uncompressed meta-block header. | 61 /* Stores the meta-block without doing any block splitting, just collects |
42 // REQUIRES: length > 0 | 62 one histogram per block category and uses that for entropy coding. |
43 // REQUIRES: length <= (1 << 24) | 63 REQUIRES: length > 0 |
44 void StoreUncompressedMetaBlockHeader(size_t length, | 64 REQUIRES: length <= (1 << 24) */ |
45 size_t* storage_ix, | 65 BROTLI_INTERNAL void BrotliStoreMetaBlockTrivial(MemoryManager* m, |
46 uint8_t* storage); | 66 const uint8_t* input, |
| 67 size_t start_pos, |
| 68 size_t length, |
| 69 size_t mask, |
| 70 BROTLI_BOOL is_last, |
| 71 const Command *commands, |
| 72 size_t n_commands, |
| 73 size_t* storage_ix, |
| 74 uint8_t* storage); |
47 | 75 |
48 // Stores a context map where the histogram type is always the block type. | 76 /* Same as above, but uses static prefix codes for histograms with a only a few |
49 void StoreTrivialContextMap(size_t num_types, | 77 symbols, and uses static code length prefix codes for all other histograms. |
50 size_t context_bits, | 78 REQUIRES: length > 0 |
51 HuffmanTree* tree, | 79 REQUIRES: length <= (1 << 24) */ |
52 size_t* storage_ix, | 80 BROTLI_INTERNAL void BrotliStoreMetaBlockFast(MemoryManager* m, |
53 uint8_t* storage); | 81 const uint8_t* input, |
| 82 size_t start_pos, |
| 83 size_t length, |
| 84 size_t mask, |
| 85 BROTLI_BOOL is_last, |
| 86 const Command *commands, |
| 87 size_t n_commands, |
| 88 size_t* storage_ix, |
| 89 uint8_t* storage); |
54 | 90 |
55 void StoreHuffmanTreeOfHuffmanTreeToBitMask( | 91 /* This is for storing uncompressed blocks (simple raw storage of |
56 const int num_codes, | 92 bytes-as-bytes). |
57 const uint8_t *code_length_bitdepth, | 93 REQUIRES: length > 0 |
58 size_t *storage_ix, | 94 REQUIRES: length <= (1 << 24) */ |
59 uint8_t *storage); | 95 BROTLI_INTERNAL void BrotliStoreUncompressedMetaBlock( |
| 96 BROTLI_BOOL is_final_block, const uint8_t* input, size_t position, |
| 97 size_t mask, size_t len, size_t* storage_ix, uint8_t* storage); |
60 | 98 |
61 void StoreHuffmanTree(const uint8_t* depths, size_t num, HuffmanTree* tree, | 99 /* Stores an empty metadata meta-block and syncs to a byte boundary. */ |
62 size_t *storage_ix, uint8_t *storage); | 100 BROTLI_INTERNAL void BrotliStoreSyncMetaBlock(size_t* storage_ix, |
| 101 uint8_t* storage); |
63 | 102 |
64 // Builds a Huffman tree from histogram[0:length] into depth[0:length] and | 103 #if defined(__cplusplus) || defined(c_plusplus) |
65 // bits[0:length] and stores the encoded tree to the bit stream. | 104 } /* extern "C" */ |
66 void BuildAndStoreHuffmanTree(const uint32_t *histogram, | 105 #endif |
67 const size_t length, | |
68 HuffmanTree* tree, | |
69 uint8_t* depth, | |
70 uint16_t* bits, | |
71 size_t* storage_ix, | |
72 uint8_t* storage); | |
73 | 106 |
74 void BuildAndStoreHuffmanTreeFast(const uint32_t *histogram, | 107 #endif /* BROTLI_ENC_BROTLI_BIT_STREAM_H_ */ |
75 const size_t histogram_total, | |
76 const size_t max_bits, | |
77 uint8_t* depth, | |
78 uint16_t* bits, | |
79 size_t* storage_ix, | |
80 uint8_t* storage); | |
81 | |
82 // Encodes the given context map to the bit stream. The number of different | |
83 // histogram ids is given by num_clusters. | |
84 void EncodeContextMap(const std::vector<uint32_t>& context_map, | |
85 size_t num_clusters, | |
86 HuffmanTree* tree, | |
87 size_t* storage_ix, uint8_t* storage); | |
88 | |
89 // Data structure that stores everything that is needed to encode each block | |
90 // switch command. | |
91 struct BlockSplitCode { | |
92 std::vector<uint32_t> type_code; | |
93 std::vector<uint32_t> length_prefix; | |
94 std::vector<uint32_t> length_nextra; | |
95 std::vector<uint32_t> length_extra; | |
96 std::vector<uint8_t> type_depths; | |
97 std::vector<uint16_t> type_bits; | |
98 uint8_t length_depths[kNumBlockLenPrefixes]; | |
99 uint16_t length_bits[kNumBlockLenPrefixes]; | |
100 }; | |
101 | |
102 // Builds a BlockSplitCode data structure from the block split given by the | |
103 // vector of block types and block lengths and stores it to the bit stream. | |
104 void BuildAndStoreBlockSplitCode(const std::vector<uint8_t>& types, | |
105 const std::vector<uint32_t>& lengths, | |
106 const size_t num_types, | |
107 BlockSplitCode* code, | |
108 size_t* storage_ix, | |
109 uint8_t* storage); | |
110 | |
111 // Stores the block switch command with index block_ix to the bit stream. | |
112 void StoreBlockSwitch(const BlockSplitCode& code, | |
113 const size_t block_ix, | |
114 size_t* storage_ix, | |
115 uint8_t* storage); | |
116 | |
117 // REQUIRES: length > 0 | |
118 // REQUIRES: length <= (1 << 24) | |
119 void StoreMetaBlock(const uint8_t* input, | |
120 size_t start_pos, | |
121 size_t length, | |
122 size_t mask, | |
123 uint8_t prev_byte, | |
124 uint8_t prev_byte2, | |
125 bool final_block, | |
126 uint32_t num_direct_distance_codes, | |
127 uint32_t distance_postfix_bits, | |
128 ContextType literal_context_mode, | |
129 const brotli::Command *commands, | |
130 size_t n_commands, | |
131 const MetaBlockSplit& mb, | |
132 size_t *storage_ix, | |
133 uint8_t *storage); | |
134 | |
135 // Stores the meta-block without doing any block splitting, just collects | |
136 // one histogram per block category and uses that for entropy coding. | |
137 // REQUIRES: length > 0 | |
138 // REQUIRES: length <= (1 << 24) | |
139 void StoreMetaBlockTrivial(const uint8_t* input, | |
140 size_t start_pos, | |
141 size_t length, | |
142 size_t mask, | |
143 bool is_last, | |
144 const brotli::Command *commands, | |
145 size_t n_commands, | |
146 size_t *storage_ix, | |
147 uint8_t *storage); | |
148 | |
149 // Same as above, but uses static prefix codes for histograms with a only a few | |
150 // symbols, and uses static code length prefix codes for all other histograms. | |
151 // REQUIRES: length > 0 | |
152 // REQUIRES: length <= (1 << 24) | |
153 void StoreMetaBlockFast(const uint8_t* input, | |
154 size_t start_pos, | |
155 size_t length, | |
156 size_t mask, | |
157 bool is_last, | |
158 const brotli::Command *commands, | |
159 size_t n_commands, | |
160 size_t *storage_ix, | |
161 uint8_t *storage); | |
162 | |
163 // This is for storing uncompressed blocks (simple raw storage of | |
164 // bytes-as-bytes). | |
165 // REQUIRES: length > 0 | |
166 // REQUIRES: length <= (1 << 24) | |
167 void StoreUncompressedMetaBlock(bool final_block, | |
168 const uint8_t* input, | |
169 size_t position, size_t mask, | |
170 size_t len, | |
171 size_t* storage_ix, | |
172 uint8_t* storage); | |
173 | |
174 // Stores an empty metadata meta-block and syncs to a byte boundary. | |
175 void StoreSyncMetaBlock(size_t* storage_ix, uint8_t* storage); | |
176 | |
177 } // namespace brotli | |
178 | |
179 #endif // BROTLI_ENC_BROTLI_BIT_STREAM_H_ | |
OLD | NEW |