/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 | 378k | bool *gradient_only) { |
46 | 378k | *num_props = 0; |
47 | 378k | bool has_wp = false; |
48 | 378k | bool has_non_wp = false; |
49 | 378k | *gradient_only = true; |
50 | 1.03M | const auto mark_property = [&](int32_t p) { |
51 | 1.03M | if (p == kWPProp) { |
52 | 96.7k | has_wp = true; |
53 | 937k | } else if (p >= kNumStaticProperties) { |
54 | 570k | has_non_wp = true; |
55 | 570k | } |
56 | 1.03M | if (p >= kNumStaticProperties && p != kGradientProp) { |
57 | 602k | *gradient_only = false; |
58 | 602k | } |
59 | 1.03M | }; |
60 | 378k | FlatTree output; |
61 | 378k | std::queue<size_t> nodes; |
62 | 378k | 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 | 2.13M | while (!nodes.empty()) { |
70 | 1.75M | size_t cur = nodes.front(); |
71 | 1.75M | nodes.pop(); |
72 | | // Skip nodes that we can decide now, by jumping directly to their children. |
73 | 1.81M | while (global_tree[cur].property < kNumStaticProperties && |
74 | 1.47M | global_tree[cur].property != -1) { |
75 | 58.9k | if (static_props[global_tree[cur].property] > global_tree[cur].splitval) { |
76 | 31.0k | cur = global_tree[cur].lchild; |
77 | 31.0k | } else { |
78 | 27.9k | cur = global_tree[cur].rchild; |
79 | 27.9k | } |
80 | 58.9k | } |
81 | 1.75M | FlatDecisionNode flat; |
82 | 1.75M | if (global_tree[cur].property == -1) { |
83 | 1.41M | flat.property0 = -1; |
84 | 1.41M | flat.childID = global_tree[cur].lchild; |
85 | 1.41M | flat.predictor = global_tree[cur].predictor; |
86 | 1.41M | flat.predictor_offset = global_tree[cur].predictor_offset; |
87 | 1.41M | flat.multiplier = global_tree[cur].multiplier; |
88 | 1.41M | *gradient_only &= flat.predictor == Predictor::Gradient; |
89 | 1.41M | has_wp |= flat.predictor == Predictor::Weighted; |
90 | 1.41M | has_non_wp |= flat.predictor != Predictor::Weighted; |
91 | 1.41M | output.push_back(flat); |
92 | 1.41M | continue; |
93 | 1.41M | } |
94 | 344k | flat.childID = output.size() + nodes.size() + 1; |
95 | | |
96 | 344k | flat.property0 = global_tree[cur].property; |
97 | 344k | *num_props = std::max<size_t>(flat.property0 + 1, *num_props); |
98 | 344k | flat.splitval0 = global_tree[cur].splitval; |
99 | | |
100 | 1.03M | for (size_t i = 0; i < 2; i++) { |
101 | 689k | size_t cur_child = |
102 | 689k | i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild; |
103 | | // Skip nodes that we can decide now. |
104 | 709k | while (global_tree[cur_child].property < kNumStaticProperties && |
105 | 387k | global_tree[cur_child].property != -1) { |
106 | 20.1k | if (static_props[global_tree[cur_child].property] > |
107 | 20.1k | global_tree[cur_child].splitval) { |
108 | 10.1k | cur_child = global_tree[cur_child].lchild; |
109 | 10.1k | } else { |
110 | 10.0k | cur_child = global_tree[cur_child].rchild; |
111 | 10.0k | } |
112 | 20.1k | } |
113 | | // We ended up in a leaf, add a placeholder decision and two copies of the |
114 | | // leaf. |
115 | 689k | if (global_tree[cur_child].property == -1) { |
116 | 366k | flat.properties[i] = 0; |
117 | 366k | flat.splitvals[i] = 0; |
118 | 366k | nodes.push(cur_child); |
119 | 366k | nodes.push(cur_child); |
120 | 366k | } else { |
121 | 322k | flat.properties[i] = global_tree[cur_child].property; |
122 | 322k | flat.splitvals[i] = global_tree[cur_child].splitval; |
123 | 322k | nodes.push(global_tree[cur_child].lchild); |
124 | 322k | nodes.push(global_tree[cur_child].rchild); |
125 | 322k | *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props); |
126 | 322k | } |
127 | 689k | } |
128 | | |
129 | 689k | for (int16_t property : flat.properties) mark_property(property); |
130 | 344k | mark_property(flat.property0); |
131 | 344k | output.push_back(flat); |
132 | 344k | } |
133 | 378k | if (*num_props > kNumNonrefProperties) { |
134 | 2.49k | *num_props = |
135 | 2.49k | DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) * |
136 | 2.49k | kExtraPropsPerChannel + |
137 | 2.49k | kNumNonrefProperties; |
138 | 376k | } else { |
139 | 376k | *num_props = kNumNonrefProperties; |
140 | 376k | } |
141 | 378k | *use_wp = has_wp; |
142 | 378k | *wp_only = has_wp && !has_non_wp; |
143 | | |
144 | 378k | return output; |
145 | 378k | } |
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 | 354k | uint32_t &fl_v) { |
157 | 354k | JxlMemoryManager *memory_manager = image->memory_manager(); |
158 | 354k | Channel &channel = image->channel[chan]; |
159 | | |
160 | 354k | std::array<pixel_type, kNumStaticProperties> static_props = { |
161 | 354k | {chan, static_cast<int>(group_id)}}; |
162 | | // TODO(veluca): filter the tree according to static_props. |
163 | | |
164 | | // zero pixel channel? could happen |
165 | 354k | if (channel.w == 0 || channel.h == 0) return true; |
166 | | |
167 | 354k | bool tree_has_wp_prop_or_pred = false; |
168 | 354k | bool is_wp_only = false; |
169 | 354k | bool is_gradient_only = false; |
170 | 354k | size_t num_props; |
171 | 354k | FlatTree tree = |
172 | 354k | FilterTree(global_tree, static_props, &num_props, |
173 | 354k | &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 | 515k | for (auto &node : tree) { |
178 | 515k | if (node.property0 == -1) { |
179 | 474k | node.childID = context_map[node.childID]; |
180 | 474k | } |
181 | 515k | } |
182 | | |
183 | 354k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); |
184 | | |
185 | | // MAANS decode |
186 | 354k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, |
187 | 239M | pixel_type_w offset) -> pixel_type { |
188 | 239M | JXL_DASSERT((v & 0xFFFFFFFF) == v); |
189 | 239M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); |
190 | | // if it overflows, it overflows, and we have a problem anyway |
191 | 239M | return val * multiplier + offset; |
192 | 239M | }; 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 | 44.9M | pixel_type_w offset) -> pixel_type { | 188 | 44.9M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 44.9M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 44.9M | return val * multiplier + offset; | 192 | 44.9M | }; |
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 | 194M | pixel_type_w offset) -> pixel_type { | 188 | 194M | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 194M | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 194M | return val * multiplier + offset; | 192 | 194M | }; |
|
193 | | |
194 | 354k | if (tree.size() == 1) { |
195 | | // special optimized case: no meta-adaptation, so no need |
196 | | // to compute properties. |
197 | 341k | Predictor predictor = tree[0].predictor; |
198 | 341k | int64_t offset = tree[0].predictor_offset; |
199 | 341k | int32_t multiplier = tree[0].multiplier; |
200 | 341k | size_t ctx_id = tree[0].childID; |
201 | 341k | if (predictor == Predictor::Zero) { |
202 | 318k | uint32_t value; |
203 | 318k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, |
204 | 318k | channel.w * channel.h)) { |
205 | | // Special-case: histogram has a single symbol, with no extra bits, and |
206 | | // we use ANS mode. |
207 | 131k | JXL_DEBUG_V(8, "Fastest track."); |
208 | 131k | pixel_type v = make_pixel(value, multiplier, offset); |
209 | 4.13M | for (size_t y = 0; y < channel.h; y++) { |
210 | 3.99M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
211 | 3.99M | std::fill(r, r + channel.w, v); |
212 | 3.99M | } |
213 | 187k | } else { |
214 | 187k | JXL_DEBUG_V(8, "Fast track."); |
215 | 187k | if (multiplier == 1 && offset == 0) { |
216 | 2.69M | for (size_t y = 0; y < channel.h; y++) { |
217 | 2.53M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
218 | 175M | for (size_t x = 0; x < channel.w; x++) { |
219 | 173M | uint32_t v = |
220 | 173M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
221 | 173M | r[x] = UnpackSigned(v); |
222 | 173M | } |
223 | 2.53M | } |
224 | 152k | } else { |
225 | 1.46M | for (size_t y = 0; y < channel.h; y++) { |
226 | 1.42M | pixel_type *JXL_RESTRICT r = channel.Row(y); |
227 | 157M | for (size_t x = 0; x < channel.w; x++) { |
228 | 155M | uint32_t v = |
229 | 155M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, |
230 | 155M | br); |
231 | 155M | r[x] = make_pixel(v, multiplier, offset); |
232 | 155M | } |
233 | 1.42M | } |
234 | 34.4k | } |
235 | 187k | } |
236 | 318k | return true; |
237 | 318k | } else if (uses_lz77 && predictor == Predictor::Gradient && offset == 0 && |
238 | 990 | multiplier == 1 && reader->IsHuffRleOnly()) { |
239 | 194 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); |
240 | 194 | pixel_type_w sv = UnpackSigned(fl_v); |
241 | 7.83k | for (size_t y = 0; y < channel.h; y++) { |
242 | 7.64k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
243 | 7.64k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
244 | 7.64k | const pixel_type *JXL_RESTRICT rtopleft = |
245 | 7.64k | (y ? channel.Row(y - 1) - 1 : r - 1); |
246 | 7.64k | pixel_type_w guess_0 = (y ? rtop[0] : 0); |
247 | 7.64k | if (fl_run == 0) { |
248 | 1.61k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
249 | 1.61k | &fl_run); |
250 | 1.61k | sv = UnpackSigned(fl_v); |
251 | 6.02k | } else { |
252 | 6.02k | fl_run--; |
253 | 6.02k | } |
254 | 7.64k | r[0] = sv + guess_0; |
255 | 253k | for (size_t x = 1; x < channel.w; x++) { |
256 | 245k | pixel_type left = r[x - 1]; |
257 | 245k | pixel_type top = rtop[x]; |
258 | 245k | pixel_type topleft = rtopleft[x]; |
259 | 245k | pixel_type_w guess = ClampedGradient(top, left, topleft); |
260 | 245k | if (!fl_run) { |
261 | 53.1k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, |
262 | 53.1k | &fl_run); |
263 | 53.1k | sv = UnpackSigned(fl_v); |
264 | 192k | } else { |
265 | 192k | fl_run--; |
266 | 192k | } |
267 | 245k | r[x] = sv + guess; |
268 | 245k | } |
269 | 7.64k | } |
270 | 194 | return true; |
271 | 23.1k | } else if (predictor == Predictor::Gradient && offset == 0 && |
272 | 2.57k | multiplier == 1) { |
273 | 2.18k | JXL_DEBUG_V(8, "Gradient very fast track."); |
274 | 2.18k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
275 | 53.0k | for (size_t y = 0; y < channel.h; y++) { |
276 | 50.8k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
277 | 2.69M | for (size_t x = 0; x < channel.w; x++) { |
278 | 2.64M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
279 | 2.64M | pixel_type top = (y ? *(r + x - onerow) : left); |
280 | 2.64M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); |
281 | 2.64M | pixel_type guess = ClampedGradient(top, left, topleft); |
282 | 2.64M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
283 | 2.64M | ctx_id, br); |
284 | 2.64M | r[x] = make_pixel(v, 1, guess); |
285 | 2.64M | } |
286 | 50.8k | } |
287 | 2.18k | return true; |
288 | 2.18k | } |
289 | 341k | } |
290 | | |
291 | | // Check if this tree is a WP-only tree with a small enough property value |
292 | | // range. |
293 | 33.2k | if (is_wp_only) { |
294 | 4.65k | is_wp_only = TreeToLookupTable(tree, tree_lut); |
295 | 4.65k | } |
296 | 33.2k | if (is_gradient_only) { |
297 | 1.80k | is_gradient_only = TreeToLookupTable(tree, tree_lut); |
298 | 1.80k | } |
299 | | |
300 | 33.2k | if (is_gradient_only) { |
301 | 750 | JXL_DEBUG_V(8, "Gradient fast track."); |
302 | 750 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
303 | 16.1k | for (size_t y = 0; y < channel.h; y++) { |
304 | 15.4k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
305 | 572k | for (size_t x = 0; x < channel.w; x++) { |
306 | 556k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); |
307 | 556k | pixel_type_w top = (y ? *(r + x - onerow) : left); |
308 | 556k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); |
309 | 556k | int32_t guess = ClampedGradient(top, left, topleft); |
310 | 556k | uint32_t pos = |
311 | 556k | kPropRangeFast + |
312 | 556k | std::min<pixel_type_w>( |
313 | 556k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), |
314 | 556k | kPropRangeFast - 1); |
315 | 556k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
316 | 556k | uint64_t v = |
317 | 556k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); |
318 | 556k | r[x] = make_pixel(v, 1, guess); |
319 | 556k | } |
320 | 15.4k | } |
321 | 32.4k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { |
322 | 887 | JXL_DEBUG_V(8, "WP fast track."); |
323 | 887 | weighted::State wp_state(wp_header, channel.w, channel.h); |
324 | 887 | Properties properties(1); |
325 | 22.4k | for (size_t y = 0; y < channel.h; y++) { |
326 | 21.5k | pixel_type *JXL_RESTRICT r = channel.Row(y); |
327 | 21.5k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); |
328 | 21.5k | const pixel_type *JXL_RESTRICT rtoptop = |
329 | 21.5k | (y > 1 ? channel.Row(y - 2) : rtop); |
330 | 21.5k | const pixel_type *JXL_RESTRICT rtopleft = |
331 | 21.5k | (y ? channel.Row(y - 1) - 1 : r - 1); |
332 | 21.5k | const pixel_type *JXL_RESTRICT rtopright = |
333 | 21.5k | (y ? channel.Row(y - 1) + 1 : r - 1); |
334 | 21.5k | size_t x = 0; |
335 | 21.5k | { |
336 | 21.5k | size_t offset = 0; |
337 | 21.5k | pixel_type_w left = y ? rtop[x] : 0; |
338 | 21.5k | pixel_type_w toptop = y ? rtoptop[x] : 0; |
339 | 21.5k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); |
340 | 21.5k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
341 | 21.5k | x, y, channel.w, left, left, topright, left, toptop, &properties, |
342 | 21.5k | offset); |
343 | 21.5k | uint32_t pos = |
344 | 21.5k | kPropRangeFast + |
345 | 21.5k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
346 | 21.5k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
347 | 21.5k | uint64_t v = |
348 | 21.5k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
349 | 21.5k | r[x] = make_pixel(v, 1, guess); |
350 | 21.5k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
351 | 21.5k | } |
352 | 2.03M | for (x = 1; x + 1 < channel.w; x++) { |
353 | 2.01M | size_t offset = 0; |
354 | 2.01M | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
355 | 2.01M | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], |
356 | 2.01M | rtoptop[x], &properties, offset); |
357 | 2.01M | uint32_t pos = |
358 | 2.01M | kPropRangeFast + |
359 | 2.01M | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
360 | 2.01M | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
361 | 2.01M | uint64_t v = |
362 | 2.01M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
363 | 2.01M | r[x] = make_pixel(v, 1, guess); |
364 | 2.01M | wp_state.UpdateErrors(r[x], x, y, channel.w); |
365 | 2.01M | } |
366 | 21.5k | { |
367 | 21.5k | size_t offset = 0; |
368 | 21.5k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( |
369 | 21.5k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], |
370 | 21.5k | rtoptop[x], &properties, offset); |
371 | 21.5k | uint32_t pos = |
372 | 21.5k | kPropRangeFast + |
373 | 21.5k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); |
374 | 21.5k | uint32_t ctx_id = tree_lut.context_lookup[pos]; |
375 | 21.5k | uint64_t v = |
376 | 21.5k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); |
377 | 21.5k | r[x] = make_pixel(v, 1, guess); |
378 | 21.5k | wp_state.UpdateErrors(r[x], x, y, channel.w); |
379 | 21.5k | } |
380 | 21.5k | } |
381 | 31.5k | } else if (!tree_has_wp_prop_or_pred) { |
382 | | // special optimized case: the weighted predictor and its properties are not |
383 | | // used, so no need to compute weights and properties. |
384 | 21.2k | JXL_DEBUG_V(8, "Slow track."); |
385 | 21.2k | MATreeLookup tree_lookup(tree); |
386 | 21.2k | Properties properties = Properties(num_props); |
387 | 21.2k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
388 | 21.2k | JXL_ASSIGN_OR_RETURN( |
389 | 21.2k | Channel references, |
390 | 21.2k | Channel::Create(memory_manager, |
391 | 21.2k | properties.size() - kNumNonrefProperties, channel.w)); |
392 | 637k | for (size_t y = 0; y < channel.h; y++) { |
393 | 616k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
394 | 616k | PrecomputeReferences(channel, y, *image, chan, &references); |
395 | 616k | InitPropsRow(&properties, static_props, y); |
396 | 616k | if (y > 1 && channel.w > 8 && references.w == 0) { |
397 | 1.52M | for (size_t x = 0; x < 2; x++) { |
398 | 1.01M | PredictionResult res = |
399 | 1.01M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
400 | 1.01M | tree_lookup, references); |
401 | 1.01M | uint64_t v = |
402 | 1.01M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
403 | 1.01M | p[x] = make_pixel(v, res.multiplier, res.guess); |
404 | 1.01M | } |
405 | 55.7M | for (size_t x = 2; x < channel.w - 2; x++) { |
406 | 55.2M | PredictionResult res = |
407 | 55.2M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, |
408 | 55.2M | tree_lookup, references); |
409 | 55.2M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
410 | 55.2M | res.context, br); |
411 | 55.2M | p[x] = make_pixel(v, res.multiplier, res.guess); |
412 | 55.2M | } |
413 | 1.52M | for (size_t x = channel.w - 2; x < channel.w; x++) { |
414 | 1.01M | PredictionResult res = |
415 | 1.01M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
416 | 1.01M | tree_lookup, references); |
417 | 1.01M | uint64_t v = |
418 | 1.01M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
419 | 1.01M | p[x] = make_pixel(v, res.multiplier, res.guess); |
420 | 1.01M | } |
421 | 508k | } else { |
422 | 2.23M | for (size_t x = 0; x < channel.w; x++) { |
423 | 2.12M | PredictionResult res = |
424 | 2.12M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, |
425 | 2.12M | tree_lookup, references); |
426 | 2.12M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( |
427 | 2.12M | res.context, br); |
428 | 2.12M | p[x] = make_pixel(v, res.multiplier, res.guess); |
429 | 2.12M | } |
430 | 108k | } |
431 | 616k | } |
432 | 21.2k | } else { |
433 | 10.3k | JXL_DEBUG_V(8, "Slowest track."); |
434 | 10.3k | MATreeLookup tree_lookup(tree); |
435 | 10.3k | Properties properties = Properties(num_props); |
436 | 10.3k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); |
437 | 10.3k | JXL_ASSIGN_OR_RETURN( |
438 | 10.3k | Channel references, |
439 | 10.3k | Channel::Create(memory_manager, |
440 | 10.3k | properties.size() - kNumNonrefProperties, channel.w)); |
441 | 10.3k | weighted::State wp_state(wp_header, channel.w, channel.h); |
442 | 265k | for (size_t y = 0; y < channel.h; y++) { |
443 | 255k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
444 | 255k | InitPropsRow(&properties, static_props, y); |
445 | 255k | PrecomputeReferences(channel, y, *image, chan, &references); |
446 | 255k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { |
447 | 599k | for (size_t x = 0; x < 2; x++) { |
448 | 399k | PredictionResult res = |
449 | 399k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
450 | 399k | tree_lookup, references, &wp_state); |
451 | 399k | uint64_t v = |
452 | 399k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
453 | 399k | p[x] = make_pixel(v, res.multiplier, res.guess); |
454 | 399k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
455 | 399k | } |
456 | 15.7M | for (size_t x = 2; x < channel.w - 2; x++) { |
457 | 15.5M | PredictionResult res = |
458 | 15.5M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, |
459 | 15.5M | tree_lookup, references, &wp_state); |
460 | 15.5M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( |
461 | 15.5M | res.context, br); |
462 | 15.5M | p[x] = make_pixel(v, res.multiplier, res.guess); |
463 | 15.5M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
464 | 15.5M | } |
465 | 599k | for (size_t x = channel.w - 2; x < channel.w; x++) { |
466 | 399k | PredictionResult res = |
467 | 399k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
468 | 399k | tree_lookup, references, &wp_state); |
469 | 399k | uint64_t v = |
470 | 399k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
471 | 399k | p[x] = make_pixel(v, res.multiplier, res.guess); |
472 | 399k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
473 | 399k | } |
474 | 199k | } else { |
475 | 2.46M | for (size_t x = 0; x < channel.w; x++) { |
476 | 2.40M | PredictionResult res = |
477 | 2.40M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, |
478 | 2.40M | tree_lookup, references, &wp_state); |
479 | 2.40M | uint64_t v = |
480 | 2.40M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); |
481 | 2.40M | p[x] = make_pixel(v, res.multiplier, res.guess); |
482 | 2.40M | wp_state.UpdateErrors(p[x], x, y, channel.w); |
483 | 2.40M | } |
484 | 55.4k | } |
485 | 255k | } |
486 | 10.3k | } |
487 | 33.2k | return true; |
488 | 33.2k | } 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 | 37.9k | uint32_t &fl_v) { | 157 | 37.9k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 37.9k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 37.9k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 37.9k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 37.9k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 37.9k | bool tree_has_wp_prop_or_pred = false; | 168 | 37.9k | bool is_wp_only = false; | 169 | 37.9k | bool is_gradient_only = false; | 170 | 37.9k | size_t num_props; | 171 | 37.9k | FlatTree tree = | 172 | 37.9k | FilterTree(global_tree, static_props, &num_props, | 173 | 37.9k | &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 | 51.3k | for (auto &node : tree) { | 178 | 51.3k | if (node.property0 == -1) { | 179 | 47.9k | node.childID = context_map[node.childID]; | 180 | 47.9k | } | 181 | 51.3k | } | 182 | | | 183 | 37.9k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 37.9k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 37.9k | pixel_type_w offset) -> pixel_type { | 188 | 37.9k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 37.9k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 37.9k | return val * multiplier + offset; | 192 | 37.9k | }; | 193 | | | 194 | 37.9k | if (tree.size() == 1) { | 195 | | // special optimized case: no meta-adaptation, so no need | 196 | | // to compute properties. | 197 | 35.3k | Predictor predictor = tree[0].predictor; | 198 | 35.3k | int64_t offset = tree[0].predictor_offset; | 199 | 35.3k | int32_t multiplier = tree[0].multiplier; | 200 | 35.3k | size_t ctx_id = tree[0].childID; | 201 | 35.3k | if (predictor == Predictor::Zero) { | 202 | 28.3k | uint32_t value; | 203 | 28.3k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 204 | 28.3k | channel.w * channel.h)) { | 205 | | // Special-case: histogram has a single symbol, with no extra bits, and | 206 | | // we use ANS mode. | 207 | 10.6k | JXL_DEBUG_V(8, "Fastest track."); | 208 | 10.6k | pixel_type v = make_pixel(value, multiplier, offset); | 209 | 384k | for (size_t y = 0; y < channel.h; y++) { | 210 | 374k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 211 | 374k | std::fill(r, r + channel.w, v); | 212 | 374k | } | 213 | 17.7k | } else { | 214 | 17.7k | JXL_DEBUG_V(8, "Fast track."); | 215 | 17.7k | if (multiplier == 1 && offset == 0) { | 216 | 328k | for (size_t y = 0; y < channel.h; y++) { | 217 | 323k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 218 | 39.0M | for (size_t x = 0; x < channel.w; x++) { | 219 | 38.7M | uint32_t v = | 220 | 38.7M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 221 | 38.7M | r[x] = UnpackSigned(v); | 222 | 38.7M | } | 223 | 323k | } | 224 | 11.7k | } else { | 225 | 370k | for (size_t y = 0; y < channel.h; y++) { | 226 | 359k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 227 | 24.3M | for (size_t x = 0; x < channel.w; x++) { | 228 | 23.9M | uint32_t v = | 229 | 23.9M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 230 | 23.9M | br); | 231 | 23.9M | r[x] = make_pixel(v, multiplier, offset); | 232 | 23.9M | } | 233 | 359k | } | 234 | 11.7k | } | 235 | 17.7k | } | 236 | 28.3k | return true; | 237 | 28.3k | } else if (uses_lz77 && predictor == Predictor::Gradient && offset == 0 && | 238 | 990 | multiplier == 1 && reader->IsHuffRleOnly()) { | 239 | 194 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); | 240 | 194 | pixel_type_w sv = UnpackSigned(fl_v); | 241 | 7.83k | for (size_t y = 0; y < channel.h; y++) { | 242 | 7.64k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 243 | 7.64k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 244 | 7.64k | const pixel_type *JXL_RESTRICT rtopleft = | 245 | 7.64k | (y ? channel.Row(y - 1) - 1 : r - 1); | 246 | 7.64k | pixel_type_w guess_0 = (y ? rtop[0] : 0); | 247 | 7.64k | if (fl_run == 0) { | 248 | 1.61k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 249 | 1.61k | &fl_run); | 250 | 1.61k | sv = UnpackSigned(fl_v); | 251 | 6.02k | } else { | 252 | 6.02k | fl_run--; | 253 | 6.02k | } | 254 | 7.64k | r[0] = sv + guess_0; | 255 | 253k | for (size_t x = 1; x < channel.w; x++) { | 256 | 245k | pixel_type left = r[x - 1]; | 257 | 245k | pixel_type top = rtop[x]; | 258 | 245k | pixel_type topleft = rtopleft[x]; | 259 | 245k | pixel_type_w guess = ClampedGradient(top, left, topleft); | 260 | 245k | if (!fl_run) { | 261 | 53.1k | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 262 | 53.1k | &fl_run); | 263 | 53.1k | sv = UnpackSigned(fl_v); | 264 | 192k | } else { | 265 | 192k | fl_run--; | 266 | 192k | } | 267 | 245k | r[x] = sv + guess; | 268 | 245k | } | 269 | 7.64k | } | 270 | 194 | return true; | 271 | 6.77k | } else if (predictor == Predictor::Gradient && offset == 0 && | 272 | 796 | multiplier == 1) { | 273 | 633 | JXL_DEBUG_V(8, "Gradient very fast track."); | 274 | 633 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 275 | 10.3k | for (size_t y = 0; y < channel.h; y++) { | 276 | 9.69k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 277 | 268k | for (size_t x = 0; x < channel.w; x++) { | 278 | 258k | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 279 | 258k | pixel_type top = (y ? *(r + x - onerow) : left); | 280 | 258k | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 281 | 258k | pixel_type guess = ClampedGradient(top, left, topleft); | 282 | 258k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 283 | 258k | ctx_id, br); | 284 | 258k | r[x] = make_pixel(v, 1, guess); | 285 | 258k | } | 286 | 9.69k | } | 287 | 633 | return true; | 288 | 633 | } | 289 | 35.3k | } | 290 | | | 291 | | // Check if this tree is a WP-only tree with a small enough property value | 292 | | // range. | 293 | 8.73k | if (is_wp_only) { | 294 | 360 | is_wp_only = TreeToLookupTable(tree, tree_lut); | 295 | 360 | } | 296 | 8.73k | if (is_gradient_only) { | 297 | 718 | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 298 | 718 | } | 299 | | | 300 | 8.73k | if (is_gradient_only) { | 301 | 139 | JXL_DEBUG_V(8, "Gradient fast track."); | 302 | 139 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 303 | 3.07k | for (size_t y = 0; y < channel.h; y++) { | 304 | 2.93k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 305 | 153k | for (size_t x = 0; x < channel.w; x++) { | 306 | 150k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 307 | 150k | pixel_type_w top = (y ? *(r + x - onerow) : left); | 308 | 150k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 309 | 150k | int32_t guess = ClampedGradient(top, left, topleft); | 310 | 150k | uint32_t pos = | 311 | 150k | kPropRangeFast + | 312 | 150k | std::min<pixel_type_w>( | 313 | 150k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 314 | 150k | kPropRangeFast - 1); | 315 | 150k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 316 | 150k | uint64_t v = | 317 | 150k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 318 | 150k | r[x] = make_pixel(v, 1, guess); | 319 | 150k | } | 320 | 2.93k | } | 321 | 8.59k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 322 | 0 | JXL_DEBUG_V(8, "WP fast track."); | 323 | 0 | weighted::State wp_state(wp_header, channel.w, channel.h); | 324 | 0 | Properties properties(1); | 325 | 0 | for (size_t y = 0; y < channel.h; y++) { | 326 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 327 | 0 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 328 | 0 | const pixel_type *JXL_RESTRICT rtoptop = | 329 | 0 | (y > 1 ? channel.Row(y - 2) : rtop); | 330 | 0 | const pixel_type *JXL_RESTRICT rtopleft = | 331 | 0 | (y ? channel.Row(y - 1) - 1 : r - 1); | 332 | 0 | const pixel_type *JXL_RESTRICT rtopright = | 333 | 0 | (y ? channel.Row(y - 1) + 1 : r - 1); | 334 | 0 | size_t x = 0; | 335 | 0 | { | 336 | 0 | size_t offset = 0; | 337 | 0 | pixel_type_w left = y ? rtop[x] : 0; | 338 | 0 | pixel_type_w toptop = y ? rtoptop[x] : 0; | 339 | 0 | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 340 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 341 | 0 | x, y, channel.w, left, left, topright, left, toptop, &properties, | 342 | 0 | offset); | 343 | 0 | uint32_t pos = | 344 | 0 | kPropRangeFast + | 345 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 346 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 347 | 0 | uint64_t v = | 348 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 349 | 0 | r[x] = make_pixel(v, 1, guess); | 350 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 351 | 0 | } | 352 | 0 | for (x = 1; x + 1 < channel.w; x++) { | 353 | 0 | size_t offset = 0; | 354 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 355 | 0 | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 356 | 0 | rtoptop[x], &properties, offset); | 357 | 0 | uint32_t pos = | 358 | 0 | kPropRangeFast + | 359 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 360 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 361 | 0 | uint64_t v = | 362 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 363 | 0 | r[x] = make_pixel(v, 1, guess); | 364 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 365 | 0 | } | 366 | 0 | { | 367 | 0 | size_t offset = 0; | 368 | 0 | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 369 | 0 | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 370 | 0 | rtoptop[x], &properties, offset); | 371 | 0 | uint32_t pos = | 372 | 0 | kPropRangeFast + | 373 | 0 | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 374 | 0 | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 375 | 0 | uint64_t v = | 376 | 0 | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 377 | 0 | r[x] = make_pixel(v, 1, guess); | 378 | 0 | wp_state.UpdateErrors(r[x], x, y, channel.w); | 379 | 0 | } | 380 | 0 | } | 381 | 8.59k | } else if (!tree_has_wp_prop_or_pred) { | 382 | | // special optimized case: the weighted predictor and its properties are not | 383 | | // used, so no need to compute weights and properties. | 384 | 7.45k | JXL_DEBUG_V(8, "Slow track."); | 385 | 7.45k | MATreeLookup tree_lookup(tree); | 386 | 7.45k | Properties properties = Properties(num_props); | 387 | 7.45k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 388 | 7.45k | JXL_ASSIGN_OR_RETURN( | 389 | 7.45k | Channel references, | 390 | 7.45k | Channel::Create(memory_manager, | 391 | 7.45k | properties.size() - kNumNonrefProperties, channel.w)); | 392 | 180k | for (size_t y = 0; y < channel.h; y++) { | 393 | 172k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 394 | 172k | PrecomputeReferences(channel, y, *image, chan, &references); | 395 | 172k | InitPropsRow(&properties, static_props, y); | 396 | 172k | if (y > 1 && channel.w > 8 && references.w == 0) { | 397 | 435k | for (size_t x = 0; x < 2; x++) { | 398 | 290k | PredictionResult res = | 399 | 290k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 400 | 290k | tree_lookup, references); | 401 | 290k | uint64_t v = | 402 | 290k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 403 | 290k | p[x] = make_pixel(v, res.multiplier, res.guess); | 404 | 290k | } | 405 | 19.0M | for (size_t x = 2; x < channel.w - 2; x++) { | 406 | 18.9M | PredictionResult res = | 407 | 18.9M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 408 | 18.9M | tree_lookup, references); | 409 | 18.9M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 410 | 18.9M | res.context, br); | 411 | 18.9M | p[x] = make_pixel(v, res.multiplier, res.guess); | 412 | 18.9M | } | 413 | 435k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 414 | 290k | PredictionResult res = | 415 | 290k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 416 | 290k | tree_lookup, references); | 417 | 290k | uint64_t v = | 418 | 290k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 419 | 290k | p[x] = make_pixel(v, res.multiplier, res.guess); | 420 | 290k | } | 421 | 145k | } else { | 422 | 507k | for (size_t x = 0; x < channel.w; x++) { | 423 | 479k | PredictionResult res = | 424 | 479k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 425 | 479k | tree_lookup, references); | 426 | 479k | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 427 | 479k | res.context, br); | 428 | 479k | p[x] = make_pixel(v, res.multiplier, res.guess); | 429 | 479k | } | 430 | 27.4k | } | 431 | 172k | } | 432 | 7.45k | } else { | 433 | 1.14k | JXL_DEBUG_V(8, "Slowest track."); | 434 | 1.14k | MATreeLookup tree_lookup(tree); | 435 | 1.14k | Properties properties = Properties(num_props); | 436 | 1.14k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 437 | 1.14k | JXL_ASSIGN_OR_RETURN( | 438 | 1.14k | Channel references, | 439 | 1.14k | Channel::Create(memory_manager, | 440 | 1.14k | properties.size() - kNumNonrefProperties, channel.w)); | 441 | 1.14k | weighted::State wp_state(wp_header, channel.w, channel.h); | 442 | 13.9k | for (size_t y = 0; y < channel.h; y++) { | 443 | 12.8k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 444 | 12.8k | InitPropsRow(&properties, static_props, y); | 445 | 12.8k | PrecomputeReferences(channel, y, *image, chan, &references); | 446 | 12.8k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 447 | 0 | for (size_t x = 0; x < 2; x++) { | 448 | 0 | PredictionResult res = | 449 | 0 | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 450 | 0 | tree_lookup, references, &wp_state); | 451 | 0 | uint64_t v = | 452 | 0 | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 453 | 0 | p[x] = make_pixel(v, res.multiplier, res.guess); | 454 | 0 | wp_state.UpdateErrors(p[x], x, y, channel.w); | 455 | 0 | } | 456 | 0 | for (size_t x = 2; x < channel.w - 2; x++) { | 457 | 0 | PredictionResult res = | 458 | 0 | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 459 | 0 | tree_lookup, references, &wp_state); | 460 | 0 | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 461 | 0 | res.context, br); | 462 | 0 | p[x] = make_pixel(v, res.multiplier, res.guess); | 463 | 0 | wp_state.UpdateErrors(p[x], x, y, channel.w); | 464 | 0 | } | 465 | 0 | for (size_t x = channel.w - 2; x < channel.w; 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 | 12.8k | } else { | 475 | 521k | for (size_t x = 0; x < channel.w; x++) { | 476 | 508k | PredictionResult res = | 477 | 508k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 478 | 508k | tree_lookup, references, &wp_state); | 479 | 508k | uint64_t v = | 480 | 508k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 481 | 508k | p[x] = make_pixel(v, res.multiplier, res.guess); | 482 | 508k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 483 | 508k | } | 484 | 12.8k | } | 485 | 12.8k | } | 486 | 1.14k | } | 487 | 8.73k | return true; | 488 | 8.73k | } |
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 | 316k | uint32_t &fl_v) { | 157 | 316k | JxlMemoryManager *memory_manager = image->memory_manager(); | 158 | 316k | Channel &channel = image->channel[chan]; | 159 | | | 160 | 316k | std::array<pixel_type, kNumStaticProperties> static_props = { | 161 | 316k | {chan, static_cast<int>(group_id)}}; | 162 | | // TODO(veluca): filter the tree according to static_props. | 163 | | | 164 | | // zero pixel channel? could happen | 165 | 316k | if (channel.w == 0 || channel.h == 0) return true; | 166 | | | 167 | 316k | bool tree_has_wp_prop_or_pred = false; | 168 | 316k | bool is_wp_only = false; | 169 | 316k | bool is_gradient_only = false; | 170 | 316k | size_t num_props; | 171 | 316k | FlatTree tree = | 172 | 316k | FilterTree(global_tree, static_props, &num_props, | 173 | 316k | &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 | 463k | for (auto &node : tree) { | 178 | 463k | if (node.property0 == -1) { | 179 | 427k | node.childID = context_map[node.childID]; | 180 | 427k | } | 181 | 463k | } | 182 | | | 183 | 316k | JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size()); | 184 | | | 185 | | // MAANS decode | 186 | 316k | const auto make_pixel = [](uint64_t v, pixel_type multiplier, | 187 | 316k | pixel_type_w offset) -> pixel_type { | 188 | 316k | JXL_DASSERT((v & 0xFFFFFFFF) == v); | 189 | 316k | pixel_type_w val = static_cast<pixel_type_w>(UnpackSigned(v)); | 190 | | // if it overflows, it overflows, and we have a problem anyway | 191 | 316k | return val * multiplier + offset; | 192 | 316k | }; | 193 | | | 194 | 316k | if (tree.size() == 1) { | 195 | | // special optimized case: no meta-adaptation, so no need | 196 | | // to compute properties. | 197 | 306k | Predictor predictor = tree[0].predictor; | 198 | 306k | int64_t offset = tree[0].predictor_offset; | 199 | 306k | int32_t multiplier = tree[0].multiplier; | 200 | 306k | size_t ctx_id = tree[0].childID; | 201 | 306k | if (predictor == Predictor::Zero) { | 202 | 290k | uint32_t value; | 203 | 290k | if (reader->IsSingleValueAndAdvance(ctx_id, &value, | 204 | 290k | channel.w * channel.h)) { | 205 | | // Special-case: histogram has a single symbol, with no extra bits, and | 206 | | // we use ANS mode. | 207 | 120k | JXL_DEBUG_V(8, "Fastest track."); | 208 | 120k | pixel_type v = make_pixel(value, multiplier, offset); | 209 | 3.74M | for (size_t y = 0; y < channel.h; y++) { | 210 | 3.62M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 211 | 3.62M | std::fill(r, r + channel.w, v); | 212 | 3.62M | } | 213 | 169k | } else { | 214 | 169k | JXL_DEBUG_V(8, "Fast track."); | 215 | 169k | if (multiplier == 1 && offset == 0) { | 216 | 2.36M | for (size_t y = 0; y < channel.h; y++) { | 217 | 2.21M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 218 | 136M | for (size_t x = 0; x < channel.w; x++) { | 219 | 134M | uint32_t v = | 220 | 134M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 221 | 134M | r[x] = UnpackSigned(v); | 222 | 134M | } | 223 | 2.21M | } | 224 | 147k | } else { | 225 | 1.08M | for (size_t y = 0; y < channel.h; y++) { | 226 | 1.06M | pixel_type *JXL_RESTRICT r = channel.Row(y); | 227 | 132M | for (size_t x = 0; x < channel.w; x++) { | 228 | 131M | uint32_t v = | 229 | 131M | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, | 230 | 131M | br); | 231 | 131M | r[x] = make_pixel(v, multiplier, offset); | 232 | 131M | } | 233 | 1.06M | } | 234 | 22.7k | } | 235 | 169k | } | 236 | 290k | return true; | 237 | 290k | } else if (uses_lz77 && predictor == Predictor::Gradient && offset == 0 && | 238 | 0 | multiplier == 1 && reader->IsHuffRleOnly()) { | 239 | 0 | JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track."); | 240 | 0 | pixel_type_w sv = UnpackSigned(fl_v); | 241 | 0 | for (size_t y = 0; y < channel.h; y++) { | 242 | 0 | pixel_type *JXL_RESTRICT r = channel.Row(y); | 243 | 0 | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 244 | 0 | const pixel_type *JXL_RESTRICT rtopleft = | 245 | 0 | (y ? channel.Row(y - 1) - 1 : r - 1); | 246 | 0 | pixel_type_w guess_0 = (y ? rtop[0] : 0); | 247 | 0 | if (fl_run == 0) { | 248 | 0 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 249 | 0 | &fl_run); | 250 | 0 | sv = UnpackSigned(fl_v); | 251 | 0 | } else { | 252 | 0 | fl_run--; | 253 | 0 | } | 254 | 0 | r[0] = sv + guess_0; | 255 | 0 | for (size_t x = 1; x < channel.w; x++) { | 256 | 0 | pixel_type left = r[x - 1]; | 257 | 0 | pixel_type top = rtop[x]; | 258 | 0 | pixel_type topleft = rtopleft[x]; | 259 | 0 | pixel_type_w guess = ClampedGradient(top, left, topleft); | 260 | 0 | if (!fl_run) { | 261 | 0 | reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &fl_v, | 262 | 0 | &fl_run); | 263 | 0 | sv = UnpackSigned(fl_v); | 264 | 0 | } else { | 265 | 0 | fl_run--; | 266 | 0 | } | 267 | 0 | r[x] = sv + guess; | 268 | 0 | } | 269 | 0 | } | 270 | 0 | return true; | 271 | 16.3k | } else if (predictor == Predictor::Gradient && offset == 0 && | 272 | 1.77k | multiplier == 1) { | 273 | 1.55k | JXL_DEBUG_V(8, "Gradient very fast track."); | 274 | 1.55k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 275 | 42.7k | for (size_t y = 0; y < channel.h; y++) { | 276 | 41.1k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 277 | 2.42M | for (size_t x = 0; x < channel.w; x++) { | 278 | 2.38M | pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 279 | 2.38M | pixel_type top = (y ? *(r + x - onerow) : left); | 280 | 2.38M | pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left); | 281 | 2.38M | pixel_type guess = ClampedGradient(top, left, topleft); | 282 | 2.38M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 283 | 2.38M | ctx_id, br); | 284 | 2.38M | r[x] = make_pixel(v, 1, guess); | 285 | 2.38M | } | 286 | 41.1k | } | 287 | 1.55k | return true; | 288 | 1.55k | } | 289 | 306k | } | 290 | | | 291 | | // Check if this tree is a WP-only tree with a small enough property value | 292 | | // range. | 293 | 24.4k | if (is_wp_only) { | 294 | 4.29k | is_wp_only = TreeToLookupTable(tree, tree_lut); | 295 | 4.29k | } | 296 | 24.4k | if (is_gradient_only) { | 297 | 1.08k | is_gradient_only = TreeToLookupTable(tree, tree_lut); | 298 | 1.08k | } | 299 | | | 300 | 24.4k | if (is_gradient_only) { | 301 | 611 | JXL_DEBUG_V(8, "Gradient fast track."); | 302 | 611 | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 303 | 13.1k | for (size_t y = 0; y < channel.h; y++) { | 304 | 12.5k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 305 | 418k | for (size_t x = 0; x < channel.w; x++) { | 306 | 406k | pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0); | 307 | 406k | pixel_type_w top = (y ? *(r + x - onerow) : left); | 308 | 406k | pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left); | 309 | 406k | int32_t guess = ClampedGradient(top, left, topleft); | 310 | 406k | uint32_t pos = | 311 | 406k | kPropRangeFast + | 312 | 406k | std::min<pixel_type_w>( | 313 | 406k | std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft), | 314 | 406k | kPropRangeFast - 1); | 315 | 406k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 316 | 406k | uint64_t v = | 317 | 406k | reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br); | 318 | 406k | r[x] = make_pixel(v, 1, guess); | 319 | 406k | } | 320 | 12.5k | } | 321 | 23.8k | } else if (!uses_lz77 && is_wp_only && channel.w > 8) { | 322 | 887 | JXL_DEBUG_V(8, "WP fast track."); | 323 | 887 | weighted::State wp_state(wp_header, channel.w, channel.h); | 324 | 887 | Properties properties(1); | 325 | 22.4k | for (size_t y = 0; y < channel.h; y++) { | 326 | 21.5k | pixel_type *JXL_RESTRICT r = channel.Row(y); | 327 | 21.5k | const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1); | 328 | 21.5k | const pixel_type *JXL_RESTRICT rtoptop = | 329 | 21.5k | (y > 1 ? channel.Row(y - 2) : rtop); | 330 | 21.5k | const pixel_type *JXL_RESTRICT rtopleft = | 331 | 21.5k | (y ? channel.Row(y - 1) - 1 : r - 1); | 332 | 21.5k | const pixel_type *JXL_RESTRICT rtopright = | 333 | 21.5k | (y ? channel.Row(y - 1) + 1 : r - 1); | 334 | 21.5k | size_t x = 0; | 335 | 21.5k | { | 336 | 21.5k | size_t offset = 0; | 337 | 21.5k | pixel_type_w left = y ? rtop[x] : 0; | 338 | 21.5k | pixel_type_w toptop = y ? rtoptop[x] : 0; | 339 | 21.5k | pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left); | 340 | 21.5k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 341 | 21.5k | x, y, channel.w, left, left, topright, left, toptop, &properties, | 342 | 21.5k | offset); | 343 | 21.5k | uint32_t pos = | 344 | 21.5k | kPropRangeFast + | 345 | 21.5k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 346 | 21.5k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 347 | 21.5k | uint64_t v = | 348 | 21.5k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 349 | 21.5k | r[x] = make_pixel(v, 1, guess); | 350 | 21.5k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 351 | 21.5k | } | 352 | 2.03M | for (x = 1; x + 1 < channel.w; x++) { | 353 | 2.01M | size_t offset = 0; | 354 | 2.01M | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 355 | 2.01M | x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x], | 356 | 2.01M | rtoptop[x], &properties, offset); | 357 | 2.01M | uint32_t pos = | 358 | 2.01M | kPropRangeFast + | 359 | 2.01M | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 360 | 2.01M | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 361 | 2.01M | uint64_t v = | 362 | 2.01M | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 363 | 2.01M | r[x] = make_pixel(v, 1, guess); | 364 | 2.01M | wp_state.UpdateErrors(r[x], x, y, channel.w); | 365 | 2.01M | } | 366 | 21.5k | { | 367 | 21.5k | size_t offset = 0; | 368 | 21.5k | int32_t guess = wp_state.Predict</*compute_properties=*/true>( | 369 | 21.5k | x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x], | 370 | 21.5k | rtoptop[x], &properties, offset); | 371 | 21.5k | uint32_t pos = | 372 | 21.5k | kPropRangeFast + | 373 | 21.5k | jxl::Clamp1(properties[0], -kPropRangeFast, kPropRangeFast - 1); | 374 | 21.5k | uint32_t ctx_id = tree_lut.context_lookup[pos]; | 375 | 21.5k | uint64_t v = | 376 | 21.5k | reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br); | 377 | 21.5k | r[x] = make_pixel(v, 1, guess); | 378 | 21.5k | wp_state.UpdateErrors(r[x], x, y, channel.w); | 379 | 21.5k | } | 380 | 21.5k | } | 381 | 22.9k | } else if (!tree_has_wp_prop_or_pred) { | 382 | | // special optimized case: the weighted predictor and its properties are not | 383 | | // used, so no need to compute weights and properties. | 384 | 13.7k | JXL_DEBUG_V(8, "Slow track."); | 385 | 13.7k | MATreeLookup tree_lookup(tree); | 386 | 13.7k | Properties properties = Properties(num_props); | 387 | 13.7k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 388 | 13.7k | JXL_ASSIGN_OR_RETURN( | 389 | 13.7k | Channel references, | 390 | 13.7k | Channel::Create(memory_manager, | 391 | 13.7k | properties.size() - kNumNonrefProperties, channel.w)); | 392 | 457k | for (size_t y = 0; y < channel.h; y++) { | 393 | 444k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 394 | 444k | PrecomputeReferences(channel, y, *image, chan, &references); | 395 | 444k | InitPropsRow(&properties, static_props, y); | 396 | 444k | if (y > 1 && channel.w > 8 && references.w == 0) { | 397 | 1.08M | for (size_t x = 0; x < 2; x++) { | 398 | 726k | PredictionResult res = | 399 | 726k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 400 | 726k | tree_lookup, references); | 401 | 726k | uint64_t v = | 402 | 726k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 403 | 726k | p[x] = make_pixel(v, res.multiplier, res.guess); | 404 | 726k | } | 405 | 36.7M | for (size_t x = 2; x < channel.w - 2; x++) { | 406 | 36.3M | PredictionResult res = | 407 | 36.3M | PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y, | 408 | 36.3M | tree_lookup, references); | 409 | 36.3M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 410 | 36.3M | res.context, br); | 411 | 36.3M | p[x] = make_pixel(v, res.multiplier, res.guess); | 412 | 36.3M | } | 413 | 1.08M | for (size_t x = channel.w - 2; x < channel.w; x++) { | 414 | 726k | PredictionResult res = | 415 | 726k | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 416 | 726k | tree_lookup, references); | 417 | 726k | uint64_t v = | 418 | 726k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 419 | 726k | p[x] = make_pixel(v, res.multiplier, res.guess); | 420 | 726k | } | 421 | 363k | } else { | 422 | 1.72M | for (size_t x = 0; x < channel.w; x++) { | 423 | 1.64M | PredictionResult res = | 424 | 1.64M | PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y, | 425 | 1.64M | tree_lookup, references); | 426 | 1.64M | uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>( | 427 | 1.64M | res.context, br); | 428 | 1.64M | p[x] = make_pixel(v, res.multiplier, res.guess); | 429 | 1.64M | } | 430 | 81.0k | } | 431 | 444k | } | 432 | 13.7k | } else { | 433 | 9.20k | JXL_DEBUG_V(8, "Slowest track."); | 434 | 9.20k | MATreeLookup tree_lookup(tree); | 435 | 9.20k | Properties properties = Properties(num_props); | 436 | 9.20k | const ptrdiff_t onerow = channel.plane.PixelsPerRow(); | 437 | 9.20k | JXL_ASSIGN_OR_RETURN( | 438 | 9.20k | Channel references, | 439 | 9.20k | Channel::Create(memory_manager, | 440 | 9.20k | properties.size() - kNumNonrefProperties, channel.w)); | 441 | 9.20k | weighted::State wp_state(wp_header, channel.w, channel.h); | 442 | 251k | for (size_t y = 0; y < channel.h; y++) { | 443 | 242k | pixel_type *JXL_RESTRICT p = channel.Row(y); | 444 | 242k | InitPropsRow(&properties, static_props, y); | 445 | 242k | PrecomputeReferences(channel, y, *image, chan, &references); | 446 | 242k | if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) { | 447 | 599k | for (size_t x = 0; x < 2; x++) { | 448 | 399k | PredictionResult res = | 449 | 399k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 450 | 399k | tree_lookup, references, &wp_state); | 451 | 399k | uint64_t v = | 452 | 399k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 453 | 399k | p[x] = make_pixel(v, res.multiplier, res.guess); | 454 | 399k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 455 | 399k | } | 456 | 15.7M | for (size_t x = 2; x < channel.w - 2; x++) { | 457 | 15.5M | PredictionResult res = | 458 | 15.5M | PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y, | 459 | 15.5M | tree_lookup, references, &wp_state); | 460 | 15.5M | uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>( | 461 | 15.5M | res.context, br); | 462 | 15.5M | p[x] = make_pixel(v, res.multiplier, res.guess); | 463 | 15.5M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 464 | 15.5M | } | 465 | 599k | for (size_t x = channel.w - 2; x < channel.w; x++) { | 466 | 399k | PredictionResult res = | 467 | 399k | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 468 | 399k | tree_lookup, references, &wp_state); | 469 | 399k | uint64_t v = | 470 | 399k | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 471 | 399k | p[x] = make_pixel(v, res.multiplier, res.guess); | 472 | 399k | wp_state.UpdateErrors(p[x], x, y, channel.w); | 473 | 399k | } | 474 | 199k | } else { | 475 | 1.94M | for (size_t x = 0; x < channel.w; x++) { | 476 | 1.90M | PredictionResult res = | 477 | 1.90M | PredictTreeWP(&properties, channel.w, p + x, onerow, x, y, | 478 | 1.90M | tree_lookup, references, &wp_state); | 479 | 1.90M | uint64_t v = | 480 | 1.90M | reader->ReadHybridUintClustered<uses_lz77>(res.context, br); | 481 | 1.90M | p[x] = make_pixel(v, res.multiplier, res.guess); | 482 | 1.90M | wp_state.UpdateErrors(p[x], x, y, channel.w); | 483 | 1.90M | } | 484 | 42.6k | } | 485 | 242k | } | 486 | 9.20k | } | 487 | 24.4k | return true; | 488 | 24.4k | } |
|
489 | | } // namespace detail |
490 | | |
491 | | Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader, |
492 | | const std::vector<uint8_t> &context_map, |
493 | | const Tree &global_tree, |
494 | | const weighted::Header &wp_header, |
495 | | pixel_type chan, size_t group_id, |
496 | | TreeLut<uint8_t, false, false> &tree_lut, |
497 | | Image *image, uint32_t &fl_run, |
498 | 354k | uint32_t &fl_v) { |
499 | 354k | if (reader->UsesLZ77()) { |
500 | 37.9k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>( |
501 | 37.9k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
502 | 37.9k | tree_lut, image, fl_run, fl_v); |
503 | 316k | } else { |
504 | 316k | return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>( |
505 | 316k | br, reader, context_map, global_tree, wp_header, chan, group_id, |
506 | 316k | tree_lut, image, fl_run, fl_v); |
507 | 316k | } |
508 | 354k | } |
509 | | |
510 | 183k | GroupHeader::GroupHeader() { Bundle::Init(this); } |
511 | | |
512 | | Status ValidateChannelDimensions(const Image &image, |
513 | 42.4k | const ModularOptions &options) { |
514 | 42.4k | size_t nb_channels = image.channel.size(); |
515 | 84.9k | for (bool is_dc : {true, false}) { |
516 | 84.9k | size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1); |
517 | 84.9k | size_t c = image.nb_meta_channels; |
518 | 824k | for (; c < nb_channels; c++) { |
519 | 742k | const Channel &ch = image.channel[c]; |
520 | 742k | if (ch.w > options.group_dim || ch.h > options.group_dim) break; |
521 | 742k | } |
522 | 106k | for (; c < nb_channels; c++) { |
523 | 21.6k | const Channel &ch = image.channel[c]; |
524 | 21.6k | if (ch.w == 0 || ch.h == 0) continue; // skip empty |
525 | 20.8k | bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3; |
526 | 20.8k | if (is_dc_channel != is_dc) continue; |
527 | 10.4k | size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift); |
528 | 10.4k | if (tile_dim == 0) { |
529 | 3 | return JXL_FAILURE("Inconsistent transforms"); |
530 | 3 | } |
531 | 10.4k | } |
532 | 84.9k | } |
533 | 42.4k | return true; |
534 | 42.4k | } |
535 | | |
536 | | Status ModularDecode(BitReader *br, Image &image, GroupHeader &header, |
537 | | size_t group_id, ModularOptions *options, |
538 | | const Tree *global_tree, const ANSCode *global_code, |
539 | | const std::vector<uint8_t> *global_ctx_map, |
540 | 48.5k | const bool allow_truncated_group) { |
541 | 48.5k | if (image.channel.empty()) return true; |
542 | 42.0k | JxlMemoryManager *memory_manager = image.memory_manager(); |
543 | | |
544 | | // decode transforms |
545 | 42.0k | Status status = Bundle::Read(br, &header); |
546 | 42.0k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status); |
547 | 40.9k | if (status.IsFatalError()) return status; |
548 | 40.9k | if (!br->AllReadsWithinBounds()) { |
549 | | // Don't do/undo transforms if header is incomplete. |
550 | 0 | header.transforms.clear(); |
551 | 0 | image.transform = header.transforms; |
552 | 0 | for (auto &ch : image.channel) { |
553 | 0 | ZeroFillImage(&ch.plane); |
554 | 0 | } |
555 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun before ModularDecode"); |
556 | 0 | } |
557 | | |
558 | 40.9k | JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ", |
559 | 40.9k | header.transforms.size()); |
560 | 40.9k | image.transform = header.transforms; |
561 | 40.9k | for (Transform &transform : image.transform) { |
562 | 24.2k | JXL_RETURN_IF_ERROR(transform.MetaApply(image)); |
563 | 24.2k | } |
564 | 40.7k | if (image.error) { |
565 | 0 | return JXL_FAILURE("Corrupt file. Aborting."); |
566 | 0 | } |
567 | 40.7k | JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options)); |
568 | | |
569 | 40.7k | size_t nb_channels = image.channel.size(); |
570 | | |
571 | 40.7k | size_t num_chans = 0; |
572 | 40.7k | size_t distance_multiplier = 0; |
573 | 411k | for (size_t i = 0; i < nb_channels; i++) { |
574 | 371k | Channel &channel = image.channel[i]; |
575 | 371k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
576 | 367k | channel.h > options->max_chan_size)) { |
577 | 1.17k | break; |
578 | 1.17k | } |
579 | 370k | if (!channel.w || !channel.h) { |
580 | 5.28k | continue; // skip empty channels |
581 | 5.28k | } |
582 | 365k | if (channel.w > distance_multiplier) { |
583 | 63.2k | distance_multiplier = channel.w; |
584 | 63.2k | } |
585 | 365k | num_chans++; |
586 | 365k | } |
587 | 40.7k | if (num_chans == 0) return true; |
588 | | |
589 | 40.2k | size_t next_channel = 0; |
590 | 40.2k | auto scope_guard = MakeScopeGuard([&]() { |
591 | 17.4k | for (size_t c = next_channel; c < image.channel.size(); c++) { |
592 | 14.5k | ZeroFillImage(&image.channel[c].plane); |
593 | 14.5k | } |
594 | 2.86k | }); |
595 | | // Do not do anything if truncated groups are not allowed. |
596 | 40.2k | if (allow_truncated_group) scope_guard.Disarm(); |
597 | | |
598 | | // Read tree. |
599 | 40.2k | Tree tree_storage; |
600 | 40.2k | std::vector<uint8_t> context_map_storage; |
601 | 40.2k | ANSCode code_storage; |
602 | 40.2k | const Tree *tree = &tree_storage; |
603 | 40.2k | const ANSCode *code = &code_storage; |
604 | 40.2k | const std::vector<uint8_t> *context_map = &context_map_storage; |
605 | 40.2k | if (!header.use_global_tree) { |
606 | 24.7k | uint64_t max_tree_size = 1024; |
607 | 293k | for (size_t i = 0; i < nb_channels; i++) { |
608 | 268k | Channel &channel = image.channel[i]; |
609 | 268k | if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size || |
610 | 266k | channel.h > options->max_chan_size)) { |
611 | 48 | break; |
612 | 48 | } |
613 | 268k | uint64_t pixels = channel.w * channel.h; |
614 | 268k | max_tree_size += pixels; |
615 | 268k | } |
616 | 24.7k | max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size); |
617 | 24.7k | JXL_RETURN_IF_ERROR( |
618 | 24.7k | DecodeTree(memory_manager, br, &tree_storage, max_tree_size)); |
619 | 24.4k | JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, |
620 | 24.4k | (tree_storage.size() + 1) / 2, |
621 | 24.4k | &code_storage, &context_map_storage)); |
622 | 24.4k | } else { |
623 | 15.4k | if (!global_tree || !global_code || !global_ctx_map || |
624 | 15.4k | global_tree->empty()) { |
625 | 76 | return JXL_FAILURE("No global tree available but one was requested"); |
626 | 76 | } |
627 | 15.4k | tree = global_tree; |
628 | 15.4k | code = global_code; |
629 | 15.4k | context_map = global_ctx_map; |
630 | 15.4k | } |
631 | | |
632 | | // Read channels |
633 | 79.5k | JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader, |
634 | 79.5k | ANSSymbolReader::Create(code, br, distance_multiplier)); |
635 | 79.5k | auto tree_lut = jxl::make_unique<TreeLut<uint8_t, false, false>>(); |
636 | 79.5k | uint32_t fl_run = 0; |
637 | 79.5k | uint32_t fl_v = 0; |
638 | 396k | for (; next_channel < nb_channels; next_channel++) { |
639 | 359k | Channel &channel = image.channel[next_channel]; |
640 | 359k | if (next_channel >= image.nb_meta_channels && |
641 | 355k | (channel.w > options->max_chan_size || |
642 | 355k | channel.h > options->max_chan_size)) { |
643 | 678 | break; |
644 | 678 | } |
645 | 358k | if (!channel.w || !channel.h) { |
646 | 4.87k | continue; // skip empty channels |
647 | 4.87k | } |
648 | 354k | JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS( |
649 | 354k | br, &reader, *context_map, *tree, header.wp_header, next_channel, |
650 | 354k | group_id, *tree_lut, &image, fl_run, fl_v)); |
651 | | |
652 | | // Truncated group. |
653 | 354k | if (!br->AllReadsWithinBounds()) { |
654 | 2.39k | if (!allow_truncated_group) return JXL_FAILURE("Truncated input"); |
655 | 0 | return JXL_NOT_ENOUGH_BYTES("Read overrun in ModularDecode"); |
656 | 2.39k | } |
657 | 354k | } |
658 | | |
659 | | // Make sure no zero-filling happens even if next_channel < nb_channels. |
660 | 37.3k | scope_guard.Disarm(); |
661 | | |
662 | 37.3k | if (!reader.CheckANSFinalState()) { |
663 | 0 | return JXL_FAILURE("ANS decode final state failed"); |
664 | 0 | } |
665 | 37.3k | return true; |
666 | 37.3k | } |
667 | | |
668 | | Status ModularGenericDecompress(BitReader *br, Image &image, |
669 | | GroupHeader *header, size_t group_id, |
670 | | ModularOptions *options, bool undo_transforms, |
671 | | const Tree *tree, const ANSCode *code, |
672 | | const std::vector<uint8_t> *ctx_map, |
673 | 48.5k | bool allow_truncated_group) { |
674 | 48.5k | std::vector<std::pair<size_t, size_t>> req_sizes; |
675 | 48.5k | req_sizes.reserve(image.channel.size()); |
676 | 152k | for (const auto &c : image.channel) { |
677 | 152k | req_sizes.emplace_back(c.w, c.h); |
678 | 152k | } |
679 | 48.5k | GroupHeader local_header; |
680 | 48.5k | if (header == nullptr) header = &local_header; |
681 | 48.5k | size_t bit_pos = br->TotalBitsConsumed(); |
682 | 48.5k | auto dec_status = ModularDecode(br, image, *header, group_id, options, tree, |
683 | 48.5k | code, ctx_map, allow_truncated_group); |
684 | 48.5k | if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status); |
685 | 44.3k | if (dec_status.IsFatalError()) return dec_status; |
686 | 44.3k | if (undo_transforms) image.undo_transforms(header->wp_header); |
687 | 44.3k | if (image.error) return JXL_FAILURE("Corrupt file. Aborting."); |
688 | 44.3k | JXL_DEBUG_V(4, |
689 | 44.3k | "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS |
690 | 44.3k | " image from %" PRIuS " bytes", |
691 | 44.3k | image.w, image.h, image.channel.size(), |
692 | 44.3k | (br->TotalBitsConsumed() - bit_pos) / 8); |
693 | 44.3k | JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str()); |
694 | 44.3k | (void)bit_pos; |
695 | | // Check that after applying all transforms we are back to the requested |
696 | | // image sizes, otherwise there's a programming error with the |
697 | | // transformations. |
698 | 44.3k | if (undo_transforms) { |
699 | 12.2k | JXL_ENSURE(image.channel.size() == req_sizes.size()); |
700 | 60.8k | for (size_t c = 0; c < req_sizes.size(); c++) { |
701 | 48.6k | JXL_ENSURE(req_sizes[c].first == image.channel[c].w); |
702 | 48.6k | JXL_ENSURE(req_sizes[c].second == image.channel[c].h); |
703 | 48.6k | } |
704 | 12.2k | } |
705 | 44.3k | return dec_status; |
706 | 44.3k | } |
707 | | |
708 | | } // namespace jxl |