/* 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 <brotli/types.h>1213#include "../common/platform.h"1415#if defined(__cplusplus) || defined(c_plusplus)16extern "C" {17#endif1819#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 152021/* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */22#define BROTLI_HUFFMAN_MAX_SIZE_26 39623/* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */24#define BROTLI_HUFFMAN_MAX_SIZE_258 63225/* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */26#define BROTLI_HUFFMAN_MAX_SIZE_272 6462728#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 52930#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \31BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))32#define BROTLI_HUFFMAN_CODE_FAST_LOAD33#endif3435#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)36/* Do not create this struct directly - use the ConstructHuffmanCode37* constructor below! */38typedef struct {39uint8_t bits; /* number of bits used for this symbol */40uint16_t value; /* symbol value or table offset */41} HuffmanCode;4243static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,44const uint16_t value) {45HuffmanCode h;46h.bits = bits;47h.value = value;48return h;49}5051/* Please use the following macros to optimize HuffmanCode accesses in hot52* paths.53*54* For example, assuming |table| contains a HuffmanCode pointer:55*56* BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);57* BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);58* *bits = BROTLI_HC_GET_BITS(table);59* *value = BROTLI_HC_GET_VALUE(table);60* BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);61* *bits2 = BROTLI_HC_GET_BITS(table);62* *value2 = BROTLI_HC_GET_VALUE(table);63*64*/6566#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)67#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)6869/* These must be given a HuffmanCode pointer! */70#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)71#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)7273#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */7475typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;7677static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,78const uint16_t value) {79return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF);80}8182#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)83#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)8485/* These must be given a HuffmanCode pointer! */86#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)87#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)88#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */8990/* Builds Huffman lookup table assuming code lengths are in symbol order. */91BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,92const uint8_t* const code_lengths, uint16_t* count);9394/* Builds Huffman lookup table assuming code lengths are in symbol order.95Returns size of resulting table. */96BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,97int root_bits, const uint16_t* const symbol_lists, uint16_t* count);9899/* Builds a simple Huffman table. The |num_symbols| parameter is to be100interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,1012 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],1024 means 4 symbols with lengths [1, 2, 3, 3]. */103BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,104int root_bits, uint16_t* symbols, uint32_t num_symbols);105106/* Contains a collection of Huffman trees with the same alphabet size. */107/* alphabet_size_limit is needed due to simple codes, since108log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */109typedef struct {110HuffmanCode** htrees;111HuffmanCode* codes;112uint16_t alphabet_size_max;113uint16_t alphabet_size_limit;114uint16_t num_htrees;115} HuffmanTreeGroup;116117#if defined(__cplusplus) || defined(c_plusplus)118} /* extern "C" */119#endif120121#endif /* BROTLI_DEC_HUFFMAN_H_ */122123124