From: Ralph Giles Date: Thu, 2 Jul 2020 21:44:37 +0000 (-0700) Subject: Rebuild specification. X-Git-Tag: v1.3.7~4 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fupstream%2Flibvorbis.git;a=commitdiff_plain;h=94140bc2eebeb93b3de1bad905f3c2cd9d079f7e Rebuild specification. The tools to build the Vorbis format specification are a heavy dependency, so we keep a built copy of that documentation in the repository for the convenience of casual developers. Rebuild the documents to bump the date and footer link for the 1.3.7 release. The spec build is broken on current texlive releases, with symptoms like a missing table of contents in the pdf and incorrect size attributes on figure images in the html output. This version was build in a debian:9 (stretch) container to recover more of the expected behaviour. Signed-off-by: Mark Harris --- diff --git a/doc/Vorbis_I_spec.html b/doc/Vorbis_I_spec.html index a314c97..a1d9496 100644 --- a/doc/Vorbis_I_spec.html +++ b/doc/Vorbis_I_spec.html @@ -7,7 +7,6 @@ - @@ -15,15 +14,12 @@ - - -

Vorbis I specification

Xiph.Org Foundation

February 27, 2015
+class="cmr-17">July 4, 2020

Contents

@@ -248,19 +244,19 @@ href="#x1-134000B" id="QQ2-1-146">Vorbis encapsulation in RTP

1. Introduction and Description

-

+

1.1. Overview

-

This document provides a high level description of the Vorbis codec’s construction. A bit-by-bit +

This document provides a high level description of the Vorbis codec’s construction. A bit-by-bit specification appears beginning in section 4, “Codec Setup and Packet Decode”. The later sections assume a high-level understanding of the Vorbis decode process, which is provided here. -

+

1.1.1. Application
-

Vorbis is a general purpose perceptual audio CODEC intended to allow maximum encoder +

Vorbis is a general purpose perceptual audio CODEC intended to allow maximum encoder flexibility, thus allowing it to scale competitively over an exceptionally wide range of bitrates. At the high quality/bitrate end of the scale (CD or DAT rate stereo, 16/24 bits) it is in the same league as MPEG-2 and MPC. Similarly, the 1.0 encoder can encode high-quality CD and DAT @@ -268,27 +264,27 @@ rate stereo at below 48kbps without resampling to a lower rate. Vorbis is also i and higher sample rates (from 8kHz telephony to 192kHz digital masters) and a range of channel representations (monaural, polyphonic, stereo, quadraphonic, 5.1, ambisonic, or up to 255 discrete channels). -

+

1.1.2. Classification
-

Vorbis I is a forward-adaptive monolithic transform CODEC based on the Modified Discrete +

Vorbis I is a forward-adaptive monolithic transform CODEC based on the Modified Discrete Cosine Transform. The codec is structured to allow addition of a hybrid wavelet filterbank in Vorbis II to offer better transient response and reproduction using a transform better suited to localized time events. -

+

1.1.3. Assumptions
-

The Vorbis CODEC design assumes a complex, psychoacoustically-aware encoder and simple, +

The Vorbis CODEC design assumes a complex, psychoacoustically-aware encoder and simple, low-complexity decoder. Vorbis decode is computationally simpler than mp3, although it does require more working memory as Vorbis has no static probability model; the vector codebooks used in the first stage of decoding from the bitstream are packed in their entirety into the Vorbis bitstream headers. In packed form, these codebooks occupy only a few kilobytes; the extent to which they are pre-decoded into a cache is the dominant factor in decoder memory usage. -

Vorbis provides none of its own framing, synchronization or protection against errors; it +

Vorbis provides none of its own framing, synchronization or protection against errors; it is solely a method of accepting input audio, dividing it into individual frames and compressing these frames into raw, unformatted ’packets’. The decoder then accepts these raw packets in sequence, decodes them, synthesizes audio frames from them, and @@ -299,121 +295,121 @@ and remain decodable; this is not to be considered an error condition and is use 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 +

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

The specification for embedding Vorbis into an Ogg transport stream is in section 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 +

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 +

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 +

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.Org’s Vorbis encoder follows this suggestion). -

Our own design work indicates the primary liability of the required header is in mindshare; it is +

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 software/interface designs, such as Windows’ ACM codec framework). However, we find that it does not fundamentally limit Vorbis’ suitable application space. -

+

1.1.5. Format Specification
-

The Vorbis format is well-defined by its decode specification; any encoder that produces packets +

The Vorbis format is well-defined by its decode specification; any encoder that produces packets that are correctly decoded by the reference Vorbis decoder described below may be considered a proper Vorbis encoder. A decoder must faithfully and completely implement the specification defined below (except where noted) to be considered a proper Vorbis decoder. -

+

1.1.6. Hardware Profile
-

Although Vorbis decode is computationally simple, it may still run into specific limitations of an +

Although Vorbis decode is computationally simple, it may still run into specific limitations of an embedded design. For this reason, embedded designs are allowed to deviate in limited ways from the ‘full’ decode specification yet still be certified compliant. These optional omissions are labelled in the spec where relevant. -

+

1.2. Decoder Configuration

-

Decoder setup consists of configuration of multiple, self-contained component abstractions that +

Decoder setup consists of configuration of multiple, self-contained component abstractions that perform specific functions in the decode pipeline. Each different component instance of a specific type is semantically interchangeable; decoder configuration consists both of internal component configuration, as well as arrangement of specific instances into a decode pipeline. Componentry arrangement is roughly as follows:

-

+

-

PIC

Figure 1: decoder pipeline configuration
-

+

1.2.1. Global Config
-

Global codec configuration consists of a few audio related fields (sample rate, channels), Vorbis +

Global codec configuration consists of a few audio related fields (sample rate, channels), Vorbis version (always ’0’ in Vorbis I), bitrate hints, and the lists of component instances. All other configuration is in the context of specific components. -

+

1.2.2. Mode
-

Each Vorbis frame is coded according to a master ’mode’. A bitstream may use one or many +

Each Vorbis frame is coded according to a master ’mode’. A bitstream may use one or many modes. -

The mode mechanism is used to encode a frame according to one of multiple possible +

The mode mechanism is used to encode a frame according to one of multiple possible methods with the intention of choosing a method best suited to that frame. Different modes are, e.g. how frame size is changed from frame to frame. The mode number of a frame serves as a top level configuration switch for all other specific aspects of frame decode. -

A ’mode’ configuration consists of a frame size setting, window type (always 0, the Vorbis +

A ’mode’ configuration consists of a frame size setting, window type (always 0, the Vorbis window, in Vorbis I), transform type (always type 0, the MDCT, in Vorbis I) and a mapping number. The mapping number specifies which mapping configuration instance to use for low-level packet decode and synthesis. -

+

1.2.3. Mapping
-

A mapping contains a channel coupling description and a list of ’submaps’ that bundle sets +

A mapping contains a channel coupling description and a list of ’submaps’ that bundle sets of channel vectors together for grouped encoding and decoding. These submaps are not references to external components; the submap list is internal and specific to a mapping. -

A ’submap’ is a configuration/grouping that applies to a subset of floor and residue vectors +

A ’submap’ is a configuration/grouping that applies to a subset of floor and residue vectors within a mapping. The submap functions as a last layer of indirection such that specific special floor or residue settings can be applied not only to all the vectors in a given mode, but also specific vectors in a specific mode. Each submap specifies the proper floor and residue instance number to use for decoding that submap’s spectral floor and spectral residue vectors. -

As an example: -

Assume a Vorbis stream that contains six channels in the standard 5.1 format. The sixth +

As an example: +

Assume a Vorbis stream that contains six channels in the standard 5.1 format. The sixth channel, as is normal in 5.1, is bass only. Therefore it would be wasteful to encode a full-spectrum version of it as with the other channels. The submapping mechanism can be used to apply a full range floor and residue encoding to channels 0 through 4, and a bass-only @@ -423,66 +419,66 @@ uses a bass-only representation. -

+

1.2.4. Floor
-

Vorbis encodes a spectral ’floor’ vector for each PCM channel. This vector is a low-resolution +

Vorbis encodes a spectral ’floor’ vector for each PCM channel. This vector is a low-resolution 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 +

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 coupled-stereo 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 +

Floor 0 is not to be considered deprecated, but it is of limited modern use. No known Vorbis encoder past Xiph.Org’s own beta 4 makes use of floor 0. -

The values coded/decoded by a floor are both compactly formatted and make use of entropy +

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 +

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 +

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 +

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 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). -

+

1.3. High-level Decode Process

-

+

1.3.1. Decode Setup
-

Before decoding can begin, a decoder must initialize using the bitstream headers matching the +

Before decoding can begin, a decoder must initialize using the bitstream headers matching the stream to be decoded. Vorbis uses three header packets; all are required, in-order, by this specification. Once set up, decode may begin at any audio packet belonging to the Vorbis stream. In Vorbis I, all packets after the three initial headers are audio packets. -

The header packets are, in order, the identification header, the comments header, and the setup +

The header packets are, in order, the identification header, the comments header, and the setup header. -

Identification Header The identification header identifies the bitstream as Vorbis, Vorbis version, and the simple audio @@ -490,7 +486,7 @@ characteristics of the stream such as sample rate and number of channels. -

Comment Header The comment header includes user text comments (“tags”) and a vendor string for the @@ -498,15 +494,15 @@ application/library that produced the bitstream. The encoding and proper use of header is described in section 5, “comment field and header specification”. -

Setup Header The setup header includes extensive CODEC setup information as well as the complete VQ and Huffman codebooks needed for decode. -

+

1.3.2. Decode Procedure
-

The decoding and synthesis procedure for all audio packets is fundamentally the same. +

The decoding and synthesis procedure for all audio packets is fundamentally the same.

1.
decode packet type flag @@ -547,33 +543,33 @@ class="enumerate-enumitem">store right hand-data from transform of current frame
12.
if not first frame, return results of overlap/add as audio result of current frame
-

Note that clever rearrangement of the synthesis arithmetic is possible; as an example, one can +

Note that clever rearrangement of the synthesis arithmetic is possible; as an example, one can take advantage of symmetries in the MDCT to store the right-hand transform data of a partial MDCT for a 50% inter-frame buffer space savings, and then complete the transform later before overlap/add with the next frame. This optimization produces entirely equivalent output and is naturally perfectly legal. The decoder must be entirely mathematically equivalent to the specification, it need not be a literal semantic implementation. -

Packet type decode Vorbis I uses four packet types. The first three packet types mark each of the three Vorbis headers described above. The fourth packet type marks an audio packet. All other packet types are reserved; packets marked with a reserved type should be ignored. -

Following the three header packets, all packets in a Vorbis I stream are audio. The first step of +

Following the three header packets, all packets in a Vorbis I stream are audio. The first step of audio packet decode is to read and verify the packet type; a non-audio packet when audio is expected indicates stream corruption or a non-compliant stream. The decoder must ignore the packet and not attempt decoding it to audio. -

Mode decode Vorbis allows an encoder to set up multiple, numbered packet ’modes’, as described earlier, all of which may be used in a given Vorbis stream. The mode is encoded as an integer used as a direct offset into the mode instance index. -

Window shape decode (long windows only) Vorbis frames may be one of two PCM sample sizes specified during codec setup. In Vorbis I, @@ -583,35 +579,35 @@ channel. -

Vorbis uses an overlapping transform, namely the MDCT, to blend one frame into the next, +

Vorbis uses an overlapping transform, namely the MDCT, to blend one frame into the next, avoiding most inter-frame block boundary artifacts. The MDCT output of one frame is windowed according to MDCT requirements, overlapped 50% with the output of the previous frame and added. The window shape assures seamless reconstruction. -

This is easy to visualize in the case of equal sized-windows: +

This is easy to visualize in the case of equal sized-windows:

-

+

-

PIC

Figure 2: overlap of two equal-sized windows
-

And slightly more complex in the case of overlapping unequal sized windows: +

And slightly more complex in the case of overlapping unequal sized windows:

-

+

-

PIC

Figure 3: overlap of a long and a short window
-

In the unequal-sized window case, the window shape of the long window must be modified for +

In the unequal-sized window case, the window shape of the long window must be modified for seamless lapping as above. It is possible to correctly infer window shape to be applied to the current window from knowing the sizes of the current, previous and next window. It is legal for a decoder to use this method. However, in the case of a long window (short windows require no @@ -620,7 +616,7 @@ not strictly necessary for function, this minor redundancy allows a packet to be the point of lapping entirely independently of any other packet, allowing easier abstraction of decode layers as well as allowing a greater level of easy parallelism in encode and decode. -

A description of valid window functions for use with an inverse MDCT can be found in [

A description of valid window functions for use with an inverse MDCT can be found in [1]. Vorbis windows all use the slope function

@@ -630,14 +626,14 @@ src="Vorbis_I_spec0x.png" alt="y = sin (.5 * π sin2((x + .5)∕n * & " class="math-display" >
-

-

+

floor decode Each floor is encoded/decoded in channel order, however each floor belongs to a ’submap’ that specifies which floor configuration to use. All floors are decoded before residue decode begins. -

residue decode Although the number of residue vectors equals the number of channels, channel coupling may @@ -645,10 +641,10 @@ mean that the raw residue vectors extracted during decode do not map directly to 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 +

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 @@ -656,17 +652,17 @@ A detailed discussion of stereo in the Vorbis codec can be found in the document href="stereo.html" >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 +

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 +

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 @@ -674,20 +670,20 @@ generate the output curve when the floor data is decoded from the raw packet, or 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 +

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 +

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

However, floor vector values can span ~140dB (~24 bits unsigned), and the audio spectrum vector should represent a minimum of 120dB (

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 +

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 @@ -725,12 +721,12 @@ window such that the 3/4 point of the previous window is aligned with the 1/4 po 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 @@ -741,14 +737,17 @@ overlapped portions. When overlapping a short and long window, much of the retur 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 +class="cmtt-8">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). @@ -759,20 +758,20 @@ PCM output offset is ’0’ (as no data has been returned yet).

2. Bitpacking Convention

-

+

2.1. Overview

-

The Vorbis codec uses relatively unstructured raw packets containing arbitrary-width binary +

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-by-one 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. +

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 @@ -780,45 +779,45 @@ assumed to be eight bits (not necessarily because of the processor but because o 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 +

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 +

A byte has a well-defined ’least significant’ bit (LSb), which is the only bit set when the byte is storing the two’s complement integer value +1. A byte’s ’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 +

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 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 +

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 +

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 +

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 @@ -831,19 +830,19 @@ the next bit into the bit position 0 of that byte. Decoding follows the same pro 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 +

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, two’s 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: -

+

Code the 4 bit integer value ’12’ [b1100] into an empty bytestream. Bytestream result: +

1       |
2       V
3  
3
4        7 6 5 1 0
5  bytebyte 0 [0 0  <-
6  bytebyte 1 [  ]
7  bytebyte 2 [   ]
8  bytebyte 3 [        ...
10  bytebyte n [  == 1 byte
11  
-

Continue by coding the 3 bit integer value ’-1’ [b111]: -

+class="cmr-6">11 +

Continue by coding the 3 bit integer value ’-1’ [b111]: +

1       |
2       V
3  
3
4        7 6 5 1 0
5  bytebyte 0 [0 1  <-
6  bytebyte 1 [      ] -
 ]
7  bytebyte 2 [      ]
 ] +
8  bytebyte 3 [        ...
10  bytebyte n [  length == 1 byte
-

Continue by coding the 7 bit integer value ’17’ [b0010001]: -

+

Continue by coding the 7 bit integer value ’17’ [b0010001]: +

1       |
2       V
3  
3
4        7 6 5 1 0
5  bytebyte 0 [1 1 0 0]
6  bytebyte 1 [0 0  <-
7  bytebyte 2 [  ]
8  bytebyte 3 [        ...
10  bytebyte n [        bit cursor == 6
-

Continue by coding the 13 bit integer value ’6969’ [b110 11001110 01]: -

+

Continue by coding the 13 bit integer value ’6969’ [b110 11001110 01]: +

1       |
2       V
3  
3
4        7 6 5 1 0
5  bytebyte 0 [1 1 0 0]
6  bytebyte 1 [0 1 0]
7  bytebyte 2 [1 1 1 0]
8  bytebyte 3 [0 0       ...
10  bytebyte n [  == 4 bytes
11  
+class="cmr-6">11 -

+

2.1.7. decoding example
-

Reading from the beginning of the bytestream encoded in the above example: -

+

Reading from the beginning of the bytestream encoded in the above example: +

1       |
2       V
3  
3
4        7 6 5 1 0
5  bytebyte 0 [1 1 1 0 0]  <- -
 <-
6  bytebyte 1 [0 1 0 1 0 0 0]
 0] +
7  bytebyte 2 [1 1 1 0]
8  bytebyte 3 [0 0 == 4 bytes
9  
-

We read two, two-bit integer fields, resulting in the returned numbers ’b00’ and ’b11’. Two things +class="cmr-6">9 +

We read two, two-bit integer fields, resulting in the returned numbers ’b00’ and ’b11’. Two things are worth noting here:

-

+

2.1.8. end-of-packet alignment
-

The typical use of bitpacking is to produce many independent byte-aligned packets which are +

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, +

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. +

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 @@ -1753,10 +1672,10 @@ insufficient remaining data to fulfill the desired read size. Vorbis uses trunca 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 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’ @@ -1769,28 +1688,28 @@ shall also fail with ’end-of-packet’.

3. Probability Model and Codebooks

-

+

3.1. Overview

-

Unlike practically every other mainstream audio codec, Vorbis has no statically configured +

Unlike practically every other mainstream audio codec, Vorbis has no statically configured probability model, instead packing all entropy decoding configuration, VQ and Huffman, into the bitstream itself in the third header, the codec setup header. This packed configuration consists of multiple ’codebooks’, each containing a specific Huffman-equivalent representation for decoding compressed codewords as well as an optional lookup table of output vector values to which a decoded Huffman value is applied as an offset, generating the final decoded output corresponding to a given compressed codeword. -

+

3.1.1. Bitwise operation
-

The codebook mechanism is built on top of the vorbis bitpacker. Both the codebooks themselves +

The codebook mechanism is built on top of the vorbis bitpacker. Both the codebooks themselves and the codewords they decode are unrolled from a packet as a series of arbitrary-width values read from the stream according to section 2, “Bitpacking Convention”. -

+

3.2. Packed codebook format

-

For purposes of the examples below, we assume that the storage system’s native byte width is +

For purposes of the examples below, we assume that the storage system’s native byte width is eight bits. This is not universally true; see section 2, “Bitpacking Convention” for discussion @@ -1798,16 +1717,15 @@ relating to non-eight-bit bytes. -

+

3.2.1. codebook decode
-

A codebook begins with a 24 bit sync pattern, 0x564342: -

+

A codebook begins with a 24 bit sync pattern, 0x564342: +

1  bytebyte 0: [ 0 ] (0x42)
2  bytebyte 1: [ 0 ] (0x43)
3  bytebyte 2: [ 0 1 0 ] (0x56)
-

16 bit [codebook_dimensions] and 24 bit [codebook_entries] fields: -

+

16 bit [codebook_dimensions] and 24 bit [codebook_entries] fields: +

1  
1
2  bytebyte 3: [ X X ]
3  bytebyte 4: [ X X X X ] [codebook_dimensions] [codebook_dimensions] (16 bit unsigned)
4   -
4
5  bytebyte 5: [ X X X X X ]
 ] +
6  bytebyte 6: [ X X ]
7  bytebyte 7: [ X X X X ] [codebook_entries] [codebook_entries] (24 bit unsigned)
8  
-

Next is the 8 +

Next is the [ordered] bit flag: -

+

1  
1
2  bytebyte 8: [  [ordered] (1 bit)
3  
-

Each entry, numbering a total of [codebook_entries], is assigned a codeword length. +class="cmr-6">3 +

Each entry, numbering a total of [codebook_entries], is assigned a codeword length. We now read the list of codeword lengths and store these lengths in the array [codebook_codeword_lengths]. Decode of lengths is according to whether the [codebook_codeword_lengths]. Decode of lengths is according to whether the [ordered] flag is set or unset.

-

After all codeword lengths have been decoded, the decoder reads the vector lookup table. Vorbis +

After all codeword lengths have been decoded, the decoder reads the vector lookup table. Vorbis I supports three lookup types:

1.
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: +

The lookup table type is read as a four bit unsigned integer:

1    1) [codebook_lookup_type] [codebook_lookup_type] = read four as an unsigned integer
-

Codebook decode precedes according to [codebook_lookup_type]: +

Codebook decode precedes according to [codebook_lookup_type]:

-

An ’end of packet’ during any read operation in the above steps is considered an error condition +

An ’end of packet’ during any read operation in the above steps is considered an error condition rendering the stream undecodable. -

Huffman decision tree representation The [codebook_codeword_lengths] array and [codebook_entries] value uniquely define the +class="cmtt-12">[codebook_codeword_lengths] array and [codebook_entries] value uniquely define the Huffman decision tree used for entropy decoding. -

Briefly, each used codebook entry (recall that length-unordered codebooks support unused +

Briefly, each used codebook entry (recall that length-unordered codebooks support unused codeword entries) is assigned, in order, the lowest valued unused binary Huffman codeword possible. Assume the following codeword length list: -

+

1  entryentry 0: length 2
2  entryentry 1: length 4
3  entryentry 2: length 4
4  entryentry 3: length 4
5  entryentry 4: length 4
6  entryentry 5: length 2 -
 2
7  entryentry 6: length 3
8  entryentry 7: length 3
-

Assigning codewords in order (lowest possible value of the appropriate length to highest) results +

Assigning codewords in order (lowest possible value of the appropriate length to highest) results in the following codeword list: -

+

1  entryentry 0: length 2 codeword 00
2  entryentry 1: length 4 codeword 0100
3  entryentry 2: length 4 codeword 0101
4  entryentry 3: length 4 0110
5  entryentry 4: length 4 codeword 0111
6  entryentry 5: length 2 codeword 10
7  entryentry 6: length 3 codeword 110
8  entryentry 7: length 3 codeword 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 +

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:

-

+

-

PIC

Figure 4: huffman tree illustration
-

As we assign codewords in order, we see that each choice constructs a new leaf in the leftmost +

As we assign codewords in order, we see that each choice constructs a new leaf in the leftmost possible position. -

Note that it’s possible to underspecify or overspecify a Huffman tree via the length list. +

Note that it’s possible to underspecify or overspecify a Huffman tree via the length list. In the above example, if codeword seven were eliminated, it’s clear that the tree is unfinished:

-

+

-

PIC

Figure 5: underspecified huffman tree illustration
-

Similarly, in the original codebook, it’s clear that the tree is fully populated and a ninth +

Similarly, in the original codebook, it’s clear that the tree is fully populated and a ninth codeword is impossible. Both underspecified and overspecified trees are an error condition rendering the stream undecodable. -

Codebook entries marked ’unused’ are simply skipped in the assigning process. They have no +

Codebook entries marked ’unused’ are simply skipped in the assigning process. They have no codeword and do not appear in the decision tree, thus it’s impossible for any bit pattern read from the stream to decode to that entry number. -

Errata 20150226: Single entry codebooks A ’single-entry codebook’ is a codebook with one active codeword entry. A single-entry codebook @@ -2926,43 +2766,48 @@ valid tree structure. -

In r14811 of the libvorbis reference implementation, Xiph added an additional check to the +

In r14811 of the libvorbis reference implementation, Xiph added an additional check to the codebook implementation to reject underpopulated Huffman trees. This change led to the discovery of single-entry books used ’in the wild’ when the new, stricter checks rejected a number of apparently working streams. -

In order to minimize breakage of deployed (if technically erroneous) streams, r16073 of the +

In order to minimize breakage of deployed (if technically erroneous) streams, r16073 of the reference implementation explicitly special-cased single-entry codebooks to tolerate the single-entry case. Commit r16073 also added the following to the specification: -

Take special care that a codebook with a single used entry is handled properly; it consists of a +

Take special care that a codebook with a single used entry is handled properly; it consists of a single codework of zero bits and reading a value out of such a codebook always returns the single used value and sinks zero bits. ” -

The intent was to clarify the spec and codify current practice. However, this addition is +

The intent was to clarify the spec and codify current practice. However, this addition is erroneously at odds with the intent of preserving usability of existing streams using single-entry codebooks, disagrees with the code changes that reinstated decoding, and does not address how single-entry codebooks should be encoded. -

As such, the above addition made in r16037 is struck from the specification and replaced by the +

As such, the above addition made in r16037 is struck from the specification and replaced by the following: -

+

-

It is possible to declare a Vorbis codebook containing a single codework +

It is possible to declare a Vorbis codebook containing a single codework entry. A single-entry codebook may be either a fully populated codebook with [codebook_entries] set to 1, or a sparse codebook marking only one entry +class="cmtt-12">[codebook_entries] set to 1, or a sparse codebook marking only one entry used. Note that it is not possible to also encode a [codeword_length] of zero +class="cmtt-12">[codeword_length] of zero for the single used codeword, as the unsigned value written to the stream is [codeword_length]-1. Instead, encoder implementations should indicate a +class="cmtt-12">[codeword_length]-1. Instead, encoder implementations should indicate a [codeword_length] of 1 and ’write’ the codeword to a stream during audio +class="cmtt-12">[codeword_length] of 1 and ’write’ the codeword to a stream during audio encoding by writing a single zero bit. -

Decoder implementations shall reject a codebook if it contains only one used +

Decoder implementations shall reject a codebook if it contains only one used entry and the encoded [codeword_length] of that entry is not 1. ’Reading’ a +class="cmtt-12">[codeword_length] of that entry is not 1. ’Reading’ a value from single-entry codebook always returns the single used codeword value and sinks one bit. Decoders should tolerate that the bit read from the stream be ’1’ instead of ’0’; both values shall return the single used codeword.

-

VQ lookup table vector representation Unpacking the VQ lookup table vectors relies on the following values: @@ -2972,61 +2817,56 @@ Unpacking the VQ lookup table vectors relies on the following values:

1  thethe [codebook\_multiplicands] array
2  [codebook\_minimum\_value]
[codebook\_minimum\_value]
3  [codebook\_delta\_value]
[codebook\_delta\_value]
4  [codebook\_sequence\_p] +class="cmtt-8">[codebook\_sequence\_p]
5  [codebook\_lookup\_type]
[codebook\_lookup\_type]
6  [codebook\_entries]
[codebook\_entries]
7  [codebook\_dimensions]
[codebook\_dimensions]
8  [codebook\_lookup\_values]
-

Decoding (unpacking) a specific vector in the vector lookup table proceeds according to +class="cmtt-8">[codebook\_lookup\_values] +

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 +class="cmtt-12">[codebook_lookup_type]. The unpacked vector values are what a codebook would return during audio packet decode in a VQ context. -

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 +class="cmtt-12">[codebook_multiplicands] as follows ([value_vector] is the output vector representing the vector of values for entry number [lookup_offset] in this +class="cmtt-12">[lookup_offset] in this codebook): -

+

1    1) [last] = 0;
2    2) [index_divisor] [index_divisor] = 1;
3    3) iterate [i] the range 0 ... [codebook_dimensions]-1 [codebook_dimensions]-1 (once for each the value vector) { -
{
4  
4 +
5        4) [multiplicand_offset] [multiplicand_offset] = ( [lookup_offset] [lookup_offset] divided by [index_divisor] [index_divisor] using integer
       division ) integer modulo [codebook_lookup_values]
 [codebook_lookup_values]
7  
7
8        5) vector [value_vector] [value_vector] element [i] = @@ -3143,14 +2977,14 @@ class="cmtt-8">       ( [codebook_multiplicands] [codebook_multiplicands] array element number [multiplicand_offset] [multiplicand_offset] ) *
       [codebook_delta_value] [codebook_delta_value] + [codebook_minimum_value] [codebook_minimum_value] + [last];
11   +class="cmr-6">11
12       6) if ( [codebook_sequence_p] [codebook_sequence_p] is set ) set [last] = vector [value_vector] [value_vector] element [i]
13   +class="cmr-6">13
14       7) [index_divisor] [index_divisor] = [index_divisor] [index_divisor] * [codebook_lookup_values]
 [codebook_lookup_values]
15  
15
16       }
17  
17
18    8) vector calculation completed.
-

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 +class="cmtt-12">[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 +class="cmtt-12">[codebook_multiplicands] as follows ([value_vector] is the output vector representing the vector of values for entry number +class="cmtt-12">[value_vector] is the output vector representing the vector of values for entry number [lookup_offset] in this codebook): -

+class="cmtt-12">[lookup_offset] in this codebook): +

1    1) [last] = 0;
2    2) [multiplicand_offset] [multiplicand_offset] = [lookup_offset] [lookup_offset] * [codebook_dimensions] +class="cmtt-8"> [codebook_dimensions]
3    3) iterate [i] the range 0 ... [codebook_dimensions]-1 [codebook_dimensions]-1 (once for each vector) {
4   +class="cmr-6">4
5       4) vector [value_vector] [value_vector] element [i] =
       ( [codebook_multiplicands] [codebook_multiplicands] array element number [multiplicand_offset] [multiplicand_offset] ) *
       [codebook_delta_value] [codebook_delta_value] + [codebook_minimum_value] [codebook_minimum_value] + [last];
8   +class="cmr-6">8
9       5) if ( [codebook_sequence_p] [codebook_sequence_p] is set ) set [last] = vector [value_vector] [value_vector] element [i]
10   +class="cmr-6">10
11       6) increment [multiplicand_offset]
 [multiplicand_offset]
12  
12
13       }
14  
14
15    7) vector calculation completed.
-

+

3.3. Use of the codebook abstraction

-

The decoder uses the codebook abstraction much as it does the bit-unpacking convention; a +

The decoder uses the codebook abstraction much as it does the bit-unpacking convention; a specific codebook reads a codeword from the bitstream, decoding it into an entry number, and then returns that entry number to the decoder (when used in a scalar entropy coding 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 +

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 +

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 @@ -3479,9 +3295,9 @@ branch (right in the above examples). Walking the tree finishes when the decode 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 +

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 +

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. @@ -3492,10 +3308,10 @@ offset.

4. Codec Setup and Packet Decode

-

+

4.1. Overview

-

This document serves as the top-level reference document for the bit-by-bit decode specification +

This document serves as the top-level reference document for the bit-by-bit decode specification of Vorbis I. This document assumes a high-level understanding of the Vorbis decode process, which is provided in section 1, “section 2, “Bitpacking Convention” covers reading and writing bit fields from and to bitstream packets. -

+

4.2. Header decode and decode setup

-

A Vorbis bitstream begins with three header packets. The header packets are, in order, the +

A Vorbis bitstream begins with three header packets. The header packets are, in order, the identification header, the comments header, and the setup header. All are required for decode compliance. An end-of-packet condition during decoding the first or third header packet renders the stream undecodable. End-of-packet decoding the comment header is a non-fatal error condition. -

+

4.2.1. Common header decode
-

Each header packet begins with the same header fields. -

+

Each header packet begins with the same header fields. +

1    1) [packet_type] [packet_type] : 8 bit value
2    2) 0x76, 0x6f, ’v’,’o’,’r’,’b& class="cmtt-8"> as six octets
-

Decode continues according to packet type; the identification header is type 1, the comment +

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 +

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   1) [vorbis_version] [vorbis_version] = read 32 unsigned integer
2   2) [audio_channels] [audio_channels] = read 8 unsigned
3   3) [audio_sample_rate] [audio_sample_rate] = read 32 unsigned integer
4   4) [bitrate_maximum] [bitrate_maximum] = read 32 integer
5   5) [bitrate_nominal] [bitrate_nominal] = read 32 signed integer
6   6) [bitrate_minimum] [bitrate_minimum] = read 32 integer
7   7) [blocksize_0] [blocksize_0] = 2 exponent unsigned integer)
8   8) [blocksize_1] [blocksize_1] = 2 exponent integer)
9   9) [framing_flag] [framing_flag] = read one bit
-

[vorbis_version] is to read ’0’ in order to be compatible with this document. Both +

[vorbis_version] is to read ’0’ in order to be compatible with this document. Both [audio_channels] and [audio_sample_rate] must read greater than zero. Allowed final +class="cmtt-12">[audio_channels] and [audio_sample_rate] must read greater than zero. Allowed final blocksize values are 64, 128, 256, 512, 1024, 2048, 4096 and 8192 in Vorbis I. [blocksize_0] +class="cmtt-12">[blocksize_0] must be less than or equal to [blocksize_1]. The framing bit must be nonzero. Failure to meet +class="cmtt-12">[blocksize_1]. The framing bit must be nonzero. Failure to meet any of these conditions renders a stream undecodable. -

The bitrate fields above are used only as hints. The nominal bitrate field especially may be +

The bitrate fields above are used only as hints. The nominal bitrate field especially may be considerably off in purely VBR streams. The fields are meaningful only when greater than zero.