
Chris@1: % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Chris@1: %!TEX root = Vorbis_I_spec.tex
Chris@1: % $Id$
Chris@1: \section{Probability Model and Codebooks} \label{vorbis:spec:codebook}
Chris@1: 
Chris@1: \subsection{Overview}
Chris@1: 
Chris@1: Unlike practically every other mainstream audio codec, Vorbis has no
Chris@1: statically configured probability model, instead packing all entropy
Chris@1: decoding configuration, VQ and Huffman, into the bitstream itself in
Chris@1: the third header, the codec setup header.  This packed configuration
Chris@1: consists of multiple 'codebooks', each containing a specific
Chris@1: Huffman-equivalent representation for decoding compressed codewords as
Chris@1: well as an optional lookup table of output vector values to which a
Chris@1: decoded Huffman value is applied as an offset, generating the final
Chris@1: decoded output corresponding to a given compressed codeword.
Chris@1: 
Chris@1: \subsubsection{Bitwise operation}
Chris@1: The codebook mechanism is built on top of the vorbis bitpacker. Both
Chris@1: the codebooks themselves and the codewords they decode are unrolled
Chris@1: from a packet as a series of arbitrary-width values read from the
Chris@1: stream according to \xref{vorbis:spec:bitpacking}.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsection{Packed codebook format}
Chris@1: 
Chris@1: For purposes of the examples below, we assume that the storage
Chris@1: system's native byte width is eight bits.  This is not universally
Chris@1: true; see \xref{vorbis:spec:bitpacking} for discussion
Chris@1: relating to non-eight-bit bytes.
Chris@1: 
Chris@1: \subsubsection{codebook decode}
Chris@1: 
Chris@1: A codebook begins with a 24 bit sync pattern, 0x564342:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: byte 0: [ 0 1 0 0 0 0 1 0 ] (0x42)
Chris@1: byte 1: [ 0 1 0 0 0 0 1 1 ] (0x43)
Chris@1: byte 2: [ 0 1 0 1 0 1 1 0 ] (0x56)
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: 16 bit \varname{[codebook\_dimensions]} and 24 bit \varname{[codebook\_entries]} fields:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: 
Chris@1: byte 3: [ X X X X X X X X ]
Chris@1: byte 4: [ X X X X X X X X ] [codebook\_dimensions] (16 bit unsigned)
Chris@1: 
Chris@1: byte 5: [ X X X X X X X X ]
Chris@1: byte 6: [ X X X X X X X X ]
Chris@1: byte 7: [ X X X X X X X X ] [codebook\_entries] (24 bit unsigned)
Chris@1: 
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: Next is the \varname{[ordered]} bit flag:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: 
Chris@1: byte 8: [               X ] [ordered] (1 bit)
Chris@1: 
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: Each entry, numbering a
Chris@1: total of \varname{[codebook\_entries]}, is assigned a codeword length.
Chris@1: We now read the list of codeword lengths and store these lengths in
Chris@1: the array \varname{[codebook\_codeword\_lengths]}. Decode of lengths is
Chris@1: according to whether the \varname{[ordered]} flag is set or unset.
Chris@1: 
Chris@1: \begin{itemize}
Chris@1: \item
Chris@1:   If the \varname{[ordered]} flag is unset, the codeword list is not
Chris@1:   length ordered and the decoder needs to read each codeword length
Chris@1:   one-by-one.
Chris@1: 
Chris@1:   The decoder first reads one additional bit flag, the
Chris@1:   \varname{[sparse]} flag.  This flag determines whether or not the
Chris@1:   codebook contains unused entries that are not to be included in the
Chris@1:   codeword decode tree:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: byte 8: [             X 1 ] [sparse] flag (1 bit)
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1:   The decoder now performs for each of the \varname{[codebook\_entries]}
Chris@1:   codebook entries:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: 
Chris@1:   1) if([sparse] is set) \{
Chris@1: 
Chris@1:          2) [flag] = read one bit;
Chris@1:          3) if([flag] is set) \{
Chris@1: 
Chris@1:               4) [length] = read a five bit unsigned integer;
Chris@1:               5) codeword length for this entry is [length]+1;
Chris@1: 
Chris@1:             \} else \{
Chris@1: 
Chris@1:               6) this entry is unused.  mark it as such.
Chris@1: 
Chris@1:             \}
Chris@1: 
Chris@1:      \} else the sparse flag is not set \{
Chris@1: 
Chris@1:         7) [length] = read a five bit unsigned integer;
Chris@1:         8) the codeword length for this entry is [length]+1;
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: \item
Chris@1:   If the \varname{[ordered]} flag is set, the codeword list for this
Chris@1:   codebook is encoded in ascending length order.  Rather than reading
Chris@1:   a length for every codeword, the encoder reads the number of
Chris@1:   codewords per length.  That is, beginning at entry zero:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [current\_entry] = 0;
Chris@1:   2) [current\_length] = read a five bit unsigned integer and add 1;
Chris@1:   3) [number] = read \link{vorbis:spec:ilog}{ilog}([codebook\_entries] - [current\_entry]) bits as an unsigned integer
Chris@1:   4) set the entries [current\_entry] through [current\_entry]+[number]-1, inclusive,
Chris@1:     of the [codebook\_codeword\_lengths] array to [current\_length]
Chris@1:   5) set [current\_entry] to [number] + [current\_entry]
Chris@1:   6) increment [current\_length] by 1
Chris@1:   7) if [current\_entry] is greater than [codebook\_entries] ERROR CONDITION;
Chris@1:     the decoder will not be able to read this stream.
Chris@1:   8) if [current\_entry] is less than [codebook\_entries], repeat process starting at 3)
Chris@1:   9) done.
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: \end{itemize}
Chris@1: 
Chris@1: After all codeword lengths have been decoded, the decoder reads the
Chris@1: vector lookup table.  Vorbis I supports three lookup types:
Chris@1: \begin{enumerate}
Chris@1: \item
Chris@1: No lookup
Chris@1: \item
Chris@1: Implicitly populated value mapping (lattice VQ)
Chris@1: \item
Chris@1: Explicitly populated value mapping (tessellated or 'foam'
Chris@1: VQ)
Chris@1: \end{enumerate}
Chris@1: 
Chris@1: 
Chris@1: The lookup table type is read as a four bit unsigned integer:
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [codebook\_lookup\_type] = read four bits as an unsigned integer
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: Codebook decode precedes according to \varname{[codebook\_lookup\_type]}:
Chris@1: \begin{itemize}
Chris@1: \item
Chris@1: Lookup type zero indicates no lookup to be read.  Proceed past
Chris@1: lookup decode.
Chris@1: \item
Chris@1: Lookup types one and two are similar, differing only in the
Chris@1: number of lookup values to be read.  Lookup type one reads a list of
Chris@1: values that are permuted in a set pattern to build a list of vectors,
Chris@1: each vector of order \varname{[codebook\_dimensions]} scalars.  Lookup
Chris@1: type two builds the same vector list, but reads each scalar for each
Chris@1: vector explicitly, rather than building vectors from a smaller list of
Chris@1: possible scalar values.  Lookup decode proceeds as follows:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [codebook\_minimum\_value] = \link{vorbis:spec:float32:unpack}{float32\_unpack}( read 32 bits as an unsigned integer)
Chris@1:   2) [codebook\_delta\_value] = \link{vorbis:spec:float32:unpack}{float32\_unpack}( read 32 bits as an unsigned integer)
Chris@1:   3) [codebook\_value\_bits] = read 4 bits as an unsigned integer and add 1
Chris@1:   4) [codebook\_sequence\_p] = read 1 bit as a boolean flag
Chris@1: 
Chris@1:   if ( [codebook\_lookup\_type] is 1 ) \{
Chris@1: 
Chris@1:      5) [codebook\_lookup\_values] = \link{vorbis:spec:lookup1:values}{lookup1\_values}(\varname{[codebook\_entries]}, \varname{[codebook\_dimensions]} )
Chris@1: 
Chris@1:   \} else \{
Chris@1: 
Chris@1:      6) [codebook\_lookup\_values] = \varname{[codebook\_entries]} * \varname{[codebook\_dimensions]}
Chris@1: 
Chris@1:   \}
Chris@1: 
Chris@1:   7) read a total of [codebook\_lookup\_values] unsigned integers of [codebook\_value\_bits] each;
Chris@1:      store these in order in the array [codebook\_multiplicands]
Chris@1: \end{Verbatim}
Chris@1: \item
Chris@1: A \varname{[codebook\_lookup\_type]} of greater than two is reserved
Chris@1: and indicates a stream that is not decodable by the specification in this
Chris@1: document.
Chris@1: 
Chris@1: \end{itemize}
Chris@1: 
Chris@1: 
Chris@1: An 'end of packet' during any read operation in the above steps is
Chris@1: considered an error condition rendering the stream undecodable.
Chris@1: 
Chris@1: \paragraph{Huffman decision tree representation}
Chris@1: 
Chris@1: The \varname{[codebook\_codeword\_lengths]} array and
Chris@1: \varname{[codebook\_entries]} value uniquely define the Huffman decision
Chris@1: tree used for entropy decoding.
Chris@1: 
Chris@1: Briefly, each used codebook entry (recall that length-unordered
Chris@1: codebooks support unused codeword entries) is assigned, in order, the
Chris@1: lowest valued unused binary Huffman codeword possible.  Assume the
Chris@1: following codeword length list:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: entry 0: length 2
Chris@1: entry 1: length 4
Chris@1: entry 2: length 4
Chris@1: entry 3: length 4
Chris@1: entry 4: length 4
Chris@1: entry 5: length 2
Chris@1: entry 6: length 3
Chris@1: entry 7: length 3
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: Assigning codewords in order (lowest possible value of the appropriate
Chris@1: length to highest) results in the following codeword list:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: entry 0: length 2 codeword 00
Chris@1: entry 1: length 4 codeword 0100
Chris@1: entry 2: length 4 codeword 0101
Chris@1: entry 3: length 4 codeword 0110
Chris@1: entry 4: length 4 codeword 0111
Chris@1: entry 5: length 2 codeword 10
Chris@1: entry 6: length 3 codeword 110
Chris@1: entry 7: length 3 codeword 111
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: 
Chris@1: \begin{note}
Chris@1: Unlike most binary numerical values in this document, we
Chris@1: intend the above codewords to be read and used bit by bit from left to
Chris@1: right, thus the codeword '001' is the bit string 'zero, zero, one'.
Chris@1: When determining 'lowest possible value' in the assignment definition
Chris@1: above, the leftmost bit is the MSb.
Chris@1: \end{note}
Chris@1: 
Chris@1: It is clear that the codeword length list represents a Huffman
Chris@1: decision tree with the entry numbers equivalent to the leaves numbered
Chris@1: left-to-right:
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=10cm]{hufftree}
Chris@1: \captionof{figure}{huffman tree illustration}
Chris@1: \end{center}
Chris@1: 
Chris@1: 
Chris@1: As we assign codewords in order, we see that each choice constructs a
Chris@1: new leaf in the leftmost possible position.
Chris@1: 
Chris@1: Note that it's possible to underspecify or overspecify a Huffman tree
Chris@1: via the length list.  In the above example, if codeword seven were
Chris@1: eliminated, it's clear that the tree is unfinished:
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=10cm]{hufftree-under}
Chris@1: \captionof{figure}{underspecified huffman tree illustration}
Chris@1: \end{center}
Chris@1: 
Chris@1: 
Chris@1: Similarly, in the original codebook, it's clear that the tree is fully
Chris@1: populated and a ninth codeword is impossible.  Both underspecified and
Chris@1: overspecified trees are an error condition rendering the stream
Chris@1: undecodable. Take special care that a codebook with a single used
Chris@1: entry is handled properly; it consists of a single codework of zero
Chris@1: bits and 'reading' a value out of such a codebook always returns the
Chris@1: single used value and sinks zero bits.  
Chris@1: 
Chris@1: Codebook entries marked 'unused' are simply skipped in the assigning
Chris@1: process.  They have no codeword and do not appear in the decision
Chris@1: tree, thus it's impossible for any bit pattern read from the stream to
Chris@1: decode to that entry number.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \paragraph{VQ lookup table vector representation}
Chris@1: 
Chris@1: Unpacking the VQ lookup table vectors relies on the following values:
Chris@1: \begin{programlisting}
Chris@1: the [codebook\_multiplicands] array
Chris@1: [codebook\_minimum\_value]
Chris@1: [codebook\_delta\_value]
Chris@1: [codebook\_sequence\_p]
Chris@1: [codebook\_lookup\_type]
Chris@1: [codebook\_entries]
Chris@1: [codebook\_dimensions]
Chris@1: [codebook\_lookup\_values]
Chris@1: \end{programlisting}
Chris@1: 
Chris@1: \bigskip
Chris@1: 
Chris@1: Decoding (unpacking) a specific vector in the vector lookup table
Chris@1: proceeds according to \varname{[codebook\_lookup\_type]}.  The unpacked
Chris@1: vector values are what a codebook would return during audio packet
Chris@1: decode in a VQ context.
Chris@1: 
Chris@1: \paragraph{Vector value decode: Lookup type 1}
Chris@1: 
Chris@1: Lookup type one specifies a lattice VQ lookup table built
Chris@1: algorithmically from a list of scalar values.  Calculate (unpack) the
Chris@1: final values of a codebook entry vector from the entries in
Chris@1: \varname{[codebook\_multiplicands]} as follows (\varname{[value\_vector]}
Chris@1: is the output vector representing the vector of values for entry number
Chris@1: \varname{[lookup\_offset]} in this codebook):
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [last] = 0;
Chris@1:   2) [index\_divisor] = 1;
Chris@1:   3) iterate [i] over the range 0 ... [codebook\_dimensions]-1 (once for each scalar value in the value vector) \{
Chris@1: 
Chris@1:        4) [multiplicand\_offset] = ( [lookup\_offset] divided by [index\_divisor] using integer
Chris@1:           division ) integer modulo [codebook\_lookup\_values]
Chris@1: 
Chris@1:        5) vector [value\_vector] element [i] =
Chris@1:             ( [codebook\_multiplicands] array element number [multiplicand\_offset] ) *
Chris@1:             [codebook\_delta\_value] + [codebook\_minimum\_value] + [last];
Chris@1: 
Chris@1:        6) if ( [codebook\_sequence\_p] is set ) then set [last] = vector [value\_vector] element [i]
Chris@1: 
Chris@1:        7) [index\_divisor] = [index\_divisor] * [codebook\_lookup\_values]
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:   8) vector calculation completed.
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \paragraph{Vector value decode: Lookup type 2}
Chris@1: 
Chris@1: Lookup type two specifies a VQ lookup table in which each scalar in
Chris@1: each vector is explicitly set by the \varname{[codebook\_multiplicands]}
Chris@1: array in a one-to-one mapping.  Calculate [unpack] the
Chris@1: final values of a codebook entry vector from the entries in
Chris@1: \varname{[codebook\_multiplicands]} as follows (\varname{[value\_vector]}
Chris@1: is the output vector representing the vector of values for entry number
Chris@1: \varname{[lookup\_offset]} in this codebook):
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [last] = 0;
Chris@1:   2) [multiplicand\_offset] = [lookup\_offset] * [codebook\_dimensions]
Chris@1:   3) iterate [i] over the range 0 ... [codebook\_dimensions]-1 (once for each scalar value in the value vector) \{
Chris@1: 
Chris@1:        4) vector [value\_vector] element [i] =
Chris@1:             ( [codebook\_multiplicands] array element number [multiplicand\_offset] ) *
Chris@1:             [codebook\_delta\_value] + [codebook\_minimum\_value] + [last];
Chris@1: 
Chris@1:        5) if ( [codebook\_sequence\_p] is set ) then set [last] = vector [value\_vector] element [i]
Chris@1: 
Chris@1:        6) increment [multiplicand\_offset]
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:   7) vector calculation completed.
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsection{Use of the codebook abstraction}
Chris@1: 
Chris@1: The decoder uses the codebook abstraction much as it does the
Chris@1: bit-unpacking convention; a specific codebook reads a
Chris@1: codeword from the bitstream, decoding it into an entry number, and then
Chris@1: returns that entry number to the decoder (when used in a scalar
Chris@1: entropy coding context), or uses that entry number as an offset into
Chris@1: the VQ lookup table, returning a vector of values (when used in a context
Chris@1: desiring a VQ value). Scalar or VQ context is always explicit; any call
Chris@1: to the codebook mechanism requests either a scalar entry number or a
Chris@1: lookup vector.
Chris@1: 
Chris@1: Note that VQ lookup type zero indicates that there is no lookup table;
Chris@1: requesting decode using a codebook of lookup type 0 in any context
Chris@1: expecting a vector return value (even in a case where a vector of
Chris@1: dimension one) is forbidden.  If decoder setup or decode requests such
Chris@1: an action, that is an error condition rendering the packet
Chris@1: undecodable.
Chris@1: 
Chris@1: Using a codebook to read from the packet bitstream consists first of
Chris@1: reading and decoding the next codeword in the bitstream. The decoder
Chris@1: reads bits until the accumulated bits match a codeword in the
Chris@1: codebook.  This process can be though of as logically walking the
Chris@1: Huffman decode tree by reading one bit at a time from the bitstream,
Chris@1: and using the bit as a decision boolean to take the 0 branch (left in
Chris@1: the above examples) or the 1 branch (right in the above examples).
Chris@1: Walking the tree finishes when the decode process hits a leaf in the
Chris@1: decision tree; the result is the entry number corresponding to that
Chris@1: leaf.  Reading past the end of a packet propagates the 'end-of-stream'
Chris@1: condition to the decoder.
Chris@1: 
Chris@1: When used in a scalar context, the resulting codeword entry is the
Chris@1: desired return value.
Chris@1: 
Chris@1: When used in a VQ context, the codeword entry number is used as an
Chris@1: offset into the VQ lookup table.  The value returned to the decoder is
Chris@1: the vector of scalars corresponding to this offset.




