Blame doc/08-residue.tex

Packit 06404a
% -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Packit 06404a
%!TEX root = Vorbis_I_spec.tex
Packit 06404a
% $Id$
Packit 06404a
\section{Residue setup and decode} \label{vorbis:spec:residue}
Packit 06404a
Packit 06404a
\subsection{Overview}
Packit 06404a
Packit 06404a
A residue vector represents the fine detail of the audio spectrum of
Packit 06404a
one channel in an audio frame after the encoder subtracts the floor
Packit 06404a
curve and performs any channel coupling.  A residue vector may
Packit 06404a
represent spectral lines, spectral magnitude, spectral phase or
Packit 06404a
hybrids as mixed by channel coupling.  The exact semantic content of
Packit 06404a
the vector does not matter to the residue abstraction.
Packit 06404a
Packit 06404a
Whatever the exact qualities, the Vorbis residue abstraction codes the
Packit 06404a
residue vectors into the bitstream packet, and then reconstructs the
Packit 06404a
vectors during decode.  Vorbis makes use of three different encoding
Packit 06404a
variants (numbered 0, 1 and 2) of the same basic vector encoding
Packit 06404a
abstraction.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{Residue format}
Packit 06404a
Packit 06404a
Residue format partitions each vector in the vector bundle into chunks,
Packit 06404a
classifies each chunk, encodes the chunk classifications and finally
Packit 06404a
encodes the chunks themselves using the the specific VQ arrangement
Packit 06404a
defined for each selected classification.
Packit 06404a
The exact interleaving and partitioning vary by residue encoding number,
Packit 06404a
however the high-level process used to classify and encode the residue
Packit 06404a
vector is the same in all three variants.
Packit 06404a
Packit 06404a
A set of coded residue vectors are all of the same length.  High level
Packit 06404a
coding structure, ignoring for the moment exactly how a partition is
Packit 06404a
encoded and simply trusting that it is, is as follows:
Packit 06404a
Packit 06404a
\begin{itemize}
Packit 06404a
\item Each vector is partitioned into multiple equal sized chunks
Packit 06404a
according to configuration specified.  If we have a vector size of
Packit 06404a
\emph{n}, a partition size \emph{residue\_partition\_size}, and a total
Packit 06404a
of \emph{ch} residue vectors, the total number of partitioned chunks
Packit 06404a
coded is \emph{n}/\emph{residue\_partition\_size}*\emph{ch}.  It is
Packit 06404a
important to note that the integer division truncates.  In the below
Packit 06404a
example, we assume an example \emph{residue\_partition\_size} of 8.
Packit 06404a
Packit 06404a
\item Each partition in each vector has a classification number that
Packit 06404a
specifies which of multiple configured VQ codebook setups are used to
Packit 06404a
decode that partition.  The classification numbers of each partition
Packit 06404a
can be thought of as forming a vector in their own right, as in the
Packit 06404a
illustration below.  Just as the residue vectors are coded in grouped
Packit 06404a
partitions to increase encoding efficiency, the classification vector
Packit 06404a
is also partitioned into chunks.  The integer elements of each scalar
Packit 06404a
in a classification chunk are built into a single scalar that
Packit 06404a
represents the classification numbers in that chunk.  In the below
Packit 06404a
example, the classification codeword encodes two classification
Packit 06404a
numbers.
Packit 06404a
Packit 06404a
\item The values in a residue vector may be encoded monolithically in a
Packit 06404a
single pass through the residue vector, but more often efficient
Packit 06404a
codebook design dictates that each vector is encoded as the additive
Packit 06404a
sum of several passes through the residue vector using more than one
Packit 06404a
VQ codebook.  Thus, each residue value potentially accumulates values
Packit 06404a
from multiple decode passes.  The classification value associated with
Packit 06404a
a partition is the same in each pass, thus the classification codeword
Packit 06404a
is coded only in the first pass.
Packit 06404a
Packit 06404a
\end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
\begin{center}
Packit 06404a
\includegraphics[width=\textwidth]{residue-pack}
Packit 06404a
\captionof{figure}{illustration of residue vector format}
Packit 06404a
\end{center}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{residue 0}
Packit 06404a
Packit 06404a
Residue 0 and 1 differ only in the way the values within a residue
Packit 06404a
partition are interleaved during partition encoding (visually treated
Packit 06404a
as a black box--or cyan box or brown box--in the above figure).
Packit 06404a
Packit 06404a
Residue encoding 0 interleaves VQ encoding according to the
Packit 06404a
dimension of the codebook used to encode a partition in a specific
Packit 06404a
pass.  The dimension of the codebook need not be the same in multiple
Packit 06404a
passes, however the partition size must be an even multiple of the
Packit 06404a
codebook dimension.
Packit 06404a
Packit 06404a
As an example, assume a partition vector of size eight, to be encoded
Packit 06404a
by residue 0 using codebook sizes of 8, 4, 2 and 1:
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
Packit 06404a
            original residue vector: [ 0 1 2 3 4 5 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 8  encoded as: [ 0 1 2 3 4 5 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 4  encoded as: [ 0 2 4 6 ], [ 1 3 5 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 2  encoded as: [ 0 4 ], [ 1 5 ], [ 2 6 ], [ 3 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 1  encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
Packit 06404a
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
It is worth mentioning at this point that no configurable value in the
Packit 06404a
residue coding setup is restricted to a power of two.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{residue 1}
Packit 06404a
Packit 06404a
Residue 1 does not interleave VQ encoding.  It represents partition
Packit 06404a
vector scalars in order.  As with residue 0, however, partition length
Packit 06404a
must be an integer multiple of the codebook dimension, although
Packit 06404a
dimension may vary from pass to pass.
Packit 06404a
Packit 06404a
As an example, assume a partition vector of size eight, to be encoded
Packit 06404a
by residue 0 using codebook sizes of 8, 4, 2 and 1:
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
Packit 06404a
            original residue vector: [ 0 1 2 3 4 5 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 8  encoded as: [ 0 1 2 3 4 5 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 4  encoded as: [ 0 1 2 3 ], [ 4 5 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 2  encoded as: [ 0 1 ], [ 2 3 ], [ 4 5 ], [ 6 7 ]
Packit 06404a
Packit 06404a
codebook dimensions = 1  encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
Packit 06404a
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{residue 2}
Packit 06404a
Packit 06404a
Residue type two can be thought of as a variant of residue type 1.
Packit 06404a
Rather than encoding multiple passed-in vectors as in residue type 1,
Packit 06404a
the \emph{ch} passed in vectors of length \emph{n} are first
Packit 06404a
interleaved and flattened into a single vector of length
Packit 06404a
\emph{ch}*\emph{n}.  Encoding then proceeds as in type 1. Decoding is
Packit 06404a
as in type 1 with decode interleave reversed. If operating on a single
Packit 06404a
vector to begin with, residue type 1 and type 2 are equivalent.
Packit 06404a
Packit 06404a
\begin{center}
Packit 06404a
\includegraphics[width=\textwidth]{residue2}
Packit 06404a
\captionof{figure}{illustration of residue type 2}
Packit 06404a
\end{center}
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{Residue decode}
Packit 06404a
Packit 06404a
\subsubsection{header decode}
Packit 06404a
Packit 06404a
Header decode for all three residue types is identical.
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) [residue\_begin] = read 24 bits as unsigned integer
Packit 06404a
  2) [residue\_end] = read 24 bits as unsigned integer
Packit 06404a
  3) [residue\_partition\_size] = read 24 bits as unsigned integer and add one
Packit 06404a
  4) [residue\_classifications] = read 6 bits as unsigned integer and add one
Packit 06404a
  5) [residue\_classbook] = read 8 bits as unsigned integer
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
\varname{[residue\_begin]} and
Packit 06404a
\varname{[residue\_end]} select the specific sub-portion of
Packit 06404a
each vector that is actually coded; it implements akin to a bandpass
Packit 06404a
where, for coding purposes, the vector effectively begins at element
Packit 06404a
\varname{[residue\_begin]} and ends at
Packit 06404a
\varname{[residue\_end]}.  Preceding and following values in
Packit 06404a
the unpacked vectors are zeroed.  Note that for residue type 2, these
Packit 06404a
values as well as \varname{[residue\_partition\_size]}apply to
Packit 06404a
the interleaved vector, not the individual vectors before interleave.
Packit 06404a
\varname{[residue\_partition\_size]} is as explained above,
Packit 06404a
\varname{[residue\_classifications]} is the number of possible
Packit 06404a
classification to which a partition can belong and
Packit 06404a
\varname{[residue\_classbook]} is the codebook number used to
Packit 06404a
code classification codewords.  The number of dimensions in book
Packit 06404a
\varname{[residue\_classbook]} determines how many
Packit 06404a
classification values are grouped into a single classification
Packit 06404a
codeword.  Note that the number of entries and dimensions in book
Packit 06404a
\varname{[residue\_classbook]}, along with
Packit 06404a
\varname{[residue\_classifications]}, overdetermines to
Packit 06404a
possible number of classification codewords.  
Packit 06404a
If \varname{[residue\_classifications]}\^{}\varname{[residue\_classbook]}.dimensions
Packit 06404a
exceeds \varname{[residue\_classbook]}.entries, the
Packit 06404a
bitstream should be regarded to be undecodable.
Packit 06404a
Packit 06404a
Next we read a bitmap pattern that specifies which partition classes
Packit 06404a
code values in which passes.
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) iterate [i] over the range 0 ... [residue\_classifications]-1 {
Packit 06404a
Packit 06404a
       2) [high\_bits] = 0
Packit 06404a
       3) [low\_bits] = read 3 bits as unsigned integer
Packit 06404a
       4) [bitflag] = read one bit as boolean
Packit 06404a
       5) if ( [bitflag] is set ) then [high\_bits] = read five bits as unsigned integer
Packit 06404a
       6) vector [residue\_cascade] element [i] = [high\_bits] * 8 + [low\_bits]
Packit 06404a
     }
Packit 06404a
  7) done
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
Finally, we read in a list of book numbers, each corresponding to
Packit 06404a
specific bit set in the cascade bitmap.  We loop over the possible
Packit 06404a
codebook classifications and the maximum possible number of encoding
Packit 06404a
stages (8 in Vorbis I, as constrained by the elements of the cascade
Packit 06404a
bitmap being eight bits):
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) iterate [i] over the range 0 ... [residue\_classifications]-1 {
Packit 06404a
Packit 06404a
       2) iterate [j] over the range 0 ... 7 {
Packit 06404a
Packit 06404a
            3) if ( vector [residue\_cascade] element [i] bit [j] is set ) {
Packit 06404a
Packit 06404a
                 4) array [residue\_books] element [i][j] = read 8 bits as unsigned integer
Packit 06404a
Packit 06404a
               } else {
Packit 06404a
Packit 06404a
                 5) array [residue\_books] element [i][j] = unused
Packit 06404a
Packit 06404a
               }
Packit 06404a
          }
Packit 06404a
      }
Packit 06404a
Packit 06404a
  6) done
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
An end-of-packet condition at any point in header decode renders the
Packit 06404a
stream undecodable.  In addition, any codebook number greater than the
Packit 06404a
maximum numbered codebook set up in this stream also renders the
Packit 06404a
stream undecodable. All codebooks in array [residue\_books] are
Packit 06404a
required to have a value mapping.  The presence of codebook in array
Packit 06404a
[residue\_books] without a value mapping (maptype equals zero) renders
Packit 06404a
the stream undecodable.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{packet decode}
Packit 06404a
Packit 06404a
Format 0 and 1 packet decode is identical except for specific
Packit 06404a
partition interleave.  Format 2 packet decode can be built out of the
Packit 06404a
format 1 decode process.  Thus we describe first the decode
Packit 06404a
infrastructure identical to all three formats.
Packit 06404a
Packit 06404a
In addition to configuration information, the residue decode process
Packit 06404a
is passed the number of vectors in the submap bundle and a vector of
Packit 06404a
flags indicating if any of the vectors are not to be decoded.  If the
Packit 06404a
passed in number of vectors is 3 and vector number 1 is marked 'do not
Packit 06404a
decode', decode skips vector 1 during the decode loop.  However, even
Packit 06404a
'do not decode' vectors are allocated and zeroed.
Packit 06404a
Packit 06404a
Depending on the values of \varname{[residue\_begin]} and
Packit 06404a
\varname{[residue\_end]}, it is obvious that the encoded
Packit 06404a
portion of a residue vector may be the entire possible residue vector
Packit 06404a
or some other strict subset of the actual residue vector size with
Packit 06404a
zero padding at either uncoded end.  However, it is also possible to
Packit 06404a
set \varname{[residue\_begin]} and
Packit 06404a
\varname{[residue\_end]} to specify a range partially or
Packit 06404a
wholly beyond the maximum vector size.  Before beginning residue
Packit 06404a
decode, limit \varname{[residue\_begin]} and
Packit 06404a
\varname{[residue\_end]} to the maximum possible vector size
Packit 06404a
as follows.  We assume that the number of vectors being encoded,
Packit 06404a
\varname{[ch]} is provided by the higher level decoding
Packit 06404a
process.
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) [actual\_size] = current blocksize/2;
Packit 06404a
  2) if residue encoding is format 2
Packit 06404a
       3) [actual\_size] = [actual\_size] * [ch];
Packit 06404a
  4) [limit\_residue\_begin] = maximum of ([residue\_begin],[actual\_size]);
Packit 06404a
  5) [limit\_residue\_end] = maximum of ([residue\_end],[actual\_size]);
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
The following convenience values are conceptually useful to clarifying
Packit 06404a
the decode process:
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) [classwords\_per\_codeword] = [codebook\_dimensions] value of codebook [residue\_classbook]
Packit 06404a
  2) [n\_to\_read] = [limit\_residue\_end] - [limit\_residue\_begin]
Packit 06404a
  3) [partitions\_to\_read] = [n\_to\_read] / [residue\_partition\_size]
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
Packet decode proceeds as follows, matching the description offered earlier in the document.
Packit 06404a
\begin{programlisting}
Packit 06404a
  1) allocate and zero all vectors that will be returned.
Packit 06404a
  2) if ([n\_to\_read] is zero), stop; there is no residue to decode.
Packit 06404a
  3) iterate [pass] over the range 0 ... 7 {
Packit 06404a
Packit 06404a
       4) [partition\_count] = 0
Packit 06404a
Packit 06404a
       5) while [partition\_count] is less than [partitions\_to\_read]
Packit 06404a
Packit 06404a
            6) if ([pass] is zero) {
Packit 06404a
Packit 06404a
                 7) iterate [j] over the range 0 .. [ch]-1 {
Packit 06404a
Packit 06404a
                      8) if vector [j] is not marked 'do not decode' {
Packit 06404a
Packit 06404a
                           9) [temp] = read from packet using codebook [residue\_classbook] in scalar context
Packit 06404a
                          10) iterate [i] descending over the range [classwords\_per\_codeword]-1 ... 0 {
Packit 06404a
Packit 06404a
                               11) array [classifications] element [j],([i]+[partition\_count]) =
Packit 06404a
                                   [temp] integer modulo [residue\_classifications]
Packit 06404a
                               12) [temp] = [temp] / [residue\_classifications] using integer division
Packit 06404a
Packit 06404a
                              }
Packit 06404a
Packit 06404a
                         }
Packit 06404a
Packit 06404a
                    }
Packit 06404a
Packit 06404a
               }
Packit 06404a
Packit 06404a
           13) iterate [i] over the range 0 .. ([classwords\_per\_codeword] - 1) while [partition\_count]
Packit 06404a
               is also less than [partitions\_to\_read] {
Packit 06404a
Packit 06404a
                 14) iterate [j] over the range 0 .. [ch]-1 {
Packit 06404a
Packit 06404a
                      15) if vector [j] is not marked 'do not decode' {
Packit 06404a
Packit 06404a
                           16) [vqclass] = array [classifications] element [j],[partition\_count]
Packit 06404a
                           17) [vqbook] = array [residue\_books] element [vqclass],[pass]
Packit 06404a
                           18) if ([vqbook] is not 'unused') {
Packit 06404a
Packit 06404a
                                19) decode partition into output vector number [j], starting at scalar
Packit 06404a
                                    offset [limit\_residue\_begin]+[partition\_count]*[residue\_partition\_size] using
Packit 06404a
                                    codebook number [vqbook] in VQ context
Packit 06404a
                          }
Packit 06404a
                     }
Packit 06404a
Packit 06404a
                 20) increment [partition\_count] by one
Packit 06404a
Packit 06404a
               }
Packit 06404a
          }
Packit 06404a
     }
Packit 06404a
Packit 06404a
 21) done
Packit 06404a
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
An end-of-packet condition during packet decode is to be considered a
Packit 06404a
nominal occurrence.  Decode returns the result of vector decode up to
Packit 06404a
that point.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{format 0 specifics}
Packit 06404a
Packit 06404a
Format zero decodes partitions exactly as described earlier in the
Packit 06404a
'Residue Format: residue 0' section.  The following pseudocode
Packit 06404a
presents the same algorithm. Assume:
Packit 06404a
Packit 06404a
\begin{itemize}
Packit 06404a
\item  \varname{[n]} is the value in \varname{[residue\_partition\_size]}
Packit 06404a
\item \varname{[v]} is the residue vector
Packit 06404a
\item \varname{[offset]} is the beginning read offset in [v]
Packit 06404a
\end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
 1) [step] = [n] / [codebook\_dimensions]
Packit 06404a
 2) iterate [i] over the range 0 ... [step]-1 {
Packit 06404a
Packit 06404a
      3) vector [entry\_temp] = read vector from packet using current codebook in VQ context
Packit 06404a
      4) iterate [j] over the range 0 ... [codebook\_dimensions]-1 {
Packit 06404a
Packit 06404a
           5) vector [v] element ([offset]+[i]+[j]*[step]) =
Packit 06404a
	        vector [v] element ([offset]+[i]+[j]*[step]) +
Packit 06404a
                vector [entry\_temp] element [j]
Packit 06404a
Packit 06404a
         }
Packit 06404a
Packit 06404a
    }
Packit 06404a
Packit 06404a
  6) done
Packit 06404a
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{format 1 specifics}
Packit 06404a
Packit 06404a
Format 1 decodes partitions exactly as described earlier in the
Packit 06404a
'Residue Format: residue 1' section.  The following pseudocode
Packit 06404a
presents the same algorithm. Assume:
Packit 06404a
Packit 06404a
\begin{itemize}
Packit 06404a
\item  \varname{[n]} is the value in
Packit 06404a
\varname{[residue\_partition\_size]}
Packit 06404a
\item \varname{[v]} is the residue vector
Packit 06404a
\item \varname{[offset]} is the beginning read offset in [v]
Packit 06404a
\end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
 1) [i] = 0
Packit 06404a
 2) vector [entry\_temp] = read vector from packet using current codebook in VQ context
Packit 06404a
 3) iterate [j] over the range 0 ... [codebook\_dimensions]-1 {
Packit 06404a
Packit 06404a
      4) vector [v] element ([offset]+[i]) =
Packit 06404a
	  vector [v] element ([offset]+[i]) +
Packit 06404a
          vector [entry\_temp] element [j]
Packit 06404a
      5) increment [i]
Packit 06404a
Packit 06404a
    }
Packit 06404a
Packit 06404a
  6) if ( [i] is less than [n] ) continue at step 2
Packit 06404a
  7) done
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{format 2 specifics}
Packit 06404a
Packit 06404a
Format 2 is reducible to format 1.  It may be implemented as an additional step prior to and an additional post-decode step after a normal format 1 decode.
Packit 06404a
Packit 06404a
Packit 06404a
Format 2 handles 'do not decode' vectors differently than residue 0 or
Packit 06404a
1; if all vectors are marked 'do not decode', no decode occurrs.
Packit 06404a
However, if at least one vector is to be decoded, all the vectors are
Packit 06404a
decoded.  We then request normal format 1 to decode a single vector
Packit 06404a
representing all output channels, rather than a vector for each
Packit 06404a
channel.  After decode, deinterleave the vector into independent vectors, one for each output channel.  That is:
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item If all vectors 0 through \emph{ch}-1 are marked 'do not decode', allocate and clear a single vector \varname{[v]}of length \emph{ch*n} and skip step 2 below; proceed directly to the post-decode step.
Packit 06404a
 \item Rather than performing format 1 decode to produce \emph{ch} vectors of length \emph{n} each, call format 1 decode to produce a single vector \varname{[v]} of length \emph{ch*n}.
Packit 06404a
 \item Post decode: Deinterleave the single vector \varname{[v]} returned by format 1 decode as described above into \emph{ch} independent vectors, one for each outputchannel, according to:
Packit 06404a
  \begin{programlisting}
Packit 06404a
  1) iterate [i] over the range 0 ... [n]-1 {
Packit 06404a
Packit 06404a
       2) iterate [j] over the range 0 ... [ch]-1 {
Packit 06404a
Packit 06404a
            3) output vector number [j] element [i] = vector [v] element ([i] * [ch] + [j])
Packit 06404a
Packit 06404a
          }
Packit 06404a
     }
Packit 06404a
Packit 06404a
  4) done
Packit 06404a
  \end{programlisting}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a