/src/ghostpdl/brotli/c/enc/entropy_encode.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
27 | | static BROTLI_INLINE void InitHuffmanTree(HuffmanTree* self, uint32_t count, |
28 | 0 | int16_t left, int16_t right) { |
29 | 0 | self->total_count_ = count; |
30 | 0 | self->index_left_ = left; |
31 | 0 | self->index_right_or_value_ = right; |
32 | 0 | } Unexecuted instantiation: encode.c:InitHuffmanTree Unexecuted instantiation: metablock.c:InitHuffmanTree Unexecuted instantiation: brotli_bit_stream.c:InitHuffmanTree Unexecuted instantiation: compress_fragment.c:InitHuffmanTree Unexecuted instantiation: compress_fragment_two_pass.c:InitHuffmanTree Unexecuted instantiation: entropy_encode.c:InitHuffmanTree |
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*); |
84 | | static BROTLI_INLINE void SortHuffmanTreeItems(HuffmanTree* items, |
85 | 0 | const size_t n, HuffmanTreeComparator comparator) { |
86 | 0 | if (n < 13) { |
87 | | /* Insertion sort. */ |
88 | 0 | size_t i; |
89 | 0 | for (i = 1; i < n; ++i) { |
90 | 0 | HuffmanTree tmp = items[i]; |
91 | 0 | size_t k = i; |
92 | 0 | size_t j = i - 1; |
93 | 0 | while (comparator(&tmp, &items[j])) { |
94 | 0 | items[k] = items[j]; |
95 | 0 | k = j; |
96 | 0 | if (!j--) break; |
97 | 0 | } |
98 | 0 | items[k] = tmp; |
99 | 0 | } |
100 | 0 | return; |
101 | 0 | } else { |
102 | | /* Shell sort. */ |
103 | 0 | int g = n < 57 ? 2 : 0; |
104 | 0 | for (; g < 6; ++g) { |
105 | 0 | size_t gap = kBrotliShellGaps[g]; |
106 | 0 | size_t i; |
107 | 0 | for (i = gap; i < n; ++i) { |
108 | 0 | size_t j = i; |
109 | 0 | HuffmanTree tmp = items[i]; |
110 | 0 | for (; j >= gap && comparator(&tmp, &items[j - gap]); j -= gap) { |
111 | 0 | items[j] = items[j - gap]; |
112 | 0 | } |
113 | 0 | items[j] = tmp; |
114 | 0 | } |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } Unexecuted instantiation: encode.c:SortHuffmanTreeItems Unexecuted instantiation: metablock.c:SortHuffmanTreeItems Unexecuted instantiation: brotli_bit_stream.c:SortHuffmanTreeItems Unexecuted instantiation: compress_fragment.c:SortHuffmanTreeItems Unexecuted instantiation: compress_fragment_two_pass.c:SortHuffmanTreeItems Unexecuted instantiation: entropy_encode.c:SortHuffmanTreeItems |
118 | | |
119 | | #if defined(__cplusplus) || defined(c_plusplus) |
120 | | } /* extern "C" */ |
121 | | #endif |
122 | | |
123 | | #endif /* BROTLI_ENC_ENTROPY_ENCODE_H_ */ |