Speex is a CELP speech codec from the Xiph.Org Foundation, usually found in
.spx files. It has been replaced by Opus, but old files remain. In Ogg, a stream
has an 80-byte header packet, a comment packet, any extra headers, and then audio packets.
The same frames appear in Flash movies (SWF sound format 11), FLV audio tags and RTP
(RFC 5574).
The explorer decodes every Speex stream with its own decoder, written from the Speex manual, RFC 5574 and the reference decoder, libspeex: narrowband, wideband and ultra-wideband, every sub-mode, the perceptual enhancer, intensity stereo, in-band data and discontinuous transmission. The packet table names each packet’s frames, with their modes and any in-band requests.
Header packet
The first packet is 80 bytes. Numbers are little-endian 32-bit integers.
| Offset | Size | Name | Description |
|---|---|---|---|
| 0 | 8 | speex_string | the ASCII characters Speex (three spaces) |
| 8 | 20 | speex_version | the encoder version, as text |
| 28 | 4 | speex_version_id | a version number |
| 32 | 4 | header_size | the size of this header, 80 |
| 36 | 4 | rate | the sample rate in Hz |
| 40 | 4 | mode | 0 narrowband, 1 wideband, 2 ultra-wideband |
| 44 | 4 | mode_bitstream_version | the bit stream version of that mode |
| 48 | 4 | nb_channels | the number of channels |
| 52 | 4 | bitrate | bit rate; −1 when not known |
| 56 | 4 | frame_size | samples in each frame |
| 60 | 4 | vbr | 1 when variable bit rate |
| 64 | 4 | frames_per_packet | the number of frames in each Ogg packet |
| 68 | 4 | extra_headers | further header packets after the comments |
| 72 | 8 | reserved | two more 32-bit values |
Comment packet
The second packet holds the Vorbis comments, with no framing bit.
Audio packets
Each packet holds the header’s frames_per_packet frames, packed bit after
bit with no gap between them, most significant bit first. After the last frame the
packet is padded to a whole byte with a 0 bit and then 1 bits; read as a frame, that
is mode 15, the terminator. A decoder stops at a terminator or when fewer than 5 bits
are left. Every frame is 20 ms: 160 samples in narrowband (8 kHz), 320 in wideband
(16 kHz) and 640 in ultra-wideband (32 kHz).
The bit stream is embedded: a frame begins with a whole narrowband frame for the band up to 4 kHz, and the wider modes add layers after it.
| Part | Bits | Meaning |
|---|---|---|
| narrowband frame | 1 + 4 + the sub-mode’s bits | a 0 bit, the narrowband sub-mode (0 to 8) and its parameters |
| wideband layer | 1 + 3 + the sub-mode’s bits | a 1 bit, the high-band sub-mode (0 to 4) and its parameters, for 4 to 8 kHz; in a wideband or ultra-wideband stream |
| ultra-wideband layer | 1 + 3 + the sub-mode’s bits | a 1 bit, sub-mode 0 or 1, for 8 to 16 kHz; in an ultra-wideband stream |
Where a layer would start and the next bit is 0 (the next frame’s narrowband frame), the layer is absent and counts as sub-mode 0: no high band. A narrowband decoder skips any layers it meets before a narrowband frame, so a narrowband decoder can play the low band of a wideband stream. In-band messages (modes 13 and 14 below) take the place of a narrowband frame and are followed by the real one.
Narrowband modes
A narrowband frame has four 40-sample sub-frames. Its 4-bit mode number picks one of these sub-modes:
| Mode | Bits in a 20 ms frame | Bit rate | Type |
|---|---|---|---|
| 0 | 5 | 250 bit/s | no transmission: noise at the last frame’s level, through a widened copy of the last filter |
| 1 | 43 | 2.15 kbit/s | vocoder: pulses at the pitch mixed with noise, mostly for comfort noise |
| 2 | 119 | 5.95 kbit/s | CELP |
| 3 | 160 | 8 kbit/s | CELP |
| 4 | 220 | 11 kbit/s | CELP |
| 5 | 300 | 15 kbit/s | CELP |
| 6 | 364 | 18.2 kbit/s | CELP |
| 7 | 492 | 24.6 kbit/s | CELP, two fixed codebooks |
| 8 | 79 | 3.95 kbit/s | CELP |
| 9–12 | reserved: a decoder treats the stream as corrupted | ||
| 13 | application-defined in-band data | ||
| 14 | Speex in-band requests | ||
| 15 | 5 | terminator: the end of the packet’s frames |
The bits of each sub-mode, in the order they are read. Fields marked “frame” come once, the others once in each of the four sub-frames:
| Field | Per | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| wideband bit and mode | frame | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 |
| LSPs | frame | 18 | 18 | 18 | 18 | 30 | 30 | 30 | 18 |
| open-loop pitch | frame | 7 | 7 | 7 | |||||
| open-loop pitch gain | frame | 4 | 4 | ||||||
| excitation gain | frame | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 |
| DTX flag (15 starts discontinuous transmission) | frame | 4 | |||||||
| pitch lag | sub-frame | 7 | 7 | 7 | 7 | 7 | |||
| pitch gains | sub-frame | 5 | 5 | 5 | 7 | 7 | 7 | ||
| sub-frame gain | sub-frame | 1 | 1 | 3 | 3 | 3 | |||
| fixed codebook | sub-frame | 16 | 20 | 35 | 48 | 64 | 96 | 10 | |
| Total | frame | 43 | 119 | 160 | 220 | 300 | 364 | 492 | 79 |
Source: The Speex Codec Manual (Jean-Marc Valin, 2007-05-23), section Speex narrowband mode, Table 3 (bit allocation) and Table 4 (bit rates), and
nb_celp.c of libspeex 1.2.1. The manual lists mode 1’s last four bits as a 1-bit innovation gain
in each sub-frame; libspeex nb_decode reads them once, as a 4-bit value
before the sub-frames (15 turns discontinuous transmission on). The total is the same, 43 bits, and
the table follows libspeex. Wideband table: manual Table 5; header packet: manual Table 2 and speex_header.h;
in-band codes: manual Table 1 and speex_callbacks.c. Every number is compared with these texts by
scripts/ogg/spec-check.mjs. The wideband and ultra-wideband sub-mode table above (bits, shapes, gains,
folding gains 0.9 and 0.7, the two-stage LSP codebooks) is compared with modes_wb.c, sb_celp.c
and quant_lsp.c of libspeex 1.2.1, and every codebook and filter table of the viewer’s decoder
(LSP, gain, innovation, QMF, pitch interpolation, high-pass) element by element with libspeex’s C arrays, by
scripts/ogg/speex-tables-check.mjs (124 comparisons, no difference).
- LSPs. Ten line spectral pairs. 18 bits: a 6-bit index into a 64-entry codebook of ten values, then two 6-bit refinements of the first and last five. 30 bits (modes 5 to 7): a first stage and four 6-bit refinements, two for each half. Each sub-frame uses LSPs interpolated between the last frame’s and this frame’s, kept a small margin apart, turned into the coefficients of the synthesis filter.
- Pitch. Modes 3 to 7 code a lag from 17 to 144 samples in each sub-frame and three filter taps from a gain codebook (32 entries in modes 2 to 4, 128 in 5 to 7). Mode 2 uses the frame’s open-loop pitch as the lag. Modes 1 and 8 use one lag and one 4-bit gain for the whole frame.
- Fixed codebook. The sub-frame is cut into sub-vectors, each an index into a shape codebook: mode 2 four of 10 samples from 16 shapes, modes 3 four of 10 from 32, mode 4 five of 8 from 128, modes 5 and 7 eight of 5 from 64, mode 6 eight of 5 from 256, mode 8 two of 20 from 32. Mode 7 adds a second set at 0.4545 of the gain. The excitation gain is eq/3.5 for the 5-bit q, corrected by the sub-frame gain; mode 1 uses noise instead.
Wideband and ultra-wideband layers
A wideband layer codes 4 to 8 kHz in four 40-sample sub-frames (80-sample sub-frames in an ultra-wideband layer), with an order-8 filter whose LSPs take 12 bits (two 6-bit stages). The decoder joins the two bands with a 64-tap QMF filter bank.
| Sub-mode | Bits | Bit rate | High band |
|---|---|---|---|
| 0 | 4, or none | 0.2 kbit/s | none: silence above 4 kHz |
| 1 | 36 | 1.8 kbit/s | LSPs and a 5-bit gain a sub-frame; the excitation is the low band’s, folded up (every other sample negated) |
| 2 | 112 | 5.6 kbit/s | LSPs, a 4-bit gain and four 10-sample shapes from 32 a sub-frame |
| 3 | 192 | 9.6 kbit/s | LSPs, a 4-bit gain and five 8-sample shapes from 128, each with a sign bit |
| 4 | 352 | 17.6 kbit/s | as 3, with a second set of shapes at 0.4 of the gain |
The ultra-wideband layer has only sub-modes 0 and 1. The high band’s gain is scaled by the low band’s excitation and by the ratio of the two filters’ responses where the bands meet.
In-band signalling
Mode 14 carries a request: a 4-bit code and a payload whose length depends on the code, so that a decoder can skip codes it does not handle.
| Code | Payload | Meaning |
|---|---|---|
| 0 | 1 bit | asks the decoder to turn perceptual enhancement off (0) or on (1) |
| 1 | 1 bit | reserved; the manual: asks the encoder to be less aggressive after packet loss |
| 2 | 4 bits | asks the encoder for mode N |
| 3 | 4 bits | asks the encoder for low-band mode N |
| 4 | 4 bits | asks the encoder for high-band mode N |
| 5 | 4 bits | asks the encoder for VBR quality N |
| 6 | 4 bits | acknowledgement request: 0 none, 1 all, 2 in-band data only |
| 7 | 4 bits | asks the encoder for a rate mode: the manual says CBR (0), VAD (1), DTX (3), VBR (5) or VBR with DTX (7); speex_callbacks.h says “VBR (1 for on, 0 for off)” |
| 8 | 8 bits | one character for the other end |
| 9 | 8 bits | intensity stereo, below |
| 10 | 16 bits | the maximum bit rate acceptable, in bytes a second |
| 11 | 16 bits | reserved |
| 12 | 32 bits | acknowledges packet N |
| 13 | 32 bits | reserved |
| 14, 15 | 64 bits | reserved |
Mode 13 carries application data. The manual says its size in bytes is coded in 5 bits so that a decoder can skip it; libspeex’s default handler, which speexdec uses, reads a 4-bit size and then skips 5 + 8 × size bits, and the explorer reads it the same way. speexdec acts only on intensity stereo and skips every other request; so does the explorer.
Intensity stereo
A stereo stream (nb_channels 2) codes one mono signal; in-band request
9, sent before a frame, spreads it to two channels. Its 8 bits are a sign bit, a 5-bit
exponent e and a 2-bit energy ratio index: the balance is
e±e/4 (left over right), and the ratio is 0.25, 0.315, 0.397
or 0.5. The right gain is 1 / √(ratio × (1 + balance)), the left gain √balance times
that, and both glide towards new values sample by sample. Until the first request
the balance is 1 and the ratio 0.5, so both channels carry the mono signal.
Decoding
- Enhancement. libspeex’s decoder passes the narrowband excitation through a comb filter at the pitch before synthesis, at a strength set by the sub-mode, and speexdec turns it on. The explorer does the same. A high-pass filter then removes the lowest frequencies.
- Delay. The decoder’s output is late by 40 samples in narrowband (one sub-frame), 80 in wideband and 160 in ultra-wideband. The encoder has its own delay, which it leaves out of the granule positions, so the first page’s granule position is smaller than the samples its packets hold. Like speexdec, the explorer drops both from the start, and stops at the last granule position.
- Discontinuous transmission. A mode-0 frame always gives noise at the last level. A mode-1 frame whose 4-bit flag is 15 starts DTX, and until a frame of mode 2 or more ends it, a wideband frame with no high band gets noise above 4 kHz too instead of silence, and a missing packet is filled with that comfort noise instead of being concealed.
- Level. Samples come out on a 16-bit scale and are clipped to it, as speexdec writes 16-bit samples.
- Checked against libspeex. The explorer’s decoder was compared with speexdec 1.2.1 (floating-point build, with libogg 1.3.5) on 68 files made by speexenc 1.2.1: narrowband, wideband and ultra-wideband at quality 0 to 10, VBR, ABR and bit-rate limits, VAD and DTX, complexity 0 to 10, 1 to 10 frames a packet, intensity stereo, music and noise, comments and Skeleton. Every file has the same length as speexdec’s output and agrees to 70 to 79 dB (largest difference 10 to 28 steps of 16 bits), and near-silence matches sample for sample in 98 to 99 percent of places. The two are not bit-exact: libspeex works in single precision, the explorer in double. Intensity stereo is the one place the output differs by more than rounding: speexdec scales the 16-bit mono sample by the balance in integers and wraps past 16 bits, where the explorer clips.
Links
Developers
- libspeex – the reference Speex encoder and decoder, with speexenc and speexdec (C)
Reference
- Speex project – the codec’s home page and manual
- RFC 5574 – the RTP payload format for Speex, with the mode table and the padding rule