1 % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
2 %!TEX root = Vorbis_I_spec.tex
4 \section{Helper equations} \label{vorbis:spec:helper}
8 The equations below are used in multiple places by the Vorbis codec
9 specification. Rather than cluttering up the main specification
10 documents, they are defined here and referenced where appropriate.
13 \subsection{Functions}
15 \subsubsection{ilog} \label{vorbis:spec:ilog}
17 The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
18 \varname{[x]}. Values of \varname{[x]} less than zero are defined to return zero.
20 \begin{programlisting}
21 1) [return_value] = 0;
22 2) if ( [x] is greater than zero ) {
24 3) increment [return_value];
25 4) logical shift [x] one bit to the right, padding the MSb with zero
42 \item ilog(negative number) = 0;
48 \subsubsection{float32_unpack} \label{vorbis:spec:float32:unpack}
50 "float32_unpack(x)" is intended to translate the packed binary
51 representation of a Vorbis codebook float value into the
52 representation used by the decoder for floating point numbers. For
53 purposes of this example, we will unpack a Vorbis float32 into a
54 host-native floating point number.
56 \begin{programlisting}
57 1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
58 2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
59 3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
60 4) if ( [sign] is nonzero ) then negate [mantissa]
61 5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
66 \subsubsection{lookup1_values} \label{vorbis:spec:lookup1:values}
68 "lookup1_values(codebook_entries,codebook_dimensions)" is used to
69 compute the correct length of the value index for a codebook VQ lookup
70 table of lookup type 1. The values on this list are permuted to
71 construct the VQ vector lookup table of size
72 \varname{[codebook_entries]}.
74 The return value for this function is defined to be 'the greatest
75 integer value for which \varname{[return_value]} to the power of
76 \varname{[codebook_dimensions]} is less than or equal to
77 \varname{[codebook_entries]}'.
81 \subsubsection{low_neighbor} \label{vorbis:spec:low:neighbor}
83 "low_neighbor(v,x)" finds the position \varname{n} in vector \varname{[v]} of
84 the greatest value scalar element for which \varname{n} is less than
85 \varname{[x]} and vector \varname{[v]} element \varname{n} is less
86 than vector \varname{[v]} element \varname{[x]}.
88 \subsubsection{high_neighbor} \label{vorbis:spec:high:neighbor}
90 "high_neighbor(v,x)" finds the position \varname{n} in vector [v] of
91 the lowest value scalar element for which \varname{n} is less than
92 \varname{[x]} and vector \varname{[v]} element \varname{n} is greater
93 than vector \varname{[v]} element \varname{[x]}.
97 \subsubsection{render_point} \label{vorbis:spec:render:point}
99 "render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
100 along the line specified by x0, x1, y0 and y1. This function uses an
101 integer algorithm to solve for the point directly without calculating
102 intervening values along the line.
104 \begin{programlisting}
105 1) [dy] = [y1] - [y0]
106 2) [adx] = [x1] - [x0]
107 3) [ady] = absolute value of [dy]
108 4) [err] = [ady] * ([X] - [x0])
109 5) [off] = [err] / [adx] using integer division
110 6) if ( [dy] is less than zero ) {
112 7) [Y] = [y0] - [off]
116 8) [Y] = [y0] + [off]
125 \subsubsection{render_line} \label{vorbis:spec:render:line}
127 Floor decode type one uses the integer line drawing algorithm of
128 "render_line(x0, y0, x1, y1, v)" to construct an integer floor
129 curve for contiguous piecewise line segments. Note that it has not
130 been relevant elsewhere, but here we must define integer division as
131 rounding division of both positive and negative numbers toward zero.
134 \begin{programlisting}
135 1) [dy] = [y1] - [y0]
136 2) [adx] = [x1] - [x0]
137 3) [ady] = absolute value of [dy]
138 4) [base] = [dy] / [adx] using integer division
143 8) if ( [dy] is less than 0 ) {
149 10) [sy] = [base] + 1
153 11) [ady] = [ady] - (absolute value of [base]) * [adx]
154 12) vector [v] element [x] = [y]
156 13) iterate [x] over the range [x0]+1 ... [x1]-1 {
158 14) [err] = [err] + [ady];
159 15) if ( [err] >= [adx] ) {
161 16) [err] = [err] - [adx]
166 18) [y] = [y] + [base]
170 19) vector [v] element [x] = [y]