Expand description
A complete, pure-Rust implementation of the Opus audio codec (RFC 6716 / RFC 8251): encoder and decoder, SILK, CELT and Hybrid modes, with no C code and no dependencies.
§Quick start
use rusty_opus::{Application, Error, OpusDecoder, OpusEncoder};
const RATE: i32 = 48_000;
const FRAME: usize = 960; // 20 ms at 48 kHz
let mut encoder = OpusEncoder::new(RATE, 2, Application::Audio)?;
encoder.bitrate_bps = 96_000;
let pcm = vec![0.0f32; FRAME * 2]; // interleaved stereo, nominal range [-1, 1]
let mut packet = [0u8; 1500];
let len = encoder.encode(&pcm, FRAME, &mut packet)?;
let mut decoder = OpusDecoder::new(RATE, 2)?;
let mut out = vec![0.0f32; FRAME * 2];
assert_eq!(decoder.decode(&packet[..len], FRAME, &mut out)?, FRAME);
// An empty packet signals a lost frame: the decoder conceals it.
assert_eq!(decoder.decode(&[], FRAME, &mut out)?, FRAME);
// Malformed input is an error, never a panic.
assert!(decoder.decode(&[0xFF, 0x03], FRAME, &mut out).is_err());§Public API
OpusEncoderandOpusDecoder— mono/stereo coding, packet-loss concealment (OpusDecoder::decodewith an empty packet) and in-band FEC (OpusDecoder::decode_fec).multistream— surround encoding and decoding (mapping families 0 and 1).repacketizer— merge, split, pad and unpad packets without re-encoding.parallel— frame-parallel and batch encoding across threads.Error— the error type of every fallible operation; variants mirrorlibopuserror codes.
Other modules are internal codec stages: they are public so integration tests can reach them, but hidden from this documentation and not covered by semantic versioning.
§Performance and platforms
SIMD kernels (AVX2/FMA, AVX and SSE2 on x86; NEON on aarch64) are selected at runtime,
each with a scalar fallback; RUSTY_OPUS_ISA=scalar|sse2|avx|avx2 caps the instruction
set. After warm-up, OpusEncoder::encode and OpusDecoder::decode perform no heap
allocation per frame. The crate builds
for every Rust target, including wasm32.
Modules§
- multistream
- Opus multistream (surround) — port of the core of
src/opus_multistream_{encoder,decoder}.c. Wraps N mono/coupled Opus coders behind a channel-mapping layout so >2-channel audio (quad, 5.1, 7.1) can be coded as a set of standard Opus streams concatenated with the self-delimited framing. - parallel
- Frame/chunk-parallel Opus encoding (R1) — the structural win that beats a single-threaded libopus on wall-clock.
- repacketizer
- Port of libopus
src/repacketizer.c+ the packet helpers fromsrc/opus.c: split Opus packets into frames and recombine/re-frame/pad them WITHOUT re-encoding. Used to merge several packets into a longer one, split a multi-frame packet, or pad a packet to a target size (e.g. for CBR transport). All frames in a repacketizer must share the same TOC config (mode/bandwidth/frame-size); only the code (0..3) and framing change.
Macros§
Structs§
- Opus
Decoder - An Opus decoder for one mono or stereo stream.
- Opus
Encoder - An Opus encoder for one mono or stereo stream.
Enums§
- Application
- The intended use of an encoder, which steers its mode and tuning decisions
(libopus
OPUS_APPLICATION_*; the discriminants are the libopus values). - Bandwidth
- Audio bandwidth of a coded stream (libopus
OPUS_BANDWIDTH_*; the discriminants are the libopus values). - Error
- Why an encoder, decoder, or packet operation failed.
- Signal
Type - OPUS_SET_SIGNAL hint: bias mode selection toward speech or music.
None= OPUS_AUTO (let the analysis decide).