09-helper.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?xml version="1.0" standalone="no"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
  4. ]>
  5. <section id="vorbis-spec-helper">
  6. <sectioninfo>
  7. <releaseinfo>
  8. $Id$
  9. </releaseinfo>
  10. </sectioninfo>
  11. <title>Helper equations</title>
  12. <section>
  13. <title>Overview</title>
  14. <para>
  15. The equations below are used in multiple places by the Vorbis codec
  16. specification. Rather than cluttering up the main specification
  17. documents, they are defined here and referenced where appropriate.
  18. </para>
  19. </section>
  20. <section>
  21. <title>Functions</title>
  22. <section id="vorbis-spec-ilog">
  23. <title>ilog</title>
  24. <para>
  25. The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
  26. <varname>[x]</varname>. Values of <varname>[x]</varname> less than zero are defined to return zero.</para>
  27. <programlisting>
  28. 1) [return_value] = 0;
  29. 2) if ( [x] is greater than zero ){
  30. 3) increment [return_value];
  31. 4) logical shift [x] one bit to the right, padding the MSb with zero
  32. 5) repeat at step 2)
  33. }
  34. 6) done
  35. </programlisting>
  36. <para>
  37. Examples:
  38. <itemizedlist>
  39. <listitem><simpara>ilog(0) = 0;</simpara></listitem>
  40. <listitem><simpara>ilog(1) = 1;</simpara></listitem>
  41. <listitem><simpara>ilog(2) = 2;</simpara></listitem>
  42. <listitem><simpara>ilog(3) = 2;</simpara></listitem>
  43. <listitem><simpara>ilog(4) = 3;</simpara></listitem>
  44. <listitem><simpara>ilog(7) = 3;</simpara></listitem>
  45. <listitem><simpara>ilog(negative number) = 0;</simpara></listitem>
  46. </itemizedlist>
  47. </para>
  48. </section>
  49. <section id="vorbis-spec-float32_unpack">
  50. <title>float32_unpack</title>
  51. <para>
  52. "float32_unpack(x)" is intended to translate the packed binary
  53. representation of a Vorbis codebook float value into the
  54. representation used by the decoder for floating point numbers. For
  55. purposes of this example, we will unpack a Vorbis float32 into a
  56. host-native floating point number.</para>
  57. <programlisting>
  58. 1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
  59. 2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
  60. 3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
  61. 4) if ( [sign] is nonzero ) then negate [mantissa]
  62. 5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
  63. </programlisting>
  64. </section>
  65. <section id="vorbis-spec-lookup1_values">
  66. <title>lookup1_values</title>
  67. <para>
  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]</varname>.</para>
  73. <para>
  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. [codebook_dimensions] is less than or equal to
  77. [codebook_entries]</varname>'.</para>
  78. </section>
  79. <section id="vorbis-spec-low_neighbor">
  80. <title>low_neighbor</title>
  81. <para>
  82. "low_neighbor(v,x)" finds the position <varname>n</varname> in vector <varname>[v]</varname> of
  83. the greatest value scalar element for which <varname>n</varname> is less than
  84. <varname>[x]</varname> and vector <varname>[v]</varname> element <varname>n</varname> is less
  85. than vector <varname>[v]</varname> element <varname>[x]</varname>.</para>
  86. <section id="vorbis-spec-high_neighbor">
  87. <title>high_neighbor</title>
  88. <para>
  89. "high_neighbor(v,x)" finds the position <varname>n</varname> in vector [v] of
  90. the lowest value scalar element for which <varname>n</varname> is less than
  91. <varname>[x]</varname> and vector <varname>[v]</varname> element <varname>n</varname> is greater
  92. than vector <varname>[v]</varname> element <varname>[x]</varname>.</para>
  93. </section>
  94. <section id="vorbis-spec-render_point">
  95. <title>render_point</title>
  96. <para>
  97. "render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
  98. along the line specified by x0, x1, y0 and y1. This function uses an
  99. integer algorithm to solve for the point directly without calculating
  100. intervening values along the line.</para>
  101. <programlisting>
  102. 1) [dy] = [y1] - [y0]
  103. 2) [adx] = [x1] - [x0]
  104. 3) [ady] = absolute value of [dy]
  105. 4) [err] = [ady] * ([X] - [x0])
  106. 5) [off] = [err] / [adx] using integer division
  107. 6) if ( [dy] is less than zero ) {
  108. 7) [Y] = [y0] - [off]
  109. } else {
  110. 8) [Y] = [y0] + [off]
  111. }
  112. 9) done
  113. </programlisting>
  114. </section>
  115. <section id="vorbis-spec-render_line">
  116. <title>render_line</title>
  117. <para>
  118. Floor decode type one uses the integer line drawing algorithm of
  119. "render_line(x0, y0, x1, y1, v)" to construct an integer floor
  120. curve for contiguous piecewise line segments. Note that it has not
  121. been relevant elsewhere, but here we must define integer division as
  122. rounding division of both positive and negative numbers toward zero.
  123. </para>
  124. <programlisting>
  125. 1) [dy] = [y1] - [y0]
  126. 2) [adx] = [x1] - [x0]
  127. 3) [ady] = absolute value of [dy]
  128. 4) [base] = [dy] / [adx] using integer division
  129. 5) [x] = [x0]
  130. 6) [y] = [y0]
  131. 7) [err] = 0
  132. 8) if ( [dy] is less than 0 ) {
  133. 9) [sy] = [base] - 1
  134. } else {
  135. 10) [sy] = [base] + 1
  136. }
  137. 11) [ady] = [ady] - (absolute value of [base]) * [adx]
  138. 12) vector [v] element [x] = [y]
  139. 13) iterate [x] over the range [x0]+1 ... [x1]-1 {
  140. 14) [err] = [err] + [ady];
  141. 15) if ( [err] >= [adx] ) {
  142. 16) [err] = [err] - [adx]
  143. 17) [y] = [y] + [sy]
  144. } else {
  145. 18) [y] = [y] + [base]
  146. }
  147. 19) vector [v] element [x] = [y]
  148. }
  149. </programlisting>
  150. </section>
  151. </section>
  152. </section>
  153. </section>