| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use rustfft::Length; |
| 4 |
|
| 5 |
use crate::common::mdct_error_inplace; |
| 6 |
use crate::mdct::Mdct; |
| 7 |
use crate::RequiredScratch; |
| 8 |
use crate::{DctNum, TransformType4}; |
| 9 |
|
| 10 |
/// MDCT implementation that converts the problem to a DCT Type 4 of the same size. |
| 11 |
/// |
| 12 |
/// It is much easier to express a MDCT as a DCT Type 4 than it is to express it as a FFT, so converting the MDCT |
| 13 |
/// to a DCT4 before converting it to a FFT results in greatly simplified code |
| 14 |
/// |
| 15 |
/// ~~~ |
| 16 |
/// // Computes a MDCT of input size 1234 via a DCT4, using the MP3 window function |
| 17 |
/// use rustdct::mdct::{Mdct, MdctViaDct4, window_fn}; |
| 18 |
/// use rustdct::{DctPlanner, RequiredScratch}; |
| 19 |
/// |
| 20 |
/// let len = 1234; |
| 21 |
/// |
| 22 |
/// let mut planner = DctPlanner::new(); |
| 23 |
/// let inner_dct4 = planner.plan_dct4(len); |
| 24 |
/// |
| 25 |
/// let dct = MdctViaDct4::new(inner_dct4, window_fn::mp3); |
| 26 |
/// |
| 27 |
/// let input = vec![0f32; len * 2]; |
| 28 |
/// let (input_a, input_b) = input.split_at(len); |
| 29 |
/// let mut output = vec![0f32; len]; |
| 30 |
/// let mut scratch = vec![0f32; dct.get_scratch_len()]; |
| 31 |
/// |
| 32 |
/// dct.process_mdct_with_scratch(input_a, input_b, &mut output, &mut scratch); |
| 33 |
/// ~~~ |
| 34 |
pub struct MdctViaDct4<T> { |
| 35 |
dct: Arc<dyn TransformType4<T>>, |
| 36 |
window: Box<[T]>, |
| 37 |
scratch_len: usize, |
| 38 |
} |
| 39 |
|
| 40 |
impl<T: DctNum> MdctViaDct4<T> { |
| 41 |
/// Creates a new MDCT context that will process signals of length `inner_dct.len() * 2`, with an output of length `inner_dct.len()` |
| 42 |
/// |
| 43 |
/// `inner_dct.len()` must be even. |
| 44 |
/// |
| 45 |
/// `window_fn` is a function that takes a `size` and returns a `Vec` containing `size` window values. |
| 46 |
/// See the [`window_fn`](mdct/window_fn/index.html) module for provided window functions. |
| 47 |
pub fn new<F>(inner_dct: Arc<dyn TransformType4<T>>, window_fn: F) -> Self |
| 48 |
where |
| 49 |
F: FnOnce(usize) -> Vec<T>, |
| 50 |
{ |
| 51 |
let len = inner_dct.len(); |
| 52 |
|
| 53 |
assert!(len % 2 == 0, "The MDCT inner_dct.len() must be even"); |
| 54 |
|
| 55 |
let window = window_fn(len * 2); |
| 56 |
assert_eq!( |
| 57 |
window.len(), |
| 58 |
len * 2, |
| 59 |
"Window function returned incorrect number of values" |
| 60 |
); |
| 61 |
|
| 62 |
Self { |
| 63 |
scratch_len: len + inner_dct.get_scratch_len(), |
| 64 |
dct: inner_dct, |
| 65 |
window: window.into_boxed_slice(), |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
impl<T: DctNum> Mdct<T> for MdctViaDct4<T> { |
| 70 |
fn process_mdct_with_scratch( |
| 71 |
&self, |
| 72 |
input_a: &[T], |
| 73 |
input_b: &[T], |
| 74 |
output: &mut [T], |
| 75 |
scratch: &mut [T], |
| 76 |
) { |
| 77 |
let scratch = validate_buffers_mdct!( |
| 78 |
input_a, |
| 79 |
input_b, |
| 80 |
output, |
| 81 |
scratch, |
| 82 |
self.len(), |
| 83 |
self.get_scratch_len() |
| 84 |
); |
| 85 |
|
| 86 |
let group_size = self.len() / 2; |
| 87 |
|
| 88 |
//we're going to divide input_a into two subgroups, (a,b), and input_b into two subgroups: (c,d) |
| 89 |
//then scale them by the window function, then combine them into two subgroups: (-D-Cr, A-Br) where R means reversed |
| 90 |
let group_a_iter = input_a |
| 91 |
.iter() |
| 92 |
.zip(self.window.iter()) |
| 93 |
.map(|(a, window_val)| *a * *window_val) |
| 94 |
.take(group_size); |
| 95 |
let group_b_rev_iter = input_a |
| 96 |
.iter() |
| 97 |
.zip(self.window.iter()) |
| 98 |
.map(|(b, window_val)| *b * *window_val) |
| 99 |
.rev() |
| 100 |
.take(group_size); |
| 101 |
let group_c_rev_iter = input_b |
| 102 |
.iter() |
| 103 |
.zip(&self.window[self.len()..]) |
| 104 |
.map(|(c, window_val)| *c * *window_val) |
| 105 |
.rev() |
| 106 |
.skip(group_size); |
| 107 |
let group_d_iter = input_b |
| 108 |
.iter() |
| 109 |
.zip(&self.window[self.len()..]) |
| 110 |
.map(|(d, window_val)| *d * *window_val) |
| 111 |
.skip(group_size); |
| 112 |
|
| 113 |
//the first half of the dct input is -Cr - D |
| 114 |
for (element, (cr_val, d_val)) in output.iter_mut().zip(group_c_rev_iter.zip(group_d_iter)) |
| 115 |
{ |
| 116 |
*element = -cr_val - d_val; |
| 117 |
} |
| 118 |
|
| 119 |
//the second half of the dct input is is A - Br |
| 120 |
for (element, (a_val, br_val)) in output[group_size..] |
| 121 |
.iter_mut() |
| 122 |
.zip(group_a_iter.zip(group_b_rev_iter)) |
| 123 |
{ |
| 124 |
*element = a_val - br_val; |
| 125 |
} |
| 126 |
|
| 127 |
self.dct.process_dct4_with_scratch(output, scratch); |
| 128 |
} |
| 129 |
|
| 130 |
fn process_imdct_with_scratch( |
| 131 |
&self, |
| 132 |
input: &[T], |
| 133 |
output_a: &mut [T], |
| 134 |
output_b: &mut [T], |
| 135 |
scratch: &mut [T], |
| 136 |
) { |
| 137 |
let scratch = validate_buffers_mdct!( |
| 138 |
input, |
| 139 |
output_a, |
| 140 |
output_b, |
| 141 |
scratch, |
| 142 |
self.len(), |
| 143 |
self.get_scratch_len() |
| 144 |
); |
| 145 |
|
| 146 |
let (dct_buffer, dct_scratch) = scratch.split_at_mut(self.len()); |
| 147 |
dct_buffer.copy_from_slice(input); |
| 148 |
|
| 149 |
self.dct.process_dct4_with_scratch(dct_buffer, dct_scratch); |
| 150 |
|
| 151 |
let group_size = self.len() / 2; |
| 152 |
|
| 153 |
//copy the second half of the DCT output into the result |
| 154 |
for ((output, window_val), val) in output_a |
| 155 |
.iter_mut() |
| 156 |
.zip(&self.window[..]) |
| 157 |
.zip(dct_buffer[group_size..].iter()) |
| 158 |
{ |
| 159 |
*output = *output + *val * *window_val; |
| 160 |
} |
| 161 |
|
| 162 |
//copy the second half of the DCT output again, but this time reversed and negated |
| 163 |
for ((output, window_val), val) in output_a |
| 164 |
.iter_mut() |
| 165 |
.zip(&self.window[..]) |
| 166 |
.skip(group_size) |
| 167 |
.zip(dct_buffer[group_size..].iter().rev()) |
| 168 |
{ |
| 169 |
*output = *output - *val * *window_val; |
| 170 |
} |
| 171 |
|
| 172 |
//copy the first half of the DCT output into the result, reversde+negated |
| 173 |
for ((output, window_val), val) in output_b |
| 174 |
.iter_mut() |
| 175 |
.zip(&self.window[self.len()..]) |
| 176 |
.zip(dct_buffer[..group_size].iter().rev()) |
| 177 |
{ |
| 178 |
*output = *output - *val * *window_val; |
| 179 |
} |
| 180 |
|
| 181 |
//copy the first half of the DCT output again, but this time not reversed |
| 182 |
for ((output, window_val), val) in output_b |
| 183 |
.iter_mut() |
| 184 |
.zip(&self.window[self.len()..]) |
| 185 |
.skip(group_size) |
| 186 |
.zip(dct_buffer[..group_size].iter()) |
| 187 |
{ |
| 188 |
*output = *output - *val * *window_val; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
impl<T> Length for MdctViaDct4<T> { |
| 193 |
fn len(&self) -> usize { |
| 194 |
self.dct.len() |
| 195 |
} |
| 196 |
} |
| 197 |
impl<T> RequiredScratch for MdctViaDct4<T> { |
| 198 |
fn get_scratch_len(&self) -> usize { |
| 199 |
self.scratch_len |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
#[cfg(test)] |
| 204 |
mod unit_tests { |
| 205 |
use super::*; |
| 206 |
|
| 207 |
use crate::algorithm::Type4Naive; |
| 208 |
use crate::mdct::window_fn; |
| 209 |
use crate::mdct::MdctNaive; |
| 210 |
use crate::test_utils::{compare_float_vectors, random_signal}; |
| 211 |
|
| 212 |
/// Verify that our fast implementation of the MDCT and IMDCT gives the same output as the slow version, for many different inputs |
| 213 |
#[test] |
| 214 |
fn test_mdct_via_dct4() { |
| 215 |
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] { |
| 216 |
for i in 1..11 { |
| 217 |
let input_len = i * 4; |
| 218 |
let output_len = i * 2; |
| 219 |
|
| 220 |
let input = random_signal(input_len); |
| 221 |
let (input_a, input_b) = input.split_at(output_len); |
| 222 |
|
| 223 |
let mut naive_output = vec![0f32; output_len]; |
| 224 |
let mut fast_output = vec![0f32; output_len]; |
| 225 |
|
| 226 |
let naive_mdct = MdctNaive::new(output_len, current_window_fn); |
| 227 |
|
| 228 |
let inner_dct4 = Arc::new(Type4Naive::new(output_len)); |
| 229 |
let fast_mdct = MdctViaDct4::new(inner_dct4, current_window_fn); |
| 230 |
|
| 231 |
let mut naive_scratch = vec![0f32; naive_mdct.get_scratch_len()]; |
| 232 |
let mut fast_scratch = vec![0f32; fast_mdct.get_scratch_len()]; |
| 233 |
|
| 234 |
naive_mdct.process_mdct_with_scratch( |
| 235 |
&input_a, |
| 236 |
&input_b, |
| 237 |
&mut naive_output, |
| 238 |
&mut naive_scratch, |
| 239 |
); |
| 240 |
fast_mdct.process_mdct_with_scratch( |
| 241 |
&input_a, |
| 242 |
&input_b, |
| 243 |
&mut fast_output, |
| 244 |
&mut fast_scratch, |
| 245 |
); |
| 246 |
|
| 247 |
assert!( |
| 248 |
compare_float_vectors(&naive_output, &fast_output), |
| 249 |
"i = {}", |
| 250 |
i |
| 251 |
); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/// Verify that our fast implementation of the MDCT and IMDCT gives the same output as the slow version, for many different inputs |
| 257 |
#[test] |
| 258 |
fn test_imdct_via_dct4() { |
| 259 |
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] { |
| 260 |
for i in 1..11 { |
| 261 |
let input_len = i * 2; |
| 262 |
let output_len = i * 4; |
| 263 |
|
| 264 |
let input = random_signal(input_len); |
| 265 |
|
| 266 |
// Fill both output buffers with ones, instead of zeroes, to verify that the IMDCT doesn't overwrite the output buffer |
| 267 |
let mut naive_output = vec![1f32; output_len]; |
| 268 |
let (naive_output_a, naive_output_b) = naive_output.split_at_mut(input_len); |
| 269 |
|
| 270 |
let mut fast_output = vec![1f32; output_len]; |
| 271 |
let (fast_output_a, fast_output_b) = fast_output.split_at_mut(input_len); |
| 272 |
|
| 273 |
let naive_mdct = MdctNaive::new(input_len, current_window_fn); |
| 274 |
|
| 275 |
let inner_dct4 = Arc::new(Type4Naive::new(input_len)); |
| 276 |
let fast_mdct = MdctViaDct4::new(inner_dct4, current_window_fn); |
| 277 |
|
| 278 |
let mut naive_scratch = vec![0f32; naive_mdct.get_scratch_len()]; |
| 279 |
let mut fast_scratch = vec![0f32; fast_mdct.get_scratch_len()]; |
| 280 |
|
| 281 |
naive_mdct.process_imdct_with_scratch( |
| 282 |
&input, |
| 283 |
naive_output_a, |
| 284 |
naive_output_b, |
| 285 |
&mut naive_scratch, |
| 286 |
); |
| 287 |
fast_mdct.process_imdct_with_scratch( |
| 288 |
&input, |
| 289 |
fast_output_a, |
| 290 |
fast_output_b, |
| 291 |
&mut fast_scratch, |
| 292 |
); |
| 293 |
|
| 294 |
assert!( |
| 295 |
compare_float_vectors(&naive_output, &fast_output), |
| 296 |
"i = {}", |
| 297 |
i |
| 298 |
); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |