Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 * // Copyright (c) Radzivon Bartoshyk 5/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//! Modular property traversal, sampling, and DC plane tokenization.

use super::ma_lookup::MaLookup;
use super::predictor::{PredictorNeighbors, predictor_value};
use super::tokens::RawTokens;
use crate::entropy::{Token, pack_signed, uint_encode};
use crate::ma_tree::{LearnedTree, MA_REF_CHANNELS, MaSamples, NUM_MA_PREDS, NUM_MA_PROPS};
use crate::weighted_predictor::{WpNeighbors, WpParams, WpState};

/// Walk one channel rectangle in scan order, feeding the visitor the property
/// vector (libjxl ids 0..=15), the neighborhood, and the WP prediction of
/// every pixel.
/// A previously coded channel of the same size as the one being walked,
/// cropped like it: the source of the decoder's reference properties.
#[derive(Clone, Copy)]
pub(crate) struct MaRefPlane<'a> {
    pub(crate) pixels: &'a [i32],
    pub(crate) stride: usize,
    pub(crate) x0: usize,
    pub(crate) y0: usize,
}

/// libjxl `PrecomputeReferences`: per reference channel |v|, v, |v − g|,
/// v − g, with left = 0 at the crop's left edge, top = left on its first row
/// and top-left = left on either edge, g the clamped gradient.
#[inline]
fn ref_props(refs: &[MaRefPlane<'_>], x: usize, y: usize, p: &mut [i32; NUM_MA_PROPS]) {
    for (r, props) in refs.iter().zip(p[16..].as_chunks_mut::<4>().0) {
        let row = &r.pixels[(r.y0 + y) * r.stride + r.x0..];
        let v = row[x] as i64;
        let left = if x > 0 { row[x - 1] as i64 } else { 0 };
        let (top, top_left) = if y > 0 {
            let above = &r.pixels[(r.y0 + y - 1) * r.stride + r.x0..];
            (
                above[x] as i64,
                if x > 0 { above[x - 1] as i64 } else { left },
            )
        } else {
            (left, left)
        };
        let g = (left + top - top_left).clamp(left.min(top), left.max(top));
        let residual = v - g;
        *props = [
            v.abs() as i32,
            v as i32,
            residual.abs() as i32,
            residual as i32,
        ];
    }
}

pub(super) fn walk_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    mut visit: impl FnMut(usize, usize, i64, &[i32; NUM_MA_PROPS], PredictorNeighbors, i64),
) where
    i64: From<T>,
{
    let disabled = !use_wp;
    let mut wp = WpState::with_params(gw, wp_params);
    let mut p = [0i32; NUM_MA_PROPS];
    p[0] = chan as i32;
    p[1] = stream;
    let mut north_row: &[T] = &[];
    let mut north_north_row: &[T] = &[];
    for y in 0..gh {
        let current_row = get_row(y);
        assert_eq!(current_row.len(), gw);
        // Bind valid rows even at the top border, so all three lengths are
        // established before entering the pixel loop.
        if y == 0 {
            north_row = current_row;
        }
        if y < 2 {
            north_north_row = north_row;
        }
        assert_eq!(north_row.len(), gw);
        assert_eq!(north_north_row.len(), gw);
        let row = wp.row_offsets(y);
        p[2] = y as i32;
        p[9] = 0; // "local gradient" carry, reset per row (InitPropsRow)
        // Keep property updates in each loop, but fuse WP only in the hot
        // interior. First-row, border, and tail visits share the WP operation.
        macro_rules! visit_pixel {
            ($predict:ident, $x:expr, $value:expr, $neighbors:expr) => {{
                let x = $x;
                let value = $value;
                let n = $neighbors;
                p[3] = x as i32;
                p[4] = n.top.abs() as i32;
                p[5] = n.left.abs() as i32;
                p[6] = n.top as i32;
                p[7] = n.left as i32;
                // p[8] reads the previous pixel's p[9] (0 at row start).
                p[8] = (n.left - p[9] as i64) as i32;
                p[9] = (n.left + n.top - n.top_left) as i32;
                p[10] = (n.left - n.top_left) as i32;
                p[11] = (n.top_left - n.top) as i32;
                p[12] = (n.top - n.top_right) as i32;
                p[13] = (n.top - n.top_top) as i32;
                p[14] = (n.left - n.left_left) as i32;
                let wp_pred = if disabled {
                    0
                } else {
                    wp.$predict(
                        value,
                        x,
                        row,
                        WpNeighbors {
                            north: n.top,
                            west: n.left,
                            north_east: n.top_right,
                            north_west: n.top_left,
                            north_north: n.top_top,
                        },
                    )
                };
                p[15] = if disabled { 0 } else { wp.wp_prop as i32 };
                ref_props(refs, x, y, &mut p);
                visit(x, y, value, &p, n, wp_pred);
            }};
        }
        if y == 0 {
            if let Some((&first, tail)) = current_row.split_first() {
                let first = i64::from(first);
                visit_pixel!(
                    predict_and_update_flat,
                    0,
                    first,
                    PredictorNeighbors {
                        left: 0,
                        top: 0,
                        top_left: 0,
                        top_right: 0,
                        left_left: 0,
                        top_top: 0,
                        top_right_right: 0,
                    }
                );
                let mut left = first;
                let mut left_left = first;
                for (x, &value) in tail.iter().enumerate() {
                    let value = i64::from(value);
                    visit_pixel!(
                        predict_and_update_flat,
                        x + 1,
                        value,
                        PredictorNeighbors {
                            left,
                            top: left,
                            top_left: left,
                            top_right: left,
                            left_left,
                            top_top: left,
                            top_right_right: left,
                        }
                    );
                    left_left = left;
                    left = value;
                }
            }
        } else {
            let edge = |x: usize| {
                let top = i64::from(north_row[x]);
                let left = if x > 0 {
                    i64::from(current_row[x - 1])
                } else {
                    top
                };
                let top_right = north_row.get(x + 1).map_or(top, |&v| i64::from(v));
                PredictorNeighbors {
                    left,
                    top,
                    top_left: if x > 0 {
                        i64::from(north_row[x - 1])
                    } else {
                        left
                    },
                    top_right,
                    left_left: if x > 1 {
                        i64::from(current_row[x - 2])
                    } else {
                        left
                    },
                    top_top: i64::from(north_north_row[x]),
                    top_right_right: north_row.get(x + 2).map_or(top_right, |&v| i64::from(v)),
                }
            };
            for (x, &value) in current_row.iter().take(2).enumerate() {
                visit_pixel!(predict_and_update_flat, x, i64::from(value), edge(x));
            }
            if gw > 4 {
                let mut left = i64::from(current_row[1]);
                let mut left_left = i64::from(current_row[0]);
                let pixels = current_row[2..gw - 2]
                    .iter()
                    .zip(north_row[1..].array_windows::<4>())
                    .zip(&north_north_row[2..]);
                for (x, ((&value, &[nw, n, ne, nee]), &nn)) in pixels.enumerate() {
                    let value = i64::from(value);
                    visit_pixel!(
                        predict_and_update_flat_inlined,
                        x + 2,
                        value,
                        PredictorNeighbors {
                            left,
                            top: i64::from(n),
                            top_left: i64::from(nw),
                            top_right: i64::from(ne),
                            left_left,
                            top_top: i64::from(nn),
                            top_right_right: i64::from(nee),
                        }
                    );
                    left_left = left;
                    left = value;
                }
            }
            for x in gw.saturating_sub(2).max(2)..gw {
                visit_pixel!(
                    predict_and_update_flat,
                    x,
                    i64::from(current_row[x]),
                    edge(x)
                );
            }
        }
        north_north_row = north_row;
        north_row = current_row;
    }
}

/// Exact sample count for the rotating row phase.
pub(super) fn ma_channel_sample_count(w: usize, h: usize, stride: usize) -> usize {
    // A complete cycle of row phases selects exactly w samples. In the
    // remaining rows, the first w % stride phases select one extra sample.
    (h / stride) * w + (h % stride) * (w / stride) + (h % stride).min(w % stride)
}

/// Visit the selected pixels with residual tokens under every decoder predictor.
pub(crate) fn sample_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    stride: usize,
    mut push: impl FnMut([i32; NUM_MA_PROPS], [u8; NUM_MA_PREDS], i32),
) where
    i64: From<T>,
{
    if use_wp {
        sample_channel_ma_shared::<T, true>(
            &get_row, gw, gh, chan, stream, refs, wp_params, stride, &mut push,
        );
    } else {
        sample_channel_ma_shared::<T, false>(
            &get_row, gw, gh, chan, stream, refs, wp_params, stride, &mut push,
        );
    }
}

// Share the loop across callback types: one getter call per row and one output
// call per selected sample. Keep WP-on/off specializations so disabled WP work
// is removed, especially for lossy modular sampling.
fn sample_channel_ma_shared<'a, T: Copy + 'a, const USE_WP: bool>(
    get_row: &dyn Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    stride: usize,
    push: &mut dyn FnMut([i32; NUM_MA_PROPS], [u8; NUM_MA_PREDS], i32),
) where
    i64: From<T>,
{
    debug_assert!(stride != 0);
    let disabled = !USE_WP;
    let mut wp = WpState::with_params(gw, wp_params);
    let mut north_row: &[T] = &[];
    let mut north_north_row: &[T] = &[];
    for y in 0..gh {
        let current_row = get_row(y);
        assert_eq!(current_row.len(), gw);
        // Bind valid rows even at the top border, so all three lengths are
        // established before entering the pixel loop.
        if y == 0 {
            north_row = current_row;
        }
        if y < 2 {
            north_north_row = north_row;
        }
        assert_eq!(north_row.len(), gw);
        assert_eq!(north_north_row.len(), gw);
        let row = wp.row_offsets(y);
        let mut west = 0i64;
        let mut west_west = 0i64;
        let mut previous_local_gradient = 0i32;
        // Rotate the sampling phase every row so the probe never locks onto
        // one column residue (a row width divisible by the stride would
        // otherwise sample a single x phase for the whole channel).
        let mut until_sample = 1 + y % stride;
        for x in 0..gw {
            let value = i64::from(current_row[x]);
            let north = if y > 0 { i64::from(north_row[x]) } else { west };
            let left = if x > 0 { west } else { north };
            let top_left = if x > 0 && y > 0 {
                i64::from(north_row[x - 1])
            } else {
                left
            };
            let top_right = if x + 1 < gw && y > 0 {
                i64::from(north_row[x + 1])
            } else {
                north
            };
            let top_top = if y > 1 {
                i64::from(north_north_row[x])
            } else {
                north
            };
            let wp_pred = if disabled {
                0
            } else {
                let wp_neighbors = WpNeighbors {
                    north,
                    west: left,
                    north_east: top_right,
                    north_west: top_left,
                    north_north: top_top,
                };
                // Sampling still advances WP on every pixel; this loop benefits
                // from fusion even when only a fraction of properties are kept.
                wp.predict_and_update_flat_inlined(value, x, row, wp_neighbors)
            };
            let local_gradient = (left + north - top_left) as i32;

            until_sample -= 1;
            if until_sample == 0 {
                until_sample = stride;
                let neighbors = PredictorNeighbors {
                    left,
                    top: north,
                    top_left,
                    top_right,
                    left_left: if x > 1 { west_west } else { left },
                    top_top,
                    top_right_right: if x + 2 < gw && y > 0 {
                        i64::from(north_row[x + 2])
                    } else {
                        top_right
                    },
                };
                let mut props = [0i32; NUM_MA_PROPS];
                props[0] = chan as i32;
                props[1] = stream;
                props[2] = y as i32;
                props[3] = x as i32;
                props[4] = north.abs() as i32;
                props[5] = left.abs() as i32;
                props[6] = north as i32;
                props[7] = left as i32;
                props[8] = (left - previous_local_gradient as i64) as i32;
                props[9] = local_gradient;
                props[10] = (left - top_left) as i32;
                props[11] = (top_left - north) as i32;
                props[12] = (north - top_right) as i32;
                props[13] = (north - top_top) as i32;
                props[14] = (left - neighbors.left_left) as i32;
                props[15] = if disabled { 0 } else { wp.wp_prop as i32 };
                ref_props(refs, x, y, &mut props);

                let mut tok = [0u8; NUM_MA_PREDS];
                for pred in 0..NUM_MA_PREDS as u32 {
                    let pv = predictor_value(pred, neighbors, wp_pred);
                    let (t, _, _) = uint_encode(pack_signed((value - pv) as i32));
                    tok[pred as usize] = t.min(u8::MAX as u32) as u8;
                }
                push(props, tok, value as i32);
            }
            previous_local_gradient = local_gradient;
            west_west = west;
            west = value;
        }
        north_north_row = north_row;
        north_row = current_row;
    }
}

/// Tokenize the channel rectangle through the learned tree: every pixel is
/// routed to its leaf's context and coded with its leaf's predictor.
pub(super) fn tokenize_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    lookup: &MaLookup,
    offsets: &[i32],
    out: &mut RawTokens,
) where
    i64: From<T>,
{
    // Keep the traversal's stores simple, then validate and pack one row at
    // a time. The temporary row fits in cache and is reused for the channel.
    let mut row = Vec::with_capacity(gw);
    walk_channel_ma(
        get_row,
        gw,
        gh,
        chan,
        stream,
        refs,
        wp_params,
        use_wp && lookup.needs_wp(),
        |x, _y, v, p, n, wp_pred| {
            let (context, pred) = lookup.lookup(p);
            let pv = predictor_value(pred, n, wp_pred);
            row.push(Token::new(
                context,
                pack_signed((v - pv) as i32 - offsets[context as usize]),
            ));
            if x + 1 == gw {
                out.extend(&row);
                row.clear();
            }
        },
    );
}

/// One VarDCT DC group's quantized planes in modular channel order (Y, X, B).
pub(crate) struct DcPlanes {
    pub(crate) planes: [Vec<i32>; 3],
    pub(crate) w: usize,
    pub(crate) h: usize,
}

impl DcPlanes {
    /// Previously coded channels of `c`, most recent first, as the decoder's
    /// reference properties see them.
    pub(super) fn refs(&self, c: usize) -> Vec<MaRefPlane<'_>> {
        (0..c)
            .rev()
            .take(MA_REF_CHANNELS)
            .map(|r| MaRefPlane {
                pixels: &self.planes[r],
                stride: self.w,
                x0: 0,
                y0: 0,
            })
            .collect()
    }
}

/// MA samples of every DC group (stream id `1 + group`, default WP), taking
/// every `stride`-th pixel.
pub(crate) fn sample_dc_planes(groups: &[DcPlanes], stride: usize) -> MaSamples {
    let capacity = groups
        .iter()
        .map(|g| 3 * ma_channel_sample_count(g.w, g.h, stride))
        .sum();
    let mut samples = MaSamples::with_capacity(capacity);
    for (group, g) in groups.iter().enumerate() {
        if g.w == 0 || g.h == 0 {
            continue;
        }
        for c in 0..3 {
            let refs = g.refs(c);
            let pixels = &g.planes[c];
            sample_channel_ma(
                |y| &pixels[y * g.w..(y + 1) * g.w],
                g.w,
                g.h,
                c as u32,
                1 + group as i32,
                &refs,
                WpParams::DEFAULT,
                true,
                stride,
                |props, tok, _| samples.push(props, tok),
            );
        }
    }
    samples
}

/// Tokenize one DC group through a learned tree: every pixel takes its leaf's
/// context (`leaf_ctx`, indexed by tree node) and predictor.
pub(crate) fn tokenize_dc_planes(
    g: &DcPlanes,
    group: usize,
    tree: &LearnedTree,
    leaf_ctx: &[u32],
    out: &mut Vec<Token>,
) {
    if g.w == 0 || g.h == 0 {
        return;
    }
    for c in 0..3 {
        let refs = g.refs(c);
        let pixels = &g.planes[c];
        walk_channel_ma(
            |y| &pixels[y * g.w..(y + 1) * g.w],
            g.w,
            g.h,
            c as u32,
            1 + group as i32,
            &refs,
            WpParams::DEFAULT,
            true,
            |_x, _y, v, p, n, wp_pred| {
                let (node, pred) = tree.lookup(p);
                let pv = predictor_value(pred, n, wp_pred);
                out.push(Token::new(
                    leaf_ctx[node as usize],
                    pack_signed((v - pv) as i32),
                ));
            },
        );
    }
}