forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

snr_floor.rs116 lines, 4.5 KiBHistoryRawDownload
1//! Encoder quality floor (lane opusq): a music-like signal (chords, noise, transients) must
2//! round-trip through the encoder and the decoder above a per-bitrate SNR floor. The floors sit
3//! about 0.1-0.5 dB under what the encoder measures after the continuous allocation trim and the
4//! analysis-driven leak boost; libopus (CELT, restricted-lowdelay, CBR) measures within ~1 dB
5//! above these on the same signal shape, so a regression of the allocation shows here.
6use ec_opus::{Application, Decoder, Encoder, MultistreamDecoder, MultistreamEncoder};
7
8const FRAME: usize = 960;
9
10/// Hides a constant chunk size from clippy's `as_chunks` lint.
11fn channels_of(n: usize) -> usize {
12 std::hint::black_box(n)
13}
14
15struct Lcg(u64);
16impl Lcg {
17 fn next(&mut self) -> f32 {
18 self.0 = self
19 .0
20 .wrapping_mul(6364136223846793005)
21 .wrapping_add(1442695040888963407);
22 ((self.0 >> 33) as f32 / (1u64 << 31) as f32) * 2.0 - 1.0
23 }
24}
25
26/// `channels` interleaved channels, `secs` seconds at 48 kHz.
27fn music(channels: usize, secs: f32, seed: u64) -> Vec<f32> {
28 let n = (48000.0 * secs) as usize;
29 let mut rng = Lcg(seed);
30 let notes = [220.0f32, 277.2, 329.6, 440.0, 554.4, 659.3];
31 let mut out = vec![0f32; n * channels];
32 for c in 0..channels {
33 let phases: Vec<f32> = notes.iter().map(|_| rng.next() * 3.0).collect();
34 for t in 0..n {
35 let tt = t as f32 / 48000.0;
36 let f0 = [1.0f32, 1.122, 1.26, 1.5][(tt as usize) % 4];
37 let mut v = 0.0;
38 for (k, f) in notes.iter().enumerate() {
39 for h in 1..=5 {
40 v += 0.08 / h as f32
41 * (2.0 * core::f32::consts::PI * f * f0 * h as f32 * tt + phases[k]).sin();
42 }
43 }
44 v += 0.004 * rng.next();
45 let since = t % 12000;
46 if since < 3000 {
47 v += 0.15 * rng.next() * (-(since as f32) / 500.0).exp();
48 }
49 out[t * channels + c] = v * 0.6;
50 }
51 }
52 out
53}
54
55fn snr_db(reference: &[f32], decoded: &[f32], channels: usize) -> f32 {
56 let n = reference.len().min(decoded.len()) / channels;
57 let (lo, hi) = (2000 * channels, n.saturating_sub(2000) * channels);
58 let (mut s, mut e) = (0f64, 0f64);
59 for i in lo..hi {
60 s += f64::from(reference[i]).powi(2);
61 e += f64::from(reference[i] - decoded[i]).powi(2);
62 }
63 (10.0 * (s / e).log10()) as f32
64}
65
66fn stereo_snr(kbps: u32) -> f32 {
67 let mono = music(1, 1.5, 3);
68 let mut pcm = music(2, 1.5, 7);
69 // Correlated channels (a common part plus a private part), as in a real mix.
70 for (t, f) in pcm.chunks_exact_mut(channels_of(2)).enumerate() {
71 let (a, b) = (f[0], f[1]);
72 f[0] = 0.8 * mono[t] + 0.2 * a;
73 f[1] = 0.6 * mono[t] + 0.4 * b;
74 }
75 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();
76 enc.set_bitrate(kbps * 1000);
77 enc.set_vbr_constrained(false);
78 let skip = enc.look_ahead(FRAME);
79 let mut dec = Decoder::new(48000, 2).unwrap();
80 let (mut out, mut buf, mut pkt) = (Vec::new(), vec![0f32; 5760 * 2], vec![0u8; 1500]);
81 for f in pcm.chunks_exact(channels_of(FRAME * 2)) {
82 let n = enc.encode_float(f, FRAME, &mut pkt).unwrap();
83 let k = dec.decode_float(&pkt[..n], &mut buf).unwrap();
84 out.extend_from_slice(&buf[..k * 2]);
85 }
86 snr_db(&pcm, &out[skip * 2..], 2)
87}
88
89#[test]
90fn stereo_snr_floor() {
91 for (kbps, floor) in [(64, 17.0), (96, 19.3), (128, 22.0), (256, 26.7)] {
92 let snr = stereo_snr(kbps);
93 eprintln!("stereo {kbps}k snr {snr:.2}");
94 assert!(snr >= floor, "{kbps} kbit/s: {snr:.2} dB < floor {floor}");
95 }
96}
97
98#[test]
99fn surround_5_1_snr_floor() {
100 let pcm = music(6, 0.8, 11);
101 let mut enc = MultistreamEncoder::surround(48000, 6, Application::Audio).unwrap();
102 enc.set_bitrate(256_000);
103 enc.set_vbr_constrained(false);
104 let skip = enc.look_ahead(FRAME);
105 let table = ec_opus::ogg::default_mapping(6).unwrap().3;
106 let mut dec = MultistreamDecoder::with_rate(48000, 4, 2, &table);
107 let (mut out, mut buf, mut pkt) = (Vec::new(), vec![0f32; 5760 * 6], vec![0u8; 4000]);
108 for f in pcm.chunks_exact(channels_of(FRAME * 6)) {
109 let n = enc.encode_float(f, FRAME, &mut pkt).unwrap();
110 let k = dec.decode_float(&pkt[..n], &mut buf).unwrap();
111 out.extend_from_slice(&buf[..k * 6]);
112 }
113 let snr = snr_db(&pcm, &out[skip * 6..], 6);
114 eprintln!("5.1 256k snr {snr:.2}");
115 assert!(snr >= 15.0, "5.1 256 kbit/s: {snr:.2} dB");
116}