Each Ogg packet of an Opus stream holds one Opus packet, framed as in section 3 of RFC 6716. Its first byte, the table-of-contents (TOC) byte, tells the mode, bandwidth and frame length.
TOC byte
| Bits | Name | Description |
|---|---|---|
| 7–3 | config | one of the 32 configurations below |
| 2 | s | 0 mono, 1 stereo |
| 1–0 | c | the frame count code |
| Configurations | Mode | Bandwidth | Frame sizes (ms) |
|---|---|---|---|
| 0–3 | SILK | narrowband (8 kHz) | 10, 20, 40, 60 |
| 4–7 | SILK | mediumband (12 kHz) | 10, 20, 40, 60 |
| 8–11 | SILK | wideband (16 kHz) | 10, 20, 40, 60 |
| 12–13 | Hybrid | super-wideband (24 kHz) | 10, 20 |
| 14–15 | Hybrid | fullband (48 kHz) | 10, 20 |
| 16–19 | CELT | narrowband (8 kHz) | 2.5, 5, 10, 20 |
| 20–23 | CELT | wideband (16 kHz) | 2.5, 5, 10, 20 |
| 24–27 | CELT | super-wideband (24 kHz) | 2.5, 5, 10, 20 |
| 28–31 | CELT | fullband (48 kHz) | 2.5, 5, 10, 20 |
Frame count codes
| Code | Contents |
|---|---|
| 0 | one frame fills the rest of the packet |
| 1 | two frames of equal size |
| 2 | two frames of different sizes; the first frame’s length comes first |
| 3 | any number of frames: a count byte follows |
Code 3 count byte
| Bits | Name | Description |
|---|---|---|
| 7 | v | 1 variable size: the first M−1 frame lengths follow; 0 equal sizes |
| 6 | p | 1 padding is present |
| 5–0 | M | frames in the packet, 1 to 48, for at most 120 ms in total |
Frame lengths
A length is coded as one or two bytes: 0 means no data in this frame (DTX), 1 to 251 is
the length, 252 to 255 starts a two-byte value: second byte × 4 + first byte.
The longest frame is 1,275 bytes.
Padding, when present, comes after the count byte: each byte of 255 adds 254 bytes of padding and continues; any other byte adds its value and ends the count. The padding bytes are at the end of the packet.
Rules for a valid packet
- R1: a packet is at least one byte.
- R2: no implicit frame length is larger than 1,275 bytes.
- R3: a code 1 packet has an odd total length, so that (N − 1) / 2 is a whole number.
- R4: a code 2 packet has enough bytes after the TOC for a valid frame length, and that length is no larger than the bytes left.
- R5: a code 3 packet holds at least one frame and no more than 120 ms in total.
- R6: a constant-bit-rate code 3 packet is at least two bytes; its padding count plus padding bytes, P, is no more than N − 2; and M divides (N − 2 − P) evenly.
- R7: a variable-bit-rate code 3 packet is large enough for all its header bytes, the first M − 1 frame lengths and any padding.
Multistream packets
An Opus file with several streams puts all their packets into one Ogg packet. Every stream but the last uses self-delimited framing (RFC 6716 appendix B): the last frame’s length is written out as well, so the next stream’s packet can be found.
Links
- RFC 6716 – definition of the Opus audio codec