09-helper.tex 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. \subsection{Overview}
  6. The equations below are used in multiple places by the Vorbis codec
  7. specification. Rather than cluttering up the main specification
  8. documents, they are defined here and referenced where appropriate.
  9. \subsection{Functions}
  10. \subsubsection{ilog} \label{vorbis:spec:ilog}
  11. The "ilog(x)" function returns the position number (1 through n) of the highest set bit in the two's complement integer value
  12. \varname{[x]}. Values of \varname{[x]} less than zero are defined to return zero.
  13. \begin{programlisting}
  14. 1) [return\_value] = 0;
  15. 2) if ( [x] is greater than zero ) {
  16. 3) increment [return\_value];
  17. 4) logical shift [x] one bit to the right, padding the MSb with zero
  18. 5) repeat at step 2)
  19. }
  20. 6) done
  21. \end{programlisting}
  22. Examples:
  23. \begin{itemize}
  24. \item ilog(0) = 0;
  25. \item ilog(1) = 1;
  26. \item ilog(2) = 2;
  27. \item ilog(3) = 2;
  28. \item ilog(4) = 3;
  29. \item ilog(7) = 3;
  30. \item ilog(negative number) = 0;
  31. \end{itemize}
  32. \subsubsection{float32\_unpack} \label{vorbis:spec:float32:unpack}
  33. "float32\_unpack(x)" is intended to translate the packed binary
  34. representation of a Vorbis codebook float value into the
  35. representation used by the decoder for floating point numbers. For
  36. purposes of this example, we will unpack a Vorbis float32 into a
  37. host-native floating point number.
  38. \begin{programlisting}
  39. 1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result)
  40. 2) [sign] = [x] bitwise AND 0x80000000 (unsigned result)
  41. 3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result)
  42. 4) if ( [sign] is nonzero ) then negate [mantissa]
  43. 5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) )
  44. \end{programlisting}
  45. \subsubsection{lookup1\_values} \label{vorbis:spec:lookup1:values}
  46. "lookup1\_values(codebook\_entries,codebook\_dimensions)" is used to
  47. compute the correct length of the value index for a codebook VQ lookup
  48. table of lookup type 1. The values on this list are permuted to
  49. construct the VQ vector lookup table of size
  50. \varname{[codebook\_entries]}.
  51. The return value for this function is defined to be 'the greatest
  52. integer value for which \varname{[return\_value]} to the power of
  53. \varname{[codebook\_dimensions]} is less than or equal to
  54. \varname{[codebook\_entries]}'.
  55. \subsubsection{low\_neighbor} \label{vorbis:spec:low:neighbor}
  56. "low\_neighbor(v,x)" finds the position \varname{n} in vector \varname{[v]} of
  57. the greatest value scalar element for which \varname{n} is less than
  58. \varname{[x]} and vector \varname{[v]} element \varname{n} is less
  59. than vector \varname{[v]} element \varname{[x]}.
  60. \subsubsection{high\_neighbor} \label{vorbis:spec:high:neighbor}
  61. "high\_neighbor(v,x)" finds the position \varname{n} in vector [v] of
  62. the lowest value scalar element for which \varname{n} is less than
  63. \varname{[x]} and vector \varname{[v]} element \varname{n} is greater
  64. than vector \varname{[v]} element \varname{[x]}.
  65. \subsubsection{render\_point} \label{vorbis:spec:render:point}
  66. "render\_point(x0,y0,x1,y1,X)" is used to find the Y value at point X
  67. along the line specified by x0, x1, y0 and y1. This function uses an
  68. integer algorithm to solve for the point directly without calculating
  69. intervening values along the line.
  70. \begin{programlisting}
  71. 1) [dy] = [y1] - [y0]
  72. 2) [adx] = [x1] - [x0]
  73. 3) [ady] = absolute value of [dy]
  74. 4) [err] = [ady] * ([X] - [x0])
  75. 5) [off] = [err] / [adx] using integer division
  76. 6) if ( [dy] is less than zero ) {
  77. 7) [Y] = [y0] - [off]
  78. } else {
  79. 8) [Y] = [y0] + [off]
  80. }
  81. 9) done
  82. \end{programlisting}
  83. \subsubsection{render\_line} \label{vorbis:spec:render:line}
  84. Floor decode type one uses the integer line drawing algorithm of
  85. "render\_line(x0, y0, x1, y1, v)" to construct an integer floor
  86. curve for contiguous piecewise line segments. Note that it has not
  87. been relevant elsewhere, but here we must define integer division as
  88. rounding division of both positive and negative numbers toward zero.
  89. \begin{programlisting}
  90. 1) [dy] = [y1] - [y0]
  91. 2) [adx] = [x1] - [x0]
  92. 3) [ady] = absolute value of [dy]
  93. 4) [base] = [dy] / [adx] using integer division
  94. 5) [x] = [x0]
  95. 6) [y] = [y0]
  96. 7) [err] = 0
  97. 8) if ( [dy] is less than 0 ) {
  98. 9) [sy] = [base] - 1
  99. } else {
  100. 10) [sy] = [base] + 1
  101. }
  102. 11) [ady] = [ady] - (absolute value of [base]) * [adx]
  103. 12) vector [v] element [x] = [y]
  104. 13) iterate [x] over the range [x0]+1 ... [x1]-1 {
  105. 14) [err] = [err] + [ady];
  106. 15) if ( [err] >= [adx] ) {
  107. 16) [err] = [err] - [adx]
  108. 17) [y] = [y] + [sy]
  109. } else {
  110. 18) [y] = [y] + [base]
  111. }
  112. 19) vector [v] element [x] = [y]
  113. }
  114. \end{programlisting}