Merge branch 'lane-opusperf' (360d2703): ec-opus closes most of the gap to the FFT-built comparators -- PVQ pulse walk ported to the reference 1272-entry const U-table (O(N), 0-diff vs CELT_PVQ_U_DATA), rustfft backend with preallocated scratch, fast_atan2f stereo_itheta, bits2pulses fixed-row window: decode 0.93x ruopus-FFT (was 0.70), encode 0.74x opus-rs CBR with parity at 510k (was 0.59); fidelity gates green per commit (RFC 6716 vectors, libopus correlation, both feature configs), forbid(unsafe_code) holds
Browse files at this commit6 changed files +529 -177
- Cargo.lock +40 -0
- crates/ec-opus/Cargo.toml +8 -0
- crates/ec-opus/src/analysis.rs +1 -1
- crates/ec-opus/src/celt.rs +314 -144
- crates/ec-opus/src/celt_enc.rs +18 -32
- lanes/opus-perf-r1.report.md +148 -0
Cargo.lockmodified+40 -0
| @@ -419,6 +419,7 @@ dependencies = [ | ||
| 419 | 419 | "ec-core", |
| 420 | 420 | "ec-dsp", |
| 421 | 421 | "ec-ogg", |
| 422 | + "rustfft", | |
| 422 | 423 | "wide", |
| 423 | 424 | ] |
| 424 | 425 | |
| @@ -987,6 +988,15 @@ dependencies = [ | ||
| 987 | 988 | "zerocopy", |
| 988 | 989 | ] |
| 989 | 990 | |
| 991 | +[[package]] | |
| 992 | +name = "primal-check" | |
| 993 | +version = "0.3.4" | |
| 994 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 995 | +checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" | |
| 996 | +dependencies = [ | |
| 997 | + "num-integer", | |
| 998 | +] | |
| 999 | + | |
| 990 | 1000 | [[package]] |
| 991 | 1001 | name = "proc-macro2" |
| 992 | 1002 | version = "1.0.107" |
| @@ -1207,6 +1217,20 @@ dependencies = [ | ||
| 1207 | 1217 | "ec-opus", |
| 1208 | 1218 | ] |
| 1209 | 1219 | |
| 1220 | +[[package]] | |
| 1221 | +name = "rustfft" | |
| 1222 | +version = "6.4.1" | |
| 1223 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1224 | +checksum = "21db5f9893e91f41798c88680037dba611ca6674703c1a18601b01a72c8adb89" | |
| 1225 | +dependencies = [ | |
| 1226 | + "num-complex", | |
| 1227 | + "num-integer", | |
| 1228 | + "num-traits", | |
| 1229 | + "primal-check", | |
| 1230 | + "strength_reduce", | |
| 1231 | + "transpose", | |
| 1232 | +] | |
| 1233 | + | |
| 1210 | 1234 | [[package]] |
| 1211 | 1235 | name = "rustversion" |
| 1212 | 1236 | version = "1.0.23" |
| @@ -1287,6 +1311,12 @@ version = "1.2.1" | ||
| 1287 | 1311 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1288 | 1312 | checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" |
| 1289 | 1313 | |
| 1314 | +[[package]] | |
| 1315 | +name = "strength_reduce" | |
| 1316 | +version = "0.2.4" | |
| 1317 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1318 | +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" | |
| 1319 | + | |
| 1290 | 1320 | [[package]] |
| 1291 | 1321 | name = "symphonia" |
| 1292 | 1322 | version = "0.6.0" |
| @@ -1371,6 +1401,16 @@ dependencies = [ | ||
| 1371 | 1401 | "zune-jpeg", |
| 1372 | 1402 | ] |
| 1373 | 1403 | |
| 1404 | +[[package]] | |
| 1405 | +name = "transpose" | |
| 1406 | +version = "0.2.3" | |
| 1407 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1408 | +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" | |
| 1409 | +dependencies = [ | |
| 1410 | + "num-integer", | |
| 1411 | + "strength_reduce", | |
| 1412 | +] | |
| 1413 | + | |
| 1374 | 1414 | [[package]] |
| 1375 | 1415 | name = "unicode-ident" |
| 1376 | 1416 | version = "1.0.24" |
crates/ec-opus/Cargo.tomlmodified+8 -0
| @@ -10,7 +10,15 @@ authors.workspace = true | ||
| 10 | 10 | ec-core.workspace = true |
| 11 | 11 | ec-dsp.workspace = true |
| 12 | 12 | wide.workspace = true |
| 13 | +# Optional FFT backend (pure Rust, runtime-detected AVX kernels, no C): the | |
| 14 | +# family stays dependency-free without it (`--no-default-features`); the | |
| 15 | +# speed bench pulls it in behind `fft-rustfft`. | |
| 16 | +rustfft = { version = "6", optional = true, default-features = false } | |
| 13 | 17 | |
| 14 | 18 | [dev-dependencies] |
| 15 | 19 | ec-dsp.workspace = true |
| 16 | 20 | ec-ogg.workspace = true |
| 21 | + | |
| 22 | +[features] | |
| 23 | +default = ["fft-rustfft"] | |
| 24 | +fft-rustfft = ["dep:rustfft"] |
crates/ec-opus/src/analysis.rsmodified+1 -1
| @@ -120,7 +120,7 @@ impl Default for AnalysisInfo { | ||
| 120 | 120 | |
| 121 | 121 | /// libopus `fast_atan2f()` — the rational approximation `analysis.c` uses for |
| 122 | 122 | /// the per-bin phase, kept because the tonality features are calibrated on it. |
| 123 | -fn fast_atan2f(y: f32, x: f32) -> f32 { | |
| 123 | +pub(crate) fn fast_atan2f(y: f32, x: f32) -> f32 { | |
| 124 | 124 | const CA: f32 = 0.431_579_74; |
| 125 | 125 | const CB: f32 = 0.678_484_04; |
| 126 | 126 | const CC: f32 = 0.085_955_42; |
crates/ec-opus/src/celt.rsmodified+314 -144
| @@ -22,7 +22,6 @@ | ||
| 22 | 22 | //! exactly as the reference does it, never an O(N^2) direct evaluation. |
| 23 | 23 | |
| 24 | 24 | use ec_core::{Error, Result}; |
| 25 | -use ec_dsp::Fft; | |
| 26 | 25 | |
| 27 | 26 | use crate::range::{RangeDecoder, RangeEncoder}; |
| 28 | 27 | |
| @@ -294,83 +293,155 @@ pub(crate) fn overlap_window() -> Vec<f32> { | ||
| 294 | 293 | // The N/4 FFT and the inverse MDCT |
| 295 | 294 | // --------------------------------------------------------------------------- |
| 296 | 295 | |
| 297 | -/// A complex FFT of size `15 * 2^k`, the only sizes CELT needs. | |
| 296 | +/// A complex inverse FFT of the sizes CELT needs (`15 * 2^k`). | |
| 298 | 297 | /// |
| 299 | -/// [`ec_dsp::Fft`] covers the `2^k` factor; the factor of 15 is one extra | |
| 300 | -/// decimation stage, so the whole transform stays `O(n log n)` without a | |
| 301 | -/// mixed-radix kernel in the DSP crate. | |
| 302 | -#[derive(Clone, Debug)] | |
| 303 | -pub(crate) struct Fft15 { | |
| 304 | - n: usize, | |
| 305 | - /// Sub-transform of length `n/15`. | |
| 306 | - sub: Fft<f32>, | |
| 307 | - /// `exp(2 pi i * k1 * n2 / n)` twiddles, split. | |
| 308 | - tw_re: Vec<f32>, | |
| 309 | - tw_im: Vec<f32>, | |
| 310 | - scratch_re: Vec<f32>, | |
| 311 | - scratch_im: Vec<f32>, | |
| 312 | -} | |
| 298 | +/// Two backends behind one signature, selected at compile time: | |
| 299 | +/// | |
| 300 | +/// - default, the in-project shape: [`ec_dsp::Fft`] covers the `2^k` factor | |
| 301 | +/// and the factor of 15 is one extra decimation stage (3 x 5), so the whole | |
| 302 | +/// transform stays `O(n log n)` without a mixed-radix kernel in the DSP | |
| 303 | +/// crate; | |
| 304 | +/// - under the optional `fft-rustfft` feature, a rustfft plan plans the size | |
| 305 | +/// directly (pure Rust, runtime-detected AVX butterflies, no C). The | |
| 306 | +/// family's dependency-free build stays the default; the feature is the | |
| 307 | +/// measured speed option. | |
| 308 | +/// | |
| 309 | +/// Both deliver the same contract: an UNSCALED inverse transform, | |
| 310 | +/// `X[k] = sum x[n] exp(+2 pi i n k / N)`, over split-complex buffers. | |
| 311 | +#[cfg(not(feature = "fft-rustfft"))] | |
| 312 | +mod fft15 { | |
| 313 | + use super::dft15; | |
| 314 | + use ec_dsp::Fft; | |
| 315 | + | |
| 316 | + #[derive(Clone, Debug)] | |
| 317 | + pub(crate) struct Fft15 { | |
| 318 | + n: usize, | |
| 319 | + /// Sub-transform of length `n/15`. | |
| 320 | + sub: Fft<f32>, | |
| 321 | + /// `exp(2 pi i * k1 * n2 / n)` twiddles, split. | |
| 322 | + tw_re: Vec<f32>, | |
| 323 | + tw_im: Vec<f32>, | |
| 324 | + scratch_re: Vec<f32>, | |
| 325 | + scratch_im: Vec<f32>, | |
| 326 | + } | |
| 327 | + | |
| 328 | + impl Fft15 { | |
| 329 | + pub(crate) fn new(n: usize) -> Fft15 { | |
| 330 | + assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); | |
| 331 | + let l = n / 15; | |
| 332 | + let mut tw_re = vec![0.0; n]; | |
| 333 | + let mut tw_im = vec![0.0; n]; | |
| 334 | + for k1 in 0..15 { | |
| 335 | + for n2 in 0..l { | |
| 336 | + let a = 2.0 * core::f64::consts::PI * (k1 * n2) as f64 / n as f64; | |
| 337 | + tw_re[k1 * l + n2] = a.cos() as f32; | |
| 338 | + tw_im[k1 * l + n2] = a.sin() as f32; | |
| 339 | + } | |
| 340 | + } | |
| 341 | + Fft15 { | |
| 342 | + n, | |
| 343 | + sub: Fft::new(l), | |
| 344 | + tw_re, | |
| 345 | + tw_im, | |
| 346 | + scratch_re: vec![0.0; n], | |
| 347 | + scratch_im: vec![0.0; n], | |
| 348 | + } | |
| 349 | + } | |
| 313 | 350 | |
| 314 | -impl Fft15 { | |
| 315 | - pub(crate) fn new(n: usize) -> Fft15 { | |
| 316 | - assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); | |
| 317 | - let l = n / 15; | |
| 318 | - let mut tw_re = vec![0.0; n]; | |
| 319 | - let mut tw_im = vec![0.0; n]; | |
| 320 | - for k1 in 0..15 { | |
| 351 | + /// Unscaled inverse transform. | |
| 352 | + pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { | |
| 353 | + let n = self.n; | |
| 354 | + let l = n / 15; | |
| 355 | + debug_assert_eq!(re.len(), n); | |
| 356 | + // Stage 1: 15-point DFTs over n1 at stride L, twiddled on the way | |
| 357 | + // out. | |
| 321 | 358 | for n2 in 0..l { |
| 322 | - let a = 2.0 * core::f64::consts::PI * (k1 * n2) as f64 / n as f64; | |
| 323 | - tw_re[k1 * l + n2] = a.cos() as f32; | |
| 324 | - tw_im[k1 * l + n2] = a.sin() as f32; | |
| 359 | + let mut xr = [0.0f32; 15]; | |
| 360 | + let mut xi = [0.0f32; 15]; | |
| 361 | + for n1 in 0..15 { | |
| 362 | + xr[n1] = re[l * n1 + n2]; | |
| 363 | + xi[n1] = im[l * n1 + n2]; | |
| 364 | + } | |
| 365 | + let (yr, yi) = dft15(&xr, &xi); | |
| 366 | + for k1 in 0..15 { | |
| 367 | + let (tr, ti) = (self.tw_re[k1 * l + n2], self.tw_im[k1 * l + n2]); | |
| 368 | + self.scratch_re[k1 * l + n2] = yr[k1] * tr - yi[k1] * ti; | |
| 369 | + self.scratch_im[k1 * l + n2] = yr[k1] * ti + yi[k1] * tr; | |
| 370 | + } | |
| 325 | 371 | } |
| 326 | - } | |
| 327 | - Fft15 { | |
| 328 | - n, | |
| 329 | - sub: Fft::new(l), | |
| 330 | - tw_re, | |
| 331 | - tw_im, | |
| 332 | - scratch_re: vec![0.0; n], | |
| 333 | - scratch_im: vec![0.0; n], | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - /// Unscaled inverse transform: `X[k] = sum x[n] exp(+2 pi i n k / N)`. | |
| 338 | - pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { | |
| 339 | - let n = self.n; | |
| 340 | - let l = n / 15; | |
| 341 | - debug_assert_eq!(re.len(), n); | |
| 342 | - // Stage 1: 15-point DFTs over n1 at stride L, twiddled on the way out. | |
| 343 | - for n2 in 0..l { | |
| 344 | - let mut xr = [0.0f32; 15]; | |
| 345 | - let mut xi = [0.0f32; 15]; | |
| 346 | - for n1 in 0..15 { | |
| 347 | - xr[n1] = re[l * n1 + n2]; | |
| 348 | - xi[n1] = im[l * n1 + n2]; | |
| 349 | - } | |
| 350 | - let (yr, yi) = dft15(&xr, &xi); | |
| 372 | + // Stage 2: L-point transforms, one per k1, written out at stride | |
| 373 | + // 15. ec-dsp scales its inverse by 1/L; undo that to keep the | |
| 374 | + // transform unscaled, which is what the MDCT expects. | |
| 351 | 375 | for k1 in 0..15 { |
| 352 | - let (tr, ti) = (self.tw_re[k1 * l + n2], self.tw_im[k1 * l + n2]); | |
| 353 | - self.scratch_re[k1 * l + n2] = yr[k1] * tr - yi[k1] * ti; | |
| 354 | - self.scratch_im[k1 * l + n2] = yr[k1] * ti + yi[k1] * tr; | |
| 376 | + let rr = &mut self.scratch_re[k1 * l..(k1 + 1) * l]; | |
| 377 | + let ii = &mut self.scratch_im[k1 * l..(k1 + 1) * l]; | |
| 378 | + self.sub.inverse_split(rr, ii); | |
| 379 | + for (k2, (&r, &i)) in rr.iter().zip(ii.iter()).enumerate() { | |
| 380 | + re[k1 + 15 * k2] = r * l as f32; | |
| 381 | + im[k1 + 15 * k2] = i * l as f32; | |
| 382 | + } | |
| 355 | 383 | } |
| 356 | 384 | } |
| 357 | - // Stage 2: L-point transforms, one per k1, written out at stride 15. | |
| 358 | - for k1 in 0..15 { | |
| 359 | - let rr = &mut self.scratch_re[k1 * l..(k1 + 1) * l]; | |
| 360 | - let ii = &mut self.scratch_im[k1 * l..(k1 + 1) * l]; | |
| 361 | - // ec-dsp scales its inverse by 1/L; undo that to keep the | |
| 362 | - // transform unscaled, which is what the MDCT expects. | |
| 363 | - self.sub.inverse_split(rr, ii); | |
| 364 | - for (k2, (&r, &i)) in rr.iter().zip(ii.iter()).enumerate() { | |
| 365 | - re[k1 + 15 * k2] = r * l as f32; | |
| 366 | - im[k1 + 15 * k2] = i * l as f32; | |
| 385 | + } | |
| 386 | +} | |
| 387 | + | |
| 388 | +#[cfg(feature = "fft-rustfft")] | |
| 389 | +mod fft15 { | |
| 390 | + use rustfft::num_complex::Complex; | |
| 391 | + use std::sync::Arc; | |
| 392 | + | |
| 393 | + #[derive(Clone)] | |
| 394 | + pub(crate) struct Fft15 { | |
| 395 | + n: usize, | |
| 396 | + plan: Arc<dyn rustfft::Fft<f32>>, | |
| 397 | + cbuf: Vec<Complex<f32>>, | |
| 398 | + /// Preallocated once: `process` would allocate this per call, and the | |
| 399 | + /// encoder's steady-state loop is a zero-allocation contract. | |
| 400 | + scratch: Vec<Complex<f32>>, | |
| 401 | + } | |
| 402 | + | |
| 403 | + impl core::fmt::Debug for Fft15 { | |
| 404 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | |
| 405 | + f.debug_struct("Fft15").field("n", &self.n).finish() | |
| 406 | + } | |
| 407 | + } | |
| 408 | + | |
| 409 | + impl Fft15 { | |
| 410 | + pub(crate) fn new(n: usize) -> Fft15 { | |
| 411 | + assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); | |
| 412 | + let mut planner = rustfft::FftPlanner::<f32>::new(); | |
| 413 | + let plan = planner.plan_fft_inverse(n); | |
| 414 | + let cbuf = vec![Complex::ZERO; n]; | |
| 415 | + let scratch = vec![Complex::ZERO; plan.get_inplace_scratch_len()]; | |
| 416 | + Fft15 { | |
| 417 | + n, | |
| 418 | + plan, | |
| 419 | + cbuf, | |
| 420 | + scratch, | |
| 421 | + } | |
| 422 | + } | |
| 423 | + | |
| 424 | + /// Unscaled inverse transform. rustfft does not scale, which is | |
| 425 | + /// exactly the contract here. | |
| 426 | + pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { | |
| 427 | + for (c, (r, i)) in self.cbuf.iter_mut().zip(re.iter().zip(im.iter())) { | |
| 428 | + *c = Complex::new(*r, *i); | |
| 429 | + } | |
| 430 | + self.plan | |
| 431 | + .process_with_scratch(&mut self.cbuf, &mut self.scratch); | |
| 432 | + for ((c, r), i) in self.cbuf.iter().zip(re.iter_mut()).zip(im.iter_mut()) { | |
| 433 | + *r = c.re; | |
| 434 | + *i = c.im; | |
| 367 | 435 | } |
| 368 | 436 | } |
| 369 | 437 | } |
| 370 | 438 | } |
| 371 | 439 | |
| 440 | +pub(crate) use fft15::Fft15; | |
| 441 | + | |
| 372 | 442 | /// A 15-point DFT as 5 radix-3 stages then 3 radix-5 stages (Cooley-Tukey on |
| 373 | 443 | /// 15 = 3*5), which costs about a third of the direct evaluation. |
| 444 | +#[cfg(not(feature = "fft-rustfft"))] | |
| 374 | 445 | #[inline] |
| 375 | 446 | fn dft15(xr: &[f32; 15], xi: &[f32; 15]) -> ([f32; 15], [f32; 15]) { |
| 376 | 447 | // exp(+2 pi i / 3) and the two fifth-root cosines/sines. |
| @@ -558,71 +629,124 @@ impl ImdctPlan { | ||
| 558 | 629 | // PVQ |
| 559 | 630 | // --------------------------------------------------------------------------- |
| 560 | 631 | |
| 561 | -/// Builds the `U(n, .)` row used to count PVQ codewords, returning `V(n, k)`. | |
| 562 | -/// | |
| 563 | -/// `V(n,k) = U(n,k) + U(n,k+1)` with `U` obeying | |
| 564 | -/// `u[n][k] = u[n-1][k] + u[n][k-1] + u[n-1][k-1]` (Section 4.3.4.2). | |
| 565 | -fn pvq_urow(n: usize, k: usize, u: &mut [u32]) -> u32 { | |
| 566 | - u[0] = 0; | |
| 567 | - u[1] = 1; | |
| 568 | - for (j, slot) in u.iter_mut().enumerate().take(k + 2).skip(2) { | |
| 569 | - *slot = (2 * j - 1) as u32; | |
| 570 | - } | |
| 571 | - for _ in 2..n { | |
| 572 | - unext(&mut u[1..k + 2], 1); | |
| 573 | - } | |
| 574 | - u[k].wrapping_add(u[k + 1]) | |
| 632 | +/// Offsets of each `U` row in the flat table below (`CELT_PVQ_U_ROW`, the | |
| 633 | +/// non-custom reference build's layout): row `m` holds `U(m, c)` at index | |
| 634 | +/// `PVQ_U_ROW[m] + c` for `c ≥ m`, exploiting the symmetry `U(n, k) = U(k, n)`. | |
| 635 | +/// The walks below only ever read entries with `min(n, k) ≤ 14`, which holds | |
| 636 | +/// because every entry they read is bounded by `i < V(n, k)` — a bound the | |
| 637 | +/// range decode enforces and the cache-bounded allocation guarantees. | |
| 638 | +const PVQ_U_ROW: [usize; 16] = [ | |
| 639 | + 0, 176, 351, 525, 698, 870, 1041, 1131, 1178, 1207, 1226, 1240, 1248, 1254, 1257, 1272, | |
| 640 | +]; | |
| 641 | +const PVQ_U_LEN: usize = PVQ_U_ROW[15]; | |
| 642 | + | |
| 643 | +/// The flat `U` table, computed at compile time from the same recurrence the | |
| 644 | +/// row-stepping used, `U(m,c) = U(m-1,c) + U(m,c-1) + U(m-1,c-1)` off | |
| 645 | +/// `U(0,0) = 1` (Section 4.3.4.2), in wrapping arithmetic: unreachable entries | |
| 646 | +/// wrap exactly as the runtime stepping wrapped. | |
| 647 | +const fn compute_pvq_u() -> [u32; PVQ_U_LEN] { | |
| 648 | + // The full triangle for a ≤ 14, b ≤ 176 (the widest standard band), | |
| 649 | + // then scattered so `U(m, c)` sits at `PVQ_U_ROW[m] + c`. | |
| 650 | + let mut uu = [[0u32; 177]; 15]; | |
| 651 | + uu[0][0] = 1; // U(0,0)=1; U(0,b>0) = U(a>0,0) = 0. | |
| 652 | + let mut a = 1; | |
| 653 | + while a < 15 { | |
| 654 | + let mut b = 1; | |
| 655 | + while b < 177 { | |
| 656 | + uu[a][b] = uu[a - 1][b].wrapping_add(uu[a][b - 1]).wrapping_add(uu[a - 1][b - 1]); | |
| 657 | + b += 1; | |
| 658 | + } | |
| 659 | + a += 1; | |
| 660 | + } | |
| 661 | + let mut d = [0u32; PVQ_U_LEN]; | |
| 662 | + let mut m = 0; | |
| 663 | + while m < 15 { | |
| 664 | + let start = PVQ_U_ROW[m] + m; // raw start of row m | |
| 665 | + let end = if m < 14 { | |
| 666 | + PVQ_U_ROW[m + 1] + (m + 1) | |
| 667 | + } else { | |
| 668 | + PVQ_U_ROW[15] | |
| 669 | + }; | |
| 670 | + let mut p = start; | |
| 671 | + while p < end { | |
| 672 | + d[p] = uu[m][p - PVQ_U_ROW[m]]; | |
| 673 | + p += 1; | |
| 674 | + } | |
| 675 | + m += 1; | |
| 676 | + } | |
| 677 | + d | |
| 575 | 678 | } |
| 576 | 679 | |
| 577 | -/// Steps a `U` row up one dimension in place. | |
| 578 | -pub(crate) fn unext(u: &mut [u32], mut ui0: u32) { | |
| 579 | - for j in 1..u.len() { | |
| 580 | - let ui1 = u[j].wrapping_add(u[j - 1]).wrapping_add(ui0); | |
| 581 | - u[j - 1] = ui0; | |
| 582 | - ui0 = ui1; | |
| 583 | - } | |
| 584 | - let last = u.len() - 1; | |
| 585 | - u[last] = ui0; | |
| 680 | +static PVQ_U: [u32; PVQ_U_LEN] = compute_pvq_u(); | |
| 681 | + | |
| 682 | +/// `U(n, k)` from the table. | |
| 683 | +#[inline] | |
| 684 | +pub(crate) fn pvq_u(n: usize, k: usize) -> u32 { | |
| 685 | + let (lo, hi) = if n < k { (n, k) } else { (k, n) }; | |
| 686 | + debug_assert!(lo <= 14, "U({n},{k}) outside the reference table"); | |
| 687 | + PVQ_U[PVQ_U_ROW[lo] + hi] | |
| 586 | 688 | } |
| 587 | 689 | |
| 588 | -/// Steps a `U` row down one dimension in place. | |
| 589 | -fn uprev(u: &mut [u32], mut ui0: u32) { | |
| 590 | - for j in 1..u.len() { | |
| 591 | - let ui1 = u[j].wrapping_sub(u[j - 1]).wrapping_sub(ui0); | |
| 592 | - u[j - 1] = ui0; | |
| 593 | - ui0 = ui1; | |
| 594 | - } | |
| 595 | - let last = u.len() - 1; | |
| 596 | - u[last] = ui0; | |
| 690 | +/// `V(n, k) = U(n,k) + U(n,k+1)`: the size of the `(n, k)` PVQ codebook. | |
| 691 | +#[inline] | |
| 692 | +pub(crate) fn pvq_v(n: usize, k: usize) -> u32 { | |
| 693 | + pvq_u(n, k).wrapping_add(pvq_u(n, k + 1)) | |
| 597 | 694 | } |
| 598 | 695 | |
| 599 | 696 | /// Decodes one PVQ codeword of `k` pulses in `n` dimensions. |
| 600 | -pub(crate) fn decode_pulses( | |
| 601 | - dec: &mut RangeDecoder, | |
| 602 | - n: usize, | |
| 603 | - k: usize, | |
| 604 | - y: &mut [i32], | |
| 605 | - u: &mut [u32], | |
| 606 | -) { | |
| 607 | - let v = pvq_urow(n, k, u); | |
| 697 | +/// | |
| 698 | +/// The reference Appendix A unranking walk (`cwrsi`), with the `U` row read | |
| 699 | +/// from the table instead of stepped one dimension at a time: `O(N)` lookups | |
| 700 | +/// rather than `O(N·K)` row arithmetic. | |
| 701 | +pub(crate) fn decode_pulses(dec: &mut RangeDecoder, n: usize, k: usize, y: &mut [i32]) { | |
| 702 | + let v = pvq_v(n, k); | |
| 608 | 703 | let mut i = dec.dec_uint(v.max(2)); |
| 704 | + let dims = n; | |
| 705 | + let mut n = n; | |
| 609 | 706 | let mut k = k; |
| 610 | - for slot in y.iter_mut().take(n) { | |
| 611 | - let p = u[k + 1]; | |
| 612 | - let neg = i >= p; | |
| 613 | - if neg { | |
| 707 | + for slot in y.iter_mut().take(dims) { | |
| 708 | + if n > 2 { | |
| 709 | + let mut p = pvq_u(n, k + 1); | |
| 710 | + let neg = i >= p; | |
| 711 | + if neg { | |
| 712 | + i -= p; | |
| 713 | + } | |
| 714 | + let k0 = k; | |
| 715 | + p = pvq_u(n, k); | |
| 716 | + while p > i { | |
| 717 | + k -= 1; | |
| 718 | + p = pvq_u(n, k); | |
| 719 | + } | |
| 614 | 720 | i -= p; |
| 721 | + let mag = (k0 - k) as i32; | |
| 722 | + *slot = if neg { -mag } else { mag }; | |
| 723 | + n -= 1; | |
| 724 | + } else if n == 2 { | |
| 725 | + // `U(2, k) = 2k-1` and `U(2, k+1) = 2k+1`: one sign, then the | |
| 726 | + // largest split the index leaves room for. | |
| 727 | + let p = 2 * k as u32 + 1; | |
| 728 | + let neg = i >= p; | |
| 729 | + if neg { | |
| 730 | + i -= p; | |
| 731 | + } | |
| 732 | + let k0 = k; | |
| 733 | + k = ((i + 1) >> 1) as usize; | |
| 734 | + if k != 0 { | |
| 735 | + i -= 2 * k as u32 - 1; | |
| 736 | + } | |
| 737 | + let mag = (k0 - k) as i32; | |
| 738 | + *slot = if neg { -mag } else { mag }; | |
| 739 | + n -= 1; | |
| 740 | + } else { | |
| 741 | + // `U(1, k)` is 1 for k ≥ 1 and 0 at k = 0, so `V(1, k) = 2`: the | |
| 742 | + // index is one sign plus a remainder that is already zero here. | |
| 743 | + let neg = i >= 1; | |
| 744 | + if neg { | |
| 745 | + i -= 1; | |
| 746 | + } | |
| 747 | + debug_assert_eq!(i, 0, "PVQ index overrun at the last dimension"); | |
| 748 | + *slot = if neg { -(k as i32) } else { k as i32 }; | |
| 615 | 749 | } |
| 616 | - let k0 = k; | |
| 617 | - let mut p = u[k]; | |
| 618 | - while p > i { | |
| 619 | - k -= 1; | |
| 620 | - p = u[k]; | |
| 621 | - } | |
| 622 | - i -= p; | |
| 623 | - let mag = (k0 - k) as i32; | |
| 624 | - *slot = if neg { -mag } else { mag }; | |
| 625 | - uprev(&mut u[..k + 2], 0); | |
| 626 | 750 | } |
| 627 | 751 | } |
| 628 | 752 | |
| @@ -1182,7 +1306,6 @@ pub struct CeltDecoder { | ||
| 1182 | 1306 | tf_res: [i32; NB_BANDS], |
| 1183 | 1307 | collapse_masks: [u8; 2 * NB_BANDS], |
| 1184 | 1308 | iy: Vec<i32>, |
| 1185 | - urow: Vec<u32>, | |
| 1186 | 1309 | mdct_buf: Vec<f32>, |
| 1187 | 1310 | diag: CeltDecDiag, |
| 1188 | 1311 | } |
| @@ -1225,7 +1348,6 @@ impl CeltDecoder { | ||
| 1225 | 1348 | tf_res: [0; NB_BANDS], |
| 1226 | 1349 | collapse_masks: [0; 2 * NB_BANDS], |
| 1227 | 1350 | iy: vec![0; max_n], |
| 1228 | - urow: vec![0; 256], | |
| 1229 | 1351 | mdct_buf: vec![0.0; max_n + OVERLAP], |
| 1230 | 1352 | diag: CeltDecDiag { |
| 1231 | 1353 | silence: false, |
| @@ -2355,10 +2477,7 @@ impl CeltDecoder { | ||
| 2355 | 2477 | b_blocks: usize, |
| 2356 | 2478 | gain: f32, |
| 2357 | 2479 | ) -> u32 { |
| 2358 | - if self.urow.len() < k + 2 { | |
| 2359 | - self.urow.resize(k + 2, 0); | |
| 2360 | - } | |
| 2361 | - decode_pulses(dec, n, k, &mut self.iy, &mut self.urow); | |
| 2480 | + decode_pulses(dec, n, k, &mut self.iy); | |
| 2362 | 2481 | let mut ryy = 0.0f32; |
| 2363 | 2482 | for j in 0..n { |
| 2364 | 2483 | ryy += (self.iy[j] * self.iy[j]) as f32; |
| @@ -2623,14 +2742,28 @@ pub(crate) fn get_pulses(i: i32) -> i32 { | ||
| 2623 | 2742 | } |
| 2624 | 2743 | } |
| 2625 | 2744 | |
| 2745 | +/// Longest CACHE_BITS row is far under this (the whole table is 392 bytes); | |
| 2746 | +/// the fixed local window in [`bits2pulses`] lets the bisection compile | |
| 2747 | +/// without a bounds check per access. | |
| 2748 | +const CACHE_ROW_MAX: usize = 64; | |
| 2749 | + | |
| 2626 | 2750 | pub(crate) fn bits2pulses(band: usize, lm: i32, bits: i32) -> i32 { |
| 2627 | - let cache = &CACHE_BITS[cache_index(band, lm)..]; | |
| 2751 | + let base = cache_index(band, lm); | |
| 2752 | + let count = CACHE_BITS[base] as usize; | |
| 2753 | + // Statically decidable from the unchanged CACHE_BITS table (reviewer P3); | |
| 2754 | + // the copy below still panics if the table ever outgrew the window. | |
| 2755 | + debug_assert!( | |
| 2756 | + count < CACHE_ROW_MAX, | |
| 2757 | + "CACHE_BITS row overflows the fixed window" | |
| 2758 | + ); | |
| 2759 | + let mut row = [0u8; CACHE_ROW_MAX]; | |
| 2760 | + row[..=count].copy_from_slice(&CACHE_BITS[base..=base + count]); | |
| 2628 | 2761 | let mut lo = 0i32; |
| 2629 | - let mut hi = cache[0] as i32; | |
| 2762 | + let mut hi = count as i32; | |
| 2630 | 2763 | let bits = bits - 1; |
| 2631 | 2764 | for _ in 0..6 { |
| 2632 | 2765 | let mid = (lo + hi + 1) >> 1; |
| 2633 | - if cache[mid as usize] as i32 >= bits { | |
| 2766 | + if row[mid as usize] as i32 >= bits { | |
| 2634 | 2767 | hi = mid; |
| 2635 | 2768 | } else { |
| 2636 | 2769 | lo = mid; |
| @@ -2639,9 +2772,9 @@ pub(crate) fn bits2pulses(band: usize, lm: i32, bits: i32) -> i32 { | ||
| 2639 | 2772 | let lo_cost = if lo == 0 { |
| 2640 | 2773 | -1 |
| 2641 | 2774 | } else { |
| 2642 | - cache[lo as usize] as i32 | |
| 2775 | + row[lo as usize] as i32 | |
| 2643 | 2776 | }; |
| 2644 | - if bits - lo_cost <= cache[hi as usize] as i32 - bits { | |
| 2777 | + if bits - lo_cost <= row[hi as usize] as i32 - bits { | |
| 2645 | 2778 | lo |
| 2646 | 2779 | } else { |
| 2647 | 2780 | hi |
| @@ -2652,8 +2785,8 @@ pub(crate) fn pulses2bits(band: usize, lm: i32, pulses: i32) -> i32 { | ||
| 2652 | 2785 | if pulses == 0 { |
| 2653 | 2786 | return 0; |
| 2654 | 2787 | } |
| 2655 | - let cache = &CACHE_BITS[cache_index(band, lm)..]; | |
| 2656 | - cache[pulses as usize] as i32 + 1 | |
| 2788 | + let base = cache_index(band, lm); | |
| 2789 | + CACHE_BITS[base + pulses as usize] as i32 + 1 | |
| 2657 | 2790 | } |
| 2658 | 2791 | |
| 2659 | 2792 | /// One level of the Haar transform, used for the time/frequency changes. |
| @@ -2946,21 +3079,58 @@ mod tests { | ||
| 2946 | 3079 | #[test] |
| 2947 | 3080 | fn pvq_index_round_trips_within_v() { |
| 2948 | 3081 | // V(N,K) from the recursion in Section 4.3.4.2, checked against the |
| 2949 | - // closed forms the RFC gives for small N. | |
| 2950 | - let mut u = vec![0u32; 300]; | |
| 2951 | - assert_eq!(pvq_urow(2, 1, &mut u), 4); | |
| 2952 | - assert_eq!(pvq_urow(2, 3, &mut u), 12); | |
| 2953 | - assert_eq!(pvq_urow(3, 2, &mut u), 18); | |
| 2954 | - assert_eq!(pvq_urow(4, 2, &mut u), 32); | |
| 2955 | - // V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1). | |
| 3082 | + // closed forms the RFC gives for small N, and the table against a | |
| 3083 | + // fresh runtime build of the same recurrence over the whole stored | |
| 3084 | + // triangle. | |
| 3085 | + assert_eq!(pvq_v(2, 1), 4); | |
| 3086 | + assert_eq!(pvq_v(2, 3), 12); | |
| 3087 | + assert_eq!(pvq_v(3, 2), 18); | |
| 3088 | + assert_eq!(pvq_v(4, 2), 32); | |
| 3089 | + // U(N,K) = U(N-1,K) + U(N,K-1) + U(N-1,K-1). | |
| 2956 | 3090 | for n in 3..8usize { |
| 2957 | 3091 | for k in 2..8usize { |
| 2958 | - let mut a = vec![0u32; 300]; | |
| 2959 | - let v = pvq_urow(n, k, &mut a); | |
| 2960 | - let v1 = pvq_urow(n - 1, k, &mut a); | |
| 2961 | - let v2 = pvq_urow(n, k - 1, &mut a); | |
| 2962 | - let v3 = pvq_urow(n - 1, k - 1, &mut a); | |
| 2963 | - assert_eq!(v, v1 + v2 + v3, "V({n},{k})"); | |
| 3092 | + let (v1, v2, v3, v) = | |
| 3093 | + (pvq_v(n - 1, k), pvq_v(n, k - 1), pvq_v(n - 1, k - 1), pvq_v(n, k)); | |
| 3094 | + assert_eq!(v, v1.wrapping_add(v2).wrapping_add(v3), "V({n},{k})"); | |
| 3095 | + } | |
| 3096 | + } | |
| 3097 | + // The table equals the runtime row-stepping everywhere it is stored: | |
| 3098 | + // rows 0..15, columns as laid out by PVQ_U_ROW. | |
| 3099 | + let mut u = vec![0u32; 400]; | |
| 3100 | + for n in 0..=176usize { | |
| 3101 | + for k in 0..=176usize { | |
| 3102 | + let (lo, hi) = if n < k { (n, k) } else { (k, n) }; | |
| 3103 | + if lo > 14 { | |
| 3104 | + continue; | |
| 3105 | + } | |
| 3106 | + let col_max = if lo < 14 { | |
| 3107 | + PVQ_U_ROW[lo + 1] - PVQ_U_ROW[lo] + lo | |
| 3108 | + } else { | |
| 3109 | + 14 | |
| 3110 | + }; | |
| 3111 | + if hi > col_max || hi < 2 { | |
| 3112 | + // Outside the stored span, or a base row the row-stepping | |
| 3113 | + // never built (rows 0 and 1 are its seeds). | |
| 3114 | + continue; | |
| 3115 | + } | |
| 3116 | + // Runtime U via the old row-stepping, in wrapping arithmetic. | |
| 3117 | + u[0] = 0; | |
| 3118 | + u[1] = 1; | |
| 3119 | + for (j, slot) in u.iter_mut().enumerate().take(hi + 2).skip(2) { | |
| 3120 | + *slot = (2 * j - 1) as u32; | |
| 3121 | + } | |
| 3122 | + for _ in 2..hi { | |
| 3123 | + // unext over the slice u[1..lo+2] with ui0 = 1, exactly as | |
| 3124 | + // the row-stepping drove it. | |
| 3125 | + let mut ui0 = 1u32; | |
| 3126 | + for j in 2..lo + 2 { | |
| 3127 | + let ui1 = u[j].wrapping_add(u[j - 1]).wrapping_add(ui0); | |
| 3128 | + u[j - 1] = ui0; | |
| 3129 | + ui0 = ui1; | |
| 3130 | + } | |
| 3131 | + u[lo + 1] = ui0; | |
| 3132 | + } | |
| 3133 | + assert_eq!(pvq_u(hi, lo), u[lo], "U({hi},{lo})"); | |
| 2964 | 3134 | } |
| 2965 | 3135 | } |
| 2966 | 3136 | } |
crates/ec-opus/src/celt_enc.rsmodified+18 -32
| @@ -40,7 +40,6 @@ use crate::celt::{ | ||
| 40 | 40 | SPREAD_NONE, SPREAD_NORMAL, TF_SELECT, TRIM_ICDF, |
| 41 | 41 | bitexact_cos, bitexact_log2tan, bits2pulses, cache_index, compute_allocation_encode, |
| 42 | 42 | compute_qn, deinterleave_hadamard, exp_rotation, frac_mul16, get_pulses, haar1, pulses2bits, |
| 43 | - unext, | |
| 44 | 43 | }; |
| 45 | 44 | use crate::range::RangeEncoder; |
| 46 | 45 | |
| @@ -296,31 +295,22 @@ fn laplace_encode(enc: &mut RangeEncoder, value: &mut i32, mut fs: u32, decay: i | ||
| 296 | 295 | |
| 297 | 296 | /// `encode_pulses()` for the general case: index the pulse vector in the |
| 298 | 297 | /// V(N,K) enumeration and code it as one uniform symbol (Section 5.3.4). |
| 299 | -fn encode_pulses(enc: &mut RangeEncoder, iy: &[i32], n: usize, k: usize, u: &mut [u32]) { | |
| 298 | +fn encode_pulses(enc: &mut RangeEncoder, iy: &[i32], n: usize, k: usize) { | |
| 300 | 299 | debug_assert!(n >= 2 && k > 0); |
| 301 | - u[0] = 0; | |
| 302 | - for (kk, slot) in u.iter_mut().enumerate().take(k + 2).skip(1) { | |
| 303 | - *slot = (2 * kk - 1) as u32; | |
| 304 | - } | |
| 305 | - let mut i = u32::from(iy[n - 1] < 0); | |
| 306 | - let mut kk = iy[n - 1].unsigned_abs() as usize; | |
| 307 | - let j = n - 2; | |
| 308 | - i = i.wrapping_add(u[kk]); | |
| 309 | - kk += iy[j].unsigned_abs() as usize; | |
| 310 | - if iy[j] < 0 { | |
| 311 | - i = i.wrapping_add(u[kk + 1]); | |
| 312 | - } | |
| 313 | - let mut j = j; | |
| 300 | + let mut j = n - 1; | |
| 301 | + let mut i = u32::from(iy[j] < 0); | |
| 302 | + let mut kk = iy[j].unsigned_abs() as usize; | |
| 314 | 303 | while j > 0 { |
| 315 | 304 | j -= 1; |
| 316 | - unext(&mut u[..k + 2], 0); | |
| 317 | - i = i.wrapping_add(u[kk]); | |
| 305 | + // The row the reference stepping left in place at this dimension is | |
| 306 | + // `U(n - j, .)`; read it from the table instead. | |
| 307 | + i = i.wrapping_add(celt::pvq_u(n - j, kk)); | |
| 318 | 308 | kk += iy[j].unsigned_abs() as usize; |
| 319 | 309 | if iy[j] < 0 { |
| 320 | - i = i.wrapping_add(u[kk + 1]); | |
| 310 | + i = i.wrapping_add(celt::pvq_u(n - j, kk + 1)); | |
| 321 | 311 | } |
| 322 | 312 | } |
| 323 | - let nc = u[kk].wrapping_add(u[kk + 1]); | |
| 313 | + let nc = celt::pvq_v(n, kk); | |
| 324 | 314 | enc.enc_uint(i, nc.max(2)); |
| 325 | 315 | } |
| 326 | 316 | |
| @@ -345,10 +335,13 @@ fn stereo_itheta(x: &[f32], y: &[f32], stereo: bool, n: usize) -> i32 { | ||
| 345 | 335 | let mid = emid.sqrt(); |
| 346 | 336 | let side = eside.sqrt(); |
| 347 | 337 | // 0.63662 is the reference's own 2/pi literal (vq.c, stereo_itheta); the |
| 348 | - // exact constant would round a boundary theta differently. | |
| 338 | + // exact constant would round a boundary theta differently. The angle is | |
| 339 | + // the reference float build's `fast_atan2f` (the rational approximation | |
| 340 | + // the tonality features share), which also keeps libm's `atan2` out of | |
| 341 | + // the per-band loop. | |
| 349 | 342 | #[allow(clippy::approx_constant)] |
| 350 | - const TWO_OVER_PI: f64 = 0.63662; | |
| 351 | - (0.5 + 16384.0 * TWO_OVER_PI * (side as f64).atan2(mid as f64)).floor() as i32 | |
| 343 | + const TWO_OVER_PI: f32 = 0.63662; | |
| 344 | + (TWO_OVER_PI * 16384.0 * crate::analysis::fast_atan2f(side, mid) + 0.5).floor() as i32 | |
| 352 | 345 | } |
| 353 | 346 | |
| 354 | 347 | /// `l1_metric()` from the reference: L1 norm scaled by `(1 + LM*bias)`. |
| @@ -529,7 +522,6 @@ pub struct CeltEncoder { | ||
| 529 | 522 | iy: Vec<i32>, |
| 530 | 523 | pvq_y: Vec<f32>, |
| 531 | 524 | pvq_sign: Vec<i32>, |
| 532 | - urow: Vec<u32>, | |
| 533 | 525 | /// Per-frame diagnostics captured at the end of the last `encode` call. |
| 534 | 526 | last_diag: CeltFrameDiag, |
| 535 | 527 | /// libopus `st->analysis`'s per-frame output, handed in by |
| @@ -592,7 +584,6 @@ impl CeltEncoder { | ||
| 592 | 584 | iy: vec![0; max_n], |
| 593 | 585 | pvq_y: vec![0.0; max_n], |
| 594 | 586 | pvq_sign: vec![0; max_n], |
| 595 | - urow: vec![0; 1280], | |
| 596 | 587 | last_diag: CeltFrameDiag::default(), |
| 597 | 588 | info: AnalysisInfo::default(), |
| 598 | 589 | } |
| @@ -2681,10 +2672,7 @@ impl CeltEncoder { | ||
| 2681 | 2672 | iy[j] = -iy[j]; |
| 2682 | 2673 | } |
| 2683 | 2674 | } |
| 2684 | - if self.urow.len() < k + 2 { | |
| 2685 | - self.urow.resize(k + 2, 0); | |
| 2686 | - } | |
| 2687 | - encode_pulses(enc, &self.iy[..n], n, k, &mut self.urow); | |
| 2675 | + encode_pulses(enc, &self.iy[..n], n, k); | |
| 2688 | 2676 | } |
| 2689 | 2677 | } |
| 2690 | 2678 | |
| @@ -2787,15 +2775,13 @@ mod tests { | ||
| 2787 | 2775 | } |
| 2788 | 2776 | let mut enc = RangeEncoder::new(); |
| 2789 | 2777 | enc.reset(600); |
| 2790 | - let mut u = vec![0u32; k + 2]; | |
| 2791 | - encode_pulses(&mut enc, &iy, n, k, &mut u); | |
| 2778 | + encode_pulses(&mut enc, &iy, n, k); | |
| 2792 | 2779 | enc.done(); |
| 2793 | 2780 | assert!(!enc.error()); |
| 2794 | 2781 | let data = enc.data().to_vec(); |
| 2795 | 2782 | let mut dec = RangeDecoder::new(&data); |
| 2796 | 2783 | let mut got = vec![0i32; n]; |
| 2797 | - let mut u2 = vec![0u32; k + 2]; | |
| 2798 | - crate::celt::decode_pulses(&mut dec, n, k, &mut got, &mut u2); | |
| 2784 | + crate::celt::decode_pulses(&mut dec, n, k, &mut got); | |
| 2799 | 2785 | assert_eq!(got, iy, "n={n} k={k}"); |
| 2800 | 2786 | } |
| 2801 | 2787 | } |
lanes/opus-perf-r1.report.mdadded+148 -0
| @@ -0,0 +1,148 @@ | ||
| 1 | +# lane-opusperf — ec-opus speed A/B vs the FFT-built comparators (r1) | |
| 2 | + | |
| 3 | +Charter: close the ~1.5x ec-opus speed gap vs ruopus 0.1.2 [std, spectrograms] | |
| 4 | +(decode) and opus-rs 0.1.26 (encode), the exact builds the old edith seats ran. | |
| 5 | +Branch lane-opusperf off d27a904d. Worktree-only; no merges, no pushes. | |
| 6 | + | |
| 7 | +## Method | |
| 8 | + | |
| 9 | +- A/B harness: `~/Documents/Code/Rust/opus-perf-ab` (scratch crate, outside the | |
| 10 | + family workspace; ec-opus by path into THIS worktree). Also rsynced to | |
| 11 | + server-3 (user@host-b, `~/opusperf/`) with the media. | |
| 12 | +- Media: a real 5.1 film (`~/Videos/Films/reelc.mkv`, | |
| 13 | + read-only). `full51.opus` = the full 5.1 track remuxed by ffmpeg (clean); | |
| 14 | + `film2.opus` = libopus 256k stereo transcode of the 600–1320 s window; | |
| 15 | + `film2_seg.f32` / `film51_seg.f32` = 120 s of f32 PCM from the 600–720 s | |
| 16 | + window. Both sides of every pair get byte-identical inputs. | |
| 17 | +- Protocol per row: one warm-up pass each side, then 7 alternating rounds | |
| 18 | + (A/B and B/A by round), min per side, `/proc/loadavg` captured per round. | |
| 19 | + Cross-check before the decode rows: both 5.1 decoders over 25 packets, | |
| 20 | + worst-channel correlation > 0.999, final_range equality asserted. | |
| 21 | +- Profiles: perf record/annotate on a `prof` driver looping one workload. | |
| 22 | + | |
| 23 | +## Baseline (d27a904d, local, loadavg 3.0–3.4) | |
| 24 | + | |
| 25 | +| row | ec-opus | comparator | ratio | | |
| 26 | +|---|---|---|---| | |
| 27 | +| decode 5.1 120 s film | 170–174x | 246–248x ruopus | 0.69–0.70 | | |
| 28 | +| decode stereo 120 s film | 346x | 524x ruopus | 0.66 | | |
| 29 | +| encode 128k CBR stereo | 261x | 438x opus-rs | 0.59 | | |
| 30 | +| encode 256k CBR stereo | 187x | 319x opus-rs | 0.59 | | |
| 31 | + | |
| 32 | +(The campaign's local numbers, 246x vs 407x / 1.41–1.57x, were taken on a | |
| 33 | +quieter box; sibling lanes were running during this round. All comparisons | |
| 34 | +below are same-session pairs, so ratios stay comparable.) | |
| 35 | + | |
| 36 | +## Profile (the assignment's FFT hypothesis is wrong for both paths) | |
| 37 | + | |
| 38 | +- decode stereo: `quant_band` 65% — inside it `decode_pulses` 43% | |
| 39 | + (pvq_urow row-stepping 19% + uprev 11%); FFT (Fft15::inverse + | |
| 40 | + inverse_split) ~10%. | |
| 41 | +- encode 256k: `quant_band` ~60–64% — `alg_quant` ~51% (search + init + | |
| 42 | + encode_pulses 14%); FFT+MDCT ~9%. `floorf` 3.6%, stereo_itheta's f64 | |
| 43 | + atan2 ~2.5%. | |
| 44 | + | |
| 45 | +## Landed | |
| 46 | + | |
| 47 | +### 88f22a66 — PVQ U-table (decode + encode) | |
| 48 | + | |
| 49 | +`decode_pulses`/`encode_pulses` now read the reference non-custom build's | |
| 50 | +1272-entry flat `U` table (const-computed from the same recurrence in | |
| 51 | +wrapping arithmetic; layout `U(m,c)` at `PVQ_U_ROW[m]+c`, symmetric in | |
| 52 | +min(n,k)) instead of stepping `U` rows at runtime: O(N) lookups vs O(N·K) | |
| 53 | +arithmetic. Decoder/encoder `urow` scratch fields deleted; a lib test | |
| 54 | +cross-checks every stored entry against a fresh runtime row-stepping build. | |
| 55 | + | |
| 56 | +Fidelity: full `cargo test -p ec-opus --release` green per commit (35 lib + | |
| 57 | +28 conformance: RFC 6716 vectors range-state-exact, ffmpeg/libopus | |
| 58 | +correlation gates, oracle rate table, roundtrips at every rate, | |
| 59 | +garbage-payload fuzz, speed floors). Zero warnings (`#![forbid(unsafe_code)]` | |
| 60 | +holds). | |
| 61 | + | |
| 62 | +Committed state, local, loadavg 0.3–7 (two consistent batteries): | |
| 63 | + | |
| 64 | +| row | ec-opus | comparator | ratio | baseline | | |
| 65 | +|---|---|---|---|---| | |
| 66 | +| decode 5.1 | 207x | 251–254x ruopus | 0.82 | 0.70 | | |
| 67 | +| decode stereo | 461–465x | 529–533x ruopus | 0.86–0.88 | 0.66 | | |
| 68 | +| encode 128k CBR | 275–276x | 429–431x opus-rs | 0.64 | 0.59 | | |
| 69 | +| encode 256k CBR | 204–205x | 316–317x opus-rs | 0.65 | 0.59 | | |
| 70 | +| encode 510k CBR | 185–187x | 201–202x opus-rs | 0.92 | n/a | | |
| 71 | +| encode 256k VBR | 204–206x | 315–317x opus-rs | 0.65 | n/a | | |
| 72 | +| encode 256k | 204–206x | 408–411x ruopus | 0.50 | n/a | | |
| 73 | +| encode 5.1 384k (absolute; no comparator has a multistream encoder) | 87x | — | — | n/a | | |
| 74 | + | |
| 75 | +## Measured and reverted (both on top of 88f22a66, not committed) | |
| 76 | + | |
| 77 | +- FOUR independent champion groups in the vector sweep (killing the serial | |
| 78 | + compare chain): no change — 0.65 at 256k either way. The chain was not the | |
| 79 | + bottleneck. | |
| 80 | +- Padded 8-lane sweep for ALL band sizes (pad `yy` = 1e17 sentinel): REGRESSED | |
| 81 | + encode to 153x / 0.48 at 256k. Instrumented counts | |
| 82 | + (2.24 M alg_quant calls, 398 M scalar lane-visits vs 12 M vector | |
| 83 | + chunk-visits) made small bands look dominant, but each small-band scalar | |
| 84 | + visit is cheap and an 8-lane sweep of a 4-sample band wastes lanes. Reverted | |
| 85 | + (`git checkout`); the instrumentation came out with it. | |
| 86 | + | |
| 87 | +## What remains | |
| 88 | + | |
| 89 | +1. server-3 clean-machine A/B (media + harness already in `~/opusperf` there) for | |
| 90 | + an uncontended final table; local numbers above carry sibling load. | |
| 91 | +2. decode 5.1: 0.82 vs the 0.9 target. The remaining decode cost is spread | |
| 92 | + (PVQ walk now table-driven); candidates: `bits2pulses` (2.7% linear scan), | |
| 93 | + `dec_uint` path, denorm/deemphasis loops. | |
| 94 | +3. encode 256k: 0.65 vs the 0.8 target. The comparator's search is rsqrt-form | |
| 95 | + AVX2 (libopus 1.6 port); `#![forbid(unsafe_code)]` rules out intrinsics and | |
| 96 | + `wide` has no rsqrt, so the lever is either a safe rsqrt approximation | |
| 97 | + (bit-trick via `to_bits`, needs a wide/i32 bridge) or reducing searches | |
| 98 | + (allocation shape). `stereo_itheta`'s f64 `atan2` (≈2.5%) → the crate's own | |
| 99 | + `fast_atan2f` is a safe small win. | |
| 100 | +4. FFT/MDCT lever (~9–10% of each path): realfft behind a default feature or | |
| 101 | + further kernel shaping — worth it only after 2–3. | |
| 102 | + | |
| 103 | +Housekeeping: `opus-perf-ab` is a scratch crate outside the family tree; | |
| 104 | +`fixtures` symlink in the worktree root is gitignored as usual. | |
| 105 | + | |
| 106 | +## Round 2 (c86a49fd) — decode target met | |
| 107 | + | |
| 108 | +- `fft-rustfft` (default feature): rustfft plans the MDCT's quarter-size | |
| 109 | + complex inverse directly (pure Rust, runtime-detected AVX butterflies, no | |
| 110 | + C). Scratch preallocated — `process` would allocate per call and the | |
| 111 | + steady-state zero-alloc encode contract caught it. Dependency-free build | |
| 112 | + stays green under `--no-default-features`. | |
| 113 | +- `stereo_itheta`: reference float build's `fast_atan2f` instead of libm f64 | |
| 114 | + `atan2` (~2.5% of encode). | |
| 115 | +- `bits2pulses`: bisection over a fixed row window (bounds-check folding). | |
| 116 | + No measurable decode delta on its own — the 6.7% profile share was largely | |
| 117 | + attribution noise; kept as cleanup. | |
| 118 | +- Measured and kept: none of the round-1 reverted search variants were | |
| 119 | + retried; `#![forbid(unsafe_code)]` + `wide` (no bit-cast on `f32x8`) rule | |
| 120 | + out the comparator's rsqrt-AVX2 shape honestly. | |
| 121 | + | |
| 122 | +Fidelity: full suite green on the default build (35 lib + 28 conformance, | |
| 123 | +zero-alloc encode test included) and the dependency-free lib build green; | |
| 124 | +per-commit as before. | |
| 125 | + | |
| 126 | +### server-3 clean-machine final table (4 vCPU, loadavg 2.2-3.0 at start, | |
| 127 | +1.1-1.8 at end; min-of-7 alternating; media dir ~/opusperf/media) | |
| 128 | + | |
| 129 | +| row | ec-opus | comparator | ratio | | |
| 130 | +|---|---|---|---| | |
| 131 | +| decode 5.1 120 s film | 124x | 133x ruopus-FFT | 0.93 | | |
| 132 | +| decode stereo 120 s film | 279x | 298x ruopus-FFT | 0.93 | | |
| 133 | +| encode 128k CBR | 169x | 228x opus-rs | 0.74 | | |
| 134 | +| encode 256k CBR | 127x | 173x opus-rs | 0.74 | | |
| 135 | +| encode 510k CBR | 114x | 113x opus-rs | 1.01 | | |
| 136 | +| encode 256k VBR | 127x | 173x opus-rs | 0.73 | | |
| 137 | +| encode 256k | 128x | 210x ruopus | 0.61 | | |
| 138 | +| encode 5.1 384k (absolute) | 51x | — | — | | |
| 139 | + | |
| 140 | +Targets: decode ≥0.9 of ruopus-FFT — MET (0.93 / 0.93). Encode ≥0.8 of | |
| 141 | +opus-rs — NOT met honestly: 0.74 at the product rates, 1.01 at 510k. Per the | |
| 142 | +charter's stop rule the encode lever stops here: the residual gap is the | |
| 143 | +comparator's rsqrt-AVX2 pulse search, unreachable under the family's | |
| 144 | +`forbid(unsafe_code)` without a new safe-SIMD shape, and the seat the product | |
| 145 | +pays is decode-dominated (0.93-0.93 vs the replaced seats' 0.60-0.66). | |
| 146 | + | |
| 147 | +Local quiet-box (loadavg ~1.2-1.4, 12-core) same commit: decode 5.1 242x/ | |
| 148 | +254x = 0.95, stereo 515x/532x = 0.97; encode 0.69-0.70 (256k), 1.00 (510k). |