Skip to main content

opus_rs/
lib.rs

1#![allow(unsafe_op_in_unsafe_fn)]
2#![allow(clippy::too_many_arguments)]
3#![allow(clippy::needless_range_loop)]
4
5pub mod bands;
6pub mod celt;
7pub mod celt_lpc;
8pub mod hp_cutoff;
9pub mod kiss_fft;
10pub mod mdct;
11pub mod modes;
12pub mod pitch;
13pub mod pvq;
14pub mod quant_bands;
15pub mod range_coder;
16pub mod rate;
17pub mod silk;
18
19pub use silk::{SilkResampler, SilkResamplerDown1_3, SilkResamplerDown1_6};
20
21pub use celt::{CeltDecoder, CeltEncoder};
22use hp_cutoff::hp_cutoff;
23use range_coder::RangeCoder;
24use silk::control_codec::silk_control_encoder;
25use silk::enc_api::silk_encode;
26use silk::init_encoder::silk_init_encoder;
27use silk::lin2log::silk_lin2log;
28use silk::log2lin::silk_log2lin;
29use silk::macros::*;
30use silk::resampler::{silk_resampler_down2, silk_resampler_down2_3};
31use silk::structs::SilkEncoderState;
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum Application {
35    Voip = 2048,
36    Audio = 2049,
37    RestrictedLowDelay = 2051,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum Bandwidth {
42    Auto = -1000,
43    Narrowband = 1101,
44    Mediumband = 1102,
45    Wideband = 1103,
46    Superwideband = 1104,
47    Fullband = 1105,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51enum OpusMode {
52    SilkOnly,
53    Hybrid,
54    CeltOnly,
55}
56
57pub struct OpusEncoder {
58    celt_enc: CeltEncoder,
59    silk_enc: Box<SilkEncoderState>,
60    application: Application,
61    sampling_rate: i32,
62    channels: usize,
63    bandwidth: Bandwidth,
64    pub bitrate_bps: i32,
65    pub complexity: i32,
66    pub use_cbr: bool,
67
68    pub use_inband_fec: bool,
69
70    pub packet_loss_perc: i32,
71    silk_initialized: bool,
72    mode: OpusMode,
73    prev_enc_mode: Option<OpusMode>,
74
75    variable_hp_smth2_q15: i32,
76    hp_mem: Vec<i32>,
77
78    buf_filtered: Vec<i16>,
79    buf_silk_input: Vec<i16>,
80    buf_stereo_mid: Vec<i16>,
81    buf_stereo_side: Vec<i16>,
82    buf_celt_input: Vec<f32>,
83    down2_state_first: [i32; 2],
84    down2_state_second: [i32; 2],
85    down2_3_state: [i32; 6],
86    down_1_3_state: silk::resampler::SilkResamplerDown1_3,
87
88    rc: RangeCoder,
89}
90
91fn compute_equiv_rate(
92    bitrate: i32,
93    channels: usize,
94    frame_rate: i32,
95    vbr: bool,
96    complexity: i32,
97    loss: i32,
98) -> i32 {
99    let mut equiv = bitrate;
100    if frame_rate > 50 {
101        equiv -= (40 * channels as i32 + 20) * (frame_rate - 50);
102    }
103    if !vbr {
104        equiv -= equiv / 12;
105    }
106    equiv = equiv * (90 + complexity) / 100;
107    if loss > 0 {
108        equiv -= equiv * loss / (12 * loss + 20);
109    }
110    equiv
111}
112
113fn compute_mode_threshold(application: Application, channels: usize, prev_was_celt: bool) -> i32 {
114    let mode_voice = if channels == 1 { 64000 } else { 44000 };
115    let mode_music = 10000;
116
117    let offset = (mode_voice - mode_music) >> 14;
118    let mut threshold = mode_music + offset;
119
120    if application == Application::Voip {
121        threshold += 8000;
122    }
123
124    if prev_was_celt {
125        threshold -= 4000;
126    } else {
127        threshold += 4000;
128    }
129
130    match application {
131        Application::Audio => threshold = threshold.max(25000),
132        Application::Voip => threshold = threshold.max(55000),
133        Application::RestrictedLowDelay => threshold = 0,
134    }
135
136    threshold
137}
138
139fn compute_silk_rate_for_hybrid(rate_bps: i32, frame20ms: bool) -> i32 {
140    const RATE_TABLE: &[(i32, i32, i32)] = &[
141        (0, 0, 0),
142        (12000, 10000, 10000),
143        (16000, 13500, 13500),
144        (20000, 16000, 16000),
145        (24000, 18000, 18000),
146        (32000, 22000, 22000),
147        (64000, 38000, 38000),
148    ];
149    let n = RATE_TABLE.len();
150    let mut i = 1;
151    while i < n && RATE_TABLE[i].0 <= rate_bps {
152        i += 1;
153    }
154    if i == n {
155        let (x_last, r10_last, r20_last) = RATE_TABLE[n - 1];
156        let base = if frame20ms { r20_last } else { r10_last };
157        base + (rate_bps - x_last) / 2
158    } else {
159        let (x0, lo10, lo20) = RATE_TABLE[i - 1];
160        let (x1, hi10, hi20) = RATE_TABLE[i];
161        let (lo, hi) = if frame20ms {
162            (lo20, hi20)
163        } else {
164            (lo10, hi10)
165        };
166        (lo * (x1 - rate_bps) + hi * (rate_bps - x0)) / (x1 - x0)
167    }
168}
169
170#[cfg(test)]
171mod silk_rate_tests {
172    use super::compute_silk_rate_for_hybrid;
173
174    #[test]
175    fn test_reference_table_exact_entries() {
176        assert_eq!(compute_silk_rate_for_hybrid(12000, true), 10000);
177        assert_eq!(compute_silk_rate_for_hybrid(16000, true), 13500);
178        assert_eq!(compute_silk_rate_for_hybrid(20000, true), 16000);
179        assert_eq!(compute_silk_rate_for_hybrid(24000, true), 18000);
180        assert_eq!(compute_silk_rate_for_hybrid(32000, true), 22000);
181        assert_eq!(compute_silk_rate_for_hybrid(64000, true), 38000);
182    }
183
184    #[test]
185    fn test_32kbps_gives_22kbps_silk() {
186        assert_eq!(compute_silk_rate_for_hybrid(32000, true), 22000);
187    }
188
189    #[test]
190    fn test_interpolation_between_table_entries() {
191        let r = compute_silk_rate_for_hybrid(18000, true);
192        assert_eq!(r, 14750);
193    }
194
195    #[test]
196    fn test_above_table_max_gives_half_extra() {
197        let r = compute_silk_rate_for_hybrid(72000, true);
198        assert_eq!(r, 38000 + (72000 - 64000) / 2);
199    }
200}
201
202impl OpusEncoder {
203    pub fn new(
204        sampling_rate: i32,
205        channels: usize,
206        application: Application,
207    ) -> Result<Self, &'static str> {
208        if ![8000, 12000, 16000, 24000, 48000].contains(&sampling_rate) {
209            return Err("Invalid sampling rate");
210        }
211        if ![1, 2].contains(&channels) {
212            return Err("Invalid number of channels");
213        }
214
215        let mode = modes::default_mode();
216        let celt_enc = CeltEncoder::new(mode, channels);
217
218        let mut silk_enc = Box::new(SilkEncoderState::default());
219        if silk_init_encoder(&mut silk_enc, 0) != 0 {
220            return Err("SILK encoder initialization failed");
221        }
222
223        let (opus_mode, bw) = match application {
224            Application::Voip => {
225                let bw = match sampling_rate {
226                    8000 => Bandwidth::Narrowband,
227                    12000 => Bandwidth::Mediumband,
228                    16000 => Bandwidth::Wideband,
229                    24000 => Bandwidth::Superwideband,
230                    48000 => Bandwidth::Fullband,
231                    _ => Bandwidth::Narrowband,
232                };
233
234                let mode = if sampling_rate > 16000 {
235                    OpusMode::Hybrid
236                } else {
237                    OpusMode::SilkOnly
238                };
239                (mode, bw)
240            }
241            Application::RestrictedLowDelay => {
242                let bw = match sampling_rate {
243                    8000 => Bandwidth::Narrowband,
244                    12000 => Bandwidth::Mediumband,
245                    16000 => Bandwidth::Wideband,
246                    24000 => Bandwidth::Superwideband,
247                    _ => Bandwidth::Fullband,
248                };
249                (OpusMode::CeltOnly, bw)
250            }
251            Application::Audio => {
252                if sampling_rate <= 16000 {
253                    let bw = match sampling_rate {
254                        8000 => Bandwidth::Narrowband,
255                        12000 => Bandwidth::Mediumband,
256                        _ => Bandwidth::Wideband,
257                    };
258                    (OpusMode::SilkOnly, bw)
259                } else {
260                    let bw = match sampling_rate {
261                        24000 => Bandwidth::Superwideband,
262                        _ => Bandwidth::Fullband,
263                    };
264                    (OpusMode::Hybrid, bw)
265                }
266            }
267        };
268
269        use silk::lin2log::silk_lin2log;
270        let variable_hp_smth2_q15 = silk_lin2log(60) << 8;
271
272        Ok(Self {
273            celt_enc,
274            silk_enc,
275            application,
276            sampling_rate,
277            channels,
278            bandwidth: bw,
279            bitrate_bps: 64000,
280            complexity: 9,
281            use_cbr: false,
282            use_inband_fec: false,
283            packet_loss_perc: 0,
284            silk_initialized: false,
285            prev_enc_mode: None,
286            mode: opus_mode,
287            variable_hp_smth2_q15,
288            hp_mem: vec![0; channels * 2],
289
290            buf_filtered: Vec::new(),
291            buf_silk_input: Vec::new(),
292            buf_stereo_mid: Vec::new(),
293            buf_stereo_side: Vec::new(),
294            buf_celt_input: Vec::new(),
295            down2_state_first: [0; 2],
296            down2_state_second: [0; 2],
297            down2_3_state: [0; 6],
298            down_1_3_state: silk::resampler::SilkResamplerDown1_3::default(),
299            rc: RangeCoder::new_encoder(1),
300        })
301    }
302
303    pub fn enable_hybrid_mode(&mut self) -> Result<(), &'static str> {
304        if self.sampling_rate != 24000 && self.sampling_rate != 48000 {
305            return Err("Hybrid mode requires 24kHz or 48kHz sampling rate");
306        }
307        let bw = if self.sampling_rate == 48000 {
308            Bandwidth::Fullband
309        } else {
310            Bandwidth::Superwideband
311        };
312        self.mode = OpusMode::Hybrid;
313        self.bandwidth = bw;
314        self.silk_initialized = false;
315        Ok(())
316    }
317
318    pub fn encode(
319        &mut self,
320        input: &[f32],
321        frame_size: usize,
322        output: &mut [u8],
323    ) -> Result<usize, &'static str> {
324        if output.len() < 2 {
325            return Err("Output buffer too small");
326        }
327
328        let frame_rate = frame_rate_from_params(self.sampling_rate, frame_size)
329            .ok_or("Invalid frame size for sampling rate")?;
330
331        // Mode selection: match C's opus_encode_native() behavior.
332        // C reference auto-selects between SILK_ONLY and CELT_ONLY; Hybrid is
333        // produced afterwards by bandwidth overrides (SILK-only + FB/SWB → Hybrid).
334        let mut mode = if self.application == Application::RestrictedLowDelay {
335            OpusMode::CeltOnly
336        } else {
337            let equiv = compute_equiv_rate(
338                self.bitrate_bps,
339                self.channels,
340                frame_rate,
341                !self.use_cbr,
342                self.complexity,
343                self.packet_loss_perc,
344            );
345            let prev_was_celt = self.prev_enc_mode == Some(OpusMode::CeltOnly);
346            let threshold = compute_mode_threshold(self.application, self.channels, prev_was_celt);
347            if equiv >= threshold {
348                OpusMode::CeltOnly
349            } else {
350                OpusMode::SilkOnly
351            }
352        };
353
354        let curr_bw = self.bandwidth;
355        if mode == OpusMode::SilkOnly
356            && (curr_bw == Bandwidth::Superwideband || curr_bw == Bandwidth::Fullband)
357        {
358            mode = OpusMode::Hybrid;
359        }
360        if mode == OpusMode::Hybrid
361            && (curr_bw == Bandwidth::Narrowband
362                || curr_bw == Bandwidth::Mediumband
363                || curr_bw == Bandwidth::Wideband)
364        {
365            mode = OpusMode::SilkOnly;
366        }
367
368        if mode == OpusMode::CeltOnly {
369            match frame_rate {
370                400 | 200 | 100 | 50 => {}
371                _ => return Err("Unsupported frame size for CELT-only mode"),
372            }
373        }
374
375        let toc = gen_toc(mode, frame_rate, self.bandwidth, self.channels);
376        output[0] = toc;
377
378        let target_bits =
379            (self.bitrate_bps as i64 * frame_size as i64 / self.sampling_rate as i64) as i32;
380        let cbr_bytes = ((target_bits + 4) / 8) as usize;
381        let max_data_bytes = output.len();
382
383        let n_bytes = cbr_bytes.min(max_data_bytes).max(1);
384
385        // Initialize range coder with CBR-capped buffer size, matching C's
386        // ec_enc_init(&enc, data, max_data_bytes-1) where max_data_bytes is CBR-capped.
387        let init_rc_size = n_bytes - 1;
388        self.rc.reset_for_encode(init_rc_size as u32);
389
390        if mode == OpusMode::SilkOnly || mode == OpusMode::Hybrid {
391            let silk_fs_khz = if mode == OpusMode::Hybrid {
392                16
393            } else {
394                self.sampling_rate.min(16000) / 1000
395            };
396
397            let frame_ms = (frame_size as i32 * 1000) / self.sampling_rate;
398            if !self.silk_initialized || self.silk_enc.s_cmn.fs_khz != silk_fs_khz {
399                let silk_init_bitrate = (((n_bytes - 1) * 8) as i64 * self.sampling_rate as i64
400                    / frame_size as i64) as i32;
401                silk_control_encoder(
402                    &mut self.silk_enc,
403                    silk_fs_khz,
404                    frame_ms,
405                    silk_init_bitrate,
406                    self.complexity,
407                );
408                self.silk_enc.s_cmn.use_cbr = if self.use_cbr { 1 } else { 0 };
409
410                self.silk_enc.s_cmn.n_channels = self.channels as i32;
411                self.silk_initialized = true;
412                self.down2_state_first = [0; 2];
413                self.down2_state_second = [0; 2];
414                self.down2_3_state = [0; 6];
415                self.down_1_3_state = silk::resampler::SilkResamplerDown1_3::default();
416            }
417
418            self.silk_enc.s_cmn.use_in_band_fec = if self.use_inband_fec { 1 } else { 0 };
419            self.silk_enc.s_cmn.packet_loss_perc = self.packet_loss_perc.clamp(0, 100);
420
421            self.silk_enc.s_cmn.lbrr_enabled = if self.use_inband_fec { 1 } else { 0 };
422
423            if self.silk_enc.s_cmn.lbrr_gain_increases == 0 {
424                self.silk_enc.s_cmn.lbrr_gain_increases = 2;
425            }
426
427            let hp_freq_smth1 = if mode == OpusMode::CeltOnly {
428                silk_lin2log(60) << 8
429            } else {
430                self.silk_enc.s_cmn.variable_hp_smth1_q15
431            };
432
433            const VARIABLE_HP_SMTH_COEF2_Q16: i32 = 984;
434            self.variable_hp_smth2_q15 = silk_smlawb(
435                self.variable_hp_smth2_q15,
436                hp_freq_smth1 - self.variable_hp_smth2_q15,
437                VARIABLE_HP_SMTH_COEF2_Q16,
438            );
439
440            let cutoff_hz = silk_log2lin(silk_rshift(self.variable_hp_smth2_q15, 8));
441
442            let required_size = frame_size * self.channels;
443            self.buf_filtered.resize(required_size, 0);
444            if self.application == Application::Voip {
445                hp_cutoff(
446                    input,
447                    cutoff_hz,
448                    &mut self.buf_filtered,
449                    &mut self.hp_mem,
450                    frame_size,
451                    self.channels,
452                    self.sampling_rate,
453                );
454            } else {
455                for (i, &x) in input.iter().enumerate() {
456                    self.buf_filtered[i] = (x * 32768.0).clamp(-32768.0, 32767.0) as i16;
457                }
458            }
459
460            let input_i16 = &self.buf_filtered;
461
462            let silk_input: &[i16] = if mode == OpusMode::SilkOnly && self.sampling_rate > 16000 {
463                if self.sampling_rate == 48000 {
464                    let stage1_size = frame_size / 2;
465                    let mut stage1_buf = [0i16; 480];
466                    silk_resampler_down2(
467                        &mut self.down2_state_first,
468                        &mut stage1_buf[..stage1_size],
469                        input_i16,
470                        frame_size as i32,
471                    );
472                    let silk_frame_size = stage1_size * 2 / 3;
473                    self.buf_silk_input.resize(silk_frame_size, 0);
474                    silk_resampler_down2_3(
475                        &mut self.down2_3_state,
476                        &mut self.buf_silk_input,
477                        &stage1_buf[..stage1_size],
478                        stage1_size as i32,
479                    );
480                    &self.buf_silk_input
481                } else if self.sampling_rate == 24000 {
482                    let silk_frame_size = frame_size * 2 / 3;
483                    self.buf_silk_input.resize(silk_frame_size, 0);
484                    silk_resampler_down2_3(
485                        &mut self.down2_3_state,
486                        &mut self.buf_silk_input,
487                        input_i16,
488                        frame_size as i32,
489                    );
490                    &self.buf_silk_input
491                } else {
492                    input_i16
493                }
494            } else if mode == OpusMode::SilkOnly && self.channels == 2 {
495                let frame_length = input_i16.len() / 2;
496                self.buf_stereo_mid.resize(frame_length, 0);
497                self.buf_stereo_side.resize(frame_length, 0);
498                for i in 0..frame_length {
499                    let l = input_i16[2 * i] as i32;
500                    let r = input_i16[2 * i + 1] as i32;
501                    self.buf_stereo_mid[i] = ((l + r) / 2) as i16;
502                    self.buf_stereo_side[i] = (l - r) as i16;
503                }
504
505                self.silk_enc.stereo.side.resize(frame_length, 0);
506                self.silk_enc
507                    .stereo
508                    .side
509                    .copy_from_slice(&self.buf_stereo_side[..frame_length]);
510                &self.buf_stereo_mid
511            } else if mode == OpusMode::Hybrid && self.sampling_rate > 16000 {
512                if self.sampling_rate == 48000 {
513                    let silk_frame_size = frame_size / 3;
514                    self.buf_silk_input.resize(silk_frame_size, 0);
515                    silk::resampler::silk_resampler_down_1_3(
516                        &mut self.down_1_3_state,
517                        &mut self.buf_silk_input,
518                        input_i16,
519                    );
520                } else {
521                    let silk_frame_size = frame_size * 2 / 3;
522                    self.buf_silk_input.resize(silk_frame_size, 0);
523                    silk_resampler_down2_3(
524                        &mut self.down2_3_state,
525                        &mut self.buf_silk_input,
526                        input_i16,
527                        frame_size as i32,
528                    );
529                }
530                &self.buf_silk_input
531            } else {
532                input_i16
533            };
534
535            let mut pn_bytes = 0;
536
537            let silk_rate_for_calc = if mode == OpusMode::Hybrid {
538                16000
539            } else {
540                self.sampling_rate
541            };
542            let silk_frame_len = silk_input.len();
543
544            let silk_bitrate = if mode == OpusMode::Hybrid {
545                let frame_duration_ms = frame_size as i32 * 1000 / self.sampling_rate;
546                let frame20ms = frame_duration_ms >= 20;
547                compute_silk_rate_for_hybrid(self.bitrate_bps, frame20ms)
548            } else {
549                (8i64 * (n_bytes - 1) as i64 * silk_rate_for_calc as i64 / silk_frame_len as i64)
550                    as i32
551            };
552            let silk_max_bits = if mode == OpusMode::Hybrid {
553                // Match C: for VBR Hybrid, maxBits starts at (max_data_bytes-1)*8,
554                // then constrained via compute_silk_rate_for_hybrid.
555                // For CBR Hybrid, allow SILK to steal up to 25% of remaining bits.
556                let total_max_bits = ((n_bytes - 1) * 8) as i32;
557                if self.use_cbr {
558                    let silk_bits = (silk_bitrate as i64 * silk_frame_len as i64
559                        / silk_rate_for_calc as i64) as i32;
560                    let other_bits = 0i32.max(total_max_bits - silk_bits);
561                    0i32.max(total_max_bits - other_bits * 3 / 4)
562                } else {
563                    // Constrained VBR: compute max SILK bits from total budget
564                    let frame_duration_ms = frame_size as i32 * 1000 / self.sampling_rate;
565                    let frame20ms = frame_duration_ms >= 20;
566                    let max_bit_rate = compute_silk_rate_for_hybrid(
567                        total_max_bits * self.sampling_rate / frame_size as i32,
568                        frame20ms,
569                    );
570                    max_bit_rate * frame_size as i32 / self.sampling_rate
571                }
572            } else {
573                ((n_bytes - 1) * 8) as i32
574            };
575            // For Hybrid CBR, force SILK to use VBR (matching C behavior)
576            let silk_use_cbr = if mode == OpusMode::Hybrid && self.use_cbr {
577                0
578            } else if self.use_cbr {
579                1
580            } else {
581                0
582            };
583            let ret = silk_encode(
584                &mut self.silk_enc,
585                silk_input,
586                silk_input.len(),
587                &mut self.rc,
588                &mut pn_bytes,
589                silk_bitrate,
590                silk_max_bits,
591                silk_use_cbr,
592                1,
593            );
594            if ret != 0 {
595                return Err("SILK encoding failed");
596            }
597        }
598
599        if mode == OpusMode::Hybrid {
600            self.rc.encode_bit_logp(false, 12); // redundancy = 0
601        }
602
603        if mode == OpusMode::Hybrid {
604            let nb_compr_bytes = (n_bytes - 1) as u32;
605            self.rc.shrink(nb_compr_bytes);
606        }
607
608        let silk_ret_bytes = if mode == OpusMode::SilkOnly {
609            ((self.rc.tell() + 7) >> 3) as usize
610        } else {
611            0
612        };
613
614        if mode == OpusMode::CeltOnly || mode == OpusMode::Hybrid {
615            self.celt_enc.complexity = self.complexity;
616            let start_band = if mode == OpusMode::Hybrid { 17 } else { 0 };
617            let total_packet_bits = ((n_bytes - 1) * 8) as i32;
618
619            let celt_input: &[f32] = if self.channels == 1 {
620                input
621            } else {
622                let n = frame_size * self.channels;
623                self.buf_celt_input.resize(n, 0.0);
624                for i in 0..frame_size {
625                    for ch in 0..self.channels {
626                        self.buf_celt_input[ch * frame_size + i] = input[i * self.channels + ch];
627                    }
628                }
629                &self.buf_celt_input
630            };
631
632            if self.rc.tell() <= total_packet_bits {
633                self.celt_enc.encode_with_budget(
634                    celt_input,
635                    frame_size,
636                    &mut self.rc,
637                    start_band,
638                    total_packet_bits,
639                );
640            }
641        }
642
643        self.rc.done();
644
645        if mode == OpusMode::SilkOnly {
646            let mut ret = silk_ret_bytes.min(self.rc.storage as usize);
647            while ret > 2 && self.rc.buf[ret - 1] == 0 {
648                ret -= 1;
649            }
650
651            let target_total = if self.use_cbr {
652                n_bytes.min(output.len())
653            } else {
654                (ret + 1).min(output.len())
655            };
656
657            let silk_len = ret;
658
659            if !self.use_cbr || silk_len + 1 >= target_total {
660                // VBR or payload fills the target: simple code 0 packet
661                output[0] = toc;
662                let copy_len = silk_len.min(target_total - 1);
663                output[1..1 + copy_len].copy_from_slice(&self.rc.buf[..copy_len]);
664                return Ok((copy_len + 1).min(output.len()));
665            }
666
667            output[0] = toc | 0x03;
668
669            if silk_len + 2 >= target_total {
670                output[1] = 0x01;
671                let copy_len = (target_total - 2).min(silk_len);
672                output[2..2 + copy_len].copy_from_slice(&self.rc.buf[..copy_len]);
673                self.prev_enc_mode = Some(mode);
674                return Ok(target_total.min(output.len()));
675            }
676
677            let pad_amount = target_total - silk_len - 2;
678            output[1] = 0x41;
679
680            let nb_255s = (pad_amount - 1) / 255;
681            let mut ptr = 2;
682            for _ in 0..nb_255s {
683                output[ptr] = 255;
684                ptr += 1;
685            }
686            output[ptr] = (pad_amount - 255 * nb_255s - 1) as u8;
687            ptr += 1;
688
689            output[ptr..ptr + silk_len].copy_from_slice(&self.rc.buf[..silk_len]);
690            ptr += silk_len;
691
692            let fill_end = target_total.min(output.len());
693            for byte in output[ptr..fill_end].iter_mut() {
694                *byte = 0;
695            }
696
697            self.prev_enc_mode = Some(mode);
698            return Ok(target_total.min(output.len()));
699        }
700
701        let payload_len = n_bytes - 1;
702        output[1..1 + payload_len].copy_from_slice(&self.rc.buf[..payload_len]);
703        self.prev_enc_mode = Some(mode);
704        Ok(n_bytes)
705    }
706}
707
708pub struct OpusDecoder {
709    celt_dec: CeltDecoder,
710    silk_dec: silk::dec_api::SilkDecoder,
711    sampling_rate: i32,
712    channels: usize,
713
714    prev_mode: Option<OpusMode>,
715    frame_size: usize,
716
717    bandwidth: Bandwidth,
718
719    stream_channels: usize,
720
721    silk_resampler: silk::resampler::SilkResampler,
722
723    prev_internal_rate: i32,
724
725    pub hybrid_skip_celt: bool,
726
727    w_pcm_i16: Vec<i16>,
728    w_silk_out: Vec<f32>,
729    w_pcm_resampled: Vec<i16>,
730    w_celt_planar: Vec<f32>,
731    w_celt_out: Vec<f32>,
732}
733
734impl OpusDecoder {
735    pub fn new(sampling_rate: i32, channels: usize) -> Result<Self, &'static str> {
736        if ![8000, 12000, 16000, 24000, 48000].contains(&sampling_rate) {
737            return Err("Invalid sampling rate");
738        }
739        if ![1, 2].contains(&channels) {
740            return Err("Invalid number of channels");
741        }
742
743        let mode = modes::default_mode();
744        let celt_dec = CeltDecoder::new(mode, channels);
745
746        let mut silk_dec = silk::dec_api::SilkDecoder::new();
747        silk_dec.init(sampling_rate.min(16000), channels as i32);
748        silk_dec.channel_state[0].fs_api_hz = sampling_rate;
749
750        Ok(Self {
751            celt_dec,
752            silk_dec,
753            sampling_rate,
754            channels,
755            prev_mode: None,
756            frame_size: 0,
757            bandwidth: Bandwidth::Auto,
758            stream_channels: channels,
759            silk_resampler: silk::resampler::SilkResampler::default(),
760            prev_internal_rate: 0,
761            hybrid_skip_celt: false,
762
763            w_pcm_i16: vec![0i16; 640],
764
765            w_silk_out: vec![0.0f32; 5760 * channels],
766            w_pcm_resampled: vec![0i16; 5760 * channels],
767            w_celt_planar: vec![0.0f32; 5760 * channels],
768            w_celt_out: vec![0.0f32; 5760 * channels],
769        })
770    }
771
772    pub fn decode(
773        &mut self,
774        input: &[u8],
775        frame_size: usize,
776        output: &mut [f32],
777    ) -> Result<usize, &'static str> {
778        if input.is_empty() {
779            return Err("Input packet empty");
780        }
781
782        let toc = input[0];
783        let mode = mode_from_toc(toc);
784        let packet_channels = channels_from_toc(toc);
785        let bandwidth = bandwidth_from_toc(toc);
786        let frame_duration_ms = frame_duration_ms_from_toc(toc);
787
788        if packet_channels != self.channels {
789            return Err("Channel count mismatch between packet and decoder");
790        }
791
792        let code = toc & 0x03;
793        let payload_data;
794
795        match code {
796            0 => {
797                payload_data = &input[1..];
798            }
799            3 => {
800                if input.len() < 2 {
801                    return Err("Code 3 packet too short");
802                }
803                let count_byte = input[1];
804                let _frame_count = (count_byte & 0x3F) as usize;
805                let padding_flag = (count_byte & 0x40) != 0;
806
807                let mut ptr = 2usize;
808                if padding_flag {
809                    let mut pad_len = 0usize;
810                    loop {
811                        if ptr >= input.len() {
812                            return Err("Padding overflow");
813                        }
814                        let p = input[ptr] as usize;
815                        ptr += 1;
816                        if p == 255 {
817                            pad_len += 254;
818                        } else {
819                            pad_len += p;
820                            break;
821                        }
822                    }
823
824                    let end = input.len().saturating_sub(pad_len);
825                    if ptr > end {
826                        return Err("Padding exceeds packet");
827                    }
828                    payload_data = &input[ptr..end];
829                } else {
830                    payload_data = &input[ptr..];
831                }
832            }
833            _ => {
834                payload_data = &input[1..];
835            }
836        }
837
838        self.frame_size = frame_size;
839        self.bandwidth = bandwidth;
840        self.stream_channels = packet_channels;
841
842        match mode {
843            OpusMode::SilkOnly => {
844                let internal_sample_rate = match bandwidth {
845                    Bandwidth::Narrowband => 8000,
846                    Bandwidth::Mediumband => 12000,
847                    Bandwidth::Wideband => 16000,
848                    _ => 16000,
849                };
850
851                let mut rc = RangeCoder::new_decoder(payload_data);
852                let internal_frame_size =
853                    (frame_duration_ms * internal_sample_rate / 1000) as usize;
854                let pcm_i16_len = internal_frame_size * self.channels;
855                debug_assert!(pcm_i16_len <= self.w_pcm_i16.len());
856
857                let payload_size_ms = frame_duration_ms;
858
859                let ret = {
860                    let (silk_dec, pcm_i16) = (&mut self.silk_dec, &mut self.w_pcm_i16);
861                    silk_dec.decode(
862                        &mut rc,
863                        &mut pcm_i16[..pcm_i16_len],
864                        silk::decode_frame::FLAG_DECODE_NORMAL,
865                        true,
866                        payload_size_ms,
867                        internal_sample_rate,
868                    )
869                };
870
871                if ret < 0 {
872                    return Err("SILK decoding failed");
873                }
874
875                let decoded_samples = ret as usize;
876
877                if self.sampling_rate == internal_sample_rate {
878                    let frames = decoded_samples.min(frame_size);
879                    let total = (frames * self.channels).min(output.len());
880                    for i in 0..total {
881                        output[i] = self.w_pcm_i16[i] as f32 / 32768.0;
882                    }
883                    self.prev_mode = Some(OpusMode::SilkOnly);
884                    Ok(frames)
885                } else {
886                    if internal_sample_rate != self.prev_internal_rate {
887                        self.silk_resampler
888                            .init(internal_sample_rate, self.sampling_rate);
889                        self.prev_internal_rate = internal_sample_rate;
890                    }
891
892                    let ratio = self.sampling_rate as f64 / internal_sample_rate as f64;
893                    let out_len = ((decoded_samples as f64 * ratio) as usize).min(frame_size);
894                    debug_assert!(out_len <= self.w_pcm_resampled.len());
895                    let ret2 = {
896                        let (silk_res, pcm_i16, pcm_out) = (
897                            &mut self.silk_resampler,
898                            &self.w_pcm_i16,
899                            &mut self.w_pcm_resampled,
900                        );
901                        silk_res.process(
902                            &mut pcm_out[..out_len],
903                            &pcm_i16[..decoded_samples],
904                            decoded_samples as i32,
905                        )
906                    };
907                    let _ = ret2;
908
909                    let frames = out_len.min(frame_size);
910                    let total = (frames * self.channels).min(output.len());
911                    for i in 0..total {
912                        output[i] = self.w_pcm_resampled[i] as f32 / 32768.0;
913                    }
914                    self.prev_mode = Some(OpusMode::SilkOnly);
915                    Ok(frames)
916                }
917            }
918
919            OpusMode::CeltOnly => {
920                let mut rc = RangeCoder::new_decoder(payload_data);
921                let total_bits = (payload_data.len() * 8) as i32;
922                let celt_end_band = self.celt_end_band_from_toc(toc);
923                let needed = frame_size * self.channels;
924                if output.len() < needed {
925                    return Err("Output buffer too small");
926                }
927
928                if self.channels == 1 {
929                    self.celt_dec.decode_from_range_coder_with_band_range(
930                        &mut rc,
931                        total_bits,
932                        frame_size,
933                        &mut output[..needed],
934                        0,
935                        celt_end_band,
936                    );
937                    for sample in &mut output[..needed] {
938                        *sample = sample.clamp(-1.0, 1.0);
939                    }
940                } else {
941                    self.celt_dec.decode_from_range_coder_with_band_range(
942                        &mut rc,
943                        total_bits,
944                        frame_size,
945                        &mut self.w_celt_planar[..needed],
946                        0,
947                        celt_end_band,
948                    );
949                    for i in 0..frame_size {
950                        for ch in 0..self.channels {
951                            output[i * self.channels + ch] =
952                                self.w_celt_planar[ch * frame_size + i].clamp(-1.0, 1.0);
953                        }
954                    }
955                }
956                self.prev_mode = Some(OpusMode::CeltOnly);
957                Ok(frame_size)
958            }
959
960            OpusMode::Hybrid => {
961                let internal_sample_rate = match bandwidth {
962                    Bandwidth::Superwideband => 16000,
963                    Bandwidth::Fullband => 16000,
964                    _ => 16000,
965                };
966
967                let mut rc = RangeCoder::new_decoder(payload_data);
968                let internal_frame_size =
969                    (frame_duration_ms * internal_sample_rate / 1000) as usize;
970                let pcm_silk_i16_len = internal_frame_size * self.channels;
971                debug_assert!(pcm_silk_i16_len <= self.w_pcm_i16.len());
972
973                let ret = {
974                    let (silk_dec, pcm_i16) = (&mut self.silk_dec, &mut self.w_pcm_i16);
975                    silk_dec.decode(
976                        &mut rc,
977                        &mut pcm_i16[..pcm_silk_i16_len],
978                        silk::decode_frame::FLAG_DECODE_NORMAL,
979                        true,
980                        frame_duration_ms,
981                        internal_sample_rate,
982                    )
983                };
984
985                if ret < 0 {
986                    return Err("SILK decoding failed");
987                }
988
989                let silk_out_len = frame_size * self.channels;
990                debug_assert!(silk_out_len <= self.w_silk_out.len());
991                self.w_silk_out[..silk_out_len].fill(0.0);
992                if ret > 0 {
993                    let decoded_samples = ret as usize;
994                    if self.sampling_rate == internal_sample_rate {
995                        let frames = decoded_samples.min(frame_size);
996                        let total = frames * self.channels;
997                        for i in 0..total {
998                            self.w_silk_out[i] = self.w_pcm_i16[i] as f32 / 32768.0;
999                        }
1000                    } else {
1001                        if internal_sample_rate != self.prev_internal_rate {
1002                            self.silk_resampler
1003                                .init(internal_sample_rate, self.sampling_rate);
1004                            self.prev_internal_rate = internal_sample_rate;
1005                        }
1006                        let ratio = self.sampling_rate as f64 / internal_sample_rate as f64;
1007                        let out_len = ((decoded_samples as f64 * ratio) as usize).min(frame_size);
1008                        debug_assert!(out_len <= self.w_pcm_resampled.len());
1009                        {
1010                            let (silk_res, pcm_i16, pcm_resampled) = (
1011                                &mut self.silk_resampler,
1012                                &self.w_pcm_i16,
1013                                &mut self.w_pcm_resampled,
1014                            );
1015                            silk_res.process(
1016                                &mut pcm_resampled[..out_len],
1017                                &pcm_i16[..decoded_samples],
1018                                decoded_samples as i32,
1019                            );
1020                        }
1021                        let frames = out_len.min(frame_size);
1022                        let total = frames * self.channels;
1023                        for i in 0..total {
1024                            self.w_silk_out[i] = self.w_pcm_resampled[i] as f32 / 32768.0;
1025                        }
1026                    }
1027                }
1028
1029                // In Hybrid mode the encoder always writes a redundancy flag (logp=12)
1030                // immediately after SILK.  Read it here to keep the range-coder in sync.
1031                // We don't support redundancy frames, so if the flag is somehow 1 we
1032                // still just consume the extra bit and fall through to normal CELT decode.
1033                let total_bits = (payload_data.len() * 8) as i32;
1034                let redundancy = rc.decode_bit_logp(12);
1035                if redundancy {
1036                    // Consume celt_to_silk flag; actual redundancy frame handling not implemented.
1037                    let _ = rc.decode_bit_logp(1);
1038                }
1039
1040                let celt_out_len = frame_size * self.channels;
1041                let celt_end_band = self.celt_end_band_from_toc(toc);
1042                debug_assert!(celt_out_len <= self.w_celt_out.len());
1043                if self.hybrid_skip_celt {
1044                    self.w_celt_out[..celt_out_len].fill(0.0);
1045                } else {
1046                    let (celt_dec, celt_planar) = (&mut self.celt_dec, &mut self.w_celt_planar);
1047                    celt_dec.decode_from_range_coder_with_band_range(
1048                        &mut rc,
1049                        total_bits,
1050                        frame_size,
1051                        &mut celt_planar[..celt_out_len],
1052                        17,
1053                        celt_end_band,
1054                    );
1055
1056                    if self.channels == 1 {
1057                        self.w_celt_out[..celt_out_len]
1058                            .copy_from_slice(&self.w_celt_planar[..celt_out_len]);
1059                    } else {
1060                        for i in 0..frame_size {
1061                            for ch in 0..self.channels {
1062                                self.w_celt_out[i * self.channels + ch] =
1063                                    self.w_celt_planar[ch * frame_size + i];
1064                            }
1065                        }
1066                    }
1067                }
1068
1069                let total = (frame_size * self.channels).min(output.len());
1070                for i in 0..total {
1071                    output[i] = (self.w_silk_out[i] + self.w_celt_out[i]).clamp(-1.0, 1.0);
1072                }
1073
1074                self.prev_mode = Some(OpusMode::Hybrid);
1075                Ok(frame_size)
1076            }
1077        }
1078    }
1079}
1080
1081impl OpusDecoder {
1082    #[inline(always)]
1083    fn celt_end_band_from_toc(&self, toc: u8) -> usize {
1084        let mode = modes::default_mode();
1085        let top = mode.eff_ebands;
1086        if mode_from_toc(toc) == OpusMode::CeltOnly && toc >= 0x80 {
1087            const FROM_OPUS_TABLE: [u8; 16] = [
1088                0x80, 0x88, 0x90, 0x98, 0x40, 0x48, 0x50, 0x58, 0x20, 0x28, 0x30, 0x38, 0x00, 0x08,
1089                0x10, 0x18,
1090            ];
1091            let idx = ((toc >> 3) - 16) as usize;
1092            let data0 = FROM_OPUS_TABLE[idx] | (toc & 0x7);
1093            let trim = (data0 >> 5) as usize;
1094            return top.saturating_sub(2 * trim).max(1);
1095        }
1096        top
1097    }
1098}
1099
1100fn frame_rate_from_params(sampling_rate: i32, frame_size: usize) -> Option<i32> {
1101    let frame_size = frame_size as i32;
1102    if frame_size == 0 || sampling_rate % frame_size != 0 {
1103        return None;
1104    }
1105    Some(sampling_rate / frame_size)
1106}
1107
1108fn gen_toc(mode: OpusMode, frame_rate: i32, bandwidth: Bandwidth, channels: usize) -> u8 {
1109    let mut rate = frame_rate;
1110    let mut period = 0;
1111    while rate < 400 {
1112        rate <<= 1;
1113        period += 1;
1114    }
1115
1116    let mut toc = match mode {
1117        OpusMode::SilkOnly => {
1118            let bw = (bandwidth as i32 - Bandwidth::Narrowband as i32) << 5;
1119            let per = (period - 2) << 3;
1120            (bw | per) as u8
1121        }
1122        OpusMode::CeltOnly => {
1123            let mut tmp = bandwidth as i32 - Bandwidth::Mediumband as i32;
1124            if tmp < 0 {
1125                tmp = 0;
1126            }
1127            let per = period << 3;
1128            (0x80 | (tmp << 5) | per) as u8
1129        }
1130        OpusMode::Hybrid => {
1131            let base_config = if bandwidth == Bandwidth::Superwideband {
1132                12
1133            } else {
1134                14
1135            };
1136            let period_offset = if frame_rate >= 100 { 0 } else { 1 };
1137            ((base_config + period_offset) << 3) as u8
1138        }
1139    };
1140
1141    if channels == 2 {
1142        toc |= 0x04;
1143    }
1144    toc
1145}
1146
1147fn mode_from_toc(toc: u8) -> OpusMode {
1148    if toc & 0x80 != 0 {
1149        OpusMode::CeltOnly
1150    } else if toc & 0x60 == 0x60 {
1151        OpusMode::Hybrid
1152    } else {
1153        OpusMode::SilkOnly
1154    }
1155}
1156
1157fn bandwidth_from_toc(toc: u8) -> Bandwidth {
1158    let mode = mode_from_toc(toc);
1159    match mode {
1160        OpusMode::SilkOnly => {
1161            let bw_bits = (toc >> 5) & 0x03;
1162            match bw_bits {
1163                0 => Bandwidth::Narrowband,
1164                1 => Bandwidth::Mediumband,
1165                2 => Bandwidth::Wideband,
1166                _ => Bandwidth::Wideband,
1167            }
1168        }
1169        OpusMode::Hybrid => {
1170            let bw_bit = (toc >> 4) & 0x01;
1171            if bw_bit == 0 {
1172                Bandwidth::Superwideband
1173            } else {
1174                Bandwidth::Fullband
1175            }
1176        }
1177        OpusMode::CeltOnly => {
1178            let bw_bits = (toc >> 5) & 0x03;
1179            match bw_bits {
1180                0 => Bandwidth::Mediumband,
1181                1 => Bandwidth::Wideband,
1182                2 => Bandwidth::Superwideband,
1183                3 => Bandwidth::Fullband,
1184                _ => Bandwidth::Fullband,
1185            }
1186        }
1187    }
1188}
1189
1190fn frame_duration_ms_from_toc(toc: u8) -> i32 {
1191    let mode = mode_from_toc(toc);
1192    match mode {
1193        OpusMode::SilkOnly => {
1194            let config = (toc >> 3) & 0x03;
1195            match config {
1196                0 => 10,
1197                1 => 20,
1198                2 => 40,
1199                3 => 60,
1200                _ => 20,
1201            }
1202        }
1203        OpusMode::Hybrid => {
1204            let config = (toc >> 3) & 0x01;
1205            if config == 0 { 10 } else { 20 }
1206        }
1207        OpusMode::CeltOnly => {
1208            let config = (toc >> 3) & 0x03;
1209            match config {
1210                0 => 2,
1211                1 => 5,
1212                2 => 10,
1213                3 => 20,
1214                _ => 20,
1215            }
1216        }
1217    }
1218}
1219
1220fn channels_from_toc(toc: u8) -> usize {
1221    if toc & 0x04 != 0 { 2 } else { 1 }
1222}
1223
1224#[cfg(test)]
1225mod tests {
1226    use super::*;
1227
1228    fn frame_size_from_toc(toc: u8, sampling_rate: i32) -> Option<usize> {
1229        let mode = mode_from_toc(toc);
1230        match mode {
1231            OpusMode::CeltOnly => {
1232                let period = ((toc >> 3) & 0x03) as i32;
1233                let frame_rate = 400 >> period;
1234                if frame_rate == 0 || sampling_rate % frame_rate != 0 {
1235                    return None;
1236                }
1237                Some((sampling_rate / frame_rate) as usize)
1238            }
1239            OpusMode::SilkOnly => {
1240                let duration_ms = frame_duration_ms_from_toc(toc);
1241                Some((sampling_rate as i64 * duration_ms as i64 / 1000) as usize)
1242            }
1243            OpusMode::Hybrid => {
1244                let duration_ms = frame_duration_ms_from_toc(toc);
1245                Some((sampling_rate as i64 * duration_ms as i64 / 1000) as usize)
1246            }
1247        }
1248    }
1249
1250    #[test]
1251    fn gen_toc_matches_celt_reference_values() {
1252        let sampling_rate = 48_000;
1253        let cases = [
1254            (120usize, 0xE0u8),
1255            (240usize, 0xE8u8),
1256            (480usize, 0xF0u8),
1257            (960usize, 0xF8u8),
1258        ];
1259
1260        for (frame_size, expected_toc) in cases {
1261            let frame_rate = frame_rate_from_params(sampling_rate, frame_size).unwrap();
1262            let toc = gen_toc(OpusMode::CeltOnly, frame_rate, Bandwidth::Fullband, 1);
1263            assert_eq!(
1264                toc, expected_toc,
1265                "frame_size {} expected TOC {:02X} got {:02X}",
1266                frame_size, expected_toc, toc
1267            );
1268            let decoded_size = frame_size_from_toc(toc, sampling_rate).unwrap();
1269            assert_eq!(decoded_size, frame_size);
1270        }
1271
1272        let stereo_toc = gen_toc(
1273            OpusMode::CeltOnly,
1274            frame_rate_from_params(sampling_rate, 960).unwrap(),
1275            Bandwidth::Fullband,
1276            2,
1277        );
1278        assert_eq!(channels_from_toc(stereo_toc), 2);
1279    }
1280
1281    #[test]
1282    fn test_celt_decoder_large_frame_sizes() {
1283        let sampling_rate = 48000;
1284        let channels = 1;
1285
1286        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1287
1288        let frame_sizes = [120, 240, 480, 960];
1289
1290        for frame_size in frame_sizes {
1291            let toc = gen_toc(
1292                OpusMode::CeltOnly,
1293                frame_rate_from_params(sampling_rate, frame_size).unwrap(),
1294                Bandwidth::Fullband,
1295                channels,
1296            );
1297            let packet = [toc, 0, 0, 0, 0];
1298
1299            let mut output = vec![0.0f32; frame_size * channels];
1300
1301            let _ = decoder.decode(&packet, frame_size, &mut output);
1302        }
1303
1304        let channels = 2;
1305        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1306
1307        for frame_size in frame_sizes {
1308            let toc = gen_toc(
1309                OpusMode::CeltOnly,
1310                frame_rate_from_params(sampling_rate, frame_size).unwrap(),
1311                Bandwidth::Fullband,
1312                channels,
1313            );
1314            let packet = [toc, 0, 0, 0, 0];
1315
1316            let mut output = vec![0.0f32; frame_size * channels];
1317            let _ = decoder.decode(&packet, frame_size, &mut output);
1318        }
1319    }
1320
1321    #[test]
1322    fn test_celt_decoder_edge_case_frame_sizes() {
1323        let sampling_rate = 48000;
1324        let channels = 1;
1325        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1326
1327        let edge_sizes = [2048, 2167, 2168, 2169, 2880, 3072];
1328
1329        for frame_size in edge_sizes {
1330            let mut output = vec![0.0f32; frame_size * channels];
1331
1332            let _ = decoder.decode(&[0x80, 0, 0, 0], frame_size, &mut output);
1333        }
1334    }
1335}