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