entropy_encode.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2010 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. /* Entropy encoding (Huffman) utilities. */
  6. #ifndef BROTLI_ENC_ENTROPY_ENCODE_H_
  7. #define BROTLI_ENC_ENTROPY_ENCODE_H_
  8. #include "../common/platform.h"
  9. #include <brotli/types.h>
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. /* A node of a Huffman tree. */
  14. typedef struct HuffmanTree {
  15. uint32_t total_count_;
  16. int16_t index_left_;
  17. int16_t index_right_or_value_;
  18. } HuffmanTree;
  19. static BROTLI_INLINE void InitHuffmanTree(HuffmanTree* self, uint32_t count,
  20. int16_t left, int16_t right) {
  21. self->total_count_ = count;
  22. self->index_left_ = left;
  23. self->index_right_or_value_ = right;
  24. }
  25. /* Returns 1 is assignment of depths succeeded, otherwise 0. */
  26. BROTLI_INTERNAL BROTLI_BOOL BrotliSetDepth(
  27. int p, HuffmanTree* pool, uint8_t* depth, int max_depth);
  28. /* This function will create a Huffman tree.
  29. The (data,length) contains the population counts.
  30. The tree_limit is the maximum bit depth of the Huffman codes.
  31. The depth contains the tree, i.e., how many bits are used for
  32. the symbol.
  33. The actual Huffman tree is constructed in the tree[] array, which has to
  34. be at least 2 * length + 1 long.
  35. See http://en.wikipedia.org/wiki/Huffman_coding */
  36. BROTLI_INTERNAL void BrotliCreateHuffmanTree(const uint32_t* data,
  37. const size_t length,
  38. const int tree_limit,
  39. HuffmanTree* tree,
  40. uint8_t* depth);
  41. /* Change the population counts in a way that the consequent
  42. Huffman tree compression, especially its RLE-part will be more
  43. likely to compress this data more efficiently.
  44. length contains the size of the histogram.
  45. counts contains the population counts.
  46. good_for_rle is a buffer of at least length size */
  47. BROTLI_INTERNAL void BrotliOptimizeHuffmanCountsForRle(
  48. size_t length, uint32_t* counts, uint8_t* good_for_rle);
  49. /* Write a Huffman tree from bit depths into the bit-stream representation
  50. of a Huffman tree. The generated Huffman tree is to be compressed once
  51. more using a Huffman tree */
  52. BROTLI_INTERNAL void BrotliWriteHuffmanTree(const uint8_t* depth,
  53. size_t num,
  54. size_t* tree_size,
  55. uint8_t* tree,
  56. uint8_t* extra_bits_data);
  57. /* Get the actual bit values for a tree of bit depths. */
  58. BROTLI_INTERNAL void BrotliConvertBitDepthsToSymbols(const uint8_t* depth,
  59. size_t len,
  60. uint16_t* bits);
  61. /* Input size optimized Shell sort. */
  62. typedef BROTLI_BOOL (*HuffmanTreeComparator)(
  63. const HuffmanTree*, const HuffmanTree*);
  64. static BROTLI_INLINE void SortHuffmanTreeItems(HuffmanTree* items,
  65. const size_t n, HuffmanTreeComparator comparator) {
  66. static const size_t gaps[] = {132, 57, 23, 10, 4, 1};
  67. if (n < 13) {
  68. /* Insertion sort. */
  69. size_t i;
  70. for (i = 1; i < n; ++i) {
  71. HuffmanTree tmp = items[i];
  72. size_t k = i;
  73. size_t j = i - 1;
  74. while (comparator(&tmp, &items[j])) {
  75. items[k] = items[j];
  76. k = j;
  77. if (!j--) break;
  78. }
  79. items[k] = tmp;
  80. }
  81. return;
  82. } else {
  83. /* Shell sort. */
  84. int g = n < 57 ? 2 : 0;
  85. for (; g < 6; ++g) {
  86. size_t gap = gaps[g];
  87. size_t i;
  88. for (i = gap; i < n; ++i) {
  89. size_t j = i;
  90. HuffmanTree tmp = items[i];
  91. for (; j >= gap && comparator(&tmp, &items[j - gap]); j -= gap) {
  92. items[j] = items[j - gap];
  93. }
  94. items[j] = tmp;
  95. }
  96. }
  97. }
  98. }
  99. #if defined(__cplusplus) || defined(c_plusplus)
  100. } /* extern "C" */
  101. #endif
  102. #endif /* BROTLI_ENC_ENTROPY_ENCODE_H_ */