huffman.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/types.h"
  9. #include "./port.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};
  19. /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
  20. #define BROTLI_HUFFMAN_MAX_SIZE_26 396
  21. /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
  22. #define BROTLI_HUFFMAN_MAX_SIZE_258 632
  23. /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
  24. #define BROTLI_HUFFMAN_MAX_SIZE_272 646
  25. #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
  26. typedef struct {
  27. uint8_t bits; /* number of bits used for this symbol */
  28. uint16_t value; /* symbol value or table offset */
  29. } HuffmanCode;
  30. /* Builds Huffman lookup table assuming code lengths are in symbol order. */
  31. BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
  32. const uint8_t* const code_lengths, uint16_t* count);
  33. /* Builds Huffman lookup table assuming code lengths are in symbol order. */
  34. /* Returns size of resulting table. */
  35. BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
  36. int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
  37. /* Builds a simple Huffman table. The num_symbols parameter is to be */
  38. /* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
  39. /* symbols, 3 means 4 symbols with lengths 2,2,2,2, 4 means 4 symbols with */
  40. /* lengths 1,2,3,3. */
  41. BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
  42. int root_bits, uint16_t* symbols, uint32_t num_symbols);
  43. /* Contains a collection of Huffman trees with the same alphabet size. */
  44. typedef struct {
  45. HuffmanCode** htrees;
  46. HuffmanCode* codes;
  47. uint16_t alphabet_size;
  48. uint16_t num_htrees;
  49. } HuffmanTreeGroup;
  50. #if defined(__cplusplus) || defined(c_plusplus)
  51. } /* extern "C" */
  52. #endif
  53. #endif /* BROTLI_DEC_HUFFMAN_H_ */