1#[cfg(feature = "plc")]
2use super::plc::PitchConcealer;
3use super::{CodecError, Decoder, Encoder, Sample};
4#[cfg(feature = "plc")]
5use alloc::boxed::Box;
6
7const BIAS: i16 = 0x84;
8const CLIP: i16 = 32635;
9
10const fn linear2ulaw_algo(mut sample: i16) -> u8 {
11 if sample > CLIP {
13 sample = CLIP;
14 } else if sample < -CLIP {
15 sample = -CLIP;
16 }
17
18 let sign: i16 = if sample < 0 { 0x80 } else { 0x00 };
20 if sign == 0x80 {
21 sample = -sample;
22 }
23
24 sample += BIAS;
26
27 let mut segment: i16 = 0;
29 let mut value = sample;
30 while value >= 256 {
31 segment += 1;
32 value >>= 1;
33 }
34
35 let uval = if segment >= 8 {
37 0x7F ^ sign
38 } else {
39 (sign | (segment << 4) | ((sample >> (segment + 3)) & 0x0F)) ^ 0xFF
45 };
46
47 uval as u8
48}
49
50static MULAW_ENCODE_TABLE: [u8; 65536] = {
51 let mut table = [0; 65536];
52 let mut i = 0;
53 while i < 65536 {
54 let s = (i as i32 - 32768) as i16;
55 table[i] = linear2ulaw_algo(s);
56 i += 1;
57 }
58 table
59};
60
61static MULAW_DECODE_TABLE: [i16; 256] = [
62 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860,
63 -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
64 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908,
65 -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772,
66 -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108,
67 -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180,
68 -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556,
69 -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212,
70 -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32,
71 -24, -16, -8, 0, 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884,
72 20860, 19836, 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
73 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396,
74 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132,
75 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500,
76 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876, 844, 812, 780, 748, 716, 684, 652,
77 620, 588, 556, 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276, 260, 244, 228, 212,
78 196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0,
79];
80
81fn decode_mu_law(mu_law_sample: u8) -> i16 {
83 MULAW_DECODE_TABLE[mu_law_sample as usize]
84}
85
86pub struct PcmuDecoder {
88 #[cfg(feature = "plc")]
90 plc: Option<Box<PitchConcealer>>,
91}
92
93impl PcmuDecoder {
94 pub fn new() -> Self {
96 Self {
97 #[cfg(feature = "plc")]
98 plc: None,
99 }
100 }
101}
102
103impl Default for PcmuDecoder {
104 fn default() -> Self {
105 Self::new()
106 }
107}
108
109impl Decoder for PcmuDecoder {
110 fn decode_into(&mut self, data: &[u8], out: &mut [Sample]) -> Result<usize, CodecError> {
111 if out.len() < data.len() {
112 return Err(CodecError::BufferTooSmall);
113 }
114 for (i, &sample) in data.iter().enumerate() {
115 out[i] = decode_mu_law(sample);
116 }
117 #[cfg(feature = "plc")]
118 if let Some(plc) = &mut self.plc {
119 plc.on_decoded(&mut out[..data.len()]);
120 }
121 Ok(data.len())
122 }
123
124 #[cfg(feature = "plc")]
125 fn enable_concealment(&mut self) -> bool {
126 self.plc
127 .get_or_insert_with(|| Box::new(PitchConcealer::new(8000)));
128 true
129 }
130
131 #[cfg(feature = "plc")]
133 fn conceal_into(&mut self, samples: usize, out: &mut [Sample]) -> Result<usize, CodecError> {
134 let Some(plc) = &mut self.plc else {
135 return Ok(0);
136 };
137 let out = out.get_mut(..samples).ok_or(CodecError::BufferTooSmall)?;
138 plc.conceal(out);
139 Ok(samples)
140 }
141
142 fn max_decode_samples(&self, n_bytes: usize) -> usize {
143 n_bytes
144 }
145
146 fn sample_rate(&self) -> u32 {
147 8000
148 }
149
150 fn channels(&self) -> u16 {
151 1
152 }
153}
154
155#[derive(Default)]
157pub struct PcmuEncoder {}
158
159impl PcmuEncoder {
160 pub fn new() -> Self {
162 Self {}
163 }
164
165 fn linear2ulaw(&self, sample: i16) -> u8 {
167 let index = (sample as i32 + 32768) as usize;
168 MULAW_ENCODE_TABLE[index]
169 }
170}
171
172impl Encoder for PcmuEncoder {
173 fn encode_into(&mut self, samples: &[Sample], out: &mut [u8]) -> Result<usize, CodecError> {
174 if out.len() < samples.len() {
175 return Err(CodecError::BufferTooSmall);
176 }
177 for (i, &sample) in samples.iter().enumerate() {
178 out[i] = self.linear2ulaw(sample);
179 }
180 Ok(samples.len())
181 }
182
183 fn max_encode_bytes(&self, n_samples: usize) -> usize {
184 n_samples
185 }
186
187 fn sample_rate(&self) -> u32 {
188 8000
189 }
190
191 fn channels(&self) -> u16 {
192 1
193 }
194}