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. This table describes table sizes
23 for alphabets containing up to 1152 = 36 * 32 symbols. */
24 static const uint16_t kMaxHuffmanTableSize[] = {
25 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
26 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272,
27 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528};
28 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
29 #define BROTLI_HUFFMAN_MAX_SIZE_26 396
30 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
31 #define BROTLI_HUFFMAN_MAX_SIZE_258 632
32 /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
33 #define BROTLI_HUFFMAN_MAX_SIZE_272 646
34
35 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
36
37 #if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
38 BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
39 #define BROTLI_HUFFMAN_CODE_FAST_LOAD
40 #endif
41
42 #if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
43 /* Do not create this struct directly - use the ConstructHuffmanCode
44 * constructor below! */
45 typedef struct {
46 uint8_t bits; /* number of bits used for this symbol */
47 uint16_t value; /* symbol value or table offset */
48 } HuffmanCode;
49
ConstructHuffmanCode(const uint8_t bits,const uint16_t value)50 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
51 const uint16_t value) {
52 HuffmanCode h;
53 h.bits = bits;
54 h.value = value;
55 return h;
56 }
57
58 /* Please use the following macros to optimize HuffmanCode accesses in hot
59 * paths.
60 *
61 * For example, assuming |table| contains a HuffmanCode pointer:
62 *
63 * BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
64 * BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
65 * *bits = BROTLI_HC_GET_BITS(table);
66 * *value = BROTLI_HC_GET_VALUE(table);
67 * BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
68 * *bits2 = BROTLI_HC_GET_BITS(table);
69 * *value2 = BROTLI_HC_GET_VALUE(table);
70 *
71 */
72
73 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
74 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
75
76 /* These must be given a HuffmanCode pointer! */
77 #define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
78 #define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
79
80 #else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
81
82 typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
83
ConstructHuffmanCode(const uint8_t bits,const uint16_t value)84 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
85 const uint16_t value) {
86 return ((value & 0xFFFF) << 16) | (bits & 0xFF);
87 }
88
89 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
90 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
91
92 /* These must be given a HuffmanCode pointer! */
93 #define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
94 #define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
95 #endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
96
97 /* Builds Huffman lookup table assuming code lengths are in symbol order. */
98 BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
99 const uint8_t* const code_lengths, uint16_t* count);
100
101 /* Builds Huffman lookup table assuming code lengths are in symbol order.
102 Returns size of resulting table. */
103 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
104 int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
105
106 /* Builds a simple Huffman table. The |num_symbols| parameter is to be
107 interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
108 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
109 4 means 4 symbols with lengths [1, 2, 3, 3]. */
110 BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
111 int root_bits, uint16_t* symbols, uint32_t num_symbols);
112
113 /* Contains a collection of Huffman trees with the same alphabet size. */
114 /* alphabet_size_limit is needed due to simple codes, since
115 log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */
116 typedef struct {
117 HuffmanCode** htrees;
118 HuffmanCode* codes;
119 uint16_t alphabet_size_max;
120 uint16_t alphabet_size_limit;
121 uint16_t num_htrees;
122 } HuffmanTreeGroup;
123
124 #if defined(__cplusplus) || defined(c_plusplus)
125 } /* extern "C" */
126 #endif
127
128 #endif /* BROTLI_DEC_HUFFMAN_H_ */
129