forge
Explore
Log in

Don't you dare go Hollow

The Opus decoder gets the transform it was written around

Browse files at this commit

edbb3e368288de76116a9001c5ab9a9938bedf8eAhmet Kaan GümüşParent: 267d45b
  • perf
    • the Opus decoder gets the transform it was written around
Show full message
⚡ perf(engine): the Opus decoder gets the transform it was written around

`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.lockmodified+151 -0
@@ -3372,6 +3372,16 @@ dependencies = [
33723372 "libc",
33733373 ]
33743374
3375+[[package]]
3376+name = "matrixmultiply"
3377+version = "0.3.11"
3378+source = "registry+https://github.com/rust-lang/crates.io-index"
3379+checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7"
3380+dependencies = [
3381+ "autocfg",
3382+ "rawpointer",
3383+]
3384+
33753385 [[package]]
33763386 name = "maybe-rayon"
33773387 version = "0.1.1"
@@ -3538,6 +3548,22 @@ dependencies = [
35383548 "getrandom 0.2.17",
35393549 ]
35403550
3551+[[package]]
3552+name = "ndarray"
3553+version = "0.17.2"
3554+source = "registry+https://github.com/rust-lang/crates.io-index"
3555+checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
3556+dependencies = [
3557+ "matrixmultiply",
3558+ "num-complex",
3559+ "num-integer",
3560+ "num-traits",
3561+ "portable-atomic",
3562+ "portable-atomic-util",
3563+ "rawpointer",
3564+ "serde",
3565+]
3566+
35413567 [[package]]
35423568 name = "new_debug_unreachable"
35433569 version = "1.0.6"
@@ -3608,6 +3634,34 @@ dependencies = [
36083634 "memchr",
36093635 ]
36103636
3637+[[package]]
3638+name = "non-empty-iter"
3639+version = "0.3.1"
3640+source = "registry+https://github.com/rust-lang/crates.io-index"
3641+checksum = "ac123b8e4bd1fef3e8e972ba7a07f046ac6b608633ad761c61876b7197313f63"
3642+dependencies = [
3643+ "non-zero-size",
3644+]
3645+
3646+[[package]]
3647+name = "non-empty-slice"
3648+version = "0.6.0"
3649+source = "registry+https://github.com/rust-lang/crates.io-index"
3650+checksum = "9935c822e11be79ef40f986558b00da0ff176b7c87ddf6d9fb8b9138a6112c20"
3651+dependencies = [
3652+ "non-empty-iter",
3653+ "non-zero-size",
3654+ "ownership",
3655+ "serde",
3656+ "thiserror 2.0.20",
3657+]
3658+
3659+[[package]]
3660+name = "non-zero-size"
3661+version = "0.2.0"
3662+source = "registry+https://github.com/rust-lang/crates.io-index"
3663+checksum = "401b3a4027292c1c318fd2305783f791d5579d18e7143ef72d653bf0c29e6b36"
3664+
36113665 [[package]]
36123666 name = "noop_proc_macro"
36133667 version = "0.3.0"
@@ -3672,6 +3726,7 @@ checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
36723726 dependencies = [
36733727 "bytemuck",
36743728 "num-traits",
3729+ "serde",
36753730 ]
36763731
36773732 [[package]]
@@ -3951,6 +4006,12 @@ dependencies = [
39514006 "pin-project-lite",
39524007 ]
39534008
4009+[[package]]
4010+name = "ownership"
4011+version = "0.3.0"
4012+source = "registry+https://github.com/rust-lang/crates.io-index"
4013+checksum = "cfb1aeb68b9f2683ee3f83960c10668b37a8a640224ba1169697ee8fd1e906c1"
4014+
39544015 [[package]]
39554016 name = "oxideav-ac3"
39564017 version = "0.0.10"
@@ -4279,6 +4340,21 @@ version = "0.2.5"
42794340 source = "registry+https://github.com/rust-lang/crates.io-index"
42804341 checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7"
42814342
4343+[[package]]
4344+name = "portable-atomic"
4345+version = "1.15.0"
4346+source = "registry+https://github.com/rust-lang/crates.io-index"
4347+checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
4348+
4349+[[package]]
4350+name = "portable-atomic-util"
4351+version = "0.2.7"
4352+source = "registry+https://github.com/rust-lang/crates.io-index"
4353+checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618"
4354+dependencies = [
4355+ "portable-atomic",
4356+]
4357+
42824358 [[package]]
42834359 name = "postage"
42844360 version = "0.5.0"
@@ -4324,6 +4400,15 @@ dependencies = [
43244400 "syn 2.0.119",
43254401 ]
43264402
4403+[[package]]
4404+name = "primal-check"
4405+version = "0.3.4"
4406+source = "registry+https://github.com/rust-lang/crates.io-index"
4407+checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08"
4408+dependencies = [
4409+ "num-integer",
4410+]
4411+
43274412 [[package]]
43284413 name = "proc-macro-crate"
43294414 version = "3.5.0"
@@ -4700,6 +4785,12 @@ dependencies = [
47004785 "raw-window-handle",
47014786 ]
47024787
4788+[[package]]
4789+name = "rawpointer"
4790+version = "0.2.1"
4791+source = "registry+https://github.com/rust-lang/crates.io-index"
4792+checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
4793+
47034794 [[package]]
47044795 name = "rayon"
47054796 version = "1.12.0"
@@ -4731,6 +4822,15 @@ dependencies = [
47314822 "once_cell",
47324823 ]
47334824
4825+[[package]]
4826+name = "realfft"
4827+version = "3.5.0"
4828+source = "registry+https://github.com/rust-lang/crates.io-index"
4829+checksum = "f821338fddb99d089116342c46e9f1fbf3828dba077674613e734e01d6ea8677"
4830+dependencies = [
4831+ "rustfft",
4832+]
4833+
47344834 [[package]]
47354835 name = "reborrow"
47364836 version = "0.5.5"
@@ -4869,6 +4969,9 @@ name = "ruopus"
48694969 version = "0.1.2"
48704970 source = "registry+https://github.com/rust-lang/crates.io-index"
48714971 checksum = "fcfcd963c83fc1ada2180ccdac7f1568d87fb0f2ad7236b266fdc9fb9122fbfd"
4972+dependencies = [
4973+ "spectrograms",
4974+]
48724975
48734976 [[package]]
48744977 name = "rust-embed"
@@ -4927,6 +5030,20 @@ dependencies = [
49275030 "semver",
49285031 ]
49295032
5033+[[package]]
5034+name = "rustfft"
5035+version = "6.4.1"
5036+source = "registry+https://github.com/rust-lang/crates.io-index"
5037+checksum = "21db5f9893e91f41798c88680037dba611ca6674703c1a18601b01a72c8adb89"
5038+dependencies = [
5039+ "num-complex",
5040+ "num-integer",
5041+ "num-traits",
5042+ "primal-check",
5043+ "strength_reduce",
5044+ "transpose",
5045+]
5046+
49305047 [[package]]
49315048 name = "rustix"
49325049 version = "0.38.44"
@@ -5506,6 +5623,24 @@ dependencies = [
55065623 "windows-sys 0.61.2",
55075624 ]
55085625
5626+[[package]]
5627+name = "spectrograms"
5628+version = "2.1.3"
5629+source = "registry+https://github.com/rust-lang/crates.io-index"
5630+checksum = "3ff1e83612f5a16e413dbfaf0381753b5eee714932fc95db19fe0974d5e6462b"
5631+dependencies = [
5632+ "ndarray",
5633+ "non-empty-slice",
5634+ "num-complex",
5635+ "num-traits",
5636+ "once_cell",
5637+ "pkg-config",
5638+ "realfft",
5639+ "regex",
5640+ "rustfft",
5641+ "thiserror 2.0.20",
5642+]
5643+
55095644 [[package]]
55105645 name = "spin"
55115646 version = "0.9.9"
@@ -5570,6 +5705,12 @@ version = "1.1.0"
55705705 source = "registry+https://github.com/rust-lang/crates.io-index"
55715706 checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
55725707
5708+[[package]]
5709+name = "strength_reduce"
5710+version = "0.2.4"
5711+source = "registry+https://github.com/rust-lang/crates.io-index"
5712+checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82"
5713+
55735714 [[package]]
55745715 name = "strict-num"
55755716 version = "0.1.1"
@@ -6401,6 +6542,16 @@ dependencies = [
64016542 "once_cell",
64026543 ]
64036544
6545+[[package]]
6546+name = "transpose"
6547+version = "0.2.3"
6548+source = "registry+https://github.com/rust-lang/crates.io-index"
6549+checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e"
6550+dependencies = [
6551+ "num-integer",
6552+ "strength_reduce",
6553+]
6554+
64046555 [[package]]
64056556 name = "try-lock"
64066557 version = "0.2.5"
crates/engine/Cargo.tomlmodified+18 -3
@@ -81,8 +81,23 @@ rusty_mp3 = "0.6"
8181 # packet. This one does the whole pipeline. Its quirks are measured and worked
8282 # around in [`crate::export::write_ogg`] -- read that before touching this line.
8383 rusty_vorbis = "0.1.1"
84-# The Opus decoder: pure Rust, MIT, no C and no FFI, and no dependencies at all
85-# without its default features (`spectrograms` is for its own analysis tools).
84+# The Opus decoder: pure Rust, MIT, no C and no FFI.
85+#
86+# `spectrograms` is not, as this line long claimed, "for its own analysis tools":
87+# it is the crate's *FFT backend*, and it is default-on because CELT synthesis is
88+# an N/4 complex FFT per block. Turned off -- which is what asking for `std`
89+# alone did -- `celt/mdct.rs` falls back to an O(n²) DFT evaluated in f64, and a
90+# 5.1 film soundtrack decodes at **0.95x realtime**: slower than playing it.
91+# With the backend on, the same file decodes at **104x** (7.1: 0.93x -> 112x,
92+# stereo: 10.4x -> 135x). The cost is fifteen pure-Rust crates (`spectrograms` ->
93+# `realfft`/`rustfft`, `ndarray`, `regex`), still no C and no `build.rs`.
94+# The output is *not* bit-identical to the DFT build -- FFT summation order
95+# differs -- but the two agree to 144 dB SNR (max |Δ| 3.7e-8, half an LSB at
96+# 24-bit) while *both* sit 93 dB from ffmpeg's own libopus decode of the same
97+# file. The change is 40 dB below the decoder's existing distance to the
98+# reference, i.e. inside float noise, and upstream says as much: Opus conformance
99+# is a quality threshold (`opus_compare`), not bit-exactness, and every
100+# bitstream-affecting computation stays exact either way.
86101 # Symphonia has no Opus at any version -- its feature list stops at vorbis and
87102 # there is no `symphonia-codec-opus` crate -- while both of its containers *read*
88103 # the format already: `A_OPUS` and `OpusHead` have always mapped to
@@ -99,7 +114,7 @@ rusty_vorbis = "0.1.1"
99114 # where the film is silent. This one matches ffmpeg's own decode of the same file to a
100115 # correlation of 1.0000 on all six channels. Pinned exactly, like `oxideav-ac3`
101116 # below, because a decoder is a bit-exactness dependency.
102-ruopus = { version = "=0.1.2", default-features = false, features = ["std"] }
117+ruopus = { version = "=0.1.2", default-features = false, features = ["std", "spectrograms"] }
103118 # The Opus *encoder*, the other half of that pair: a pure-Rust port of libopus
104119 # 1.6, BSD-3, `build.rs`-less and with **no dependencies at all** -- checked, not
105120 # taken from the README (`cargo tree` is two lines; there is not one `.c`, `.h`