forge
Explore
Log in

Don't you dare go Hollow

Cap the ATH at -80 dBFS so an onset inside a short block is not coded against a -10 dB floor

Browse files at this commit

9b7e0e0e2c0d4598b6bc63c9746ae977c4a92a5aAhmet Kaan GümüşParent: 63f547a
  • fix
    • cap the ATH at -80 dBFS so an onset inside a short block is not coded against a -10 dB floor
Show full message
fix(vorbis-enc): cap the ATH at -80 dBFS so an onset inside a short block is not coded against a -10 dB floor

The pre-echo the verifier measured (gap RMS 0.002261 at 44.1k, peak 0.073)
lived entirely in the one short block whose window straddles the onset, not
in the long->short transition block: the block plan already puts the onset
mid short-run. Its DC bin and every bin above ~17 kHz were quantised against
the absolute-threshold floor, whose own curve sits at -13 / -10 dBFS there,
so a 0.1-amplitude step coefficient took a 0.2-0.3 quantiser step and its
error spread over the whole 256-sample window at -38 dB. Capping the ATH at
-80 dBFS gives gap RMS 0.000059 / peak 0.0028 (reference encoder on the same
input: 0.000120 / 0.0057).

Also gate a steady long block on its lookahead hop being buffered: with
chunked input the plan was committed before the detector could see the
transient it is supposed to make room for.

Test now places the onset off the block grid (0.9137 s) as well as on it,
measures the whole [0, onset) gap plus its peak, mono and stereo, 44.1k/48k,
our decoder and the reference oracle.
crates/ec-vorbis/src/encode.rsmodified+15 -1
@@ -457,6 +457,14 @@ impl VorbisEncoder {
457457 if buffer_start < 0 || self.buffer[0].len() < buffer_start as usize + n {
458458 break;
459459 }
460+ // A steady long block is typed by the hop *after* it; until that
461+ // hop is buffered the plan just peeked was decided blind.
462+ if !self.finished
463+ && matches!(self.scheduler, Sched::Steady)
464+ && self.buffer[0].len() < buffer_start as usize + n + BLOCK_LONG
465+ {
466+ break;
467+ }
460468 let granule = match self.finished {
461469 true => centre.min(self.fed),
462470 false => centre,
@@ -933,11 +941,17 @@ fn design_residue_books() -> Vec<CodebookSpec> {
933941 }
934942
935943 /// Absolute threshold of hearing in dBFS, full scale taken as 96 dB SPL.
944+///
945+/// Capped at -80 dBFS: the curve's own values at DC and above ~17 kHz
946+/// (-13 / -10 dBFS) are not a quantiser step the rest of a block can live
947+/// with — an onset inside a short block puts real energy in exactly those
948+/// bins, and coding them that coarsely leaked -38 dB of pre-echo across the
949+/// whole 256-sample window.
936950 fn absolute_threshold(hz: f64) -> f64 {
937951 let khz = (hz / 1000.0).clamp(0.02, 20.0);
938952 let spl =
939953 3.64 * khz.powf(-0.8) - 6.5 * (-0.6 * (khz - 3.3).powi(2)).exp() + 0.001 * khz.powi(4);
940- (spl - 96.0).clamp(-120.0, -10.0)
954+ (spl - 96.0).clamp(-120.0, -80.0)
941955 }
942956
943957 /// The three header packets, in order.
crates/ec-vorbis/tests/oracle.rsmodified+54 -54
@@ -561,61 +561,61 @@ fn busy(channels: usize, rate: u32, samples: usize) -> Vec<Vec<f32>> {
561561 /// does.
562562 #[test]
563563 fn onset_after_silence_has_no_pre_echo() {
564+ // Two onsets: one off the block grid (0.9137 s lands mid short block) and
565+ // one on it. The gap is measured over the WHOLE silence up to the onset,
566+ // so a leak in the last few hundred samples before it cannot hide behind a
567+ // guard band, and the peak is barred too.
564568 for rate in [44_100u32, 48_000] {
565- let silence = rate as usize;
566- let tone_len = rate as usize;
567- let mut left = vec![0.0f32; silence + tone_len];
568- let mut right = vec![0.0f32; silence + tone_len];
569- for i in 0..tone_len {
570- let t = i as f64 / f64::from(rate);
571- let v = (2.0 * std::f64::consts::PI * 1_000.0 * t).sin() as f32;
572- left[silence + i] = v;
573- right[silence + i] = v;
574- }
575- let source = vec![left, right];
576- let name = format!("onset-{rate}.ogg");
577- let path = encode_to_file(&source, rate, -1, &name);
578-
579- // The reference oracle must decode the short-block stream cleanly:
580- // no warnings, let alone errors.
581- let warn = Command::new("ffmpeg")
582- .args(["-v", "warning", "-i"])
583- .arg(&path)
584- .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"])
585- .output();
586- if let Ok(out) = warn {
587- assert!(
588- out.stderr.is_empty(),
589- "{name}: oracle decode warnings: {}",
590- String::from_utf8_lossy(&out.stderr)
591- );
592- }
593-
594- let window = |plane: &[f32], rate: u32| -> f64 {
595- let lo = (0.90 * f64::from(rate)) as usize;
596- let hi = ((0.995 * f64::from(rate)) as usize).min(plane.len());
597- let lo = lo.min(hi);
598- rms(&plane[lo..hi])
599- };
600- let tone_rms = {
601- let lo = silence + rate as usize / 20;
602- let hi = (silence + tone_len).min(silence + tone_len);
603- rms(&source[0][lo..hi])
604- };
605-
606- let (ours, _) = our_decode(&path);
607- let gap = window(&ours[0], rate);
608- assert!(
609- gap <= 0.002 * tone_rms,
610- "{name}: our decode gap RMS {gap:.6} vs tone RMS {tone_rms:.6}"
611- );
612-
613- if let Some((theirs, _)) = ffmpeg_decode(&path) {
614- let gap = window(&theirs[0], rate);
615- assert!(
616- gap <= 0.002 * tone_rms,
617- "{name}: oracle decode gap RMS {gap:.6} vs tone RMS {tone_rms:.6}"
618- );
569+ for channels in [1usize, 2] {
570+ for onset_s in [0.9137f64, 1.0] {
571+ let onset = (onset_s * f64::from(rate)) as usize;
572+ let tone_len = rate as usize;
573+ let mut source = vec![vec![0.0f32; onset + tone_len]; channels];
574+ for i in 0..tone_len {
575+ let t = i as f64 / f64::from(rate);
576+ let v = (0.7 * (2.0 * std::f64::consts::PI * 1_000.0 * t).sin()) as f32;
577+ for plane in &mut source {
578+ plane[onset + i] = v;
579+ }
580+ }
581+ let name = format!("onset-{rate}-{channels}ch-{onset}.ogg");
582+ let path = encode_to_file(&source, rate, 128_000, &name);
583+
584+ // The reference oracle must decode the short-block stream
585+ // cleanly: no warnings, let alone errors.
586+ let warn = Command::new("ffmpeg")
587+ .args(["-v", "warning", "-i"])
588+ .arg(&path)
589+ .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"])
590+ .output();
591+ if let Ok(out) = warn {
592+ assert!(
593+ out.stderr.is_empty(),
594+ "{name}: oracle decode warnings: {}",
595+ String::from_utf8_lossy(&out.stderr)
596+ );
597+ }
598+
599+ let tone_rms = rms(&source[0][onset..]);
600+ let check = |label: &str, planes: &[Vec<f32>]| {
601+ for (c, plane) in planes.iter().enumerate() {
602+ let gap = &plane[..onset.min(plane.len())];
603+ let gap_rms = rms(gap);
604+ let peak = gap.iter().fold(0.0f32, |m, v| m.max(v.abs()));
605+ println!("{name}/{label} ch{c}: gap RMS {gap_rms:.6} peak {peak:.4}");
606+ assert!(
607+ gap_rms <= 0.002 * tone_rms,
608+ "{name}/{label} ch{c}: gap RMS {gap_rms:.6} vs tone RMS {tone_rms:.6}"
609+ );
610+ assert!(peak <= 0.007, "{name}/{label} ch{c}: gap peak {peak:.4}");
611+ }
612+ };
613+ let (ours, _) = our_decode(&path);
614+ check("ours", &ours);
615+ if let Some((theirs, _)) = ffmpeg_decode(&path) {
616+ check("oracle", &theirs);
617+ }
618+ }
619619 }
620620 }
621621 }