| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2010 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 // Entropy encoding (Huffman) utilities. |
| 8 |
| 9 #ifndef BROTLI_ENC_ENTROPY_ENCODE_H_ |
| 10 #define BROTLI_ENC_ENTROPY_ENCODE_H_ |
| 11 |
| 12 #include <string.h> |
| 13 #include "./histogram.h" |
| 14 #include "./prefix.h" |
| 15 #include "./types.h" |
| 16 |
| 17 namespace brotli { |
| 18 |
| 19 // A node of a Huffman tree. |
| 20 struct HuffmanTree { |
| 21 HuffmanTree() {} |
| 22 HuffmanTree(uint32_t count, int16_t left, int16_t right) |
| 23 : total_count_(count), |
| 24 index_left_(left), |
| 25 index_right_or_value_(right) { |
| 26 } |
| 27 uint32_t total_count_; |
| 28 int16_t index_left_; |
| 29 int16_t index_right_or_value_; |
| 30 }; |
| 31 |
| 32 void SetDepth(const HuffmanTree &p, HuffmanTree *pool, |
| 33 uint8_t *depth, uint8_t level); |
| 34 |
| 35 // This function will create a Huffman tree. |
| 36 // |
| 37 // The (data,length) contains the population counts. |
| 38 // The tree_limit is the maximum bit depth of the Huffman codes. |
| 39 // |
| 40 // The depth contains the tree, i.e., how many bits are used for |
| 41 // the symbol. |
| 42 // |
| 43 // The actual Huffman tree is constructed in the tree[] array, which has to |
| 44 // be at least 2 * length + 1 long. |
| 45 // |
| 46 // See http://en.wikipedia.org/wiki/Huffman_coding |
| 47 void CreateHuffmanTree(const uint32_t *data, |
| 48 const size_t length, |
| 49 const int tree_limit, |
| 50 HuffmanTree* tree, |
| 51 uint8_t *depth); |
| 52 |
| 53 // Change the population counts in a way that the consequent |
| 54 // Huffman tree compression, especially its rle-part will be more |
| 55 // likely to compress this data more efficiently. |
| 56 // |
| 57 // length contains the size of the histogram. |
| 58 // counts contains the population counts. |
| 59 // good_for_rle is a buffer of at least length size |
| 60 void OptimizeHuffmanCountsForRle(size_t length, uint32_t* counts, |
| 61 uint8_t* good_for_rle); |
| 62 |
| 63 // Write a Huffman tree from bit depths into the bitstream representation |
| 64 // of a Huffman tree. The generated Huffman tree is to be compressed once |
| 65 // more using a Huffman tree |
| 66 void WriteHuffmanTree(const uint8_t* depth, |
| 67 size_t num, |
| 68 size_t* tree_size, |
| 69 uint8_t* tree, |
| 70 uint8_t* extra_bits_data); |
| 71 |
| 72 // Get the actual bit values for a tree of bit depths. |
| 73 void ConvertBitDepthsToSymbols(const uint8_t *depth, |
| 74 size_t len, |
| 75 uint16_t *bits); |
| 76 |
| 77 template<int kSize> |
| 78 struct EntropyCode { |
| 79 // How many bits for symbol. |
| 80 uint8_t depth_[kSize]; |
| 81 // Actual bits used to represent the symbol. |
| 82 uint16_t bits_[kSize]; |
| 83 // How many non-zero depth. |
| 84 int count_; |
| 85 // First four symbols with non-zero depth. |
| 86 int symbols_[4]; |
| 87 }; |
| 88 |
| 89 static const int kCodeLengthCodes = 18; |
| 90 |
| 91 // Literal entropy code. |
| 92 typedef EntropyCode<256> EntropyCodeLiteral; |
| 93 // Prefix entropy codes. |
| 94 typedef EntropyCode<kNumCommandPrefixes> EntropyCodeCommand; |
| 95 typedef EntropyCode<kNumDistancePrefixes> EntropyCodeDistance; |
| 96 typedef EntropyCode<kNumBlockLenPrefixes> EntropyCodeBlockLength; |
| 97 // Context map entropy code, 256 Huffman tree indexes + 16 run length codes. |
| 98 typedef EntropyCode<272> EntropyCodeContextMap; |
| 99 // Block type entropy code, 256 block types + 2 special symbols. |
| 100 typedef EntropyCode<258> EntropyCodeBlockType; |
| 101 |
| 102 } // namespace brotli |
| 103 |
| 104 #endif // BROTLI_ENC_ENTROPY_ENCODE_H_ |
| OLD | NEW |