Skip to content

feat: Opus codec sketch — SILK + CELT modes#3

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/plan-extend-opus-codec
Draft

feat: Opus codec sketch — SILK + CELT modes#3
Copilot wants to merge 2 commits intomainfrom
copilot/plan-extend-opus-codec

Conversation

Copy link
Copy Markdown

Copilot AI commented May 3, 2026

Summary

Adds a structural Opus codec skeleton under src/codecs/opus/, gated on a new opus-codec feature flag (which depends on psychoacoustic). This follows the same structural pattern as the existing PerceptualCodec / StereoPerceptualCodec, and extends the work started for the MP3 direction.


Architecture

The implementation mirrors the real Opus (RFC 6716) two-codec design:

audio_samples::codecs::opus
├── lpc.rs       — Levinson–Durbin LPC analysis + analysis/synthesis filters
├── mode.rs      — OpusMode, OpusBandwidth, OpusConfig, detect_mode (SFM-based)
├── silk.rs      — SILK frame encode/decode: LPC residual + i16 quantisation
├── celt.rs      — CELT frame encode/decode: delegates to existing PerceptualCodec pipeline
├── stereo.rs    — OpusStereoCodec with M/S matrix coding (mirrors StereoPerceptualCodec)
├── codec.rs     — OpusCodec + OpusEncodedAudio (AudioCodec impl, frames SILK or CELT)
└── mod.rs       — re-exports

CELT reuse: the CELT path deliberately delegates to the existing analyse_signal_with_window_sizeallocate_bitsquantizedequantizereconstruct_signal pipeline, using BandLayout::celt (RFC 6716 band edges) instead of Bark/Mel.

SILK independence: the SILK path uses new lpc.rs primitives — compute_autocorrelation, levinson_durbin, lpc_residual, lpc_synthesis — that are pure time-domain and have no dependency on spectrograms.

Auto-mode detection: detect_mode computes the Spectral Flatness Measure (SFM) of a frame and chooses SILK for speech-like signals (high tonal structure, low SFM) and CELT for music/noise.


What lives in audio_samples_io

Bitstream packing (range coding, Ogg/RIFF containers), .opus file I/O, and LSF quantisation of LPC coefficients remain responsibilities of the IO crate. This module provides only the algorithmic core.


Quality notes

Mode Signal Observed SNR Floor
SILK sine/noise >40 dB >20 dB
CELT sine ~2.6 dB >1 dB
CELT white noise ~0.9 dB >−1 dB

CELT quality is bounded by the same MDCT window boundary effect that limits PerceptualCodec (the existing codec_quality tests also fail). Each Opus frame is encoded as a single analysis window, so edge samples are windowed to near-zero by the Hanning function. This is a known sketch limitation documented in mod.rs and the test file.


Changes

  • New: src/codecs/opus/ (7 files, ~900 lines)
  • Updated: src/codecs/mod.rs, src/lib.rs, Cargo.toml
  • New test: tests/opus_codec_quality.rs — 12 tests (12/12 pass)
  • Full lib suite: 265/265 pass (no regressions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants