j2k-native 0.13.1

Pure-Rust JPEG 2000 and HTJ2K codec engine for j2k
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! A serial region decode on the streamed IDWT hands its rows to a row sink
//! as they are finished, with every component decoded together and no
//! component planes. Its samples must match the planes bit for bit, every
//! sample must arrive once, and it must hold less.

use alloc::vec::Vec;

use super::banded_tier1::{cases, noisy_pixels, Case, HEIGHT, TILE, WIDTH};
use crate::{
    encode, ComponentPlane, ComponentRowSink, CpuDecodeParallelism, DecodeSettings, DecoderContext,
    EncodeOptions, Image,
};

/// Collects what a sink receives into region-sized planes of sample bits,
/// counting how often each sample arrives.
struct CollectingSink {
    width: usize,
    planes: Vec<Vec<u32>>,
    arrivals: Vec<u8>,
    depths: Vec<(u8, bool)>,
}

impl CollectingSink {
    fn new(width: u32, height: u32) -> Self {
        let samples = (width * height) as usize;
        Self {
            width: width as usize,
            planes: Vec::new(),
            arrivals: alloc::vec![0; samples],
            depths: Vec::new(),
        }
    }
}

impl ComponentRowSink for CollectingSink {
    fn write_rows(&mut self, x: u32, y: u32, planes: &[ComponentPlane<'_>]) {
        let samples = self.arrivals.len();
        if self.planes.is_empty() {
            self.planes = alloc::vec![alloc::vec![0; samples]; planes.len()];
            self.depths = planes
                .iter()
                .map(|plane| (plane.bit_depth(), plane.signed()))
                .collect();
        }
        assert_eq!(planes.len(), self.planes.len());
        let (width, height) = planes[0].dimensions();
        for (component, plane) in planes.iter().enumerate() {
            assert_eq!(plane.dimensions(), (width, height));
            assert_eq!((plane.bit_depth(), plane.signed()), self.depths[component]);
            for row in 0..height as usize {
                for column in 0..width as usize {
                    let at = (y as usize + row) * self.width + x as usize + column;
                    self.planes[component][at] =
                        plane.samples()[row * width as usize + column].to_bits();
                    if component == 0 {
                        self.arrivals[at] += 1;
                    }
                }
            }
        }
    }
}

struct Decoded {
    planes: Vec<Vec<u32>>,
    depths: Vec<(u8, bool)>,
    streamed: bool,
    channel_bytes: usize,
    peak_bytes: usize,
}

fn decode_planes(image: &Image<'_>, roi: (u32, u32, u32, u32)) -> Decoded {
    let mut context = DecoderContext::default();
    context.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
    let components = image
        .decode_region_components_for_integer_output_with_context(roi, &mut context)
        .expect("decode region planes");
    let planes = components
        .planes()
        .iter()
        .map(|plane| {
            plane.samples()[..(roi.2 * roi.3) as usize]
                .iter()
                .map(|sample| sample.to_bits())
                .collect()
        })
        .collect();
    let depths = components
        .planes()
        .iter()
        .map(|plane| (plane.bit_depth(), plane.signed()))
        .collect();
    drop(components);
    let peak = context
        .tile_decode_context
        .debug_counters
        .workspace_peak
        .bytes();
    if std::env::var_os("ROW_SINK_DEBUG").is_some() {
        std::eprintln!("planes {roi:?} {peak:?}");
    }
    Decoded {
        planes,
        depths,
        streamed: false,
        channel_bytes: peak.channels,
        peak_bytes: peak.total(),
    }
}

fn decode_rows(
    image: &Image<'_>,
    roi: (u32, u32, u32, u32),
    parallelism: CpuDecodeParallelism,
) -> Decoded {
    let mut context = DecoderContext::default();
    context.set_cpu_decode_parallelism(parallelism);
    let mut sink = CollectingSink::new(roi.2, roi.3);
    image
        .decode_region_rows_for_integer_output_with_context(roi, &mut context, &mut sink)
        .expect("decode region rows");
    assert!(
        sink.arrivals.iter().all(|&count| count == 1),
        "every sample arrives once"
    );
    let peak = context
        .tile_decode_context
        .debug_counters
        .workspace_peak
        .bytes();
    if std::env::var_os("ROW_SINK_DEBUG").is_some() {
        std::eprintln!("rows {roi:?} {peak:?}");
    }
    Decoded {
        planes: sink.planes,
        depths: sink.depths,
        streamed: context.tile_decode_context.stream_rows,
        channel_bytes: peak.channels,
        peak_bytes: peak.total(),
    }
}

fn row_sink_cases() -> Vec<Case> {
    let mut cases = cases();
    let base = EncodeOptions {
        num_decomposition_levels: 4,
        ..EncodeOptions::default()
    };
    cases.push(Case {
        label: "rgba 9/7",
        components: 4,
        options: EncodeOptions {
            reversible: false,
            ..base.clone()
        },
    });
    cases.push(Case {
        label: "rgb 5/3 without MCT, tiled",
        components: 3,
        options: EncodeOptions {
            reversible: true,
            use_mct: false,
            tile_size: Some((TILE, TILE)),
            ..base.clone()
        },
    });
    cases.push(Case {
        label: "gray 9/7, 2 layers",
        components: 1,
        options: EncodeOptions {
            reversible: false,
            num_layers: 2,
            ..base
        },
    });
    cases
}

#[test]
fn row_sink_matches_component_planes_bit_for_bit() {
    for case in row_sink_cases() {
        let pixels = noisy_pixels(case.components);
        let bytes = encode(
            &pixels,
            WIDTH,
            HEIGHT,
            u16::from(case.components),
            8,
            false,
            &case.options,
        )
        .expect("encode row sink fixture");
        for reduction in [0_u8, 1, 2] {
            let image = Image::new_with_reduction(&bytes, &DecodeSettings::default(), reduction)
                .expect("parse row sink fixture");
            let (width, height) = (image.width(), image.height());
            let tile = TILE >> reduction;
            let large = (
                width / 20,
                height / 20,
                width - width / 10,
                height - height / 10,
            );
            for roi in [
                (0, 0, width, height),
                large,
                (width / 3, height / 3, width / 7, height / 9),
                (tile + 1, tile + 2, tile - 3, tile - 4),
                (tile - 5, tile - 3, 11, 7),
                (0, 0, width, 9),
                (0, height - 9, width, 9),
                (width - 1, height - 1, 1, 1),
            ] {
                let label = format!("{} reduction={reduction} roi={roi:?}", case.label);
                let planes = decode_planes(&image, roi);
                let rows = decode_rows(&image, roi, CpuDecodeParallelism::Serial);
                assert!(rows.streamed, "{label}: streamed");
                assert_eq!(rows.depths, planes.depths, "{label}");
                assert_eq!(rows.planes.len(), planes.planes.len(), "{label}");
                for (component, (rows, planes)) in
                    rows.planes.iter().zip(&planes.planes).enumerate()
                {
                    let first = rows.iter().zip(planes).position(|(a, b)| a != b);
                    assert_eq!(first, None, "{label}: component {component}");
                }
                // Only the component records, with no samples.
                assert!(rows.channel_bytes <= 256, "{label}: no planes");
                // Every component's bands are live together, which costs more
                // than small planes save; on a large region the planes win.
                assert!(
                    roi != large || reduction != 0 || rows.peak_bytes < planes.peak_bytes,
                    "{label}: peak {} vs {}",
                    rows.peak_bytes,
                    planes.peak_bytes
                );
                // A parallel decode keeps its planes and hands them over at
                // the end.
                let parallel = decode_rows(&image, roi, CpuDecodeParallelism::Auto);
                #[cfg(feature = "parallel")]
                if rayon::current_num_threads() > 1 {
                    assert!(!parallel.streamed, "{label}: parallel planes");
                }
                assert_eq!(parallel.planes, planes.planes, "{label}: parallel");
            }
        }
    }
}

/// Outcome of a serial region decode, through the planes or the row sink:
/// a hash of the samples, or the error.
fn outcome(bytes: &[u8], strict: bool, roi: (u32, u32, u32, u32), rows: bool) -> String {
    let settings = DecodeSettings {
        strict,
        ..DecodeSettings::default()
    };
    let image = match Image::new(bytes, &settings) {
        Ok(image) => image,
        Err(error) => return format!("parse {error:?}"),
    };
    let mut context = DecoderContext::default();
    context.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
    let hash = |planes: &[Vec<u32>]| {
        planes
            .iter()
            .flatten()
            .fold(0xcbf2_9ce4_8422_2325_u64, |hash, &bits| {
                (hash ^ u64::from(bits)).wrapping_mul(0x0100_0000_01b3)
            })
    };
    if rows {
        let mut sink = CollectingSink::new(roi.2, roi.3);
        match image.decode_region_rows_for_integer_output_with_context(roi, &mut context, &mut sink)
        {
            Ok(()) => format!("ok {:016x}", hash(&sink.planes)),
            Err(error) => format!("decode {error:?}"),
        }
    } else {
        match image.decode_region_components_for_integer_output_with_context(roi, &mut context) {
            Ok(components) => {
                let planes: Vec<Vec<u32>> = components
                    .planes()
                    .iter()
                    .map(|plane| {
                        plane.samples()[..(roi.2 * roi.3) as usize]
                            .iter()
                            .map(|sample| sample.to_bits())
                            .collect()
                    })
                    .collect();
                format!("ok {:016x}", hash(&planes))
            }
            Err(error) => format!("decode {error:?}"),
        }
    }
}

#[test]
fn row_sink_rejects_corrupt_code_blocks_as_component_planes_do() {
    // Small code-blocks give each component many bands, and the components
    // decode together, so two corrupt blocks often fail in a different order
    // than the planes path decodes them.
    const SIZE: u32 = 48;
    let mut state = 0x9e37_79b9_7f4a_7c15_u64;
    let mut next = move || {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        state
    };
    let pixels: Vec<u8> = (0..SIZE * SIZE * 3).map(|_| (next() >> 56) as u8).collect();
    for use_ht_block_coding in [false, true] {
        let bytes = encode(
            &pixels,
            SIZE,
            SIZE,
            3,
            8,
            false,
            &EncodeOptions {
                reversible: true,
                num_decomposition_levels: 2,
                code_block_width_exp: 0,
                code_block_height_exp: 0,
                num_layers: 2,
                use_ht_block_coding,
                ..EncodeOptions::default()
            },
        )
        .expect("encode corrupt-block fixture");
        let mut failures = 0;
        for iteration in 0..300 {
            let mut mutant = bytes.clone();
            for _ in 0..=next() % 4 {
                let span = u64::try_from(mutant.len() - 120).expect("fixture length");
                let at = 120 + usize::try_from(next() % span).expect("offset in the fixture");
                mutant[at] ^= 1 << (next() % 8);
            }
            for strict in [false, true] {
                let roi = [(0, 0, SIZE, SIZE), (30, 30, 18, 18), (5, 9, 13, 7)]
                    [usize::try_from(next() % 3).expect("region index")];
                let planes = outcome(&mutant, strict, roi, false);
                let rows = outcome(&mutant, strict, roi, true);
                assert_eq!(
                    rows, planes,
                    "ht={use_ht_block_coding} iteration={iteration} strict={strict} roi={roi:?}"
                );
                failures += usize::from(planes.starts_with("decode"));
            }
        }
        // The mutants must reach Tier-1 failures for the check to mean much.
        assert!(
            failures > 20,
            "ht={use_ht_block_coding}: {failures} failures"
        );
    }
}

/// The rows of a streamed decode on `context`, and its starting budget.
fn streamed_rows<'a>(
    image: &Image<'a>,
    roi: (u32, u32, u32, u32),
    context: &mut DecoderContext<'a>,
) -> (Vec<Vec<u32>>, usize) {
    let mut sink = CollectingSink::new(roi.2, roi.3);
    image
        .decode_region_rows_for_integer_output_with_context(roi, context, &mut sink)
        .expect("decode region rows");
    assert!(context.tile_decode_context.stream_rows);
    assert!(sink.arrivals.iter().all(|&count| count == 1));
    (
        sink.planes,
        context
            .tile_decode_context
            .debug_counters
            .decode_baseline_bytes,
    )
}

/// A decode that streams rows can't be rerun once rows reach the sink, so a
/// reused context must start it from a fresh context's budget: no retained
/// scratch or component planes charged, and the same rows.
#[test]
fn reused_context_streams_rows_from_a_fresh_budget() {
    let pixels = noisy_pixels(3);
    let bytes = encode(
        &pixels,
        WIDTH,
        HEIGHT,
        3,
        8,
        false,
        &EncodeOptions {
            num_decomposition_levels: 4,
            tile_size: Some((TILE, TILE)),
            ..EncodeOptions::default()
        },
    )
    .expect("encode reuse fixture");
    let image = Image::new(&bytes, &DecodeSettings::default()).expect("parse reuse fixture");
    let roi = (37, 21, 130, 101);

    let mut fresh = DecoderContext::default();
    fresh.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
    let (fresh_planes, fresh_baseline) = streamed_rows(&image, roi, &mut fresh);

    let mut reused = DecoderContext::default();
    reused.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
    drop(
        image
            .decode_region_components_for_integer_output_with_context(
                (0, 0, WIDTH, HEIGHT),
                &mut reused,
            )
            .expect("decode full planes"),
    );
    assert!(reused.tile_decode_context.retained_channel_bytes().unwrap() > 0);
    assert!(reused.tile_decode_context.retained_scratch_bytes().unwrap() > 0);
    let (reused_planes, reused_baseline) = streamed_rows(&image, roi, &mut reused);

    assert_eq!(reused_planes, fresh_planes);
    assert_eq!(reused_baseline, fresh_baseline);
    assert_eq!(
        reused.tile_decode_context.retained_channel_bytes().unwrap(),
        fresh.tile_decode_context.retained_channel_bytes().unwrap()
    );
}

/// The ROI plan's IDWT charge must cover the ring buffer each streamed tile
/// takes: every component's rings, and a scratch row each. `take_line_buffer`
/// asserts it for every tile of every streamed decode; this runs it over the
/// row-sink cases, multi-tile ones included, and three regions.
#[test]
fn streamed_rows_charge_every_components_rings() {
    for case in row_sink_cases() {
        let pixels = noisy_pixels(case.components);
        let bytes = encode(
            &pixels,
            WIDTH,
            HEIGHT,
            u16::from(case.components),
            8,
            false,
            &case.options,
        )
        .expect("encode ring charge fixture");
        let image = Image::new(&bytes, &DecodeSettings::default()).expect("parse ring fixture");
        for roi in [(0, 0, WIDTH, HEIGHT), (37, 21, 130, 101), (63, 60, 9, 70)] {
            let mut context = DecoderContext::default();
            context.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
            streamed_rows(&image, roi, &mut context);
        }
    }
}

/// A streamed decode that holds the image's packet directory and runs out of
/// budget gives the directory up and reruns only while no row has reached the
/// sink, since a rerun after that would send those rows again. This decode
/// peaks before its first row, so it reruns, and every sample still arrives
/// once.
#[cfg(feature = "std")]
#[test]
fn streamed_rows_evict_the_packet_directory_before_the_first_row() {
    let pixels = noisy_pixels(3);
    let bytes = encode(
        &pixels,
        WIDTH,
        HEIGHT,
        3,
        8,
        false,
        &EncodeOptions {
            num_decomposition_levels: 4,
            // A packet directory needs one RPCL tile.
            progression_order: crate::EncodeProgressionOrder::Rpcl,
            ..EncodeOptions::default()
        },
    )
    .expect("encode directory fixture");
    let roi = (37, 21, 130, 101);
    let decode = |image: &Image<'_>, baseline: usize| {
        let mut context = DecoderContext::default();
        context.set_cpu_decode_parallelism(CpuDecodeParallelism::Serial);
        let mut sink = CollectingSink::new(roi.2, roi.3);
        let result = image.run_decode_rows(
            &mut context,
            Some(roi),
            None,
            true,
            baseline,
            Some(&mut sink),
        );
        (result, sink, context.tile_decode_context.stream_rows)
    };
    // The largest baseline a decode without a directory fits in.
    let (mut fits, mut fails) = (0, crate::DEFAULT_MAX_DECODE_BYTES);
    while fails - fits > 1 {
        let candidate = fits + (fails - fits) / 2;
        let fresh = Image::new(&bytes, &DecodeSettings::default()).expect("parse fixture");
        match decode(&fresh, candidate).0 {
            Ok(_) => fits = candidate,
            Err(error) => {
                assert!(crate::j2c::is_capacity_error(&error), "{error:?}");
                fails = candidate;
            }
        }
    }
    let fresh = Image::new(&bytes, &DecodeSettings::default()).expect("parse fixture");
    let (result, expected, streamed) = decode(&fresh, fits);
    result.expect("a fresh image fits");
    assert!(streamed);

    let image = Image::new(&bytes, &DecodeSettings::default()).expect("parse fixture");
    let metadata = image.retained_allocation_bytes().expect("metadata bytes");
    for _ in 0..2 {
        decode(&image, metadata).0.expect("decode region rows");
    }
    assert!(
        image.retained_allocation_bytes().expect("retained bytes") > metadata,
        "a repeated region builds a directory"
    );
    let (result, sink, streamed) = decode(&image, fits);
    result.expect("evicting the directory restores a fitting request");
    assert!(streamed);
    assert!(sink.arrivals.iter().all(|&count| count == 1));
    assert_eq!(sink.planes, expected.planes);
    assert_eq!(
        image.retained_allocation_bytes().expect("retained bytes"),
        metadata
    );
}