Vorbis I Specification: February 3, 2012
Vorbis I Specification: February 3, 2012
Vorbis I Specification: February 3, 2012
Xiph.Org Foundation
February 3, 2012
Contents
1. Introduction and Description
1.1. Overview . . . . . . . . . . . . . . . . . . .
1.1.1. Application . . . . . . . . . . . . .
1.1.2. Classification . . . . . . . . . . . .
1.1.3. Assumptions . . . . . . . . . . . . .
1.1.4. Codec Setup and Probability Model
1.1.5. Format Specification . . . . . . . .
1.1.6. Hardware Profile . . . . . . . . . .
1.2. Decoder Configuration . . . . . . . . . . .
1.2.1. Global Config . . . . . . . . . . . .
1.2.2. Mode . . . . . . . . . . . . . . . . .
1.2.3. Mapping . . . . . . . . . . . . . . .
1.2.4. Floor . . . . . . . . . . . . . . . . .
1.2.5. Residue . . . . . . . . . . . . . . .
1.2.6. Codebooks . . . . . . . . . . . . . .
1.3. High-level Decode Process . . . . . . . . .
1.3.1. Decode Setup . . . . . . . . . . . .
1.3.2. Decode Procedure . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4
4
4
4
4
5
6
6
6
7
7
7
8
8
8
9
9
9
2. Bitpacking Convention
2.1. Overview . . . . . . . . . . . . . . . . .
2.1.1. octets, bytes and words . . . . .
2.1.2. bit order . . . . . . . . . . . . .
2.1.3. byte order . . . . . . . . . . . .
2.1.4. coding bits into byte sequences
2.1.5. signedness . . . . . . . . . . . .
2.1.6. coding example . . . . . . . . .
2.1.7. decoding example . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
14
14
14
14
14
15
15
15
16
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
17
.
.
.
.
.
18
18
18
18
18
23
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
25
25
25
25
25
26
26
30
30
32
32
33
33
34
35
35
36
.
.
.
.
.
37
37
37
37
38
39
.
.
.
.
.
41
41
41
41
41
42
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
45
45
45
45
47
48
49
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
53
53
53
56
56
57
58
58
59
61
61
62
.
.
.
.
.
.
.
.
.
63
63
63
63
63
64
64
64
64
65
10.Tables
10.1. floor1 inverse dB table . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
66
68
68
68
68
69
9. Helper equations
9.1. Overview . . . . . . . .
9.2. Functions . . . . . . .
9.2.1. ilog . . . . . . .
9.2.2. float32 unpack
9.2.3. lookup1 values
9.2.4. low neighbor . .
9.2.5. high neighbor .
9.2.6. render point . .
9.2.7. render line . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
71
in sequence, decodes them, synthesizes audio frames from them, and reassembles the frames
into a facsimile of the original audio stream. Vorbis is a free-form variable bit rate (VBR)
codec and packets have no minimum size, maximum size, or fixed/expected size. Packets
are designed that they may be truncated (or padded) and remain decodable; this is not to
be considered an error condition and is used extensively in bitrate management in peeling.
Both the transport mechanism and decoder must allow that a packet may be any size, or
end before or after packet decode expects.
Vorbis packets are thus intended to be used with a transport mechanism that provides
free-form framing, sync, positioning and error correction in accordance with these design
assumptions, such as Ogg (for file transport) or RTP (for network multicast). For purposes
of a few examples in this document, we will assume that Vorbis is to be embedded in an Ogg
stream specifically, although this is by no means a requirement or fundamental assumption
in the Vorbis design.
The specification for embedding Vorbis into an Ogg transport stream is in Appendix A,
Embedding Vorbis into an Ogg stream.
1.1.4. Codec Setup and Probability Model
Vorbis heritage is as a research CODEC and its current design reflects a desire to allow
multiple decades of continuous encoder improvement before running out of room within
the codec specification. For these reasons, configurable aspects of codec setup intentionally
lean toward the extreme of forward adaptive.
The single most controversial design decision in Vorbis (and the most unusual for a Vorbis
developer to keep in mind) is that the entire probability model of the codec, the Huffman
and VQ codebooks, is packed into the bitstream header along with extensive CODEC
setup parameters (often several hundred fields). This makes it impossible, as it would
be with MPEG audio layers, to embed a simple frame type flag in each audio packet, or
begin decode at any frame in the stream without having previously fetched the codec setup
header.
Note: Vorbis can initiate decode at any arbitrary packet within a bitstream so long as
the codec has been initialized/setup with the setup headers.
Thus, Vorbis headers are both required for decode to begin and relatively large as bitstream
headers go. The header size is unbounded, although for streaming a rule-of-thumb of 4kB
or less is recommended (and Xiph.Orgs Vorbis encoder follows this suggestion).
Our own design work indicates the primary liability of the required header is in mindshare;
it is an unusual design and thus causes some amount of complaint among engineers as
this runs against current design trends (and also points out limitations in some existing
1.2.4. Floor
Vorbis encodes a spectral floor vector for each PCM channel. This vector is a lowresolution representation of the audio spectrum for the given channel in the current frame,
generally used akin to a whitening filter. It is named a floor because the Xiph.Org
reference encoder has historically used it as a unit-baseline for spectral resolution.
A floor encoding may be of two types. Floor 0 uses a packed LSP representation on a
dB amplitude scale and Bark frequency scale. Floor 1 represents the curve as a piecewise
linear interpolated representation on a dB amplitude scale and linear frequency scale. The
two floors are semantically interchangeable in encoding/decoding. However, floor type 1
provides more stable inter-frame behavior, and so is the preferred choice in all coupledstereo and high bitrate modes. Floor 1 is also considerably less expensive to decode than
floor 0.
Floor 0 is not to be considered deprecated, but it is of limited modern use. No known
Vorbis encoder past Xiph.Orgs own beta 4 makes use of floor 0.
The values coded/decoded by a floor are both compactly formatted and make use of entropy coding to save space. For this reason, a floor configuration generally refers to multiple codebooks in the codebook component list. Entropy coding is thus provided as an
abstraction, and each floor instance may choose from any and all available codebooks when
coding/decoding.
1.2.5. Residue
The spectral residue is the fine structure of the audio spectrum once the floor curve has been
subtracted out. In simplest terms, it is coded in the bitstream using cascaded (multi-pass)
vector quantization according to one of three specific packing/coding algorithms numbered
0 through 2. The packing algorithm details are configured by residue instance. As with
the floor components, the final VQ/entropy encoding is provided by external codebook
instances and each residue instance may choose from any and all available codebooks.
1.2.6. Codebooks
Codebooks are a self-contained abstraction that perform entropy decoding and, optionally,
use the entropy-decoded integer value as an offset into an index of output value vectors,
returning the indicated vector of values.
The entropy coding in a Vorbis I codebook is provided by a standard Huffman binary tree
representation. This tree is tightly packed using one of several methods, depending on
whether codeword lengths are ordered or unordered, or the tree is sparse.
The codebook vector index is similarly packed according to index characteristic. Most
commonly, the vector index is encoded as a single list of values of possible values that are
then permuted into a list of n-dimensional rows (lattice VQ).
10
11
residue decode Although the number of residue vectors equals the number of channels,
channel coupling may mean that the raw residue vectors extracted during decode do not
map directly to specific channels. When channel coupling is in use, some vectors will
correspond to coupled magnitude or angle. The coupling relationships are described in the
codec setup and may differ from frame to frame, due to different mode numbers.
Vorbis codes residue vectors in groups by submap; the coding is done in submap order
from submap 0 through n-1. This differs from floors which are coded using a configuration
provided by submap number, but are coded individually in channel order.
inverse channel coupling A detailed discussion of stereo in the Vorbis codec can be found
in the document Stereo Channel Coupling in the Vorbis CODEC. Vorbis is not limited to
only stereo coupling, but the stereo document also gives a good overview of the generic
coupling mechanism.
Vorbis coupling applies to pairs of residue vectors at a time; decoupling is done in-place
a pair at a time in the order and using the vectors specified in the current mapping
configuration. The decoupling operation is the same for all pairs, converting square polar
representation (where one vector is magnitude and the second angle) back to Cartesian
representation.
After decoupling, in order, each pair of vectors on the coupling list, the resulting residue
vectors represent the fine spectral detail of each output channel.
generate floor curve The decoder may choose to generate the floor curve at any appropriate time. It is reasonable to generate the output curve when the floor data is decoded from
the raw packet, or it can be generated after inverse coupling and applied to the spectral
residue directly, combining generation and the dot product into one step and eliminating
some working space.
Both floor 0 and floor 1 generate a linear-range, linear-domain output vector to be multiplied (dot product) by the linear-range, linear-domain spectral residue.
compute floor/residue dot product This step is straightforward; for each output channel, the decoder multiplies the floor curve and residue vectors element by element, producing the finished audio spectrum of each channel.
One point is worth mentioning about this dot product; a common mistake in a fixed point
implementation might be to assume that a 32 bit fixed-point representation for floor and
residue and direct multiplication of the vectors is sufficient for acceptable spectral depth in
all cases because it happens to mostly work with the current Xiph.Org reference encoder.
However, floor vector values can span 140dB (24 bits unsigned), and the audio spectrum
vector should represent a minimum of 120dB (21 bits with sign), even when output is to
12
a 16 bit PCM device. For the residue vector to represent full scale if the floor is nailed to
140dB, it must be able to span 0 to +140dB. For the residue vector to reach full scale if
the floor is nailed at 0dB, it must be able to represent 140dB to +0dB. Thus, in order to
handle full range dynamics, a residue vector may span 140dB to +140dB entirely within
spec. A 280dB range is approximately 48 bits with sign; thus the residue vector must be
able to represent a 48 bit range and the dot product must be able to handle an effective 48
bit times 24 bit multiplication. This range may be achieved using large (64 bit or larger)
integers, or implementing a movable binary point representation.
inverse monolithic transform (MDCT) The audio spectrum is converted back into time
domain PCM audio via an inverse Modified Discrete Cosine Transform (MDCT). A detailed
description of the MDCT is available in [1].
Note that the PCM produced directly from the MDCT is not yet finished audio; it must be
lapped with surrounding frames using an appropriate window (such as the Vorbis window)
before the MDCT can be considered orthogonal.
overlap/add data Windowed MDCT output is overlapped and added with the right hand
data of the previous window such that the 3/4 point of the previous window is aligned with
the 1/4 point of the current window (as illustrated in the window overlap diagram). At
this point, the audio data between the center of the previous frame and the center of the
current frame is now finished and ready to be returned.
cache right hand data The decoder must cache the right hand portion of the current
frame to be lapped with the left hand portion of the next frame.
return finished audio data The overlapped portion produced from overlapping the previous and current frame data is finished data to be returned by the decoder. This data
spans from the center of the previous window to the center of the current window. In the
case of same-sized windows, the amount of data to return is one-half block consisting of
and only of the overlapped portions. When overlapping a short and long window, much of
the returned range is not actually overlap. This does not damage transform orthogonality. Pay attention however to returning the correct data range; the amount of data to be
returned is:
1
window_blocksize(previous_window)/4+window_blocksize(current_window)/4
from the center of the previous window to the center of the current window.
Data is not returned from the first frame; it must be used to prime the decode engine.
The encoder accounts for this priming when calculating PCM offsets; after the first frame,
the proper PCM output offset is 0 (as no data has been returned yet).
13
2. Bitpacking Convention
2.1. Overview
The Vorbis codec uses relatively unstructured raw packets containing arbitrary-width binary integer fields. Logically, these packets are a bitstream in which bits are coded one-byone by the encoder and then read one-by-one in the same monotonically increasing order
by the decoder. Most current binary storage arrangements group bits into a native word
size of eight bits (octets), sixteen bits, thirty-two bits or, less commonly other fixed word
sizes. The Vorbis bitpacking convention specifies the correct mapping of the logical packet
bitstream into an actual representation in fixed-width words.
2.1.1. octets, bytes and words
In most contemporary architectures, a byte is synonymous with an octet, that is, eight
bits. This has not always been the case; seven, ten, eleven and sixteen bit bytes have
been used. For purposes of the bitpacking convention, a byte implies the native, smallest
integer storage representation offered by a platform. On modern platforms, this is generally assumed to be eight bits (not necessarily because of the processor but because of
the filesystem/memory architecture. Modern filesystems invariably offer bytes as the fundamental atom of storage). A word is an integer size that is a grouped multiple of this
smallest size.
The most ubiquitous architectures today consider a byte to be an octet (eight bits) and
a word to be a group of two, four or eight bytes (16, 32 or 64 bits). Note however that
the Vorbis bitpacking convention is still well defined for any native byte size; Vorbis uses
the native bit-width of a given storage system. This document assumes that a byte is one
octet for purposes of example.
2.1.2. bit order
A byte has a well-defined least significant bit (LSb), which is the only bit set when the
byte is storing the twos complement integer value +1. A bytes most significant bit
(MSb) is at the opposite end of the byte. Bits in a byte are numbered from zero at the
LSb to n (n = 7 in an octet) for the MSb.
2.1.3. byte order
Words are native groupings of multiple bytes. Several byte orderings are possible in a
word; the common ones are 3-2-1-0 (big endian or most significant byte first in which
14
the highest-valued byte comes first), 0-1-2-3 (little endian or least significant byte first
in which the lowest value byte comes first) and less commonly 3-1-2-0 and 0-2-1-3 (mixed
endian).
The Vorbis bitpacking convention specifies storage and bitstream manipulation at the byte,
not word, level, thus host word ordering is of a concern only during optimization when
writing high performance code that operates on a word of storage at a time rather than by
byte. Logically, bytes are always coded and decoded in order from byte zero through byte
n.
2.1.4. coding bits into byte sequences
The Vorbis codec has need to code arbitrary bit-width integers, from zero to 32 bits wide,
into packets. These integer fields are not aligned to the boundaries of the byte representation; the next field is written at the bit position at which the previous field ends.
The encoder logically packs integers by writing the LSb of a binary integer to the logical
bitstream first, followed by next least significant bit, etc, until the requested number of
bits have been coded. When packing the bits into bytes, the encoder begins by placing
the LSb of the integer to be written into the least significant unused bit position of the
destination byte, followed by the next-least significant bit of the source integer and so on
up to the requested number of bits. When all bits of the destination byte have been filled,
encoding continues by zeroing all bits of the next byte and writing the next bit into the
bit position 0 of that byte. Decoding follows the same process as encoding, but by reading
bits from the byte stream and reassembling them into integers.
2.1.5. signedness
The signedness of a specific number resulting from decode is to be interpreted by the
decoder given decode context. That is, the three bit binary pattern b111 can be taken
to represent either seven as an unsigned integer, or -1 as a signed, twos complement
integer. The encoder and decoder are responsible for knowing if fields are to be treated as
signed or unsigned.
2.1.6. coding example
Code the 4 bit integer value 12 [b1100] into an empty bytestream. Bytestream result:
1
2
|
V
3
4
5
6
7 6 5 4 3 2 1 0
byte 0 [0 0 0 0 1 1 0 0]
byte 1 [
]
<-
15
7
8
byte 2 [
byte 3 [
]
]
...
9
10
byte n [
11
1
2
3
4
5
6
7
8
9
10
7 6 5 4 3 2 1 0
[0 1 1 1 1 1 0 0]
[
]
[
]
[
]
...
byte n [
]
byte
byte
byte
byte
0
1
2
3
<-
1
2
3
4
5
6
7
8
9
10
7 6 5 4 3 2 1 0
[1 1 1 1 1 1 0 0]
[0 0 0 0 1 0 0 0]
[
]
[
]
...
byte n [
]
byte
byte
byte
byte
0
1
2
3
11
<-
Continue by coding the 13 bit integer value 6969 [b110 11001110 01]:
|
V
1
2
3
4
5
6
7
8
byte
byte
byte
byte
0
1
2
3
7
[1
[0
[1
[0
6
1
1
1
0
9
10
5 4 3
1 1 1
0 0 1
0 0 1
0 0 0
...
2
1
0
1
1
1
0
0
1
1
byte n [
0
0]
0]
0]
0]
]
11
1
2
3
4
5
6
7
8
byte
byte
byte
byte
0
1
2
3
7
[1
[0
[1
[0
6
1
1
1
0
5
1
0
0
0
4
1
0
0
0
3
1
1
1
0
2
1
0
1
1
1
0
0
1
1
0
0]
0]
0]
0]
<-
16
We read two, two-bit integer fields, resulting in the returned numbers b00 and b11. Two
things are worth noting here:
Although these four bits were originally written as a single four-bit integer, reading
some other combination of bit-widths from the bitstream is well defined. There are
no artificial alignment boundaries maintained in the bitstream.
The second value is the two-bit-wide integer b11. This value may be interpreted
either as the unsigned value 3, or the signed value -1. Signedness is dependent on
decode context.
2.1.8. end-of-packet alignment
The typical use of bitpacking is to produce many independent byte-aligned packets which
are embedded into a larger byte-aligned container structure, such as an Ogg transport
bitstream. Externally, each bytestream (encoded bitstream) must begin and end on a byte
boundary. Often, the encoded bitstream is not an integer number of bytes, and so there is
unused (uncoded) space in the last byte of a packet.
Unused space in the last byte of a bytestream is always zeroed during the coding process.
Thus, should this unused space be read, it will return binary zeroes.
Attempting to read past the end of an encoded packet results in an end-of-packet condition. End-of-packet is not to be considered an error; it is merely a state indicating that
there is insufficient remaining data to fulfill the desired read size. Vorbis uses truncated
packets as a normal mode of operation, and as such, decoders must handle reading past
the end of a packet as a typical mode of operation. Any further read operations after an
end-of-packet condition shall also return end-of-packet.
2.1.9. reading zero bits
Reading a zero-bit-wide integer returns the value 0 and does not increment the stream
cursor. Reading to the end of the packet (but not past, such that an end-of-packet
condition has not triggered) and then reading a zero bit integer shall succeed, returning 0,
and not trigger an end-of-packet condition. Reading a zero-bit-wide integer after a previous
read sets end-of-packet shall also fail with end-of-packet.
17
byte 0: [ 0 1 0 0 0 0 1 0 ] (0x42)
byte 1: [ 0 1 0 0 0 0 1 1 ] (0x43)
byte 2: [ 0 1 0 1 0 1 1 0 ] (0x56)
byte 3: [ X X X X X X X X ]
byte 4: [ X X X X X X X X ] [codebook_dimensions] (16 bit unsigned)
4
5
6
7
byte 5: [ X X X X X X X X ]
byte 6: [ X X X X X X X X ]
byte 7: [ X X X X X X X X ] [codebook_entries] (24 bit unsigned)
18
1
2
byte 8: [
X ] [ordered] (1 bit)
byte 8: [
The decoder now performs for each of the [codebook_entries] codebook entries:
1
2
1) if([sparse] is set) {
4
5
6
7
8
9
} else {
10
11
12
mark it as such.
13
14
15
16
17
18
19
20
21
22
If the [ordered] flag is set, the codeword list for this codebook is encoded in ascending length order. Rather than reading a length for every codeword, the encoder
reads the number of codewords per length. That is, beginning at entry zero:
1
2
3
4
5
6
7
8
9
10
11
1) [current_entry] = 0;
2) [current_length] = read a five bit unsigned integer and add 1;
3) [number] = read ilog([codebook_entries] - [current_entry]) bits as an unsigned integer
4) set the entries [current_entry] through [current_entry]+[number]-1, inclusive,
of the [codebook_codeword_lengths] array to [current_length]
5) set [current_entry] to [number] + [current_entry]
6) increment [current_length] by 1
7) if [current_entry] is greater than [codebook_entries] ERROR CONDITION;
the decoder will not be able to read this stream.
8) if [current_entry] is less than [codebook_entries], repeat process starting at 3)
9) done.
19
After all codeword lengths have been decoded, the decoder reads the vector lookup table.
Vorbis I supports three lookup types:
1. No lookup
2. Implicitly populated value mapping (lattice VQ)
3. Explicitly populated value mapping (tessellated or foam VQ)
The lookup table type is read as a four bit unsigned integer:
1
1)
2)
3)
4)
5
6
if ( [codebook_lookup_type] is 1 ) {
8
9
10
} else {
11
12
13
14
15
16
17
20
entry
entry
entry
entry
entry
entry
entry
entry
0:
1:
2:
3:
4:
5:
6:
7:
length
length
length
length
length
length
length
length
2
4
4
4
4
2
3
3
Assigning codewords in order (lowest possible value of the appropriate length to highest)
results in the following codeword list:
1
2
3
4
5
6
7
8
entry
entry
entry
entry
entry
entry
entry
entry
0:
1:
2:
3:
4:
5:
6:
7:
length
length
length
length
length
length
length
length
2
4
4
4
4
2
3
3
codeword
codeword
codeword
codeword
codeword
codeword
codeword
codeword
00
0100
0101
0110
0111
10
110
111
Note: Unlike most binary numerical values in this document, we intend the above codewords to be read and used bit by bit from left to right, thus the codeword 001 is the
bit string zero, zero, one. When determining lowest possible value in the assignment
definition above, the leftmost bit is the MSb.
It is clear that the codeword length list represents a Huffman decision tree with the entry
numbers equivalent to the leaves numbered left-to-right:
21
Note that its possible to underspecify or overspecify a Huffman tree via the length list. In
the above example, if codeword seven were eliminated, its clear that the tree is unfinished:
Decoding (unpacking) a specific vector in the vector lookup table proceeds according to
[codebook_lookup_type]. The unpacked vector values are what a codebook would return
during audio packet decode in a VQ context.
22
Vector value decode: Lookup type 1 Lookup type one specifies a lattice VQ lookup
table built algorithmically from a list of scalar values. Calculate (unpack) the final values
of a codebook entry vector from the entries in [codebook_multiplicands] as follows
([value_vector] is the output vector representing the vector of values for entry number
[lookup_offset] in this codebook):
1
2
3
1) [last] = 0;
2) [index_divisor] = 1;
3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) {
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Vector value decode: Lookup type 2 Lookup type two specifies a VQ lookup table
in which each scalar in each vector is explicitly set by the [codebook_multiplicands]
array in a one-to-one mapping. Calculate [unpack] the final values of a codebook entry
vector from the entries in [codebook_multiplicands] as follows ([value_vector] is the
output vector representing the vector of values for entry number [lookup_offset] in this
codebook):
1
2
3
1) [last] = 0;
2) [multiplicand_offset] = [lookup_offset] * [codebook_dimensions]
3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) {
5
6
7
8
9
10
6) increment [multiplicand_offset]
11
12
13
14
15
23
context), or uses that entry number as an offset into the VQ lookup table, returning a
vector of values (when used in a context desiring a VQ value). Scalar or VQ context is
always explicit; any call to the codebook mechanism requests either a scalar entry number
or a lookup vector.
Note that VQ lookup type zero indicates that there is no lookup table; requesting decode
using a codebook of lookup type 0 in any context expecting a vector return value (even in
a case where a vector of dimension one) is forbidden. If decoder setup or decode requests
such an action, that is an error condition rendering the packet undecodable.
Using a codebook to read from the packet bitstream consists first of reading and decoding
the next codeword in the bitstream. The decoder reads bits until the accumulated bits
match a codeword in the codebook. This process can be though of as logically walking the
Huffman decode tree by reading one bit at a time from the bitstream, and using the bit as
a decision boolean to take the 0 branch (left in the above examples) or the 1 branch (right
in the above examples). Walking the tree finishes when the decode process hits a leaf in
the decision tree; the result is the entry number corresponding to that leaf. Reading past
the end of a packet propagates the end-of-stream condition to the decoder.
When used in a scalar context, the resulting codeword entry is the desired return value.
When used in a VQ context, the codeword entry number is used as an offset into the VQ
lookup table. The value returned to the decoder is the vector of scalars corresponding to
this offset.
24
Decode continues according to packet type; the identification header is type 1, the comment
header type 3 and the setup header type 5 (these types are all odd as a packet with a leading
single bit of 0 is an audio packet). The packets must occur in the order of identification,
comment, setup.
4.2.2. Identification header
The identification header is a short header of only a few fields used to declare the stream
definitively as Vorbis, and provide a few externally relevant pieces of information about
the audio stream. The identification header is coded as follows:
1
2
3
4
5
6
7
8
9
1)
2)
3)
4)
5)
6)
7)
8)
9)
25
26
Codebooks
1. [vorbis_codebook_count] = read eight bits as unsigned integer and add one
2. Decode [vorbis_codebook_count] codebooks in order as defined in Section 3, Probability Model and Codebooks. Save each configuration, in order, in an array of
codebook configurations [vorbis_codebook_configurations].
Time domain transforms These hooks are placeholders in Vorbis I. Nevertheless, the
configuration placeholder values must be read to maintain bitstream sync.
1. [vorbis_time_count] = read 6 bits as unsigned integer and add one
2. read [vorbis_time_count] 16 bit values; each value should be zero. If any value is
nonzero, this is an error condition and the stream is undecodable.
Floors Vorbis uses two floor types; header decode is handed to the decode abstraction of
the appropriate type.
1. [vorbis_floor_count] = read 6 bits as unsigned integer and add one
2. For each [i] of [vorbis_floor_count] floor numbers:
a) read the floor type: vector [vorbis_floor_types] element [i] = read 16 bits
as unsigned integer
b) If the floor type is zero, decode the floor configuration as defined in Section 6,
Floor type 0 setup and decode; save this configuration in slot [i] of the floor
configuration array [vorbis_floor_configurations].
c) If the floor type is one, decode the floor configuration as defined in Section 7,
Floor type 1 setup and decode; save this configuration in slot [i] of the floor
configuration array [vorbis_floor_configurations].
d) If the the floor type is greater than one, this stream is undecodable; ERROR
CONDITION
Residues Vorbis uses three residue types; header decode of each type is identical.
1. [vorbis_residue_count] = read 6 bits as unsigned integer and add one
2. For each of [vorbis_residue_count] residue numbers:
a) read the residue type; vector [vorbis_residue_types] element [i] = read 16
bits as unsigned integer
27
b) If the residue type is zero, one or two, decode the residue configuration as defined
in Section 8, Residue setup and decode; save this configuration in slot [i] of
the residue configuration array [vorbis_residue_configurations].
c) If the the residue type is greater than two, this stream is undecodable; ERROR
CONDITION
Mappings Mappings are used to set up specific pipelines for encoding multichannel audio
with varying channel mapping applications. Vorbis I uses a single mapping type (0), with
implicit PCM channel mappings.
1. [vorbis_mapping_count] = read 6 bits as unsigned integer and add one
2. For each [i] of [vorbis_mapping_count] mapping numbers:
a) read the mapping type: 16 bits as unsigned integer. Theres no reason to save
the mapping type in Vorbis I.
b) If the mapping type is nonzero, the stream is undecodable
c) If the mapping type is zero:
i. read 1 bit as a boolean flag
A. if set, [vorbis_mapping_submaps] = read 4 bits as unsigned integer
and add one
B. if unset, [vorbis_mapping_submaps] = 1
ii. read 1 bit as a boolean flag
A. if set, square polar channel mapping is in use:
[vorbis_mapping_coupling_steps] = read 8 bits as unsigned integer and add one
for [j] each of [vorbis_mapping_coupling_steps] steps:
vector [vorbis_mapping_magnitude] element [j]= read ilog([audio_
channels] - 1) bits as unsigned integer
vector [vorbis_mapping_angle] element [j]= read ilog([audio_
channels] - 1) bits as unsigned integer
the numbers read in the above two steps are channel numbers representing the channel to treat as magnitude and the channel to treat
as angle, respectively. If for any coupling step the angle channel number equals the magnitude channel number, the magnitude
channel number is greater than [audio_channels]-1, or the angle
28
29
e) verify ranges; zero is the only legal value in Vorbis I for [vorbis_mode_windowtype]
and [vorbis_mode_transformtype]. [vorbis_mode_mapping] must not be
greater than the highest number mapping in use. Any illegal values render the
stream undecodable.
f) save this mode configuration in slot [i] of the mode configuration array [vorbis_
mode_configurations].
3. read 1 bit as a framing flag. If unset, a framing error occurred and the stream is not
decodable.
After reading mode descriptions, setup header decode is complete.
30
b) if this is a short window, the window is always the same short-window shape.
Vorbis windows all use the slope function y = sin( 2 sin2 ((x + 0.5)/n )), where n is
window size and x ranges 0 . . . n 1, but dissimilar lapping requirements can affect overall
shape. Window generation proceeds as follows:
1. [window_center] = [n] / 2
2. if ([vorbis_mode_blockflag] is set and [previous_window_flag] is not set) then
a) [left_window_start] = [n]/4 - [blocksize_0]/4
b) [left_window_end] = [n]/4 + [blocksize_0]/4
c) [left_n] = [blocksize_0]/2
else
a) [left_window_start] = 0
b) [left_window_end] = [window_center]
c) [left_n] = [n]/2
3. if ([vorbis_mode_blockflag] is set and [next_window_flag] is not set) then
a) [right_window_start] = [n]*3/4 - [blocksize_0]/4
b) [right_window_end] = [n]*3/4 + [blocksize_0]/4
c) [right_n] = [blocksize_0]/2
else
a) [right_window_start] = [window_center]
b) [right_window_end] = [n]
c) [right_n] = [n]/2
4. window from range 0 ... [left_window_start]-1 inclusive is zero
5. for [i] in range [left_window_start] ... [left_window_end]-1, window([i]) =
sin( 2 sin2 ( ([i]-[left_window_start]+0.5) / [left_n] 2 ) )
6. window from range [left_window_end] ... [right_window_start]-1 inclusive is
one
7. for [i] in range [right_window_start] ... [right_window_end]-1, window([i])
= sin( 2 sin2 ( ([i]-[right_window_start]+0.5) / [right_n] 2 + 2 ) )
8. window from range [right_window_start] ... [n]-1 is zero
31
32
must be set to false. Note that an unused floor has no decoded floor information;
it is important that this is remembered at floor curve synthesis time.
4.3.4. residue decode
Unlike floors, which are decoded in channel order, the residue vectors are decoded in
submap order.
for each submap [i] in order from 0 ... [vorbis_mapping_submaps]-1
1. [ch] = 0
2. for each channel [j] in order from 0 ... [audio_channels] - 1
a) if channel [j] in submap [i] (vector [vorbis_mapping_mux] element [j] is
equal to [i])
i. if vector [no_residue] element [j] is true
A. vector [do_not_decode_flag] element [ch] is set
else
A. vector [do_not_decode_flag] element [ch] is unset
ii. increment [ch]
3. [residue_number] = vector [vorbis_mapping_submap_residue] element [i]
4. [residue_type] = vector [vorbis_residue_types] element [residue_number]
5. decode [ch] vectors using residue [residue_number], according to type [residue_
type], also passing vector [do_not_decode_flag] to indicate which vectors in the
bundle should not be decoded. Correct per-vector decode length is [n]/2.
6. [ch] = 0
7. for each channel [j] in order from 0 ... [audio_channels]
a) if channel [j] is in submap [i] (vector [vorbis_mapping_mux] element [j] is
equal to [i])
i. residue vector for channel [j] is set to decoded residue vector [ch]
ii. increment [ch]
4.3.5. inverse coupling
for each [i] from [vorbis_mapping_coupling_steps]-1 descending to 0
33
34
residue and direct multiplication of the vectors is sufficient for acceptable spectral depth in
all cases because it happens to mostly work with the current Xiph.Org reference encoder.
However, floor vector values can span 140dB (24 bits unsigned), and the audio spectrum
vector should represent a minimum of 120dB (21 bits with sign), even when output is to
a 16 bit PCM device. For the residue vector to represent full scale if the floor is nailed to
140dB, it must be able to span 0 to +140dB. For the residue vector to reach full scale if
the floor is nailed at 0dB, it must be able to represent 140dB to +0dB. Thus, in order to
handle full range dynamics, a residue vector may span 140dB to +140dB entirely within
spec. A 280dB range is approximately 48 bits with sign; thus the residue vector must be
able to represent a 48 bit range and the dot product must be able to handle an effective 48
bit times 24 bit multiplication. This range may be achieved using large (64 bit or larger)
integers, or implementing a movable binary point representation.
4.3.7. inverse MDCT
Convert the audio spectrum vector of each channel back into time domain PCM audio
via an inverse Modified Discrete Cosine Transform (MDCT). A detailed description of
the MDCT is available in [1]. The window function used for the MDCT is the function
described earlier.
4.3.8. overlap add
Windowed MDCT output is overlapped and added with the right hand data of the previous
window such that the 3/4 point of the previous window is aligned with the 1/4 point of
the current window (as illustrated in Section 1.3.2, Window shape decode (long windows
only)). The overlapped portion produced from overlapping the previous and current frame
data is finished data to be returned by the decoder. This data spans from the center of the
previous window to the center of the current window. In the case of same-sized windows,
the amount of data to return is one-half block consisting of and only of the overlapped
portions. When overlapping a short and long window, much of the returned range does not
actually overlap. This does not damage transform orthogonality. Pay attention however
to returning the correct data range; the amount of data to be returned is:
1
window\_blocksize(previous\_window)/4+window\_blocksize(current\_window)/4
from the center (element windowsize/2) of the previous window to the center (element
windowsize/2-1, inclusive) of the current window.
Data is not returned from the first frame; it must be used to prime the decode engine.
The encoder accounts for this priming when calculating PCM offsets; after the first frame,
the proper PCM output offset is 0 (as no data has been returned yet).
35
36
1)
2)
3)
4)
37
comment[0]="ARTIST=me";
comment[1]="TITLE=the sound of Vorbis";
The field name is case-insensitive and may consist of ASCII 0x20 through 0x7D, 0x3D
(=) excluded. ASCII 0x41 through 0x5A inclusive (characters A-Z) is to be considered
equivalent to ASCII 0x61 through 0x7A inclusive (characters a-z).
The field name is immediately followed by ASCII 0x3D (=); this equals sign is used to
terminate the field name.
0x3D is followed by 8 bit clean UTF-8 encoded value of the field contents to the end of the
field.
Field names Below is a proposed, minimal list of standard field names with a description
of intended use. No single or group of field names is mandatory; a comment header may
contain one, all or none of the names in this list.
TITLE Track/Work name
VERSION The version field may be used to differentiate multiple versions of the same
track title in a single collection. (e.g. remix info)
ALBUM The collection name to which this track belongs
TRACKNUMBER The track number of this piece if part of a specific larger collection or
album
ARTIST The artist generally considered responsible for the work. In popular music this is
usually the performing band or singer. For classical music it would be the composer.
For an audio book it would be the author of the original text.
PERFORMER The artist(s) who performed the work. In classical music this would be the
conductor, orchestra, soloists. In an audio book it would be the actor who did the
reading. In popular music this is typically the same as the ARTIST and is omitted.
COPYRIGHT Copyright attribution, e.g., 2001 Nobodys Band or 1999 Jack Moffitt
LICENSE License information, eg, All Rights Reserved, Any Use Permitted, a URL to a
license such as a Creative Commons license (www.creativecommons.org/blahblah/license.html)
or the EFF Open Audio License (distributed under the terms of the Open Audio
License. see http://www.eff.org/IP/Open licenses/eff oal.html for details), etc.
ORGANIZATION Name of the organization producing the track (i.e. the record label)
DESCRIPTION A short text description of the contents
38
ARTIST=Dizzy Gillespie
ARTIST=Sonny Rollins
ARTIST=Sonny Stitt
5.2.3. Encoding
The comment header comprises the entirety of the second bitstream header packet. Unlike
the first bitstream header packet, it is not generally the only packet on the second page
and may not be restricted to within the second bitstream page. The length of the comment
header packet is (practically) unbounded. The comment header packet is not optional; it
must be present in the bitstream even if it is effectively empty.
The comment header is encoded as follows (as per Oggs standard bitstream mapping which
renders least-significant-bit of the word to be coded into the least significant available bit
of the current bitstream octet first):
1. Vendor string length (32 bit unsigned quantity specifying number of octets)
39
2. Vendor string ([vendor string length] octets coded from beginning of string to end of
string, not null terminated)
3. Number of comment fields (32 bit unsigned quantity specifying number of fields)
4. Comment field 0 length (if [Number of comment fields] > 0; 32 bit unsigned quantity
specifying number of octets)
5. Comment field 0 ([Comment field 0 length] octets coded from beginning of string to
end of string, not null terminated)
6. Comment field 1 length (if [Number of comment fields] > 1...)...
This is actually somewhat easier to describe in code; implementation of the above can be
found in vorbis/lib/info.c, _vorbis_pack_comment() and _vorbis_unpack_comment().
40
1)
2)
3)
4)
5)
6)
7)
An end-of-packet condition during any of these bitstream reads renders this stream undecodable. In addition, any element of the array [floor0_book_list] that is greater than
the maximum codebook number for this bitstream is an error condition that also renders
the stream undecodable.
6.2.2. packet decode
Extracting a floor0 curve from an audio packet consists of first decoding the curve amplitude
and [floor0_order] LSP coefficient values from the bitstream, and then computing the
floor curve, which is defined as the frequency response of the decoded LSP filter.
Packet decode proceeds as follows:
1
2
3
4
5
41
6
7
8
9
10
11
6)
7)
8)
9)
10)
11)
[last] = zero;
vector [temp_vector] = read vector from bitstream using codebook number [floor0_book_list] element [booknumber] in
add the scalar value [last] to each scalar in vector [temp_vector]
[last] = the value of the last scalar in vector [temp_vector]
concatenate [temp_vector] onto the end of the [coefficients] vector
if (length of vector [coefficients] is less than [floor0_order], continue at step 6
12
13
14
15
12) done.
16
mapi =
42
where
$
floor0_bark_map_size
floor0_rate i
f oobar = bark
2n
bark(.5 floor0_rate)
and
bark(x) = 13.1 arctan(.00074x) + 2.24 arctan(.0000000185x2 + .0001x)
The above is used to synthesize the LSP curve on a Bark-scale frequency axis, then map
the result to a linear-scale frequency axis. Similarly, the below calculation synthesizes the
output LSP curve [output] on a log (dB) amplitude scale, mapping it to linear amplitude
in the last step:
1. [i] = 0
2. [] = * map element [i] / [floor0_bark_map_size]
3. if ( [floor0_order] is odd )
a) calculate [p] and [q] according to:
_order3
floor0
2
Y
p = (1 cos )
4(cos([coefficients]2j+1 ) cos )2
j=0
_order1
floor0
q =
1
4
2
Y
4(cos([coefficients]2j ) cos )2
j=0
floor0
(1 cos )
p =
2
2
Y
4(cos([coefficients]2j+1 ) cos )2
j=0
_order2
floor0
(1 + cos )
q =
2
2
Y
4(cos([coefficients]2j ) cos )2
j=0
amplitude floor0_amplitute_offset
exp .11512925
floor0_amplitude_offset
(2floor0_amplitude_bits 1) p + q
5. [iteration_condition] = map element [i]
43
44
45
46
6
7
8
9
10
11
12
13
14
15
7) vector [floor1_class_dimensions] element [i] = read 3 bits as unsigned integer and add 1
8) vector [floor1_class_subclasses] element [i] = read 2 bits as unsigned integer
9) if ( vector [floor1_class_subclasses] element [i] is nonzero ) {
16
17
18
19
47
20
11) iterate [j] over the range 0 ... (2 exponent [floor1_class_subclasses] element [i]) - 1 {
21
22
23
24
25
26
27
28
29
30
31
32
33
13)
14)
15)
16)
17)
18)
34
35
36
37
38
39
40
41
42
43
23) done
An end-of-packet condition while reading any aspect of a floor 1 configuration during setup
renders a stream undecodable. In addition, a [floor1_class_masterbooks] or [floor1_
subclass_books] scalar element greater than the highest numbered codebook configured
in this stream is an error condition that renders the stream undecodable. Vector [floor1
x list] is limited to a maximum length of 65 elements; a setup indicating more than 65
total elements (including elements 0 and 1 set prior to the read loop) renders the stream
undecodable. All vector [floor1 x list] element values must be unique within the vector; a
non-unique value renders the stream undecodable.
7.2.3. packet decode
Packet decode begins by checking the [nonzero] flag:
1
If [nonzero] is unset, that indicates this channel contained no audio energy in this frame.
Decode immediately returns a status indicating this floor curve (and thus this channel) is
unused this frame. (A return status of unused is different from decoding a floor that has
all points set to minimum representation amplitude, which happens to be approximately
-140dB).
Assuming [nonzero] is set, decode proceeds as follows:
1
2
3
4
5
1)
2)
3)
4)
5)
6
7
element [i]
48
8
9
10
11
12
7)
8)
9)
10)
11)
13
14
15
16
17
18
19
20
21
22
23
17) vector [floor1_Y] element ([j]+[offset]) = read from packet using codebook
[book] in scalar context
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
20) done
49
by clamping each element to [0, [range]) after step 1. Variants of this suggestion are
acceptable as valid floor1 setups cannot produce out of range values.
step 1: amplitude value synthesis Unwrap the always-positive-or-zero values read from
the packet into +/- difference values, then apply to line prediction.
1
2
3
4
5
6
1)
2)
3)
4)
5)
6)
7
8
9
7) [low_neighbor_offset] = low_neighbor([floor1_X_list],[i])
8) [high_neighbor_offset] = high_neighbor([floor1_X_list],[i])
10
11
12
13
14
15
16
17
18
19
20
10)
11)
12)
13)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
17)
18)
19)
20)
vector [floor1_step2_flag]
vector [floor1_step2_flag]
vector [floor1_step2_flag]
if ( [val] is greater than
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
50
59
60
61
62
63
64
65
66
67
68
69
70
71
72
29) done
73
step 2: curve synthesis Curve synthesis generates a return vector [floor] of length [n]
(where [n] is provided by the decode process calling to floor decode). Floor 1 curve
synthesis makes use of the [floor1_X_list], [floor1_final_Y] and [floor1_
step2_flag] vectors, as well as [floor1 multiplier] and [floor1 values] values.
Decode begins by sorting the scalars from vectors [floor1_X_list], [floor1_final_
Y] and [floor1_step2_flag] together into new vectors [floor1_X_list], [floor1_
final_Y] and [floor1_step2_flag] according to ascending sort order of the values in [floor1_X_list]. That is, sort the values of [floor1_X_list] and then
apply the same permutation to elements of the other two vectors so that the X, Y
and step2 flag values still match.
Then compute the final curve in one pass:
1
2
3
4
1)
2)
3)
4)
[hx] = 0
[lx] = 0
[ly] = vector [floor1_final_Y] element [0] * [floor1_multiplier]
iterate [i] over the range 1 ... [floor1_values]-1 {
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
15) for each scalar in vector [floor], perform a lookup substitution using
the scalar value from [floor] as an offset into the vector [floor1_inverse_dB_static_table]
30
51
31
16) done
32
52
53
is encoded as the additive sum of several passes through the residue vector using more
than one VQ codebook. Thus, each residue value potentially accumulates values
from multiple decode passes. The classification value associated with a partition is
the same in each pass, thus the classification codeword is coded only in the first pass.
54
55
8.3. residue 0
Residue 0 and 1 differ only in the way the values within a residue partition are interleaved
during partition encoding (visually treated as a black boxor cyan box or brown boxin
the above figure).
Residue encoding 0 interleaves VQ encoding according to the dimension of the codebook
used to encode a partition in a specific pass. The dimension of the codebook need not be
the same in multiple passes, however the partition size must be an even multiple of the
codebook dimension.
As an example, assume a partition vector of size eight, to be encoded by residue 0 using
codebook sizes of 8, 4, 2 and 1:
1
2
3
4
codebook dimensions = 8
encoded as: [ 0 1 2 3 4 5 6 7 ]
codebook dimensions = 4
encoded as: [ 0 2 4 6 ], [ 1 3 5 7 ]
codebook dimensions = 2
encoded as: [ 0 4 ], [ 1 5 ], [ 2 6 ], [ 3 7 ]
codebook dimensions = 1
encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
5
6
7
8
9
10
11
It is worth mentioning at this point that no configurable value in the residue coding setup
is restricted to a power of two.
8.4. residue 1
Residue 1 does not interleave VQ encoding. It represents partition vector scalars in order.
As with residue 0, however, partition length must be an integer multiple of the codebook
dimension, although dimension may vary from pass to pass.
As an example, assume a partition vector of size eight, to be encoded by residue 0 using
codebook sizes of 8, 4, 2 and 1:
1
2
3
4
codebook dimensions = 8
encoded as: [ 0 1 2 3 4 5 6 7 ]
codebook dimensions = 4
encoded as: [ 0 1 2 3 ], [ 4 5 6 7 ]
codebook dimensions = 2
encoded as: [ 0 1 ], [ 2 3 ], [ 4 5 ], [ 6 7 ]
codebook dimensions = 1
encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
5
6
7
8
9
10
11
56
8.5. residue 2
Residue type two can be thought of as a variant of residue type 1. Rather than encoding
multiple passed-in vectors as in residue type 1, the ch passed in vectors of length n are
first interleaved and flattened into a single vector of length ch*n. Encoding then proceeds
as in type 1. Decoding is as in type 1 with decode interleave reversed. If operating on a
single vector to begin with, residue type 1 and type 2 are equivalent.
57
1)
2)
3)
4)
5)
[residue_begin] and [residue_end] select the specific sub-portion of each vector that
is actually coded; it implements akin to a bandpass where, for coding purposes, the vector effectively begins at element [residue_begin] and ends at [residue_end]. Preceding and following values in the unpacked vectors are zeroed. Note that for residue
type 2, these values as well as [residue_partition_size]apply to the interleaved vector,
not the individual vectors before interleave. [residue_partition_size] is as explained
above, [residue_classifications] is the number of possible classification to which a
partition can belong and [residue_classbook] is the codebook number used to code
classification codewords. The number of dimensions in book [residue_classbook] determines how many classification values are grouped into a single classification codeword.
Note that the number of entries and dimensions in book [residue_classbook], along
with [residue_classifications], overdetermines to possible number of classification
codewords. If [residue_classifications][residue_classbook].dimensions exceeds
[residue_classbook].entries, the bitstream should be regarded to be undecodable.
Next we read a bitmap pattern that specifies which partition classes code values in which
passes.
1
2
3
4
5
6
7
8
9
2)
3)
4)
5)
6)
[high\_bits] = 0
[low\_bits] = read 3 bits as unsigned integer
[bitflag] = read one bit as boolean
if ( [bitflag] is set ) then [high\_bits] = read five bits as unsigned integer
vector [residue\_cascade] element [i] = [high\_bits] * 8 + [low\_bits]
}
7) done
Finally, we read in a list of book numbers, each corresponding to specific bit set in the
cascade bitmap. We loop over the possible codebook classifications and the maximum
possible number of encoding stages (8 in Vorbis I, as constrained by the elements of the
cascade bitmap being eight bits):
1
2
3
4
5
6
7
58
} else {
9
10
11
12
13
14
15
16
17
6) done
An end-of-packet condition at any point in header decode renders the stream undecodable.
In addition, any codebook number greater than the maximum numbered codebook set up
in this stream also renders the stream undecodable. All codebooks in array [residue books]
are required to have a value mapping. The presence of codebook in array [residue books]
without a value mapping (maptype equals zero) renders the stream undecodable.
8.6.2. packet decode
Format 0 and 1 packet decode is identical except for specific partition interleave. Format
2 packet decode can be built out of the format 1 decode process. Thus we describe first
the decode infrastructure identical to all three formats.
In addition to configuration information, the residue decode process is passed the number
of vectors in the submap bundle and a vector of flags indicating if any of the vectors are not
to be decoded. If the passed in number of vectors is 3 and vector number 1 is marked do
not decode, decode skips vector 1 during the decode loop. However, even do not decode
vectors are allocated and zeroed.
Depending on the values of [residue_begin] and [residue_end], it is obvious that the
encoded portion of a residue vector may be the entire possible residue vector or some
other strict subset of the actual residue vector size with zero padding at either uncoded
end. However, it is also possible to set [residue_begin] and [residue_end] to specify
a range partially or wholly beyond the maximum vector size. Before beginning residue
decode, limit [residue_begin] and [residue_end] to the maximum possible vector size
as follows. We assume that the number of vectors being encoded, [ch] is provided by the
higher level decoding process.
1
2
3
4
5
The following convenience values are conceptually useful to clarifying the decode process:
1
2
3
Packet decode proceeds as follows, matching the description offered earlier in the document.
59
1
2
3
4) [partition\_count] = 0
5
6
7
8
6) if ([pass] is zero) {
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
19) decode partition into output vector number [j], starting at scalar
offset [limit\_residue\_begin]+[partition\_count]*[residue\_partition\_size] using
codebook number [vqbook] in VQ context
41
42
43
44
45
46
47
48
49
50
51
52
53
21) done
54
60
3) vector [entry\_temp] = read vector from packet using current codebook in VQ context
4) iterate [j] over the range 0 ... [codebook\_dimensions]-1 {
4
5
6
7
8
9
10
11
12
13
14
15
6) done
16
1) [i] = 0
2) vector [entry\_temp] = read vector from packet using current codebook in VQ context
3) iterate [j] over the range 0 ... [codebook\_dimensions]-1 {
5
6
7
8
9
10
11
12
13
61
3
4
3) output vector number [j] element [i] = vector [v] element ([i] * [ch] + [j])
5
6
7
8
9
10
4) done
62
9. Helper equations
9.1. Overview
The equations below are used in multiple places by the Vorbis codec specification. Rather
than cluttering up the main specification documents, they are defined here and referenced
where appropriate.
9.2. Functions
9.2.1. ilog
The ilog(x) function returns the position number (1 through n) of the highest set bit in
the twos complement integer value [x]. Values of [x] less than zero are defined to return
zero.
1
2
1) [return\_value] = 0;
2) if ( [x] is greater than zero ) {
3) increment [return\_value];
4) logical shift [x] one bit to the right, padding the MSb with zero
5) repeat at step 2)
4
5
6
7
8
9
10
6) done
Examples:
ilog(0) = 0;
ilog(1) = 1;
ilog(2) = 2;
ilog(3) = 2;
ilog(4) = 3;
ilog(7) = 3;
ilog(negative number) = 0;
9.2.2. float32 unpack
float32 unpack(x) is intended to translate the packed binary representation of a Vorbis
codebook float value into the representation used by the decoder for floating point numbers.
63
For purposes of this example, we will unpack a Vorbis float32 into a host-native floating
point number.
1
2
3
4
5
1)
2)
3)
4)
5)
1)
2)
3)
4)
5)
6)
7
8
9
10
} else {
11
64
12
13
14
15
16
9) done
1)
[dy] = [y1] - [y0]
2) [adx] = [x1] - [x0]
3) [ady] = absolute value of [dy]
4) [base] = [dy] / [adx] using integer division
5)
[x] = [x0]
6)
[y] = [y0]
7) [err] = 0
8
9
10
9) [sy] = [base] - 1
11
12
13
} else {
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
} else {
30
31
32
33
34
35
36
37
38
65
10. Tables
10.1. floor1 inverse dB table
The vector [floor1_inverse_dB_table] is a 256 element static lookup table consiting of
the following values (read left to right then top to bottom):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1.0649863e-07,
1.3699951e-07,
1.7623575e-07,
2.2670913e-07,
2.9163793e-07,
3.7516214e-07,
4.8260743e-07,
6.2082472e-07,
7.9862701e-07,
1.0273513e-06,
1.3215816e-06,
1.7000785e-06,
2.1869758e-06,
2.8133190e-06,
3.6190449e-06,
4.6555282e-06,
5.9888572e-06,
7.7040476e-06,
9.9104632e-06,
1.2748789e-05,
1.6400004e-05,
2.1096914e-05,
2.7139006e-05,
3.4911534e-05,
4.4910090e-05,
5.7772202e-05,
7.4317983e-05,
9.5602426e-05,
0.00012298267,
0.00015820453,
0.00020351382,
0.00026179955,
0.00033677814,
0.00043323036,
0.00055730621,
0.00071691700,
0.00092223983,
0.0011863665,
0.0015261382,
0.0019632195,
0.0025254795,
0.0032487691,
0.0041792066,
0.0053761186,
0.0069158225,
0.0088964928,
0.011444421,
0.014722068,
0.018938423,
0.024362330,
0.031339626,
0.040315199,
0.051861348,
0.066714279,
1.1341951e-07,
1.4590251e-07,
1.8768855e-07,
2.4144197e-07,
3.1059021e-07,
3.9954229e-07,
5.1396998e-07,
6.6116941e-07,
8.5052630e-07,
1.0941144e-06,
1.4074654e-06,
1.8105592e-06,
2.3290978e-06,
2.9961443e-06,
3.8542308e-06,
4.9580707e-06,
6.3780469e-06,
8.2047000e-06,
1.0554501e-05,
1.3577278e-05,
1.7465768e-05,
2.2467911e-05,
2.8902651e-05,
3.7180282e-05,
4.7828601e-05,
6.1526565e-05,
7.9147585e-05,
0.00010181521,
0.00013097477,
0.00016848555,
0.00021673929,
0.00027881276,
0.00035866388,
0.00046138411,
0.00059352311,
0.00076350630,
0.00098217216,
0.0012634633,
0.0016253153,
0.0020908006,
0.0026895994,
0.0034598925,
0.0044507950,
0.0057254891,
0.0073652516,
0.009474637,
0.012188144,
0.015678791,
0.020169149,
0.025945531,
0.033376252,
0.042935108,
0.055231591,
0.071049749,
1.2079015e-07,
1.5538408e-07,
1.9988561e-07,
2.5713223e-07,
3.3077411e-07,
4.2550680e-07,
5.4737065e-07,
7.0413592e-07,
9.0579828e-07,
1.1652161e-06,
1.4989305e-06,
1.9282195e-06,
2.4804557e-06,
3.1908506e-06,
4.1047004e-06,
5.2802740e-06,
6.7925283e-06,
8.7378876e-06,
1.1240392e-05,
1.4459606e-05,
1.8600792e-05,
2.3928002e-05,
3.0780908e-05,
3.9596466e-05,
5.0936773e-05,
6.5524908e-05,
8.4291040e-05,
0.00010843174,
0.00013948625,
0.00017943469,
0.00023082423,
0.00029693158,
0.00038197188,
0.00049136745,
0.00063209358,
0.00081312324,
0.0010459992,
0.0013455702,
0.0017309374,
0.0022266726,
0.0028643847,
0.0036847358,
0.0047400328,
0.0060975636,
0.0078438871,
0.010090352,
0.012980198,
0.016697687,
0.021479854,
0.027631618,
0.035545228,
0.045725273,
0.058820850,
0.075666962,
1.2863978e-07,
1.6548181e-07,
2.1287530e-07,
2.7384213e-07,
3.5226968e-07,
4.5315863e-07,
5.8294187e-07,
7.4989464e-07,
9.6466216e-07,
1.2409384e-06,
1.5963394e-06,
2.0535261e-06,
2.6416497e-06,
3.3982101e-06,
4.3714470e-06,
5.6234160e-06,
7.2339451e-06,
9.3057248e-06,
1.1970856e-05,
1.5399272e-05,
1.9809576e-05,
2.5482978e-05,
3.2781225e-05,
4.2169667e-05,
5.4246931e-05,
6.9783085e-05,
8.9768747e-05,
0.00011547824,
0.00014855085,
0.00019109536,
0.00024582449,
0.00031622787,
0.00040679456,
0.00052329927,
0.00067317058,
0.00086596457,
0.0011139742,
0.0014330129,
0.0018434235,
0.0023713743,
0.0030505286,
0.0039241906,
0.0050480668,
0.0064938176,
0.0083536271,
0.010746080,
0.013823725,
0.017782797,
0.022875735,
0.029427276,
0.037855157,
0.048696758,
0.062643361,
0.080584227,
66
55
56
57
58
59
60
61
62
63
64
0.085821044,
0.11039993,
0.14201813,
0.18269168,
0.23501402,
0.30232132,
0.38890521,
0.50028648,
0.64356699,
0.82788260,
0.091398179,
0.11757434,
0.15124727,
0.19456402,
0.25028656,
0.32196786,
0.41417847,
0.53279791,
0.68538959,
0.88168307,
0.097337747,
0.12521498,
0.16107617,
0.20720788,
0.26655159,
0.34289114,
0.44109412,
0.56742212,
0.72993007,
0.9389798,
0.10366330,
0.13335215,
0.17154380,
0.22067342,
0.28387361,
0.36517414,
0.46975890,
0.60429640,
0.77736504,
1.
67
68
A.2. Encapsulation
Ogg encapsulation of a Vorbis packet stream is straightforward.
The first Vorbis packet (the identification header), which uniquely identifies a stream
as Vorbis audio, is placed alone in the first page of the logical Ogg stream. This
results in a first Ogg page of exactly 58 bytes at the very beginning of the logical
stream.
This first page is marked beginning of stream in the page flags.
The second and third vorbis packets (comment and setup headers) may span one or
more pages beginning on the second page of the logical stream. However many pages
they span, the third header packet finishes the page on which it ends. The next (first
audio) packet must begin on a fresh page.
The granule position of these first pages containing only headers is zero.
The first audio packet of the logical stream begins a fresh Ogg page.
Packets are placed into ogg pages in order until the end of stream.
The last page is marked end of stream in the page flags.
Vorbis packets may span page boundaries.
The granule position of pages containing Vorbis audio is in units of PCM audio
samples (per channel; a stereo streams granule position does not increment at twice
the speed of a mono stream).
The granule position of a page represents the end PCM sample position of the last
packet completed on that page. The last PCM sample is the last complete sample
returned by decode, not an internal sample awaiting lapping with a subsequent block.
A page that is entirely spanned by a single packet (that completes on a subsequent
page) has no granule position, and the granule position is set to -1.
Note that the last decoded (fully lapped) PCM sample from a packet is not necessarily
the middle sample from that block. If, eg, the current Vorbis packet encodes a
long block and the next Vorbis packet encodes a short block, the last decodable
sample from the current packet be at position (3*long block length/4) - (short block
length/4).
The granule (PCM) position of the first page need not indicate that the stream
started at position zero. Although the granule position belongs to the last completed
packet on the page and a valid granule position must be positive, by inference it may
indicate that the PCM position of the beginning of audio is positive or negative.
A positive starting value simply indicates that this stream begins at some positive time offset, potentially within a larger program. This is a common case
69
70
71
Colophon
Ogg is a Xiph.Org Foundation effort to protect essential tenets of Internet multimedia from
corporate hostage-taking; Open Source is the nets greatest tool to keep everyone honest.
See About the Xiph.Org Foundation for details.
Ogg Vorbis is the first Ogg audio CODEC. Anyone may freely use and distribute the Ogg
and Vorbis specification, whether in a private, public or corporate capacity. However,
the Xiph.Org Foundation and the Ogg project (xiph.org) reserve the right to set the Ogg
Vorbis specification and certify specification compliance.
Xiph.Orgs Vorbis software CODEC implementation is distributed under a BSD-like license. This does not restrict third parties from distributing independent implementations
of Vorbis software under other licenses.
Ogg, Vorbis, Xiph.Org Foundation and their logos are trademarks (tm) of the Xiph.Org
Foundation. These pages are copyright (C) 1994-2007 Xiph.Org Foundation. All rights
reserved.
This document is set using LATEX.
72
References
[1] T. Sporer, K. Brandenburg and B. Edler, The use of multirate filter banks for coding
of high quality digital audio, http://www.iocon.com/resource/docs/ps/eusipco_
corrected.ps.
73