07-floor1.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
  2. %!TEX root = Vorbis_I_spec.tex
  3. % $Id$
  4. \section{Floor type 1 setup and decode} \label{vorbis:spec:floor1}
  5. \subsection{Overview}
  6. Vorbis floor type one uses a piecewise straight-line representation to
  7. encode a spectral envelope curve. The representation plots this curve
  8. mechanically on a linear frequency axis and a logarithmic (dB)
  9. amplitude axis. The integer plotting algorithm used is similar to
  10. Bresenham's algorithm.
  11. \subsection{Floor 1 format}
  12. \subsubsection{model}
  13. Floor type one represents a spectral curve as a series of
  14. line segments. Synthesis constructs a floor curve using iterative
  15. prediction in a process roughly equivalent to the following simplified
  16. description:
  17. \begin{itemize}
  18. \item the first line segment (base case) is a logical line spanning
  19. from x_0,y_0 to x_1,y_1 where in the base case x_0=0 and x_1=[n], the
  20. full range of the spectral floor to be computed.
  21. \item the induction step chooses a point x_new within an existing
  22. logical line segment and produces a y_new value at that point computed
  23. from the existing line's y value at x_new (as plotted by the line) and
  24. a difference value decoded from the bitstream packet.
  25. \item floor computation produces two new line segments, one running from
  26. x_0,y_0 to x_new,y_new and from x_new,y_new to x_1,y_1. This step is
  27. performed logically even if y_new represents no change to the
  28. amplitude value at x_new so that later refinement is additionally
  29. bounded at x_new.
  30. \item the induction step repeats, using a list of x values specified in
  31. the codec setup header at floor 1 initialization time. Computation
  32. is completed at the end of the x value list.
  33. \end{itemize}
  34. Consider the following example, with values chosen for ease of
  35. understanding rather than representing typical configuration:
  36. For the below example, we assume a floor setup with an [n] of 128.
  37. The list of selected X values in increasing order is
  38. 0,16,32,48,64,80,96,112 and 128. In list order, the values interleave
  39. as 0, 128, 64, 32, 96, 16, 48, 80 and 112. The corresponding
  40. list-order Y values as decoded from an example packet are 110, 20, -5,
  41. -45, 0, -25, -10, 30 and -10. We compute the floor in the following
  42. way, beginning with the first line:
  43. \begin{center}
  44. \includegraphics[width=8cm]{floor1-1}
  45. \captionof{figure}{graph of example floor}
  46. \end{center}
  47. We now draw new logical lines to reflect the correction to new_Y, and
  48. iterate for X positions 32 and 96:
  49. \begin{center}
  50. \includegraphics[width=8cm]{floor1-2}
  51. \captionof{figure}{graph of example floor}
  52. \end{center}
  53. Although the new Y value at X position 96 is unchanged, it is still
  54. used later as an endpoint for further refinement. From here on, the
  55. pattern should be clear; we complete the floor computation as follows:
  56. \begin{center}
  57. \includegraphics[width=8cm]{floor1-3}
  58. \captionof{figure}{graph of example floor}
  59. \end{center}
  60. \begin{center}
  61. \includegraphics[width=8cm]{floor1-4}
  62. \captionof{figure}{graph of example floor}
  63. \end{center}
  64. A more efficient algorithm with carefully defined integer rounding
  65. behavior is used for actual decode, as described later. The actual
  66. algorithm splits Y value computation and line plotting into two steps
  67. with modifications to the above algorithm to eliminate noise
  68. accumulation through integer roundoff/truncation.
  69. \subsubsection{header decode}
  70. A list of floor X values is stored in the packet header in interleaved
  71. format (used in list order during packet decode and synthesis). This
  72. list is split into partitions, and each partition is assigned to a
  73. partition class. X positions 0 and [n] are implicit and do not belong
  74. to an explicit partition or partition class.
  75. A partition class consists of a representation vector width (the
  76. number of Y values which the partition class encodes at once), a
  77. 'subclass' value representing the number of alternate entropy books
  78. the partition class may use in representing Y values, the list of
  79. [subclass] books and a master book used to encode which alternate
  80. books were chosen for representation in a given packet. The
  81. master/subclass mechanism is meant to be used as a flexible
  82. representation cascade while still using codebooks only in a scalar
  83. context.
  84. \begin{Verbatim}[commandchars=\\\{\}]
  85. 1) [floor1_partitions] = read 5 bits as unsigned integer
  86. 2) [maximum_class] = -1
  87. 3) iterate [i] over the range 0 ... [floor1_partitions]-1 \{
  88. 4) vector [floor1_partition_class_list] element [i] = read 4 bits as unsigned integer
  89. \}
  90. 5) [maximum_class] = largest integer scalar value in vector [floor1_partition_class_list]
  91. 6) iterate [i] over the range 0 ... [maximum_class] \{
  92. 7) vector [floor1_class_dimensions] element [i] = read 3 bits as unsigned integer and add 1
  93. 8) vector [floor1_class_subclasses] element [i] = read 2 bits as unsigned integer
  94. 9) if ( vector [floor1_class_subclasses] element [i] is nonzero ) \{
  95. 10) vector [floor1_class_masterbooks] element [i] = read 8 bits as unsigned integer
  96. \}
  97. 11) iterate [j] over the range 0 ... (2 exponent [floor1_class_subclasses] element [i]) - 1 \{
  98. 12) array [floor1_subclass_books] element [i],[j] =
  99. read 8 bits as unsigned integer and subtract one
  100. \}
  101. \}
  102. 13) [floor1_multiplier] = read 2 bits as unsigned integer and add one
  103. 14) [rangebits] = read 4 bits as unsigned integer
  104. 15) vector [floor1_X_list] element [0] = 0
  105. 16) vector [floor1_X_list] element [1] = 2 exponent [rangebits];
  106. 17) [floor1_values] = 2
  107. 18) iterate [i] over the range 0 ... [floor1_partitions]-1 \{
  108. 19) [current_class_number] = vector [floor1_partition_class_list] element [i]
  109. 20) iterate [j] over the range 0 ... ([floor1_class_dimensions] element [current_class_number])-1 \{
  110. 21) vector [floor1_X_list] element ([floor1_values]) =
  111. read [rangebits] bits as unsigned integer
  112. 22) increment [floor1_values] by one
  113. \}
  114. \}
  115. 23) done
  116. \end{Verbatim}
  117. An end-of-packet condition while reading any aspect of a floor 1
  118. configuration during setup renders a stream undecodable. In
  119. addition, a \varname{[floor1_class_masterbooks]} or
  120. \varname{[floor1_subclass_books]} scalar element greater than the
  121. highest numbered codebook configured in this stream is an error
  122. condition that renders the stream undecodable.
  123. \paragraph{packet decode} \label{vorbis:spec:floor1-decode}
  124. Packet decode begins by checking the \varname{[nonzero]} flag:
  125. \begin{Verbatim}[commandchars=\\\{\}]
  126. 1) [nonzero] = read 1 bit as boolean
  127. \end{Verbatim}
  128. If \varname{[nonzero]} is unset, that indicates this channel contained
  129. no audio energy in this frame. Decode immediately returns a status
  130. indicating this floor curve (and thus this channel) is unused this
  131. frame. (A return status of 'unused' is different from decoding a
  132. floor that has all points set to minimum representation amplitude,
  133. which happens to be approximately -140dB).
  134. Assuming \varname{[nonzero]} is set, decode proceeds as follows:
  135. \begin{Verbatim}[commandchars=\\\{\}]
  136. 1) [range] = vector \{ 256, 128, 86, 64 \} element ([floor1_multiplier]-1)
  137. 2) vector [floor1_Y] element [0] = read \link{vorbis:spec:ilog}{ilog}([range]-1) bits as unsigned integer
  138. 3) vector [floor1_Y] element [1] = read \link{vorbis:spec:ilog}{ilog}([range]-1) bits as unsigned integer
  139. 4) [offset] = 2;
  140. 5) iterate [i] over the range 0 ... [floor1_partitions]-1 \{
  141. 6) [class] = vector [floor1_partition_class] element [i]
  142. 7) [cdim] = vector [floor1_class_dimensions] element [class]
  143. 8) [cbits] = vector [floor1_class_subclasses] element [class]
  144. 9) [csub] = (2 exponent [cbits])-1
  145. 10) [cval] = 0
  146. 11) if ( [cbits] is greater than zero ) \{
  147. 12) [cval] = read from packet using codebook number
  148. (vector [floor1_class_masterbooks] element [class]) in scalar context
  149. \}
  150. 13) iterate [j] over the range 0 ... [cdim]-1 \{
  151. 14) [book] = array [floor1_subclass_books] element [class],([cval] bitwise AND [csub])
  152. 15) [cval] = [cval] right shifted [cbits] bits
  153. 16) if ( [book] is not less than zero ) \{
  154. 17) vector [floor1_Y] element ([j]+[offset]) = read from packet using codebook
  155. [book] in scalar context
  156. \} else [book] is less than zero \{
  157. 18) vector [floor1_Y] element ([j]+[offset]) = 0
  158. \}
  159. \}
  160. 19) [offset] = [offset] + [cdim]
  161. \}
  162. 20) done
  163. \end{Verbatim}
  164. An end-of-packet condition during curve decode should be considered a
  165. nominal occurrence; if end-of-packet is reached during any read
  166. operation above, floor decode is to return 'unused' status as if the
  167. \varname{[nonzero]} flag had been unset at the beginning of decode.
  168. Vector \varname{[floor1_Y]} contains the values from packet decode
  169. needed for floor 1 synthesis.
  170. \paragraph{curve computation} \label{vorbis:spec:floor1-synth}
  171. Curve computation is split into two logical steps; the first step
  172. derives final Y amplitude values from the encoded, wrapped difference
  173. values taken from the bitstream. The second step plots the curve
  174. lines. Also, although zero-difference values are used in the
  175. iterative prediction to find final Y values, these points are
  176. conditionally skipped during final line computation in step two.
  177. Skipping zero-difference values allows a smoother line fit.
  178. Although some aspects of the below algorithm look like inconsequential
  179. optimizations, implementors are warned to follow the details closely.
  180. Deviation from implementing a strictly equivalent algorithm can result
  181. in serious decoding errors.
  182. \begin{description}
  183. \item[step 1: amplitude value synthesis]
  184. Unwrap the always-positive-or-zero values read from the packet into
  185. +/- difference values, then apply to line prediction.
  186. \begin{Verbatim}[commandchars=\\\{\}]
  187. 1) [range] = vector \{ 256, 128, 86, 64 \} element ([floor1_multiplier]-1)
  188. 2) vector [floor1_step2_flag] element [0] = set
  189. 3) vector [floor1_step2_flag] element [1] = set
  190. 4) vector [floor1_final_Y] element [0] = vector [floor1_Y] element [0]
  191. 5) vector [floor1_final_Y] element [1] = vector [floor1_Y] element [1]
  192. 6) iterate [i] over the range 2 ... [floor1_values]-1 \{
  193. 7) [low_neighbor_offset] = \link{vorbis:spec:low:neighbor}{low_neighbor}([floor1_X_list],[i])
  194. 8) [high_neighbor_offset] = \link{vorbis:spec:high:neighbor}{high_neighbor}([floor1_X_list],[i])
  195. 9) [predicted] = \link{vorbis:spec:render:point}{render_point}( vector [floor1_X_list] element [low_neighbor_offset],
  196. vector [floor1_final_Y] element [low_neighbor_offset],
  197. vector [floor1_X_list] element [high_neighbor_offset],
  198. vector [floor1_final_Y] element [high_neighbor_offset],
  199. vector [floor1_X_list] element [i] )
  200. 10) [val] = vector [floor1_Y] element [i]
  201. 11) [highroom] = [range] - [predicted]
  202. 12) [lowroom] = [predicted]
  203. 13) if ( [highroom] is less than [lowroom] ) \{
  204. 14) [room] = [highroom] * 2
  205. \} else [highroom] is not less than [lowroom] \{
  206. 15) [room] = [lowroom] * 2
  207. \}
  208. 16) if ( [val] is nonzero ) \{
  209. 17) vector [floor1_step2_flag] element [low_neighbor_offset] = set
  210. 18) vector [floor1_step2_flag] element [high_neighbor_offset] = set
  211. 19) vector [floor1_step2_flag] element [i] = set
  212. 20) if ( [val] is greater than or equal to [room] ) \{
  213. 21) if ( [highroom] is greater than [lowroom] ) \{
  214. 22) vector [floor1_final_Y] element [i] = [val] - [lowroom] + [predicted]
  215. \} else [highroom] is not greater than [lowroom] \{
  216. 23) vector [floor1_final_Y] element [i] = [predicted] - [val] + [highroom] - 1
  217. \}
  218. \} else [val] is less than [room] \{
  219. 24) if ([val] is odd) \{
  220. 25) vector [floor1_final_Y] element [i] =
  221. [predicted] - (([val] + 1) divided by 2 using integer division)
  222. \} else [val] is even \{
  223. 26) vector [floor1_final_Y] element [i] =
  224. [predicted] + ([val] / 2 using integer division)
  225. \}
  226. \}
  227. \} else [val] is zero \{
  228. 27) vector [floor1_step2_flag] element [i] = unset
  229. 28) vector [floor1_final_Y] element [i] = [predicted]
  230. \}
  231. \}
  232. 29) done
  233. \end{Verbatim}
  234. \item[step 2: curve synthesis]
  235. Curve synthesis generates a return vector \varname{[floor]} of length
  236. \varname{[n]} (where \varname{[n]} is provided by the decode process
  237. calling to floor decode). Floor 1 curve synthesis makes use of the
  238. \varname{[floor1_X_list]}, \varname{[floor1_final_Y]} and
  239. \varname{[floor1_step2_flag]} vectors, as well as [floor1_multiplier]
  240. and [floor1_values] values.
  241. Decode begins by sorting the scalars from vectors
  242. \varname{[floor1_X_list]}, \varname{[floor1_final_Y]} and
  243. \varname{[floor1_step2_flag]} together into new vectors
  244. \varname{[floor1_X_list]'}, \varname{[floor1_final_Y]'} and
  245. \varname{[floor1_step2_flag]'} according to ascending sort order of the
  246. values in \varname{[floor1_X_list]}. That is, sort the values of
  247. \varname{[floor1_X_list]} and then apply the same permutation to
  248. elements of the other two vectors so that the X, Y and step2_flag
  249. values still match.
  250. Then compute the final curve in one pass:
  251. \begin{Verbatim}[commandchars=\\\{\}]
  252. 1) [hx] = 0
  253. 2) [lx] = 0
  254. 3) [ly] = vector [floor1_final_Y]' element [0] * [floor1_multiplier]
  255. 4) iterate [i] over the range 1 ... [floor1_values]-1 \{
  256. 5) if ( [floor1_step2_flag]' element [i] is set ) \{
  257. 6) [hy] = [floor1_final_Y]' element [i] * [floor1_multiplier]
  258. 7) [hx] = [floor1_X_list]' element [i]
  259. 8) \link{vorbis:spec:render:line}{render_line}( [lx], [ly], [hx], [hy], [floor] )
  260. 9) [lx] = [hx]
  261. 10) [ly] = [hy]
  262. \}
  263. \}
  264. 11) if ( [hx] is less than [n] ) \{
  265. 12) \link{vorbis:spec:render:line}{render_line}( [hx], [hy], [n], [hy], [floor] )
  266. \}
  267. 13) if ( [hx] is greater than [n] ) \{
  268. 14) truncate vector [floor] to [n] elements
  269. \}
  270. 15) for each scalar in vector [floor], perform a lookup substitution using
  271. the scalar value from [floor] as an offset into the vector \link{vorbis:spec:floor1:inverse:dB:table}{[floor1_inverse_dB_static_table]}
  272. 16) done
  273. \end{Verbatim}
  274. \end{description}