更新libclamav库1.0.0版本

This commit is contained in:
2023-01-14 18:28:39 +08:00
parent b879ee0b2e
commit 45fe15f472
8531 changed files with 1222046 additions and 177272 deletions

View File

@@ -0,0 +1 @@
{"files":{"Cargo.lock":"21d58914e730a7d27ae1c513c1830c5f7eb7e7b3841740ac33a4d96d3f4f874d","Cargo.toml":"2a63f29a139ed4489c13613aaad9c36fea3ddff0314b33a2c5ab589680ec27ca","LICENSE-APACHE":"2e54cd84a645bea25943c75dd8ae67cb291e66a47a11578333c9b4b3b6b86c85","LICENSE-MIT":"8f5442dfa8e9169045697e386bc91d19f393c939635741fa2a665ec36ca6f0ad","README.md":"1bd9a4d41ef00c518cdfe4747a7b8da1d5204ac1519bac4db79ca816b2517a96","RELEASES.md":"e813cc1e56c3686d2c9348ae73a9e1a33ae9ff2f0ac2c165dc49ce0f3bd77ac0","benches/bench_dct_naive.rs":"44440d4ddcf7b3824839fe17a4317491027c55a488d8adcc72afa30973f1b51e","benches/bench_dct_planned.rs":"20893eaf375a47d036d72cef61a32fa7098c38c9dc1f7b54094805b036cd7619","benches/bench_dct_via_fft.rs":"55f77b61534863977a9fab643910886f9be001b25501a3dbd4821f2db7e6f4fa","examples/asmtest.rs":"d99a72b5247976384e63342540232c11e885a8ea817efc9bcafa6bcff4f32669","src/algorithm/mod.rs":"d2b00eec735184f4c87de552b6e7197b35abc9e433208b06bb6b28eb6e0588a3","src/algorithm/type1_convert_to_fft.rs":"d1bea808583e6ee0f7fb4e8ea3f7d4b102483466aacf51df69c267cb06ffff36","src/algorithm/type1_naive.rs":"65ad6ce687b7c1ea736c0dedf4264cefcccf42db1a144d9a9f8221127706a58c","src/algorithm/type2and3_butterflies.rs":"94319e93202520ec9cb52d495fe840c85d9a310bf72f8161e6a714d5668f824f","src/algorithm/type2and3_convert_to_fft.rs":"4ab2ed227c7ed32f3b6c79e807cfcefebe83352950b5963651ea56ca79a9dc9b","src/algorithm/type2and3_naive.rs":"32f8f3712b28af1c96003f43e09daa8571bdff4735d6758656fdba76bd9af4ce","src/algorithm/type2and3_splitradix.rs":"15e727cb7397e829884044932731b9db786cd3f66d242da4379e9025930e341d","src/algorithm/type4_convert_to_fft.rs":"14fec6e94af324452af697359504e357cc3a36cd9fc0ebde597923207f072fe0","src/algorithm/type4_convert_to_type3.rs":"bb52c2165d7d00d280fb67ae2298409ce482b6c9c80e72ea5b9e20a28c1030a1","src/algorithm/type4_naive.rs":"ab627d2ed08df22e6b273604f95e50fd23635dde3be4951257cd1f6815555a41","src/algorithm/type5_naive.rs":"94ba2f6a85b473cd027b8add689c357d400ced5400b9143e651ab80eefa64575","src/algorithm/type6and7_convert_to_fft.rs":"e5187d42ba1de755b8c77ef2be53442806d5116f30f2587839916fb2201e5595","src/algorithm/type6and7_naive.rs":"8ea6208d15d0c951336363d7f81e681a19f017c0e672dba899172e981b4a9486","src/algorithm/type8_naive.rs":"b681a0a2dd7a0c64d7ce77196dc24ec85e32e7539b0aa7bc7db4afa078150484","src/array_utils.rs":"a02671a1221ac3622a35d90c3574bd551ea7505e279896c74a883b33c2675dcd","src/common.rs":"18bdd77f5b7b3cb0ab5f84b965f79e6d5451afd99d8940e7200a6524d518c93b","src/lib.rs":"1437407e15889151676e19c1f11666e495a7245f383a4eaabb1dee08ee4bb429","src/mdct/mdct_naive.rs":"d2a2d33189a5f80ef6aca9671fad667e15bd87eff12b8842d6415b5871c86bf0","src/mdct/mdct_via_dct4.rs":"9ac420a3f4463432ccdcdcba6cd7e46e78ede7e8c864a27167a342a6375af7ed","src/mdct/mod.rs":"1d5bc72e0727ac88cd5bec1f3041d0f925e459e829f164b72e1071252043aea9","src/mdct/window_fn.rs":"9f0d5ed8ee5de3bcb4f7b8d3002fda2351f79a40e222cd57cda0a4d3b9044463","src/plan.rs":"234497f159fa348bb99166c55a1ff2844692db2e3efefd4fbac59c48d62e89c0","src/test_utils.rs":"531c2bbb01fcefd38b5ea8dda1d90da571904bb8eaa3abe3e1318bb89ddcb9bb","src/twiddles.rs":"fec377c84c11e30505f04057f815e11c820a23137fd9a1e380dd3dcfa3bbba9f","tests/common/known_data.rs":"c6e2eddc8cba82790f46a69e2f3784ee4c497cc23f864ddaad83b31ba97b1e9d","tests/common/macros.rs":"35d69f0632fe9f0997b5e45318f4aae0cef6eefe97b53d40e199bf46c0a35517","tests/common/mod.rs":"5dde96d1a424fed7389541217c50ae23c5767aa998c4cf80049c7f24008cc7d7","tests/common/reference_impls.rs":"221a44cdc1c98031a8c0bf17b89d52d068cb2a184752c6ecc94b796cdc12e2f9","tests/test_dct.rs":"cc10684907811aa3973300666aa109b38da7f301f1073500437ed7d245c653c9"},"package":"8b61555105d6a9bf98797c063c362a1d24ed8ab0431655e38f1cf51e52089551"}

149
clamav/libclamav_rust/.cargo/vendor/rustdct/Cargo.lock generated vendored Normal file
View File

@@ -0,0 +1,149 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "num-complex"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "primal-check"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01419cee72c1a1ca944554e23d83e483e1bccf378753344e881de28b5487511d"
dependencies = [
"num-integer",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rustdct"
version = "0.7.1"
dependencies = [
"rand",
"rustfft",
]
[[package]]
name = "rustfft"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d089e5c57521629a59f5f39bca7434849ff89bd6873b521afe389c1c602543"
dependencies = [
"num-complex",
"num-integer",
"num-traits",
"primal-check",
"strength_reduce",
"transpose",
]
[[package]]
name = "strength_reduce"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254"
[[package]]
name = "transpose"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95f9c900aa98b6ea43aee227fd680550cdec726526aab8ac801549eadb25e39f"
dependencies = [
"num-integer",
"strength_reduce",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@@ -0,0 +1,40 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "rustdct"
version = "0.7.1"
authors = ["Elliott Mahler <join.together at gmail>"]
description = "Compute Discrete Cosine Transforms (DCT) of any size in O(nlogn) time, in pure Rust."
documentation = "https://docs.rs/rustdct/"
readme = "README.md"
keywords = [
"dct",
"mdct",
"discrete",
"cosine",
"transform",
]
categories = [
"algorithms",
"compression",
"multimedia::encoding",
"science",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/ejmahler/rust_dct"
[dependencies.rustfft]
version = "6"
[dev-dependencies.rand]
version = "0.8"

View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,19 @@
Copyright (c) 2015 The RustFFT Developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,31 @@
# RustDCT
[![](https://img.shields.io/crates/v/rustdct.svg)](https://crates.io/crates/rustdct)
[![](https://img.shields.io/crates/l/rustdct.svg)](https://crates.io/crates/rustdct)
[![](https://docs.rs/rustdct/badge.svg)](https://docs.rs/rustdct/)
![minimum rustc 1.37](https://img.shields.io/badge/rustc-1.37+-red.svg)
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)
## Example
```rust
// Compute a DCT Type 2 of size 1234
use rustdct::DctPlanner;
let mut planner = DctPlanner::new();
let mut dct = planner.plan_dct2(1234);
let mut buffer = vec![0f32; 1234];
dct.process_dct2(&mut buffer);
```
## Compatibility
The `rustdct` crate requires rustc 1.37 or greater.
## Releases
Release notes are available in [RELEASES.md](RELEASES.md).

View File

@@ -0,0 +1,42 @@
# Release 0.7.1
- Upgraded Rand to 0.8
- Small style improvements to unsafe blocks
# Release 0.7
- Upgraded RustFFT to 6.0
- Added consistent support for supplying oversized scratch to DCT methods. Instead of checking that the scratch buffer len is exactly the requested len, we now only check that it's greater than or equal to the requested len.
- Documented the normalization of all DCT/DST methods.
# Release 0.6
- Upgraded RustFFT to 5.0
- Renamed most stucts and traits in the library in order to conform to the [Rust API guidelines](https://rust-lang.github.io/api-guidelines/naming.html) on acronyms
- Refactored all the process() methods to adopt a "in-place with scratch" architecture. This means that, for example, none of the "convert to FFT" algorithms have to allocate scratch space internally, because they now request it from the caller.
# Release 0.5.1
- Added a blanket impl for the DCTnum trait, making it easier to use arbitrary numeric types.
# Release 0.5.0
- Upgraded rustfft version from 3 to 4
- Fixed warning spam from missing `dyn` keyword
# Release 0.4.0
- Renamed `Type2and3` to `TransformType2And3`
- Renamed `Type4` to `TransformType4`
- Upgraded rustfft version from ^2.1 to ^3
- Added a size-3 butterfly for TransformType2And3
# Release 0.3.0
- Merged each DCT2 and DCT3 algorithm into a single struct that implements both both DCT2 and DCT3 traits, and created a "Type2And3" trait to encompass both. They both require the same precomputed data, so we can save memory and setup time by computing both fro mthe same trait.
- Also implemented DST2 and DST3 on the Type2And3 trait -- so a single call to "plan_dct2" or "plan_dct3" etc will let you compute a DCT2, DST3, DCT3, DST3 all from the same instance.
- DCT Type 4 instances can also compute DST Type 4. They implement the "Type4" trait, which includes both DCT4, and DST4.
- Merged MDCT and IMDCT into the same trait
- All of the above are breaking changes that will be very relevant to you if you're referring to specific algorithms or writing your own, but if you're just calling "plan_dct4" or etc, not much should be different.
- Added a DST1 trait and added naive and FFT implementations
- Added DST2, DST3, DST4 traits, and O(nlogn) implementations for each
# Release 0.2.1
- Removed the `pub` keyword from some methods on the `DCTplanner` that should not have been public
# Release 0.2.0
- All of the `DCT#ViaFFT` algorithms now allocate a Vec for internal scratch space, rather than using a member variable, allowing them to be immutable.
- [Breaking Change] The `process` method of all DCT traits take `&self` instead of `&mut self`
- [Breaking Change] Because all DCT instances are now immutable, all DCT instances that depend on other DCT instances take `Arc` pointers instead of `Box` pointers
- [Breaking Change] Because all DCT instances are now immutable, the DCT planner now creates and stores `Arc` pointers to DCT instances instead of `Box`, and has a cache of algorithm instances for each DCT type
- Implemented a "split radix" algorithm for DCT2 and DCT3 that processes power-of-two DCTs much faster than converting them to FFTs
- Used the split radix alogirthm to derive several hardcoded "butterfly" algorithms for small DCT2 and DCT3 sizes (specifically, sizes 2,4,8,16)
- [Breaking Change] Deleted the obsolete `DCT4ViaFFT` algorithm. For odd sizes, use `DCT4ViaFFTOdd` instead. For even sizes, use `DCT4ViaDCT3`. Both are considerably faster and use considerably less memory in all scenarios.
- Lots of style improvements
# Prior releases
No prior release notes were kept.

View File

@@ -0,0 +1,513 @@
#![feature(test)]
extern crate rustdct;
extern crate test;
use rustdct::mdct::{window_fn, Mdct, MdctNaive};
use rustdct::{
algorithm::{Dct1Naive, Dst6And7Naive, Type2And3Naive, Type4Naive},
RequiredScratch,
};
use rustdct::{Dct1, Dct2, Dct3, Dct4, Dst6, Dst7};
use test::Bencher;
/// Times just the DCT1 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct1_naive(b: &mut Bencher, len: usize) {
let dct = Dct1Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct1_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct1_naive_002(b: &mut Bencher) {
bench_dct1_naive(b, 2);
}
#[bench]
fn dct1_naive_004(b: &mut Bencher) {
bench_dct1_naive(b, 4);
}
#[bench]
fn dct1_naive_006(b: &mut Bencher) {
bench_dct1_naive(b, 6);
}
#[bench]
fn dct1_naive_008(b: &mut Bencher) {
bench_dct1_naive(b, 8);
}
#[bench]
fn dct1_naive_010(b: &mut Bencher) {
bench_dct1_naive(b, 10);
}
/// Times just the DCT2 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct2_naive(b: &mut Bencher, len: usize) {
let dct = Type2And3Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct2_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct2_naive_06(b: &mut Bencher) {
bench_dct2_naive(b, 6);
}
#[bench]
fn dct2_naive_05(b: &mut Bencher) {
bench_dct2_naive(b, 5);
}
#[bench]
fn dct2_naive_04(b: &mut Bencher) {
bench_dct2_naive(b, 4);
}
#[bench]
fn dct2_naive_03(b: &mut Bencher) {
bench_dct2_naive(b, 3);
}
#[bench]
fn dct2_naive_02(b: &mut Bencher) {
bench_dct2_naive(b, 2);
}
#[bench]
fn dct2_naive_01(b: &mut Bencher) {
bench_dct2_naive(b, 1);
}
/// Times just the DCT3 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct3_naive(b: &mut Bencher, len: usize) {
let dct = Type2And3Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct3_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct3_naive_0002(b: &mut Bencher) {
bench_dct3_naive(b, 2);
}
#[bench]
fn dct3_naive_0003(b: &mut Bencher) {
bench_dct3_naive(b, 3);
}
#[bench]
fn dct3_naive_0004(b: &mut Bencher) {
bench_dct3_naive(b, 4);
}
#[bench]
fn dct3_naive_0005(b: &mut Bencher) {
bench_dct3_naive(b, 5);
}
#[bench]
fn dct3_naive_0006(b: &mut Bencher) {
bench_dct3_naive(b, 6);
}
/// Times just the DCT4 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct4_naive(b: &mut Bencher, len: usize) {
let dct = Type4Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct4_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct4_even_naive_02(b: &mut Bencher) {
bench_dct4_naive(b, 2);
}
#[bench]
fn dct4_even_naive_04(b: &mut Bencher) {
bench_dct4_naive(b, 4);
}
#[bench]
fn dct4_even_naive_06(b: &mut Bencher) {
bench_dct4_naive(b, 6);
}
#[bench]
fn dct4_even_naive_08(b: &mut Bencher) {
bench_dct4_naive(b, 8);
}
#[bench]
fn dct4_even_naive_10(b: &mut Bencher) {
bench_dct4_naive(b, 10);
}
#[bench]
fn dct4_odd_naive_01(b: &mut Bencher) {
bench_dct4_naive(b, 1);
}
#[bench]
fn dct4_odd_naive_03(b: &mut Bencher) {
bench_dct4_naive(b, 3);
}
#[bench]
fn dct4_odd_naive_05(b: &mut Bencher) {
bench_dct4_naive(b, 5);
}
#[bench]
fn dct4_odd_naive_07(b: &mut Bencher) {
bench_dct4_naive(b, 7);
}
#[bench]
fn dct4_odd_naive_09(b: &mut Bencher) {
bench_dct4_naive(b, 9);
}
/// Times just the MDCT execution (not allocation and pre-calculation)
/// for a given length
fn bench_mdct_naive(b: &mut Bencher, len: usize) {
let dct = MdctNaive::new(len, window_fn::mp3);
let input = vec![0_f32; len * 2];
let (input_a, input_b) = input.split_at(len);
let mut output = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_mdct_with_scratch(input_a, input_b, &mut output, &mut scratch);
});
}
#[bench]
fn mdct_naive_02(b: &mut Bencher) {
bench_mdct_naive(b, 2);
}
#[bench]
fn mdct_naive_04(b: &mut Bencher) {
bench_mdct_naive(b, 4);
}
#[bench]
fn mdct_naive_06(b: &mut Bencher) {
bench_mdct_naive(b, 6);
}
#[bench]
fn mdct_naive_08(b: &mut Bencher) {
bench_mdct_naive(b, 8);
}
#[bench]
fn mdct_naive_10(b: &mut Bencher) {
bench_mdct_naive(b, 10);
}
#[bench]
fn mdct_naive_12(b: &mut Bencher) {
bench_mdct_naive(b, 12);
}
/// Times just the IMDCT execution (not allocation and pre-calculation)
/// for a given length
fn bench_imdct_naive(b: &mut Bencher, len: usize) {
let dct = MdctNaive::new(len, window_fn::mp3);
let input = vec![0_f32; len];
let mut output = vec![0_f32; len * 2];
let (output_a, output_b) = output.split_at_mut(len);
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_imdct_with_scratch(&input, output_a, output_b, &mut scratch);
});
}
#[bench]
fn imdct_naive_02(b: &mut Bencher) {
bench_imdct_naive(b, 2);
}
#[bench]
fn imdct_naive_04(b: &mut Bencher) {
bench_imdct_naive(b, 4);
}
#[bench]
fn imdct_naive_06(b: &mut Bencher) {
bench_imdct_naive(b, 6);
}
#[bench]
fn imdct_naive_08(b: &mut Bencher) {
bench_imdct_naive(b, 8);
}
#[bench]
fn imdct_naive_10(b: &mut Bencher) {
bench_imdct_naive(b, 10);
}
#[bench]
fn imdct_naive_12(b: &mut Bencher) {
bench_imdct_naive(b, 12);
}
/// Times just the DST6 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dst6_naive(b: &mut Bencher, len: usize) {
let dct = Dst6And7Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dst6_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dst6_even_naive_10(b: &mut Bencher) {
bench_dst6_naive(b, 10);
}
#[bench]
fn dst6_even_naive_11(b: &mut Bencher) {
bench_dst6_naive(b, 11);
}
#[bench]
fn dst6_even_naive_12(b: &mut Bencher) {
bench_dst6_naive(b, 12);
}
#[bench]
fn dst6_even_naive_13(b: &mut Bencher) {
bench_dst6_naive(b, 13);
}
#[bench]
fn dst6_even_naive_14(b: &mut Bencher) {
bench_dst6_naive(b, 14);
}
#[bench]
fn dst6_even_naive_15(b: &mut Bencher) {
bench_dst6_naive(b, 15);
}
#[bench]
fn dst6_even_naive_16(b: &mut Bencher) {
bench_dst6_naive(b, 16);
}
#[bench]
fn dst6_even_naive_17(b: &mut Bencher) {
bench_dst6_naive(b, 17);
}
#[bench]
fn dst6_even_naive_18(b: &mut Bencher) {
bench_dst6_naive(b, 18);
}
#[bench]
fn dst6_even_naive_19(b: &mut Bencher) {
bench_dst6_naive(b, 19);
}
#[bench]
fn dst6_even_naive_20(b: &mut Bencher) {
bench_dst6_naive(b, 20);
}
#[bench]
fn dst6_even_naive_21(b: &mut Bencher) {
bench_dst6_naive(b, 21);
}
#[bench]
fn dst6_even_naive_22(b: &mut Bencher) {
bench_dst6_naive(b, 22);
}
#[bench]
fn dst6_even_naive_23(b: &mut Bencher) {
bench_dst6_naive(b, 23);
}
#[bench]
fn dst6_even_naive_24(b: &mut Bencher) {
bench_dst6_naive(b, 24);
}
#[bench]
fn dst6_even_naive_25(b: &mut Bencher) {
bench_dst6_naive(b, 25);
}
#[bench]
fn dst6_even_naive_26(b: &mut Bencher) {
bench_dst6_naive(b, 26);
}
#[bench]
fn dst6_even_naive_27(b: &mut Bencher) {
bench_dst6_naive(b, 27);
}
#[bench]
fn dst6_even_naive_28(b: &mut Bencher) {
bench_dst6_naive(b, 28);
}
#[bench]
fn dst6_even_naive_29(b: &mut Bencher) {
bench_dst6_naive(b, 29);
}
#[bench]
fn dst6_even_naive_30(b: &mut Bencher) {
bench_dst6_naive(b, 30);
}
#[bench]
fn dst6_even_naive_31(b: &mut Bencher) {
bench_dst6_naive(b, 31);
}
#[bench]
fn dst6_even_naive_32(b: &mut Bencher) {
bench_dst6_naive(b, 32);
}
#[bench]
fn dst6_even_naive_33(b: &mut Bencher) {
bench_dst6_naive(b, 33);
}
#[bench]
fn dst6_even_naive_34(b: &mut Bencher) {
bench_dst6_naive(b, 34);
}
#[bench]
fn dst6_even_naive_35(b: &mut Bencher) {
bench_dst6_naive(b, 35);
}
#[bench]
fn dst6_even_naive_36(b: &mut Bencher) {
bench_dst6_naive(b, 36);
}
#[bench]
fn dst6_even_naive_37(b: &mut Bencher) {
bench_dst6_naive(b, 37);
}
#[bench]
fn dst6_even_naive_38(b: &mut Bencher) {
bench_dst6_naive(b, 38);
}
#[bench]
fn dst6_even_naive_39(b: &mut Bencher) {
bench_dst6_naive(b, 39);
}
/// Times just the DST7 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dst7_naive(b: &mut Bencher, len: usize) {
let dct = Dst6And7Naive::new(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dst7_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dst7_even_naive_10(b: &mut Bencher) {
bench_dst7_naive(b, 10);
}
#[bench]
fn dst7_even_naive_11(b: &mut Bencher) {
bench_dst7_naive(b, 11);
}
#[bench]
fn dst7_even_naive_12(b: &mut Bencher) {
bench_dst7_naive(b, 12);
}
#[bench]
fn dst7_even_naive_13(b: &mut Bencher) {
bench_dst7_naive(b, 13);
}
#[bench]
fn dst7_even_naive_14(b: &mut Bencher) {
bench_dst7_naive(b, 14);
}
#[bench]
fn dst7_even_naive_15(b: &mut Bencher) {
bench_dst7_naive(b, 15);
}
#[bench]
fn dst7_even_naive_16(b: &mut Bencher) {
bench_dst7_naive(b, 16);
}
#[bench]
fn dst7_even_naive_17(b: &mut Bencher) {
bench_dst7_naive(b, 17);
}
#[bench]
fn dst7_even_naive_18(b: &mut Bencher) {
bench_dst7_naive(b, 18);
}
#[bench]
fn dst7_even_naive_19(b: &mut Bencher) {
bench_dst7_naive(b, 19);
}
#[bench]
fn dst7_even_naive_20(b: &mut Bencher) {
bench_dst7_naive(b, 20);
}
#[bench]
fn dst7_even_naive_21(b: &mut Bencher) {
bench_dst7_naive(b, 21);
}
#[bench]
fn dst7_even_naive_22(b: &mut Bencher) {
bench_dst7_naive(b, 22);
}
#[bench]
fn dst7_even_naive_23(b: &mut Bencher) {
bench_dst7_naive(b, 23);
}
#[bench]
fn dst7_even_naive_24(b: &mut Bencher) {
bench_dst7_naive(b, 24);
}
#[bench]
fn dst7_even_naive_25(b: &mut Bencher) {
bench_dst7_naive(b, 25);
}
#[bench]
fn dst7_even_naive_26(b: &mut Bencher) {
bench_dst7_naive(b, 26);
}
#[bench]
fn dst7_even_naive_27(b: &mut Bencher) {
bench_dst7_naive(b, 27);
}
#[bench]
fn dst7_even_naive_28(b: &mut Bencher) {
bench_dst7_naive(b, 28);
}
#[bench]
fn dst7_even_naive_29(b: &mut Bencher) {
bench_dst7_naive(b, 29);
}
#[bench]
fn dst7_even_naive_30(b: &mut Bencher) {
bench_dst7_naive(b, 30);
}
#[bench]
fn dst7_even_naive_31(b: &mut Bencher) {
bench_dst7_naive(b, 31);
}
#[bench]
fn dst7_even_naive_32(b: &mut Bencher) {
bench_dst7_naive(b, 32);
}
#[bench]
fn dst7_even_naive_33(b: &mut Bencher) {
bench_dst7_naive(b, 33);
}
#[bench]
fn dst7_even_naive_34(b: &mut Bencher) {
bench_dst7_naive(b, 34);
}
#[bench]
fn dst7_even_naive_35(b: &mut Bencher) {
bench_dst7_naive(b, 35);
}
#[bench]
fn dst7_even_naive_36(b: &mut Bencher) {
bench_dst7_naive(b, 36);
}
#[bench]
fn dst7_even_naive_37(b: &mut Bencher) {
bench_dst7_naive(b, 37);
}
#[bench]
fn dst7_even_naive_38(b: &mut Bencher) {
bench_dst7_naive(b, 38);
}
#[bench]
fn dst7_even_naive_39(b: &mut Bencher) {
bench_dst7_naive(b, 39);
}

View File

@@ -0,0 +1,97 @@
#![feature(test)]
extern crate rustdct;
extern crate test;
use test::Bencher;
/// Times just the DCT1 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct1_planned(b: &mut Bencher, len: usize) {
let mut planner = rustdct::DctPlanner::new();
let dct = planner.plan_dct1(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct1_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct1_planned_00256(b: &mut Bencher) {
bench_dct1_planned(b, 256);
}
#[bench]
fn dct1_planned_65536(b: &mut Bencher) {
bench_dct1_planned(b, 65536);
}
/// Times just the DCT2 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct2_planned(b: &mut Bencher, len: usize) {
let mut planner = rustdct::DctPlanner::new();
let dct = planner.plan_dct2(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct2_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct2_planned_00256(b: &mut Bencher) {
bench_dct2_planned(b, 256);
}
#[bench]
fn dct2_planned_65536(b: &mut Bencher) {
bench_dct2_planned(b, 65536);
}
/// Times just the DCT3 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct3_planned(b: &mut Bencher, len: usize) {
let mut planner = rustdct::DctPlanner::new();
let dct = planner.plan_dct3(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct3_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct3_planned_00256(b: &mut Bencher) {
bench_dct3_planned(b, 256);
}
#[bench]
fn dct3_planned_65536(b: &mut Bencher) {
bench_dct3_planned(b, 65536);
}
/// Times just the DCT4 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct4_planned(b: &mut Bencher, len: usize) {
let mut planner = rustdct::DctPlanner::new();
let dct = planner.plan_dct4(len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct4_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct4_planned_0000256(b: &mut Bencher) {
bench_dct4_planned(b, 256);
}
#[bench]
fn dct4_planned_0999999(b: &mut Bencher) {
bench_dct4_planned(b, 999999);
}
#[bench]
fn dct4_planned_1000000(b: &mut Bencher) {
bench_dct4_planned(b, 1000000);
}

View File

@@ -0,0 +1,734 @@
#![feature(test)]
extern crate rustdct;
extern crate test;
use std::sync::Arc;
use rustdct::algorithm::*;
use rustdct::mdct::{window_fn, Mdct, MdctViaDct4};
use rustdct::rustfft::FftPlanner;
use rustdct::DctPlanner;
use rustdct::{algorithm::type2and3_butterflies::*, RequiredScratch};
use rustdct::{Dct1, Dct2, Dct3, Dct4, Dst6, Dst7, TransformType2And3};
use test::Bencher;
/// Times just the DCT1 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct1_fft(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let dct = Dct1ConvertToFft::new(planner.plan_fft_forward((len - 1) * 2));
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct1_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct1_fft_002(b: &mut Bencher) {
bench_dct1_fft(b, 2);
}
#[bench]
fn dct1_fft_004(b: &mut Bencher) {
bench_dct1_fft(b, 4);
}
#[bench]
fn dct1_fft_006(b: &mut Bencher) {
bench_dct1_fft(b, 6);
}
#[bench]
fn dct1_fft_008(b: &mut Bencher) {
bench_dct1_fft(b, 8);
}
#[bench]
fn dct1_fft_010(b: &mut Bencher) {
bench_dct1_fft(b, 10);
}
/// Times just the DCT2 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct2_fft(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let dct = Type2And3ConvertToFft::new(planner.plan_fft_forward(len));
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct2_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct2_fft_06(b: &mut Bencher) {
bench_dct2_fft(b, 6);
}
#[bench]
fn dct2_fft_05(b: &mut Bencher) {
bench_dct2_fft(b, 5);
}
#[bench]
fn dct2_fft_04(b: &mut Bencher) {
bench_dct2_fft(b, 4);
}
#[bench]
fn dct2_fft_03(b: &mut Bencher) {
bench_dct2_fft(b, 3);
}
#[bench]
fn dct2_fft_02(b: &mut Bencher) {
bench_dct2_fft(b, 2);
}
#[bench]
fn dct2_fft_01(b: &mut Bencher) {
bench_dct2_fft(b, 1);
}
/// Times just the DCT2 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct2_split(b: &mut Bencher, len: usize) {
let power = len.trailing_zeros() as usize;
let mut instances = vec![
Arc::new(Type2And3Naive::new(1)) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly2::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly4::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly8::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly16::new()) as Arc<dyn TransformType2And3<f32>>,
];
for i in instances.len()..(power + 1) {
let dct = Arc::new(Type2And3SplitRadix::new(
instances[i - 1].clone(),
instances[i - 2].clone(),
));
instances.push(dct);
}
let dct = instances[power].clone();
assert_eq!(dct.len(), len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct2_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct2_power2_split_0002(b: &mut Bencher) {
bench_dct2_split(b, 2);
}
#[bench]
fn dct2_power2_split_0004(b: &mut Bencher) {
bench_dct2_split(b, 4);
}
#[bench]
fn dct2_power2_split_0008(b: &mut Bencher) {
bench_dct2_split(b, 4);
}
#[bench]
fn dct2_power2_split_0016(b: &mut Bencher) {
bench_dct2_split(b, 16);
}
#[bench]
fn dct2_power2_split_0032(b: &mut Bencher) {
bench_dct2_split(b, 32);
}
#[bench]
fn dct2_power2_split_0064(b: &mut Bencher) {
bench_dct2_split(b, 64);
}
#[bench]
fn dct2_power2_split_0128(b: &mut Bencher) {
bench_dct2_split(b, 128);
}
#[bench]
fn dct2_power2_split_0256(b: &mut Bencher) {
bench_dct2_split(b, 256);
}
#[bench]
fn dct2_power2_split_065536(b: &mut Bencher) {
bench_dct2_split(b, 65536);
}
#[bench]
fn dct2_power2_fft_0002(b: &mut Bencher) {
bench_dct2_fft(b, 2);
}
#[bench]
fn dct2_power2_fft_0004(b: &mut Bencher) {
bench_dct2_fft(b, 4);
}
#[bench]
fn dct2_power2_fft_0008(b: &mut Bencher) {
bench_dct2_fft(b, 4);
}
#[bench]
fn dct2_power2_fft_0016(b: &mut Bencher) {
bench_dct2_fft(b, 16);
}
#[bench]
fn dct2_power2_fft_0032(b: &mut Bencher) {
bench_dct2_fft(b, 32);
}
#[bench]
fn dct2_power2_fft_0064(b: &mut Bencher) {
bench_dct2_fft(b, 64);
}
#[bench]
fn dct2_power2_fft_0128(b: &mut Bencher) {
bench_dct2_fft(b, 128);
}
#[bench]
fn dct2_power2_fft_0256(b: &mut Bencher) {
bench_dct2_fft(b, 256);
}
#[bench]
fn dct2_power2_fft_065536(b: &mut Bencher) {
bench_dct2_fft(b, 65536);
}
/// Times just the DCT3 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct3_fft(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let dct = Type2And3ConvertToFft::new(planner.plan_fft_forward(len));
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct3_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct3_fft_003(b: &mut Bencher) {
bench_dct3_fft(b, 3);
}
#[bench]
fn dct3_fft_004(b: &mut Bencher) {
bench_dct3_fft(b, 4);
}
#[bench]
fn dct3_fft_005(b: &mut Bencher) {
bench_dct3_fft(b, 5);
}
#[bench]
fn dct3_fft_006(b: &mut Bencher) {
bench_dct3_fft(b, 6);
}
#[bench]
fn dct3_power2_fft_00004(b: &mut Bencher) {
bench_dct3_fft(b, 4);
}
#[bench]
fn dct3_power2_fft_00008(b: &mut Bencher) {
bench_dct3_fft(b, 8);
}
#[bench]
fn dct3_power2_fft_00016(b: &mut Bencher) {
bench_dct3_fft(b, 16);
}
#[bench]
fn dct3_power2_fft_00032(b: &mut Bencher) {
bench_dct3_fft(b, 32);
}
#[bench]
fn dct3_power2_fft_00064(b: &mut Bencher) {
bench_dct3_fft(b, 64);
}
#[bench]
fn dct3_power2_fft_00256(b: &mut Bencher) {
bench_dct3_fft(b, 256);
}
#[bench]
fn dct3_power2_fft_065536(b: &mut Bencher) {
bench_dct3_fft(b, 65536);
}
#[bench]
fn dct3_power2_fft_16777216(b: &mut Bencher) {
bench_dct3_fft(b, 16777216);
}
/// Times just the DCT2 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct3_split(b: &mut Bencher, len: usize) {
let power = len.trailing_zeros() as usize;
let mut instances = vec![
Arc::new(Type2And3Naive::new(1)) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly2::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly4::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly8::new()) as Arc<dyn TransformType2And3<f32>>,
Arc::new(Type2And3Butterfly16::new()) as Arc<dyn TransformType2And3<f32>>,
];
for i in instances.len()..(power + 1) {
let dct = Arc::new(Type2And3SplitRadix::new(
instances[i - 1].clone(),
instances[i - 2].clone(),
));
instances.push(dct);
}
let dct = instances[power].clone();
assert_eq!(dct.len(), len);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct3_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct3_power2_split_0002(b: &mut Bencher) {
bench_dct3_split(b, 4);
}
#[bench]
fn dct3_power2_split_0004(b: &mut Bencher) {
bench_dct3_split(b, 4);
}
#[bench]
fn dct3_power2_split_0008(b: &mut Bencher) {
bench_dct3_split(b, 4);
}
#[bench]
fn dct3_power2_split_0016(b: &mut Bencher) {
bench_dct3_split(b, 16);
}
#[bench]
fn dct3_power2_split_0064(b: &mut Bencher) {
bench_dct3_split(b, 64);
}
#[bench]
fn dct3_power2_split_0256(b: &mut Bencher) {
bench_dct3_split(b, 256);
}
#[bench]
fn dct3_power2_split_065536(b: &mut Bencher) {
bench_dct3_split(b, 65536);
}
/// Times just the DCT4 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct4_via_dct3(b: &mut Bencher, len: usize) {
let mut planner = DctPlanner::new();
let inner_dct3 = planner.plan_dct3(len / 2);
let dct = Type4ConvertToType3Even::new(inner_dct3);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct4_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct4_even_via_dct3_02(b: &mut Bencher) {
bench_dct4_via_dct3(b, 2);
}
#[bench]
fn dct4_even_via_dct3_04(b: &mut Bencher) {
bench_dct4_via_dct3(b, 4);
}
#[bench]
fn dct4_even_via_dct3_06(b: &mut Bencher) {
bench_dct4_via_dct3(b, 6);
}
#[bench]
fn dct4_even_via_dct3_08(b: &mut Bencher) {
bench_dct4_via_dct3(b, 8);
}
#[bench]
fn dct4_even_via_dct3_10(b: &mut Bencher) {
bench_dct4_via_dct3(b, 10);
}
#[bench]
fn dct4_even_via_dct3_1000000(b: &mut Bencher) {
bench_dct4_via_dct3(b, 1000000);
}
/// Times just the DCT4 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dct4_via_fft_odd(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let inner_fft = planner.plan_fft_forward(len);
let dct = Type4ConvertToFftOdd::new(inner_fft);
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dct4_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dct4_odd_via_fft_01(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 1);
}
#[bench]
fn dct4_odd_via_fft_03(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 3);
}
#[bench]
fn dct4_odd_via_fft_05(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 5);
}
#[bench]
fn dct4_odd_via_fft_07(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 7);
}
#[bench]
fn dct4_odd_via_fft_09(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 9);
}
#[bench]
fn dct4_odd_via_fft_999999(b: &mut Bencher) {
bench_dct4_via_fft_odd(b, 999999);
}
/// Times just the MDCT execution (not allocation and pre-calculation)
/// for a given length
fn bench_mdct_fft(b: &mut Bencher, len: usize) {
let mut planner = DctPlanner::new();
let dct = MdctViaDct4::new(planner.plan_dct4(len), window_fn::mp3);
let input = vec![0_f32; len * 2];
let (input_a, input_b) = input.split_at(len);
let mut output = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_mdct_with_scratch(input_a, input_b, &mut output, &mut scratch);
});
}
#[bench]
fn mdct_fft_02(b: &mut Bencher) {
bench_mdct_fft(b, 2);
}
#[bench]
fn mdct_fft_04(b: &mut Bencher) {
bench_mdct_fft(b, 4);
}
#[bench]
fn mdct_fft_06(b: &mut Bencher) {
bench_mdct_fft(b, 6);
}
#[bench]
fn mdct_fft_08(b: &mut Bencher) {
bench_mdct_fft(b, 8);
}
#[bench]
fn mdct_fft_10(b: &mut Bencher) {
bench_mdct_fft(b, 10);
}
#[bench]
fn mdct_fft_12(b: &mut Bencher) {
bench_mdct_fft(b, 12);
}
/// Times just the IMDCT execution (not allocation and pre-calculation)
/// for a given length
fn bench_imdct_fft(b: &mut Bencher, len: usize) {
let mut planner = DctPlanner::new();
let dct = MdctViaDct4::new(planner.plan_dct4(len), window_fn::mp3);
let input = vec![0_f32; len];
let mut output = vec![0_f32; len * 2];
let (output_a, output_b) = output.split_at_mut(len);
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_imdct_with_scratch(&input, output_a, output_b, &mut scratch);
});
}
#[bench]
fn imdct_fft_02(b: &mut Bencher) {
bench_imdct_fft(b, 2);
}
#[bench]
fn imdct_fft_04(b: &mut Bencher) {
bench_imdct_fft(b, 4);
}
#[bench]
fn imdct_fft_06(b: &mut Bencher) {
bench_imdct_fft(b, 6);
}
#[bench]
fn imdct_fft_08(b: &mut Bencher) {
bench_imdct_fft(b, 8);
}
#[bench]
fn imdct_fft_10(b: &mut Bencher) {
bench_imdct_fft(b, 10);
}
#[bench]
fn imdct_fft_12(b: &mut Bencher) {
bench_imdct_fft(b, 12);
}
/// Times just the DST6 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dst6_fft(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let dct = Dst6And7ConvertToFft::new(planner.plan_fft_forward(len * 2 + 1));
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dst6_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dst6_fft_10(b: &mut Bencher) {
bench_dst6_fft(b, 10);
}
#[bench]
fn dst6_fft_11(b: &mut Bencher) {
bench_dst6_fft(b, 11);
}
#[bench]
fn dst6_fft_12(b: &mut Bencher) {
bench_dst6_fft(b, 12);
}
#[bench]
fn dst6_fft_13(b: &mut Bencher) {
bench_dst6_fft(b, 13);
}
#[bench]
fn dst6_fft_14(b: &mut Bencher) {
bench_dst6_fft(b, 14);
}
#[bench]
fn dst6_fft_15(b: &mut Bencher) {
bench_dst6_fft(b, 15);
}
#[bench]
fn dst6_fft_16(b: &mut Bencher) {
bench_dst6_fft(b, 16);
}
#[bench]
fn dst6_fft_17(b: &mut Bencher) {
bench_dst6_fft(b, 17);
}
#[bench]
fn dst6_fft_18(b: &mut Bencher) {
bench_dst6_fft(b, 18);
}
#[bench]
fn dst6_fft_19(b: &mut Bencher) {
bench_dst6_fft(b, 19);
}
#[bench]
fn dst6_fft_20(b: &mut Bencher) {
bench_dst6_fft(b, 20);
}
#[bench]
fn dst6_fft_21(b: &mut Bencher) {
bench_dst6_fft(b, 21);
}
#[bench]
fn dst6_fft_22(b: &mut Bencher) {
bench_dst6_fft(b, 22);
}
#[bench]
fn dst6_fft_23(b: &mut Bencher) {
bench_dst6_fft(b, 23);
}
#[bench]
fn dst6_fft_24(b: &mut Bencher) {
bench_dst6_fft(b, 24);
}
#[bench]
fn dst6_fft_25(b: &mut Bencher) {
bench_dst6_fft(b, 25);
}
#[bench]
fn dst6_fft_26(b: &mut Bencher) {
bench_dst6_fft(b, 26);
}
#[bench]
fn dst6_fft_27(b: &mut Bencher) {
bench_dst6_fft(b, 27);
}
#[bench]
fn dst6_fft_28(b: &mut Bencher) {
bench_dst6_fft(b, 28);
}
#[bench]
fn dst6_fft_29(b: &mut Bencher) {
bench_dst6_fft(b, 29);
}
#[bench]
fn dst6_fft_30(b: &mut Bencher) {
bench_dst6_fft(b, 30);
}
#[bench]
fn dst6_fft_31(b: &mut Bencher) {
bench_dst6_fft(b, 31);
}
#[bench]
fn dst6_fft_32(b: &mut Bencher) {
bench_dst6_fft(b, 32);
}
#[bench]
fn dst6_fft_33(b: &mut Bencher) {
bench_dst6_fft(b, 33);
}
#[bench]
fn dst6_fft_34(b: &mut Bencher) {
bench_dst6_fft(b, 34);
}
#[bench]
fn dst6_fft_35(b: &mut Bencher) {
bench_dst6_fft(b, 35);
}
#[bench]
fn dst6_fft_36(b: &mut Bencher) {
bench_dst6_fft(b, 36);
}
#[bench]
fn dst6_fft_37(b: &mut Bencher) {
bench_dst6_fft(b, 37);
}
#[bench]
fn dst6_fft_38(b: &mut Bencher) {
bench_dst6_fft(b, 38);
}
#[bench]
fn dst6_fft_39(b: &mut Bencher) {
bench_dst6_fft(b, 39);
}
/// Times just the DST6 execution (not allocation and pre-calculation)
/// for a given length
fn bench_dst7_fft(b: &mut Bencher, len: usize) {
let mut planner = FftPlanner::new();
let dct = Dst6And7ConvertToFft::new(planner.plan_fft_forward(len * 2 + 1));
let mut buffer = vec![0_f32; len];
let mut scratch = vec![0_f32; dct.get_scratch_len()];
b.iter(|| {
dct.process_dst7_with_scratch(&mut buffer, &mut scratch);
});
}
#[bench]
fn dst7_fft_10(b: &mut Bencher) {
bench_dst7_fft(b, 10);
}
#[bench]
fn dst7_fft_11(b: &mut Bencher) {
bench_dst7_fft(b, 11);
}
#[bench]
fn dst7_fft_12(b: &mut Bencher) {
bench_dst7_fft(b, 12);
}
#[bench]
fn dst7_fft_13(b: &mut Bencher) {
bench_dst7_fft(b, 13);
}
#[bench]
fn dst7_fft_14(b: &mut Bencher) {
bench_dst7_fft(b, 14);
}
#[bench]
fn dst7_fft_15(b: &mut Bencher) {
bench_dst7_fft(b, 15);
}
#[bench]
fn dst7_fft_16(b: &mut Bencher) {
bench_dst7_fft(b, 16);
}
#[bench]
fn dst7_fft_17(b: &mut Bencher) {
bench_dst7_fft(b, 17);
}
#[bench]
fn dst7_fft_18(b: &mut Bencher) {
bench_dst7_fft(b, 18);
}
#[bench]
fn dst7_fft_19(b: &mut Bencher) {
bench_dst7_fft(b, 19);
}
#[bench]
fn dst7_fft_20(b: &mut Bencher) {
bench_dst7_fft(b, 20);
}
#[bench]
fn dst7_fft_21(b: &mut Bencher) {
bench_dst7_fft(b, 21);
}
#[bench]
fn dst7_fft_22(b: &mut Bencher) {
bench_dst7_fft(b, 22);
}
#[bench]
fn dst7_fft_23(b: &mut Bencher) {
bench_dst7_fft(b, 23);
}
#[bench]
fn dst7_fft_24(b: &mut Bencher) {
bench_dst7_fft(b, 24);
}
#[bench]
fn dst7_fft_25(b: &mut Bencher) {
bench_dst7_fft(b, 25);
}
#[bench]
fn dst7_fft_26(b: &mut Bencher) {
bench_dst7_fft(b, 26);
}
#[bench]
fn dst7_fft_27(b: &mut Bencher) {
bench_dst7_fft(b, 27);
}
#[bench]
fn dst7_fft_28(b: &mut Bencher) {
bench_dst7_fft(b, 28);
}
#[bench]
fn dst7_fft_29(b: &mut Bencher) {
bench_dst7_fft(b, 29);
}
#[bench]
fn dst7_fft_30(b: &mut Bencher) {
bench_dst7_fft(b, 30);
}
#[bench]
fn dst7_fft_31(b: &mut Bencher) {
bench_dst7_fft(b, 31);
}
#[bench]
fn dst7_fft_32(b: &mut Bencher) {
bench_dst7_fft(b, 32);
}
#[bench]
fn dst7_fft_33(b: &mut Bencher) {
bench_dst7_fft(b, 33);
}
#[bench]
fn dst7_fft_34(b: &mut Bencher) {
bench_dst7_fft(b, 34);
}
#[bench]
fn dst7_fft_35(b: &mut Bencher) {
bench_dst7_fft(b, 35);
}
#[bench]
fn dst7_fft_36(b: &mut Bencher) {
bench_dst7_fft(b, 36);
}
#[bench]
fn dst7_fft_37(b: &mut Bencher) {
bench_dst7_fft(b, 37);
}
#[bench]
fn dst7_fft_38(b: &mut Bencher) {
bench_dst7_fft(b, 38);
}
#[bench]
fn dst7_fft_39(b: &mut Bencher) {
bench_dst7_fft(b, 39);
}

View File

@@ -0,0 +1,22 @@
//! This example is meant to be used for inspecting the generated assembly.
//! This can be interesting when working with simd intrinsics, or trying to reduce code size
//!
//! To use:
//! - Mark the function that should be investigated with `#[inline(never)]`.
//! - If needed, add any required feature to the function, for example `#[target_feature(enable = "sse4.1")]`
//! - Change the code below to use the changed function.
//! Currently it is set up to call a generic planned DCT, which will cause all dct code to be compiled
//! - Ask rustc to output assembly code:
//! `cargo rustc --release --example asmtest -- --emit=asm`
//! - This will create a file at `target/release/examples/asmtest-0123456789abcdef.s` (with a random number in the filename).
//! - Open this file and search for the function.
use rustdct::DctPlanner;
fn main() {
let mut planner = DctPlanner::new();
let dct = planner.plan_dct2(4);
let mut buffer: Vec<f32> = vec![0.0; dct.len()];
dct.process_dct2(&mut buffer);
}

View File

@@ -0,0 +1,39 @@
mod type1_convert_to_fft;
mod type1_naive;
pub mod type2and3_butterflies;
mod type2and3_convert_to_fft;
mod type2and3_naive;
mod type2and3_splitradix;
mod type4_convert_to_fft;
mod type4_convert_to_type3;
mod type4_naive;
mod type5_naive;
mod type6and7_convert_to_fft;
mod type6and7_naive;
mod type8_naive;
pub use self::type1_convert_to_fft::Dct1ConvertToFft;
pub use self::type1_convert_to_fft::Dst1ConvertToFft;
pub use self::type1_naive::Dct1Naive;
pub use self::type1_naive::Dst1Naive;
pub use self::type2and3_convert_to_fft::Type2And3ConvertToFft;
pub use self::type2and3_naive::Type2And3Naive;
pub use self::type2and3_splitradix::Type2And3SplitRadix;
pub use self::type4_convert_to_fft::Type4ConvertToFftOdd;
pub use self::type4_convert_to_type3::Type4ConvertToType3Even;
pub use self::type4_naive::Type4Naive;
pub use self::type5_naive::Dct5Naive;
pub use self::type5_naive::Dst5Naive;
pub use self::type6and7_convert_to_fft::Dst6And7ConvertToFft;
pub use self::type6and7_naive::Dct6And7Naive;
pub use self::type6and7_naive::Dst6And7Naive;
pub use self::type8_naive::Dct8Naive;
pub use self::type8_naive::Dst8Naive;

View File

@@ -0,0 +1,263 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::FftDirection;
use rustfft::{Fft, Length};
use crate::common::dct_error_inplace;
use crate::{array_utils::into_complex_mut, DctNum, RequiredScratch};
use crate::{Dct1, Dst1};
/// DCT Type 1 implementation that converts the problem into a FFT of size 2 * (n - 1)
///
/// ~~~
/// // Computes a DCT Type 1 of size 1234
/// use rustdct::Dct1;
/// use rustdct::algorithm::Dct1ConvertToFft;
/// use rustdct::rustfft::FftPlanner;
///
/// let len = 1234;
///
/// let mut planner = FftPlanner::new();
/// let fft = planner.plan_fft_forward(2 * (len - 1));
///
/// let dct = Dct1ConvertToFft::new(fft);
///
/// let mut buffer = vec![0f32; len];
/// dct.process_dct1(&mut buffer);
pub struct Dct1ConvertToFft<T> {
fft: Arc<dyn Fft<T>>,
len: usize,
scratch_len: usize,
inner_fft_len: usize,
}
impl<T: DctNum> Dct1ConvertToFft<T> {
/// Creates a new DCT1 context that will process signals of length `inner_fft.len() / 2 + 1`.
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
let inner_fft_len = inner_fft.len();
assert!(
inner_fft_len % 2 == 0,
"For DCT1 via FFT, the inner FFT size must be even. Got {}",
inner_fft_len
);
assert_eq!(
inner_fft.fft_direction(),
FftDirection::Forward,
"The 'DCT type 1 via FFT' algorithm requires a forward FFT, but an inverse FFT \
was provided"
);
let len = inner_fft_len / 2 + 1;
Self {
scratch_len: 2 * (inner_fft_len + inner_fft.get_inplace_scratch_len()),
inner_fft_len,
fft: inner_fft,
len,
}
}
}
impl<T: DctNum> Dct1<T> for Dct1ConvertToFft<T> {
fn process_dct1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(self.inner_fft_len);
for (&input_val, fft_cell) in buffer.iter().zip(&mut fft_buffer[..buffer.len()]) {
*fft_cell = Complex {
re: input_val,
im: T::zero(),
};
}
for (&input_val, fft_cell) in buffer
.iter()
.rev()
.skip(1)
.zip(&mut fft_buffer[buffer.len()..])
{
*fft_cell = Complex {
re: input_val,
im: T::zero(),
};
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// apply a correction factor to the result
let half = T::half();
for (fft_entry, output_val) in fft_buffer.iter().zip(buffer.iter_mut()) {
*output_val = fft_entry.re * half;
}
}
}
impl<T: DctNum> RequiredScratch for Dct1ConvertToFft<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
impl<T> Length for Dct1ConvertToFft<T> {
fn len(&self) -> usize {
self.len
}
}
/// DST Type 1 implementation that converts the problem into a FFT of size 2 * (n + 1)
///
/// ~~~
/// // Computes a DST Type 1 of size 1234
/// use rustdct::Dst1;
/// use rustdct::algorithm::Dst1ConvertToFft;
/// use rustdct::rustfft::FftPlanner;
///
/// let len = 1234;
///
/// let mut planner = FftPlanner::new();
/// let fft = planner.plan_fft_forward(2 * (len + 1));
///
/// let dct = Dst1ConvertToFft::new(fft);
///
/// let mut buffer = vec![0f32; len];
/// dct.process_dst1(&mut buffer);
/// ~~~
pub struct Dst1ConvertToFft<T> {
fft: Arc<dyn Fft<T>>,
len: usize,
scratch_len: usize,
inner_fft_len: usize,
}
impl<T: DctNum> Dst1ConvertToFft<T> {
/// Creates a new DST1 context that will process signals of length `inner_fft.len() / 2 - 1`.
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
let inner_fft_len = inner_fft.len();
assert!(
inner_fft_len % 2 == 0,
"For DCT1 via FFT, the inner FFT size must be even. Got {}",
inner_fft_len
);
assert_eq!(
inner_fft.fft_direction(),
FftDirection::Forward,
"The 'DCT type 1 via FFT' algorithm requires a forward FFT, but an inverse FFT \
was provided"
);
let len = inner_fft_len / 2 - 1;
Self {
scratch_len: 2 * (inner_fft_len + inner_fft.get_inplace_scratch_len()),
inner_fft_len,
fft: inner_fft,
len,
}
}
}
impl<T: DctNum> Dst1<T> for Dst1ConvertToFft<T> {
fn process_dst1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(self.inner_fft_len);
// the first half of the FFT input will be a 0, followed by the input array
for (input_val, fft_cell) in buffer.iter().zip(fft_buffer.iter_mut().skip(1)) {
*fft_cell = Complex::from(input_val);
}
// the second half of the FFT input will be a 0, followed by the input array, reversed and negated
for (input_val, fft_cell) in buffer.iter().zip(fft_buffer.iter_mut().rev()) {
*fft_cell = Complex::from(-*input_val);
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// apply a correction factor to the result
let half = T::half();
for (fft_entry, output_val) in fft_buffer.iter().rev().zip(buffer.iter_mut()) {
*output_val = fft_entry.im * half;
}
}
}
impl<T: DctNum> RequiredScratch for Dst1ConvertToFft<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
impl<T> Length for Dst1ConvertToFft<T> {
fn len(&self) -> usize {
self.len
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::{Dct1Naive, Dst1Naive};
use crate::test_utils::{compare_float_vectors, random_signal};
use rustfft::FftPlanner;
/// Verify that our fast implementation of the DCT1 gives the same output as the slow version, for many different inputs
#[test]
fn test_dct1_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Dct1Naive::new(size);
naive_dct.process_dct1(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let inner_fft = fft_planner.plan_fft_forward((size - 1) * 2);
println!("size: {}", size);
println!("inner fft len: {}", inner_fft.len());
let dct = Dct1ConvertToFft::new(inner_fft);
println!("dct len: {}", dct.len());
dct.process_dct1(&mut actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DST1 gives the same output as the slow version, for many different inputs
#[test]
fn test_dst1_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Dst1Naive::new(size);
naive_dct.process_dst1(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let inner_fft = fft_planner.plan_fft_forward((size + 1) * 2);
println!("size: {}", size);
println!("inner fft len: {}", inner_fft.len());
let dct = Dst1ConvertToFft::new(inner_fft);
println!("dst len: {}", dct.len());
dct.process_dst1(&mut actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,157 @@
use std::f64;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{Dct1, DctNum, Dst1};
/// Naive O(n^2 ) DCT Type 1 implementation
///
/// This implementation is primarily used to test other DCT1 algorithms. For small scratch sizes, this is actually
/// faster than `DCT1ViaFFT` because we don't have to pay the cost associated with converting the problem to a FFT.
///
/// ~~~
/// // Computes a naive DCT1 of size 23
/// use rustdct::Dct1;
/// use rustdct::algorithm::Dct1Naive;
///
/// let len = 23;
///
/// let dct = Dct1Naive::new(len);
///
/// let mut buffer = vec![0f32; len];
/// dct.process_dct1(&mut buffer);
/// ~~~
pub struct Dct1Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dct1Naive<T> {
pub fn new(len: usize) -> Self {
assert_ne!(len, 1, "DCT Type 1 is undefined for len == 1");
let constant_factor = f64::consts::PI / ((len - 1) as f64);
let twiddles: Vec<T> = (0..(len - 1) * 2)
.map(|i| (constant_factor * (i as f64)).cos())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct1<T> for Dct1Naive<T> {
fn process_dct1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
let half = T::half();
scratch[0] = scratch[0] * half;
scratch[self.len() - 1] = scratch[self.len() - 1] * half;
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = scratch[0];
let twiddle_stride = k;
let mut twiddle_index = twiddle_stride;
for i in 1..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> Length for Dct1Naive<T> {
fn len(&self) -> usize {
self.twiddles.len() / 2 + 1
}
}
impl<T> RequiredScratch for Dct1Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
/// Naive O(n^2 ) DST Type 1 implementation
///
/// This implementation is primarily used to test other DST1 algorithms.
///
/// ~~~
/// // Computes a naive DST1 of size 23
/// use rustdct::Dst1;
/// use rustdct::algorithm::Dst1Naive;
///
/// let len = 23;
///
/// let dst = Dst1Naive::new(len);
///
/// let mut buffer = vec![0f32; len];
/// dst.process_dst1(&mut buffer);
/// ~~~
pub struct Dst1Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dst1Naive<T> {
/// Creates a new DST1 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = f64::consts::PI / ((len + 1) as f64);
let twiddles: Vec<T> = (0..(len + 1) * 2)
.map(|i| (constant_factor * (i as f64)).sin())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dst1<T> for Dst1Naive<T> {
fn process_dst1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k + 1;
let mut twiddle_index = twiddle_stride;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> Length for Dst1Naive<T> {
fn len(&self) -> usize {
self.twiddles.len() / 2 - 1
}
}
impl<T> RequiredScratch for Dst1Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}

View File

@@ -0,0 +1,940 @@
use std::marker::PhantomData;
use rustfft::num_complex::Complex;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::{twiddles, DctNum, RequiredScratch};
use crate::{Dct2, Dct3, Dst2, Dst3, TransformType2And3};
macro_rules! butterfly_boilerplate {
($struct_name:ident, $size:expr) => {
impl<T: DctNum> Dct2<T> for $struct_name<T> {
fn process_dct2_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
unsafe {
self.process_inplace_dct2(buffer);
}
}
}
impl<T: DctNum> Dct3<T> for $struct_name<T> {
fn process_dct3_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
unsafe {
self.process_inplace_dct3(buffer);
}
}
}
impl<T: DctNum> Dst2<T> for $struct_name<T> {
fn process_dst2_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
unsafe {
self.process_inplace_dst2(buffer);
}
}
}
impl<T: DctNum> Dst3<T> for $struct_name<T> {
fn process_dst3_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
unsafe {
self.process_inplace_dst3(buffer);
}
}
}
impl<T: DctNum> TransformType2And3<T> for $struct_name<T> {}
impl<T> RequiredScratch for $struct_name<T> {
fn get_scratch_len(&self) -> usize {
0
}
}
impl<T> Length for $struct_name<T> {
fn len(&self) -> usize {
$size
}
}
};
}
pub struct Type2And3Butterfly2<T> {
_phantom: PhantomData<T>,
}
impl<T: DctNum> Type2And3Butterfly2<T> {
pub fn new() -> Self {
Type2And3Butterfly2 {
_phantom: PhantomData,
}
}
pub unsafe fn process_inplace_dct2(&self, buffer: &mut [T]) {
let sum = *buffer.get_unchecked(0) + *buffer.get_unchecked(1);
*buffer.get_unchecked_mut(1) =
(*buffer.get_unchecked(0) - *buffer.get_unchecked(1)) * T::FRAC_1_SQRT_2();
*buffer.get_unchecked_mut(0) = sum;
}
unsafe fn process_scattered_dct2(buffer: &mut [T], zero: usize, one: usize) {
let sum = *buffer.get_unchecked(zero) + *buffer.get_unchecked(one);
*buffer.get_unchecked_mut(one) =
(*buffer.get_unchecked(zero) - *buffer.get_unchecked(one)) * T::FRAC_1_SQRT_2();
*buffer.get_unchecked_mut(zero) = sum;
}
pub unsafe fn process_inplace_dct3(&self, buffer: &mut [T]) {
let half_0 = *buffer.get_unchecked(0) * T::half();
let frac_1 = *buffer.get_unchecked(1) * T::FRAC_1_SQRT_2();
*buffer.get_unchecked_mut(0) = half_0 + frac_1;
*buffer.get_unchecked_mut(1) = half_0 - frac_1;
}
unsafe fn process_scattered_dct3(buffer: &mut [T], zero: usize, one: usize) {
let half_0 = *buffer.get_unchecked(zero) * T::half();
let frac_1 = *buffer.get_unchecked(one) * T::FRAC_1_SQRT_2();
*buffer.get_unchecked_mut(zero) = half_0 + frac_1;
*buffer.get_unchecked_mut(one) = half_0 - frac_1;
}
pub unsafe fn process_inplace_dst2(&self, buffer: &mut [T]) {
let sum = *buffer.get_unchecked(0) - *buffer.get_unchecked(1);
*buffer.get_unchecked_mut(0) =
(*buffer.get_unchecked(0) + *buffer.get_unchecked(1)) * T::FRAC_1_SQRT_2();
*buffer.get_unchecked_mut(1) = sum;
}
pub unsafe fn process_inplace_dst3(&self, buffer: &mut [T]) {
let frac_0 = *buffer.get_unchecked(0) * T::FRAC_1_SQRT_2();
let half_1 = *buffer.get_unchecked(1) * T::half();
*buffer.get_unchecked_mut(0) = frac_0 + half_1;
*buffer.get_unchecked_mut(1) = frac_0 - half_1;
}
}
impl<T: DctNum> Dct2<T> for Type2And3Butterfly2<T> {
fn process_dct2_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
let sum = buffer[0] + buffer[1];
buffer[1] = (buffer[0] - buffer[1]) * T::FRAC_1_SQRT_2();
buffer[0] = sum;
}
}
impl<T: DctNum> Dct3<T> for Type2And3Butterfly2<T> {
fn process_dct3_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
let half_0 = buffer[0] * T::half();
let frac_1 = buffer[1] * T::FRAC_1_SQRT_2();
buffer[0] = half_0 + frac_1;
buffer[1] = half_0 - frac_1;
}
}
impl<T: DctNum> Dst2<T> for Type2And3Butterfly2<T> {
fn process_dst2_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
let sum = (buffer[0] + buffer[1]) * T::FRAC_1_SQRT_2();
buffer[1] = buffer[0] - buffer[1];
buffer[0] = sum;
}
}
impl<T: DctNum> Dst3<T> for Type2And3Butterfly2<T> {
fn process_dst3_with_scratch(&self, buffer: &mut [T], _scratch: &mut [T]) {
validate_buffer!(buffer, self.len());
let frac_0 = buffer[0] * T::FRAC_1_SQRT_2();
let half_1 = buffer[1] * T::half();
buffer[0] = frac_0 + half_1;
buffer[1] = frac_0 - half_1;
}
}
impl<T: DctNum> TransformType2And3<T> for Type2And3Butterfly2<T> {}
impl<T> Length for Type2And3Butterfly2<T> {
fn len(&self) -> usize {
2
}
}
impl<T> RequiredScratch for Type2And3Butterfly2<T> {
fn get_scratch_len(&self) -> usize {
0
}
}
pub struct Type2And3Butterfly3<T> {
twiddle: T,
}
impl<T: DctNum> Type2And3Butterfly3<T> {
pub fn new() -> Self {
Self {
twiddle: twiddles::single_twiddle_re(1, 12),
}
}
pub unsafe fn process_inplace_dct2(&self, buffer: &mut [T]) {
// This algorithm is derived by hardcoding the dct2 naive algorithm for size 3
let buffer_0 = *buffer.get_unchecked(0);
let buffer_1 = *buffer.get_unchecked(1);
let buffer_2 = *buffer.get_unchecked(2);
*buffer.get_unchecked_mut(0) = buffer_0 + buffer_1 + buffer_2;
*buffer.get_unchecked_mut(1) = (buffer_0 - buffer_2) * self.twiddle;
*buffer.get_unchecked_mut(2) = (buffer_0 + buffer_2) * T::half() - buffer_1;
}
pub unsafe fn process_inplace_dct3(&self, buffer: &mut [T]) {
// This algorithm is derived by hardcoding the dct3 naive algorithm for size 3
let buffer0_half = *buffer.get_unchecked(0) * T::half();
let buffer1 = *buffer.get_unchecked(1);
let buffer2 = *buffer.get_unchecked(2);
let buffer2_half = buffer2 * T::half();
*buffer.get_unchecked_mut(0) = buffer0_half + buffer1 * self.twiddle + buffer2_half;
*buffer.get_unchecked_mut(1) = buffer0_half - buffer2;
*buffer.get_unchecked_mut(2) = buffer0_half + buffer1 * -self.twiddle + buffer2_half;
}
pub unsafe fn process_inplace_dst2(&self, buffer: &mut [T]) {
// This algorithm is derived by hardcoding the dct2 naive algorithm for size 3, then negating the odd inputs and revering the outputs
let buffer_0 = *buffer.get_unchecked(0);
let buffer_1 = *buffer.get_unchecked(1);
let buffer_2 = *buffer.get_unchecked(2);
*buffer.get_unchecked_mut(2) = buffer_0 - buffer_1 + buffer_2;
*buffer.get_unchecked_mut(1) = (buffer_0 - buffer_2) * self.twiddle;
*buffer.get_unchecked_mut(0) = (buffer_0 + buffer_2) * T::half() + buffer_1;
}
pub unsafe fn process_inplace_dst3(&self, buffer: &mut [T]) {
// This algorithm is derived by hardcoding the dct3 naive algorithm for size 3, then reversing the inputs and negating the odd outputs
let buffer0_half = *buffer.get_unchecked(2) * T::half();
let buffer1 = *buffer.get_unchecked(1);
let buffer2 = *buffer.get_unchecked(0);
let buffer2_half = buffer2 * T::half();
*buffer.get_unchecked_mut(0) = buffer0_half + buffer1 * self.twiddle + buffer2_half;
*buffer.get_unchecked_mut(1) = buffer2 - buffer0_half;
*buffer.get_unchecked_mut(2) = buffer0_half + buffer1 * -self.twiddle + buffer2_half;
}
}
butterfly_boilerplate!(Type2And3Butterfly3, 3);
pub struct Type2And3Butterfly4<T> {
twiddle: Complex<T>,
}
impl<T: DctNum> Type2And3Butterfly4<T> {
pub fn new() -> Self {
Type2And3Butterfly4 {
twiddle: twiddles::single_twiddle(1, 16).conj(),
}
}
pub unsafe fn process_inplace_dct2(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT2SplitRadix with n = 4
let lower_dct4 = *buffer.get_unchecked(0) - *buffer.get_unchecked(3);
let upper_dct4 = *buffer.get_unchecked(2) - *buffer.get_unchecked(1);
*buffer.get_unchecked_mut(0) = *buffer.get_unchecked(0) + *buffer.get_unchecked(3);
*buffer.get_unchecked_mut(2) = *buffer.get_unchecked(2) + *buffer.get_unchecked(1);
Type2And3Butterfly2::process_scattered_dct2(buffer, 0, 2);
*buffer.get_unchecked_mut(1) = lower_dct4 * self.twiddle.re - upper_dct4 * self.twiddle.im;
*buffer.get_unchecked_mut(3) = upper_dct4 * self.twiddle.re + lower_dct4 * self.twiddle.im;
}
pub unsafe fn process_inplace_dct3(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT3SplitRadix with n = 4
// inner DCT3 of size 2
Type2And3Butterfly2::process_scattered_dct3(buffer, 0, 2);
// inner DCT3 of size 1, then sclared by twiddle factors
let lower_dct4 =
*buffer.get_unchecked(1) * self.twiddle.re + *buffer.get_unchecked(3) * self.twiddle.im;
let upper_dct4 =
*buffer.get_unchecked(1) * self.twiddle.im - *buffer.get_unchecked(3) * self.twiddle.re;
// Merge our results
*buffer.get_unchecked_mut(1) = *buffer.get_unchecked(2) + upper_dct4;
*buffer.get_unchecked_mut(3) = *buffer.get_unchecked(0) - lower_dct4;
*buffer.get_unchecked_mut(0) = *buffer.get_unchecked(0) + lower_dct4;
*buffer.get_unchecked_mut(2) = *buffer.get_unchecked(2) - upper_dct4;
}
pub unsafe fn process_inplace_dst2(&self, buffer: &mut [T]) {
// Derived from process_inplace_dct2 by negating the odd inputs, and reversing the outputs
let lower_dct4 = *buffer.get_unchecked(0) + *buffer.get_unchecked(3);
let upper_dct4 = *buffer.get_unchecked(2) + *buffer.get_unchecked(1);
*buffer.get_unchecked_mut(3) = *buffer.get_unchecked(0) - *buffer.get_unchecked(3);
*buffer.get_unchecked_mut(1) = *buffer.get_unchecked(2) - *buffer.get_unchecked(1);
Type2And3Butterfly2::process_scattered_dct2(buffer, 3, 1);
*buffer.get_unchecked_mut(2) = lower_dct4 * self.twiddle.re - upper_dct4 * self.twiddle.im;
*buffer.get_unchecked_mut(0) = upper_dct4 * self.twiddle.re + lower_dct4 * self.twiddle.im;
}
pub unsafe fn process_inplace_dst3(&self, buffer: &mut [T]) {
// Derived from process_inplace_dst3 by reversing the inputs, and negating the odd outputs
// inner DCT3 of size 2
Type2And3Butterfly2::process_scattered_dct3(buffer, 3, 1);
// inner DCT3 of size 1, then sclared by twiddle factors
let lower_dct4 =
*buffer.get_unchecked(2) * self.twiddle.re + *buffer.get_unchecked(0) * self.twiddle.im;
let upper_dct4 =
*buffer.get_unchecked(2) * self.twiddle.im - *buffer.get_unchecked(0) * self.twiddle.re;
// Merge our results
*buffer.get_unchecked_mut(0) = *buffer.get_unchecked(3) + lower_dct4;
*buffer.get_unchecked_mut(2) = *buffer.get_unchecked(1) - upper_dct4;
*buffer.get_unchecked_mut(1) = -(*buffer.get_unchecked(1) + upper_dct4);
*buffer.get_unchecked_mut(3) = lower_dct4 - *buffer.get_unchecked(3);
}
}
butterfly_boilerplate!(Type2And3Butterfly4, 4);
pub struct Type2And3Butterfly8<T> {
butterfly4: Type2And3Butterfly4<T>,
butterfly2: Type2And3Butterfly2<T>,
twiddles: [Complex<T>; 2],
}
impl<T: DctNum> Type2And3Butterfly8<T> {
pub fn new() -> Self {
Type2And3Butterfly8 {
butterfly4: Type2And3Butterfly4::new(),
butterfly2: Type2And3Butterfly2::new(),
twiddles: [
twiddles::single_twiddle(1, 32).conj(),
twiddles::single_twiddle(3, 32).conj(),
],
}
}
pub unsafe fn process_inplace_dct2(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT2SplitRadix with n = 8
//process the evens
let mut dct2_buffer = [
*buffer.get_unchecked(0) + *buffer.get_unchecked(7),
*buffer.get_unchecked(1) + *buffer.get_unchecked(6),
*buffer.get_unchecked(2) + *buffer.get_unchecked(5),
*buffer.get_unchecked(3) + *buffer.get_unchecked(4),
];
self.butterfly4.process_inplace_dct2(&mut dct2_buffer);
//process the odds
let differences = [
*buffer.get_unchecked(0) - *buffer.get_unchecked(7),
*buffer.get_unchecked(3) - *buffer.get_unchecked(4),
*buffer.get_unchecked(1) - *buffer.get_unchecked(6),
*buffer.get_unchecked(2) - *buffer.get_unchecked(5),
];
let mut dct4_even_buffer = [
differences[0] * self.twiddles[0].re + differences[1] * self.twiddles[0].im,
differences[2] * self.twiddles[1].re + differences[3] * self.twiddles[1].im,
];
let mut dct4_odd_buffer = [
differences[3] * self.twiddles[1].re - differences[2] * self.twiddles[1].im,
differences[1] * self.twiddles[0].re - differences[0] * self.twiddles[0].im,
];
self.butterfly2.process_inplace_dct2(&mut dct4_even_buffer);
self.butterfly2.process_inplace_dst2(&mut dct4_odd_buffer);
// combine the results
*buffer.get_unchecked_mut(0) = dct2_buffer[0];
*buffer.get_unchecked_mut(1) = dct4_even_buffer[0];
*buffer.get_unchecked_mut(2) = dct2_buffer[1];
*buffer.get_unchecked_mut(3) = dct4_even_buffer[1] - dct4_odd_buffer[0];
*buffer.get_unchecked_mut(4) = dct2_buffer[2];
*buffer.get_unchecked_mut(5) = dct4_even_buffer[1] + dct4_odd_buffer[0];
*buffer.get_unchecked_mut(6) = dct2_buffer[3];
*buffer.get_unchecked_mut(7) = dct4_odd_buffer[1];
}
pub unsafe fn process_inplace_dct3(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT3SplitRadix with n = 8
//process the evens
let mut dct3_buffer = [
*buffer.get_unchecked(0),
*buffer.get_unchecked(2),
*buffer.get_unchecked(4),
*buffer.get_unchecked(6),
];
self.butterfly4.process_inplace_dct3(&mut dct3_buffer);
//process the odds
let mut recursive_buffer_n1 = [
*buffer.get_unchecked(1) * T::two(),
*buffer.get_unchecked(3) + *buffer.get_unchecked(5),
];
let mut recursive_buffer_n3 = [
*buffer.get_unchecked(3) - *buffer.get_unchecked(5),
*buffer.get_unchecked(7) * T::two(),
];
self.butterfly2
.process_inplace_dct3(&mut recursive_buffer_n1);
self.butterfly2
.process_inplace_dst3(&mut recursive_buffer_n3);
// merge the temp buffers into the final output
for i in 0..2 {
let twiddle = self.twiddles[i];
let lower_dct4 =
recursive_buffer_n1[i] * twiddle.re + recursive_buffer_n3[i] * twiddle.im;
let upper_dct4 =
recursive_buffer_n1[i] * twiddle.im - recursive_buffer_n3[i] * twiddle.re;
let lower_dct3 = dct3_buffer[i];
let upper_dct3 = dct3_buffer[3 - i];
*buffer.get_unchecked_mut(i) = lower_dct3 + lower_dct4;
*buffer.get_unchecked_mut(7 - i) = lower_dct3 - lower_dct4;
*buffer.get_unchecked_mut(3 - i) = upper_dct3 + upper_dct4;
*buffer.get_unchecked_mut(4 + i) = upper_dct3 - upper_dct4;
}
}
pub unsafe fn process_inplace_dst2(&self, buffer: &mut [T]) {
// Derived from process_inplace_dct2, negating the odd inputs and reversing the outputs
//process the evens
let mut dct2_buffer = [
*buffer.get_unchecked(0) - *buffer.get_unchecked(7),
*buffer.get_unchecked(6) - *buffer.get_unchecked(1),
*buffer.get_unchecked(2) - *buffer.get_unchecked(5),
*buffer.get_unchecked(4) - *buffer.get_unchecked(3),
];
self.butterfly4.process_inplace_dct2(&mut dct2_buffer);
//process the odds
let differences = [
*buffer.get_unchecked(0) + *buffer.get_unchecked(7),
-*buffer.get_unchecked(3) - *buffer.get_unchecked(4),
-*buffer.get_unchecked(1) - *buffer.get_unchecked(6),
*buffer.get_unchecked(2) + *buffer.get_unchecked(5),
];
let mut dct4_even_buffer = [
differences[0] * self.twiddles[0].re + differences[1] * self.twiddles[0].im,
differences[2] * self.twiddles[1].re + differences[3] * self.twiddles[1].im,
];
let mut dct4_odd_buffer = [
differences[3] * self.twiddles[1].re - differences[2] * self.twiddles[1].im,
differences[1] * self.twiddles[0].re - differences[0] * self.twiddles[0].im,
];
self.butterfly2.process_inplace_dct2(&mut dct4_even_buffer);
self.butterfly2.process_inplace_dst2(&mut dct4_odd_buffer);
// combine the results
*buffer.get_unchecked_mut(7) = dct2_buffer[0];
*buffer.get_unchecked_mut(6) = dct4_even_buffer[0];
*buffer.get_unchecked_mut(5) = dct2_buffer[1];
*buffer.get_unchecked_mut(4) = dct4_even_buffer[1] - dct4_odd_buffer[0];
*buffer.get_unchecked_mut(3) = dct2_buffer[2];
*buffer.get_unchecked_mut(2) = dct4_even_buffer[1] + dct4_odd_buffer[0];
*buffer.get_unchecked_mut(1) = dct2_buffer[3];
*buffer.get_unchecked_mut(0) = dct4_odd_buffer[1];
}
pub unsafe fn process_inplace_dst3(&self, buffer: &mut [T]) {
// Derived from process_inplace_dct3, reversing the inputs and negating the odd outputs
//process the evens
let mut dct3_buffer = [
*buffer.get_unchecked(7),
*buffer.get_unchecked(5),
*buffer.get_unchecked(3),
*buffer.get_unchecked(1),
];
self.butterfly4.process_inplace_dct3(&mut dct3_buffer);
//process the odds
let mut recursive_buffer_n1 = [
*buffer.get_unchecked(6) * T::two(),
*buffer.get_unchecked(4) + *buffer.get_unchecked(2),
];
let mut recursive_buffer_n3 = [
*buffer.get_unchecked(4) - *buffer.get_unchecked(2),
*buffer.get_unchecked(0) * T::two(),
];
self.butterfly2
.process_inplace_dct3(&mut recursive_buffer_n1);
self.butterfly2
.process_inplace_dst3(&mut recursive_buffer_n3);
let merged_odds = [
recursive_buffer_n1[0] * self.twiddles[0].re
+ recursive_buffer_n3[0] * self.twiddles[0].im,
recursive_buffer_n1[0] * self.twiddles[0].im
- recursive_buffer_n3[0] * self.twiddles[0].re,
recursive_buffer_n1[1] * self.twiddles[1].re
+ recursive_buffer_n3[1] * self.twiddles[1].im,
recursive_buffer_n1[1] * self.twiddles[1].im
- recursive_buffer_n3[1] * self.twiddles[1].re,
];
// merge the temp buffers into the final output
*buffer.get_unchecked_mut(0) = dct3_buffer[0] + merged_odds[0];
*buffer.get_unchecked_mut(7) = merged_odds[0] - dct3_buffer[0];
*buffer.get_unchecked_mut(3) = -(dct3_buffer[3] + merged_odds[1]);
*buffer.get_unchecked_mut(4) = dct3_buffer[3] - merged_odds[1];
*buffer.get_unchecked_mut(1) = -(dct3_buffer[1] + merged_odds[2]);
*buffer.get_unchecked_mut(6) = dct3_buffer[1] - merged_odds[2];
*buffer.get_unchecked_mut(2) = dct3_buffer[2] + merged_odds[3];
*buffer.get_unchecked_mut(5) = merged_odds[3] - dct3_buffer[2];
}
}
butterfly_boilerplate!(Type2And3Butterfly8, 8);
pub struct Type2And3Butterfly16<T> {
butterfly8: Type2And3Butterfly8<T>,
butterfly4: Type2And3Butterfly4<T>,
twiddles: [Complex<T>; 4],
}
impl<T: DctNum> Type2And3Butterfly16<T> {
pub fn new() -> Self {
Type2And3Butterfly16 {
butterfly8: Type2And3Butterfly8::new(),
butterfly4: Type2And3Butterfly4::new(),
twiddles: [
twiddles::single_twiddle(1, 64).conj(),
twiddles::single_twiddle(3, 64).conj(),
twiddles::single_twiddle(5, 64).conj(),
twiddles::single_twiddle(7, 64).conj(),
],
}
}
pub unsafe fn process_inplace_dct2(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT2SplitRadix with n = 16
//process the evens
let mut dct2_buffer = [
*buffer.get_unchecked(0) + *buffer.get_unchecked(15),
*buffer.get_unchecked(1) + *buffer.get_unchecked(14),
*buffer.get_unchecked(2) + *buffer.get_unchecked(13),
*buffer.get_unchecked(3) + *buffer.get_unchecked(12),
*buffer.get_unchecked(4) + *buffer.get_unchecked(11),
*buffer.get_unchecked(5) + *buffer.get_unchecked(10),
*buffer.get_unchecked(6) + *buffer.get_unchecked(9),
*buffer.get_unchecked(7) + *buffer.get_unchecked(8),
];
self.butterfly8.process_inplace_dct2(&mut dct2_buffer);
//process the odds
let differences = [
*buffer.get_unchecked(0) - *buffer.get_unchecked(15),
*buffer.get_unchecked(7) - *buffer.get_unchecked(8),
*buffer.get_unchecked(1) - *buffer.get_unchecked(14),
*buffer.get_unchecked(6) - *buffer.get_unchecked(9),
*buffer.get_unchecked(2) - *buffer.get_unchecked(13),
*buffer.get_unchecked(5) - *buffer.get_unchecked(10),
*buffer.get_unchecked(3) - *buffer.get_unchecked(12),
*buffer.get_unchecked(4) - *buffer.get_unchecked(11),
];
let mut dct4_even_buffer = [
differences[0] * self.twiddles[0].re + differences[1] * self.twiddles[0].im,
differences[2] * self.twiddles[1].re + differences[3] * self.twiddles[1].im,
differences[4] * self.twiddles[2].re + differences[5] * self.twiddles[2].im,
differences[6] * self.twiddles[3].re + differences[7] * self.twiddles[3].im,
];
let mut dct4_odd_buffer = [
differences[7] * self.twiddles[3].re - differences[6] * self.twiddles[3].im,
differences[5] * self.twiddles[2].re - differences[4] * self.twiddles[2].im,
differences[3] * self.twiddles[1].re - differences[2] * self.twiddles[1].im,
differences[1] * self.twiddles[0].re - differences[0] * self.twiddles[0].im,
];
self.butterfly4.process_inplace_dct2(&mut dct4_even_buffer);
self.butterfly4.process_inplace_dst2(&mut dct4_odd_buffer);
// combine the results
*buffer.get_unchecked_mut(0) = dct2_buffer[0];
*buffer.get_unchecked_mut(1) = dct4_even_buffer[0];
*buffer.get_unchecked_mut(2) = dct2_buffer[1];
*buffer.get_unchecked_mut(3) = dct4_even_buffer[1] - dct4_odd_buffer[0];
*buffer.get_unchecked_mut(4) = dct2_buffer[2];
*buffer.get_unchecked_mut(5) = dct4_even_buffer[1] + dct4_odd_buffer[0];
*buffer.get_unchecked_mut(6) = dct2_buffer[3];
*buffer.get_unchecked_mut(7) = dct4_even_buffer[2] + dct4_odd_buffer[1];
*buffer.get_unchecked_mut(8) = dct2_buffer[4];
*buffer.get_unchecked_mut(9) = dct4_even_buffer[2] - dct4_odd_buffer[1];
*buffer.get_unchecked_mut(10) = dct2_buffer[5];
*buffer.get_unchecked_mut(11) = dct4_even_buffer[3] - dct4_odd_buffer[2];
*buffer.get_unchecked_mut(12) = dct2_buffer[6];
*buffer.get_unchecked_mut(13) = dct4_even_buffer[3] + dct4_odd_buffer[2];
*buffer.get_unchecked_mut(14) = dct2_buffer[7];
*buffer.get_unchecked_mut(15) = dct4_odd_buffer[3];
}
pub unsafe fn process_inplace_dst2(&self, buffer: &mut [T]) {
// Derived from process_inplace_dct2, negating the odd inputs and reversing the outputs
//process the evens
let mut dct2_buffer = [
*buffer.get_unchecked(0) - *buffer.get_unchecked(15),
-*buffer.get_unchecked(1) + *buffer.get_unchecked(14),
*buffer.get_unchecked(2) - *buffer.get_unchecked(13),
-*buffer.get_unchecked(3) + *buffer.get_unchecked(12),
*buffer.get_unchecked(4) - *buffer.get_unchecked(11),
-*buffer.get_unchecked(5) + *buffer.get_unchecked(10),
*buffer.get_unchecked(6) - *buffer.get_unchecked(9),
-*buffer.get_unchecked(7) + *buffer.get_unchecked(8),
];
self.butterfly8.process_inplace_dct2(&mut dct2_buffer);
//process the odds
let differences = [
*buffer.get_unchecked(0) + *buffer.get_unchecked(15),
-*buffer.get_unchecked(7) - *buffer.get_unchecked(8),
-*buffer.get_unchecked(1) - *buffer.get_unchecked(14),
*buffer.get_unchecked(6) + *buffer.get_unchecked(9),
*buffer.get_unchecked(2) + *buffer.get_unchecked(13),
-*buffer.get_unchecked(5) - *buffer.get_unchecked(10),
-*buffer.get_unchecked(3) - *buffer.get_unchecked(12),
*buffer.get_unchecked(4) + *buffer.get_unchecked(11),
];
let mut dct4_even_buffer = [
differences[0] * self.twiddles[0].re + differences[1] * self.twiddles[0].im,
differences[2] * self.twiddles[1].re + differences[3] * self.twiddles[1].im,
differences[4] * self.twiddles[2].re + differences[5] * self.twiddles[2].im,
differences[6] * self.twiddles[3].re + differences[7] * self.twiddles[3].im,
];
let mut dct4_odd_buffer = [
differences[7] * self.twiddles[3].re - differences[6] * self.twiddles[3].im,
differences[5] * self.twiddles[2].re - differences[4] * self.twiddles[2].im,
differences[3] * self.twiddles[1].re - differences[2] * self.twiddles[1].im,
differences[1] * self.twiddles[0].re - differences[0] * self.twiddles[0].im,
];
self.butterfly4.process_inplace_dct2(&mut dct4_even_buffer);
self.butterfly4.process_inplace_dst2(&mut dct4_odd_buffer);
// combine the results
*buffer.get_unchecked_mut(15) = dct2_buffer[0];
*buffer.get_unchecked_mut(14) = dct4_even_buffer[0];
*buffer.get_unchecked_mut(13) = dct2_buffer[1];
*buffer.get_unchecked_mut(12) = dct4_even_buffer[1] - dct4_odd_buffer[0];
*buffer.get_unchecked_mut(11) = dct2_buffer[2];
*buffer.get_unchecked_mut(10) = dct4_even_buffer[1] + dct4_odd_buffer[0];
*buffer.get_unchecked_mut(9) = dct2_buffer[3];
*buffer.get_unchecked_mut(8) = dct4_even_buffer[2] + dct4_odd_buffer[1];
*buffer.get_unchecked_mut(7) = dct2_buffer[4];
*buffer.get_unchecked_mut(6) = dct4_even_buffer[2] - dct4_odd_buffer[1];
*buffer.get_unchecked_mut(5) = dct2_buffer[5];
*buffer.get_unchecked_mut(4) = dct4_even_buffer[3] - dct4_odd_buffer[2];
*buffer.get_unchecked_mut(3) = dct2_buffer[6];
*buffer.get_unchecked_mut(2) = dct4_even_buffer[3] + dct4_odd_buffer[2];
*buffer.get_unchecked_mut(1) = dct2_buffer[7];
*buffer.get_unchecked_mut(0) = dct4_odd_buffer[3];
}
pub unsafe fn process_inplace_dct3(&self, buffer: &mut [T]) {
// perform a step of split radix -- derived from DCT3SplitRadix with n = 16
//process the evens
let mut dct3_buffer = [
*buffer.get_unchecked(0),
*buffer.get_unchecked(2),
*buffer.get_unchecked(4),
*buffer.get_unchecked(6),
*buffer.get_unchecked(8),
*buffer.get_unchecked(10),
*buffer.get_unchecked(12),
*buffer.get_unchecked(14),
];
self.butterfly8.process_inplace_dct3(&mut dct3_buffer);
//process the odds
let mut recursive_buffer_n1 = [
*buffer.get_unchecked(1) * T::two(),
*buffer.get_unchecked(3) + *buffer.get_unchecked(5),
*buffer.get_unchecked(7) + *buffer.get_unchecked(9),
*buffer.get_unchecked(11) + *buffer.get_unchecked(13),
];
let mut recursive_buffer_n3 = [
*buffer.get_unchecked(3) - *buffer.get_unchecked(5),
*buffer.get_unchecked(7) - *buffer.get_unchecked(9),
*buffer.get_unchecked(11) - *buffer.get_unchecked(13),
*buffer.get_unchecked(15) * T::two(),
];
self.butterfly4
.process_inplace_dct3(&mut recursive_buffer_n1);
self.butterfly4
.process_inplace_dst3(&mut recursive_buffer_n3);
// merge the temp buffers into the final output
for i in 0..4 {
let lower_dct4 = recursive_buffer_n1[i] * self.twiddles[i].re
+ recursive_buffer_n3[i] * self.twiddles[i].im;
let upper_dct4 = recursive_buffer_n1[i] * self.twiddles[i].im
- recursive_buffer_n3[i] * self.twiddles[i].re;
let lower_dct3 = dct3_buffer[i];
let upper_dct3 = dct3_buffer[7 - i];
*buffer.get_unchecked_mut(i) = lower_dct3 + lower_dct4;
*buffer.get_unchecked_mut(15 - i) = lower_dct3 - lower_dct4;
*buffer.get_unchecked_mut(7 - i) = upper_dct3 + upper_dct4;
*buffer.get_unchecked_mut(8 + i) = upper_dct3 - upper_dct4;
}
}
pub unsafe fn process_inplace_dst3(&self, buffer: &mut [T]) {
// Derived from process_inplace_dct3, reversing the inputs and negating the odd outputs
//process the evens
let mut dct3_buffer = [
*buffer.get_unchecked(15),
*buffer.get_unchecked(13),
*buffer.get_unchecked(11),
*buffer.get_unchecked(9),
*buffer.get_unchecked(7),
*buffer.get_unchecked(5),
*buffer.get_unchecked(3),
*buffer.get_unchecked(1),
];
self.butterfly8.process_inplace_dct3(&mut dct3_buffer);
//process the odds
let mut recursive_buffer_n1 = [
*buffer.get_unchecked(14) * T::two(),
*buffer.get_unchecked(12) + *buffer.get_unchecked(10),
*buffer.get_unchecked(8) + *buffer.get_unchecked(6),
*buffer.get_unchecked(4) + *buffer.get_unchecked(2),
];
let mut recursive_buffer_n3 = [
*buffer.get_unchecked(12) - *buffer.get_unchecked(10),
*buffer.get_unchecked(8) - *buffer.get_unchecked(6),
*buffer.get_unchecked(4) - *buffer.get_unchecked(2),
*buffer.get_unchecked(0) * T::two(),
];
self.butterfly4
.process_inplace_dct3(&mut recursive_buffer_n1);
self.butterfly4
.process_inplace_dst3(&mut recursive_buffer_n3);
let merged_odds = [
recursive_buffer_n1[0] * self.twiddles[0].re
+ recursive_buffer_n3[0] * self.twiddles[0].im,
recursive_buffer_n1[0] * self.twiddles[0].im
- recursive_buffer_n3[0] * self.twiddles[0].re,
recursive_buffer_n1[1] * self.twiddles[1].re
+ recursive_buffer_n3[1] * self.twiddles[1].im,
recursive_buffer_n1[1] * self.twiddles[1].im
- recursive_buffer_n3[1] * self.twiddles[1].re,
recursive_buffer_n1[2] * self.twiddles[2].re
+ recursive_buffer_n3[2] * self.twiddles[2].im,
recursive_buffer_n1[2] * self.twiddles[2].im
- recursive_buffer_n3[2] * self.twiddles[2].re,
recursive_buffer_n1[3] * self.twiddles[3].re
+ recursive_buffer_n3[3] * self.twiddles[3].im,
recursive_buffer_n1[3] * self.twiddles[3].im
- recursive_buffer_n3[3] * self.twiddles[3].re,
];
// merge the temp buffers into the final output
*buffer.get_unchecked_mut(0) = dct3_buffer[0] + merged_odds[0];
*buffer.get_unchecked_mut(15) = merged_odds[0] - dct3_buffer[0];
*buffer.get_unchecked_mut(7) = -(dct3_buffer[7] + merged_odds[1]);
*buffer.get_unchecked_mut(8) = dct3_buffer[7] - merged_odds[1];
*buffer.get_unchecked_mut(1) = -(dct3_buffer[1] + merged_odds[2]);
*buffer.get_unchecked_mut(14) = dct3_buffer[1] - merged_odds[2];
*buffer.get_unchecked_mut(6) = dct3_buffer[6] + merged_odds[3];
*buffer.get_unchecked_mut(9) = merged_odds[3] - dct3_buffer[6];
*buffer.get_unchecked_mut(2) = dct3_buffer[2] + merged_odds[4];
*buffer.get_unchecked_mut(13) = merged_odds[4] - dct3_buffer[2];
*buffer.get_unchecked_mut(5) = -(dct3_buffer[5] + merged_odds[5]);
*buffer.get_unchecked_mut(10) = dct3_buffer[5] - merged_odds[5];
*buffer.get_unchecked_mut(3) = -(dct3_buffer[3] + merged_odds[6]);
*buffer.get_unchecked_mut(12) = dct3_buffer[3] - merged_odds[6];
*buffer.get_unchecked_mut(4) = dct3_buffer[4] + merged_odds[7];
*buffer.get_unchecked_mut(11) = merged_odds[7] - dct3_buffer[4];
}
}
butterfly_boilerplate!(Type2And3Butterfly16, 16);
#[cfg(test)]
mod test {
use super::*;
//the tests for all butterflies will be identical except for the identifiers used and size
//so it's ideal for a macro
macro_rules! test_butterfly_func {
($test_name:ident, $struct_name:ident, $size:expr) => {
mod $test_name {
use super::*;
use crate::algorithm::Type2And3Naive;
use crate::test_utils::{compare_float_vectors, random_signal};
#[test]
fn $test_name() {
let size = $size;
println!("{}", size);
let naive = Type2And3Naive::new(size);
let butterfly = $struct_name::new();
check_dct2(&butterfly, &naive);
check_dct3(&butterfly, &naive);
check_dst2(&butterfly, &naive);
check_dst3(&butterfly, &naive);
}
fn check_dct2(
butterfly_instance: &$struct_name<f32>,
naive_instance: &dyn Dct2<f32>,
) {
// set up buffers
let mut expected_buffer = random_signal($size);
let mut inplace_buffer = expected_buffer.clone();
let mut actual_buffer = expected_buffer.clone();
// perform the test
naive_instance.process_dct2(&mut expected_buffer);
unsafe {
butterfly_instance.process_inplace_dct2(&mut inplace_buffer);
}
butterfly_instance.process_dct2(&mut actual_buffer);
println!("");
println!("expected output: {:?}", expected_buffer);
println!("inplace output: {:?}", inplace_buffer);
println!("process output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &inplace_buffer),
"process_inplace_dct2() failed, length = {}",
$size
);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"process_dct2() failed, length = {}",
$size
);
}
fn check_dct3(
butterfly_instance: &$struct_name<f32>,
naive_instance: &dyn Dct3<f32>,
) {
// set up buffers
let mut expected_buffer = random_signal($size);
let mut inplace_buffer = expected_buffer.clone();
let mut actual_buffer = expected_buffer.clone();
// perform the test
naive_instance.process_dct3(&mut expected_buffer);
unsafe {
butterfly_instance.process_inplace_dct3(&mut inplace_buffer);
}
butterfly_instance.process_dct3(&mut actual_buffer);
println!("");
println!("expected output: {:?}", expected_buffer);
println!("inplace output: {:?}", inplace_buffer);
println!("process output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &inplace_buffer),
"process_inplace_dct3() failed, length = {}",
$size
);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"process_dct3() failed, length = {}",
$size
);
}
fn check_dst2(
butterfly_instance: &$struct_name<f32>,
naive_instance: &dyn Dst2<f32>,
) {
// set up buffers
let mut expected_buffer = random_signal($size);
let mut inplace_buffer = expected_buffer.clone();
let mut actual_buffer = expected_buffer.clone();
// perform the test
naive_instance.process_dst2(&mut expected_buffer);
unsafe {
butterfly_instance.process_inplace_dst2(&mut inplace_buffer);
}
butterfly_instance.process_dst2(&mut actual_buffer);
println!("");
println!("expected output: {:?}", expected_buffer);
println!("inplace output: {:?}", inplace_buffer);
println!("process output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &inplace_buffer),
"process_inplace_dst2() failed, length = {}",
$size
);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"process_dst2() failed, length = {}",
$size
);
}
fn check_dst3(
butterfly_instance: &$struct_name<f32>,
naive_instance: &dyn Dst3<f32>,
) {
// set up buffers
let mut expected_buffer = random_signal($size);
let mut inplace_buffer = expected_buffer.clone();
let mut actual_buffer = expected_buffer.clone();
// perform the test
naive_instance.process_dst3(&mut expected_buffer);
unsafe {
butterfly_instance.process_inplace_dst3(&mut inplace_buffer);
}
butterfly_instance.process_dst3(&mut actual_buffer);
println!("");
println!("expected output: {:?}", expected_buffer);
println!("inplace output: {:?}", inplace_buffer);
println!("process output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &inplace_buffer),
"process_inplace_dst3() failed, length = {}",
$size
);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"process_dst3() failed, length = {}",
$size
);
}
}
};
}
test_butterfly_func!(test_butterfly2_type2and3, Type2And3Butterfly2, 2);
test_butterfly_func!(test_butterfly3_type2and3, Type2And3Butterfly3, 3);
test_butterfly_func!(test_butterfly4_type2and3, Type2And3Butterfly4, 4);
test_butterfly_func!(test_butterfly8_type2and3, Type2And3Butterfly8, 8);
test_butterfly_func!(test_butterfly16_type2and3, Type2And3Butterfly16, 16);
}

View File

@@ -0,0 +1,349 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::{Fft, FftDirection, Length};
use crate::common::dct_error_inplace;
use crate::{array_utils::into_complex_mut, twiddles, RequiredScratch};
use crate::{Dct2, Dct3, DctNum, Dst2, Dst3, TransformType2And3};
/// DCT2, DST2, DCT3, and DST3 implementation that converts the problem into a FFT of the same size
///
/// ~~~
/// // Computes a O(NlogN) DCT2, DST2, DCT3, and DST3 of size 1234 by converting them to FFTs
/// use rustdct::{Dct2, Dst2, Dct3, Dst3};
/// use rustdct::algorithm::Type2And3ConvertToFft;
/// use rustdct::rustfft::FftPlanner;
///
/// let len = 1234;
/// let mut planner = FftPlanner::new();
/// let fft = planner.plan_fft_forward(len);
///
/// let dct = Type2And3ConvertToFft::new(fft);
///
/// let mut dct2_buffer = vec![0f32; len];
/// dct.process_dct2(&mut dct2_buffer);
///
/// let mut dst2_buffer = vec![0f32; len];
/// dct.process_dst2(&mut dst2_buffer);
///
/// let mut dct3_buffer = vec![0f32; len];
/// dct.process_dct3(&mut dct3_buffer);
///
/// let mut dst3_buffer = vec![0f32; len];
/// dct.process_dst3(&mut dst3_buffer);
/// ~~~
pub struct Type2And3ConvertToFft<T> {
fft: Arc<dyn Fft<T>>,
twiddles: Box<[Complex<T>]>,
scratch_len: usize,
}
impl<T: DctNum> Type2And3ConvertToFft<T> {
/// Creates a new DCT2, DST2, DCT3, and DST3 context that will process signals of length `inner_fft.len()`.
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
assert_eq!(
inner_fft.fft_direction(),
FftDirection::Forward,
"The 'DCT type 2 via FFT' algorithm requires a forward FFT, but an inverse FFT was provided"
);
let len = inner_fft.len();
let twiddles: Vec<Complex<T>> = (0..len)
.map(|i| twiddles::single_twiddle(i, len * 4))
.collect();
let scratch_len = 2 * (len + inner_fft.get_inplace_scratch_len());
Self {
fft: inner_fft,
twiddles: twiddles.into_boxed_slice(),
scratch_len,
}
}
}
impl<T: DctNum> Dct2<T> for Type2And3ConvertToFft<T> {
fn process_dct2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
// the first half of the array will be the even elements, in order
let even_end = (buffer.len() + 1) / 2;
for i in 0..even_end {
fft_buffer[i] = Complex::from(buffer[i * 2]);
}
// the second half is the odd elements, in reverse order
if self.len() > 1 {
let odd_end = self.len() - 1 - self.len() % 2;
for i in 0..self.len() / 2 {
fft_buffer[even_end + i] = Complex::from(buffer[odd_end - 2 * i]);
}
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// apply a correction factor to the result
for ((fft_entry, correction_entry), spectrum_entry) in fft_buffer
.iter()
.zip(self.twiddles.iter())
.zip(buffer.iter_mut())
{
*spectrum_entry = (fft_entry * correction_entry).re;
}
}
}
impl<T: DctNum> Dst2<T> for Type2And3ConvertToFft<T> {
fn process_dst2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
// the first half of the array will be the even elements, in order
let even_end = (buffer.len() + 1) / 2;
for i in 0..even_end {
fft_buffer[i] = Complex::from(buffer[i * 2]);
}
// the second half is the odd elements, in reverse order and negated
if self.len() > 1 {
let odd_end = self.len() - 1 - self.len() % 2;
for i in 0..self.len() / 2 {
fft_buffer[even_end + i] = Complex::from(-buffer[odd_end - 2 * i]);
}
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// apply a correction factor to the result, and put it in reversed order in the output buffer
for ((fft_entry, correction_entry), spectrum_entry) in fft_buffer
.iter()
.zip(self.twiddles.iter())
.zip(buffer.iter_mut().rev())
{
*spectrum_entry = (fft_entry * correction_entry).re;
}
}
}
impl<T: DctNum> Dct3<T> for Type2And3ConvertToFft<T> {
fn process_dct3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
// compute the FFT buffer based on the correction factors
fft_buffer[0] = Complex::from(buffer[0] * T::half());
for (i, (fft_input_element, twiddle)) in fft_buffer
.iter_mut()
.zip(self.twiddles.iter())
.enumerate()
.skip(1)
{
let c = Complex {
re: buffer[i],
im: buffer[buffer.len() - i],
};
*fft_input_element = c * twiddle * T::half();
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// copy the first half of the fft output into the even elements of the buffer
let even_end = (buffer.len() + 1) / 2;
for i in 0..even_end {
buffer[i * 2] = fft_buffer[i].re;
}
// copy the second half of the fft buffer into the odd elements, reversed
if self.len() > 1 {
let odd_end = self.len() - 1 - self.len() % 2;
for i in 0..self.len() / 2 {
buffer[odd_end - 2 * i] = fft_buffer[i + even_end].re;
}
}
}
}
impl<T: DctNum> Dst3<T> for Type2And3ConvertToFft<T> {
fn process_dst3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
// compute the FFT buffer based on the correction factors
fft_buffer[0] = Complex::from(buffer[buffer.len() - 1] * T::half());
for (i, (fft_input_element, twiddle)) in fft_buffer
.iter_mut()
.zip(self.twiddles.iter())
.enumerate()
.skip(1)
{
let c = Complex {
re: buffer[buffer.len() - i - 1],
im: buffer[i - 1],
};
*fft_input_element = c * twiddle * T::half();
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// copy the first half of the fft output into the even elements of the output
let even_end = (self.len() + 1) / 2;
for i in 0..even_end {
buffer[i * 2] = fft_buffer[i].re;
}
// copy the second half of the fft output into the odd elements, reversed
if self.len() > 1 {
let odd_end = self.len() - 1 - self.len() % 2;
for i in 0..self.len() / 2 {
buffer[odd_end - 2 * i] = -fft_buffer[i + even_end].re;
}
}
}
}
impl<T: DctNum> TransformType2And3<T> for Type2And3ConvertToFft<T> {}
impl<T> Length for Type2And3ConvertToFft<T> {
fn len(&self) -> usize {
self.twiddles.len()
}
}
impl<T: DctNum> RequiredScratch for Type2And3ConvertToFft<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::Type2And3Naive;
use crate::test_utils::{compare_float_vectors, random_signal};
use rustfft::FftPlanner;
/// Verify that our fast implementation of the DCT2 gives the same output as the naive version, for many different inputs
#[test]
fn test_dct2_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Type2And3Naive::new(size);
naive_dct.process_dct2(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dct = Type2And3ConvertToFft::new(fft_planner.plan_fft_forward(size));
dct.process_dct2(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DST2 gives the same output as the naive version, for many different inputs
#[test]
fn test_dst2_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dst = Type2And3Naive::new(size);
naive_dst.process_dst2(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dst = Type2And3ConvertToFft::new(fft_planner.plan_fft_forward(size));
dst.process_dst2(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DCT3 gives the same output as the naive version, for many different inputs
#[test]
fn test_dct3_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Type2And3Naive::new(size);
naive_dct.process_dct3(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dct = Type2And3ConvertToFft::new(fft_planner.plan_fft_forward(size));
dct.process_dct3(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DST3 gives the same output as the naive version, for many different inputs
#[test]
fn test_dst3_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dst = Type2And3Naive::new(size);
naive_dst.process_dst3(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dst = Type2And3ConvertToFft::new(fft_planner.plan_fft_forward(size));
dst.process_dst3(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,163 @@
use rustfft::num_complex::Complex;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{twiddles, DctNum};
use crate::{Dct2, Dct3, Dst2, Dst3, TransformType2And3};
/// Naive O(n^2 ) DCT Type 2, DST Type 2, DCT Type 3, and DST Type 3 implementation
///
/// ~~~
/// // Computes a naive DCT2, DST2, DCT3, and DST3 of size 23
/// use rustdct::{Dct2, Dst2, Dct3, Dst3};
/// use rustdct::algorithm::Type2And3Naive;
///
/// let len = 23;
/// let naive = Type2And3Naive::new(len);
///
/// let mut dct2_buffer = vec![0f32; len];
/// naive.process_dct2(&mut dct2_buffer);
///
/// let mut dst2_buffer = vec![0f32; len];
/// naive.process_dst2(&mut dst2_buffer);
///
/// let mut dct3_buffer = vec![0f32; len];
/// naive.process_dct3(&mut dct3_buffer);
///
/// let mut dst3_buffer = vec![0f32; len];
/// naive.process_dst3(&mut dst3_buffer);
/// ~~~
pub struct Type2And3Naive<T> {
twiddles: Box<[Complex<T>]>,
}
impl<T: DctNum> Type2And3Naive<T> {
/// Creates a new DCT2, DCT3, DST2, and DST3 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let twiddles: Vec<Complex<T>> = (0..len * 4)
.map(|i| twiddles::single_twiddle(i, len * 4))
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct2<T> for Type2And3Naive<T> {
fn process_dct2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k * 2;
let mut twiddle_index = k;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle.re;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dst2<T> for Type2And3Naive<T> {
fn process_dst2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = (k + 1) * 2;
let mut twiddle_index = k + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell - scratch[i] * twiddle.im;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dct3<T> for Type2And3Naive<T> {
fn process_dct3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
let half_first = T::half() * scratch[0];
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = half_first;
let twiddle_stride = k * 2 + 1;
let mut twiddle_index = twiddle_stride;
for i in 1..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle.re;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dst3<T> for Type2And3Naive<T> {
fn process_dst3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
// scale the last scratch value by half before going into the loop
scratch[scratch.len() - 1] = scratch[scratch.len() - 1] * T::half();
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k * 2 + 1;
let mut twiddle_index = twiddle_stride;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell - scratch[i] * twiddle.im;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> TransformType2And3<T> for Type2And3Naive<T> {}
impl<T> Length for Type2And3Naive<T> {
fn len(&self) -> usize {
self.twiddles.len() / 4
}
}
impl<T> RequiredScratch for Type2And3Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}

View File

@@ -0,0 +1,313 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::{twiddles, DctNum, RequiredScratch};
use crate::{Dct2, Dct3, Dst2, Dst3, TransformType2And3};
/// DCT2, DCT3, DST2, and DST3 implemention that recursively divides the problem in half.
///
/// The problem size must be 2^n, n > 1
///
/// ~~~
/// // Computes a DCT Type 2 of size 1024
/// use rustdct::algorithm::Type2And3SplitRadix;
/// use rustdct::Dct2;
/// use rustdct::DctPlanner;
///
/// let len = 1024;
///
/// let mut planner = DctPlanner::new();
/// let quarter_dct = planner.plan_dct2(len / 4);
/// let half_dct = planner.plan_dct2(len / 2);
///
/// let dct = Type2And3SplitRadix::new(half_dct, quarter_dct);
///
/// let mut buffer = vec![0f32; len];
/// dct.process_dct2(&mut buffer);
/// ~~~
pub struct Type2And3SplitRadix<T> {
half_dct: Arc<dyn TransformType2And3<T>>,
quarter_dct: Arc<dyn TransformType2And3<T>>,
twiddles: Box<[Complex<T>]>,
}
impl<T: DctNum> Type2And3SplitRadix<T> {
/// Creates a new DCT2, DCT3, DST2, and DST3 context that will process signals of length `half_dct.len() * 2`
pub fn new(
half_dct: Arc<dyn TransformType2And3<T>>,
quarter_dct: Arc<dyn TransformType2And3<T>>,
) -> Self {
let half_len = half_dct.len();
let quarter_len = quarter_dct.len();
let len = half_len * 2;
assert!(
len.is_power_of_two() && len > 2,
"The DCT2SplitRadix algorithm requires a power-of-two input size greater than two. Got {}", len
);
assert_eq!(half_len, quarter_len * 2,
"half_dct.len() must be 2 * quarter_dct.len(). Got half_dct.len()={}, quarter_dct.len()={}", half_len, quarter_len
);
let twiddles: Vec<Complex<T>> = (0..(len / 4))
.map(|i| twiddles::single_twiddle(2 * i + 1, len * 4).conj())
.collect();
Self {
half_dct: half_dct,
quarter_dct: quarter_dct,
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct2<T> for Type2And3SplitRadix<T> {
fn process_dct2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let half_len = len / 2;
let quarter_len = len / 4;
//preprocess the data by splitting it up into vectors of size n/2, n/4, and n/4
let (input_dct2, input_dct4) = scratch.split_at_mut(half_len);
let (input_dct4_even, input_dct4_odd) = input_dct4.split_at_mut(quarter_len);
for i in 0..quarter_len {
let input_bottom = unsafe { *buffer.get_unchecked(i) };
let input_top = unsafe { *buffer.get_unchecked(len - i - 1) };
let input_half_bottom = unsafe { *buffer.get_unchecked(half_len - i - 1) };
let input_half_top = unsafe { *buffer.get_unchecked(half_len + i) };
//prepare the inner DCT2
unsafe {*input_dct2.get_unchecked_mut(i) = input_top + input_bottom };
unsafe {*input_dct2.get_unchecked_mut(half_len - i - 1) =
input_half_bottom + input_half_top };
//prepare the inner DCT4 - which consists of two DCT2s of half size
let lower_dct4 = input_bottom - input_top;
let upper_dct4 = input_half_bottom - input_half_top;
let twiddle = unsafe { self.twiddles.get_unchecked(i) };
let cos_input = lower_dct4 * twiddle.re + upper_dct4 * twiddle.im;
let sin_input = upper_dct4 * twiddle.re - lower_dct4 * twiddle.im;
unsafe {*input_dct4_even.get_unchecked_mut(i) = cos_input };
unsafe {*input_dct4_odd.get_unchecked_mut(quarter_len - i - 1) =
if i % 2 == 0 { sin_input } else { -sin_input } };
}
// compute the recursive DCT2s, using the original buffer as scratch space
self.half_dct.process_dct2_with_scratch(input_dct2, buffer);
self.quarter_dct
.process_dct2_with_scratch(input_dct4_even, buffer);
self.quarter_dct
.process_dct2_with_scratch(input_dct4_odd, buffer);
unsafe {
//post process the 3 DCT2 outputs. the first few and the last will be done outside of the loop
*buffer.get_unchecked_mut(0) = *input_dct2.get_unchecked(0);
*buffer.get_unchecked_mut(1) = *input_dct4_even.get_unchecked(0);
*buffer.get_unchecked_mut(2) = *input_dct2.get_unchecked(1);
for i in 1..quarter_len {
let dct4_cos_output = *input_dct4_even.get_unchecked(i);
let dct4_sin_output = if (i + quarter_len) % 2 == 0 {
-*input_dct4_odd.get_unchecked(quarter_len - i)
} else {
*input_dct4_odd.get_unchecked(quarter_len - i)
};
*buffer.get_unchecked_mut(i * 4 - 1) = dct4_cos_output + dct4_sin_output;
*buffer.get_unchecked_mut(i * 4) = *input_dct2.get_unchecked(i * 2);
*buffer.get_unchecked_mut(i * 4 + 1) = dct4_cos_output - dct4_sin_output;
*buffer.get_unchecked_mut(i * 4 + 2) = *input_dct2.get_unchecked(i * 2 + 1);
}
*buffer.get_unchecked_mut(len - 1) = -*input_dct4_odd.get_unchecked(0);
}
}
}
impl<T: DctNum> Dst2<T> for Type2And3SplitRadix<T> {
fn process_dst2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
for i in 0..(self.len() / 2) {
buffer[2 * i + 1] = buffer[2 * i + 1].neg();
}
self.process_dct2_with_scratch(buffer, scratch);
buffer.reverse();
}
}
impl<T: DctNum> Dct3<T> for Type2And3SplitRadix<T> {
fn process_dct3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = buffer.len();
let half_len = len / 2;
let quarter_len = len / 4;
// divide the output into 3 sub-lists to use for our inner DCTs, one of size N/2 and two of size N/4
let (recursive_input_evens, recursive_input_odds) = scratch.split_at_mut(half_len);
let (recursive_input_n1, recursive_input_n3) =
recursive_input_odds.split_at_mut(quarter_len);
// do the same pre-loop setup as DCT4ViaDCT3, and since we're skipping the first iteration of the loop we
// to also set up the corresponding evens cells
recursive_input_evens[0] = buffer[0];
recursive_input_evens[1] = buffer[2];
recursive_input_n1[0] = buffer[1] * T::two();
recursive_input_n3[0] = buffer[len - 1] * T::two();
// populate the recursive input arrays
for i in 1..quarter_len {
let k = 4 * i;
unsafe {
// the evens are the easy ones - just copy straight over
*recursive_input_evens.get_unchecked_mut(i * 2) = *buffer.get_unchecked(k);
*recursive_input_evens.get_unchecked_mut(i * 2 + 1) = *buffer.get_unchecked(k + 2);
// for the odd ones we're going to do the same addition/subtraction we do in the setup for DCT4ViaDCT3
*recursive_input_n1.get_unchecked_mut(i) =
*buffer.get_unchecked(k - 1) + *buffer.get_unchecked(k + 1);
*recursive_input_n3.get_unchecked_mut(quarter_len - i) =
*buffer.get_unchecked(k - 1) - *buffer.get_unchecked(k + 1);
}
}
//perform our recursive DCTs, using the original buffer as scratch space
self.half_dct
.process_dct3_with_scratch(recursive_input_evens, buffer);
self.quarter_dct
.process_dct3_with_scratch(recursive_input_n1, buffer);
self.quarter_dct
.process_dct3_with_scratch(recursive_input_n3, buffer);
//merge the results. we're going to combine 2 separate things:
// - merging the two smaller DCT3 outputs into a DCT4 output
// - marging the DCT4 outputand the larger DCT3 output into the final output
for i in 0..quarter_len {
let twiddle = self.twiddles[i];
let cosine_value = recursive_input_n1[i];
// flip the sign of every other sine value to finish the job of using a DCT3 to compute a DST3
let sine_value = if i % 2 == 0 {
recursive_input_n3[i]
} else {
-recursive_input_n3[i]
};
let lower_dct4 = cosine_value * twiddle.re + sine_value * twiddle.im;
let upper_dct4 = cosine_value * twiddle.im - sine_value * twiddle.re;
unsafe {
let lower_dct3 = *recursive_input_evens.get_unchecked(i);
let upper_dct3 = *recursive_input_evens.get_unchecked(half_len - i - 1);
*buffer.get_unchecked_mut(i) = lower_dct3 + lower_dct4;
*buffer.get_unchecked_mut(len - i - 1) = lower_dct3 - lower_dct4;
*buffer.get_unchecked_mut(half_len - i - 1) = upper_dct3 + upper_dct4;
*buffer.get_unchecked_mut(half_len + i) = upper_dct3 - upper_dct4;
}
}
}
}
impl<T: DctNum> Dst3<T> for Type2And3SplitRadix<T> {
fn process_dst3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
buffer.reverse();
self.process_dct3_with_scratch(buffer, scratch);
for i in 0..(self.len() / 2) {
buffer[2 * i + 1] = buffer[2 * i + 1].neg();
}
}
}
impl<T: DctNum> TransformType2And3<T> for Type2And3SplitRadix<T> {}
impl<T> Length for Type2And3SplitRadix<T> {
fn len(&self) -> usize {
self.twiddles.len() * 4
}
}
impl<T> RequiredScratch for Type2And3SplitRadix<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::Type2And3Naive;
use crate::test_utils::{compare_float_vectors, random_signal};
/// Verify that our fast implementation of the DCT2 gives the same output as the slow version, for many different inputs
#[test]
fn test_dct2_splitradix() {
for i in 2..8 {
let size = 1 << i;
println!("len: {}", size);
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Type2And3Naive::new(size);
naive_dct.process_dct2(&mut expected_buffer);
let quarter_dct = Arc::new(Type2And3Naive::new(size / 4));
let half_dct = Arc::new(Type2And3Naive::new(size / 2));
let dct = Type2And3SplitRadix::new(half_dct, quarter_dct);
dct.process_dct2(&mut actual_buffer);
println!("expected: {:?}", expected_buffer);
println!("fast output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DCT3 gives the same output as the slow version, for many different inputs
#[test]
fn test_dct3_splitradix() {
for i in 2..8 {
let size = 1 << i;
println!("len: {}", size);
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct = Type2And3Naive::new(size);
naive_dct.process_dct3(&mut expected_buffer);
let quarter_dct = Arc::new(Type2And3Naive::new(size / 4));
let half_dct = Arc::new(Type2And3Naive::new(size / 2));
let dct = Type2And3SplitRadix::new(half_dct, quarter_dct);
dct.process_dct3(&mut actual_buffer);
println!("expected: {:?}", expected_buffer);
println!("fast output: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,348 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::FftDirection;
use rustfft::{Fft, Length};
use crate::common::dct_error_inplace;
use crate::{array_utils::into_complex_mut, DctNum, RequiredScratch};
use crate::{Dct4, Dst4, TransformType4};
/// DCT Type 4 and DST Type 4 implementation that converts the problem into a FFT of the same size.
///
/// This algorithm can only be used if the problem size is odd.
///
/// ~~~
/// // Computes a DCT Type 4 and DST Type 4 of size 1233
/// use rustdct::{Dct4, Dst4};
/// use rustdct::algorithm::Type4ConvertToFftOdd;
/// use rustdct::rustfft::FftPlanner;
///
/// let len = 1233;
///
/// let mut planner = FftPlanner::new();
/// let fft = planner.plan_fft_forward(len);
/// let dct = Type4ConvertToFftOdd::new(fft);
///
/// let mut dct4_buffer = vec![0f32; len];
/// dct.process_dct4(&mut dct4_buffer);
///
/// let mut dst4_buffer = vec![0f32; len];
/// dct.process_dst4(&mut dst4_buffer);
/// ~~~
pub struct Type4ConvertToFftOdd<T> {
fft: Arc<dyn Fft<T>>,
len: usize,
scratch_len: usize,
}
impl<T: DctNum> Type4ConvertToFftOdd<T> {
/// Creates a new DCT4 context that will process signals of length `inner_fft.len()`. `inner_fft.len()` must be odd.
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
assert_eq!(
inner_fft.fft_direction(),
FftDirection::Forward,
"Type4ConvertToFFTOdd requires a forward FFT, but an inverse FFT was provided"
);
let len = inner_fft.len();
assert!(
len % 2 == 1,
"Type4ConvertToFFTOdd size must be odd. Got {}",
len
);
Self {
scratch_len: 2 * (len + inner_fft.get_inplace_scratch_len()),
fft: inner_fft,
len,
}
}
}
impl<T: DctNum> Dct4<T> for Type4ConvertToFftOdd<T> {
fn process_dct4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let half_len = len / 2;
let quarter_len = len / 4;
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
//start by reordering the input into the FFT input
let mut input_index = half_len;
let mut fft_index = 0;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[input_index],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
//subtract len to simulate modular arithmetic
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: -buffer[len - input_index - 1],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: -buffer[input_index],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[len - input_index - 1],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while fft_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[input_index],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
let result_scale = T::SQRT_2() * T::half();
let second_half_sign = if len % 4 == 1 { T::one() } else { -T::one() };
//post-process the results 4 at a time
let mut output_sign = T::one();
for i in 0..quarter_len {
let fft_result = fft_buffer[4 * i + 1] * (output_sign * result_scale);
let next_result = fft_buffer[4 * i + 3] * (output_sign * result_scale);
buffer[i * 2] = fft_result.re + fft_result.im;
buffer[i * 2 + 1] = -next_result.re + next_result.im;
buffer[len - i * 2 - 2] = (next_result.re + next_result.im) * second_half_sign;
buffer[len - i * 2 - 1] = (fft_result.re - fft_result.im) * second_half_sign;
output_sign = output_sign.neg();
}
//we either have 1 or 3 elements left over that we couldn't get in the above loop, handle them here
if len % 4 == 1 {
buffer[half_len] = fft_buffer[0].re * output_sign * result_scale;
} else {
let fft_result = fft_buffer[len - 2] * (output_sign * result_scale);
buffer[half_len - 1] = fft_result.re + fft_result.im;
buffer[half_len + 1] = -fft_result.re + fft_result.im;
buffer[half_len] = -fft_buffer[0].re * output_sign * result_scale;
}
}
}
impl<T: DctNum> Dst4<T> for Type4ConvertToFftOdd<T> {
fn process_dst4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let len = self.len();
let half_len = len / 2;
let quarter_len = len / 4;
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(len);
//start by reordering the input into the FFT input
let mut input_index = half_len;
let mut fft_index = 0;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[len - input_index - 1],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
//subtract len to simulate modular arithmetic
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: -buffer[input_index],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: -buffer[len - input_index - 1],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while input_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[input_index],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
input_index = input_index - len;
while fft_index < len {
fft_buffer[fft_index] = Complex {
re: buffer[len - input_index - 1],
im: T::zero(),
};
input_index += 4;
fft_index += 1;
}
// run the fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
let result_scale = T::SQRT_2() * T::half();
let second_half_sign = if len % 4 == 1 { T::one() } else { -T::one() };
//post-process the results 4 at a time
let mut output_sign = T::one();
for i in 0..quarter_len {
let fft_result = fft_buffer[4 * i + 1] * (output_sign * result_scale);
let next_result = fft_buffer[4 * i + 3] * (output_sign * result_scale);
buffer[i * 2] = fft_result.re + fft_result.im;
buffer[i * 2 + 1] = next_result.re - next_result.im;
buffer[len - i * 2 - 2] = -(next_result.re + next_result.im) * second_half_sign;
buffer[len - i * 2 - 1] = (fft_result.re - fft_result.im) * second_half_sign;
output_sign = output_sign.neg();
}
//we either have 1 or 3 elements left over that we couldn't get in the above loop, handle them here
if len % 4 == 1 {
buffer[half_len] = fft_buffer[0].re * output_sign * result_scale;
} else {
let fft_result = fft_buffer[len - 2] * (output_sign * result_scale);
buffer[half_len - 1] = fft_result.re + fft_result.im;
buffer[half_len + 1] = -fft_result.re + fft_result.im;
buffer[half_len] = fft_buffer[0].re * output_sign * result_scale;
}
}
}
impl<T: DctNum> RequiredScratch for Type4ConvertToFftOdd<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
impl<T: DctNum> TransformType4<T> for Type4ConvertToFftOdd<T> {}
impl<T> Length for Type4ConvertToFftOdd<T> {
fn len(&self) -> usize {
self.len
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::Type4Naive;
use crate::test_utils::{compare_float_vectors, random_signal};
use rustfft::FftPlanner;
/// Verify that our fast implementation of the DCT4 gives the same output as the slow version, for many different inputs
#[test]
fn test_dct4_via_fft_odd() {
for n in 0..50 {
let size = 2 * n + 1;
println!("{}", size);
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
println!("input: {:?}", actual_buffer);
let naive_dct = Type4Naive::new(size);
naive_dct.process_dct4(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dct = Type4ConvertToFftOdd::new(fft_planner.plan_fft_forward(size));
dct.process_dct4(&mut actual_buffer);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DST4 gives the same output as the slow version, for many different inputs
#[test]
fn test_dst4_via_fft_odd() {
for n in 0..50 {
let size = 2 * n + 1;
println!("{}", size);
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
println!("input: {:?}", actual_buffer);
let naive_dst = Type4Naive::new(size);
naive_dst.process_dst4(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dst = Type4ConvertToFftOdd::new(fft_planner.plan_fft_forward(size));
dst.process_dst4(&mut actual_buffer);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,214 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::{twiddles, Dct4, DctNum, Dst4, RequiredScratch, TransformType2And3, TransformType4};
/// DCT4 and DST4 implementation that converts the problem into two DCT3 of half size.
///
/// If the inner DCT3 is O(nlogn), then so is this. This algorithm can only be used if the problem size is even.
///
/// ~~~
/// // Computes a DCT Type 4 of size 1234
/// use std::sync::Arc;
/// use rustdct::Dct4;
/// use rustdct::algorithm::Type4ConvertToType3Even;
/// use rustdct::DctPlanner;
///
/// let len = 1234;
/// let mut planner = DctPlanner::new();
/// let inner_dct3 = planner.plan_dct3(len / 2);
///
/// let dct = Type4ConvertToType3Even::new(inner_dct3);
///
/// let mut buffer = vec![0f32; len];
/// dct.process_dct4(&mut buffer);
/// ~~~
pub struct Type4ConvertToType3Even<T> {
inner_dct: Arc<dyn TransformType2And3<T>>,
twiddles: Box<[Complex<T>]>,
scratch_len: usize,
}
impl<T: DctNum> Type4ConvertToType3Even<T> {
/// Creates a new DCT4 context that will process signals of length `inner_dct.len() * 2`.
pub fn new(inner_dct: Arc<dyn TransformType2And3<T>>) -> Self {
let inner_len = inner_dct.len();
let len = inner_len * 2;
let twiddles: Vec<Complex<T>> = (0..inner_len)
.map(|i| twiddles::single_twiddle(2 * i + 1, len * 8).conj())
.collect();
let inner_scratch = inner_dct.get_scratch_len();
let scratch_len = if inner_scratch <= len {
len
} else {
len + inner_scratch
};
Self {
inner_dct: inner_dct,
twiddles: twiddles.into_boxed_slice(),
scratch_len,
}
}
}
impl<T: DctNum> Dct4<T> for Type4ConvertToType3Even<T> {
fn process_dct4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let (self_scratch, extra_scratch) = scratch.split_at_mut(self.len());
let len = self.len();
let inner_len = len / 2;
//pre-process the input by splitting into into two arrays, one for the inner DCT3, and the other for the DST3
let (mut output_left, mut output_right) = self_scratch.split_at_mut(inner_len);
output_left[0] = buffer[0] * T::two();
for k in 1..inner_len {
output_left[k] = buffer[2 * k - 1] + buffer[2 * k];
output_right[k - 1] = buffer[2 * k - 1] - buffer[2 * k];
}
output_right[inner_len - 1] = buffer[len - 1] * T::two();
//run the two inner DCTs on our separated arrays
let inner_scratch = if extra_scratch.len() > 0 {
extra_scratch
} else {
&mut buffer[..]
};
self.inner_dct
.process_dct3_with_scratch(&mut output_left, inner_scratch);
self.inner_dct
.process_dst3_with_scratch(&mut output_right, inner_scratch);
//post-process the data by combining it back into a single array
for k in 0..inner_len {
let twiddle = self.twiddles[k];
let cos_value = output_left[k];
let sin_value = output_right[k];
buffer[k] = cos_value * twiddle.re + sin_value * twiddle.im;
buffer[len - 1 - k] = cos_value * twiddle.im - sin_value * twiddle.re;
}
}
}
impl<T: DctNum> Dst4<T> for Type4ConvertToType3Even<T> {
fn process_dst4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let (self_scratch, extra_scratch) = scratch.split_at_mut(self.len());
let len = self.len();
let inner_len = len / 2;
//pre-process the input by splitting into into two arrays, one for the inner DCT3, and the other for the DST3
let (mut output_left, mut output_right) = self_scratch.split_at_mut(inner_len);
output_right[0] = buffer[0] * T::two();
for k in 1..inner_len {
output_left[k - 1] = buffer[2 * k - 1] + buffer[2 * k];
output_right[k] = buffer[2 * k] - buffer[2 * k - 1];
}
output_left[inner_len - 1] = buffer[len - 1] * T::two();
//run the two inner DCTs on our separated arrays
let inner_scratch = if extra_scratch.len() > 0 {
extra_scratch
} else {
&mut buffer[..]
};
self.inner_dct
.process_dst3_with_scratch(&mut output_left, inner_scratch);
self.inner_dct
.process_dct3_with_scratch(&mut output_right, inner_scratch);
//post-process the data by combining it back into a single array
for k in 0..inner_len {
let twiddle = self.twiddles[k];
let cos_value = output_left[k];
let sin_value = output_right[k];
buffer[k] = cos_value * twiddle.re + sin_value * twiddle.im;
buffer[len - 1 - k] = sin_value * twiddle.re - cos_value * twiddle.im;
}
}
}
impl<T> RequiredScratch for Type4ConvertToType3Even<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
impl<T: DctNum> TransformType4<T> for Type4ConvertToType3Even<T> {}
impl<T> Length for Type4ConvertToType3Even<T> {
fn len(&self) -> usize {
self.twiddles.len() * 2
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::{Type2And3Naive, Type4Naive};
use crate::test_utils::{compare_float_vectors, random_signal};
#[test]
fn unittest_dct4_via_type3() {
for inner_size in 1..20 {
let size = inner_size * 2;
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dct4 = Type4Naive::new(size);
naive_dct4.process_dct4(&mut expected_buffer);
let inner_dct3 = Arc::new(Type2And3Naive::new(inner_size));
let dct = Type4ConvertToType3Even::new(inner_dct3);
dct.process_dct4(&mut actual_buffer);
println!("");
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"len = {}",
size
);
}
}
#[test]
fn unittest_dst4_via_type3() {
for inner_size in 1..20 {
let size = inner_size * 2;
let mut expected_buffer = random_signal(size);
let mut actual_buffer = expected_buffer.clone();
let naive_dst4 = Type4Naive::new(size);
naive_dst4.process_dst4(&mut expected_buffer);
let inner_dst3 = Arc::new(Type2And3Naive::new(inner_size));
let dst = Type4ConvertToType3Even::new(inner_dst3);
dst.process_dst4(&mut actual_buffer);
println!("");
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&expected_buffer, &actual_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,102 @@
use rustfft::num_complex::Complex;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{twiddles, DctNum};
use crate::{Dct4, Dst4, TransformType4};
/// Naive O(n^2 ) DCT Type 4 and DST Type 4 implementation
///
/// ~~~
/// // Computes a naive DCT4 of size 23
/// use rustdct::{Dct4, Dst4};
/// use rustdct::algorithm::Type4Naive;
///
/// let len = 23;
/// let naive = Type4Naive::new(len);
///
/// let mut dct4_buffer: Vec<f32> = vec![0f32; len];
/// naive.process_dct4(&mut dct4_buffer);
///
/// let mut dst4_buffer: Vec<f32> = vec![0f32; len];
/// naive.process_dst4(&mut dst4_buffer);
/// ~~~
pub struct Type4Naive<T> {
twiddles: Box<[Complex<T>]>,
}
impl<T: DctNum> Type4Naive<T> {
/// Creates a new DCT4 and DTS4 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let twiddles: Vec<Complex<T>> = (0..len * 4)
.map(|i| twiddles::single_twiddle_halfoffset(i, len * 4))
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct4<T> for Type4Naive<T> {
fn process_dct4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let mut twiddle_index = k;
let twiddle_stride = k * 2 + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle.re;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dst4<T> for Type4Naive<T> {
fn process_dst4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let mut twiddle_index = k;
let twiddle_stride = k * 2 + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell - scratch[i] * twiddle.im;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Type4Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T: DctNum> TransformType4<T> for Type4Naive<T> {}
impl<T> Length for Type4Naive<T> {
fn len(&self) -> usize {
self.twiddles.len() / 4
}
}

View File

@@ -0,0 +1,151 @@
use std::f64;
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{Dct5, DctNum, Dst5};
/// Naive O(n^2 ) DCT Type 5 implementation
///
/// This implementation is primarily used to test other DCT5 algorithms.
///
/// ~~~
/// // Computes a naive DCT5 of size 23
/// use rustdct::Dct5;
/// use rustdct::algorithm::Dct5Naive;
///
/// let len = 23;
/// let mut buffer = vec![0f32; len];
///
/// let dct = Dct5Naive::new(len);
/// dct.process_dct5(&mut buffer);
/// ~~~
pub struct Dct5Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dct5Naive<T> {
pub fn new(len: usize) -> Self {
let constant_factor = f64::consts::PI / (len as f64 - 0.5);
let twiddles: Vec<T> = (0..len * 2 - 1)
.map(|i| (constant_factor * (i as f64)).cos())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct5<T> for Dct5Naive<T> {
fn process_dct5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
scratch[0] = scratch[0] * T::half();
buffer[0] = scratch.iter().fold(T::zero(), |acc, e| acc + *e);
for k in 1..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = scratch[0];
let twiddle_stride = k;
let mut twiddle_index = twiddle_stride;
for i in 1..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dct5Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T> Length for Dct5Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() + 1) / 2
}
}
/// Naive O(n^2 ) DST Type 5 implementation
///
/// This implementation is primarily used to test other DST5 algorithms.
///
/// ~~~
/// // Computes a naive DST5 of size 23
/// use rustdct::Dst5;
/// use rustdct::algorithm::Dst5Naive;
///
/// let len = 23;
/// let mut buffer = vec![0f32; len];
///
/// let dst = Dst5Naive::new(len);
/// dst.process_dst5(&mut buffer);
/// ~~~
pub struct Dst5Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dst5Naive<T> {
/// Creates a new DST5 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = f64::consts::PI / (len as f64 + 0.5);
let twiddles: Vec<T> = (0..len * 2 + 1)
.map(|i| (constant_factor * (i as f64)).sin())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dst5<T> for Dst5Naive<T> {
fn process_dst5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k + 1;
let mut twiddle_index = twiddle_stride;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dst5Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T> Length for Dst5Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() - 1) / 2
}
}

View File

@@ -0,0 +1,207 @@
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::{Fft, FftDirection, Length};
use crate::common::dct_error_inplace;
use crate::{array_utils::into_complex_mut, DctNum, RequiredScratch};
use crate::{Dst6, Dst6And7, Dst7};
/// DST6 and DST7 implementation that converts the problem into a FFT of the same size
///
/// ~~~
/// // Computes a O(NlogN) DST6 and DST7 of size 1234 by converting them to FFTs
/// use rustdct::{Dst6, Dst7};
/// use rustdct::algorithm::Dst6And7ConvertToFft;
/// use rustdct::rustfft::FftPlanner;
///
/// let len = 1234;
/// let mut planner = FftPlanner::new();
/// let fft = planner.plan_fft_forward(len * 2 + 1);
///
/// let dct = Dst6And7ConvertToFft::new(fft);
///
/// let mut dst6_buffer = vec![0f32; len];
/// dct.process_dst6(&mut dst6_buffer);
///
/// let mut dst7_buffer = vec![0f32; len];
/// dct.process_dst7(&mut dst6_buffer);
/// ~~~
pub struct Dst6And7ConvertToFft<T> {
fft: Arc<dyn Fft<T>>,
len: usize,
scratch_len: usize,
inner_fft_len: usize,
}
impl<T: DctNum> Dst6And7ConvertToFft<T> {
/// Creates a new DST6 and DST7 context that will process signals of length `(inner_fft.len() - 1) / 2`.
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
let inner_fft_len = inner_fft.len();
assert!(
inner_fft_len % 2 == 1,
"The 'DST6And7ConvertToFFT' algorithm requires an odd-len FFT. Provided len={}",
inner_fft_len
);
assert_eq!(
inner_fft.fft_direction(),
FftDirection::Forward, "The 'DST6And7ConvertToFFT' algorithm requires a forward FFT, but an inverse FFT was provided");
let len = (inner_fft_len - 1) / 2;
Self {
scratch_len: 2 * (inner_fft_len + inner_fft.get_inplace_scratch_len()),
inner_fft_len,
fft: inner_fft,
len,
}
}
}
impl<T: DctNum> Dst6<T> for Dst6And7ConvertToFft<T> {
fn process_dst6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(self.inner_fft_len);
// Copy the buffer to the odd imaginary components of the FFT inputs
for i in 0..buffer.len() {
fft_buffer[i * 2 + 1].im = buffer[i];
}
// inner fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// Copy the first half of the array to the odd-indexd elements
let even_count = (buffer.len() + 1) / 2;
let odd_count = buffer.len() - even_count;
for i in 0..odd_count {
let output_index = i * 2 + 1;
buffer[output_index] = fft_buffer[i + 1].re;
}
// Copy the second half of the array to the reversed even-indexed elements
for i in 0..even_count {
let output_index = 2 * (even_count - i - 1);
buffer[output_index] = fft_buffer[i + odd_count + 1].re;
}
}
}
impl<T: DctNum> Dst7<T> for Dst6And7ConvertToFft<T> {
fn process_dst7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
let complex_scratch = into_complex_mut(scratch);
let (fft_buffer, fft_scratch) = complex_scratch.split_at_mut(self.inner_fft_len);
// Copy all the even-indexed elements to the back of the FFT buffer array
let even_count = (buffer.len() + 1) / 2;
for i in 0..even_count {
let input_index = i * 2;
let inner_index = buffer.len() + 1 + i;
fft_buffer[inner_index] = Complex {
re: buffer[input_index],
im: T::zero(),
};
}
// Copy all the odd-indexed elements in reverse order
let odd_count = buffer.len() - even_count;
for i in 0..odd_count {
let input_index = 2 * (odd_count - i) - 1;
let inner_index = buffer.len() + even_count + 1 + i;
fft_buffer[inner_index] = Complex {
re: buffer[input_index],
im: T::zero(),
};
}
// Copy the back of the array to the front, negated and reversed
for i in 0..buffer.len() {
fft_buffer[i + 1] = -fft_buffer[fft_buffer.len() - 1 - i];
}
// inner fft
self.fft.process_with_scratch(fft_buffer, fft_scratch);
// copy buffer back
for i in 0..buffer.len() {
buffer[i] = fft_buffer[i * 2 + 1].im * T::half();
}
}
}
impl<T: DctNum> Dst6And7<T> for Dst6And7ConvertToFft<T> {}
impl<T: DctNum> RequiredScratch for Dst6And7ConvertToFft<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
impl<T> Length for Dst6And7ConvertToFft<T> {
fn len(&self) -> usize {
self.len
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::algorithm::Dst6And7Naive;
use crate::test_utils::{compare_float_vectors, random_signal};
use rustfft::FftPlanner;
/// Verify that our fast implementation of the DCT6 gives the same buffer as the naive version, for many different inputs
#[test]
fn test_dst6_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = random_signal(size);
let naive_dst = Dst6And7Naive::new(size);
naive_dst.process_dst6(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dst = Dst6And7ConvertToFft::new(fft_planner.plan_fft_forward(size * 2 + 1));
assert_eq!(dst.len(), size);
dst.process_dst6(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
/// Verify that our fast implementation of the DST7 gives the same buffer as the naive version, for many different inputs
#[test]
fn test_dst7_via_fft() {
for size in 2..20 {
let mut expected_buffer = random_signal(size);
let mut actual_buffer = random_signal(size);
let naive_dst = Dst6And7Naive::new(size);
naive_dst.process_dst7(&mut expected_buffer);
let mut fft_planner = FftPlanner::new();
let dst = Dst6And7ConvertToFft::new(fft_planner.plan_fft_forward(size * 2 + 1));
assert_eq!(dst.len(), size);
dst.process_dst7(&mut actual_buffer);
println!("{}", size);
println!("expected: {:?}", expected_buffer);
println!("actual: {:?}", actual_buffer);
assert!(
compare_float_vectors(&actual_buffer, &expected_buffer),
"len = {}",
size
);
}
}
}

View File

@@ -0,0 +1,206 @@
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{Dct6, Dct6And7, Dct7, DctNum, Dst6, Dst6And7, Dst7};
/// Naive O(n^2 ) DCT Type 6 and DCT Type 7 implementation
///
/// ~~~
/// // Computes a naive DCT6 and DCT7 of size 23
/// use rustdct::{Dct6, Dct7};
/// use rustdct::algorithm::Dct6And7Naive;
///
/// let len = 23;
/// let naive = Dct6And7Naive::new(len);
///
/// let mut dct6_buffer = vec![0f32; len];
/// naive.process_dct6(&mut dct6_buffer);
///
/// let mut dct7_buffer = vec![0f32; len];
/// naive.process_dct7(&mut dct7_buffer);
/// ~~~
pub struct Dct6And7Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dct6And7Naive<T> {
/// Creates a new DCT6 and DCT7 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = std::f64::consts::PI / (len * 2 - 1) as f64;
let twiddles: Vec<T> = (0..len * 4 - 2)
.map(|i| (constant_factor * (i as f64)).cos())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct6<T> for Dct6And7Naive<T> {
fn process_dct6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
scratch[scratch.len() - 1] = scratch[scratch.len() - 1] * T::half();
buffer[0] = scratch.iter().fold(T::zero(), |acc, e| acc + *e);
for k in 1..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k * 2;
let mut twiddle_index = k;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dct7<T> for Dct6And7Naive<T> {
fn process_dct7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
scratch[0] = scratch[0] * T::half();
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = scratch[0];
let twiddle_stride = k * 2 + 1;
let mut twiddle_index = twiddle_stride;
for i in 1..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dct6And7Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T: DctNum> Dct6And7<T> for Dct6And7Naive<T> {}
impl<T> Length for Dct6And7Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() + 2) / 4
}
}
/// Naive O(n^2 ) DST Type 6 and DST Type 7 implementation
///
/// ~~~
/// // Computes a naive DST6 and DST7 of size 23
/// use rustdct::{Dst6, Dst7};
/// use rustdct::algorithm::Dst6And7Naive;
///
/// let len = 23;
/// let naive = Dst6And7Naive::new(len);
///
/// let mut dst6_buffer = vec![0f32; len];
/// naive.process_dst6(&mut dst6_buffer);
///
/// let mut dst7_buffer = vec![0f32; len];
/// naive.process_dst7(&mut dst7_buffer);
/// ~~~
pub struct Dst6And7Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dst6And7Naive<T> {
/// Creates a new DST6 and DST7 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = std::f64::consts::PI / (len * 2 + 1) as f64;
let twiddles: Vec<T> = (0..len * 4 + 2)
.map(|i| (constant_factor * (i as f64)).sin())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dst6<T> for Dst6And7Naive<T> {
fn process_dst6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = (k + 1) * 2;
let mut twiddle_index = k + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T: DctNum> Dst7<T> for Dst6And7Naive<T> {
fn process_dst7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let twiddle_stride = k * 2 + 1;
let mut twiddle_index = twiddle_stride;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dst6And7Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T: DctNum> Dst6And7<T> for Dst6And7Naive<T> {}
impl<T> Length for Dst6And7Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() - 2) / 4
}
}

View File

@@ -0,0 +1,143 @@
use rustfft::Length;
use crate::common::dct_error_inplace;
use crate::RequiredScratch;
use crate::{Dct8, DctNum, Dst8};
/// Naive O(n^2 ) DCT Type 8 implementation
///
/// ~~~
/// // Computes a naive DCT8 of size 23
/// use rustdct::Dct8;
/// use rustdct::algorithm::Dct8Naive;
///
/// let len = 23;
/// let naive = Dct8Naive::new(len);
///
/// let mut buffer = vec![0f32; len];
/// naive.process_dct8(&mut buffer);
/// ~~~
pub struct Dct8Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dct8Naive<T> {
/// Creates a new DCT8 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = std::f64::consts::PI / (len * 2 + 1) as f64;
let twiddles: Vec<T> = (0..len * 4 + 2)
.map(|i| (constant_factor * (i as f64 + 0.5)).cos())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dct8<T> for Dct8Naive<T> {
fn process_dct8_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let mut twiddle_index = k;
let twiddle_stride = k * 2 + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dct8Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T> Length for Dct8Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() - 2) / 4
}
}
/// Naive O(n^2 ) DST Type 8 implementation
///
/// ~~~
/// // Computes a naive DST8 of size 23
/// use rustdct::Dst8;
/// use rustdct::algorithm::Dst8Naive;
///
/// let len = 23;
/// let naive = Dst8Naive::new(len);
///
/// let mut buffer = vec![0f32; len];
/// naive.process_dst8(&mut buffer);
/// ~~~
pub struct Dst8Naive<T> {
twiddles: Box<[T]>,
}
impl<T: DctNum> Dst8Naive<T> {
/// Creates a new DST8 context that will process signals of length `len`
pub fn new(len: usize) -> Self {
let constant_factor = std::f64::consts::PI / (len * 2 - 1) as f64;
let twiddles: Vec<T> = (0..len * 4 - 2)
.map(|i| (constant_factor * (i as f64 + 0.5)).sin())
.map(|c| T::from_f64(c).unwrap())
.collect();
Self {
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: DctNum> Dst8<T> for Dst8Naive<T> {
fn process_dst8_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]) {
let scratch = validate_buffers!(buffer, scratch, self.len(), self.get_scratch_len());
scratch.copy_from_slice(buffer);
scratch[scratch.len() - 1] = scratch[scratch.len() - 1] * T::half();
for k in 0..buffer.len() {
let output_cell = buffer.get_mut(k).unwrap();
*output_cell = T::zero();
let mut twiddle_index = k;
let twiddle_stride = k * 2 + 1;
for i in 0..scratch.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + scratch[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
}
impl<T> RequiredScratch for Dst8Naive<T> {
fn get_scratch_len(&self) -> usize {
self.len()
}
}
impl<T> Length for Dst8Naive<T> {
fn len(&self) -> usize {
(self.twiddles.len() + 2) / 4
}
}

View File

@@ -0,0 +1,14 @@
use rustfft::num_complex::Complex;
#[allow(unused)]
pub fn into_complex<T>(buffer: &[T]) -> &[Complex<T>] {
let complex_len = buffer.len() / 2;
let ptr = buffer.as_ptr() as *const Complex<T>;
unsafe { std::slice::from_raw_parts(ptr, complex_len) }
}
pub fn into_complex_mut<T>(buffer: &mut [T]) -> &mut [Complex<T>] {
let complex_len = buffer.len() / 2;
let ptr = buffer.as_mut_ptr() as *mut Complex<T>;
unsafe { std::slice::from_raw_parts_mut(ptr, complex_len) }
}

View File

@@ -0,0 +1,148 @@
use rustfft::num_traits::FloatConst;
use rustfft::FftNum;
/// Generic floating point number
pub trait DctNum: FftNum + FloatConst {
fn half() -> Self;
fn two() -> Self;
}
impl<T: FftNum + FloatConst> DctNum for T {
fn half() -> Self {
Self::from_f64(0.5).unwrap()
}
fn two() -> Self {
Self::from_f64(2.0).unwrap()
}
}
// Validates the given buffer verifying that it has the correct length.
macro_rules! validate_buffer {
($buffer: expr,$expected_buffer_len: expr) => {{
if $buffer.len() != $expected_buffer_len {
dct_error_inplace($buffer.len(), 0, $expected_buffer_len, 0);
return;
}
}};
}
// Validates the given buffer and scratch by verifying that they have the correct length. Then, slices the scratch down to just the required amount
macro_rules! validate_buffers {
($buffer: expr, $scratch: expr, $expected_buffer_len: expr, $expected_scratch_len: expr) => {{
if $buffer.len() != $expected_buffer_len {
dct_error_inplace(
$buffer.len(),
$scratch.len(),
$expected_buffer_len,
$expected_scratch_len,
);
return;
}
if let Some(sliced_scratch) = $scratch.get_mut(0..$expected_scratch_len) {
sliced_scratch
} else {
dct_error_inplace(
$buffer.len(),
$scratch.len(),
$expected_buffer_len,
$expected_scratch_len,
);
return;
}
}};
}
// Validates the given buffer and scratch by verifying that they have the correct length. Then, slices the scratch down to just the required amount
macro_rules! validate_buffers_mdct {
($buffer_a: expr, $buffer_b: expr, $buffer_c: expr, $scratch: expr, $expected_buffer_len: expr, $expected_scratch_len: expr) => {{
if $buffer_a.len() != $expected_buffer_len
|| $buffer_b.len() != $expected_buffer_len
|| $buffer_c.len() != $expected_buffer_len
{
mdct_error_inplace(
$buffer_a.len(),
$buffer_b.len(),
$buffer_c.len(),
$scratch.len(),
$expected_buffer_len,
$expected_scratch_len,
);
return;
}
if let Some(sliced_scratch) = $scratch.get_mut(0..$expected_scratch_len) {
sliced_scratch
} else {
mdct_error_inplace(
$buffer_a.len(),
$buffer_b.len(),
$buffer_c.len(),
$scratch.len(),
$expected_buffer_len,
$expected_scratch_len,
);
return;
}
}};
}
// Prints an error raised by an in-place FFT algorithm's `process_inplace` method
// Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
#[cold]
#[inline(never)]
pub fn dct_error_inplace(
actual_len: usize,
actual_scratch: usize,
expected_len: usize,
expected_scratch: usize,
) {
assert!(
actual_len == expected_len,
"Provided buffer must be equal to the transform size. Expected len = {}, got len = {}",
expected_len,
actual_len
);
assert!(
actual_scratch >= expected_scratch,
"Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
expected_scratch,
actual_scratch
);
}
// Prints an error raised by an in-place FFT algorithm's `process_inplace` method
// Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
#[cold]
#[inline(never)]
pub fn mdct_error_inplace(
actual_len_a: usize,
actual_len_b: usize,
actual_len_c: usize,
actual_scratch: usize,
expected_len: usize,
expected_scratch: usize,
) {
assert!(
actual_len_a == expected_len,
"All three MDCT buffers must be equal to the transform size. Expected len = {}, but first buffer was len = {}",
expected_len,
actual_len_a
);
assert!(
actual_len_b == expected_len,
"All three MDCT buffers must be equal to the transform size. Expected len = {}, but second buffer was len = {}",
expected_len,
actual_len_b
);
assert!(
actual_len_c == expected_len,
"All three MDCT buffers must be equal to the transform size. Expected len = {}, but third buffer was len = {}",
expected_len,
actual_len_c
);
assert!(
actual_scratch >= expected_scratch,
"Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
expected_scratch,
actual_scratch
);
}

View File

@@ -0,0 +1,376 @@
pub use rustfft;
pub use rustfft::num_complex;
pub use rustfft::num_traits;
use rustfft::Length;
#[macro_use]
mod common;
/// Algorithms for computing the Modified Discrete Cosine Transform
pub mod mdct;
pub mod algorithm;
mod array_utils;
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;
}
/// A 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, consider calling `process_dct1_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct2_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct3_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst4_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct5_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct6_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct7_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dct8_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dct8_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst1_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst1_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst2_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst2_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst3_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst3_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst4_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst4_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst4_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst5_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst6_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst6_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst7_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
fn process_dst7_with_scratch(&self, buffer: &mut [T], scratch: &mut [T]);
}
/// A 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, consider calling `process_dst8_with_scratch` instead.
///
/// Does not normalize outputs.
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.
///
/// Does not normalize outputs.
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>>();
}

View File

@@ -0,0 +1,492 @@
use std::f64;
use rustfft::Length;
use crate::common::mdct_error_inplace;
use crate::RequiredScratch;
use crate::{mdct::Mdct, DctNum};
/// Naive O(n^2 ) MDCT implementation
///
/// This implementation is primarily used to test other MDCT algorithms.
///
/// ~~~
/// // Computes a naive MDCT of output size 124, using the MP3 window function
/// use rustdct::mdct::{Mdct, MdctNaive, window_fn};
/// use rustdct::RequiredScratch;
///
/// let len = 124;
///
/// let dct = MdctNaive::new(len, window_fn::mp3);
///
/// let input = vec![0f32; len * 2];
/// let (input_a, input_b) = input.split_at(len);
/// let mut output = vec![0f32; len];
/// let mut scratch = vec![0f32; dct.get_scratch_len()];
///
/// dct.process_mdct_with_scratch(input_a, input_b, &mut output, &mut scratch);
/// ~~~
pub struct MdctNaive<T> {
twiddles: Box<[T]>,
window: Box<[T]>,
}
impl<T: DctNum> MdctNaive<T> {
/// Creates a new MDCT context that will process inputs of length `output_len * 2` and produce
/// outputs of length `output_len`
///
/// `output_len` must be even.
///
/// `window_fn` is a function that takes a `size` and returns a `Vec` containing `size` window values.
/// See the [`window_fn`](mdct/window_fn/index.html) module for provided window functions.
pub fn new<F>(output_len: usize, window_fn: F) -> Self
where
F: FnOnce(usize) -> Vec<T>,
{
assert!(
output_len % 2 == 0,
"The MDCT len must be even. Got {}",
output_len
);
let constant_factor = 0.5f64 * f64::consts::PI / (output_len as f64);
let twiddles: Vec<T> = (0..output_len * 4)
.map(|i| (constant_factor * (i as f64 + 0.5_f64)).cos())
.map(|c| T::from_f64(c).unwrap())
.collect();
let window = window_fn(output_len * 2);
assert_eq!(
window.len(),
output_len * 2,
"Window function returned incorrect number of values"
);
Self {
twiddles: twiddles.into_boxed_slice(),
window: window.into_boxed_slice(),
}
}
}
impl<T: DctNum> Mdct<T> for MdctNaive<T> {
fn process_mdct_with_scratch(
&self,
input_a: &[T],
input_b: &[T],
output: &mut [T],
scratch: &mut [T],
) {
validate_buffers_mdct!(
input_a,
input_b,
output,
scratch,
self.len(),
self.get_scratch_len()
);
let output_len = output.len();
let half_output = output.len() / 2;
for k in 0..output_len {
let output_cell = output.get_mut(k).unwrap();
*output_cell = T::zero();
let mut twiddle_index = (half_output + k * (output_len + 1)) % self.twiddles.len();
let twiddle_stride = k * 2 + 1;
for i in 0..input_a.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + input_a[i] * self.window[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
for i in 0..input_b.len() {
let twiddle = self.twiddles[twiddle_index];
*output_cell = *output_cell + input_b[i] * self.window[i + output_len] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
}
}
fn process_imdct_with_scratch(
&self,
input: &[T],
output_a: &mut [T],
output_b: &mut [T],
scratch: &mut [T],
) {
validate_buffers_mdct!(
input,
output_a,
output_b,
scratch,
self.len(),
self.get_scratch_len()
);
let input_len = input.len();
let half_input = input_len / 2;
for k in 0..input_len {
let mut output_val = T::zero();
let mut twiddle_index = half_input + k;
let twiddle_stride = input_len + k * 2 + 1;
for i in 0..input.len() {
let twiddle = self.twiddles[twiddle_index];
output_val = output_val + input[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
output_a[k] = output_a[k] + output_val * self.window[k];
}
for k in 0..input_len {
let mut output_val = T::zero();
let twiddle_k = if k < input_len / 2 {
k
} else {
input_len - k - 1
};
let mut twiddle_index =
self.twiddles.len() - input_len * 2 + half_input - twiddle_k - 1;
let twiddle_stride = input_len - 1 - twiddle_k * 2;
for i in 0..input.len() {
let twiddle = self.twiddles[twiddle_index];
output_val = output_val + input[i] * twiddle;
twiddle_index += twiddle_stride;
if twiddle_index >= self.twiddles.len() {
twiddle_index -= self.twiddles.len();
}
}
output_b[k] = output_b[k] + output_val * self.window[k + input_len];
}
}
}
impl<T> Length for MdctNaive<T> {
fn len(&self) -> usize {
self.twiddles.len() / 4
}
}
impl<T> RequiredScratch for MdctNaive<T> {
fn get_scratch_len(&self) -> usize {
0
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::mdct::window_fn;
use std::f32;
use crate::test_utils::{compare_float_vectors, random_signal};
/// Verify our naive implementation against some known values
#[test]
fn test_known_values_mdct() {
let input_list = vec![
vec![0_f32, 0_f32, 0_f32, 0_f32],
vec![1_f32, 1_f32, -5_f32, 5_f32],
vec![7_f32, 3_f32, 8_f32, 4_f32, -1_f32, 3_f32, 0_f32, 4_f32],
vec![
7_f32, 3_f32, 8_f32, 4_f32, -1_f32, 3_f32, 0_f32, 4_f32, 1f32, 1f32, 1f32, 1f32,
],
];
let expected_list = vec![
vec![0_f32, 0_f32],
vec![0_f32, 0_f32],
vec![-4.7455063, -2.073643, -2.2964284, 8.479767],
vec![
-2.90775651,
-12.30026278,
6.92661442,
2.79403335,
3.56420194,
-2.40007133,
],
];
for (input, expected) in input_list.iter().zip(expected_list.iter()) {
let output = slow_mdct(&input, window_fn::one);
assert!(compare_float_vectors(&expected, &output));
}
}
/// Verify our naive windowed implementation against some known values
#[test]
fn test_known_values_windowed_mdct() {
let input_list = vec![
vec![0_f32, 0_f32, 0_f32, 0_f32],
vec![1_f32, 1_f32, -5_f32, 5_f32],
vec![7_f32, 3_f32, 8_f32, 4_f32, -1_f32, 3_f32, 0_f32, 4_f32],
vec![
7_f32, 3_f32, 8_f32, 4_f32, -1_f32, 3_f32, 0_f32, 4_f32, 1f32, 1f32, 1f32, 1f32,
],
];
let expected_list = vec![
vec![0_f32, 0_f32],
vec![2.29289322, 1.53553391],
vec![-4.67324308, 3.1647844, -6.22625186, 2.1647844],
vec![
-5.50153067,
-3.46580575,
3.79375195,
-1.25072987,
4.6738204,
3.16506351,
],
];
for (input, expected) in input_list.iter().zip(expected_list.iter()) {
let output = slow_mdct(&input, window_fn::mp3);
assert!(compare_float_vectors(&expected, &output));
}
}
/// Verify that our fast implementation of the MDCT gives the same output as the slow version, for many different inputs
#[test]
fn test_matches_mdct() {
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] {
for i in 1..10 {
let input_len = i * 4;
let output_len = i * 2;
let input = random_signal(input_len);
let (input_a, input_b) = input.split_at(output_len);
let slow_output = slow_mdct(&input, current_window_fn);
let mut fast_output = vec![0f32; output_len];
let dct = MdctNaive::new(output_len, current_window_fn);
dct.process_mdct_with_scratch(&input_a, &input_b, &mut fast_output, &mut []);
println!("{}", output_len);
println!("expected: {:?}", slow_output);
println!("actual: {:?}", fast_output);
assert!(
compare_float_vectors(&slow_output, &fast_output),
"i = {}",
i
);
}
}
}
fn slow_mdct<F>(input: &[f32], window_fn: F) -> Vec<f32>
where
F: Fn(usize) -> Vec<f32>,
{
let mut output = vec![0f32; input.len() / 2];
let size_float = output.len() as f32;
let window = window_fn(input.len());
let windowed_input: Vec<f32> = input.iter().zip(window).map(|(i, w)| i * w).collect();
for k in 0..output.len() {
let mut current_value = 0_f32;
let k_float = k as f32;
for n in 0..input.len() {
let n_float = n as f32;
let twiddle = (f32::consts::PI
* (n_float + 0.5_f32 + size_float * 0.5)
* (k_float + 0.5_f32)
/ size_float)
.cos();
current_value += windowed_input[n] * twiddle;
}
output[k] = current_value;
}
output
}
/// Verify our naive implementation against some known values
#[test]
fn test_known_values_imdct() {
let input_list = vec![
vec![0f32, 0f32],
vec![1f32, 5f32],
vec![7f32, 3f32, 8f32, 4f32],
vec![7f32, 3f32, 8f32, 4f32, -1f32, 3f32],
];
let expected_list = vec![
vec![0f32, 0f32, 0f32, 0f32],
vec![-4.2367144, 4.2367153, -2.837299, -2.8372989],
vec![
5.833236, 2.4275358, -2.4275393, -5.833232, 4.8335495, -14.584825, -14.584811,
4.8335423,
],
vec![
2.4138875,
8.921771,
-2.4359043,
2.4359055,
-8.921769,
-2.4138737,
3.1458342,
-0.63405657,
-18.502512,
-18.502502,
-0.6340414,
3.1458292,
],
];
for (input, expected) in input_list.iter().zip(expected_list.iter()) {
let slow_output = slow_imdct(&input, window_fn::one);
let mut fast_output = vec![0f32; input.len() * 2];
let (fast_output_a, fast_output_b) = fast_output.split_at_mut(input.len());
let dct = MdctNaive::new(input.len(), window_fn::one);
dct.process_imdct_with_scratch(&input, fast_output_a, fast_output_b, &mut []);
assert!(compare_float_vectors(&expected, &slow_output));
assert!(compare_float_vectors(&expected, &fast_output));
}
}
/// Verify our naive windowed implementation against some known values
#[test]
fn test_known_values_windowed_imdct() {
let input_list = vec![
vec![0f32, 0f32],
vec![1_f32, 5_f32],
vec![7_f32, 3_f32, 8_f32, 4_f32],
vec![7_f32, 3_f32, 8_f32, 4_f32, -1_f32, 3_f32],
];
let expected_list = vec![
vec![0f32, 0f32, 0f32, 0f32],
vec![
-1.6213203435596431,
3.9142135623730936,
-2.6213203435596433,
-1.0857864376269069,
],
vec![
1.1380080486867217,
1.3486674811260955,
-2.0184235241728627,
-5.7211528055198331,
4.7406716077536428,
-12.126842074178105,
-8.1028968193867765,
0.94297821246780911,
],
vec![
0.3150751815802082,
3.4142135623730949,
-1.4828837895525038,
1.9325317795197492,
-8.2426406871192732,
-2.3932336063055089,
3.1189227588735786,
-0.58578643762689731,
-14.679036212259122,
-11.263620643186901,
-0.24264068711929426,
0.41061397098787894,
],
];
for (input, expected) in input_list.iter().zip(expected_list.iter()) {
let slow_output = slow_imdct(&input, window_fn::mp3);
let mut fast_output = vec![0f32; input.len() * 2];
let (fast_output_a, fast_output_b) = fast_output.split_at_mut(input.len());
let dct = MdctNaive::new(input.len(), window_fn::mp3);
dct.process_imdct_with_scratch(&input, fast_output_a, fast_output_b, &mut []);
assert!(compare_float_vectors(&expected, &slow_output));
assert!(compare_float_vectors(&expected, &fast_output));
}
}
/// Verify that our fast implementation of the MDCT gives the same output as the slow version, for many different inputs
#[test]
fn test_matches_imdct() {
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] {
for i in 1..10 {
let input_len = i * 2;
let output_len = i * 4;
let input = random_signal(input_len);
let slow_output = slow_imdct(&input, current_window_fn);
let mut fast_output = vec![0f32; output_len];
let (fast_output_a, fast_output_b) = fast_output.split_at_mut(input_len);
let dct = MdctNaive::new(input_len, current_window_fn);
dct.process_imdct_with_scratch(&input, fast_output_a, fast_output_b, &mut []);
assert!(
compare_float_vectors(&slow_output, &fast_output),
"i = {}",
i
);
}
}
}
fn slow_imdct<F>(input: &[f32], window_fn: F) -> Vec<f32>
where
F: Fn(usize) -> Vec<f32>,
{
let mut output = vec![0f32; input.len() * 2];
let size_float = input.len() as f32;
for n in 0..output.len() {
let mut current_value = 0_f32;
let n_float = n as f32;
for k in 0..input.len() {
let k_float = k as f32;
let twiddle = (f32::consts::PI
* (n_float + 0.5_f32 + size_float * 0.5)
* (k_float + 0.5_f32)
/ size_float)
.cos();
current_value += input[k] * twiddle;
}
output[n] = current_value;
}
let window = window_fn(output.len());
output.iter().zip(window).map(|(e, w)| e * w).collect()
}
}

View File

@@ -0,0 +1,302 @@
use std::sync::Arc;
use rustfft::Length;
use crate::common::mdct_error_inplace;
use crate::mdct::Mdct;
use crate::RequiredScratch;
use crate::{DctNum, TransformType4};
/// MDCT implementation that converts the problem to a DCT Type 4 of the same size.
///
/// 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
/// to a DCT4 before converting it to a FFT results in greatly simplified code
///
/// ~~~
/// // Computes a MDCT of input size 1234 via a DCT4, using the MP3 window function
/// use rustdct::mdct::{Mdct, MdctViaDct4, window_fn};
/// use rustdct::{DctPlanner, RequiredScratch};
///
/// let len = 1234;
///
/// let mut planner = DctPlanner::new();
/// let inner_dct4 = planner.plan_dct4(len);
///
/// let dct = MdctViaDct4::new(inner_dct4, window_fn::mp3);
///
/// let input = vec![0f32; len * 2];
/// let (input_a, input_b) = input.split_at(len);
/// let mut output = vec![0f32; len];
/// let mut scratch = vec![0f32; dct.get_scratch_len()];
///
/// dct.process_mdct_with_scratch(input_a, input_b, &mut output, &mut scratch);
/// ~~~
pub struct MdctViaDct4<T> {
dct: Arc<dyn TransformType4<T>>,
window: Box<[T]>,
scratch_len: usize,
}
impl<T: DctNum> MdctViaDct4<T> {
/// Creates a new MDCT context that will process signals of length `inner_dct.len() * 2`, with an output of length `inner_dct.len()`
///
/// `inner_dct.len()` must be even.
///
/// `window_fn` is a function that takes a `size` and returns a `Vec` containing `size` window values.
/// See the [`window_fn`](mdct/window_fn/index.html) module for provided window functions.
pub fn new<F>(inner_dct: Arc<dyn TransformType4<T>>, window_fn: F) -> Self
where
F: FnOnce(usize) -> Vec<T>,
{
let len = inner_dct.len();
assert!(len % 2 == 0, "The MDCT inner_dct.len() must be even");
let window = window_fn(len * 2);
assert_eq!(
window.len(),
len * 2,
"Window function returned incorrect number of values"
);
Self {
scratch_len: len + inner_dct.get_scratch_len(),
dct: inner_dct,
window: window.into_boxed_slice(),
}
}
}
impl<T: DctNum> Mdct<T> for MdctViaDct4<T> {
fn process_mdct_with_scratch(
&self,
input_a: &[T],
input_b: &[T],
output: &mut [T],
scratch: &mut [T],
) {
let scratch = validate_buffers_mdct!(
input_a,
input_b,
output,
scratch,
self.len(),
self.get_scratch_len()
);
let group_size = self.len() / 2;
//we're going to divide input_a into two subgroups, (a,b), and input_b into two subgroups: (c,d)
//then scale them by the window function, then combine them into two subgroups: (-D-Cr, A-Br) where R means reversed
let group_a_iter = input_a
.iter()
.zip(self.window.iter())
.map(|(a, window_val)| *a * *window_val)
.take(group_size);
let group_b_rev_iter = input_a
.iter()
.zip(self.window.iter())
.map(|(b, window_val)| *b * *window_val)
.rev()
.take(group_size);
let group_c_rev_iter = input_b
.iter()
.zip(&self.window[self.len()..])
.map(|(c, window_val)| *c * *window_val)
.rev()
.skip(group_size);
let group_d_iter = input_b
.iter()
.zip(&self.window[self.len()..])
.map(|(d, window_val)| *d * *window_val)
.skip(group_size);
//the first half of the dct input is -Cr - D
for (element, (cr_val, d_val)) in output.iter_mut().zip(group_c_rev_iter.zip(group_d_iter))
{
*element = -cr_val - d_val;
}
//the second half of the dct input is is A - Br
for (element, (a_val, br_val)) in output[group_size..]
.iter_mut()
.zip(group_a_iter.zip(group_b_rev_iter))
{
*element = a_val - br_val;
}
self.dct.process_dct4_with_scratch(output, scratch);
}
fn process_imdct_with_scratch(
&self,
input: &[T],
output_a: &mut [T],
output_b: &mut [T],
scratch: &mut [T],
) {
let scratch = validate_buffers_mdct!(
input,
output_a,
output_b,
scratch,
self.len(),
self.get_scratch_len()
);
let (dct_buffer, dct_scratch) = scratch.split_at_mut(self.len());
dct_buffer.copy_from_slice(input);
self.dct.process_dct4_with_scratch(dct_buffer, dct_scratch);
let group_size = self.len() / 2;
//copy the second half of the DCT output into the result
for ((output, window_val), val) in output_a
.iter_mut()
.zip(&self.window[..])
.zip(dct_buffer[group_size..].iter())
{
*output = *output + *val * *window_val;
}
//copy the second half of the DCT output again, but this time reversed and negated
for ((output, window_val), val) in output_a
.iter_mut()
.zip(&self.window[..])
.skip(group_size)
.zip(dct_buffer[group_size..].iter().rev())
{
*output = *output - *val * *window_val;
}
//copy the first half of the DCT output into the result, reversde+negated
for ((output, window_val), val) in output_b
.iter_mut()
.zip(&self.window[self.len()..])
.zip(dct_buffer[..group_size].iter().rev())
{
*output = *output - *val * *window_val;
}
//copy the first half of the DCT output again, but this time not reversed
for ((output, window_val), val) in output_b
.iter_mut()
.zip(&self.window[self.len()..])
.skip(group_size)
.zip(dct_buffer[..group_size].iter())
{
*output = *output - *val * *window_val;
}
}
}
impl<T> Length for MdctViaDct4<T> {
fn len(&self) -> usize {
self.dct.len()
}
}
impl<T> RequiredScratch for MdctViaDct4<T> {
fn get_scratch_len(&self) -> usize {
self.scratch_len
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::algorithm::Type4Naive;
use crate::mdct::window_fn;
use crate::mdct::MdctNaive;
use crate::test_utils::{compare_float_vectors, random_signal};
/// Verify that our fast implementation of the MDCT and IMDCT gives the same output as the slow version, for many different inputs
#[test]
fn test_mdct_via_dct4() {
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] {
for i in 1..11 {
let input_len = i * 4;
let output_len = i * 2;
let input = random_signal(input_len);
let (input_a, input_b) = input.split_at(output_len);
let mut naive_output = vec![0f32; output_len];
let mut fast_output = vec![0f32; output_len];
let naive_mdct = MdctNaive::new(output_len, current_window_fn);
let inner_dct4 = Arc::new(Type4Naive::new(output_len));
let fast_mdct = MdctViaDct4::new(inner_dct4, current_window_fn);
let mut naive_scratch = vec![0f32; naive_mdct.get_scratch_len()];
let mut fast_scratch = vec![0f32; fast_mdct.get_scratch_len()];
naive_mdct.process_mdct_with_scratch(
&input_a,
&input_b,
&mut naive_output,
&mut naive_scratch,
);
fast_mdct.process_mdct_with_scratch(
&input_a,
&input_b,
&mut fast_output,
&mut fast_scratch,
);
assert!(
compare_float_vectors(&naive_output, &fast_output),
"i = {}",
i
);
}
}
}
/// Verify that our fast implementation of the MDCT and IMDCT gives the same output as the slow version, for many different inputs
#[test]
fn test_imdct_via_dct4() {
for current_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] {
for i in 1..11 {
let input_len = i * 2;
let output_len = i * 4;
let input = random_signal(input_len);
// Fill both output buffers with ones, instead of zeroes, to verify that the IMDCT doesn't overwrite the output buffer
let mut naive_output = vec![1f32; output_len];
let (naive_output_a, naive_output_b) = naive_output.split_at_mut(input_len);
let mut fast_output = vec![1f32; output_len];
let (fast_output_a, fast_output_b) = fast_output.split_at_mut(input_len);
let naive_mdct = MdctNaive::new(input_len, current_window_fn);
let inner_dct4 = Arc::new(Type4Naive::new(input_len));
let fast_mdct = MdctViaDct4::new(inner_dct4, current_window_fn);
let mut naive_scratch = vec![0f32; naive_mdct.get_scratch_len()];
let mut fast_scratch = vec![0f32; fast_mdct.get_scratch_len()];
naive_mdct.process_imdct_with_scratch(
&input,
naive_output_a,
naive_output_b,
&mut naive_scratch,
);
fast_mdct.process_imdct_with_scratch(
&input,
fast_output_a,
fast_output_b,
&mut fast_scratch,
);
assert!(
compare_float_vectors(&naive_output, &fast_output),
"i = {}",
i
);
}
}
}
}

View File

@@ -0,0 +1,46 @@
use rustfft::Length;
mod mdct_naive;
mod mdct_via_dct4;
pub mod window_fn;
/// An umbrella trait for algorithms which compute the Modified Discrete Cosine Transform (MDCT)
pub trait Mdct<T: DctNum>: RequiredScratch + Length + Sync + Send {
/// Computes the MDCT on the `input` buffer and places the result in the `output` buffer.
/// Uses `input_a` for the first half of the input, and `input_b` for the second half of the input
///
/// To make overlapping array segments easier, this method DOES NOT modify the input buffer.
///
/// Normalization depends on which window function was chosen when planning the mdct --
/// each built-in window function documents whether it does normalization or not.
fn process_mdct_with_scratch(
&self,
input_a: &[T],
input_b: &[T],
output: &mut [T],
scratch: &mut [T],
);
/// Computes the IMDCT on the `input` buffer and places the result in the `output` buffer.
/// Puts the first half of the output in `output_a`, and puts the first half of the output in `output_b`.
///
/// Since the IMDCT is designed with overlapping output segments in mind, this method DOES NOT zero
/// out the output buffer before writing like most other DCT algorithms. Instead, it sums
/// the result of the IMDCT with what's already in the output buffer.
///
/// Normalization depends on which window function was chosen when planning the mdct --
/// each built-in window function documents whether it does normalization or not.
fn process_imdct_with_scratch(
&self,
input: &[T],
output_a: &mut [T],
output_b: &mut [T],
scratch: &mut [T],
);
}
use crate::{DctNum, RequiredScratch};
pub use self::mdct_naive::MdctNaive;
pub use self::mdct_via_dct4::MdctViaDct4;

View File

@@ -0,0 +1,91 @@
use std::f64;
use crate::DctNum;
/// MP3 window function for MDCT
pub fn mp3<T: DctNum>(len: usize) -> Vec<T> {
let constant_term = f64::consts::PI / len as f64;
(0..len)
.map(|n| (constant_term * (n as f64 + 0.5f64)).sin())
.map(|w| T::from_f64(w).unwrap())
.collect()
}
/// MP3 window function for MDCT. Combines a scale for normalization into the window function so that the process is conveniently invertible.
pub fn mp3_invertible<T: DctNum>(len: usize) -> Vec<T> {
let outer_scale = (4.0 / len as f64).sqrt();
let constant_term = f64::consts::PI / len as f64;
(0..len)
.map(|n| (constant_term * (n as f64 + 0.5f64)).sin() * outer_scale)
.map(|w| T::from_f64(w).unwrap())
.collect()
}
/// Ogg Vorbis window function for MDCT
pub fn vorbis<T: DctNum>(len: usize) -> Vec<T> {
let constant_term = f64::consts::PI / len as f64;
(0..len)
.map(|n| {
let inner_sin = (constant_term * (n as f64 + 0.5f64)).sin();
(f64::consts::PI * 0.5f64 * inner_sin * inner_sin).sin()
})
.map(|w| T::from_f64(w).unwrap())
.collect()
}
/// Ogg Vorbis window function for MDCT. Combines a scale for normalization into the window function so that the process is conveniently invertible.
pub fn vorbis_invertible<T: DctNum>(len: usize) -> Vec<T> {
let outer_scale = (4.0 / len as f64).sqrt();
let constant_term = f64::consts::PI / len as f64;
(0..len)
.map(|n| {
let inner_sin = (constant_term * (n as f64 + 0.5f64)).sin();
(f64::consts::PI * 0.5f64 * inner_sin * inner_sin).sin() * outer_scale
})
.map(|w| T::from_f64(w).unwrap())
.collect()
}
/// MDCT window function which is all ones (IE, no windowing will be applied)
pub fn one<T: DctNum>(len: usize) -> Vec<T> {
(0..len).map(|_| T::one()).collect()
}
/// MDCT window function which is all ones (IE, no windowing will be applied). Combines a scale for normalization into the window function so that the process is conveniently invertible.
pub fn invertible<T: DctNum>(len: usize) -> Vec<T> {
let constant_term = (2.0 / len as f64).sqrt();
(0..len)
.map(|_| constant_term)
.map(|w| T::from_f64(w).unwrap())
.collect()
}
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::test_utils::fuzzy_cmp;
/// Verify that each of the built-in window functions does what we expect
#[test]
fn test_window_fns() {
for test_fn in &[mp3, vorbis] {
for half_size in 1..20 {
let evaluated_window: Vec<f32> = test_fn(half_size * 2);
//verify that for all i from 0 to half_size, window[i]^2 + window[i+half_size]^2 == 1
//also known as the "Princen-Bradley condition"
for i in 0..half_size {
let first = evaluated_window[i];
let second = evaluated_window[i + half_size];
assert!(fuzzy_cmp(first * first + second * second, 1f32, 0.001f32));
}
}
}
}
}

View File

@@ -0,0 +1,356 @@
use std::collections::HashMap;
use std::sync::Arc;
use crate::algorithm::type2and3_butterflies::*;
use crate::algorithm::*;
use crate::mdct::*;
use crate::{
Dct1, Dct5, Dct6And7, Dct8, Dst1, Dst5, Dst6And7, Dst8, TransformType2And3, TransformType4,
};
use rustfft::FftPlanner;
use crate::DctNum;
const DCT2_BUTTERFLIES: [usize; 5] = [2, 3, 4, 8, 16];
/// The DCT planner is used to make new DCT algorithm instances.
///
/// RustDCT has several DCT algorithms available for each DCT type; For a given DCT type and problem size, the DctPlanner
/// decides which of the available DCT algorithms to use and then initializes them.
///
/// ~~~
/// // Perform a DCT Type 4 of size 1234
/// use std::sync::Arc;
/// use rustdct::DctPlanner;
///
/// let mut planner = DctPlanner::new();
/// let dct4 = planner.plan_dct4(1234);
///
/// let mut buffer = vec![0f32; 1234];
/// dct4.process_dct4(&mut buffer);
///
/// // The DCT instance returned by the planner is stored behind an `Arc`, so it's cheap to clone
/// let dct4_clone = Arc::clone(&dct4);
/// ~~~
///
/// If you plan on creating multiple DCT instances, it is recommnded to reuse the same planner for all of them. This
/// is because the planner re-uses internal data across DCT instances wherever possible, saving memory and reducing
/// setup time. (DCT instances created with one planner will never re-use data and buffers with DCT instances created
/// by a different planner)
///
/// Each DCT instance owns `Arc`s to its shared internal data, rather than borrowing it from the planner, so it's
/// perfectly safe to drop the planner after creating DCT instances.
pub struct DctPlanner<T: DctNum> {
fft_planner: FftPlanner<T>,
dct1_cache: HashMap<usize, Arc<dyn Dct1<T>>>,
dst1_cache: HashMap<usize, Arc<dyn Dst1<T>>>,
dct23_cache: HashMap<usize, Arc<dyn TransformType2And3<T>>>,
dct4_cache: HashMap<usize, Arc<dyn TransformType4<T>>>,
dct5_cache: HashMap<usize, Arc<dyn Dct5<T>>>,
dst5_cache: HashMap<usize, Arc<dyn Dst5<T>>>,
dct6_cache: HashMap<usize, Arc<dyn Dct6And7<T>>>,
dst6_cache: HashMap<usize, Arc<dyn Dst6And7<T>>>,
dct8_cache: HashMap<usize, Arc<dyn Dct8<T>>>,
dst8_cache: HashMap<usize, Arc<dyn Dst8<T>>>,
mdct_cache: HashMap<usize, Arc<dyn Mdct<T>>>,
}
impl<T: DctNum> DctPlanner<T> {
pub fn new() -> Self {
Self {
fft_planner: FftPlanner::new(),
dct1_cache: HashMap::new(),
dst1_cache: HashMap::new(),
dct23_cache: HashMap::new(),
dct4_cache: HashMap::new(),
dct5_cache: HashMap::new(),
dst5_cache: HashMap::new(),
dct6_cache: HashMap::new(),
dst6_cache: HashMap::new(),
dct8_cache: HashMap::new(),
dst8_cache: HashMap::new(),
mdct_cache: HashMap::new(),
}
}
/// Returns a DCT Type 1 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct1(&mut self, len: usize) -> Arc<dyn Dct1<T>> {
if self.dct1_cache.contains_key(&len) {
Arc::clone(self.dct1_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct1(len);
self.dct1_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct1(&mut self, len: usize) -> Arc<dyn Dct1<T>> {
//benchmarking shows that below about 10, it's faster to just use the naive DCT1 algorithm
if len < 10 {
Arc::new(Dct1Naive::new(len))
} else {
let fft = self.fft_planner.plan_fft_forward((len - 1) * 2);
Arc::new(Dct1ConvertToFft::new(fft))
}
}
/// Returns a DCT Type 2 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
if self.dct23_cache.contains_key(&len) {
Arc::clone(self.dct23_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct2(len);
self.dct23_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
if DCT2_BUTTERFLIES.contains(&len) {
self.plan_dct2_butterfly(len)
} else if len.is_power_of_two() && len > 2 {
let half_dct = self.plan_dct2(len / 2);
let quarter_dct = self.plan_dct2(len / 4);
Arc::new(Type2And3SplitRadix::new(half_dct, quarter_dct))
} else {
// Benchmarking shows that it's always faster
let fft = self.fft_planner.plan_fft_forward(len);
Arc::new(Type2And3ConvertToFft::new(fft))
}
}
fn plan_dct2_butterfly(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
match len {
2 => Arc::new(Type2And3Butterfly2::new()),
3 => Arc::new(Type2And3Butterfly3::new()),
4 => Arc::new(Type2And3Butterfly4::new()),
8 => Arc::new(Type2And3Butterfly8::new()),
16 => Arc::new(Type2And3Butterfly16::new()),
_ => panic!("Invalid butterfly size for DCT2: {}", len),
}
}
/// Returns DCT Type 3 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct3(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
self.plan_dct2(len)
}
/// Returns a DCT Type 4 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> {
if self.dct4_cache.contains_key(&len) {
Arc::clone(self.dct4_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct4(len);
self.dct4_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> {
//if we have an even size, we can use the DCT4 Via DCT3 algorithm
if len % 2 == 0 {
//benchmarking shows that below 6, it's faster to just use the naive DCT4 algorithm
if len < 6 {
Arc::new(Type4Naive::new(len))
} else {
let inner_dct = self.plan_dct3(len / 2);
Arc::new(Type4ConvertToType3Even::new(inner_dct))
}
} else {
//odd size, so we can use the "DCT4 via FFT odd" algorithm
//benchmarking shows that below about 7, it's faster to just use the naive DCT4 algorithm
if len < 7 {
Arc::new(Type4Naive::new(len))
} else {
let fft = self.fft_planner.plan_fft_forward(len);
Arc::new(Type4ConvertToFftOdd::new(fft))
}
}
}
/// Returns a DCT Type 5 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct5(&mut self, len: usize) -> Arc<dyn Dct5<T>> {
if self.dct5_cache.contains_key(&len) {
Arc::clone(self.dct5_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct5(len);
self.dct5_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct5(&mut self, len: usize) -> Arc<dyn Dct5<T>> {
Arc::new(Dct5Naive::new(len))
}
/// Returns a DCT Type 6 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct6(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> {
if self.dct6_cache.contains_key(&len) {
Arc::clone(self.dct6_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct6(len);
self.dct6_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct6(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> {
Arc::new(Dct6And7Naive::new(len))
}
/// Returns DCT Type 7 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct7(&mut self, len: usize) -> Arc<dyn Dct6And7<T>> {
self.plan_dct6(len)
}
/// Returns a DCT Type 8 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dct8(&mut self, len: usize) -> Arc<dyn Dct8<T>> {
if self.dct8_cache.contains_key(&len) {
Arc::clone(self.dct8_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dct8(len);
self.dct8_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dct8(&mut self, len: usize) -> Arc<dyn Dct8<T>> {
Arc::new(Dct8Naive::new(len))
}
/// Returns a DST Type 1 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst1(&mut self, len: usize) -> Arc<dyn Dst1<T>> {
if self.dst1_cache.contains_key(&len) {
Arc::clone(self.dst1_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dst1(len);
self.dst1_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dst1(&mut self, len: usize) -> Arc<dyn Dst1<T>> {
//benchmarking shows that below about 25, it's faster to just use the naive DCT1 algorithm
if len < 25 {
Arc::new(Dst1Naive::new(len))
} else {
let fft = self.fft_planner.plan_fft_forward((len + 1) * 2);
Arc::new(Dst1ConvertToFft::new(fft))
}
}
/// Returns DST Type 2 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst2(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
self.plan_dct2(len)
}
/// Returns DST Type 3 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst3(&mut self, len: usize) -> Arc<dyn TransformType2And3<T>> {
self.plan_dct2(len)
}
/// Returns DST Type 4 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst4(&mut self, len: usize) -> Arc<dyn TransformType4<T>> {
self.plan_dct4(len)
}
/// Returns a DST Type 5 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst5(&mut self, len: usize) -> Arc<dyn Dst5<T>> {
if self.dst5_cache.contains_key(&len) {
Arc::clone(self.dst5_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dst5(len);
self.dst5_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dst5(&mut self, len: usize) -> Arc<dyn Dst5<T>> {
Arc::new(Dst5Naive::new(len))
}
/// Returns a DST Type 6 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst6(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> {
if self.dst6_cache.contains_key(&len) {
Arc::clone(self.dst6_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dst6(len);
self.dst6_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dst6(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> {
if len < 45 {
Arc::new(Dst6And7Naive::new(len))
} else {
let fft = self.fft_planner.plan_fft_forward(len * 2 + 1);
Arc::new(Dst6And7ConvertToFft::new(fft))
}
}
/// Returns DST Type 7 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst7(&mut self, len: usize) -> Arc<dyn Dst6And7<T>> {
self.plan_dst6(len)
}
/// Returns a DST Type 8 instance which processes signals of size `len`.
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_dst8(&mut self, len: usize) -> Arc<dyn Dst8<T>> {
if self.dst8_cache.contains_key(&len) {
Arc::clone(self.dst8_cache.get(&len).unwrap())
} else {
let result = self.plan_new_dst8(len);
self.dst8_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_dst8(&mut self, len: usize) -> Arc<dyn Dst8<T>> {
Arc::new(Dst8Naive::new(len))
}
/// Returns a MDCT instance which processes inputs of size ` len * 2` and produces outputs of size `len`.
///
/// `window_fn` is a function that takes a `size` and returns a `Vec` containing `size` window values.
/// See the [`window_fn`](mdct/window_fn/index.html) module for provided window functions.
///
/// If this is called multiple times, it will attempt to re-use internal data between instances
pub fn plan_mdct<F>(&mut self, len: usize, window_fn: F) -> Arc<dyn Mdct<T>>
where
F: (FnOnce(usize) -> Vec<T>),
{
if self.mdct_cache.contains_key(&len) {
Arc::clone(self.mdct_cache.get(&len).unwrap())
} else {
let result = self.plan_new_mdct(len, window_fn);
self.mdct_cache.insert(len, Arc::clone(&result));
result
}
}
fn plan_new_mdct<F>(&mut self, len: usize, window_fn: F) -> Arc<dyn Mdct<T>>
where
F: (FnOnce(usize) -> Vec<T>),
{
//benchmarking shows that using the inner dct4 algorithm is always faster than computing the naive algorithm
let inner_dct4 = self.plan_dct4(len);
Arc::new(MdctViaDct4::new(inner_dct4, window_fn))
}
}

View File

@@ -0,0 +1,38 @@
extern crate rand;
use std::f32;
use rand::{distributions::Uniform, prelude::Distribution, rngs::StdRng, SeedableRng};
pub fn fuzzy_cmp(a: f32, b: f32, tolerance: f32) -> bool {
a >= b - tolerance && a <= b + tolerance
}
pub fn compare_float_vectors(expected: &[f32], observed: &[f32]) -> bool {
assert_eq!(expected.len(), observed.len());
let tolerance: f32 = 0.001;
for i in 0..expected.len() {
if !fuzzy_cmp(observed[i], expected[i], tolerance) {
return false;
}
}
true
}
pub fn random_signal(length: usize) -> Vec<f32> {
let mut sig = Vec::with_capacity(length);
let normal_dist = Uniform::new(0.0, 10.0);
let seed: [u8; 32] = [
1, 5, 6, 4, 1, 5, 3, 7, 4, 2, 6, 2, 6, 4, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 3, 0,
1, 0,
];
let mut rng: StdRng = SeedableRng::from_seed(seed);
for _ in 0..length {
sig.push(normal_dist.sample(&mut rng) as f32);
}
return sig;
}

View File

@@ -0,0 +1,83 @@
use rustfft::num_complex::Complex;
use std::f64;
use crate::DctNum;
#[inline(always)]
pub fn single_twiddle<T: DctNum>(i: usize, fft_len: usize) -> Complex<T> {
let angle_constant = f64::consts::PI * -2f64 / fft_len as f64;
let c = Complex::from_polar(1f64, angle_constant * i as f64);
Complex {
re: T::from_f64(c.re).unwrap(),
im: T::from_f64(c.im).unwrap(),
}
}
// Same as above, but only return the real portion, not the imaginary portion
#[inline(always)]
pub fn single_twiddle_re<T: DctNum>(i: usize, fft_len: usize) -> T {
let angle_constant = f64::consts::PI * -2f64 / fft_len as f64;
let c = (angle_constant * i as f64).cos();
T::from_f64(c).unwrap()
}
// Same as above, but we add 0.5 to 0 before
#[inline(always)]
pub fn single_twiddle_halfoffset<T: DctNum>(i: usize, fft_len: usize) -> Complex<T> {
let angle_constant = f64::consts::PI * -2f64 / fft_len as f64;
let c = Complex::from_polar(1f64, angle_constant * (i as f64 + 0.5f64));
Complex {
re: T::from_f64(c.re).unwrap(),
im: T::from_f64(c.im).unwrap(),
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
use std::f32;
use crate::test_utils::fuzzy_cmp;
#[test]
fn test_single() {
let len = 20;
for i in 0..len {
let single: Complex<f32> = single_twiddle(i, len);
let single_inverse: Complex<f32> = single_twiddle(i, len).conj();
let expected =
Complex::from_polar(1f32, -2f32 * f32::consts::PI * i as f32 / len as f32);
let expected_inverse =
Complex::from_polar(1f32, 2f32 * f32::consts::PI * i as f32 / len as f32);
assert!(
fuzzy_cmp(single.re, expected.re, 0.001f32),
"forwards, i = {}",
i
);
assert!(
fuzzy_cmp(single.im, expected.im, 0.001f32),
"forwards, i = {}",
i
);
assert!(
fuzzy_cmp(single_inverse.re, expected_inverse.re, 0.001f32),
"inverse, i = {}",
i
);
assert!(
fuzzy_cmp(single_inverse.im, expected_inverse.im, 0.001f32),
"inverse, i = {}",
i
);
}
}
}

View File

@@ -0,0 +1,520 @@
pub struct KnownTestValues {
pub input: Vec<f64>,
pub expected_output: Vec<f64>,
}
// [Autogenerated Test Tata]
// known input/output values for DCT Type 1
pub fn known_values_dct1() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![2.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![4.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![4.6, -1.3],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![6.6, 2.6, -5.2],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![8.775, 2.425, 2.775, -9.275],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![12.2, 1.1025, 3.85, 1.5975, -12.1],
},
]
}
// known input/output values for DCT Type 2
pub fn known_values_dct2() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![1.0],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![2.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![3.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![4.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![5.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![3.3],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![9.2, -1.8385],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![7.3, 4.5033, -5.2],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![13.55, 0.25949, 3.9244, -8.3352],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![14.15, 2.3621, 1.3006, 1.9199, -10.524],
},
]
}
// known input/output values for DCT Type 3
pub fn known_values_dct3() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![0.5],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.2071, -0.20711],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![1.866, -0.5, 0.13397],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![2.5137, -0.7483, 0.33409, -0.099456],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.1569, -0.98131, 0.5, -0.25476, 0.079192],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![1.65],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![5.8219, -2.5219],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![5.8095, 3.55, -4.4095],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![8.1492, -0.52291, 6.5099, -7.5362],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![9.5832, -0.72445, 4.15, 4.2279, -8.9866],
},
]
}
// known input/output values for DCT Type 4
pub fn known_values_dct4() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![0.70711],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.3066, -0.5412],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![1.9319, -0.70711, 0.51764],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![2.5629, -0.89998, 0.60134, -0.5098],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.1962, -1.1013, 0.70711, -0.56116, 0.50623],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![2.3335],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![5.3066, -4.188],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![6.8677, -0.49497, -5.1531],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![8.306, -0.016005, 0.87276, -10.344],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![10.104, -1.2387, 4.3487, -5.2296, -8.482],
},
]
}
// known input/output values for DST Type 1
pub fn known_values_dst1() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![1.0],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.7321, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![2.4142, 0.0, 0.41421],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.0777, 0.0, 0.72654, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.7321, 0.0, 1.0, 0.0, 0.26795],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![3.3],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![7.9674, -2.2517],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![6.8899, 5.2, -4.9101],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![9.4176, 1.7791, 6.7314, -9.1522],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![10.572, 2.0352, 5.8, 2.6414, -10.472],
},
]
}
// known input/output values for DST Type 2
pub fn known_values_dst2() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![1.0],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.4142, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![2.0, 0.0, 1.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![2.6131, 0.0, 1.0824, 0.0],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.2361, 0.0, 1.2361, 0.0, 1.0],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![3.3],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![6.5054, -2.6],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![6.6, 4.5033, -4.5],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![7.3501, 3.4295, 7.2923, -10.75],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![9.1347, 1.2542, 8.8097, 2.7736, -10.15],
},
]
}
// known input/output values for DST Type 3
pub fn known_values_dst3() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![0.5],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.2071, 0.20711],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![1.866, 0.5, 0.13397],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![2.5137, 0.7483, 0.33409, 0.099456],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.1569, 0.98131, 0.5, 0.25476, 0.079192],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![1.65],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![5.2835, -0.61655],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![5.8095, 4.25, -4.4095],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![6.8044, 4.8228, 2.729, -7.7894],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![9.1947, 3.7202, 5.5, -0.15495, -9.6294],
},
]
}
// known input/output values for DST Type 4
pub fn known_values_dst4() -> Vec<KnownTestValues> {
vec![
KnownTestValues {
input: vec![0.0],
expected_output: vec![0.0],
},
KnownTestValues {
input: vec![0.0, 0.0],
expected_output: vec![0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![0.0, 0.0, 0.0, 0.0, 0.0],
expected_output: vec![0.0, 0.0, 0.0, 0.0, 0.0],
},
KnownTestValues {
input: vec![1.0],
expected_output: vec![0.70711],
},
KnownTestValues {
input: vec![1.0, 1.0],
expected_output: vec![1.3066, 0.5412],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0],
expected_output: vec![1.9319, 0.70711, 0.51764],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0],
expected_output: vec![2.5629, 0.89998, 0.60134, 0.5098],
},
KnownTestValues {
input: vec![1.0, 1.0, 1.0, 1.0, 1.0],
expected_output: vec![3.1962, 1.1013, 0.70711, 0.56116, 0.50623],
},
KnownTestValues {
input: vec![3.3],
expected_output: vec![2.3335],
},
KnownTestValues {
input: vec![3.3, 5.9],
expected_output: vec![6.7137, 0.79097],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9],
expected_output: vec![3.1908, 7.8489, -1.4761],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25],
expected_output: vec![8.4718, 2.0527, 9.2307, -3.944],
},
KnownTestValues {
input: vec![3.3, 5.9, -1.9, 6.25, 0.6],
expected_output: vec![8.0127, 4.4697, 3.8537, 9.2615, -6.0846],
},
]
}

View File

@@ -0,0 +1,191 @@
use crate::common::{compare_float_vectors, random_signal};
use rustdct::DctPlanner;
macro_rules! dct_test_with_known_data {
($reference_fn:ident, $naive_struct:ident, $process_fn: ident, $known_data_fn:ident) => (
// Compare our naive struct and our reference_fn implementation against a bunch of known data
let known_data = $known_data_fn();
for entry in known_data {
let len = entry.input.len();
assert_eq!(len, entry.expected_output.len(), "Invalid test data -- input and known output are not the same length");
let mut naive_buffer = entry.input.clone();
let naive_dct = $naive_struct::new(len);
naive_dct.$process_fn(&mut naive_buffer);
let slow_output = $reference_fn(&entry.input);
println!("input: {:?}", entry.input);
println!("expected output:{:?}", entry.expected_output);
println!("naive output: {:?}", naive_buffer);
println!("slow output: {:?}", slow_output);
assert!(compare_float_vectors(&entry.expected_output, &naive_buffer));
assert!(compare_float_vectors(&entry.expected_output, &slow_output));
}
)
}
macro_rules! dct_test_inverse {
($reference_fn:ident, $inverse_fn:ident, $inverse_scale_fn:ident, $first_size:expr) => (
// Test that the slow fn, paired with the correct inverse fn, actually yields the original data
for len in $first_size..20 {
let input = random_signal(len);
let intermediate = $reference_fn(&input);
let inverse = $inverse_fn(&intermediate);
let inverse_scale = $inverse_scale_fn(len);
let scaled_inverse: Vec<f64> = inverse.into_iter().map(|entry| entry * inverse_scale).collect();
println!("input: {:?}", input);
println!("scaled inverse: {:?}", scaled_inverse);
assert!(compare_float_vectors(&input, &scaled_inverse));
}
)
}
macro_rules! dct_test_with_planner {
($reference_fn:ident, $naive_struct:ident, $process_fn: ident, $planner_fn:ident, $first_size:expr) => {
// Compare our naive struct against the output from the planner
for len in $first_size..20 {
let input = random_signal(len);
let mut naive_buffer = input.clone();
let mut actual_buffer = input.clone();
let naive_dct = $naive_struct::new(len);
let mut planner = DctPlanner::new();
let actual_dct = planner.$planner_fn(len);
assert_eq!(
actual_dct.len(),
len,
"Planner created a DCT of incorrect length. Expected {}, got {}",
len,
actual_dct.len()
);
let reference_output = $reference_fn(&input);
naive_dct.$process_fn(&mut naive_buffer);
actual_dct.$process_fn(&mut actual_buffer);
println!("input: {:?}", input);
println!("reference output:{:?}", reference_output);
println!("naive output: {:?}", naive_buffer);
println!("planned output: {:?}", actual_buffer);
assert!(compare_float_vectors(&reference_output, &naive_buffer));
assert!(compare_float_vectors(&reference_output, &actual_buffer));
}
};
}
pub mod test_mdct {
use super::*;
use rustdct::{
mdct::{Mdct, MdctNaive},
RequiredScratch,
};
pub fn planned_matches_naive<F>(len: usize, window_fn: F)
where
F: Fn(usize) -> Vec<f32>,
{
let input = random_signal(len * 2);
println!("input: {:?}", input);
let (input_a, input_b) = input.split_at(len);
let mut naive_output = vec![0f32; len];
let mut actual_output = vec![0f32; len];
let naive_dct = MdctNaive::new(len, &window_fn);
let mut planner = DctPlanner::new();
let actual_dct = planner.plan_mdct(len, window_fn);
assert_eq!(
actual_dct.len(),
len,
"Planner created a DCT of incorrect length"
);
let mut naive_scratch = vec![0f32; naive_dct.get_scratch_len()];
let mut fast_scratch = vec![0f32; actual_dct.get_scratch_len()];
naive_dct.process_mdct_with_scratch(
input_a,
input_b,
&mut naive_output,
&mut naive_scratch,
);
actual_dct.process_mdct_with_scratch(
input_a,
input_b,
&mut actual_output,
&mut fast_scratch,
);
println!("Naive output: {:?}", naive_output);
println!("Planned output: {:?}", actual_output);
assert!(
compare_float_vectors(&naive_output, &actual_output),
"len = {}",
len
);
}
pub fn test_tdac<F>(len: usize, scale_factor: f32, window_fn: F)
where
F: Fn(usize) -> Vec<f32>,
{
let mut planner = DctPlanner::new();
let mdct = planner.plan_mdct(len, &window_fn);
const NUM_SEGMENTS: usize = 5;
let input = random_signal(len * (NUM_SEGMENTS + 1));
let mut output = vec![0f32; len * NUM_SEGMENTS];
let mut inverse = vec![0f32; len * (NUM_SEGMENTS + 1)];
let mut scratch = vec![0f32; mdct.get_scratch_len()];
for i in 0..NUM_SEGMENTS {
let input_chunk = &input[len * i..(len * (i + 2))];
let output_chunk = &mut output[len * i..(len * (i + 1))];
let (input_a, input_b) = input_chunk.split_at(len);
mdct.process_mdct_with_scratch(input_a, input_b, output_chunk, &mut scratch);
}
for i in 0..NUM_SEGMENTS {
let input_chunk = &output[len * i..(len * (i + 1))];
let output_chunk = &mut inverse[len * i..(len * (i + 2))];
let (output_a, output_b) = output_chunk.split_at_mut(len);
mdct.process_imdct_with_scratch(input_chunk, output_a, output_b, &mut scratch);
}
//we have to scale the inverse by 1/len
for element in inverse.iter_mut() {
*element = *element * scale_factor;
}
println!("scale: {:?}", scale_factor);
println!("input: {:?}", &input[len..input.len() - len]);
println!("inverse: {:?}", &inverse[len..input.len() - len]);
assert!(
compare_float_vectors(
&input[len..input.len() - len],
&inverse[len..inverse.len() - len],
),
"len = {}",
len
);
}
}

View File

@@ -0,0 +1,41 @@
use rand::{distributions::Uniform, prelude::Distribution, rngs::StdRng, SeedableRng};
use rustdct::num_traits::{Float, FromPrimitive};
pub mod known_data;
pub mod reference_impls;
#[macro_use]
pub mod macros;
pub fn fuzzy_cmp<T: Float>(a: T, b: T, tolerance: T) -> bool {
a >= b - tolerance && a <= b + tolerance
}
pub fn compare_float_vectors<T: Float + FromPrimitive>(expected: &[T], observed: &[T]) -> bool {
assert_eq!(expected.len(), observed.len());
let tolerance = T::from_f64(0.001).unwrap();
for i in 0..expected.len() {
if !fuzzy_cmp(observed[i], expected[i], tolerance) {
return false;
}
}
true
}
pub fn random_signal<T: Float + FromPrimitive>(length: usize) -> Vec<T> {
let mut sig = Vec::with_capacity(length);
let distribution = Uniform::new(0.0, 10.0);
let seed: [u8; 32] = [
1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 1, 0,
1, 7,
];
let mut rng: StdRng = SeedableRng::from_seed(seed);
for _ in 0..length {
sig.push(T::from_f64(distribution.sample(&mut rng)).unwrap());
}
return sig;
}

View File

@@ -0,0 +1,312 @@
/// This file contains reference implementations of all DCT and DST transforms.
/// The goal of these implementations is not to be fast, but to match the mathematical definitions as closely as possible and to be easy to follow and debug
/// The reference for the mathematical definitions was section 9 of "The Discrete W Transforms" by Wang and Hunt, but with the normalization/orthogonalization factors omitted.
use std::f64;
/// Simplified version of DCT1
pub fn reference_dct1(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == 0 || input_index == input.len() - 1 {
0.5
} else {
1.0
};
let cos_inner = (output_index as f64) * (input_index as f64) * f64::consts::PI
/ ((input.len() - 1) as f64);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DCT2
pub fn reference_dct2(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let cos_inner = (output_index as f64) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DCT3
pub fn reference_dct3(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
let cos_inner = (output_index as f64 + 0.5) * (input_index as f64) * f64::consts::PI
/ (input.len() as f64);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DCT4
pub fn reference_dct4(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let cos_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DCT5
pub fn reference_dct5(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
let cos_inner = (output_index as f64) * (input_index as f64) * f64::consts::PI
/ (input.len() as f64 - 0.5);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DCT6
pub fn reference_dct6(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == input.len() - 1 {
0.5
} else {
1.0
};
let cos_inner = (output_index as f64) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64 - 0.5);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DCT7
pub fn reference_dct7(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
let cos_inner = (output_index as f64 + 0.5) * (input_index as f64) * f64::consts::PI
/ (input.len() as f64 - 0.5);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DCT8
pub fn reference_dct8(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let cos_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64 + 0.5);
let twiddle = cos_inner.cos();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST1
pub fn reference_dst1(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 1.0) * (input_index as f64 + 1.0) * f64::consts::PI
/ ((input.len() + 1) as f64);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST2
pub fn reference_dst2(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 1.0) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST3
pub fn reference_dst3(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == input.len() - 1 {
0.5
} else {
1.0
};
let sin_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 1.0) * f64::consts::PI
/ (input.len() as f64);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}
/// Simplified version of DST4
pub fn reference_dst4(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST5
pub fn reference_dst5(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 1.0) * (input_index as f64 + 1.0) * f64::consts::PI
/ ((input.len()) as f64 + 0.5);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST6
pub fn reference_dst6(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 1.0) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64 + 0.5);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST7
pub fn reference_dst7(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let sin_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 1.0) * f64::consts::PI
/ (input.len() as f64 + 0.5);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle;
}
result.push(entry);
}
result
}
/// Simplified version of DST8
pub fn reference_dst8(input: &[f64]) -> Vec<f64> {
let mut result = Vec::new();
for output_index in 0..input.len() {
let mut entry = 0.0;
for input_index in 0..input.len() {
let multiplier = if input_index == input.len() - 1 {
0.5
} else {
1.0
};
let sin_inner =
(output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
/ (input.len() as f64 - 0.5);
let twiddle = sin_inner.sin();
entry += input[input_index] * twiddle * multiplier;
}
result.push(entry);
}
result
}

View File

@@ -0,0 +1,216 @@
extern crate rand;
extern crate rustdct;
#[macro_use]
mod common;
use rustdct::algorithm::{
Dct1Naive, Dct5Naive, Dct6And7Naive, Dct8Naive, Dst1Naive, Dst5Naive, Dst6And7Naive, Dst8Naive,
Type2And3Naive, Type4Naive,
};
use rustdct::mdct::window_fn;
use rustdct::DctPlanner;
use rustdct::{
Dct1, Dct2, Dct3, Dct4, Dct5, Dct6, Dct7, Dct8, Dst1, Dst2, Dst3, Dst4, Dst5, Dst6, Dst7, Dst8,
};
use crate::common::known_data::*;
use crate::common::macros::test_mdct;
use crate::common::reference_impls::*;
use crate::common::{compare_float_vectors, random_signal};
use std::f32;
fn inverse_scale_dct1(len: usize) -> f64 {
2.0 / (len - 1) as f64
}
fn inverse_scale_dst1(len: usize) -> f64 {
2.0 / (len + 1) as f64
}
fn inverse_scale_normal(len: usize) -> f64 {
2.0 / len as f64
}
fn inverse_scale_minushalf(len: usize) -> f64 {
2.0 / (len as f64 - 0.5)
}
fn inverse_scale_plushalf(len: usize) -> f64 {
2.0 / (len as f64 + 0.5)
}
#[test]
fn test_dct1_accuracy() {
dct_test_with_known_data!(reference_dct1, Dct1Naive, process_dct1, known_values_dct1);
dct_test_with_planner!(reference_dct1, Dct1Naive, process_dct1, plan_dct1, 2);
dct_test_inverse!(reference_dct1, reference_dct1, inverse_scale_dct1, 2);
}
#[test]
fn test_dct2_accuracy() {
dct_test_with_known_data!(
reference_dct2,
Type2And3Naive,
process_dct2,
known_values_dct2
);
dct_test_with_planner!(reference_dct2, Type2And3Naive, process_dct2, plan_dct2, 1);
dct_test_inverse!(reference_dct2, reference_dct3, inverse_scale_normal, 1);
}
#[test]
fn test_dct3_accuracy() {
dct_test_with_known_data!(
reference_dct3,
Type2And3Naive,
process_dct3,
known_values_dct3
);
dct_test_with_planner!(reference_dct3, Type2And3Naive, process_dct3, plan_dct3, 1);
dct_test_inverse!(reference_dct3, reference_dct2, inverse_scale_normal, 1);
}
#[test]
fn test_dct4_accuracy() {
dct_test_with_known_data!(reference_dct4, Type4Naive, process_dct4, known_values_dct4);
dct_test_with_planner!(reference_dct4, Type4Naive, process_dct4, plan_dct4, 1);
dct_test_inverse!(reference_dct4, reference_dct4, inverse_scale_normal, 1);
}
#[test]
fn test_dct5_accuracy() {
dct_test_with_planner!(reference_dct5, Dct5Naive, process_dct5, plan_dct5, 1);
dct_test_inverse!(reference_dct5, reference_dct5, inverse_scale_minushalf, 1);
}
#[test]
fn test_dct6_accuracy() {
dct_test_with_planner!(reference_dct6, Dct6And7Naive, process_dct6, plan_dct6, 1);
dct_test_inverse!(reference_dct6, reference_dct7, inverse_scale_minushalf, 1);
}
#[test]
fn test_dct7_accuracy() {
dct_test_with_planner!(reference_dct7, Dct6And7Naive, process_dct7, plan_dct7, 1);
dct_test_inverse!(reference_dct7, reference_dct6, inverse_scale_minushalf, 1);
}
#[test]
fn test_dct8_accuracy() {
dct_test_with_planner!(reference_dct8, Dct8Naive, process_dct8, plan_dct8, 6);
dct_test_inverse!(reference_dct8, reference_dct8, inverse_scale_plushalf, 1);
}
#[test]
fn test_dst1_accuracy() {
dct_test_with_known_data!(reference_dst1, Dst1Naive, process_dst1, known_values_dst1);
dct_test_with_planner!(reference_dst1, Dst1Naive, process_dst1, plan_dst1, 1);
dct_test_inverse!(reference_dst1, reference_dst1, inverse_scale_dst1, 1);
}
#[test]
fn test_dst2_accuracy() {
dct_test_with_known_data!(
reference_dst2,
Type2And3Naive,
process_dst2,
known_values_dst2
);
dct_test_with_planner!(reference_dst2, Type2And3Naive, process_dst2, plan_dst2, 1);
dct_test_inverse!(reference_dst2, reference_dst3, inverse_scale_normal, 1);
}
#[test]
fn test_dst3_accuracy() {
dct_test_with_known_data!(
reference_dst3,
Type2And3Naive,
process_dst3,
known_values_dst3
);
dct_test_with_planner!(reference_dst3, Type2And3Naive, process_dst3, plan_dst3, 1);
dct_test_inverse!(reference_dst3, reference_dst2, inverse_scale_normal, 1);
}
#[test]
fn test_dst4_accuracy() {
dct_test_with_known_data!(reference_dst4, Type4Naive, process_dst4, known_values_dst4);
dct_test_with_planner!(reference_dst4, Type4Naive, process_dst4, plan_dst4, 1);
dct_test_inverse!(reference_dst4, reference_dst4, inverse_scale_normal, 1);
}
#[test]
fn test_dst5_accuracy() {
dct_test_with_planner!(reference_dst5, Dst5Naive, process_dst5, plan_dst5, 1);
dct_test_inverse!(reference_dst5, reference_dst5, inverse_scale_plushalf, 1);
}
#[test]
fn test_dst6_accuracy() {
dct_test_with_planner!(reference_dst6, Dst6And7Naive, process_dst6, plan_dst6, 1);
dct_test_inverse!(reference_dst6, reference_dst7, inverse_scale_plushalf, 1);
}
#[test]
fn test_dst7_accuracy() {
dct_test_with_planner!(reference_dst7, Dst6And7Naive, process_dst7, plan_dst7, 6);
dct_test_inverse!(reference_dst7, reference_dst6, inverse_scale_plushalf, 1);
}
#[test]
fn test_dst8_accuracy() {
dct_test_with_planner!(reference_dst8, Dst8Naive, process_dst8, plan_dst8, 6);
dct_test_inverse!(reference_dst8, reference_dst8, inverse_scale_minushalf, 1);
}
#[test]
fn test_mdct_accuracy() {
for curent_window_fn in &[window_fn::one, window_fn::mp3, window_fn::vorbis] {
for len in 1..10 {
test_mdct::planned_matches_naive(len * 2, curent_window_fn);
}
for &len in &[50, 52] {
test_mdct::planned_matches_naive(len * 2, curent_window_fn);
}
}
}
#[test]
fn test_mdct_tdac() {
struct TdacTestStruct<'a> {
name: &'static str,
window: &'a dyn Fn(usize) -> Vec<f32>,
scale_fn: &'a dyn Fn(usize) -> f32,
}
impl<'a> TdacTestStruct<'a> {
fn new(
name: &'static str,
window: &'a dyn Fn(usize) -> Vec<f32>,
scale_fn: &'a dyn Fn(usize) -> f32,
) -> Self {
Self {
name,
window,
scale_fn,
}
}
}
let non_window_scale = |len: usize| 1.0 / (len as f32);
let window_scale = |len: usize| 2.0 / (len as f32);
let invertible_scale = |_| 1.0;
let tests = [
TdacTestStruct::new("one", &window_fn::one, &non_window_scale),
TdacTestStruct::new("mp3", &window_fn::mp3, &window_scale),
TdacTestStruct::new("vorbis", &window_fn::vorbis, &window_scale),
TdacTestStruct::new("invertible", &window_fn::invertible, &invertible_scale),
TdacTestStruct::new(
"mp3_invertible",
&window_fn::mp3_invertible,
&invertible_scale,
),
TdacTestStruct::new(
"vorbis_invertible",
&window_fn::vorbis_invertible,
&invertible_scale,
),
];
for test_data in &tests {
for i in 1..10 {
let len = i * 2;
println!("name: {}, len: {}", test_data.name, len);
test_mdct::test_tdac(len, (test_data.scale_fn)(len), test_data.window);
}
for &i in &[50, 52] {
let len = i * 2;
println!("name: {}, len: {}", test_data.name, len);
test_mdct::test_tdac(len, (test_data.scale_fn)(len), test_data.window);
}
}
}