huffman.h 4.0 KB

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