1#[cfg(feature = "alloc")]
13extern crate alloc;
14
15use super::{
16 arithmetic_codec::{self, ArithmeticData, ArithmeticDecodeError},
17 buffer_reader::BufferReader,
18 global_gain,
19 long_term_post_filter::LongTermPostFilter,
20 modified_dct::ModDiscreteCosTrans,
21 noise_filling, output_scaling,
22 packet_loss_concealment::PacketLossConcealment,
23 residual_spectrum,
24 side_info::SideInfo,
25 side_info_reader::{self, SideInfoError},
26 spectral_noise_shaping, temporal_noise_shaping,
27};
28use crate::common::{
29 complex::{Complex, Scaler},
30 config::{FrameDuration, Lc3Config, SamplingFrequency},
31 constants::MAX_LEN_SPECTRAL,
32};
33
34#[derive(Debug)]
37pub enum Lc3DecoderError {
38 SideInfo(SideInfoError),
39 ArithmeticDecode(ArithmeticDecodeError),
40 InvalidSampleOutBuffer(OutputBufferErrorDetails),
41 Only16BitsPerAudioSampleSupported,
42}
43
44#[derive(Debug)]
45pub struct OutputBufferErrorDetails {
46 pub required_length: usize,
47 pub actual_length: usize,
48}
49
50#[cfg(feature = "alloc")]
51pub struct Lc3Decoder<'a> {
52 config: Lc3Config,
53 channels: alloc::vec::Vec<DecoderChannel<'a>>,
54}
55
56#[cfg(not(feature = "alloc"))]
57pub struct Lc3Decoder<'a, const NUM_CHANNELS: usize = 2> {
58 config: Lc3Config,
59 channels: heapless::Vec<DecoderChannel<'a>, NUM_CHANNELS>,
60}
61
62struct DecoderChannel<'a> {
63 spec_lines: &'a mut [Scaler], freq_samples: &'a mut [Scaler], packet_loss: PacketLossConcealment<'a>,
66 modified_dct: ModDiscreteCosTrans<'a>,
67 post_filter: LongTermPostFilter<'a>,
68 frame_index: usize,
69}
70
71impl<'a> DecoderChannel<'a> {
72 pub fn decode(
74 &mut self,
75 config: &Lc3Config,
76 num_bits_per_audio_sample: usize,
77 buf_in: &[u8],
78 samples_out: &mut [i16],
79 ) -> Result<(), Lc3DecoderError> {
80 if num_bits_per_audio_sample != 16 {
81 return Err(Lc3DecoderError::Only16BitsPerAudioSampleSupported);
82 }
83
84 self.frame_index += 1;
85 let nbits = buf_in.len() * 8;
86
87 let mut spec_lines_int = [0; MAX_LEN_SPECTRAL];
89
90 let long_term_post_filter_info = match read_frame(buf_in, config, &mut spec_lines_int) {
92 Ok((side_info, arithmetic_data)) => {
93 for (to, from) in self.spec_lines.iter_mut().zip(&spec_lines_int[..config.ne]) {
95 *to = *from as Scaler;
96 }
97
98 residual_spectrum::decode(side_info.lsb_mode, &arithmetic_data.residual_bits, self.spec_lines);
100
101 noise_filling::apply_noise_filling(
103 arithmetic_data.is_zero_frame,
104 arithmetic_data.noise_filling_seed,
105 side_info.bandwidth,
106 config.n_ms,
107 side_info.noise_factor,
108 &spec_lines_int,
109 self.spec_lines,
110 );
111
112 global_gain::apply_global_gain(
114 arithmetic_data.frame_num_bits,
115 config.fs_ind,
116 side_info.global_gain_index,
117 self.spec_lines,
118 );
119
120 temporal_noise_shaping::apply_temporal_noise_shaping(
122 config.n_ms,
123 side_info.bandwidth,
124 side_info.num_tns_filters,
125 &arithmetic_data.reflect_coef_order,
126 &arithmetic_data.reflect_coef_ints,
127 self.spec_lines,
128 );
129
130 spectral_noise_shaping::decode(config, &side_info.sns_vq, self.spec_lines);
132
133 self.packet_loss.save(self.spec_lines);
135
136 side_info.long_term_post_filter_info
137 }
138 Err(_e) => {
139 self.packet_loss.load_into(self.spec_lines)
141 }
142 };
143
144 self.modified_dct.run(self.spec_lines, self.freq_samples);
146
147 self.post_filter
149 .run(&long_term_post_filter_info, nbits, self.freq_samples);
150
151 output_scaling::scale_and_round(self.freq_samples, 16, samples_out);
153 Ok(())
154 }
155
156 pub const fn calc_working_buffer_lengths(config: &Lc3Config) -> (usize, usize) {
157 let (dct_scaler_length, dct_complex_length) = ModDiscreteCosTrans::calc_working_buffer_length(config);
158 let packet_loss_length = PacketLossConcealment::calc_working_buffer_length(config);
159 let long_term_length = LongTermPostFilter::calc_working_buffer_length(config);
160 let scaler_length = config.ne + packet_loss_length + dct_scaler_length + long_term_length;
161 (scaler_length, dct_complex_length)
162 }
163}
164
165fn read_frame(buf: &[u8], config: &Lc3Config, x: &mut [i32]) -> Result<(SideInfo, ArithmeticData), Lc3DecoderError> {
166 let mut reader = BufferReader::new();
167
168 let side_info =
170 side_info_reader::read(buf, &mut reader, config.fs_ind, config.ne).map_err(Lc3DecoderError::SideInfo)?;
171
172 let arithmetic_data =
173 arithmetic_codec::decode(buf, &mut reader, config.fs_ind, config.ne, &side_info, &config.n_ms, x)
174 .map_err(Lc3DecoderError::ArithmeticDecode)?;
175
176 Ok((side_info, arithmetic_data))
177}
178
179#[cfg(feature = "alloc")]
180impl<'a> Lc3Decoder<'a> {
181 pub fn new(
182 num_channels: usize,
183 frame_duration: FrameDuration,
184 sampling_frequency: SamplingFrequency,
185 scaler_buf: &'a mut [Scaler],
186 complex_buf: &'a mut [Complex],
187 ) -> Self {
188 let mut channels = alloc::vec::Vec::new();
189 let config = Lc3Config::new(sampling_frequency, frame_duration);
190 let mut scaler_buf_saved = scaler_buf;
191 let mut complex_buf_saved = complex_buf;
192
193 for _ in 0..num_channels {
194 let (x_hat, scaler_buf) = scaler_buf_saved.split_at_mut(config.ne);
195 let (packet_loss, scaler_buf) = PacketLossConcealment::new(config.ne, scaler_buf);
196 let (mdct, scaler_buf, complex_buf) = ModDiscreteCosTrans::new(config, scaler_buf, complex_buf_saved);
197 let (post_filter, scaler_buf) = LongTermPostFilter::new(config, scaler_buf);
198 let (freq_buf, scaler_buf) = scaler_buf.split_at_mut(config.nf);
199
200 let channel = DecoderChannel {
201 spec_lines: x_hat,
202 packet_loss,
203 modified_dct: mdct,
204 post_filter,
205 frame_index: 0,
206 freq_samples: freq_buf,
207 };
208
209 channels.push(channel);
210 scaler_buf_saved = scaler_buf;
211 complex_buf_saved = complex_buf;
212 }
213
214 Self { config, channels }
215 }
216
217 pub fn decode_frame(
218 &mut self,
219 num_bits_per_audio_sample: usize, channel_index: usize,
221 buf_in: &[u8],
222 samples_out: &mut [i16],
223 ) -> Result<(), Lc3DecoderError> {
224 if channel_index < self.channels.len() {
225 let channel = &mut self.channels[channel_index];
226 channel.decode(&self.config, num_bits_per_audio_sample, buf_in, samples_out)
227 } else {
228 panic!(
229 "Cannot decode channel index {} as config only specifies {} channels",
230 channel_index,
231 self.channels.len()
232 );
233 }
234 }
235
236 pub const fn calc_working_buffer_lengths(
237 num_channels: usize,
238 frame_duration: FrameDuration,
239 sampling_frequency: SamplingFrequency,
240 ) -> (usize, usize) {
241 let config = Lc3Config::new(sampling_frequency, frame_duration);
242 let (scaler_length, complex_length) = DecoderChannel::calc_working_buffer_lengths(&config);
243 (num_channels * scaler_length, num_channels * complex_length)
244 }
245}
246
247#[cfg(not(feature = "alloc"))]
248impl<'a, const NUM_CHANNELS: usize> Lc3Decoder<'a, NUM_CHANNELS> {
249 pub fn new(
250 frame_duration: FrameDuration,
251 sampling_frequency: SamplingFrequency,
252 scaler_buf: &'a mut [Scaler],
253 complex_buf: &'a mut [Complex],
254 ) -> Self {
255 let config = Lc3Config::new(sampling_frequency, frame_duration);
256 let mut channels: heapless::Vec<DecoderChannel<'a>, NUM_CHANNELS> = heapless::Vec::new();
257 let mut scaler_buf_saved = scaler_buf;
258 let mut complex_buf_saved = complex_buf;
259
260 for _ in 0..NUM_CHANNELS {
261 let (x_hat, scaler_buf) = scaler_buf_saved.split_at_mut(config.ne);
262 let (packet_loss, scaler_buf) = PacketLossConcealment::new(config.ne, scaler_buf);
263 let (mdct, scaler_buf, complex_buf) = ModDiscreteCosTrans::new(config, scaler_buf, complex_buf_saved);
264 let (post_filter, scaler_buf) = LongTermPostFilter::new(config, scaler_buf);
265 let (freq_buf, scaler_buf) = scaler_buf.split_at_mut(config.nf);
266
267 let channel = DecoderChannel {
268 spec_lines: x_hat,
269 packet_loss,
270 modified_dct: mdct,
271 post_filter,
272 frame_index: 0,
273 freq_samples: freq_buf,
274 };
275
276 channels.push(channel).ok();
277 scaler_buf_saved = scaler_buf;
278 complex_buf_saved = complex_buf;
279 }
280
281 Self { config, channels }
282 }
283
284 pub fn decode_frame(
285 &mut self,
286 num_bits_per_audio_sample: usize, channel_index: usize,
288 buf_in: &[u8],
289 samples_out: &mut [i16],
290 ) -> Result<(), Lc3DecoderError> {
291 if channel_index < NUM_CHANNELS {
292 let channel = &mut self.channels[channel_index];
293 channel.decode(&self.config, num_bits_per_audio_sample, buf_in, samples_out)
294 } else {
295 panic!(
296 "Cannot decode channel index {} as config only specifies {} channels",
297 channel_index, NUM_CHANNELS
298 );
299 }
300 }
301
302 pub const fn calc_working_buffer_lengths(
303 frame_duration: FrameDuration,
304 sampling_frequency: SamplingFrequency,
305 ) -> (usize, usize) {
306 let config = Lc3Config::new(sampling_frequency, frame_duration);
307 let (scaler_length, complex_length) = DecoderChannel::calc_working_buffer_lengths(&config);
308 (NUM_CHANNELS * scaler_length, NUM_CHANNELS * complex_length)
309 }
310}
311
312#[cfg(test)]
313mod tests {
314 extern crate std;
315 use super::*;
316 use crate::common::config::{FrameDuration, SamplingFrequency};
317
318 #[cfg(not(feature = "alloc"))]
319 #[test]
320 fn lc3_decode_channel() {
321 const NUM_CH: usize = 1;
322 const FREQ: SamplingFrequency = SamplingFrequency::Hz48000;
323 const DURATION: FrameDuration = FrameDuration::TenMs;
324 const SCALER_COMPLEX_LENS: (usize, usize) = Lc3Decoder::<NUM_CH>::calc_working_buffer_lengths(DURATION, FREQ);
325 let mut scaler_buf = [0.0; SCALER_COMPLEX_LENS.0];
326 let mut complex_buf = [Complex::default(); SCALER_COMPLEX_LENS.1];
327 let mut decoder = Lc3Decoder::<NUM_CH>::new(DURATION, FREQ, &mut scaler_buf, &mut complex_buf);
328 let buf_in = [
329 187, 56, 111, 155, 76, 236, 70, 99, 10, 135, 219, 76, 176, 3, 108, 203, 131, 111, 206, 221, 195, 25, 96,
330 240, 18, 202, 163, 241, 109, 142, 198, 122, 176, 70, 37, 6, 35, 190, 110, 184, 251, 162, 71, 7, 151, 58,
331 42, 79, 200, 192, 99, 157, 234, 156, 245, 43, 84, 64, 167, 32, 52, 106, 43, 75, 4, 102, 213, 123, 168, 120,
332 213, 252, 208, 118, 78, 115, 154, 158, 157, 26, 152, 231, 121, 146, 203, 11, 169, 227, 75, 154, 237, 154,
333 227, 145, 196, 182, 207, 94, 95, 26, 184, 248, 1, 118, 72, 47, 18, 205, 56, 96, 195, 139, 216, 240, 113,
334 233, 44, 198, 245, 157, 139, 70, 162, 182, 139, 136, 165, 68, 79, 247, 161, 126, 17, 135, 36, 30, 229, 24,
335 196, 2, 5, 65, 111, 80, 124, 168, 70, 156, 198, 60,
336 ];
337 let mut samples_out = [0; 480];
338
339 decoder.decode_frame(16, 0, &buf_in, &mut samples_out).unwrap();
340
341 let samples_out_expected = [
342 0, 1, 1, 1, 0, -1, -2, -3, -5, -7, -9, -10, -11, -11, -9, -6, -1, 4, 13, 24, 37, 53, 71, 91, 107, 122, 138,
343 148, 153, 153, 148, 140, 128, 114, 102, 90, 82, 80, 80, 86, 97, 108, 121, 135, 152, 166, 185, 204, 221,
344 245, 263, 276, 291, 287, 271, 251, 212, 161, 100, 33, -34, -108, -181, -250, -310, -351, -391, -416, -413,
345 -414, -405, -383, -373, -355, -336, -323, -303, -285, -270, -255, -240, -237, -251, -271, -315, -379, -451,
346 -541, -631, -712, -787, -841, -872, -875, -838, -793, -726, -615, -499, -381, -268, -164, -59, 22, 96, 163,
347 217, 252, 254, 261, 238, 190, 149, 90, 34, -26, -62, -88, -126, -119, -119, -100, -61, -31, 36, 65, 107,
348 153, 146, 185, 194, 180, 192, 185, 195, 188, 196, 213, 210, 226, 246, 291, 325, 371, 437, 466, 534, 586,
349 609, 658, 663, 660, 629, 547, 471, 336, 143, -59, -288, -532, -760, -983, -1161, -1289, -1401, -1453,
350 -1463, -1431, -1346, -1249, -1109, -952, -786, -607, -451, -292, -165, -51, 58, 117, 192, 236, 233, 241,
351 225, 214, 199, 177, 186, 189, 209, 248, 263, 289, 312, 322, 327, 299, 257, 201, 125, 38, -72, -174, -269,
352 -366, -454, -521, -555, -593, -591, -554, -534, -466, -399, -338, -250, -189, -108, -40, 19, 115, 197, 289,
353 380, 484, 596, 678, 772, 850, 914, 969, 994, 1033, 1071, 1117, 1174, 1231, 1291, 1344, 1401, 1429, 1449,
354 1456, 1408, 1357, 1255, 1102, 950, 752, 531, 301, 52, -183, -416, -636, -818, -971, -1092, -1171, -1205,
355 -1190, -1128, -1036, -909, -754, -594, -420, -233, -67, 75, 203, 300, 347, 370, 363, 291, 206, 97, -45,
356 -158, -296, -434, -536, -644, -705, -740, -771, -776, -796, -811, -839, -879, -876, -834, -780, -726, -671,
357 -654, -665, -697, -750, -770, -807, -841, -821, -800, -727, -592, -408, -131, 163, 431, 649, 789, 928,
358 1073, 1220, 1405, 1543, 1615, 1646, 1599, 1525, 1449, 1391, 1392, 1453, 1574, 1663, 1714, 1716, 1657, 1665,
359 1680, 1691, 1745, 1772, 1795, 1788, 1766, 1721, 1622, 1541, 1433, 1335, 1264, 1200, 1204, 1200, 1216, 1266,
360 1291, 1364, 1484, 1626, 1779, 1943, 2096, 2211, 2333, 2450, 2541, 2613, 2656, 2664, 2680, 2713, 2737, 2807,
361 2839, 2815, 2778, 2662, 2564, 2475, 2405, 2441, 2508, 2646, 2776, 2832, 2869, 2843, 2814, 2789, 2786, 2815,
362 2829, 2899, 2963, 2994, 3007, 2944, 2846, 2728, 2625, 2556, 2495, 2479, 2459, 2399, 2285, 2089, 1879, 1689,
363 1533, 1498, 1556, 1667, 1817, 1929, 2020, 2052, 2031, 2026, 1927, 1769, 1546, 1234, 947, 633, 349, 107,
364 -156, -372, -563, -698, -784, -830, -828, -870, -934, -1060, -1257, -1491, -1747, -1956, -2120, -2175,
365 -2178, -2164, -2072, -1994, -1873, -1727, -1603, -1451, -1341, -1245, -1193, -1172, -1139, -1138, -1080,
366 -980, -853, -684, -529, -397, -328, -312, -387, -564, -784, -1066, -1359, -1629, -1854, -2020, -2164,
367 -2266, -2337, -2388, -2406, -2382, -2338, -2307, -2263, -2233,
368 ];
369 assert_eq!(samples_out, samples_out_expected);
370 }
371
372 #[cfg(feature = "alloc")]
373 #[test]
374 fn lc3_decode_channel() {
375 const NUM_CH: usize = 1;
376 const FREQ: SamplingFrequency = SamplingFrequency::Hz48000;
377 const DURATION: FrameDuration = FrameDuration::TenMs;
378 const SCALER_COMPLEX_LENS: (usize, usize) = Lc3Decoder::calc_working_buffer_lengths(1, DURATION, FREQ);
379
380 let mut scaler_buf = [0.0; SCALER_COMPLEX_LENS.0];
381 let mut complex_buf = [Complex::default(); SCALER_COMPLEX_LENS.1];
382 let mut decoder = Lc3Decoder::new(NUM_CH, DURATION, FREQ, &mut scaler_buf, &mut complex_buf);
383 let buf_in = [
384 187, 56, 111, 155, 76, 236, 70, 99, 10, 135, 219, 76, 176, 3, 108, 203, 131, 111, 206, 221, 195, 25, 96,
385 240, 18, 202, 163, 241, 109, 142, 198, 122, 176, 70, 37, 6, 35, 190, 110, 184, 251, 162, 71, 7, 151, 58,
386 42, 79, 200, 192, 99, 157, 234, 156, 245, 43, 84, 64, 167, 32, 52, 106, 43, 75, 4, 102, 213, 123, 168, 120,
387 213, 252, 208, 118, 78, 115, 154, 158, 157, 26, 152, 231, 121, 146, 203, 11, 169, 227, 75, 154, 237, 154,
388 227, 145, 196, 182, 207, 94, 95, 26, 184, 248, 1, 118, 72, 47, 18, 205, 56, 96, 195, 139, 216, 240, 113,
389 233, 44, 198, 245, 157, 139, 70, 162, 182, 139, 136, 165, 68, 79, 247, 161, 126, 17, 135, 36, 30, 229, 24,
390 196, 2, 5, 65, 111, 80, 124, 168, 70, 156, 198, 60,
391 ];
392 let mut samples_out = [0; 480];
393
394 decoder.decode_frame(16, 0, &buf_in, &mut samples_out).unwrap();
395
396 let samples_out_expected = [
397 0, 1, 1, 1, 0, -1, -2, -3, -5, -7, -9, -10, -11, -11, -9, -6, -1, 4, 13, 24, 37, 53, 71, 91, 107, 122, 138,
398 148, 153, 153, 148, 140, 128, 114, 102, 90, 82, 80, 80, 86, 97, 108, 121, 135, 152, 166, 185, 204, 221,
399 245, 263, 276, 291, 287, 271, 251, 212, 161, 100, 33, -34, -108, -181, -250, -310, -351, -391, -416, -413,
400 -414, -405, -383, -373, -355, -336, -323, -303, -285, -270, -255, -240, -237, -251, -271, -315, -379, -451,
401 -541, -631, -712, -787, -841, -872, -875, -838, -793, -726, -615, -499, -381, -268, -164, -59, 22, 96, 163,
402 217, 252, 254, 261, 238, 190, 149, 90, 34, -26, -62, -88, -126, -119, -119, -100, -61, -31, 36, 65, 107,
403 153, 146, 185, 194, 180, 192, 185, 195, 188, 196, 213, 210, 226, 246, 291, 325, 371, 437, 466, 534, 586,
404 609, 658, 663, 660, 629, 547, 471, 336, 143, -59, -288, -532, -760, -983, -1161, -1289, -1401, -1453,
405 -1463, -1431, -1346, -1249, -1109, -952, -786, -607, -451, -292, -165, -51, 58, 117, 192, 236, 233, 241,
406 225, 214, 199, 177, 186, 189, 209, 248, 263, 289, 312, 322, 327, 299, 257, 201, 125, 38, -72, -174, -269,
407 -366, -454, -521, -555, -593, -591, -554, -534, -466, -399, -338, -250, -189, -108, -40, 19, 115, 197, 289,
408 380, 484, 596, 678, 772, 850, 914, 969, 994, 1033, 1071, 1117, 1174, 1231, 1291, 1344, 1401, 1429, 1449,
409 1456, 1408, 1357, 1255, 1102, 950, 752, 531, 301, 52, -183, -416, -636, -818, -971, -1092, -1171, -1205,
410 -1190, -1128, -1036, -909, -754, -594, -420, -233, -67, 75, 203, 300, 347, 370, 363, 291, 206, 97, -45,
411 -158, -296, -434, -536, -644, -705, -740, -771, -776, -796, -811, -839, -879, -876, -834, -780, -726, -671,
412 -654, -665, -697, -750, -770, -807, -841, -821, -800, -727, -592, -408, -131, 163, 431, 649, 789, 928,
413 1073, 1220, 1405, 1543, 1615, 1646, 1599, 1525, 1449, 1391, 1392, 1453, 1574, 1663, 1714, 1716, 1657, 1665,
414 1680, 1691, 1745, 1772, 1795, 1788, 1766, 1721, 1622, 1541, 1433, 1335, 1264, 1200, 1204, 1200, 1216, 1266,
415 1291, 1364, 1484, 1626, 1779, 1943, 2096, 2211, 2333, 2450, 2541, 2613, 2656, 2664, 2680, 2713, 2737, 2807,
416 2839, 2815, 2778, 2662, 2564, 2475, 2405, 2441, 2508, 2646, 2776, 2832, 2869, 2843, 2814, 2789, 2786, 2815,
417 2829, 2899, 2963, 2994, 3007, 2944, 2846, 2728, 2625, 2556, 2495, 2479, 2459, 2399, 2285, 2089, 1879, 1689,
418 1533, 1498, 1556, 1667, 1817, 1929, 2020, 2052, 2031, 2026, 1927, 1769, 1546, 1234, 947, 633, 349, 107,
419 -156, -372, -563, -698, -784, -830, -828, -870, -934, -1060, -1257, -1491, -1747, -1956, -2120, -2175,
420 -2178, -2164, -2072, -1994, -1873, -1727, -1603, -1451, -1341, -1245, -1193, -1172, -1139, -1138, -1080,
421 -980, -853, -684, -529, -397, -328, -312, -387, -564, -784, -1066, -1359, -1629, -1854, -2020, -2164,
422 -2266, -2337, -2388, -2406, -2382, -2338, -2307, -2263, -2233,
423 ];
424 assert_eq!(samples_out, samples_out_expected);
425 }
426}