Each frame in a packet is compressed with a range coder. Its contents are in the order given in RFC 6716 sections 4.2 (SILK) and 4.3 (CELT), and the fields are not byte-aligned: their widths depend on probability tables, so a frame can only be read by decoding it.
The range coder
Frames use an arithmetic range coder. The decoder is initialised from the frame’s first bytes, and each symbol is read with a table of cumulative probabilities or as uniform raw bits. Raw bits come from the end of the frame, working backwards, while range-coded symbols work forwards from the front. A SILK frame uses range-coded symbols only; CELT uses both, sharing bytes from either end.
How sizes are given
Because the coder is arithmetic, a field has a probability rather than a width. The tables below give each field as one of:
- flag, logp n: a yes/no symbol whose “yes” has probability 1/2ⁿ, so it costs about n bits when set and very little when not;
- symbol of k values: read with a fixed table of cumulative frequencies (an inverse CDF) that RFC 6716 gives;
- uniform(k): k equally likely values;
- raw n bits: plain bits taken from the end of the frame.
Decoding follows RFC 6716 as updated by RFC 8251, which corrects the reference decoder (the folding of Hybrid frames, a cap on decoded band energies, and some smaller faults). A conforming decoder must give the corrected output.
SILK layer
SILK is a linear-prediction codec for speech, run at 8 kHz (narrowband), 12 kHz (medium-band) or 16 kHz (wideband) inside Opus. A SILK frame is 10 or 20 ms, of 5 ms sub-frames (2 or 4); a 40 or 60 ms Opus frame holds 2 or 3 SILK frames of 20 ms. With stereo, each frame is coded as mid, then side. In order (RFC 6716 section 4.2):
| Field | Coded as | When | Meaning |
|---|---|---|---|
| VAD flags | flag, logp 1, one per SILK frame | once per Opus frame, for mid then side | voice activity: 1 means the SILK frame is active speech |
| LBRR flag | flag, logp 1 | after each channel’s VAD flags | low-bit-rate redundancy follows for at least one SILK frame |
| Per-frame LBRR flags | symbol of 3 (two frames) or 7 (three frames) values, a bit mask | 40 and 60 ms frames whose LBRR flag is set; a 20 ms frame’s is implied | which SILK frames carry an LBRR copy |
| LBRR frames | as ordinary frames below | for each flagged frame | a lower-quality copy of the previous packet’s frame, for loss recovery; skipped in normal decoding |
| Stereo prediction weights | a joint symbol of 25 values, then for each of the two weights a symbol of 3 and a uniform(5) | stereo, for each frame, before mid | two Q13 weights that predict side from mid (one low-passed, one full-band); they are interpolated over the first 8 ms |
| Mid-only flag | flag, symbol of 2 | stereo, when the side channel’s VAD flag is 0 | 1: the side channel is not coded in this frame (taken as silence) |
| Frame type | symbol of 2 values (VAD 0) or 4 (VAD 1) | every frame | signal type × quantisation offset type (low, high). A frame with VAD 0 is inactive; one with VAD 1 is unvoiced or voiced |
| Sub-frame gains | first sub-frame: either independent (a symbol of 8 by signal type for the top 3 bits, then uniform(8)) or a delta (symbol of 41, −4 to +36); the rest: deltas | independent for the first SILK frame of each packet (and for a side channel after a mid-only frame); otherwise delta | a 6-bit log-gain index per 5 ms sub-frame (about 1.4 dB a step), clamped to 0 to 63 |
| NLSF stage 1 | symbol of 32 | every frame | a vector of 10 (NB, MB) or 16 (WB) normalised line spectral frequencies, from a codebook chosen by bandwidth and voicing |
| NLSF stage 2 | one symbol of 9 per coefficient (−4 to 4), with an escape symbol of 7 extending it to ±10 | every frame | residuals added to stage 1, then the LSFs are stabilised (minimum spacing) |
| NLSF interpolation | symbol of 5 (0 to 4) | 20 ms frames only | the first half of the frame uses LSFs interpolated n/4 of the way from the previous frame’s; 4 means none. Forced to 4 after a reset |
| Pitch lag | absolute: a symbol of 32 (high part) and uniform(4, 6 or 8 for NB, MB, WB) (low part); relative: a symbol of 21 (−8 to +11, 0 = absolute follows) | voiced frames; relative only when the previous SILK frame of the packet was voiced | the pitch period in samples, 2 to 18 ms |
| Pitch contour | symbol of 3 or 11 (NB, 10 and 20 ms) or 12 or 34 (MB and WB) | voiced frames | per-sub-frame offsets from the lag, from a table |
| LTP periodicity | symbol of 3 (0 to 2) | voiced frames | which long-term prediction codebook follows: 8, 16 or 32 five-tap filters |
| LTP filters | one symbol of 8, 16 or 32 per sub-frame | voiced frames | the 5-tap pitch filter of each sub-frame |
| LTP scaling | symbol of 3 | voiced frames coded independently; otherwise implied | scales the LTP state at a resynchronisation: 15565, 12288 or 8192 in Q14 (implied 15565) |
| LCG seed | uniform(4) | every frame | the seed of the generator that dithers the excitation signs |
| Rate level | symbol of 9, table by signal type | every frame | which pulse-count table the 16-sample shell blocks use |
| Pulse counts | symbol of 18 per 16-sample block (0 to 16; 17 is an escape) | every block | the number of pulses in the block; each escape adds one bit level of LSBs and reads the count again with rate level 9 (then 10) |
| Shell splits | symbols splitting each count in halves: 16→8→4→2→1 | blocks with pulses | how the pulses fall on the 16 samples |
| LSBs | symbol of 2 per sample per extra bit level | blocks that escaped | low bits of the magnitudes |
| Signs | symbol of 2 per non-zero sample, table by signal type, offset type and pulse count | non-zero samples | the sign of each excitation sample |
CELT layer
CELT is a transform codec based on the modified discrete cosine transform, for music and low delay. A frame is 2.5, 5, 10 or 20 ms (LM 0 to 3, 120 × 2LM samples) and its spectrum is split into 21 bands, whose edges in 200 Hz steps (for 2.5 ms; scaled by 2LM) are 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78 and 100. The bandwidth chooses the last band coded: 13 for narrowband (to 4 kHz), 17 for wideband (8 kHz), 19 for super-wideband (12 kHz) and 21 for fullband (20 kHz). A Hybrid frame codes only bands 17 upwards. In order (RFC 6716 section 4.3):
| Field | Coded as | When | Meaning |
|---|---|---|---|
| Silence | flag, logp 15 | only as the first symbol | the frame is silent: every later symbol reads as zero, and nothing more is coded |
| Post-filter | flag, logp 1; then octave uniform(6), period raw 4 + octave bits, gain raw 3 bits, tapset symbol of 3 | CELT-only frames (start band 0) with enough bits | a pitch comb filter on the output: period = (16 << octave) + bits − 1 (15 to 1022 samples), gain = 0.09375 × (g + 1), and one of three tap shapes |
| Transient | flag, logp 3 | LM > 0 and enough bits | the frame uses 2LM short MDCTs instead of one long one |
| Intra | flag, logp 3 | enough bits | the coarse energy is coded without prediction from the previous frame |
| Coarse energy | a Laplace symbol per band and channel, with a per-band model; 1 or 2 bit fallbacks when bits run short | every band from start to end | band energies in 6 dB steps, predicted in time (unless intra) and frequency |
| tf_change | flag per band, logp 4 or 5 (2 or 4 in transient frames), coded as changes from the previous band | every band | whether the band’s time–frequency resolution is changed |
| tf_select | flag, logp 1 | LM > 0, and only when it would change the result | which table turns tf_change into a resolution change |
| Spread | symbol of 4 | enough bits (otherwise 2) | the rotation that spreads PVQ pulses: 0 none, 1 light, 2 normal, 3 aggressive |
| Dynamic allocation | per band, repeated flags starting at logp 6 (down to 2) | while bits allow | boosts that give a band extra bits, each flag one quantum more |
| Allocation trim | symbol of 11 (0 to 10) | enough bits (otherwise 5) | tilts the allocation towards low (below 5) or high bands |
| Anti-collapse reservation | 1 bit kept back | transient frames with LM ≥ 2 and enough bits | keeps room for the anti-collapse flag |
| Skip flags | flag per band, logp 1, from the top band down | during allocation, for bands that could be skipped | 1 stops skipping: the bands above get no shape bits |
| Intensity | uniform(end − start + 1) | stereo with enough bits | the first band coded as intensity stereo (one shape with an angle) |
| Dual stereo | flag, logp 1 | stereo, intensity above start, enough bits | bands below the intensity band code left and right separately rather than mid and side |
| Fine energy | raw bits, per band and channel, as the allocation gives | bands with fine bits | refines each band energy below the 6 dB step |
| Band shapes | per band: split angles (theta), then pulse vectors by PVQ (uniform indices), with folding for bands given too few bits | every band to the last coded | each band’s normalised spectrum |
| Anti-collapse | raw 1 bit | when reserved | fill short blocks that came out empty with noise |
| Energy finalisation | raw bits, one per band and channel, in two priority passes | bits left over | a last half-step of energy |
Hybrid frames
A Hybrid frame (super-wideband or fullband, 10 or 20 ms) codes a wideband SILK layer at 16 kHz first, then, in the same range coder, a CELT layer for bands 17 and up (8 kHz and above). The SILK output is resampled to 48 kHz and added to the CELT output.
Redundancy and mode transitions
A change between SILK (or Hybrid) and CELT can carry a 5 ms redundant CELT frame, so the decoder can cross-fade (RFC 6716 section 4.5.1). It is signalled after the SILK layer:
| Field | Coded as | When | Meaning |
|---|---|---|---|
| Redundancy flag | Hybrid: flag, logp 12. SILK-only: not coded, implied 1 | Hybrid: when at least 37 bits remain; SILK-only: when at least 17 bits remain after the SILK layer | a redundant CELT frame is in this frame |
| celt_to_silk | flag, logp 1 | redundancy | 1: the redundant frame leads into this frame (from CELT); 0: it leads out of it (to CELT) |
| Redundancy size | Hybrid: uniform(256), plus 2; SILK-only: every byte left | redundancy | the length in bytes of the redundant frame, which sits at the end of the Opus frame with its own range coder |
The redundant frame is a 5 ms CELT frame covering every band up to the frame’s bandwidth. When it leads in, its first 2.5 ms are played in place of the frame’s start and its second 2.5 ms are cross-faded into the frame. When it leads out, it is decoded after a reset of the CELT state, and its second 2.5 ms fade the end of this frame towards the next CELT frame. A mode change with no redundant frame instead fades over 2.5 ms from 5 ms of concealment in the old mode, and resets CELT.