1 /* Copyright 2010 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7 /* Entropy encoding (Huffman) utilities. */
9 #ifndef BROTLI_ENC_ENTROPY_ENCODE_H_
10 #define BROTLI_ENC_ENTROPY_ENCODE_H_
12 #include "../common/types.h"
15 #if defined(__cplusplus) || defined(c_plusplus)
19 /* A node of a Huffman tree. */
20 typedef struct HuffmanTree
{
21 uint32_t total_count_
;
23 int16_t index_right_or_value_
;
26 static BROTLI_INLINE
void InitHuffmanTree(HuffmanTree
* self
, uint32_t count
,
27 int16_t left
, int16_t right
) {
28 self
->total_count_
= count
;
29 self
->index_left_
= left
;
30 self
->index_right_or_value_
= right
;
33 /* Returns 1 is assignment of depths succeded, otherwise 0. */
34 BROTLI_INTERNAL BROTLI_BOOL
BrotliSetDepth(
35 int p
, HuffmanTree
* pool
, uint8_t* depth
, int max_depth
);
37 /* This function will create a Huffman tree.
39 The (data,length) contains the population counts.
40 The tree_limit is the maximum bit depth of the Huffman codes.
42 The depth contains the tree, i.e., how many bits are used for
45 The actual Huffman tree is constructed in the tree[] array, which has to
46 be at least 2 * length + 1 long.
48 See http://en.wikipedia.org/wiki/Huffman_coding */
49 BROTLI_INTERNAL
void BrotliCreateHuffmanTree(const uint32_t *data
,
55 /* Change the population counts in a way that the consequent
56 Huffman tree compression, especially its rle-part will be more
57 likely to compress this data more efficiently.
59 length contains the size of the histogram.
60 counts contains the population counts.
61 good_for_rle is a buffer of at least length size */
62 BROTLI_INTERNAL
void BrotliOptimizeHuffmanCountsForRle(
63 size_t length
, uint32_t* counts
, uint8_t* good_for_rle
);
65 /* Write a Huffman tree from bit depths into the bitstream representation
66 of a Huffman tree. The generated Huffman tree is to be compressed once
67 more using a Huffman tree */
68 BROTLI_INTERNAL
void BrotliWriteHuffmanTree(const uint8_t* depth
,
72 uint8_t* extra_bits_data
);
74 /* Get the actual bit values for a tree of bit depths. */
75 BROTLI_INTERNAL
void BrotliConvertBitDepthsToSymbols(const uint8_t *depth
,
79 /* Input size optimized Shell sort. */
80 typedef BROTLI_BOOL (*HuffmanTreeComparator
)(
81 const HuffmanTree
*, const HuffmanTree
*);
82 static BROTLI_INLINE
void SortHuffmanTreeItems(HuffmanTree
* items
,
83 const size_t n
, HuffmanTreeComparator comparator
) {
84 static const size_t gaps
[] = {132, 57, 23, 10, 4, 1};
88 for (i
= 1; i
< n
; ++i
) {
89 HuffmanTree tmp
= items
[i
];
92 while (comparator(&tmp
, &items
[j
])) {
102 int g
= n
< 57 ? 2 : 0;
104 size_t gap
= gaps
[g
];
106 for (i
= gap
; i
< n
; ++i
) {
108 HuffmanTree tmp
= items
[i
];
109 for (; j
>= gap
&& comparator(&tmp
, &items
[j
- gap
]); j
-= gap
) {
110 items
[j
] = items
[j
- gap
];
118 #if defined(__cplusplus) || defined(c_plusplus)
122 #endif /* BROTLI_ENC_ENTROPY_ENCODE_H_ */