mod encoder;
pub use encoder::Encoder;
mod decoder;
pub use decoder::Decoder;
pub struct Config<const CH: usize> {
streams: u8,
coupled_streams: u8,
mapping: [u8; CH],
}
impl<const CH: usize> Config<CH> {
const CHANNELS: u8 = match CH {
0 => panic!("Unsupported number of channels. Allowed range: 0..=255"),
_ => CH as _,
};
pub const fn try_new(streams: u8, coupled_streams: u8, mapping: [u8; CH]) -> Option<Self> {
if streams == 0 || coupled_streams > streams {
return None;
}
let mut idx = 0;
while idx < CH {
if mapping[idx] != 255 && (mapping[idx] as usize) >= CH {
return None;
}
idx = idx.saturating_add(1);
}
match streams.checked_add(coupled_streams) {
Some(total) => if total == 0 || total > Self::CHANNELS {
None
} else {
Some(Self {
streams,
coupled_streams,
mapping,
})
}
None => None,
}
}
pub const fn new(streams: u8, coupled_streams: u8, mapping: [u8; CH]) -> Self {
assert!(streams != 0);
assert!(coupled_streams <= streams);
let mut idx = 0;
while idx < CH {
if mapping[idx] != 255 {
assert!((mapping[idx] as usize) < CH, "Non 255 mapping values must be in range 0..CH");
}
idx = idx.saturating_add(1);
}
match streams.checked_add(coupled_streams) {
Some(total_streams) => {
assert!(total_streams != 0);
assert!(total_streams <= Self::CHANNELS);
Self {
streams,
coupled_streams,
mapping
}
},
None => panic!("sum(streams, coupled_streams) cannot exceed 255"),
}
}
#[inline(always)]
pub fn mapping(&self) -> &[u8; CH] {
&self.mapping
}
#[inline(always)]
pub fn mapping_mut(&mut self) -> &mut [u8; CH] {
&mut self.mapping
}
}