Skip to main content

Module opus

Module opus 

Source
Expand description

Opus encoder wrapping audiopus (libopus FFI; libopus is BSD, audiopus is ISC). Squad-23’s MP4 mux side consumes the packets + extra_data() (dOps body per RFC 7845 §4.5) + pre_skip() (samples at 48 kHz queried via OPUS_GET_LOOKAHEAD).

Constraints we enforce on the Opus side:

  • Native sample rates are 8/12/16/24/48 kHz only. We always run the internal libopus encoder at 48 kHz and resample the input ourselves via AudioResampler when the source isn’t 48 k. This keeps pre_skip semantics simple (always reported in 48 kHz ticks per the RFC) and means the dOps InputSampleRate field cleanly reflects the original source rate.
  • Frame sizes must be 2.5/5/10/20/40/60 ms. We use 20 ms = 960 samples at 48 kHz. This is libopus’s default and matches what browsers / WebRTC expect.
  • Channels: 1 (mono) and 2 (stereo) use the regular audiopus::coder::Encoder API. 3..=8 channels (3.0 / quad / 5.0 / 5.1 / 6.1 / 7.1) use the libopus Multistream API via audiopus_sys FFI (Squad-28). Channel counts above 8 return AudioError::Unsupported — RFC 7845 §5.1.1.2 only specifies channel-mapping family 1 for 1..=8 channels.

§Defaults

  • 96 kbps for stereo, 64 kbps for mono if the caller passes 0. Multichannel: 64 kbps per uncoupled stream + 96 kbps per coupled stream (so 5.1 = 96 + 96 + 64 + 64 = 320 kbps total) — well above transparency for music/speech (Opus reaches transparency around 64 kbps stereo for music).
  • Application = Audio (vs Voip / LowDelay): tuned for fidelity over latency. Latency from a 20 ms frame size + ~6.5 ms libopus lookahead is ~26 ms one-way which is fine for offline transcode.

§Multistream API (Squad-28)

audiopus 0.3.0-rc.0 ships a multistream = [] Cargo feature that’s a stub — it gates no Rust code (the high-level wrapper just doesn’t exist for the multistream side in this crate version). We call the underlying FFI symbols directly via audiopus::ffi::* (which re-exports audiopus_sys 0.2.2’s opus_multistream_encoder_* functions). The channel-mapping family 1 layouts we wire follow RFC 7845 §5.1.1.2 verbatim (3.0 / quad / 5.0 / 5.1 / 6.1 / 7.1).

Structs§

OpusEncoder