j2k-cuda 0.8.1

CUDA adapter for resident HTJ2K decode/encode and shared JPEG 2000 stages
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
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::*;
use j2k_core::CodecError;
use j2k_native::{
    encode, DecodeSettings, DecoderContext, EncodeOptions, HtOwnedCodeBlockBatchJob,
    HtOwnedSubBandPlan, Image, J2kDirectIdwtStep, J2kDirectStoreStep, J2kRect,
};

fn one_block_direct_plan(
    cleanup_length: u32,
    refinement_length: u32,
    data: Vec<u8>,
    output_stride: usize,
) -> J2kDirectGrayscalePlan {
    J2kDirectGrayscalePlan {
        dimensions: (1, 1),
        bit_depth: 8,
        steps: vec![
            J2kDirectGrayscaleStep::HtSubBand(HtOwnedSubBandPlan {
                band_id: 0,
                rect: J2kRect {
                    x0: 0,
                    y0: 0,
                    x1: 1,
                    y1: 1,
                },
                width: 1,
                height: 1,
                jobs: vec![HtOwnedCodeBlockBatchJob {
                    output_x: 0,
                    output_y: 0,
                    data,
                    cleanup_length,
                    refinement_length,
                    width: 1,
                    height: 1,
                    output_stride,
                    missing_bit_planes: 0,
                    number_of_coding_passes: 1,
                    num_bitplanes: 8,
                    roi_shift: 0,
                    stripe_causal: false,
                    strict: true,
                    dequantization_step: 1.0,
                }],
            }),
            J2kDirectGrayscaleStep::Store(J2kDirectStoreStep {
                input_band_id: 0,
                input_rect: J2kRect {
                    x0: 0,
                    y0: 0,
                    x1: 1,
                    y1: 1,
                },
                source_x: 0,
                source_y: 0,
                copy_width: 1,
                copy_height: 1,
                output_width: 1,
                output_height: 1,
                output_x: 0,
                output_y: 0,
                addend: 128.0,
            }),
        ],
    }
}

fn one_block_plan(data: Vec<u8>) -> CudaHtj2kDecodePlan {
    let payload_len = u32::try_from(data.len()).expect("fixture payload length");
    let direct = one_block_direct_plan(payload_len, 0, data, 1);
    CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
        .expect("CUDA plan")
}

fn two_block_direct_plan() -> J2kDirectGrayscalePlan {
    J2kDirectGrayscalePlan {
        dimensions: (2, 1),
        bit_depth: 8,
        steps: vec![
            J2kDirectGrayscaleStep::HtSubBand(HtOwnedSubBandPlan {
                band_id: 0,
                rect: J2kRect {
                    x0: 0,
                    y0: 0,
                    x1: 2,
                    y1: 1,
                },
                width: 2,
                height: 1,
                jobs: vec![
                    HtOwnedCodeBlockBatchJob {
                        output_x: 0,
                        output_y: 0,
                        data: vec![1],
                        cleanup_length: 1,
                        refinement_length: 0,
                        width: 1,
                        height: 1,
                        output_stride: 2,
                        missing_bit_planes: 0,
                        number_of_coding_passes: 1,
                        num_bitplanes: 8,
                        roi_shift: 0,
                        stripe_causal: false,
                        strict: true,
                        dequantization_step: 1.0,
                    },
                    HtOwnedCodeBlockBatchJob {
                        output_x: 1,
                        output_y: 0,
                        data: vec![2],
                        cleanup_length: 1,
                        refinement_length: 0,
                        width: 1,
                        height: 1,
                        output_stride: 2,
                        missing_bit_planes: 0,
                        number_of_coding_passes: 1,
                        num_bitplanes: 8,
                        roi_shift: 0,
                        stripe_causal: false,
                        strict: true,
                        dequantization_step: 1.0,
                    },
                ],
            }),
            J2kDirectGrayscaleStep::Store(J2kDirectStoreStep {
                input_band_id: 0,
                input_rect: J2kRect {
                    x0: 0,
                    y0: 0,
                    x1: 2,
                    y1: 1,
                },
                source_x: 0,
                source_y: 0,
                copy_width: 2,
                copy_height: 1,
                output_width: 2,
                output_height: 1,
                output_x: 0,
                output_y: 0,
                addend: 128.0,
            }),
        ],
    }
}

#[test]
fn classic_cuda_plan_retains_irreversible_midpoint_reconstruction() {
    let pixels = j2k_test_support::gradient_u8(16, 16, 1);
    let bytes = encode(
        &pixels,
        16,
        16,
        1,
        8,
        false,
        &EncodeOptions {
            reversible: false,
            num_decomposition_levels: 2,
            ..EncodeOptions::default()
        },
    )
    .expect("encode irreversible grayscale");
    let image = Image::new(&bytes, &DecodeSettings::default()).expect("image");
    let mut context = DecoderContext::default();
    let direct = image
        .build_direct_grayscale_plan_with_context(&mut context)
        .expect("direct plan");

    let cuda = CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
        .expect("CUDA plan");

    assert!(
        cuda.classic_subbands
            .iter()
            .all(|subband| subband.irreversible_midpoint),
        "every classic CUDA sub-band must retain the 9/7 reconstruction rule"
    );
}

#[test]
fn classic_cuda_plan_accepts_roi_maxshift() {
    let pixels = j2k_test_support::gradient_u8(16, 16, 1);
    let bytes = encode(
        &pixels,
        16,
        16,
        1,
        8,
        false,
        &EncodeOptions {
            reversible: true,
            num_decomposition_levels: 2,
            roi_component_shifts: vec![7],
            ..EncodeOptions::default()
        },
    )
    .expect("encode ROI maxshift grayscale");
    let image = Image::new(&bytes, &DecodeSettings::default()).expect("image");
    let mut context = DecoderContext::default();
    let direct = image
        .build_direct_grayscale_plan_with_context(&mut context)
        .expect("direct ROI plan");

    let cuda = CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
        .expect("CUDA classic ROI plan");
    assert!(
        cuda.classic_code_blocks
            .iter()
            .all(|block| block.roi_shift == 7),
        "every classic CUDA code-block must retain the component ROI maxshift"
    );
}

#[test]
fn append_payload_to_shared_offsets_blocks_and_drains_local_payload() {
    let mut first = one_block_plan(vec![1, 2]);
    let mut second = one_block_plan(vec![3, 4, 5]);
    let mut shared = Vec::new();

    first
        .append_payload_to_shared(&mut shared)
        .expect("append first payload");
    second
        .append_payload_to_shared(&mut shared)
        .expect("append second payload");

    assert_eq!(shared, vec![1, 2, 3, 4, 5]);
    assert!(first.payload().is_empty());
    assert!(second.payload().is_empty());
    assert_eq!(first.payload.capacity(), 0);
    assert_eq!(second.payload.capacity(), 0);
    assert_eq!(first.code_blocks()[0].payload_offset, 0);
    assert_eq!(second.code_blocks()[0].payload_offset, 2);
}

#[test]
fn rebase_payload_offsets_preserves_shared_payload_for_larger_batch() {
    let mut plan = one_block_plan(vec![7, 8]);
    let mut shared = Vec::new();
    plan.append_payload_to_shared(&mut shared)
        .expect("append local payload");

    plan.rebase_payload_offsets(4096).expect("rebase payload");

    assert_eq!(shared, vec![7, 8]);
    assert_eq!(plan.code_blocks()[0].payload_offset, 4096);
}

#[test]
fn full_frame_plan_keeps_all_blocks_while_region_plan_prunes() {
    let direct = two_block_direct_plan();
    let full = CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
        .expect("full CUDA plan");
    let mut region_direct = two_block_direct_plan();
    let J2kDirectGrayscaleStep::Store(store) = &mut region_direct.steps[1] else {
        panic!("expected store fixture");
    };
    store.source_x = 1;
    store.copy_width = 1;
    store.output_x = 1;
    let region = CudaHtj2kDecodePlan::from_grayscale_direct_plan_region(
        &region_direct,
        PixelFormat::Gray8,
        (1, 0),
        (1, 1),
    )
    .expect("region CUDA plan");

    assert_eq!(full.code_blocks().len(), 2);
    assert_eq!(region.code_blocks().len(), 1);
    assert_eq!(region.code_blocks()[0].output_x, 1);
}

#[test]
fn rejects_block_length_mismatch() {
    let direct = one_block_direct_plan(1, 2, vec![0xAA, 0xBB], 1);

    let error =
        CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
            .expect_err("mismatched cleanup/refinement lengths must be rejected");

    assert!(error.is_unsupported());
    assert!(
        error
            .to_string()
            .contains("block lengths do not match payload bytes"),
        "unexpected error: {error}"
    );
}

#[test]
fn rejects_ht_roi_maxshift_jobs() {
    let mut direct = one_block_direct_plan(1, 0, vec![0xAA], 1);
    let J2kDirectGrayscaleStep::HtSubBand(subband) = &mut direct.steps[0] else {
        panic!("fixture starts with one HT sub-band");
    };
    subband.jobs[0].roi_shift = 7;

    let error =
        CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
            .expect_err("ROI maxshift jobs must be rejected");

    assert!(error.is_unsupported());
    assert!(
        error.to_string().contains("ROI maxshift decode"),
        "unexpected error: {error}"
    );
}

#[test]
fn rejects_output_stride_overflow() {
    let direct = one_block_direct_plan(1, 0, vec![0xAA], usize::MAX);

    let error =
        CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
            .expect_err("unrepresentable output stride must be rejected");

    assert!(error.is_unsupported());
}

#[test]
fn rejects_mixed_idwt_transforms() {
    let mut direct = one_block_direct_plan(1, 0, vec![0xAA], 1);
    let rect = J2kRect {
        x0: 0,
        y0: 0,
        x1: 1,
        y1: 1,
    };
    direct.steps.insert(
        1,
        J2kDirectGrayscaleStep::Idwt(J2kDirectIdwtStep {
            output_band_id: 4,
            rect,
            transform: J2kWaveletTransform::Reversible53,
            ll_band_id: 0,
            ll: rect,
            hl_band_id: 1,
            hl: rect,
            lh_band_id: 2,
            lh: rect,
            hh_band_id: 3,
            hh: rect,
        }),
    );
    direct.steps.insert(
        2,
        J2kDirectGrayscaleStep::Idwt(J2kDirectIdwtStep {
            output_band_id: 8,
            rect,
            transform: J2kWaveletTransform::Irreversible97,
            ll_band_id: 4,
            ll: rect,
            hl_band_id: 5,
            hl: rect,
            lh_band_id: 6,
            lh: rect,
            hh_band_id: 7,
            hh: rect,
        }),
    );

    let error =
        CudaHtj2kDecodePlan::from_grayscale_direct_plan(&direct, PixelFormat::Gray8, (0, 0))
            .expect_err("mixed transforms must be rejected");

    assert!(error.is_unsupported());
    assert!(
        error.to_string().contains("mixed DWT transforms"),
        "unexpected error: {error}"
    );
}

#[test]
fn region_plan_rejects_store_outside_output_rect() {
    let direct = one_block_direct_plan(1, 0, vec![0xAA], 1);

    let error = CudaHtj2kDecodePlan::from_grayscale_direct_plan_region(
        &direct,
        PixelFormat::Gray8,
        (1, 1),
        (0, 0),
    )
    .expect_err("store outside compact output rectangle must be rejected");

    assert!(error.is_unsupported());
    assert!(
        error
            .to_string()
            .contains("store does not fit the requested output rectangle"),
        "unexpected error: {error}"
    );
}

#[test]
fn referenced_prepared_plan_materializes_only_the_execution_arena() {
    let encoded = std::sync::Arc::<[u8]>::from(
        j2k_native::encode_htj2k(
            &(0_u8..64).collect::<Vec<_>>(),
            8,
            8,
            1,
            8,
            false,
            &j2k_native::EncodeOptions {
                reversible: true,
                num_decomposition_levels: 1,
                ..j2k_native::EncodeOptions::default()
            },
        )
        .expect("encode referenced-plan fixture"),
    );
    let prepared = j2k::prepare_batch(
        vec![j2k::EncodedImage::full(std::sync::Arc::clone(&encoded))],
        j2k::BatchDecodeOptions::default(),
    )
    .expect("prepare referenced-plan fixture");
    let image = &prepared.groups()[0].images()[0];
    let prepared_plan = image.htj2k_plan().expect("retained HTJ2K plan");
    let referenced = prepared_plan
        .adapter_view()
        .downcast_ref::<j2k_native::J2kReferencedHtj2kPlan>()
        .expect("native referenced HTJ2K plan adapter");
    let tile = &referenced.tiles()[0];
    let geometry = tile
        .grayscale_geometry()
        .expect("grayscale referenced tile geometry");
    let span = tile.payload_records();
    let payload_end = span.end_record().expect("payload span end");
    let tile_payloads = &referenced.payloads()[span.first_record..payload_end];
    let mut expected = Vec::new();
    for payload in referenced.payloads() {
        let cleanup_end = payload.cleanup.end().expect("cleanup range end");
        expected.extend_from_slice(&encoded[payload.cleanup.offset..cleanup_end]);
        if let Some(refinement) = payload.refinement {
            let refinement_end = refinement.end().expect("refinement range end");
            expected.extend_from_slice(&encoded[refinement.offset..refinement_end]);
        }
    }

    let output = image.plan().output_rect();
    let mut shared = Vec::new();
    let mut budget = crate::allocation::HostPhaseBudget::new("referenced CUDA plan test");
    let cuda = CudaHtj2kDecodePlan::from_referenced_tile_grayscale_plan_into_shared(
        geometry,
        tile_payloads,
        &encoded,
        PixelFormat::Gray8,
        (output.x, output.y),
        (output.w, output.h),
        &mut shared,
        &mut budget,
    )
    .expect("flatten referenced CUDA plan");

    assert_eq!(shared, expected);
    assert!(cuda.payload().is_empty());
    assert_eq!(cuda.code_blocks().len(), referenced.payloads().len());
    assert_eq!(cuda.code_blocks()[0].payload_offset, 0);
}