use crate::util::{DctSample, try_vec, validate_scratch};
use crate::{PxdctError, PxdctExecutor};
use num_traits::AsPrimitive;
use std::sync::Arc;
pub(crate) struct Dst4OverDct4<T: DctSample> {
dct4: Arc<dyn PxdctExecutor<T> + Send + Sync>,
execution_length: usize,
}
impl<T: DctSample> Dst4OverDct4<T>
where
f64: AsPrimitive<T>,
{
pub(crate) fn new(
dct4: Arc<dyn PxdctExecutor<T> + Send + Sync>,
) -> Result<Dst4OverDct4<T>, PxdctError> {
let len = dct4.length();
Ok(Dst4OverDct4 {
dct4,
execution_length: len,
})
}
#[inline]
fn postprocess(chunk: &mut [T]) {
let mut sign = T::one();
for x in chunk.iter_mut() {
*x *= sign;
sign = -sign;
}
}
}
impl<T: DctSample> PxdctExecutor<T> for Dst4OverDct4<T>
where
f64: AsPrimitive<T>,
{
fn execute(&self, data: &mut [T]) -> Result<(), PxdctError> {
let mut scratch = try_vec![T::default(); self.scratch_size()];
self.execute_with_scratch(data, &mut scratch)
}
fn execute_with_scratch(&self, data: &mut [T], scratch: &mut [T]) -> Result<(), PxdctError> {
if !data.len().is_multiple_of(self.execution_length) {
return Err(PxdctError::InvalidSizeMultiplier(
data.len(),
self.execution_length,
));
}
let full_scratch = validate_scratch!(scratch, self.scratch_size());
for chunk in data.chunks_exact_mut(self.execution_length) {
chunk.reverse();
self.dct4.execute_with_scratch(chunk, full_scratch)?;
Self::postprocess(chunk);
}
Ok(())
}
fn execute_into(&self, input: &[T], output: &mut [T]) -> Result<(), PxdctError> {
let mut scratch = try_vec![T::default(); self.scratch_size()];
self.execute_into_with_scratch(input, output, &mut scratch)
}
fn execute_into_with_scratch(
&self,
input: &[T],
output: &mut [T],
scratch: &mut [T],
) -> Result<(), PxdctError> {
use crate::util::validate_oof_sizes;
validate_oof_sizes!(input, output, self.execution_length);
let full_scratch = validate_scratch!(scratch, self.scratch_size());
for (src, dst) in input
.chunks_exact(self.execution_length)
.zip(output.chunks_exact_mut(self.execution_length))
{
dst.iter_mut()
.zip(src.iter().rev())
.for_each(|(d, &s)| *d = s);
self.dct4.execute_with_scratch(dst, full_scratch)?;
Self::postprocess(dst);
}
Ok(())
}
#[inline]
fn length(&self) -> usize {
self.execution_length
}
fn scratch_size(&self) -> usize {
self.dct4.scratch_size()
}
}
#[cfg(test)]
mod tests {
use super::Dst4OverDct4;
use crate::tests::naive_dst4;
use crate::{Pxdct, PxdctExecutor};
#[test]
fn test_dst4_size7() {
let input = vec![1., 2., 3., 4., 5., 6., 7.];
let mut working = input.clone();
let control = naive_dst4(&input);
let dct4 = Pxdct::strategy_dct4(input.len()).unwrap();
let dst4 = Dst4OverDct4::new(dct4).unwrap();
dst4.execute(&mut working).unwrap();
working
.iter()
.zip(control.iter())
.enumerate()
.for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-9, "index {i}: got {x}, expected {c}");
});
}
#[test]
fn test_dst4_size8() {
let input = vec![1., 2., 3., 4., 5., 6., 7., 8.];
let mut working = input.clone();
let control = naive_dst4(&input);
let dct4 = Pxdct::strategy_dct4(input.len()).unwrap();
let dst4 = Dst4OverDct4::new(dct4).unwrap();
dst4.execute(&mut working).unwrap();
working
.iter()
.zip(control.iter())
.enumerate()
.for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-9, "index {i}: got {x}, expected {c}");
});
}
#[test]
fn test_dst4_size14() {
let input: Vec<f64> = (1..=14).map(|x| x as f64).collect();
let mut working = input.clone();
let control = naive_dst4(&input);
let dct4 = Pxdct::strategy_dct4(input.len()).unwrap();
let dst4 = Dst4OverDct4::new(dct4).unwrap();
dst4.execute(&mut working).unwrap();
working
.iter()
.zip(control.iter())
.enumerate()
.for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-9, "index {i}: got {x}, expected {c}");
});
}
#[test]
fn test_dst4_into_size7() {
let input = vec![1., 2., 3., 4., 5., 6., 7.];
let mut output = vec![0.0f64; 7];
let control = naive_dst4(&input);
let dct4 = Pxdct::strategy_dct4(input.len()).unwrap();
let dst4 = Dst4OverDct4::new(dct4).unwrap();
dst4.execute_into(&input, &mut output).unwrap();
output
.iter()
.zip(control.iter())
.enumerate()
.for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-9, "index {i}: got {x}, expected {c}");
});
}
#[test]
fn test_dst4_all_sizes() {
for n in 1..150 {
let input: Vec<f64> = (1..=n).map(|x| x as f64).collect();
let mut working = input.clone();
let control = naive_dst4(&input);
let dct4 = Pxdct::strategy_dct4(n).unwrap();
let dst4 = Dst4OverDct4::new(dct4).unwrap();
dst4.execute(&mut working).unwrap();
working
.iter()
.zip(control.iter())
.enumerate()
.for_each(|(i, (&x, &c))| {
assert!(
(x - c).abs() < 1e-7,
"size {n}, index {i}: got {x}, expected {c}"
);
});
}
}
}