Skip to main content

opus_rs/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![allow(unsafe_op_in_unsafe_fn)]
3#![allow(clippy::too_many_arguments)]
4#![allow(clippy::needless_range_loop)]
5
6mod compat;
7mod fixedvec;
8
9pub mod bands;
10pub mod celt;
11pub mod celt_lpc;
12pub mod hp_cutoff;
13pub mod kiss_fft;
14pub mod mdct;
15pub mod modes;
16pub mod pitch;
17pub mod pvq;
18pub mod quant_bands;
19pub mod range_coder;
20pub mod rate;
21pub mod silk;
22
23pub use silk::{SilkResampler, SilkResamplerDown1_3, SilkResamplerDown1_6};
24
25pub use celt::{CeltDecoder, CeltEncoder};
26use hp_cutoff::hp_cutoff;
27use range_coder::RangeCoder;
28use silk::control_codec::silk_control_encoder;
29use silk::enc_api::silk_encode;
30use silk::init_encoder::silk_init_encoder;
31use silk::lin2log::silk_lin2log;
32use silk::log2lin::silk_log2lin;
33use silk::macros::*;
34use silk::resampler::{silk_resampler_down2, silk_resampler_down2_3};
35use silk::structs::SilkEncoderState;
36use crate::fixedvec::FixedVec;
37
38// --- Heap-free buffer capacity constants (worst case: 2 channels). ---
39const OPUS_MAX_CHANNELS: usize = 2;
40/// Largest API frame in samples/channel (120 ms @ 48 kHz = 5760). Used by the
41/// encoder's per-frame input buffers (sized `frame_size * channels`).
42const OPUS_MAX_FRAME: usize = 5760;
43/// Largest *single-frame* samples/channel (60 ms @ 48 kHz = 2880). The decoder's
44/// staging buffers hold one sub-frame at a time, so they're sized to this — not
45/// the full packet. (Halves the decoder footprint vs. a naive 5760/channel.)
46const OPUS_MAX_SUBFRAME: usize = 2880;
47/// Decoder per-sub-frame staging cap: `OPUS_MAX_SUBFRAME * max_channels`.
48const OPUS_SUBFRAME_SCRATCH: usize = OPUS_MAX_SUBFRAME * OPUS_MAX_CHANNELS;
49/// High-pass filter state memory (`channels * 2`).
50const OPUS_HP_MEM: usize = OPUS_MAX_CHANNELS * 2;
51/// Decoder `w_pcm_i16` cap (`960 * max_channels`).
52const OPUS_PCM_I16: usize = 960 * OPUS_MAX_CHANNELS;
53/// Decoder `prev_pcm_tail` cap (`240 * max_channels`).
54const OPUS_PCM_TAIL: usize = 240 * OPUS_MAX_CHANNELS;
55/// Max number of frames encoded in one Opus packet (RFC 6716 caps at 48 for
56/// 2.5 ms codes in a 120 ms packet).
57const OPUS_MAX_PACKET_FRAMES: usize = 48;
58/// RFC 6716 §3.1: a single Opus packet carries at most 1276 bytes of data.
59const OPUS_MAX_PACKET_BYTES: usize = 1276;
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub enum Application {
63    Voip = 2048,
64    Audio = 2049,
65    RestrictedLowDelay = 2051,
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub enum Bandwidth {
70    Auto = -1000,
71    Narrowband = 1101,
72    Mediumband = 1102,
73    Wideband = 1103,
74    Superwideband = 1104,
75    Fullband = 1105,
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79enum OpusMode {
80    SilkOnly,
81    Hybrid,
82    CeltOnly,
83}
84
85pub struct OpusEncoder {
86    celt_enc: CeltEncoder,
87    silk_enc: SilkEncoderState,
88    application: Application,
89    sampling_rate: i32,
90    channels: usize,
91    bandwidth: Bandwidth,
92    pub bitrate_bps: i32,
93    pub complexity: i32,
94    pub use_cbr: bool,
95
96    pub use_inband_fec: bool,
97
98    pub packet_loss_perc: i32,
99    silk_initialized: bool,
100    mode: OpusMode,
101    prev_enc_mode: Option<OpusMode>,
102
103    variable_hp_smth2_q15: i32,
104    hp_mem: FixedVec<i32, OPUS_HP_MEM>,
105
106    buf_filtered: FixedVec<i16, OPUS_MAX_FRAME>,
107    buf_silk_input: FixedVec<i16, OPUS_MAX_FRAME>,
108    buf_stereo_mid: FixedVec<i16, OPUS_MAX_FRAME>,
109    buf_stereo_side: FixedVec<i16, OPUS_MAX_FRAME>,
110    buf_celt_input: FixedVec<f32, OPUS_MAX_FRAME>,
111    down2_state_first: [i32; 2],
112    down2_state_second: [i32; 2],
113    down2_3_state: [i32; 6],
114    down_1_3_state: silk::resampler::SilkResamplerDown1_3,
115
116    rc: RangeCoder,
117}
118
119fn compute_equiv_rate(
120    bitrate: i32,
121    channels: usize,
122    frame_rate: i32,
123    vbr: bool,
124    complexity: i32,
125    loss: i32,
126) -> i32 {
127    let mut equiv = bitrate;
128    if frame_rate > 50 {
129        equiv -= (40 * channels as i32 + 20) * (frame_rate - 50);
130    }
131    if !vbr {
132        equiv -= equiv / 12;
133    }
134    equiv = equiv * (90 + complexity) / 100;
135    if loss > 0 {
136        equiv -= equiv * loss / (12 * loss + 20);
137    }
138    equiv
139}
140
141fn compute_mode_threshold(
142    application: Application,
143    channels: usize,
144    prev_was_celt: bool,
145    has_prev_mode: bool,
146    voice_est: i32,
147) -> i32 {
148    let mode_voice = if channels == 1 { 64000 } else { 44000 };
149    let mode_music = 10000;
150
151    let diff = mode_voice - mode_music;
152    let offset = (voice_est * voice_est * diff) >> 14;
153    let mut threshold = mode_music + offset;
154
155    if application == Application::Voip {
156        threshold += 8000;
157    }
158
159    if has_prev_mode {
160        if prev_was_celt {
161            threshold -= 4000;
162        } else {
163            threshold += 4000;
164        }
165    }
166
167    if application == Application::RestrictedLowDelay {
168        threshold = 0;
169    }
170
171    threshold
172}
173
174fn compute_silk_rate_for_hybrid(rate_bps: i32, frame20ms: bool) -> i32 {
175    const RATE_TABLE: &[(i32, i32, i32)] = &[
176        (0, 0, 0),
177        (12000, 10000, 10000),
178        (16000, 13500, 13500),
179        (20000, 16000, 16000),
180        (24000, 18000, 18000),
181        (32000, 22000, 22000),
182        (64000, 38000, 38000),
183    ];
184    let n = RATE_TABLE.len();
185    let mut i = 1;
186    while i < n && RATE_TABLE[i].0 <= rate_bps {
187        i += 1;
188    }
189    if i == n {
190        let (x_last, r10_last, r20_last) = RATE_TABLE[n - 1];
191        let base = if frame20ms { r20_last } else { r10_last };
192        base + (rate_bps - x_last) / 2
193    } else {
194        let (x0, lo10, lo20) = RATE_TABLE[i - 1];
195        let (x1, hi10, hi20) = RATE_TABLE[i];
196        let (lo, hi) = if frame20ms {
197            (lo20, hi20)
198        } else {
199            (lo10, hi10)
200        };
201        (lo * (x1 - rate_bps) + hi * (rate_bps - x0)) / (x1 - x0)
202    }
203}
204
205#[cfg(all(test, feature = "std"))]
206mod silk_rate_tests {
207    use super::compute_silk_rate_for_hybrid;
208
209    #[test]
210    fn test_reference_table_exact_entries() {
211        assert_eq!(compute_silk_rate_for_hybrid(12000, true), 10000);
212        assert_eq!(compute_silk_rate_for_hybrid(16000, true), 13500);
213        assert_eq!(compute_silk_rate_for_hybrid(20000, true), 16000);
214        assert_eq!(compute_silk_rate_for_hybrid(24000, true), 18000);
215        assert_eq!(compute_silk_rate_for_hybrid(32000, true), 22000);
216        assert_eq!(compute_silk_rate_for_hybrid(64000, true), 38000);
217    }
218
219    #[test]
220    fn test_32kbps_gives_22kbps_silk() {
221        assert_eq!(compute_silk_rate_for_hybrid(32000, true), 22000);
222    }
223
224    #[test]
225    fn test_interpolation_between_table_entries() {
226        let r = compute_silk_rate_for_hybrid(18000, true);
227        assert_eq!(r, 14750);
228    }
229
230    #[test]
231    fn test_above_table_max_gives_half_extra() {
232        let r = compute_silk_rate_for_hybrid(72000, true);
233        assert_eq!(r, 38000 + (72000 - 64000) / 2);
234    }
235}
236
237impl OpusEncoder {
238    pub fn new(
239        sampling_rate: i32,
240        channels: usize,
241        application: Application,
242    ) -> Result<Self, &'static str> {
243        if ![8000, 12000, 16000, 24000, 48000].contains(&sampling_rate) {
244            return Err("Invalid sampling rate");
245        }
246        if ![1, 2].contains(&channels) {
247            return Err("Invalid number of channels");
248        }
249
250        let mode = modes::default_mode();
251        let celt_enc = CeltEncoder::new(mode, channels);
252
253        let mut silk_enc = SilkEncoderState::default();
254        if silk_init_encoder(&mut silk_enc, 0) != 0 {
255            return Err("SILK encoder initialization failed");
256        }
257
258        let (opus_mode, bw) = match application {
259            Application::Voip => {
260                let bw = match sampling_rate {
261                    8000 => Bandwidth::Narrowband,
262                    12000 => Bandwidth::Mediumband,
263                    16000 => Bandwidth::Wideband,
264                    24000 => Bandwidth::Superwideband,
265                    48000 => Bandwidth::Fullband,
266                    _ => Bandwidth::Narrowband,
267                };
268
269                let mode = if sampling_rate > 16000 {
270                    OpusMode::Hybrid
271                } else {
272                    OpusMode::SilkOnly
273                };
274                (mode, bw)
275            }
276            Application::RestrictedLowDelay => {
277                let bw = match sampling_rate {
278                    8000 => Bandwidth::Narrowband,
279                    12000 => Bandwidth::Mediumband,
280                    16000 => Bandwidth::Wideband,
281                    24000 => Bandwidth::Superwideband,
282                    _ => Bandwidth::Fullband,
283                };
284                (OpusMode::CeltOnly, bw)
285            }
286            Application::Audio => {
287                if sampling_rate <= 16000 {
288                    let bw = match sampling_rate {
289                        8000 => Bandwidth::Narrowband,
290                        12000 => Bandwidth::Mediumband,
291                        _ => Bandwidth::Wideband,
292                    };
293                    (OpusMode::SilkOnly, bw)
294                } else {
295                    let bw = match sampling_rate {
296                        24000 => Bandwidth::Superwideband,
297                        _ => Bandwidth::Fullband,
298                    };
299                    (OpusMode::Hybrid, bw)
300                }
301            }
302        };
303
304        use silk::lin2log::silk_lin2log;
305        let variable_hp_smth2_q15 = silk_lin2log(60) << 8;
306
307        Ok(Self {
308            celt_enc,
309            silk_enc,
310            application,
311            sampling_rate,
312            channels,
313            bandwidth: bw,
314            bitrate_bps: 64000,
315            complexity: 9,
316            use_cbr: false,
317            use_inband_fec: false,
318            packet_loss_perc: 0,
319            silk_initialized: false,
320            prev_enc_mode: None,
321            mode: opus_mode,
322            variable_hp_smth2_q15,
323            hp_mem: FixedVec::from_value(0, channels * 2),
324
325            buf_filtered: FixedVec::new(),
326            buf_silk_input: FixedVec::new(),
327            buf_stereo_mid: FixedVec::new(),
328            buf_stereo_side: FixedVec::new(),
329            buf_celt_input: FixedVec::new(),
330            down2_state_first: [0; 2],
331            down2_state_second: [0; 2],
332            down2_3_state: [0; 6],
333            down_1_3_state: silk::resampler::SilkResamplerDown1_3::default(),
334            rc: RangeCoder::new_encoder(1),
335        })
336    }
337
338    pub fn enable_hybrid_mode(&mut self) -> Result<(), &'static str> {
339        if self.sampling_rate != 24000 && self.sampling_rate != 48000 {
340            return Err("Hybrid mode requires 24kHz or 48kHz sampling rate");
341        }
342        let bw = if self.sampling_rate == 48000 {
343            Bandwidth::Fullband
344        } else {
345            Bandwidth::Superwideband
346        };
347        self.mode = OpusMode::Hybrid;
348        self.bandwidth = bw;
349        self.silk_initialized = false;
350        Ok(())
351    }
352
353    pub fn encode(
354        &mut self,
355        input: &[f32],
356        frame_size: usize,
357        output: &mut [u8],
358    ) -> Result<usize, &'static str> {
359        if output.len() < 2 {
360            return Err("Output buffer too small");
361        }
362
363        let frame_rate = frame_rate_from_params(self.sampling_rate, frame_size)
364            .ok_or("Invalid frame size for sampling rate")?;
365
366        // Mode selection: match C's opus_encode_native() behavior.
367        // C reference auto-selects between SILK_ONLY and CELT_ONLY; Hybrid is
368        // produced afterwards by bandwidth overrides (SILK-only + FB/SWB → Hybrid).
369        let mut mode = if self.application == Application::RestrictedLowDelay {
370            OpusMode::CeltOnly
371        } else {
372            let equiv = compute_equiv_rate(
373                self.bitrate_bps,
374                self.channels,
375                frame_rate,
376                !self.use_cbr,
377                self.complexity,
378                self.packet_loss_perc,
379            );
380            let prev_was_celt = self.prev_enc_mode == Some(OpusMode::CeltOnly);
381            let has_prev_mode = self.prev_enc_mode.is_some();
382            let voice_est = match self.application {
383                Application::Voip => 115,
384                Application::Audio => 48,
385                Application::RestrictedLowDelay => 0,
386            };
387            let threshold = compute_mode_threshold(
388                self.application,
389                self.channels,
390                prev_was_celt,
391                has_prev_mode,
392                voice_est,
393            );
394            if equiv >= threshold && self.sampling_rate >= 24000 {
395                OpusMode::CeltOnly
396            } else {
397                OpusMode::SilkOnly
398            }
399        };
400
401        let curr_bw = self.bandwidth;
402        if mode == OpusMode::SilkOnly
403            && (curr_bw == Bandwidth::Superwideband || curr_bw == Bandwidth::Fullband)
404        {
405            mode = OpusMode::Hybrid;
406        }
407        if mode == OpusMode::Hybrid
408            && (curr_bw == Bandwidth::Narrowband
409                || curr_bw == Bandwidth::Mediumband
410                || curr_bw == Bandwidth::Wideband)
411        {
412            mode = OpusMode::SilkOnly;
413        }
414
415        if mode == OpusMode::CeltOnly {
416            match frame_rate {
417                400 | 200 | 100 | 50 => {}
418                _ => return Err("Unsupported frame size for CELT-only mode"),
419            }
420        }
421
422        if mode == OpusMode::Hybrid {
423            match frame_rate {
424                100 | 50 => {}
425                _ => return Err("Unsupported frame size for Hybrid mode"),
426            }
427        }
428
429        if mode == OpusMode::SilkOnly {
430            match frame_rate {
431                400 | 200 | 100 | 50 | 25 => {}
432                _ => return Err("Unsupported frame size for SILK-only mode"),
433            }
434        }
435
436        let toc = gen_toc(mode, frame_rate, self.bandwidth, self.channels);
437        output[0] = toc;
438
439        let target_bits =
440            (self.bitrate_bps as i64 * frame_size as i64 / self.sampling_rate as i64) as i32;
441        let cbr_bytes = ((target_bits + 4) / 8) as usize;
442        let max_data_bytes = output.len();
443
444        // Cap at the Opus per-packet maximum (RFC 6716); the range coder's buffer
445        // is heap-free and sized to this constant.
446        let n_bytes = cbr_bytes
447            .min(max_data_bytes)
448            .max(1)
449            .min(OPUS_MAX_PACKET_BYTES);
450
451        let init_rc_size = n_bytes - 1;
452        self.rc.reset_for_encode(init_rc_size as u32);
453
454        if mode == OpusMode::SilkOnly || mode == OpusMode::Hybrid {
455            let silk_fs_khz = if mode == OpusMode::Hybrid {
456                16
457            } else {
458                self.sampling_rate.min(16000) / 1000
459            };
460
461            let frame_ms = (frame_size as i32 * 1000) / self.sampling_rate;
462            if !self.silk_initialized || self.silk_enc.s_cmn.fs_khz != silk_fs_khz {
463                let silk_init_bitrate = (((n_bytes - 1) * 8) as i64 * self.sampling_rate as i64
464                    / frame_size as i64) as i32;
465                silk_control_encoder(
466                    &mut self.silk_enc,
467                    silk_fs_khz,
468                    frame_ms,
469                    silk_init_bitrate,
470                    self.complexity,
471                );
472                self.silk_enc.s_cmn.use_cbr = if self.use_cbr { 1 } else { 0 };
473
474                self.silk_enc.s_cmn.n_channels = self.channels as i32;
475                self.silk_initialized = true;
476                self.down2_state_first = [0; 2];
477                self.down2_state_second = [0; 2];
478                self.down2_3_state = [0; 6];
479                self.down_1_3_state = silk::resampler::SilkResamplerDown1_3::default();
480            }
481
482            self.silk_enc.s_cmn.use_in_band_fec = if self.use_inband_fec { 1 } else { 0 };
483            self.silk_enc.s_cmn.packet_loss_perc = self.packet_loss_perc.clamp(0, 100);
484
485            self.silk_enc.s_cmn.lbrr_enabled = if self.use_inband_fec { 1 } else { 0 };
486
487            if self.silk_enc.s_cmn.lbrr_gain_increases == 0 {
488                self.silk_enc.s_cmn.lbrr_gain_increases = 2;
489            }
490
491            let hp_freq_smth1 = if mode == OpusMode::CeltOnly {
492                silk_lin2log(60) << 8
493            } else {
494                self.silk_enc.s_cmn.variable_hp_smth1_q15
495            };
496
497            const VARIABLE_HP_SMTH_COEF2_Q16: i32 = 984;
498            self.variable_hp_smth2_q15 = silk_smlawb(
499                self.variable_hp_smth2_q15,
500                hp_freq_smth1 - self.variable_hp_smth2_q15,
501                VARIABLE_HP_SMTH_COEF2_Q16,
502            );
503
504            let cutoff_hz = silk_log2lin(silk_rshift(self.variable_hp_smth2_q15, 8));
505
506            let required_size = frame_size * self.channels;
507            self.buf_filtered.resize(required_size, 0);
508            if self.application == Application::Voip {
509                hp_cutoff(
510                    input,
511                    cutoff_hz,
512                    &mut self.buf_filtered,
513                    &mut self.hp_mem,
514                    frame_size,
515                    self.channels,
516                    self.sampling_rate,
517                );
518            } else {
519                for (i, &x) in input.iter().enumerate() {
520                    self.buf_filtered[i] = (x * 32768.0).clamp(-32768.0, 32767.0) as i16;
521                }
522            }
523
524            let input_i16 = &self.buf_filtered;
525
526            let silk_input: &[i16] = if mode == OpusMode::SilkOnly && self.sampling_rate > 16000 {
527                if self.sampling_rate == 48000 {
528                    let stage1_size = frame_size / 2;
529                    let mut stage1_buf = [0i16; 480];
530                    silk_resampler_down2(
531                        &mut self.down2_state_first,
532                        &mut stage1_buf[..stage1_size],
533                        input_i16,
534                        frame_size as i32,
535                    );
536                    let silk_frame_size = stage1_size * 2 / 3;
537                    self.buf_silk_input.resize(silk_frame_size, 0);
538                    silk_resampler_down2_3(
539                        &mut self.down2_3_state,
540                        &mut self.buf_silk_input,
541                        &stage1_buf[..stage1_size],
542                        stage1_size as i32,
543                    );
544                    &self.buf_silk_input
545                } else if self.sampling_rate == 24000 {
546                    let silk_frame_size = frame_size * 2 / 3;
547                    self.buf_silk_input.resize(silk_frame_size, 0);
548                    silk_resampler_down2_3(
549                        &mut self.down2_3_state,
550                        &mut self.buf_silk_input,
551                        input_i16,
552                        frame_size as i32,
553                    );
554                    &self.buf_silk_input
555                } else {
556                    input_i16
557                }
558            } else if mode == OpusMode::SilkOnly && self.channels == 2 {
559                let frame_length = input_i16.len() / 2;
560                self.buf_stereo_mid.resize(frame_length, 0);
561                self.buf_stereo_side.resize(frame_length, 0);
562                for i in 0..frame_length {
563                    let l = input_i16[2 * i] as i32;
564                    let r = input_i16[2 * i + 1] as i32;
565                    self.buf_stereo_mid[i] = ((l + r) / 2) as i16;
566                    self.buf_stereo_side[i] = (l - r) as i16;
567                }
568
569                self.silk_enc.stereo.side.resize(frame_length, 0);
570                self.silk_enc
571                    .stereo
572                    .side
573                    .copy_from_slice(&self.buf_stereo_side[..frame_length]);
574                &self.buf_stereo_mid
575            } else if mode == OpusMode::Hybrid && self.sampling_rate > 16000 {
576                if self.sampling_rate == 48000 {
577                    let silk_frame_size = frame_size / 3;
578                    self.buf_silk_input.resize(silk_frame_size, 0);
579                    silk::resampler::silk_resampler_down_1_3(
580                        &mut self.down_1_3_state,
581                        &mut self.buf_silk_input,
582                        input_i16,
583                    );
584                } else {
585                    let silk_frame_size = frame_size * 2 / 3;
586                    self.buf_silk_input.resize(silk_frame_size, 0);
587                    silk_resampler_down2_3(
588                        &mut self.down2_3_state,
589                        &mut self.buf_silk_input,
590                        input_i16,
591                        frame_size as i32,
592                    );
593                }
594                &self.buf_silk_input
595            } else {
596                input_i16
597            };
598
599            let mut pn_bytes = 0;
600
601            let silk_rate_for_calc = if mode == OpusMode::Hybrid {
602                16000
603            } else {
604                self.sampling_rate
605            };
606            let silk_frame_len = silk_input.len();
607
608            let silk_bitrate = if mode == OpusMode::Hybrid {
609                let frame_duration_ms = frame_size as i32 * 1000 / self.sampling_rate;
610                let frame20ms = frame_duration_ms >= 20;
611                compute_silk_rate_for_hybrid(self.bitrate_bps, frame20ms)
612            } else {
613                (8i64 * (n_bytes - 1) as i64 * silk_rate_for_calc as i64 / silk_frame_len as i64)
614                    as i32
615            };
616            let silk_max_bits = if mode == OpusMode::Hybrid {
617                let total_max_bits = ((n_bytes - 1) * 8) as i32;
618                if self.use_cbr {
619                    let silk_bits = (silk_bitrate as i64 * silk_frame_len as i64
620                        / silk_rate_for_calc as i64) as i32;
621                    let other_bits = 0i32.max(total_max_bits - silk_bits);
622                    0i32.max(total_max_bits - other_bits * 3 / 4)
623                } else {
624                    let frame_duration_ms = frame_size as i32 * 1000 / self.sampling_rate;
625                    let frame20ms = frame_duration_ms >= 20;
626                    let max_bit_rate = compute_silk_rate_for_hybrid(
627                        total_max_bits * self.sampling_rate / frame_size as i32,
628                        frame20ms,
629                    );
630                    max_bit_rate * frame_size as i32 / self.sampling_rate
631                }
632            } else {
633                ((n_bytes - 1) * 8) as i32
634            };
635            let silk_use_cbr = if mode == OpusMode::Hybrid && self.use_cbr {
636                0
637            } else if self.use_cbr {
638                1
639            } else {
640                0
641            };
642            let ret = silk_encode(
643                &mut self.silk_enc,
644                silk_input,
645                silk_input.len(),
646                &mut self.rc,
647                &mut pn_bytes,
648                silk_bitrate,
649                silk_max_bits,
650                silk_use_cbr,
651                1,
652            );
653            if ret != 0 {
654                return Err("SILK encoding failed");
655            }
656        }
657
658        if mode == OpusMode::Hybrid {
659            self.rc.encode_bit_logp(false, 12); // redundancy = 0
660        }
661
662        if mode == OpusMode::Hybrid {
663            let nb_compr_bytes = (n_bytes - 1) as u32;
664            self.rc.shrink(nb_compr_bytes);
665        }
666
667        let silk_ret_bytes = if mode == OpusMode::SilkOnly {
668            ((self.rc.tell() + 7) >> 3) as usize
669        } else {
670            0
671        };
672
673        if mode == OpusMode::CeltOnly || mode == OpusMode::Hybrid {
674            self.celt_enc.complexity = self.complexity;
675            let start_band = if mode == OpusMode::Hybrid { 17 } else { 0 };
676            let total_packet_bits = ((n_bytes - 1) * 8) as i32;
677
678            let celt_input: &[f32] = if self.channels == 1 {
679                input
680            } else {
681                let n = frame_size * self.channels;
682                self.buf_celt_input.resize(n, 0.0);
683                for i in 0..frame_size {
684                    for ch in 0..self.channels {
685                        self.buf_celt_input[ch * frame_size + i] = input[i * self.channels + ch];
686                    }
687                }
688                &self.buf_celt_input
689            };
690
691            if self.rc.tell() <= total_packet_bits {
692                self.celt_enc.encode_with_budget(
693                    celt_input,
694                    frame_size,
695                    &mut self.rc,
696                    start_band,
697                    total_packet_bits,
698                );
699            }
700        }
701
702        self.rc.done();
703
704        if mode == OpusMode::SilkOnly {
705            let mut ret = silk_ret_bytes.min(self.rc.storage as usize);
706            while ret > 2 && self.rc.buf[ret - 1] == 0 {
707                ret -= 1;
708            }
709
710            let target_total = if self.use_cbr {
711                n_bytes.min(output.len())
712            } else {
713                (ret + 1).min(output.len())
714            };
715
716            let silk_len = ret;
717
718            if !self.use_cbr || silk_len + 1 >= target_total {
719                // VBR or payload fills the target: simple code 0 packet
720                output[0] = toc;
721                let copy_len = silk_len.min(target_total - 1);
722                output[1..1 + copy_len].copy_from_slice(&self.rc.buf[..copy_len]);
723                return Ok((copy_len + 1).min(output.len()));
724            }
725
726            output[0] = toc | 0x03;
727
728            if silk_len + 2 >= target_total {
729                output[1] = 0x01;
730                let copy_len = (target_total - 2).min(silk_len);
731                output[2..2 + copy_len].copy_from_slice(&self.rc.buf[..copy_len]);
732                self.prev_enc_mode = Some(mode);
733                return Ok(target_total.min(output.len()));
734            }
735
736            let pad_amount = target_total - silk_len - 2;
737            output[1] = 0x41;
738
739            let nb_255s = (pad_amount - 1) / 255;
740            let mut ptr = 2;
741            for _ in 0..nb_255s {
742                output[ptr] = 255;
743                ptr += 1;
744            }
745            output[ptr] = (pad_amount - 255 * nb_255s - 1) as u8;
746            ptr += 1;
747
748            output[ptr..ptr + silk_len].copy_from_slice(&self.rc.buf[..silk_len]);
749            ptr += silk_len;
750
751            let fill_end = target_total.min(output.len());
752            for byte in output[ptr..fill_end].iter_mut() {
753                *byte = 0;
754            }
755
756            self.prev_enc_mode = Some(mode);
757            return Ok(target_total.min(output.len()));
758        }
759
760        let payload_len = n_bytes - 1;
761        output[1..1 + payload_len].copy_from_slice(&self.rc.buf[..payload_len]);
762        self.prev_enc_mode = Some(mode);
763        Ok(n_bytes)
764    }
765}
766
767pub struct OpusDecoder {
768    celt_dec: CeltDecoder,
769    silk_dec: silk::dec_api::SilkDecoder,
770    sampling_rate: i32,
771    channels: usize,
772
773    prev_mode: Option<OpusMode>,
774
775    /// Whether the previous frame had redundancy (mode transition marker).
776    prev_redundancy: bool,
777    frame_size: usize,
778
779    bandwidth: Bandwidth,
780
781    stream_channels: usize,
782
783    silk_resampler: silk::resampler::SilkResampler,
784
785    /// Second resampler instance for stereo channel 1.
786    silk_resampler_2: silk::resampler::SilkResampler,
787
788    prev_internal_rate: i32,
789
790    pub hybrid_skip_celt: bool,
791
792    w_pcm_i16: FixedVec<i16, OPUS_PCM_I16>,
793    w_silk_out: FixedVec<f32, OPUS_SUBFRAME_SCRATCH>,
794    w_pcm_resampled: FixedVec<i16, OPUS_SUBFRAME_SCRATCH>,
795    w_celt_planar: FixedVec<f32, OPUS_SUBFRAME_SCRATCH>,
796    w_celt_out: FixedVec<f32, OPUS_SUBFRAME_SCRATCH>,
797
798    /// Tail of the previous frame's output, used for smooth_fade at mode
799    /// transitions (libopus pcm_transition + smooth_fade).
800    prev_pcm_tail: FixedVec<f32, OPUS_PCM_TAIL>,
801}
802
803impl OpusDecoder {
804    pub fn new(sampling_rate: i32, channels: usize) -> Result<Self, &'static str> {
805        if ![8000, 12000, 16000, 24000, 48000].contains(&sampling_rate) {
806            return Err("Invalid sampling rate");
807        }
808        if ![1, 2].contains(&channels) {
809            return Err("Invalid number of channels");
810        }
811
812        let mode = modes::default_mode();
813        let celt_dec = CeltDecoder::new(mode, channels, sampling_rate);
814
815        let mut silk_dec = silk::dec_api::SilkDecoder::new();
816        silk_dec.init(sampling_rate.min(16000), channels as i32);
817        silk_dec.channel_state[0].fs_api_hz = sampling_rate;
818
819        Ok(Self {
820            celt_dec,
821            silk_dec,
822            sampling_rate,
823            channels,
824            prev_mode: None,
825            prev_redundancy: false,
826            frame_size: 0,
827            bandwidth: Bandwidth::Auto,
828            stream_channels: channels,
829            silk_resampler: silk::resampler::SilkResampler::default(),
830            silk_resampler_2: silk::resampler::SilkResampler::default(),
831            prev_internal_rate: 0,
832            hybrid_skip_celt: false,
833
834            w_pcm_i16: FixedVec::from_value(0i16, 960 * channels),
835
836            w_silk_out: FixedVec::from_value(0.0f32, OPUS_MAX_SUBFRAME * channels),
837            w_pcm_resampled: FixedVec::from_value(0i16, OPUS_MAX_SUBFRAME * channels),
838            w_celt_planar: FixedVec::from_value(0.0f32, OPUS_MAX_SUBFRAME * channels),
839            w_celt_out: FixedVec::from_value(0.0f32, OPUS_MAX_SUBFRAME * channels),
840
841            prev_pcm_tail: FixedVec::from_value(0.0f32, 240 * channels),
842        })
843    }
844
845    pub fn decode(
846        &mut self,
847        input: &[u8],
848        frame_size: usize,
849        output: &mut [f32],
850    ) -> Result<usize, &'static str> {
851        if input.is_empty() {
852            return Err("Input packet empty");
853        }
854
855        let toc = input[0];
856        let mode = mode_from_toc(toc);
857        let packet_channels = channels_from_toc(toc);
858        let bandwidth = bandwidth_from_toc(toc);
859        let frame_duration_ms = frame_duration_ms_from_toc(toc);
860
861        // A packet of 0 or 1 bytes (ToC only) is a lost/DTX frame. libopus
862        // triggers PLC in this case (opus_decoder.c:315-321). We decode the
863        // frame using the previous mode's concealment.
864        let lost_frame = input.len() <= 1;
865
866        if packet_channels != self.channels {
867            return Err("Channel count mismatch between packet and decoder");
868        }
869
870        let code = toc & 0x03;
871        let frame_count: usize;
872        let frame_payloads: FixedVec<&[u8], OPUS_MAX_PACKET_FRAMES>;
873
874        match code {
875            0 => {
876                frame_count = 1;
877                frame_payloads = FixedVec::from_slice(&[&input[1..]]);
878            }
879            1 => {
880                frame_count = 2;
881                let data_len = input.len() - 1;
882                // RFC 6716 §3.2.1: code 1 carries two equal-size (CBR) frames,
883                // so the payload length must be even. libopus rejects odd lengths.
884                if data_len % 2 != 0 {
885                    return Err("Code 1: payload length must be even");
886                }
887                let half = data_len / 2;
888                if half == 0 {
889                    return Err("Code 1: empty frame");
890                }
891                frame_payloads = FixedVec::from_slice(&[&input[1..1 + half], &input[1 + half..]]);
892            }
893            2 => {
894                frame_count = 2;
895                let data = &input[1..];
896                if data.is_empty() {
897                    return Err("Code 2 packet has no data");
898                }
899                let (first_len, header_size) = parse_frame_size(data)?;
900                if header_size + first_len > data.len() {
901                    return Err("Code 2: first frame size exceeds packet");
902                }
903                frame_payloads = FixedVec::from_slice(&[
904                    &data[header_size..header_size + first_len],
905                    &data[header_size + first_len..],
906                ]);
907            }
908            3 => {
909                if input.len() < 2 {
910                    return Err("Code 3 packet too short");
911                }
912                let count_byte = input[1];
913                let n_frames = (count_byte & 0x3F) as usize;
914                if n_frames < 1 || n_frames > 48 {
915                    return Err("Code 3: invalid frame count");
916                }
917                frame_count = n_frames;
918                // Bit 6 = padding flag, bit 7 = VBR flag (RFC 6716 §3.2.1).
919                let padding_flag = (count_byte & 0x40) != 0;
920                let vbr = (count_byte & 0x80) != 0;
921
922                // Parse the optional padding length bytes that follow the count
923                // byte. The padding *content* (pad_len bytes) lives at the end of
924                // the packet and is not part of any frame.
925                let mut ptr = 2usize;
926                let mut pad_len = 0usize;
927                if padding_flag {
928                    loop {
929                        if ptr >= input.len() {
930                            return Err("Code 3: padding overflow");
931                        }
932                        let p = input[ptr] as usize;
933                        ptr += 1;
934                        if p == 255 {
935                            pad_len += 254;
936                        } else {
937                            pad_len += p;
938                            break;
939                        }
940                    }
941                }
942                if ptr + pad_len > input.len() {
943                    return Err("Code 3: padding exceeds packet");
944                }
945                let payload_end = input.len() - pad_len;
946                let payload = &input[ptr..payload_end];
947
948                let mut payloads: FixedVec<&[u8], OPUS_MAX_PACKET_FRAMES> = FixedVec::new();
949                if frame_count == 1 {
950                    // Single frame: the entire payload region is the frame, both
951                    // for VBR and CBR (no length prefix is present).
952                    payloads.push(payload);
953                } else if vbr {
954                    // VBR (V=1): per-frame lengths for all frames except the last,
955                    // which takes the remaining bytes (RFC 6716 §3.2.1).
956                    let mut cursor = 0usize;
957                    for i in 0..frame_count {
958                        if i + 1 < frame_count {
959                            if cursor >= payload.len() {
960                                return Err("Code 3: unexpected end in VBR header");
961                            }
962                            let (frame_len, header_bytes) =
963                                parse_frame_size(&payload[cursor..])?;
964                            cursor += header_bytes;
965                            if cursor + frame_len > payload.len() {
966                                return Err("Code 3: frame length exceeds packet");
967                            }
968                            payloads.push(&payload[cursor..cursor + frame_len]);
969                            cursor += frame_len;
970                        } else {
971                            // Last frame: remaining bytes, no length prefix.
972                            if cursor > payload.len() {
973                                return Err("Code 3: no data for last frame");
974                            }
975                            payloads.push(&payload[cursor..]);
976                        }
977                    }
978                } else {
979                    // CBR (V=0): remaining bytes are split equally into M frames
980                    // (RFC 6716 §3.2.1: "the remaining bytes are split into M
981                    // equal chunks").
982                    if payload.len() % frame_count != 0 {
983                        return Err("Code 3 CBR: payload not divisible by frame count");
984                    }
985                    let frame_len = payload.len() / frame_count;
986                    for i in 0..frame_count {
987                        payloads.push(&payload[i * frame_len..(i + 1) * frame_len]);
988                    }
989                }
990                frame_payloads = payloads;
991            }
992            _ => unreachable!(),
993        }
994
995        self.frame_size = frame_size;
996        self.bandwidth = bandwidth;
997        self.stream_channels = packet_channels;
998
999        // Derive the actual per-frame sample count from the TOC, not from the
1000        // caller's frame_size. This prevents panics in bands.rs/celt.rs when
1001        // the caller passes a mismatched frame_size (issue #7 sub-item 1):
1002        // the internal decoders always get the correct geometry.
1003        let toc_frame_size = frame_samples_from_toc(toc, self.sampling_rate)
1004            .ok_or("Invalid TOC for sampling rate")?;
1005        let decoded_total = toc_frame_size * frame_count;
1006        if frame_size < decoded_total {
1007            return Err("frame_size too small for packet");
1008        }
1009        if output.len() < decoded_total * self.channels {
1010            return Err("Output buffer too small for packet");
1011        }
1012        // Zero-fill any extra space the caller provided beyond what the packet
1013        // actually produces, so stale data is never left in the buffer.
1014        if output.len() > decoded_total * self.channels {
1015            for v in &mut output[decoded_total * self.channels..] {
1016                *v = 0.0;
1017            }
1018        }
1019        let sub_frame_size = toc_frame_size;
1020        let sub_output_len = sub_frame_size * self.channels;
1021
1022        // Detect mode transition and reset CELT decoder state to prevent
1023        // cross-mode artifacts (libopus opus_decoder.c:602-604).
1024        // This is the primary fix for issue #8/#9 alignment divergence:
1025        // stale CELT MDCT/prefilter state at SILK↔CELT boundaries causes
1026        // discontinuities that accumulate across transitions.
1027        let mode_transition = match self.prev_mode {
1028            Some(prev) if prev != mode && !self.prev_redundancy => true,
1029            _ => false,
1030        };
1031        if mode_transition {
1032            self.celt_dec.reset_state();
1033        }
1034
1035        // Generate SILK PLC audio for the mode-transition bridge. libopus
1036        // synthesizes 5ms (F5) of pitch-extrapolated audio in the OLD mode
1037        // (opus_decoder.c:387-391) and crossfades it with the new frame. We
1038        // reuse the F5-sized prev_pcm_tail buffer for this bridge.
1039        let f5_bridge = self.sampling_rate as usize / 200; // F5 = Fs/200
1040        if mode_transition
1041            && f5_bridge > 0
1042            && matches!(
1043                self.prev_mode,
1044                Some(OpusMode::SilkOnly) | Some(OpusMode::Hybrid)
1045            )
1046            && self.prev_internal_rate > 0
1047        {
1048            let internal_rate = self.prev_internal_rate;
1049            let plc_internal_len = (10 * internal_rate / 1000) as usize;
1050            let mut plc_rc = RangeCoder::new_decoder(&[]);
1051            let mut plc_i16: FixedVec<i16, OPUS_PCM_I16> =
1052                FixedVec::from_value(0i16, plc_internal_len * self.channels);
1053            let n = self.silk_dec.decode(
1054                &mut plc_rc,
1055                &mut plc_i16,
1056                silk::decode_frame::FLAG_PACKET_LOST,
1057                true,
1058                10,
1059                internal_rate,
1060            );
1061            if n > 0 {
1062                let bridge_ch = f5_bridge * self.channels;
1063                let bridge_len = bridge_ch.min(self.prev_pcm_tail.len());
1064                if internal_rate == self.sampling_rate {
1065                    // No resampling: copy PLC samples directly (ch0 planar).
1066                    let n_us = n as usize;
1067                    for ch in 0..self.channels {
1068                        let src_base = ch * n_us;
1069                        for i in 0..(bridge_len / self.channels).min(n_us) {
1070                            let dst = i * self.channels + ch;
1071                            if dst < bridge_len {
1072                                self.prev_pcm_tail[dst] = plc_i16[src_base + i] as f32 / 32768.0;
1073                            }
1074                        }
1075                    }
1076                } else if self.silk_resampler.is_initialized() {
1077                    // Resample channel 0 to the API rate for the bridge.
1078                    let ratio = self.sampling_rate as f64 / internal_rate as f64;
1079                    let out_len = ((n as f64 * ratio) as usize).min(f5_bridge);
1080                    let n_us = n as usize;
1081                    let mut resampled: FixedVec<i16, OPUS_MAX_FRAME> = FixedVec::from_value(0i16, out_len);
1082                    self.silk_resampler.process(
1083                        &mut resampled,
1084                        &plc_i16[..n_us],
1085                        n,
1086                    );
1087                    for i in 0..out_len {
1088                        if i < bridge_len / self.channels {
1089                            for ch in 0..self.channels {
1090                                self.prev_pcm_tail[i * self.channels + ch] =
1091                                    resampled[i] as f32 / 32768.0;
1092                            }
1093                        }
1094                    }
1095                }
1096            }
1097        }
1098
1099        // Track whether this packet uses Hybrid redundancy.
1100        let mut has_redundancy = false;
1101
1102        match mode {
1103            OpusMode::SilkOnly => {
1104                let internal_sample_rate = match bandwidth {
1105                    Bandwidth::Narrowband => 8000,
1106                    Bandwidth::Mediumband => 12000,
1107                    Bandwidth::Wideband => 16000,
1108                    _ => 16000,
1109                };
1110                let internal_frame_size =
1111                    (frame_duration_ms * internal_sample_rate / 1000) as usize;
1112
1113                if self.sampling_rate != internal_sample_rate
1114                    && internal_sample_rate != self.prev_internal_rate
1115                {
1116                    self.silk_resampler
1117                        .init(internal_sample_rate, self.sampling_rate);
1118                    self.silk_resampler_2
1119                        .init(internal_sample_rate, self.sampling_rate);
1120                }
1121                // Always track the SILK internal rate so the mode-transition
1122                // PLC bridge can be generated (even when no resampling is
1123                // needed, e.g. 16kHz decoder + SILK WB).
1124                self.prev_internal_rate = internal_sample_rate;
1125
1126                for (fi, payload) in frame_payloads.iter().enumerate() {
1127                    let mut rc = RangeCoder::new_decoder(payload);
1128                    let pcm_i16_len = internal_frame_size * self.channels;
1129                    debug_assert!(pcm_i16_len <= self.w_pcm_i16.len());
1130
1131                    let ret = {
1132                        let (silk_dec, pcm_i16) = (&mut self.silk_dec, &mut self.w_pcm_i16);
1133                        let lost_flag = if lost_frame {
1134                            silk::decode_frame::FLAG_PACKET_LOST
1135                        } else {
1136                            silk::decode_frame::FLAG_DECODE_NORMAL
1137                        };
1138                        silk_dec.decode(
1139                            &mut rc,
1140                            &mut pcm_i16[..pcm_i16_len],
1141                            lost_flag,
1142                            true,
1143                            frame_duration_ms,
1144                            internal_sample_rate,
1145                        )
1146                    };
1147
1148                    if ret < 0 {
1149                        return Err("SILK decoding failed");
1150                    }
1151
1152                    let decoded_samples = ret as usize;
1153                    let out_start = fi * sub_output_len;
1154
1155                    // SILK decoder outputs planar: ch0 at [0..fl], ch1 at [fl..2*fl].
1156                    if self.sampling_rate == internal_sample_rate {
1157                        let frames = decoded_samples.min(sub_frame_size);
1158                        for i in 0..frames {
1159                            for ch in 0..self.channels {
1160                                let src = if ch == 0 { i } else { internal_frame_size + i };
1161                                let v = self.w_pcm_i16[src] as f32 / 32768.0;
1162                                let idx = out_start + i * self.channels + ch;
1163                                if idx < output.len() {
1164                                    output[idx] = v;
1165                                }
1166                            }
1167                        }
1168                    } else {
1169                        let ratio = self.sampling_rate as f64 / internal_sample_rate as f64;
1170                        let out_len =
1171                            ((decoded_samples as f64 * ratio) as usize).min(sub_frame_size);
1172                        debug_assert!(out_len * self.channels <= self.w_pcm_resampled.len());
1173                        // Resample channel 0.
1174                        {
1175                            let (res, inp, out) = (
1176                                &mut self.silk_resampler,
1177                                &self.w_pcm_i16,
1178                                &mut self.w_pcm_resampled,
1179                            );
1180                            res.process(
1181                                &mut out[..out_len],
1182                                &inp[..decoded_samples],
1183                                decoded_samples as i32,
1184                            );
1185                        }
1186                        // Resample channel 1 (stereo only).
1187                        if self.channels == 2 {
1188                            let (res, inp, out) = (
1189                                &mut self.silk_resampler_2,
1190                                &self.w_pcm_i16,
1191                                &mut self.w_pcm_resampled,
1192                            );
1193                            res.process(
1194                                &mut out[out_len..2 * out_len],
1195                                &inp[internal_frame_size..internal_frame_size + decoded_samples],
1196                                decoded_samples as i32,
1197                            );
1198                        }
1199                        let frames = out_len.min(sub_frame_size);
1200                        for i in 0..frames {
1201                            for ch in 0..self.channels {
1202                                let v = self.w_pcm_resampled[ch * out_len + i] as f32 / 32768.0;
1203                                let idx = out_start + i * self.channels + ch;
1204                                if idx < output.len() {
1205                                    output[idx] = v;
1206                                }
1207                            }
1208                        }
1209                    }
1210                }
1211                decoded_total
1212            }
1213
1214            OpusMode::CeltOnly => {
1215                let celt_end_band = self.celt_end_band_from_toc(toc);
1216
1217                for (fi, payload) in frame_payloads.iter().enumerate() {
1218                    let mut rc = RangeCoder::new_decoder(payload);
1219                    let total_bits = (payload.len() * 8) as i32;
1220                    let needed = sub_frame_size * self.channels;
1221                    let out_start = fi * needed;
1222                    let out_end = (out_start + needed).min(output.len());
1223
1224                    if output.len() < out_end {
1225                        return Err("Output buffer too small");
1226                    }
1227
1228                    if self.channels == 1 {
1229                        self.celt_dec.decode_from_range_coder_with_band_range(
1230                            &mut rc,
1231                            total_bits,
1232                            sub_frame_size,
1233                            &mut output[out_start..out_end],
1234                            0,
1235                            celt_end_band,
1236                        );
1237                        for sample in &mut output[out_start..out_end] {
1238                            *sample = sample.clamp(-1.0, 1.0);
1239                        }
1240                    } else {
1241                        self.celt_dec.decode_from_range_coder_with_band_range(
1242                            &mut rc,
1243                            total_bits,
1244                            sub_frame_size,
1245                            &mut self.w_celt_planar[..needed],
1246                            0,
1247                            celt_end_band,
1248                        );
1249                        for i in 0..sub_frame_size {
1250                            for ch in 0..self.channels {
1251                                let idx = out_start + i * self.channels + ch;
1252                                output[idx] =
1253                                    self.w_celt_planar[ch * sub_frame_size + i].clamp(-1.0, 1.0);
1254                            }
1255                        }
1256                    }
1257                }
1258                decoded_total
1259            }
1260
1261            OpusMode::Hybrid => {
1262                let internal_sample_rate = 16000;
1263                let internal_frame_size =
1264                    (frame_duration_ms * internal_sample_rate / 1000) as usize;
1265                let celt_end_band = self.celt_end_band_from_toc(toc);
1266
1267                if self.sampling_rate != internal_sample_rate
1268                    && internal_sample_rate != self.prev_internal_rate
1269                {
1270                    self.silk_resampler
1271                        .init(internal_sample_rate, self.sampling_rate);
1272                    self.silk_resampler_2
1273                        .init(internal_sample_rate, self.sampling_rate);
1274                }
1275                self.prev_internal_rate = internal_sample_rate;
1276
1277                for (fi, payload) in frame_payloads.iter().enumerate() {
1278                    let mut rc = RangeCoder::new_decoder(payload);
1279                    let pcm_silk_i16_len = internal_frame_size * self.channels;
1280                    debug_assert!(pcm_silk_i16_len <= self.w_pcm_i16.len());
1281
1282                    let ret = {
1283                        let (silk_dec, pcm_i16) = (&mut self.silk_dec, &mut self.w_pcm_i16);
1284                        let lost_flag = if lost_frame {
1285                            silk::decode_frame::FLAG_PACKET_LOST
1286                        } else {
1287                            silk::decode_frame::FLAG_DECODE_NORMAL
1288                        };
1289                        silk_dec.decode(
1290                            &mut rc,
1291                            &mut pcm_i16[..pcm_silk_i16_len],
1292                            lost_flag,
1293                            true,
1294                            frame_duration_ms,
1295                            internal_sample_rate,
1296                        )
1297                    };
1298
1299                    if ret < 0 {
1300                        return Err("SILK decoding failed");
1301                    }
1302
1303                    let silk_out_len = sub_frame_size * self.channels;
1304                    self.w_silk_out[..silk_out_len].fill(0.0);
1305                    if ret > 0 {
1306                        let decoded_samples = ret as usize;
1307                        // SILK decoder outputs planar: ch0 at [0..fl], ch1 at [fl..2*fl].
1308                        if self.sampling_rate == internal_sample_rate {
1309                            let frames = decoded_samples.min(sub_frame_size);
1310                            for i in 0..frames {
1311                                for ch in 0..self.channels {
1312                                    let src = if ch == 0 { i } else { internal_frame_size + i };
1313                                    let v = self.w_pcm_i16[src] as f32 / 32768.0;
1314                                    let idx = i * self.channels + ch;
1315                                    if idx < silk_out_len {
1316                                        self.w_silk_out[idx] = v;
1317                                    }
1318                                }
1319                            }
1320                        } else {
1321                            let ratio = self.sampling_rate as f64 / internal_sample_rate as f64;
1322                            let out_len =
1323                                ((decoded_samples as f64 * ratio) as usize).min(sub_frame_size);
1324                            debug_assert!(out_len * self.channels <= self.w_pcm_resampled.len());
1325                            // Resample channel 0.
1326                            {
1327                                let (res, inp, out) = (
1328                                    &mut self.silk_resampler,
1329                                    &self.w_pcm_i16,
1330                                    &mut self.w_pcm_resampled,
1331                                );
1332                                res.process(
1333                                    &mut out[..out_len],
1334                                    &inp[..decoded_samples],
1335                                    decoded_samples as i32,
1336                                );
1337                            }
1338                            // Resample channel 1 (stereo only).
1339                            if self.channels == 2 {
1340                                let (res, inp, out) = (
1341                                    &mut self.silk_resampler_2,
1342                                    &self.w_pcm_i16,
1343                                    &mut self.w_pcm_resampled,
1344                                );
1345                                res.process(
1346                                    &mut out[out_len..2 * out_len],
1347                                    &inp[internal_frame_size..internal_frame_size + decoded_samples],
1348                                    decoded_samples as i32,
1349                                );
1350                            }
1351                            let frames = out_len.min(sub_frame_size);
1352                            for i in 0..frames {
1353                                for ch in 0..self.channels {
1354                                    let v = self.w_pcm_resampled[ch * out_len + i] as f32 / 32768.0;
1355                                    let idx = i * self.channels + ch;
1356                                    if idx < silk_out_len {
1357                                        self.w_silk_out[idx] = v;
1358                                    }
1359                                }
1360                            }
1361                        }
1362                    }
1363
1364                    let total_bits = (payload.len() * 8) as i32;
1365                    let redundancy = rc.decode_bit_logp(12);
1366                    let skip_celt = if redundancy {
1367                        let _celt_to_silk = rc.decode_bit_logp(1);
1368                        has_redundancy = true;
1369                        // When redundancy is present, the redundant CELT frame
1370                        // provides the transition audio. We skip the main CELT
1371                        // decode for this sub-frame (the SILK output stands alone)
1372                        // — a simplified version of libopus's behaviour where the
1373                        // redundant frame is decoded separately and crossfaded.
1374                        true
1375                    } else {
1376                        false
1377                    };
1378
1379                    if skip_celt {
1380                        self.w_celt_out[..silk_out_len].fill(0.0);
1381                    } else {
1382                        let (celt_dec, celt_planar) = (&mut self.celt_dec, &mut self.w_celt_planar);
1383                        celt_dec.decode_from_range_coder_with_band_range(
1384                            &mut rc,
1385                            total_bits,
1386                            sub_frame_size,
1387                            &mut celt_planar[..silk_out_len],
1388                            17,
1389                            celt_end_band,
1390                        );
1391
1392                        if self.channels == 1 {
1393                            self.w_celt_out[..silk_out_len]
1394                                .copy_from_slice(&self.w_celt_planar[..silk_out_len]);
1395                        } else {
1396                            for i in 0..sub_frame_size {
1397                                for ch in 0..self.channels {
1398                                    self.w_celt_out[i * self.channels + ch] =
1399                                        self.w_celt_planar[ch * sub_frame_size + i];
1400                                }
1401                            }
1402                        }
1403                    }
1404
1405                    let out_start = fi * silk_out_len;
1406                    let total = silk_out_len.min(output.len() - out_start);
1407                    for j in 0..total {
1408                        output[out_start + j] =
1409                            (self.w_silk_out[j] + self.w_celt_out[j]).clamp(-1.0, 1.0);
1410                    }
1411                }
1412                decoded_total
1413            }
1414        };
1415
1416        // Apply PLC-style bridging at mode transitions (libopus
1417        // opus_decoder.c:660-679). The first F2_5 of the output is replaced
1418        // with the previous frame's tail (PLC bridge), and the next F2_5 is
1419        // crossfaded between the bridge and the new frame's CELT output.
1420        // F5 = Fs/200, F2_5 = Fs/400.
1421        let f2_5 = self.sampling_rate as usize / 400;
1422        let f5 = f2_5 * 2;
1423        if mode_transition && f5 > 0 && decoded_total >= f5 {
1424            let window = modes::default_mode().window;
1425            let inc = (48000 / self.sampling_rate) as usize;
1426            let f2_5_ch = f2_5 * self.channels;
1427            let f5_ch = f5 * self.channels;
1428            // First F2_5: pure bridging audio from previous frame's tail.
1429            output[..f2_5_ch].copy_from_slice(&self.prev_pcm_tail[..f2_5_ch]);
1430            // Next F2_5: crossfade bridge → new CELT output.
1431            let new_mid: FixedVec<f32, OPUS_PCM_TAIL> = FixedVec::from_slice(&output[f2_5_ch..f5_ch]);
1432            smooth_fade(
1433                &self.prev_pcm_tail[f2_5_ch..f5_ch],
1434                &new_mid,
1435                &mut output[f2_5_ch..f5_ch],
1436                f2_5,
1437                self.channels,
1438                window,
1439                inc,
1440            );
1441        }
1442
1443        // Save the tail of this frame for the next transition (F5 samples).
1444        let tail_len = f5 * self.channels;
1445        let out_total = decoded_total * self.channels;
1446        if out_total >= tail_len && tail_len <= self.prev_pcm_tail.len() {
1447            self.prev_pcm_tail[..tail_len]
1448                .copy_from_slice(&output[out_total - tail_len..out_total]);
1449        }
1450
1451        self.prev_mode = Some(mode);
1452        self.prev_redundancy = has_redundancy;
1453        Ok(decoded_total)
1454    }
1455}
1456
1457impl OpusDecoder {
1458    #[inline(always)]
1459    fn celt_end_band_from_toc(&self, toc: u8) -> usize {
1460        let mode = modes::default_mode();
1461        let top = mode.eff_ebands;
1462        if mode_from_toc(toc) == OpusMode::CeltOnly && toc >= 0x80 {
1463            const FROM_OPUS_TABLE: [u8; 16] = [
1464                0x80, 0x88, 0x90, 0x98, 0x40, 0x48, 0x50, 0x58, 0x20, 0x28, 0x30, 0x38, 0x00, 0x08,
1465                0x10, 0x18,
1466            ];
1467            let idx = ((toc >> 3) - 16) as usize;
1468            let data0 = FROM_OPUS_TABLE[idx] | (toc & 0x7);
1469            let trim = (data0 >> 5) as usize;
1470            return top.saturating_sub(2 * trim).max(1);
1471        }
1472        top
1473    }
1474}
1475
1476fn frame_rate_from_params(sampling_rate: i32, frame_size: usize) -> Option<i32> {
1477    let frame_size = frame_size as i32;
1478    if frame_size == 0 || sampling_rate % frame_size != 0 {
1479        return None;
1480    }
1481    Some(sampling_rate / frame_size)
1482}
1483
1484fn gen_toc(mode: OpusMode, frame_rate: i32, bandwidth: Bandwidth, channels: usize) -> u8 {
1485    let mut rate = frame_rate;
1486    let mut period = 0;
1487    while rate < 400 {
1488        rate <<= 1;
1489        period += 1;
1490    }
1491
1492    let mut toc = match mode {
1493        OpusMode::SilkOnly => {
1494            let bw = (bandwidth as i32 - Bandwidth::Narrowband as i32) << 5;
1495            let per = (period - 2) << 3;
1496            (bw | per) as u8
1497        }
1498        OpusMode::CeltOnly => {
1499            let mut tmp = bandwidth as i32 - Bandwidth::Mediumband as i32;
1500            if tmp < 0 {
1501                tmp = 0;
1502            }
1503            let per = period << 3;
1504            (0x80 | (tmp << 5) | per) as u8
1505        }
1506        OpusMode::Hybrid => {
1507            let base_config = if bandwidth == Bandwidth::Superwideband {
1508                12
1509            } else {
1510                14
1511            };
1512            let period_offset = if frame_rate >= 100 { 0 } else { 1 };
1513            ((base_config + period_offset) << 3) as u8
1514        }
1515    };
1516
1517    if channels == 2 {
1518        toc |= 0x04;
1519    }
1520    toc
1521}
1522
1523fn mode_from_toc(toc: u8) -> OpusMode {
1524    if toc & 0x80 != 0 {
1525        OpusMode::CeltOnly
1526    } else if toc & 0x60 == 0x60 {
1527        OpusMode::Hybrid
1528    } else {
1529        OpusMode::SilkOnly
1530    }
1531}
1532
1533fn bandwidth_from_toc(toc: u8) -> Bandwidth {
1534    let mode = mode_from_toc(toc);
1535    match mode {
1536        OpusMode::SilkOnly => {
1537            let bw_bits = (toc >> 5) & 0x03;
1538            match bw_bits {
1539                0 => Bandwidth::Narrowband,
1540                1 => Bandwidth::Mediumband,
1541                2 => Bandwidth::Wideband,
1542                _ => Bandwidth::Wideband,
1543            }
1544        }
1545        OpusMode::Hybrid => {
1546            let bw_bit = (toc >> 4) & 0x01;
1547            if bw_bit == 0 {
1548                Bandwidth::Superwideband
1549            } else {
1550                Bandwidth::Fullband
1551            }
1552        }
1553        OpusMode::CeltOnly => {
1554            let bw_bits = (toc >> 5) & 0x03;
1555            match bw_bits {
1556                0 => Bandwidth::Mediumband,
1557                1 => Bandwidth::Wideband,
1558                2 => Bandwidth::Superwideband,
1559                3 => Bandwidth::Fullband,
1560                _ => Bandwidth::Fullband,
1561            }
1562        }
1563    }
1564}
1565
1566fn frame_duration_ms_from_toc(toc: u8) -> i32 {
1567    let mode = mode_from_toc(toc);
1568    match mode {
1569        OpusMode::SilkOnly => {
1570            let config = (toc >> 3) & 0x03;
1571            match config {
1572                0 => 10,
1573                1 => 20,
1574                2 => 40,
1575                3 => 60,
1576                _ => 20,
1577            }
1578        }
1579        OpusMode::Hybrid => {
1580            let config = (toc >> 3) & 0x01;
1581            if config == 0 { 10 } else { 20 }
1582        }
1583        OpusMode::CeltOnly => {
1584            let config = (toc >> 3) & 0x03;
1585            match config {
1586                0 => 2,
1587                1 => 5,
1588                2 => 10,
1589                3 => 20,
1590                _ => 20,
1591            }
1592        }
1593    }
1594}
1595
1596/// Compute the per-frame sample count implied by the TOC byte at a given
1597/// sampling rate. For CELT this uses the frame-rate derivation (which handles
1598/// the 2.5 ms case correctly, unlike integer millisecond arithmetic).
1599fn frame_samples_from_toc(toc: u8, sampling_rate: i32) -> Option<usize> {
1600    let mode = mode_from_toc(toc);
1601    match mode {
1602        OpusMode::CeltOnly => {
1603            let period = ((toc >> 3) & 0x03) as i32;
1604            let frame_rate = 400 >> period;
1605            if frame_rate == 0 || sampling_rate % frame_rate != 0 {
1606                return None;
1607            }
1608            Some((sampling_rate / frame_rate) as usize)
1609        }
1610        OpusMode::SilkOnly | OpusMode::Hybrid => {
1611            let duration_ms = frame_duration_ms_from_toc(toc);
1612            Some((sampling_rate as i64 * duration_ms as i64 / 1000) as usize)
1613        }
1614    }
1615}
1616
1617fn channels_from_toc(toc: u8) -> usize {
1618    if toc & 0x04 != 0 { 2 } else { 1 }
1619}
1620
1621/// Crossfade two signals using a squared-sine window (libopus smooth_fade).
1622/// `window` is the 120-sample CELT window at 48 kHz; `inc` = 48000/Fs strides it.
1623fn smooth_fade(
1624    in1: &[f32],
1625    in2: &[f32],
1626    out: &mut [f32],
1627    overlap: usize,
1628    channels: usize,
1629    window: &[f32],
1630    inc: usize,
1631) {
1632    for c in 0..channels {
1633        for i in 0..overlap {
1634            let wi = i * inc;
1635            if wi >= window.len() {
1636                break;
1637            }
1638            let w = window[wi] * window[wi];
1639            out[i * channels + c] = w * in2[i * channels + c] + (1.0 - w) * in1[i * channels + c];
1640        }
1641    }
1642}
1643
1644/// Parse an Opus frame length per RFC 6716 §3.2.1, identical to libopus
1645/// `parse_size()`:
1646///   - `0`: no frame (DTX / lost packet)
1647///   - `1..=251`: length of the frame in bytes (one byte consumed)
1648///   - `252..=255`: a second byte is read; length = `second*4 + first`
1649///
1650/// Returns `(length, bytes_consumed)`.
1651fn parse_frame_size(data: &[u8]) -> Result<(usize, usize), &'static str> {
1652    let first = *data.first().ok_or("truncated frame length")? as usize;
1653    if first < 252 {
1654        Ok((first, 1))
1655    } else {
1656        let second = *data.get(1).ok_or("truncated frame length")? as usize;
1657        Ok((second * 4 + first, 2))
1658    }
1659}
1660
1661#[cfg(all(test, feature = "std"))]
1662mod tests {
1663    use super::*;
1664
1665    fn frame_size_from_toc(toc: u8, sampling_rate: i32) -> Option<usize> {
1666        let mode = mode_from_toc(toc);
1667        match mode {
1668            OpusMode::CeltOnly => {
1669                let period = ((toc >> 3) & 0x03) as i32;
1670                let frame_rate = 400 >> period;
1671                if frame_rate == 0 || sampling_rate % frame_rate != 0 {
1672                    return None;
1673                }
1674                Some((sampling_rate / frame_rate) as usize)
1675            }
1676            OpusMode::SilkOnly => {
1677                let duration_ms = frame_duration_ms_from_toc(toc);
1678                Some((sampling_rate as i64 * duration_ms as i64 / 1000) as usize)
1679            }
1680            OpusMode::Hybrid => {
1681                let duration_ms = frame_duration_ms_from_toc(toc);
1682                Some((sampling_rate as i64 * duration_ms as i64 / 1000) as usize)
1683            }
1684        }
1685    }
1686
1687    #[test]
1688    fn gen_toc_matches_celt_reference_values() {
1689        let sampling_rate = 48_000;
1690        let cases = [
1691            (120usize, 0xE0u8),
1692            (240usize, 0xE8u8),
1693            (480usize, 0xF0u8),
1694            (960usize, 0xF8u8),
1695        ];
1696
1697        for (frame_size, expected_toc) in cases {
1698            let frame_rate = frame_rate_from_params(sampling_rate, frame_size).unwrap();
1699            let toc = gen_toc(OpusMode::CeltOnly, frame_rate, Bandwidth::Fullband, 1);
1700            assert_eq!(
1701                toc, expected_toc,
1702                "frame_size {} expected TOC {:02X} got {:02X}",
1703                frame_size, expected_toc, toc
1704            );
1705            let decoded_size = frame_size_from_toc(toc, sampling_rate).unwrap();
1706            assert_eq!(decoded_size, frame_size);
1707        }
1708
1709        let stereo_toc = gen_toc(
1710            OpusMode::CeltOnly,
1711            frame_rate_from_params(sampling_rate, 960).unwrap(),
1712            Bandwidth::Fullband,
1713            2,
1714        );
1715        assert_eq!(channels_from_toc(stereo_toc), 2);
1716    }
1717
1718    #[test]
1719    fn test_celt_decoder_large_frame_sizes() {
1720        let sampling_rate = 48000;
1721        let channels = 1;
1722
1723        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1724
1725        let frame_sizes = [120, 240, 480, 960];
1726
1727        for frame_size in frame_sizes {
1728            let toc = gen_toc(
1729                OpusMode::CeltOnly,
1730                frame_rate_from_params(sampling_rate, frame_size).unwrap(),
1731                Bandwidth::Fullband,
1732                channels,
1733            );
1734            let packet = [toc, 0, 0, 0, 0];
1735
1736            let mut output = vec![0.0f32; frame_size * channels];
1737
1738            let _ = decoder.decode(&packet, frame_size, &mut output);
1739        }
1740
1741        let channels = 2;
1742        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1743
1744        for frame_size in frame_sizes {
1745            let toc = gen_toc(
1746                OpusMode::CeltOnly,
1747                frame_rate_from_params(sampling_rate, frame_size).unwrap(),
1748                Bandwidth::Fullband,
1749                channels,
1750            );
1751            let packet = [toc, 0, 0, 0, 0];
1752
1753            let mut output = vec![0.0f32; frame_size * channels];
1754            let _ = decoder.decode(&packet, frame_size, &mut output);
1755        }
1756    }
1757
1758    #[test]
1759    fn test_celt_decoder_edge_case_frame_sizes() {
1760        let sampling_rate = 48000;
1761        let channels = 1;
1762        let mut decoder = OpusDecoder::new(sampling_rate, channels).unwrap();
1763
1764        let edge_sizes = [2048, 2167, 2168, 2169, 2880, 3072];
1765
1766        for frame_size in edge_sizes {
1767            let mut output = vec![0.0f32; frame_size * channels];
1768
1769            let _ = decoder.decode(&[0x80, 0, 0, 0], frame_size, &mut output);
1770        }
1771    }
1772
1773    // Regression test for: "index out of bounds: the len is 48 but the index is 119"
1774    // Root cause: frame_size=48 at 48kHz gives frame_rate=1000, which is not a valid
1775    // Hybrid-mode frame rate but was not validated.  CELT's lm-search then silently
1776    // fell back to lm=0, computed n2=120, and wrote output[119] into a 48-element
1777    // slice.  Triggered via G.729-decoded PCM (8kHz) passed to a 48kHz Opus encoder
1778    // without proper resampling, so the encoder received 48 samples instead of 480.
1779    #[test]
1780    fn test_invalid_small_frame_size_returns_error_not_panic() {
1781        let mut enc = OpusEncoder::new(48000, 2, Application::Voip).unwrap();
1782        enc.bitrate_bps = 64000;
1783        enc.complexity = 5;
1784        enc.use_cbr = true;
1785
1786        // 48 samples at 48kHz = 1ms → frame_rate=1000, invalid for Hybrid mode.
1787        let input = vec![0.0f32; 48 * 2]; // stereo interleaved
1788        let mut output = vec![0u8; 256];
1789
1790        let result = enc.encode(&input, 48, &mut output);
1791        assert!(
1792            result.is_err(),
1793            "encode with invalid frame_size=48 should return Err, not panic"
1794        );
1795    }
1796
1797    // Also verify that the Audio application path (always Hybrid at 48 kHz) rejects
1798    // the same bad frame size.
1799    #[test]
1800    fn test_invalid_small_frame_size_audio_application_returns_error() {
1801        let mut enc = OpusEncoder::new(48000, 1, Application::Audio).unwrap();
1802        let input = vec![0.0f32; 48];
1803        let mut output = vec![0u8; 256];
1804
1805        let result = enc.encode(&input, 48, &mut output);
1806        assert!(
1807            result.is_err(),
1808            "Audio/48kHz encoder with frame_size=48 should return Err"
1809        );
1810    }
1811}