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