1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
pub use rustfft;
pub use rustfft::num_complex;
pub use rustfft::num_traits;

use rustfft::Length;

/// Algorithms for computing the Modified Discrete Cosine Transform
pub mod mdct;

pub mod algorithm;

mod array_utils;
mod common;
mod plan;
mod twiddles;
pub use crate::common::DctNum;

pub use self::plan::DctPlanner;

#[cfg(test)]
mod test_utils;

pub trait RequiredScratch {
    fn get_scratch_len(&self) -> usize;
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 1 (DCT1)
pub trait Dct1<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 1 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct1_with_scratch` instead.
    fn process_dct1(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct1_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 1 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 2 (DCT2)
pub trait Dct2<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 2 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct2_with_scratch` instead.
    fn process_dct2(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct2_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 2 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 3 (DCT3)
pub trait Dct3<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 3 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct3_with_scratch` instead.
    fn process_dct3(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct3_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 3 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 4 (DCT4)
pub trait Dct4<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 4 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst4_with_scratch` instead.
    fn process_dct4(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct4_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 4 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 5 (DCT5)
pub trait Dct5<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 5 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct5_with_scratch` instead.
    fn process_dct5(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct5_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 5 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 6 (DCT6)
pub trait Dct6<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 6 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct6_with_scratch` instead.
    fn process_dct6(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct6_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 6 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 7 (DCT7)
pub trait Dct7<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 7 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct7_with_scratch` instead.
    fn process_dct7(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct7_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 7 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 8 (DCT8)
pub trait Dct8<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DCT Type 8 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dct8_with_scratch` instead.
    fn process_dct8(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dct8_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DCT Type 8 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dct8_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 1 (DST1)
pub trait Dst1<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 1 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst1_with_scratch` instead.
    fn process_dst1(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst1_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 1 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 2 (DST2)
pub trait Dst2<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 2 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst2_with_scratch` instead.
    fn process_dst2(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst2_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 2 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 3 (DST3)
pub trait Dst3<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 3 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst3_with_scratch` instead.
    fn process_dst3(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst3_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 3 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 4 (DST4)
pub trait Dst4<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 4 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst4_with_scratch` instead.
    fn process_dst4(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst4_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 4 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 5 (DST5)
pub trait Dst5<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 5 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst4_with_scratch` instead.
    fn process_dst5(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst5_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 5 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 6 (DST6)
pub trait Dst6<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 6 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst6_with_scratch` instead.
    fn process_dst6(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst6_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 6 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 7 (DST7)
pub trait Dst7<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 7 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst7_with_scratch` instead.
    fn process_dst7(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst7_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 7 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 8 (DST8)
pub trait Dst8<T: DctNum>: RequiredScratch + Length + Sync + Send {
    /// Computes the DST Type 8 on the provided buffer, in-place.
    ///
    /// This method may allocate a Vec<T> of scratch space as needed. If you'd like to reuse that allocation between
    /// multiple computations, considr calling `process_dst8_with_scratch` instead.
    fn process_dst8(&self, buffer: &mut [T]) {
        let mut scratch = vec![T::zero(); self.get_scratch_len()];
        self.process_dst8_with_scratch(buffer, &mut scratch);
    }
    /// Computes the DST Type 8 on the provided buffer, in-place. Uses the provided `scratch` buffer as scratch space.
    fn process_dst8_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}

/// A trait for algorithms that can compute all of DCT2, DCT3, DST2, DST3, all in one struct
pub trait TransformType2And3<T: DctNum>: Dct2<T> + Dct3<T> + Dst2<T> + Dst3<T> {}

/// A trait for algorithms that can compute both DCT4 and DST4, all in one struct
pub trait TransformType4<T: DctNum>: Dct4<T> + Dst4<T> {}

/// A trait for algorithms that can compute both DCT6 and DCT7, all in one struct
pub trait Dct6And7<T: DctNum>: Dct6<T> + Dct7<T> {}

/// A trait for algorithms that can compute both DST6 and DST7, all in one struct
pub trait Dst6And7<T: DctNum>: Dst6<T> + Dst7<T> {}

#[test]
fn test_send_sync_impls() {
    fn assert_send_sync<T: ?Sized>()
    where
        T: Send + Sync,
    {
    }

    assert_send_sync::<dyn Dct1<f32>>();
    assert_send_sync::<dyn Dct2<f32>>();
    assert_send_sync::<dyn Dct3<f32>>();
    assert_send_sync::<dyn Dct4<f32>>();
    assert_send_sync::<dyn Dct5<f32>>();
    assert_send_sync::<dyn Dct6<f32>>();
    assert_send_sync::<dyn Dct7<f32>>();
    assert_send_sync::<dyn Dct8<f32>>();

    assert_send_sync::<dyn Dct1<f64>>();
    assert_send_sync::<dyn Dct2<f64>>();
    assert_send_sync::<dyn Dct3<f64>>();
    assert_send_sync::<dyn Dct4<f64>>();
    assert_send_sync::<dyn Dct5<f64>>();
    assert_send_sync::<dyn Dct6<f64>>();
    assert_send_sync::<dyn Dct7<f64>>();
    assert_send_sync::<dyn Dct8<f64>>();

    assert_send_sync::<dyn Dst1<f32>>();
    assert_send_sync::<dyn Dst2<f32>>();
    assert_send_sync::<dyn Dst3<f32>>();
    assert_send_sync::<dyn Dst4<f32>>();
    assert_send_sync::<dyn Dst5<f32>>();
    assert_send_sync::<dyn Dst6<f32>>();
    assert_send_sync::<dyn Dst7<f32>>();
    assert_send_sync::<dyn Dst8<f32>>();

    assert_send_sync::<dyn Dst1<f64>>();
    assert_send_sync::<dyn Dst2<f64>>();
    assert_send_sync::<dyn Dst3<f64>>();
    assert_send_sync::<dyn Dst4<f64>>();
    assert_send_sync::<dyn Dst5<f64>>();
    assert_send_sync::<dyn Dst6<f64>>();
    assert_send_sync::<dyn Dst7<f64>>();
    assert_send_sync::<dyn Dst8<f64>>();

    assert_send_sync::<dyn mdct::Mdct<f32>>();
    assert_send_sync::<dyn mdct::Mdct<f64>>();
}