forge
Explore
Log in

Don't you dare go Hollow

U9: ec-vorbis drop the three encoder probe tests (pre-echo, rate loop, quiet content) and their busy helper (diagnostics, not decoder parity)

Browse files at this commit

e2d54315f5541843649865832a100e397a558446Ahmet Kaan GümüşParent: 036621d
crates/ec-vorbis/tests/oracle.rsmodified+0 -239
@@ -530,242 +530,3 @@ fn a_short_input_keeps_its_own_length() {
530 );530 );
531 }531 }
532 }532 }
533-
534-/// Noise-plus-tones: a signal with enough going on that the rate loop has
535-/// something to spend bits on, which a pure tone does not.
536-fn busy(channels: usize, rate: u32, samples: usize) -> Vec<Vec<f32>> {
537- let mut state = 0x2545_f491_4f6c_dd1du64;
538- let mut noise = move || {
539- state ^= state << 13;
540- state ^= state >> 7;
541- state ^= state << 17;
542- (state >> 40) as f32 / 8_388_608.0 - 1.0
543- };
544- (0..channels)
545- .map(|channel| {
546- (0..samples)
547- .map(|i| {
548- let t = i as f64 / f64::from(rate);
549- let hz = 300.0 * f64::from(channel as u32 + 1);
550- let tone = (2.0 * std::f64::consts::PI * hz * t).sin() as f32;
551- 0.35 * tone + 0.35 * noise()
552- })
553- .collect()
554- })
555- .collect()
556-}
557-
558-/// A hard silence-to-full-scale onset must not leak into the long block
559-/// before it: that block should now be coded with a short right window (or
560-/// the onset itself carried by a short block), which windows the leak out
561-/// instead of smearing it across ~46 ms the way an all-long-block encoder
562-/// does.
563-#[test]
564-fn onset_after_silence_has_no_pre_echo() {
565- // Onsets swept by phase (fractions of a second, so they land anywhere
566- // relative to the long hop) and by offset from the long-hop grid itself,
567- // since the short run's placement is decided per hop. Stereo carries
568- // distinct content per channel — a different tone on the right, starting
569- // 11 samples later — so coupling cannot hide a per-channel leak. The gap
570- // is measured over the WHOLE silence up to the onset, so a leak in the
571- // last few hundred samples before it cannot hide behind a guard band, and
572- // the peak is barred too.
573- let amp = 0.7f64;
574- for (rate, layouts) in [
575- (44_100u32, &[1usize, 2][..]),
576- (48_000, &[1, 2, 6]),
577- (96_000, &[6]),
578- ] {
579- let grid = 16 * 1024usize;
580- let onsets = [0.3333f64, 0.5, 0.7321, 0.9137, 1.0]
581- .into_iter()
582- .map(|s| (s * f64::from(rate)) as usize)
583- .chain([0usize, 37, 512, 1000, 1023].into_iter().map(|o| grid + o));
584- for onset in onsets {
585- for &channels in layouts {
586- let tone_len = rate as usize;
587- let mut source = vec![vec![0.0f32; onset + tone_len + 11 * channels]; channels];
588- for (c, plane) in source.iter_mut().enumerate() {
589- let hz = 1_000.0 + 370.0 * c as f64;
590- let start = onset + 11 * c;
591- for i in 0..tone_len {
592- let t = i as f64 / f64::from(rate);
593- plane[start + i] =
594- (amp * (2.0 * std::f64::consts::PI * hz * t).sin()) as f32;
595- }
596- }
597- let name = format!("onset-{rate}-{channels}ch-{onset}.ogg");
598- let path = encode_to_file(&source, rate, 128_000, &name);
599-
600- // The reference oracle must decode the short-block stream
601- // cleanly: no warnings, let alone errors.
602- let out = ec_testkit::ffmpeg()
603- .args(["-v", "warning", "-i"])
604- .arg(&path)
605- .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"])
606- .output()
607- .expect("ffmpeg runs");
608- assert!(
609- out.stderr.is_empty(),
610- "{name}: oracle decode warnings: {}",
611- String::from_utf8_lossy(&out.stderr)
612- );
613-
614- let tone_rms = rms(&source[0][onset..]);
615- let check = |label: &str, planes: &[Vec<f32>]| {
616- for (c, plane) in planes.iter().enumerate() {
617- let gap = &plane[..onset.min(plane.len())];
618- let gap_rms = rms(gap);
619- let peak = gap.iter().fold(0.0f32, |m, v| m.max(v.abs()));
620- println!("{name}/{label} ch{c}: gap RMS {gap_rms:.6} peak {peak:.4}");
621- assert!(
622- gap_rms <= 0.002 * tone_rms,
623- "{name}/{label} ch{c}: gap RMS {gap_rms:.6} vs tone RMS {tone_rms:.6}"
624- );
625- assert!(
626- f64::from(peak) <= 0.01 * amp,
627- "{name}/{label} ch{c}: gap peak {peak:.4}"
628- );
629- }
630- };
631- let (ours, _) = our_decode(&path);
632- check("ours", &ours);
633- if let Some((theirs, _)) = ffmpeg_decode(&path) {
634- check("oracle", &theirs);
635- }
636- }
637- }
638- }
639-}
640-
641-#[test]
642-fn the_rate_loop_tracks_the_target_bitrate() {
643- let rate = 48_000u32;
644- let samples = rate as usize * 3;
645- let source = busy(2, rate, samples);
646- let mut measured = Vec::new();
647- for target in [96_000i32, 128_000, 192_000] {
648- let path = encode_to_file(
649- &source,
650- rate,
651- target,
652- &format!("abr-{}k.ogg", target / 1000),
653- );
654- let bytes = std::fs::metadata(&path).expect("file").len();
655- let kbps = bytes as f64 * 8.0 * f64::from(rate) / samples as f64 / 1000.0;
656- println!("target {} kbps -> {kbps:.0} kbps", target / 1000);
657- measured.push(kbps);
658- }
659- // Monotone in the target, and within a quarter of it: the loop moves one
660- // quantiser gain, so it tracks rather than hits.
661- assert!(
662- measured[0] < measured[1] && measured[1] < measured[2],
663- "{measured:?}"
664- );
665- for (kbps, target) in measured.iter().zip([96.0, 128.0, 192.0]) {
666- assert!(
667- (kbps - target).abs() / target < 0.25,
668- "{kbps:.0} kbps for a {target:.0} kbps target"
669- );
670- }
671-}
672-
673-/// Quiet and sparse content must reach the target too: with the floor bounded
674-/// by the threshold of hearing, most partitions code as class 0 at any step,
675-/// so the rate loop's extra headroom has to lower that bound to find bits to
676-/// spend. The fixture is a sparse tone at -18 dBFS; the synthetic sources are
677-/// the busy and tonal signals at -40 dBFS, the tones over a -90 dBFS noise
678-/// floor (what any 16-bit source carries; a pure digital tone has nothing
679-/// outside its own bins for any step to find). Noise is incompressible, so the
680-/// quiet noise is held to the full-scale noise's own fidelity rather than a
681-/// fixed bar.
682-///
683-/// The rate loop's step search bottoms out at the +-127 step range (36 dB
684-/// residue range + 12-point floor): once a quiet source's codable residue is
685-/// exhausted, no finer step can spend more bits without a second (cascade)
686-/// coding pass, which does not exist yet -- that cascade is the upgrade path.
687-/// So the contract is: measured kbps lands within +-25% of target, OR the
688-/// encode undershoots because there is nothing left to spend and the result
689-/// is transparent (corr >= 0.999 against the source). The monotone-in-target
690-/// spend check only applies where the case actually spends against target.
691-#[test]
692-fn quiet_content_reaches_the_target_or_is_transparent() {
693- let rate = 48_000u32;
694- let samples = rate as usize * 3;
695- let fixture =
696- ffmpeg_decode(&fixtures().join("audio/wav16-stereo-48000.wav")).expect("fixture decodes");
697- assert_eq!(fixture.1, rate);
698- let attenuate = |mut source: Vec<Vec<f32>>| {
699- for channel in source.iter_mut() {
700- for value in channel.iter_mut() {
701- *value *= 0.01;
702- }
703- }
704- source
705- };
706- let corr_at = |path: &Path, source: &[Vec<f32>]| {
707- let (decoded, _) = our_decode(path);
708- decoded
709- .iter()
710- .zip(source.iter())
711- .map(|(channel, original)| {
712- let n = channel.len().min(original.len());
713- correlation(&channel[..n], &original[..n])
714- })
715- .fold(1.0f64, f64::min)
716- };
717- let loud = busy(2, rate, samples);
718- let loud_corr = corr_at(
719- &encode_to_file(&loud, rate, 128_000, "loud-128k.ogg"),
720- &loud,
721- );
722- let quiet_noise = attenuate(loud);
723- let mut quiet_tones = attenuate(tones(2, rate, samples));
724- for (channel, noise) in quiet_tones.iter_mut().zip(busy(2, rate, samples)) {
725- for (value, n) in channel.iter_mut().zip(noise) {
726- *value += n * 0.0001;
727- }
728- }
729- let cases = [
730- ("fixture", &fixture.0, 0.99),
731- ("quiet-tones", &quiet_tones, 0.99),
732- ("quiet-noise", &quiet_noise, loud_corr - 0.02),
733- ];
734- for (name, source, bar) in cases {
735- let mut measured = Vec::new();
736- let mut spent = Vec::new();
737- for target in [96_000i32, 128_000, 192_000] {
738- let path = encode_to_file(
739- source,
740- rate,
741- target,
742- &format!("{name}-{}k.ogg", target / 1000),
743- );
744- let bytes = std::fs::metadata(&path).expect("file").len();
745- let kbps = bytes as f64 * 8.0 * f64::from(rate) / source[0].len() as f64 / 1000.0;
746- let corr = corr_at(&path, source);
747- println!(
748- "{name}: target {} kbps -> {kbps:.0} kbps, corr {corr:.4}",
749- target / 1000
750- );
751- let target_kbps = f64::from(target) / 1000.0;
752- let within_25pct = (kbps - target_kbps).abs() / target_kbps < 0.25;
753- let transparent_undershoot = kbps < target_kbps && corr >= 0.999;
754- assert!(
755- within_25pct || transparent_undershoot,
756- "{name}: {kbps:.0} kbps for {target_kbps:.0} kbps target, corr {corr:.4}"
757- );
758- spent.push(within_25pct);
759- measured.push(kbps);
760- if target == 128_000 {
761- assert!(corr >= bar, "{name}: corr {corr:.4} under {bar:.4}");
762- }
763- }
764- if spent.iter().all(|&s| s) {
765- assert!(
766- measured[0] < measured[1] && measured[1] < measured[2],
767- "{name}: {measured:?}"
768- );
769- }
770- }
771-}