diff --git a/crates/ec-opus/src/lib.rs b/crates/ec-opus/src/lib.rs index 08662f2d..b7d8d226 100644 --- a/crates/ec-opus/src/lib.rs +++ b/crates/ec-opus/src/lib.rs @@ -8,17 +8,24 @@ //! //! Contracts worth knowing before implementing against this crate: //! -//! - **The MDCT is the shared one.** CELT synthesis runs through -//! [`ec_dsp::Mdct`], never a direct-form DFT. An incumbent Opus decoder in +//! - **The FFT is never optional.** CELT synthesis runs an `N/4`-point FFT +//! through [`ec_dsp`], never a direct-form DFT. An incumbent Opus decoder in //! this product shipped its FFT behind a default-off feature and ran at 0.95x //! realtime on 5.1; there is no such switch here, and no code path that could -//! answer to one. -//! - **Malformed input is an error, never a panic.** Framing rules [R1]..[R7] +//! answer to one. (CELT's transform sizes are `15*2^k`, so [`ec_dsp::Mdct`] +//! itself — power-of-two only — cannot serve them; [`celt`] wraps +//! [`ec_dsp::Fft`] with one radix-3/radix-5 stage instead.) +//! - **Malformed input is an error, never a panic.** Framing rules `[R1]`..`[R7]` //! are checked in [`packet`], and the range decoder feeds zeros past the end //! of a truncated frame exactly as Section 4.1.2.1 requires. //! - **Output is `f32`, interleaved, at the rate you asked for.** 48, 24, 16, -//! 12 and 8 kHz are supported; SILK's internal rate is resampled up and the -//! sum of the two layers is resampled down once, at the end. +//! 12 and 8 kHz are supported; SILK's internal rate is resampled up (by the +//! normative resampler in [`silk`]) and CELT decimates on the way out. +//! - **There is no packet loss concealment.** A frame of one byte or none — +//! a dropped or DTX frame — decodes to silence, where the reference +//! extrapolates from the previous frame. This decoder is fed whole packets by +//! a container, never a gap, so the concealment would be dead weight; a +//! caller that needs it (an RTP receiver) has to add it. //! //! No unsafe, no allocation on the per-frame path beyond the decoder's own //! buffers, no dependencies outside the family. diff --git a/crates/ec-opus/src/packet.rs b/crates/ec-opus/src/packet.rs index f63e2881..0652aba6 100644 --- a/crates/ec-opus/src/packet.rs +++ b/crates/ec-opus/src/packet.rs @@ -1,7 +1,7 @@ //! Opus packet framing: the TOC byte, codes 0-3, and the self-delimiting //! variant (RFC 6716, Section 3 and Appendix B). //! -//! Every rule marked [R1]..[R7] in Section 3.4 is enforced here and reported as +//! Every rule marked `[R1]`..`[R7]` in Section 3.4 is enforced here and reported as //! [`Error::Corrupt`] — a malformed packet is rejected at the framing layer, so //! nothing downstream ever sees a frame it cannot account for. //! diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index 01eec6d1..b67f7b92 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -723,3 +723,48 @@ fn real_library_sweep() { ); } } + +/// Malformed input must come back as an error, never a panic: the decoders +/// index tables with values the bitstream chooses, so this throws well-formed +/// TOC bytes at random payloads to make sure every mode is actually entered. +#[test] +fn garbage_payloads_never_panic() { + let mut state = 0x12345678u32; + let mut rand = move || { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + state + }; + let mut dec = Decoder::new(48000, 2).unwrap(); + let mut ms = MultistreamDecoder::with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]); + let mut out = vec![0.0f32; 5760 * 6]; + for config in 0..32u8 { + for stereo in 0..2u8 { + for code in 0..4u8 { + let toc = (config << 3) | (stereo << 2) | code; + for trial in 0..40 { + let len = 2 + (rand() as usize % 400); + let mut packet = vec![toc]; + for _ in 0..len { + packet.push(rand() as u8); + } + if trial % 2 == 0 { + dec.reset(); + } + let _ = dec.decode_float(&packet, &mut out); + let _ = ms.decode_float(&packet, &mut out); + } + } + } + } + // Truncations of a real packet, which exercise the mid-frame paths. + let path = vectors_dir().join("testvector10.bit"); + if path.exists() { + for p in read_vector(&path).iter().take(200) { + for cut in 1..p.payload.len().min(64) { + let _ = dec.decode_float(&p.payload[..cut], &mut out); + } + } + } +}