/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 | 5.97M | uint32_t* JXL_RESTRICT value) override { |
26 | 5.97M | *value = default_value; |
27 | 5.97M | return true; |
28 | 5.97M | } |
29 | | |
30 | | Status U32(const U32Enc /*unused*/, const uint32_t default_value, |
31 | 13.3M | uint32_t* JXL_RESTRICT value) override { |
32 | 13.3M | *value = default_value; |
33 | 13.3M | return true; |
34 | 13.3M | } |
35 | | |
36 | | Status U64(const uint64_t default_value, |
37 | 854k | uint64_t* JXL_RESTRICT value) override { |
38 | 854k | *value = default_value; |
39 | 854k | return true; |
40 | 854k | } |
41 | | |
42 | 7.24M | Status Bool(bool default_value, bool* JXL_RESTRICT value) override { |
43 | 7.24M | *value = default_value; |
44 | 7.24M | return true; |
45 | 7.24M | } |
46 | | |
47 | 24.2M | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
48 | 24.2M | *value = default_value; |
49 | 24.2M | return true; |
50 | 24.2M | } |
51 | | |
52 | | // Always visit conditional fields to ensure they are initialized. |
53 | 14.0M | Status Conditional(bool /*condition*/) override { return true; } |
54 | | |
55 | | Status AllDefault(const Fields& /*fields*/, |
56 | 1.62M | bool* JXL_RESTRICT all_default) override { |
57 | | // Just initialize this field and don't skip initializing others. |
58 | 1.62M | JXL_RETURN_IF_ERROR(Bool(true, all_default)); |
59 | 1.62M | return false; |
60 | 1.62M | } |
61 | | |
62 | 4.50M | Status VisitNested(Fields* /*fields*/) override { |
63 | | // Avoid re-initializing nested bundles (their ctors already called |
64 | | // Bundle::Init for their fields). |
65 | 4.50M | return true; |
66 | 4.50M | } |
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 | 893k | uint32_t* JXL_RESTRICT value) override { |
73 | 893k | *value = default_value; |
74 | 893k | return true; |
75 | 893k | } |
76 | | |
77 | | Status U32(const U32Enc /*unused*/, const uint32_t default_value, |
78 | 1.71M | uint32_t* JXL_RESTRICT value) override { |
79 | 1.71M | *value = default_value; |
80 | 1.71M | return true; |
81 | 1.71M | } |
82 | | |
83 | | Status U64(const uint64_t default_value, |
84 | 71.7k | uint64_t* JXL_RESTRICT value) override { |
85 | 71.7k | *value = default_value; |
86 | 71.7k | return true; |
87 | 71.7k | } |
88 | | |
89 | 1.28M | Status Bool(bool default_value, bool* JXL_RESTRICT value) override { |
90 | 1.28M | *value = default_value; |
91 | 1.28M | return true; |
92 | 1.28M | } |
93 | | |
94 | 18.4M | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
95 | 18.4M | *value = default_value; |
96 | 18.4M | return true; |
97 | 18.4M | } |
98 | | |
99 | | // Always visit conditional fields to ensure they are initialized. |
100 | 1.81M | Status Conditional(bool /*condition*/) override { return true; } |
101 | | |
102 | | Status AllDefault(const Fields& /*fields*/, |
103 | 434k | bool* JXL_RESTRICT all_default) override { |
104 | | // Just initialize this field and don't skip initializing others. |
105 | 434k | JXL_RETURN_IF_ERROR(Bool(true, all_default)); |
106 | 434k | return false; |
107 | 434k | } |
108 | | }; |
109 | | |
110 | | class AllDefaultVisitor : public VisitorBase { |
111 | | public: |
112 | 112k | explicit AllDefaultVisitor() = default; |
113 | | |
114 | | Status Bits(const size_t bits, const uint32_t default_value, |
115 | 112k | uint32_t* JXL_RESTRICT value) override { |
116 | 112k | all_default_ &= *value == default_value; |
117 | 112k | return true; |
118 | 112k | } |
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 | 336k | Status F16(const float default_value, float* JXL_RESTRICT value) override { |
133 | 336k | all_default_ &= std::abs(*value - default_value) < 1E-6f; |
134 | 336k | return true; |
135 | 336k | } |
136 | | |
137 | | Status AllDefault(const Fields& /*fields*/, |
138 | 112k | bool* JXL_RESTRICT /*all_default*/) override { |
139 | | // Visit all fields so we can compute the actual all_default_ value. |
140 | 112k | return false; |
141 | 112k | } |
142 | | |
143 | 112k | bool AllDefault() const { return all_default_; } |
144 | | |
145 | | private: |
146 | | bool all_default_ = true; |
147 | | }; |
148 | | |
149 | | class ReadVisitor : public VisitorBase { |
150 | | public: |
151 | 663k | explicit ReadVisitor(BitReader* reader) : reader_(reader) {} |
152 | | |
153 | | Status Bits(const size_t bits, const uint32_t /*default_value*/, |
154 | 3.02M | uint32_t* JXL_RESTRICT value) override { |
155 | 3.02M | *value = BitsCoder::Read(bits, reader_); |
156 | 3.02M | if (!reader_->AllReadsWithinBounds()) { |
157 | 10.5k | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
158 | 10.5k | "Not enough bytes for header"); |
159 | 10.5k | } |
160 | 3.01M | return true; |
161 | 3.02M | } |
162 | | |
163 | | Status U32(const U32Enc dist, const uint32_t /*default_value*/, |
164 | 1.53M | uint32_t* JXL_RESTRICT value) override { |
165 | 1.53M | *value = U32Coder::Read(dist, reader_); |
166 | 1.53M | if (!reader_->AllReadsWithinBounds()) { |
167 | 14.1k | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
168 | 14.1k | "Not enough bytes for header"); |
169 | 14.1k | } |
170 | 1.51M | return true; |
171 | 1.53M | } |
172 | | |
173 | | Status U64(const uint64_t /*default_value*/, |
174 | 498k | uint64_t* JXL_RESTRICT value) override { |
175 | 498k | *value = U64Coder::Read(reader_); |
176 | 498k | if (!reader_->AllReadsWithinBounds()) { |
177 | 2.93k | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
178 | 2.93k | "Not enough bytes for header"); |
179 | 2.93k | } |
180 | 495k | return true; |
181 | 498k | } |
182 | | |
183 | | Status F16(const float /*default_value*/, |
184 | 240k | float* JXL_RESTRICT value) override { |
185 | 240k | ok_ &= F16Coder::Read(reader_, value); |
186 | 240k | if (!reader_->AllReadsWithinBounds()) { |
187 | 14.1k | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
188 | 14.1k | "Not enough bytes for header"); |
189 | 14.1k | } |
190 | 226k | return true; |
191 | 240k | } |
192 | | |
193 | 299k | void SetDefault(Fields* fields) override { Bundle::SetDefault(fields); } |
194 | | |
195 | 557k | 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 | 223k | Status BeginExtensions(uint64_t* JXL_RESTRICT extensions) override { |
200 | 223k | JXL_QUIET_RETURN_IF_ERROR(VisitorBase::BeginExtensions(extensions)); |
201 | 222k | if (*extensions == 0) return true; |
202 | | |
203 | | // For each nonzero bit, i.e. extension that is present: |
204 | 208k | for (uint64_t remaining_extensions = *extensions; remaining_extensions != 0; |
205 | 173k | remaining_extensions &= remaining_extensions - 1) { |
206 | 173k | const size_t idx_extension = |
207 | 173k | Num0BitsBelowLS1Bit_Nonzero(remaining_extensions); |
208 | | // Read additional U64 (one per extension) indicating the number of bits |
209 | | // (allows skipping individual extensions). |
210 | 173k | JXL_RETURN_IF_ERROR(U64(0, &extension_bits_[idx_extension])); |
211 | 171k | if (!SafeAdd(total_extension_bits_, extension_bits_[idx_extension], |
212 | 171k | total_extension_bits_)) { |
213 | 33 | return JXL_FAILURE("Extension bits overflowed, invalid codestream"); |
214 | 33 | } |
215 | 171k | } |
216 | | // Used by EndExtensions to skip past any _remaining_ extensions. |
217 | 35.0k | pos_after_ext_size_ = reader_->TotalBitsConsumed(); |
218 | 35.0k | JXL_ENSURE(pos_after_ext_size_ != 0); |
219 | 35.0k | return true; |
220 | 35.0k | } |
221 | | |
222 | 221k | Status EndExtensions() override { |
223 | 221k | JXL_QUIET_RETURN_IF_ERROR(VisitorBase::EndExtensions()); |
224 | | // Happens if extensions == 0: don't read size, done. |
225 | 221k | 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 | 35.0k | if (!enough_bytes_) return true; |
234 | | |
235 | | // Skip new fields this (old?) decoder didn't know about, if any. |
236 | 35.0k | const size_t bits_read = reader_->TotalBitsConsumed(); |
237 | 35.0k | uint64_t end; |
238 | 35.0k | if (!SafeAdd(pos_after_ext_size_, total_extension_bits_, end)) { |
239 | 3 | return JXL_FAILURE("Invalid extension size, caused overflow"); |
240 | 3 | } |
241 | 35.0k | if (bits_read > end) { |
242 | 10 | return JXL_FAILURE("Read more extension bits than budgeted"); |
243 | 10 | } |
244 | 35.0k | const size_t remaining_bits = end - bits_read; |
245 | 35.0k | if (remaining_bits != 0) { |
246 | 30.9k | JXL_WARNING("Skipping %" PRIuS "-bit extension(s)", remaining_bits); |
247 | 30.9k | reader_->SkipBits(remaining_bits); |
248 | 30.9k | if (!reader_->AllReadsWithinBounds()) { |
249 | 22.6k | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
250 | 22.6k | "Not enough bytes for header"); |
251 | 22.6k | } |
252 | 30.9k | } |
253 | 12.3k | return true; |
254 | 35.0k | } |
255 | | |
256 | 453k | 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_ENSURE(pos_after_ext_ == 0); |
371 | 0 | pos_after_ext_ = encoded_bits_; |
372 | 0 | JXL_ENSURE(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_ENSURE(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.37M | void Bundle::Init(Fields* fields) { |
415 | 6.37M | InitVisitor visitor; |
416 | 6.37M | if (!visitor.Visit(fields)) { |
417 | 0 | JXL_DEBUG_ABORT("Init should never fail"); |
418 | 0 | } |
419 | 6.37M | } |
420 | 299k | void Bundle::SetDefault(Fields* fields) { |
421 | 299k | SetDefaultVisitor visitor; |
422 | 299k | if (!visitor.Visit(fields)) { |
423 | 0 | JXL_DEBUG_ABORT("SetDefault should never fail"); |
424 | 0 | } |
425 | 299k | } |
426 | 112k | bool Bundle::AllDefault(const Fields& fields) { |
427 | 112k | AllDefaultVisitor visitor; |
428 | 112k | if (!visitor.VisitConst(fields)) { |
429 | 0 | JXL_DEBUG_ABORT("AllDefault should never fail"); |
430 | 0 | } |
431 | 112k | return visitor.AllDefault(); |
432 | 112k | } |
433 | 0 | size_t Bundle::MaxBits(const Fields& fields) { |
434 | 0 | MaxBitsVisitor visitor; |
435 | 0 | Status ret = visitor.VisitConst(fields); |
436 | 0 | (void)ret; |
437 | 0 | JXL_DASSERT(ret); |
438 | 0 | return visitor.MaxBits(); |
439 | 0 | } |
440 | | Status Bundle::CanEncode(const Fields& fields, size_t* extension_bits, |
441 | 0 | size_t* total_bits) { |
442 | 0 | CanEncodeVisitor visitor; |
443 | 0 | JXL_QUIET_RETURN_IF_ERROR(visitor.VisitConst(fields)); |
444 | 0 | JXL_QUIET_RETURN_IF_ERROR(visitor.GetSizes(extension_bits, total_bits)); |
445 | 0 | return true; |
446 | 0 | } |
447 | 470k | Status Bundle::Read(BitReader* reader, Fields* fields) { |
448 | 470k | ReadVisitor visitor(reader); |
449 | 470k | JXL_RETURN_IF_ERROR(visitor.Visit(fields)); |
450 | 453k | return visitor.OK(); |
451 | 470k | } |
452 | 193k | bool Bundle::CanRead(BitReader* reader, Fields* fields) { |
453 | 193k | ReadVisitor visitor(reader); |
454 | 193k | Status status = visitor.Visit(fields); |
455 | | // We are only checking here whether there are enough bytes. We still return |
456 | | // true for other errors because it means there are enough bytes to determine |
457 | | // there's an error. Use Read() to determine which error it is. |
458 | 193k | return status.code() != StatusCode::kNotEnoughBytes; |
459 | 193k | } |
460 | | |
461 | 0 | size_t BitsCoder::MaxEncodedBits(const size_t bits) { return bits; } |
462 | | |
463 | | Status BitsCoder::CanEncode(const size_t bits, const uint32_t value, |
464 | 0 | size_t* JXL_RESTRICT encoded_bits) { |
465 | 0 | *encoded_bits = bits; |
466 | 0 | if (value >= (1ULL << bits)) { |
467 | 0 | return JXL_FAILURE("Value %u too large for %" PRIu64 " bits", value, |
468 | 0 | static_cast<uint64_t>(bits)); |
469 | 0 | } |
470 | 0 | return true; |
471 | 0 | } |
472 | | |
473 | 3.02M | uint32_t BitsCoder::Read(const size_t bits, BitReader* JXL_RESTRICT reader) { |
474 | 3.02M | return reader->ReadBits(bits); |
475 | 3.02M | } |
476 | | |
477 | 0 | size_t U32Coder::MaxEncodedBits(const U32Enc enc) { |
478 | 0 | size_t extra_bits = 0; |
479 | 0 | for (uint32_t selector = 0; selector < 4; ++selector) { |
480 | 0 | const U32Distr d = enc.GetDistr(selector); |
481 | 0 | if (d.IsDirect()) { |
482 | 0 | continue; |
483 | 0 | } else { |
484 | 0 | extra_bits = std::max<size_t>(extra_bits, d.ExtraBits()); |
485 | 0 | } |
486 | 0 | } |
487 | 0 | return 2 + extra_bits; |
488 | 0 | } |
489 | | |
490 | | Status U32Coder::CanEncode(const U32Enc enc, const uint32_t value, |
491 | 0 | size_t* JXL_RESTRICT encoded_bits) { |
492 | 0 | uint32_t selector; |
493 | 0 | size_t total_bits; |
494 | 0 | const Status ok = ChooseSelector(enc, value, &selector, &total_bits); |
495 | 0 | *encoded_bits = ok ? total_bits : 0; |
496 | 0 | return ok; |
497 | 0 | } |
498 | | |
499 | 2.12M | uint32_t U32Coder::Read(const U32Enc enc, BitReader* JXL_RESTRICT reader) { |
500 | 2.12M | const uint32_t selector = reader->ReadFixedBits<2>(); |
501 | 2.12M | const U32Distr d = enc.GetDistr(selector); |
502 | 2.12M | if (d.IsDirect()) { |
503 | 1.17M | return d.Direct(); |
504 | 1.17M | } else { |
505 | 947k | return reader->ReadBits(d.ExtraBits()) + d.Offset(); |
506 | 947k | } |
507 | 2.12M | } |
508 | | |
509 | | Status U32Coder::ChooseSelector(const U32Enc enc, const uint32_t value, |
510 | | uint32_t* JXL_RESTRICT selector, |
511 | 0 | size_t* JXL_RESTRICT total_bits) { |
512 | 0 | const size_t bits_required = 32 - Num0BitsAboveMS1Bit(value); |
513 | 0 | JXL_ENSURE(bits_required <= 32); |
514 | | |
515 | 0 | *selector = 0; |
516 | 0 | *total_bits = 0; |
517 | | |
518 | | // It is difficult to verify whether Dist32Byte are sorted, so check all |
519 | | // selectors and keep the one with the fewest total_bits. |
520 | 0 | *total_bits = 64; // more than any valid encoding |
521 | 0 | for (uint32_t s = 0; s < 4; ++s) { |
522 | 0 | const U32Distr d = enc.GetDistr(s); |
523 | 0 | if (d.IsDirect()) { |
524 | 0 | if (d.Direct() == value) { |
525 | 0 | *selector = s; |
526 | 0 | *total_bits = 2; |
527 | 0 | return true; // Done, direct is always the best possible. |
528 | 0 | } |
529 | 0 | continue; |
530 | 0 | } |
531 | 0 | const size_t extra_bits = d.ExtraBits(); |
532 | 0 | const uint32_t offset = d.Offset(); |
533 | 0 | if (value < offset || value >= offset + (1ULL << extra_bits)) continue; |
534 | | |
535 | | // Better than prior encoding, remember it: |
536 | 0 | if (2 + extra_bits < *total_bits) { |
537 | 0 | *selector = s; |
538 | 0 | *total_bits = 2 + extra_bits; |
539 | 0 | } |
540 | 0 | } |
541 | | |
542 | 0 | if (*total_bits == 64) { |
543 | 0 | return JXL_FAILURE("No feasible selector for %u", value); |
544 | 0 | } |
545 | | |
546 | 0 | return true; |
547 | 0 | } |
548 | | |
549 | 512k | uint64_t U64Coder::Read(BitReader* JXL_RESTRICT reader) { |
550 | 512k | uint64_t selector = reader->ReadFixedBits<2>(); |
551 | 512k | if (selector == 0) { |
552 | 360k | return 0; |
553 | 360k | } |
554 | 151k | if (selector == 1) { |
555 | 53.1k | return 1 + reader->ReadFixedBits<4>(); |
556 | 53.1k | } |
557 | 98.3k | if (selector == 2) { |
558 | 36.4k | return 17 + reader->ReadFixedBits<8>(); |
559 | 36.4k | } |
560 | | |
561 | | // selector 3, varint, groups have first 12, then 8, and last 4 bits. |
562 | 61.9k | uint64_t result = reader->ReadFixedBits<12>(); |
563 | | |
564 | 61.9k | uint64_t shift = 12; |
565 | 102k | while (reader->ReadFixedBits<1>()) { |
566 | 41.4k | if (shift == 60) { |
567 | 841 | result |= static_cast<uint64_t>(reader->ReadFixedBits<4>()) << shift; |
568 | 841 | break; |
569 | 841 | } |
570 | 40.5k | result |= static_cast<uint64_t>(reader->ReadFixedBits<8>()) << shift; |
571 | 40.5k | shift += 8; |
572 | 40.5k | } |
573 | | |
574 | 61.9k | return result; |
575 | 98.3k | } |
576 | | |
577 | | // Can always encode, but useful because it also returns bit size. |
578 | 0 | Status U64Coder::CanEncode(uint64_t value, size_t* JXL_RESTRICT encoded_bits) { |
579 | 0 | if (value == 0) { |
580 | 0 | *encoded_bits = 2; // 2 selector bits |
581 | 0 | } else if (value <= 16) { |
582 | 0 | *encoded_bits = 2 + 4; // 2 selector bits + 4 payload bits |
583 | 0 | } else if (value <= 272) { |
584 | 0 | *encoded_bits = 2 + 8; // 2 selector bits + 8 payload bits |
585 | 0 | } else { |
586 | 0 | *encoded_bits = 2 + 12; // 2 selector bits + 12 payload bits |
587 | 0 | value >>= 12; |
588 | 0 | int shift = 12; |
589 | 0 | while (value > 0 && shift < 60) { |
590 | 0 | *encoded_bits += 1 + 8; // 1 continuation bit + 8 payload bits |
591 | 0 | value >>= 8; |
592 | 0 | shift += 8; |
593 | 0 | } |
594 | 0 | if (value > 0) { |
595 | | // This only could happen if shift == N - 4. |
596 | 0 | *encoded_bits += 1 + 4; // 1 continuation bit + 4 payload bits |
597 | 0 | } else { |
598 | 0 | *encoded_bits += 1; // 1 stop bit |
599 | 0 | } |
600 | 0 | } |
601 | |
|
602 | 0 | return true; |
603 | 0 | } |
604 | | |
605 | | Status F16Coder::Read(BitReader* JXL_RESTRICT reader, |
606 | 271k | float* JXL_RESTRICT value) { |
607 | 271k | const uint32_t bits16 = reader->ReadFixedBits<16>(); |
608 | 271k | const uint32_t sign = bits16 >> 15; |
609 | 271k | const uint32_t biased_exp = (bits16 >> 10) & 0x1F; |
610 | 271k | const uint32_t mantissa = bits16 & 0x3FF; |
611 | | |
612 | 271k | if (JXL_UNLIKELY(biased_exp == 31)) { |
613 | 4.18k | return JXL_FAILURE("F16 infinity or NaN are not supported"); |
614 | 4.18k | } |
615 | | |
616 | | // Subnormal or zero |
617 | 266k | if (JXL_UNLIKELY(biased_exp == 0)) { |
618 | 85.6k | *value = (1.0f / 16384) * (mantissa * (1.0f / 1024)); |
619 | 85.6k | if (sign) *value = -*value; |
620 | 85.6k | return true; |
621 | 85.6k | } |
622 | | |
623 | | // Normalized: convert the representation directly (faster than ldexp/tables). |
624 | 181k | const uint32_t biased_exp32 = biased_exp + (127 - 15); |
625 | 181k | const uint32_t mantissa32 = mantissa << (23 - 10); |
626 | 181k | const uint32_t bits32 = (sign << 31) | (biased_exp32 << 23) | mantissa32; |
627 | 181k | memcpy(value, &bits32, sizeof(bits32)); |
628 | 181k | return true; |
629 | 266k | } |
630 | | |
631 | 0 | Status F16Coder::CanEncode(float value, size_t* JXL_RESTRICT encoded_bits) { |
632 | 0 | *encoded_bits = MaxEncodedBits(); |
633 | 0 | if (std::isnan(value) || std::isinf(value)) { |
634 | 0 | return JXL_FAILURE("Should not attempt to store NaN and infinity"); |
635 | 0 | } |
636 | 0 | return std::abs(value) <= 65504.0f; |
637 | 0 | } |
638 | | |
639 | 0 | Status CheckHasEnoughBits(Visitor* visitor, size_t bits) { |
640 | 0 | if (!visitor->IsReading()) return false; |
641 | 0 | ReadVisitor* rv = static_cast<ReadVisitor*>(visitor); |
642 | 0 | size_t have_bits = rv->reader_->TotalBytes() * kBitsPerByte; |
643 | 0 | size_t want_bits = bits + rv->reader_->TotalBitsConsumed(); |
644 | 0 | if (have_bits < want_bits) { |
645 | 0 | return JXL_STATUS(StatusCode::kNotEnoughBytes, |
646 | 0 | "Not enough bytes for header"); |
647 | 0 | } |
648 | 0 | return true; |
649 | 0 | } |
650 | | |
651 | | } // namespace jxl |