huffman.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Utilities for building Huffman decoding tables. */
  6. #ifndef BROTLI_DEC_HUFFMAN_H_
  7. #define BROTLI_DEC_HUFFMAN_H_
  8. #include "../common/platform.h"
  9. #include <brotli/types.h>
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
  14. /* Maximum possible Huffman table size for an alphabet size of (index * 32),
  15. max code length 15 and root table bits 8. */
  16. static const uint16_t kMaxHuffmanTableSize[] = {
  17. 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
  18. 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272,
  19. 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528};
  20. /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
  21. #define BROTLI_HUFFMAN_MAX_SIZE_26 396
  22. /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
  23. #define BROTLI_HUFFMAN_MAX_SIZE_258 632
  24. /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
  25. #define BROTLI_HUFFMAN_MAX_SIZE_272 646
  26. #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
  27. #if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
  28. BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
  29. #define BROTLI_HUFFMAN_CODE_FAST_LOAD
  30. #endif
  31. #if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
  32. /* Do not create this struct directly - use the ConstructHuffmanCode
  33. * constructor below! */
  34. typedef struct {
  35. uint8_t bits; /* number of bits used for this symbol */
  36. uint16_t value; /* symbol value or table offset */
  37. } HuffmanCode;
  38. static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
  39. const uint16_t value) {
  40. HuffmanCode h;
  41. h.bits = bits;
  42. h.value = value;
  43. return h;
  44. }
  45. /* Please use the following macros to optimize HuffmanCode accesses in hot
  46. * paths.
  47. *
  48. * For example, assuming |table| contains a HuffmanCode pointer:
  49. *
  50. * BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
  51. * BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
  52. * *bits = BROTLI_HC_GET_BITS(table);
  53. * *value = BROTLI_HC_GET_VALUE(table);
  54. * BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
  55. * *bits2 = BROTLI_HC_GET_BITS(table);
  56. * *value2 = BROTLI_HC_GET_VALUE(table);
  57. *
  58. */
  59. #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
  60. #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
  61. /* These must be given a HuffmanCode pointer! */
  62. #define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
  63. #define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
  64. #else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
  65. typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
  66. static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
  67. const uint16_t value) {
  68. return ((value & 0xFFFF) << 16) | (bits & 0xFF);
  69. }
  70. #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
  71. #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
  72. /* These must be given a HuffmanCode pointer! */
  73. #define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
  74. #define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
  75. #endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
  76. /* Builds Huffman lookup table assuming code lengths are in symbol order. */
  77. BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
  78. const uint8_t* const code_lengths, uint16_t* count);
  79. /* Builds Huffman lookup table assuming code lengths are in symbol order.
  80. Returns size of resulting table. */
  81. BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
  82. int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
  83. /* Builds a simple Huffman table. The |num_symbols| parameter is to be
  84. interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
  85. 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
  86. 4 means 4 symbols with lengths [1, 2, 3, 3]. */
  87. BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
  88. int root_bits, uint16_t* symbols, uint32_t num_symbols);
  89. /* Contains a collection of Huffman trees with the same alphabet size. */
  90. /* max_symbol is needed due to simple codes since log2(alphabet_size) could be
  91. greater than log2(max_symbol). */
  92. typedef struct {
  93. HuffmanCode** htrees;
  94. HuffmanCode* codes;
  95. uint16_t alphabet_size;
  96. uint16_t max_symbol;
  97. uint16_t num_htrees;
  98. } HuffmanTreeGroup;
  99. #if defined(__cplusplus) || defined(c_plusplus)
  100. } /* extern "C" */
  101. #endif
  102. #endif /* BROTLI_DEC_HUFFMAN_H_ */