• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
37   BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
38 #define BROTLI_HUFFMAN_CODE_FAST_LOAD
39 #endif
40 
41 #if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
42 /* Do not create this struct directly - use the ConstructHuffmanCode
43  * constructor below! */
44 typedef struct {
45   uint8_t bits;    /* number of bits used for this symbol */
46   uint16_t value;  /* symbol value or table offset */
47 } HuffmanCode;
48 
ConstructHuffmanCode(const uint8_t bits,const uint16_t value)49 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
50     const uint16_t value) {
51   HuffmanCode h;
52   h.bits = bits;
53   h.value = value;
54   return h;
55 }
56 
57 /* Please use the following macros to optimize HuffmanCode accesses in hot
58  * paths.
59  *
60  * For example, assuming |table| contains a HuffmanCode pointer:
61  *
62  *   BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
63  *   BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
64  *   *bits = BROTLI_HC_GET_BITS(table);
65  *   *value = BROTLI_HC_GET_VALUE(table);
66  *   BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
67  *   *bits2 = BROTLI_HC_GET_BITS(table);
68  *   *value2 = BROTLI_HC_GET_VALUE(table);
69  *
70  */
71 
72 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
73 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
74 
75 /* These must be given a HuffmanCode pointer! */
76 #define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
77 #define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
78 
79 #else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
80 
81 typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
82 
ConstructHuffmanCode(const uint8_t bits,const uint16_t value)83 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
84     const uint16_t value) {
85   return ((value & 0xFFFF) << 16) | (bits & 0xFF);
86 }
87 
88 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
89 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
90 
91 /* These must be given a HuffmanCode pointer! */
92 #define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
93 #define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
94 #endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
95 
96 /* Builds Huffman lookup table assuming code lengths are in symbol order. */
97 BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
98     const uint8_t* const code_lengths, uint16_t* count);
99 
100 /* Builds Huffman lookup table assuming code lengths are in symbol order.
101    Returns size of resulting table. */
102 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
103     int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
104 
105 /* Builds a simple Huffman table. The |num_symbols| parameter is to be
106    interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
107    2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
108    4 means 4 symbols with lengths [1, 2, 3, 3]. */
109 BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
110     int root_bits, uint16_t* symbols, uint32_t num_symbols);
111 
112 /* Contains a collection of Huffman trees with the same alphabet size. */
113 /* max_symbol is needed due to simple codes since log2(alphabet_size) could be
114    greater than log2(max_symbol). */
115 typedef struct {
116   HuffmanCode** htrees;
117   HuffmanCode* codes;
118   uint16_t alphabet_size;
119   uint16_t max_symbol;
120   uint16_t num_htrees;
121 } HuffmanTreeGroup;
122 
123 #if defined(__cplusplus) || defined(c_plusplus)
124 }  /* extern "C" */
125 #endif
126 
127 #endif  /* BROTLI_DEC_HUFFMAN_H_ */
128