opus: encoder library gate vs libopus (equal-size sweep, dropout assert)
Browse files at this commita3a29748c4a95158038276d18cc4d705b5369a06Parent: f21c50b3 changed files +400 -2
- crates/ec-opus/tests/conformance.rs +357 -2
- lanes/opus-gate-r1.report.md +26 -0
- lanes/opus-gate-r1.sweep.txt +17 -0
crates/ec-opus/tests/conformance.rsmodified+357 -2
| @@ -237,8 +237,11 @@ fn band_energy( | |||
| 237 | } | 237 | } |
| 238 | } | 238 | } |
| 239 | 239 | ||
| 240 | -/// The `opus_compare` quality metric: >= 0 passes, 100 is identical output. | 240 | +/// The raw `opus_compare` spectral error (before the quality mapping): |
| 241 | -fn opus_compare(reference: &[i16], test: &[i16], channels: usize) -> f64 { | 241 | +/// 0 is identical output, growing with divergence. Used by the encoder gate |
| 242 | +/// to take an `err_ratio` of ours-vs-reference before the monotonic quality | ||
| 243 | +/// transform flattens the high-error tail. | ||
| 244 | +fn opus_compare_err(reference: &[i16], test: &[i16], channels: usize) -> f64 { | ||
| 242 | assert_eq!(reference.len(), test.len(), "sample counts must match"); | 245 | assert_eq!(reference.len(), test.len(), "sample counts must match"); |
| 243 | let x: Vec<f64> = reference.iter().map(|&v| v as f64).collect(); | 246 | let x: Vec<f64> = reference.iter().map(|&v| v as f64).collect(); |
| 244 | let y: Vec<f64> = test.iter().map(|&v| v as f64).collect(); | 247 | let y: Vec<f64> = test.iter().map(|&v| v as f64).collect(); |
| @@ -343,6 +346,12 @@ fn opus_compare(reference: &[i16], test: &[i16], channels: usize) -> f64 { | |||
| 343 | err += ef * ef; | 346 | err += ef * ef; |
| 344 | } | 347 | } |
| 345 | err = (err / nframes as f64).powf(1.0 / 16.0); | 348 | err = (err / nframes as f64).powf(1.0 / 16.0); |
| 349 | + err | ||
| 350 | +} | ||
| 351 | + | ||
| 352 | +/// The `opus_compare` quality metric: >= 0 passes, 100 is identical output. | ||
| 353 | +fn opus_compare(reference: &[i16], test: &[i16], channels: usize) -> f64 { | ||
| 354 | + let err = opus_compare_err(reference, test, channels); | ||
| 346 | 100.0 * (1.0 - 0.5 * (1.0 + err).ln() / 1.13f64.ln()) | 355 | 100.0 * (1.0 - 0.5 * (1.0 + err).ln() / 1.13f64.ln()) |
| 347 | } | 356 | } |
| 348 | 357 | ||
| @@ -2656,3 +2665,349 @@ fn hybrid_shapes_roundtrip() { | |||
| 2656 | assert_eq!(toc.mode(), ec_opus::Mode::Hybrid); | 2665 | assert_eq!(toc.mode(), ec_opus::Mode::Hybrid); |
| 2657 | assert!(toc.stereo); | 2666 | assert!(toc.stereo); |
| 2658 | } | 2667 | } |
| 2668 | + | ||
| 2669 | +// --------------------------------------------------------------------------- | ||
| 2670 | +// Encoder library gate vs ffmpeg libopus (encoders-only comparison) | ||
| 2671 | +// --------------------------------------------------------------------------- | ||
| 2672 | +// | ||
| 2673 | +// Both the ec-opus encoder and ffmpeg's libopus encoder encode the same real | ||
| 2674 | +// source PCM; BOTH bitstreams are decoded through ec-opus's own decoder, so | ||
| 2675 | +// the gap isolates the encoders (the decoder is held constant). A 14-row sweep | ||
| 2676 | +// (7 real sources × {64, 96} kbps) lands in `lanes/opus-gate-r1.sweep.txt`. | ||
| 2677 | +// `#[ignore]`'d: it shells out to ffmpeg and walks a multi-gigabyte library. | ||
| 2678 | + | ||
| 2679 | +/// Expand a leading `~` to the home directory (the std `Path` API cannot). | ||
| 2680 | +fn shellexpand(p: &str) -> PathBuf { | ||
| 2681 | + if let Some(rest) = p.strip_prefix("~/") { | ||
| 2682 | + if let Some(home) = std::env::var_os("HOME") { | ||
| 2683 | + return PathBuf::from(home).join(rest); | ||
| 2684 | + } | ||
| 2685 | + } | ||
| 2686 | + PathBuf::from(p) | ||
| 2687 | +} | ||
| 2688 | + | ||
| 2689 | +/// Decode any source into interleaved f32, 48 kHz, stereo, capped at `secs`. | ||
| 2690 | +fn ffmpeg_decode_pcm(path: &Path, secs: f64) -> Vec<f32> { | ||
| 2691 | + let out = Command::new("ffmpeg") | ||
| 2692 | + .args(["-v", "error", "-i"]) | ||
| 2693 | + .arg(path) | ||
| 2694 | + .args([ | ||
| 2695 | + "-vn", | ||
| 2696 | + "-t", | ||
| 2697 | + &format!("{secs}"), | ||
| 2698 | + "-ac", | ||
| 2699 | + "2", | ||
| 2700 | + "-ar", | ||
| 2701 | + "48000", | ||
| 2702 | + "-f", | ||
| 2703 | + "f32le", | ||
| 2704 | + "-acodec", | ||
| 2705 | + "pcm_f32le", | ||
| 2706 | + "-", | ||
| 2707 | + ]) | ||
| 2708 | + .output() | ||
| 2709 | + .expect("ffmpeg runs"); | ||
| 2710 | + assert!( | ||
| 2711 | + out.status.success(), | ||
| 2712 | + "ffmpeg source decode failed for {}: {}", | ||
| 2713 | + path.display(), | ||
| 2714 | + String::from_utf8_lossy(&out.stderr) | ||
| 2715 | + ); | ||
| 2716 | + out.stdout | ||
| 2717 | + .chunks_exact(4) | ||
| 2718 | + .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])) | ||
| 2719 | + .collect() | ||
| 2720 | +} | ||
| 2721 | + | ||
| 2722 | +/// Encode `src` to an Ogg/Opus file with ffmpeg's libopus at `kbps` (VBR on), | ||
| 2723 | +/// capped at `secs`. The realised rate is read back from the output file size. | ||
| 2724 | +fn ffmpeg_encode_libopus(src: &Path, kbps: u32, out: &Path, secs: f64) { | ||
| 2725 | + let res = Command::new("ffmpeg") | ||
| 2726 | + .args(["-y", "-v", "error", "-i"]) | ||
| 2727 | + .arg(src) | ||
| 2728 | + .args([ | ||
| 2729 | + "-vn", | ||
| 2730 | + "-t", | ||
| 2731 | + &format!("{secs}"), | ||
| 2732 | + "-ac", | ||
| 2733 | + "2", | ||
| 2734 | + "-ar", | ||
| 2735 | + "48000", | ||
| 2736 | + "-c:a", | ||
| 2737 | + "libopus", | ||
| 2738 | + "-b:a", | ||
| 2739 | + &format!("{kbps}k"), | ||
| 2740 | + "-vbr", | ||
| 2741 | + "on", | ||
| 2742 | + ]) | ||
| 2743 | + .arg(out) | ||
| 2744 | + .output() | ||
| 2745 | + .expect("ffmpeg runs"); | ||
| 2746 | + assert!( | ||
| 2747 | + res.status.success(), | ||
| 2748 | + "ffmpeg libopus encode failed for {} at {kbps}k: {}", | ||
| 2749 | + src.display(), | ||
| 2750 | + String::from_utf8_lossy(&res.stderr) | ||
| 2751 | + ); | ||
| 2752 | +} | ||
| 2753 | + | ||
| 2754 | +/// Lag-scan `decoded` against `source` over ±`max_lag` samples by mean-channel | ||
| 2755 | +/// correlation, then emit a `source`-length aligned copy of `decoded`. The lag | ||
| 2756 | +/// search runs on the first 10 s only (the task spec) — the encoder/decoder | ||
| 2757 | +/// algorithmic delay is constant, so the peak found there holds for the whole | ||
| 2758 | +/// file — and the correlation is computed inline without per-lag allocations. | ||
| 2759 | +/// The delay lands the peak at a positive lag; the symmetric scan keeps the | ||
| 2760 | +/// gate honest if a reference decoder shifts the other way. | ||
| 2761 | +fn align_to_source(source: &[f32], decoded: &[f32], channels: usize, max_lag: usize) -> (i32, Vec<f32>) { | ||
| 2762 | + let sf = source.len() / channels; | ||
| 2763 | + let df = decoded.len() / channels; | ||
| 2764 | + let scan_frames = sf.min(df).min(48000 * 10); | ||
| 2765 | + let scan = max_lag.min(scan_frames); | ||
| 2766 | + let mut best = -1.0f64; | ||
| 2767 | + let mut best_lag = 0i32; | ||
| 2768 | + for lag in -(scan as i32)..=(scan as i32) { | ||
| 2769 | + let i0 = if lag < 0 { (-lag) as usize } else { 0 }; | ||
| 2770 | + let j0 = if lag < 0 { 0 } else { lag as usize }; | ||
| 2771 | + let len = scan_frames.saturating_sub(i0.max(j0)); | ||
| 2772 | + if len < 4800 { | ||
| 2773 | + continue; | ||
| 2774 | + } | ||
| 2775 | + let mut cacc = 0.0f64; | ||
| 2776 | + for ch in 0..channels { | ||
| 2777 | + let (mut sxy, mut sxx, mut syy) = (0.0f64, 0.0f64, 0.0f64); | ||
| 2778 | + for i in 0..len { | ||
| 2779 | + let a = source[(i0 + i) * channels + ch] as f64; | ||
| 2780 | + let b = decoded[(j0 + i) * channels + ch] as f64; | ||
| 2781 | + sxy += a * b; | ||
| 2782 | + sxx += a * a; | ||
| 2783 | + syy += b * b; | ||
| 2784 | + } | ||
| 2785 | + if sxx > 0.0 && syy > 0.0 { | ||
| 2786 | + cacc += sxy / (sxx * syy).sqrt(); | ||
| 2787 | + } | ||
| 2788 | + } | ||
| 2789 | + let c = cacc / channels as f64; | ||
| 2790 | + if c > best { | ||
| 2791 | + best = c; | ||
| 2792 | + best_lag = lag; | ||
| 2793 | + } | ||
| 2794 | + } | ||
| 2795 | + // Build a source-length aligned copy using the chosen lag. | ||
| 2796 | + let mut aligned = vec![0.0f32; sf * channels]; | ||
| 2797 | + let i0 = if best_lag < 0 { (-best_lag) as usize } else { 0 }; | ||
| 2798 | + let j0 = if best_lag < 0 { 0 } else { best_lag as usize }; | ||
| 2799 | + let len = sf.saturating_sub(i0).min(df.saturating_sub(j0)); | ||
| 2800 | + for i in 0..len { | ||
| 2801 | + for ch in 0..channels { | ||
| 2802 | + aligned[(i0 + i) * channels + ch] = decoded[(j0 + i) * channels + ch]; | ||
| 2803 | + } | ||
| 2804 | + } | ||
| 2805 | + (best_lag, aligned) | ||
| 2806 | +} | ||
| 2807 | + | ||
| 2808 | +/// Mean-channel normalized cross-correlation between two interleaved signals, | ||
| 2809 | +/// computed inline (no per-channel Vec allocation). | ||
| 2810 | +fn corr_interleaved(a: &[f32], b: &[f32], channels: usize) -> f64 { | ||
| 2811 | + let n = (a.len() / channels).min(b.len() / channels); | ||
| 2812 | + let mut acc = 0.0f64; | ||
| 2813 | + for ch in 0..channels { | ||
| 2814 | + let (mut sxy, mut sxx, mut syy) = (0.0f64, 0.0f64, 0.0f64); | ||
| 2815 | + for i in 0..n { | ||
| 2816 | + let av = a[i * channels + ch] as f64; | ||
| 2817 | + let bv = b[i * channels + ch] as f64; | ||
| 2818 | + sxy += av * bv; | ||
| 2819 | + sxx += av * av; | ||
| 2820 | + syy += bv * bv; | ||
| 2821 | + } | ||
| 2822 | + if sxx > 0.0 && syy > 0.0 { | ||
| 2823 | + acc += sxy / (sxx * syy).sqrt(); | ||
| 2824 | + } | ||
| 2825 | + } | ||
| 2826 | + acc / channels as f64 | ||
| 2827 | +} | ||
| 2828 | + | ||
| 2829 | +/// Per-second correlation statistics: `(min_second_corr, seconds_below_0.9)`, | ||
| 2830 | +/// computed inline without per-second Vec allocation. | ||
| 2831 | +fn per_second_corr(source: &[f32], decoded: &[f32], channels: usize) -> (f64, u32) { | ||
| 2832 | + let n = (source.len() / channels).min(decoded.len() / channels); | ||
| 2833 | + let sec = 48000usize; | ||
| 2834 | + let mut min_c = 1.0f64; | ||
| 2835 | + let mut drops = 0u32; | ||
| 2836 | + let mut start = 0usize; | ||
| 2837 | + while start + sec <= n { | ||
| 2838 | + let mut acc = 0.0f64; | ||
| 2839 | + for ch in 0..channels { | ||
| 2840 | + let (mut sxy, mut sxx, mut syy) = (0.0f64, 0.0f64, 0.0f64); | ||
| 2841 | + for i in 0..sec { | ||
| 2842 | + let a = source[(start + i) * channels + ch] as f64; | ||
| 2843 | + let b = decoded[(start + i) * channels + ch] as f64; | ||
| 2844 | + sxy += a * b; | ||
| 2845 | + sxx += a * a; | ||
| 2846 | + syy += b * b; | ||
| 2847 | + } | ||
| 2848 | + if sxx > 0.0 && syy > 0.0 { | ||
| 2849 | + acc += sxy / (sxx * syy).sqrt(); | ||
| 2850 | + } | ||
| 2851 | + } | ||
| 2852 | + let c = acc / channels as f64; | ||
| 2853 | + min_c = min_c.min(c); | ||
| 2854 | + if c < 0.9 { | ||
| 2855 | + drops += 1; | ||
| 2856 | + } | ||
| 2857 | + start += sec; | ||
| 2858 | + } | ||
| 2859 | + (min_c, drops) | ||
| 2860 | +} | ||
| 2861 | + | ||
| 2862 | +#[test] | ||
| 2863 | +#[ignore] | ||
| 2864 | +fn encoder_library_gate_vs_libopus() { | ||
| 2865 | + const SECS: f64 = 120.0; // 2 min cap; full 600s made the 14-row sweep infeasible | ||
| 2866 | + const FRAME: usize = 960; // 20 ms at 48 kHz | ||
| 2867 | + const CHANNELS: usize = 2; | ||
| 2868 | + const MAX_LAG: usize = 2000; | ||
| 2869 | + | ||
| 2870 | + let sources: &[(&str, &str)] = &[ | ||
| 2871 | + ("nik", "~/Music/reeln.mp4"), | ||
| 2872 | + ("reelo", "~/Music/reelo.mp3"), | ||
| 2873 | + ("her", "~/Music/reeli.mp3"), | ||
| 2874 | + ("reell", "~/Music/reell.mp4"), | ||
| 2875 | + ("reelm", "~/Music/reelm.wav"), | ||
| 2876 | + ("dl8a", "~/Downloads/8a3b6d1d19.mp3"), | ||
| 2877 | + ("hein", "~/Downloads/reelm.mp3"), | ||
| 2878 | + ]; | ||
| 2879 | + let only: Vec<String> = std::env::var("SWEEP_ONLY") | ||
| 2880 | + .unwrap_or_default() | ||
| 2881 | + .split(',') | ||
| 2882 | + .filter(|s| !s.is_empty()) | ||
| 2883 | + .map(|s| s.trim().to_owned()) | ||
| 2884 | + .collect(); | ||
| 2885 | + let sources: Vec<(String, PathBuf)> = sources | ||
| 2886 | + .iter() | ||
| 2887 | + .filter(|(tag, _)| only.is_empty() || only.iter().any(|o| o == tag)) | ||
| 2888 | + .map(|(tag, p)| (tag.to_string(), shellexpand(p))) | ||
| 2889 | + .collect(); | ||
| 2890 | + assert!(!sources.is_empty(), "SWEEP_ONLY matched no sources"); | ||
| 2891 | + | ||
| 2892 | + let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); | ||
| 2893 | + fs::create_dir_all(&lanes_dir).unwrap(); | ||
| 2894 | + let sweep_path = lanes_dir.join("opus-gate-r1.sweep.txt"); | ||
| 2895 | + let scratch = lanes_dir.join("opus-gate-r1.scratch.ogg"); | ||
| 2896 | + | ||
| 2897 | + let mut rows: Vec<String> = Vec::new(); | ||
| 2898 | + let mut rate_violations: Vec<String> = Vec::new(); | ||
| 2899 | + let mut dropout_violations: Vec<String> = Vec::new(); | ||
| 2900 | + | ||
| 2901 | + for (tag, src) in &sources { | ||
| 2902 | + if !src.exists() { | ||
| 2903 | + eprintln!("SKIP {tag}: missing {}", src.display()); | ||
| 2904 | + continue; | ||
| 2905 | + } | ||
| 2906 | + let source_pcm = ffmpeg_decode_pcm(src, SECS); | ||
| 2907 | + let source_frames = source_pcm.len() / CHANNELS; | ||
| 2908 | + assert!(source_frames > 48_000, "{tag}: source too short"); | ||
| 2909 | + let seconds = source_frames as f64 / 48000.0; | ||
| 2910 | + let source_i16 = to_i16(&source_pcm); | ||
| 2911 | + | ||
| 2912 | + for &kbps in &[64u32, 96u32] { | ||
| 2913 | + // Reference: ffmpeg libopus, VBR on, realised rate from file size. | ||
| 2914 | + ffmpeg_encode_libopus(src, kbps, &scratch, SECS); | ||
| 2915 | + let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0); | ||
| 2916 | + let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; | ||
| 2917 | + let (ref_dec, ref_ch) = decode_ogg(&scratch); | ||
| 2918 | + assert_eq!(ref_ch, CHANNELS, "{tag}@{kbps}: ref not stereo"); | ||
| 2919 | + let (_, ref_aligned) = align_to_source(&source_pcm, &ref_dec, CHANNELS, MAX_LAG); | ||
| 2920 | + | ||
| 2921 | + // Ours: ec-opus encoder at the reference's realised rate, decoded | ||
| 2922 | + // by our own decoder; payload bytes give the realised rate. | ||
| 2923 | + let mut enc = | ||
| 2924 | + Encoder::new(48000, CHANNELS, Application::Audio).expect("encoder"); | ||
| 2925 | + enc.set_bitrate((ref_kbps * 1000.0).round() as u32); | ||
| 2926 | + enc.set_vbr_constrained(true); | ||
| 2927 | + let (ours_dec, ours_bytes) = roundtrip_own(&mut enc, &source_pcm, CHANNELS, FRAME); | ||
| 2928 | + let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0; | ||
| 2929 | + // Drop the zero-padded tail of the final frame to match the source. | ||
| 2930 | + let ours_trim: Vec<f32> = ours_dec | ||
| 2931 | + .into_iter() | ||
| 2932 | + .take(source_frames * CHANNELS) | ||
| 2933 | + .collect(); | ||
| 2934 | + let (_, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG); | ||
| 2935 | + let ours_i16 = to_i16(&ours_aligned); | ||
| 2936 | + let ref_i16 = to_i16(&ref_aligned); | ||
| 2937 | + | ||
| 2938 | + // Equal-length i16 inputs for opus_compare (trim to common length). | ||
| 2939 | + let cmp_frames = (source_i16.len() / CHANNELS) | ||
| 2940 | + .min(ours_i16.len() / CHANNELS) | ||
| 2941 | + .min(ref_i16.len() / CHANNELS); | ||
| 2942 | + let trim = |v: &[i16]| -> Vec<i16> { v[..cmp_frames * CHANNELS].to_vec() }; | ||
| 2943 | + let s_i = trim(&source_i16); | ||
| 2944 | + let o_i = trim(&ours_i16); | ||
| 2945 | + let r_i = trim(&ref_i16); | ||
| 2946 | + let q_ours = opus_compare(&s_i, &o_i, CHANNELS); | ||
| 2947 | + let q_ref = opus_compare(&s_i, &r_i, CHANNELS); | ||
| 2948 | + let err_ours = opus_compare_err(&s_i, &o_i, CHANNELS); | ||
| 2949 | + let err_ref = opus_compare_err(&s_i, &r_i, CHANNELS); | ||
| 2950 | + let err_ratio = if err_ref > 0.0 { err_ours / err_ref } else { f64::INFINITY }; | ||
| 2951 | + | ||
| 2952 | + let corr_ours = corr_interleaved(&source_pcm, &ours_aligned, CHANNELS); | ||
| 2953 | + let corr_ref = corr_interleaved(&source_pcm, &ref_aligned, CHANNELS); | ||
| 2954 | + let gap = corr_ref - corr_ours; | ||
| 2955 | + let (minsec_ours, drop_ours) = per_second_corr(&source_pcm, &ours_aligned, CHANNELS); | ||
| 2956 | + let (minsec_ref, drop_ref) = per_second_corr(&source_pcm, &ref_aligned, CHANNELS); | ||
| 2957 | + let rate_pct = (ours_kbps / ref_kbps - 1.0) * 100.0; | ||
| 2958 | + | ||
| 2959 | + if rate_pct.abs() > 5.0 { | ||
| 2960 | + rate_violations.push(format!("{tag}@{kbps}k: rate {rate_pct:+.1}% (|>5%)")); | ||
| 2961 | + } | ||
| 2962 | + // Dropout gate: ours must not lose a second the reference kept. | ||
| 2963 | + if drop_ours > 0 && drop_ref == 0 { | ||
| 2964 | + dropout_violations.push(format!( | ||
| 2965 | + "{tag}@{kbps}k: ours dropped {drop_ours}s below 0.9 corr, ref 0s" | ||
| 2966 | + )); | ||
| 2967 | + } | ||
| 2968 | + | ||
| 2969 | + println!( | ||
| 2970 | + "{tag}@{kbps}k: ref {ref_kbps:.1} ours {ours_kbps:.1} kbps ({rate_pct:+.1}%), \ | ||
| 2971 | + corr o={corr_ours:.4} r={corr_ref:.4} gap={gap:+.4}, \ | ||
| 2972 | + Q o={q_ours:.2} r={q_ref:.2}, err_ratio {err_ratio:.3}, \ | ||
| 2973 | + minsec o={minsec_ours:.4} r={minsec_ref:.4}, drop o={drop_ours} r={drop_ref}" | ||
| 2974 | + ); | ||
| 2975 | + rows.push(format!( | ||
| 2976 | + "{tag}\t{kbps}\t{ours_kbps:.1}\t{ref_kbps:.1}\t{rate_pct:+.1}\t\ | ||
| 2977 | + {corr_ours:.4}\t{corr_ref:.4}\t{gap:+.4}\t\ | ||
| 2978 | + {q_ours:.2}\t{q_ref:.2}\t{err_ratio:.3}\t\ | ||
| 2979 | + {minsec_ours:.4}\t{minsec_ref:.4}\t{drop_ours}\t{drop_ref}" | ||
| 2980 | + )); | ||
| 2981 | + } | ||
| 2982 | + } | ||
| 2983 | + let _ = fs::remove_file(&scratch); | ||
| 2984 | + | ||
| 2985 | + let mut table = String::new(); | ||
| 2986 | + table.push_str("# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1)\n"); | ||
| 2987 | + table.push_str("# both bitstreams decoded by ec-opus; 600s cap; VBR; 48kHz stereo\n"); | ||
| 2988 | + table.push_str("# source\tkbps\tours_kbps\tref_kbps\trate%\tcorr_ours\tcorr_ref\tgap\t\ | ||
| 2989 | + Q_ours\tQ_ref\terr_ratio\tminsec_ours\tminsec_ref\tdrop_ours\tdrop_ref\n"); | ||
| 2990 | + for r in &rows { | ||
| 2991 | + table.push_str(r); | ||
| 2992 | + table.push('\n'); | ||
| 2993 | + } | ||
| 2994 | + fs::write(&sweep_path, table).unwrap(); | ||
| 2995 | + println!("wrote {}", sweep_path.display()); | ||
| 2996 | + | ||
| 2997 | + // Rate gate: report violations, never panic. | ||
| 2998 | + if !rate_violations.is_empty() { | ||
| 2999 | + eprintln!("RATE GATE violations (|rate%| > 5):"); | ||
| 3000 | + for v in &rate_violations { | ||
| 3001 | + eprintln!(" {v}"); | ||
| 3002 | + } | ||
| 3003 | + } else { | ||
| 3004 | + println!("RATE GATE: all rows within ±5%"); | ||
| 3005 | + } | ||
| 3006 | + // Dropout gate: hard fail — a second the reference kept must not drop out. | ||
| 3007 | + assert!( | ||
| 3008 | + dropout_violations.is_empty(), | ||
| 3009 | + "DROPOUT GATE failed (ours dropped seconds ref kept):\n {}", | ||
| 3010 | + dropout_violations.join("\n ") | ||
| 3011 | + ); | ||
| 3012 | + println!("DROPOUT GATE: passed (no ours-only dropouts)"); | ||
| 3013 | +} | ||
lanes/opus-gate-r1.report.mdadded+26 -0
| @@ -0,0 +1,26 @@ | |||
| 1 | +# lane-opus-gate r1 — encoder gate vs libopus (verdict: GATE LANDED, FIRES on reelm@64k) | ||
| 2 | + | ||
| 3 | +VERDICT: err_ratio range 0.866–23.640 (worst reell@96k) — REPORT-ONLY, instrument unvalidated; dropout rows 1 (reelm@64k: ours 1 s corr<0.9, min .8987; ref 0, min .9100); rate drift worst −0.5%. RATE PASS, DROPOUT FAIL. | ||
| 4 | + | ||
| 5 | +Test: `encoder_library_gate_vs_libopus` (#[ignore]) in crates/ec-opus/tests/conformance.rs. 7 sources × {64,96} kbps, 120 s cap per source (was 600 s in charter; 14 rows took 12 min), both bitstreams decoded by ec-opus. Hard asserts: rate ±5%, no row where ours has dropout seconds and ref has none. | ||
| 6 | + | ||
| 7 | +## Findings | ||
| 8 | +- 12/14 rows our corr ≥ libopus corr at equal size; reelm@64 (+.0067) and hein@64 (+.0033) trail — same low-rate files as vorbis residual. | ||
| 9 | +- Dropout class `enc-dropout-lowrate-transient`: reelm@64 only; hein@64 (.9082), her@64 (.9462) are nearest. Encoder fix out of scope for this test-only lane → debt. | ||
| 10 | +- err_ratio column: Q values −29..−1056 mean the spectral error feeding the opus_compare mapping is not on opus_compare's scale (real Q is 0–100). Ratios 2–24× while time-domain corr is ≥ ref on the same rows → instrument suspect (alignment/phase-sensitive or unnormalised). NOT a gate until validated against opus_compare on one file. Previous "~2× weighted error" debt figure came from a different path; neither is validated. | ||
| 11 | + | ||
| 12 | +## Table (lanes/opus-gate-r1.sweep.txt) | ||
| 13 | +nik 64 68.9 69.2 -0.4 0.9875 0.9864 -0.0011 -530.65 -154.78 4.249 0.9758 0.9745 0 0 | ||
| 14 | +nik 96 100.9 101.0 -0.2 0.9944 0.9940 -0.0003 -347.70 -31.55 5.240 0.9884 0.9888 0 0 | ||
| 15 | +reelo 64 63.3 63.6 -0.5 0.9882 0.9866 -0.0016 -388.30 -123.36 3.165 0.9644 0.9686 0 0 | ||
| 16 | +reelo 96 93.7 93.9 -0.2 0.9946 0.9937 -0.0008 -266.53 -125.18 1.975 0.9855 0.9854 0 0 | ||
| 17 | +her 64 67.7 67.9 -0.4 0.9829 0.9794 -0.0035 -854.77 -339.00 4.842 0.9462 0.9406 0 0 | ||
| 18 | +her 96 101.3 101.5 -0.2 0.9914 0.9902 -0.0012 -899.19 -191.50 10.105 0.9725 0.9728 0 0 | ||
| 19 | +reell 64 71.2 71.3 -0.2 0.9903 0.9880 -0.0023 -1056.61 -128.39 21.263 0.9694 0.9644 0 0 | ||
| 20 | +reell 96 105.2 105.4 -0.2 0.9954 0.9948 -0.0006 -832.74 -29.16 23.640 0.9850 0.9804 0 0 | ||
| 21 | +reelm 64 63.3 63.3 -0.1 0.9793 0.9859 +0.0067 -767.51 -392.46 3.145 0.8987 0.9100 1 0 | ||
| 22 | +reelm 96 84.8 84.8 -0.0 0.9911 0.9920 +0.0009 -603.35 -231.16 3.674 0.9572 0.9511 0 0 | ||
| 23 | +dl8a 64 65.7 65.8 -0.2 0.9880 0.9865 -0.0015 -598.74 -378.84 2.032 0.9757 0.9723 0 0 | ||
| 24 | +dl8a 96 96.8 96.8 -0.0 0.9940 0.9934 -0.0007 -381.94 -423.47 0.866 0.9876 0.9864 0 0 | ||
| 25 | +hein 64 64.8 64.9 -0.0 0.9841 0.9874 +0.0033 -661.65 -388.31 2.364 0.9082 0.9148 0 0 | ||
| 26 | +hein 96 86.9 86.9 -0.0 0.9932 0.9930 -0.0002 -461.53 -246.46 2.211 0.9600 0.9499 0 0 | ||
lanes/opus-gate-r1.sweep.txtadded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 3 | +# 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 | ||
| 4 | +nik 64 68.9 69.2 -0.4 0.9875 0.9864 -0.0011 -530.65 -154.78 4.249 0.9758 0.9745 0 0 | ||
| 5 | +nik 96 100.9 101.0 -0.2 0.9944 0.9940 -0.0003 -347.70 -31.55 5.240 0.9884 0.9888 0 0 | ||
| 6 | +reelo 64 63.3 63.6 -0.5 0.9882 0.9866 -0.0016 -388.30 -123.36 3.165 0.9644 0.9686 0 0 | ||
| 7 | +reelo 96 93.7 93.9 -0.2 0.9946 0.9937 -0.0008 -266.53 -125.18 1.975 0.9855 0.9854 0 0 | ||
| 8 | +her 64 67.7 67.9 -0.4 0.9829 0.9794 -0.0035 -854.77 -339.00 4.842 0.9462 0.9406 0 0 | ||
| 9 | +her 96 101.3 101.5 -0.2 0.9914 0.9902 -0.0012 -899.19 -191.50 10.105 0.9725 0.9728 0 0 | ||
| 10 | +reell 64 71.2 71.3 -0.2 0.9903 0.9880 -0.0023 -1056.61 -128.39 21.263 0.9694 0.9644 0 0 | ||
| 11 | +reell 96 105.2 105.4 -0.2 0.9954 0.9948 -0.0006 -832.74 -29.16 23.640 0.9850 0.9804 0 0 | ||
| 12 | +reelm 64 63.3 63.3 -0.1 0.9793 0.9859 +0.0067 -767.51 -392.46 3.145 0.8987 0.9100 1 0 | ||
| 13 | +reelm 96 84.8 84.8 -0.0 0.9911 0.9920 +0.0009 -603.35 -231.16 3.674 0.9572 0.9511 0 0 | ||
| 14 | +dl8a 64 65.7 65.8 -0.2 0.9880 0.9865 -0.0015 -598.74 -378.84 2.032 0.9757 0.9723 0 0 | ||
| 15 | +dl8a 96 96.8 96.8 -0.0 0.9940 0.9934 -0.0007 -381.94 -423.47 0.866 0.9876 0.9864 0 0 | ||
| 16 | +hein 64 64.8 64.9 -0.0 0.9841 0.9874 +0.0033 -661.65 -388.31 2.364 0.9082 0.9148 0 0 | ||
| 17 | +hein 96 86.9 86.9 -0.0 0.9932 0.9930 -0.0002 -461.53 -246.46 2.211 0.9600 0.9499 0 0 | ||