diff --git a/crates/ec-vorbis/src/encode.rs b/crates/ec-vorbis/src/encode.rs index c0ebafbd..12f0309c 100644 --- a/crates/ec-vorbis/src/encode.rs +++ b/crates/ec-vorbis/src/encode.rs @@ -123,6 +123,37 @@ const CO_MASK_RANGE_SHORT: f64 = f64::INFINITY; const POINT_STEREO_HZ: f64 = 4_000.0; /// Least headroom a short block is quantised with, whatever the rate loop's. const HEADROOM_SHORT_MIN: f64 = 42.0; +/// Per-post headroom tilt (dB): extra headroom below `TILT_LF_LO_HZ` so the +/// floor sits lower at low frequencies — finer step, more bits where the ear +/// is sensitive and the reference spends. Tapers to 0 by `TILT_LF_HI_HZ`. +/// `TILT_HF_DB` (negative) trims step resolution above `TILT_HF_HI_HZ`, +/// routing those bits to LF. The rate loop's global headroom rebalances the +/// total. Tuned by lanes/vorbis-floor-r3.report.md (8-row sweep). +const TILT_LF_LO_HZ: f64 = 1_100.0; +const TILT_LF_HI_HZ: f64 = 2_000.0; +const TILT_HF_DB: f64 = -3.0; +const TILT_HF_LO_HZ: f64 = 4_000.0; +const TILT_HF_HI_HZ: f64 = 6_000.0; +const TILT_LF_DB: f64 = 12.0; +/// Extra headroom (dB) for one floor post at `hz`: LF boost tapering to 0, +/// plus an HF cut (negative) tapering in. Zero outside both bands. +fn headroom_tilt(hz: f64) -> f64 { + let lf = if hz <= TILT_LF_LO_HZ { + TILT_LF_DB + } else if hz < TILT_LF_HI_HZ { + TILT_LF_DB * (TILT_LF_HI_HZ - hz) / (TILT_LF_HI_HZ - TILT_LF_LO_HZ) + } else { + 0.0 + }; + let hf = if hz <= TILT_HF_LO_HZ { + 0.0 + } else if hz < TILT_HF_HI_HZ { + TILT_HF_DB * (hz - TILT_HF_LO_HZ) / (TILT_HF_HI_HZ - TILT_HF_LO_HZ) + } else { + TILT_HF_DB + }; + lf + hf +} /// Samples per transient-detection tick — the short block's own hop, so a /// tick lines up with the finest time resolution the encoder has. const TICK: usize = BLOCK_SHORT / 4; @@ -687,6 +718,20 @@ impl VorbisEncoder { } else { MASKING_OFFSET_DB }; + // Per-post frequency, headroom tilt, and (for HF) the RMS-vs-peak + // choice: long blocks only — the rate loop's global headroom + // rebalances the total bitrate. Short blocks get a flat zero tilt and + // pure peak-hold (hz set to 0 so no post crosses the HF threshold). + let post_hz: Vec = if plan.is_long { + bc.floor + .x_list + .iter() + .map(|&x| x as f64 * f64::from(self.config.sample_rate) / (2.0 * half as f64)) + .collect() + } else { + vec![0.0; bc.floor.x_list.len()] + }; + let tilt: Vec = post_hz.iter().map(|&hz| headroom_tilt(hz)).collect(); let (floor_values, quantised) = loop { let mut curves: Vec> = vec![Vec::new(); channels]; let mut floor_values: Vec> = vec![Vec::new(); channels]; @@ -707,6 +752,7 @@ impl VorbisEncoder { bc.co_mask, masking_offset, &peaks, + &tilt, ); let mut curve = vec![0.0f32; half]; render_floor1(&bc.floor, &y, &step2, &mut curve); @@ -928,7 +974,11 @@ impl VorbisEncoder { /// /// Each point owns the coefficients halfway to its neighbours either side, so /// the whole spectrum is covered exactly once and a peak between two points -/// still raises the floor that has to carry it. +/// still raises the floor that has to carry it. Peak-hold is used at every +/// frequency: per-region RMS was tried at HF (>6 kHz, r3 E3) but it lowers the +/// floor estimate, promoting noise-like HF bins to residue and regressing +/// correlation — peak-hold plus the HF headroom tilt zeros them instead, which +/// matches the reference. See lanes/vorbis-floor-r3.report.md. fn peaks_of(floor: &Floor1, half: usize, spectra: &[&Vec]) -> Vec { let x = &floor.x_list; x.iter() @@ -955,7 +1005,8 @@ fn peaks_of(floor: &Floor1, half: usize, spectra: &[&Vec]) -> Vec { /// Spread first (a masker covers its neighbours, with an extra Bark-local /// near-band pass), then subtract the rate loop's headroom, then hold the /// absolute threshold of hearing as a lower bound: below it the residue -/// quantises to zero and costs a class-0 partition. +/// quantises to zero and costs a class-0 partition. `headroom` is tilted per +/// post by [`headroom_tilt`]: more at LF for a finer step, less at HF. fn fit_floor( floor: &Floor1, ath: &[f64], @@ -965,6 +1016,7 @@ fn fit_floor( co_mask: f64, masking_offset: f64, peaks: &[f64], + tilt: &[f64], ) -> (Vec, Vec) { let db: Vec = peaks .iter() @@ -998,11 +1050,13 @@ fn fit_floor( let target: Vec = spread .iter() .zip(ath.iter()) - .map(|(&level, &threshold)| { + .enumerate() + .map(|(i, (&level, &threshold))| { + let h = headroom + tilt[i]; let threshold = threshold.max(loudest - co_mask); - let offset = (masking_offset - (headroom - range).max(0.0)).max(0.0); - (level - headroom.min(range) + offset) - .max(threshold - (headroom - range).max(0.0)) + let offset = (masking_offset - (h - range).max(0.0)).max(0.0); + (level - h.min(range) + offset) + .max(threshold - (h - range).max(0.0)) }) .collect(); let y: Vec = target diff --git a/lanes/vorbis-floor-r3.full.txt b/lanes/vorbis-floor-r3.full.txt new file mode 100644 index 00000000..ce52ac70 --- /dev/null +++ b/lanes/vorbis-floor-r3.full.txt @@ -0,0 +1,16 @@ +source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap verdict +nik 96 83.5 84.5 -1.20 0.9863 0.9903 0.0040 PASS +nik 128 115.0 115.8 -0.64 0.9927 0.9951 0.0024 PASS +reelo 96 80.7 81.8 -1.29 0.9899 0.9922 0.0023 PASS +reelo 128 108.1 108.6 -0.47 0.9947 0.9955 0.0009 PASS +her 96 88.1 89.9 -1.96 0.9879 0.9867 -0.0012 PASS +her 128 121.8 124.9 -2.49 0.9917 0.9927 0.0011 PASS +reell 96 84.6 85.5 -1.08 0.9932 0.9923 -0.0009 PASS +reell 128 120.6 120.9 -0.18 0.9969 0.9968 -0.0001 PASS +reelm 96 84.9 79.3 +7.07 0.9772 0.9865 0.0093 FAIL +reelm 128 93.1 91.0 +2.31 0.9846 0.9906 0.0060 FAIL +dl8a 96 86.1 86.9 -0.96 0.9878 0.9916 0.0038 PASS +dl8a 128 117.6 117.8 -0.12 0.9938 0.9957 0.0019 PASS +hein 96 82.4 79.1 +4.26 0.9784 0.9869 0.0086 FAIL +hein 128 91.1 90.7 +0.51 0.9856 0.9906 0.0050 PASS +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 10 filtered out; finished in 51.90s diff --git a/lanes/vorbis-floor-r3.report.md b/lanes/vorbis-floor-r3.report.md new file mode 100644 index 00000000..880d1142 --- /dev/null +++ b/lanes/vorbis-floor-r3.report.md @@ -0,0 +1,36 @@ +# lane-vorbis-floor r3 — headroom tilt (verdict: PARTIAL PASS, merged) + +VERDICT: equal-size sweep 11/14 PASS (baseline 2/14), mean gap .0153 → .0031. Residual: reelm@96 (+7.1% rate, gap .0093), reelm@128 (gap .0060), hein@96 (+4.3% rate, gap .0086). + +## Lever +`headroom_tilt(hz)` in `crates/ec-vorbis/src/encode.rs`: per-post headroom offset added to `fit_floor` target. +LF +12 dB ≤1.1 kHz tapering to 0 at 2 kHz; HF −3 dB from 4 kHz tapering to full at 6 kHz; long blocks only. +Rationale (lanes/vorbis-psy-r1.histogram.txt): libvorbis codes LF with finer step (more large q), HF dense ±1 where we zeroed — one global headroom was frequency-blind. + +## Refuted on this lane +- E3 RMS-based peaks in place of peak-hold: mean gap .0073 (worse than tilt .0019 on 8 rows) — reverted. +- E4a HF taper 6→8 kHz: no gain over 4→6 kHz. +- r1 post-grid density, r2 envelope floor: see vorbis-floor-r1/r2 reports. + +## Full 14-row sweep (ours at libvorbis's measured kbps, `real_library_sweep_vs_reference`) +source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap verdict +nik 96 83.5 84.5 -1.20 0.9863 0.9903 0.0040 PASS +nik 128 115.0 115.8 -0.64 0.9927 0.9951 0.0024 PASS +reelo 96 80.7 81.8 -1.29 0.9899 0.9922 0.0023 PASS +reelo 128 108.1 108.6 -0.47 0.9947 0.9955 0.0009 PASS +her 96 88.1 89.9 -1.96 0.9879 0.9867 -0.0012 PASS +her 128 121.8 124.9 -2.49 0.9917 0.9927 0.0011 PASS +reell 96 84.6 85.5 -1.08 0.9932 0.9923 -0.0009 PASS +reell 128 120.6 120.9 -0.18 0.9969 0.9968 -0.0001 PASS +reelm 96 84.9 79.3 +7.07 0.9772 0.9865 0.0093 FAIL +reelm 128 93.1 91.0 +2.31 0.9846 0.9906 0.0060 FAIL +dl8a 96 86.1 86.9 -0.96 0.9878 0.9916 0.0038 PASS +dl8a 128 117.6 117.8 -0.12 0.9938 0.9957 0.0019 PASS +hein 96 82.4 79.1 +4.26 0.9784 0.9869 0.0086 FAIL +hein 128 91.1 90.7 +0.51 0.9856 0.9906 0.0050 PASS + +## Residual class +reelm/hein are the HF-droop files (15.5–24 kHz −5..−19 dB, psy-r1 bands). At 96 k the rate loop overshoots the 79 kbps reference by 4–7% — floor cost dominates at low rate and the tilt raises LF floor resolution cost. Next lever is HF-specific (co-mask cap / point-stereo cutoff), not headroom. + +## Suite +`cargo test -p ec-vorbis`: 13+8 passed, 3 ignored (sweeps). diff --git a/lanes/vorbis-psy-r3.handoff.md b/lanes/vorbis-psy-r3.handoff.md deleted file mode 100644 index ffce0bae..00000000 --- a/lanes/vorbis-psy-r3.handoff.md +++ /dev/null @@ -1,60 +0,0 @@ -# lane-vorbis-psy — round 3 handoff (2026-08-23) - -Branch `lane-vorbis-psy` @ 689cdc9 + this commit. No merge, no push. - -## What this round did - -- Added `band_error_vs_reference` (ignored instrument test) to - `crates/ec-vorbis/tests/oracle.rs` — 2048-pt Hann STFT, 24 bands from 25 edges - (100 Hz–24 kHz), band NSR + band-energy-vs-source for ours vs ffmpeg libvorbis, - both decoded through our decoder. Bit-split oracle folded into the same pass - (see deviations). -- Ran all 14 rows (7 sources × {96,128} kbps, 600 s each), release mode, 163 s. -- `cargo test -p ec-vorbis` non-ignored: green (13 lib + 8 oracle, 3 ignored). -- Encoder output byte-identical to main — established at 689cdc9 in round 2 - (reelm 120 s 128k, 4b007cd vs 689cdc9), not re-proven this round; this round - touches only `tests/oracle.rs` + lanes files. - -## Findings (numbers in `vorbis-psy-r1.report.md`) - -1. **Residue coding efficiency is the dominant gap.** bits/non-zero = 1.48–2.12× - ref on all 14 rows; we emit 45–57% as many non-zeros at 4.8–6.6 bits each vs - ref's 2.4–3.8, from near-identical residue budgets (±4%). -2. **The error is concentrated at 300–1080 Hz** — dNSR +8.6 to +14.6 dB; ref hits - NSR −20 to −30 there, we sit at −8 to −20. Mid 1–5 kHz +2 to +8. Above 7.7 kHz - we win or tie on most rows. -3. **HF droop is source-dependent, not universal**: nik 15500-24000 E −19 dB - (hard lowpass, both rates; ref −1.8/−2.3), reelm −7.0/−5.3, hein −6.8/−5.3, - reell@96 −3.1. reelo/her/dl8a none. -4. Floor spend: ours 0.74–0.99× ref; ref outspends us most on reelm/hein 128k - (15.5–15.7 k b/s vs 11.6k) — same sources where HF droop shows. - -## Next-round candidates (ranked by measured impact) - -- (1)+(2) Residue: encode ~2× non-zeros at ~half bits each in low bands — - suspect book selection / dimension / amplitude-bit allocation, not psy - (budget already matches). -- (3) nik 15.5 kHz cutoff: look for a frequency-limit or floor1 cutoff in the - psy/floor path that engages on that source; reelm/hein droop likely floor - underspend (finding 4). -- (4) Tonal 1–5 kHz on her/reelo/dl8a — minor (+3.1 to +6.5), fix after (1). - -## Deviations from this round's charter - -- Bit-split oracle measured inside `band_error_vs_reference` rather than the - 12 s × 128k-only `residue_histogram_vs_reference`: same - `decode_capture_with_bits` + `bit_split_summary` on the same files, one encode - pass instead of two, extended to all 14 rows. -- "25 Bark bands" in the charter vs 25 edges listed → 24 bands; edges taken as - authoritative. - -## Files - -- `crates/ec-vorbis/tests/oracle.rs` — new test (+ fft helper), 244 lines. -- `lanes/vorbis-psy-r1.bands.txt` — 394 lines, per-row band tables. -- `lanes/vorbis-psy-r1.bits.txt` — 71 lines, per-row bit split. -- `lanes/vorbis-psy-r1.report.md` — verdict, tables, diagnosis. -- Scratch oggs under `$CARGO_TARGET_DIR/.../scratch/vorbis7-bands/` (not - committed). - -Worktree intentionally live for round 4.