
Chris@1: % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Chris@1: %!TEX root = Vorbis_I_spec.tex
Chris@1: % $Id$
Chris@1: \section{Helper equations} \label{vorbis:spec:helper}
Chris@1: 
Chris@1: \subsection{Overview}
Chris@1: 
Chris@1: The equations below are used in multiple places by the Vorbis codec
Chris@1: specification.  Rather than cluttering up the main specification
Chris@1: documents, they are defined here and referenced where appropriate.
Chris@1: 
Chris@1: 
Chris@1: \subsection{Functions}
Chris@1: 
Chris@1: \subsubsection{ilog} \label{vorbis:spec:ilog}
Chris@1: 
Chris@1: The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
Chris@1: \varname{[x]}.  Values of \varname{[x]} less than zero are defined to return zero.
Chris@1: 
Chris@1: \begin{programlisting}
Chris@1:   1) [return\_value] = 0;
Chris@1:   2) if ( [x] is greater than zero ) {
Chris@1: 
Chris@1:        3) increment [return\_value];
Chris@1:        4) logical shift [x] one bit to the right, padding the MSb with zero
Chris@1:        5) repeat at step 2)
Chris@1: 
Chris@1:      }
Chris@1: 
Chris@1:    6) done
Chris@1: \end{programlisting}
Chris@1: 
Chris@1: Examples:
Chris@1: 
Chris@1: \begin{itemize}
Chris@1:  \item ilog(0) = 0;
Chris@1:  \item ilog(1) = 1;
Chris@1:  \item ilog(2) = 2;
Chris@1:  \item ilog(3) = 2;
Chris@1:  \item ilog(4) = 3;
Chris@1:  \item ilog(7) = 3;
Chris@1:  \item ilog(negative number) = 0;
Chris@1: \end{itemize}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{float32\_unpack} \label{vorbis:spec:float32:unpack}
Chris@1: 
Chris@1: "float32\_unpack(x)" is intended to translate the packed binary
Chris@1: representation of a Vorbis codebook float value into the
Chris@1: representation used by the decoder for floating point numbers.  For
Chris@1: purposes of this example, we will unpack a Vorbis float32 into a
Chris@1: host-native floating point number.
Chris@1: 
Chris@1: \begin{programlisting}
Chris@1:   1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
Chris@1:   2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
Chris@1:   3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
Chris@1:   4) if ( [sign] is nonzero ) then negate [mantissa]
Chris@1:   5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
Chris@1: \end{programlisting}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{lookup1\_values} \label{vorbis:spec:lookup1:values}
Chris@1: 
Chris@1: "lookup1\_values(codebook\_entries,codebook\_dimensions)" is used to
Chris@1: compute the correct length of the value index for a codebook VQ lookup
Chris@1: table of lookup type 1.  The values on this list are permuted to
Chris@1: construct the VQ vector lookup table of size
Chris@1: \varname{[codebook\_entries]}.
Chris@1: 
Chris@1: The return value for this function is defined to be 'the greatest
Chris@1: integer value for which \varname{[return\_value]} to the power of
Chris@1: \varname{[codebook\_dimensions]} is less than or equal to
Chris@1: \varname{[codebook\_entries]}'.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{low\_neighbor} \label{vorbis:spec:low:neighbor}
Chris@1: 
Chris@1: "low\_neighbor(v,x)" finds the position \varname{n} in vector \varname{[v]} of
Chris@1: the greatest value scalar element for which \varname{n} is less than
Chris@1: \varname{[x]} and vector \varname{[v]} element \varname{n} is less
Chris@1: than vector \varname{[v]} element \varname{[x]}.
Chris@1: 
Chris@1: \subsubsection{high\_neighbor} \label{vorbis:spec:high:neighbor}
Chris@1: 
Chris@1: "high\_neighbor(v,x)" finds the position \varname{n} in vector [v] of
Chris@1: the lowest value scalar element for which \varname{n} is less than
Chris@1: \varname{[x]} and vector \varname{[v]} element \varname{n} is greater
Chris@1: than vector \varname{[v]} element \varname{[x]}.
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{render\_point} \label{vorbis:spec:render:point}
Chris@1: 
Chris@1: "render\_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
Chris@1: along the line specified by x0, x1, y0 and y1.  This function uses an
Chris@1: integer algorithm to solve for the point directly without calculating
Chris@1: intervening values along the line.
Chris@1: 
Chris@1: \begin{programlisting}
Chris@1:   1)  [dy] = [y1] - [y0]
Chris@1:   2) [adx] = [x1] - [x0]
Chris@1:   3) [ady] = absolute value of [dy]
Chris@1:   4) [err] = [ady] * ([X] - [x0])
Chris@1:   5) [off] = [err] / [adx] using integer division
Chris@1:   6) if ( [dy] is less than zero ) {
Chris@1: 
Chris@1:        7) [Y] = [y0] - [off]
Chris@1: 
Chris@1:      } else {
Chris@1: 
Chris@1:        8) [Y] = [y0] + [off]
Chris@1: 
Chris@1:      }
Chris@1: 
Chris@1:   9) done
Chris@1: \end{programlisting}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: \subsubsection{render\_line} \label{vorbis:spec:render:line}
Chris@1: 
Chris@1: Floor decode type one uses the integer line drawing algorithm of
Chris@1: "render\_line(x0, y0, x1, y1, v)" to construct an integer floor
Chris@1: curve for contiguous piecewise line segments. Note that it has not
Chris@1: been relevant elsewhere, but here we must define integer division as
Chris@1: rounding division of both positive and negative numbers toward zero.
Chris@1: 
Chris@1: 
Chris@1: \begin{programlisting}
Chris@1:   1)   [dy] = [y1] - [y0]
Chris@1:   2)  [adx] = [x1] - [x0]
Chris@1:   3)  [ady] = absolute value of [dy]
Chris@1:   4) [base] = [dy] / [adx] using integer division
Chris@1:   5)    [x] = [x0]
Chris@1:   6)    [y] = [y0]
Chris@1:   7)  [err] = 0
Chris@1: 
Chris@1:   8) if ( [dy] is less than 0 ) {
Chris@1: 
Chris@1:         9) [sy] = [base] - 1
Chris@1: 
Chris@1:      } else {
Chris@1: 
Chris@1:        10) [sy] = [base] + 1
Chris@1: 
Chris@1:      }
Chris@1: 
Chris@1:  11) [ady] = [ady] - (absolute value of [base]) * [adx]
Chris@1:  12) vector [v] element [x] = [y]
Chris@1: 
Chris@1:  13) iterate [x] over the range [x0]+1 ... [x1]-1 {
Chris@1: 
Chris@1:        14) [err] = [err] + [ady];
Chris@1:        15) if ( [err] >= [adx] ) {
Chris@1: 
Chris@1:              16) [err] = [err] - [adx]
Chris@1:              17)   [y] = [y] + [sy]
Chris@1: 
Chris@1:            } else {
Chris@1: 
Chris@1:              18) [y] = [y] + [base]
Chris@1: 
Chris@1:            }
Chris@1: 
Chris@1:        19) vector [v] element [x] = [y]
Chris@1: 
Chris@1:      }
Chris@1: \end{programlisting}
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 
Chris@1: 




