/src/libjxl/lib/jxl/modular/encoding/encoding.cc
Line | Count | Source |
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/modular/encoding/encoding.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <array> |
12 | | #include <cstddef> |
13 | | #include <cstdint> |
14 | | #include <cstdlib> |
15 | | #include <queue> |
16 | | #include <utility> |
17 | | #include <vector> |
18 | | |
19 | | #include "lib/jxl/base/common.h" |
20 | | #include "lib/jxl/base/compiler_specific.h" |
21 | | #include "lib/jxl/base/printf_macros.h" |
22 | | #include "lib/jxl/base/scope_guard.h" |
23 | | #include "lib/jxl/base/status.h" |
24 | | #include "lib/jxl/dec_ans.h" |
25 | | #include "lib/jxl/dec_bit_reader.h" |
26 | | #include "lib/jxl/fields.h" |
27 | | #include "lib/jxl/frame_dimensions.h" |
28 | | #include "lib/jxl/image_ops.h" |
29 | | #include "lib/jxl/modular/encoding/context_predict.h" |
30 | | #include "lib/jxl/modular/encoding/dec_ma.h" |
31 | | #include "lib/jxl/modular/modular_image.h" |
32 | | #include "lib/jxl/modular/options.h" |
33 | | #include "lib/jxl/modular/transform/transform.h" |
34 | | #include "lib/jxl/pack_signed.h" |
35 | | |
36 | | namespace jxl { |
37 | | |
38 | | // Removes all nodes that use a static property (i.e. channel or group ID) from |
39 | | // the tree and collapses each node on even levels with its two children to |
40 | | // produce a flatter tree. Also computes whether the resulting tree requires |
41 | | // using the weighted predictor. |
42 | | FlatTree FilterTree(const Tree &global_tree, |
43 | | std::array<pixel_type, kNumStaticProperties> &static_props, |
44 | | size_t *num_props, bool *use_wp, bool *wp_only, |
45 | 281k | bool *gradient_only) { |
46 | 281k | *num_props = 0; |
47 | 281k | bool has_wp = false; |
48 | 281k | bool has_non_wp = false; |
49 | 281k | *gradient_only = true; |
50 | 281k | const auto mark_property = [&](int32_t p) { |
51 | 213k | if (p == kWPProp) { |
52 | 10.8k | has_wp = true; |
53 | 202k | } else if (p >= kNumStaticProperties) { |
54 | 128k | has_non_wp = true; |
55 | 128k | } |
56 | 213k | if (p >= kNumStaticProperties && p != kGradientProp) { |
57 | 122k | *gradient_only = false; |
58 | 122k | } |
59 | 213k | }; |
60 | 281k | FlatTree output; |
61 | 281k | std::queue<size_t> nodes; |
62 | 281k | nodes.push(0); |
63 | | // Produces a trimmed and flattened tree by doing a BFS visit of the original |
64 | | // tree, ignoring branches that are known to be false and proceeding two |
65 | | // levels at a time to collapse nodes in a flatter tree; if an inner parent |
66 | | // node has a leaf as a child, the leaf is duplicated and an implicit fake |
67 | | // node is added. This allows to reduce the number of branches when traversing |
68 | | // the resulting flat tree. |
69 | 857k | while (!nodes.empty()) { |
70 | 575k | size_t cur = nodes.front(); |
71 | 575k | nodes.pop(); |
72 | | // Skip nodes that we can decide now, by jumping directly to their children. |
73 | 604k | while (global_tree[cur].property < kNumStaticProperties && |
74 | 527k | global_tree[cur].property != -1) { |
75 | 28.3k | if (static_props[global_tree[cur].property] > global_tree[cur].splitval) { |
76 | 16.5k | cur = global_tree[cur].lchild; |
77 | 16.5k | } else { |
78 | 11.8k | cur = global_tree[cur].rchild; |
79 | 11.8k | } |
80 | 28.3k | } |
81 | 575k | FlatDecisionNode flat; |
82 | 575k | if (global_tree[cur].property == -1) { |
83 | 502k | flat.property0 = -1; |
84 | 502k | flat.childID = global_tree[cur].lchild; |
85 | 502k | flat.predictor = global_tree[cur].predictor; |
86 | 502k | flat.predictor_offset = global_tree[cur].predictor_offset; |
87 | 502k | flat.multiplier = global_tree[cur].multiplier; |
88 | 502k | *gradient_only &= flat.predictor == Predictor::Gradient; |
89 | 502k | has_wp |= flat.predictor == Predictor::Weighted; |
90 | 502k | has_non_wp |= flat.predictor != Predictor::Weighted; |
91 | 502k | output.push_back(flat); |
92 | 502k | continue; |
93 | 502k | } |
94 | 73.1k | flat.childID = output.size() + nodes.size() + 1; |
95 | | |
96 | 73.1k | flat.property0 = global_tree[cur].property; |
97 | 73.1k | *num_props = std::max<size_t>(flat.property0 + 1, *num_props); |
98 | 73.1k | flat.splitval0 = global_tree[cur].splitval; |
99 | | |
100 | 218k | for (size_t i = 0; i < 2; i++) { |
101 | 145k | size_t cur_child = |
102 | 145k | i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild; |
103 | | // Skip nodes that we can decide now. |
104 | 165k | while (global_tree[cur_child].property < kNumStaticProperties && |
105 | 98.5k | global_tree[cur_child].property != -1) { |
106 | 19.7k | if (static_props[global_tree[cur_child].property] > |
107 | 19.7k | global_tree[cur_child].splitval) { |
108 | 12.1k | cur_child = global_tree[cur_child].lchild; |
109 | 12.1k | } else { |
110 | 7.60k | cur_child = global_tree[cur_child].rchild; |
111 | 7.60k | } |
112 | 19.7k | } |
113 | | // We ended up in a leaf, add a placeholder decision and two copies of the |
114 | | // leaf. |
115 | 145k | if (global_tree[cur_child].property == -1) { |
116 | 79.8k | flat.properties[i] = 0; |
117 | 79.8k | flat.splitvals[i] = 0; |
118 | 79.8k | nodes.push(cur_child); |
119 | 79.8k | nodes.push(cur_child); |
120 | 79.8k | } else { |
121 | 65.7k | flat.properties[i] = global_tree[cur_child].property; |
122 | 65.7k | flat.splitvals[i] = global_tree[cur_child].splitval; |
123 | 65.7k | nodes.push(global_tree[cur_child].lchild); |
124 | 65.7k | nodes.push(global_tree[cur_child].rchild); |
125 | 65.7k | *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props); |
126 | 65.7k | } |
127 | 145k | } |
128 | | |
129 | 143k | for (int16_t property : flat.properties) mark_property(property); |
130 | 73.1k | mark_property(flat.property0); |
131 | 73.1k | output.push_back(flat); |
132 | 73.1k | } |
133 | 281k | if (*num_props > kNumNonrefProperties) { |
134 | 163 | *num_props = |
135 | 163 | DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) * |
136 | 163 | kExtraPropsPerChannel + |
137 | 163 | kNumNonrefProperties; |
138 | 281k | } else { |
139 | 281k | *num_props = kNumNonrefProperties; |
140 | 281k | } |
141 | 281k | *use_wp = has_wp; |
142 | 281k | *wp_only = has_wp && !has_non_wp; |
143 | | |
144 | 281k | return output; |
145 | 281k | } |
146 | | |
147 | | namespace detail { |
148 | | template <bool uses_lz77> |
149 | | Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader, |
150 | | const std::vector<uint8_t> &context_map, |
151 | | const Tree &global_tree, |
152 | | const weighted::Header &wp_header, |
153 | | pixel_type chan, size_t group_id, |
154 | | TreeLut<uint8_t, false, false> &tree_lut, |
155 | | Image *image, uint32_t &fl_run, |
156 | 281k | uint32_t &fl_v) { |
157 | 281k | JxlMemoryManager *memory_manager = image->memory_manager(); |
158 | 281k | Channel &channel = image->channel[chan]; |
159 | | |
160 | 281k | std::array<pixel_type, kNumStaticProperties> static_props = { |
161 | 281k | {chan, static_cast<int>(group_id)}}; |
162 | | // TODO(veluca): filter the tree according to static_props. |
163 | | |
164 | | // zero pixel channel? could happen |
165 | 281k | if (channel.w == 0 || channel.h == 0) return true; |
166 | | |
167 | 281k | bool tree_has_wp_prop_or_pred = false; |
168 | 281k | bool is_wp_only = false; |
169 | 281k | bool is_gradient_only = false; |
170 | 281k | size_t num_props; |
171 | 281k | FlatTree tree = |
172 | 281k | FilterTree(global_tree, static_props, &num_props, |
173 | 281k | &tree_has_wp_prop_or_pred, &is_wp_only, &is_gradient_only); |
174 | | |
175 | | // From here on, tree lookup returns a *clustered* context ID. |
176 | | // This avoids an extra memory lookup after tree traversal. |
177 | 578k | for (auto &node : tree) { |
178 | 578k | if (node.property0 == -1) { |
179 | 505k | node.childID = context_map[node.childID]; |
180 | 505k | } |
181 | 578k | } |
182 | | |
183 | 281k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); |
184 | | |
185 | | // MAANS decode |
186 | 281k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, |
187 | 41.5M | pixel_type_w offset) -> pixel_type { |
188 | 41.5M | JXL_DASSERT((v & 0xFFFFFFFF) == v); |
189 | 41.5M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); |
190 | | // if it overflows, it overflows, and we have a problem anyway |
191 | 41.5M | return val * multiplier + offset; |
192 | 41.5M | }; jxl::detail::DecodeModularChannelMAANS<true>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&)::{lambda(unsigned long, int, long)#1}::operator()(unsigned long, int, long) constLine | Count | Source | 187 | 7.44M | pixel_type_w offset) -> pixel_type { | 188 | 7.44M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 7.44M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 7.44M | return val * multiplier + offset; | 192 | 7.44M | }; |
jxl::detail::DecodeModularChannelMAANS<false>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&)::{lambda(unsigned long, int, long)#1}::operator()(unsigned long, int, long) constLine | Count | Source | 187 | 34.1M | pixel_type_w offset) -> pixel_type { | 188 | 34.1M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 34.1M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 34.1M | return val * multiplier + offset; | 192 | 34.1M | }; |
|
193 | | |
194 | | // True iff every decision node in global_tree splits on a static property |
195 | | // (channel or group_id) and every leaf has Gradient predictor with identity |
196 | | // transform. When this holds, all channels collapse to a single-leaf |
197 | | // Gradient+noop tree regardless of channel index, so the shared fl_run/fl_v |
198 | | // RLE state remains consistent across channel calls. |
199 | 281k | const bool global_tree_is_all_gradient_noop = [&] { |
200 | 288k | for (const auto& n : global_tree) { |
201 | 288k | if (n.property == -1) { |
202 | 275k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || |
203 | 360 | n.multiplier != 1) |
204 | 275k | return false; |
205 | 275k | } else if (n.property >= kNumStaticProperties) { |
206 | 5.99k | return false; |
207 | 5.99k | } |
208 | 288k | } |
209 | 413 | return true; |
210 | 281k | }(); jxl::detail::DecodeModularChannelMAANS<true>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&)::{lambda()#1}::operator()() constLine | Count | Source | 199 | 11.4k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 11.6k | for (const auto& n : global_tree) { | 201 | 11.6k | if (n.property == -1) { | 202 | 11.4k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 24 | n.multiplier != 1) | 204 | 11.4k | return false; | 205 | 11.4k | } else if (n.property >= kNumStaticProperties) { | 206 | 3 | return false; | 207 | 3 | } | 208 | 11.6k | } | 209 | 26 | return true; | 210 | 11.4k | }(); |
jxl::detail::DecodeModularChannelMAANS<false>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&)::{lambda()#1}::operator()() constLine | Count | Source | 199 | 270k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 277k | for (const auto& n : global_tree) { | 201 | 277k | if (n.property == -1) { | 202 | 264k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 336 | n.multiplier != 1) | 204 | 263k | return false; | 205 | 264k | } else if (n.property >= kNumStaticProperties) { | 206 | 5.99k | return false; | 207 | 5.99k | } | 208 | 277k | } | 209 | 387 | return true; | 210 | 270k | }(); |
|
211 | | |
212 | 281k | if (tree.size() == 1) { |
213 | | // special optimized case: no meta-adaptation, so no need |
214 | | // to compute properties. |
215 | 275k | Predictor predictor = tree[0].predictor; |
216 | 275k | int64_t offset = tree[0].predictor_offset; |
217 | 275k | int32_t multiplier = tree[0].multiplier; |
218 | 275k | size_t ctx_id = tree[0].childID; |
219 | 275k | if (predictor == Predictor::Zero) { |
220 | 274k | uint32_t value; |
221 | 274k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, |
222 | 274k | channel.w * channel.h)) { |
223 | | // Special-case: histogram has a single symbol, with no extra bits, and |
224 | | // we use ANS mode. |
225 | 62.6k | JXL_DEBUG_V(8, "Fastest track."); |
226 | 62.6k | pixel_type v = make_pixel(value, multiplier, offset); |
227 | 2.94M | for (size_t y = 0; y < channel.h; y++) { |
228 | 2.88M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
229 | 2.88M | std::fill(r, r + channel.w, v); |
230 | 2.88M | } |
231 | 211k | } else { |
232 | 211k | JXL_DEBUG_V(8, "Fast track."); |
233 | 211k | if (multiplier == 1 && offset == 0) { |
234 | 4.87M | for (size_t y = 0; y < channel.h; y++) { |
235 | 4.65M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
236 | 228M | for (size_t x = 0; x < channel.w; x++) { |
237 | 223M | uint32_t v = |
238 | 223M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
239 | 223M | r[x] = UnpackSigned(v); |
240 | 223M | } |
241 | 4.65M | } |
242 | 211k | } else { |
243 | 20.4k | for (size_t y = 0; y < channel.h; y++) { |
244 | 20.2k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
245 | 5.22M | for (size_t x = 0; x < channel.w; x++) { |
246 | 5.20M | uint32_t v = |
247 | 5.20M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, |
248 | 5.20M | br); |
249 | 5.20M | r[x] = make_pixel(v, multiplier, offset); |
250 | 5.20M | } |
251 | 20.2k | } |
252 | 162 | } |
253 | 211k | } |
254 | 274k | return true; |
255 | 274k | } else if (uses_lz77 && reader->IsHuffRleOnly() && |
256 | 7 | global_tree_is_all_gradient_noop) { |
257 | 7 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); |
258 | 7 | pixel_type_w sv = UnpackSigned(fl_v); |
259 | 14 | for (size_t y = 0; y < channel.h; y++) { |
260 | 7 | pixel_type *JXL_RESTRICT r = channel.Row(y); |
261 | 7 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
262 | 7 | const pixel_type *JXL_RESTRICT rtopleft = |
263 | 7 | (y ? channel.Row(y - 1) - 1 : r - 1); |
264 | 7 | pixel_type_w guess_0 = (y ? rtop[0] : 0); |
265 | 7 | if (fl_run == 0) { |
266 | 7 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
267 | 7 | &fl_run); |
268 | 7 | sv = UnpackSigned(fl_v); |
269 | 7 | } else { |
270 | 0 | fl_run--; |
271 | 0 | } |
272 | 7 | r[0] = sv + guess_0; |
273 | 14 | for (size_t x = 1; x < channel.w; x++) { |
274 | 7 | pixel_type left = r[x - 1]; |
275 | 7 | pixel_type top = rtop[x]; |
276 | 7 | pixel_type topleft = rtopleft[x]; |
277 | 7 | pixel_type_w guess = ClampedGradient(top, left, topleft); |
278 | 7 | if (!fl_run) { |
279 | 7 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
280 | 7 | &fl_run); |
281 | 7 | sv = UnpackSigned(fl_v); |
282 | 7 | } else { |
283 | 0 | fl_run--; |
284 | 0 | } |
285 | 7 | r[x] = sv + guess; |
286 | 7 | } |
287 | 7 | } |
288 | 7 | return true; |
289 | 1.33k | } else if (predictor == Predictor::Gradient && offset == 0 && |
290 | 415 | multiplier == 1) { |
291 | 415 | JXL_DEBUG_V(8, "Gradient very fast track."); |
292 | 415 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
293 | 3.75k | for (size_t y = 0; y < channel.h; y++) { |
294 | 3.33k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
295 | 43.5k | for (size_t x = 0; x < channel.w; x++) { |
296 | 40.2k | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
297 | 40.2k | pixel_type top = (y ? *(r + x - onerow) : left); |
298 | 40.2k | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); |
299 | 40.2k | pixel_type guess = ClampedGradient(top, left, topleft); |
300 | 40.2k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
301 | 40.2k | ctx_id, br); |
302 | 40.2k | r[x] = make_pixel(v, 1, guess); |
303 | 40.2k | } |
304 | 3.33k | } |
305 | 415 | return true; |
306 | 415 | } |
307 | 275k | } |
308 | | |
309 | | // Check if this tree is a WP-only tree with a small enough property value |
310 | | // range. |
311 | 6.74k | if (is_wp_only) { |
312 | 553 | is_wp_only = TreeToLookupTable(tree, tree_lut); |
313 | 553 | } |
314 | 6.74k | if (is_gradient_only) { |
315 | 95 | is_gradient_only = TreeToLookupTable(tree, tree_lut); |
316 | 95 | } |
317 | | |
318 | 6.74k | if (is_gradient_only) { |
319 | 53 | JXL_DEBUG_V(8, "Gradient fast track."); |
320 | 53 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
321 | 896 | for (size_t y = 0; y < channel.h; y++) { |
322 | 843 | pixel_type *JXL_RESTRICT r = channel.Row(y); |
323 | 20.1k | for (size_t x = 0; x < channel.w; x++) { |
324 | 19.3k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
325 | 19.3k | pixel_type_w top = (y ? *(r + x - onerow) : left); |
326 | 19.3k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); |
327 | 19.3k | int32_t guess = ClampedGradient(top, left, topleft); |
328 | 19.3k | uint32_t pos = |
329 | 19.3k | kPropRangeFast + |
330 | 19.3k | std::min<pixel_type_w>( |
331 | 19.3k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), |
332 | 19.3k | kPropRangeFast - 1); |
333 | 19.3k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
334 | 19.3k | uint64_t v = |
335 | 19.3k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); |
336 | 19.3k | r[x] = make_pixel(v, 1, guess); |
337 | 19.3k | } |
338 | 843 | } |
339 | 6.69k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { |
340 | 529 | JXL_DEBUG_V(8, "WP fast track."); |
341 | 529 | weighted::State wp_state(wp_header, channel.w, channel.h); |
342 | 529 | Properties properties(1); |
343 | 6.07k | for (size_t y = 0; y < channel.h; y++) { |
344 | 5.54k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
345 | 5.54k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
346 | 5.54k | const pixel_type *JXL_RESTRICT rtoptop = |
347 | 5.54k | (y > 1 ? channel.Row(y - 2) : rtop); |
348 | 5.54k | const pixel_type *JXL_RESTRICT rtopleft = |
349 | 5.54k | (y ? channel.Row(y - 1) - 1 : r - 1); |
350 | 5.54k | const pixel_type *JXL_RESTRICT rtopright = |
351 | 5.54k | (y ? channel.Row(y - 1) + 1 : r - 1); |
352 | 5.54k | size_t x = 0; |
353 | 5.54k | { |
354 | 5.54k | size_t offset = 0; |
355 | 5.54k | pixel_type_w left = y ? rtop[x] : 0; |
356 | 5.54k | pixel_type_w toptop = y ? rtoptop[x] : 0; |
357 | 5.54k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); |
358 | 5.54k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
359 | 5.54k | x, y, channel.w, left, left, topright, left, toptop, &properties, |
360 | 5.54k | offset); |
361 | 5.54k | uint32_t pos = |
362 | 5.54k | kPropRangeFast + |
363 | 5.54k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
364 | 5.54k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
365 | 5.54k | uint64_t v = |
366 | 5.54k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
367 | 5.54k | r[x] = make_pixel(v, 1, guess); |
368 | 5.54k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
369 | 5.54k | } |
370 | 228k | for (x = 1; x + 1 < channel.w; x++) { |
371 | 222k | size_t offset = 0; |
372 | 222k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
373 | 222k | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], |
374 | 222k | rtoptop[x], &properties, offset); |
375 | 222k | uint32_t pos = |
376 | 222k | kPropRangeFast + |
377 | 222k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
378 | 222k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
379 | 222k | uint64_t v = |
380 | 222k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
381 | 222k | r[x] = make_pixel(v, 1, guess); |
382 | 222k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
383 | 222k | } |
384 | 5.54k | { |
385 | 5.54k | size_t offset = 0; |
386 | 5.54k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
387 | 5.54k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], |
388 | 5.54k | rtoptop[x], &properties, offset); |
389 | 5.54k | uint32_t pos = |
390 | 5.54k | kPropRangeFast + |
391 | 5.54k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
392 | 5.54k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
393 | 5.54k | uint64_t v = |
394 | 5.54k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
395 | 5.54k | r[x] = make_pixel(v, 1, guess); |
396 | 5.54k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
397 | 5.54k | } |
398 | 5.54k | } |
399 | 6.16k | } else if (!tree_has_wp_prop_or_pred) { |
400 | | // special optimized case: the weighted predictor and its properties are not |
401 | | // used, so no need to compute weights and properties. |
402 | 4.06k | JXL_DEBUG_V(8, "Slow track."); |
403 | 4.06k | MATreeLookup tree_lookup(tree); |
404 | 4.06k | Properties properties = Properties(num_props); |
405 | 4.06k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
406 | 4.06k | JXL_ASSIGN_OR_RETURN( |
407 | 4.06k | Channel references, |
408 | 4.06k | Channel::Create(memory_manager, |
409 | 4.06k | properties.size() - kNumNonrefProperties, channel.w)); |
410 | 158k | for (size_t y = 0; y < channel.h; y++) { |
411 | 154k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
412 | 154k | PrecomputeReferences(channel, y, *image, chan, &references); |
413 | 154k | InitPropsRow(&properties, static_props, y); |
414 | 154k | if (y > 1 && channel.w > 8 && references.w == 0) { |
415 | 419k | for (size_t x = 0; x < 2; x++) { |
416 | 279k | PredictionResult res = |
417 | 279k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
418 | 279k | tree_lookup, references); |
419 | 279k | uint64_t v = |
420 | 279k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
421 | 279k | p[x] = make_pixel(v, res.multiplier, res.guess); |
422 | 279k | } |
423 | 19.0M | for (size_t x = 2; x < channel.w - 2; x++) { |
424 | 18.9M | PredictionResult res = |
425 | 18.9M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, |
426 | 18.9M | tree_lookup, references); |
427 | 18.9M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
428 | 18.9M | res.context, br); |
429 | 18.9M | p[x] = make_pixel(v, res.multiplier, res.guess); |
430 | 18.9M | } |
431 | 419k | for (size_t x = channel.w - 2; x < channel.w; x++) { |
432 | 279k | PredictionResult res = |
433 | 279k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
434 | 279k | tree_lookup, references); |
435 | 279k | uint64_t v = |
436 | 279k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
437 | 279k | p[x] = make_pixel(v, res.multiplier, res.guess); |
438 | 279k | } |
439 | 139k | } else { |
440 | 776k | for (size_t x = 0; x < channel.w; x++) { |
441 | 762k | PredictionResult res = |
442 | 762k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
443 | 762k | tree_lookup, references); |
444 | 762k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
445 | 762k | res.context, br); |
446 | 762k | p[x] = make_pixel(v, res.multiplier, res.guess); |
447 | 762k | } |
448 | 14.6k | } |
449 | 154k | } |
450 | 4.06k | } else { |
451 | 2.09k | JXL_DEBUG_V(8, "Slowest track."); |
452 | 2.09k | MATreeLookup tree_lookup(tree); |
453 | 2.09k | Properties properties = Properties(num_props); |
454 | 2.09k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
455 | 2.09k | JXL_ASSIGN_OR_RETURN( |
456 | 2.09k | Channel references, |
457 | 2.09k | Channel::Create(memory_manager, |
458 | 2.09k | properties.size() - kNumNonrefProperties, channel.w)); |
459 | 2.09k | weighted::State wp_state(wp_header, channel.w, channel.h); |
460 | 124k | for (size_t y = 0; y < channel.h; y++) { |
461 | 122k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
462 | 122k | InitPropsRow(&properties, static_props, y); |
463 | 122k | PrecomputeReferences(channel, y, *image, chan, &references); |
464 | 122k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { |
465 | 348k | for (size_t x = 0; x < 2; x++) { |
466 | 232k | PredictionResult res = |
467 | 232k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
468 | 232k | tree_lookup, references, &wp_state); |
469 | 232k | uint64_t v = |
470 | 232k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
471 | 232k | p[x] = make_pixel(v, res.multiplier, res.guess); |
472 | 232k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
473 | 232k | } |
474 | 17.7M | for (size_t x = 2; x < channel.w - 2; x++) { |
475 | 17.6M | PredictionResult res = |
476 | 17.6M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, |
477 | 17.6M | tree_lookup, references, &wp_state); |
478 | 17.6M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
479 | 17.6M | res.context, br); |
480 | 17.6M | p[x] = make_pixel(v, res.multiplier, res.guess); |
481 | 17.6M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
482 | 17.6M | } |
483 | 348k | for (size_t x = channel.w - 2; x < channel.w; x++) { |
484 | 231k | PredictionResult res = |
485 | 231k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
486 | 231k | tree_lookup, references, &wp_state); |
487 | 231k | uint64_t v = |
488 | 231k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
489 | 231k | p[x] = make_pixel(v, res.multiplier, res.guess); |
490 | 231k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
491 | 231k | } |
492 | 116k | } else { |
493 | 1.30M | for (size_t x = 0; x < channel.w; x++) { |
494 | 1.30M | PredictionResult res = |
495 | 1.30M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
496 | 1.30M | tree_lookup, references, &wp_state); |
497 | 1.30M | uint64_t v = |
498 | 1.30M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
499 | 1.30M | p[x] = make_pixel(v, res.multiplier, res.guess); |
500 | 1.30M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
501 | 1.30M | } |
502 | 5.98k | } |
503 | 122k | } |
504 | 2.09k | } |
505 | 6.74k | return true; |
506 | 6.74k | } jxl::Status jxl::detail::DecodeModularChannelMAANS<true>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&) Line | Count | Source | 156 | 11.4k | uint32_t &fl_v) { | 157 | 11.4k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 11.4k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 11.4k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 11.4k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 11.4k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 11.4k | bool tree_has_wp_prop_or_pred = false; | 168 | 11.4k | bool is_wp_only = false; | 169 | 11.4k | bool is_gradient_only = false; | 170 | 11.4k | size_t num_props; | 171 | 11.4k | FlatTree tree = | 172 | 11.4k | FilterTree(global_tree, static_props, &num_props, | 173 | 11.4k | &tree_has_wp_prop_or_pred, &is_wp_only, &is_gradient_only); | 174 | | | 175 | | // From here on, tree lookup returns a *clustered* context ID. | 176 | | // This avoids an extra memory lookup after tree traversal. | 177 | 12.0k | for (auto &node : tree) { | 178 | 12.0k | if (node.property0 == -1) { | 179 | 11.8k | node.childID = context_map[node.childID]; | 180 | 11.8k | } | 181 | 12.0k | } | 182 | | | 183 | 11.4k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 11.4k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 11.4k | pixel_type_w offset) -> pixel_type { | 188 | 11.4k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 11.4k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 11.4k | return val * multiplier + offset; | 192 | 11.4k | }; | 193 | | | 194 | | // True iff every decision node in global_tree splits on a static property | 195 | | // (channel or group_id) and every leaf has Gradient predictor with identity | 196 | | // transform. When this holds, all channels collapse to a single-leaf | 197 | | // Gradient+noop tree regardless of channel index, so the shared fl_run/fl_v | 198 | | // RLE state remains consistent across channel calls. | 199 | 11.4k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 11.4k | for (const auto& n : global_tree) { | 201 | 11.4k | if (n.property == -1) { | 202 | 11.4k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 11.4k | n.multiplier != 1) | 204 | 11.4k | return false; | 205 | 11.4k | } else if (n.property >= kNumStaticProperties) { | 206 | 11.4k | return false; | 207 | 11.4k | } | 208 | 11.4k | } | 209 | 11.4k | return true; | 210 | 11.4k | }(); | 211 | | | 212 | 11.4k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 11.4k | Predictor predictor = tree[0].predictor; | 216 | 11.4k | int64_t offset = tree[0].predictor_offset; | 217 | 11.4k | int32_t multiplier = tree[0].multiplier; | 218 | 11.4k | size_t ctx_id = tree[0].childID; | 219 | 11.4k | if (predictor == Predictor::Zero) { | 220 | 11.1k | uint32_t value; | 221 | 11.1k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 11.1k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 1.21k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 1.21k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 151k | for (size_t y = 0; y < channel.h; y++) { | 228 | 150k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 150k | std::fill(r, r + channel.w, v); | 230 | 150k | } | 231 | 9.96k | } else { | 232 | 9.96k | JXL_DEBUG_V(8, "Fast track."); | 233 | 9.96k | if (multiplier == 1 && offset == 0) { | 234 | 1.19M | for (size_t y = 0; y < channel.h; y++) { | 235 | 1.18M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 42.8M | for (size_t x = 0; x < channel.w; x++) { | 237 | 41.6M | uint32_t v = | 238 | 41.6M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 41.6M | r[x] = UnpackSigned(v); | 240 | 41.6M | } | 241 | 1.18M | } | 242 | 9.84k | } else { | 243 | 15.9k | for (size_t y = 0; y < channel.h; y++) { | 244 | 15.8k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 3.77M | for (size_t x = 0; x < channel.w; x++) { | 246 | 3.75M | uint32_t v = | 247 | 3.75M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 3.75M | br); | 249 | 3.75M | r[x] = make_pixel(v, multiplier, offset); | 250 | 3.75M | } | 251 | 15.8k | } | 252 | 120 | } | 253 | 9.96k | } | 254 | 11.1k | return true; | 255 | 11.1k | } else if (uses_lz77 && reader->IsHuffRleOnly() && | 256 | 7 | global_tree_is_all_gradient_noop) { | 257 | 7 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); | 258 | 7 | pixel_type_w sv = UnpackSigned(fl_v); | 259 | 14 | for (size_t y = 0; y < channel.h; y++) { | 260 | 7 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 261 | 7 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 262 | 7 | const pixel_type *JXL_RESTRICT rtopleft = | 263 | 7 | (y ? channel.Row(y - 1) - 1 : r - 1); | 264 | 7 | pixel_type_w guess_0 = (y ? rtop[0] : 0); | 265 | 7 | if (fl_run == 0) { | 266 | 7 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 267 | 7 | &fl_run); | 268 | 7 | sv = UnpackSigned(fl_v); | 269 | 7 | } else { | 270 | 0 | fl_run--; | 271 | 0 | } | 272 | 7 | r[0] = sv + guess_0; | 273 | 14 | for (size_t x = 1; x < channel.w; x++) { | 274 | 7 | pixel_type left = r[x - 1]; | 275 | 7 | pixel_type top = rtop[x]; | 276 | 7 | pixel_type topleft = rtopleft[x]; | 277 | 7 | pixel_type_w guess = ClampedGradient(top, left, topleft); | 278 | 7 | if (!fl_run) { | 279 | 7 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 280 | 7 | &fl_run); | 281 | 7 | sv = UnpackSigned(fl_v); | 282 | 7 | } else { | 283 | 0 | fl_run--; | 284 | 0 | } | 285 | 7 | r[x] = sv + guess; | 286 | 7 | } | 287 | 7 | } | 288 | 7 | return true; | 289 | 246 | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 17 | multiplier == 1) { | 291 | 17 | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 17 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 95 | for (size_t y = 0; y < channel.h; y++) { | 294 | 78 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 371 | for (size_t x = 0; x < channel.w; x++) { | 296 | 293 | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 293 | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 293 | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 293 | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 293 | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 293 | ctx_id, br); | 302 | 293 | r[x] = make_pixel(v, 1, guess); | 303 | 293 | } | 304 | 78 | } | 305 | 17 | return true; | 306 | 17 | } | 307 | 11.4k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 224 | if (is_wp_only) { | 312 | 0 | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 0 | } | 314 | 224 | if (is_gradient_only) { | 315 | 0 | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 0 | } | 317 | | | 318 | 224 | if (is_gradient_only) { | 319 | 0 | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 0 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 0 | for (size_t y = 0; y < channel.h; y++) { | 322 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 0 | for (size_t x = 0; x < channel.w; x++) { | 324 | 0 | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 0 | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 0 | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 0 | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 0 | uint32_t pos = | 329 | 0 | kPropRangeFast + | 330 | 0 | std::min<pixel_type_w>( | 331 | 0 | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 0 | kPropRangeFast - 1); | 333 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 0 | uint64_t v = | 335 | 0 | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 0 | r[x] = make_pixel(v, 1, guess); | 337 | 0 | } | 338 | 0 | } | 339 | 224 | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 340 | 0 | JXL_DEBUG_V(8, "WP fast track."); | 341 | 0 | weighted::State wp_state(wp_header, channel.w, channel.h); | 342 | 0 | Properties properties(1); | 343 | 0 | for (size_t y = 0; y < channel.h; y++) { | 344 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 345 | 0 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 346 | 0 | const pixel_type *JXL_RESTRICT rtoptop = | 347 | 0 | (y > 1 ? channel.Row(y - 2) : rtop); | 348 | 0 | const pixel_type *JXL_RESTRICT rtopleft = | 349 | 0 | (y ? channel.Row(y - 1) - 1 : r - 1); | 350 | 0 | const pixel_type *JXL_RESTRICT rtopright = | 351 | 0 | (y ? channel.Row(y - 1) + 1 : r - 1); | 352 | 0 | size_t x = 0; | 353 | 0 | { | 354 | 0 | size_t offset = 0; | 355 | 0 | pixel_type_w left = y ? rtop[x] : 0; | 356 | 0 | pixel_type_w toptop = y ? rtoptop[x] : 0; | 357 | 0 | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 358 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 359 | 0 | x, y, channel.w, left, left, topright, left, toptop, &properties, | 360 | 0 | offset); | 361 | 0 | uint32_t pos = | 362 | 0 | kPropRangeFast + | 363 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 364 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 365 | 0 | uint64_t v = | 366 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 367 | 0 | r[x] = make_pixel(v, 1, guess); | 368 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 369 | 0 | } | 370 | 0 | for (x = 1; x + 1 < channel.w; x++) { | 371 | 0 | size_t offset = 0; | 372 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 373 | 0 | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 374 | 0 | rtoptop[x], &properties, offset); | 375 | 0 | uint32_t pos = | 376 | 0 | kPropRangeFast + | 377 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 378 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 379 | 0 | uint64_t v = | 380 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 381 | 0 | r[x] = make_pixel(v, 1, guess); | 382 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 383 | 0 | } | 384 | 0 | { | 385 | 0 | size_t offset = 0; | 386 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 387 | 0 | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 388 | 0 | rtoptop[x], &properties, offset); | 389 | 0 | uint32_t pos = | 390 | 0 | kPropRangeFast + | 391 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 392 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 393 | 0 | uint64_t v = | 394 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 395 | 0 | r[x] = make_pixel(v, 1, guess); | 396 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 397 | 0 | } | 398 | 0 | } | 399 | 229 | } else if (!tree_has_wp_prop_or_pred) { | 400 | | // special optimized case: the weighted predictor and its properties are not | 401 | | // used, so no need to compute weights and properties. | 402 | 229 | JXL_DEBUG_V(8, "Slow track."); | 403 | 229 | MATreeLookup tree_lookup(tree); | 404 | 229 | Properties properties = Properties(num_props); | 405 | 229 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 229 | JXL_ASSIGN_OR_RETURN( | 407 | 229 | Channel references, | 408 | 229 | Channel::Create(memory_manager, | 409 | 229 | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 14.4k | for (size_t y = 0; y < channel.h; y++) { | 411 | 14.2k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 14.2k | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 14.2k | InitPropsRow(&properties, static_props, y); | 414 | 14.2k | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 40.8k | for (size_t x = 0; x < 2; x++) { | 416 | 27.2k | PredictionResult res = | 417 | 27.2k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 27.2k | tree_lookup, references); | 419 | 27.2k | uint64_t v = | 420 | 27.2k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 27.2k | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 27.2k | } | 423 | 3.48M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 3.47M | PredictionResult res = | 425 | 3.47M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 3.47M | tree_lookup, references); | 427 | 3.47M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 3.47M | res.context, br); | 429 | 3.47M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 3.47M | } | 431 | 40.8k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 27.2k | PredictionResult res = | 433 | 27.2k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 27.2k | tree_lookup, references); | 435 | 27.2k | uint64_t v = | 436 | 27.2k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 27.2k | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 27.2k | } | 439 | 13.6k | } else { | 440 | 38.4k | for (size_t x = 0; x < channel.w; x++) { | 441 | 37.8k | PredictionResult res = | 442 | 37.8k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 37.8k | tree_lookup, references); | 444 | 37.8k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 37.8k | res.context, br); | 446 | 37.8k | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 37.8k | } | 448 | 583 | } | 449 | 14.2k | } | 450 | 18.4E | } else { | 451 | 18.4E | JXL_DEBUG_V(8, "Slowest track."); | 452 | 18.4E | MATreeLookup tree_lookup(tree); | 453 | 18.4E | Properties properties = Properties(num_props); | 454 | 18.4E | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 18.4E | JXL_ASSIGN_OR_RETURN( | 456 | 18.4E | Channel references, | 457 | 18.4E | Channel::Create(memory_manager, | 458 | 18.4E | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 18.4E | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 18.4E | for (size_t y = 0; y < channel.h; y++) { | 461 | 480 | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 480 | InitPropsRow(&properties, static_props, y); | 463 | 480 | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 480 | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 465 | 0 | for (size_t x = 0; x < 2; x++) { | 466 | 0 | PredictionResult res = | 467 | 0 | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 0 | tree_lookup, references, &wp_state); | 469 | 0 | uint64_t v = | 470 | 0 | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 0 | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 0 | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 0 | } | 474 | 0 | for (size_t x = 2; x < channel.w - 2; x++) { | 475 | 0 | PredictionResult res = | 476 | 0 | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 477 | 0 | tree_lookup, references, &wp_state); | 478 | 0 | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 479 | 0 | res.context, br); | 480 | 0 | p[x] = make_pixel(v, res.multiplier, res.guess); | 481 | 0 | wp_state.UpdateErrors(p[x], x, y, channel.w); | 482 | 0 | } | 483 | 0 | for (size_t x = channel.w - 2; x < channel.w; x++) { | 484 | 0 | PredictionResult res = | 485 | 0 | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 486 | 0 | tree_lookup, references, &wp_state); | 487 | 0 | uint64_t v = | 488 | 0 | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 489 | 0 | p[x] = make_pixel(v, res.multiplier, res.guess); | 490 | 0 | wp_state.UpdateErrors(p[x], x, y, channel.w); | 491 | 0 | } | 492 | 480 | } else { | 493 | 123k | for (size_t x = 0; x < channel.w; x++) { | 494 | 122k | PredictionResult res = | 495 | 122k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 122k | tree_lookup, references, &wp_state); | 497 | 122k | uint64_t v = | 498 | 122k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 122k | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 122k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 122k | } | 502 | 480 | } | 503 | 480 | } | 504 | 18.4E | } | 505 | 224 | return true; | 506 | 224 | } |
jxl::Status jxl::detail::DecodeModularChannelMAANS<false>(jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, std::__1::vector<jxl::PropertyDecisionNode, std::__1::allocator<jxl::PropertyDecisionNode> > const&, jxl::weighted::Header const&, int, unsigned long, jxl::TreeLut<unsigned char, false, false>&, jxl::Image*, unsigned int&, unsigned int&) Line | Count | Source | 156 | 270k | uint32_t &fl_v) { | 157 | 270k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 270k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 270k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 270k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 270k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 270k | bool tree_has_wp_prop_or_pred = false; | 168 | 270k | bool is_wp_only = false; | 169 | 270k | bool is_gradient_only = false; | 170 | 270k | size_t num_props; | 171 | 270k | FlatTree tree = | 172 | 270k | FilterTree(global_tree, static_props, &num_props, | 173 | 270k | &tree_has_wp_prop_or_pred, &is_wp_only, &is_gradient_only); | 174 | | | 175 | | // From here on, tree lookup returns a *clustered* context ID. | 176 | | // This avoids an extra memory lookup after tree traversal. | 177 | 566k | for (auto &node : tree) { | 178 | 566k | if (node.property0 == -1) { | 179 | 493k | node.childID = context_map[node.childID]; | 180 | 493k | } | 181 | 566k | } | 182 | | | 183 | 270k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 270k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 270k | pixel_type_w offset) -> pixel_type { | 188 | 270k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 270k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 270k | return val * multiplier + offset; | 192 | 270k | }; | 193 | | | 194 | | // True iff every decision node in global_tree splits on a static property | 195 | | // (channel or group_id) and every leaf has Gradient predictor with identity | 196 | | // transform. When this holds, all channels collapse to a single-leaf | 197 | | // Gradient+noop tree regardless of channel index, so the shared fl_run/fl_v | 198 | | // RLE state remains consistent across channel calls. | 199 | 270k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 270k | for (const auto& n : global_tree) { | 201 | 270k | if (n.property == -1) { | 202 | 270k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 270k | n.multiplier != 1) | 204 | 270k | return false; | 205 | 270k | } else if (n.property >= kNumStaticProperties) { | 206 | 270k | return false; | 207 | 270k | } | 208 | 270k | } | 209 | 270k | return true; | 210 | 270k | }(); | 211 | | | 212 | 270k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 264k | Predictor predictor = tree[0].predictor; | 216 | 264k | int64_t offset = tree[0].predictor_offset; | 217 | 264k | int32_t multiplier = tree[0].multiplier; | 218 | 264k | size_t ctx_id = tree[0].childID; | 219 | 264k | if (predictor == Predictor::Zero) { | 220 | 263k | uint32_t value; | 221 | 263k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 263k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 61.4k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 61.4k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 2.79M | for (size_t y = 0; y < channel.h; y++) { | 228 | 2.73M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 2.73M | std::fill(r, r + channel.w, v); | 230 | 2.73M | } | 231 | 201k | } else { | 232 | 201k | JXL_DEBUG_V(8, "Fast track."); | 233 | 201k | if (multiplier == 1 && offset == 0) { | 234 | 3.67M | for (size_t y = 0; y < channel.h; y++) { | 235 | 3.47M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 185M | for (size_t x = 0; x < channel.w; x++) { | 237 | 181M | uint32_t v = | 238 | 181M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 181M | r[x] = UnpackSigned(v); | 240 | 181M | } | 241 | 3.47M | } | 242 | 201k | } else { | 243 | 4.52k | for (size_t y = 0; y < channel.h; y++) { | 244 | 4.48k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 1.45M | for (size_t x = 0; x < channel.w; x++) { | 246 | 1.44M | uint32_t v = | 247 | 1.44M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 1.44M | br); | 249 | 1.44M | r[x] = make_pixel(v, multiplier, offset); | 250 | 1.44M | } | 251 | 4.48k | } | 252 | 42 | } | 253 | 201k | } | 254 | 263k | return true; | 255 | 263k | } else if (uses_lz77 && reader->IsHuffRleOnly() && | 256 | 0 | global_tree_is_all_gradient_noop) { | 257 | 0 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); | 258 | 0 | pixel_type_w sv = UnpackSigned(fl_v); | 259 | 0 | for (size_t y = 0; y < channel.h; y++) { | 260 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 261 | 0 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 262 | 0 | const pixel_type *JXL_RESTRICT rtopleft = | 263 | 0 | (y ? channel.Row(y - 1) - 1 : r - 1); | 264 | 0 | pixel_type_w guess_0 = (y ? rtop[0] : 0); | 265 | 0 | if (fl_run == 0) { | 266 | 0 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 267 | 0 | &fl_run); | 268 | 0 | sv = UnpackSigned(fl_v); | 269 | 0 | } else { | 270 | 0 | fl_run--; | 271 | 0 | } | 272 | 0 | r[0] = sv + guess_0; | 273 | 0 | for (size_t x = 1; x < channel.w; x++) { | 274 | 0 | pixel_type left = r[x - 1]; | 275 | 0 | pixel_type top = rtop[x]; | 276 | 0 | pixel_type topleft = rtopleft[x]; | 277 | 0 | pixel_type_w guess = ClampedGradient(top, left, topleft); | 278 | 0 | if (!fl_run) { | 279 | 0 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 280 | 0 | &fl_run); | 281 | 0 | sv = UnpackSigned(fl_v); | 282 | 0 | } else { | 283 | 0 | fl_run--; | 284 | 0 | } | 285 | 0 | r[x] = sv + guess; | 286 | 0 | } | 287 | 0 | } | 288 | 0 | return true; | 289 | 1.09k | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 398 | multiplier == 1) { | 291 | 398 | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 398 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 3.65k | for (size_t y = 0; y < channel.h; y++) { | 294 | 3.25k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 43.2k | for (size_t x = 0; x < channel.w; x++) { | 296 | 39.9k | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 39.9k | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 39.9k | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 39.9k | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 39.9k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 39.9k | ctx_id, br); | 302 | 39.9k | r[x] = make_pixel(v, 1, guess); | 303 | 39.9k | } | 304 | 3.25k | } | 305 | 398 | return true; | 306 | 398 | } | 307 | 264k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 6.52k | if (is_wp_only) { | 312 | 553 | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 553 | } | 314 | 6.52k | if (is_gradient_only) { | 315 | 95 | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 95 | } | 317 | | | 318 | 6.52k | if (is_gradient_only) { | 319 | 53 | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 53 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 896 | for (size_t y = 0; y < channel.h; y++) { | 322 | 843 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 20.1k | for (size_t x = 0; x < channel.w; x++) { | 324 | 19.3k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 19.3k | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 19.3k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 19.3k | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 19.3k | uint32_t pos = | 329 | 19.3k | kPropRangeFast + | 330 | 19.3k | std::min<pixel_type_w>( | 331 | 19.3k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 19.3k | kPropRangeFast - 1); | 333 | 19.3k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 19.3k | uint64_t v = | 335 | 19.3k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 19.3k | r[x] = make_pixel(v, 1, guess); | 337 | 19.3k | } | 338 | 843 | } | 339 | 6.55k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 340 | 529 | JXL_DEBUG_V(8, "WP fast track."); | 341 | 529 | weighted::State wp_state(wp_header, channel.w, channel.h); | 342 | 529 | Properties properties(1); | 343 | 6.07k | for (size_t y = 0; y < channel.h; y++) { | 344 | 5.54k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 345 | 5.54k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 346 | 5.54k | const pixel_type *JXL_RESTRICT rtoptop = | 347 | 5.54k | (y > 1 ? channel.Row(y - 2) : rtop); | 348 | 5.54k | const pixel_type *JXL_RESTRICT rtopleft = | 349 | 5.54k | (y ? channel.Row(y - 1) - 1 : r - 1); | 350 | 5.54k | const pixel_type *JXL_RESTRICT rtopright = | 351 | 5.54k | (y ? channel.Row(y - 1) + 1 : r - 1); | 352 | 5.54k | size_t x = 0; | 353 | 5.54k | { | 354 | 5.54k | size_t offset = 0; | 355 | 5.54k | pixel_type_w left = y ? rtop[x] : 0; | 356 | 5.54k | pixel_type_w toptop = y ? rtoptop[x] : 0; | 357 | 5.54k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 358 | 5.54k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 359 | 5.54k | x, y, channel.w, left, left, topright, left, toptop, &properties, | 360 | 5.54k | offset); | 361 | 5.54k | uint32_t pos = | 362 | 5.54k | kPropRangeFast + | 363 | 5.54k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 364 | 5.54k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 365 | 5.54k | uint64_t v = | 366 | 5.54k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 367 | 5.54k | r[x] = make_pixel(v, 1, guess); | 368 | 5.54k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 369 | 5.54k | } | 370 | 228k | for (x = 1; x + 1 < channel.w; x++) { | 371 | 222k | size_t offset = 0; | 372 | 222k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 373 | 222k | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 374 | 222k | rtoptop[x], &properties, offset); | 375 | 222k | uint32_t pos = | 376 | 222k | kPropRangeFast + | 377 | 222k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 378 | 222k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 379 | 222k | uint64_t v = | 380 | 222k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 381 | 222k | r[x] = make_pixel(v, 1, guess); | 382 | 222k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 383 | 222k | } | 384 | 5.54k | { | 385 | 5.54k | size_t offset = 0; | 386 | 5.54k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 387 | 5.54k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 388 | 5.54k | rtoptop[x], &properties, offset); | 389 | 5.54k | uint32_t pos = | 390 | 5.54k | kPropRangeFast + | 391 | 5.54k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 392 | 5.54k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 393 | 5.54k | uint64_t v = | 394 | 5.54k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 395 | 5.54k | r[x] = make_pixel(v, 1, guess); | 396 | 5.54k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 397 | 5.54k | } | 398 | 5.54k | } | 399 | 5.93k | } else if (!tree_has_wp_prop_or_pred) { | 400 | | // special optimized case: the weighted predictor and its properties are not | 401 | | // used, so no need to compute weights and properties. | 402 | 3.83k | JXL_DEBUG_V(8, "Slow track."); | 403 | 3.83k | MATreeLookup tree_lookup(tree); | 404 | 3.83k | Properties properties = Properties(num_props); | 405 | 3.83k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 3.83k | JXL_ASSIGN_OR_RETURN( | 407 | 3.83k | Channel references, | 408 | 3.83k | Channel::Create(memory_manager, | 409 | 3.83k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 144k | for (size_t y = 0; y < channel.h; y++) { | 411 | 140k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 140k | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 140k | InitPropsRow(&properties, static_props, y); | 414 | 140k | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 379k | for (size_t x = 0; x < 2; x++) { | 416 | 252k | PredictionResult res = | 417 | 252k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 252k | tree_lookup, references); | 419 | 252k | uint64_t v = | 420 | 252k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 252k | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 252k | } | 423 | 15.5M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 15.4M | PredictionResult res = | 425 | 15.4M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 15.4M | tree_lookup, references); | 427 | 15.4M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 15.4M | res.context, br); | 429 | 15.4M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 15.4M | } | 431 | 379k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 252k | PredictionResult res = | 433 | 252k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 252k | tree_lookup, references); | 435 | 252k | uint64_t v = | 436 | 252k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 252k | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 252k | } | 439 | 126k | } else { | 440 | 738k | for (size_t x = 0; x < channel.w; x++) { | 441 | 724k | PredictionResult res = | 442 | 724k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 724k | tree_lookup, references); | 444 | 724k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 724k | res.context, br); | 446 | 724k | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 724k | } | 448 | 14.0k | } | 449 | 140k | } | 450 | 3.83k | } else { | 451 | 2.10k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 2.10k | MATreeLookup tree_lookup(tree); | 453 | 2.10k | Properties properties = Properties(num_props); | 454 | 2.10k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 2.10k | JXL_ASSIGN_OR_RETURN( | 456 | 2.10k | Channel references, | 457 | 2.10k | Channel::Create(memory_manager, | 458 | 2.10k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 2.10k | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 123k | for (size_t y = 0; y < channel.h; y++) { | 461 | 121k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 121k | InitPropsRow(&properties, static_props, y); | 463 | 121k | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 121k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 465 | 348k | for (size_t x = 0; x < 2; x++) { | 466 | 232k | PredictionResult res = | 467 | 232k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 232k | tree_lookup, references, &wp_state); | 469 | 232k | uint64_t v = | 470 | 232k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 232k | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 232k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 232k | } | 474 | 17.7M | for (size_t x = 2; x < channel.w - 2; x++) { | 475 | 17.6M | PredictionResult res = | 476 | 17.6M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 477 | 17.6M | tree_lookup, references, &wp_state); | 478 | 17.6M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 479 | 17.6M | res.context, br); | 480 | 17.6M | p[x] = make_pixel(v, res.multiplier, res.guess); | 481 | 17.6M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 482 | 17.6M | } | 483 | 348k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 484 | 231k | PredictionResult res = | 485 | 231k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 486 | 231k | tree_lookup, references, &wp_state); | 487 | 231k | uint64_t v = | 488 | 231k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 489 | 231k | p[x] = make_pixel(v, res.multiplier, res.guess); | 490 | 231k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 491 | 231k | } | 492 | 116k | } else { | 493 | 1.18M | for (size_t x = 0; x < channel.w; x++) { | 494 | 1.17M | PredictionResult res = | 495 | 1.17M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 1.17M | tree_lookup, references, &wp_state); | 497 | 1.17M | uint64_t v = | 498 | 1.17M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 1.17M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 1.17M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 1.17M | } | 502 | 5.50k | } | 503 | 121k | } | 504 | 2.10k | } | 505 | 6.52k | return true; | 506 | 6.52k | } |
|
507 | | } // namespace detail |
508 | | |
509 | | Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader, |
510 | | const std::vector<uint8_t> &context_map, |
511 | | const Tree &global_tree, |
512 | | const weighted::Header &wp_header, |
513 | | pixel_type chan, size_t group_id, |
514 | | TreeLut<uint8_t, false, false> &tree_lut, |
515 | | Image *image, uint32_t &fl_run, |
516 | 281k | uint32_t &fl_v) { |
517 | 281k | if (reader->UsesLZ77()) { |
518 | 11.4k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>( |
519 | 11.4k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
520 | 11.4k | tree_lut, image, fl_run, fl_v); |
521 | 270k | } else { |
522 | 270k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>( |
523 | 270k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
524 | 270k | tree_lut, image, fl_run, fl_v); |
525 | 270k | } |
526 | 281k | } |
527 | | |
528 | 193k | GroupHeader::GroupHeader() { Bundle::Init(this); } |
529 | | |
530 | | Status ValidateChannelDimensions(const Image &image, |
531 | 38.3k | const ModularOptions &options) { |
532 | 38.3k | size_t nb_channels = image.channel.size(); |
533 | 76.6k | for (bool is_dc : {true, false}) { |
534 | 76.6k | size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1); |
535 | 76.6k | size_t c = image.nb_meta_channels; |
536 | 645k | for (; c < nb_channels; c++) { |
537 | 572k | const Channel &ch = image.channel[c]; |
538 | 572k | if (ch.w > options.group_dim || ch.h > options.group_dim) break; |
539 | 572k | } |
540 | 116k | for (; c < nb_channels; c++) { |
541 | 39.8k | const Channel &ch = image.channel[c]; |
542 | 39.8k | if (ch.w == 0 || ch.h == 0) continue; // skip empty |
543 | 39.6k | bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3; |
544 | 39.6k | if (is_dc_channel != is_dc) continue; |
545 | 19.8k | size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift); |
546 | 19.8k | if (tile_dim == 0) { |
547 | 0 | return JXL_FAILURE("Inconsistent transforms"); |
548 | 0 | } |
549 | 19.8k | } |
550 | 76.6k | } |
551 | 38.3k | return true; |
552 | 38.3k | } |
553 | | |
554 | | Status ModularDecode(BitReader *br, Image &image, GroupHeader &header, |
555 | | size_t group_id, ModularOptions *options, |
556 | | const Tree *global_tree, const ANSCode *global_code, |
557 | | const std::vector<uint8_t> *global_ctx_map, |
558 | 44.1k | const bool allow_truncated_group) { |
559 | 44.1k | if (image.channel.empty()) return true; |
560 | 38.3k | JxlMemoryManager *memory_manager = image.memory_manager(); |
561 | | |
562 | | // decode transforms |
563 | 38.3k | Status status = Bundle::Read(br, &header); |
564 | 38.3k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status); |
565 | 38.3k | if (status.IsFatalError()) return status; |
566 | 38.3k | if (!br->AllReadsWithinBounds()) { |
567 | | // Don't do/undo transforms if header is incomplete. |
568 | 0 | header.transforms.clear(); |
569 | 0 | image.transform = header.transforms; |
570 | 0 | for (auto &ch : image.channel) { |
571 | 0 | ZeroFillImage(&ch.plane); |
572 | 0 | } |
573 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun before ModularDecode"); |
574 | 0 | } |
575 | | |
576 | 38.3k | JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ", |
577 | 38.3k | header.transforms.size()); |
578 | 38.3k | image.transform = header.transforms; |
579 | 38.3k | for (Transform &transform : image.transform) { |
580 | 20.9k | JXL_RETURN_IF_ERROR(transform.MetaApply(image)); |
581 | 20.9k | } |
582 | 38.2k | if (image.error) { |
583 | 0 | return JXL_FAILURE("Corrupt file. Aborting."); |
584 | 0 | } |
585 | 38.2k | JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options)); |
586 | | |
587 | 38.2k | size_t nb_channels = image.channel.size(); |
588 | | |
589 | 38.2k | size_t num_chans = 0; |
590 | 38.2k | size_t distance_multiplier = 0; |
591 | 327k | for (size_t i = 0; i < nb_channels; i++) { |
592 | 291k | Channel &channel = image.channel[i]; |
593 | 291k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
594 | 285k | channel.h > options->max_chan_size)) { |
595 | 1.90k | break; |
596 | 1.90k | } |
597 | 289k | if (!channel.w || !channel.h) { |
598 | 5.85k | continue; // skip empty channels |
599 | 5.85k | } |
600 | 283k | if (channel.w > distance_multiplier) { |
601 | 54.6k | distance_multiplier = channel.w; |
602 | 54.6k | } |
603 | 283k | num_chans++; |
604 | 283k | } |
605 | 38.2k | if (num_chans == 0) return true; |
606 | | |
607 | 38.0k | size_t next_channel = 0; |
608 | 38.0k | auto scope_guard = MakeScopeGuard([&]() { |
609 | 2.57k | for (size_t c = next_channel; c < image.channel.size(); c++) { |
610 | 2.48k | ZeroFillImage(&image.channel[c].plane); |
611 | 2.48k | } |
612 | 95 | }); |
613 | | // Do not do anything if truncated groups are not allowed. |
614 | 38.0k | if (allow_truncated_group) scope_guard.Disarm(); |
615 | | |
616 | | // Read tree. |
617 | 38.0k | Tree tree_storage; |
618 | 38.0k | std::vector<uint8_t> context_map_storage; |
619 | 38.0k | ANSCode code_storage; |
620 | 38.0k | const Tree *tree = &tree_storage; |
621 | 38.0k | const ANSCode *code = &code_storage; |
622 | 38.0k | const std::vector<uint8_t> *context_map = &context_map_storage; |
623 | 38.0k | if (!header.use_global_tree) { |
624 | 11.8k | uint64_t max_tree_size = 1024; |
625 | 97.4k | for (size_t i = 0; i < nb_channels; i++) { |
626 | 85.5k | Channel &channel = image.channel[i]; |
627 | 85.5k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
628 | 85.4k | channel.h > options->max_chan_size)) { |
629 | 2 | break; |
630 | 2 | } |
631 | 85.5k | uint64_t pixels = channel.w * channel.h; |
632 | 85.5k | max_tree_size += pixels; |
633 | 85.5k | } |
634 | 11.8k | max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size); |
635 | 11.8k | JXL_RETURN_IF_ERROR( |
636 | 11.8k | DecodeTree(memory_manager, br, &tree_storage, max_tree_size)); |
637 | 11.8k | JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, |
638 | 11.8k | (tree_storage.size() + 1) / 2, |
639 | 11.8k | &code_storage, &context_map_storage)); |
640 | 26.1k | } else { |
641 | 26.1k | if (!global_tree || !global_code || !global_ctx_map || |
642 | 26.1k | global_tree->empty()) { |
643 | 10 | return JXL_FAILURE("No global tree available but one was requested"); |
644 | 10 | } |
645 | 26.1k | tree = global_tree; |
646 | 26.1k | code = global_code; |
647 | 26.1k | context_map = global_ctx_map; |
648 | 26.1k | } |
649 | | |
650 | | // Read channels |
651 | 75.9k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader, |
652 | 75.9k | ANSSymbolReader::Create(code, br, distance_multiplier)); |
653 | 75.9k | auto tree_lut = jxl::make_unique<TreeLut<uint8_t, false, false>>(); |
654 | 75.9k | uint32_t fl_run = 0; |
655 | 75.9k | uint32_t fl_v = 0; |
656 | 325k | for (; next_channel < nb_channels; next_channel++) { |
657 | 289k | Channel &channel = image.channel[next_channel]; |
658 | 289k | if (next_channel >= image.nb_meta_channels && |
659 | 283k | (channel.w > options->max_chan_size || |
660 | 283k | channel.h > options->max_chan_size)) { |
661 | 1.63k | break; |
662 | 1.63k | } |
663 | 287k | if (!channel.w || !channel.h) { |
664 | 5.85k | continue; // skip empty channels |
665 | 5.85k | } |
666 | 281k | JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS( |
667 | 281k | br, &reader, *context_map, *tree, header.wp_header, next_channel, |
668 | 281k | group_id, *tree_lut, &image, fl_run, fl_v)); |
669 | | |
670 | | // Truncated group. |
671 | 281k | if (!br->AllReadsWithinBounds()) { |
672 | 49 | if (!allow_truncated_group) return JXL_FAILURE("Truncated input"); |
673 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun in ModularDecode"); |
674 | 49 | } |
675 | 281k | } |
676 | | |
677 | | // Make sure no zero-filling happens even if next_channel < nb_channels. |
678 | 37.9k | scope_guard.Disarm(); |
679 | | |
680 | 37.9k | if (!reader.CheckANSFinalState()) { |
681 | 0 | return JXL_FAILURE("ANS decode final state failed"); |
682 | 0 | } |
683 | 37.9k | return true; |
684 | 37.9k | } |
685 | | |
686 | | Status ModularGenericDecompress(BitReader *br, Image &image, |
687 | | GroupHeader *header, size_t group_id, |
688 | | ModularOptions *options, bool undo_transforms, |
689 | | const Tree *tree, const ANSCode *code, |
690 | | const std::vector<uint8_t> *ctx_map, |
691 | 44.2k | bool allow_truncated_group) { |
692 | 44.2k | std::vector<std::pair<size_t, size_t>> req_sizes; |
693 | 44.2k | req_sizes.reserve(image.channel.size()); |
694 | 158k | for (const auto &c : image.channel) { |
695 | 158k | req_sizes.emplace_back(c.w, c.h); |
696 | 158k | } |
697 | 44.2k | GroupHeader local_header; |
698 | 44.2k | if (header == nullptr) header = &local_header; |
699 | 44.2k | size_t bit_pos = br->TotalBitsConsumed(); |
700 | 44.2k | auto dec_status = ModularDecode(br, image, *header, group_id, options, tree, |
701 | 44.2k | code, ctx_map, allow_truncated_group); |
702 | 44.2k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status); |
703 | 44.0k | if (dec_status.IsFatalError()) return dec_status; |
704 | 44.0k | if (undo_transforms) image.undo_transforms(header->wp_header); |
705 | 44.0k | if (image.error) return JXL_FAILURE("Corrupt file. Aborting."); |
706 | 44.0k | JXL_DEBUG_V(4, |
707 | 44.0k | "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS |
708 | 44.0k | " image from %" PRIuS " bytes", |
709 | 44.0k | image.w, image.h, image.channel.size(), |
710 | 44.0k | (br->TotalBitsConsumed() - bit_pos) / 8); |
711 | 44.0k | JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str()); |
712 | 44.0k | (void)bit_pos; |
713 | | // Check that after applying all transforms we are back to the requested |
714 | | // image sizes, otherwise there's a programming error with the |
715 | | // transformations. |
716 | 44.0k | if (undo_transforms) { |
717 | 21.1k | JXL_ENSURE(image.channel.size() == req_sizes.size()); |
718 | 120k | for (size_t c = 0; c < req_sizes.size(); c++) { |
719 | 99.5k | JXL_ENSURE(req_sizes[c].first == image.channel[c].w); |
720 | 99.5k | JXL_ENSURE(req_sizes[c].second == image.channel[c].h); |
721 | 99.5k | } |
722 | 21.1k | } |
723 | 44.0k | return dec_status; |
724 | 44.0k | } |
725 | | |
726 | | } // namespace jxl |