oxideav-opus 0.0.13

Opus audio codec — orphan-rebuild scaffold pending clean-room re-implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! SILK frame synthesis composition — RFC 6716 §4.2.7.9.
//!
//! This module is the composition seam between the bitstream-consuming
//! front half of the SILK decode (everything up to and including the
//! [`crate::silk_decode::SilkFrameDecoded`] parameter set) and the
//! signal-reconstructing back half. It takes one fully decoded regular
//! SILK frame plus the cross-frame synthesis state and produces the
//! frame's time-domain output samples at the **internal SILK rate**
//! (8 kHz NB / 12 kHz MB / 16 kHz WB), running, subframe by subframe in
//! source order:
//!
//! 1. §4.2.7.9.1 LTP synthesis ([`crate::silk_ltp_synth`]) — for voiced
//!    frames the excitation is passed through the rewhiten + LTP
//!    convolution; for unvoiced frames it is the normalized excitation
//!    copy.
//! 2. §4.2.7.9.2 LPC synthesis ([`crate::silk_lpc_synth`]) — the LPC
//!    residual `res[]` is run through the short-term predictor producing
//!    the clamped `out[]` samples and the unclamped `lpc[]` values.
//! 3. The §4.2.7.9.2 state carry: the unclamped `lpc[]` feeds the next
//!    subframe's LPC filter and the LTP rewhitening's region-B path; the
//!    clamped `out[]` feeds the LTP rewhitening's region-A path.
//!
//! ## Per-subframe LPC selection (§4.2.7.9)
//!
//! The RFC §4.2.7.9 preamble says: "If this is the first or second
//! subframe of a 20 ms SILK frame and the LSF interpolation factor,
//! w_Q2 …, is less than 4, then [a_Q12] correspond to the final LPC
//! coefficients produced … from the interpolated LSF coefficients,
//! n1_Q15[k] …. Otherwise, they correspond to the final LPC
//! coefficients produced from the uninterpolated LSF coefficients for
//! the current frame, n2_Q15[k]."
//!
//! [`SilkFrameDecoded`] carries both: `lpc_first_half` is the n1-derived
//! filter (present only for a 20 ms frame with an interpolation split),
//! and `lpc_second_half` is the n2-derived filter (always present). This
//! module maps subframes 0 / 1 of an interpolation-split 20 ms frame to
//! `lpc_first_half` and every other subframe to `lpc_second_half`.
//!
//! The companion §4.2.7.9.1 LSF-interpolation-split flag for subframes
//! 2 / 3 (`lsf_interp_used`, which switches the LTP rewhitening to the
//! `out_end = j - (s-2)*n`, `LTP_scale_Q14 = 16384` branch) fires under
//! the same condition — a 20 ms frame whose decoded `w_Q2 < 4`.
//!
//! ## Cross-frame state
//!
//! The §4.2.7.9.1 LTP `out[]` / `lpc[]` histories and the §4.2.7.9.2 LPC
//! history persist across SILK frames (and across Opus frames within a
//! packet); they are cleared only on a §4.5.2 decoder reset or after an
//! uncoded regular SILK frame for the side channel. [`SilkSynthState`]
//! holds both and is threaded by the caller across the SILK frames of an
//! Opus frame.
//!
//! All truth is taken from RFC 6716 §4.2.7.9. No external library source
//! is consulted.

use crate::silk_decode::SilkFrameDecoded;
use crate::silk_excitation::SilkFrameSize;
use crate::silk_frame::SignalType;
use crate::silk_gains::SILK_MAX_SUBFRAMES;
use crate::silk_lpc_synth::{lpc_synthesis_subframe, subframe_samples, LpcSynthState};
use crate::silk_ltp::LTP_FILTER_TAPS;
use crate::silk_ltp_synth::{
    ltp_synth_commit_subframe, ltp_synthesis_subframe, LtpSynthState, LtpSynthSubframe,
};
use crate::toc::Bandwidth;
use crate::Error;

/// Cross-frame SILK synthesis state for one channel: the §4.2.7.9.1 LTP
/// histories (`out[]` clamped + `lpc[]` unclamped) and the §4.2.7.9.2 LPC
/// synthesis history.
///
/// Both are zero on construction and after [`Self::reset`] (the §4.5.2
/// decoder-reset path / uncoded side-channel frame). They persist across
/// SILK frames within an Opus frame and across Opus frames within a
/// stream.
#[derive(Debug, Clone)]
pub struct SilkSynthState {
    bandwidth: Bandwidth,
    ltp: LtpSynthState,
    lpc: LpcSynthState,
}

impl SilkSynthState {
    /// Construct a fresh zero-initialised synthesis state for `bandwidth`.
    ///
    /// Rejects SWB / FB (the SILK layer never sees them after the §4.2.2
    /// hybrid split).
    pub fn new(bandwidth: Bandwidth) -> Result<Self, Error> {
        Ok(Self {
            bandwidth,
            ltp: LtpSynthState::new(bandwidth)?,
            lpc: LpcSynthState::new(bandwidth)?,
        })
    }

    /// Bandwidth this state was created for.
    pub fn bandwidth(&self) -> Bandwidth {
        self.bandwidth
    }

    /// Clear all histories (the §4.5.2 decoder reset / uncoded
    /// side-channel-frame path).
    pub fn reset(&mut self) {
        self.ltp.reset();
        self.lpc.reset();
    }

    /// Read-only access to the LTP synthesis state (for tests / callers
    /// that want to inspect history).
    pub fn ltp(&self) -> &LtpSynthState {
        &self.ltp
    }

    /// Read-only access to the LPC synthesis state.
    pub fn lpc(&self) -> &LpcSynthState {
        &self.lpc
    }
}

/// Convert a Q12 LPC coefficient slice (`&[i32]`, as produced by
/// [`crate::silk_lsf_to_lpc::LpcQ12::a_q12`]) into the `i16` slice the
/// §4.2.7.9 synthesis filters expect. The §4.2.7.5.8 prediction-gain
/// limiter guarantees the coefficients fit in `i16`; values are clamped
/// defensively so a malformed upstream value can never wrap.
fn a_q12_i16(a_q12: &[i32]) -> Vec<i16> {
    a_q12
        .iter()
        .map(|&c| c.clamp(i16::MIN as i32, i16::MAX as i32) as i16)
        .collect()
}

/// Synthesize one fully decoded regular SILK frame into time-domain
/// output samples at the internal SILK rate, threading the cross-frame
/// [`SilkSynthState`].
///
/// Returns the clamped `out[]` signal for the whole SILK frame
/// (`subframe_samples(bandwidth) * num_subframes` samples, nominal range
/// `[-1.0, 1.0]`). The caller resamples this internal-rate signal to the
/// decoder output rate (§4.2.9, non-normative).
///
/// `decoded` is the parameter set from
/// [`crate::silk_decode::decode_silk_frame`]; `state` carries the
/// §4.2.7.9 histories across SILK frames.
///
/// Errors:
///
/// * [`Error::MalformedPacket`] if `state.bandwidth()` disagrees with
///   `bandwidth`, if a per-subframe LPC filter has the wrong order, or if
///   any inner synthesis stage rejects.
pub fn synthesize_silk_frame(
    bandwidth: Bandwidth,
    frame_size: SilkFrameSize,
    decoded: &SilkFrameDecoded,
    state: &mut SilkSynthState,
) -> Result<Vec<f32>, Error> {
    if state.bandwidth != bandwidth {
        return Err(Error::MalformedPacket);
    }
    let n = subframe_samples(bandwidth)?;
    let num_subframes = match frame_size {
        SilkFrameSize::TenMs => 2usize,
        SilkFrameSize::TwentyMs => 4usize,
    };

    // §4.2.7.4: dequantise the per-subframe log-gain to Q16.
    let gains_q16 = decoded.gains.dequant_q16();
    if decoded.gains.len() != num_subframes {
        return Err(Error::MalformedPacket);
    }

    // §4.2.7.9: the LSF-interpolation split fires for a 20 ms frame whose
    // decoded w_Q2 < 4 (then `lpc_first_half` is Some). When it does,
    // subframes 0/1 use the interpolated (n1) LPC and subframes 2/3 use
    // the uninterpolated (n2) LPC with the §4.2.7.9.1 split rewhitening.
    let interp_split =
        matches!(frame_size, SilkFrameSize::TwentyMs) && decoded.lpc_first_half.is_some();

    // The excitation for the whole SILK frame (Q23), partitioned into
    // per-subframe windows below. §4.2.7.8: a 10 ms MB frame codes 8
    // shell blocks (128 samples) of which only the first 120 are used
    // — the parsed excitation may therefore be LONGER than the frame's
    // sample count, and the tail is discarded (a round-382 find: this
    // was previously rejected, making every 10 ms MB SILK packet fail
    // to synthesize).
    let e_q23 = decoded.excitation.e_q23();
    if e_q23.len() < n * num_subframes {
        return Err(Error::MalformedPacket);
    }
    let e_q23 = &e_q23[..n * num_subframes];

    // The voiced LTP parameters (pitch lags + 5-tap filters). Empty for
    // unvoiced frames; the LTP-synth path consults them only when voiced.
    let pitch_lags = decoded.ltp.pitch_lags();
    let filter_taps = decoded.ltp.filter_taps_q7();
    let ltp_scaling_q14 = decoded.ltp.ltp_scaling_q14();
    let is_voiced = decoded.signal_type == SignalType::Voiced;
    if is_voiced && (pitch_lags.len() != num_subframes || filter_taps.len() != num_subframes) {
        return Err(Error::MalformedPacket);
    }

    state.ltp.start_frame();

    let mut out = vec![0.0f32; n * num_subframes];

    for s in 0..num_subframes {
        // §4.2.7.9 per-subframe LPC selection.
        let a_q12 = if interp_split && s < 2 {
            // first / second subframe of an interpolation-split 20 ms
            // frame → interpolated (n1) filter.
            decoded
                .lpc_first_half
                .as_ref()
                .ok_or(Error::MalformedPacket)?
                .a_q12()
        } else {
            decoded.lpc_second_half.a_q12()
        };
        let a_i16 = a_q12_i16(a_q12);

        // §4.2.7.9.1 LTP synthesis → LPC residual `res[]`.
        let (pitch_lag, b_q7) = if is_voiced {
            (pitch_lags[s], filter_taps[s])
        } else {
            // Unvoiced: pitch_lag / b_q7 are unused but must be a valid
            // (positive) placeholder so the subframe config validates.
            (1i32, [0i8; LTP_FILTER_TAPS])
        };
        // §4.2.7.9.1: the "third/fourth subframe of a 20 ms frame with
        // w_Q2 < 4" split branch fires when interp_split && s >= 2.
        let lsf_interp_used = interp_split;

        let j = s * n;
        let e_sub = &e_q23[j..j + n];
        let mut res = vec![0.0f32; n];
        let cfg = LtpSynthSubframe {
            bandwidth,
            signal_type: decoded.signal_type,
            frame_size,
            subframe_index: s as u8,
            gain_q16: gains_q16[s],
            pitch_lag,
            b_q7,
            ltp_scaling_q14,
            a_q12: &a_i16,
            lsf_interp_used,
        };
        ltp_synthesis_subframe(&state.ltp, cfg, e_sub, &mut res)?;

        // §4.2.7.9.2 LPC synthesis → clamped out[] + unclamped lpc[].
        let mut out_sub = vec![0.0f32; n];
        let lpc_unclamped = lpc_synthesis_subframe(
            bandwidth,
            &mut state.lpc,
            &res,
            gains_q16[s],
            &a_i16,
            &mut out_sub,
        )?;

        // §4.2.7.9.2 state carry: commit the clamped out[] and unclamped
        // lpc[] into the LTP histories for the next subframe's rewhiten.
        ltp_synth_commit_subframe(&mut state.ltp, &out_sub, &lpc_unclamped)?;

        out[j..j + n].copy_from_slice(&out_sub);
    }

    Ok(out)
}

/// Synthesize a sequence of decoded SILK frames into one contiguous
/// internal-rate output buffer, threading `state` across the frames.
///
/// Convenience for an Opus frame that carries 2 or 3 SILK frames (40 / 60
/// ms). Each frame's samples are appended in order; `state` carries the
/// §4.2.7.9 histories across them.
pub fn synthesize_silk_frames(
    bandwidth: Bandwidth,
    frame_size: SilkFrameSize,
    decoded: &[SilkFrameDecoded],
    state: &mut SilkSynthState,
) -> Result<Vec<f32>, Error> {
    let n = subframe_samples(bandwidth)?;
    let per_frame = n * match frame_size {
        SilkFrameSize::TenMs => 2usize,
        SilkFrameSize::TwentyMs => 4usize,
    };
    let mut out = Vec::with_capacity(per_frame * decoded.len());
    for frame in decoded {
        let frame_out = synthesize_silk_frame(bandwidth, frame_size, frame, state)?;
        out.extend_from_slice(&frame_out);
    }
    Ok(out)
}

/// The internal-rate sample count one SILK frame of the given bandwidth ×
/// duration produces (a convenience mirror of
/// [`crate::silk_resampler::silk_frame_samples_internal`] expressed via
/// the synthesis subframe geometry).
pub fn silk_frame_internal_samples(
    bandwidth: Bandwidth,
    frame_size: SilkFrameSize,
) -> Result<usize, Error> {
    let n = subframe_samples(bandwidth)?;
    let num_subframes = match frame_size {
        SilkFrameSize::TenMs => 2usize,
        SilkFrameSize::TwentyMs => 4usize,
    };
    debug_assert!(num_subframes <= SILK_MAX_SUBFRAMES);
    Ok(n * num_subframes)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::range_decoder::RangeDecoder;
    use crate::silk_decode::{decode_silk_frame, SilkFrameConfig};

    fn fresh_cfg(bandwidth: Bandwidth, frame_size: SilkFrameSize, voiced: bool) -> SilkFrameConfig {
        SilkFrameConfig {
            bandwidth,
            frame_size,
            voice_active: voiced,
            first_subframe_independent: true,
            previous_log_gain: None,
            previous_primary_lag: None,
            ltp_scaling_present: true,
            lsf_interp_after_reset: true,
            previous_nlsf_q15: None,
            previous_nlsf_len: 0,
            stereo: None,
        }
    }

    /// Synthesis state constructor routes d_LPC and rejects SWB / FB.
    #[test]
    fn state_new_routes_and_rejects() {
        for bw in [Bandwidth::Nb, Bandwidth::Mb, Bandwidth::Wb] {
            let s = SilkSynthState::new(bw).unwrap();
            assert_eq!(s.bandwidth(), bw);
        }
        assert!(SilkSynthState::new(Bandwidth::Swb).is_err());
        assert!(SilkSynthState::new(Bandwidth::Fb).is_err());
    }

    /// `silk_frame_internal_samples` matches the §4.2.9 geometry.
    #[test]
    fn internal_sample_counts() {
        assert_eq!(
            silk_frame_internal_samples(Bandwidth::Nb, SilkFrameSize::TenMs).unwrap(),
            80
        );
        assert_eq!(
            silk_frame_internal_samples(Bandwidth::Nb, SilkFrameSize::TwentyMs).unwrap(),
            160
        );
        assert_eq!(
            silk_frame_internal_samples(Bandwidth::Mb, SilkFrameSize::TwentyMs).unwrap(),
            240
        );
        assert_eq!(
            silk_frame_internal_samples(Bandwidth::Wb, SilkFrameSize::TwentyMs).unwrap(),
            320
        );
        assert_eq!(
            silk_frame_internal_samples(Bandwidth::Wb, SilkFrameSize::TenMs).unwrap(),
            160
        );
    }

    /// Bandwidth mismatch between the synthesis state and the requested
    /// frame is rejected.
    #[test]
    fn rejects_bandwidth_mismatch() {
        let buf = [0x33u8; 96];
        let mut rd = RangeDecoder::new(&buf);
        let cfg = fresh_cfg(Bandwidth::Nb, SilkFrameSize::TwentyMs, false);
        if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
            let mut state = SilkSynthState::new(Bandwidth::Wb).unwrap();
            assert!(matches!(
                synthesize_silk_frame(Bandwidth::Nb, SilkFrameSize::TwentyMs, &decoded, &mut state),
                Err(Error::MalformedPacket)
            ));
        }
    }

    /// A real decoded frame synthesizes to the right number of samples and
    /// every sample is in nominal range / finite.
    #[test]
    fn synthesize_produces_in_range_samples() {
        let buf: Vec<u8> = (0..160u16)
            .map(|i| (i.wrapping_mul(67).wrapping_add(5) & 0xff) as u8)
            .collect();
        for (bw, expected) in [
            (Bandwidth::Nb, 160usize),
            (Bandwidth::Mb, 240),
            (Bandwidth::Wb, 320),
        ] {
            for voiced in [false, true] {
                let mut rd = RangeDecoder::new(&buf);
                let cfg = fresh_cfg(bw, SilkFrameSize::TwentyMs, voiced);
                if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
                    let mut state = SilkSynthState::new(bw).unwrap();
                    let out =
                        synthesize_silk_frame(bw, SilkFrameSize::TwentyMs, &decoded, &mut state)
                            .unwrap();
                    assert_eq!(out.len(), expected, "bw={bw:?} voiced={voiced}");
                    for (i, &v) in out.iter().enumerate() {
                        assert!(v.is_finite(), "non-finite at {i}: {v}");
                        assert!(
                            (-1.0..=1.0).contains(&v),
                            "out[{i}]={v} outside nominal range (bw={bw:?})"
                        );
                    }
                }
            }
        }
    }

    /// A 10 ms frame synthesizes to two subframes; no interpolation split.
    #[test]
    fn ten_ms_two_subframes() {
        let buf: Vec<u8> = (0..96u16)
            .map(|i| (i.wrapping_mul(43).wrapping_add(9) & 0xff) as u8)
            .collect();
        let mut rd = RangeDecoder::new(&buf);
        let cfg = fresh_cfg(Bandwidth::Wb, SilkFrameSize::TenMs, false);
        if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
            assert!(decoded.lpc_first_half.is_none());
            let mut state = SilkSynthState::new(Bandwidth::Wb).unwrap();
            let out =
                synthesize_silk_frame(Bandwidth::Wb, SilkFrameSize::TenMs, &decoded, &mut state)
                    .unwrap();
            assert_eq!(out.len(), 160); // 80 * 2
        }
    }

    /// Synthesis is deterministic: the same decoded frame + fresh state
    /// yields identical output.
    #[test]
    fn synthesis_is_deterministic() {
        let buf: Vec<u8> = (0..160u16)
            .map(|i| (i.wrapping_mul(89).wrapping_add(1) & 0xff) as u8)
            .collect();
        let mut rd = RangeDecoder::new(&buf);
        let cfg = fresh_cfg(Bandwidth::Nb, SilkFrameSize::TwentyMs, true);
        if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
            let mut s1 = SilkSynthState::new(Bandwidth::Nb).unwrap();
            let mut s2 = SilkSynthState::new(Bandwidth::Nb).unwrap();
            let o1 =
                synthesize_silk_frame(Bandwidth::Nb, SilkFrameSize::TwentyMs, &decoded, &mut s1)
                    .unwrap();
            let o2 =
                synthesize_silk_frame(Bandwidth::Nb, SilkFrameSize::TwentyMs, &decoded, &mut s2)
                    .unwrap();
            assert_eq!(o1, o2);
        }
    }

    /// The multi-frame helper concatenates per-frame outputs and threads
    /// state (the second frame's history is non-zero going in).
    #[test]
    fn multi_frame_concatenates() {
        let buf: Vec<u8> = (0..160u16)
            .map(|i| (i.wrapping_mul(31).wrapping_add(7) & 0xff) as u8)
            .collect();
        let mut rd = RangeDecoder::new(&buf);
        let cfg = fresh_cfg(Bandwidth::Nb, SilkFrameSize::TwentyMs, false);
        if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
            let frames = [decoded.clone(), decoded.clone()];
            let mut state = SilkSynthState::new(Bandwidth::Nb).unwrap();
            let out =
                synthesize_silk_frames(Bandwidth::Nb, SilkFrameSize::TwentyMs, &frames, &mut state)
                    .unwrap();
            assert_eq!(out.len(), 320); // 160 * 2
        }
    }

    /// Reset clears the carried histories.
    #[test]
    fn reset_clears_histories() {
        let buf: Vec<u8> = (0..160u16)
            .map(|i| (i.wrapping_mul(53).wrapping_add(2) & 0xff) as u8)
            .collect();
        let mut rd = RangeDecoder::new(&buf);
        let cfg = fresh_cfg(Bandwidth::Wb, SilkFrameSize::TwentyMs, true);
        if let Ok(decoded) = decode_silk_frame(&mut rd, cfg) {
            let mut state = SilkSynthState::new(Bandwidth::Wb).unwrap();
            synthesize_silk_frame(Bandwidth::Wb, SilkFrameSize::TwentyMs, &decoded, &mut state)
                .unwrap();
            // After a frame the LPC history may be non-zero; reset clears it.
            state.reset();
            assert!(state.lpc().history().iter().all(|&x| x == 0.0));
            assert!(state.ltp().out_history().iter().all(|&x| x == 0.0));
            assert!(state.ltp().lpc_history().iter().all(|&x| x == 0.0));
        }
    }
}