jixel 0.5.13

JPEG XL encoder
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
/*
 * // 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.
 */

//! Per-group local learned-tree training and encoding.

use super::frame::keep_smaller_writer;
use super::groups::{DeepLzScratchPool, group_lz_threads};
use super::lz77::{
    ConstantTail, LzToken, build_lz_pixel_code_tails, estimate_coded_bits,
    estimate_literal_and_run_bits_single, lz77_compress_for_speed_with_depth, lz77_run_count,
    write_lz_section_tail, write_tree_lz77,
};
use super::ma_encoding::tokenize_ma_rect;
use super::ma_learning::{finish_ma_candidate, ma_leaf_price, ma_learn_params, with_ma_prune};
use super::ma_sampling::{ma_channel_sample_count, sample_channel_ma};
use super::ma_source::{
    LearnedCandidate, MA_MIN_NODE_SAMPLES, MaPlacement, MaSource, MaTransform, ac_stream_id,
    with_ma_pixels,
};
use super::ma_streams::{
    LZ_LITERAL_ALTERNATIVE_MAX_RATIO, VARIANT_WRITE_TOLERANCE, priced_tree_streams,
};
use super::tokens::RawTokens;
use super::{GroupLayout, lz77};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::entropy::Token;
use crate::ma_tree::{MaSamples, learn_ma_tree_in_place};
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::{WpParams, write_wp_header};

/// Samples the local learner draws per group (a full 1024-px RGB group is
/// sampled at stride 3, a 256-px one fully). Denser than the global learn:
/// the payoff of local trees is finding more of the near-deterministic
/// contexts of text and graphics, which the global stride starves (the IDE
/// screenshot: local trees at the global stride are neutral, at stride 7
/// −4%, at stride 3 −8%; the deterministic-node shortcut in the learner
/// keeps the dense learn affordable).
const LOCAL_TREE_TARGET_SAMPLES: usize = 1 << 20;

/// Groups learned concurrently; each lane holds one group's samples
/// (~110 B per sample, ~115 MiB at the sample target). Beyond four lanes the
/// learners' own row-chunk and per-property parallelism already occupy the
/// workers.
const LOCAL_TREE_LEARN_LANES: usize = 4;

/// Local trees are tried when the global tree codes the frame under this
/// many bits per value: near-deterministic contexts are the profile that
/// keeps gaining from denser sampling. Screenshot frames measure 0.03–0.24
/// at any leaf count (513–4095), a Buddhabrot render 1.0, photos 1.6–3.4.
/// The frame then takes, per AC group, the smaller of its global-tree and
/// local-tree section, so flat screenshots whose all-local frame loses
/// (+13..+17 % at 256-px groups) still gain from the groups that win:
/// screenshots −0.8..−5.0 % at 1.2–2.3× encode time, everything above the
/// bound untouched. (The former floor of half the leaf budget skipped
/// those frames; a whole-image constant-block screen flag instead fires on
/// illustrations and manga for no gain at 1.5–1.9×.)
pub(super) const LOCAL_TREE_MAX_BITS_PER_VALUE: f64 = 0.5;

/// Local per-group learned trees: every AC group section carries its own
/// tree, histograms and context map (`use_global_tree = 0`), learned from
/// that group's pixels alone, and the DC-global section declares no tree.
/// This is libjxl's streaming layout for large images. Groups with distinct
/// content (code text next to UI chrome) each get a full leaf budget and
/// their own cluster budget without spending splits on the stream id, and
/// each is learned from a denser sample of its own pixels
/// (`LOCAL_TREE_TARGET_SAMPLES`), which is where the gain comes from: at the
/// global learn's stride local trees are neutral. Every group picks literal
/// or LZ77 tokens on its own code. Returns the sections in codestream
/// order, or `None` when the source has a global stream.
#[allow(clippy::too_many_arguments)]
pub(super) fn write_local_tree_sections(
    frame_kind: crate::patches::ModularFrameKind<'_>,
    speed: crate::Speed,
    source: &MaSource<'_>,
    placement: &[MaPlacement],
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    num_dc_groups: usize,
    min_symbol: u32,
    wp_params: WpParams,
    use_wp: bool,
    dictionary: Option<&BitWriter>,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Option<Vec<BitWriter>> {
    let gdim = layout.dim();
    let xsize_groups = xsize.div_ceil(gdim);
    let ysize_groups = ysize.div_ceil(gdim);
    let num_ac_groups = xsize_groups * ysize_groups;
    if num_ac_groups < 2 || placement.iter().any(|p| p.global) {
        return None;
    }
    let target = LOCAL_TREE_TARGET_SAMPLES;
    let max_candidates = source.max_candidates();
    let leaf_offsets = matches!(source.transform, MaTransform::Rct(_));
    let group_rect = |group_index: usize| {
        let x0 = (group_index % xsize_groups) * gdim;
        let y0 = (group_index / xsize_groups) * gdim;
        (x0, y0)
    };

    // Sample and learn each group in its own lane (the learner also
    // parallelizes inside); a lane holds one group's sample set at a time.
    let cands: Vec<LearnedCandidate> = pool.steal_map_with_threads(
        scratch,
        num_ac_groups,
        LOCAL_TREE_LEARN_LANES,
        |group_index, scratch| {
            let (x0, y0) = group_rect(group_index);
            let stream = ac_stream_id(num_dc_groups, group_index);
            let sampled = || {
                placement
                    .iter()
                    .filter(|p| source.channels[p.channel].constant.is_none())
            };
            let group_values: usize = sampled()
                .map(|p| {
                    let ch = &source.channels[p.channel];
                    gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
                })
                .sum();
            let mut stride = group_values.div_ceil(target).max(1);
            if stride > 1 && stride.is_multiple_of(2) {
                stride += 1;
            }
            let capacity = sampled()
                .map(|p| {
                    let ch = &source.channels[p.channel];
                    ma_channel_sample_count(gdim.min(ch.w - x0), gdim.min(ch.h - y0), stride)
                })
                .sum();
            let mut samples = MaSamples::with_capacity(capacity);
            for p in sampled() {
                let ch = &source.channels[p.channel];
                let gw = gdim.min(ch.w - x0);
                let gh = gdim.min(ch.h - y0);
                let refs = source.ref_planes(placement, p, x0, y0);
                with_ma_pixels!(ch, |pixels| sample_channel_ma(
                    |y| &pixels[(y0 + y) * ch.w + x0..][..gw],
                    gw,
                    gh,
                    p.chan_id,
                    stream,
                    &refs,
                    wp_params,
                    use_wp,
                    stride,
                    |props, tok, val| samples.push_val(props, tok, val),
                ));
            }
            // Too few samples to learn from: the root alone (one predictor).
            let max_leaves = if samples.len() < 4 * MA_MIN_NODE_SAMPLES {
                1
            } else {
                source.max_leaves()
            };
            // Offsets inspect every fourth row in the original order. Keep
            // only those rows; the dense learner can partition the full set
            // in place instead of allocating a second full set and indices.
            let offsets = (leaf_offsets && max_leaves > 1).then(|| MaSamples {
                props: samples.props.sampled(4),
                tok: samples.tok.iter().step_by(4).copied().collect(),
                val: Vec::new(),
            });
            let params = with_ma_prune(
                ma_learn_params(
                    min_symbol,
                    max_leaves,
                    max_candidates,
                    stride as f64,
                    use_wp,
                    !leaf_offsets,
                ),
                stride as f64,
            );
            let leaf_price = ma_leaf_price(&params, stride as f64);
            let tree = learn_ma_tree_in_place(&mut samples, params, pool, scratch);
            drop(samples);
            let mut cand = finish_ma_candidate(tree, stride as f64, leaf_price);
            if let Some(offsets) = offsets {
                cand.set_leaf_offsets(&offsets, 1);
            }
            cand
        },
    );

    let constant = source.constant_placement(placement);
    let cands: Vec<LearnedCandidate> = match constant {
        Some((p, value)) => cands
            .iter()
            .map(|cand| cand.with_constant_channel(p.chan_id, value))
            .collect(),
        None => cands,
    };

    // Tokenize every group through its own tree, then code each group on
    // its own histograms. Retain raw tokens only for active coding lanes.
    let cands_ref = &cands;
    let deep_lz_max_ratio = source.deep_lz_max_ratio();
    let deep_lz = DeepLzScratchPool::new(group_lz_threads(speed, pool));
    let group_sections: Vec<BitWriter> = pool.steal_map_with_threads(
        scratch,
        num_ac_groups,
        group_lz_threads(speed, pool),
        |group_index, scratch| {
            let (x0, y0) = group_rect(group_index);
            let num_values = placement
                .iter()
                .map(|p| {
                    let ch = &source.channels[p.channel];
                    gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
                })
                .sum();
            let mut toks = RawTokens::with_capacity(num_values);
            let mut tail = ConstantTail::NONE;
            for p in placement {
                let ch = &source.channels[p.channel];
                if ch.constant.is_some() {
                    tail = cands_ref[group_index]
                        .constant_tail(gdim.min(ch.w - x0), gdim.min(ch.h - y0));
                    continue;
                }
                tokenize_ma_rect(
                    source,
                    placement,
                    p,
                    ac_stream_id(num_dc_groups, group_index),
                    x0,
                    y0,
                    gdim.min(ch.w - x0),
                    gdim.min(ch.h - y0),
                    &cands_ref[group_index],
                    wp_params,
                    use_wp,
                    &mut toks,
                );
            }
            let cand = &cands_ref[group_index];
            match &toks {
                RawTokens::Compact(tokens) => write_local_tree_group(
                    speed,
                    tokens,
                    tail,
                    cand,
                    min_symbol,
                    wp_params,
                    deep_lz_max_ratio,
                    &deep_lz,
                    scratch,
                ),
                RawTokens::Wide(tokens) => write_local_tree_group(
                    speed,
                    tokens,
                    tail,
                    cand,
                    min_symbol,
                    wp_params,
                    deep_lz_max_ratio,
                    &deep_lz,
                    scratch,
                ),
            }
        },
    );

    let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
    let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();
    if let Some(dictionary) = dictionary {
        sections[0].append(dictionary);
    }
    super::header::write_dc_quant(frame_kind, &mut sections[0]);
    sections[0].write(1, 0); // has_tree = 0
    sections[0].write(1, 0); // use_global_tree = 0 (no channel is coded here)
    write_wp_header(wp_params, &mut sections[0]);
    source.write_transforms(&mut sections[0]);
    // libjxl skips an empty stream's tree; jxl-oxide parses one.
    let empty_code = crate::modular::build_pixel_code(&[Token::new(0, 0)], scratch);
    crate::modular::write_tree_and_pixel_histograms(&empty_code, scratch, &mut sections[0]);
    sections[0].zero_pad_to_byte();
    for section in sections[1..num_dc_groups + 2].iter_mut() {
        section.write(1, 0); // use_global_tree = 0
        write_wp_header(wp_params, section);
        section.write(2, 0); // no transforms
        section.zero_pad_to_byte();
    }
    for (group_index, section) in group_sections.into_iter().enumerate() {
        sections[2 + num_dc_groups + group_index] = section;
    }
    Some(sections)
}

/// One AC group section under its local tree: GroupHeader, tree,
/// histograms and tokens. The literal and LZ77 variants are ordered by their
/// order-0 estimates and a later one is written only when its code-based
/// estimate is within `VARIANT_WRITE_TOLERANCE`, as on the global path.
fn write_local_tree_group<T: lz77::LiteralToken>(
    speed: crate::Speed,
    tokens: &[T],
    tail: ConstantTail,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    deep_lz_max_ratio: f64,
    deep_lz: &DeepLzScratchPool,
    scratch: &mut CoderScratch,
) -> BitWriter {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let run_count = lz77_run_count(tokens);
    let mut best: Option<BitWriter> = None;
    let mut best_estimate = f64::INFINITY;
    let deep = |scratch: &mut CoderScratch| -> Vec<LzToken> {
        deep_lz.with_depth(|depth| {
            lz77_compress_for_speed_with_depth(
                tokens,
                distance_ctx,
                speed,
                depth,
                Some(run_count),
                scratch,
            )
        })
    };
    if (run_count as f64) >= LZ_LITERAL_ALTERNATIVE_MAX_RATIO * tokens.len() as f64 {
        let lz = deep(scratch);
        write_local_tree_variant(
            speed,
            &lz,
            tail,
            cand,
            min_symbol,
            wp_params,
            scratch,
            &mut best,
            &mut best_estimate,
        );
    } else {
        let (e_lit, e_run) =
            estimate_literal_and_run_bits_single(tokens, num_ctx as usize + 1, min_symbol);
        if e_run > e_lit * deep_lz_max_ratio {
            write_local_tree_variant(
                speed,
                tokens,
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
        } else if e_run < e_lit {
            let lz = deep(scratch);
            write_local_tree_variant(
                speed,
                &lz,
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
            write_local_tree_variant(
                speed,
                tokens,
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
        } else {
            write_local_tree_variant(
                speed,
                tokens,
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
            let lz = deep(scratch);
            write_local_tree_variant(
                speed,
                &lz,
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
        }
    }
    if speed.effort().lz_priced_search() {
        let (e_lit, _) =
            estimate_literal_and_run_bits_single(tokens, num_ctx as usize + 1, min_symbol);
        if let Some(streams) = priced_tree_streams(
            speed,
            &[tokens],
            e_lit,
            num_ctx as usize + 1,
            min_symbol,
            &ThreadPool::new_lossless(1),
            scratch,
        ) {
            write_local_tree_variant(
                speed,
                &streams[0],
                tail,
                cand,
                min_symbol,
                wp_params,
                scratch,
                &mut best,
                &mut best_estimate,
            );
        }
    }
    best.expect("local-tree group section")
}

fn write_local_tree_variant<T: lz77::LzTokenSource>(
    speed: crate::Speed,
    stream: &[T],
    tail: ConstantTail,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    scratch: &mut CoderScratch,
    best: &mut Option<BitWriter>,
    best_estimate: &mut f64,
) {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let code = build_lz_pixel_code_tails(
        speed.effort().lz_exhaustive_entropy(),
        &[stream],
        &[tail],
        num_ctx as usize,
        min_symbol,
        true,
        true,
        None,
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    let estimate = estimate_coded_bits(&[stream], distance_ctx, &code, min_symbol, None);
    if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
        return;
    }
    *best_estimate = best_estimate.min(estimate);
    let mut section = BitWriter::new();
    section.write(1, 0); // use_global_tree = 0
    write_wp_header(wp_params, &mut section);
    section.write(2, 0); // no transforms
    write_tree_lz77(
        &cand.tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut section,
    );
    write_lz_section_tail(stream, tail, distance_ctx, &code, min_symbol, &mut section);
    section.zero_pad_to_byte();
    keep_smaller_writer(best, section);
}