Blame doc/04-codec.tex

Packit 06404a
Packit 06404a
% -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Packit 06404a
%!TEX root = Vorbis_I_spec.tex
Packit 06404a
% $Id$
Packit 06404a
\section{Codec Setup and Packet Decode} \label{vorbis:spec:codec}
Packit 06404a
Packit 06404a
\subsection{Overview}
Packit 06404a
Packit 06404a
This document serves as the top-level reference document for the
Packit 06404a
bit-by-bit decode specification of Vorbis I.  This document assumes a
Packit 06404a
high-level understanding of the Vorbis decode process, which is
Packit 06404a
provided in \xref{vorbis:spec:intro}.  \xref{vorbis:spec:bitpacking} covers reading and writing bit fields from
Packit 06404a
and to bitstream packets.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{Header decode and decode setup}
Packit 06404a
Packit 06404a
A Vorbis bitstream begins with three header packets. The header
Packit 06404a
packets are, in order, the identification header, the comments header,
Packit 06404a
and the setup header. All are required for decode compliance.  An
Packit 06404a
end-of-packet condition during decoding the first or third header
Packit 06404a
packet renders the stream undecodable.  End-of-packet decoding the
Packit 06404a
comment header is a non-fatal error condition.
Packit 06404a
Packit 06404a
\subsubsection{Common header decode}
Packit 06404a
Packit 06404a
Each header packet begins with the same header fields.
Packit 06404a
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
  1) [packet\_type] : 8 bit value
Packit 06404a
  2) 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73: the characters 'v','o','r','b','i','s' as six octets
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Decode continues according to packet type; the identification header
Packit 06404a
is type 1, the comment header type 3 and the setup header type 5
Packit 06404a
(these types are all odd as a packet with a leading single bit of '0'
Packit 06404a
is an audio packet).  The packets must occur in the order of
Packit 06404a
identification, comment, setup.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{Identification header}
Packit 06404a
Packit 06404a
The identification header is a short header of only a few fields used
Packit 06404a
to declare the stream definitively as Vorbis, and provide a few externally
Packit 06404a
relevant pieces of information about the audio stream. The
Packit 06404a
identification header is coded as follows:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
 1) [vorbis\_version] = read 32 bits as unsigned integer
Packit 06404a
 2) [audio\_channels] = read 8 bit integer as unsigned
Packit 06404a
 3) [audio\_sample\_rate] = read 32 bits as unsigned integer
Packit 06404a
 4) [bitrate\_maximum] = read 32 bits as signed integer
Packit 06404a
 5) [bitrate\_nominal] = read 32 bits as signed integer
Packit 06404a
 6) [bitrate\_minimum] = read 32 bits as signed integer
Packit 06404a
 7) [blocksize\_0] = 2 exponent (read 4 bits as unsigned integer)
Packit 06404a
 8) [blocksize\_1] = 2 exponent (read 4 bits as unsigned integer)
Packit 06404a
 9) [framing\_flag] = read one bit
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
\varname{[vorbis\_version]} is to read '0' in order to be compatible
Packit 06404a
with this document.  Both \varname{[audio\_channels]} and
Packit 06404a
\varname{[audio\_sample\_rate]} must read greater than zero.  Allowed final
Packit 06404a
blocksize values are 64, 128, 256, 512, 1024, 2048, 4096 and 8192 in
Packit 06404a
Vorbis I.  \varname{[blocksize\_0]} must be less than or equal to
Packit 06404a
\varname{[blocksize\_1]}.  The framing bit must be nonzero.  Failure to
Packit 06404a
meet any of these conditions renders a stream undecodable.
Packit 06404a
Packit 06404a
The bitrate fields above are used only as hints. The nominal bitrate
Packit 06404a
field especially may be considerably off in purely VBR streams.  The
Packit 06404a
fields are meaningful only when greater than zero.
Packit 06404a
Packit 06404a
\begin{itemize}
Packit 06404a
  \item All three fields set to the same value implies a fixed rate, or tightly bounded, nearly fixed-rate bitstream
Packit 06404a
  \item Only nominal set implies a VBR or ABR stream that averages the nominal bitrate
Packit 06404a
  \item Maximum and or minimum set implies a VBR bitstream that obeys the bitrate limits
Packit 06404a
  \item None set indicates the encoder does not care to speculate.
Packit 06404a
\end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{Comment header}
Packit 06404a
Comment header decode and data specification is covered in
Packit 06404a
\xref{vorbis:spec:comment}.
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{Setup header}
Packit 06404a
Packit 06404a
Vorbis codec setup is configurable to an extreme degree:
Packit 06404a
Packit 06404a
\begin{center}
Packit 06404a
\includegraphics[width=\textwidth]{components}
Packit 06404a
\captionof{figure}{decoder pipeline configuration}
Packit 06404a
\end{center}
Packit 06404a
Packit 06404a
Packit 06404a
The setup header contains the bulk of the codec setup information
Packit 06404a
needed for decode.  The setup header contains, in order, the lists of
Packit 06404a
codebook configurations, time-domain transform configurations
Packit 06404a
(placeholders in Vorbis I), floor configurations, residue
Packit 06404a
configurations, channel mapping configurations and mode
Packit 06404a
configurations. It finishes with a framing bit of '1'.  Header decode
Packit 06404a
proceeds in the following order:
Packit 06404a
Packit 06404a
\paragraph{Codebooks}
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
\item \varname{[vorbis\_codebook\_count]} = read eight bits as unsigned integer and add one
Packit 06404a
\item Decode \varname{[vorbis\_codebook\_count]} codebooks in order as defined
Packit 06404a
in \xref{vorbis:spec:codebook}.  Save each configuration, in
Packit 06404a
order, in an array of
Packit 06404a
codebook configurations \varname{[vorbis\_codebook\_configurations]}.
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\paragraph{Time domain transforms}
Packit 06404a
Packit 06404a
These hooks are placeholders in Vorbis I.  Nevertheless, the
Packit 06404a
configuration placeholder values must be read to maintain bitstream
Packit 06404a
sync.
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
\item \varname{[vorbis\_time\_count]} = read 6 bits as unsigned integer and add one
Packit 06404a
\item read \varname{[vorbis\_time\_count]} 16 bit values; each value should be zero.  If any value is nonzero, this is an error condition and the stream is undecodable.
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\paragraph{Floors}
Packit 06404a
Packit 06404a
Vorbis uses two floor types; header decode is handed to the decode
Packit 06404a
abstraction of the appropriate type.
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item \varname{[vorbis\_floor\_count]} = read 6 bits as unsigned integer and add one
Packit 06404a
 \item For each \varname{[i]} of \varname{[vorbis\_floor\_count]} floor numbers:
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item read the floor type: vector \varname{[vorbis\_floor\_types]} element \varname{[i]} =
Packit 06404a
read 16 bits as unsigned integer
Packit 06404a
   \item If the floor type is zero, decode the floor
Packit 06404a
configuration as defined in \xref{vorbis:spec:floor0}; save
Packit 06404a
this
Packit 06404a
configuration in slot \varname{[i]} of the floor configuration array \varname{[vorbis\_floor\_configurations]}.
Packit 06404a
   \item If the floor type is one,
Packit 06404a
decode the floor configuration as defined in \xref{vorbis:spec:floor1}; save this configuration in slot \varname{[i]} of the floor configuration array \varname{[vorbis\_floor\_configurations]}.
Packit 06404a
   \item If the the floor type is greater than one, this stream is undecodable; ERROR CONDITION
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\paragraph{Residues}
Packit 06404a
Packit 06404a
Vorbis uses three residue types; header decode of each type is identical.
Packit 06404a
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
\item \varname{[vorbis\_residue\_count]} = read 6 bits as unsigned integer and add one
Packit 06404a
Packit 06404a
\item For each of \varname{[vorbis\_residue\_count]} residue numbers:
Packit 06404a
 \begin{enumerate}
Packit 06404a
  \item read the residue type; vector \varname{[vorbis\_residue\_types]} element \varname{[i]} = read 16 bits as unsigned integer
Packit 06404a
  \item If the residue type is zero,
Packit 06404a
one or two, decode the residue configuration as defined in \xref{vorbis:spec:residue}; save this configuration in slot \varname{[i]} of the residue configuration array \varname{[vorbis\_residue\_configurations]}.
Packit 06404a
  \item If the the residue type is greater than two, this stream is undecodable; ERROR CONDITION
Packit 06404a
 \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\paragraph{Mappings}
Packit 06404a
Packit 06404a
Mappings are used to set up specific pipelines for encoding
Packit 06404a
multichannel audio with varying channel mapping applications. Vorbis I
Packit 06404a
uses a single mapping type (0), with implicit PCM channel mappings.
Packit 06404a
Packit 06404a
% FIXME/TODO: LaTeX cannot nest enumerate that deeply, so I have to use
Packit 06404a
% itemize at the innermost level. However, it would be much better to 
Packit 06404a
% rewrite this pseudocode using listings or algoritmicx or some other
Packit 06404a
% package geared towards this.
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item \varname{[vorbis\_mapping\_count]} = read 6 bits as unsigned integer and add one
Packit 06404a
 \item For each \varname{[i]} of \varname{[vorbis\_mapping\_count]} mapping numbers:
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item read the mapping type: 16 bits as unsigned integer.  There's no reason to save the mapping type in Vorbis I.
Packit 06404a
   \item If the mapping type is nonzero, the stream is undecodable
Packit 06404a
   \item If the mapping type is zero:
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item read 1 bit as a boolean flag
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item if set, \varname{[vorbis\_mapping\_submaps]} = read 4 bits as unsigned integer and add one
Packit 06404a
       \item if unset, \varname{[vorbis\_mapping\_submaps]} = 1
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
     \item read 1 bit as a boolean flag
Packit 06404a
       \begin{enumerate}
Packit 06404a
         \item if set, square polar channel mapping is in use:
Packit 06404a
           \begin{itemize}
Packit 06404a
             \item \varname{[vorbis\_mapping\_coupling\_steps]} = read 8 bits as unsigned integer and add one
Packit 06404a
             \item for \varname{[j]} each of \varname{[vorbis\_mapping\_coupling\_steps]} steps:
Packit 06404a
               \begin{itemize}
Packit 06404a
                 \item vector \varname{[vorbis\_mapping\_magnitude]} element \varname{[j]}= read \link{vorbis:spec:ilog}{ilog}(\varname{[audio\_channels]} - 1) bits as unsigned integer
Packit 06404a
                 \item vector \varname{[vorbis\_mapping\_angle]} element \varname{[j]}= read \link{vorbis:spec:ilog}{ilog}(\varname{[audio\_channels]} - 1) bits as unsigned integer
Packit 06404a
                 \item the numbers read in the above two steps are channel numbers representing the channel to treat as magnitude and the channel to treat as angle, respectively.  If for any coupling step the angle channel number equals the magnitude channel number, the magnitude channel number is greater than \varname{[audio\_channels]}-1, or the angle channel is greater than \varname{[audio\_channels]}-1, the stream is undecodable.
Packit 06404a
               \end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
           \end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
         \item if unset, \varname{[vorbis\_mapping\_coupling\_steps]} = 0
Packit 06404a
       \end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
     \item read 2 bits (reserved field); if the value is nonzero, the stream is undecodable
Packit 06404a
     \item if \varname{[vorbis\_mapping\_submaps]} is greater than one, we read channel multiplex settings. For each \varname{[j]} of \varname{[audio\_channels]} channels:
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} = read 4 bits as unsigned integer
Packit 06404a
       \item if the value is greater than the highest numbered submap (\varname{[vorbis\_mapping\_submaps]} - 1), this in an error condition rendering the stream undecodable
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
     \item for each submap \varname{[j]} of \varname{[vorbis\_mapping\_submaps]} submaps, read the floor and residue numbers for use in decoding that submap:
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item read and discard 8 bits (the unused time configuration placeholder)
Packit 06404a
       \item read 8 bits as unsigned integer for the floor number; save in vector \varname{[vorbis\_mapping\_submap\_floor]} element \varname{[j]}
Packit 06404a
       \item verify the floor number is not greater than the highest number floor configured for the bitstream. If it is, the bitstream is undecodable
Packit 06404a
       \item read 8 bits as unsigned integer for the residue number; save in vector \varname{[vorbis\_mapping\_submap\_residue]} element \varname{[j]}
Packit 06404a
       \item verify the residue number is not greater than the highest number residue configured for the bitstream.  If it is, the bitstream is undecodable
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
     \item save this mapping configuration in slot \varname{[i]} of the mapping configuration array \varname{[vorbis\_mapping\_configurations]}.
Packit 06404a
    \end{enumerate}
Packit 06404a
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\paragraph{Modes}
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item \varname{[vorbis\_mode\_count]} = read 6 bits as unsigned integer and add one
Packit 06404a
 \item For each of \varname{[vorbis\_mode\_count]} mode numbers:
Packit 06404a
  \begin{enumerate}
Packit 06404a
  \item \varname{[vorbis\_mode\_blockflag]} = read 1 bit
Packit 06404a
  \item \varname{[vorbis\_mode\_windowtype]} = read 16 bits as unsigned integer
Packit 06404a
  \item \varname{[vorbis\_mode\_transformtype]} = read 16 bits as unsigned integer
Packit 06404a
  \item \varname{[vorbis\_mode\_mapping]} = read 8 bits as unsigned integer
Packit 06404a
  \item verify ranges; zero is the only legal value in Vorbis I for
Packit 06404a
\varname{[vorbis\_mode\_windowtype]}
Packit 06404a
and \varname{[vorbis\_mode\_transformtype]}.  \varname{[vorbis\_mode\_mapping]} must not be greater than the highest number mapping in use.  Any illegal values render the stream undecodable.
Packit 06404a
  \item save this mode configuration in slot \varname{[i]} of the mode configuration array
Packit 06404a
\varname{[vorbis\_mode\_configurations]}.
Packit 06404a
 \end{enumerate}
Packit 06404a
Packit 06404a
\item read 1 bit as a framing flag.  If unset, a framing error occurred and the stream is not
Packit 06404a
decodable.
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
After reading mode descriptions, setup header decode is complete.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsection{Audio packet decode and synthesis}
Packit 06404a
Packit 06404a
Following the three header packets, all packets in a Vorbis I stream
Packit 06404a
are audio.  The first step of audio packet decode is to read and
Packit 06404a
verify the packet type. \emph{A non-audio packet when audio is expected
Packit 06404a
indicates stream corruption or a non-compliant stream. The decoder
Packit 06404a
must ignore the packet and not attempt decoding it to audio}.
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{packet type, mode and window decode}
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item read 1 bit \varname{[packet\_type]}; check that packet type is 0 (audio)
Packit 06404a
 \item read \link{vorbis:spec:ilog}{ilog}([vorbis\_mode\_count]-1) bits
Packit 06404a
\varname{[mode\_number]}
Packit 06404a
 \item decode blocksize \varname{[n]} is equal to \varname{[blocksize\_0]} if
Packit 06404a
\varname{[vorbis\_mode\_blockflag]} is 0, else \varname{[n]} is equal to \varname{[blocksize\_1]}.
Packit 06404a
 \item perform window selection and setup; this window is used later by the inverse MDCT:
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item if this is a long window (the \varname{[vorbis\_mode\_blockflag]} flag of this mode is
Packit 06404a
set):
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item read 1 bit for \varname{[previous\_window\_flag]}
Packit 06404a
     \item read 1 bit for \varname{[next\_window\_flag]}
Packit 06404a
     \item if \varname{[previous\_window\_flag]} is not set, the left half
Packit 06404a
         of the window will be a hybrid window for lapping with a
Packit 06404a
         short block.  See \xref{vorbis:spec:window} for an illustration of overlapping
Packit 06404a
dissimilar
Packit 06404a
         windows. Else, the left half window will have normal long
Packit 06404a
         shape.
Packit 06404a
     \item if \varname{[next\_window\_flag]} is not set, the right half of
Packit 06404a
         the window will be a hybrid window for lapping with a short
Packit 06404a
         block.  See \xref{vorbis:spec:window} for an
Packit 06404a
illustration of overlapping dissimilar
Packit 06404a
         windows. Else, the left right window will have normal long
Packit 06404a
         shape.
Packit 06404a
    \end{enumerate}
Packit 06404a
Packit 06404a
   \item  if this is a short window, the window is always the same
Packit 06404a
       short-window shape.
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Vorbis windows all use the slope function $y=\sin(\frac{\pi}{2} * \sin^2((x+0.5)/n * \pi))$,
Packit 06404a
where $n$ is window size and $x$ ranges $0 \ldots n-1$, but dissimilar
Packit 06404a
lapping requirements can affect overall shape.  Window generation
Packit 06404a
proceeds as follows:
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item  \varname{[window\_center]} = \varname{[n]} / 2
Packit 06404a
 \item  if (\varname{[vorbis\_mode\_blockflag]} is set and \varname{[previous\_window\_flag]} is
Packit 06404a
not set) then
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item \varname{[left\_window\_start]} = \varname{[n]}/4 -
Packit 06404a
\varname{[blocksize\_0]}/4
Packit 06404a
   \item \varname{[left\_window\_end]} = \varname{[n]}/4 + \varname{[blocksize\_0]}/4
Packit 06404a
   \item \varname{[left\_n]} = \varname{[blocksize\_0]}/2
Packit 06404a
  \end{enumerate}
Packit 06404a
 else
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item \varname{[left\_window\_start]} = 0
Packit 06404a
   \item \varname{[left\_window\_end]} = \varname{[window\_center]}
Packit 06404a
   \item \varname{[left\_n]} = \varname{[n]}/2
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
 \item  if (\varname{[vorbis\_mode\_blockflag]} is set and \varname{[next\_window\_flag]} is not
Packit 06404a
set) then
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item \varname{[right\_window\_start]} = \varname{[n]*3}/4 -
Packit 06404a
\varname{[blocksize\_0]}/4
Packit 06404a
   \item \varname{[right\_window\_end]} = \varname{[n]*3}/4 +
Packit 06404a
\varname{[blocksize\_0]}/4
Packit 06404a
   \item \varname{[right\_n]} = \varname{[blocksize\_0]}/2
Packit 06404a
  \end{enumerate}
Packit 06404a
 else
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item \varname{[right\_window\_start]} = \varname{[window\_center]}
Packit 06404a
   \item \varname{[right\_window\_end]} = \varname{[n]}
Packit 06404a
   \item \varname{[right\_n]} = \varname{[n]}/2
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
 \item  window from range 0 ... \varname{[left\_window\_start]}-1 inclusive is zero
Packit 06404a
 \item  for \varname{[i]} in range \varname{[left\_window\_start]} ...
Packit 06404a
\varname{[left\_window\_end]}-1, window(\varname{[i]}) = $\sin(\frac{\pi}{2} * \sin^2($ (\varname{[i]}-\varname{[left\_window\_start]}+0.5) / \varname{[left\_n]} $* \frac{\pi}{2})$ )
Packit 06404a
 \item  window from range \varname{[left\_window\_end]} ... \varname{[right\_window\_start]}-1
Packit 06404a
inclusive is one\item  for \varname{[i]} in range \varname{[right\_window\_start]} ... \varname{[right\_window\_end]}-1, window(\varname{[i]}) = $\sin(\frac{\pi}{2} * \sin^2($ (\varname{[i]}-\varname{[right\_window\_start]}+0.5) / \varname{[right\_n]} $ * \frac{\pi}{2} + \frac{\pi}{2})$ )
Packit 06404a
\item  window from range \varname{[right\_window\_start]} ... \varname{[n]}-1 is
Packit 06404a
zero
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
An end-of-packet condition up to this point should be considered an
Packit 06404a
error that discards this packet from the stream.  An end of packet
Packit 06404a
condition past this point is to be considered a possible nominal
Packit 06404a
occurrence.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{floor curve decode}
Packit 06404a
Packit 06404a
From this point on, we assume out decode context is using mode number
Packit 06404a
\varname{[mode\_number]} from configuration array
Packit 06404a
\varname{[vorbis\_mode\_configurations]} and the map number
Packit 06404a
\varname{[vorbis\_mode\_mapping]} (specified by the current mode) taken
Packit 06404a
from the mapping configuration array
Packit 06404a
\varname{[vorbis\_mapping\_configurations]}.
Packit 06404a
Packit 06404a
Floor curves are decoded one-by-one in channel order.
Packit 06404a
Packit 06404a
For each floor \varname{[i]} of \varname{[audio\_channels]}
Packit 06404a
 \begin{enumerate}
Packit 06404a
  \item \varname{[submap\_number]} = element \varname{[i]} of vector [vorbis\_mapping\_mux]
Packit 06404a
  \item \varname{[floor\_number]} = element \varname{[submap\_number]} of vector
Packit 06404a
[vorbis\_submap\_floor]
Packit 06404a
  \item if the floor type of this
Packit 06404a
floor (vector \varname{[vorbis\_floor\_types]} element
Packit 06404a
\varname{[floor\_number]}) is zero then decode the floor for
Packit 06404a
channel \varname{[i]} according to the
Packit 06404a
\xref{vorbis:spec:floor0-decode}
Packit 06404a
  \item if the type of this floor
Packit 06404a
is one then decode the floor for channel \varname{[i]} according
Packit 06404a
to the \xref{vorbis:spec:floor1-decode}
Packit 06404a
  \item save the needed decoded floor information for channel for later synthesis
Packit 06404a
  \item if the decoded floor returned 'unused', set vector \varname{[no\_residue]} element
Packit 06404a
\varname{[i]} to true, else set vector \varname{[no\_residue]} element \varname{[i]} to
Packit 06404a
false
Packit 06404a
 \end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
An end-of-packet condition during floor decode shall result in packet
Packit 06404a
decode zeroing all channel output vectors and skipping to the
Packit 06404a
add/overlap output stage.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{nonzero vector propagate}
Packit 06404a
Packit 06404a
A possible result of floor decode is that a specific vector is marked
Packit 06404a
'unused' which indicates that that final output vector is all-zero
Packit 06404a
values (and the floor is zero).  The residue for that vector is not
Packit 06404a
coded in the stream, save for one complication.  If some vectors are
Packit 06404a
used and some are not, channel coupling could result in mixing a
Packit 06404a
zeroed and nonzeroed vector to produce two nonzeroed vectors.
Packit 06404a
Packit 06404a
for each \varname{[i]} from 0 ... \varname{[vorbis\_mapping\_coupling\_steps]}-1
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item if either \varname{[no\_residue]} entry for channel
Packit 06404a
(\varname{[vorbis\_mapping\_magnitude]} element \varname{[i]})
Packit 06404a
or channel
Packit 06404a
(\varname{[vorbis\_mapping\_angle]} element \varname{[i]})
Packit 06404a
are set to false, then both must be set to false.  Note that an 'unused'
Packit 06404a
floor has no decoded floor information; it is important that this is
Packit 06404a
remembered at floor curve synthesis time.
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{residue decode}
Packit 06404a
Packit 06404a
Unlike floors, which are decoded in channel order, the residue vectors
Packit 06404a
are decoded in submap order.
Packit 06404a
Packit 06404a
for each submap \varname{[i]} in order from 0 ... \varname{[vorbis\_mapping\_submaps]}-1
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item \varname{[ch]} = 0
Packit 06404a
 \item for each channel \varname{[j]} in order from 0 ... \varname{[audio\_channels]} - 1
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item if channel \varname{[j]} in submap \varname{[i]} (vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} is equal to \varname{[i]})
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item if vector \varname{[no\_residue]} element \varname{[j]} is true
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item vector \varname{[do\_not\_decode\_flag]} element \varname{[ch]} is set
Packit 06404a
      \end{enumerate}
Packit 06404a
     else
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item vector \varname{[do\_not\_decode\_flag]} element \varname{[ch]} is unset
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
     \item increment \varname{[ch]}
Packit 06404a
    \end{enumerate}
Packit 06404a
Packit 06404a
  \end{enumerate}
Packit 06404a
 \item \varname{[residue\_number]} = vector \varname{[vorbis\_mapping\_submap\_residue]} element \varname{[i]}
Packit 06404a
 \item \varname{[residue\_type]} = vector \varname{[vorbis\_residue\_types]} element \varname{[residue\_number]}
Packit 06404a
 \item decode \varname{[ch]} vectors using residue \varname{[residue\_number]}, according to type \varname{[residue\_type]}, also passing vector \varname{[do\_not\_decode\_flag]} to indicate which vectors in the bundle should not be decoded. Correct per-vector decode length is \varname{[n]}/2.
Packit 06404a
 \item \varname{[ch]} = 0
Packit 06404a
 \item for each channel \varname{[j]} in order from 0 ... \varname{[audio\_channels]}
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item if channel \varname{[j]} is in submap \varname{[i]} (vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} is equal to \varname{[i]})
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item residue vector for channel \varname{[j]} is set to decoded residue vector \varname{[ch]}
Packit 06404a
     \item increment \varname{[ch]}
Packit 06404a
    \end{enumerate}
Packit 06404a
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{inverse coupling}
Packit 06404a
Packit 06404a
for each \varname{[i]} from \varname{[vorbis\_mapping\_coupling\_steps]}-1 descending to 0
Packit 06404a
Packit 06404a
\begin{enumerate}
Packit 06404a
 \item \varname{[magnitude\_vector]} = the residue vector for channel
Packit 06404a
(vector \varname{[vorbis\_mapping\_magnitude]} element \varname{[i]})
Packit 06404a
 \item \varname{[angle\_vector]} = the residue vector for channel (vector
Packit 06404a
\varname{[vorbis\_mapping\_angle]} element \varname{[i]})
Packit 06404a
 \item for each scalar value \varname{[M]} in vector \varname{[magnitude\_vector]} and the corresponding scalar value \varname{[A]} in vector \varname{[angle\_vector]}:
Packit 06404a
  \begin{enumerate}
Packit 06404a
   \item if (\varname{[M]} is greater than zero)
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item if (\varname{[A]} is greater than zero)
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item \varname{[new\_M]} = \varname{[M]}
Packit 06404a
       \item \varname{[new\_A]} = \varname{[M]}-\varname{[A]}
Packit 06404a
      \end{enumerate}
Packit 06404a
     else
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item \varname{[new\_A]} = \varname{[M]}
Packit 06404a
       \item \varname{[new\_M]} = \varname{[M]}+\varname{[A]}
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
    \end{enumerate}
Packit 06404a
   else
Packit 06404a
    \begin{enumerate}
Packit 06404a
     \item if (\varname{[A]} is greater than zero)
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item \varname{[new\_M]} = \varname{[M]}
Packit 06404a
       \item \varname{[new\_A]} = \varname{[M]}+\varname{[A]}
Packit 06404a
      \end{enumerate}
Packit 06404a
     else
Packit 06404a
      \begin{enumerate}
Packit 06404a
       \item \varname{[new\_A]} = \varname{[M]}
Packit 06404a
       \item \varname{[new\_M]} = \varname{[M]}-\varname{[A]}
Packit 06404a
      \end{enumerate}
Packit 06404a
Packit 06404a
    \end{enumerate}
Packit 06404a
Packit 06404a
   \item set scalar value \varname{[M]} in vector \varname{[magnitude\_vector]} to \varname{[new\_M]}
Packit 06404a
   \item set scalar value \varname{[A]} in vector \varname{[angle\_vector]} to \varname{[new\_A]}
Packit 06404a
  \end{enumerate}
Packit 06404a
Packit 06404a
\end{enumerate}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{dot product}
Packit 06404a
Packit 06404a
For each channel, synthesize the floor curve from the decoded floor
Packit 06404a
information, according to packet type. Note that the vector synthesis
Packit 06404a
length for floor computation is \varname{[n]}/2.
Packit 06404a
Packit 06404a
For each channel, multiply each element of the floor curve by each
Packit 06404a
element of that channel's residue vector.  The result is the dot
Packit 06404a
product of the floor and residue vectors for each channel; the produced
Packit 06404a
vectors are the length \varname{[n]}/2 audio spectrum for each
Packit 06404a
channel.
Packit 06404a
Packit 06404a
% TODO/FIXME: The following two paragraphs have identical twins
Packit 06404a
%   in section 1 (under "compute floor/residue dot product")
Packit 06404a
One point is worth mentioning about this dot product; a common mistake
Packit 06404a
in a fixed point implementation might be to assume that a 32 bit
Packit 06404a
fixed-point representation for floor and residue and direct
Packit 06404a
multiplication of the vectors is sufficient for acceptable spectral
Packit 06404a
depth in all cases because it happens to mostly work with the current
Packit 06404a
Xiph.Org reference encoder.
Packit 06404a
Packit 06404a
However, floor vector values can span \~140dB (\~24 bits unsigned), and
Packit 06404a
the audio spectrum vector should represent a minimum of 120dB (\~21
Packit 06404a
bits with sign), even when output is to a 16 bit PCM device.  For the
Packit 06404a
residue vector to represent full scale if the floor is nailed to
Packit 06404a
$-140$dB, it must be able to span 0 to $+140$dB.  For the residue vector
Packit 06404a
to reach full scale if the floor is nailed at 0dB, it must be able to
Packit 06404a
represent $-140$dB to $+0$dB.  Thus, in order to handle full range
Packit 06404a
dynamics, a residue vector may span $-140$dB to $+140$dB entirely within
Packit 06404a
spec.  A 280dB range is approximately 48 bits with sign; thus the
Packit 06404a
residue vector must be able to represent a 48 bit range and the dot
Packit 06404a
product must be able to handle an effective 48 bit times 24 bit
Packit 06404a
multiplication.  This range may be achieved using large (64 bit or
Packit 06404a
larger) integers, or implementing a movable binary point
Packit 06404a
representation.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{inverse MDCT}
Packit 06404a
Packit 06404a
Convert the audio spectrum vector of each channel back into time
Packit 06404a
domain PCM audio via an inverse Modified Discrete Cosine Transform
Packit 06404a
(MDCT).  A detailed description of the MDCT is available in \cite{Sporer/Brandenburg/Edler}.  The window
Packit 06404a
function used for the MDCT is the function described earlier.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{overlap\_add}
Packit 06404a
Packit 06404a
Windowed MDCT output is overlapped and added with the right hand data
Packit 06404a
of the previous window such that the 3/4 point of the previous window
Packit 06404a
is aligned with the 1/4 point of the current window (as illustrated in
Packit 06404a
\xref{vorbis:spec:window}).  The overlapped portion
Packit 06404a
produced from overlapping the previous and current frame data is
Packit 06404a
finished data to be returned by the decoder.  This data spans from the
Packit 06404a
center of the previous window to the center of the current window.  In
Packit 06404a
the case of same-sized windows, the amount of data to return is
Packit 06404a
one-half block consisting of and only of the overlapped portions. When
Packit 06404a
overlapping a short and long window, much of the returned range does not
Packit 06404a
actually overlap.  This does not damage transform orthogonality.  Pay
Packit 06404a
attention however to returning the correct data range; the amount of
Packit 06404a
data to be returned is:
Packit 06404a
Packit 06404a
\begin{programlisting}
Packit 06404a
window_blocksize(previous_window)/4+window_blocksize(current_window)/4
Packit 06404a
\end{programlisting}
Packit 06404a
Packit 06404a
from the center (element windowsize/2) of the previous window to the
Packit 06404a
center (element windowsize/2-1, inclusive) of the current window.
Packit 06404a
Packit 06404a
Data is not returned from the first frame; it must be used to 'prime'
Packit 06404a
the decode engine.  The encoder accounts for this priming when
Packit 06404a
calculating PCM offsets; after the first frame, the proper PCM output
Packit 06404a
offset is '0' (as no data has been returned yet).
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{output channel order}
Packit 06404a
Packit 06404a
Vorbis I specifies only a channel mapping type 0.  In mapping type 0,
Packit 06404a
channel mapping is implicitly defined as follows for standard audio
Packit 06404a
applications. As of revision 16781 (20100113), the specification adds
Packit 06404a
defined channel locations for 6.1 and 7.1 surround.  Ordering/location
Packit 06404a
for greater-than-eight channels remains 'left to the implementation'.
Packit 06404a
Packit 06404a
These channel orderings refer to order within the encoded stream.  It
Packit 06404a
is naturally possible for a decoder to produce output with channels in
Packit 06404a
any order. Any such decoder should explicitly document channel
Packit 06404a
reordering behavior.
Packit 06404a
Packit 06404a
\begin{description} %[style=nextline]
Packit 06404a
 \item[one channel]
Packit 06404a
	the stream is monophonic
Packit 06404a
Packit 06404a
\item[two channels]
Packit 06404a
	the stream is stereo.  channel order: left, right
Packit 06404a
Packit 06404a
\item[three channels]
Packit 06404a
	the stream is a 1d-surround encoding.  channel order: left,
Packit 06404a
center, right
Packit 06404a
Packit 06404a
\item[four channels]
Packit 06404a
	the stream is quadraphonic surround.  channel order: front left,
Packit 06404a
front right, rear left, rear right
Packit 06404a
Packit 06404a
\item[five channels]
Packit 06404a
	the stream is five-channel surround.  channel order: front left,
Packit 06404a
center, front right, rear left, rear right
Packit 06404a
Packit 06404a
\item[six channels]
Packit 06404a
	the stream is 5.1 surround.  channel order: front left, center, 
Packit 06404a
front right, rear left, rear right, LFE
Packit 06404a
Packit 06404a
\item[seven channels]
Packit 06404a
        the stream is 6.1 surround.  channel order: front left, center, 
Packit 06404a
front right, side left, side right, rear center, LFE
Packit 06404a
Packit 06404a
\item[eight channels]
Packit 06404a
        the stream is 7.1 surround.  channel order: front left, center, 
Packit 06404a
front right, side left, side right, rear left, rear right, 
Packit 06404a
LFE
Packit 06404a
Packit 06404a
\item[greater than eight channels]
Packit 06404a
	channel use and order is defined by the application
Packit 06404a
Packit 06404a
\end{description}
Packit 06404a
Packit 06404a
Applications using Vorbis for dedicated purposes may define channel
Packit 06404a
mapping as seen fit.  Future channel mappings (such as three and four
Packit 06404a
channel \href{http://www.ambisonic.net/}{Ambisonics}) will
Packit 06404a
make use of channel mappings other than mapping 0.
Packit 06404a
Packit 06404a