1 /* Copyright 2013 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 /* Utilities for building Huffman decoding tables. */ 8 9 #ifndef BROTLI_DEC_HUFFMAN_H_ 10 #define BROTLI_DEC_HUFFMAN_H_ 11 12 #include "../common/platform.h" 13 #include <brotli/types.h> 14 15 #if defined(__cplusplus) || defined(c_plusplus) 16 extern "C" { 17 #endif 18 19 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15 20 21 /* Maximum possible Huffman table size for an alphabet size of (index * 32), 22 max code length 15 and root table bits 8. */ 23 static const uint16_t kMaxHuffmanTableSize[] = { 24 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822, 25 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272, 26 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528}; 27 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */ 28 #define BROTLI_HUFFMAN_MAX_SIZE_26 396 29 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */ 30 #define BROTLI_HUFFMAN_MAX_SIZE_258 632 31 /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */ 32 #define BROTLI_HUFFMAN_MAX_SIZE_272 646 33 34 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5 35 36 typedef struct { 37 uint8_t bits; /* number of bits used for this symbol */ 38 uint16_t value; /* symbol value or table offset */ 39 } HuffmanCode; 40 41 /* Builds Huffman lookup table assuming code lengths are in symbol order. */ 42 BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table, 43 const uint8_t* const code_lengths, uint16_t* count); 44 45 /* Builds Huffman lookup table assuming code lengths are in symbol order. 46 Returns size of resulting table. */ 47 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, 48 int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg); 49 50 /* Builds a simple Huffman table. The |num_symbols| parameter is to be 51 interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 52 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2], 53 4 means 4 symbols with lengths [1, 2, 3, 3]. */ 54 BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, 55 int root_bits, uint16_t* symbols, uint32_t num_symbols); 56 57 /* Contains a collection of Huffman trees with the same alphabet size. */ 58 /* max_symbol is needed due to simple codes since log2(alphabet_size) could be 59 greater than log2(max_symbol). */ 60 typedef struct { 61 HuffmanCode** htrees; 62 HuffmanCode* codes; 63 uint16_t alphabet_size; 64 uint16_t max_symbol; 65 uint16_t num_htrees; 66 } HuffmanTreeGroup; 67 68 #if defined(__cplusplus) || defined(c_plusplus) 69 } /* extern "C" */ 70 #endif 71 72 #endif /* BROTLI_DEC_HUFFMAN_H_ */ 73