
Chris@1: % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Chris@1: %!TEX root = Vorbis_I_spec.tex
Chris@1: % $Id$
Chris@1: \section{Floor type 1 setup and decode} \label{vorbis:spec:floor1}
Chris@1: 
Chris@1: \subsection{Overview}
Chris@1: 
Chris@1: Vorbis floor type one uses a piecewise straight-line representation to
Chris@1: encode a spectral envelope curve. The representation plots this curve
Chris@1: mechanically on a linear frequency axis and a logarithmic (dB)
Chris@1: amplitude axis. The integer plotting algorithm used is similar to
Chris@1: Bresenham's algorithm.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsection{Floor 1 format}
Chris@1: 
Chris@1: \subsubsection{model}
Chris@1: 
Chris@1: Floor type one represents a spectral curve as a series of
Chris@1: line segments.  Synthesis constructs a floor curve using iterative
Chris@1: prediction in a process roughly equivalent to the following simplified
Chris@1: description:
Chris@1: 
Chris@1: \begin{itemize}
Chris@1:  \item  the first line segment (base case) is a logical line spanning
Chris@1: from x_0,y_0 to x_1,y_1 where in the base case x_0=0 and x_1=[n], the
Chris@1: full range of the spectral floor to be computed.
Chris@1: 
Chris@1: \item the induction step chooses a point x_new within an existing
Chris@1: logical line segment and produces a y_new value at that point computed
Chris@1: from the existing line's y value at x_new (as plotted by the line) and
Chris@1: a difference value decoded from the bitstream packet.
Chris@1: 
Chris@1: \item floor computation produces two new line segments, one running from
Chris@1: x_0,y_0 to x_new,y_new and from x_new,y_new to x_1,y_1. This step is
Chris@1: performed logically even if y_new represents no change to the
Chris@1: amplitude value at x_new so that later refinement is additionally
Chris@1: bounded at x_new.
Chris@1: 
Chris@1: \item the induction step repeats, using a list of x values specified in
Chris@1: the codec setup header at floor 1 initialization time.  Computation
Chris@1: is completed at the end of the x value list.
Chris@1: 
Chris@1: \end{itemize}
Chris@1: 
Chris@1: 
Chris@1: Consider the following example, with values chosen for ease of
Chris@1: understanding rather than representing typical configuration:
Chris@1: 
Chris@1: For the below example, we assume a floor setup with an [n] of 128.
Chris@1: The list of selected X values in increasing order is
Chris@1: 0,16,32,48,64,80,96,112 and 128.  In list order, the values interleave
Chris@1: as 0, 128, 64, 32, 96, 16, 48, 80 and 112.  The corresponding
Chris@1: list-order Y values as decoded from an example packet are 110, 20, -5,
Chris@1: -45, 0, -25, -10, 30 and -10.  We compute the floor in the following
Chris@1: way, beginning with the first line:
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=8cm]{floor1-1}
Chris@1: \captionof{figure}{graph of example floor}
Chris@1: \end{center}
Chris@1: 
Chris@1: We now draw new logical lines to reflect the correction to new_Y, and
Chris@1: iterate for X positions 32 and 96:
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=8cm]{floor1-2}
Chris@1: \captionof{figure}{graph of example floor}
Chris@1: \end{center}
Chris@1: 
Chris@1: Although the new Y value at X position 96 is unchanged, it is still
Chris@1: used later as an endpoint for further refinement.  From here on, the
Chris@1: pattern should be clear; we complete the floor computation as follows:
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=8cm]{floor1-3}
Chris@1: \captionof{figure}{graph of example floor}
Chris@1: \end{center}
Chris@1: 
Chris@1: \begin{center}
Chris@1: \includegraphics[width=8cm]{floor1-4}
Chris@1: \captionof{figure}{graph of example floor}
Chris@1: \end{center}
Chris@1: 
Chris@1: A more efficient algorithm with carefully defined integer rounding
Chris@1: behavior is used for actual decode, as described later.  The actual
Chris@1: algorithm splits Y value computation and line plotting into two steps
Chris@1: with modifications to the above algorithm to eliminate noise
Chris@1: accumulation through integer roundoff/truncation.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{header decode}
Chris@1: 
Chris@1: A list of floor X values is stored in the packet header in interleaved
Chris@1: format (used in list order during packet decode and synthesis).  This
Chris@1: list is split into partitions, and each partition is assigned to a
Chris@1: partition class.  X positions 0 and [n] are implicit and do not belong
Chris@1: to an explicit partition or partition class.
Chris@1: 
Chris@1: A partition class consists of a representation vector width (the
Chris@1: number of Y values which the partition class encodes at once), a
Chris@1: 'subclass' value representing the number of alternate entropy books
Chris@1: the partition class may use in representing Y values, the list of
Chris@1: [subclass] books and a master book used to encode which alternate
Chris@1: books were chosen for representation in a given packet.  The
Chris@1: master/subclass mechanism is meant to be used as a flexible
Chris@1: representation cascade while still using codebooks only in a scalar
Chris@1: context.
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1: 
Chris@1:   1) [floor1\_partitions] = read 5 bits as unsigned integer
Chris@1:   2) [maximum\_class] = -1
Chris@1:   3) iterate [i] over the range 0 ... [floor1\_partitions]-1 \{
Chris@1: 
Chris@1:         4) vector [floor1\_partition\_class\_list] element [i] = read 4 bits as unsigned integer
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:   5) [maximum\_class] = largest integer scalar value in vector [floor1\_partition\_class\_list]
Chris@1:   6) iterate [i] over the range 0 ... [maximum\_class] \{
Chris@1: 
Chris@1:         7) vector [floor1\_class\_dimensions] element [i] = read 3 bits as unsigned integer and add 1
Chris@1: 	8) vector [floor1\_class\_subclasses] element [i] = read 2 bits as unsigned integer
Chris@1:         9) if ( vector [floor1\_class\_subclasses] element [i] is nonzero ) \{
Chris@1: 
Chris@1:              10) vector [floor1\_class\_masterbooks] element [i] = read 8 bits as unsigned integer
Chris@1: 
Chris@1:            \}
Chris@1: 
Chris@1:        11) iterate [j] over the range 0 ... (2 exponent [floor1\_class\_subclasses] element [i]) - 1 \{
Chris@1: 
Chris@1:              12) array [floor1\_subclass\_books] element [i],[j] =
Chris@1:                  read 8 bits as unsigned integer and subtract one
Chris@1:            \}
Chris@1:       \}
Chris@1: 
Chris@1:  13) [floor1\_multiplier] = read 2 bits as unsigned integer and add one
Chris@1:  14) [rangebits] = read 4 bits as unsigned integer
Chris@1:  15) vector [floor1\_X\_list] element [0] = 0
Chris@1:  16) vector [floor1\_X\_list] element [1] = 2 exponent [rangebits];
Chris@1:  17) [floor1\_values] = 2
Chris@1:  18) iterate [i] over the range 0 ... [floor1\_partitions]-1 \{
Chris@1: 
Chris@1:        19) [current\_class\_number] = vector [floor1\_partition\_class\_list] element [i]
Chris@1:        20) iterate [j] over the range 0 ... ([floor1\_class\_dimensions] element [current\_class\_number])-1 \{
Chris@1:              21) vector [floor1\_X\_list] element ([floor1\_values]) =
Chris@1:                  read [rangebits] bits as unsigned integer
Chris@1:              22) increment [floor1\_values] by one
Chris@1:            \}
Chris@1:      \}
Chris@1: 
Chris@1:  23) done
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: An end-of-packet condition while reading any aspect of a floor 1
Chris@1: configuration during setup renders a stream undecodable.  In addition,
Chris@1: a \varname{[floor1\_class\_masterbooks]} or
Chris@1: \varname{[floor1\_subclass\_books]} scalar element greater than the
Chris@1: highest numbered codebook configured in this stream is an error
Chris@1: condition that renders the stream undecodable.  Vector
Chris@1: [floor1\_x\_list] is limited to a maximum length of 65 elements; a
Chris@1: setup indicating more than 65 total elements (including elements 0 and
Chris@1: 1 set prior to the read loop) renders the stream undecodable.  All
Chris@1: vector [floor1\_x\_list] element values must be unique within the
Chris@1: vector; a non-unique value renders the stream undecodable.
Chris@1: 
Chris@1: \subsubsection{packet decode} \label{vorbis:spec:floor1-decode}
Chris@1: 
Chris@1: Packet decode begins by checking the \varname{[nonzero]} flag:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [nonzero] = read 1 bit as boolean
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: If \varname{[nonzero]} is unset, that indicates this channel contained
Chris@1: no audio energy in this frame.  Decode immediately returns a status
Chris@1: indicating this floor curve (and thus this channel) is unused this
Chris@1: frame.  (A return status of 'unused' is different from decoding a
Chris@1: floor that has all points set to minimum representation amplitude,
Chris@1: which happens to be approximately -140dB).
Chris@1: 
Chris@1: 
Chris@1: Assuming \varname{[nonzero]} is set, decode proceeds as follows:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [range] = vector \{ 256, 128, 86, 64 \} element ([floor1\_multiplier]-1)
Chris@1:   2) vector [floor1\_Y] element [0] = read \link{vorbis:spec:ilog}{ilog}([range]-1) bits as unsigned integer
Chris@1:   3) vector [floor1\_Y] element [1] = read \link{vorbis:spec:ilog}{ilog}([range]-1) bits as unsigned integer
Chris@1:   4) [offset] = 2;
Chris@1:   5) iterate [i] over the range 0 ... [floor1\_partitions]-1 \{
Chris@1: 
Chris@1:        6) [class] = vector [floor1\_partition\_class]  element [i]
Chris@1:        7) [cdim]  = vector [floor1\_class\_dimensions] element [class]
Chris@1:        8) [cbits] = vector [floor1\_class\_subclasses] element [class]
Chris@1:        9) [csub]  = (2 exponent [cbits])-1
Chris@1:       10) [cval]  = 0
Chris@1:       11) if ( [cbits] is greater than zero ) \{
Chris@1: 
Chris@1:              12) [cval] = read from packet using codebook number
Chris@1:                  (vector [floor1\_class\_masterbooks] element [class]) in scalar context
Chris@1:           \}
Chris@1: 
Chris@1:       13) iterate [j] over the range 0 ... [cdim]-1 \{
Chris@1: 
Chris@1:              14) [book] = array [floor1\_subclass\_books] element [class],([cval] bitwise AND [csub])
Chris@1:              15) [cval] = [cval] right shifted [cbits] bits
Chris@1: 	     16) if ( [book] is not less than zero ) \{
Chris@1: 
Chris@1: 	           17) vector [floor1\_Y] element ([j]+[offset]) = read from packet using codebook
Chris@1:                        [book] in scalar context
Chris@1: 
Chris@1:                  \} else [book] is less than zero \{
Chris@1: 
Chris@1: 	           18) vector [floor1\_Y] element ([j]+[offset]) = 0
Chris@1: 
Chris@1:                  \}
Chris@1:           \}
Chris@1: 
Chris@1:       19) [offset] = [offset] + [cdim]
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:  20) done
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: An end-of-packet condition during curve decode should be considered a
Chris@1: nominal occurrence; if end-of-packet is reached during any read
Chris@1: operation above, floor decode is to return 'unused' status as if the
Chris@1: \varname{[nonzero]} flag had been unset at the beginning of decode.
Chris@1: 
Chris@1: 
Chris@1: Vector \varname{[floor1\_Y]} contains the values from packet decode
Chris@1: needed for floor 1 synthesis.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{curve computation} \label{vorbis:spec:floor1-synth}
Chris@1: 
Chris@1: Curve computation is split into two logical steps; the first step
Chris@1: derives final Y amplitude values from the encoded, wrapped difference
Chris@1: values taken from the bitstream.  The second step plots the curve
Chris@1: lines.  Also, although zero-difference values are used in the
Chris@1: iterative prediction to find final Y values, these points are
Chris@1: conditionally skipped during final line computation in step two.
Chris@1: Skipping zero-difference values allows a smoother line fit.
Chris@1: 
Chris@1: Although some aspects of the below algorithm look like inconsequential
Chris@1: optimizations, implementors are warned to follow the details closely.
Chris@1: Deviation from implementing a strictly equivalent algorithm can result
Chris@1: in serious decoding errors.
Chris@1: 
Chris@1: {\em Additional note:} Although \varname{[floor1\_final\_Y]} values in
Chris@1: the prediction loop and at the end of step 1 are inherently limited by
Chris@1: the prediction algorithm to [0, \varname{[range]}), it is possible to
Chris@1:   abuse the setup and codebook machinery to produce negative or
Chris@1:   over-range results.  We suggest that decoder implementations guard
Chris@1:   the values in vector \varname{[floor1\_final\_Y]} by clamping each
Chris@1:   element to [0, \varname{[range]}) after step 1.  Variants of this
Chris@1:     suggestion are acceptable as valid floor1 setups cannot produce
Chris@1:     out of range values.
Chris@1: 
Chris@1: \begin{description}
Chris@1: \item[step 1: amplitude value synthesis]
Chris@1: 
Chris@1: Unwrap the always-positive-or-zero values read from the packet into
Chris@1: +/- difference values, then apply to line prediction.
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [range] = vector \{ 256, 128, 86, 64 \} element ([floor1\_multiplier]-1)
Chris@1:   2) vector [floor1\_step2\_flag] element [0] = set
Chris@1:   3) vector [floor1\_step2\_flag] element [1] = set
Chris@1:   4) vector [floor1\_final\_Y] element [0] = vector [floor1\_Y] element [0]
Chris@1:   5) vector [floor1\_final\_Y] element [1] = vector [floor1\_Y] element [1]
Chris@1:   6) iterate [i] over the range 2 ... [floor1\_values]-1 \{
Chris@1: 
Chris@1:        7) [low\_neighbor\_offset] = \link{vorbis:spec:low:neighbor}{low\_neighbor}([floor1\_X\_list],[i])
Chris@1:        8) [high\_neighbor\_offset] = \link{vorbis:spec:high:neighbor}{high\_neighbor}([floor1\_X\_list],[i])
Chris@1: 
Chris@1:        9) [predicted] = \link{vorbis:spec:render:point}{render\_point}( vector [floor1\_X\_list] element [low\_neighbor\_offset],
Chris@1: 				      vector [floor1\_final\_Y] element [low\_neighbor\_offset],
Chris@1:                                       vector [floor1\_X\_list] element [high\_neighbor\_offset],
Chris@1: 				      vector [floor1\_final\_Y] element [high\_neighbor\_offset],
Chris@1:                                       vector [floor1\_X\_list] element [i] )
Chris@1: 
Chris@1:       10) [val] = vector [floor1\_Y] element [i]
Chris@1:       11) [highroom] = [range] - [predicted]
Chris@1:       12) [lowroom]  = [predicted]
Chris@1:       13) if ( [highroom] is less than [lowroom] ) \{
Chris@1: 
Chris@1:             14) [room] = [highroom] * 2
Chris@1: 
Chris@1:           \} else [highroom] is not less than [lowroom] \{
Chris@1: 
Chris@1:             15) [room] = [lowroom] * 2
Chris@1: 
Chris@1:           \}
Chris@1: 
Chris@1:       16) if ( [val] is nonzero ) \{
Chris@1: 
Chris@1:             17) vector [floor1\_step2\_flag] element [low\_neighbor\_offset] = set
Chris@1:             18) vector [floor1\_step2\_flag] element [high\_neighbor\_offset] = set
Chris@1:             19) vector [floor1\_step2\_flag] element [i] = set
Chris@1:             20) if ( [val] is greater than or equal to [room] ) \{
Chris@1: 
Chris@1:                   21) if ( [highroom] is greater than [lowroom] ) \{
Chris@1: 
Chris@1:                         22) vector [floor1\_final\_Y] element [i] = [val] - [lowroom] + [predicted]
Chris@1: 
Chris@1: 		      \} else [highroom] is not greater than [lowroom] \{
Chris@1: 
Chris@1:                         23) vector [floor1\_final\_Y] element [i] = [predicted] - [val] + [highroom] - 1
Chris@1: 
Chris@1:                       \}
Chris@1: 
Chris@1:                 \} else [val] is less than [room] \{
Chris@1: 
Chris@1:                     24) if ([val] is odd) \{
Chris@1: 
Chris@1:                         25) vector [floor1\_final\_Y] element [i] =
Chris@1:                             [predicted] - (([val] + 1) divided by  2 using integer division)
Chris@1: 
Chris@1:                       \} else [val] is even \{
Chris@1: 
Chris@1:                         26) vector [floor1\_final\_Y] element [i] =
Chris@1:                             [predicted] + ([val] / 2 using integer division)
Chris@1: 
Chris@1:                       \}
Chris@1: 
Chris@1:                 \}
Chris@1: 
Chris@1:           \} else [val] is zero \{
Chris@1: 
Chris@1:             27) vector [floor1\_step2\_flag] element [i] = unset
Chris@1:             28) vector [floor1\_final\_Y] element [i] = [predicted]
Chris@1: 
Chris@1:           \}
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:  29) done
Chris@1: 
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \item[step 2: curve synthesis]
Chris@1: 
Chris@1: Curve synthesis generates a return vector \varname{[floor]} of length
Chris@1: \varname{[n]} (where \varname{[n]} is provided by the decode process
Chris@1: calling to floor decode).  Floor 1 curve synthesis makes use of the
Chris@1: \varname{[floor1\_X\_list]}, \varname{[floor1\_final\_Y]} and
Chris@1: \varname{[floor1\_step2\_flag]} vectors, as well as [floor1\_multiplier]
Chris@1: and [floor1\_values] values.
Chris@1: 
Chris@1: Decode begins by sorting the scalars from vectors
Chris@1: \varname{[floor1\_X\_list]}, \varname{[floor1\_final\_Y]} and
Chris@1: \varname{[floor1\_step2\_flag]} together into new vectors
Chris@1: \varname{[floor1\_X\_list]'}, \varname{[floor1\_final\_Y]'} and
Chris@1: \varname{[floor1\_step2\_flag]'} according to ascending sort order of the
Chris@1: values in \varname{[floor1\_X\_list]}.  That is, sort the values of
Chris@1: \varname{[floor1\_X\_list]} and then apply the same permutation to
Chris@1: elements of the other two vectors so that the X, Y and step2\_flag
Chris@1: values still match.
Chris@1: 
Chris@1: Then compute the final curve in one pass:
Chris@1: 
Chris@1: \begin{Verbatim}[commandchars=\\\{\}]
Chris@1:   1) [hx] = 0
Chris@1:   2) [lx] = 0
Chris@1:   3) [ly] = vector [floor1\_final\_Y]' element [0] * [floor1\_multiplier]
Chris@1:   4) iterate [i] over the range 1 ... [floor1\_values]-1 \{
Chris@1: 
Chris@1:        5) if ( [floor1\_step2\_flag]' element [i] is set ) \{
Chris@1: 
Chris@1:              6) [hy] = [floor1\_final\_Y]' element [i] * [floor1\_multiplier]
Chris@1:  	     7) [hx] = [floor1\_X\_list]' element [i]
Chris@1:              8) \link{vorbis:spec:render:line}{render\_line}( [lx], [ly], [hx], [hy], [floor] )
Chris@1:              9) [lx] = [hx]
Chris@1: 	    10) [ly] = [hy]
Chris@1:           \}
Chris@1:      \}
Chris@1: 
Chris@1:  11) if ( [hx] is less than [n] ) \{
Chris@1: 
Chris@1:         12) \link{vorbis:spec:render:line}{render\_line}( [hx], [hy], [n], [hy], [floor] )
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:  13) if ( [hx] is greater than [n] ) \{
Chris@1: 
Chris@1:             14) truncate vector [floor] to [n] elements
Chris@1: 
Chris@1:      \}
Chris@1: 
Chris@1:  15) for each scalar in vector [floor], perform a lookup substitution using
Chris@1:      the scalar value from [floor] as an offset into the vector \link{vorbis:spec:floor1:inverse:dB:table}{[floor1\_inverse\_dB\_static\_table]}
Chris@1: 
Chris@1:  16) done
Chris@1: 
Chris@1: \end{Verbatim}
Chris@1: 
Chris@1: \end{description}




