/* Copyright 2013 Google Inc. All Rights Reserved.12Distributed under MIT license.3See file LICENSE for detail or copy at https://opensource.org/licenses/MIT4*/56/* Utilities for building Huffman decoding tables. */78#ifndef BROTLI_DEC_HUFFMAN_H_9#define BROTLI_DEC_HUFFMAN_H_1011#include "../common/platform.h"1213#if defined(__cplusplus) || defined(c_plusplus)14extern "C" {15#endif1617#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 151819/* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */20#define BROTLI_HUFFMAN_MAX_SIZE_26 39621/* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */22#define BROTLI_HUFFMAN_MAX_SIZE_258 63223/* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */24#define BROTLI_HUFFMAN_MAX_SIZE_272 6462526#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 52728#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \29BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))30#define BROTLI_HUFFMAN_CODE_FAST_LOAD31#endif3233#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)34/* Do not create this struct directly - use the ConstructHuffmanCode35* constructor below! */36typedef struct {37uint8_t bits; /* number of bits used for this symbol */38uint16_t value; /* symbol value or table offset */39} HuffmanCode;4041static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,42const uint16_t value) {43HuffmanCode h;44h.bits = bits;45h.value = value;46return h;47}4849/* Please use the following macros to optimize HuffmanCode accesses in hot50* paths.51*52* For example, assuming |table| contains a HuffmanCode pointer:53*54* BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);55* BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);56* *bits = BROTLI_HC_GET_BITS(table);57* *value = BROTLI_HC_GET_VALUE(table);58* BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);59* *bits2 = BROTLI_HC_GET_BITS(table);60* *value2 = BROTLI_HC_GET_VALUE(table);61*62*/6364#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)65#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)6667/* These must be given a HuffmanCode pointer! */68#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)69#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)7071#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */7273typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;7475static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,76const uint16_t value) {77return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF);78}7980#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)81#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)8283/* These must be given a HuffmanCode pointer! */84#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)85#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)86#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */8788/* Builds Huffman lookup table assuming code lengths are in symbol order. */89BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,90const uint8_t* const code_lengths, uint16_t* count);9192/* Builds Huffman lookup table assuming code lengths are in symbol order.93Returns size of resulting table. */94BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,95int root_bits, const uint16_t* const symbol_lists, uint16_t* count);9697/* Builds a simple Huffman table. The |num_symbols| parameter is to be98interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,992 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],1004 means 4 symbols with lengths [1, 2, 3, 3]. */101BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,102int root_bits, uint16_t* symbols, uint32_t num_symbols);103104/* Contains a collection of Huffman trees with the same alphabet size. */105/* alphabet_size_limit is needed due to simple codes, since106log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */107typedef struct {108HuffmanCode** htrees;109HuffmanCode* codes;110uint16_t alphabet_size_max;111uint16_t alphabet_size_limit;112uint16_t num_htrees;113} HuffmanTreeGroup;114115#if defined(__cplusplus) || defined(c_plusplus)116} /* extern "C" */117#endif118119#endif /* BROTLI_DEC_HUFFMAN_H_ */120121122