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