j2k-metal 0.7.2

Metal decoder and encode-stage adapter 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
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::{
    compute, encode_lossless_tile_to_metal_buffer_with_report, encode_lossless_tile_with_report,
    lossless_sample_shape, plan_resident_lossless_buffer_encode,
    resident_lossless_encode_config_for_mode, resolve_lossless_encode_config,
    should_try_resident_lossless_host_encode, should_try_resident_lossless_host_encode_for_tiles,
    submit_planned_resident_lossless_tiles, try_encode_lossless_tile_device_resident_with_report,
    validate_metal_encode_tile, validate_padded_contiguous_metal_encode_tile,
    wait_submitted_resident_lossless_buffer_encode_batch, EncodeBackendPreference, Instant,
    J2kBlockCodingMode, J2kLosslessEncodeOptions, MetalEncodeInputStaging,
    MetalEncodeStageAccelerator, MetalLosslessBufferEncodeBatchOutcome,
    MetalLosslessBufferEncodeOutcome, MetalLosslessEncodeBatchRequest,
    MetalLosslessEncodeBatchStats, MetalLosslessEncodeConfig, MetalLosslessEncodeOutcome,
    MetalLosslessEncodeTile, OwnedMetalLosslessEncodeTile, PlannedResidentLosslessBufferEncode,
    SubmittedJ2kLosslessMetalBufferEncodeBatch, SubmittedJ2kLosslessMetalBufferEncodeBatchState,
    SubmittedJ2kLosslessMetalEncodeBatch, SubmittedJ2kLosslessMetalEncodeBatchState,
    SubmittedResidentLosslessMetalBufferEncodeBatch,
};

#[cfg(target_os = "macos")]
/// Submit a lossless tile batch that resolves to host codestream bytes.
pub fn submit_lossless_batch(
    request: MetalLosslessEncodeBatchRequest<'_, '_>,
    options: &J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
) -> Result<SubmittedJ2kLosslessMetalEncodeBatch, crate::Error> {
    validate_resident_tile_devices(request.tiles, session)?;
    submit_lossless_tiles(
        request.tiles,
        *options,
        session,
        request.staging,
        request.config,
    )
}

#[cfg(target_os = "macos")]
/// Submit a lossless tile batch that resolves to Metal-backed codestreams.
pub fn submit_lossless_batch_to_metal(
    request: MetalLosslessEncodeBatchRequest<'_, '_>,
    options: &J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
) -> Result<SubmittedJ2kLosslessMetalBufferEncodeBatch, crate::Error> {
    validate_resident_tile_devices(request.tiles, session)?;
    submit_lossless_tiles_to_metal_buffer_batch(
        request.tiles,
        *options,
        session,
        request.staging,
        request.config,
    )
}

#[cfg(target_os = "macos")]
/// Encode a lossless tile batch and return host-byte timing reports.
#[doc(hidden)]
pub fn encode_lossless_batch_with_report(
    request: MetalLosslessEncodeBatchRequest<'_, '_>,
    options: &J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
) -> Result<Vec<MetalLosslessEncodeOutcome>, crate::Error> {
    validate_resident_tile_devices(request.tiles, session)?;
    encode_lossless_tiles_with_report(
        request.tiles,
        *options,
        session,
        request.staging,
        request.config,
    )
}

#[cfg(target_os = "macos")]
fn validate_resident_tile_devices(
    tiles: &[MetalLosslessEncodeTile<'_>],
    session: &crate::MetalBackendSession,
) -> Result<(), crate::Error> {
    for &tile in tiles {
        tile.validate_device(session.device())?;
    }
    Ok(())
}

#[cfg(target_os = "macos")]
pub(super) fn host_outcome_from_buffer_outcome(
    outcome: MetalLosslessBufferEncodeOutcome,
) -> Result<MetalLosslessEncodeOutcome, crate::Error> {
    let (encoded, host_readback_duration) =
        outcome.encoded.to_encoded_j2k_with_readback_duration()?;
    Ok(MetalLosslessEncodeOutcome {
        encoded,
        input_copy_used: outcome.input_copy_used,
        resident: outcome.resident,
        input_copy_duration: outcome.input_copy_duration,
        encode_duration: outcome.encode_duration,
        gpu_duration: outcome.gpu_duration,
        validation_duration: outcome.validation_duration,
        host_readback_duration,
    })
}

#[cfg(target_os = "macos")]
fn encode_lossless_tiles_with_report(
    tiles: &[MetalLosslessEncodeTile<'_>],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<Vec<MetalLosslessEncodeOutcome>, crate::Error> {
    if should_try_resident_lossless_host_encode_for_tiles(tiles, options, staging) {
        let batch = try_encode_resident_lossless_tiles_to_metal_buffer_batch(
            tiles, options, session, staging, config,
        )?;
        if let Some(outcomes) = batch {
            let mut budget = crate::batch_allocation::BatchMetadataBudget::new(
                "J2K Metal resident host encode outcomes",
            );
            let mut results = budget.try_vec(
                outcomes.outcomes.len(),
                "J2K Metal resident host encode outcome slots",
            )?;
            for outcome in outcomes.outcomes {
                results.push(host_outcome_from_buffer_outcome(outcome)?);
            }
            return Ok(results);
        }
    }

    let mut accelerator = MetalEncodeStageAccelerator::for_host_output(options);
    let mut budget =
        crate::batch_allocation::BatchMetadataBudget::new("J2K Metal host encode outcomes");
    let mut results = budget.try_vec(tiles.len(), "J2K Metal host encode outcome slots")?;
    for &tile in tiles {
        results.push(encode_lossless_tile_with_report(
            tile,
            options,
            session,
            staging,
            &mut accelerator,
        )?);
    }
    Ok(results)
}

#[cfg(target_os = "macos")]
pub(super) fn encode_lossless_owned_tiles_with_report(
    tiles: &[OwnedMetalLosslessEncodeTile],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<Vec<MetalLosslessEncodeOutcome>, crate::Error> {
    let mut budget =
        crate::batch_allocation::BatchMetadataBudget::new("J2K Metal owned host encode batch");
    let mut borrowed = budget.try_vec(tiles.len(), "J2K Metal borrowed owned encode tiles")?;
    borrowed.extend(tiles.iter().map(OwnedMetalLosslessEncodeTile::as_tile));
    if should_try_resident_lossless_host_encode_for_tiles(&borrowed, options, staging) {
        let batch = try_encode_resident_lossless_tiles_to_metal_buffer_batch(
            &borrowed, options, session, staging, config,
        )?;
        if let Some(outcomes) = batch {
            let mut results = budget.try_vec(
                outcomes.outcomes.len(),
                "J2K Metal owned resident host encode outcomes",
            )?;
            for outcome in outcomes.outcomes {
                results.push(host_outcome_from_buffer_outcome(outcome)?);
            }
            return Ok(results);
        }
    }

    let mut accelerator = MetalEncodeStageAccelerator::for_host_output(options);
    let mut results =
        budget.try_vec(borrowed.len(), "J2K Metal owned host encode outcome slots")?;
    for &tile in &borrowed {
        results.push(encode_lossless_tile_with_report(
            tile,
            options,
            session,
            staging,
            &mut accelerator,
        )?);
    }
    Ok(results)
}

#[cfg(target_os = "macos")]
fn submit_lossless_tiles_to_metal_buffer_batch(
    tiles: &[MetalLosslessEncodeTile<'_>],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<SubmittedJ2kLosslessMetalBufferEncodeBatch, crate::Error> {
    if options.backend != EncodeBackendPreference::CpuOnly {
        if let Some(submitted) = try_submit_resident_lossless_tiles_to_metal_buffer_batch(
            tiles, options, session, staging, config,
        )? {
            return Ok(SubmittedJ2kLosslessMetalBufferEncodeBatch {
                state: SubmittedJ2kLosslessMetalBufferEncodeBatchState::Resident(Box::new(
                    submitted,
                )),
            });
        }
    }

    let mut budget =
        crate::batch_allocation::BatchMetadataBudget::new("J2K Metal deferred buffer encode batch");
    let mut owned = budget.try_vec(tiles.len(), "J2K Metal owned deferred buffer encode tiles")?;
    for &tile in tiles {
        validate_metal_encode_tile(tile)?;
        if matches!(staging, MetalEncodeInputStaging::AlreadyPaddedContiguous) {
            lossless_sample_shape(tile.format)?;
            validate_padded_contiguous_metal_encode_tile(tile, tile.format.bytes_per_pixel())?;
        }
        owned.push(OwnedMetalLosslessEncodeTile::from_tile(tile));
    }
    Ok(SubmittedJ2kLosslessMetalBufferEncodeBatch {
        state: SubmittedJ2kLosslessMetalBufferEncodeBatchState::Deferred {
            tiles: owned,
            options,
            session: session.clone(),
            staging,
        },
    })
}

#[cfg(target_os = "macos")]
pub(super) fn encode_owned_lossless_tiles_to_metal_buffer_fallback_batch(
    tiles: &[OwnedMetalLosslessEncodeTile],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
) -> Result<MetalLosslessBufferEncodeBatchOutcome, crate::Error> {
    let mut budget = crate::batch_allocation::BatchMetadataBudget::new(
        "J2K Metal fallback buffer encode outcomes",
    );
    let mut outcomes = budget.try_vec(
        tiles.len(),
        "J2K Metal fallback buffer encode outcome slots",
    )?;
    for tile in tiles {
        outcomes.push(encode_lossless_tile_to_metal_buffer_with_report(
            tile.as_tile(),
            options,
            session,
            staging,
        )?);
    }
    Ok(MetalLosslessBufferEncodeBatchOutcome {
        outcomes,
        stats: MetalLosslessEncodeBatchStats::default(),
    })
}

#[cfg(target_os = "macos")]
fn try_submit_resident_lossless_tiles_to_metal_buffer_batch(
    tiles: &[MetalLosslessEncodeTile<'_>],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<Option<SubmittedResidentLosslessMetalBufferEncodeBatch>, crate::Error> {
    let profile_stages = compute::metal_profile_stages_enabled();
    if tiles.is_empty() {
        return Ok(Some(SubmittedResidentLosslessMetalBufferEncodeBatch {
            options,
            session: session.clone(),
            stats: resolve_lossless_encode_config(0, 1, config)?,
            encode_started: Instant::now(),
            tiles: Vec::new(),
            staging,
            pipeline: None,
        }));
    }

    let plan_started = profile_stages.then(Instant::now);
    let mut budget =
        crate::batch_allocation::BatchMetadataBudget::new("J2K Metal resident encode batch plan");
    let mut planned = budget.try_vec(tiles.len(), "J2K Metal resident encode planned tiles")?;
    for (index, &tile) in tiles.iter().enumerate() {
        let Some(item) = plan_resident_lossless_buffer_encode(index, tile, options, staging)?
        else {
            return Ok(None);
        };
        planned.push(item);
    }
    let estimated_peak_bytes_per_tile = planned
        .iter()
        .map(PlannedResidentLosslessBufferEncode::estimated_peak_bytes)
        .max()
        .unwrap_or(1);
    let classic_resident_mode = planned
        .iter()
        .all(|planned| planned.metadata.plan.block_coding_mode == J2kBlockCodingMode::Classic);
    let ht_resident_mode = planned.iter().all(|planned| {
        planned.metadata.plan.block_coding_mode == J2kBlockCodingMode::HighThroughput
    });
    if !(classic_resident_mode || ht_resident_mode) {
        return Ok(None);
    }
    let resolved_config =
        resident_lossless_encode_config_for_mode(config, classic_resident_mode, tiles.len());
    let mut stats = resolve_lossless_encode_config(
        tiles.len(),
        estimated_peak_bytes_per_tile,
        resolved_config,
    )?;
    if let Some(started) = plan_started {
        stats.stage_stats.plan_duration = started.elapsed();
    }
    let encode_started = Instant::now();
    let pipeline = submit_planned_resident_lossless_tiles(
        planned,
        session,
        stats.effective_inflight_tiles,
        &mut stats,
    )?;
    let mut owned_tiles =
        budget.try_vec(tiles.len(), "J2K Metal retained resident encode tiles")?;
    owned_tiles.extend(
        tiles
            .iter()
            .map(|&tile| OwnedMetalLosslessEncodeTile::from_tile(tile)),
    );
    Ok(Some(SubmittedResidentLosslessMetalBufferEncodeBatch {
        options,
        session: session.clone(),
        stats,
        encode_started,
        tiles: owned_tiles,
        staging,
        pipeline,
    }))
}

#[cfg(target_os = "macos")]
fn try_encode_resident_lossless_tiles_to_metal_buffer_batch(
    tiles: &[MetalLosslessEncodeTile<'_>],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<Option<MetalLosslessBufferEncodeBatchOutcome>, crate::Error> {
    let Some(submitted) = try_submit_resident_lossless_tiles_to_metal_buffer_batch(
        tiles, options, session, staging, config,
    )?
    else {
        return Ok(None);
    };
    wait_submitted_resident_lossless_buffer_encode_batch(submitted).map(Some)
}

#[cfg(target_os = "macos")]
fn submit_lossless_tiles(
    tiles: &[MetalLosslessEncodeTile<'_>],
    options: J2kLosslessEncodeOptions,
    session: &crate::MetalBackendSession,
    staging: MetalEncodeInputStaging,
    config: MetalLosslessEncodeConfig,
) -> Result<SubmittedJ2kLosslessMetalEncodeBatch, crate::Error> {
    if matches!(staging, MetalEncodeInputStaging::AlreadyPaddedContiguous)
        && should_try_resident_lossless_host_encode(options)
    {
        let mut budget = crate::batch_allocation::BatchMetadataBudget::new(
            "J2K Metal ready resident encode batch",
        );
        let mut ready = budget.try_vec(tiles.len(), "J2K Metal ready resident encode results")?;
        let mut all_ready = true;
        for &tile in tiles {
            validate_metal_encode_tile(tile)?;
            lossless_sample_shape(tile.format)?;
            validate_padded_contiguous_metal_encode_tile(tile, tile.format.bytes_per_pixel())?;
            if let Some(outcome) = try_encode_lossless_tile_device_resident_with_report(
                tile, options, session, staging,
            )? {
                ready.push(outcome.encoded);
            } else {
                all_ready = false;
                break;
            }
        }
        if all_ready {
            return Ok(SubmittedJ2kLosslessMetalEncodeBatch {
                state: SubmittedJ2kLosslessMetalEncodeBatchState::Ready(ready),
            });
        }
        if options.backend == EncodeBackendPreference::RequireDevice {
            return Err(crate::Error::UnsupportedMetalRequest {
                reason: "J2K Metal resident encode requires classic padded contiguous Gray/RGB lossless input with at most one DWT level",
            });
        }
    }

    let mut budget =
        crate::batch_allocation::BatchMetadataBudget::new("J2K Metal deferred encode batch");
    let mut owned = budget.try_vec(tiles.len(), "J2K Metal owned deferred encode tiles")?;
    for &tile in tiles {
        validate_metal_encode_tile(tile)?;
        if matches!(staging, MetalEncodeInputStaging::AlreadyPaddedContiguous) {
            lossless_sample_shape(tile.format)?;
            validate_padded_contiguous_metal_encode_tile(tile, tile.format.bytes_per_pixel())?;
        }
        owned.push(OwnedMetalLosslessEncodeTile::from_tile(tile));
    }
    Ok(SubmittedJ2kLosslessMetalEncodeBatch {
        state: SubmittedJ2kLosslessMetalEncodeBatchState::Deferred {
            tiles: owned,
            options,
            session: session.clone(),
            staging,
            config,
        },
    })
}