1#![deny(missing_docs)]
11
12use burn_core::tensor::{backend::Backend, Int, Tensor};
13use j2k::{BatchGroupInfo, IndexedBatchError, J2kDecodeWarning, Rect};
14
15#[cfg(any(
16 feature = "cpu",
17 feature = "cuda",
18 all(feature = "metal", target_os = "macos")
19))]
20mod batch_contract;
21#[cfg(test)]
22mod completion;
23mod error;
24#[cfg(any(feature = "cuda", all(feature = "metal", target_os = "macos")))]
25mod staging;
26
27#[cfg(feature = "cpu")]
28pub mod cpu;
29#[cfg(feature = "cuda")]
30pub mod cuda;
31#[cfg(feature = "metal")]
32pub mod metal;
33
34#[cfg(feature = "cpu")]
35pub use cpu::CpuBurnDecoder;
36#[cfg(feature = "cuda")]
37pub use cuda::{
38 CudaBurnDecoder, CudaUploadBurnDecoder, SubmittedCudaBurnBatch, SubmittedCudaUploadBurnBatch,
39};
40pub use error::BurnDecodeError;
41#[cfg(feature = "metal")]
42pub use metal::{
43 MetalBurnDecoder, MetalUploadBurnDecoder, SubmittedMetalBurnBatch,
44 SubmittedMetalUploadBurnBatch,
45};
46
47#[derive(Debug)]
49pub enum BurnBatchTensor<B: Backend> {
50 U8(Tensor<B, 4, Int>),
52 U16(Tensor<B, 4, Int>),
54 I16(Tensor<B, 4, Int>),
56}
57
58impl<B: Backend> BurnBatchTensor<B> {
59 #[must_use]
61 pub const fn tensor(&self) -> &Tensor<B, 4, Int> {
62 match self {
63 Self::U8(tensor) | Self::U16(tensor) | Self::I16(tensor) => tensor,
64 }
65 }
66
67 #[must_use]
69 pub fn into_tensor(self) -> Tensor<B, 4, Int> {
70 match self {
71 Self::U8(tensor) | Self::U16(tensor) | Self::I16(tensor) => tensor,
72 }
73 }
74}
75
76#[derive(Debug)]
78pub struct BurnBatchGroup<B: Backend> {
79 pub tensor: BurnBatchTensor<B>,
81 pub info: BatchGroupInfo,
83 pub source_indices: Vec<usize>,
85 pub decoded_rects: Vec<Rect>,
87 pub warnings: Vec<Vec<J2kDecodeWarning>>,
89}
90
91#[derive(Debug, thiserror::Error)]
97#[error("Burn batch group containing source indices {source_indices:?} failed: {source}")]
98pub struct BurnBatchGroupError {
99 source_indices: Vec<usize>,
100 #[source]
101 source: BurnDecodeError,
102}
103
104impl BurnBatchGroupError {
105 #[cfg(any(feature = "cuda", all(feature = "metal", target_os = "macos"), test))]
106 pub(crate) fn new(source_indices: Vec<usize>, source: BurnDecodeError) -> Self {
107 Self {
108 source_indices,
109 source,
110 }
111 }
112
113 #[must_use]
115 pub fn source_indices(&self) -> &[usize] {
116 &self.source_indices
117 }
118
119 #[must_use]
121 pub const fn source(&self) -> &BurnDecodeError {
122 &self.source
123 }
124
125 #[must_use]
127 pub fn into_parts(self) -> (Vec<usize>, BurnDecodeError) {
128 (self.source_indices, self.source)
129 }
130}
131
132#[derive(Debug)]
134pub struct BurnBatchDecode<B: Backend> {
135 pub groups: Vec<BurnBatchGroup<B>>,
137 pub errors: Vec<IndexedBatchError>,
139 pub group_errors: Vec<BurnBatchGroupError>,
141}