/src/libjxl/lib/jxl/fields.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/fields.h" |
7 | | |
8 | | #include <algorithm> |
9 | | #include <cinttypes> // PRIu64 |
10 | | #include <cmath> |
11 | | #include <cstddef> |
12 | | #include <cstring> |
13 | | #include <hwy/base.h> |
14 | | |
15 | | #include "lib/jxl/base/bits.h" |
16 | | #include "lib/jxl/base/common.h" |
17 | | #include "lib/jxl/base/compiler_specific.h" |
18 | | #include "lib/jxl/base/printf_macros.h" |
19 | | #include "lib/jxl/base/status.h" |
20 | | #include "lib/jxl/dec_bit_reader.h" |
21 | | #include "lib/jxl/field_encodings.h" |
22 | | |
23 | | namespace jxl { |
24 | | |
25 | | namespace { |
26 | | |
27 | | using ::jxl::fields_internal::VisitorBase; |
28 | | |
29 | | struct InitVisitor : public VisitorBase { |
30 | | Status Bits(const size_t /*unused*/, const uint32_t default_value, |
31 | 2.60M | uint32_t* JXL_RESTRICT value) override { |
32 | 2.60M | *value = default_value; |
33 | 2.60M | return true; |
34 | 2.60M | } |
35 | | |
36 | | Status U32(const U32Enc /*unused*/, const uint32_t default_value, |
37 | 6.54M | uint32_t* JXL_RESTRICT value) override { |
38 | 6.54M | *value = default_value; |
39 | 6.54M | return true; |
40 | 6.54M | } |
41 | | |
42 | | Status U64(const uint64_t default_value, |
43 | 359k | uint64_t* JXL_RESTRICT value) override { |
44 | 359k | *value = default_value; |
45 | 359k | return true; |
46 | 359k | } |
47 | | |
48 | 3.69M | Status Bool(bool default_value, bool* JXL_RESTRICT value) override { |
49 | 3.69M | *value = default_value; |
50 | 3.69M | return true; |
51 | 3.69M | } |
52 | | |
53 | 12.5M | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
54 | 12.5M | *value = default_value; |
55 | 12.5M | return true; |
56 | 12.5M | } |
57 | | |
58 | | // Always visit conditional fields to ensure they are initialized. |
59 | 6.50M | Status Conditional(bool /*condition*/) override { return true; } |
60 | | |
61 | | Status AllDefault(const Fields& /*fields*/, |
62 | 884k | bool* JXL_RESTRICT all_default) override { |
63 | | // Just initialize this field and don't skip initializing others. |
64 | 884k | JXL_RETURN_IF_ERROR(Bool(true, all_default)); |
65 | 884k | return false; |
66 | 884k | } |
67 | | |
68 | 2.05M | Status VisitNested(Fields* /*fields*/) override { |
69 | | // Avoid re-initializing nested bundles (their ctors already called |
70 | | // Bundle::Init for their fields). |
71 | 2.05M | return true; |
72 | 2.05M | } |
73 | | }; |
74 | | |
75 | | // Similar to InitVisitor, but also initializes nested fields. |
76 | | struct SetDefaultVisitor : public VisitorBase { |
77 | | Status Bits(const size_t /*unused*/, const uint32_t default_value, |
78 | 406k | uint32_t* JXL_RESTRICT value) override { |
79 | 406k | *value = default_value; |
80 | 406k | return true; |
81 | 406k | } |
82 | | |
83 | | Status U32(const U32Enc /*unused*/, const uint32_t default_value, |
84 | 602k | uint32_t* JXL_RESTRICT value) override { |
85 | 602k | *value = default_value; |
86 | 602k | return true; |
87 | 602k | } |
88 | | |
89 | | Status U64(const uint64_t default_value, |
90 | 43.3k | uint64_t* JXL_RESTRICT value) override { |
91 | 43.3k | *value = default_value; |
92 | 43.3k | return true; |
93 | 43.3k | } |
94 | | |
95 | 500k | Status Bool(bool default_value, bool* JXL_RESTRICT value) override { |
96 | 500k | *value = default_value; |
97 | 500k | return true; |
98 | 500k | } |
99 | | |
100 | 5.36M | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
101 | 5.36M | *value = default_value; |
102 | 5.36M | return true; |
103 | 5.36M | } |
104 | | |
105 | | // Always visit conditional fields to ensure they are initialized. |
106 | 756k | Status Conditional(bool /*condition*/) override { return true; } |
107 | | |
108 | | Status AllDefault(const Fields& /*fields*/, |
109 | 147k | bool* JXL_RESTRICT all_default) override { |
110 | | // Just initialize this field and don't skip initializing others. |
111 | 147k | JXL_RETURN_IF_ERROR(Bool(true, all_default)); |
112 | 147k | return false; |
113 | 147k | } |
114 | | }; |
115 | | |
116 | | class AllDefaultVisitor : public VisitorBase { |
117 | | public: |
118 | 60.6k | explicit AllDefaultVisitor() = default; |
119 | | |
120 | | Status Bits(const size_t bits, const uint32_t default_value, |
121 | 69.9k | uint32_t* JXL_RESTRICT value) override { |
122 | 69.9k | all_default_ &= *value == default_value; |
123 | 69.9k | return true; |
124 | 69.9k | } |
125 | | |
126 | | Status U32(const U32Enc /*unused*/, const uint32_t default_value, |
127 | 3.39k | uint32_t* JXL_RESTRICT value) override { |
128 | 3.39k | all_default_ &= *value == default_value; |
129 | 3.39k | return true; |
130 | 3.39k | } |
131 | | |
132 | | Status U64(const uint64_t default_value, |
133 | 1.34k | uint64_t* JXL_RESTRICT value) override { |
134 | 1.34k | all_default_ &= *value == default_value; |
135 | 1.34k | return true; |
136 | 1.34k | } |
137 | | |
138 | 180k | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
139 | 180k | all_default_ &= std::abs(*value - default_value) < 1E-6f; |
140 | 180k | return true; |
141 | 180k | } |
142 | | |
143 | | Status AllDefault(const Fields& /*fields*/, |
144 | 61.3k | bool* JXL_RESTRICT /*all_default*/) override { |
145 | | // Visit all fields so we can compute the actual all_default_ value. |
146 | 61.3k | return false; |
147 | 61.3k | } |
148 | | |
149 | 60.6k | bool AllDefault() const { return all_default_; } |
150 | | |
151 | | private: |
152 | | bool all_default_ = true; |
153 | | }; |
154 | | |
155 | | class ReadVisitor : public VisitorBase { |
156 | | public: |
157 | 237k | explicit ReadVisitor(BitReader* reader) : reader_(reader) {} |
158 | | |
159 | | Status Bits(const size_t bits, const uint32_t /*default_value*/, |
160 | 809k | uint32_t* JXL_RESTRICT value) override { |
161 | 809k | *value = BitsCoder::Read(bits, reader_); |
162 | 809k | if (!reader_->AllReadsWithinBounds()) { |
163 | 1.96k | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
164 | 1.96k | } |
165 | 807k | return true; |
166 | 809k | } |
167 | | |
168 | | Status U32(const U32Enc dist, const uint32_t /*default_value*/, |
169 | 440k | uint32_t* JXL_RESTRICT value) override { |
170 | 440k | *value = U32Coder::Read(dist, reader_); |
171 | 440k | if (!reader_->AllReadsWithinBounds()) { |
172 | 1.28k | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
173 | 1.28k | } |
174 | 439k | return true; |
175 | 440k | } |
176 | | |
177 | | Status U64(const uint64_t /*default_value*/, |
178 | 137k | uint64_t* JXL_RESTRICT value) override { |
179 | 137k | *value = U64Coder::Read(reader_); |
180 | 137k | if (!reader_->AllReadsWithinBounds()) { |
181 | 634 | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
182 | 634 | } |
183 | 136k | return true; |
184 | 137k | } |
185 | | |
186 | | Status F16(const float /*default_value*/, |
187 | 49.3k | float* JXL_RESTRICT value) override { |
188 | 49.3k | ok_ &= F16Coder::Read(reader_, value); |
189 | 49.3k | if (!reader_->AllReadsWithinBounds()) { |
190 | 703 | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
191 | 703 | } |
192 | 48.6k | return true; |
193 | 49.3k | } |
194 | | |
195 | 96.2k | void SetDefault(Fields* fields) override { Bundle::SetDefault(fields); } |
196 | | |
197 | 170k | bool IsReading() const override { return true; } |
198 | | |
199 | | // This never fails because visitors are expected to keep reading until |
200 | | // EndExtensions, see comment there. |
201 | 70.9k | Status BeginExtensions(uint64_t* JXL_RESTRICT extensions) override { |
202 | 70.9k | JXL_QUIET_RETURN_IF_ERROR(VisitorBase::BeginExtensions(extensions)); |
203 | 70.7k | if (*extensions == 0) return true; |
204 | | |
205 | | // For each nonzero bit, i.e. extension that is present: |
206 | 35.4k | for (uint64_t remaining_extensions = *extensions; remaining_extensions != 0; |
207 | 28.0k | remaining_extensions &= remaining_extensions - 1) { |
208 | 28.0k | const size_t idx_extension = |
209 | 28.0k | Num0BitsBelowLS1Bit_Nonzero(remaining_extensions); |
210 | | // Read additional U64 (one per extension) indicating the number of bits |
211 | | // (allows skipping individual extensions). |
212 | 28.0k | JXL_RETURN_IF_ERROR(U64(0, &extension_bits_[idx_extension])); |
213 | 27.7k | if (!SafeAdd(total_extension_bits_, extension_bits_[idx_extension], |
214 | 27.7k | total_extension_bits_)) { |
215 | 39 | return JXL_FAILURE("Extension bits overflowed, invalid codestream"); |
216 | 39 | } |
217 | 27.7k | } |
218 | | // Used by EndExtensions to skip past any _remaining_ extensions. |
219 | 7.38k | pos_after_ext_size_ = reader_->TotalBitsConsumed(); |
220 | 7.38k | JXL_ENSURE(pos_after_ext_size_ != 0); |
221 | 7.38k | return true; |
222 | 7.38k | } |
223 | | |
224 | 70.4k | Status EndExtensions() override { |
225 | 70.4k | JXL_QUIET_RETURN_IF_ERROR(VisitorBase::EndExtensions()); |
226 | | // Happens if extensions == 0: don't read size, done. |
227 | 70.4k | if (pos_after_ext_size_ == 0) return true; |
228 | | |
229 | | // Not enough bytes as set by BeginExtensions or earlier. Do not return |
230 | | // this as a JXL_FAILURE or false (which can also propagate to error |
231 | | // through e.g. JXL_RETURN_IF_ERROR), since this may be used while |
232 | | // silently checking whether there are enough bytes. If this case must be |
233 | | // treated as an error, reader_>Close() will do this, just like is already |
234 | | // done for non-extension fields. |
235 | 7.40k | if (!enough_bytes_) return true; |
236 | | |
237 | | // Skip new fields this (old?) decoder didn't know about, if any. |
238 | 7.40k | const size_t bits_read = reader_->TotalBitsConsumed(); |
239 | 7.40k | uint64_t end; |
240 | 7.40k | if (!SafeAdd(pos_after_ext_size_, total_extension_bits_, end)) { |
241 | 6 | return JXL_FAILURE("Invalid extension size, caused overflow"); |
242 | 6 | } |
243 | 7.39k | if (bits_read > end) { |
244 | 19 | return JXL_FAILURE("Read more extension bits than budgeted"); |
245 | 19 | } |
246 | 7.37k | const size_t remaining_bits = end - bits_read; |
247 | 7.37k | if (remaining_bits != 0) { |
248 | 3.40k | JXL_WARNING("Skipping %" PRIuS "-bit extension(s)", remaining_bits); |
249 | 3.40k | reader_->SkipBits(remaining_bits); |
250 | 3.40k | if (!reader_->AllReadsWithinBounds()) { |
251 | 877 | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
252 | 877 | } |
253 | 3.40k | } |
254 | 6.50k | return true; |
255 | 7.37k | } |
256 | | |
257 | 196k | Status OK() const { return ok_; } |
258 | | |
259 | | private: |
260 | | // Whether any error other than not enough bytes occurred. |
261 | | bool ok_ = true; |
262 | | |
263 | | // Whether there are enough input bytes to read from. |
264 | | bool enough_bytes_ = true; |
265 | | BitReader* const reader_; |
266 | | // May be 0 even if the corresponding extension is present. |
267 | | uint64_t extension_bits_[Bundle::kMaxExtensions] = {0}; |
268 | | uint64_t total_extension_bits_ = 0; |
269 | | size_t pos_after_ext_size_ = 0; // 0 iff extensions == 0. |
270 | | |
271 | | friend Status jxl::CheckHasEnoughBits(Visitor* /* visitor */, |
272 | | size_t /* bits */); |
273 | | }; |
274 | | |
275 | | class CanEncodeVisitor : public VisitorBase { |
276 | | public: |
277 | 5.12k | explicit CanEncodeVisitor() = default; |
278 | | |
279 | | Status Bits(const size_t bits, const uint32_t /*default_value*/, |
280 | 8.99k | uint32_t* JXL_RESTRICT value) override { |
281 | 8.99k | size_t encoded_bits = 0; |
282 | 8.99k | ok_ &= BitsCoder::CanEncode(bits, *value, &encoded_bits); |
283 | 8.99k | encoded_bits_ += encoded_bits; |
284 | 8.99k | return true; |
285 | 8.99k | } |
286 | | |
287 | | Status U32(const U32Enc enc, const uint32_t /*default_value*/, |
288 | 3.71k | uint32_t* JXL_RESTRICT value) override { |
289 | 3.71k | size_t encoded_bits = 0; |
290 | 3.71k | ok_ &= U32Coder::CanEncode(enc, *value, &encoded_bits); |
291 | 3.71k | encoded_bits_ += encoded_bits; |
292 | 3.71k | return true; |
293 | 3.71k | } |
294 | | |
295 | | Status U64(const uint64_t /*default_value*/, |
296 | 906 | uint64_t* JXL_RESTRICT value) override { |
297 | 906 | size_t encoded_bits = 0; |
298 | 906 | ok_ &= U64Coder::CanEncode(*value, &encoded_bits); |
299 | 906 | encoded_bits_ += encoded_bits; |
300 | 906 | return true; |
301 | 906 | } |
302 | | |
303 | | Status F16(const float /*default_value*/, |
304 | 0 | float* JXL_RESTRICT value) override { |
305 | 0 | size_t encoded_bits = 0; |
306 | 0 | ok_ &= F16Coder::CanEncode(*value, &encoded_bits); |
307 | 0 | encoded_bits_ += encoded_bits; |
308 | 0 | return true; |
309 | 0 | } |
310 | | |
311 | | Status AllDefault(const Fields& fields, |
312 | 1.54k | bool* JXL_RESTRICT all_default) override { |
313 | 1.54k | *all_default = Bundle::AllDefault(fields); |
314 | 1.54k | JXL_RETURN_IF_ERROR(Bool(true, all_default)); |
315 | 1.54k | return *all_default; |
316 | 1.54k | } |
317 | | |
318 | 623 | Status BeginExtensions(uint64_t* JXL_RESTRICT extensions) override { |
319 | 623 | JXL_QUIET_RETURN_IF_ERROR(VisitorBase::BeginExtensions(extensions)); |
320 | 623 | extensions_ = *extensions; |
321 | 623 | if (*extensions != 0) { |
322 | 0 | JXL_ENSURE(pos_after_ext_ == 0); |
323 | 0 | pos_after_ext_ = encoded_bits_; |
324 | 0 | JXL_ENSURE(pos_after_ext_ != 0); // visited "extensions" |
325 | 0 | } |
326 | 623 | return true; |
327 | 623 | } |
328 | | // EndExtensions = default. |
329 | | |
330 | | Status GetSizes(size_t* JXL_RESTRICT extension_bits, |
331 | 5.12k | size_t* JXL_RESTRICT total_bits) { |
332 | 5.12k | JXL_RETURN_IF_ERROR(ok_); |
333 | 5.12k | *extension_bits = 0; |
334 | 5.12k | *total_bits = encoded_bits_; |
335 | | // Only if extension field was nonzero will we encode their sizes. |
336 | 5.12k | if (pos_after_ext_ != 0) { |
337 | 0 | JXL_ENSURE(encoded_bits_ >= pos_after_ext_); |
338 | 0 | *extension_bits = encoded_bits_ - pos_after_ext_; |
339 | | // Also need to encode *extension_bits and bill it to *total_bits. |
340 | 0 | size_t encoded_bits = 0; |
341 | 0 | ok_ &= U64Coder::CanEncode(*extension_bits, &encoded_bits); |
342 | 0 | *total_bits += encoded_bits; |
343 | | |
344 | | // TODO(janwas): support encoding individual extension sizes. We |
345 | | // currently ascribe all bits to the first and send zeros for the |
346 | | // others. |
347 | 0 | for (size_t i = 1; i < hwy::PopCount(extensions_); ++i) { |
348 | 0 | encoded_bits = 0; |
349 | 0 | ok_ &= U64Coder::CanEncode(0, &encoded_bits); |
350 | 0 | *total_bits += encoded_bits; |
351 | 0 | } |
352 | 0 | } |
353 | 5.12k | return true; |
354 | 5.12k | } |
355 | | |
356 | | private: |
357 | | bool ok_ = true; |
358 | | size_t encoded_bits_ = 0; |
359 | | uint64_t extensions_ = 0; |
360 | | // Snapshot of encoded_bits_ after visiting the extension field, but NOT |
361 | | // including the hidden extension sizes. |
362 | | uint64_t pos_after_ext_ = 0; |
363 | | }; |
364 | | } // namespace |
365 | | |
366 | 3.06M | void Bundle::Init(Fields* fields) { |
367 | 3.06M | InitVisitor visitor; |
368 | 3.06M | if (!visitor.Visit(fields)) { |
369 | 0 | JXL_DEBUG_ABORT("Init should never fail"); |
370 | 0 | } |
371 | 3.06M | } |
372 | 96.2k | void Bundle::SetDefault(Fields* fields) { |
373 | 96.2k | SetDefaultVisitor visitor; |
374 | 96.2k | if (!visitor.Visit(fields)) { |
375 | 0 | JXL_DEBUG_ABORT("SetDefault should never fail"); |
376 | 0 | } |
377 | 96.2k | } |
378 | 60.6k | bool Bundle::AllDefault(const Fields& fields) { |
379 | 60.6k | AllDefaultVisitor visitor; |
380 | 60.6k | if (!visitor.VisitConst(fields)) { |
381 | 0 | JXL_DEBUG_ABORT("AllDefault should never fail"); |
382 | 0 | } |
383 | 60.6k | return visitor.AllDefault(); |
384 | 60.6k | } |
385 | | Status Bundle::CanEncode(const Fields& fields, size_t* extension_bits, |
386 | 5.12k | size_t* total_bits) { |
387 | 5.12k | CanEncodeVisitor visitor; |
388 | 5.12k | JXL_QUIET_RETURN_IF_ERROR(visitor.VisitConst(fields)); |
389 | 5.12k | JXL_QUIET_RETURN_IF_ERROR(visitor.GetSizes(extension_bits, total_bits)); |
390 | 5.12k | return true; |
391 | 5.12k | } |
392 | 199k | Status Bundle::Read(BitReader* reader, Fields* fields) { |
393 | 199k | ReadVisitor visitor(reader); |
394 | 199k | JXL_RETURN_IF_ERROR(visitor.Visit(fields)); |
395 | 196k | return visitor.OK(); |
396 | 199k | } |
397 | 37.6k | bool Bundle::CanRead(BitReader* reader, Fields* fields) { |
398 | 37.6k | ReadVisitor visitor(reader); |
399 | 37.6k | Status status = visitor.Visit(fields); |
400 | | // We are only checking here whether there are enough bytes. We still return |
401 | | // true for other errors because it means there are enough bytes to determine |
402 | | // there's an error. Use Read() to determine which error it is. |
403 | 37.6k | return status.code() != StatusCode::kNotEnoughBytes; |
404 | 37.6k | } |
405 | | |
406 | 0 | size_t BitsCoder::MaxEncodedBits(const size_t bits) { return bits; } |
407 | | |
408 | | Status BitsCoder::CanEncode(const size_t bits, const uint32_t value, |
409 | 8.99k | size_t* JXL_RESTRICT encoded_bits) { |
410 | 8.99k | *encoded_bits = bits; |
411 | 8.99k | if (value >= (1ULL << bits)) { |
412 | 0 | return JXL_FAILURE("Value %u too large for %" PRIu64 " bits", value, |
413 | 0 | static_cast<uint64_t>(bits)); |
414 | 0 | } |
415 | 8.99k | return true; |
416 | 8.99k | } |
417 | | |
418 | 809k | uint32_t BitsCoder::Read(const size_t bits, BitReader* JXL_RESTRICT reader) { |
419 | 809k | return reader->ReadBits(bits); |
420 | 809k | } |
421 | | |
422 | 283 | size_t U32Coder::MaxEncodedBits(const U32Enc enc) { |
423 | 283 | size_t extra_bits = 0; |
424 | 1.41k | for (uint32_t selector = 0; selector < 4; ++selector) { |
425 | 1.13k | const U32Distr d = enc.GetDistr(selector); |
426 | 1.13k | if (d.IsDirect()) { |
427 | 0 | continue; |
428 | 1.13k | } else { |
429 | 1.13k | extra_bits = std::max<size_t>(extra_bits, d.ExtraBits()); |
430 | 1.13k | } |
431 | 1.13k | } |
432 | 283 | return 2 + extra_bits; |
433 | 283 | } |
434 | | |
435 | | Status U32Coder::CanEncode(const U32Enc enc, const uint32_t value, |
436 | 3.90k | size_t* JXL_RESTRICT encoded_bits) { |
437 | 3.90k | uint32_t selector; |
438 | 3.90k | size_t total_bits; |
439 | 3.90k | const Status ok = ChooseSelector(enc, value, &selector, &total_bits); |
440 | 3.90k | *encoded_bits = ok ? total_bits : 0; |
441 | 3.90k | return ok; |
442 | 3.90k | } |
443 | | |
444 | 551k | uint32_t U32Coder::Read(const U32Enc enc, BitReader* JXL_RESTRICT reader) { |
445 | 551k | const uint32_t selector = reader->ReadFixedBits<2>(); |
446 | 551k | const U32Distr d = enc.GetDistr(selector); |
447 | 551k | if (d.IsDirect()) { |
448 | 381k | return d.Direct(); |
449 | 381k | } else { |
450 | 169k | return reader->ReadBits(d.ExtraBits()) + d.Offset(); |
451 | 169k | } |
452 | 551k | } |
453 | | |
454 | | Status U32Coder::ChooseSelector(const U32Enc enc, const uint32_t value, |
455 | | uint32_t* JXL_RESTRICT selector, |
456 | 8.48k | size_t* JXL_RESTRICT total_bits) { |
457 | 8.48k | const size_t bits_required = 32 - Num0BitsAboveMS1Bit(value); |
458 | 8.48k | JXL_ENSURE(bits_required <= 32); |
459 | | |
460 | 8.48k | *selector = 0; |
461 | 8.48k | *total_bits = 0; |
462 | | |
463 | | // It is difficult to verify whether Dist32Byte are sorted, so check all |
464 | | // selectors and keep the one with the fewest total_bits. |
465 | 8.48k | *total_bits = 64; // more than any valid encoding |
466 | 22.6k | for (uint32_t s = 0; s < 4; ++s) { |
467 | 19.4k | const U32Distr d = enc.GetDistr(s); |
468 | 19.4k | if (d.IsDirect()) { |
469 | 7.95k | if (d.Direct() == value) { |
470 | 5.28k | *selector = s; |
471 | 5.28k | *total_bits = 2; |
472 | 5.28k | return true; // Done, direct is always the best possible. |
473 | 5.28k | } |
474 | 2.67k | continue; |
475 | 7.95k | } |
476 | 11.5k | const size_t extra_bits = d.ExtraBits(); |
477 | 11.5k | const uint32_t offset = d.Offset(); |
478 | 11.5k | if (value < offset || value >= offset + (1ULL << extra_bits)) continue; |
479 | | |
480 | | // Better than prior encoding, remember it: |
481 | 5.82k | if (2 + extra_bits < *total_bits) { |
482 | 3.20k | *selector = s; |
483 | 3.20k | *total_bits = 2 + extra_bits; |
484 | 3.20k | } |
485 | 5.82k | } |
486 | | |
487 | 3.20k | if (*total_bits == 64) { |
488 | 0 | return JXL_FAILURE("No feasible selector for %u", value); |
489 | 0 | } |
490 | | |
491 | 3.20k | return true; |
492 | 3.20k | } |
493 | | |
494 | 139k | uint64_t U64Coder::Read(BitReader* JXL_RESTRICT reader) { |
495 | 139k | uint64_t selector = reader->ReadFixedBits<2>(); |
496 | 139k | if (selector == 0) { |
497 | 115k | return 0; |
498 | 115k | } |
499 | 24.2k | if (selector == 1) { |
500 | 11.2k | return 1 + reader->ReadFixedBits<4>(); |
501 | 11.2k | } |
502 | 12.9k | if (selector == 2) { |
503 | 6.97k | return 17 + reader->ReadFixedBits<8>(); |
504 | 6.97k | } |
505 | | |
506 | | // selector 3, varint, groups have first 12, then 8, and last 4 bits. |
507 | 6.01k | uint64_t result = reader->ReadFixedBits<12>(); |
508 | | |
509 | 6.01k | uint64_t shift = 12; |
510 | 13.4k | while (reader->ReadFixedBits<1>()) { |
511 | 8.06k | if (shift == 60) { |
512 | 588 | result |= static_cast<uint64_t>(reader->ReadFixedBits<4>()) << shift; |
513 | 588 | break; |
514 | 588 | } |
515 | 7.47k | result |= static_cast<uint64_t>(reader->ReadFixedBits<8>()) << shift; |
516 | 7.47k | shift += 8; |
517 | 7.47k | } |
518 | | |
519 | 6.01k | return result; |
520 | 12.9k | } |
521 | | |
522 | | // Can always encode, but useful because it also returns bit size. |
523 | 906 | Status U64Coder::CanEncode(uint64_t value, size_t* JXL_RESTRICT encoded_bits) { |
524 | 906 | if (value == 0) { |
525 | 809 | *encoded_bits = 2; // 2 selector bits |
526 | 809 | } else if (value <= 16) { |
527 | 97 | *encoded_bits = 2 + 4; // 2 selector bits + 4 payload bits |
528 | 97 | } else if (value <= 272) { |
529 | 0 | *encoded_bits = 2 + 8; // 2 selector bits + 8 payload bits |
530 | 0 | } else { |
531 | 0 | *encoded_bits = 2 + 12; // 2 selector bits + 12 payload bits |
532 | 0 | value >>= 12; |
533 | 0 | int shift = 12; |
534 | 0 | while (value > 0 && shift < 60) { |
535 | 0 | *encoded_bits += 1 + 8; // 1 continuation bit + 8 payload bits |
536 | 0 | value >>= 8; |
537 | 0 | shift += 8; |
538 | 0 | } |
539 | 0 | if (value > 0) { |
540 | | // This only could happen if shift == N - 4. |
541 | 0 | *encoded_bits += 1 + 4; // 1 continuation bit + 4 payload bits |
542 | 0 | } else { |
543 | 0 | *encoded_bits += 1; // 1 stop bit |
544 | 0 | } |
545 | 0 | } |
546 | | |
547 | 906 | return true; |
548 | 906 | } |
549 | | |
550 | | Status F16Coder::Read(BitReader* JXL_RESTRICT reader, |
551 | 77.2k | float* JXL_RESTRICT value) { |
552 | 77.2k | const uint32_t bits16 = reader->ReadFixedBits<16>(); |
553 | 77.2k | const uint32_t sign = bits16 >> 15; |
554 | 77.2k | const uint32_t biased_exp = (bits16 >> 10) & 0x1F; |
555 | 77.2k | const uint32_t mantissa = bits16 & 0x3FF; |
556 | | |
557 | 77.2k | if (JXL_UNLIKELY(biased_exp == 31)) { |
558 | 955 | return JXL_FAILURE("F16 infinity or NaN are not supported"); |
559 | 955 | } |
560 | | |
561 | | // Subnormal or zero |
562 | 76.2k | if (JXL_UNLIKELY(biased_exp == 0)) { |
563 | 35.1k | *value = (1.0f / 16384) * (mantissa * (1.0f / 1024)); |
564 | 35.1k | if (sign) *value = -*value; |
565 | 35.1k | return true; |
566 | 35.1k | } |
567 | | |
568 | | // Normalized: convert the representation directly (faster than ldexp/tables). |
569 | 41.1k | const uint32_t biased_exp32 = biased_exp + (127 - 15); |
570 | 41.1k | const uint32_t mantissa32 = mantissa << (23 - 10); |
571 | 41.1k | const uint32_t bits32 = (sign << 31) | (biased_exp32 << 23) | mantissa32; |
572 | 41.1k | memcpy(value, &bits32, sizeof(bits32)); |
573 | 41.1k | return true; |
574 | 76.2k | } |
575 | | |
576 | 0 | Status F16Coder::CanEncode(float value, size_t* JXL_RESTRICT encoded_bits) { |
577 | 0 | *encoded_bits = MaxEncodedBits(); |
578 | 0 | if (std::isnan(value) || std::isinf(value)) { |
579 | 0 | return JXL_FAILURE("Should not attempt to store NaN and infinity"); |
580 | 0 | } |
581 | 0 | return std::abs(value) <= 65504.0f; |
582 | 0 | } |
583 | | |
584 | 0 | Status CheckHasEnoughBits(Visitor* visitor, size_t bits) { |
585 | 0 | if (!visitor->IsReading()) return false; |
586 | 0 | ReadVisitor* rv = static_cast<ReadVisitor*>(visitor); |
587 | 0 | size_t have_bits = rv->reader_->TotalBytes() * kBitsPerByte; |
588 | 0 | size_t want_bits = bits + rv->reader_->TotalBitsConsumed(); |
589 | 0 | if (have_bits < want_bits) { |
590 | 0 | return JXL_NOT_ENOUGH_BYTES("Not enough bytes for header"); |
591 | 0 | } |
592 | 0 | return true; |
593 | 0 | } |
594 | | |
595 | | } // namespace jxl |