1% -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
2%!TEX root = Vorbis_I_spec.tex
3% $Id$
4\section{Helper equations} \label{vorbis:spec:helper}
5
6\subsection{Overview}
7
8The equations below are used in multiple places by the Vorbis codec
9specification.  Rather than cluttering up the main specification
10documents, they are defined here and referenced where appropriate.
11
12
13\subsection{Functions}
14
15\subsubsection{ilog} \label{vorbis:spec:ilog}
16
17The "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.
19
20\begin{programlisting}
21  1) [return_value] = 0;
22  2) if ( [x] is greater than zero ) {
23
24       3) increment [return_value];
25       4) logical shift [x] one bit to the right, padding the MSb with zero
26       5) repeat at step 2)
27
28     }
29
30   6) done
31\end{programlisting}
32
33Examples:
34
35\begin{itemize}
36 \item ilog(0) = 0;
37 \item ilog(1) = 1;
38 \item ilog(2) = 2;
39 \item ilog(3) = 2;
40 \item ilog(4) = 3;
41 \item ilog(7) = 3;
42 \item ilog(negative number) = 0;
43\end{itemize}
44
45
46
47
48\subsubsection{float32_unpack} \label{vorbis:spec:float32:unpack}
49
50"float32_unpack(x)" is intended to translate the packed binary
51representation of a Vorbis codebook float value into the
52representation used by the decoder for floating point numbers.  For
53purposes of this example, we will unpack a Vorbis float32 into a
54host-native floating point number.
55
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 ) )
62\end{programlisting}
63
64
65
66\subsubsection{lookup1_values} \label{vorbis:spec:lookup1:values}
67
68"lookup1_values(codebook_entries,codebook_dimensions)" is used to
69compute the correct length of the value index for a codebook VQ lookup
70table of lookup type 1.  The values on this list are permuted to
71construct the VQ vector lookup table of size
72\varname{[codebook_entries]}.
73
74The return value for this function is defined to be 'the greatest
75integer value for which \varname{[return_value]} to the power of
76\varname{[codebook_dimensions]} is less than or equal to
77\varname{[codebook_entries]}'.
78
79
80
81\subsubsection{low_neighbor} \label{vorbis:spec:low:neighbor}
82
83"low_neighbor(v,x)" finds the position \varname{n} in vector \varname{[v]} of
84the greatest value scalar element for which \varname{n} is less than
85\varname{[x]} and vector \varname{[v]} element \varname{n} is less
86than vector \varname{[v]} element \varname{[x]}.
87
88\subsubsection{high_neighbor} \label{vorbis:spec:high:neighbor}
89
90"high_neighbor(v,x)" finds the position \varname{n} in vector [v] of
91the lowest value scalar element for which \varname{n} is less than
92\varname{[x]} and vector \varname{[v]} element \varname{n} is greater
93than vector \varname{[v]} element \varname{[x]}.
94
95
96
97\subsubsection{render_point} \label{vorbis:spec:render:point}
98
99"render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
100along the line specified by x0, x1, y0 and y1.  This function uses an
101integer algorithm to solve for the point directly without calculating
102intervening values along the line.
103
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 ) {
111
112       7) [Y] = [y0] - [off]
113
114     } else {
115
116       8) [Y] = [y0] + [off]
117
118     }
119
120  9) done
121\end{programlisting}
122
123
124
125\subsubsection{render_line} \label{vorbis:spec:render:line}
126
127Floor decode type one uses the integer line drawing algorithm of
128"render_line(x0, y0, x1, y1, v)" to construct an integer floor
129curve for contiguous piecewise line segments. Note that it has not
130been relevant elsewhere, but here we must define integer division as
131rounding division of both positive and negative numbers toward zero.
132
133
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
139  5)    [x] = [x0]
140  6)    [y] = [y0]
141  7)  [err] = 0
142
143  8) if ( [dy] is less than 0 ) {
144
145        9) [sy] = [base] - 1
146
147     } else {
148
149       10) [sy] = [base] + 1
150
151     }
152
153 11) [ady] = [ady] - (absolute value of [base]) * [adx]
154 12) vector [v] element [x] = [y]
155
156 13) iterate [x] over the range [x0]+1 ... [x1]-1 {
157
158       14) [err] = [err] + [ady];
159       15) if ( [err] >= [adx] ) {
160
161             16) [err] = [err] - [adx]
162             17)   [y] = [y] + [sy]
163
164           } else {
165
166             18) [y] = [y] + [base]
167
168           }
169
170       19) vector [v] element [x] = [y]
171
172     }
173\end{programlisting}
174
175
176
177
178
179
180
181
182