/src/libjxl/lib/jxl/enc_huffman.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #include "lib/jxl/enc_huffman.h" |
7 | | |
8 | | #include <algorithm> |
9 | | #include <cstddef> |
10 | | #include <cstdint> |
11 | | #include <memory> |
12 | | |
13 | | #include "lib/jxl/base/status.h" |
14 | | #include "lib/jxl/enc_bit_writer.h" |
15 | | #include "lib/jxl/enc_huffman_tree.h" |
16 | | |
17 | | namespace jxl { |
18 | | |
19 | | namespace { |
20 | | |
21 | | constexpr int kCodeLengthCodes = 18; |
22 | | |
23 | | void StoreHuffmanTreeOfHuffmanTreeToBitMask(const int num_codes, |
24 | | const uint8_t* code_length_bitdepth, |
25 | 1.05k | BitWriter* writer) { |
26 | 1.05k | static const uint8_t kStorageOrder[kCodeLengthCodes] = { |
27 | 1.05k | 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
28 | | // The bit lengths of the Huffman code over the code length alphabet |
29 | | // are compressed with the following static Huffman code: |
30 | | // Symbol Code |
31 | | // ------ ---- |
32 | | // 0 00 |
33 | | // 1 1110 |
34 | | // 2 110 |
35 | | // 3 01 |
36 | | // 4 10 |
37 | | // 5 1111 |
38 | 1.05k | static const uint8_t kHuffmanBitLengthHuffmanCodeSymbols[6] = {0, 7, 3, |
39 | 1.05k | 2, 1, 15}; |
40 | 1.05k | static const uint8_t kHuffmanBitLengthHuffmanCodeBitLengths[6] = {2, 4, 3, |
41 | 1.05k | 2, 2, 4}; |
42 | | |
43 | | // Throw away trailing zeros: |
44 | 1.05k | size_t codes_to_store = kCodeLengthCodes; |
45 | 1.05k | if (num_codes > 1) { |
46 | 14.9k | for (; codes_to_store > 0; --codes_to_store) { |
47 | 14.9k | if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) { |
48 | 1.04k | break; |
49 | 1.04k | } |
50 | 14.9k | } |
51 | 1.04k | } |
52 | 1.05k | size_t skip_some = 0; // skips none. |
53 | 1.05k | if (code_length_bitdepth[kStorageOrder[0]] == 0 && |
54 | 1.05k | code_length_bitdepth[kStorageOrder[1]] == 0) { |
55 | 68 | skip_some = 2; // skips two. |
56 | 68 | if (code_length_bitdepth[kStorageOrder[2]] == 0) { |
57 | 0 | skip_some = 3; // skips three. |
58 | 0 | } |
59 | 68 | } |
60 | 1.05k | writer->Write(2, skip_some); |
61 | 5.99k | for (size_t i = skip_some; i < codes_to_store; ++i) { |
62 | 4.93k | size_t l = code_length_bitdepth[kStorageOrder[i]]; |
63 | 4.93k | writer->Write(kHuffmanBitLengthHuffmanCodeBitLengths[l], |
64 | 4.93k | kHuffmanBitLengthHuffmanCodeSymbols[l]); |
65 | 4.93k | } |
66 | 1.05k | } |
67 | | |
68 | | Status StoreHuffmanTreeToBitMask(const size_t huffman_tree_size, |
69 | | const uint8_t* huffman_tree, |
70 | | const uint8_t* huffman_tree_extra_bits, |
71 | | const uint8_t* code_length_bitdepth, |
72 | | const uint16_t* code_length_bitdepth_symbols, |
73 | 1.05k | BitWriter* writer) { |
74 | 9.75k | for (size_t i = 0; i < huffman_tree_size; ++i) { |
75 | 8.69k | size_t ix = huffman_tree[i]; |
76 | 8.69k | writer->Write(code_length_bitdepth[ix], code_length_bitdepth_symbols[ix]); |
77 | 8.69k | JXL_ENSURE(ix <= 17); |
78 | | // Extra bits |
79 | 8.69k | switch (ix) { |
80 | 0 | case 16: |
81 | 0 | writer->Write(2, huffman_tree_extra_bits[i]); |
82 | 0 | break; |
83 | 33 | case 17: |
84 | 33 | writer->Write(3, huffman_tree_extra_bits[i]); |
85 | 33 | break; |
86 | 8.66k | default: |
87 | | // no-op |
88 | 8.66k | break; |
89 | 8.69k | } |
90 | 8.69k | } |
91 | 1.05k | return true; |
92 | 1.05k | } |
93 | | |
94 | | void StoreSimpleHuffmanTree(const uint8_t* depths, size_t symbols[4], |
95 | | size_t num_symbols, size_t max_bits, |
96 | 1.19k | BitWriter* writer) { |
97 | | // value of 1 indicates a simple Huffman code |
98 | 1.19k | writer->Write(2, 1); |
99 | 1.19k | writer->Write(2, num_symbols - 1); // NSYM - 1 |
100 | | |
101 | | // Sort |
102 | 4.49k | for (size_t i = 0; i < num_symbols; i++) { |
103 | 6.41k | for (size_t j = i + 1; j < num_symbols; j++) { |
104 | 3.11k | if (depths[symbols[j]] < depths[symbols[i]]) { |
105 | 311 | std::swap(symbols[j], symbols[i]); |
106 | 311 | } |
107 | 3.11k | } |
108 | 3.29k | } |
109 | | |
110 | 1.19k | if (num_symbols == 2) { |
111 | 382 | writer->Write(max_bits, symbols[0]); |
112 | 382 | writer->Write(max_bits, symbols[1]); |
113 | 811 | } else if (num_symbols == 3) { |
114 | 710 | writer->Write(max_bits, symbols[0]); |
115 | 710 | writer->Write(max_bits, symbols[1]); |
116 | 710 | writer->Write(max_bits, symbols[2]); |
117 | 710 | } else { |
118 | 101 | writer->Write(max_bits, symbols[0]); |
119 | 101 | writer->Write(max_bits, symbols[1]); |
120 | 101 | writer->Write(max_bits, symbols[2]); |
121 | 101 | writer->Write(max_bits, symbols[3]); |
122 | | // tree-select |
123 | 101 | writer->Write(1, depths[symbols[0]] == 1 ? 1 : 0); |
124 | 101 | } |
125 | 1.19k | } |
126 | | |
127 | | // num = alphabet size |
128 | | // depths = symbol depths |
129 | 1.05k | Status StoreHuffmanTree(const uint8_t* depths, size_t num, BitWriter* writer) { |
130 | | // Write the Huffman tree into the compact representation. |
131 | 1.05k | std::unique_ptr<uint8_t[]> arena(new uint8_t[2 * num]); |
132 | 1.05k | uint8_t* huffman_tree = arena.get(); |
133 | 1.05k | uint8_t* huffman_tree_extra_bits = arena.get() + num; |
134 | 1.05k | size_t huffman_tree_size = 0; |
135 | 1.05k | WriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree, |
136 | 1.05k | huffman_tree_extra_bits); |
137 | | |
138 | | // Calculate the statistics of the Huffman tree in the compact representation. |
139 | 1.05k | uint32_t huffman_tree_histogram[kCodeLengthCodes] = {0}; |
140 | 9.75k | for (size_t i = 0; i < huffman_tree_size; ++i) { |
141 | 8.69k | ++huffman_tree_histogram[huffman_tree[i]]; |
142 | 8.69k | } |
143 | | |
144 | 1.05k | int num_codes = 0; |
145 | 1.05k | int code = 0; |
146 | 4.29k | for (int i = 0; i < kCodeLengthCodes; ++i) { |
147 | 4.28k | if (huffman_tree_histogram[i]) { |
148 | 2.09k | if (num_codes == 0) { |
149 | 1.05k | code = i; |
150 | 1.05k | num_codes = 1; |
151 | 1.05k | } else if (num_codes == 1) { |
152 | 1.04k | num_codes = 2; |
153 | 1.04k | break; |
154 | 1.04k | } |
155 | 2.09k | } |
156 | 4.28k | } |
157 | | |
158 | | // Calculate another Huffman tree to use for compressing both the |
159 | | // earlier Huffman tree with. |
160 | 1.05k | uint8_t code_length_bitdepth[kCodeLengthCodes] = {0}; |
161 | 1.05k | uint16_t code_length_bitdepth_symbols[kCodeLengthCodes] = {0}; |
162 | 1.05k | CreateHuffmanTree(&huffman_tree_histogram[0], kCodeLengthCodes, 5, |
163 | 1.05k | &code_length_bitdepth[0]); |
164 | 1.05k | ConvertBitDepthsToSymbols(code_length_bitdepth, kCodeLengthCodes, |
165 | 1.05k | &code_length_bitdepth_symbols[0]); |
166 | | |
167 | | // Now, we have all the data, let's start storing it |
168 | 1.05k | StoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth, |
169 | 1.05k | writer); |
170 | | |
171 | 1.05k | if (num_codes == 1) { |
172 | 13 | code_length_bitdepth[code] = 0; |
173 | 13 | } |
174 | | |
175 | | // Store the real huffman tree now. |
176 | 1.05k | JXL_RETURN_IF_ERROR(StoreHuffmanTreeToBitMask( |
177 | 1.05k | huffman_tree_size, huffman_tree, huffman_tree_extra_bits, |
178 | 1.05k | &code_length_bitdepth[0], code_length_bitdepth_symbols, writer)); |
179 | 1.05k | return true; |
180 | 1.05k | } |
181 | | |
182 | | } // namespace |
183 | | |
184 | | Status BuildAndStoreHuffmanTree(const uint32_t* histogram, const size_t length, |
185 | | uint8_t* depth, uint16_t* bits, |
186 | 2.24k | BitWriter* writer) { |
187 | 2.24k | size_t count = 0; |
188 | 2.24k | size_t s4[4] = {0}; |
189 | 11.0k | for (size_t i = 0; i < length; i++) { |
190 | 9.78k | if (histogram[i]) { |
191 | 9.57k | if (count < 4) { |
192 | 7.51k | s4[count] = i; |
193 | 7.51k | } else if (count > 4) { |
194 | 1.00k | break; |
195 | 1.00k | } |
196 | 8.57k | count++; |
197 | 8.57k | } |
198 | 9.78k | } |
199 | | |
200 | 2.24k | size_t max_bits_counter = length - 1; |
201 | 2.24k | size_t max_bits = 0; |
202 | 7.82k | while (max_bits_counter) { |
203 | 5.57k | max_bits_counter >>= 1; |
204 | 5.57k | ++max_bits; |
205 | 5.57k | } |
206 | | |
207 | 2.24k | if (count <= 1) { |
208 | | // Output symbol bits and depths are initialized with 0, nothing to do. |
209 | 0 | writer->Write(4, 1); |
210 | 0 | writer->Write(max_bits, s4[0]); |
211 | 0 | return true; |
212 | 0 | } |
213 | | |
214 | 2.24k | CreateHuffmanTree(histogram, length, 15, depth); |
215 | 2.24k | ConvertBitDepthsToSymbols(depth, length, bits); |
216 | | |
217 | 2.24k | if (count <= 4) { |
218 | 1.19k | StoreSimpleHuffmanTree(depth, s4, count, max_bits, writer); |
219 | 1.19k | } else { |
220 | 1.05k | JXL_RETURN_IF_ERROR(StoreHuffmanTree(depth, length, writer)); |
221 | 1.05k | } |
222 | 2.24k | return true; |
223 | 2.24k | } |
224 | | |
225 | | } // namespace jxl |