1 <section id="vorbis-spec-intro">
4 $Id: 01-introduction.xml,v 1.3 2002/10/14 23:47:54 giles Exp $
5 <emphasis>Last update to this document: July 18, 2002</emphasis>
8 <title>introduction and description</title>
11 <title>Overview</title>
14 This document provides a high level description of the Vorbis codec's
15 construction. A bit-by-bit specification appears beginning in <xref
16 linkend="vorbis-spec-codec"/>. The other reference documents assumes
17 a high-level understanding of the Vorbis decode process, which is
18 provided in this document.</para>
21 <title>Application</title>
23 Vorbis is a general purpose perceptual audio CODEC intended to allow
24 maximum encoder flexibility, thus allowing it to scale competitively
25 over an exceptionally wide range of bitrates. At the high
26 quality/bitrate end of the scale (CD or DAT rate stereo, 16/24 bits),
27 it is in the same league as MPEG-2 and MPC. Similarly, the 1.0
28 encoder can encode high-quality CD and DAT rate stereo at below 48kpbs
29 without resampling to a lower rate. Vorbis is also intended for
30 lower and higher sample rates (from 8kHz telephony to 192kHz digital
31 masters) and a range of channel representations (monaural,
32 polyphonic, stereo, quadraphonic, 5.1, ambisonic, or up to 255
38 <title>Classification</title>
40 Vorbis I is a forward-adaptive monolithic transform CODEC based on the
41 Modified Discrete Cosine Transform. The codec is structured to allow
42 addition of a hybrid wavelet filterbank in Vorbis II to offer better
43 transient response and reproduction using a transform better suited to
44 localized time events.
49 <title>Assumptions</title>
52 The Vorbis CODEC design assumes a complex, psychoacoustically-aware
53 encoder and simple, low-complexity decoder. Vorbis decode is
54 computationally simpler than mp3, although it does require more
55 working memory as Vorbis has no static probability model; the vector
56 codebooks used in the first stage of decoding from the bitstream are
57 packed, in their entirety, into the Vorbis bitstream headers. In
58 packed form, these codebooks occupy only a few kilobytes; the extent
59 to which they are pre-decoded into a cache is the dominant factor in
64 Vorbis provides none of its own framing, synchronization or protection
65 against errors; it is solely a method of accepting input audio,
66 dividing it into individual frames and compressing these frames into
67 raw, unformatted 'packets'. The decoder then accepts these raw
68 packets in sequence, decodes them, synthesizes audio frames from
69 them, and reassembles the frames into a facsimile of the original
70 audio stream. Vorbis is a free-form VBR codec and packets have no
71 minimum size, maximum size, or fixed/expected size. Packets
72 are designed that they may be truncated (or padded) and remain
73 decodable; this is not to be considered an error condition and is used
74 extensively in bitrate management in peeling. Both the transport
75 mechanism and decoder must allow that a packet may be any size, or
76 end before or after packet decode expects.</para>
79 Vorbis packets are thus intended to be used with a transport mechanism
80 that provides free-form framing, sync, positioning and error correction
81 in accordance with these design assumptions, such as Ogg (for file
82 transport) or RTP (for network multicast). For purposes of a few
83 examples in this document, we will assume that Vorbis is to be
84 embedded in an Ogg stream specifically, although this is by no means a
85 requirement or fundamental assumption in the Vorbis design.</para>
88 <ulink url="vorbis-ogg.html">The specifications for embedding Vorbis into
89 an Ogg transport stream is in a separate document.</ulink></para>
93 <title>Codec Setup and Probability Model</title>
96 Vorbis's heritage is as a research CODEC and its current design
97 reflects a desire to allow multiple decades of continuous encoder
98 improvement before running out of room within the codec specification.
99 For these reasons, configurable aspects codec setup intentionally
100 lean toward the extreme of forward adaptive.</para>
103 The single most controversial design decision in Vorbis (and the most
104 unusual for a Vorbis developer to keep in mind) is that the entire
105 probability model of the codec, the Huffman and VQ codebooks, is
106 packed into the bitstream header along with extensive CODEC setup
107 parameters (often several hundred fields). This makes it impossible,
108 as it would be with MPEG audio layers, to embed a simple frame type
109 flag in each audio packet, or begin decode at any frame in the stream
110 without having previously fetched the codec setup header. [Note:
111 Vorbis *can* initiate decode at any arbitrary packet within a
112 bitstream so long as the codec has been initialized/setup with the
113 setup headers].</para>
116 Thus, Vorbis headers are both required for decode to begin and
117 relatively large as bitstream headers go. The header size is
118 unbounded, although for streaming a rule-of-thumb of 4kB or less is
119 recommended (and Xiph.Org's Vorbis encoder follows this suggestion).</para>
122 Our own design work indicates the the primary liability of the
123 required header is in mindshare; it is an unusual design and thus
124 causes some amount of complaint among engineers as this runs against
125 current design trends (and also points out limitations in some
126 existing software/interface designs, such as Windows' ACM codec
127 framework). However, we find that it does not fundamentally limit
128 Vorbis's suitable application space.</para>
132 <section><title>Format Specification</title>
134 The Vorbis format is well-defined by its decode specification; any
135 encoder that produces packets that are correctly decoded by the
136 reference Vorbis decoder described below may be considered a proper
137 Vorbis encoder. A decoder must faithfully and completely implement
138 the specification defined below (except where noted) to be considered
139 a proper Vorbis decoder.</para>
142 <section><title>Hardware Profile</title>
144 Although Vorbis decode is computationally simple, it may still run
145 into specific limitations of an embedded design. For this reason,
146 embedded designs are allowed to deviate in limited ways from the
147 'full' decode specification yet still be certified compliant. These
148 optional omissions are labelled in the spec where relevant.</para>
154 <title>Decoder Configuration</title>
157 Decoder setup consists of configuration of multiple, self-contained
158 component abstractions that perform specific functions in the decode
159 pipeline. Each different component instance of a specific type is
160 semantically interchangeable; decoder configuration consists both of
161 internal component configuration, as well as arrangement of specific
162 instances into a decode pipeline. Componentry arrangement is roughly
167 <imagedata fileref="components.png" format="PNG"/>
170 <phrase>decoder pipeline configuration</phrase>
174 <section><title>Global Config</title>
176 Global codec configuration consists of a few audio related fields
177 (sample rate, channels), Vorbis version (always '0' in Vorbis I),
178 bitrate hints, and the lists of component instances. All other
179 configuration is in the context of specific components.</para>
182 <section><title>Mode</title>
185 Each Vorbis frame is coded according to a master 'mode'. A bitstream
186 may use one or many modes.</para>
189 The mode mechanism is used to encode a frame according to one of
190 multiple possible methods with the intention of choosing a method best
191 suited to that frame. Different modes are, e.g. how frame size
192 is changed from frame to frame. The mode number of a frame serves as a
193 top level configuration switch for all other specific aspects of frame
197 A 'mode' configuration consists of a frame size setting, window type
198 (always 0, the Vorbis window, in Vorbis I), transform type (always
199 type 0, the MDCT, in Vorbis I) and a mapping number. The mapping
200 number specifies which mapping configuration instance to use for
201 low-level packet decode and synthesis.</para>
205 <section><title>Mapping</title>
208 A mapping contains a channel coupling description and a list of
209 'submaps' that bundle sets of channel vectors together for grouped
210 encoding and decoding. These submaps are not references to external
211 components; the submap list is internal and specific to a mapping.</para>
214 A 'submap' is a configuration/grouping that applies to a subset of
215 floor and residue vectors within a mapping. The submap functions as a
216 last layer of indirection such that specific special floor or residue
217 settings can be applied not only to all the vectors in a given mode,
218 but also specific vectors in a specific mode. Each submap specifies
219 the proper floor and residue instance number to use for decoding that
220 submap's spectral floor and spectral residue vectors.</para>
223 As an example:</para>
226 Assume a Vorbis stream that contains six channels in the standard 5.1
227 format. The sixth channel, as is normal in 5.1, is bass only.
228 Therefore it would be wasteful to encode a full-spectrum version of it
229 as with the other channels. The submapping mechanism can be used to
230 apply a full range floor and residue encoding to channels 0 through 4,
231 and a bass-only representation to the bass channel, thus saving space.
232 In this example, channels 0-4 belong to submap 0 (which indicates use
233 of a full-range floor) and channel 5 belongs to submap 1, which uses a
234 bass-only representation.</para>
238 <section><title>Floor</title>
241 Vorbis encodes a spectral 'floor' vector for each PCM channel. This
242 vector is a low-resolution representation of the audio spectrum for
243 the given channel in the current frame, generally used akin to a
244 whitening filter. It is named a 'floor' because the Xiph.Org
245 reference encoder has historically used it as a unit-baseline for
246 spectral resolution.</para>
249 A floor encoding may be of two types. Floor 0 uses a packed LSP
250 representation on a dB amplitude scale and Bark frequency scale.
251 Floor 1 represents the curve as a piecewise linear interpolated
252 representation on a dB amplitude scale and linear frequency scale.
253 The two floors are semantically interchangeable in
254 encoding/decoding. However, floor type 1 provides more stable
255 inter-frame behavior, and so is the preferred choice in all
256 coupled-stereo and high bitrate modes. Floor 1 is also considerably
257 less expensive to decode than floor 0.</para>
260 Floor 0 is not to be considered deprecated, but it is of limited
261 modern use. No known Vorbis encoder past Xiph.org's own beta 4 makes
262 use of floor 0.</para>
265 The values coded/decoded by a floor are both compactly formatted and
266 make use of entropy coding to save space. For this reason, a floor
267 configuration generally refers to multiple codebooks in the codebook
268 component list. Entropy coding is thus provided as an abstraction,
269 and each floor instance may choose from any and all available
270 codebooks when coding/decoding.</para>
274 <section><title>Residue</title>
276 The spectral residue is the fine structure of the audio spectrum
277 once the floor curve has been subtracted out. In simplest terms, it
278 is coded in the bitstream using cascaded (multi-pass) vector
279 quantization according to one of three specific packing/coding
280 algorithms numbered 0 through 2. The packing algorithm details are
281 configured by residue instance. As with the floor components, the
282 final VQ/entropy encoding is provided by external codebook instances
283 and each residue instance may choose from any and all available
287 <section><title>Codebooks</title>
290 Codebooks are a self-contained abstraction that perform entropy
291 decoding and, optionally, use the entropy-decoded integer value as an
292 offset into an index of output value vectors, returning the indicated
293 vector of values.</para>
296 The entropy coding in a Vorbis I codebook is provided by a standard
297 Huffman binary tree representation. This tree is tightly packed using
298 one of several methods, depending on whether codeword lengths are
299 ordered or unordered, or the tree is sparse.</para>
302 The codebook vector index is similarly packed according to index
303 characteristic. Most commonly, the vector index is encoded as a
304 single list of values of possible values that are then permuted into
305 a list of n-dimensional rows (lattice VQ).</para>
313 <title>High-level Decode Process</title>
316 <title>Decode setup</title>
319 Before decoding can begin, a decoder must initialize using the
320 bitstream headers matching the stream to be decoded. Vorbis uses
321 three header packets; all are required, in-order, by this
322 specification. Once set up, decode may begin at any audio packet
323 belonging to the Vorbis stream. In Vorbis I, all packets after the
324 three initial headers are audio packets. </para>
327 The header packets are, in order, the identification
328 header, the comments header, and the setup header.</para>
330 <section><title>Identification Header</title>
332 The identification header identifies the bitstream as Vorbis, Vorbis
333 version, and the simple audio characteristics of the stream such as
334 sample rate and number of channels.</para>
337 <section><title>Comment Header</title>
339 The comment header includes user text comments ("tags") and a vendor
340 string for the application/library that produced the bitstream. The
341 encoding of the comment header is described within this document; the
342 proper use of the comment fields is described in
343 <xref linkend="vorbis-spec-comment"/>.</para>
346 <section><title>Setup Header</title>
348 The setup header includes extensive CODEC setup information as well as
349 the complete VQ and Huffman codebooks needed for decode.</para>
354 <section><title>Decode Procedure</title>
358 The decoding and synthesis procedure for all audio packets is
359 fundamentally the same.
361 <listitem><simpara>decode packet type flag</simpara></listitem>
362 <listitem><simpara>decode mode number</simpara></listitem>
363 <listitem><simpara>decode window shape (long windows only)</simpara></listitem>
364 <listitem><simpara>decode floor</simpara></listitem>
365 <listitem><simpara>decode residue into residue vectors</simpara></listitem>
366 <listitem><simpara>inverse channel coupling of residue vectors</simpara></listitem>
367 <listitem><simpara>generate floor curve from decoded floor data</simpara></listitem>
368 <listitem><simpara>compute dot product of floor and residue, producing audio spectrum vector</simpara></listitem>
369 <listitem><simpara>inverse monolithic transform of audio spectrum vector, always an MDCT in Vorbis I</simpara></listitem>
370 <listitem><simpara>overlap/add left-hand output of transform with right-hand output of previous frame</simpara></listitem>
371 <listitem><simpara>store right hand-data from transform of current frame for future lapping.</simpara></listitem>
372 <listitem><simpara>if not first frame, return results of overlap/add as audio result of current frame</simpara></listitem>
378 Note that clever rearrangement of the synthesis arithmetic is
379 possible; as an example, one can take advantage of symmetries in the
380 MDCT to store the right-hand transform data of a partial MDCT for a
381 50% inter-frame buffer space savings, and then complete the transform
382 later before overlap/add with the next frame. This optimization
383 produces entirely equivalent output and is naturally perfectly legal.
384 The decoder must be <emphasis>entirely mathematically equivalent</emphasis> to the
385 specification, it need not be a literal semantic implementation.</para>
387 <section><title>Packet type decode</title>
390 Vorbis I uses four packet types. The first three packet types mark each
391 of the three Vorbis headers described above. The fourth packet type
392 marks an audio packet. All others packet types are reserved; packets
393 marked with a reserved flag type should be ignored.</para>
396 Following the three header packets, all packets in a Vorbis I stream
397 are audio. The first step of audio packet decode is to read and
398 verify the packet type; <emphasis>a non-audio packet when audio is expected
399 indicates stream corruption or a non-compliant stream. The decoder
400 must ignore the packet and not attempt decoding it to
401 audio</emphasis>.</para>
406 <section><title>Mode decode</title>
408 Vorbis allows an encoder to set up multiple, numbered packet 'modes',
409 as described earlier, all of which may be used in a given Vorbis
410 stream. The mode is encoded as an integer used as a direct offset into
411 the mode instance index. </para>
414 <section id="vorbis-spec-window">
415 <title>Window shape decode (long windows only)</title>
418 Vorbis frames may be one of two PCM sample sizes specified during
419 codec setup. In Vorbis I, legal frame sizes are powers of two from 64
420 to 8192 samples. Aside from coupling, Vorbis handles channels as
421 independent vectors and these frame sizes are in samples per channel.</para>
424 Vorbis uses an overlapping transform, namely the MDCT, to blend one
425 frame into the next, avoiding most inter-frame block boundary
426 artifacts. The MDCT output of one frame is windowed according to MDCT
427 requirements, overlapped 50% with the output of the previous frame and
428 added. The window shape assures seamless reconstruction. </para>
431 This is easy to visualize in the case of equal sized-windows:</para>
435 <imagedata fileref="window1.png" format="PNG"/>
438 <phrase>overlap of two equal-sized windows</phrase>
443 And slightly more complex in the case of overlapping unequal sized
448 <imagedata fileref="window2.png" format="PNG"/>
451 <phrase>overlap of a long and a short window</phrase>
456 In the unequal-sized window case, the window shape of the long window
457 must be modified for seamless lapping as above. It is possible to
458 correctly infer window shape to be applied to the current window from
459 knowing the sizes of the current, previous and next window. It is
460 legal for a decoder to use this method; However, in the case of a long
461 window (short windows require no modification), Vorbis also codes two
462 flag bits to specify pre- and post- window shape. Although not
463 strictly necessary for function, this minor redundancy allows a packet
464 to be fully decoded to the point of lapping entirely independently of
465 any other packet, allowing easier abstraction of decode layers as well
466 as allowing a greater level of easy parallelism in encode and
470 A description of valid window functions for use with an inverse MDCT
471 can be found in the paper
472 <citetitle pubwork="article">
473 <ulink url="http://www.iocon.com/resource/docs/ps/eusipco_corrected.ps">
474 The use of multirate filter banks for coding of high quality digital
475 audio</ulink></citetitle>, by T. Sporer, K. Brandenburg and B. Edler. Vorbis windows
476 all use the slope function
478 <alt>y=sin(2PI*sin^2(x/n))</alt>
481 <phrase>$y = \sin(2\pi \, \sin^2(x/n))$</phrase>
489 <section><title>floor decode</title>
491 Each floor is encoded/decoded in channel order, however each floor
492 belongs to a 'submap' that specifies which floor configuration to
493 use. All floors are decoded before residue decode begins.</para>
496 <section><title>residue decode</title>
499 Although the number of residue vectors equals the number of channels,
500 channel coupling may mean that the raw residue vectors extracted
501 during decode do not map directly to specific channels. When channel
502 coupling is in use, some vectors will correspond to coupled magnitude
503 or angle. The coupling relationships are described in the codec setup
504 and may differ from frame to frame, due to different mode numbers.</para>
507 Vorbis codes residue vectors in groups by submap; the coding is done
508 in submap order from submap 0 through n-1. This differs from floors
509 which are coded using a configuration provided by submap number, but
510 are coded individually in channel order.</para>
514 <section><title>inverse channel coupling</title>
517 A detailed discussion of stereo in the Vorbis codec can be found in
518 the document <ulink url="stereo.html">_Stereo Channel Coupling in the
519 Vorbis CODEC_</ulink>. Vorbis is not limited to only stereo coupling, but
520 the stereo document also gives a good overview of the generic coupling
524 Vorbis coupling applies to pairs of residue vectors at a time;
525 decoupling is done in-place a pair at a time in the order and using
526 the vectors specified in the current mapping configuration. The
527 decoupling operation is the same for all pairs, converting square
528 polar representation (where one vector is magnitude and the second
529 angle) back to Cartesian representation.</para>
532 After decoupling, in order, each pair of vectors on the coupling list
533 in, the resulting residue vector represents the fine spectral detail
534 of each output channel.</para>
538 <section><title>generate floor curve</title>
541 The decoder may choose to generate the floor curve at any appropriate
542 time. It is reasonable to generate the output curve when the floor
543 data is decoded from the raw packet, or it can be generated after
544 inverse coupling and applied to the spectral residue directly,
545 combining generation and the dot product into one step and eliminating
546 some working space.</para>
549 Both floor 0 and floor 1 generate a linear-range, linear-domain output
550 vector to be multiplied (dot product) by the linear-range,
551 linear-domain spectral residue.</para>
555 <section><title>compute floor/residue dot product</title>
558 This step is straightforward; for each output channel, the decoder
559 multiplies the floor curve and residue vectors element by element,
560 producing the finished audio spectrum of each channel.</para>
563 One point is worth mentioning about this dot product; a common mistake
564 in a fixed point implementation might be to assume that a 32 bit
565 fixed-point representation for floor and residue and direct
566 multiplication of the vectors is sufficient for acceptable spectral
567 depth in all cases because it happens to mostly work with the current
568 Xiph.Org reference encoder.</para>
571 However, floor vector values can span ~140dB (~24 bits unsigned), and
572 the audio spectrum vector should represent a minimum of 120dB (~21
573 bits with sign), even when output is to a 16 bit PCM device. For the
574 residue vector to represent full scale if the floor is nailed to
575 -140dB, it must be able to span 0 to +140dB. For the residue vector
576 to reach full scale if the floor is nailed at 0dB, it must be able to
577 represent -140dB to +0dB. Thus, in order to handle full range
578 dynamics, a residue vector may span -140dB to +140dB entirely within
579 spec. A 280dB range is approximately 48 bits with sign; thus the
580 residue vector must be able to represent a 48 bit range and the dot
581 product must be able to handle an effective 48 bit times 24 bit
582 multiplication. This range may be achieved using large (64 bit or
583 larger) integers, or implementing a movable binary point
584 representation.</para>
588 <section><title>inverse monolithic transform (MDCT)</title>
591 The audio spectrum is converted back into time domain PCM audio via an
592 inverse Modified Discrete Cosine Transform (MDCT). A detailed
593 description of the MDCT is available in the paper
594 <citetitle pubwork="article"><ulink
595 url="http://www.iocon.com/resource/docs/ps/eusipco_corrected.ps">
596 The use of multirate filter banks for coding of high quality digital
597 audio</ulink></citetitle>, by T. Sporer, K. Brandenburg and B. Edler.</para>
600 Note that the PCM produced directly from the MDCT is not yet finished
601 audio; it must be lapped with surrounding frames using an appropriate
602 window (such as the Vorbis window) before the MDCT can be considered
607 <section><title>overlap/add data</title>
609 Windowed MDCT output is overlapped and added with the right hand data
610 of the previous window such that the 3/4 point of the previous window
611 is aligned with the 1/4 point of the current window (as illustrated in
612 the window overlap diagram). At this point, the audio data between the
613 center of the previous frame and the center of the current frame is
614 now finished and ready to be returned. </para>
617 <section><title>cache right hand data</title>
619 The decoder must cache the right hand portion of the current frame to
620 be lapped with the left hand portion of the next frame.
624 <section><title>return finished audio data</title>
627 The overlapped portion produced from overlapping the previous and
628 current frame data is finished data to be returned by the decoder.
629 This data spans from the center of the previous window to the center
630 of the current window. In the case of same-sized windows, the amount
631 of data to return is one-half block consisting of and only of the
632 overlapped portions. When overlapping a short and long window, much of
633 the returned range is not actually overlap. This does not damage
634 transform orthogonality. Pay attention however to returning the
635 correct data range; the amount of data to be returned is:
638 window_blocksize(previous_window)/4+window_blocksize(current_window)/4
641 from the center of the previous window to the center of the current
645 Data is not returned from the first frame; it must be used to 'prime'
646 the decode engine. The encoder accounts for this priming when
647 calculating PCM offsets; after the first frame, the proper PCM output
648 offset is '0' (as no data has been returned yet).</para>
655 <!-- end Vorbis I specification introduction and description -->