/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 | 161k | bool *gradient_only) { |
46 | 161k | *num_props = 0; |
47 | 161k | bool has_wp = false; |
48 | 161k | bool has_non_wp = false; |
49 | 161k | *gradient_only = true; |
50 | 180k | const auto mark_property = [&](int32_t p) { |
51 | 180k | if (p == kWPProp) { |
52 | 9.77k | has_wp = true; |
53 | 170k | } else if (p >= kNumStaticProperties) { |
54 | 107k | has_non_wp = true; |
55 | 107k | } |
56 | 180k | if (p >= kNumStaticProperties && p != kGradientProp) { |
57 | 103k | *gradient_only = false; |
58 | 103k | } |
59 | 180k | }; |
60 | 161k | FlatTree output; |
61 | 161k | std::queue<size_t> nodes; |
62 | 161k | 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 | 563k | while (!nodes.empty()) { |
70 | 401k | size_t cur = nodes.front(); |
71 | 401k | nodes.pop(); |
72 | | // Skip nodes that we can decide now, by jumping directly to their children. |
73 | 427k | while (global_tree[cur].property < kNumStaticProperties && |
74 | 368k | global_tree[cur].property != -1) { |
75 | 25.8k | if (static_props[global_tree[cur].property] > global_tree[cur].splitval) { |
76 | 15.5k | cur = global_tree[cur].lchild; |
77 | 15.5k | } else { |
78 | 10.2k | cur = global_tree[cur].rchild; |
79 | 10.2k | } |
80 | 25.8k | } |
81 | 401k | FlatDecisionNode flat; |
82 | 401k | if (global_tree[cur].property == -1) { |
83 | 340k | flat.property0 = -1; |
84 | 340k | flat.childID = global_tree[cur].lchild; |
85 | 340k | flat.predictor = global_tree[cur].predictor; |
86 | 340k | flat.predictor_offset = global_tree[cur].predictor_offset; |
87 | 340k | flat.multiplier = global_tree[cur].multiplier; |
88 | 340k | *gradient_only &= flat.predictor == Predictor::Gradient; |
89 | 340k | has_wp |= flat.predictor == Predictor::Weighted; |
90 | 340k | has_non_wp |= flat.predictor != Predictor::Weighted; |
91 | 340k | output.push_back(flat); |
92 | 340k | continue; |
93 | 340k | } |
94 | 61.8k | flat.childID = output.size() + nodes.size() + 1; |
95 | | |
96 | 61.8k | flat.property0 = global_tree[cur].property; |
97 | 61.8k | *num_props = std::max<size_t>(flat.property0 + 1, *num_props); |
98 | 61.8k | flat.splitval0 = global_tree[cur].splitval; |
99 | | |
100 | 182k | for (size_t i = 0; i < 2; i++) { |
101 | 120k | size_t cur_child = |
102 | 120k | i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild; |
103 | | // Skip nodes that we can decide now. |
104 | 139k | while (global_tree[cur_child].property < kNumStaticProperties && |
105 | 83.8k | global_tree[cur_child].property != -1) { |
106 | 18.4k | if (static_props[global_tree[cur_child].property] > |
107 | 18.4k | global_tree[cur_child].splitval) { |
108 | 11.7k | cur_child = global_tree[cur_child].lchild; |
109 | 11.7k | } else { |
110 | 6.68k | cur_child = global_tree[cur_child].rchild; |
111 | 6.68k | } |
112 | 18.4k | } |
113 | | // We ended up in a leaf, add a placeholder decision and two copies of the |
114 | | // leaf. |
115 | 120k | if (global_tree[cur_child].property == -1) { |
116 | 66.9k | flat.properties[i] = 0; |
117 | 66.9k | flat.splitvals[i] = 0; |
118 | 66.9k | nodes.push(cur_child); |
119 | 66.9k | nodes.push(cur_child); |
120 | 66.9k | } else { |
121 | 53.7k | flat.properties[i] = global_tree[cur_child].property; |
122 | 53.7k | flat.splitvals[i] = global_tree[cur_child].splitval; |
123 | 53.7k | nodes.push(global_tree[cur_child].lchild); |
124 | 53.7k | nodes.push(global_tree[cur_child].rchild); |
125 | 53.7k | *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props); |
126 | 53.7k | } |
127 | 120k | } |
128 | | |
129 | 120k | for (int16_t property : flat.properties) mark_property(property); |
130 | 61.8k | mark_property(flat.property0); |
131 | 61.8k | output.push_back(flat); |
132 | 61.8k | } |
133 | 161k | if (*num_props > kNumNonrefProperties) { |
134 | 24 | *num_props = |
135 | 24 | DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) * |
136 | 24 | kExtraPropsPerChannel + |
137 | 24 | kNumNonrefProperties; |
138 | 161k | } else { |
139 | 161k | *num_props = kNumNonrefProperties; |
140 | 161k | } |
141 | 161k | *use_wp = has_wp; |
142 | 161k | *wp_only = has_wp && !has_non_wp; |
143 | | |
144 | 161k | return output; |
145 | 161k | } |
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 | 161k | uint32_t &fl_v) { |
157 | 161k | JxlMemoryManager *memory_manager = image->memory_manager(); |
158 | 161k | Channel &channel = image->channel[chan]; |
159 | | |
160 | 161k | std::array<pixel_type, kNumStaticProperties> static_props = { |
161 | 161k | {chan, static_cast<int>(group_id)}}; |
162 | | // TODO(veluca): filter the tree according to static_props. |
163 | | |
164 | | // zero pixel channel? could happen |
165 | 161k | if (channel.w == 0 || channel.h == 0) return true; |
166 | | |
167 | 161k | bool tree_has_wp_prop_or_pred = false; |
168 | 161k | bool is_wp_only = false; |
169 | 161k | bool is_gradient_only = false; |
170 | 161k | size_t num_props; |
171 | 161k | FlatTree tree = |
172 | 161k | FilterTree(global_tree, static_props, &num_props, |
173 | 161k | &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 | 440k | for (auto &node : tree) { |
178 | 440k | if (node.property0 == -1) { |
179 | 371k | node.childID = context_map[node.childID]; |
180 | 371k | } |
181 | 440k | } |
182 | | |
183 | 161k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); |
184 | | |
185 | | // MAANS decode |
186 | 161k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, |
187 | 30.4M | pixel_type_w offset) -> pixel_type { |
188 | 30.4M | JXL_DASSERT((v & 0xFFFFFFFF) == v); |
189 | 30.4M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); |
190 | | // if it overflows, it overflows, and we have a problem anyway |
191 | 30.4M | return val * multiplier + offset; |
192 | 30.4M | }; 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 | 3.96M | pixel_type_w offset) -> pixel_type { | 188 | 3.96M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 3.96M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 3.96M | return val * multiplier + offset; | 192 | 3.96M | }; |
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 | 26.4M | pixel_type_w offset) -> pixel_type { | 188 | 26.4M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 26.4M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 26.4M | return val * multiplier + offset; | 192 | 26.4M | }; |
|
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 | 162k | const bool global_tree_is_all_gradient_noop = [&] { |
200 | 168k | for (const auto& n : global_tree) { |
201 | 168k | if (n.property == -1) { |
202 | 156k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || |
203 | 318 | n.multiplier != 1) |
204 | 156k | return false; |
205 | 156k | } else if (n.property >= kNumStaticProperties) { |
206 | 5.26k | return false; |
207 | 5.26k | } |
208 | 168k | } |
209 | 309 | return true; |
210 | 162k | }(); 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 | 9.20k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 9.21k | for (const auto& n : global_tree) { | 201 | 9.21k | if (n.property == -1) { | 202 | 9.20k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 0 | n.multiplier != 1) | 204 | 9.20k | return false; | 205 | 9.20k | } else if (n.property >= kNumStaticProperties) { | 206 | 3 | return false; | 207 | 3 | } | 208 | 9.21k | } | 209 | 18.4E | return true; | 210 | 9.20k | }(); |
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 | 152k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 159k | for (const auto& n : global_tree) { | 201 | 159k | if (n.property == -1) { | 202 | 147k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 318 | n.multiplier != 1) | 204 | 147k | return false; | 205 | 147k | } else if (n.property >= kNumStaticProperties) { | 206 | 5.25k | return false; | 207 | 5.25k | } | 208 | 159k | } | 209 | 317 | return true; | 210 | 152k | }(); |
|
211 | | |
212 | 161k | if (tree.size() == 1) { |
213 | | // special optimized case: no meta-adaptation, so no need |
214 | | // to compute properties. |
215 | 156k | Predictor predictor = tree[0].predictor; |
216 | 156k | int64_t offset = tree[0].predictor_offset; |
217 | 156k | int32_t multiplier = tree[0].multiplier; |
218 | 156k | size_t ctx_id = tree[0].childID; |
219 | 156k | if (predictor == Predictor::Zero) { |
220 | 155k | uint32_t value; |
221 | 155k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, |
222 | 155k | channel.w * channel.h)) { |
223 | | // Special-case: histogram has a single symbol, with no extra bits, and |
224 | | // we use ANS mode. |
225 | 10.6k | JXL_DEBUG_V(8, "Fastest track."); |
226 | 10.6k | pixel_type v = make_pixel(value, multiplier, offset); |
227 | 688k | for (size_t y = 0; y < channel.h; y++) { |
228 | 677k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
229 | 677k | std::fill(r, r + channel.w, v); |
230 | 677k | } |
231 | 145k | } else { |
232 | 145k | JXL_DEBUG_V(8, "Fast track."); |
233 | 145k | if (multiplier == 1 && offset == 0) { |
234 | 2.79M | for (size_t y = 0; y < channel.h; y++) { |
235 | 2.64M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
236 | 120M | for (size_t x = 0; x < channel.w; x++) { |
237 | 117M | uint32_t v = |
238 | 117M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
239 | 117M | r[x] = UnpackSigned(v); |
240 | 117M | } |
241 | 2.64M | } |
242 | 145k | } else { |
243 | 8.41k | for (size_t y = 0; y < channel.h; y++) { |
244 | 8.30k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
245 | 2.01M | for (size_t x = 0; x < channel.w; x++) { |
246 | 2.00M | uint32_t v = |
247 | 2.00M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, |
248 | 2.00M | br); |
249 | 2.00M | r[x] = make_pixel(v, multiplier, offset); |
250 | 2.00M | } |
251 | 8.30k | } |
252 | 114 | } |
253 | 145k | } |
254 | 155k | return true; |
255 | 155k | } 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 | 879 | } else if (predictor == Predictor::Gradient && offset == 0 && |
290 | 318 | multiplier == 1) { |
291 | 318 | JXL_DEBUG_V(8, "Gradient very fast track."); |
292 | 318 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
293 | 3.18k | for (size_t y = 0; y < channel.h; y++) { |
294 | 2.86k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
295 | 37.9k | for (size_t x = 0; x < channel.w; x++) { |
296 | 35.0k | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
297 | 35.0k | pixel_type top = (y ? *(r + x - onerow) : left); |
298 | 35.0k | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); |
299 | 35.0k | pixel_type guess = ClampedGradient(top, left, topleft); |
300 | 35.0k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
301 | 35.0k | ctx_id, br); |
302 | 35.0k | r[x] = make_pixel(v, 1, guess); |
303 | 35.0k | } |
304 | 2.86k | } |
305 | 318 | return true; |
306 | 318 | } |
307 | 156k | } |
308 | | |
309 | | // Check if this tree is a WP-only tree with a small enough property value |
310 | | // range. |
311 | 5.64k | if (is_wp_only) { |
312 | 300 | is_wp_only = TreeToLookupTable(tree, tree_lut); |
313 | 300 | } |
314 | 5.64k | if (is_gradient_only) { |
315 | 87 | is_gradient_only = TreeToLookupTable(tree, tree_lut); |
316 | 87 | } |
317 | | |
318 | 5.64k | if (is_gradient_only) { |
319 | 45 | JXL_DEBUG_V(8, "Gradient fast track."); |
320 | 45 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
321 | 869 | for (size_t y = 0; y < channel.h; y++) { |
322 | 824 | pixel_type *JXL_RESTRICT r = channel.Row(y); |
323 | 19.0k | for (size_t x = 0; x < channel.w; x++) { |
324 | 18.2k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
325 | 18.2k | pixel_type_w top = (y ? *(r + x - onerow) : left); |
326 | 18.2k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); |
327 | 18.2k | int32_t guess = ClampedGradient(top, left, topleft); |
328 | 18.2k | uint32_t pos = |
329 | 18.2k | kPropRangeFast + |
330 | 18.2k | std::min<pixel_type_w>( |
331 | 18.2k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), |
332 | 18.2k | kPropRangeFast - 1); |
333 | 18.2k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
334 | 18.2k | uint64_t v = |
335 | 18.2k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); |
336 | 18.2k | r[x] = make_pixel(v, 1, guess); |
337 | 18.2k | } |
338 | 824 | } |
339 | 5.60k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { |
340 | 275 | JXL_DEBUG_V(8, "WP fast track."); |
341 | 275 | weighted::State wp_state(wp_header, channel.w, channel.h); |
342 | 275 | Properties properties(1); |
343 | 2.45k | for (size_t y = 0; y < channel.h; y++) { |
344 | 2.17k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
345 | 2.17k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
346 | 2.17k | const pixel_type *JXL_RESTRICT rtoptop = |
347 | 2.17k | (y > 1 ? channel.Row(y - 2) : rtop); |
348 | 2.17k | const pixel_type *JXL_RESTRICT rtopleft = |
349 | 2.17k | (y ? channel.Row(y - 1) - 1 : r - 1); |
350 | 2.17k | const pixel_type *JXL_RESTRICT rtopright = |
351 | 2.17k | (y ? channel.Row(y - 1) + 1 : r - 1); |
352 | 2.17k | size_t x = 0; |
353 | 2.17k | { |
354 | 2.17k | size_t offset = 0; |
355 | 2.17k | pixel_type_w left = y ? rtop[x] : 0; |
356 | 2.17k | pixel_type_w toptop = y ? rtoptop[x] : 0; |
357 | 2.17k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); |
358 | 2.17k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
359 | 2.17k | x, y, channel.w, left, left, topright, left, toptop, &properties, |
360 | 2.17k | offset); |
361 | 2.17k | uint32_t pos = |
362 | 2.17k | kPropRangeFast + |
363 | 2.17k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
364 | 2.17k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
365 | 2.17k | uint64_t v = |
366 | 2.17k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
367 | 2.17k | r[x] = make_pixel(v, 1, guess); |
368 | 2.17k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
369 | 2.17k | } |
370 | 90.6k | for (x = 1; x + 1 < channel.w; x++) { |
371 | 88.4k | size_t offset = 0; |
372 | 88.4k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
373 | 88.4k | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], |
374 | 88.4k | rtoptop[x], &properties, offset); |
375 | 88.4k | uint32_t pos = |
376 | 88.4k | kPropRangeFast + |
377 | 88.4k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
378 | 88.4k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
379 | 88.4k | uint64_t v = |
380 | 88.4k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
381 | 88.4k | r[x] = make_pixel(v, 1, guess); |
382 | 88.4k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
383 | 88.4k | } |
384 | 2.17k | { |
385 | 2.17k | size_t offset = 0; |
386 | 2.17k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
387 | 2.17k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], |
388 | 2.17k | rtoptop[x], &properties, offset); |
389 | 2.17k | uint32_t pos = |
390 | 2.17k | kPropRangeFast + |
391 | 2.17k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
392 | 2.17k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
393 | 2.17k | uint64_t v = |
394 | 2.17k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
395 | 2.17k | r[x] = make_pixel(v, 1, guess); |
396 | 2.17k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
397 | 2.17k | } |
398 | 2.17k | } |
399 | 5.32k | } 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.46k | JXL_DEBUG_V(8, "Slow track."); |
403 | 3.46k | MATreeLookup tree_lookup(tree); |
404 | 3.46k | Properties properties = Properties(num_props); |
405 | 3.46k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
406 | 3.46k | JXL_ASSIGN_OR_RETURN( |
407 | 3.46k | Channel references, |
408 | 3.46k | Channel::Create(memory_manager, |
409 | 3.46k | 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 | 390k | for (size_t x = 0; x < 2; x++) { |
416 | 260k | PredictionResult res = |
417 | 260k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
418 | 260k | tree_lookup, references); |
419 | 260k | uint64_t v = |
420 | 260k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
421 | 260k | p[x] = make_pixel(v, res.multiplier, res.guess); |
422 | 260k | } |
423 | 17.6M | for (size_t x = 2; x < channel.w - 2; x++) { |
424 | 17.5M | PredictionResult res = |
425 | 17.5M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, |
426 | 17.5M | tree_lookup, references); |
427 | 17.5M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
428 | 17.5M | res.context, br); |
429 | 17.5M | p[x] = make_pixel(v, res.multiplier, res.guess); |
430 | 17.5M | } |
431 | 390k | for (size_t x = channel.w - 2; x < channel.w; x++) { |
432 | 260k | PredictionResult res = |
433 | 260k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
434 | 260k | tree_lookup, references); |
435 | 260k | uint64_t v = |
436 | 260k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
437 | 260k | p[x] = make_pixel(v, res.multiplier, res.guess); |
438 | 260k | } |
439 | 130k | } else { |
440 | 515k | for (size_t x = 0; x < channel.w; x++) { |
441 | 504k | PredictionResult res = |
442 | 504k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
443 | 504k | tree_lookup, references); |
444 | 504k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
445 | 504k | res.context, br); |
446 | 504k | p[x] = make_pixel(v, res.multiplier, res.guess); |
447 | 504k | } |
448 | 10.2k | } |
449 | 140k | } |
450 | 3.46k | } else { |
451 | 1.86k | JXL_DEBUG_V(8, "Slowest track."); |
452 | 1.86k | MATreeLookup tree_lookup(tree); |
453 | 1.86k | Properties properties = Properties(num_props); |
454 | 1.86k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
455 | 1.86k | JXL_ASSIGN_OR_RETURN( |
456 | 1.86k | Channel references, |
457 | 1.86k | Channel::Create(memory_manager, |
458 | 1.86k | properties.size() - kNumNonrefProperties, channel.w)); |
459 | 1.86k | weighted::State wp_state(wp_header, channel.w, channel.h); |
460 | 120k | for (size_t y = 0; y < channel.h; y++) { |
461 | 118k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
462 | 118k | InitPropsRow(&properties, static_props, y); |
463 | 118k | PrecomputeReferences(channel, y, *image, chan, &references); |
464 | 118k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { |
465 | 341k | for (size_t x = 0; x < 2; x++) { |
466 | 227k | PredictionResult res = |
467 | 227k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
468 | 227k | tree_lookup, references, &wp_state); |
469 | 227k | uint64_t v = |
470 | 227k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
471 | 227k | p[x] = make_pixel(v, res.multiplier, res.guess); |
472 | 227k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
473 | 227k | } |
474 | 9.59M | for (size_t x = 2; x < channel.w - 2; x++) { |
475 | 9.47M | PredictionResult res = |
476 | 9.47M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, |
477 | 9.47M | tree_lookup, references, &wp_state); |
478 | 9.47M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
479 | 9.47M | res.context, br); |
480 | 9.47M | p[x] = make_pixel(v, res.multiplier, res.guess); |
481 | 9.47M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
482 | 9.47M | } |
483 | 340k | for (size_t x = channel.w - 2; x < channel.w; x++) { |
484 | 226k | PredictionResult res = |
485 | 226k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
486 | 226k | tree_lookup, references, &wp_state); |
487 | 226k | uint64_t v = |
488 | 226k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
489 | 226k | p[x] = make_pixel(v, res.multiplier, res.guess); |
490 | 226k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
491 | 226k | } |
492 | 114k | } else { |
493 | 1.16M | for (size_t x = 0; x < channel.w; x++) { |
494 | 1.15M | PredictionResult res = |
495 | 1.15M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
496 | 1.15M | tree_lookup, references, &wp_state); |
497 | 1.15M | uint64_t v = |
498 | 1.15M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
499 | 1.15M | p[x] = make_pixel(v, res.multiplier, res.guess); |
500 | 1.15M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
501 | 1.15M | } |
502 | 4.69k | } |
503 | 118k | } |
504 | 1.86k | } |
505 | 5.64k | return true; |
506 | 5.64k | } 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 | 9.11k | uint32_t &fl_v) { | 157 | 9.11k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 9.11k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 9.11k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 9.11k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 9.12k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 9.11k | bool tree_has_wp_prop_or_pred = false; | 168 | 9.11k | bool is_wp_only = false; | 169 | 9.11k | bool is_gradient_only = false; | 170 | 9.11k | size_t num_props; | 171 | 9.11k | FlatTree tree = | 172 | 9.11k | FilterTree(global_tree, static_props, &num_props, | 173 | 9.11k | &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 | 9.81k | for (auto &node : tree) { | 178 | 9.81k | if (node.property0 == -1) { | 179 | 9.66k | node.childID = context_map[node.childID]; | 180 | 9.66k | } | 181 | 9.81k | } | 182 | | | 183 | 9.11k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 9.11k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 9.11k | pixel_type_w offset) -> pixel_type { | 188 | 9.11k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 9.11k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 9.11k | return val * multiplier + offset; | 192 | 9.11k | }; | 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 | 9.11k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 9.11k | for (const auto& n : global_tree) { | 201 | 9.11k | if (n.property == -1) { | 202 | 9.11k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 9.11k | n.multiplier != 1) | 204 | 9.11k | return false; | 205 | 9.11k | } else if (n.property >= kNumStaticProperties) { | 206 | 9.11k | return false; | 207 | 9.11k | } | 208 | 9.11k | } | 209 | 9.11k | return true; | 210 | 9.11k | }(); | 211 | | | 212 | 9.19k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 9.19k | Predictor predictor = tree[0].predictor; | 216 | 9.19k | int64_t offset = tree[0].predictor_offset; | 217 | 9.19k | int32_t multiplier = tree[0].multiplier; | 218 | 9.19k | size_t ctx_id = tree[0].childID; | 219 | 9.19k | if (predictor == Predictor::Zero) { | 220 | 9.03k | uint32_t value; | 221 | 9.03k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 9.03k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 12 | JXL_DEBUG_V(8, "Fastest track."); | 226 | 12 | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 912 | for (size_t y = 0; y < channel.h; y++) { | 228 | 900 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 900 | std::fill(r, r + channel.w, v); | 230 | 900 | } | 231 | 9.02k | } else { | 232 | 9.02k | JXL_DEBUG_V(8, "Fast track."); | 233 | 9.02k | if (multiplier == 1 && offset == 0) { | 234 | 701k | for (size_t y = 0; y < channel.h; y++) { | 235 | 692k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 41.1M | for (size_t x = 0; x < channel.w; x++) { | 237 | 40.4M | uint32_t v = | 238 | 40.4M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 40.4M | r[x] = UnpackSigned(v); | 240 | 40.4M | } | 241 | 692k | } | 242 | 8.96k | } else { | 243 | 6.17k | for (size_t y = 0; y < channel.h; y++) { | 244 | 6.12k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 1.46M | for (size_t x = 0; x < channel.w; x++) { | 246 | 1.46M | uint32_t v = | 247 | 1.46M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 1.46M | br); | 249 | 1.46M | r[x] = make_pixel(v, multiplier, offset); | 250 | 1.46M | } | 251 | 6.12k | } | 252 | 53 | } | 253 | 9.02k | } | 254 | 9.03k | return true; | 255 | 9.03k | } 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 | 163 | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 0 | multiplier == 1) { | 291 | 0 | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 0 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 0 | for (size_t y = 0; y < channel.h; y++) { | 294 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 0 | for (size_t x = 0; x < channel.w; x++) { | 296 | 0 | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 0 | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 0 | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 0 | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 0 | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 0 | ctx_id, br); | 302 | 0 | r[x] = make_pixel(v, 1, guess); | 303 | 0 | } | 304 | 0 | } | 305 | 0 | return true; | 306 | 0 | } | 307 | 9.19k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 82 | if (is_wp_only) { | 312 | 0 | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 0 | } | 314 | 82 | if (is_gradient_only) { | 315 | 0 | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 0 | } | 317 | | | 318 | 82 | 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 | 82 | } 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 | 162 | } 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 | 162 | JXL_DEBUG_V(8, "Slow track."); | 403 | 162 | MATreeLookup tree_lookup(tree); | 404 | 162 | Properties properties = Properties(num_props); | 405 | 162 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 162 | JXL_ASSIGN_OR_RETURN( | 407 | 162 | Channel references, | 408 | 162 | Channel::Create(memory_manager, | 409 | 162 | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 10.0k | for (size_t y = 0; y < channel.h; y++) { | 411 | 9.93k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 9.93k | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 9.93k | InitPropsRow(&properties, static_props, y); | 414 | 9.93k | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 28.5k | for (size_t x = 0; x < 2; x++) { | 416 | 19.0k | PredictionResult res = | 417 | 19.0k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 19.0k | tree_lookup, references); | 419 | 19.0k | uint64_t v = | 420 | 19.0k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 19.0k | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 19.0k | } | 423 | 2.33M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 2.32M | PredictionResult res = | 425 | 2.32M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 2.32M | tree_lookup, references); | 427 | 2.32M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 2.32M | res.context, br); | 429 | 2.32M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 2.32M | } | 431 | 28.5k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 19.0k | PredictionResult res = | 433 | 19.0k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 19.0k | tree_lookup, references); | 435 | 19.0k | uint64_t v = | 436 | 19.0k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 19.0k | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 19.0k | } | 439 | 9.51k | } else { | 440 | 26.3k | for (size_t x = 0; x < channel.w; x++) { | 441 | 25.9k | PredictionResult res = | 442 | 25.9k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 25.9k | tree_lookup, references); | 444 | 25.9k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 25.9k | res.context, br); | 446 | 25.9k | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 25.9k | } | 448 | 421 | } | 449 | 9.93k | } | 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 | 82 | return true; | 506 | 82 | } |
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 | 152k | uint32_t &fl_v) { | 157 | 152k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 152k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 152k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 152k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 152k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 152k | bool tree_has_wp_prop_or_pred = false; | 168 | 152k | bool is_wp_only = false; | 169 | 152k | bool is_gradient_only = false; | 170 | 152k | size_t num_props; | 171 | 152k | FlatTree tree = | 172 | 152k | FilterTree(global_tree, static_props, &num_props, | 173 | 152k | &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 | 431k | for (auto &node : tree) { | 178 | 431k | if (node.property0 == -1) { | 179 | 361k | node.childID = context_map[node.childID]; | 180 | 361k | } | 181 | 431k | } | 182 | | | 183 | 152k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 152k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 152k | pixel_type_w offset) -> pixel_type { | 188 | 152k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 152k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 152k | return val * multiplier + offset; | 192 | 152k | }; | 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 | 152k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 152k | for (const auto& n : global_tree) { | 201 | 152k | if (n.property == -1) { | 202 | 152k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 152k | n.multiplier != 1) | 204 | 152k | return false; | 205 | 152k | } else if (n.property >= kNumStaticProperties) { | 206 | 152k | return false; | 207 | 152k | } | 208 | 152k | } | 209 | 152k | return true; | 210 | 152k | }(); | 211 | | | 212 | 152k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 147k | Predictor predictor = tree[0].predictor; | 216 | 147k | int64_t offset = tree[0].predictor_offset; | 217 | 147k | int32_t multiplier = tree[0].multiplier; | 218 | 147k | size_t ctx_id = tree[0].childID; | 219 | 147k | if (predictor == Predictor::Zero) { | 220 | 146k | uint32_t value; | 221 | 146k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 146k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 10.6k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 10.6k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 687k | for (size_t y = 0; y < channel.h; y++) { | 228 | 676k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 676k | std::fill(r, r + channel.w, v); | 230 | 676k | } | 231 | 136k | } else { | 232 | 136k | JXL_DEBUG_V(8, "Fast track."); | 233 | 136k | if (multiplier == 1 && offset == 0) { | 234 | 2.09M | for (size_t y = 0; y < channel.h; y++) { | 235 | 1.95M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 79.2M | for (size_t x = 0; x < channel.w; x++) { | 237 | 77.3M | uint32_t v = | 238 | 77.3M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 77.3M | r[x] = UnpackSigned(v); | 240 | 77.3M | } | 241 | 1.95M | } | 242 | 136k | } else { | 243 | 2.24k | for (size_t y = 0; y < channel.h; y++) { | 244 | 2.18k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 546k | for (size_t x = 0; x < channel.w; x++) { | 246 | 544k | uint32_t v = | 247 | 544k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 544k | br); | 249 | 544k | r[x] = make_pixel(v, multiplier, offset); | 250 | 544k | } | 251 | 2.18k | } | 252 | 61 | } | 253 | 136k | } | 254 | 146k | return true; | 255 | 146k | } 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 | 716 | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 318 | multiplier == 1) { | 291 | 318 | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 318 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 3.18k | for (size_t y = 0; y < channel.h; y++) { | 294 | 2.86k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 37.9k | for (size_t x = 0; x < channel.w; x++) { | 296 | 35.0k | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 35.0k | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 35.0k | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 35.0k | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 35.0k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 35.0k | ctx_id, br); | 302 | 35.0k | r[x] = make_pixel(v, 1, guess); | 303 | 35.0k | } | 304 | 2.86k | } | 305 | 318 | return true; | 306 | 318 | } | 307 | 147k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 5.56k | if (is_wp_only) { | 312 | 300 | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 300 | } | 314 | 5.56k | if (is_gradient_only) { | 315 | 87 | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 87 | } | 317 | | | 318 | 5.56k | if (is_gradient_only) { | 319 | 45 | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 45 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 869 | for (size_t y = 0; y < channel.h; y++) { | 322 | 824 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 19.0k | for (size_t x = 0; x < channel.w; x++) { | 324 | 18.2k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 18.2k | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 18.2k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 18.2k | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 18.2k | uint32_t pos = | 329 | 18.2k | kPropRangeFast + | 330 | 18.2k | std::min<pixel_type_w>( | 331 | 18.2k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 18.2k | kPropRangeFast - 1); | 333 | 18.2k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 18.2k | uint64_t v = | 335 | 18.2k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 18.2k | r[x] = make_pixel(v, 1, guess); | 337 | 18.2k | } | 338 | 824 | } | 339 | 5.59k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 340 | 275 | JXL_DEBUG_V(8, "WP fast track."); | 341 | 275 | weighted::State wp_state(wp_header, channel.w, channel.h); | 342 | 275 | Properties properties(1); | 343 | 2.45k | for (size_t y = 0; y < channel.h; y++) { | 344 | 2.17k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 345 | 2.17k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 346 | 2.17k | const pixel_type *JXL_RESTRICT rtoptop = | 347 | 2.17k | (y > 1 ? channel.Row(y - 2) : rtop); | 348 | 2.17k | const pixel_type *JXL_RESTRICT rtopleft = | 349 | 2.17k | (y ? channel.Row(y - 1) - 1 : r - 1); | 350 | 2.17k | const pixel_type *JXL_RESTRICT rtopright = | 351 | 2.17k | (y ? channel.Row(y - 1) + 1 : r - 1); | 352 | 2.17k | size_t x = 0; | 353 | 2.17k | { | 354 | 2.17k | size_t offset = 0; | 355 | 2.17k | pixel_type_w left = y ? rtop[x] : 0; | 356 | 2.17k | pixel_type_w toptop = y ? rtoptop[x] : 0; | 357 | 2.17k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 358 | 2.17k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 359 | 2.17k | x, y, channel.w, left, left, topright, left, toptop, &properties, | 360 | 2.17k | offset); | 361 | 2.17k | uint32_t pos = | 362 | 2.17k | kPropRangeFast + | 363 | 2.17k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 364 | 2.17k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 365 | 2.17k | uint64_t v = | 366 | 2.17k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 367 | 2.17k | r[x] = make_pixel(v, 1, guess); | 368 | 2.17k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 369 | 2.17k | } | 370 | 90.6k | for (x = 1; x + 1 < channel.w; x++) { | 371 | 88.4k | size_t offset = 0; | 372 | 88.4k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 373 | 88.4k | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 374 | 88.4k | rtoptop[x], &properties, offset); | 375 | 88.4k | uint32_t pos = | 376 | 88.4k | kPropRangeFast + | 377 | 88.4k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 378 | 88.4k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 379 | 88.4k | uint64_t v = | 380 | 88.4k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 381 | 88.4k | r[x] = make_pixel(v, 1, guess); | 382 | 88.4k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 383 | 88.4k | } | 384 | 2.17k | { | 385 | 2.17k | size_t offset = 0; | 386 | 2.17k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 387 | 2.17k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 388 | 2.17k | rtoptop[x], &properties, offset); | 389 | 2.17k | uint32_t pos = | 390 | 2.17k | kPropRangeFast + | 391 | 2.17k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 392 | 2.17k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 393 | 2.17k | uint64_t v = | 394 | 2.17k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 395 | 2.17k | r[x] = make_pixel(v, 1, guess); | 396 | 2.17k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 397 | 2.17k | } | 398 | 2.17k | } | 399 | 5.24k | } 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.29k | JXL_DEBUG_V(8, "Slow track."); | 403 | 3.29k | MATreeLookup tree_lookup(tree); | 404 | 3.29k | Properties properties = Properties(num_props); | 405 | 3.29k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 3.29k | JXL_ASSIGN_OR_RETURN( | 407 | 3.29k | Channel references, | 408 | 3.29k | Channel::Create(memory_manager, | 409 | 3.29k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 133k | for (size_t y = 0; y < channel.h; y++) { | 411 | 130k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 130k | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 130k | InitPropsRow(&properties, static_props, y); | 414 | 130k | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 362k | for (size_t x = 0; x < 2; x++) { | 416 | 241k | PredictionResult res = | 417 | 241k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 241k | tree_lookup, references); | 419 | 241k | uint64_t v = | 420 | 241k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 241k | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 241k | } | 423 | 15.3M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 15.2M | PredictionResult res = | 425 | 15.2M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 15.2M | tree_lookup, references); | 427 | 15.2M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 15.2M | res.context, br); | 429 | 15.2M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 15.2M | } | 431 | 362k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 241k | PredictionResult res = | 433 | 241k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 241k | tree_lookup, references); | 435 | 241k | uint64_t v = | 436 | 241k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 241k | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 241k | } | 439 | 120k | } else { | 440 | 488k | for (size_t x = 0; x < channel.w; x++) { | 441 | 479k | PredictionResult res = | 442 | 479k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 479k | tree_lookup, references); | 444 | 479k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 479k | res.context, br); | 446 | 479k | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 479k | } | 448 | 9.80k | } | 449 | 130k | } | 450 | 3.29k | } else { | 451 | 1.94k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 1.94k | MATreeLookup tree_lookup(tree); | 453 | 1.94k | Properties properties = Properties(num_props); | 454 | 1.94k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 1.94k | JXL_ASSIGN_OR_RETURN( | 456 | 1.94k | Channel references, | 457 | 1.94k | Channel::Create(memory_manager, | 458 | 1.94k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 1.94k | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 120k | for (size_t y = 0; y < channel.h; y++) { | 461 | 118k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 118k | InitPropsRow(&properties, static_props, y); | 463 | 118k | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 118k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 465 | 341k | for (size_t x = 0; x < 2; x++) { | 466 | 227k | PredictionResult res = | 467 | 227k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 227k | tree_lookup, references, &wp_state); | 469 | 227k | uint64_t v = | 470 | 227k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 227k | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 227k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 227k | } | 474 | 9.59M | for (size_t x = 2; x < channel.w - 2; x++) { | 475 | 9.47M | PredictionResult res = | 476 | 9.47M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 477 | 9.47M | tree_lookup, references, &wp_state); | 478 | 9.47M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 479 | 9.47M | res.context, br); | 480 | 9.47M | p[x] = make_pixel(v, res.multiplier, res.guess); | 481 | 9.47M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 482 | 9.47M | } | 483 | 340k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 484 | 226k | PredictionResult res = | 485 | 226k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 486 | 226k | tree_lookup, references, &wp_state); | 487 | 226k | uint64_t v = | 488 | 226k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 489 | 226k | p[x] = make_pixel(v, res.multiplier, res.guess); | 490 | 226k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 491 | 226k | } | 492 | 114k | } else { | 493 | 1.03M | for (size_t x = 0; x < channel.w; x++) { | 494 | 1.03M | PredictionResult res = | 495 | 1.03M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 1.03M | tree_lookup, references, &wp_state); | 497 | 1.03M | uint64_t v = | 498 | 1.03M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 1.03M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 1.03M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 1.03M | } | 502 | 4.21k | } | 503 | 118k | } | 504 | 1.94k | } | 505 | 5.56k | return true; | 506 | 5.56k | } |
|
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 | 161k | uint32_t &fl_v) { |
517 | 161k | if (reader->UsesLZ77()) { |
518 | 9.04k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>( |
519 | 9.04k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
520 | 9.04k | tree_lut, image, fl_run, fl_v); |
521 | 152k | } else { |
522 | 152k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>( |
523 | 152k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
524 | 152k | tree_lut, image, fl_run, fl_v); |
525 | 152k | } |
526 | 161k | } |
527 | | |
528 | 169k | GroupHeader::GroupHeader() { Bundle::Init(this); } |
529 | | |
530 | | Status ValidateChannelDimensions(const Image &image, |
531 | 25.6k | const ModularOptions &options) { |
532 | 25.6k | size_t nb_channels = image.channel.size(); |
533 | 51.3k | for (bool is_dc : {true, false}) { |
534 | 51.3k | size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1); |
535 | 51.3k | size_t c = image.nb_meta_channels; |
536 | 378k | for (; c < nb_channels; c++) { |
537 | 329k | const Channel &ch = image.channel[c]; |
538 | 329k | if (ch.w > options.group_dim || ch.h > options.group_dim) break; |
539 | 329k | } |
540 | 73.3k | for (; c < nb_channels; c++) { |
541 | 21.9k | const Channel &ch = image.channel[c]; |
542 | 21.9k | if (ch.w == 0 || ch.h == 0) continue; // skip empty |
543 | 21.9k | bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3; |
544 | 21.9k | if (is_dc_channel != is_dc) continue; |
545 | 10.9k | size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift); |
546 | 10.9k | if (tile_dim == 0) { |
547 | 0 | return JXL_FAILURE("Inconsistent transforms"); |
548 | 0 | } |
549 | 10.9k | } |
550 | 51.3k | } |
551 | 25.6k | return true; |
552 | 25.6k | } |
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 | 30.6k | const bool allow_truncated_group) { |
559 | 30.6k | if (image.channel.empty()) return true; |
560 | 25.7k | JxlMemoryManager *memory_manager = image.memory_manager(); |
561 | | |
562 | | // decode transforms |
563 | 25.7k | Status status = Bundle::Read(br, &header); |
564 | 25.7k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status); |
565 | 25.7k | if (status.IsFatalError()) return status; |
566 | 25.7k | 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 | 25.7k | JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ", |
577 | 25.7k | header.transforms.size()); |
578 | 25.7k | image.transform = header.transforms; |
579 | 25.7k | for (Transform &transform : image.transform) { |
580 | 13.3k | JXL_RETURN_IF_ERROR(transform.MetaApply(image)); |
581 | 13.3k | } |
582 | 25.7k | if (image.error) { |
583 | 0 | return JXL_FAILURE("Corrupt file. Aborting."); |
584 | 0 | } |
585 | 25.7k | JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options)); |
586 | | |
587 | 25.7k | size_t nb_channels = image.channel.size(); |
588 | | |
589 | 25.7k | size_t num_chans = 0; |
590 | 25.7k | size_t distance_multiplier = 0; |
591 | 192k | for (size_t i = 0; i < nb_channels; i++) { |
592 | 168k | Channel &channel = image.channel[i]; |
593 | 168k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
594 | 164k | channel.h > options->max_chan_size)) { |
595 | 1.16k | break; |
596 | 1.16k | } |
597 | 167k | if (!channel.w || !channel.h) { |
598 | 3.47k | continue; // skip empty channels |
599 | 3.47k | } |
600 | 163k | if (channel.w > distance_multiplier) { |
601 | 35.6k | distance_multiplier = channel.w; |
602 | 35.6k | } |
603 | 163k | num_chans++; |
604 | 163k | } |
605 | 25.7k | if (num_chans == 0) return true; |
606 | | |
607 | 25.6k | size_t next_channel = 0; |
608 | 25.6k | auto scope_guard = MakeScopeGuard([&]() { |
609 | 2.06k | for (size_t c = next_channel; c < image.channel.size(); c++) { |
610 | 1.99k | ZeroFillImage(&image.channel[c].plane); |
611 | 1.99k | } |
612 | 63 | }); |
613 | | // Do not do anything if truncated groups are not allowed. |
614 | 25.6k | if (allow_truncated_group) scope_guard.Disarm(); |
615 | | |
616 | | // Read tree. |
617 | 25.6k | Tree tree_storage; |
618 | 25.6k | std::vector<uint8_t> context_map_storage; |
619 | 25.6k | ANSCode code_storage; |
620 | 25.6k | const Tree *tree = &tree_storage; |
621 | 25.6k | const ANSCode *code = &code_storage; |
622 | 25.6k | const std::vector<uint8_t> *context_map = &context_map_storage; |
623 | 25.6k | if (!header.use_global_tree) { |
624 | 4.94k | uint64_t max_tree_size = 1024; |
625 | 28.3k | for (size_t i = 0; i < nb_channels; i++) { |
626 | 23.3k | Channel &channel = image.channel[i]; |
627 | 23.3k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
628 | 23.2k | channel.h > options->max_chan_size)) { |
629 | 0 | break; |
630 | 0 | } |
631 | 23.3k | uint64_t pixels = channel.w * channel.h; |
632 | 23.3k | max_tree_size += pixels; |
633 | 23.3k | } |
634 | 4.94k | max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size); |
635 | 4.94k | JXL_RETURN_IF_ERROR( |
636 | 4.94k | DecodeTree(memory_manager, br, &tree_storage, max_tree_size)); |
637 | 4.92k | JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, |
638 | 4.92k | (tree_storage.size() + 1) / 2, |
639 | 4.92k | &code_storage, &context_map_storage)); |
640 | 20.6k | } else { |
641 | 20.6k | if (!global_tree || !global_code || !global_ctx_map || |
642 | 20.6k | global_tree->empty()) { |
643 | 4 | return JXL_FAILURE("No global tree available but one was requested"); |
644 | 4 | } |
645 | 20.6k | tree = global_tree; |
646 | 20.6k | code = global_code; |
647 | 20.6k | context_map = global_ctx_map; |
648 | 20.6k | } |
649 | | |
650 | | // Read channels |
651 | 51.1k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader, |
652 | 51.1k | ANSSymbolReader::Create(code, br, distance_multiplier)); |
653 | 51.1k | auto tree_lut = jxl::make_unique<TreeLut<uint8_t, false, false>>(); |
654 | 51.1k | uint32_t fl_run = 0; |
655 | 51.1k | uint32_t fl_v = 0; |
656 | 190k | for (; next_channel < nb_channels; next_channel++) { |
657 | 166k | Channel &channel = image.channel[next_channel]; |
658 | 166k | if (next_channel >= image.nb_meta_channels && |
659 | 162k | (channel.w > options->max_chan_size || |
660 | 162k | channel.h > options->max_chan_size)) { |
661 | 1.04k | break; |
662 | 1.04k | } |
663 | 165k | if (!channel.w || !channel.h) { |
664 | 3.47k | continue; // skip empty channels |
665 | 3.47k | } |
666 | 161k | JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS( |
667 | 161k | br, &reader, *context_map, *tree, header.wp_header, next_channel, |
668 | 161k | group_id, *tree_lut, &image, fl_run, fl_v)); |
669 | | |
670 | | // Truncated group. |
671 | 161k | if (!br->AllReadsWithinBounds()) { |
672 | 40 | if (!allow_truncated_group) return JXL_FAILURE("Truncated input"); |
673 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun in ModularDecode"); |
674 | 40 | } |
675 | 161k | } |
676 | | |
677 | | // Make sure no zero-filling happens even if next_channel < nb_channels. |
678 | 25.5k | scope_guard.Disarm(); |
679 | | |
680 | 25.5k | if (!reader.CheckANSFinalState()) { |
681 | 0 | return JXL_FAILURE("ANS decode final state failed"); |
682 | 0 | } |
683 | 25.5k | return true; |
684 | 25.5k | } |
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 | 30.6k | bool allow_truncated_group) { |
692 | 30.6k | std::vector<std::pair<size_t, size_t>> req_sizes; |
693 | 30.6k | req_sizes.reserve(image.channel.size()); |
694 | 98.7k | for (const auto &c : image.channel) { |
695 | 98.7k | req_sizes.emplace_back(c.w, c.h); |
696 | 98.7k | } |
697 | 30.6k | GroupHeader local_header; |
698 | 30.6k | if (header == nullptr) header = &local_header; |
699 | 30.6k | size_t bit_pos = br->TotalBitsConsumed(); |
700 | 30.6k | auto dec_status = ModularDecode(br, image, *header, group_id, options, tree, |
701 | 30.6k | code, ctx_map, allow_truncated_group); |
702 | 30.7k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status); |
703 | 30.6k | if (dec_status.IsFatalError()) return dec_status; |
704 | 30.6k | if (undo_transforms) image.undo_transforms(header->wp_header); |
705 | 30.6k | if (image.error) return JXL_FAILURE("Corrupt file. Aborting."); |
706 | 30.6k | JXL_DEBUG_V(4, |
707 | 30.6k | "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS |
708 | 30.6k | " image from %" PRIuS " bytes", |
709 | 30.6k | image.w, image.h, image.channel.size(), |
710 | 30.6k | (br->TotalBitsConsumed() - bit_pos) / 8); |
711 | 30.6k | JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str()); |
712 | 30.6k | (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 | 30.6k | if (undo_transforms) { |
717 | 16.8k | JXL_ENSURE(image.channel.size() == req_sizes.size()); |
718 | 84.1k | for (size_t c = 0; c < req_sizes.size(); c++) { |
719 | 67.3k | JXL_ENSURE(req_sizes[c].first == image.channel[c].w); |
720 | 67.3k | JXL_ENSURE(req_sizes[c].second == image.channel[c].h); |
721 | 67.3k | } |
722 | 16.8k | } |
723 | 30.6k | return dec_status; |
724 | 30.6k | } |
725 | | |
726 | | } // namespace jxl |