/src/libjxl/lib/jxl/entropy_coder.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/entropy_coder.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <cstdint> |
11 | | #include <vector> |
12 | | |
13 | | #include "lib/jxl/ac_context.h" |
14 | | #include "lib/jxl/base/status.h" |
15 | | #include "lib/jxl/coeff_order.h" |
16 | | #include "lib/jxl/coeff_order_fwd.h" |
17 | | #include "lib/jxl/dec_bit_reader.h" |
18 | | #include "lib/jxl/dec_context_map.h" |
19 | | #include "lib/jxl/fields.h" |
20 | | #include "lib/jxl/pack_signed.h" |
21 | | |
22 | | namespace jxl { |
23 | | |
24 | | Status DecodeBlockCtxMap(JxlMemoryManager* memory_manager, BitReader* br, |
25 | 0 | BlockCtxMap* block_ctx_map) { |
26 | 0 | auto& dct = block_ctx_map->dc_thresholds; |
27 | 0 | auto& qft = block_ctx_map->qf_thresholds; |
28 | 0 | auto& ctx_map = block_ctx_map->ctx_map; |
29 | 0 | bool is_default = static_cast<bool>(br->ReadFixedBits<1>()); |
30 | 0 | if (is_default) { |
31 | 0 | *block_ctx_map = BlockCtxMap(); |
32 | 0 | return true; |
33 | 0 | } |
34 | 0 | block_ctx_map->num_dc_ctxs = 1; |
35 | 0 | for (int j : {0, 1, 2}) { |
36 | 0 | dct[j].resize(br->ReadFixedBits<4>()); |
37 | 0 | block_ctx_map->num_dc_ctxs *= dct[j].size() + 1; |
38 | 0 | for (int& i : dct[j]) { |
39 | 0 | i = UnpackSigned(U32Coder::Read(kDCThresholdDist, br)); |
40 | 0 | } |
41 | 0 | } |
42 | 0 | qft.resize(br->ReadFixedBits<4>()); |
43 | 0 | for (uint32_t& i : qft) { |
44 | 0 | i = U32Coder::Read(kQFThresholdDist, br) + 1; |
45 | 0 | } |
46 | |
|
47 | 0 | if (block_ctx_map->num_dc_ctxs * (qft.size() + 1) > 64) { |
48 | 0 | return JXL_FAILURE("Invalid block context map: too big"); |
49 | 0 | } |
50 | | |
51 | 0 | ctx_map.resize(3 * kNumOrders * block_ctx_map->num_dc_ctxs * |
52 | 0 | (qft.size() + 1)); |
53 | 0 | JXL_RETURN_IF_ERROR( |
54 | 0 | DecodeContextMap(memory_manager, &ctx_map, &block_ctx_map->num_ctxs, br)); |
55 | 0 | if (block_ctx_map->num_ctxs > 16) { |
56 | 0 | return JXL_FAILURE("Invalid block context map: too many distinct contexts"); |
57 | 0 | } |
58 | 0 | return true; |
59 | 0 | } |
60 | | |
61 | | constexpr uint8_t BlockCtxMap::kDefaultCtxMap[]; // from ac_context.h |
62 | | |
63 | | } // namespace jxl |