/src/libjxl/lib/jxl/enc_ans.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_ans.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | #include <jxl/types.h> |
10 | | |
11 | | #include <algorithm> |
12 | | #include <cmath> |
13 | | #include <cstdint> |
14 | | #include <limits> |
15 | | #include <unordered_map> |
16 | | #include <utility> |
17 | | #include <vector> |
18 | | |
19 | | #include "lib/jxl/ans_common.h" |
20 | | #include "lib/jxl/base/bits.h" |
21 | | #include "lib/jxl/base/fast_math-inl.h" |
22 | | #include "lib/jxl/base/status.h" |
23 | | #include "lib/jxl/dec_ans.h" |
24 | | #include "lib/jxl/enc_ans_params.h" |
25 | | #include "lib/jxl/enc_aux_out.h" |
26 | | #include "lib/jxl/enc_cluster.h" |
27 | | #include "lib/jxl/enc_context_map.h" |
28 | | #include "lib/jxl/enc_fields.h" |
29 | | #include "lib/jxl/enc_huffman.h" |
30 | | #include "lib/jxl/enc_params.h" |
31 | | #include "lib/jxl/fields.h" |
32 | | |
33 | | namespace jxl { |
34 | | |
35 | | namespace { |
36 | | |
37 | | #if (!JXL_IS_DEBUG_BUILD) |
38 | | constexpr |
39 | | #endif |
40 | | bool ans_fuzzer_friendly_ = false; |
41 | | |
42 | | const int kMaxNumSymbolsForSmallCode = 4; |
43 | | |
44 | | void ANSBuildInfoTable(const ANSHistBin* counts, const AliasTable::Entry* table, |
45 | | size_t alphabet_size, size_t log_alpha_size, |
46 | 0 | ANSEncSymbolInfo* info) { |
47 | 0 | size_t log_entry_size = ANS_LOG_TAB_SIZE - log_alpha_size; |
48 | 0 | size_t entry_size_minus_1 = (1 << log_entry_size) - 1; |
49 | | // create valid alias table for empty streams. |
50 | 0 | for (size_t s = 0; s < std::max<size_t>(1, alphabet_size); ++s) { |
51 | 0 | const ANSHistBin freq = s == alphabet_size ? ANS_TAB_SIZE : counts[s]; |
52 | 0 | info[s].freq_ = static_cast<uint16_t>(freq); |
53 | 0 | #ifdef USE_MULT_BY_RECIPROCAL |
54 | 0 | if (freq != 0) { |
55 | 0 | info[s].ifreq_ = |
56 | 0 | ((1ull << RECIPROCAL_PRECISION) + info[s].freq_ - 1) / info[s].freq_; |
57 | 0 | } else { |
58 | 0 | info[s].ifreq_ = 1; // shouldn't matter (symbol shouldn't occur), but... |
59 | 0 | } |
60 | 0 | #endif |
61 | 0 | info[s].reverse_map_.resize(freq); |
62 | 0 | } |
63 | 0 | for (int i = 0; i < ANS_TAB_SIZE; i++) { |
64 | 0 | AliasTable::Symbol s = |
65 | 0 | AliasTable::Lookup(table, i, log_entry_size, entry_size_minus_1); |
66 | 0 | info[s.value].reverse_map_[s.offset] = i; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | float EstimateDataBits(const ANSHistBin* histogram, const ANSHistBin* counts, |
71 | 0 | size_t len) { |
72 | 0 | float sum = 0.0f; |
73 | 0 | int total_histogram = 0; |
74 | 0 | int total_counts = 0; |
75 | 0 | for (size_t i = 0; i < len; ++i) { |
76 | 0 | total_histogram += histogram[i]; |
77 | 0 | total_counts += counts[i]; |
78 | 0 | if (histogram[i] > 0) { |
79 | 0 | JXL_DASSERT(counts[i] > 0); |
80 | | // += histogram[i] * -log(counts[i]/total_counts) |
81 | 0 | sum += histogram[i] * |
82 | 0 | std::max(0.0f, ANS_LOG_TAB_SIZE - FastLog2f(counts[i])); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | if (total_histogram > 0) { |
86 | | // Used only in assert. |
87 | 0 | (void)total_counts; |
88 | 0 | JXL_DASSERT(total_counts == ANS_TAB_SIZE); |
89 | 0 | } |
90 | 0 | return sum; |
91 | 0 | } |
92 | | |
93 | 0 | float EstimateDataBitsFlat(const ANSHistBin* histogram, size_t len) { |
94 | 0 | const float flat_bits = std::max(FastLog2f(len), 0.0f); |
95 | 0 | float total_histogram = 0; |
96 | 0 | for (size_t i = 0; i < len; ++i) { |
97 | 0 | total_histogram += histogram[i]; |
98 | 0 | } |
99 | 0 | return total_histogram * flat_bits; |
100 | 0 | } |
101 | | |
102 | | // Static Huffman code for encoding logcounts. The last symbol is used as RLE |
103 | | // sequence. |
104 | | const uint8_t kLogCountBitLengths[ANS_LOG_TAB_SIZE + 2] = { |
105 | | 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 6, 7, 7, |
106 | | }; |
107 | | const uint8_t kLogCountSymbols[ANS_LOG_TAB_SIZE + 2] = { |
108 | | 17, 11, 15, 3, 9, 7, 4, 2, 5, 6, 0, 33, 1, 65, |
109 | | }; |
110 | | |
111 | | // Returns the difference between largest count that can be represented and is |
112 | | // smaller than "count" and smallest representable count larger than "count". |
113 | 0 | int SmallestIncrement(uint32_t count, uint32_t shift) { |
114 | 0 | int bits = count == 0 ? -1 : FloorLog2Nonzero(count); |
115 | 0 | int drop_bits = bits - GetPopulationCountPrecision(bits, shift); |
116 | 0 | return drop_bits < 0 ? 1 : (1 << drop_bits); |
117 | 0 | } |
118 | | |
119 | | template <bool minimize_error_of_sum> |
120 | | bool RebalanceHistogram(const float* targets, int max_symbol, int table_size, |
121 | 0 | uint32_t shift, int* omit_pos, ANSHistBin* counts) { |
122 | 0 | int sum = 0; |
123 | 0 | float sum_nonrounded = 0.0; |
124 | 0 | int remainder_pos = 0; // if all of them are handled in first loop |
125 | 0 | int remainder_log = -1; |
126 | 0 | for (int n = 0; n < max_symbol; ++n) { |
127 | 0 | if (targets[n] > 0 && targets[n] < 1.0f) { |
128 | 0 | counts[n] = 1; |
129 | 0 | sum_nonrounded += targets[n]; |
130 | 0 | sum += counts[n]; |
131 | 0 | } |
132 | 0 | } |
133 | 0 | const float discount_ratio = |
134 | 0 | (table_size - sum) / (table_size - sum_nonrounded); |
135 | 0 | JXL_ENSURE(discount_ratio > 0); |
136 | 0 | JXL_ENSURE(discount_ratio <= 1.0f); |
137 | | // Invariant for minimize_error_of_sum == true: |
138 | | // abs(sum - sum_nonrounded) |
139 | | // <= SmallestIncrement(max(targets[])) + max_symbol |
140 | 0 | for (int n = 0; n < max_symbol; ++n) { |
141 | 0 | if (targets[n] >= 1.0f) { |
142 | 0 | sum_nonrounded += targets[n]; |
143 | 0 | counts[n] = |
144 | 0 | static_cast<ANSHistBin>(targets[n] * discount_ratio); // truncate |
145 | 0 | if (counts[n] == 0) counts[n] = 1; |
146 | 0 | if (counts[n] == table_size) counts[n] = table_size - 1; |
147 | | // Round the count to the closest nonzero multiple of SmallestIncrement |
148 | | // (when minimize_error_of_sum is false) or one of two closest so as to |
149 | | // keep the sum as close as possible to sum_nonrounded. |
150 | 0 | int inc = SmallestIncrement(counts[n], shift); |
151 | 0 | counts[n] -= counts[n] & (inc - 1); |
152 | | // TODO(robryk): Should we rescale targets[n]? |
153 | 0 | const int target = minimize_error_of_sum |
154 | 0 | ? (static_cast<int>(sum_nonrounded) - sum) |
155 | 0 | : static_cast<int>(targets[n]); |
156 | 0 | if (counts[n] == 0 || |
157 | 0 | (target >= counts[n] + inc / 2 && counts[n] + inc < table_size)) { |
158 | 0 | counts[n] += inc; |
159 | 0 | } |
160 | 0 | sum += counts[n]; |
161 | 0 | const int count_log = FloorLog2Nonzero(static_cast<uint32_t>(counts[n])); |
162 | 0 | if (count_log > remainder_log) { |
163 | 0 | remainder_pos = n; |
164 | 0 | remainder_log = count_log; |
165 | 0 | } |
166 | 0 | } |
167 | 0 | } |
168 | 0 | JXL_ENSURE(remainder_pos != -1); |
169 | | // NOTE: This is the only place where counts could go negative. We could |
170 | | // detect that, return false and make ANSHistBin uint32_t. |
171 | 0 | counts[remainder_pos] -= sum - table_size; |
172 | 0 | *omit_pos = remainder_pos; |
173 | 0 | return counts[remainder_pos] > 0; |
174 | 0 | } Unexecuted instantiation: enc_ans.cc:bool jxl::(anonymous namespace)::RebalanceHistogram<false>(float const*, int, int, unsigned int, int*, int*) Unexecuted instantiation: enc_ans.cc:bool jxl::(anonymous namespace)::RebalanceHistogram<true>(float const*, int, int, unsigned int, int*, int*) |
175 | | |
176 | | Status NormalizeCounts(ANSHistBin* counts, int* omit_pos, const int length, |
177 | | const int precision_bits, uint32_t shift, |
178 | 0 | int* num_symbols, int* symbols) { |
179 | 0 | const int32_t table_size = 1 << precision_bits; // target sum / table size |
180 | 0 | uint64_t total = 0; |
181 | 0 | int max_symbol = 0; |
182 | 0 | int symbol_count = 0; |
183 | 0 | for (int n = 0; n < length; ++n) { |
184 | 0 | total += counts[n]; |
185 | 0 | if (counts[n] > 0) { |
186 | 0 | if (symbol_count < kMaxNumSymbolsForSmallCode) { |
187 | 0 | symbols[symbol_count] = n; |
188 | 0 | } |
189 | 0 | ++symbol_count; |
190 | 0 | max_symbol = n + 1; |
191 | 0 | } |
192 | 0 | } |
193 | 0 | *num_symbols = symbol_count; |
194 | 0 | if (symbol_count == 0) { |
195 | 0 | return true; |
196 | 0 | } |
197 | 0 | if (symbol_count == 1) { |
198 | 0 | counts[symbols[0]] = table_size; |
199 | 0 | return true; |
200 | 0 | } |
201 | 0 | if (symbol_count > table_size) |
202 | 0 | return JXL_FAILURE("Too many entries in an ANS histogram"); |
203 | | |
204 | 0 | const float norm = 1.f * table_size / total; |
205 | 0 | std::vector<float> targets(max_symbol); |
206 | 0 | for (size_t n = 0; n < targets.size(); ++n) { |
207 | 0 | targets[n] = norm * counts[n]; |
208 | 0 | } |
209 | 0 | if (!RebalanceHistogram<false>(targets.data(), max_symbol, table_size, shift, |
210 | 0 | omit_pos, counts)) { |
211 | | // Use an alternative rebalancing mechanism if the one above failed |
212 | | // to create a histogram that is positive wherever the original one was. |
213 | 0 | if (!RebalanceHistogram<true>(targets.data(), max_symbol, table_size, shift, |
214 | 0 | omit_pos, counts)) { |
215 | 0 | return JXL_FAILURE("Logic error: couldn't rebalance a histogram"); |
216 | 0 | } |
217 | 0 | } |
218 | 0 | return true; |
219 | 0 | } |
220 | | |
221 | | struct SizeWriter { |
222 | | size_t size = 0; |
223 | 0 | void Write(size_t num, size_t bits) { size += num; } |
224 | | }; |
225 | | |
226 | | template <typename Writer> |
227 | 0 | void StoreVarLenUint8(size_t n, Writer* writer) { |
228 | 0 | JXL_DASSERT(n <= 255); |
229 | 0 | if (n == 0) { |
230 | 0 | writer->Write(1, 0); |
231 | 0 | } else { |
232 | 0 | writer->Write(1, 1); |
233 | 0 | size_t nbits = FloorLog2Nonzero(n); |
234 | 0 | writer->Write(3, nbits); |
235 | 0 | writer->Write(nbits, n - (1ULL << nbits)); |
236 | 0 | } |
237 | 0 | } Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::StoreVarLenUint8<jxl::(anonymous namespace)::SizeWriter>(unsigned long, jxl::(anonymous namespace)::SizeWriter*) Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::StoreVarLenUint8<jxl::BitWriter>(unsigned long, jxl::BitWriter*) |
238 | | |
239 | | template <typename Writer> |
240 | 0 | void StoreVarLenUint16(size_t n, Writer* writer) { |
241 | 0 | JXL_DASSERT(n <= 65535); |
242 | 0 | if (n == 0) { |
243 | 0 | writer->Write(1, 0); |
244 | 0 | } else { |
245 | 0 | writer->Write(1, 1); |
246 | 0 | size_t nbits = FloorLog2Nonzero(n); |
247 | 0 | writer->Write(4, nbits); |
248 | 0 | writer->Write(nbits, n - (1ULL << nbits)); |
249 | 0 | } |
250 | 0 | } Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::StoreVarLenUint16<jxl::BitWriter>(unsigned long, jxl::BitWriter*) Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::StoreVarLenUint16<jxl::(anonymous namespace)::SizeWriter>(unsigned long, jxl::(anonymous namespace)::SizeWriter*) |
251 | | |
252 | | template <typename Writer> |
253 | | bool EncodeCounts(const ANSHistBin* counts, const int alphabet_size, |
254 | | const int omit_pos, const int num_symbols, uint32_t shift, |
255 | 0 | const int* symbols, Writer* writer) { |
256 | 0 | bool ok = true; |
257 | 0 | if (num_symbols <= 2) { |
258 | | // Small tree marker to encode 1-2 symbols. |
259 | 0 | writer->Write(1, 1); |
260 | 0 | if (num_symbols == 0) { |
261 | 0 | writer->Write(1, 0); |
262 | 0 | StoreVarLenUint8(0, writer); |
263 | 0 | } else { |
264 | 0 | writer->Write(1, num_symbols - 1); |
265 | 0 | for (int i = 0; i < num_symbols; ++i) { |
266 | 0 | StoreVarLenUint8(symbols[i], writer); |
267 | 0 | } |
268 | 0 | } |
269 | 0 | if (num_symbols == 2) { |
270 | 0 | writer->Write(ANS_LOG_TAB_SIZE, counts[symbols[0]]); |
271 | 0 | } |
272 | 0 | } else { |
273 | | // Mark non-small tree. |
274 | 0 | writer->Write(1, 0); |
275 | | // Mark non-flat histogram. |
276 | 0 | writer->Write(1, 0); |
277 | | |
278 | | // Precompute sequences for RLE encoding. Contains the number of identical |
279 | | // values starting at a given index. Only contains the value at the first |
280 | | // element of the series. |
281 | 0 | std::vector<uint32_t> same(alphabet_size, 0); |
282 | 0 | int last = 0; |
283 | 0 | for (int i = 1; i < alphabet_size; i++) { |
284 | | // Store the sequence length once different symbol reached, or we're at |
285 | | // the end, or the length is longer than we can encode, or we are at |
286 | | // the omit_pos. We don't support including the omit_pos in an RLE |
287 | | // sequence because this value may use a different amount of log2 bits |
288 | | // than standard, it is too complex to handle in the decoder. |
289 | 0 | if (counts[i] != counts[last] || i + 1 == alphabet_size || |
290 | 0 | (i - last) >= 255 || i == omit_pos || i == omit_pos + 1) { |
291 | 0 | same[last] = (i - last); |
292 | 0 | last = i + 1; |
293 | 0 | } |
294 | 0 | } |
295 | |
|
296 | 0 | int length = 0; |
297 | 0 | std::vector<int> logcounts(alphabet_size); |
298 | 0 | int omit_log = 0; |
299 | 0 | for (int i = 0; i < alphabet_size; ++i) { |
300 | 0 | JXL_ENSURE(counts[i] <= ANS_TAB_SIZE); |
301 | 0 | JXL_ENSURE(counts[i] >= 0); |
302 | 0 | if (i == omit_pos) { |
303 | 0 | length = i + 1; |
304 | 0 | } else if (counts[i] > 0) { |
305 | 0 | logcounts[i] = FloorLog2Nonzero(static_cast<uint32_t>(counts[i])) + 1; |
306 | 0 | length = i + 1; |
307 | 0 | if (i < omit_pos) { |
308 | 0 | omit_log = std::max(omit_log, logcounts[i] + 1); |
309 | 0 | } else { |
310 | 0 | omit_log = std::max(omit_log, logcounts[i]); |
311 | 0 | } |
312 | 0 | } |
313 | 0 | } |
314 | 0 | logcounts[omit_pos] = omit_log; |
315 | | |
316 | | // Elias gamma-like code for shift. Only difference is that if the number |
317 | | // of bits to be encoded is equal to FloorLog2(ANS_LOG_TAB_SIZE+1), we skip |
318 | | // the terminating 0 in unary coding. |
319 | 0 | int upper_bound_log = FloorLog2Nonzero(ANS_LOG_TAB_SIZE + 1); |
320 | 0 | int log = FloorLog2Nonzero(shift + 1); |
321 | 0 | writer->Write(log, (1 << log) - 1); |
322 | 0 | if (log != upper_bound_log) writer->Write(1, 0); |
323 | 0 | writer->Write(log, ((1 << log) - 1) & (shift + 1)); |
324 | | |
325 | | // Since num_symbols >= 3, we know that length >= 3, therefore we encode |
326 | | // length - 3. |
327 | 0 | if (length - 3 > 255) { |
328 | | // Pretend that everything is OK, but complain about correctness later. |
329 | 0 | StoreVarLenUint8(255, writer); |
330 | 0 | ok = false; |
331 | 0 | } else { |
332 | 0 | StoreVarLenUint8(length - 3, writer); |
333 | 0 | } |
334 | | |
335 | | // The logcount values are encoded with a static Huffman code. |
336 | 0 | static const size_t kMinReps = 4; |
337 | 0 | size_t rep = ANS_LOG_TAB_SIZE + 1; |
338 | 0 | for (int i = 0; i < length; ++i) { |
339 | 0 | if (i > 0 && same[i - 1] > kMinReps) { |
340 | | // Encode the RLE symbol and skip the repeated ones. |
341 | 0 | writer->Write(kLogCountBitLengths[rep], kLogCountSymbols[rep]); |
342 | 0 | StoreVarLenUint8(same[i - 1] - kMinReps - 1, writer); |
343 | 0 | i += same[i - 1] - 2; |
344 | 0 | continue; |
345 | 0 | } |
346 | 0 | writer->Write(kLogCountBitLengths[logcounts[i]], |
347 | 0 | kLogCountSymbols[logcounts[i]]); |
348 | 0 | } |
349 | 0 | for (int i = 0; i < length; ++i) { |
350 | 0 | if (i > 0 && same[i - 1] > kMinReps) { |
351 | | // Skip symbols encoded by RLE. |
352 | 0 | i += same[i - 1] - 2; |
353 | 0 | continue; |
354 | 0 | } |
355 | 0 | if (logcounts[i] > 1 && i != omit_pos) { |
356 | 0 | int bitcount = GetPopulationCountPrecision(logcounts[i] - 1, shift); |
357 | 0 | int drop_bits = logcounts[i] - 1 - bitcount; |
358 | 0 | JXL_ENSURE((counts[i] & ((1 << drop_bits) - 1)) == 0); |
359 | 0 | writer->Write(bitcount, (counts[i] >> drop_bits) - (1 << bitcount)); |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | 0 | return ok; |
364 | 0 | } Unexecuted instantiation: enc_ans.cc:bool jxl::(anonymous namespace)::EncodeCounts<jxl::(anonymous namespace)::SizeWriter>(int const*, int, int, int, unsigned int, int const*, jxl::(anonymous namespace)::SizeWriter*) Unexecuted instantiation: enc_ans.cc:bool jxl::(anonymous namespace)::EncodeCounts<jxl::BitWriter>(int const*, int, int, int, unsigned int, int const*, jxl::BitWriter*) |
365 | | |
366 | 0 | void EncodeFlatHistogram(const int alphabet_size, BitWriter* writer) { |
367 | | // Mark non-small tree. |
368 | 0 | writer->Write(1, 0); |
369 | | // Mark uniform histogram. |
370 | 0 | writer->Write(1, 1); |
371 | 0 | JXL_DASSERT(alphabet_size > 0); |
372 | | // Encode alphabet size. |
373 | 0 | StoreVarLenUint8(alphabet_size - 1, writer); |
374 | 0 | } |
375 | | |
376 | | StatusOr<float> ComputeHistoAndDataCost(const ANSHistBin* histogram, |
377 | 0 | size_t alphabet_size, uint32_t method) { |
378 | 0 | if (method == 0) { // Flat code |
379 | 0 | return ANS_LOG_TAB_SIZE + 2 + |
380 | 0 | EstimateDataBitsFlat(histogram, alphabet_size); |
381 | 0 | } |
382 | | // Non-flat: shift = method-1. |
383 | 0 | uint32_t shift = method - 1; |
384 | 0 | std::vector<ANSHistBin> counts(histogram, histogram + alphabet_size); |
385 | 0 | int omit_pos = 0; |
386 | 0 | int num_symbols; |
387 | 0 | int symbols[kMaxNumSymbolsForSmallCode] = {}; |
388 | 0 | JXL_RETURN_IF_ERROR(NormalizeCounts(counts.data(), &omit_pos, alphabet_size, |
389 | 0 | ANS_LOG_TAB_SIZE, shift, &num_symbols, |
390 | 0 | symbols)); |
391 | 0 | SizeWriter writer; |
392 | | // Ignore the correctness, no real encoding happens at this stage. |
393 | 0 | (void)EncodeCounts(counts.data(), alphabet_size, omit_pos, num_symbols, shift, |
394 | 0 | symbols, &writer); |
395 | 0 | return writer.size + |
396 | 0 | EstimateDataBits(histogram, counts.data(), alphabet_size); |
397 | 0 | } |
398 | | |
399 | | StatusOr<uint32_t> ComputeBestMethod( |
400 | | const ANSHistBin* histogram, size_t alphabet_size, float* cost, |
401 | 0 | HistogramParams::ANSHistogramStrategy ans_histogram_strategy) { |
402 | 0 | uint32_t method = 0; |
403 | 0 | JXL_ASSIGN_OR_RETURN(float fcost, |
404 | 0 | ComputeHistoAndDataCost(histogram, alphabet_size, 0)); |
405 | 0 | auto try_shift = [&](size_t shift) -> Status { |
406 | 0 | JXL_ASSIGN_OR_RETURN( |
407 | 0 | float c, ComputeHistoAndDataCost(histogram, alphabet_size, shift + 1)); |
408 | 0 | if (c < fcost) { |
409 | 0 | method = shift + 1; |
410 | 0 | fcost = c; |
411 | 0 | } |
412 | 0 | return true; |
413 | 0 | }; |
414 | 0 | switch (ans_histogram_strategy) { |
415 | 0 | case HistogramParams::ANSHistogramStrategy::kPrecise: { |
416 | 0 | for (uint32_t shift = 0; shift <= ANS_LOG_TAB_SIZE; shift++) { |
417 | 0 | JXL_RETURN_IF_ERROR(try_shift(shift)); |
418 | 0 | } |
419 | 0 | break; |
420 | 0 | } |
421 | 0 | case HistogramParams::ANSHistogramStrategy::kApproximate: { |
422 | 0 | for (uint32_t shift = 0; shift <= ANS_LOG_TAB_SIZE; shift += 2) { |
423 | 0 | JXL_RETURN_IF_ERROR(try_shift(shift)); |
424 | 0 | } |
425 | 0 | break; |
426 | 0 | } |
427 | 0 | case HistogramParams::ANSHistogramStrategy::kFast: { |
428 | 0 | JXL_RETURN_IF_ERROR(try_shift(0)); |
429 | 0 | JXL_RETURN_IF_ERROR(try_shift(ANS_LOG_TAB_SIZE / 2)); |
430 | 0 | JXL_RETURN_IF_ERROR(try_shift(ANS_LOG_TAB_SIZE)); |
431 | 0 | break; |
432 | 0 | } |
433 | 0 | }; |
434 | 0 | *cost = fcost; |
435 | 0 | return method; |
436 | 0 | } |
437 | | |
438 | | } // namespace |
439 | | |
440 | | // Returns an estimate of the cost of encoding this histogram and the |
441 | | // corresponding data. |
442 | | StatusOr<size_t> BuildAndStoreANSEncodingData( |
443 | | JxlMemoryManager* memory_manager, |
444 | | HistogramParams::ANSHistogramStrategy ans_histogram_strategy, |
445 | | const ANSHistBin* histogram, size_t alphabet_size, size_t log_alpha_size, |
446 | 0 | bool use_prefix_code, ANSEncSymbolInfo* info, BitWriter* writer) { |
447 | 0 | if (use_prefix_code) { |
448 | 0 | size_t cost = 0; |
449 | 0 | if (alphabet_size <= 1) return 0; |
450 | 0 | std::vector<uint32_t> histo(alphabet_size); |
451 | 0 | for (size_t i = 0; i < alphabet_size; i++) { |
452 | 0 | histo[i] = histogram[i]; |
453 | 0 | JXL_ENSURE(histogram[i] >= 0); |
454 | 0 | } |
455 | 0 | { |
456 | 0 | std::vector<uint8_t> depths(alphabet_size); |
457 | 0 | std::vector<uint16_t> bits(alphabet_size); |
458 | 0 | if (writer == nullptr) { |
459 | 0 | BitWriter tmp_writer{memory_manager}; |
460 | 0 | JXL_RETURN_IF_ERROR(tmp_writer.WithMaxBits( |
461 | 0 | 8 * alphabet_size + 8, // safe upper bound |
462 | 0 | LayerType::Header, /*aux_out=*/nullptr, [&] { |
463 | 0 | return BuildAndStoreHuffmanTree(histo.data(), alphabet_size, |
464 | 0 | depths.data(), bits.data(), |
465 | 0 | &tmp_writer); |
466 | 0 | })); |
467 | 0 | cost = tmp_writer.BitsWritten(); |
468 | 0 | } else { |
469 | 0 | size_t start = writer->BitsWritten(); |
470 | 0 | JXL_RETURN_IF_ERROR(BuildAndStoreHuffmanTree( |
471 | 0 | histo.data(), alphabet_size, depths.data(), bits.data(), writer)); |
472 | 0 | cost = writer->BitsWritten() - start; |
473 | 0 | } |
474 | 0 | for (size_t i = 0; i < alphabet_size; i++) { |
475 | 0 | info[i].bits = depths[i] == 0 ? 0 : bits[i]; |
476 | 0 | info[i].depth = depths[i]; |
477 | 0 | } |
478 | 0 | } |
479 | | // Estimate data cost. |
480 | 0 | for (size_t i = 0; i < alphabet_size; i++) { |
481 | 0 | cost += histogram[i] * info[i].depth; |
482 | 0 | } |
483 | 0 | return cost; |
484 | 0 | } |
485 | 0 | JXL_ENSURE(alphabet_size <= ANS_TAB_SIZE); |
486 | 0 | float fcost; |
487 | 0 | JXL_ASSIGN_OR_RETURN(uint32_t method, |
488 | 0 | ComputeBestMethod(histogram, alphabet_size, &fcost, |
489 | 0 | ans_histogram_strategy)); |
490 | 0 | JXL_ENSURE(fcost >= 0); |
491 | 0 | int num_symbols; |
492 | 0 | int symbols[kMaxNumSymbolsForSmallCode] = {}; |
493 | 0 | std::vector<ANSHistBin> counts(histogram, histogram + alphabet_size); |
494 | 0 | if (!counts.empty()) { |
495 | 0 | size_t sum = 0; |
496 | 0 | for (int count : counts) { |
497 | 0 | sum += count; |
498 | 0 | } |
499 | 0 | if (sum == 0) { |
500 | 0 | counts[0] = ANS_TAB_SIZE; |
501 | 0 | } |
502 | 0 | } |
503 | 0 | int omit_pos = 0; |
504 | 0 | uint32_t shift = method - 1; |
505 | 0 | if (method == 0) { |
506 | 0 | JXL_ENSURE(alphabet_size > 0); |
507 | 0 | counts = CreateFlatHistogram(alphabet_size, ANS_TAB_SIZE); |
508 | 0 | } else { |
509 | 0 | JXL_RETURN_IF_ERROR(NormalizeCounts(counts.data(), &omit_pos, alphabet_size, |
510 | 0 | ANS_LOG_TAB_SIZE, shift, &num_symbols, |
511 | 0 | symbols)); |
512 | 0 | } |
513 | 0 | AliasTable::Entry a[ANS_MAX_ALPHABET_SIZE]; |
514 | 0 | JXL_RETURN_IF_ERROR( |
515 | 0 | InitAliasTable(counts, ANS_LOG_TAB_SIZE, log_alpha_size, a)); |
516 | 0 | ANSBuildInfoTable(counts.data(), a, alphabet_size, log_alpha_size, info); |
517 | 0 | if (writer != nullptr) { |
518 | 0 | if (method == 0) { |
519 | 0 | JXL_ENSURE(alphabet_size > 0); |
520 | 0 | EncodeFlatHistogram(alphabet_size, writer); |
521 | 0 | } else { |
522 | 0 | if (!EncodeCounts(counts.data(), alphabet_size, omit_pos, num_symbols, |
523 | 0 | method - 1, symbols, writer)) { |
524 | 0 | return JXL_FAILURE("EncodeCounts failed"); |
525 | 0 | } |
526 | 0 | } |
527 | 0 | } |
528 | 0 | return static_cast<size_t>(fcost); |
529 | 0 | } |
530 | | |
531 | | StatusOr<float> ANSPopulationCost(const ANSHistBin* data, |
532 | 0 | size_t alphabet_size) { |
533 | 0 | float cost = 0.0f; |
534 | 0 | if (ANS_MAX_ALPHABET_SIZE < alphabet_size) { |
535 | 0 | return std::numeric_limits<float>::max(); |
536 | 0 | } |
537 | 0 | JXL_ASSIGN_OR_RETURN( |
538 | 0 | uint32_t method, |
539 | 0 | ComputeBestMethod(data, alphabet_size, &cost, |
540 | 0 | HistogramParams::ANSHistogramStrategy::kFast)); |
541 | 0 | (void)method; |
542 | 0 | return cost; |
543 | 0 | } |
544 | | |
545 | | template <typename Writer> |
546 | | void EncodeUintConfig(const HybridUintConfig uint_config, Writer* writer, |
547 | 0 | size_t log_alpha_size) { |
548 | 0 | writer->Write(CeilLog2Nonzero(log_alpha_size + 1), |
549 | 0 | uint_config.split_exponent); |
550 | 0 | if (uint_config.split_exponent == log_alpha_size) { |
551 | 0 | return; // msb/lsb don't matter. |
552 | 0 | } |
553 | 0 | size_t nbits = CeilLog2Nonzero(uint_config.split_exponent + 1); |
554 | 0 | writer->Write(nbits, uint_config.msb_in_token); |
555 | 0 | nbits = CeilLog2Nonzero(uint_config.split_exponent - |
556 | 0 | uint_config.msb_in_token + 1); |
557 | 0 | writer->Write(nbits, uint_config.lsb_in_token); |
558 | 0 | } Unexecuted instantiation: enc_ans.cc:void jxl::EncodeUintConfig<jxl::(anonymous namespace)::SizeWriter>(jxl::HybridUintConfig, jxl::(anonymous namespace)::SizeWriter*, unsigned long) Unexecuted instantiation: void jxl::EncodeUintConfig<jxl::BitWriter>(jxl::HybridUintConfig, jxl::BitWriter*, unsigned long) |
559 | | template <typename Writer> |
560 | | void EncodeUintConfigs(const std::vector<HybridUintConfig>& uint_config, |
561 | 0 | Writer* writer, size_t log_alpha_size) { |
562 | | // TODO(veluca): RLE? |
563 | 0 | for (const auto& cfg : uint_config) { |
564 | 0 | EncodeUintConfig(cfg, writer, log_alpha_size); |
565 | 0 | } |
566 | 0 | } Unexecuted instantiation: void jxl::EncodeUintConfigs<jxl::BitWriter>(std::__1::vector<jxl::HybridUintConfig, std::__1::allocator<jxl::HybridUintConfig> > const&, jxl::BitWriter*, unsigned long) Unexecuted instantiation: enc_ans.cc:void jxl::EncodeUintConfigs<jxl::(anonymous namespace)::SizeWriter>(std::__1::vector<jxl::HybridUintConfig, std::__1::allocator<jxl::HybridUintConfig> > const&, jxl::(anonymous namespace)::SizeWriter*, unsigned long) |
567 | | template void EncodeUintConfigs(const std::vector<HybridUintConfig>&, |
568 | | BitWriter*, size_t); |
569 | | |
570 | | namespace { |
571 | | |
572 | | Status ChooseUintConfigs(const HistogramParams& params, |
573 | | const std::vector<std::vector<Token>>& tokens, |
574 | | const std::vector<uint8_t>& context_map, |
575 | | std::vector<Histogram>* clustered_histograms, |
576 | 0 | EntropyEncodingData* codes, size_t* log_alpha_size) { |
577 | 0 | codes->uint_config.resize(clustered_histograms->size()); |
578 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::kNone) { |
579 | 0 | return true; |
580 | 0 | } |
581 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::k000) { |
582 | 0 | codes->uint_config.clear(); |
583 | 0 | codes->uint_config.resize(clustered_histograms->size(), |
584 | 0 | HybridUintConfig(0, 0, 0)); |
585 | 0 | return true; |
586 | 0 | } |
587 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::kContextMap) { |
588 | 0 | codes->uint_config.clear(); |
589 | 0 | codes->uint_config.resize(clustered_histograms->size(), |
590 | 0 | HybridUintConfig(2, 0, 1)); |
591 | 0 | return true; |
592 | 0 | } |
593 | | |
594 | | // If the uint config is adaptive, just stick with the default in streaming |
595 | | // mode. |
596 | 0 | if (params.streaming_mode) { |
597 | 0 | return true; |
598 | 0 | } |
599 | | |
600 | | // Brute-force method that tries a few options. |
601 | 0 | std::vector<HybridUintConfig> configs; |
602 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::kBest) { |
603 | 0 | configs = { |
604 | 0 | HybridUintConfig(4, 2, 0), // default |
605 | 0 | HybridUintConfig(4, 1, 0), // less precise |
606 | 0 | HybridUintConfig(4, 2, 1), // add sign |
607 | 0 | HybridUintConfig(4, 2, 2), // add sign+parity |
608 | 0 | HybridUintConfig(4, 1, 2), // add parity but less msb |
609 | | // Same as above, but more direct coding. |
610 | 0 | HybridUintConfig(5, 2, 0), HybridUintConfig(5, 1, 0), |
611 | 0 | HybridUintConfig(5, 2, 1), HybridUintConfig(5, 2, 2), |
612 | 0 | HybridUintConfig(5, 1, 2), |
613 | | // Same as above, but less direct coding. |
614 | 0 | HybridUintConfig(3, 2, 0), HybridUintConfig(3, 1, 0), |
615 | 0 | HybridUintConfig(3, 2, 1), HybridUintConfig(3, 1, 2), |
616 | | // For near-lossless. |
617 | 0 | HybridUintConfig(4, 1, 3), HybridUintConfig(5, 1, 4), |
618 | 0 | HybridUintConfig(5, 2, 3), HybridUintConfig(6, 1, 5), |
619 | 0 | HybridUintConfig(6, 2, 4), HybridUintConfig(6, 0, 0), |
620 | | // Other |
621 | 0 | HybridUintConfig(0, 0, 0), // varlenuint |
622 | 0 | HybridUintConfig(2, 0, 1), // works well for ctx map |
623 | 0 | HybridUintConfig(7, 0, 0), // direct coding |
624 | 0 | HybridUintConfig(8, 0, 0), // direct coding |
625 | 0 | HybridUintConfig(9, 0, 0), // direct coding |
626 | 0 | HybridUintConfig(10, 0, 0), // direct coding |
627 | 0 | HybridUintConfig(11, 0, 0), // direct coding |
628 | 0 | HybridUintConfig(12, 0, 0), // direct coding |
629 | 0 | }; |
630 | 0 | } else if (params.uint_method == HistogramParams::HybridUintMethod::kFast) { |
631 | 0 | configs = { |
632 | 0 | HybridUintConfig(4, 2, 0), // default |
633 | 0 | HybridUintConfig(4, 1, 2), // add parity but less msb |
634 | 0 | HybridUintConfig(0, 0, 0), // smallest histograms |
635 | 0 | HybridUintConfig(2, 0, 1), // works well for ctx map |
636 | 0 | }; |
637 | 0 | } |
638 | |
|
639 | 0 | std::vector<float> costs(clustered_histograms->size(), |
640 | 0 | std::numeric_limits<float>::max()); |
641 | 0 | std::vector<uint32_t> extra_bits(clustered_histograms->size()); |
642 | 0 | std::vector<uint8_t> is_valid(clustered_histograms->size()); |
643 | 0 | size_t max_alpha = |
644 | 0 | codes->use_prefix_code ? PREFIX_MAX_ALPHABET_SIZE : ANS_MAX_ALPHABET_SIZE; |
645 | 0 | for (HybridUintConfig cfg : configs) { |
646 | 0 | std::fill(is_valid.begin(), is_valid.end(), true); |
647 | 0 | std::fill(extra_bits.begin(), extra_bits.end(), 0); |
648 | |
|
649 | 0 | for (auto& histo : *clustered_histograms) { |
650 | 0 | histo.Clear(); |
651 | 0 | } |
652 | 0 | for (const auto& stream : tokens) { |
653 | 0 | for (const auto& token : stream) { |
654 | | // TODO(veluca): do not ignore lz77 commands. |
655 | 0 | if (token.is_lz77_length) continue; |
656 | 0 | size_t histo = context_map[token.context]; |
657 | 0 | uint32_t tok, nbits, bits; |
658 | 0 | cfg.Encode(token.value, &tok, &nbits, &bits); |
659 | 0 | if (tok >= max_alpha || |
660 | 0 | (codes->lz77.enabled && tok >= codes->lz77.min_symbol)) { |
661 | 0 | is_valid[histo] = JXL_FALSE; |
662 | 0 | continue; |
663 | 0 | } |
664 | 0 | extra_bits[histo] += nbits; |
665 | 0 | (*clustered_histograms)[histo].Add(tok); |
666 | 0 | } |
667 | 0 | } |
668 | |
|
669 | 0 | for (size_t i = 0; i < clustered_histograms->size(); i++) { |
670 | 0 | if (!is_valid[i]) continue; |
671 | 0 | JXL_ASSIGN_OR_RETURN(float cost, |
672 | 0 | (*clustered_histograms)[i].PopulationCost()); |
673 | 0 | cost += extra_bits[i]; |
674 | | // add signaling cost of the hybriduintconfig itself |
675 | 0 | cost += CeilLog2Nonzero(cfg.split_exponent + 1); |
676 | 0 | cost += CeilLog2Nonzero(cfg.split_exponent - cfg.msb_in_token + 1); |
677 | 0 | if (cost < costs[i]) { |
678 | 0 | codes->uint_config[i] = cfg; |
679 | 0 | costs[i] = cost; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | } |
683 | | |
684 | | // Rebuild histograms. |
685 | 0 | for (auto& histo : *clustered_histograms) { |
686 | 0 | histo.Clear(); |
687 | 0 | } |
688 | 0 | *log_alpha_size = 4; |
689 | 0 | for (const auto& stream : tokens) { |
690 | 0 | for (const auto& token : stream) { |
691 | 0 | uint32_t tok, nbits, bits; |
692 | 0 | size_t histo = context_map[token.context]; |
693 | 0 | (token.is_lz77_length ? codes->lz77.length_uint_config |
694 | 0 | : codes->uint_config[histo]) |
695 | 0 | .Encode(token.value, &tok, &nbits, &bits); |
696 | 0 | tok += token.is_lz77_length ? codes->lz77.min_symbol : 0; |
697 | 0 | (*clustered_histograms)[histo].Add(tok); |
698 | 0 | while (tok >= (1u << *log_alpha_size)) (*log_alpha_size)++; |
699 | 0 | } |
700 | 0 | } |
701 | 0 | size_t max_log_alpha_size = codes->use_prefix_code ? PREFIX_MAX_BITS : 8; |
702 | 0 | JXL_ENSURE(*log_alpha_size <= max_log_alpha_size); |
703 | 0 | return true; |
704 | 0 | } |
705 | | |
706 | | Histogram HistogramFromSymbolInfo( |
707 | 0 | const std::vector<ANSEncSymbolInfo>& encoding_info, bool use_prefix_code) { |
708 | 0 | Histogram histo; |
709 | 0 | histo.data_.resize(DivCeil(encoding_info.size(), Histogram::kRounding) * |
710 | 0 | Histogram::kRounding); |
711 | 0 | histo.total_count_ = 0; |
712 | 0 | for (size_t i = 0; i < encoding_info.size(); ++i) { |
713 | 0 | const ANSEncSymbolInfo& info = encoding_info[i]; |
714 | 0 | int count = use_prefix_code |
715 | 0 | ? (info.depth ? (1u << (PREFIX_MAX_BITS - info.depth)) : 0) |
716 | 0 | : info.freq_; |
717 | 0 | histo.data_[i] = count; |
718 | 0 | histo.total_count_ += count; |
719 | 0 | } |
720 | 0 | return histo; |
721 | 0 | } |
722 | | |
723 | | class HistogramBuilder { |
724 | | public: |
725 | | explicit HistogramBuilder(const size_t num_contexts) |
726 | 0 | : histograms_(num_contexts) {} |
727 | | |
728 | 0 | void VisitSymbol(int symbol, size_t histo_idx) { |
729 | 0 | JXL_DASSERT(histo_idx < histograms_.size()); |
730 | 0 | histograms_[histo_idx].Add(symbol); |
731 | 0 | } |
732 | | |
733 | | // NOTE: `layer` is only for clustered_entropy; caller does ReclaimAndCharge. |
734 | | // Returns cost (in bits). |
735 | | StatusOr<size_t> BuildAndStoreEntropyCodes( |
736 | | JxlMemoryManager* memory_manager, const HistogramParams& params, |
737 | | const std::vector<std::vector<Token>>& tokens, EntropyEncodingData* codes, |
738 | | std::vector<uint8_t>* context_map, BitWriter* writer, LayerType layer, |
739 | 0 | AuxOut* aux_out) const { |
740 | 0 | const size_t prev_histograms = codes->encoding_info.size(); |
741 | 0 | std::vector<Histogram> clustered_histograms; |
742 | 0 | for (size_t i = 0; i < prev_histograms; ++i) { |
743 | 0 | clustered_histograms.push_back(HistogramFromSymbolInfo( |
744 | 0 | codes->encoding_info[i], codes->use_prefix_code)); |
745 | 0 | } |
746 | 0 | size_t context_offset = context_map->size(); |
747 | 0 | context_map->resize(context_offset + histograms_.size()); |
748 | 0 | if (histograms_.size() > 1) { |
749 | 0 | if (!ans_fuzzer_friendly_) { |
750 | 0 | std::vector<uint32_t> histogram_symbols; |
751 | 0 | JXL_RETURN_IF_ERROR( |
752 | 0 | ClusterHistograms(params, histograms_, kClustersLimit, |
753 | 0 | &clustered_histograms, &histogram_symbols)); |
754 | 0 | for (size_t c = 0; c < histograms_.size(); ++c) { |
755 | 0 | (*context_map)[context_offset + c] = |
756 | 0 | static_cast<uint8_t>(histogram_symbols[c]); |
757 | 0 | } |
758 | 0 | } else { |
759 | 0 | JXL_ENSURE(codes->encoding_info.empty()); |
760 | 0 | fill(context_map->begin(), context_map->end(), 0); |
761 | 0 | size_t max_symbol = 0; |
762 | 0 | for (const Histogram& h : histograms_) { |
763 | 0 | max_symbol = std::max(h.data_.size(), max_symbol); |
764 | 0 | } |
765 | 0 | size_t num_symbols = 1 << CeilLog2Nonzero(max_symbol + 1); |
766 | 0 | clustered_histograms.resize(1); |
767 | 0 | clustered_histograms[0].Clear(); |
768 | 0 | for (size_t i = 0; i < num_symbols; i++) { |
769 | 0 | clustered_histograms[0].Add(i); |
770 | 0 | } |
771 | 0 | } |
772 | 0 | if (writer != nullptr) { |
773 | 0 | JXL_RETURN_IF_ERROR(EncodeContextMap( |
774 | 0 | *context_map, clustered_histograms.size(), writer, layer, aux_out)); |
775 | 0 | } |
776 | 0 | } else { |
777 | 0 | JXL_ENSURE(codes->encoding_info.empty()); |
778 | 0 | clustered_histograms.push_back(histograms_[0]); |
779 | 0 | } |
780 | 0 | if (aux_out != nullptr) { |
781 | 0 | for (size_t i = prev_histograms; i < clustered_histograms.size(); ++i) { |
782 | 0 | aux_out->layer(layer).clustered_entropy += |
783 | 0 | clustered_histograms[i].ShannonEntropy(); |
784 | 0 | } |
785 | 0 | } |
786 | 0 | size_t log_alpha_size = codes->lz77.enabled ? 8 : 7; // Sane default. |
787 | 0 | if (ans_fuzzer_friendly_) { |
788 | 0 | codes->uint_config.clear(); |
789 | 0 | codes->uint_config.resize(1, HybridUintConfig(7, 0, 0)); |
790 | 0 | } else { |
791 | 0 | JXL_RETURN_IF_ERROR(ChooseUintConfigs(params, tokens, *context_map, |
792 | 0 | &clustered_histograms, codes, |
793 | 0 | &log_alpha_size)); |
794 | 0 | } |
795 | 0 | if (log_alpha_size < 5) log_alpha_size = 5; |
796 | 0 | if (params.streaming_mode) { |
797 | | // TODO(szabadka) Figure out if we can use lower values here. |
798 | 0 | log_alpha_size = 8; |
799 | 0 | } |
800 | 0 | SizeWriter size_writer; // Used if writer == nullptr to estimate costs. |
801 | 0 | size_t cost = 1; |
802 | 0 | if (writer) writer->Write(1, TO_JXL_BOOL(codes->use_prefix_code)); |
803 | |
|
804 | 0 | if (codes->use_prefix_code) { |
805 | 0 | log_alpha_size = PREFIX_MAX_BITS; |
806 | 0 | } else { |
807 | 0 | cost += 2; |
808 | 0 | } |
809 | 0 | if (writer == nullptr) { |
810 | 0 | EncodeUintConfigs(codes->uint_config, &size_writer, log_alpha_size); |
811 | 0 | } else { |
812 | 0 | if (!codes->use_prefix_code) writer->Write(2, log_alpha_size - 5); |
813 | 0 | EncodeUintConfigs(codes->uint_config, writer, log_alpha_size); |
814 | 0 | } |
815 | 0 | if (codes->use_prefix_code) { |
816 | 0 | for (const auto& histo : clustered_histograms) { |
817 | 0 | size_t alphabet_size = histo.alphabet_size(); |
818 | 0 | if (writer) { |
819 | 0 | StoreVarLenUint16(alphabet_size - 1, writer); |
820 | 0 | } else { |
821 | 0 | StoreVarLenUint16(alphabet_size - 1, &size_writer); |
822 | 0 | } |
823 | 0 | } |
824 | 0 | } |
825 | 0 | cost += size_writer.size; |
826 | 0 | for (size_t c = prev_histograms; c < clustered_histograms.size(); ++c) { |
827 | 0 | size_t alphabet_size = clustered_histograms[c].alphabet_size(); |
828 | 0 | codes->encoding_info.emplace_back(); |
829 | 0 | codes->encoding_info.back().resize(alphabet_size); |
830 | 0 | BitWriter* histo_writer = writer; |
831 | 0 | if (params.streaming_mode) { |
832 | 0 | codes->encoded_histograms.emplace_back(memory_manager); |
833 | 0 | histo_writer = &codes->encoded_histograms.back(); |
834 | 0 | } |
835 | 0 | const auto& body = [&]() -> Status { |
836 | 0 | JXL_ASSIGN_OR_RETURN( |
837 | 0 | size_t ans_cost, |
838 | 0 | BuildAndStoreANSEncodingData( |
839 | 0 | memory_manager, params.ans_histogram_strategy, |
840 | 0 | clustered_histograms[c].data_.data(), alphabet_size, |
841 | 0 | log_alpha_size, codes->use_prefix_code, |
842 | 0 | codes->encoding_info.back().data(), histo_writer)); |
843 | 0 | cost += ans_cost; |
844 | 0 | return true; |
845 | 0 | }; |
846 | 0 | if (histo_writer) { |
847 | 0 | JXL_RETURN_IF_ERROR(histo_writer->WithMaxBits( |
848 | 0 | 256 + alphabet_size * 24, layer, aux_out, body, |
849 | 0 | /*finished_histogram=*/true)); |
850 | 0 | } else { |
851 | 0 | JXL_RETURN_IF_ERROR(body()); |
852 | 0 | } |
853 | 0 | if (params.streaming_mode) { |
854 | 0 | JXL_RETURN_IF_ERROR(writer->AppendUnaligned(*histo_writer)); |
855 | 0 | } |
856 | 0 | } |
857 | 0 | return cost; |
858 | 0 | } |
859 | | |
860 | 0 | const Histogram& Histo(size_t i) const { return histograms_[i]; } |
861 | | |
862 | | private: |
863 | | std::vector<Histogram> histograms_; |
864 | | }; |
865 | | |
866 | | class SymbolCostEstimator { |
867 | | public: |
868 | | SymbolCostEstimator(size_t num_contexts, bool force_huffman, |
869 | | const std::vector<std::vector<Token>>& tokens, |
870 | 0 | const LZ77Params& lz77) { |
871 | 0 | HistogramBuilder builder(num_contexts); |
872 | | // Build histograms for estimating lz77 savings. |
873 | 0 | HybridUintConfig uint_config; |
874 | 0 | for (const auto& stream : tokens) { |
875 | 0 | for (const auto& token : stream) { |
876 | 0 | uint32_t tok, nbits, bits; |
877 | 0 | (token.is_lz77_length ? lz77.length_uint_config : uint_config) |
878 | 0 | .Encode(token.value, &tok, &nbits, &bits); |
879 | 0 | tok += token.is_lz77_length ? lz77.min_symbol : 0; |
880 | 0 | builder.VisitSymbol(tok, token.context); |
881 | 0 | } |
882 | 0 | } |
883 | 0 | max_alphabet_size_ = 0; |
884 | 0 | for (size_t i = 0; i < num_contexts; i++) { |
885 | 0 | max_alphabet_size_ = |
886 | 0 | std::max(max_alphabet_size_, builder.Histo(i).data_.size()); |
887 | 0 | } |
888 | 0 | bits_.resize(num_contexts * max_alphabet_size_); |
889 | | // TODO(veluca): SIMD? |
890 | 0 | add_symbol_cost_.resize(num_contexts); |
891 | 0 | for (size_t i = 0; i < num_contexts; i++) { |
892 | 0 | float inv_total = 1.0f / (builder.Histo(i).total_count_ + 1e-8f); |
893 | 0 | float total_cost = 0; |
894 | 0 | for (size_t j = 0; j < builder.Histo(i).data_.size(); j++) { |
895 | 0 | size_t cnt = builder.Histo(i).data_[j]; |
896 | 0 | float cost = 0; |
897 | 0 | if (cnt != 0 && cnt != builder.Histo(i).total_count_) { |
898 | 0 | cost = -FastLog2f(cnt * inv_total); |
899 | 0 | if (force_huffman) cost = std::ceil(cost); |
900 | 0 | } else if (cnt == 0) { |
901 | 0 | cost = ANS_LOG_TAB_SIZE; // Highest possible cost. |
902 | 0 | } |
903 | 0 | bits_[i * max_alphabet_size_ + j] = cost; |
904 | 0 | total_cost += cost * builder.Histo(i).data_[j]; |
905 | 0 | } |
906 | | // Penalty for adding a lz77 symbol to this contest (only used for static |
907 | | // cost model). Higher penalty for contexts that have a very low |
908 | | // per-symbol entropy. |
909 | 0 | add_symbol_cost_[i] = std::max(0.0f, 6.0f - total_cost * inv_total); |
910 | 0 | } |
911 | 0 | } |
912 | 0 | float Bits(size_t ctx, size_t sym) const { |
913 | 0 | return bits_[ctx * max_alphabet_size_ + sym]; |
914 | 0 | } |
915 | 0 | float LenCost(size_t ctx, size_t len, const LZ77Params& lz77) const { |
916 | 0 | uint32_t nbits, bits, tok; |
917 | 0 | lz77.length_uint_config.Encode(len, &tok, &nbits, &bits); |
918 | 0 | tok += lz77.min_symbol; |
919 | 0 | return nbits + Bits(ctx, tok); |
920 | 0 | } |
921 | 0 | float DistCost(size_t len, const LZ77Params& lz77) const { |
922 | 0 | uint32_t nbits, bits, tok; |
923 | 0 | HybridUintConfig().Encode(len, &tok, &nbits, &bits); |
924 | 0 | return nbits + Bits(lz77.nonserialized_distance_context, tok); |
925 | 0 | } |
926 | 0 | float AddSymbolCost(size_t idx) const { return add_symbol_cost_[idx]; } |
927 | | |
928 | | private: |
929 | | size_t max_alphabet_size_; |
930 | | std::vector<float> bits_; |
931 | | std::vector<float> add_symbol_cost_; |
932 | | }; |
933 | | |
934 | | void ApplyLZ77_RLE(const HistogramParams& params, size_t num_contexts, |
935 | | const std::vector<std::vector<Token>>& tokens, |
936 | | LZ77Params& lz77, |
937 | 0 | std::vector<std::vector<Token>>& tokens_lz77) { |
938 | | // TODO(veluca): tune heuristics here. |
939 | 0 | SymbolCostEstimator sce(num_contexts, params.force_huffman, tokens, lz77); |
940 | 0 | float bit_decrease = 0; |
941 | 0 | size_t total_symbols = 0; |
942 | 0 | tokens_lz77.resize(tokens.size()); |
943 | 0 | std::vector<float> sym_cost; |
944 | 0 | HybridUintConfig uint_config; |
945 | 0 | for (size_t stream = 0; stream < tokens.size(); stream++) { |
946 | 0 | size_t distance_multiplier = |
947 | 0 | params.image_widths.size() > stream ? params.image_widths[stream] : 0; |
948 | 0 | const auto& in = tokens[stream]; |
949 | 0 | auto& out = tokens_lz77[stream]; |
950 | 0 | total_symbols += in.size(); |
951 | | // Cumulative sum of bit costs. |
952 | 0 | sym_cost.resize(in.size() + 1); |
953 | 0 | for (size_t i = 0; i < in.size(); i++) { |
954 | 0 | uint32_t tok, nbits, unused_bits; |
955 | 0 | uint_config.Encode(in[i].value, &tok, &nbits, &unused_bits); |
956 | 0 | sym_cost[i + 1] = sce.Bits(in[i].context, tok) + nbits + sym_cost[i]; |
957 | 0 | } |
958 | 0 | out.reserve(in.size()); |
959 | 0 | for (size_t i = 0; i < in.size(); i++) { |
960 | 0 | size_t num_to_copy = 0; |
961 | 0 | size_t distance_symbol = 0; // 1 for RLE. |
962 | 0 | if (distance_multiplier != 0) { |
963 | 0 | distance_symbol = 1; // Special distance 1 if enabled. |
964 | 0 | JXL_DASSERT(kSpecialDistances[1][0] == 1); |
965 | 0 | JXL_DASSERT(kSpecialDistances[1][1] == 0); |
966 | 0 | } |
967 | 0 | if (i > 0) { |
968 | 0 | for (; i + num_to_copy < in.size(); num_to_copy++) { |
969 | 0 | if (in[i + num_to_copy].value != in[i - 1].value) { |
970 | 0 | break; |
971 | 0 | } |
972 | 0 | } |
973 | 0 | } |
974 | 0 | if (num_to_copy == 0) { |
975 | 0 | out.push_back(in[i]); |
976 | 0 | continue; |
977 | 0 | } |
978 | 0 | float cost = sym_cost[i + num_to_copy] - sym_cost[i]; |
979 | | // This subtraction might overflow, but that's OK. |
980 | 0 | size_t lz77_len = num_to_copy - lz77.min_length; |
981 | 0 | float lz77_cost = num_to_copy >= lz77.min_length |
982 | 0 | ? CeilLog2Nonzero(lz77_len + 1) + 1 |
983 | 0 | : 0; |
984 | 0 | if (num_to_copy < lz77.min_length || cost <= lz77_cost) { |
985 | 0 | for (size_t j = 0; j < num_to_copy; j++) { |
986 | 0 | out.push_back(in[i + j]); |
987 | 0 | } |
988 | 0 | i += num_to_copy - 1; |
989 | 0 | continue; |
990 | 0 | } |
991 | | // Output the LZ77 length |
992 | 0 | out.emplace_back(in[i].context, lz77_len); |
993 | 0 | out.back().is_lz77_length = true; |
994 | 0 | i += num_to_copy - 1; |
995 | 0 | bit_decrease += cost - lz77_cost; |
996 | | // Output the LZ77 copy distance. |
997 | 0 | out.emplace_back(lz77.nonserialized_distance_context, distance_symbol); |
998 | 0 | } |
999 | 0 | } |
1000 | |
|
1001 | 0 | if (bit_decrease > total_symbols * 0.2 + 16) { |
1002 | 0 | lz77.enabled = true; |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | | // Hash chain for LZ77 matching |
1007 | | struct HashChain { |
1008 | | size_t size_; |
1009 | | std::vector<uint32_t> data_; |
1010 | | |
1011 | | unsigned hash_num_values_ = 32768; |
1012 | | unsigned hash_mask_ = hash_num_values_ - 1; |
1013 | | unsigned hash_shift_ = 5; |
1014 | | |
1015 | | std::vector<int> head; |
1016 | | std::vector<uint32_t> chain; |
1017 | | std::vector<int> val; |
1018 | | |
1019 | | // Speed up repetitions of zero |
1020 | | std::vector<int> headz; |
1021 | | std::vector<uint32_t> chainz; |
1022 | | std::vector<uint32_t> zeros; |
1023 | | uint32_t numzeros = 0; |
1024 | | |
1025 | | size_t window_size_; |
1026 | | size_t window_mask_; |
1027 | | size_t min_length_; |
1028 | | size_t max_length_; |
1029 | | |
1030 | | // Map of special distance codes. |
1031 | | std::unordered_map<int, int> special_dist_table_; |
1032 | | size_t num_special_distances_ = 0; |
1033 | | |
1034 | | uint32_t maxchainlength = 256; // window_size_ to allow all |
1035 | | |
1036 | | HashChain(const Token* data, size_t size, size_t window_size, |
1037 | | size_t min_length, size_t max_length, size_t distance_multiplier) |
1038 | 0 | : size_(size), |
1039 | 0 | window_size_(window_size), |
1040 | 0 | window_mask_(window_size - 1), |
1041 | 0 | min_length_(min_length), |
1042 | 0 | max_length_(max_length) { |
1043 | 0 | data_.resize(size); |
1044 | 0 | for (size_t i = 0; i < size; i++) { |
1045 | 0 | data_[i] = data[i].value; |
1046 | 0 | } |
1047 | |
|
1048 | 0 | head.resize(hash_num_values_, -1); |
1049 | 0 | val.resize(window_size_, -1); |
1050 | 0 | chain.resize(window_size_); |
1051 | 0 | for (uint32_t i = 0; i < window_size_; ++i) { |
1052 | 0 | chain[i] = i; // same value as index indicates uninitialized |
1053 | 0 | } |
1054 | |
|
1055 | 0 | zeros.resize(window_size_); |
1056 | 0 | headz.resize(window_size_ + 1, -1); |
1057 | 0 | chainz.resize(window_size_); |
1058 | 0 | for (uint32_t i = 0; i < window_size_; ++i) { |
1059 | 0 | chainz[i] = i; |
1060 | 0 | } |
1061 | | // Translate distance to special distance code. |
1062 | 0 | if (distance_multiplier) { |
1063 | | // Count down, so if due to small distance multiplier multiple distances |
1064 | | // map to the same code, the smallest code will be used in the end. |
1065 | 0 | for (int i = kNumSpecialDistances - 1; i >= 0; --i) { |
1066 | 0 | special_dist_table_[SpecialDistance(i, distance_multiplier)] = i; |
1067 | 0 | } |
1068 | 0 | num_special_distances_ = kNumSpecialDistances; |
1069 | 0 | } |
1070 | 0 | } |
1071 | | |
1072 | 0 | uint32_t GetHash(size_t pos) const { |
1073 | 0 | uint32_t result = 0; |
1074 | 0 | if (pos + 2 < size_) { |
1075 | | // TODO(lode): take the MSB's of the uint32_t values into account as well, |
1076 | | // given that the hash code itself is less than 32 bits. |
1077 | 0 | result ^= static_cast<uint32_t>(data_[pos + 0] << 0u); |
1078 | 0 | result ^= static_cast<uint32_t>(data_[pos + 1] << hash_shift_); |
1079 | 0 | result ^= static_cast<uint32_t>(data_[pos + 2] << (hash_shift_ * 2)); |
1080 | 0 | } else { |
1081 | | // No need to compute hash of last 2 bytes, the length 2 is too short. |
1082 | 0 | return 0; |
1083 | 0 | } |
1084 | 0 | return result & hash_mask_; |
1085 | 0 | } |
1086 | | |
1087 | 0 | uint32_t CountZeros(size_t pos, uint32_t prevzeros) const { |
1088 | 0 | size_t end = pos + window_size_; |
1089 | 0 | if (end > size_) end = size_; |
1090 | 0 | if (prevzeros > 0) { |
1091 | 0 | if (prevzeros >= window_mask_ && data_[end - 1] == 0 && |
1092 | 0 | end == pos + window_size_) { |
1093 | 0 | return prevzeros; |
1094 | 0 | } else { |
1095 | 0 | return prevzeros - 1; |
1096 | 0 | } |
1097 | 0 | } |
1098 | 0 | uint32_t num = 0; |
1099 | 0 | while (pos + num < end && data_[pos + num] == 0) num++; |
1100 | 0 | return num; |
1101 | 0 | } |
1102 | | |
1103 | 0 | void Update(size_t pos) { |
1104 | 0 | uint32_t hashval = GetHash(pos); |
1105 | 0 | uint32_t wpos = pos & window_mask_; |
1106 | |
|
1107 | 0 | val[wpos] = static_cast<int>(hashval); |
1108 | 0 | if (head[hashval] != -1) chain[wpos] = head[hashval]; |
1109 | 0 | head[hashval] = wpos; |
1110 | |
|
1111 | 0 | if (pos > 0 && data_[pos] != data_[pos - 1]) numzeros = 0; |
1112 | 0 | numzeros = CountZeros(pos, numzeros); |
1113 | |
|
1114 | 0 | zeros[wpos] = numzeros; |
1115 | 0 | if (headz[numzeros] != -1) chainz[wpos] = headz[numzeros]; |
1116 | 0 | headz[numzeros] = wpos; |
1117 | 0 | } |
1118 | | |
1119 | 0 | void Update(size_t pos, size_t len) { |
1120 | 0 | for (size_t i = 0; i < len; i++) { |
1121 | 0 | Update(pos + i); |
1122 | 0 | } |
1123 | 0 | } |
1124 | | |
1125 | | template <typename CB> |
1126 | 0 | void FindMatches(size_t pos, int max_dist, const CB& found_match) const { |
1127 | 0 | uint32_t wpos = pos & window_mask_; |
1128 | 0 | uint32_t hashval = GetHash(pos); |
1129 | 0 | uint32_t hashpos = chain[wpos]; |
1130 | |
|
1131 | 0 | int prev_dist = 0; |
1132 | 0 | int end = std::min<int>(pos + max_length_, size_); |
1133 | 0 | uint32_t chainlength = 0; |
1134 | 0 | uint32_t best_len = 0; |
1135 | 0 | for (;;) { |
1136 | 0 | int dist = (hashpos <= wpos) ? (wpos - hashpos) |
1137 | 0 | : (wpos - hashpos + window_mask_ + 1); |
1138 | 0 | if (dist < prev_dist) break; |
1139 | 0 | prev_dist = dist; |
1140 | 0 | uint32_t len = 0; |
1141 | 0 | if (dist > 0) { |
1142 | 0 | int i = pos; |
1143 | 0 | int j = pos - dist; |
1144 | 0 | if (numzeros > 3) { |
1145 | 0 | int r = std::min<int>(numzeros - 1, zeros[hashpos]); |
1146 | 0 | if (i + r >= end) r = end - i - 1; |
1147 | 0 | i += r; |
1148 | 0 | j += r; |
1149 | 0 | } |
1150 | 0 | while (i < end && data_[i] == data_[j]) { |
1151 | 0 | i++; |
1152 | 0 | j++; |
1153 | 0 | } |
1154 | 0 | len = i - pos; |
1155 | | // This can trigger even if the new length is slightly smaller than the |
1156 | | // best length, because it is possible for a slightly cheaper distance |
1157 | | // symbol to occur. |
1158 | 0 | if (len >= min_length_ && len + 2 >= best_len) { |
1159 | 0 | auto it = special_dist_table_.find(dist); |
1160 | 0 | int dist_symbol = (it == special_dist_table_.end()) |
1161 | 0 | ? (num_special_distances_ + dist - 1) |
1162 | 0 | : it->second; |
1163 | 0 | found_match(len, dist_symbol); |
1164 | 0 | if (len > best_len) best_len = len; |
1165 | 0 | } |
1166 | 0 | } |
1167 | |
|
1168 | 0 | chainlength++; |
1169 | 0 | if (chainlength >= maxchainlength) break; |
1170 | | |
1171 | 0 | if (numzeros >= 3 && len > numzeros) { |
1172 | 0 | if (hashpos == chainz[hashpos]) break; |
1173 | 0 | hashpos = chainz[hashpos]; |
1174 | 0 | if (zeros[hashpos] != numzeros) break; |
1175 | 0 | } else { |
1176 | 0 | if (hashpos == chain[hashpos]) break; |
1177 | 0 | hashpos = chain[hashpos]; |
1178 | 0 | if (val[hashpos] != static_cast<int>(hashval)) { |
1179 | | // outdated hash value |
1180 | 0 | break; |
1181 | 0 | } |
1182 | 0 | } |
1183 | 0 | } |
1184 | 0 | } Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::HashChain::FindMatches<jxl::(anonymous namespace)::HashChain::FindMatch(unsigned long, int, unsigned long*, unsigned long*) const::{lambda(unsigned long, unsigned long)#1}>(unsigned long, int, jxl::(anonymous namespace)::HashChain::FindMatch(unsigned long, int, unsigned long*, unsigned long*) const::{lambda(unsigned long, unsigned long)#1} const&) const Unexecuted instantiation: enc_ans.cc:void jxl::(anonymous namespace)::HashChain::FindMatches<jxl::(anonymous namespace)::ApplyLZ77_Optimal(jxl::HistogramParams const&, unsigned long, std::__1::vector<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> >, std::__1::allocator<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> > > > const&, jxl::LZ77Params&, std::__1::vector<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> >, std::__1::allocator<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> > > >&)::$_0>(unsigned long, int, jxl::(anonymous namespace)::ApplyLZ77_Optimal(jxl::HistogramParams const&, unsigned long, std::__1::vector<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> >, std::__1::allocator<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> > > > const&, jxl::LZ77Params&, std::__1::vector<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> >, std::__1::allocator<std::__1::vector<jxl::Token, std::__1::allocator<jxl::Token> > > >&)::$_0 const&) const |
1185 | | void FindMatch(size_t pos, int max_dist, size_t* result_dist_symbol, |
1186 | 0 | size_t* result_len) const { |
1187 | 0 | *result_dist_symbol = 0; |
1188 | 0 | *result_len = 1; |
1189 | 0 | FindMatches(pos, max_dist, [&](size_t len, size_t dist_symbol) { |
1190 | 0 | if (len > *result_len || |
1191 | 0 | (len == *result_len && *result_dist_symbol > dist_symbol)) { |
1192 | 0 | *result_len = len; |
1193 | 0 | *result_dist_symbol = dist_symbol; |
1194 | 0 | } |
1195 | 0 | }); |
1196 | 0 | } |
1197 | | }; |
1198 | | |
1199 | 0 | float LenCost(size_t len) { |
1200 | 0 | uint32_t nbits, bits, tok; |
1201 | 0 | HybridUintConfig(1, 0, 0).Encode(len, &tok, &nbits, &bits); |
1202 | 0 | constexpr float kCostTable[] = { |
1203 | 0 | 2.797667318563126, 3.213177690381199, 2.5706009246743737, |
1204 | 0 | 2.408392498667534, 2.829649191872326, 3.3923087753324577, |
1205 | 0 | 4.029267451554331, 4.415576699706408, 4.509357574741465, |
1206 | 0 | 9.21481543803004, 10.020590190114898, 11.858671627804766, |
1207 | 0 | 12.45853300490526, 11.713105831990857, 12.561996324849314, |
1208 | 0 | 13.775477692278367, 13.174027068768641, |
1209 | 0 | }; |
1210 | 0 | size_t table_size = sizeof kCostTable / sizeof *kCostTable; |
1211 | 0 | if (tok >= table_size) tok = table_size - 1; |
1212 | 0 | return kCostTable[tok] + nbits; |
1213 | 0 | } |
1214 | | |
1215 | | // TODO(veluca): this does not take into account usage or non-usage of distance |
1216 | | // multipliers. |
1217 | 0 | float DistCost(size_t dist) { |
1218 | 0 | uint32_t nbits, bits, tok; |
1219 | 0 | HybridUintConfig(7, 0, 0).Encode(dist, &tok, &nbits, &bits); |
1220 | 0 | constexpr float kCostTable[] = { |
1221 | 0 | 6.368282626312716, 5.680793277090298, 8.347404197105247, |
1222 | 0 | 7.641619201599141, 6.914328374119438, 7.959808291537444, |
1223 | 0 | 8.70023120759855, 8.71378518934703, 9.379132523982769, |
1224 | 0 | 9.110472749092708, 9.159029569270908, 9.430936766731973, |
1225 | 0 | 7.278284055315169, 7.8278514904267755, 10.026641158289236, |
1226 | 0 | 9.976049229827066, 9.64351607048908, 9.563403863480442, |
1227 | 0 | 10.171474111762747, 10.45950155077234, 9.994813912104219, |
1228 | 0 | 10.322524683741156, 8.465808729388186, 8.756254166066853, |
1229 | 0 | 10.160930174662234, 10.247329273413435, 10.04090403724809, |
1230 | 0 | 10.129398517544082, 9.342311691539546, 9.07608009102374, |
1231 | 0 | 10.104799540677513, 10.378079384990906, 10.165828974075072, |
1232 | 0 | 10.337595322341553, 7.940557464567944, 10.575665823319431, |
1233 | 0 | 11.023344321751955, 10.736144698831827, 11.118277044595054, |
1234 | 0 | 7.468468230648442, 10.738305230932939, 10.906980780216568, |
1235 | 0 | 10.163468216353817, 10.17805759656433, 11.167283670483565, |
1236 | 0 | 11.147050200274544, 10.517921919244333, 10.651764778156886, |
1237 | 0 | 10.17074446448919, 11.217636876224745, 11.261630721139484, |
1238 | 0 | 11.403140815247259, 10.892472096873417, 11.1859607804481, |
1239 | 0 | 8.017346947551262, 7.895143720278828, 11.036577113822025, |
1240 | 0 | 11.170562110315794, 10.326988722591086, 10.40872184751056, |
1241 | 0 | 11.213498225466386, 11.30580635516863, 10.672272515665442, |
1242 | 0 | 10.768069466228063, 11.145257364153565, 11.64668307145549, |
1243 | 0 | 10.593156194627339, 11.207499484844943, 10.767517766396908, |
1244 | 0 | 10.826629811407042, 10.737764794499988, 10.6200448518045, |
1245 | 0 | 10.191315385198092, 8.468384171390085, 11.731295299170432, |
1246 | 0 | 11.824619886654398, 10.41518844301179, 10.16310536548649, |
1247 | 0 | 10.539423685097576, 10.495136599328031, 10.469112847728267, |
1248 | 0 | 11.72057686174922, 10.910326337834674, 11.378921834673758, |
1249 | 0 | 11.847759036098536, 11.92071647623854, 10.810628276345282, |
1250 | 0 | 11.008601085273893, 11.910326337834674, 11.949212023423133, |
1251 | 0 | 11.298614839104337, 11.611603659010392, 10.472930394619985, |
1252 | 0 | 11.835564720850282, 11.523267392285337, 12.01055816679611, |
1253 | 0 | 8.413029688994023, 11.895784139536406, 11.984679534970505, |
1254 | 0 | 11.220654278717394, 11.716311684833672, 10.61036646226114, |
1255 | 0 | 10.89849965960364, 10.203762898863669, 10.997560826267238, |
1256 | 0 | 11.484217379438984, 11.792836176993665, 12.24310468755171, |
1257 | 0 | 11.464858097919262, 12.212747017409377, 11.425595666074955, |
1258 | 0 | 11.572048533398757, 12.742093965163013, 11.381874288645637, |
1259 | 0 | 12.191870445817015, 11.683156920035426, 11.152442115262197, |
1260 | 0 | 11.90303691580457, 11.653292787169159, 11.938615382266098, |
1261 | 0 | 16.970641701570223, 16.853602280380002, 17.26240782594733, |
1262 | 0 | 16.644655390108507, 17.14310889757499, 16.910935455445955, |
1263 | 0 | 17.505678976959697, 17.213498225466388, 2.4162310293553024, |
1264 | 0 | 3.494587244462329, 3.5258600986408344, 3.4959806589517095, |
1265 | 0 | 3.098390886949687, 3.343454654302911, 3.588847442290287, |
1266 | 0 | 4.14614790111827, 5.152948641990529, 7.433696808092598, |
1267 | 0 | 9.716311684833672, |
1268 | 0 | }; |
1269 | 0 | size_t table_size = sizeof kCostTable / sizeof *kCostTable; |
1270 | 0 | if (tok >= table_size) tok = table_size - 1; |
1271 | 0 | return kCostTable[tok] + nbits; |
1272 | 0 | } |
1273 | | |
1274 | | void ApplyLZ77_LZ77(const HistogramParams& params, size_t num_contexts, |
1275 | | const std::vector<std::vector<Token>>& tokens, |
1276 | | LZ77Params& lz77, |
1277 | 0 | std::vector<std::vector<Token>>& tokens_lz77) { |
1278 | | // TODO(veluca): tune heuristics here. |
1279 | 0 | SymbolCostEstimator sce(num_contexts, params.force_huffman, tokens, lz77); |
1280 | 0 | float bit_decrease = 0; |
1281 | 0 | size_t total_symbols = 0; |
1282 | 0 | tokens_lz77.resize(tokens.size()); |
1283 | 0 | HybridUintConfig uint_config; |
1284 | 0 | std::vector<float> sym_cost; |
1285 | 0 | for (size_t stream = 0; stream < tokens.size(); stream++) { |
1286 | 0 | size_t distance_multiplier = |
1287 | 0 | params.image_widths.size() > stream ? params.image_widths[stream] : 0; |
1288 | 0 | const auto& in = tokens[stream]; |
1289 | 0 | auto& out = tokens_lz77[stream]; |
1290 | 0 | total_symbols += in.size(); |
1291 | | // Cumulative sum of bit costs. |
1292 | 0 | sym_cost.resize(in.size() + 1); |
1293 | 0 | for (size_t i = 0; i < in.size(); i++) { |
1294 | 0 | uint32_t tok, nbits, unused_bits; |
1295 | 0 | uint_config.Encode(in[i].value, &tok, &nbits, &unused_bits); |
1296 | 0 | sym_cost[i + 1] = sce.Bits(in[i].context, tok) + nbits + sym_cost[i]; |
1297 | 0 | } |
1298 | |
|
1299 | 0 | out.reserve(in.size()); |
1300 | 0 | size_t max_distance = in.size(); |
1301 | 0 | size_t min_length = lz77.min_length; |
1302 | 0 | JXL_DASSERT(min_length >= 3); |
1303 | 0 | size_t max_length = in.size(); |
1304 | | |
1305 | | // Use next power of two as window size. |
1306 | 0 | size_t window_size = 1; |
1307 | 0 | while (window_size < max_distance && window_size < kWindowSize) { |
1308 | 0 | window_size <<= 1; |
1309 | 0 | } |
1310 | |
|
1311 | 0 | HashChain chain(in.data(), in.size(), window_size, min_length, max_length, |
1312 | 0 | distance_multiplier); |
1313 | 0 | size_t len; |
1314 | 0 | size_t dist_symbol; |
1315 | |
|
1316 | 0 | const size_t max_lazy_match_len = 256; // 0 to disable lazy matching |
1317 | | |
1318 | | // Whether the next symbol was already updated (to test lazy matching) |
1319 | 0 | bool already_updated = false; |
1320 | 0 | for (size_t i = 0; i < in.size(); i++) { |
1321 | 0 | out.push_back(in[i]); |
1322 | 0 | if (!already_updated) chain.Update(i); |
1323 | 0 | already_updated = false; |
1324 | 0 | chain.FindMatch(i, max_distance, &dist_symbol, &len); |
1325 | 0 | if (len >= min_length) { |
1326 | 0 | if (len < max_lazy_match_len && i + 1 < in.size()) { |
1327 | | // Try length at next symbol lazy matching |
1328 | 0 | chain.Update(i + 1); |
1329 | 0 | already_updated = true; |
1330 | 0 | size_t len2, dist_symbol2; |
1331 | 0 | chain.FindMatch(i + 1, max_distance, &dist_symbol2, &len2); |
1332 | 0 | if (len2 > len) { |
1333 | | // Use the lazy match. Add literal, and use the next length starting |
1334 | | // from the next byte. |
1335 | 0 | ++i; |
1336 | 0 | already_updated = false; |
1337 | 0 | len = len2; |
1338 | 0 | dist_symbol = dist_symbol2; |
1339 | 0 | out.push_back(in[i]); |
1340 | 0 | } |
1341 | 0 | } |
1342 | |
|
1343 | 0 | float cost = sym_cost[i + len] - sym_cost[i]; |
1344 | 0 | size_t lz77_len = len - lz77.min_length; |
1345 | 0 | float lz77_cost = LenCost(lz77_len) + DistCost(dist_symbol) + |
1346 | 0 | sce.AddSymbolCost(out.back().context); |
1347 | |
|
1348 | 0 | if (lz77_cost <= cost) { |
1349 | 0 | out.back().value = len - min_length; |
1350 | 0 | out.back().is_lz77_length = true; |
1351 | 0 | out.emplace_back(lz77.nonserialized_distance_context, dist_symbol); |
1352 | 0 | bit_decrease += cost - lz77_cost; |
1353 | 0 | } else { |
1354 | | // LZ77 match ignored, and symbol already pushed. Push all other |
1355 | | // symbols and skip. |
1356 | 0 | for (size_t j = 1; j < len; j++) { |
1357 | 0 | out.push_back(in[i + j]); |
1358 | 0 | } |
1359 | 0 | } |
1360 | |
|
1361 | 0 | if (already_updated) { |
1362 | 0 | chain.Update(i + 2, len - 2); |
1363 | 0 | already_updated = false; |
1364 | 0 | } else { |
1365 | 0 | chain.Update(i + 1, len - 1); |
1366 | 0 | } |
1367 | 0 | i += len - 1; |
1368 | 0 | } else { |
1369 | | // Literal, already pushed |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | } |
1373 | |
|
1374 | 0 | if (bit_decrease > total_symbols * 0.2 + 16) { |
1375 | 0 | lz77.enabled = true; |
1376 | 0 | } |
1377 | 0 | } |
1378 | | |
1379 | | void ApplyLZ77_Optimal(const HistogramParams& params, size_t num_contexts, |
1380 | | const std::vector<std::vector<Token>>& tokens, |
1381 | | LZ77Params& lz77, |
1382 | 0 | std::vector<std::vector<Token>>& tokens_lz77) { |
1383 | 0 | std::vector<std::vector<Token>> tokens_for_cost_estimate; |
1384 | 0 | ApplyLZ77_LZ77(params, num_contexts, tokens, lz77, tokens_for_cost_estimate); |
1385 | | // If greedy-LZ77 does not give better compression than no-lz77, no reason to |
1386 | | // run the optimal matching. |
1387 | 0 | if (!lz77.enabled) return; |
1388 | 0 | SymbolCostEstimator sce(num_contexts + 1, params.force_huffman, |
1389 | 0 | tokens_for_cost_estimate, lz77); |
1390 | 0 | tokens_lz77.resize(tokens.size()); |
1391 | 0 | HybridUintConfig uint_config; |
1392 | 0 | std::vector<float> sym_cost; |
1393 | 0 | std::vector<uint32_t> dist_symbols; |
1394 | 0 | for (size_t stream = 0; stream < tokens.size(); stream++) { |
1395 | 0 | size_t distance_multiplier = |
1396 | 0 | params.image_widths.size() > stream ? params.image_widths[stream] : 0; |
1397 | 0 | const auto& in = tokens[stream]; |
1398 | 0 | auto& out = tokens_lz77[stream]; |
1399 | | // Cumulative sum of bit costs. |
1400 | 0 | sym_cost.resize(in.size() + 1); |
1401 | 0 | for (size_t i = 0; i < in.size(); i++) { |
1402 | 0 | uint32_t tok, nbits, unused_bits; |
1403 | 0 | uint_config.Encode(in[i].value, &tok, &nbits, &unused_bits); |
1404 | 0 | sym_cost[i + 1] = sce.Bits(in[i].context, tok) + nbits + sym_cost[i]; |
1405 | 0 | } |
1406 | |
|
1407 | 0 | out.reserve(in.size()); |
1408 | 0 | size_t max_distance = in.size(); |
1409 | 0 | size_t min_length = lz77.min_length; |
1410 | 0 | JXL_DASSERT(min_length >= 3); |
1411 | 0 | size_t max_length = in.size(); |
1412 | | |
1413 | | // Use next power of two as window size. |
1414 | 0 | size_t window_size = 1; |
1415 | 0 | while (window_size < max_distance && window_size < kWindowSize) { |
1416 | 0 | window_size <<= 1; |
1417 | 0 | } |
1418 | |
|
1419 | 0 | HashChain chain(in.data(), in.size(), window_size, min_length, max_length, |
1420 | 0 | distance_multiplier); |
1421 | |
|
1422 | 0 | struct MatchInfo { |
1423 | 0 | uint32_t len; |
1424 | 0 | uint32_t dist_symbol; |
1425 | 0 | uint32_t ctx; |
1426 | 0 | float total_cost = std::numeric_limits<float>::max(); |
1427 | 0 | }; |
1428 | | // Total cost to encode the first N symbols. |
1429 | 0 | std::vector<MatchInfo> prefix_costs(in.size() + 1); |
1430 | 0 | prefix_costs[0].total_cost = 0; |
1431 | |
|
1432 | 0 | size_t rle_length = 0; |
1433 | 0 | size_t skip_lz77 = 0; |
1434 | 0 | for (size_t i = 0; i < in.size(); i++) { |
1435 | 0 | chain.Update(i); |
1436 | 0 | float lit_cost = |
1437 | 0 | prefix_costs[i].total_cost + sym_cost[i + 1] - sym_cost[i]; |
1438 | 0 | if (prefix_costs[i + 1].total_cost > lit_cost) { |
1439 | 0 | prefix_costs[i + 1].dist_symbol = 0; |
1440 | 0 | prefix_costs[i + 1].len = 1; |
1441 | 0 | prefix_costs[i + 1].ctx = in[i].context; |
1442 | 0 | prefix_costs[i + 1].total_cost = lit_cost; |
1443 | 0 | } |
1444 | 0 | if (skip_lz77 > 0) { |
1445 | 0 | skip_lz77--; |
1446 | 0 | continue; |
1447 | 0 | } |
1448 | 0 | dist_symbols.clear(); |
1449 | 0 | chain.FindMatches(i, max_distance, |
1450 | 0 | [&dist_symbols](size_t len, size_t dist_symbol) { |
1451 | 0 | if (dist_symbols.size() <= len) { |
1452 | 0 | dist_symbols.resize(len + 1, dist_symbol); |
1453 | 0 | } |
1454 | 0 | if (dist_symbol < dist_symbols[len]) { |
1455 | 0 | dist_symbols[len] = dist_symbol; |
1456 | 0 | } |
1457 | 0 | }); |
1458 | 0 | if (dist_symbols.size() <= min_length) continue; |
1459 | 0 | { |
1460 | 0 | size_t best_cost = dist_symbols.back(); |
1461 | 0 | for (size_t j = dist_symbols.size() - 1; j >= min_length; j--) { |
1462 | 0 | if (dist_symbols[j] < best_cost) { |
1463 | 0 | best_cost = dist_symbols[j]; |
1464 | 0 | } |
1465 | 0 | dist_symbols[j] = best_cost; |
1466 | 0 | } |
1467 | 0 | } |
1468 | 0 | for (size_t j = min_length; j < dist_symbols.size(); j++) { |
1469 | | // Cost model that uses results from lazy LZ77. |
1470 | 0 | float lz77_cost = sce.LenCost(in[i].context, j - min_length, lz77) + |
1471 | 0 | sce.DistCost(dist_symbols[j], lz77); |
1472 | 0 | float cost = prefix_costs[i].total_cost + lz77_cost; |
1473 | 0 | if (prefix_costs[i + j].total_cost > cost) { |
1474 | 0 | prefix_costs[i + j].len = j; |
1475 | 0 | prefix_costs[i + j].dist_symbol = dist_symbols[j] + 1; |
1476 | 0 | prefix_costs[i + j].ctx = in[i].context; |
1477 | 0 | prefix_costs[i + j].total_cost = cost; |
1478 | 0 | } |
1479 | 0 | } |
1480 | | // We are in a RLE sequence: skip all the symbols except the first 8 and |
1481 | | // the last 8. This avoid quadratic costs for sequences with long runs of |
1482 | | // the same symbol. |
1483 | 0 | if ((dist_symbols.back() == 0 && distance_multiplier == 0) || |
1484 | 0 | (dist_symbols.back() == 1 && distance_multiplier != 0)) { |
1485 | 0 | rle_length++; |
1486 | 0 | } else { |
1487 | 0 | rle_length = 0; |
1488 | 0 | } |
1489 | 0 | if (rle_length >= 8 && dist_symbols.size() > 9) { |
1490 | 0 | skip_lz77 = dist_symbols.size() - 10; |
1491 | 0 | rle_length = 0; |
1492 | 0 | } |
1493 | 0 | } |
1494 | 0 | size_t pos = in.size(); |
1495 | 0 | while (pos > 0) { |
1496 | 0 | bool is_lz77_length = prefix_costs[pos].dist_symbol != 0; |
1497 | 0 | if (is_lz77_length) { |
1498 | 0 | size_t dist_symbol = prefix_costs[pos].dist_symbol - 1; |
1499 | 0 | out.emplace_back(lz77.nonserialized_distance_context, dist_symbol); |
1500 | 0 | } |
1501 | 0 | size_t val = is_lz77_length ? prefix_costs[pos].len - min_length |
1502 | 0 | : in[pos - 1].value; |
1503 | 0 | out.emplace_back(prefix_costs[pos].ctx, val); |
1504 | 0 | out.back().is_lz77_length = is_lz77_length; |
1505 | 0 | pos -= prefix_costs[pos].len; |
1506 | 0 | } |
1507 | 0 | std::reverse(out.begin(), out.end()); |
1508 | 0 | } |
1509 | 0 | } |
1510 | | |
1511 | | void ApplyLZ77(const HistogramParams& params, size_t num_contexts, |
1512 | | const std::vector<std::vector<Token>>& tokens, LZ77Params& lz77, |
1513 | 0 | std::vector<std::vector<Token>>& tokens_lz77) { |
1514 | 0 | if (params.initialize_global_state) { |
1515 | 0 | lz77.enabled = false; |
1516 | 0 | } |
1517 | 0 | if (params.force_huffman) { |
1518 | 0 | lz77.min_symbol = std::min(PREFIX_MAX_ALPHABET_SIZE - 32, 512); |
1519 | 0 | } else { |
1520 | 0 | lz77.min_symbol = 224; |
1521 | 0 | } |
1522 | 0 | switch (params.lz77_method) { |
1523 | 0 | case HistogramParams::LZ77Method::kNone: |
1524 | 0 | return; |
1525 | 0 | case HistogramParams::LZ77Method::kRLE: |
1526 | 0 | ApplyLZ77_RLE(params, num_contexts, tokens, lz77, tokens_lz77); |
1527 | 0 | return; |
1528 | 0 | case HistogramParams::LZ77Method::kLZ77: |
1529 | 0 | ApplyLZ77_LZ77(params, num_contexts, tokens, lz77, tokens_lz77); |
1530 | 0 | return; |
1531 | 0 | case HistogramParams::LZ77Method::kOptimal: |
1532 | 0 | ApplyLZ77_Optimal(params, num_contexts, tokens, lz77, tokens_lz77); |
1533 | 0 | return; |
1534 | 0 | } |
1535 | 0 | } |
1536 | | } // namespace |
1537 | | |
1538 | | Status EncodeHistograms(const std::vector<uint8_t>& context_map, |
1539 | | const EntropyEncodingData& codes, BitWriter* writer, |
1540 | 0 | LayerType layer, AuxOut* aux_out) { |
1541 | 0 | return writer->WithMaxBits( |
1542 | 0 | 128 + kClustersLimit * 136, layer, aux_out, |
1543 | 0 | [&]() -> Status { |
1544 | 0 | JXL_RETURN_IF_ERROR(Bundle::Write(codes.lz77, writer, layer, aux_out)); |
1545 | 0 | if (codes.lz77.enabled) { |
1546 | 0 | EncodeUintConfig(codes.lz77.length_uint_config, writer, |
1547 | 0 | /*log_alpha_size=*/8); |
1548 | 0 | } |
1549 | 0 | JXL_RETURN_IF_ERROR(EncodeContextMap( |
1550 | 0 | context_map, codes.encoding_info.size(), writer, layer, aux_out)); |
1551 | 0 | writer->Write(1, TO_JXL_BOOL(codes.use_prefix_code)); |
1552 | 0 | size_t log_alpha_size = 8; |
1553 | 0 | if (codes.use_prefix_code) { |
1554 | 0 | log_alpha_size = PREFIX_MAX_BITS; |
1555 | 0 | } else { |
1556 | 0 | log_alpha_size = 8; // streaming_mode |
1557 | 0 | writer->Write(2, log_alpha_size - 5); |
1558 | 0 | } |
1559 | 0 | EncodeUintConfigs(codes.uint_config, writer, log_alpha_size); |
1560 | 0 | if (codes.use_prefix_code) { |
1561 | 0 | for (const auto& info : codes.encoding_info) { |
1562 | 0 | StoreVarLenUint16(info.size() - 1, writer); |
1563 | 0 | } |
1564 | 0 | } |
1565 | 0 | for (const auto& histo_writer : codes.encoded_histograms) { |
1566 | 0 | JXL_RETURN_IF_ERROR(writer->AppendUnaligned(histo_writer)); |
1567 | 0 | } |
1568 | 0 | return true; |
1569 | 0 | }, |
1570 | 0 | /*finished_histogram=*/true); |
1571 | 0 | } |
1572 | | |
1573 | | StatusOr<size_t> BuildAndEncodeHistograms( |
1574 | | JxlMemoryManager* memory_manager, const HistogramParams& params, |
1575 | | size_t num_contexts, std::vector<std::vector<Token>>& tokens, |
1576 | | EntropyEncodingData* codes, std::vector<uint8_t>* context_map, |
1577 | 0 | BitWriter* writer, LayerType layer, AuxOut* aux_out) { |
1578 | 0 | size_t cost = 0; |
1579 | 0 | codes->lz77.nonserialized_distance_context = num_contexts; |
1580 | 0 | std::vector<std::vector<Token>> tokens_lz77; |
1581 | 0 | ApplyLZ77(params, num_contexts, tokens, codes->lz77, tokens_lz77); |
1582 | 0 | if (ans_fuzzer_friendly_) { |
1583 | 0 | codes->lz77.length_uint_config = HybridUintConfig(10, 0, 0); |
1584 | 0 | codes->lz77.min_symbol = 2048; |
1585 | 0 | } |
1586 | |
|
1587 | 0 | const size_t max_contexts = std::min(num_contexts, kClustersLimit); |
1588 | 0 | const auto& body = [&]() -> Status { |
1589 | 0 | if (writer) { |
1590 | 0 | JXL_RETURN_IF_ERROR(Bundle::Write(codes->lz77, writer, layer, aux_out)); |
1591 | 0 | } else { |
1592 | 0 | size_t ebits, bits; |
1593 | 0 | JXL_RETURN_IF_ERROR(Bundle::CanEncode(codes->lz77, &ebits, &bits)); |
1594 | 0 | cost += bits; |
1595 | 0 | } |
1596 | 0 | if (codes->lz77.enabled) { |
1597 | 0 | if (writer) { |
1598 | 0 | size_t b = writer->BitsWritten(); |
1599 | 0 | EncodeUintConfig(codes->lz77.length_uint_config, writer, |
1600 | 0 | /*log_alpha_size=*/8); |
1601 | 0 | cost += writer->BitsWritten() - b; |
1602 | 0 | } else { |
1603 | 0 | SizeWriter size_writer; |
1604 | 0 | EncodeUintConfig(codes->lz77.length_uint_config, &size_writer, |
1605 | 0 | /*log_alpha_size=*/8); |
1606 | 0 | cost += size_writer.size; |
1607 | 0 | } |
1608 | 0 | num_contexts += 1; |
1609 | 0 | tokens = std::move(tokens_lz77); |
1610 | 0 | } |
1611 | 0 | size_t total_tokens = 0; |
1612 | | // Build histograms. |
1613 | 0 | HistogramBuilder builder(num_contexts); |
1614 | 0 | HybridUintConfig uint_config; // Default config for clustering. |
1615 | | // Unless we are using the kContextMap histogram option. |
1616 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::kContextMap) { |
1617 | 0 | uint_config = HybridUintConfig(2, 0, 1); |
1618 | 0 | } |
1619 | 0 | if (params.uint_method == HistogramParams::HybridUintMethod::k000) { |
1620 | 0 | uint_config = HybridUintConfig(0, 0, 0); |
1621 | 0 | } |
1622 | 0 | if (ans_fuzzer_friendly_) { |
1623 | 0 | uint_config = HybridUintConfig(10, 0, 0); |
1624 | 0 | } |
1625 | 0 | for (const auto& stream : tokens) { |
1626 | 0 | if (codes->lz77.enabled) { |
1627 | 0 | for (const auto& token : stream) { |
1628 | 0 | total_tokens++; |
1629 | 0 | uint32_t tok, nbits, bits; |
1630 | 0 | (token.is_lz77_length ? codes->lz77.length_uint_config : uint_config) |
1631 | 0 | .Encode(token.value, &tok, &nbits, &bits); |
1632 | 0 | tok += token.is_lz77_length ? codes->lz77.min_symbol : 0; |
1633 | 0 | builder.VisitSymbol(tok, token.context); |
1634 | 0 | } |
1635 | 0 | } else if (num_contexts == 1) { |
1636 | 0 | for (const auto& token : stream) { |
1637 | 0 | total_tokens++; |
1638 | 0 | uint32_t tok, nbits, bits; |
1639 | 0 | uint_config.Encode(token.value, &tok, &nbits, &bits); |
1640 | 0 | builder.VisitSymbol(tok, /*token.context=*/0); |
1641 | 0 | } |
1642 | 0 | } else { |
1643 | 0 | for (const auto& token : stream) { |
1644 | 0 | total_tokens++; |
1645 | 0 | uint32_t tok, nbits, bits; |
1646 | 0 | uint_config.Encode(token.value, &tok, &nbits, &bits); |
1647 | 0 | builder.VisitSymbol(tok, token.context); |
1648 | 0 | } |
1649 | 0 | } |
1650 | 0 | } |
1651 | |
|
1652 | 0 | if (params.add_missing_symbols) { |
1653 | 0 | for (size_t c = 0; c < num_contexts; ++c) { |
1654 | 0 | for (int symbol = 0; symbol < ANS_MAX_ALPHABET_SIZE; ++symbol) { |
1655 | 0 | builder.VisitSymbol(symbol, c); |
1656 | 0 | } |
1657 | 0 | } |
1658 | 0 | } |
1659 | |
|
1660 | 0 | if (params.initialize_global_state) { |
1661 | 0 | bool use_prefix_code = |
1662 | 0 | params.force_huffman || total_tokens < 100 || |
1663 | 0 | params.clustering == HistogramParams::ClusteringType::kFastest || |
1664 | 0 | ans_fuzzer_friendly_; |
1665 | 0 | if (!use_prefix_code) { |
1666 | 0 | bool all_singleton = true; |
1667 | 0 | for (size_t i = 0; i < num_contexts; i++) { |
1668 | 0 | if (builder.Histo(i).ShannonEntropy() >= 1e-5) { |
1669 | 0 | all_singleton = false; |
1670 | 0 | } |
1671 | 0 | } |
1672 | 0 | if (all_singleton) { |
1673 | 0 | use_prefix_code = true; |
1674 | 0 | } |
1675 | 0 | } |
1676 | 0 | codes->use_prefix_code = use_prefix_code; |
1677 | 0 | } |
1678 | |
|
1679 | 0 | if (params.add_fixed_histograms) { |
1680 | | // TODO(szabadka) Add more fixed histograms. |
1681 | | // TODO(szabadka) Reduce alphabet size by choosing a non-default |
1682 | | // uint_config. |
1683 | 0 | const size_t alphabet_size = ANS_MAX_ALPHABET_SIZE; |
1684 | 0 | const size_t log_alpha_size = 8; |
1685 | 0 | JXL_ENSURE(alphabet_size == 1u << log_alpha_size); |
1686 | 0 | static_assert(ANS_MAX_ALPHABET_SIZE <= ANS_TAB_SIZE); |
1687 | 0 | std::vector<int32_t> counts = |
1688 | 0 | CreateFlatHistogram(alphabet_size, ANS_TAB_SIZE); |
1689 | 0 | codes->encoding_info.emplace_back(); |
1690 | 0 | codes->encoding_info.back().resize(alphabet_size); |
1691 | 0 | codes->encoded_histograms.emplace_back(memory_manager); |
1692 | 0 | BitWriter* histo_writer = &codes->encoded_histograms.back(); |
1693 | 0 | JXL_RETURN_IF_ERROR(histo_writer->WithMaxBits( |
1694 | 0 | 256 + alphabet_size * 24, LayerType::Header, nullptr, |
1695 | 0 | [&]() -> Status { |
1696 | 0 | JXL_ASSIGN_OR_RETURN( |
1697 | 0 | size_t ans_cost, |
1698 | 0 | BuildAndStoreANSEncodingData( |
1699 | 0 | memory_manager, params.ans_histogram_strategy, |
1700 | 0 | counts.data(), alphabet_size, log_alpha_size, |
1701 | 0 | codes->use_prefix_code, codes->encoding_info.back().data(), |
1702 | 0 | histo_writer)); |
1703 | 0 | (void)ans_cost; |
1704 | 0 | return true; |
1705 | 0 | })); |
1706 | 0 | } |
1707 | | |
1708 | | // Encode histograms. |
1709 | 0 | JXL_ASSIGN_OR_RETURN( |
1710 | 0 | size_t entropy_bits, |
1711 | 0 | builder.BuildAndStoreEntropyCodes(memory_manager, params, tokens, codes, |
1712 | 0 | context_map, writer, layer, aux_out)); |
1713 | 0 | cost += entropy_bits; |
1714 | 0 | return true; |
1715 | 0 | }; |
1716 | 0 | if (writer) { |
1717 | 0 | JXL_RETURN_IF_ERROR(writer->WithMaxBits( |
1718 | 0 | 128 + num_contexts * 40 + max_contexts * 96, layer, aux_out, body, |
1719 | 0 | /*finished_histogram=*/true)); |
1720 | 0 | } else { |
1721 | 0 | JXL_RETURN_IF_ERROR(body()); |
1722 | 0 | } |
1723 | | |
1724 | 0 | if (aux_out != nullptr) { |
1725 | 0 | aux_out->layer(layer).num_clustered_histograms += |
1726 | 0 | codes->encoding_info.size(); |
1727 | 0 | } |
1728 | 0 | return cost; |
1729 | 0 | } |
1730 | | |
1731 | | size_t WriteTokens(const std::vector<Token>& tokens, |
1732 | | const EntropyEncodingData& codes, |
1733 | | const std::vector<uint8_t>& context_map, |
1734 | 0 | size_t context_offset, BitWriter* writer) { |
1735 | 0 | size_t num_extra_bits = 0; |
1736 | 0 | if (codes.use_prefix_code) { |
1737 | 0 | for (const auto& token : tokens) { |
1738 | 0 | uint32_t tok, nbits, bits; |
1739 | 0 | size_t histo = context_map[context_offset + token.context]; |
1740 | 0 | (token.is_lz77_length ? codes.lz77.length_uint_config |
1741 | 0 | : codes.uint_config[histo]) |
1742 | 0 | .Encode(token.value, &tok, &nbits, &bits); |
1743 | 0 | tok += token.is_lz77_length ? codes.lz77.min_symbol : 0; |
1744 | | // Combine two calls to the BitWriter. Equivalent to: |
1745 | | // writer->Write(codes.encoding_info[histo][tok].depth, |
1746 | | // codes.encoding_info[histo][tok].bits); |
1747 | | // writer->Write(nbits, bits); |
1748 | 0 | uint64_t data = codes.encoding_info[histo][tok].bits; |
1749 | 0 | data |= static_cast<uint64_t>(bits) |
1750 | 0 | << codes.encoding_info[histo][tok].depth; |
1751 | 0 | writer->Write(codes.encoding_info[histo][tok].depth + nbits, data); |
1752 | 0 | num_extra_bits += nbits; |
1753 | 0 | } |
1754 | 0 | return num_extra_bits; |
1755 | 0 | } |
1756 | 0 | std::vector<uint64_t> out; |
1757 | 0 | std::vector<uint8_t> out_nbits; |
1758 | 0 | out.reserve(tokens.size()); |
1759 | 0 | out_nbits.reserve(tokens.size()); |
1760 | 0 | uint64_t allbits = 0; |
1761 | 0 | size_t numallbits = 0; |
1762 | | // Writes in *reversed* order. |
1763 | 0 | auto addbits = [&](size_t bits, size_t nbits) { |
1764 | 0 | if (JXL_UNLIKELY(nbits)) { |
1765 | 0 | JXL_DASSERT(bits >> nbits == 0); |
1766 | 0 | if (JXL_UNLIKELY(numallbits + nbits > BitWriter::kMaxBitsPerCall)) { |
1767 | 0 | out.push_back(allbits); |
1768 | 0 | out_nbits.push_back(numallbits); |
1769 | 0 | numallbits = allbits = 0; |
1770 | 0 | } |
1771 | 0 | allbits <<= nbits; |
1772 | 0 | allbits |= bits; |
1773 | 0 | numallbits += nbits; |
1774 | 0 | } |
1775 | 0 | }; |
1776 | 0 | const int end = tokens.size(); |
1777 | 0 | ANSCoder ans; |
1778 | 0 | if (codes.lz77.enabled || context_map.size() > 1) { |
1779 | 0 | for (int i = end - 1; i >= 0; --i) { |
1780 | 0 | const Token token = tokens[i]; |
1781 | 0 | const uint8_t histo = context_map[context_offset + token.context]; |
1782 | 0 | uint32_t tok, nbits, bits; |
1783 | 0 | (token.is_lz77_length ? codes.lz77.length_uint_config |
1784 | 0 | : codes.uint_config[histo]) |
1785 | 0 | .Encode(tokens[i].value, &tok, &nbits, &bits); |
1786 | 0 | tok += token.is_lz77_length ? codes.lz77.min_symbol : 0; |
1787 | 0 | const ANSEncSymbolInfo& info = codes.encoding_info[histo][tok]; |
1788 | 0 | JXL_DASSERT(info.freq_ > 0); |
1789 | | // Extra bits first as this is reversed. |
1790 | 0 | addbits(bits, nbits); |
1791 | 0 | num_extra_bits += nbits; |
1792 | 0 | uint8_t ans_nbits = 0; |
1793 | 0 | uint32_t ans_bits = ans.PutSymbol(info, &ans_nbits); |
1794 | 0 | addbits(ans_bits, ans_nbits); |
1795 | 0 | } |
1796 | 0 | } else { |
1797 | 0 | for (int i = end - 1; i >= 0; --i) { |
1798 | 0 | uint32_t tok, nbits, bits; |
1799 | 0 | codes.uint_config[0].Encode(tokens[i].value, &tok, &nbits, &bits); |
1800 | 0 | const ANSEncSymbolInfo& info = codes.encoding_info[0][tok]; |
1801 | | // Extra bits first as this is reversed. |
1802 | 0 | addbits(bits, nbits); |
1803 | 0 | num_extra_bits += nbits; |
1804 | 0 | uint8_t ans_nbits = 0; |
1805 | 0 | uint32_t ans_bits = ans.PutSymbol(info, &ans_nbits); |
1806 | 0 | addbits(ans_bits, ans_nbits); |
1807 | 0 | } |
1808 | 0 | } |
1809 | 0 | const uint32_t state = ans.GetState(); |
1810 | 0 | writer->Write(32, state); |
1811 | 0 | writer->Write(numallbits, allbits); |
1812 | 0 | for (int i = out.size(); i > 0; --i) { |
1813 | 0 | writer->Write(out_nbits[i - 1], out[i - 1]); |
1814 | 0 | } |
1815 | 0 | return num_extra_bits; |
1816 | 0 | } |
1817 | | |
1818 | | Status WriteTokens(const std::vector<Token>& tokens, |
1819 | | const EntropyEncodingData& codes, |
1820 | | const std::vector<uint8_t>& context_map, |
1821 | | size_t context_offset, BitWriter* writer, LayerType layer, |
1822 | 0 | AuxOut* aux_out) { |
1823 | | // Theoretically, we could have 15 prefix code bits + 31 extra bits. |
1824 | 0 | return writer->WithMaxBits( |
1825 | 0 | 46 * tokens.size() + 32 * 1024 * 4, layer, aux_out, [&] { |
1826 | 0 | size_t num_extra_bits = |
1827 | 0 | WriteTokens(tokens, codes, context_map, context_offset, writer); |
1828 | 0 | if (aux_out != nullptr) { |
1829 | 0 | aux_out->layer(layer).extra_bits += num_extra_bits; |
1830 | 0 | } |
1831 | 0 | return true; |
1832 | 0 | }); |
1833 | 0 | } |
1834 | | |
1835 | 0 | void SetANSFuzzerFriendly(bool ans_fuzzer_friendly) { |
1836 | | #if JXL_IS_DEBUG_BUILD // Guard against accidental / malicious changes. |
1837 | | ans_fuzzer_friendly_ = ans_fuzzer_friendly; |
1838 | | #endif |
1839 | 0 | } |
1840 | | |
1841 | | HistogramParams HistogramParams::ForModular( |
1842 | | const CompressParams& cparams, |
1843 | 0 | const std::vector<uint8_t>& extra_dc_precision, bool streaming_mode) { |
1844 | 0 | HistogramParams params; |
1845 | 0 | params.streaming_mode = streaming_mode; |
1846 | 0 | if (cparams.speed_tier > SpeedTier::kKitten) { |
1847 | 0 | params.clustering = HistogramParams::ClusteringType::kFast; |
1848 | 0 | params.ans_histogram_strategy = |
1849 | 0 | cparams.speed_tier > SpeedTier::kThunder |
1850 | 0 | ? HistogramParams::ANSHistogramStrategy::kFast |
1851 | 0 | : HistogramParams::ANSHistogramStrategy::kApproximate; |
1852 | 0 | params.lz77_method = |
1853 | 0 | cparams.decoding_speed_tier >= 3 && cparams.modular_mode |
1854 | 0 | ? (cparams.speed_tier >= SpeedTier::kFalcon |
1855 | 0 | ? HistogramParams::LZ77Method::kRLE |
1856 | 0 | : HistogramParams::LZ77Method::kLZ77) |
1857 | 0 | : HistogramParams::LZ77Method::kNone; |
1858 | | // Near-lossless DC, as well as modular mode, require choosing hybrid uint |
1859 | | // more carefully. |
1860 | 0 | if ((!extra_dc_precision.empty() && extra_dc_precision[0] != 0) || |
1861 | 0 | (cparams.modular_mode && cparams.speed_tier < SpeedTier::kCheetah)) { |
1862 | 0 | params.uint_method = HistogramParams::HybridUintMethod::kFast; |
1863 | 0 | } else { |
1864 | 0 | params.uint_method = HistogramParams::HybridUintMethod::kNone; |
1865 | 0 | } |
1866 | 0 | } else if (cparams.speed_tier <= SpeedTier::kTortoise) { |
1867 | 0 | params.lz77_method = HistogramParams::LZ77Method::kOptimal; |
1868 | 0 | } else { |
1869 | 0 | params.lz77_method = HistogramParams::LZ77Method::kLZ77; |
1870 | 0 | } |
1871 | 0 | if (cparams.decoding_speed_tier >= 1) { |
1872 | 0 | params.max_histograms = 12; |
1873 | 0 | } |
1874 | 0 | if (cparams.decoding_speed_tier >= 1 && cparams.responsive) { |
1875 | 0 | params.lz77_method = cparams.speed_tier >= SpeedTier::kCheetah |
1876 | 0 | ? HistogramParams::LZ77Method::kRLE |
1877 | 0 | : cparams.speed_tier >= SpeedTier::kKitten |
1878 | 0 | ? HistogramParams::LZ77Method::kLZ77 |
1879 | 0 | : HistogramParams::LZ77Method::kOptimal; |
1880 | 0 | } |
1881 | 0 | if (cparams.decoding_speed_tier >= 2 && cparams.responsive) { |
1882 | 0 | params.uint_method = HistogramParams::HybridUintMethod::k000; |
1883 | 0 | params.force_huffman = true; |
1884 | 0 | } |
1885 | 0 | return params; |
1886 | 0 | } |
1887 | | } // namespace jxl |