/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 | 1.00M | bool *gradient_only) { |
46 | 1.00M | *num_props = 0; |
47 | 1.00M | bool has_wp = false; |
48 | 1.00M | bool has_non_wp = false; |
49 | 1.00M | *gradient_only = true; |
50 | 1.00M | const auto mark_property = [&](int32_t p) { |
51 | 960k | if (p == kWPProp) { |
52 | 156k | has_wp = true; |
53 | 803k | } else if (p >= kNumStaticProperties) { |
54 | 424k | has_non_wp = true; |
55 | 424k | } |
56 | 960k | if (p >= kNumStaticProperties && p != kGradientProp) { |
57 | 481k | *gradient_only = false; |
58 | 481k | } |
59 | 960k | }; |
60 | 1.00M | FlatTree output; |
61 | 1.00M | std::queue<size_t> nodes; |
62 | 1.00M | 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 | 3.28M | while (!nodes.empty()) { |
70 | 2.28M | size_t cur = nodes.front(); |
71 | 2.28M | nodes.pop(); |
72 | | // Skip nodes that we can decide now, by jumping directly to their children. |
73 | 2.44M | while (global_tree[cur].property < kNumStaticProperties && |
74 | 2.12M | global_tree[cur].property != -1) { |
75 | 166k | if (static_props[global_tree[cur].property] > global_tree[cur].splitval) { |
76 | 91.8k | cur = global_tree[cur].lchild; |
77 | 91.8k | } else { |
78 | 74.4k | cur = global_tree[cur].rchild; |
79 | 74.4k | } |
80 | 166k | } |
81 | 2.28M | FlatDecisionNode flat; |
82 | 2.28M | if (global_tree[cur].property == -1) { |
83 | 1.96M | flat.property0 = -1; |
84 | 1.96M | flat.childID = global_tree[cur].lchild; |
85 | 1.96M | flat.predictor = global_tree[cur].predictor; |
86 | 1.96M | flat.predictor_offset = global_tree[cur].predictor_offset; |
87 | 1.96M | flat.multiplier = global_tree[cur].multiplier; |
88 | 1.96M | *gradient_only &= flat.predictor == Predictor::Gradient; |
89 | 1.96M | has_wp |= flat.predictor == Predictor::Weighted; |
90 | 1.96M | has_non_wp |= flat.predictor != Predictor::Weighted; |
91 | 1.96M | output.push_back(flat); |
92 | 1.96M | continue; |
93 | 1.96M | } |
94 | 320k | flat.childID = output.size() + nodes.size() + 1; |
95 | | |
96 | 320k | flat.property0 = global_tree[cur].property; |
97 | 320k | *num_props = std::max<size_t>(flat.property0 + 1, *num_props); |
98 | 320k | flat.splitval0 = global_tree[cur].splitval; |
99 | | |
100 | 960k | for (size_t i = 0; i < 2; i++) { |
101 | 640k | size_t cur_child = |
102 | 640k | i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild; |
103 | | // Skip nodes that we can decide now. |
104 | 685k | while (global_tree[cur_child].property < kNumStaticProperties && |
105 | 423k | global_tree[cur_child].property != -1) { |
106 | 45.0k | if (static_props[global_tree[cur_child].property] > |
107 | 45.0k | global_tree[cur_child].splitval) { |
108 | 23.6k | cur_child = global_tree[cur_child].lchild; |
109 | 23.6k | } else { |
110 | 21.3k | cur_child = global_tree[cur_child].rchild; |
111 | 21.3k | } |
112 | 45.0k | } |
113 | | // We ended up in a leaf, add a placeholder decision and two copies of the |
114 | | // leaf. |
115 | 640k | if (global_tree[cur_child].property == -1) { |
116 | 378k | flat.properties[i] = 0; |
117 | 378k | flat.splitvals[i] = 0; |
118 | 378k | nodes.push(cur_child); |
119 | 378k | nodes.push(cur_child); |
120 | 378k | } else { |
121 | 261k | flat.properties[i] = global_tree[cur_child].property; |
122 | 261k | flat.splitvals[i] = global_tree[cur_child].splitval; |
123 | 261k | nodes.push(global_tree[cur_child].lchild); |
124 | 261k | nodes.push(global_tree[cur_child].rchild); |
125 | 261k | *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props); |
126 | 261k | } |
127 | 640k | } |
128 | | |
129 | 640k | for (int16_t property : flat.properties) mark_property(property); |
130 | 320k | mark_property(flat.property0); |
131 | 320k | output.push_back(flat); |
132 | 320k | } |
133 | 1.00M | if (*num_props > kNumNonrefProperties) { |
134 | 3.98k | *num_props = |
135 | 3.98k | DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) * |
136 | 3.98k | kExtraPropsPerChannel + |
137 | 3.98k | kNumNonrefProperties; |
138 | 998k | } else { |
139 | 998k | *num_props = kNumNonrefProperties; |
140 | 998k | } |
141 | 1.00M | *use_wp = has_wp; |
142 | 1.00M | *wp_only = has_wp && !has_non_wp; |
143 | | |
144 | 1.00M | return output; |
145 | 1.00M | } |
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 | 959k | uint32_t &fl_v) { |
157 | 959k | JxlMemoryManager *memory_manager = image->memory_manager(); |
158 | 959k | Channel &channel = image->channel[chan]; |
159 | | |
160 | 959k | std::array<pixel_type, kNumStaticProperties> static_props = { |
161 | 959k | {chan, static_cast<int>(group_id)}}; |
162 | | // TODO(veluca): filter the tree according to static_props. |
163 | | |
164 | | // zero pixel channel? could happen |
165 | 959k | if (channel.w == 0 || channel.h == 0) return true; |
166 | | |
167 | 959k | bool tree_has_wp_prop_or_pred = false; |
168 | 959k | bool is_wp_only = false; |
169 | 959k | bool is_gradient_only = false; |
170 | 959k | size_t num_props; |
171 | 959k | FlatTree tree = |
172 | 959k | FilterTree(global_tree, static_props, &num_props, |
173 | 959k | &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 | 1.77M | for (auto &node : tree) { |
178 | 1.77M | if (node.property0 == -1) { |
179 | 1.57M | node.childID = context_map[node.childID]; |
180 | 1.57M | } |
181 | 1.77M | } |
182 | | |
183 | 959k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); |
184 | | |
185 | | // MAANS decode |
186 | 959k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, |
187 | 645M | pixel_type_w offset) -> pixel_type { |
188 | 645M | JXL_DASSERT((v & 0xFFFFFFFF) == v); |
189 | 645M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); |
190 | | // if it overflows, it overflows, and we have a problem anyway |
191 | 645M | return val * multiplier + offset; |
192 | 645M | }; 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 | 248M | pixel_type_w offset) -> pixel_type { | 188 | 248M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 248M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 248M | return val * multiplier + offset; | 192 | 248M | }; |
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 | 396M | pixel_type_w offset) -> pixel_type { | 188 | 396M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 396M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 396M | return val * multiplier + offset; | 192 | 396M | }; |
|
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 | 959k | const bool global_tree_is_all_gradient_noop = [&] { |
200 | 1.02M | for (const auto& n : global_tree) { |
201 | 1.02M | if (n.property == -1) { |
202 | 906k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || |
203 | 8.56k | n.multiplier != 1) |
204 | 898k | return false; |
205 | 906k | } else if (n.property >= kNumStaticProperties) { |
206 | 57.2k | return false; |
207 | 57.2k | } |
208 | 1.02M | } |
209 | 3.71k | return true; |
210 | 959k | }(); 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 | 142k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 146k | for (const auto& n : global_tree) { | 201 | 146k | if (n.property == -1) { | 202 | 129k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 2.19k | n.multiplier != 1) | 204 | 128k | return false; | 205 | 129k | } else if (n.property >= kNumStaticProperties) { | 206 | 12.8k | return false; | 207 | 12.8k | } | 208 | 146k | } | 209 | 1.82k | return true; | 210 | 142k | }(); |
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 | 816k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 878k | for (const auto& n : global_tree) { | 201 | 878k | if (n.property == -1) { | 202 | 776k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 6.36k | n.multiplier != 1) | 204 | 770k | return false; | 205 | 776k | } else if (n.property >= kNumStaticProperties) { | 206 | 44.3k | return false; | 207 | 44.3k | } | 208 | 878k | } | 209 | 1.88k | return true; | 210 | 816k | }(); |
|
211 | | |
212 | 959k | if (tree.size() == 1) { |
213 | | // special optimized case: no meta-adaptation, so no need |
214 | | // to compute properties. |
215 | 907k | Predictor predictor = tree[0].predictor; |
216 | 907k | int64_t offset = tree[0].predictor_offset; |
217 | 907k | int32_t multiplier = tree[0].multiplier; |
218 | 907k | size_t ctx_id = tree[0].childID; |
219 | 907k | if (predictor == Predictor::Zero) { |
220 | 712k | uint32_t value; |
221 | 712k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, |
222 | 712k | channel.w * channel.h)) { |
223 | | // Special-case: histogram has a single symbol, with no extra bits, and |
224 | | // we use ANS mode. |
225 | 108k | JXL_DEBUG_V(8, "Fastest track."); |
226 | 108k | pixel_type v = make_pixel(value, multiplier, offset); |
227 | 2.25M | for (size_t y = 0; y < channel.h; y++) { |
228 | 2.14M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
229 | 2.14M | std::fill(r, r + channel.w, v); |
230 | 2.14M | } |
231 | 603k | } else { |
232 | 603k | JXL_DEBUG_V(8, "Fast track."); |
233 | 603k | if (multiplier == 1 && offset == 0) { |
234 | 7.43M | for (size_t y = 0; y < channel.h; y++) { |
235 | 6.89M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
236 | 312M | for (size_t x = 0; x < channel.w; x++) { |
237 | 305M | uint32_t v = |
238 | 305M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
239 | 305M | r[x] = UnpackSigned(v); |
240 | 305M | } |
241 | 6.89M | } |
242 | 541k | } else { |
243 | 674k | for (size_t y = 0; y < channel.h; y++) { |
244 | 611k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
245 | 28.7M | for (size_t x = 0; x < channel.w; x++) { |
246 | 28.1M | uint32_t v = |
247 | 28.1M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, |
248 | 28.1M | br); |
249 | 28.1M | r[x] = make_pixel(v, multiplier, offset); |
250 | 28.1M | } |
251 | 611k | } |
252 | 62.7k | } |
253 | 603k | } |
254 | 712k | return true; |
255 | 712k | } else if (uses_lz77 && reader->IsHuffRleOnly() && |
256 | 445 | global_tree_is_all_gradient_noop) { |
257 | 441 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); |
258 | 441 | pixel_type_w sv = UnpackSigned(fl_v); |
259 | 6.04k | for (size_t y = 0; y < channel.h; y++) { |
260 | 5.60k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
261 | 5.60k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
262 | 5.60k | const pixel_type *JXL_RESTRICT rtopleft = |
263 | 5.60k | (y ? channel.Row(y - 1) - 1 : r - 1); |
264 | 5.60k | pixel_type_w guess_0 = (y ? rtop[0] : 0); |
265 | 5.60k | if (fl_run == 0) { |
266 | 5.60k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
267 | 5.60k | &fl_run); |
268 | 5.60k | sv = UnpackSigned(fl_v); |
269 | 5.60k | } else { |
270 | 0 | fl_run--; |
271 | 0 | } |
272 | 5.60k | r[0] = sv + guess_0; |
273 | 215k | for (size_t x = 1; x < channel.w; x++) { |
274 | 210k | pixel_type left = r[x - 1]; |
275 | 210k | pixel_type top = rtop[x]; |
276 | 210k | pixel_type topleft = rtopleft[x]; |
277 | 210k | pixel_type_w guess = ClampedGradient(top, left, topleft); |
278 | 210k | if (!fl_run) { |
279 | 210k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
280 | 210k | &fl_run); |
281 | 210k | sv = UnpackSigned(fl_v); |
282 | 210k | } else { |
283 | 0 | fl_run--; |
284 | 0 | } |
285 | 210k | r[x] = sv + guess; |
286 | 210k | } |
287 | 5.60k | } |
288 | 441 | return true; |
289 | 194k | } else if (predictor == Predictor::Gradient && offset == 0 && |
290 | 10.0k | multiplier == 1) { |
291 | 9.19k | JXL_DEBUG_V(8, "Gradient very fast track."); |
292 | 9.19k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
293 | 198k | for (size_t y = 0; y < channel.h; y++) { |
294 | 189k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
295 | 4.50M | for (size_t x = 0; x < channel.w; x++) { |
296 | 4.32M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
297 | 4.32M | pixel_type top = (y ? *(r + x - onerow) : left); |
298 | 4.32M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); |
299 | 4.32M | pixel_type guess = ClampedGradient(top, left, topleft); |
300 | 4.32M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
301 | 4.32M | ctx_id, br); |
302 | 4.32M | r[x] = make_pixel(v, 1, guess); |
303 | 4.32M | } |
304 | 189k | } |
305 | 9.19k | return true; |
306 | 9.19k | } |
307 | 907k | } |
308 | | |
309 | | // Check if this tree is a WP-only tree with a small enough property value |
310 | | // range. |
311 | 237k | if (is_wp_only) { |
312 | 17.7k | is_wp_only = TreeToLookupTable(tree, tree_lut); |
313 | 17.7k | } |
314 | 237k | if (is_gradient_only) { |
315 | 7.39k | is_gradient_only = TreeToLookupTable(tree, tree_lut); |
316 | 7.39k | } |
317 | | |
318 | 237k | if (is_gradient_only) { |
319 | 4.51k | JXL_DEBUG_V(8, "Gradient fast track."); |
320 | 4.51k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
321 | 183k | for (size_t y = 0; y < channel.h; y++) { |
322 | 179k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
323 | 4.73M | for (size_t x = 0; x < channel.w; x++) { |
324 | 4.55M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
325 | 4.55M | pixel_type_w top = (y ? *(r + x - onerow) : left); |
326 | 4.55M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); |
327 | 4.55M | int32_t guess = ClampedGradient(top, left, topleft); |
328 | 4.55M | uint32_t pos = |
329 | 4.55M | kPropRangeFast + |
330 | 4.55M | std::min<pixel_type_w>( |
331 | 4.55M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), |
332 | 4.55M | kPropRangeFast - 1); |
333 | 4.55M | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
334 | 4.55M | uint64_t v = |
335 | 4.55M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); |
336 | 4.55M | r[x] = make_pixel(v, 1, guess); |
337 | 4.55M | } |
338 | 179k | } |
339 | 232k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { |
340 | 8.44k | JXL_DEBUG_V(8, "WP fast track."); |
341 | 8.44k | weighted::State wp_state(wp_header, channel.w, channel.h); |
342 | 8.44k | Properties properties(1); |
343 | 336k | for (size_t y = 0; y < channel.h; y++) { |
344 | 327k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
345 | 327k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
346 | 327k | const pixel_type *JXL_RESTRICT rtoptop = |
347 | 327k | (y > 1 ? channel.Row(y - 2) : rtop); |
348 | 327k | const pixel_type *JXL_RESTRICT rtopleft = |
349 | 327k | (y ? channel.Row(y - 1) - 1 : r - 1); |
350 | 327k | const pixel_type *JXL_RESTRICT rtopright = |
351 | 327k | (y ? channel.Row(y - 1) + 1 : r - 1); |
352 | 327k | size_t x = 0; |
353 | 327k | { |
354 | 327k | size_t offset = 0; |
355 | 327k | pixel_type_w left = y ? rtop[x] : 0; |
356 | 327k | pixel_type_w toptop = y ? rtoptop[x] : 0; |
357 | 327k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); |
358 | 327k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
359 | 327k | x, y, channel.w, left, left, topright, left, toptop, &properties, |
360 | 327k | offset); |
361 | 327k | uint32_t pos = |
362 | 327k | kPropRangeFast + |
363 | 327k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
364 | 327k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
365 | 327k | uint64_t v = |
366 | 327k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
367 | 327k | r[x] = make_pixel(v, 1, guess); |
368 | 327k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
369 | 327k | } |
370 | 13.0M | for (x = 1; x + 1 < channel.w; x++) { |
371 | 12.7M | size_t offset = 0; |
372 | 12.7M | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
373 | 12.7M | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], |
374 | 12.7M | rtoptop[x], &properties, offset); |
375 | 12.7M | uint32_t pos = |
376 | 12.7M | kPropRangeFast + |
377 | 12.7M | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
378 | 12.7M | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
379 | 12.7M | uint64_t v = |
380 | 12.7M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
381 | 12.7M | r[x] = make_pixel(v, 1, guess); |
382 | 12.7M | wp_state.UpdateErrors(r[x], x, y, channel.w); |
383 | 12.7M | } |
384 | 327k | { |
385 | 327k | size_t offset = 0; |
386 | 327k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
387 | 327k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], |
388 | 327k | rtoptop[x], &properties, offset); |
389 | 327k | uint32_t pos = |
390 | 327k | kPropRangeFast + |
391 | 327k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
392 | 327k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
393 | 327k | uint64_t v = |
394 | 327k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
395 | 327k | r[x] = make_pixel(v, 1, guess); |
396 | 327k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
397 | 327k | } |
398 | 327k | } |
399 | 224k | } 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 | 190k | JXL_DEBUG_V(8, "Slow track."); |
403 | 190k | MATreeLookup tree_lookup(tree); |
404 | 190k | Properties properties = Properties(num_props); |
405 | 190k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
406 | 190k | JXL_ASSIGN_OR_RETURN( |
407 | 190k | Channel references, |
408 | 190k | Channel::Create(memory_manager, |
409 | 190k | properties.size() - kNumNonrefProperties, channel.w)); |
410 | 9.90M | for (size_t y = 0; y < channel.h; y++) { |
411 | 9.71M | pixel_type *JXL_RESTRICT p = channel.Row(y); |
412 | 9.71M | PrecomputeReferences(channel, y, *image, chan, &references); |
413 | 9.71M | InitPropsRow(&properties, static_props, y); |
414 | 9.71M | if (y > 1 && channel.w > 8 && references.w == 0) { |
415 | 21.7M | for (size_t x = 0; x < 2; x++) { |
416 | 14.5M | PredictionResult res = |
417 | 14.5M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
418 | 14.5M | tree_lookup, references); |
419 | 14.5M | uint64_t v = |
420 | 14.5M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
421 | 14.5M | p[x] = make_pixel(v, res.multiplier, res.guess); |
422 | 14.5M | } |
423 | 426M | for (size_t x = 2; x < channel.w - 2; x++) { |
424 | 419M | PredictionResult res = |
425 | 419M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, |
426 | 419M | tree_lookup, references); |
427 | 419M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
428 | 419M | res.context, br); |
429 | 419M | p[x] = make_pixel(v, res.multiplier, res.guess); |
430 | 419M | } |
431 | 21.7M | for (size_t x = channel.w - 2; x < channel.w; x++) { |
432 | 14.5M | PredictionResult res = |
433 | 14.5M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
434 | 14.5M | tree_lookup, references); |
435 | 14.5M | uint64_t v = |
436 | 14.5M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
437 | 14.5M | p[x] = make_pixel(v, res.multiplier, res.guess); |
438 | 14.5M | } |
439 | 7.26M | } else { |
440 | 29.0M | for (size_t x = 0; x < channel.w; x++) { |
441 | 26.5M | PredictionResult res = |
442 | 26.5M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
443 | 26.5M | tree_lookup, references); |
444 | 26.5M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
445 | 26.5M | res.context, br); |
446 | 26.5M | p[x] = make_pixel(v, res.multiplier, res.guess); |
447 | 26.5M | } |
448 | 2.45M | } |
449 | 9.71M | } |
450 | 190k | } else { |
451 | 34.0k | JXL_DEBUG_V(8, "Slowest track."); |
452 | 34.0k | MATreeLookup tree_lookup(tree); |
453 | 34.0k | Properties properties = Properties(num_props); |
454 | 34.0k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
455 | 34.0k | JXL_ASSIGN_OR_RETURN( |
456 | 34.0k | Channel references, |
457 | 34.0k | Channel::Create(memory_manager, |
458 | 34.0k | properties.size() - kNumNonrefProperties, channel.w)); |
459 | 34.0k | weighted::State wp_state(wp_header, channel.w, channel.h); |
460 | 1.56M | for (size_t y = 0; y < channel.h; y++) { |
461 | 1.53M | pixel_type *JXL_RESTRICT p = channel.Row(y); |
462 | 1.53M | InitPropsRow(&properties, static_props, y); |
463 | 1.53M | PrecomputeReferences(channel, y, *image, chan, &references); |
464 | 1.53M | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { |
465 | 1.54M | for (size_t x = 0; x < 2; x++) { |
466 | 1.02M | PredictionResult res = |
467 | 1.02M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
468 | 1.02M | tree_lookup, references, &wp_state); |
469 | 1.02M | uint64_t v = |
470 | 1.02M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
471 | 1.02M | p[x] = make_pixel(v, res.multiplier, res.guess); |
472 | 1.02M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
473 | 1.02M | } |
474 | 108M | for (size_t x = 2; x < channel.w - 2; x++) { |
475 | 107M | PredictionResult res = |
476 | 107M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, |
477 | 107M | tree_lookup, references, &wp_state); |
478 | 107M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
479 | 107M | res.context, br); |
480 | 107M | p[x] = make_pixel(v, res.multiplier, res.guess); |
481 | 107M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
482 | 107M | } |
483 | 1.54M | for (size_t x = channel.w - 2; x < channel.w; x++) { |
484 | 1.02M | PredictionResult res = |
485 | 1.02M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
486 | 1.02M | tree_lookup, references, &wp_state); |
487 | 1.02M | uint64_t v = |
488 | 1.02M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
489 | 1.02M | p[x] = make_pixel(v, res.multiplier, res.guess); |
490 | 1.02M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
491 | 1.02M | } |
492 | 1.01M | } else { |
493 | 10.7M | for (size_t x = 0; x < channel.w; x++) { |
494 | 9.73M | PredictionResult res = |
495 | 9.73M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
496 | 9.73M | tree_lookup, references, &wp_state); |
497 | 9.73M | uint64_t v = |
498 | 9.73M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
499 | 9.73M | p[x] = make_pixel(v, res.multiplier, res.guess); |
500 | 9.73M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
501 | 9.73M | } |
502 | 1.01M | } |
503 | 1.53M | } |
504 | 34.0k | } |
505 | 237k | return true; |
506 | 237k | } 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 | 142k | uint32_t &fl_v) { | 157 | 142k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 142k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 142k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 142k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 142k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 142k | bool tree_has_wp_prop_or_pred = false; | 168 | 142k | bool is_wp_only = false; | 169 | 142k | bool is_gradient_only = false; | 170 | 142k | size_t num_props; | 171 | 142k | FlatTree tree = | 172 | 142k | FilterTree(global_tree, static_props, &num_props, | 173 | 142k | &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 | 197k | for (auto &node : tree) { | 178 | 197k | if (node.property0 == -1) { | 179 | 183k | node.childID = context_map[node.childID]; | 180 | 183k | } | 181 | 197k | } | 182 | | | 183 | 142k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 142k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 142k | pixel_type_w offset) -> pixel_type { | 188 | 142k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 142k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 142k | return val * multiplier + offset; | 192 | 142k | }; | 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 | 142k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 142k | for (const auto& n : global_tree) { | 201 | 142k | if (n.property == -1) { | 202 | 142k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 142k | n.multiplier != 1) | 204 | 142k | return false; | 205 | 142k | } else if (n.property >= kNumStaticProperties) { | 206 | 142k | return false; | 207 | 142k | } | 208 | 142k | } | 209 | 142k | return true; | 210 | 142k | }(); | 211 | | | 212 | 142k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 129k | Predictor predictor = tree[0].predictor; | 216 | 129k | int64_t offset = tree[0].predictor_offset; | 217 | 129k | int32_t multiplier = tree[0].multiplier; | 218 | 129k | size_t ctx_id = tree[0].childID; | 219 | 129k | if (predictor == Predictor::Zero) { | 220 | 95.5k | uint32_t value; | 221 | 95.5k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 95.5k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 21.5k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 21.5k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 672k | for (size_t y = 0; y < channel.h; y++) { | 228 | 650k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 650k | std::fill(r, r + channel.w, v); | 230 | 650k | } | 231 | 74.0k | } else { | 232 | 74.0k | JXL_DEBUG_V(8, "Fast track."); | 233 | 74.0k | if (multiplier == 1 && offset == 0) { | 234 | 1.01M | for (size_t y = 0; y < channel.h; y++) { | 235 | 954k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 31.4M | for (size_t x = 0; x < channel.w; x++) { | 237 | 30.5M | uint32_t v = | 238 | 30.5M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 30.5M | r[x] = UnpackSigned(v); | 240 | 30.5M | } | 241 | 954k | } | 242 | 58.2k | } else { | 243 | 204k | for (size_t y = 0; y < channel.h; y++) { | 244 | 189k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 8.54M | for (size_t x = 0; x < channel.w; x++) { | 246 | 8.35M | uint32_t v = | 247 | 8.35M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 8.35M | br); | 249 | 8.35M | r[x] = make_pixel(v, multiplier, offset); | 250 | 8.35M | } | 251 | 189k | } | 252 | 15.7k | } | 253 | 74.0k | } | 254 | 95.5k | return true; | 255 | 95.5k | } else if (uses_lz77 && reader->IsHuffRleOnly() && | 256 | 445 | global_tree_is_all_gradient_noop) { | 257 | 441 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); | 258 | 441 | pixel_type_w sv = UnpackSigned(fl_v); | 259 | 6.04k | for (size_t y = 0; y < channel.h; y++) { | 260 | 5.60k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 261 | 5.60k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 262 | 5.60k | const pixel_type *JXL_RESTRICT rtopleft = | 263 | 5.60k | (y ? channel.Row(y - 1) - 1 : r - 1); | 264 | 5.60k | pixel_type_w guess_0 = (y ? rtop[0] : 0); | 265 | 5.60k | if (fl_run == 0) { | 266 | 5.60k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 267 | 5.60k | &fl_run); | 268 | 5.60k | sv = UnpackSigned(fl_v); | 269 | 5.60k | } else { | 270 | 0 | fl_run--; | 271 | 0 | } | 272 | 5.60k | r[0] = sv + guess_0; | 273 | 215k | for (size_t x = 1; x < channel.w; x++) { | 274 | 210k | pixel_type left = r[x - 1]; | 275 | 210k | pixel_type top = rtop[x]; | 276 | 210k | pixel_type topleft = rtopleft[x]; | 277 | 210k | pixel_type_w guess = ClampedGradient(top, left, topleft); | 278 | 210k | if (!fl_run) { | 279 | 210k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 280 | 210k | &fl_run); | 281 | 210k | sv = UnpackSigned(fl_v); | 282 | 210k | } else { | 283 | 0 | fl_run--; | 284 | 0 | } | 285 | 210k | r[x] = sv + guess; | 286 | 210k | } | 287 | 5.60k | } | 288 | 441 | return true; | 289 | 33.8k | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 1.84k | multiplier == 1) { | 291 | 1.49k | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 1.49k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 26.6k | for (size_t y = 0; y < channel.h; y++) { | 294 | 25.1k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 1.87M | for (size_t x = 0; x < channel.w; x++) { | 296 | 1.84M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 1.84M | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 1.84M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 1.84M | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 1.84M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 1.84M | ctx_id, br); | 302 | 1.84M | r[x] = make_pixel(v, 1, guess); | 303 | 1.84M | } | 304 | 25.1k | } | 305 | 1.49k | return true; | 306 | 1.49k | } | 307 | 129k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 45.2k | if (is_wp_only) { | 312 | 1.86k | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 1.86k | } | 314 | 45.2k | if (is_gradient_only) { | 315 | 1.56k | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 1.56k | } | 317 | | | 318 | 45.2k | if (is_gradient_only) { | 319 | 155 | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 155 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 12.0k | for (size_t y = 0; y < channel.h; y++) { | 322 | 11.8k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 1.87M | for (size_t x = 0; x < channel.w; x++) { | 324 | 1.86M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 1.86M | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 1.86M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 1.86M | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 1.86M | uint32_t pos = | 329 | 1.86M | kPropRangeFast + | 330 | 1.86M | std::min<pixel_type_w>( | 331 | 1.86M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 1.86M | kPropRangeFast - 1); | 333 | 1.86M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 1.86M | uint64_t v = | 335 | 1.86M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 1.86M | r[x] = make_pixel(v, 1, guess); | 337 | 1.86M | } | 338 | 11.8k | } | 339 | 45.0k | } 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 | 45.0k | } 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 | 33.0k | JXL_DEBUG_V(8, "Slow track."); | 403 | 33.0k | MATreeLookup tree_lookup(tree); | 404 | 33.0k | Properties properties = Properties(num_props); | 405 | 33.0k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 33.0k | JXL_ASSIGN_OR_RETURN( | 407 | 33.0k | Channel references, | 408 | 33.0k | Channel::Create(memory_manager, | 409 | 33.0k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 4.91M | for (size_t y = 0; y < channel.h; y++) { | 411 | 4.87M | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 4.87M | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 4.87M | InitPropsRow(&properties, static_props, y); | 414 | 4.87M | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 9.44M | for (size_t x = 0; x < 2; x++) { | 416 | 6.29M | PredictionResult res = | 417 | 6.29M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 6.29M | tree_lookup, references); | 419 | 6.29M | uint64_t v = | 420 | 6.29M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 6.29M | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 6.29M | } | 423 | 212M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 209M | PredictionResult res = | 425 | 209M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 209M | tree_lookup, references); | 427 | 209M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 209M | res.context, br); | 429 | 209M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 209M | } | 431 | 9.44M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 6.29M | PredictionResult res = | 433 | 6.29M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 6.29M | tree_lookup, references); | 435 | 6.29M | uint64_t v = | 436 | 6.29M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 6.29M | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 6.29M | } | 439 | 3.14M | } else { | 440 | 14.3M | for (size_t x = 0; x < channel.w; x++) { | 441 | 12.6M | PredictionResult res = | 442 | 12.6M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 12.6M | tree_lookup, references); | 444 | 12.6M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 12.6M | res.context, br); | 446 | 12.6M | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 12.6M | } | 448 | 1.73M | } | 449 | 4.87M | } | 450 | 33.0k | } else { | 451 | 12.0k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 12.0k | MATreeLookup tree_lookup(tree); | 453 | 12.0k | Properties properties = Properties(num_props); | 454 | 12.0k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 12.0k | JXL_ASSIGN_OR_RETURN( | 456 | 12.0k | Channel references, | 457 | 12.0k | Channel::Create(memory_manager, | 458 | 12.0k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 12.0k | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 69.2k | for (size_t y = 0; y < channel.h; y++) { | 461 | 57.2k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 57.2k | InitPropsRow(&properties, static_props, y); | 463 | 57.2k | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 57.2k | 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 | 57.2k | } else { | 493 | 2.02M | for (size_t x = 0; x < channel.w; x++) { | 494 | 1.96M | PredictionResult res = | 495 | 1.96M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 1.96M | tree_lookup, references, &wp_state); | 497 | 1.96M | uint64_t v = | 498 | 1.96M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 1.96M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 1.96M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 1.96M | } | 502 | 57.2k | } | 503 | 57.2k | } | 504 | 12.0k | } | 505 | 45.2k | return true; | 506 | 45.2k | } |
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 | 816k | uint32_t &fl_v) { | 157 | 816k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 816k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 816k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 816k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 816k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 816k | bool tree_has_wp_prop_or_pred = false; | 168 | 816k | bool is_wp_only = false; | 169 | 816k | bool is_gradient_only = false; | 170 | 816k | size_t num_props; | 171 | 816k | FlatTree tree = | 172 | 816k | FilterTree(global_tree, static_props, &num_props, | 173 | 816k | &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 | 1.58M | for (auto &node : tree) { | 178 | 1.58M | if (node.property0 == -1) { | 179 | 1.39M | node.childID = context_map[node.childID]; | 180 | 1.39M | } | 181 | 1.58M | } | 182 | | | 183 | 816k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 816k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 816k | pixel_type_w offset) -> pixel_type { | 188 | 816k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 816k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 816k | return val * multiplier + offset; | 192 | 816k | }; | 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 | 816k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 816k | for (const auto& n : global_tree) { | 201 | 816k | if (n.property == -1) { | 202 | 816k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 816k | n.multiplier != 1) | 204 | 816k | return false; | 205 | 816k | } else if (n.property >= kNumStaticProperties) { | 206 | 816k | return false; | 207 | 816k | } | 208 | 816k | } | 209 | 816k | return true; | 210 | 816k | }(); | 211 | | | 212 | 816k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 777k | Predictor predictor = tree[0].predictor; | 216 | 777k | int64_t offset = tree[0].predictor_offset; | 217 | 777k | int32_t multiplier = tree[0].multiplier; | 218 | 777k | size_t ctx_id = tree[0].childID; | 219 | 777k | if (predictor == Predictor::Zero) { | 220 | 616k | uint32_t value; | 221 | 616k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 616k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 86.9k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 86.9k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 1.58M | for (size_t y = 0; y < channel.h; y++) { | 228 | 1.49M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 1.49M | std::fill(r, r + channel.w, v); | 230 | 1.49M | } | 231 | 529k | } else { | 232 | 529k | JXL_DEBUG_V(8, "Fast track."); | 233 | 529k | if (multiplier == 1 && offset == 0) { | 234 | 6.42M | for (size_t y = 0; y < channel.h; y++) { | 235 | 5.93M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 281M | for (size_t x = 0; x < channel.w; x++) { | 237 | 275M | uint32_t v = | 238 | 275M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 275M | r[x] = UnpackSigned(v); | 240 | 275M | } | 241 | 5.93M | } | 242 | 482k | } else { | 243 | 469k | for (size_t y = 0; y < channel.h; y++) { | 244 | 422k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 20.2M | for (size_t x = 0; x < channel.w; x++) { | 246 | 19.8M | uint32_t v = | 247 | 19.8M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 19.8M | br); | 249 | 19.8M | r[x] = make_pixel(v, multiplier, offset); | 250 | 19.8M | } | 251 | 422k | } | 252 | 47.0k | } | 253 | 529k | } | 254 | 616k | return true; | 255 | 616k | } 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 | 160k | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 8.21k | multiplier == 1) { | 291 | 7.70k | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 7.70k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 171k | for (size_t y = 0; y < channel.h; y++) { | 294 | 163k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 2.63M | for (size_t x = 0; x < channel.w; x++) { | 296 | 2.47M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 2.47M | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 2.47M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 2.47M | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 2.47M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 2.47M | ctx_id, br); | 302 | 2.47M | r[x] = make_pixel(v, 1, guess); | 303 | 2.47M | } | 304 | 163k | } | 305 | 7.70k | return true; | 306 | 7.70k | } | 307 | 777k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 192k | if (is_wp_only) { | 312 | 15.9k | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 15.9k | } | 314 | 192k | if (is_gradient_only) { | 315 | 5.83k | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 5.83k | } | 317 | | | 318 | 192k | if (is_gradient_only) { | 319 | 4.36k | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 4.36k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 171k | for (size_t y = 0; y < channel.h; y++) { | 322 | 167k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 2.86M | for (size_t x = 0; x < channel.w; x++) { | 324 | 2.69M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 2.69M | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 2.69M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 2.69M | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 2.69M | uint32_t pos = | 329 | 2.69M | kPropRangeFast + | 330 | 2.69M | std::min<pixel_type_w>( | 331 | 2.69M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 2.69M | kPropRangeFast - 1); | 333 | 2.69M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 2.69M | uint64_t v = | 335 | 2.69M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 2.69M | r[x] = make_pixel(v, 1, guess); | 337 | 2.69M | } | 338 | 167k | } | 339 | 187k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 340 | 8.44k | JXL_DEBUG_V(8, "WP fast track."); | 341 | 8.44k | weighted::State wp_state(wp_header, channel.w, channel.h); | 342 | 8.44k | Properties properties(1); | 343 | 336k | for (size_t y = 0; y < channel.h; y++) { | 344 | 327k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 345 | 327k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 346 | 327k | const pixel_type *JXL_RESTRICT rtoptop = | 347 | 327k | (y > 1 ? channel.Row(y - 2) : rtop); | 348 | 327k | const pixel_type *JXL_RESTRICT rtopleft = | 349 | 327k | (y ? channel.Row(y - 1) - 1 : r - 1); | 350 | 327k | const pixel_type *JXL_RESTRICT rtopright = | 351 | 327k | (y ? channel.Row(y - 1) + 1 : r - 1); | 352 | 327k | size_t x = 0; | 353 | 327k | { | 354 | 327k | size_t offset = 0; | 355 | 327k | pixel_type_w left = y ? rtop[x] : 0; | 356 | 327k | pixel_type_w toptop = y ? rtoptop[x] : 0; | 357 | 327k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 358 | 327k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 359 | 327k | x, y, channel.w, left, left, topright, left, toptop, &properties, | 360 | 327k | offset); | 361 | 327k | uint32_t pos = | 362 | 327k | kPropRangeFast + | 363 | 327k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 364 | 327k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 365 | 327k | uint64_t v = | 366 | 327k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 367 | 327k | r[x] = make_pixel(v, 1, guess); | 368 | 327k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 369 | 327k | } | 370 | 13.0M | for (x = 1; x + 1 < channel.w; x++) { | 371 | 12.7M | size_t offset = 0; | 372 | 12.7M | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 373 | 12.7M | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 374 | 12.7M | rtoptop[x], &properties, offset); | 375 | 12.7M | uint32_t pos = | 376 | 12.7M | kPropRangeFast + | 377 | 12.7M | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 378 | 12.7M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 379 | 12.7M | uint64_t v = | 380 | 12.7M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 381 | 12.7M | r[x] = make_pixel(v, 1, guess); | 382 | 12.7M | wp_state.UpdateErrors(r[x], x, y, channel.w); | 383 | 12.7M | } | 384 | 327k | { | 385 | 327k | size_t offset = 0; | 386 | 327k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 387 | 327k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 388 | 327k | rtoptop[x], &properties, offset); | 389 | 327k | uint32_t pos = | 390 | 327k | kPropRangeFast + | 391 | 327k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 392 | 327k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 393 | 327k | uint64_t v = | 394 | 327k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 395 | 327k | r[x] = make_pixel(v, 1, guess); | 396 | 327k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 397 | 327k | } | 398 | 327k | } | 399 | 179k | } 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 | 157k | JXL_DEBUG_V(8, "Slow track."); | 403 | 157k | MATreeLookup tree_lookup(tree); | 404 | 157k | Properties properties = Properties(num_props); | 405 | 157k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 157k | JXL_ASSIGN_OR_RETURN( | 407 | 157k | Channel references, | 408 | 157k | Channel::Create(memory_manager, | 409 | 157k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 4.99M | for (size_t y = 0; y < channel.h; y++) { | 411 | 4.83M | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 4.83M | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 4.83M | InitPropsRow(&properties, static_props, y); | 414 | 4.83M | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 12.3M | for (size_t x = 0; x < 2; x++) { | 416 | 8.23M | PredictionResult res = | 417 | 8.23M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 8.23M | tree_lookup, references); | 419 | 8.23M | uint64_t v = | 420 | 8.23M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 8.23M | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 8.23M | } | 423 | 213M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 209M | PredictionResult res = | 425 | 209M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 209M | tree_lookup, references); | 427 | 209M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 209M | res.context, br); | 429 | 209M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 209M | } | 431 | 12.3M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 8.23M | PredictionResult res = | 433 | 8.23M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 8.23M | tree_lookup, references); | 435 | 8.23M | uint64_t v = | 436 | 8.23M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 8.23M | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 8.23M | } | 439 | 4.11M | } else { | 440 | 14.6M | for (size_t x = 0; x < channel.w; x++) { | 441 | 13.9M | PredictionResult res = | 442 | 13.9M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 13.9M | tree_lookup, references); | 444 | 13.9M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 13.9M | res.context, br); | 446 | 13.9M | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 13.9M | } | 448 | 724k | } | 449 | 4.83M | } | 450 | 157k | } else { | 451 | 22.0k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 22.0k | MATreeLookup tree_lookup(tree); | 453 | 22.0k | Properties properties = Properties(num_props); | 454 | 22.0k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 22.0k | JXL_ASSIGN_OR_RETURN( | 456 | 22.0k | Channel references, | 457 | 22.0k | Channel::Create(memory_manager, | 458 | 22.0k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 22.0k | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 1.49M | for (size_t y = 0; y < channel.h; y++) { | 461 | 1.47M | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 1.47M | InitPropsRow(&properties, static_props, y); | 463 | 1.47M | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 1.47M | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 465 | 1.54M | for (size_t x = 0; x < 2; x++) { | 466 | 1.02M | PredictionResult res = | 467 | 1.02M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 1.02M | tree_lookup, references, &wp_state); | 469 | 1.02M | uint64_t v = | 470 | 1.02M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 1.02M | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 1.02M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 1.02M | } | 474 | 108M | for (size_t x = 2; x < channel.w - 2; x++) { | 475 | 107M | PredictionResult res = | 476 | 107M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 477 | 107M | tree_lookup, references, &wp_state); | 478 | 107M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 479 | 107M | res.context, br); | 480 | 107M | p[x] = make_pixel(v, res.multiplier, res.guess); | 481 | 107M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 482 | 107M | } | 483 | 1.54M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 484 | 1.02M | PredictionResult res = | 485 | 1.02M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 486 | 1.02M | tree_lookup, references, &wp_state); | 487 | 1.02M | uint64_t v = | 488 | 1.02M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 489 | 1.02M | p[x] = make_pixel(v, res.multiplier, res.guess); | 490 | 1.02M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 491 | 1.02M | } | 492 | 960k | } else { | 493 | 8.73M | for (size_t x = 0; x < channel.w; x++) { | 494 | 7.77M | PredictionResult res = | 495 | 7.77M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 7.77M | tree_lookup, references, &wp_state); | 497 | 7.77M | uint64_t v = | 498 | 7.77M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 7.77M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 7.77M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 7.77M | } | 502 | 960k | } | 503 | 1.47M | } | 504 | 22.0k | } | 505 | 192k | return true; | 506 | 192k | } |
|
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 | 959k | uint32_t &fl_v) { |
517 | 959k | if (reader->UsesLZ77()) { |
518 | 142k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>( |
519 | 142k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
520 | 142k | tree_lut, image, fl_run, fl_v); |
521 | 816k | } else { |
522 | 816k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>( |
523 | 816k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
524 | 816k | tree_lut, image, fl_run, fl_v); |
525 | 816k | } |
526 | 959k | } |
527 | | |
528 | 894k | GroupHeader::GroupHeader() { Bundle::Init(this); } |
529 | | |
530 | | Status ValidateChannelDimensions(const Image &image, |
531 | 221k | const ModularOptions &options) { |
532 | 221k | size_t nb_channels = image.channel.size(); |
533 | 443k | for (bool is_dc : {true, false}) { |
534 | 443k | size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1); |
535 | 443k | size_t c = image.nb_meta_channels; |
536 | 2.68M | for (; c < nb_channels; c++) { |
537 | 2.25M | const Channel &ch = image.channel[c]; |
538 | 2.25M | if (ch.w > options.group_dim || ch.h > options.group_dim) break; |
539 | 2.25M | } |
540 | 503k | for (; c < nb_channels; c++) { |
541 | 60.0k | const Channel &ch = image.channel[c]; |
542 | 60.0k | if (ch.w == 0 || ch.h == 0) continue; // skip empty |
543 | 59.0k | bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3; |
544 | 59.0k | if (is_dc_channel != is_dc) continue; |
545 | 29.5k | size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift); |
546 | 29.5k | if (tile_dim == 0) { |
547 | 9 | return JXL_FAILURE("Inconsistent transforms"); |
548 | 9 | } |
549 | 29.5k | } |
550 | 443k | } |
551 | 221k | return true; |
552 | 221k | } |
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 | 360k | const bool allow_truncated_group) { |
559 | 360k | if (image.channel.empty()) return true; |
560 | 255k | JxlMemoryManager *memory_manager = image.memory_manager(); |
561 | | |
562 | | // decode transforms |
563 | 255k | Status status = Bundle::Read(br, &header); |
564 | 255k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status); |
565 | 229k | if (status.IsFatalError()) return status; |
566 | 229k | 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 | 229k | JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ", |
577 | 229k | header.transforms.size()); |
578 | 229k | image.transform = header.transforms; |
579 | 229k | for (Transform &transform : image.transform) { |
580 | 60.0k | JXL_RETURN_IF_ERROR(transform.MetaApply(image)); |
581 | 60.0k | } |
582 | 219k | if (image.error) { |
583 | 0 | return JXL_FAILURE("Corrupt file. Aborting."); |
584 | 0 | } |
585 | 219k | JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options)); |
586 | | |
587 | 219k | size_t nb_channels = image.channel.size(); |
588 | | |
589 | 219k | size_t num_chans = 0; |
590 | 219k | size_t distance_multiplier = 0; |
591 | 1.34M | for (size_t i = 0; i < nb_channels; i++) { |
592 | 1.12M | Channel &channel = image.channel[i]; |
593 | 1.12M | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
594 | 1.11M | channel.h > options->max_chan_size)) { |
595 | 5.42k | break; |
596 | 5.42k | } |
597 | 1.12M | if (!channel.w || !channel.h) { |
598 | 46.8k | continue; // skip empty channels |
599 | 46.8k | } |
600 | 1.07M | if (channel.w > distance_multiplier) { |
601 | 265k | distance_multiplier = channel.w; |
602 | 265k | } |
603 | 1.07M | num_chans++; |
604 | 1.07M | } |
605 | 219k | if (num_chans == 0) return true; |
606 | | |
607 | 215k | size_t next_channel = 0; |
608 | 215k | auto scope_guard = MakeScopeGuard([&]() { |
609 | 163k | for (size_t c = next_channel; c < image.channel.size(); c++) { |
610 | 126k | ZeroFillImage(&image.channel[c].plane); |
611 | 126k | } |
612 | 37.1k | }); |
613 | | // Do not do anything if truncated groups are not allowed. |
614 | 215k | if (allow_truncated_group) scope_guard.Disarm(); |
615 | | |
616 | | // Read tree. |
617 | 215k | Tree tree_storage; |
618 | 215k | std::vector<uint8_t> context_map_storage; |
619 | 215k | ANSCode code_storage; |
620 | 215k | const Tree *tree = &tree_storage; |
621 | 215k | const ANSCode *code = &code_storage; |
622 | 215k | const std::vector<uint8_t> *context_map = &context_map_storage; |
623 | 215k | if (!header.use_global_tree) { |
624 | 161k | uint64_t max_tree_size = 1024; |
625 | 888k | for (size_t i = 0; i < nb_channels; i++) { |
626 | 727k | Channel &channel = image.channel[i]; |
627 | 727k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
628 | 724k | channel.h > options->max_chan_size)) { |
629 | 89 | break; |
630 | 89 | } |
631 | 727k | uint64_t pixels = channel.w * channel.h; |
632 | 727k | max_tree_size += pixels; |
633 | 727k | } |
634 | 161k | max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size); |
635 | 161k | JXL_RETURN_IF_ERROR( |
636 | 161k | DecodeTree(memory_manager, br, &tree_storage, max_tree_size)); |
637 | 140k | JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, |
638 | 140k | (tree_storage.size() + 1) / 2, |
639 | 140k | &code_storage, &context_map_storage)); |
640 | 140k | } else { |
641 | 54.5k | if (!global_tree || !global_code || !global_ctx_map || |
642 | 54.5k | global_tree->empty()) { |
643 | 1.00k | return JXL_FAILURE("No global tree available but one was requested"); |
644 | 1.00k | } |
645 | 53.5k | tree = global_tree; |
646 | 53.5k | code = global_code; |
647 | 53.5k | context_map = global_ctx_map; |
648 | 53.5k | } |
649 | | |
650 | | // Read channels |
651 | 374k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader, |
652 | 374k | ANSSymbolReader::Create(code, br, distance_multiplier)); |
653 | 374k | auto tree_lut = jxl::make_unique<TreeLut<uint8_t, false, false>>(); |
654 | 374k | uint32_t fl_run = 0; |
655 | 374k | uint32_t fl_v = 0; |
656 | 1.18M | for (; next_channel < nb_channels; next_channel++) { |
657 | 1.00M | Channel &channel = image.channel[next_channel]; |
658 | 1.00M | if (next_channel >= image.nb_meta_channels && |
659 | 999k | (channel.w > options->max_chan_size || |
660 | 998k | channel.h > options->max_chan_size)) { |
661 | 1.84k | break; |
662 | 1.84k | } |
663 | 1.00M | if (!channel.w || !channel.h) { |
664 | 44.9k | continue; // skip empty channels |
665 | 44.9k | } |
666 | 959k | JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS( |
667 | 959k | br, &reader, *context_map, *tree, header.wp_header, next_channel, |
668 | 959k | group_id, *tree_lut, &image, fl_run, fl_v)); |
669 | | |
670 | | // Truncated group. |
671 | 959k | if (!br->AllReadsWithinBounds()) { |
672 | 8.77k | if (!allow_truncated_group) return JXL_FAILURE("Truncated input"); |
673 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun in ModularDecode"); |
674 | 8.77k | } |
675 | 959k | } |
676 | | |
677 | | // Make sure no zero-filling happens even if next_channel < nb_channels. |
678 | 178k | scope_guard.Disarm(); |
679 | | |
680 | 178k | if (!reader.CheckANSFinalState()) { |
681 | 0 | return JXL_FAILURE("ANS decode final state failed"); |
682 | 0 | } |
683 | 178k | return true; |
684 | 178k | } |
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 | 360k | bool allow_truncated_group) { |
692 | 360k | std::vector<std::pair<size_t, size_t>> req_sizes; |
693 | 360k | req_sizes.reserve(image.channel.size()); |
694 | 859k | for (const auto &c : image.channel) { |
695 | 859k | req_sizes.emplace_back(c.w, c.h); |
696 | 859k | } |
697 | 360k | GroupHeader local_header; |
698 | 360k | if (header == nullptr) header = &local_header; |
699 | 360k | size_t bit_pos = br->TotalBitsConsumed(); |
700 | 360k | auto dec_status = ModularDecode(br, image, *header, group_id, options, tree, |
701 | 360k | code, ctx_map, allow_truncated_group); |
702 | 360k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status); |
703 | 286k | if (dec_status.IsFatalError()) return dec_status; |
704 | 286k | if (undo_transforms) image.undo_transforms(header->wp_header); |
705 | 286k | if (image.error) return JXL_FAILURE("Corrupt file. Aborting."); |
706 | 286k | JXL_DEBUG_V(4, |
707 | 286k | "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS |
708 | 286k | " image from %" PRIuS " bytes", |
709 | 286k | image.w, image.h, image.channel.size(), |
710 | 286k | (br->TotalBitsConsumed() - bit_pos) / 8); |
711 | 286k | JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str()); |
712 | 286k | (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 | 286k | if (undo_transforms) { |
717 | 115k | JXL_ENSURE(image.channel.size() == req_sizes.size()); |
718 | 518k | for (size_t c = 0; c < req_sizes.size(); c++) { |
719 | 403k | JXL_ENSURE(req_sizes[c].first == image.channel[c].w); |
720 | 403k | JXL_ENSURE(req_sizes[c].second == image.channel[c].h); |
721 | 403k | } |
722 | 115k | } |
723 | 286k | return dec_status; |
724 | 286k | } |
725 | | |
726 | | } // namespace jxl |