huffman.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Utilities for building and looking up Huffman trees.
  11. //
  12. // Author: Urvang Joshi (urvang@google.com)
  13. #ifndef WEBP_UTILS_HUFFMAN_H_
  14. #define WEBP_UTILS_HUFFMAN_H_
  15. #include <assert.h>
  16. #include "../webp/format_constants.h"
  17. #include "../webp/types.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. // A node of a Huffman tree.
  22. typedef struct {
  23. int symbol_;
  24. int children_; // delta offset to both children (contiguous) or 0 if leaf.
  25. } HuffmanTreeNode;
  26. // Huffman Tree.
  27. #define HUFF_LUT_BITS 7
  28. #define HUFF_LUT (1U << HUFF_LUT_BITS)
  29. typedef struct HuffmanTree HuffmanTree;
  30. struct HuffmanTree {
  31. // Fast lookup for short bit lengths.
  32. uint8_t lut_bits_[HUFF_LUT];
  33. int16_t lut_symbol_[HUFF_LUT];
  34. int16_t lut_jump_[HUFF_LUT];
  35. // Complete tree for lookups.
  36. HuffmanTreeNode* root_; // all the nodes, starting at root.
  37. int max_nodes_; // max number of nodes
  38. int num_nodes_; // number of currently occupied nodes
  39. };
  40. // Huffman Tree group.
  41. typedef struct HTreeGroup HTreeGroup;
  42. struct HTreeGroup {
  43. HuffmanTree htrees_[HUFFMAN_CODES_PER_META_CODE];
  44. };
  45. // Returns true if the given node is not a leaf of the Huffman tree.
  46. static WEBP_INLINE int HuffmanTreeNodeIsNotLeaf(
  47. const HuffmanTreeNode* const node) {
  48. return node->children_;
  49. }
  50. // Go down one level. Most critical function. 'right_child' must be 0 or 1.
  51. static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode(
  52. const HuffmanTreeNode* node, int right_child) {
  53. return node + node->children_ + right_child;
  54. }
  55. // Releases the nodes of the Huffman tree.
  56. // Note: It does NOT free 'tree' itself.
  57. void VP8LHuffmanTreeFree(HuffmanTree* const tree);
  58. // Creates the instance of HTreeGroup with specified number of tree-groups.
  59. HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);
  60. // Releases the memory allocated for HTreeGroup.
  61. void VP8LHtreeGroupsFree(HTreeGroup* htree_groups, int num_htree_groups);
  62. // Builds Huffman tree assuming code lengths are implicitly in symbol order.
  63. // The 'huff_codes' and 'code_lengths' are pre-allocated temporary memory
  64. // buffers, used for creating the huffman tree.
  65. // Returns false in case of error (invalid tree or memory error).
  66. int VP8LHuffmanTreeBuildImplicit(HuffmanTree* const tree,
  67. const int* const code_lengths,
  68. int* const huff_codes,
  69. int code_lengths_size);
  70. // Build a Huffman tree with explicitly given lists of code lengths, codes
  71. // and symbols. Verifies that all symbols added are smaller than max_symbol.
  72. // Returns false in case of an invalid symbol, invalid tree or memory error.
  73. int VP8LHuffmanTreeBuildExplicit(HuffmanTree* const tree,
  74. const int* const code_lengths,
  75. const int* const codes,
  76. const int* const symbols, int max_symbol,
  77. int num_symbols);
  78. // Utility: converts Huffman code lengths to corresponding Huffman codes.
  79. // 'huff_codes' should be pre-allocated.
  80. // Returns false in case of error (memory allocation, invalid codes).
  81. int VP8LHuffmanCodeLengthsToCodes(const int* const code_lengths,
  82. int code_lengths_size, int* const huff_codes);
  83. #ifdef __cplusplus
  84. } // extern "C"
  85. #endif
  86. #endif // WEBP_UTILS_HUFFMAN_H_