<?xml version="1.0" standalone="no"?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
                "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [

]>

<section id="vorbis-spec-helper">
<sectioninfo>
<releaseinfo>
 $Id: 09-helper.xml,v 1.1 2007/03/21 12:44:11 glass Exp $
</releaseinfo>
</sectioninfo>
<title>Helper equations</title>

<section>
<title>Overview</title>

<para>
The equations below are used in multiple places by the Vorbis codec
specification.  Rather than cluttering up the main specification
documents, they are defined here and referenced where appropriate.
</para>

</section>

<section>
<title>Functions</title>

<section id="vorbis-spec-ilog">
<title>ilog</title>

<para>
The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
<varname>[x]</varname>.  Values of <varname>[x]</varname> less than zero are defined to return zero.</para>

<programlisting>
  1) [return_value] = 0;
  2) if ( [x] is greater than zero ){
      
       3) increment [return_value];
       4) logical shift [x] one bit to the right, padding the MSb with zero
       5) repeat at step 2)

     }

   6) done
</programlisting>

<para>
Examples:

<itemizedlist>
 <listitem><simpara>ilog(0) = 0;</simpara></listitem>
 <listitem><simpara>ilog(1) = 1;</simpara></listitem>
 <listitem><simpara>ilog(2) = 2;</simpara></listitem>
 <listitem><simpara>ilog(3) = 2;</simpara></listitem>
 <listitem><simpara>ilog(4) = 3;</simpara></listitem>
 <listitem><simpara>ilog(7) = 3;</simpara></listitem>
 <listitem><simpara>ilog(negative number) = 0;</simpara></listitem>
</itemizedlist>
</para>

</section>

<section id="vorbis-spec-float32_unpack">
<title>float32_unpack</title>

<para>
"float32_unpack(x)" is intended to translate the packed binary
representation of a Vorbis codebook float value into the
representation used by the decoder for floating point numbers.  For
purposes of this example, we will unpack a Vorbis float32 into a
host-native floating point number.</para>

<programlisting>
  1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
  2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
  3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
  4) if ( [sign] is nonzero ) then negate [mantissa]
  5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
</programlisting>

</section>

<section id="vorbis-spec-lookup1_values">
<title>lookup1_values</title>

<para>
"lookup1_values(codebook_entries,codebook_dimensions)" is used to
compute the correct length of the value index for a codebook VQ lookup
table of lookup type 1.  The values on this list are permuted to
construct the VQ vector lookup table of size
<varname>[codebook_entries]</varname>.</para>

<para>
The return value for this function is defined to be 'the greatest
integer value for which <varname>[return_value] to the power of
[codebook_dimensions] is less than or equal to
[codebook_entries]</varname>'.</para>

</section>

<section id="vorbis-spec-low_neighbor">
<title>low_neighbor</title>

<para>
"low_neighbor(v,x)" finds the position <varname>n</varname> in vector <varname>[v]</varname> of
the greatest value scalar element for which <varname>n</varname> is less than
<varname>[x]</varname> and vector <varname>[v]</varname> element <varname>n</varname> is less
than vector <varname>[v]</varname> element <varname>[x]</varname>.</para>

<section id="vorbis-spec-high_neighbor">
<title>high_neighbor</title>

<para>
"high_neighbor(v,x)" finds the position <varname>n</varname> in vector [v] of
the lowest value scalar element for which <varname>n</varname> is less than
<varname>[x]</varname> and vector <varname>[v]</varname> element <varname>n</varname> is greater
than vector <varname>[v]</varname> element <varname>[x]</varname>.</para>

</section>

<section id="vorbis-spec-render_point">
<title>render_point</title>

<para>
"render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
along the line specified by x0, x1, y0 and y1.  This function uses an
integer algorithm to solve for the point directly without calculating
intervening values along the line.</para>

<programlisting>
  1)  [dy] = [y1] - [y0]
  2) [adx] = [x1] - [x0]
  3) [ady] = absolute value of [dy]
  4) [err] = [ady] * ([X] - [x0])
  5) [off] = [err] / [adx] using integer division
  6) if ( [dy] is less than zero ) {

       7) [Y] = [y0] - [off]

     } else {

       8) [Y] = [y0] + [off]
  
     }

  9) done
</programlisting>

</section>

<section id="vorbis-spec-render_line">
<title>render_line</title>

<para>
Floor decode type one uses the integer line drawing algorithm of
"render_line(x0, y0, x1, y1, v)" to construct an integer floor
curve for contiguous piecewise line segments. Note that it has not
been relevant elsewhere, but here we must define integer division as
rounding division of both positive and negative numbers toward zero.
</para>

<programlisting>
  1)   [dy] = [y1] - [y0]
  2)  [adx] = [x1] - [x0]
  3)  [ady] = absolute value of [dy]
  4) [base] = [dy] / [adx] using integer division
  5)    [x] = [x0]
  6)    [y] = [y0]
  7)  [err] = 0

  8) if ( [dy] is less than 0 ) {

        9) [sy] = [base] - 1

     } else {

       10) [sy] = [base] + 1

     }

 11) [ady] = [ady] - (absolute value of [base]) * [adx]
 12) vector [v] element [x] = [y]

 13) iterate [x] over the range [x0]+1 ... [x1]-1 {

       14) [err] = [err] + [ady];
       15) if ( [err] >= [adx] ) {

             16) [err] = [err] - [adx]
             17)   [y] = [y] + [sy]

           } else {

             18) [y] = [y] + [base]
   
           }

       19) vector [v] element [x] = [y]

     }
</programlisting>

</section>

</section>

</section>

</section>
