09-helper.tex 4.8 KB

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