/src/libjxl/lib/jxl/dec_modular.h
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 | | #ifndef LIB_JXL_DEC_MODULAR_H_ |
7 | | #define LIB_JXL_DEC_MODULAR_H_ |
8 | | |
9 | | #include <jxl/memory_manager.h> |
10 | | |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <string> |
14 | | #include <vector> |
15 | | |
16 | | #include "lib/jxl/base/data_parallel.h" |
17 | | #include "lib/jxl/base/rect.h" |
18 | | #include "lib/jxl/base/status.h" |
19 | | #include "lib/jxl/dec_bit_reader.h" |
20 | | #include "lib/jxl/dec_cache.h" |
21 | | #include "lib/jxl/frame_dimensions.h" |
22 | | #include "lib/jxl/frame_header.h" |
23 | | #include "lib/jxl/modular/encoding/dec_ma.h" |
24 | | #include "lib/jxl/modular/encoding/encoding.h" |
25 | | #include "lib/jxl/modular/modular_image.h" |
26 | | #include "lib/jxl/quant_weights.h" |
27 | | #include "lib/jxl/render_pipeline/render_pipeline.h" |
28 | | |
29 | | namespace jxl { |
30 | | |
31 | | struct ModularStreamId { |
32 | | enum class Kind { |
33 | | GlobalData, |
34 | | VarDCTDC, |
35 | | ModularDC, |
36 | | ACMetadata, |
37 | | QuantTable, |
38 | | ModularAC |
39 | | }; |
40 | | Kind kind; |
41 | | size_t quant_table_id; |
42 | | size_t group_id; // DC or AC group id. |
43 | | size_t pass_id; // Only for kModularAC. |
44 | 37.2k | size_t ID(const FrameDimensions& frame_dim) const { |
45 | 37.2k | size_t id = 0; |
46 | 37.2k | switch (kind) { |
47 | 25.5k | case Kind::GlobalData: |
48 | 25.5k | id = 0; |
49 | 25.5k | break; |
50 | 5.24k | case Kind::VarDCTDC: |
51 | 5.24k | id = 1 + group_id; |
52 | 5.24k | break; |
53 | 718 | case Kind::ModularDC: |
54 | 718 | id = 1 + frame_dim.num_dc_groups + group_id; |
55 | 718 | break; |
56 | 3.69k | case Kind::ACMetadata: |
57 | 3.69k | id = 1 + 2 * frame_dim.num_dc_groups + group_id; |
58 | 3.69k | break; |
59 | 250 | case Kind::QuantTable: |
60 | 250 | id = 1 + 3 * frame_dim.num_dc_groups + quant_table_id; |
61 | 250 | break; |
62 | 1.84k | case Kind::ModularAC: |
63 | 1.84k | id = 1 + 3 * frame_dim.num_dc_groups + kNumQuantTables + |
64 | 1.84k | frame_dim.num_groups * pass_id + group_id; |
65 | 1.84k | break; |
66 | 37.2k | }; |
67 | 37.2k | return id; |
68 | 37.2k | } |
69 | 25.4k | static ModularStreamId Global() { |
70 | 25.4k | return ModularStreamId{Kind::GlobalData, 0, 0, 0}; |
71 | 25.4k | } |
72 | 5.24k | static ModularStreamId VarDCTDC(size_t group_id) { |
73 | 5.24k | return ModularStreamId{Kind::VarDCTDC, 0, group_id, 0}; |
74 | 5.24k | } |
75 | 23.7k | static ModularStreamId ModularDC(size_t group_id) { |
76 | 23.7k | return ModularStreamId{Kind::ModularDC, 0, group_id, 0}; |
77 | 23.7k | } |
78 | 3.69k | static ModularStreamId ACMetadata(size_t group_id) { |
79 | 3.69k | return ModularStreamId{Kind::ACMetadata, 0, group_id, 0}; |
80 | 3.69k | } |
81 | 250 | static StatusOr<ModularStreamId> QuantTable(size_t quant_table_id) { |
82 | 250 | JXL_ENSURE(quant_table_id < kNumQuantTables); |
83 | 250 | return ModularStreamId{Kind::QuantTable, quant_table_id, 0, 0}; |
84 | 250 | } |
85 | 25.7k | static ModularStreamId ModularAC(size_t group_id, size_t pass_id) { |
86 | 25.7k | return ModularStreamId{Kind::ModularAC, 0, group_id, pass_id}; |
87 | 25.7k | } |
88 | 283 | static size_t Num(const FrameDimensions& frame_dim, size_t passes) { |
89 | 283 | return ModularAC(0, passes).ID(frame_dim); |
90 | 283 | } |
91 | | std::string DebugString() const; |
92 | | }; |
93 | | |
94 | | class ModularFrameDecoder { |
95 | | public: |
96 | | explicit ModularFrameDecoder(JxlMemoryManager* memory_manager) |
97 | 45.3k | : memory_manager_(memory_manager), full_image(memory_manager) {} |
98 | 25.9k | void Init(const FrameDimensions& new_frame_dim) { frame_dim = new_frame_dim; } |
99 | | Status DecodeGlobalInfo(BitReader* reader, const FrameHeader& frame_header, |
100 | | bool allow_truncated_group); |
101 | | Status DecodeGroup(const FrameHeader& frame_header, const Rect& rect, |
102 | | BitReader* reader, int minShift, int maxShift, |
103 | | const ModularStreamId& stream, bool zerofill, |
104 | | PassesDecoderState* dec_state, |
105 | | RenderPipelineInput* render_pipeline_input, |
106 | | bool allow_truncated, bool* should_run_pipeline = nullptr); |
107 | | // Decodes a VarDCT DC group (`group_id`) from the given `reader`. |
108 | | Status DecodeVarDCTDC(const FrameHeader& frame_header, size_t group_id, |
109 | | BitReader* reader, PassesDecoderState* dec_state); |
110 | | // Decodes a VarDCT AC Metadata group (`group_id`) from the given `reader`. |
111 | | Status DecodeAcMetadata(const FrameHeader& frame_header, size_t group_id, |
112 | | BitReader* reader, PassesDecoderState* dec_state); |
113 | | // Decodes a RAW quant table from `br` into the given `encoding`, of size |
114 | | // `required_size_x x required_size_y`. If `modular_frame_decoder` is passed, |
115 | | // its global tree is used, otherwise no global tree is used. |
116 | | static Status DecodeQuantTable(JxlMemoryManager* memory_manager, |
117 | | size_t required_size_x, size_t required_size_y, |
118 | | BitReader* br, QuantEncoding* encoding, |
119 | | size_t idx, |
120 | | ModularFrameDecoder* modular_frame_decoder); |
121 | | // if inplace is true, this can only be called once |
122 | | // if it is false, it can be called multiple times (e.g. for progressive |
123 | | // steps) |
124 | | Status FinalizeDecoding(const FrameHeader& frame_header, |
125 | | PassesDecoderState* dec_state, jxl::ThreadPool* pool, |
126 | | bool inplace); |
127 | 0 | bool have_dc() const { return have_something; } |
128 | | void MaybeDropFullImage(); |
129 | 44.2k | bool UsesFullImage() const { return use_full_image; } |
130 | 25.2k | JxlMemoryManager* memory_manager() const { return memory_manager_; } |
131 | | |
132 | | private: |
133 | | Status ModularImageToDecodedRect(const FrameHeader& frame_header, Image& gi, |
134 | | PassesDecoderState* dec_state, |
135 | | jxl::ThreadPool* pool, |
136 | | RenderPipelineInput& render_pipeline_input, |
137 | | Rect modular_rect) const; |
138 | | JxlMemoryManager* memory_manager_; |
139 | | Image full_image; |
140 | | std::vector<Transform> global_transform; |
141 | | FrameDimensions frame_dim; |
142 | | bool do_color; |
143 | | bool have_something; |
144 | | bool use_full_image = true; |
145 | | bool all_same_shift; |
146 | | Tree tree; |
147 | | ANSCode code; |
148 | | std::vector<uint8_t> context_map; |
149 | | GroupHeader global_header; |
150 | | }; |
151 | | |
152 | | } // namespace jxl |
153 | | |
154 | | #endif // LIB_JXL_DEC_MODULAR_H_ |