From edbb3e368288de76116a9001c5ab9a9938bedf8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 13 Aug 2026 13:32:07 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf(engine):=20the=20Opus=20decode?= =?UTF-8?q?r=20gets=20the=20transform=20it=20was=20written=20around?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ruopus` was asked for `std` alone, and its default `spectrograms` feature -- read here as "its own analysis tools" -- is the FFT backend of the CELT synthesis MDCT. Without it the crate evaluates an O(n²) DFT in f64 per block, and a 5.1 soundtrack decoded at 0.95x realtime: slower than playing it. Measured on the project's own fixtures, release: test_tone.opus 10.44x -> 135.01x realtime test_opus_51.mka 0.95x -> 103.80x test_opus_71.mka 0.93x -> 112.04x Not bit-identical -- FFT summation order differs -- but the two builds agree to 144 dB SNR (max |Δ| 3.7e-8) while both sit 93 dB from ffmpeg's libopus decode of the same file, so the change is 40 dB below the decoder's existing distance to the reference. --- Cargo.lock | 151 +++++++++++++++++++++++++++++++++++++++ crates/engine/Cargo.toml | 21 +++++- 2 files changed, 169 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7185421..990056d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3372,6 +3372,16 @@ dependencies = [ "libc", ] +[[package]] +name = "matrixmultiply" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7" +dependencies = [ + "autocfg", + "rawpointer", +] + [[package]] name = "maybe-rayon" version = "0.1.1" @@ -3538,6 +3548,22 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "ndarray" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "portable-atomic", + "portable-atomic-util", + "rawpointer", + "serde", +] + [[package]] name = "new_debug_unreachable" version = "1.0.6" @@ -3608,6 +3634,34 @@ dependencies = [ "memchr", ] +[[package]] +name = "non-empty-iter" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac123b8e4bd1fef3e8e972ba7a07f046ac6b608633ad761c61876b7197313f63" +dependencies = [ + "non-zero-size", +] + +[[package]] +name = "non-empty-slice" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9935c822e11be79ef40f986558b00da0ff176b7c87ddf6d9fb8b9138a6112c20" +dependencies = [ + "non-empty-iter", + "non-zero-size", + "ownership", + "serde", + "thiserror 2.0.20", +] + +[[package]] +name = "non-zero-size" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "401b3a4027292c1c318fd2305783f791d5579d18e7143ef72d653bf0c29e6b36" + [[package]] name = "noop_proc_macro" version = "0.3.0" @@ -3672,6 +3726,7 @@ checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "bytemuck", "num-traits", + "serde", ] [[package]] @@ -3951,6 +4006,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "ownership" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb1aeb68b9f2683ee3f83960c10668b37a8a640224ba1169697ee8fd1e906c1" + [[package]] name = "oxideav-ac3" version = "0.0.10" @@ -4279,6 +4340,21 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7" +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618" +dependencies = [ + "portable-atomic", +] + [[package]] name = "postage" version = "0.5.0" @@ -4324,6 +4400,15 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "primal-check" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" +dependencies = [ + "num-integer", +] + [[package]] name = "proc-macro-crate" version = "3.5.0" @@ -4700,6 +4785,12 @@ dependencies = [ "raw-window-handle", ] +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "rayon" version = "1.12.0" @@ -4731,6 +4822,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "realfft" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f821338fddb99d089116342c46e9f1fbf3828dba077674613e734e01d6ea8677" +dependencies = [ + "rustfft", +] + [[package]] name = "reborrow" version = "0.5.5" @@ -4869,6 +4969,9 @@ name = "ruopus" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcfcd963c83fc1ada2180ccdac7f1568d87fb0f2ad7236b266fdc9fb9122fbfd" +dependencies = [ + "spectrograms", +] [[package]] name = "rust-embed" @@ -4927,6 +5030,20 @@ dependencies = [ "semver", ] +[[package]] +name = "rustfft" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21db5f9893e91f41798c88680037dba611ca6674703c1a18601b01a72c8adb89" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "primal-check", + "strength_reduce", + "transpose", +] + [[package]] name = "rustix" version = "0.38.44" @@ -5506,6 +5623,24 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "spectrograms" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff1e83612f5a16e413dbfaf0381753b5eee714932fc95db19fe0974d5e6462b" +dependencies = [ + "ndarray", + "non-empty-slice", + "num-complex", + "num-traits", + "once_cell", + "pkg-config", + "realfft", + "regex", + "rustfft", + "thiserror 2.0.20", +] + [[package]] name = "spin" version = "0.9.9" @@ -5570,6 +5705,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strength_reduce" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" + [[package]] name = "strict-num" version = "0.1.1" @@ -6401,6 +6542,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "transpose" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "try-lock" version = "0.2.5" diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 4419a58..23b6059 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -81,8 +81,23 @@ rusty_mp3 = "0.6" # packet. This one does the whole pipeline. Its quirks are measured and worked # around in [`crate::export::write_ogg`] -- read that before touching this line. rusty_vorbis = "0.1.1" -# The Opus decoder: pure Rust, MIT, no C and no FFI, and no dependencies at all -# without its default features (`spectrograms` is for its own analysis tools). +# The Opus decoder: pure Rust, MIT, no C and no FFI. +# +# `spectrograms` is not, as this line long claimed, "for its own analysis tools": +# it is the crate's *FFT backend*, and it is default-on because CELT synthesis is +# an N/4 complex FFT per block. Turned off -- which is what asking for `std` +# alone did -- `celt/mdct.rs` falls back to an O(n²) DFT evaluated in f64, and a +# 5.1 film soundtrack decodes at **0.95x realtime**: slower than playing it. +# With the backend on, the same file decodes at **104x** (7.1: 0.93x -> 112x, +# stereo: 10.4x -> 135x). The cost is fifteen pure-Rust crates (`spectrograms` -> +# `realfft`/`rustfft`, `ndarray`, `regex`), still no C and no `build.rs`. +# The output is *not* bit-identical to the DFT build -- FFT summation order +# differs -- but the two agree to 144 dB SNR (max |Δ| 3.7e-8, half an LSB at +# 24-bit) while *both* sit 93 dB from ffmpeg's own libopus decode of the same +# file. The change is 40 dB below the decoder's existing distance to the +# reference, i.e. inside float noise, and upstream says as much: Opus conformance +# is a quality threshold (`opus_compare`), not bit-exactness, and every +# bitstream-affecting computation stays exact either way. # Symphonia has no Opus at any version -- its feature list stops at vorbis and # there is no `symphonia-codec-opus` crate -- while both of its containers *read* # the format already: `A_OPUS` and `OpusHead` have always mapped to @@ -99,7 +114,7 @@ rusty_vorbis = "0.1.1" # where the film is silent. This one matches ffmpeg's own decode of the same file to a # correlation of 1.0000 on all six channels. Pinned exactly, like `oxideav-ac3` # below, because a decoder is a bit-exactness dependency. -ruopus = { version = "=0.1.2", default-features = false, features = ["std"] } +ruopus = { version = "=0.1.2", default-features = false, features = ["std", "spectrograms"] } # The Opus *encoder*, the other half of that pair: a pure-Rust port of libopus # 1.6, BSD-3, `build.rs`-less and with **no dependencies at all** -- checked, not # taken from the README (`cargo tree` is two lines; there is not one `.c`, `.h`