huffman_encode_utils.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2011 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. // Author: Jyrki Alakuijala (jyrki@google.com)
  11. //
  12. // Entropy encoding (Huffman) for webp lossless
  13. #ifndef WEBP_UTILS_HUFFMAN_ENCODE_UTILS_H_
  14. #define WEBP_UTILS_HUFFMAN_ENCODE_UTILS_H_
  15. #include "../webp/types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // Struct for holding the tree header in coded form.
  20. typedef struct {
  21. uint8_t code; // value (0..15) or escape code (16,17,18)
  22. uint8_t extra_bits; // extra bits for escape codes
  23. } HuffmanTreeToken;
  24. // Struct to represent the tree codes (depth and bits array).
  25. typedef struct {
  26. int num_symbols; // Number of symbols.
  27. uint8_t* code_lengths; // Code lengths of the symbols.
  28. uint16_t* codes; // Symbol Codes.
  29. } HuffmanTreeCode;
  30. // Struct to represent the Huffman tree.
  31. typedef struct {
  32. uint32_t total_count_; // Symbol frequency.
  33. int value_; // Symbol value.
  34. int pool_index_left_; // Index for the left sub-tree.
  35. int pool_index_right_; // Index for the right sub-tree.
  36. } HuffmanTree;
  37. // Turn the Huffman tree into a token sequence.
  38. // Returns the number of tokens used.
  39. int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree,
  40. HuffmanTreeToken* tokens, int max_tokens);
  41. // Create an optimized tree, and tokenize it.
  42. // 'buf_rle' and 'huff_tree' are pre-allocated and the 'tree' is the constructed
  43. // huffman code tree.
  44. void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit,
  45. uint8_t* const buf_rle, HuffmanTree* const huff_tree,
  46. HuffmanTreeCode* const huff_code);
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #endif // WEBP_UTILS_HUFFMAN_ENCODE_UTILS_H_