| 1 |
use std::collections::HashMap; |
| 2 |
use std::sync::Arc; |
| 3 |
|
| 4 |
use crate::algorithm::type2and3_butterflies::*; |
| 5 |
use crate::algorithm::*; |
| 6 |
use crate::mdct::*; |
| 7 |
use crate::{ |
| 8 |
Dct1, Dct5, Dct6And7, Dct8, Dst1, Dst5, Dst6And7, Dst8, TransformType2And3, TransformType4, |
| 9 |
}; |
| 10 |
use rustfft::FftPlanner; |
| 11 |
|
| 12 |
use crate::DctNum; |
| 13 |
|
| 14 |
const DCT2_BUTTERFLIES: [usize; 5] = [2, 3, 4, 8, 16]; |
| 15 |
|
| 16 |
/// The DCT planner is used to make new DCT algorithm instances. |
| 17 |
/// |
| 18 |
/// RustDCT has several DCT algorithms available for each DCT type; For a given DCT type and problem size, the DctPlanner |
| 19 |
/// decides which of the available DCT algorithms to use and then initializes them. |
| 20 |
/// |
| 21 |
/// ~~~ |
| 22 |
/// // Perform a DCT Type 4 of size 1234 |
| 23 |
/// use std::sync::Arc; |
| 24 |
/// use rustdct::DctPlanner; |
| 25 |
/// |
| 26 |
/// let mut planner = DctPlanner::new(); |
| 27 |
/// let dct4 = planner.plan_dct4(1234); |
| 28 |
/// |
| 29 |
/// let mut buffer = vec![0f32; 1234]; |
| 30 |
/// dct4.process_dct4(&mut buffer); |
| 31 |
/// |
| 32 |
/// // The DCT instance returned by the planner is stored behind an `Arc`, so it's cheap to clone |
| 33 |
/// let dct4_clone = Arc::clone(&dct4); |
| 34 |
/// ~~~ |
| 35 |
/// |
| 36 |
/// If you plan on creating multiple DCT instances, it is recommnded to reuse the same planner for all of them. This |
| 37 |
/// is because the planner re-uses internal data across DCT instances wherever possible, saving memory and reducing |
| 38 |
/// setup time. (DCT instances created with one planner will never re-use data and buffers with DCT instances created |
| 39 |
/// by a different planner) |
| 40 |
/// |
| 41 |
/// Each DCT instance owns `Arc`s to its shared internal data, rather than borrowing it from the planner, so it's |
| 42 |
/// perfectly safe to drop the planner after creating DCT instances. |
| 43 |
pub struct DctPlanner<T: DctNum> { |
| 44 |
fft_planner: FftPlanner<T>, |
| 45 |
|
| 46 |
dct1_cache: HashMap<usize, Arc<dyn Dct1<T>>>, |
| 47 |
dst1_cache: HashMap<usize, Arc<dyn Dst1<T>>>, |
| 48 |
dct23_cache: HashMap<usize, Arc<dyn TransformType2And3<T>>>, |
| 49 |
dct4_cache: HashMap<usize, Arc<dyn TransformType4<T>>>, |
| 50 |
dct5_cache: HashMap<usize, Arc<dyn Dct5<T>>>, |
| 51 |
dst5_cache: HashMap<usize, Arc<dyn Dst5<T>>>, |
| 52 |
dct6_cache: HashMap<usize, Arc<dyn Dct6And7<T>>>, |
| 53 |
dst6_cache: HashMap<usize, Arc<dyn Dst6And7<T>>>, |
| 54 |
dct8_cache: HashMap<usize, Arc<dyn Dct8<T>>>, |
| 55 |
dst8_cache: HashMap<usize, Arc<dyn Dst8<T>>>, |
| 56 |
|
| 57 |
mdct_cache: HashMap<usize, Arc<dyn Mdct<T>>>, |
| 58 |
} |
| 59 |
impl<T: DctNum> DctPlanner<T> { |
| 60 |
pub fn new() -> Self { |
| 61 |
Self { |
| 62 |
fft_planner: FftPlanner::new(), |
| 63 |
dct1_cache: HashMap::new(), |
| 64 |
dst1_cache: HashMap::new(), |
| 65 |
dct23_cache: HashMap::new(), |
| 66 |
dct4_cache: HashMap::new(), |
| 67 |
dct5_cache: HashMap::new(), |
| 68 |
dst5_cache: HashMap::new(), |
| 69 |
dct6_cache: HashMap::new(), |
| 70 |
dst6_cache: HashMap::new(), |
| 71 |
dct8_cache: HashMap::new(), |
| 72 |
dst8_cache: HashMap::new(), |
| 73 |
mdct_cache: HashMap::new(), |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// Returns a DCT Type 1 instance which processes signals of size `len`. |
| 78 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 79 |
pub fn plan_dct1(&mut self, len: usize) -> Arc<dyn Dct1<T>> { |
| 80 |
if self.dct1_cache.contains_key(&len) { |
| 81 |
Arc::clone(self.dct1_cache.get(&len).unwrap()) |
| 82 |
} else { |
| 83 |
let result = self.plan_new_dct1(len); |
| 84 |
self.dct1_cache.insert(len, Arc::clone(&result)); |
| 85 |
result |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
fn plan_new_dct1(&mut self, len: usize) -> Arc<dyn Dct1<T>> { |
| 90 |
//benchmarking shows that below about 10, it's faster to just use the naive DCT1 algorithm |
| 91 |
if len < 10 { |
| 92 |
Arc::new(Dct1Naive::new(len)) |
| 93 |
} else { |
| 94 |
let fft = self.fft_planner.plan_fft_forward((len - 1) * 2); |
| 95 |
Arc::new(Dct1ConvertToFft::new(fft)) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/// Returns a DCT Type 2 instance which processes signals of size `len`. |
| 100 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 101 |
pub fn plan_dct2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 102 |
if self.dct23_cache.contains_key(&len) { |
| 103 |
Arc::clone(self.dct23_cache.get(&len).unwrap()) |
| 104 |
} else { |
| 105 |
let result = self.plan_new_dct2(len); |
| 106 |
self.dct23_cache.insert(len, Arc::clone(&result)); |
| 107 |
result |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
fn plan_new_dct2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 112 |
if DCT2_BUTTERFLIES.contains(&len) { |
| 113 |
self.plan_dct2_butterfly(len) |
| 114 |
} else if len.is_power_of_two() && len > 2 { |
| 115 |
let half_dct = self.plan_dct2(len / 2); |
| 116 |
let quarter_dct = self.plan_dct2(len / 4); |
| 117 |
Arc::new(Type2And3SplitRadix::new(half_dct, quarter_dct)) |
| 118 |
} else { |
| 119 |
// Benchmarking shows that it's always faster |
| 120 |
let fft = self.fft_planner.plan_fft_forward(len); |
| 121 |
Arc::new(Type2And3ConvertToFft::new(fft)) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
fn plan_dct2_butterfly(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 126 |
match len { |
| 127 |
2 => Arc::new(Type2And3Butterfly2::new()), |
| 128 |
3 => Arc::new(Type2And3Butterfly3::new()), |
| 129 |
4 => Arc::new(Type2And3Butterfly4::new()), |
| 130 |
8 => Arc::new(Type2And3Butterfly8::new()), |
| 131 |
16 => Arc::new(Type2And3Butterfly16::new()), |
| 132 |
_ => panic!("Invalid butterfly size for DCT2: {}", len), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/// Returns DCT Type 3 instance which processes signals of size `len`. |
| 137 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 138 |
pub fn plan_dct3(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 139 |
self.plan_dct2(len) |
| 140 |
} |
| 141 |
|
| 142 |
/// Returns a DCT Type 4 instance which processes signals of size `len`. |
| 143 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 144 |
pub fn plan_dct4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> { |
| 145 |
if self.dct4_cache.contains_key(&len) { |
| 146 |
Arc::clone(self.dct4_cache.get(&len).unwrap()) |
| 147 |
} else { |
| 148 |
let result = self.plan_new_dct4(len); |
| 149 |
self.dct4_cache.insert(len, Arc::clone(&result)); |
| 150 |
result |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
fn plan_new_dct4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> { |
| 155 |
//if we have an even size, we can use the DCT4 Via DCT3 algorithm |
| 156 |
if len % 2 == 0 { |
| 157 |
//benchmarking shows that below 6, it's faster to just use the naive DCT4 algorithm |
| 158 |
if len < 6 { |
| 159 |
Arc::new(Type4Naive::new(len)) |
| 160 |
} else { |
| 161 |
let inner_dct = self.plan_dct3(len / 2); |
| 162 |
Arc::new(Type4ConvertToType3Even::new(inner_dct)) |
| 163 |
} |
| 164 |
} else { |
| 165 |
//odd size, so we can use the "DCT4 via FFT odd" algorithm |
| 166 |
//benchmarking shows that below about 7, it's faster to just use the naive DCT4 algorithm |
| 167 |
if len < 7 { |
| 168 |
Arc::new(Type4Naive::new(len)) |
| 169 |
} else { |
| 170 |
let fft = self.fft_planner.plan_fft_forward(len); |
| 171 |
Arc::new(Type4ConvertToFftOdd::new(fft)) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/// Returns a DCT Type 5 instance which processes signals of size `len`. |
| 177 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 178 |
pub fn plan_dct5(&mut self, len: usize) -> Arc<dyn Dct5<T>> { |
| 179 |
if self.dct5_cache.contains_key(&len) { |
| 180 |
Arc::clone(self.dct5_cache.get(&len).unwrap()) |
| 181 |
} else { |
| 182 |
let result = self.plan_new_dct5(len); |
| 183 |
self.dct5_cache.insert(len, Arc::clone(&result)); |
| 184 |
result |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
fn plan_new_dct5(&mut self, len: usize) -> Arc<dyn Dct5<T>> { |
| 189 |
Arc::new(Dct5Naive::new(len)) |
| 190 |
} |
| 191 |
|
| 192 |
/// Returns a DCT Type 6 instance which processes signals of size `len`. |
| 193 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 194 |
pub fn plan_dct6(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> { |
| 195 |
if self.dct6_cache.contains_key(&len) { |
| 196 |
Arc::clone(self.dct6_cache.get(&len).unwrap()) |
| 197 |
} else { |
| 198 |
let result = self.plan_new_dct6(len); |
| 199 |
self.dct6_cache.insert(len, Arc::clone(&result)); |
| 200 |
result |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
fn plan_new_dct6(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> { |
| 205 |
Arc::new(Dct6And7Naive::new(len)) |
| 206 |
} |
| 207 |
|
| 208 |
/// Returns DCT Type 7 instance which processes signals of size `len`. |
| 209 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 210 |
pub fn plan_dct7(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> { |
| 211 |
self.plan_dct6(len) |
| 212 |
} |
| 213 |
|
| 214 |
/// Returns a DCT Type 8 instance which processes signals of size `len`. |
| 215 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 216 |
pub fn plan_dct8(&mut self, len: usize) -> Arc<dyn Dct8<T>> { |
| 217 |
if self.dct8_cache.contains_key(&len) { |
| 218 |
Arc::clone(self.dct8_cache.get(&len).unwrap()) |
| 219 |
} else { |
| 220 |
let result = self.plan_new_dct8(len); |
| 221 |
self.dct8_cache.insert(len, Arc::clone(&result)); |
| 222 |
result |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
fn plan_new_dct8(&mut self, len: usize) -> Arc<dyn Dct8<T>> { |
| 227 |
Arc::new(Dct8Naive::new(len)) |
| 228 |
} |
| 229 |
|
| 230 |
/// Returns a DST Type 1 instance which processes signals of size `len`. |
| 231 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 232 |
pub fn plan_dst1(&mut self, len: usize) -> Arc<dyn Dst1<T>> { |
| 233 |
if self.dst1_cache.contains_key(&len) { |
| 234 |
Arc::clone(self.dst1_cache.get(&len).unwrap()) |
| 235 |
} else { |
| 236 |
let result = self.plan_new_dst1(len); |
| 237 |
self.dst1_cache.insert(len, Arc::clone(&result)); |
| 238 |
result |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
fn plan_new_dst1(&mut self, len: usize) -> Arc<dyn Dst1<T>> { |
| 243 |
//benchmarking shows that below about 25, it's faster to just use the naive DCT1 algorithm |
| 244 |
if len < 25 { |
| 245 |
Arc::new(Dst1Naive::new(len)) |
| 246 |
} else { |
| 247 |
let fft = self.fft_planner.plan_fft_forward((len + 1) * 2); |
| 248 |
Arc::new(Dst1ConvertToFft::new(fft)) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/// Returns DST Type 2 instance which processes signals of size `len`. |
| 253 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 254 |
pub fn plan_dst2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 255 |
self.plan_dct2(len) |
| 256 |
} |
| 257 |
|
| 258 |
/// Returns DST Type 3 instance which processes signals of size `len`. |
| 259 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 260 |
pub fn plan_dst3(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> { |
| 261 |
self.plan_dct2(len) |
| 262 |
} |
| 263 |
|
| 264 |
/// Returns DST Type 4 instance which processes signals of size `len`. |
| 265 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 266 |
pub fn plan_dst4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> { |
| 267 |
self.plan_dct4(len) |
| 268 |
} |
| 269 |
|
| 270 |
/// Returns a DST Type 5 instance which processes signals of size `len`. |
| 271 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 272 |
pub fn plan_dst5(&mut self, len: usize) -> Arc<dyn Dst5<T>> { |
| 273 |
if self.dst5_cache.contains_key(&len) { |
| 274 |
Arc::clone(self.dst5_cache.get(&len).unwrap()) |
| 275 |
} else { |
| 276 |
let result = self.plan_new_dst5(len); |
| 277 |
self.dst5_cache.insert(len, Arc::clone(&result)); |
| 278 |
result |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
fn plan_new_dst5(&mut self, len: usize) -> Arc<dyn Dst5<T>> { |
| 283 |
Arc::new(Dst5Naive::new(len)) |
| 284 |
} |
| 285 |
|
| 286 |
/// Returns a DST Type 6 instance which processes signals of size `len`. |
| 287 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 288 |
pub fn plan_dst6(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> { |
| 289 |
if self.dst6_cache.contains_key(&len) { |
| 290 |
Arc::clone(self.dst6_cache.get(&len).unwrap()) |
| 291 |
} else { |
| 292 |
let result = self.plan_new_dst6(len); |
| 293 |
self.dst6_cache.insert(len, Arc::clone(&result)); |
| 294 |
result |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
fn plan_new_dst6(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> { |
| 299 |
if len < 45 { |
| 300 |
Arc::new(Dst6And7Naive::new(len)) |
| 301 |
} else { |
| 302 |
let fft = self.fft_planner.plan_fft_forward(len * 2 + 1); |
| 303 |
Arc::new(Dst6And7ConvertToFft::new(fft)) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/// Returns DST Type 7 instance which processes signals of size `len`. |
| 308 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 309 |
pub fn plan_dst7(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> { |
| 310 |
self.plan_dst6(len) |
| 311 |
} |
| 312 |
|
| 313 |
/// Returns a DST Type 8 instance which processes signals of size `len`. |
| 314 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 315 |
pub fn plan_dst8(&mut self, len: usize) -> Arc<dyn Dst8<T>> { |
| 316 |
if self.dst8_cache.contains_key(&len) { |
| 317 |
Arc::clone(self.dst8_cache.get(&len).unwrap()) |
| 318 |
} else { |
| 319 |
let result = self.plan_new_dst8(len); |
| 320 |
self.dst8_cache.insert(len, Arc::clone(&result)); |
| 321 |
result |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
fn plan_new_dst8(&mut self, len: usize) -> Arc<dyn Dst8<T>> { |
| 326 |
Arc::new(Dst8Naive::new(len)) |
| 327 |
} |
| 328 |
|
| 329 |
/// Returns a MDCT instance which processes inputs of size ` len * 2` and produces outputs of size `len`. |
| 330 |
/// |
| 331 |
/// `window_fn` is a function that takes a `size` and returns a `Vec` containing `size` window values. |
| 332 |
/// See the [`window_fn`](mdct/window_fn/index.html) module for provided window functions. |
| 333 |
/// |
| 334 |
/// If this is called multiple times, it will attempt to re-use internal data between instances |
| 335 |
pub fn plan_mdct<F>(&mut self, len: usize, window_fn: F) -> Arc<dyn Mdct<T>> |
| 336 |
where |
| 337 |
F: (FnOnce(usize) -> Vec<T>), |
| 338 |
{ |
| 339 |
if self.mdct_cache.contains_key(&len) { |
| 340 |
Arc::clone(self.mdct_cache.get(&len).unwrap()) |
| 341 |
} else { |
| 342 |
let result = self.plan_new_mdct(len, window_fn); |
| 343 |
self.mdct_cache.insert(len, Arc::clone(&result)); |
| 344 |
result |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
fn plan_new_mdct<F>(&mut self, len: usize, window_fn: F) -> Arc<dyn Mdct<T>> |
| 349 |
where |
| 350 |
F: (FnOnce(usize) -> Vec<T>), |
| 351 |
{ |
| 352 |
//benchmarking shows that using the inner dct4 algorithm is always faster than computing the naive algorithm |
| 353 |
let inner_dct4 = self.plan_dct4(len); |
| 354 |
Arc::new(MdctViaDct4::new(inner_dct4, window_fn)) |
| 355 |
} |
| 356 |
} |