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
//! RustDCT is a pure-Rust signal processing library that computes the most common Discrete Cosine Transforms
//!
//! * Discrete Cosine Transform (DCT) Types 1, 2, 3, 4
//! * Discrete Sine Transform (DST) Types 1, 2, 3, 4
//! * Modified Discrete Cosine Transform (MDCT)
//!
//! The recommended way to use RustDCT is to create a [`DCTplanner`](struct.DCTplanner.html) instance, then call its
//! `plan_dct1` or `plan_dct2` or etc methods. Each transform type has its own `plan_*` method which will choose the best algorithm
//! for the given size.
//!
//! ```rust
//! // Compute a DCT Type 2 of size 1234
//! use std::sync::Arc;
//! use rustdct::DCTplanner;
//!
//! let mut input:  Vec<f32> = vec![0f32; 1234];
//! let mut output: Vec<f32> = vec![0f32; 1234];
//!
//! let mut planner = DCTplanner::new();
//! let dct = planner.plan_dct2(1234);
//! dct.process_dct2(&mut input, &mut output);
//!
//! // The DCT instance returned by the planner is stored behind an `Arc`, so it's cheap to clone
//! let dct_clone = Arc::clone(&dct);
//! ```
//! 
//! RustDCT also exposes individual DCT algorithms. For example, if you're writing a JPEG compression library, it's
//! safe to assume you want a DCT2 and DCT3 of size 8. Instead of going through the planner, you can directly create
//! hardcoded DCT instances of size 8.
//! 
//! ```rust
//! // Compute a DCT type 2 of size 8, and then compute a DCT type 3 of size 8 on the output.
//! use rustdct::{DCT2, DCT3};
//! use rustdct::algorithm::type2and3_butterflies::Type2And3Butterfly8;
//! 
//! let mut input = [0f32; 8];
//! let mut intermediate = [0f32; 8];
//! let mut output = [0f32; 8];
//! 
//! let dct = Type2And3Butterfly8::new();
//! 
//! dct.process_dct2(&mut input, &mut intermediate);
//! dct.process_dct3(&mut intermediate, &mut output);
//! ```

pub extern crate rustfft;

pub use rustfft::num_complex;
pub use rustfft::num_traits;

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

pub mod algorithm;

mod plan;
mod twiddles;
mod common;
pub use common::DCTnum;

pub use self::plan::DCTplanner;

#[cfg(test)]
mod test_utils;

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 1 (DCT1)
pub trait DCT1<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 1 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct1(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 2 (DCT2)
pub trait DCT2<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 2 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct2(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 3 (DCT3)
pub trait DCT3<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 3 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct3(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 4 (DCT4)
pub trait DCT4<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 4 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct4(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 5 (DCT5)
pub trait DCT5<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 5 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct5(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 6 (DCT6)
pub trait DCT6<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 6 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct6(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 7 (DCT7)
pub trait DCT7<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 7 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct7(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 8 (DCT8)
pub trait DCT8<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DCT Type 8 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dct8(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 1 (DST1)
pub trait DST1<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 1 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst1(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 2 (DST2)
pub trait DST2<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 2 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst2(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 3 (DST3)
pub trait DST3<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 3 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst3(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Sine Transform Type 4 (DST4)
pub trait DST4<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 4 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst4(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 5 (DST5)
pub trait DST5<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 5 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst5(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 6 (DST6)
pub trait DST6<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 6 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst6(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 7 (DST7)
pub trait DST7<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 7 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst7(&self, input: &mut [T], output: &mut [T]);
}

/// An umbrella trait for algorithms which compute the Discrete Cosine Transform Type 8 (DST8)
pub trait DST8<T: common::DCTnum>: rustfft::Length + Sync + Send {
    /// Computes the DST Type 8 on the `input` buffer and places the result in the `output` buffer.
    ///
    /// This method uses the `input` buffer as scratch space, so the contents of `input` should be considered garbage
    /// after calling
    fn process_dst8(&self, input: &mut [T], output: &mut [T]);
}

/// A trait for algorithms that can compute all of DCT2, DCT3, DST2, DST3, all in one struct
pub trait TransformType2And3<T: common::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: common::DCTnum> : DCT4<T> + DST4<T> {}

/// A trait for algorithms that can compute both DCT6 and DCT7, all in one struct
pub trait DCT6And7<T: common::DCTnum> : DCT6<T> + DCT7<T> {}

/// A trait for algorithms that can compute both DST6 and DST7, all in one struct
pub trait DST6And7<T: common::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>>();
}