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