forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

plc.rs121 lines, 4.0 KiBHistoryRawDownload
1use ec_opus::{Application, Decoder, Encoder, Mode};
2
3const FRAME: usize = 960;
4
5fn voice(frames: usize) -> Vec<f32> {
6 (0..frames * FRAME)
7 .map(|i| {
8 let t = i as f32 / 48000.0;
9 let f0 = 140.0 + 10.0 * (2.0 * std::f32::consts::PI * 0.7 * t).sin();
10 (1..12)
11 .map(|h| (2.0 * std::f32::consts::PI * f0 * h as f32 * t).sin() * 0.25 / h as f32)
12 .sum()
13 })
14 .collect()
15}
16
17fn corr(a: &[f32], b: &[f32]) -> f32 {
18 let ab: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
19 let aa: f32 = a.iter().map(|x| x * x).sum();
20 let bb: f32 = b.iter().map(|x| x * x).sum();
21 ab / (aa * bb).sqrt().max(1e-9)
22}
23
24fn rms(a: &[f32]) -> f32 {
25 (a.iter().map(|x| x * x).sum::<f32>() / a.len() as f32).sqrt()
26}
27
28#[test]
29fn concealment_tracks_and_recovers() {
30 let pcm = voice(60);
31 let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap();
32 enc.set_bitrate(32000);
33 enc.set_mode(Some(Mode::Celt));
34 let mut pkt = vec![0u8; 1500];
35 let packets: Vec<Vec<u8>> = pcm
36 .chunks_exact(FRAME)
37 .map(|f| {
38 let n = enc.encode_float(f, FRAME, &mut pkt).unwrap();
39 pkt[..n].to_vec()
40 })
41 .collect();
42 let lost = [20usize, 40, 41, 42, 43, 44, 45, 46, 47];
43 let mut clean = Decoder::new(48000, 1).unwrap();
44 let mut lossy = Decoder::new(48000, 1).unwrap();
45 let mut buf = vec![0f32; 5760];
46 let (mut a, mut b) = (Vec::new(), Vec::new());
47 for (i, p) in packets.iter().enumerate() {
48 let k = clean.decode_float(p, &mut buf).unwrap();
49 a.extend_from_slice(&buf[..k]);
50 let k = if lost.contains(&i) {
51 lossy.decode_float(&[], &mut buf).unwrap()
52 } else {
53 lossy.decode_float(p, &mut buf).unwrap()
54 };
55 b.extend_from_slice(&buf[..k]);
56 }
57 let f = |i: usize| i * FRAME..(i + 1) * FRAME;
58 let c20 = corr(&a[f(20)], &b[f(20)]);
59 let r20 = rms(&b[f(20)]) / rms(&a[f(20)]);
60 let c22 = corr(&a[f(22)], &b[f(22)]);
61 let r47 = rms(&b[f(47)]) / rms(&a[f(47)]);
62 let c50 = corr(&a[f(50)], &b[f(50)]);
63 let jump = b
64 .windows(2)
65 .map(|w| (w[1] - w[0]).abs())
66 .fold(0.0f32, f32::max);
67 let jump_ref = a
68 .windows(2)
69 .map(|w| (w[1] - w[0]).abs())
70 .fold(0.0f32, f32::max);
71 eprintln!(
72 "c20 {c20:.3} r20 {r20:.3} c22 {c22:.3} r47 {r47:.3} c50 {c50:.3} jump {jump:.4} ref {jump_ref:.4}"
73 );
74 assert!(c20 > 0.5 && (0.5..1.5).contains(&r20));
75 assert!(c22 > 0.9);
76 assert!(r47 < 0.5);
77 assert!(c50 > 0.9);
78 assert!(jump < 2.0 * jump_ref);
79}
80
81#[test]
82fn inband_fec_recovers_a_lost_frame() {
83 let pcm = voice(40);
84 let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap();
85 enc.set_bitrate(48000);
86 enc.set_bandwidth(Some(ec_opus::Bandwidth::Full));
87 enc.set_inband_fec(true);
88 enc.set_packet_loss_perc(15);
89 let mut pkt = vec![0u8; 1500];
90 let packets: Vec<Vec<u8>> = pcm
91 .chunks_exact(FRAME)
92 .map(|f| {
93 let n = enc.encode_float(f, FRAME, &mut pkt).unwrap();
94 pkt[..n].to_vec()
95 })
96 .collect();
97 let mut clean = Decoder::new(48000, 1).unwrap();
98 let mut lossy = Decoder::new(48000, 1).unwrap();
99 let mut buf = vec![0f32; 5760];
100 let (mut a, mut b) = (Vec::new(), Vec::new());
101 for (i, p) in packets.iter().enumerate() {
102 let k = clean.decode_float(p, &mut buf).unwrap();
103 a.extend_from_slice(&buf[..k]);
104 let k = if i == 20 {
105 lossy.decode_fec(&packets[21], FRAME, &mut buf).unwrap()
106 } else if i == 30 {
107 lossy.conceal(FRAME, &mut buf).unwrap()
108 } else {
109 lossy.decode_float(p, &mut buf).unwrap()
110 };
111 b.extend_from_slice(&buf[..k]);
112 }
113 let f = |i: usize| i * FRAME..(i + 1) * FRAME;
114 let c20 = corr(&a[f(20)], &b[f(20)]);
115 let c22 = corr(&a[f(22)], &b[f(22)]);
116 let c30 = corr(&a[f(30)], &b[f(30)]);
117 eprintln!("fec c20 {c20:.3} c22 {c22:.3} plc c30 {c30:.3}");
118 assert!(c30 > 0.3);
119 assert!(c20 > 0.7);
120 assert!(c22 > 0.95);
121}