11cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved. 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci Distributed under MIT license. 41cb0ef41Sopenharmony_ci See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 51cb0ef41Sopenharmony_ci*/ 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci/* Utilities for building Huffman decoding tables. */ 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci#ifndef BROTLI_DEC_HUFFMAN_H_ 101cb0ef41Sopenharmony_ci#define BROTLI_DEC_HUFFMAN_H_ 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci#include "../common/platform.h" 131cb0ef41Sopenharmony_ci#include <brotli/types.h> 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 161cb0ef41Sopenharmony_ciextern "C" { 171cb0ef41Sopenharmony_ci#endif 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci/* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */ 221cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_MAX_SIZE_26 396 231cb0ef41Sopenharmony_ci/* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */ 241cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_MAX_SIZE_258 632 251cb0ef41Sopenharmony_ci/* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */ 261cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_MAX_SIZE_272 646 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \ 311cb0ef41Sopenharmony_ci BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)) 321cb0ef41Sopenharmony_ci#define BROTLI_HUFFMAN_CODE_FAST_LOAD 331cb0ef41Sopenharmony_ci#endif 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD) 361cb0ef41Sopenharmony_ci/* Do not create this struct directly - use the ConstructHuffmanCode 371cb0ef41Sopenharmony_ci * constructor below! */ 381cb0ef41Sopenharmony_citypedef struct { 391cb0ef41Sopenharmony_ci uint8_t bits; /* number of bits used for this symbol */ 401cb0ef41Sopenharmony_ci uint16_t value; /* symbol value or table offset */ 411cb0ef41Sopenharmony_ci} HuffmanCode; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cistatic BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, 441cb0ef41Sopenharmony_ci const uint16_t value) { 451cb0ef41Sopenharmony_ci HuffmanCode h; 461cb0ef41Sopenharmony_ci h.bits = bits; 471cb0ef41Sopenharmony_ci h.value = value; 481cb0ef41Sopenharmony_ci return h; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci/* Please use the following macros to optimize HuffmanCode accesses in hot 521cb0ef41Sopenharmony_ci * paths. 531cb0ef41Sopenharmony_ci * 541cb0ef41Sopenharmony_ci * For example, assuming |table| contains a HuffmanCode pointer: 551cb0ef41Sopenharmony_ci * 561cb0ef41Sopenharmony_ci * BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); 571cb0ef41Sopenharmony_ci * BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table); 581cb0ef41Sopenharmony_ci * *bits = BROTLI_HC_GET_BITS(table); 591cb0ef41Sopenharmony_ci * *value = BROTLI_HC_GET_VALUE(table); 601cb0ef41Sopenharmony_ci * BROTLI_HC_ADJUST_TABLE_INDEX(table, offset); 611cb0ef41Sopenharmony_ci * *bits2 = BROTLI_HC_GET_BITS(table); 621cb0ef41Sopenharmony_ci * *value2 = BROTLI_HC_GET_VALUE(table); 631cb0ef41Sopenharmony_ci * 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) 671cb0ef41Sopenharmony_ci#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V) 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci/* These must be given a HuffmanCode pointer! */ 701cb0ef41Sopenharmony_ci#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits) 711cb0ef41Sopenharmony_ci#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value) 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_citypedef BROTLI_ALIGNED(4) uint32_t HuffmanCode; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_cistatic BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits, 781cb0ef41Sopenharmony_ci const uint16_t value) { 791cb0ef41Sopenharmony_ci return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF); 801cb0ef41Sopenharmony_ci} 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H) 831cb0ef41Sopenharmony_ci#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H) 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci/* These must be given a HuffmanCode pointer! */ 861cb0ef41Sopenharmony_ci#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF) 871cb0ef41Sopenharmony_ci#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16) 881cb0ef41Sopenharmony_ci#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */ 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci/* Builds Huffman lookup table assuming code lengths are in symbol order. */ 911cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table, 921cb0ef41Sopenharmony_ci const uint8_t* const code_lengths, uint16_t* count); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci/* Builds Huffman lookup table assuming code lengths are in symbol order. 951cb0ef41Sopenharmony_ci Returns size of resulting table. */ 961cb0ef41Sopenharmony_ciBROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, 971cb0ef41Sopenharmony_ci int root_bits, const uint16_t* const symbol_lists, uint16_t* count); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci/* Builds a simple Huffman table. The |num_symbols| parameter is to be 1001cb0ef41Sopenharmony_ci interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 1011cb0ef41Sopenharmony_ci 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2], 1021cb0ef41Sopenharmony_ci 4 means 4 symbols with lengths [1, 2, 3, 3]. */ 1031cb0ef41Sopenharmony_ciBROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, 1041cb0ef41Sopenharmony_ci int root_bits, uint16_t* symbols, uint32_t num_symbols); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci/* Contains a collection of Huffman trees with the same alphabet size. */ 1071cb0ef41Sopenharmony_ci/* alphabet_size_limit is needed due to simple codes, since 1081cb0ef41Sopenharmony_ci log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */ 1091cb0ef41Sopenharmony_citypedef struct { 1101cb0ef41Sopenharmony_ci HuffmanCode** htrees; 1111cb0ef41Sopenharmony_ci HuffmanCode* codes; 1121cb0ef41Sopenharmony_ci uint16_t alphabet_size_max; 1131cb0ef41Sopenharmony_ci uint16_t alphabet_size_limit; 1141cb0ef41Sopenharmony_ci uint16_t num_htrees; 1151cb0ef41Sopenharmony_ci} HuffmanTreeGroup; 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus) 1181cb0ef41Sopenharmony_ci} /* extern "C" */ 1191cb0ef41Sopenharmony_ci#endif 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci#endif /* BROTLI_DEC_HUFFMAN_H_ */ 122