Skip to content

Remade-With-Rust/rusty-opus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

124 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rusty-opus

Remade With Rust License: BSD-3-Clause Pure Rust

A pure-Rust implementation of the Opus audio codec (RFC 6716 / RFC 8251) — no C, no FFI, no build-time toolchain. Encoder and decoder, SILK + CELT + Hybrid, conformance-verified against the reference, and fast enough to beat libopus on wall-clock. The only unsafe is in the SIMD kernels (AVX2 / NEON, runtime-detected, each with a scalar fallback that doubles as its correctness oracle).

rusty-opus is Mata Network's performance fork of opus-rs (BSD-3-Clause), maintained under the Remade With Rust initiative. It is the Opus backend of remade_ffmpeg_rs, and usable standalone. Every optimization is gated against a reference oracle — byte-identical where the bitstream must not move, PEAQ-validated where it may.

Why rusty-opus?

  • Pure Rust, no C — no libopus, no bindgen, no build-time toolchain surprises; trivial to cross-compile and embed. unsafe is confined to the SIMD kernels, each gated behind a runtime CPU-feature check and backed by a scalar fallback.
  • Conformance-verified — the decoder is bit-exact on all 12 official RFC 6716 / RFC 8251 test vectors (float-libopus parity to 1e-6) and decodes libopus's own streams to identical output.
  • Fast — hand-written AVX2/FMA (x86-64) and NEON (aarch64) kernels put single-thread encode at/above libopus on CELT, and frame-parallel encoding wins wall-clock outright (~3× libopus on speech, which is single-threaded per stream).
  • Complete — SILK, CELT, and Hybrid modes; VBR/CBR; DTX; in-band FEC; packet-loss concealment; comfort-noise generation; multistream / surround (5.1 / 7.1); a repacketizer.
  • Permissive — BSD-3-Clause, all the way down.

Add it to your project

[dependencies]
rusty-opus = { git = "https://github.com/Remade-With-Rust/rusty-opus" }

The crate is imported as rusty_opus:

use rusty_opus::{OpusEncoder, OpusDecoder, Application};

// --- Encode ---
let mut encoder = OpusEncoder::new(16_000, 1, Application::Voip).unwrap();
encoder.bitrate_bps = 16_000;
encoder.use_cbr = true;

let input = vec![0.0f32; 320];       // one 20 ms frame at 16 kHz, mono
let mut packet = vec![0u8; 4000];
let n = encoder.encode(&input, 320, &mut packet).unwrap();

// --- Decode ---
let mut decoder = OpusDecoder::new(16_000, 1).unwrap();
let mut pcm = vec![0.0f32; 320];
let samples = decoder.decode(&packet[..n], 320, &mut pcm).unwrap();

Runnable examples live in examples/ — WAV round-trip, packet-loss concealment, in-band FEC, multistream, and the opus_demo-compatible conformance harnesses:

cargo run --release --example roundtrip          # encode → decode a WAV
cargo run --release --example plc_test           # packet-loss concealment
cargo run --release --example roundtrip_parallel # frame-parallel encode
cargo test  --release                            # full suite incl. conformance vectors

Correctness

The reference C decoder is the oracle. Every brick is gated byte-identical against a scalar twin where the bitstream must not move (SIMD kernels, entropy paths, resampler), and PEAQ-ODG validated where it legitimately may (encoder analysis, block switching, quality tuning). The decoder passes all 12 official conformance vectors bit-exactly; the encoder round-trips through libopus and vice-versa with zero interop errors (including 5.1 / 7.1 multistream).

Quality vs libopus

Measured head-to-head on PEAQ ODG with a reconstruction-SNR guard. Mono (speech and music) sits at parity once bitrate-matched. Stereo music is at or near parity at higher rates and a modest, honest deficit at mid rates (an open item we're still closing — the decoder is exact, this is purely encoder bit-allocation tuning). Streaming robustness — PLC (SILK and CELT), FEC, DTX, CNG — is at parity with libopus, conformance untouched.

Performance

Single-thread encode, Criterion (cargo bench), real speech input, mono, wall-clock for the full frame set. rusty-opus adds byte-identical AVX2 SILK kernels (LPC short-prediction, warped-autocorrelation, cross-state NSQ shaping filter) plus AVX2 decode kernels (resampler FIR, CELT comb filter) on top of the pure-Rust base.

vs C libopus 1.6.1 — x86-64 (AVX2/FMA), AMD Ryzen 7 5700X

Config rusty-opus C libopus Ratio
8 kHz / 20 ms VoIP 39.9 ms 40.6 ms 0.98× (2 % faster)
16 kHz / 20 ms VoIP 66.8 ms 67.1 ms 1.00× (0.5 % faster)
16 kHz / 10 ms VoIP 73.2 ms 72.5 ms 1.01× (within noise)
48 kHz / 20 ms Audio 25.1 ms 28.4 ms 0.88× (12 % faster)
48 kHz / 10 ms Audio 29.7 ms 31.2 ms 0.95× (5 % faster)

vs C libopus 1.6.1 — Apple Silicon (aarch64, NEON)

Config rusty-opus C libopus Ratio
8 kHz / 20 ms VoIP 31.47 ms 31.20 ms 1.01× (C 0.9 % faster)
16 kHz / 20 ms VoIP 51.19 ms 52.81 ms 0.97× (3.1 % faster)
16 kHz / 10 ms VoIP 55.69 ms 55.49 ms 1.00× (within noise)
48 kHz / 20 ms Audio 13.97 ms 19.39 ms 0.72× (28 % faster)
48 kHz / 10 ms Audio 16.19 ms 20.28 ms 0.80× (20 % faster)

Where libopus's hand-written assembly still leads single-thread (the SILK NSQ inner loop), frame-parallel encoding (examples/roundtrip_parallel) wins the wall-clock race outright — each chunk primes its inter-frame state so the seams are PEAQ-neutral, and libopus is single-threaded per stream.

Feature flags

  • profile (dev-only, off by default) — a zero-cost-when-off stage profiler for optimization work; release builds are byte-identical with it off.

SIMD is always compiled and selected at runtime via CPU-feature detection, with a scalar fallback path — so the same binary runs on machines with or without AVX2/NEON.

License

BSD-3-Clause — see COPYING. Derived from opus-rs and, ultimately, the reference Opus implementation, both BSD-3-Clause.

About

Pure-Rust Opus codec (RFC 6716) — conformance-verified encoder + decoder, SILK/CELT/Hybrid, no C or FFI; AVX2/NEON kernels + frame-parallel encode beat libopus on wall-clock.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors