huffman_utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_UTILS_H_
  14. #define WEBP_UTILS_HUFFMAN_UTILS_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. #define HUFFMAN_TABLE_BITS 8
  22. #define HUFFMAN_TABLE_MASK ((1 << HUFFMAN_TABLE_BITS) - 1)
  23. #define LENGTHS_TABLE_BITS 7
  24. #define LENGTHS_TABLE_MASK ((1 << LENGTHS_TABLE_BITS) - 1)
  25. // Huffman lookup table entry
  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. // long version for holding 32b values
  31. typedef struct {
  32. int bits; // number of bits used for this symbol,
  33. // or an impossible value if not a literal code.
  34. uint32_t value; // 32b packed ARGB value if literal,
  35. // or non-literal symbol otherwise
  36. } HuffmanCode32;
  37. #define HUFFMAN_PACKED_BITS 6
  38. #define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)
  39. // Huffman table group.
  40. // Includes special handling for the following cases:
  41. // - is_trivial_literal: one common literal base for RED/BLUE/ALPHA (not GREEN)
  42. // - is_trivial_code: only 1 code (no bit is read from bitstream)
  43. // - use_packed_table: few enough literal symbols, so all the bit codes
  44. // can fit into a small look-up table packed_table[]
  45. // The common literal base, if applicable, is stored in 'literal_arb'.
  46. typedef struct HTreeGroup HTreeGroup;
  47. struct HTreeGroup {
  48. HuffmanCode* htrees[HUFFMAN_CODES_PER_META_CODE];
  49. int is_trivial_literal; // True, if huffman trees for Red, Blue & Alpha
  50. // Symbols are trivial (have a single code).
  51. uint32_t literal_arb; // If is_trivial_literal is true, this is the
  52. // ARGB value of the pixel, with Green channel
  53. // being set to zero.
  54. int is_trivial_code; // true if is_trivial_literal with only one code
  55. int use_packed_table; // use packed table below for short literal code
  56. // table mapping input bits to a packed values, or escape case to literal code
  57. HuffmanCode32 packed_table[HUFFMAN_PACKED_TABLE_SIZE];
  58. };
  59. // Creates the instance of HTreeGroup with specified number of tree-groups.
  60. HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);
  61. // Releases the memory allocated for HTreeGroup.
  62. void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);
  63. // Builds Huffman lookup table assuming code lengths are in symbol order.
  64. // The 'code_lengths' is pre-allocated temporary memory buffer used for creating
  65. // the huffman table.
  66. // Returns built table size or 0 in case of error (invalid tree or
  67. // memory error).
  68. // If root_table is NULL, it returns 0 if a lookup cannot be built, something
  69. // > 0 otherwise (but not the table size).
  70. int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
  71. const int code_lengths[], int code_lengths_size);
  72. #ifdef __cplusplus
  73. } // extern "C"
  74. #endif
  75. #endif // WEBP_UTILS_HUFFMAN_UTILS_H_