| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2014 Google Inc. All Rights Reserved. |
| 2 |
| 3 Distributed under MIT license. |
| 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 */ |
| 6 |
| 7 // Functions to convert brotli-related data structures into the |
| 8 // brotli bit stream. The functions here operate under |
| 9 // assumption that there is enough space in the storage, i.e., there are |
| 10 // no out-of-range checks anywhere. |
| 11 // |
| 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 |
| 14 // in function arguments. |
| 15 |
| 16 #ifndef BROTLI_ENC_BROTLI_BIT_STREAM_H_ |
| 17 #define BROTLI_ENC_BROTLI_BIT_STREAM_H_ |
| 18 |
| 19 #include <vector> |
| 20 |
| 21 #include "./entropy_encode.h" |
| 22 #include "./metablock.h" |
| 23 #include "./types.h" |
| 24 |
| 25 namespace brotli { |
| 26 |
| 27 // All Store functions here will use a storage_ix, which is always the bit |
| 28 // position for the current storage. |
| 29 |
| 30 // Stores a number between 0 and 255. |
| 31 void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage); |
| 32 |
| 33 // Stores the compressed meta-block header. |
| 34 // REQUIRES: length > 0 |
| 35 // REQUIRES: length <= (1 << 24) |
| 36 void StoreCompressedMetaBlockHeader(bool final_block, |
| 37 size_t length, |
| 38 size_t* storage_ix, |
| 39 uint8_t* storage); |
| 40 |
| 41 // Stores the uncompressed meta-block header. |
| 42 // REQUIRES: length > 0 |
| 43 // REQUIRES: length <= (1 << 24) |
| 44 void StoreUncompressedMetaBlockHeader(size_t length, |
| 45 size_t* storage_ix, |
| 46 uint8_t* storage); |
| 47 |
| 48 // Stores a context map where the histogram type is always the block type. |
| 49 void StoreTrivialContextMap(size_t num_types, |
| 50 size_t context_bits, |
| 51 HuffmanTree* tree, |
| 52 size_t* storage_ix, |
| 53 uint8_t* storage); |
| 54 |
| 55 void StoreHuffmanTreeOfHuffmanTreeToBitMask( |
| 56 const int num_codes, |
| 57 const uint8_t *code_length_bitdepth, |
| 58 size_t *storage_ix, |
| 59 uint8_t *storage); |
| 60 |
| 61 void StoreHuffmanTree(const uint8_t* depths, size_t num, HuffmanTree* tree, |
| 62 size_t *storage_ix, uint8_t *storage); |
| 63 |
| 64 // Builds a Huffman tree from histogram[0:length] into depth[0:length] and |
| 65 // bits[0:length] and stores the encoded tree to the bit stream. |
| 66 void BuildAndStoreHuffmanTree(const uint32_t *histogram, |
| 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 |
| 74 void BuildAndStoreHuffmanTreeFast(const uint32_t *histogram, |
| 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 |