1 % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
2 %!TEX root = Vorbis_I_spec.tex
4 \section{Probability Model and Codebooks} \label{vorbis:spec:codebook}
8 Unlike practically every other mainstream audio codec, Vorbis has no
9 statically configured probability model, instead packing all entropy
10 decoding configuration, VQ and Huffman, into the bitstream itself in
11 the third header, the codec setup header. This packed configuration
12 consists of multiple 'codebooks', each containing a specific
13 Huffman-equivalent representation for decoding compressed codewords as
14 well as an optional lookup table of output vector values to which a
15 decoded Huffman value is applied as an offset, generating the final
16 decoded output corresponding to a given compressed codeword.
18 \subsubsection{Bitwise operation}
19 The codebook mechanism is built on top of the vorbis bitpacker. Both
20 the codebooks themselves and the codewords they decode are unrolled
21 from a packet as a series of arbitrary-width values read from the
22 stream according to \xref{vorbis:spec:bitpacking}.
27 \subsection{Packed codebook format}
29 For purposes of the examples below, we assume that the storage
30 system's native byte width is eight bits. This is not universally
31 true; see \xref{vorbis:spec:bitpacking} for discussion
32 relating to non-eight-bit bytes.
34 \subsubsection{codebook decode}
36 A codebook begins with a 24 bit sync pattern, 0x564342:
38 \begin{Verbatim}[commandchars=\\\{\}]
39 byte 0: [ 0 1 0 0 0 0 1 0 ] (0x42)
40 byte 1: [ 0 1 0 0 0 0 1 1 ] (0x43)
41 byte 2: [ 0 1 0 1 0 1 1 0 ] (0x56)
44 16 bit \varname{[codebook_dimensions]} and 24 bit \varname{[codebook_entries]} fields:
46 \begin{Verbatim}[commandchars=\\\{\}]
48 byte 3: [ X X X X X X X X ]
49 byte 4: [ X X X X X X X X ] [codebook_dimensions] (16 bit unsigned)
51 byte 5: [ X X X X X X X X ]
52 byte 6: [ X X X X X X X X ]
53 byte 7: [ X X X X X X X X ] [codebook_entries] (24 bit unsigned)
57 Next is the \varname{[ordered]} bit flag:
59 \begin{Verbatim}[commandchars=\\\{\}]
61 byte 8: [ X ] [ordered] (1 bit)
65 Each entry, numbering a
66 total of \varname{[codebook_entries]}, is assigned a codeword length.
67 We now read the list of codeword lengths and store these lengths in
68 the array \varname{[codebook_codeword_lengths]}. Decode of lengths is
69 according to whether the \varname{[ordered]} flag is set or unset.
73 If the \varname{[ordered]} flag is unset, the codeword list is not
74 length ordered and the decoder needs to read each codeword length
77 The decoder first reads one additional bit flag, the
78 \varname{[sparse]} flag. This flag determines whether or not the
79 codebook contains unused entries that are not to be included in the
82 \begin{Verbatim}[commandchars=\\\{\}]
83 byte 8: [ X 1 ] [sparse] flag (1 bit)
86 The decoder now performs for each of the \varname{[codebook_entries]}
89 \begin{Verbatim}[commandchars=\\\{\}]
91 1) if([sparse] is set) \{
93 2) [flag] = read one bit;
94 3) if([flag] is set) \{
96 4) [length] = read a five bit unsigned integer;
97 5) codeword length for this entry is [length]+1;
101 6) this entry is unused. mark it as such.
105 \} else the sparse flag is not set \{
107 7) [length] = read a five bit unsigned integer;
108 8) the codeword length for this entry is [length]+1;
115 If the \varname{[ordered]} flag is set, the codeword list for this
116 codebook is encoded in ascending length order. Rather than reading
117 a length for every codeword, the encoder reads the number of
118 codewords per length. That is, beginning at entry zero:
120 \begin{Verbatim}[commandchars=\\\{\}]
121 1) [current_entry] = 0;
122 2) [current_length] = read a five bit unsigned integer and add 1;
123 3) [number] = read \link{vorbis:spec:ilog}{ilog}([codebook_entries] - [current_entry]) bits as an unsigned integer
124 4) set the entries [current_entry] through [current_entry]+[number]-1, inclusive,
125 of the [codebook_codeword_lengths] array to [current_length]
126 5) set [current_entry] to [number] + [current_entry]
127 6) increment [current_length] by 1
128 7) if [current_entry] is greater than [codebook_entries] ERROR CONDITION;
129 the decoder will not be able to read this stream.
130 8) if [current_entry] is less than [codebook_entries], repeat process starting at 3)
136 After all codeword lengths have been decoded, the decoder reads the
137 vector lookup table. Vorbis I supports three lookup types:
142 Implicitly populated value mapping (lattice VQ)
144 Explicitly populated value mapping (tessellated or 'foam'
149 The lookup table type is read as a four bit unsigned integer:
150 \begin{Verbatim}[commandchars=\\\{\}]
151 1) [codebook_lookup_type] = read four bits as an unsigned integer
154 Codebook decode precedes according to \varname{[codebook_lookup_type]}:
157 Lookup type zero indicates no lookup to be read. Proceed past
160 Lookup types one and two are similar, differing only in the
161 number of lookup values to be read. Lookup type one reads a list of
162 values that are permuted in a set pattern to build a list of vectors,
163 each vector of order \varname{[codebook_dimensions]} scalars. Lookup
164 type two builds the same vector list, but reads each scalar for each
165 vector explicitly, rather than building vectors from a smaller list of
166 possible scalar values. Lookup decode proceeds as follows:
168 \begin{Verbatim}[commandchars=\\\{\}]
169 1) [codebook_minimum_value] = \link{vorbis:spec:float32:unpack}{float32_unpack}( read 32 bits as an unsigned integer)
170 2) [codebook_delta_value] = \link{vorbis:spec:float32:unpack}{float32_unpack}( read 32 bits as an unsigned integer)
171 3) [codebook_value_bits] = read 4 bits as an unsigned integer and add 1
172 4) [codebook_sequence_p] = read 1 bit as a boolean flag
174 if ( [codebook_lookup_type] is 1 ) \{
176 5) [codebook_lookup_values] = \link{vorbis:spec:lookup1:values}{lookup1_values}(\varname{[codebook_entries]}, \varname{[codebook_dimensions]} )
180 6) [codebook_lookup_values] = \varname{[codebook_entries]} * \varname{[codebook_dimensions]}
184 7) read a total of [codebook_lookup_values] unsigned integers of [codebook_value_bits] each;
185 store these in order in the array [codebook_multiplicands]
188 A \varname{[codebook_lookup_type]} of greater than two is reserved
189 and indicates a stream that is not decodable by the specification in this
195 An 'end of packet' during any read operation in the above steps is
196 considered an error condition rendering the stream undecodable.
198 \paragraph{Huffman decision tree representation}
200 The \varname{[codebook_codeword_lengths]} array and
201 \varname{[codebook_entries]} value uniquely define the Huffman decision
202 tree used for entropy decoding.
204 Briefly, each used codebook entry (recall that length-unordered
205 codebooks support unused codeword entries) is assigned, in order, the
206 lowest valued unused binary Huffman codeword possible. Assume the
207 following codeword length list:
209 \begin{Verbatim}[commandchars=\\\{\}]
220 Assigning codewords in order (lowest possible value of the appropriate
221 length to highest) results in the following codeword list:
223 \begin{Verbatim}[commandchars=\\\{\}]
224 entry 0: length 2 codeword 00
225 entry 1: length 4 codeword 0100
226 entry 2: length 4 codeword 0101
227 entry 3: length 4 codeword 0110
228 entry 4: length 4 codeword 0111
229 entry 5: length 2 codeword 10
230 entry 6: length 3 codeword 110
231 entry 7: length 3 codeword 111
236 Unlike most binary numerical values in this document, we
237 intend the above codewords to be read and used bit by bit from left to
238 right, thus the codeword '001' is the bit string 'zero, zero, one'.
239 When determining 'lowest possible value' in the assignment definition
240 above, the leftmost bit is the MSb.
243 It is clear that the codeword length list represents a Huffman
244 decision tree with the entry numbers equivalent to the leaves numbered
248 \includegraphics[width=10cm]{hufftree}
249 \captionof{figure}{huffman tree illustration}
253 As we assign codewords in order, we see that each choice constructs a
254 new leaf in the leftmost possible position.
256 Note that it's possible to underspecify or overspecify a Huffman tree
257 via the length list. In the above example, if codeword seven were
258 eliminated, it's clear that the tree is unfinished:
261 \includegraphics[width=10cm]{hufftree-under}
262 \captionof{figure}{underspecified huffman tree illustration}
266 Similarly, in the original codebook, it's clear that the tree is fully
267 populated and a ninth codeword is impossible. Both underspecified and
268 overspecified trees are an error condition rendering the stream
271 Codebook entries marked 'unused' are simply skipped in the assigning
272 process. They have no codeword and do not appear in the decision
273 tree, thus it's impossible for any bit pattern read from the stream to
274 decode to that entry number.
278 \paragraph{VQ lookup table vector representation}
280 Unpacking the VQ lookup table vectors relies on the following values:
281 \begin{programlisting}
282 the [codebook_multiplicands] array
283 [codebook_minimum_value]
284 [codebook_delta_value]
285 [codebook_sequence_p]
286 [codebook_lookup_type]
288 [codebook_dimensions]
289 [codebook_lookup_values]
294 Decoding (unpacking) a specific vector in the vector lookup table
295 proceeds according to \varname{[codebook_lookup_type]}. The unpacked
296 vector values are what a codebook would return during audio packet
297 decode in a VQ context.
299 \paragraph{Vector value decode: Lookup type 1}
301 Lookup type one specifies a lattice VQ lookup table built
302 algorithmically from a list of scalar values. Calculate (unpack) the
303 final values of a codebook entry vector from the entries in
304 \varname{[codebook_multiplicands]} as follows (\varname{[value_vector]}
305 is the output vector representing the vector of values for entry number
306 \varname{[lookup_offset]} in this codebook):
308 \begin{Verbatim}[commandchars=\\\{\}]
310 2) [index_divisor] = 1;
311 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) \{
313 4) [multiplicand_offset] = ( [lookup_offset] divided by [index_divisor] using integer
314 division ) integer modulo [codebook_lookup_values]
316 5) vector [value_vector] element [i] =
317 ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
318 [codebook_delta_value] + [codebook_minimum_value] + [last];
320 6) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
322 7) [index_divisor] = [index_divisor] * [codebook_lookup_values]
326 8) vector calculation completed.
331 \paragraph{Vector value decode: Lookup type 2}
333 Lookup type two specifies a VQ lookup table in which each scalar in
334 each vector is explicitly set by the \varname{[codebook_multiplicands]}
335 array in a one-to-one mapping. Calculate [unpack] the
336 final values of a codebook entry vector from the entries in
337 \varname{[codebook_multiplicands]} as follows (\varname{[value_vector]}
338 is the output vector representing the vector of values for entry number
339 \varname{[lookup_offset]} in this codebook):
341 \begin{Verbatim}[commandchars=\\\{\}]
343 2) [multiplicand_offset] = [lookup_offset] * [codebook_dimensions]
344 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) \{
346 4) vector [value_vector] element [i] =
347 ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
348 [codebook_delta_value] + [codebook_minimum_value] + [last];
350 5) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
352 6) increment [multiplicand_offset]
356 7) vector calculation completed.
367 \subsection{Use of the codebook abstraction}
369 The decoder uses the codebook abstraction much as it does the
370 bit-unpacking convention; a specific codebook reads a
371 codeword from the bitstream, decoding it into an entry number, and then
372 returns that entry number to the decoder (when used in a scalar
373 entropy coding context), or uses that entry number as an offset into
374 the VQ lookup table, returning a vector of values (when used in a context
375 desiring a VQ value). Scalar or VQ context is always explicit; any call
376 to the codebook mechanism requests either a scalar entry number or a
379 Note that VQ lookup type zero indicates that there is no lookup table;
380 requesting decode using a codebook of lookup type 0 in any context
381 expecting a vector return value (even in a case where a vector of
382 dimension one) is forbidden. If decoder setup or decode requests such
383 an action, that is an error condition rendering the packet
386 Using a codebook to read from the packet bitstream consists first of
387 reading and decoding the next codeword in the bitstream. The decoder
388 reads bits until the accumulated bits match a codeword in the
389 codebook. This process can be though of as logically walking the
390 Huffman decode tree by reading one bit at a time from the bitstream,
391 and using the bit as a decision boolean to take the 0 branch (left in
392 the above examples) or the 1 branch (right in the above examples).
393 Walking the tree finishes when the decode process hits a leaf in the
394 decision tree; the result is the entry number corresponding to that
395 leaf. Reading past the end of a packet propagates the 'end-of-stream'
396 condition to the decoder.
398 When used in a scalar context, the resulting codeword entry is the
399 desired return value.
401 When used in a VQ context, the codeword entry number is used as an
402 offset into the VQ lookup table. The value returned to the decoder is
403 the vector of scalars corresponding to this offset.