/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 | 965k | bool *gradient_only) { |
46 | 965k | *num_props = 0; |
47 | 965k | bool has_wp = false; |
48 | 965k | bool has_non_wp = false; |
49 | 965k | *gradient_only = true; |
50 | 965k | const auto mark_property = [&](int32_t p) { |
51 | 963k | if (p == kWPProp) { |
52 | 157k | has_wp = true; |
53 | 805k | } else if (p >= kNumStaticProperties) { |
54 | 426k | has_non_wp = true; |
55 | 426k | } |
56 | 963k | if (p >= kNumStaticProperties && p != kGradientProp) { |
57 | 482k | *gradient_only = false; |
58 | 482k | } |
59 | 963k | }; |
60 | 965k | FlatTree output; |
61 | 965k | std::queue<size_t> nodes; |
62 | 965k | 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.21M | while (!nodes.empty()) { |
70 | 2.24M | size_t cur = nodes.front(); |
71 | 2.24M | nodes.pop(); |
72 | | // Skip nodes that we can decide now, by jumping directly to their children. |
73 | 2.41M | while (global_tree[cur].property < kNumStaticProperties && |
74 | 2.09M | global_tree[cur].property != -1) { |
75 | 164k | if (static_props[global_tree[cur].property] > global_tree[cur].splitval) { |
76 | 90.4k | cur = global_tree[cur].lchild; |
77 | 90.4k | } else { |
78 | 74.2k | cur = global_tree[cur].rchild; |
79 | 74.2k | } |
80 | 164k | } |
81 | 2.24M | FlatDecisionNode flat; |
82 | 2.24M | if (global_tree[cur].property == -1) { |
83 | 1.92M | flat.property0 = -1; |
84 | 1.92M | flat.childID = global_tree[cur].lchild; |
85 | 1.92M | flat.predictor = global_tree[cur].predictor; |
86 | 1.92M | flat.predictor_offset = global_tree[cur].predictor_offset; |
87 | 1.92M | flat.multiplier = global_tree[cur].multiplier; |
88 | 1.92M | *gradient_only &= flat.predictor == Predictor::Gradient; |
89 | 1.92M | has_wp |= flat.predictor == Predictor::Weighted; |
90 | 1.92M | has_non_wp |= flat.predictor != Predictor::Weighted; |
91 | 1.92M | output.push_back(flat); |
92 | 1.92M | continue; |
93 | 1.92M | } |
94 | 321k | flat.childID = output.size() + nodes.size() + 1; |
95 | | |
96 | 321k | flat.property0 = global_tree[cur].property; |
97 | 321k | *num_props = std::max<size_t>(flat.property0 + 1, *num_props); |
98 | 321k | flat.splitval0 = global_tree[cur].splitval; |
99 | | |
100 | 963k | for (size_t i = 0; i < 2; i++) { |
101 | 642k | size_t cur_child = |
102 | 642k | i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild; |
103 | | // Skip nodes that we can decide now. |
104 | 687k | while (global_tree[cur_child].property < kNumStaticProperties && |
105 | 424k | global_tree[cur_child].property != -1) { |
106 | 45.3k | if (static_props[global_tree[cur_child].property] > |
107 | 45.3k | global_tree[cur_child].splitval) { |
108 | 23.8k | cur_child = global_tree[cur_child].lchild; |
109 | 23.8k | } else { |
110 | 21.5k | cur_child = global_tree[cur_child].rchild; |
111 | 21.5k | } |
112 | 45.3k | } |
113 | | // We ended up in a leaf, add a placeholder decision and two copies of the |
114 | | // leaf. |
115 | 642k | if (global_tree[cur_child].property == -1) { |
116 | 379k | flat.properties[i] = 0; |
117 | 379k | flat.splitvals[i] = 0; |
118 | 379k | nodes.push(cur_child); |
119 | 379k | nodes.push(cur_child); |
120 | 379k | } else { |
121 | 262k | flat.properties[i] = global_tree[cur_child].property; |
122 | 262k | flat.splitvals[i] = global_tree[cur_child].splitval; |
123 | 262k | nodes.push(global_tree[cur_child].lchild); |
124 | 262k | nodes.push(global_tree[cur_child].rchild); |
125 | 262k | *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props); |
126 | 262k | } |
127 | 642k | } |
128 | | |
129 | 642k | for (int16_t property : flat.properties) mark_property(property); |
130 | 321k | mark_property(flat.property0); |
131 | 321k | output.push_back(flat); |
132 | 321k | } |
133 | 965k | if (*num_props > kNumNonrefProperties) { |
134 | 3.97k | *num_props = |
135 | 3.97k | DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) * |
136 | 3.97k | kExtraPropsPerChannel + |
137 | 3.97k | kNumNonrefProperties; |
138 | 961k | } else { |
139 | 961k | *num_props = kNumNonrefProperties; |
140 | 961k | } |
141 | 965k | *use_wp = has_wp; |
142 | 965k | *wp_only = has_wp && !has_non_wp; |
143 | | |
144 | 965k | return output; |
145 | 965k | } |
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 | 922k | uint32_t &fl_v) { |
157 | 922k | JxlMemoryManager *memory_manager = image->memory_manager(); |
158 | 922k | Channel &channel = image->channel[chan]; |
159 | | |
160 | 922k | std::array<pixel_type, kNumStaticProperties> static_props = { |
161 | 922k | {chan, static_cast<int>(group_id)}}; |
162 | | // TODO(veluca): filter the tree according to static_props. |
163 | | |
164 | | // zero pixel channel? could happen |
165 | 922k | if (channel.w == 0 || channel.h == 0) return true; |
166 | | |
167 | 922k | bool tree_has_wp_prop_or_pred = false; |
168 | 922k | bool is_wp_only = false; |
169 | 922k | bool is_gradient_only = false; |
170 | 922k | size_t num_props; |
171 | 922k | FlatTree tree = |
172 | 922k | FilterTree(global_tree, static_props, &num_props, |
173 | 922k | &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.74M | for (auto &node : tree) { |
178 | 1.74M | if (node.property0 == -1) { |
179 | 1.53M | node.childID = context_map[node.childID]; |
180 | 1.53M | } |
181 | 1.74M | } |
182 | | |
183 | 922k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); |
184 | | |
185 | | // MAANS decode |
186 | 922k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, |
187 | 634M | pixel_type_w offset) -> pixel_type { |
188 | 634M | JXL_DASSERT((v & 0xFFFFFFFF) == v); |
189 | 634M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); |
190 | | // if it overflows, it overflows, and we have a problem anyway |
191 | 634M | return val * multiplier + offset; |
192 | 634M | }; 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 | 240M | pixel_type_w offset) -> pixel_type { | 188 | 240M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 240M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 240M | return val * multiplier + offset; | 192 | 240M | }; |
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 | 393M | pixel_type_w offset) -> pixel_type { | 188 | 393M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 393M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 393M | return val * multiplier + offset; | 192 | 393M | }; |
|
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 | 922k | const bool global_tree_is_all_gradient_noop = [&] { |
200 | 986k | for (const auto& n : global_tree) { |
201 | 986k | if (n.property == -1) { |
202 | 870k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || |
203 | 8.41k | n.multiplier != 1) |
204 | 862k | return false; |
205 | 870k | } else if (n.property >= kNumStaticProperties) { |
206 | 56.4k | return false; |
207 | 56.4k | } |
208 | 986k | } |
209 | 3.67k | return true; |
210 | 922k | }(); 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 | 134k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 137k | for (const auto& n : global_tree) { | 201 | 137k | if (n.property == -1) { | 202 | 122k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 2.17k | n.multiplier != 1) | 204 | 120k | return false; | 205 | 122k | } else if (n.property >= kNumStaticProperties) { | 206 | 11.7k | return false; | 207 | 11.7k | } | 208 | 137k | } | 209 | 1.84k | return true; | 210 | 134k | }(); |
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 | 788k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 848k | for (const auto& n : global_tree) { | 201 | 848k | if (n.property == -1) { | 202 | 747k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 6.24k | n.multiplier != 1) | 204 | 741k | return false; | 205 | 747k | } else if (n.property >= kNumStaticProperties) { | 206 | 44.6k | return false; | 207 | 44.6k | } | 208 | 848k | } | 209 | 1.83k | return true; | 210 | 788k | }(); |
|
211 | | |
212 | 922k | if (tree.size() == 1) { |
213 | | // special optimized case: no meta-adaptation, so no need |
214 | | // to compute properties. |
215 | 871k | Predictor predictor = tree[0].predictor; |
216 | 871k | int64_t offset = tree[0].predictor_offset; |
217 | 871k | int32_t multiplier = tree[0].multiplier; |
218 | 871k | size_t ctx_id = tree[0].childID; |
219 | 871k | if (predictor == Predictor::Zero) { |
220 | 684k | uint32_t value; |
221 | 684k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, |
222 | 684k | channel.w * channel.h)) { |
223 | | // Special-case: histogram has a single symbol, with no extra bits, and |
224 | | // we use ANS mode. |
225 | 107k | JXL_DEBUG_V(8, "Fastest track."); |
226 | 107k | pixel_type v = make_pixel(value, multiplier, offset); |
227 | 2.22M | for (size_t y = 0; y < channel.h; y++) { |
228 | 2.11M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
229 | 2.11M | std::fill(r, r + channel.w, v); |
230 | 2.11M | } |
231 | 576k | } else { |
232 | 576k | JXL_DEBUG_V(8, "Fast track."); |
233 | 576k | if (multiplier == 1 && offset == 0) { |
234 | 6.93M | for (size_t y = 0; y < channel.h; y++) { |
235 | 6.42M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
236 | 297M | for (size_t x = 0; x < channel.w; x++) { |
237 | 291M | uint32_t v = |
238 | 291M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
239 | 291M | r[x] = UnpackSigned(v); |
240 | 291M | } |
241 | 6.42M | } |
242 | 516k | } else { |
243 | 667k | for (size_t y = 0; y < channel.h; y++) { |
244 | 607k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
245 | 28.9M | for (size_t x = 0; x < channel.w; x++) { |
246 | 28.3M | uint32_t v = |
247 | 28.3M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, |
248 | 28.3M | br); |
249 | 28.3M | r[x] = make_pixel(v, multiplier, offset); |
250 | 28.3M | } |
251 | 607k | } |
252 | 59.7k | } |
253 | 576k | } |
254 | 684k | return true; |
255 | 684k | } 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 | 186k | } else if (predictor == Predictor::Gradient && offset == 0 && |
290 | 9.93k | multiplier == 1) { |
291 | 9.14k | JXL_DEBUG_V(8, "Gradient very fast track."); |
292 | 9.14k | 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.63M | for (size_t x = 0; x < channel.w; x++) { |
296 | 4.44M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
297 | 4.44M | pixel_type top = (y ? *(r + x - onerow) : left); |
298 | 4.44M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); |
299 | 4.44M | pixel_type guess = ClampedGradient(top, left, topleft); |
300 | 4.44M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
301 | 4.44M | ctx_id, br); |
302 | 4.44M | r[x] = make_pixel(v, 1, guess); |
303 | 4.44M | } |
304 | 189k | } |
305 | 9.14k | return true; |
306 | 9.14k | } |
307 | 871k | } |
308 | | |
309 | | // Check if this tree is a WP-only tree with a small enough property value |
310 | | // range. |
311 | 228k | if (is_wp_only) { |
312 | 17.4k | is_wp_only = TreeToLookupTable(tree, tree_lut); |
313 | 17.4k | } |
314 | 228k | if (is_gradient_only) { |
315 | 7.38k | is_gradient_only = TreeToLookupTable(tree, tree_lut); |
316 | 7.38k | } |
317 | | |
318 | 228k | if (is_gradient_only) { |
319 | 4.58k | JXL_DEBUG_V(8, "Gradient fast track."); |
320 | 4.58k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
321 | 182k | for (size_t y = 0; y < channel.h; y++) { |
322 | 177k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
323 | 4.52M | for (size_t x = 0; x < channel.w; x++) { |
324 | 4.34M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
325 | 4.34M | pixel_type_w top = (y ? *(r + x - onerow) : left); |
326 | 4.34M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); |
327 | 4.34M | int32_t guess = ClampedGradient(top, left, topleft); |
328 | 4.34M | uint32_t pos = |
329 | 4.34M | kPropRangeFast + |
330 | 4.34M | std::min<pixel_type_w>( |
331 | 4.34M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), |
332 | 4.34M | kPropRangeFast - 1); |
333 | 4.34M | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
334 | 4.34M | uint64_t v = |
335 | 4.34M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); |
336 | 4.34M | r[x] = make_pixel(v, 1, guess); |
337 | 4.34M | } |
338 | 177k | } |
339 | 224k | } 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 | 334k | for (size_t y = 0; y < channel.h; y++) { |
344 | 326k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
345 | 326k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
346 | 326k | const pixel_type *JXL_RESTRICT rtoptop = |
347 | 326k | (y > 1 ? channel.Row(y - 2) : rtop); |
348 | 326k | const pixel_type *JXL_RESTRICT rtopleft = |
349 | 326k | (y ? channel.Row(y - 1) - 1 : r - 1); |
350 | 326k | const pixel_type *JXL_RESTRICT rtopright = |
351 | 326k | (y ? channel.Row(y - 1) + 1 : r - 1); |
352 | 326k | size_t x = 0; |
353 | 326k | { |
354 | 326k | size_t offset = 0; |
355 | 326k | pixel_type_w left = y ? rtop[x] : 0; |
356 | 326k | pixel_type_w toptop = y ? rtoptop[x] : 0; |
357 | 326k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); |
358 | 326k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
359 | 326k | x, y, channel.w, left, left, topright, left, toptop, &properties, |
360 | 326k | offset); |
361 | 326k | uint32_t pos = |
362 | 326k | kPropRangeFast + |
363 | 326k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
364 | 326k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
365 | 326k | uint64_t v = |
366 | 326k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
367 | 326k | r[x] = make_pixel(v, 1, guess); |
368 | 326k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
369 | 326k | } |
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 | 326k | { |
385 | 326k | size_t offset = 0; |
386 | 326k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
387 | 326k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], |
388 | 326k | rtoptop[x], &properties, offset); |
389 | 326k | uint32_t pos = |
390 | 326k | kPropRangeFast + |
391 | 326k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
392 | 326k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
393 | 326k | uint64_t v = |
394 | 326k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
395 | 326k | r[x] = make_pixel(v, 1, guess); |
396 | 326k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
397 | 326k | } |
398 | 326k | } |
399 | 215k | } 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 | 182k | JXL_DEBUG_V(8, "Slow track."); |
403 | 182k | MATreeLookup tree_lookup(tree); |
404 | 182k | Properties properties = Properties(num_props); |
405 | 182k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
406 | 182k | JXL_ASSIGN_OR_RETURN( |
407 | 182k | Channel references, |
408 | 182k | Channel::Create(memory_manager, |
409 | 182k | properties.size() - kNumNonrefProperties, channel.w)); |
410 | 9.09M | for (size_t y = 0; y < channel.h; y++) { |
411 | 8.91M | pixel_type *JXL_RESTRICT p = channel.Row(y); |
412 | 8.91M | PrecomputeReferences(channel, y, *image, chan, &references); |
413 | 8.91M | InitPropsRow(&properties, static_props, y); |
414 | 8.91M | if (y > 1 && channel.w > 8 && references.w == 0) { |
415 | 21.3M | for (size_t x = 0; x < 2; x++) { |
416 | 14.2M | PredictionResult res = |
417 | 14.2M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
418 | 14.2M | tree_lookup, references); |
419 | 14.2M | uint64_t v = |
420 | 14.2M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
421 | 14.2M | p[x] = make_pixel(v, res.multiplier, res.guess); |
422 | 14.2M | } |
423 | 418M | for (size_t x = 2; x < channel.w - 2; x++) { |
424 | 410M | PredictionResult res = |
425 | 410M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, |
426 | 410M | tree_lookup, references); |
427 | 410M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
428 | 410M | res.context, br); |
429 | 410M | p[x] = make_pixel(v, res.multiplier, res.guess); |
430 | 410M | } |
431 | 21.3M | for (size_t x = channel.w - 2; x < channel.w; x++) { |
432 | 14.2M | PredictionResult res = |
433 | 14.2M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
434 | 14.2M | tree_lookup, references); |
435 | 14.2M | uint64_t v = |
436 | 14.2M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
437 | 14.2M | p[x] = make_pixel(v, res.multiplier, res.guess); |
438 | 14.2M | } |
439 | 7.11M | } else { |
440 | 25.2M | for (size_t x = 0; x < channel.w; x++) { |
441 | 23.4M | PredictionResult res = |
442 | 23.4M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
443 | 23.4M | tree_lookup, references); |
444 | 23.4M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
445 | 23.4M | res.context, br); |
446 | 23.4M | p[x] = make_pixel(v, res.multiplier, res.guess); |
447 | 23.4M | } |
448 | 1.80M | } |
449 | 8.91M | } |
450 | 182k | } else { |
451 | 33.0k | JXL_DEBUG_V(8, "Slowest track."); |
452 | 33.0k | MATreeLookup tree_lookup(tree); |
453 | 33.0k | Properties properties = Properties(num_props); |
454 | 33.0k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
455 | 33.0k | JXL_ASSIGN_OR_RETURN( |
456 | 33.0k | Channel references, |
457 | 33.0k | Channel::Create(memory_manager, |
458 | 33.0k | properties.size() - kNumNonrefProperties, channel.w)); |
459 | 33.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.52M | pixel_type *JXL_RESTRICT p = channel.Row(y); |
462 | 1.52M | InitPropsRow(&properties, static_props, y); |
463 | 1.52M | PrecomputeReferences(channel, y, *image, chan, &references); |
464 | 1.52M | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { |
465 | 1.56M | for (size_t x = 0; x < 2; x++) { |
466 | 1.04M | PredictionResult res = |
467 | 1.04M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
468 | 1.04M | tree_lookup, references, &wp_state); |
469 | 1.04M | uint64_t v = |
470 | 1.04M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
471 | 1.04M | p[x] = make_pixel(v, res.multiplier, res.guess); |
472 | 1.04M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
473 | 1.04M | } |
474 | 109M | for (size_t x = 2; x < channel.w - 2; x++) { |
475 | 109M | PredictionResult res = |
476 | 109M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, |
477 | 109M | tree_lookup, references, &wp_state); |
478 | 109M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
479 | 109M | res.context, br); |
480 | 109M | p[x] = make_pixel(v, res.multiplier, res.guess); |
481 | 109M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
482 | 109M | } |
483 | 1.56M | for (size_t x = channel.w - 2; x < channel.w; x++) { |
484 | 1.04M | PredictionResult res = |
485 | 1.04M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
486 | 1.04M | tree_lookup, references, &wp_state); |
487 | 1.04M | uint64_t v = |
488 | 1.04M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
489 | 1.04M | p[x] = make_pixel(v, res.multiplier, res.guess); |
490 | 1.04M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
491 | 1.04M | } |
492 | 1.00M | } else { |
493 | 10.7M | for (size_t x = 0; x < channel.w; x++) { |
494 | 9.75M | PredictionResult res = |
495 | 9.75M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
496 | 9.75M | tree_lookup, references, &wp_state); |
497 | 9.75M | uint64_t v = |
498 | 9.75M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
499 | 9.75M | p[x] = make_pixel(v, res.multiplier, res.guess); |
500 | 9.75M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
501 | 9.75M | } |
502 | 1.00M | } |
503 | 1.52M | } |
504 | 33.0k | } |
505 | 228k | return true; |
506 | 228k | } 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 | 134k | uint32_t &fl_v) { | 157 | 134k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 134k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 134k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 134k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 134k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 134k | bool tree_has_wp_prop_or_pred = false; | 168 | 134k | bool is_wp_only = false; | 169 | 134k | bool is_gradient_only = false; | 170 | 134k | size_t num_props; | 171 | 134k | FlatTree tree = | 172 | 134k | FilterTree(global_tree, static_props, &num_props, | 173 | 134k | &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 | 184k | for (auto &node : tree) { | 178 | 184k | if (node.property0 == -1) { | 179 | 171k | node.childID = context_map[node.childID]; | 180 | 171k | } | 181 | 184k | } | 182 | | | 183 | 134k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 134k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 134k | pixel_type_w offset) -> pixel_type { | 188 | 134k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 134k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 134k | return val * multiplier + offset; | 192 | 134k | }; | 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 | 134k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 134k | for (const auto& n : global_tree) { | 201 | 134k | if (n.property == -1) { | 202 | 134k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 134k | n.multiplier != 1) | 204 | 134k | return false; | 205 | 134k | } else if (n.property >= kNumStaticProperties) { | 206 | 134k | return false; | 207 | 134k | } | 208 | 134k | } | 209 | 134k | return true; | 210 | 134k | }(); | 211 | | | 212 | 134k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 122k | Predictor predictor = tree[0].predictor; | 216 | 122k | int64_t offset = tree[0].predictor_offset; | 217 | 122k | int32_t multiplier = tree[0].multiplier; | 218 | 122k | size_t ctx_id = tree[0].childID; | 219 | 122k | if (predictor == Predictor::Zero) { | 220 | 91.8k | uint32_t value; | 221 | 91.8k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 91.8k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 21.2k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 21.2k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 657k | for (size_t y = 0; y < channel.h; y++) { | 228 | 636k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 636k | std::fill(r, r + channel.w, v); | 230 | 636k | } | 231 | 70.5k | } else { | 232 | 70.5k | JXL_DEBUG_V(8, "Fast track."); | 233 | 70.5k | if (multiplier == 1 && offset == 0) { | 234 | 1.00M | for (size_t y = 0; y < channel.h; y++) { | 235 | 945k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 30.7M | for (size_t x = 0; x < channel.w; x++) { | 237 | 29.8M | uint32_t v = | 238 | 29.8M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 29.8M | r[x] = UnpackSigned(v); | 240 | 29.8M | } | 241 | 945k | } | 242 | 55.0k | } else { | 243 | 205k | for (size_t y = 0; y < channel.h; y++) { | 244 | 190k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 8.57M | for (size_t x = 0; x < channel.w; x++) { | 246 | 8.38M | uint32_t v = | 247 | 8.38M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 8.38M | br); | 249 | 8.38M | r[x] = make_pixel(v, multiplier, offset); | 250 | 8.38M | } | 251 | 190k | } | 252 | 15.5k | } | 253 | 70.5k | } | 254 | 91.8k | return true; | 255 | 91.8k | } 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 | 30.4k | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 1.81k | multiplier == 1) { | 291 | 1.50k | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 1.50k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 293 | 27.1k | for (size_t y = 0; y < channel.h; y++) { | 294 | 25.6k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 295 | 1.93M | for (size_t x = 0; x < channel.w; x++) { | 296 | 1.90M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 1.90M | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 1.90M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 1.90M | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 1.90M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 1.90M | ctx_id, br); | 302 | 1.90M | r[x] = make_pixel(v, 1, guess); | 303 | 1.90M | } | 304 | 25.6k | } | 305 | 1.50k | return true; | 306 | 1.50k | } | 307 | 122k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 40.6k | if (is_wp_only) { | 312 | 1.67k | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 1.67k | } | 314 | 40.6k | if (is_gradient_only) { | 315 | 1.51k | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 1.51k | } | 317 | | | 318 | 40.6k | if (is_gradient_only) { | 319 | 153 | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 153 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 11.2k | for (size_t y = 0; y < channel.h; y++) { | 322 | 11.1k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 1.71M | for (size_t x = 0; x < channel.w; x++) { | 324 | 1.70M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 1.70M | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 1.70M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 1.70M | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 1.70M | uint32_t pos = | 329 | 1.70M | kPropRangeFast + | 330 | 1.70M | std::min<pixel_type_w>( | 331 | 1.70M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 1.70M | kPropRangeFast - 1); | 333 | 1.70M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 1.70M | uint64_t v = | 335 | 1.70M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 1.70M | r[x] = make_pixel(v, 1, guess); | 337 | 1.70M | } | 338 | 11.1k | } | 339 | 40.4k | } 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 | 40.4k | } 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 | 29.6k | JXL_DEBUG_V(8, "Slow track."); | 403 | 29.6k | MATreeLookup tree_lookup(tree); | 404 | 29.6k | Properties properties = Properties(num_props); | 405 | 29.6k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 29.6k | JXL_ASSIGN_OR_RETURN( | 407 | 29.6k | Channel references, | 408 | 29.6k | Channel::Create(memory_manager, | 409 | 29.6k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 4.19M | for (size_t y = 0; y < channel.h; y++) { | 411 | 4.16M | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 4.16M | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 4.16M | InitPropsRow(&properties, static_props, y); | 414 | 4.16M | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 9.20M | for (size_t x = 0; x < 2; x++) { | 416 | 6.13M | PredictionResult res = | 417 | 6.13M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 6.13M | tree_lookup, references); | 419 | 6.13M | uint64_t v = | 420 | 6.13M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 6.13M | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 6.13M | } | 423 | 207M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 204M | PredictionResult res = | 425 | 204M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 204M | tree_lookup, references); | 427 | 204M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 204M | res.context, br); | 429 | 204M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 204M | } | 431 | 9.20M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 6.13M | PredictionResult res = | 433 | 6.13M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 6.13M | tree_lookup, references); | 435 | 6.13M | uint64_t v = | 436 | 6.13M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 6.13M | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 6.13M | } | 439 | 3.06M | } else { | 440 | 10.8M | for (size_t x = 0; x < channel.w; x++) { | 441 | 9.74M | PredictionResult res = | 442 | 9.74M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 9.74M | tree_lookup, references); | 444 | 9.74M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 9.74M | res.context, br); | 446 | 9.74M | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 9.74M | } | 448 | 1.09M | } | 449 | 4.16M | } | 450 | 29.6k | } else { | 451 | 10.8k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 10.8k | MATreeLookup tree_lookup(tree); | 453 | 10.8k | Properties properties = Properties(num_props); | 454 | 10.8k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 10.8k | JXL_ASSIGN_OR_RETURN( | 456 | 10.8k | Channel references, | 457 | 10.8k | Channel::Create(memory_manager, | 458 | 10.8k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 10.8k | weighted::State wp_state(wp_header, channel.w, channel.h); | 460 | 66.5k | for (size_t y = 0; y < channel.h; y++) { | 461 | 55.7k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 462 | 55.7k | InitPropsRow(&properties, static_props, y); | 463 | 55.7k | PrecomputeReferences(channel, y, *image, chan, &references); | 464 | 55.7k | 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 | 55.7k | } else { | 493 | 1.96M | for (size_t x = 0; x < channel.w; x++) { | 494 | 1.91M | PredictionResult res = | 495 | 1.91M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 1.91M | tree_lookup, references, &wp_state); | 497 | 1.91M | uint64_t v = | 498 | 1.91M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 1.91M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 1.91M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 1.91M | } | 502 | 55.7k | } | 503 | 55.7k | } | 504 | 10.8k | } | 505 | 40.6k | return true; | 506 | 40.6k | } |
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 | 788k | uint32_t &fl_v) { | 157 | 788k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 788k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 788k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 788k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 788k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 788k | bool tree_has_wp_prop_or_pred = false; | 168 | 788k | bool is_wp_only = false; | 169 | 788k | bool is_gradient_only = false; | 170 | 788k | size_t num_props; | 171 | 788k | FlatTree tree = | 172 | 788k | FilterTree(global_tree, static_props, &num_props, | 173 | 788k | &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.56M | for (auto &node : tree) { | 178 | 1.56M | if (node.property0 == -1) { | 179 | 1.36M | node.childID = context_map[node.childID]; | 180 | 1.36M | } | 181 | 1.56M | } | 182 | | | 183 | 788k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 788k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 788k | pixel_type_w offset) -> pixel_type { | 188 | 788k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 788k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 788k | return val * multiplier + offset; | 192 | 788k | }; | 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 | 788k | const bool global_tree_is_all_gradient_noop = [&] { | 200 | 788k | for (const auto& n : global_tree) { | 201 | 788k | if (n.property == -1) { | 202 | 788k | if (n.predictor != Predictor::Gradient || n.predictor_offset != 0 || | 203 | 788k | n.multiplier != 1) | 204 | 788k | return false; | 205 | 788k | } else if (n.property >= kNumStaticProperties) { | 206 | 788k | return false; | 207 | 788k | } | 208 | 788k | } | 209 | 788k | return true; | 210 | 788k | }(); | 211 | | | 212 | 788k | if (tree.size() == 1) { | 213 | | // special optimized case: no meta-adaptation, so no need | 214 | | // to compute properties. | 215 | 749k | Predictor predictor = tree[0].predictor; | 216 | 749k | int64_t offset = tree[0].predictor_offset; | 217 | 749k | int32_t multiplier = tree[0].multiplier; | 218 | 749k | size_t ctx_id = tree[0].childID; | 219 | 749k | if (predictor == Predictor::Zero) { | 220 | 592k | uint32_t value; | 221 | 592k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 222 | 592k | channel.w * channel.h)) { | 223 | | // Special-case: histogram has a single symbol, with no extra bits, and | 224 | | // we use ANS mode. | 225 | 86.5k | JXL_DEBUG_V(8, "Fastest track."); | 226 | 86.5k | pixel_type v = make_pixel(value, multiplier, offset); | 227 | 1.56M | for (size_t y = 0; y < channel.h; y++) { | 228 | 1.47M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 229 | 1.47M | std::fill(r, r + channel.w, v); | 230 | 1.47M | } | 231 | 506k | } else { | 232 | 506k | JXL_DEBUG_V(8, "Fast track."); | 233 | 506k | if (multiplier == 1 && offset == 0) { | 234 | 5.93M | for (size_t y = 0; y < channel.h; y++) { | 235 | 5.47M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 236 | 266M | for (size_t x = 0; x < channel.w; x++) { | 237 | 261M | uint32_t v = | 238 | 261M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 239 | 261M | r[x] = UnpackSigned(v); | 240 | 261M | } | 241 | 5.47M | } | 242 | 461k | } else { | 243 | 461k | for (size_t y = 0; y < channel.h; y++) { | 244 | 417k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 245 | 20.3M | for (size_t x = 0; x < channel.w; x++) { | 246 | 19.9M | uint32_t v = | 247 | 19.9M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 248 | 19.9M | br); | 249 | 19.9M | r[x] = make_pixel(v, multiplier, offset); | 250 | 19.9M | } | 251 | 417k | } | 252 | 44.1k | } | 253 | 506k | } | 254 | 592k | return true; | 255 | 592k | } 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 | 156k | } else if (predictor == Predictor::Gradient && offset == 0 && | 290 | 8.12k | multiplier == 1) { | 291 | 7.63k | JXL_DEBUG_V(8, "Gradient very fast track."); | 292 | 7.63k | 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.70M | for (size_t x = 0; x < channel.w; x++) { | 296 | 2.53M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 297 | 2.53M | pixel_type top = (y ? *(r + x - onerow) : left); | 298 | 2.53M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 299 | 2.53M | pixel_type guess = ClampedGradient(top, left, topleft); | 300 | 2.53M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 301 | 2.53M | ctx_id, br); | 302 | 2.53M | r[x] = make_pixel(v, 1, guess); | 303 | 2.53M | } | 304 | 163k | } | 305 | 7.63k | return true; | 306 | 7.63k | } | 307 | 749k | } | 308 | | | 309 | | // Check if this tree is a WP-only tree with a small enough property value | 310 | | // range. | 311 | 188k | if (is_wp_only) { | 312 | 15.7k | is_wp_only = TreeToLookupTable(tree, tree_lut); | 313 | 15.7k | } | 314 | 188k | if (is_gradient_only) { | 315 | 5.87k | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 316 | 5.87k | } | 317 | | | 318 | 188k | if (is_gradient_only) { | 319 | 4.42k | JXL_DEBUG_V(8, "Gradient fast track."); | 320 | 4.42k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 321 | 171k | for (size_t y = 0; y < channel.h; y++) { | 322 | 166k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 323 | 2.81M | for (size_t x = 0; x < channel.w; x++) { | 324 | 2.64M | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 325 | 2.64M | pixel_type_w top = (y ? *(r + x - onerow) : left); | 326 | 2.64M | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 327 | 2.64M | int32_t guess = ClampedGradient(top, left, topleft); | 328 | 2.64M | uint32_t pos = | 329 | 2.64M | kPropRangeFast + | 330 | 2.64M | std::min<pixel_type_w>( | 331 | 2.64M | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 332 | 2.64M | kPropRangeFast - 1); | 333 | 2.64M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 334 | 2.64M | uint64_t v = | 335 | 2.64M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 336 | 2.64M | r[x] = make_pixel(v, 1, guess); | 337 | 2.64M | } | 338 | 166k | } | 339 | 183k | } 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 | 334k | for (size_t y = 0; y < channel.h; y++) { | 344 | 326k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 345 | 326k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 346 | 326k | const pixel_type *JXL_RESTRICT rtoptop = | 347 | 326k | (y > 1 ? channel.Row(y - 2) : rtop); | 348 | 326k | const pixel_type *JXL_RESTRICT rtopleft = | 349 | 326k | (y ? channel.Row(y - 1) - 1 : r - 1); | 350 | 326k | const pixel_type *JXL_RESTRICT rtopright = | 351 | 326k | (y ? channel.Row(y - 1) + 1 : r - 1); | 352 | 326k | size_t x = 0; | 353 | 326k | { | 354 | 326k | size_t offset = 0; | 355 | 326k | pixel_type_w left = y ? rtop[x] : 0; | 356 | 326k | pixel_type_w toptop = y ? rtoptop[x] : 0; | 357 | 326k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 358 | 326k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 359 | 326k | x, y, channel.w, left, left, topright, left, toptop, &properties, | 360 | 326k | offset); | 361 | 326k | uint32_t pos = | 362 | 326k | kPropRangeFast + | 363 | 326k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 364 | 326k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 365 | 326k | uint64_t v = | 366 | 326k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 367 | 326k | r[x] = make_pixel(v, 1, guess); | 368 | 326k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 369 | 326k | } | 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 | 326k | { | 385 | 326k | size_t offset = 0; | 386 | 326k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 387 | 326k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 388 | 326k | rtoptop[x], &properties, offset); | 389 | 326k | uint32_t pos = | 390 | 326k | kPropRangeFast + | 391 | 326k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 392 | 326k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 393 | 326k | uint64_t v = | 394 | 326k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 395 | 326k | r[x] = make_pixel(v, 1, guess); | 396 | 326k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 397 | 326k | } | 398 | 326k | } | 399 | 175k | } 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 | 152k | JXL_DEBUG_V(8, "Slow track."); | 403 | 152k | MATreeLookup tree_lookup(tree); | 404 | 152k | Properties properties = Properties(num_props); | 405 | 152k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 406 | 152k | JXL_ASSIGN_OR_RETURN( | 407 | 152k | Channel references, | 408 | 152k | Channel::Create(memory_manager, | 409 | 152k | properties.size() - kNumNonrefProperties, channel.w)); | 410 | 4.90M | for (size_t y = 0; y < channel.h; y++) { | 411 | 4.75M | pixel_type *JXL_RESTRICT p = channel.Row(y); | 412 | 4.75M | PrecomputeReferences(channel, y, *image, chan, &references); | 413 | 4.75M | InitPropsRow(&properties, static_props, y); | 414 | 4.75M | if (y > 1 && channel.w > 8 && references.w == 0) { | 415 | 12.1M | for (size_t x = 0; x < 2; x++) { | 416 | 8.08M | PredictionResult res = | 417 | 8.08M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 418 | 8.08M | tree_lookup, references); | 419 | 8.08M | uint64_t v = | 420 | 8.08M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 421 | 8.08M | p[x] = make_pixel(v, res.multiplier, res.guess); | 422 | 8.08M | } | 423 | 210M | for (size_t x = 2; x < channel.w - 2; x++) { | 424 | 206M | PredictionResult res = | 425 | 206M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 426 | 206M | tree_lookup, references); | 427 | 206M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 428 | 206M | res.context, br); | 429 | 206M | p[x] = make_pixel(v, res.multiplier, res.guess); | 430 | 206M | } | 431 | 12.1M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 432 | 8.08M | PredictionResult res = | 433 | 8.08M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 434 | 8.08M | tree_lookup, references); | 435 | 8.08M | uint64_t v = | 436 | 8.08M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 437 | 8.08M | p[x] = make_pixel(v, res.multiplier, res.guess); | 438 | 8.08M | } | 439 | 4.04M | } else { | 440 | 14.4M | for (size_t x = 0; x < channel.w; x++) { | 441 | 13.7M | PredictionResult res = | 442 | 13.7M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 443 | 13.7M | tree_lookup, references); | 444 | 13.7M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 445 | 13.7M | res.context, br); | 446 | 13.7M | p[x] = make_pixel(v, res.multiplier, res.guess); | 447 | 13.7M | } | 448 | 705k | } | 449 | 4.75M | } | 450 | 152k | } else { | 451 | 22.2k | JXL_DEBUG_V(8, "Slowest track."); | 452 | 22.2k | MATreeLookup tree_lookup(tree); | 453 | 22.2k | Properties properties = Properties(num_props); | 454 | 22.2k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 455 | 22.2k | JXL_ASSIGN_OR_RETURN( | 456 | 22.2k | Channel references, | 457 | 22.2k | Channel::Create(memory_manager, | 458 | 22.2k | properties.size() - kNumNonrefProperties, channel.w)); | 459 | 22.2k | 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.56M | for (size_t x = 0; x < 2; x++) { | 466 | 1.04M | PredictionResult res = | 467 | 1.04M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 1.04M | tree_lookup, references, &wp_state); | 469 | 1.04M | uint64_t v = | 470 | 1.04M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 1.04M | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 1.04M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 1.04M | } | 474 | 109M | for (size_t x = 2; x < channel.w - 2; x++) { | 475 | 109M | PredictionResult res = | 476 | 109M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 477 | 109M | tree_lookup, references, &wp_state); | 478 | 109M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 479 | 109M | res.context, br); | 480 | 109M | p[x] = make_pixel(v, res.multiplier, res.guess); | 481 | 109M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 482 | 109M | } | 483 | 1.56M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 484 | 1.04M | PredictionResult res = | 485 | 1.04M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 486 | 1.04M | tree_lookup, references, &wp_state); | 487 | 1.04M | uint64_t v = | 488 | 1.04M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 489 | 1.04M | p[x] = make_pixel(v, res.multiplier, res.guess); | 490 | 1.04M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 491 | 1.04M | } | 492 | 953k | } else { | 493 | 8.79M | for (size_t x = 0; x < channel.w; x++) { | 494 | 7.84M | PredictionResult res = | 495 | 7.84M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 496 | 7.84M | tree_lookup, references, &wp_state); | 497 | 7.84M | uint64_t v = | 498 | 7.84M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 499 | 7.84M | p[x] = make_pixel(v, res.multiplier, res.guess); | 500 | 7.84M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 501 | 7.84M | } | 502 | 953k | } | 503 | 1.47M | } | 504 | 22.2k | } | 505 | 188k | return true; | 506 | 188k | } |
|
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 | 922k | uint32_t &fl_v) { |
517 | 922k | if (reader->UsesLZ77()) { |
518 | 134k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>( |
519 | 134k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
520 | 134k | tree_lut, image, fl_run, fl_v); |
521 | 788k | } else { |
522 | 788k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>( |
523 | 788k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
524 | 788k | tree_lut, image, fl_run, fl_v); |
525 | 788k | } |
526 | 922k | } |
527 | | |
528 | 866k | GroupHeader::GroupHeader() { Bundle::Init(this); } |
529 | | |
530 | | Status ValidateChannelDimensions(const Image &image, |
531 | 211k | const ModularOptions &options) { |
532 | 211k | size_t nb_channels = image.channel.size(); |
533 | 423k | for (bool is_dc : {true, false}) { |
534 | 423k | size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1); |
535 | 423k | size_t c = image.nb_meta_channels; |
536 | 2.57M | for (; c < nb_channels; c++) { |
537 | 2.16M | const Channel &ch = image.channel[c]; |
538 | 2.16M | if (ch.w > options.group_dim || ch.h > options.group_dim) break; |
539 | 2.16M | } |
540 | 485k | for (; c < nb_channels; c++) { |
541 | 61.2k | const Channel &ch = image.channel[c]; |
542 | 61.2k | if (ch.w == 0 || ch.h == 0) continue; // skip empty |
543 | 60.2k | bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3; |
544 | 60.2k | if (is_dc_channel != is_dc) continue; |
545 | 30.1k | size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift); |
546 | 30.1k | if (tile_dim == 0) { |
547 | 8 | return JXL_FAILURE("Inconsistent transforms"); |
548 | 8 | } |
549 | 30.1k | } |
550 | 423k | } |
551 | 211k | return true; |
552 | 211k | } |
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 | 343k | const bool allow_truncated_group) { |
559 | 343k | if (image.channel.empty()) return true; |
560 | 244k | JxlMemoryManager *memory_manager = image.memory_manager(); |
561 | | |
562 | | // decode transforms |
563 | 244k | Status status = Bundle::Read(br, &header); |
564 | 244k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status); |
565 | 218k | if (status.IsFatalError()) return status; |
566 | 218k | 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 | 218k | JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ", |
577 | 218k | header.transforms.size()); |
578 | 218k | image.transform = header.transforms; |
579 | 218k | for (Transform &transform : image.transform) { |
580 | 58.4k | JXL_RETURN_IF_ERROR(transform.MetaApply(image)); |
581 | 58.4k | } |
582 | 209k | if (image.error) { |
583 | 0 | return JXL_FAILURE("Corrupt file. Aborting."); |
584 | 0 | } |
585 | 209k | JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options)); |
586 | | |
587 | 209k | size_t nb_channels = image.channel.size(); |
588 | | |
589 | 209k | size_t num_chans = 0; |
590 | 209k | size_t distance_multiplier = 0; |
591 | 1.28M | for (size_t i = 0; i < nb_channels; i++) { |
592 | 1.08M | Channel &channel = image.channel[i]; |
593 | 1.08M | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
594 | 1.07M | channel.h > options->max_chan_size)) { |
595 | 5.62k | break; |
596 | 5.62k | } |
597 | 1.07M | if (!channel.w || !channel.h) { |
598 | 44.2k | continue; // skip empty channels |
599 | 44.2k | } |
600 | 1.03M | if (channel.w > distance_multiplier) { |
601 | 254k | distance_multiplier = channel.w; |
602 | 254k | } |
603 | 1.03M | num_chans++; |
604 | 1.03M | } |
605 | 209k | if (num_chans == 0) return true; |
606 | | |
607 | 205k | size_t next_channel = 0; |
608 | 205k | auto scope_guard = MakeScopeGuard([&]() { |
609 | 155k | for (size_t c = next_channel; c < image.channel.size(); c++) { |
610 | 120k | ZeroFillImage(&image.channel[c].plane); |
611 | 120k | } |
612 | 35.2k | }); |
613 | | // Do not do anything if truncated groups are not allowed. |
614 | 205k | if (allow_truncated_group) scope_guard.Disarm(); |
615 | | |
616 | | // Read tree. |
617 | 205k | Tree tree_storage; |
618 | 205k | std::vector<uint8_t> context_map_storage; |
619 | 205k | ANSCode code_storage; |
620 | 205k | const Tree *tree = &tree_storage; |
621 | 205k | const ANSCode *code = &code_storage; |
622 | 205k | const std::vector<uint8_t> *context_map = &context_map_storage; |
623 | 205k | if (!header.use_global_tree) { |
624 | 152k | uint64_t max_tree_size = 1024; |
625 | 837k | for (size_t i = 0; i < nb_channels; i++) { |
626 | 685k | Channel &channel = image.channel[i]; |
627 | 685k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
628 | 682k | channel.h > options->max_chan_size)) { |
629 | 87 | break; |
630 | 87 | } |
631 | 685k | uint64_t pixels = channel.w * channel.h; |
632 | 685k | max_tree_size += pixels; |
633 | 685k | } |
634 | 152k | max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size); |
635 | 152k | JXL_RETURN_IF_ERROR( |
636 | 152k | DecodeTree(memory_manager, br, &tree_storage, max_tree_size)); |
637 | 132k | JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, |
638 | 132k | (tree_storage.size() + 1) / 2, |
639 | 132k | &code_storage, &context_map_storage)); |
640 | 132k | } else { |
641 | 53.6k | if (!global_tree || !global_code || !global_ctx_map || |
642 | 53.6k | global_tree->empty()) { |
643 | 939 | return JXL_FAILURE("No global tree available but one was requested"); |
644 | 939 | } |
645 | 52.6k | tree = global_tree; |
646 | 52.6k | code = global_code; |
647 | 52.6k | context_map = global_ctx_map; |
648 | 52.6k | } |
649 | | |
650 | | // Read channels |
651 | 358k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader, |
652 | 358k | ANSSymbolReader::Create(code, br, distance_multiplier)); |
653 | 358k | auto tree_lut = jxl::make_unique<TreeLut<uint8_t, false, false>>(); |
654 | 358k | uint32_t fl_run = 0; |
655 | 358k | uint32_t fl_v = 0; |
656 | 1.13M | for (; next_channel < nb_channels; next_channel++) { |
657 | 966k | Channel &channel = image.channel[next_channel]; |
658 | 966k | if (next_channel >= image.nb_meta_channels && |
659 | 960k | (channel.w > options->max_chan_size || |
660 | 959k | channel.h > options->max_chan_size)) { |
661 | 1.83k | break; |
662 | 1.83k | } |
663 | 965k | if (!channel.w || !channel.h) { |
664 | 42.4k | continue; // skip empty channels |
665 | 42.4k | } |
666 | 922k | JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS( |
667 | 922k | br, &reader, *context_map, *tree, header.wp_header, next_channel, |
668 | 922k | group_id, *tree_lut, &image, fl_run, fl_v)); |
669 | | |
670 | | // Truncated group. |
671 | 922k | if (!br->AllReadsWithinBounds()) { |
672 | 8.62k | if (!allow_truncated_group) return JXL_FAILURE("Truncated input"); |
673 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun in ModularDecode"); |
674 | 8.62k | } |
675 | 922k | } |
676 | | |
677 | | // Make sure no zero-filling happens even if next_channel < nb_channels. |
678 | 170k | scope_guard.Disarm(); |
679 | | |
680 | 170k | if (!reader.CheckANSFinalState()) { |
681 | 0 | return JXL_FAILURE("ANS decode final state failed"); |
682 | 0 | } |
683 | 170k | return true; |
684 | 170k | } |
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 | 343k | bool allow_truncated_group) { |
692 | 343k | std::vector<std::pair<size_t, size_t>> req_sizes; |
693 | 343k | req_sizes.reserve(image.channel.size()); |
694 | 821k | for (const auto &c : image.channel) { |
695 | 821k | req_sizes.emplace_back(c.w, c.h); |
696 | 821k | } |
697 | 343k | GroupHeader local_header; |
698 | 343k | if (header == nullptr) header = &local_header; |
699 | 343k | size_t bit_pos = br->TotalBitsConsumed(); |
700 | 343k | auto dec_status = ModularDecode(br, image, *header, group_id, options, tree, |
701 | 343k | code, ctx_map, allow_truncated_group); |
702 | 343k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status); |
703 | 273k | if (dec_status.IsFatalError()) return dec_status; |
704 | 273k | if (undo_transforms) image.undo_transforms(header->wp_header); |
705 | 273k | if (image.error) return JXL_FAILURE("Corrupt file. Aborting."); |
706 | 273k | JXL_DEBUG_V(4, |
707 | 273k | "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS |
708 | 273k | " image from %" PRIuS " bytes", |
709 | 273k | image.w, image.h, image.channel.size(), |
710 | 273k | (br->TotalBitsConsumed() - bit_pos) / 8); |
711 | 273k | JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str()); |
712 | 273k | (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 | 273k | if (undo_transforms) { |
717 | 110k | JXL_ENSURE(image.channel.size() == req_sizes.size()); |
718 | 496k | for (size_t c = 0; c < req_sizes.size(); c++) { |
719 | 385k | JXL_ENSURE(req_sizes[c].first == image.channel[c].w); |
720 | 385k | JXL_ENSURE(req_sizes[c].second == image.channel[c].h); |
721 | 385k | } |
722 | 110k | } |
723 | 273k | return dec_status; |
724 | 273k | } |
725 | | |
726 | | } // namespace jxl |