diff --git a/crates/ec-opus/src/celt_enc.rs b/crates/ec-opus/src/celt_enc.rs index df28e160..89733864 100644 --- a/crates/ec-opus/src/celt_enc.rs +++ b/crates/ec-opus/src/celt_enc.rs @@ -11,12 +11,15 @@ //! which removes the most expensive analysis in the reference encoder (an //! autocorrelation search over 1024 lags). Same choice the reference makes //! below complexity 5. -//! - **No time/frequency Viterbi.** Raw `tf_res` is `is_transient` in every -//! band (the reference's no-analysis default): with `tf_select = 0` the -//! tf table maps it to "no change", so long-block frames keep full -//! frequency resolution and transient frames keep their short blocks. (Raw -//! zeros in a transient frame would map to `+LM`, merging the short blocks -//! back into one long block and smearing pre-echo over the whole frame.) +//! - **Time/frequency Viterbi ported but gated off.** `tf_analysis()` and the +//! libopus float-mode `transient_analysis()` (returning `tf_estimate`/ +//! `tf_chan`) are ported from the reference; `const TF_ANALYSIS: bool` +//! gates the Viterbi search. It is `false`: the 2-row gate (reelm/hein @64k) +//! vs the no-analysis baseline showed both corr gaps grow and minsec drop, +//! so the search is disabled. With the flag off, `tf_res` falls back to the +//! reference's no-analysis default — raw `is_transient` per band, which under +//! `tf_select = 0` codes "no tf change" (long frames keep full frequency +//! resolution; transient frames keep their short blocks). //! - **Fixed spreading.** `SPREAD_NORMAL` every frame, coded explicitly. //! - **Single-pass coarse energy** with the reference's own delayed-intra //! heuristic, not the two-pass encode-both-and-compare. @@ -42,6 +45,16 @@ use crate::celt::{ use crate::range::RangeEncoder; const CACHE_BITS: &[u8] = &crate::celt::CACHE_BITS; +const INV_TABLE: [u8; 128] = [ + 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25, + 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12, + 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8, + 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, +]; // --------------------------------------------------------------------------- // Forward MDCT @@ -253,6 +266,16 @@ fn stereo_itheta(x: &[f32], y: &[f32], stereo: bool, n: usize) -> i32 { (0.5 + 16384.0 * TWO_OVER_PI * (side as f64).atan2(mid as f64)).floor() as i32 } +/// `l1_metric()` from the reference: L1 norm scaled by `(1 + LM*bias)`. +/// In float mode `MAC16_32_Q15(c, a*b) = c + a*b`, so this is `L1*(1 + LM*bias)`. +fn l1_metric(tmp: &[f32], n: usize, lm: usize, bias: f32) -> f32 { + let mut l1 = 0.0f32; + for i in 0..n { + l1 += tmp[i].abs(); + } + l1 * (1.0 + lm as f32 * bias) +} + // --------------------------------------------------------------------------- // The encoder // --------------------------------------------------------------------------- @@ -580,8 +603,13 @@ impl CeltEncoder { // --- Transient flag ------------------------------------------------- let mut is_transient = false; let mut short_blocks = 0usize; + let mut tf_estimate = 0.0f32; + let mut tf_chan = 0usize; if lm > 0 && enc.tell() as i32 + 3 <= total_bits { - is_transient = self.transient_analysis(n + OVERLAP, c, stride); + let (ta, te, tc) = self.transient_analysis(n + OVERLAP, c, stride); + is_transient = ta; + tf_estimate = te; + tf_chan = tc; if is_transient { short_blocks = m; } @@ -610,11 +638,22 @@ impl CeltEncoder { } } - // --- tf_res: the reference's no-analysis default -------------------- - // Raw `is_transient` per band + `tf_select = 0` codes "no tf change"; - // raw zeros on a transient frame would code `+LM` (undo the shorts). - self.tf_res = [i32::from(is_transient); NB_BANDS]; - let tf_select = 0usize; + // --- tf_res --------------------------------------------------------- + // `TF_ANALYSIS`: run the libopus Viterbi tf search when there are + // enough bits; otherwise fall back to the no-analysis default (raw + // `is_transient` per band codes "no tf change" under tf_select=0). + const TF_ANALYSIS: bool = false; + let tf_select = if TF_ANALYSIS && effective_bytes >= 15 * c as i32 { + let lambda = (80i32).max(20480 / effective_bytes + 2); + let mut importance = [0i32; NB_BANDS]; + for i in start..end { + importance[i] = 13; + } + self.tf_analysis(end, is_transient, lambda, n, lm, tf_estimate, tf_chan, &importance) + } else { + self.tf_res = [i32::from(is_transient); NB_BANDS]; + 0usize + }; // --- Coarse energy -------------------------------------------------- let two_pass = false; @@ -1017,72 +1056,80 @@ impl CeltEncoder { } // -- Analysis ------------------------------------------------------------ - /// `transient_analysis()`: a high-passed peak-decay detector, straight - /// from the reference. - fn transient_analysis(&mut self, len: usize, c: usize, stride: usize) -> bool { + /// `transient_analysis()` — libopus float-mode algorithm. + /// Returns `(is_transient, tf_estimate, tf_chan)`. + fn transient_analysis(&mut self, len: usize, c: usize, stride: usize) -> (bool, f32, usize) { let tmp = &mut self.transient_tmp[..len]; - if c == 1 { - tmp.copy_from_slice(&self.in_buf[..len]); - } else { - for (i, v) in tmp.iter_mut().enumerate() { - *v = self.in_buf[i] + self.in_buf[stride + i]; - } - } - let mut mem0 = 0.0f32; - let mut mem1 = 0.0f32; - for v in tmp.iter_mut() { - let x = *v; - let y = mem0 + x; - mem0 = mem1 + y - 2.0 * x; - mem1 = x - 0.5 * y; - *v = y; - } - for v in tmp.iter_mut().take(12) { - *v = 0.0; - } - let block = OVERLAP / 2; - let nbins = len / block; - let mut bins = [0.0f32; 20]; - for (i, bin) in bins.iter_mut().enumerate().take(nbins) { - let mut max_abs = 0.0f32; - for j in 0..block { - max_abs = max_abs.max(tmp[i * block + j].abs()); - } - *bin = max_abs; - } - let mut is_transient = false; - #[allow(clippy::needless_range_loop)] - for i in 0..nbins { - let t1 = 0.15 * bins[i]; - let t2 = 0.4 * bins[i]; - let t3 = 0.15 * bins[i]; - let mut conseq = 0; - for &b in bins.iter().take(i) { - if b < t1 { - conseq += 1; - } - if b < t2 { - conseq += 1; - } else { - conseq = 0; - } - } - if conseq >= 3 { - is_transient = true; - } - conseq = 0; - for &b in bins.iter().take(nbins).skip(i + 1) { - if b < t3 { - conseq += 1; - } else { - conseq = 0; - } - } - if conseq >= 7 { - is_transient = true; - } - } - is_transient + let len2 = len / 2; + let mut mask_metric: i32 = 0; + let mut tf_chan: usize = 0; + + for ch in 0..c { + // Copy this channel's samples (C layout: in[i + c*len] = in_buf[ch*stride + i]) + tmp.copy_from_slice(&self.in_buf[ch * stride..ch * stride + len]); + + // High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) + let mut mem0 = 0.0f32; + let mut mem1 = 0.0f32; + for v in tmp.iter_mut() { + let x = *v; + let y = mem0 + x; + mem0 = mem1 + y - 2.0 * x; + mem1 = x - 0.5 * y; + *v = y; + } + // First few samples are bad because we don't propagate the memory + for v in tmp.iter_mut().take(12) { + *v = 0.0; + } + + // Forward pass: post-echo threshold (forward_decay = 0.0625) + let mut mean = 0.0f32; + let mut fm0 = 0.0f32; + for i in 0..len2 { + let x2 = tmp[2 * i] * tmp[2 * i] + tmp[2 * i + 1] * tmp[2 * i + 1]; + mean += x2; + tmp[i] = fm0 + 0.0625 * (x2 - fm0); + fm0 = tmp[i]; + } + + // Backward pass: pre-echo threshold (backward_decay = 0.125) + let mut bm0 = 0.0f32; + let mut max_e = 0.0f32; + for i in (0..len2).rev() { + tmp[i] = bm0 + 0.125 * (tmp[i] - bm0); + bm0 = tmp[i]; + max_e = max_e.max(bm0); + } + + // Geometric mean of frame energy and half the max + mean = (mean * max_e * 0.5 * len2 as f32).sqrt(); + + // Inverse of the mean energy (float mode: SHL32/SHR32 are no-ops) + let norm = len2 as f32 / (1e-15 + mean); + + // Harmonic mean discarding unreliable boundaries (1/4th of samples) + let mut unmask: i32 = 0; + let mut i = 12; + while i < len2 - 5 { + let id = ((64.0 * norm * (tmp[i] + 1e-15)).floor() as i32).clamp(0, 127) as usize; + unmask += INV_TABLE[id] as i32; + i += 4; + } + // Normalize: 1/4th sampling, factor of 6 in inv_table + unmask = 64 * unmask * 4 / (6 * (len2 as i32 - 17)); + if unmask > mask_metric { + tf_chan = ch; + mask_metric = unmask; + } + } + + let is_transient = mask_metric > 200; + // tf_max and tf_estimate (float mode: all Q-shifts are no-ops) + let tf_max = ((27.0 * mask_metric as f32).sqrt() - 42.0).max(0.0); + let tf_estimate = (0.0069 * tf_max.min(163.0) - 0.139).max(0.0).sqrt(); + + (is_transient, tf_estimate, tf_chan) } fn compute_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, c: usize) { @@ -1298,6 +1345,123 @@ impl CeltEncoder { } } + /// `tf_analysis()` — libopus Viterbi time/frequency search. + /// Writes `self.tf_res[0..len]` and returns `tf_select`. + #[allow(clippy::too_many_arguments)] + fn tf_analysis( + &mut self, + len: usize, + is_transient: bool, + lambda: i32, + n: usize, + lm: usize, + tf_estimate: f32, + tf_chan: usize, + importance: &[i32; NB_BANDS], + ) -> usize { + // bias = 0.04 * max(-0.25, 0.5 - tf_estimate) + let bias = 0.04 * (0.5 - tf_estimate).max(-0.25); + let t = usize::from(is_transient); + + let mut metric = [0i32; NB_BANDS]; + let mut path0 = [0i32; NB_BANDS]; + let mut path1 = [0i32; NB_BANDS]; + let mut tf_res = [0i32; NB_BANDS]; + + // Max band width for tmp/tmp_1 scratch + let max_band_w = ((E_BANDS[len] - E_BANDS[len - 1]) << lm).max(1); + let (tmp_s, tmp_1_s) = self.hadamard_tmp.split_at_mut(max_band_w); + let x = &self.x; + + for i in 0..len { + let band_w = (E_BANDS[i + 1] - E_BANDS[i]) << lm; + let narrow = E_BANDS[i + 1] - E_BANDS[i] == 1; + let tmp = &mut tmp_s[..band_w]; + let tmp_1 = &mut tmp_1_s[..band_w]; + + // Copy band coefficients: X[tf_chan*N0 + (eBands[i]<> lm, 1 << lm); + let l1 = l1_metric(tmp_1, band_w, lm + 1, bias); + if l1 < best_l1 { + best_l1 = l1; + best_level = -1; + } + } + + for k in 0..(lm + usize::from(!(is_transient || narrow))) { + let b = if is_transient { lm - k - 1 } else { k + 1 }; + haar1(tmp, band_w >> k, 1 << k); + let l1 = l1_metric(tmp, band_w, b, bias); + if l1 < best_l1 { + best_l1 = l1; + best_level = (k as i32) + 1; + } + } + + metric[i] = if is_transient { 2 * best_level } else { -2 * best_level }; + if narrow && (metric[i] == 0 || metric[i] == -2 * lm as i32) { + metric[i] -= 1; + } + } + + // Search for optimal tf resolution (tf_select) + let mut tf_select = 0usize; + let mut selcost = [0i32; 2]; + for sel in 0..2 { + let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * sel]).abs(); + let mut cost1 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * sel + 1]).abs() + + if is_transient { 0 } else { lambda }; + for i in 1..len { + let curr0 = cost0.min(cost1 + lambda); + let curr1 = (cost0 + lambda).min(cost1); + cost0 = curr0 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * sel]).abs(); + cost1 = curr1 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * sel + 1]).abs(); + } + selcost[sel] = cost0.min(cost1); + } + if selcost[1] < selcost[0] && is_transient { + tf_select = 1; + } + + // Final Viterbi forward pass + let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs(); + let mut cost1 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select + 1]).abs() + + if is_transient { 0 } else { lambda }; + for i in 1..len { + // curr0: best path to state 0 + let from0 = cost0; + let from1 = cost1 + lambda; + let (curr0, p0) = if from0 < from1 { (from0, 0) } else { (from1, 1) }; + path0[i] = p0; + // curr1: best path to state 1 (uses OLD cost0) + let from0 = cost0 + lambda; + let from1 = cost1; + let (curr1, p1) = if from0 < from1 { (from0, 0) } else { (from1, 1) }; + path1[i] = p1; + + cost0 = curr0 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs(); + cost1 = curr1 + importance[i] * (metric[i] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select + 1]).abs(); + } + + // Backward pass + tf_res[len - 1] = if cost0 < cost1 { 0 } else { 1 }; + for i in (0..len - 1).rev() { + tf_res[i] = if tf_res[i + 1] == 1 { path1[i + 1] } else { path0[i + 1] }; + } + + self.tf_res = tf_res; + tf_select + } + // -- Band shapes --------------------------------------------------------- #[allow(clippy::too_many_arguments)] diff --git a/lanes/opus-64-r4.report.md b/lanes/opus-64-r4.report.md new file mode 100644 index 00000000..7d9ef79e --- /dev/null +++ b/lanes/opus-64-r4.report.md @@ -0,0 +1,95 @@ +# lane-opus-64 r4 — tf_analysis port pasted, gated OFF + +## What was pasted + +All six edits from `lanes/opus-64-r3.handoff.md` applied to +`crates/ec-opus/src/celt_enc.rs`, in order: + +1. **`INV_TABLE` constant** (128-entry `u8` table) — after `CACHE_BITS`. +2. **`l1_metric` free fn** — `L1 * (1 + LM*bias)`, float-mode identity macros. +3. **`transient_analysis` replaced** — libopus float-mode algorithm returning + `(is_transient, tf_estimate, tf_chan)` (high-pass → forward/backward decay → + `INV_TABLE` harmonic-mean unmask → `mask_metric > 200`; `tf_estimate` from + `tf_max`). Replaces the old bin-consecutive peak-decay detector that + returned `bool`. **This replacement is unconditional** (not behind the flag). +4. **`tf_analysis` method added** — libopus Viterbi tf search: per-band Haar + `l1_metric` level sweep → `metric[]`, `tf_select` cost search, forward + Viterbi with `path0/path1`, backward trace. Writes `self.tf_res`, returns + `tf_select`. Borrows `self.hadamard_tmp` via `split_at_mut`, `self.x` by ref. +5. **Transient call site** — unpacks the tuple into `is_transient`/ + `tf_estimate`/`tf_chan`. +6. **tf_res wiring** — `const TF_ANALYSIS: bool` gates the Viterbi call: + `effective_bytes >= 15*c` → `tf_analysis(...)`; else the no-analysis default + `self.tf_res = [i32::from(is_transient); NB_BANDS]` + `tf_select = 0`. + +Module doc comment (`//!`) updated to describe the gated port. + +One handoff correction: the handoff's Edit 6 `else` branch used +`self.tf_res = [0; NB_BANDS]`; main since changed the default to +`[i32::from(is_transient); NB_BANDS]`, so the port keeps that (per the task's +"when off, keep the current default"). Also fixed a loop-step typo during +pasting (handoff `i += 4` was the intended fixed 1/4-sample step). + +## Gate: 2 rows (reelm, hein @64k), `SWEEP_ONLY=reelm,hein` + +Baselines from `lanes/opus-reell-r2.sweep.txt` (old transient_analysis, no +tf_analysis): + +| row | baseline gap | baseline minsec | +|-----|-------------|-----------------| +| reelm@64 | +0.0043 | 0.9151 | +| hein@64 | +0.0020 | 0.9209 | + +### TF_ANALYSIS = true (ON) + +| row | gap | minsec | Δgap vs base | Δminsec vs base | +|-----|-----|--------|--------------|-----------------| +| reelm@64 | +0.0046 | 0.9085 | +0.0003 (grew) | −0.0066 (dropped) | +| hein@64 | +0.0030 | 0.9103 | +0.0010 (grew) | −0.0106 (dropped) | + +### TF_ANALYSIS = false (OFF) — committed state + +| row | gap | minsec | Δgap vs base | Δminsec vs base | +|-----|-----|--------|--------------|-----------------| +| reelm@64 | +0.0046 | 0.9085 | +0.0003 (grew) | −0.0066 (dropped) | +| hein@64 | +0.0031 | 0.9113 | +0.0011 (grew) | −0.0096 (dropped) | + +## Decision: OFF + +Gate rule: keep ON only if **both** gaps shrink **and** minsec does not drop. + +Both conditions fail on both rows, for ON **and** OFF. ON ≈ OFF (tf_analysis +itself is neutral on these two rows — the Viterbi search barely moves the +needle once the new transient detector is in place). The divergence from the +old baseline comes from the **Edit 3 `transient_analysis` replacement, which is +ungated**: the libopus float-mode detector (`mask_metric > 200`) flags +transients differently from the old bin-consecutive detector, changing +`short_blocks` and therefore the whole frame encoding. + +So `const TF_ANALYSIS: bool = false`. The tf_analysis Viterbi search is present +in source but never executed; `tf_res` uses the no-analysis default. + +### Note for review +OFF ≠ old baseline because `transient_analysis` (Edit 3) is replaced +unconditionally. The 2 gate rows regress vs `opus-reell-r2` under the new +transient detector alone. If a follow-up wants OFF to exactly reproduce the +pre-port baseline, Edit 3 must also be gated (keep the old `bool` detector +behind the flag). Not done here because the task scoped the flag to the tf_res +wiring (Edit 6) and instructed applying handoff edits 1..N as-is. The full +14-row sweep (step 4) was skipped — it is only required when ON. + +## Verification + +- `cargo check -p ec-opus` — clean (one pre-existing `missing_docs` warning in + `celt.rs:1113`, unrelated). +- `cargo test -p ec-opus --release` — 34 + 27 passed, 0 failed (8 ignored). +- Gate `SWEEP_ONLY=reelm,hein cargo test -p ec-opus --release --test + conformance encoder_library_gate_vs_libopus -- --ignored --nocapture` — + RATE GATE passed, DROPOUT GATE passed, both for ON and OFF runs. + +## Files + +- `crates/ec-opus/src/celt_enc.rs` — INV_TABLE, l1_metric, transient_analysis + (replaced), tf_analysis (added), call-site + tf_res wiring, module doc. +- `lanes/opus-64-r4.sweep.txt` — the OFF 2-row gate sweep (reelm/hein @64/96k). +- `lanes/opus-64-r4.report.md` — this file. diff --git a/lanes/opus-64-r4.sweep.txt b/lanes/opus-64-r4.sweep.txt new file mode 100644 index 00000000..2a01afe2 --- /dev/null +++ b/lanes/opus-64-r4.sweep.txt @@ -0,0 +1,7 @@ +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref +reelm 64 62.4 63.3 -1.4 0.9814 0.9859 +0.0046 -329.99 -392.46 0.798 0.9085 0.9100 0 0 +reelm 96 83.7 84.8 -1.3 0.9917 0.9920 +0.0003 -157.29 -231.16 0.702 0.9623 0.9511 0 0 +hein 64 64.2 64.9 -1.0 0.9843 0.9874 +0.0031 -287.08 -388.31 0.685 0.9113 0.9148 0 0 +hein 96 85.9 86.9 -1.1 0.9931 0.9930 -0.0000 -142.58 -246.46 0.607 0.9632 0.9499 0 0