08-residue.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
  2. %!TEX root = Vorbis_I_spec.tex
  3. % $Id$
  4. \section{Residue setup and decode} \label{vorbis:spec:residue}
  5. \subsection{Overview}
  6. A residue vector represents the fine detail of the audio spectrum of
  7. one channel in an audio frame after the encoder subtracts the floor
  8. curve and performs any channel coupling. A residue vector may
  9. represent spectral lines, spectral magnitude, spectral phase or
  10. hybrids as mixed by channel coupling. The exact semantic content of
  11. the vector does not matter to the residue abstraction.
  12. Whatever the exact qualities, the Vorbis residue abstraction codes the
  13. residue vectors into the bitstream packet, and then reconstructs the
  14. vectors during decode. Vorbis makes use of three different encoding
  15. variants (numbered 0, 1 and 2) of the same basic vector encoding
  16. abstraction.
  17. \subsection{Residue format}
  18. Residue format partitions each vector in the vector bundle into chunks,
  19. classifies each chunk, encodes the chunk classifications and finally
  20. encodes the chunks themselves using the the specific VQ arrangement
  21. defined for each selected classification.
  22. The exact interleaving and partitioning vary by residue encoding number,
  23. however the high-level process used to classify and encode the residue
  24. vector is the same in all three variants.
  25. A set of coded residue vectors are all of the same length. High level
  26. coding structure, ignoring for the moment exactly how a partition is
  27. encoded and simply trusting that it is, is as follows:
  28. \begin{itemize}
  29. \item Each vector is partitioned into multiple equal sized chunks
  30. according to configuration specified. If we have a vector size of
  31. \emph{n}, a partition size \emph{residue_partition_size}, and a total
  32. of \emph{ch} residue vectors, the total number of partitioned chunks
  33. coded is \emph{n}/\emph{residue_partition_size}*\emph{ch}. It is
  34. important to note that the integer division truncates. In the below
  35. example, we assume an example \emph{residue_partition_size} of 8.
  36. \item Each partition in each vector has a classification number that
  37. specifies which of multiple configured VQ codebook setups are used to
  38. decode that partition. The classification numbers of each partition
  39. can be thought of as forming a vector in their own right, as in the
  40. illustration below. Just as the residue vectors are coded in grouped
  41. partitions to increase encoding efficiency, the classification vector
  42. is also partitioned into chunks. The integer elements of each scalar
  43. in a classification chunk are built into a single scalar that
  44. represents the classification numbers in that chunk. In the below
  45. example, the classification codeword encodes two classification
  46. numbers.
  47. \item The values in a residue vector may be encoded monolithically in a
  48. single pass through the residue vector, but more often efficient
  49. codebook design dictates that each vector is encoded as the additive
  50. sum of several passes through the residue vector using more than one
  51. VQ codebook. Thus, each residue value potentially accumulates values
  52. from multiple decode passes. The classification value associated with
  53. a partition is the same in each pass, thus the classification codeword
  54. is coded only in the first pass.
  55. \end{itemize}
  56. \begin{center}
  57. \includegraphics[width=\textwidth]{residue-pack}
  58. \captionof{figure}{illustration of residue vector format}
  59. \end{center}
  60. \subsection{residue 0}
  61. Residue 0 and 1 differ only in the way the values within a residue
  62. partition are interleaved during partition encoding (visually treated
  63. as a black box--or cyan box or brown box--in the above figure).
  64. Residue encoding 0 interleaves VQ encoding according to the
  65. dimension of the codebook used to encode a partition in a specific
  66. pass. The dimension of the codebook need not be the same in multiple
  67. passes, however the partition size must be an even multiple of the
  68. codebook dimension.
  69. As an example, assume a partition vector of size eight, to be encoded
  70. by residue 0 using codebook sizes of 8, 4, 2 and 1:
  71. \begin{programlisting}
  72. original residue vector: [ 0 1 2 3 4 5 6 7 ]
  73. codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
  74. codebook dimensions = 4 encoded as: [ 0 2 4 6 ], [ 1 3 5 7 ]
  75. codebook dimensions = 2 encoded as: [ 0 4 ], [ 1 5 ], [ 2 6 ], [ 3 7 ]
  76. codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
  77. \end{programlisting}
  78. It is worth mentioning at this point that no configurable value in the
  79. residue coding setup is restricted to a power of two.
  80. \subsection{residue 1}
  81. Residue 1 does not interleave VQ encoding. It represents partition
  82. vector scalars in order. As with residue 0, however, partition length
  83. must be an integer multiple of the codebook dimension, although
  84. dimension may vary from pass to pass.
  85. As an example, assume a partition vector of size eight, to be encoded
  86. by residue 0 using codebook sizes of 8, 4, 2 and 1:
  87. \begin{programlisting}
  88. original residue vector: [ 0 1 2 3 4 5 6 7 ]
  89. codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
  90. codebook dimensions = 4 encoded as: [ 0 1 2 3 ], [ 4 5 6 7 ]
  91. codebook dimensions = 2 encoded as: [ 0 1 ], [ 2 3 ], [ 4 5 ], [ 6 7 ]
  92. codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
  93. \end{programlisting}
  94. \subsection{residue 2}
  95. Residue type two can be thought of as a variant of residue type 1.
  96. Rather than encoding multiple passed-in vectors as in residue type 1,
  97. the \emph{ch} passed in vectors of length \emph{n} are first
  98. interleaved and flattened into a single vector of length
  99. \emph{ch}*\emph{n}. Encoding then proceeds as in type 1. Decoding is
  100. as in type 1 with decode interleave reversed. If operating on a single
  101. vector to begin with, residue type 1 and type 2 are equivalent.
  102. \begin{center}
  103. \includegraphics[width=\textwidth]{residue2}
  104. \captionof{figure}{illustration of residue type 2}
  105. \end{center}
  106. \subsection{Residue decode}
  107. \subsubsection{header decode}
  108. Header decode for all three residue types is identical.
  109. \begin{programlisting}
  110. 1) [residue_begin] = read 24 bits as unsigned integer
  111. 2) [residue_end] = read 24 bits as unsigned integer
  112. 3) [residue_partition_size] = read 24 bits as unsigned integer and add one
  113. 4) [residue_classifications] = read 6 bits as unsigned integer and add one
  114. 5) [residue_classbook] = read 8 bits as unsigned integer
  115. \end{programlisting}
  116. \varname{[residue_begin]} and
  117. \varname{[residue_end]} select the specific sub-portion of
  118. each vector that is actually coded; it implements akin to a bandpass
  119. where, for coding purposes, the vector effectively begins at element
  120. \varname{[residue_begin]} and ends at
  121. \varname{[residue_end]}. Preceding and following values in
  122. the unpacked vectors are zeroed. Note that for residue type 2, these
  123. values as well as \varname{[residue_partition_size]}apply to
  124. the interleaved vector, not the individual vectors before interleave.
  125. \varname{[residue_partition_size]} is as explained above,
  126. \varname{[residue_classifications]} is the number of possible
  127. classification to which a partition can belong and
  128. \varname{[residue_classbook]} is the codebook number used to
  129. code classification codewords. The number of dimensions in book
  130. \varname{[residue_classbook]} determines how many
  131. classification values are grouped into a single classification
  132. codeword. Note that the number of entries and dimensions in book
  133. \varname{[residue_classbook]}, along with
  134. \varname{[residue_classifications]}, overdetermines to
  135. possible number of classification codewords.
  136. If \varname{[residue_classifications]}\^{}\varname{[residue_classbook]}.dimensions
  137. exceeds \varname{[residue_classbook]}.entries, the
  138. bitstream should be regarded to be undecodable.
  139. Next we read a bitmap pattern that specifies which partition classes
  140. code values in which passes.
  141. \begin{programlisting}
  142. 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
  143. 2) [high_bits] = 0
  144. 3) [low_bits] = read 3 bits as unsigned integer
  145. 4) [bitflag] = read one bit as boolean
  146. 5) if ( [bitflag] is set ) then [high_bits] = read five bits as unsigned integer
  147. 6) vector [residue_cascade] element [i] = [high_bits] * 8 + [low_bits]
  148. }
  149. 7) done
  150. \end{programlisting}
  151. Finally, we read in a list of book numbers, each corresponding to
  152. specific bit set in the cascade bitmap. We loop over the possible
  153. codebook classifications and the maximum possible number of encoding
  154. stages (8 in Vorbis I, as constrained by the elements of the cascade
  155. bitmap being eight bits):
  156. \begin{programlisting}
  157. 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
  158. 2) iterate [j] over the range 0 ... 7 {
  159. 3) if ( vector [residue_cascade] element [i] bit [j] is set ) {
  160. 4) array [residue_books] element [i][j] = read 8 bits as unsigned integer
  161. } else {
  162. 5) array [residue_books] element [i][j] = unused
  163. }
  164. }
  165. }
  166. 6) done
  167. \end{programlisting}
  168. An end-of-packet condition at any point in header decode renders the
  169. stream undecodable. In addition, any codebook number greater than the
  170. maximum numbered codebook set up in this stream also renders the
  171. stream undecodable.
  172. \subsubsection{packet decode}
  173. Format 0 and 1 packet decode is identical except for specific
  174. partition interleave. Format 2 packet decode can be built out of the
  175. format 1 decode process. Thus we describe first the decode
  176. infrastructure identical to all three formats.
  177. In addition to configuration information, the residue decode process
  178. is passed the number of vectors in the submap bundle and a vector of
  179. flags indicating if any of the vectors are not to be decoded. If the
  180. passed in number of vectors is 3 and vector number 1 is marked 'do not
  181. decode', decode skips vector 1 during the decode loop. However, even
  182. 'do not decode' vectors are allocated and zeroed.
  183. Depending on the values of \varname{[residue_begin]} and
  184. \varname{[residue_end]}, it is obvious that the encoded
  185. portion of a residue vector may be the entire possible residue vector
  186. or some other strict subset of the actual residue vector size with
  187. zero padding at either uncoded end. However, it is also possible to
  188. set \varname{[residue_begin]} and
  189. \varname{[residue_end]} to specify a range partially or
  190. wholly beyond the maximum vector size. Before beginning residue
  191. decode, limit \varname{[residue_begin]} and
  192. \varname{[residue_end]} to the maximum possible vector size
  193. as follows. We assume that the number of vectors being encoded,
  194. \varname{[ch]} is provided by the higher level decoding
  195. process.
  196. \begin{programlisting}
  197. 1) [actual_size] = current blocksize/2;
  198. 2) if residue encoding is format 2
  199. 3) [actual_size] = [actual_size] * [ch];
  200. 4) [limit_residue_begin] = maximum of ([residue_begin],[actual_size]);
  201. 5) [limit_residue_end] = maximum of ([residue_end],[actual_size]);
  202. \end{programlisting}
  203. The following convenience values are conceptually useful to clarifying
  204. the decode process:
  205. \begin{programlisting}
  206. 1) [classwords_per_codeword] = [codebook_dimensions] value of codebook [residue_classbook]
  207. 2) [n_to_read] = [limit_residue_end] - [limit_residue_begin]
  208. 3) [partitions_to_read] = [n_to_read] / [residue_partition_size]
  209. \end{programlisting}
  210. Packet decode proceeds as follows, matching the description offered earlier in the document.
  211. \begin{programlisting}
  212. 1) allocate and zero all vectors that will be returned.
  213. 2) if ([n_to_read] is zero), stop; there is no residue to decode.
  214. 3) iterate [pass] over the range 0 ... 7 {
  215. 4) [partition_count] = 0
  216. 5) while [partition_count] is less than [partitions_to_read]
  217. 6) if ([pass] is zero) {
  218. 7) iterate [j] over the range 0 .. [ch]-1 {
  219. 8) if vector [j] is not marked 'do not decode' {
  220. 9) [temp] = read from packet using codebook [residue_classbook] in scalar context
  221. 10) iterate [i] descending over the range [classwords_per_codeword]-1 ... 0 {
  222. 11) array [classifications] element [j],([i]+[partition_count]) =
  223. [temp] integer modulo [residue_classifications]
  224. 12) [temp] = [temp] / [residue_classifications] using integer division
  225. }
  226. }
  227. }
  228. }
  229. 13) iterate [i] over the range 0 .. ([classwords_per_codeword] - 1) while [partition_count]
  230. is also less than [partitions_to_read] {
  231. 14) iterate [j] over the range 0 .. [ch]-1 {
  232. 15) if vector [j] is not marked 'do not decode' {
  233. 16) [vqclass] = array [classifications] element [j],[partition_count]
  234. 17) [vqbook] = array [residue_books] element [vqclass],[pass]
  235. 18) if ([vqbook] is not 'unused') {
  236. 19) decode partition into output vector number [j], starting at scalar
  237. offset [limit_residue_begin]+[partition_count]*[residue_partition_size] using
  238. codebook number [vqbook] in VQ context
  239. }
  240. }
  241. 20) increment [partition_count] by one
  242. }
  243. }
  244. }
  245. 21) done
  246. \end{programlisting}
  247. An end-of-packet condition during packet decode is to be considered a
  248. nominal occurrence. Decode returns the result of vector decode up to
  249. that point.
  250. \subsubsection{format 0 specifics}
  251. Format zero decodes partitions exactly as described earlier in the
  252. 'Residue Format: residue 0' section. The following pseudocode
  253. presents the same algorithm. Assume:
  254. \begin{itemize}
  255. \item \varname{[n]} is the value in \varname{[residue_partition_size]}
  256. \item \varname{[v]} is the residue vector
  257. \item \varname{[offset]} is the beginning read offset in [v]
  258. \end{itemize}
  259. \begin{programlisting}
  260. 1) [step] = [n] / [codebook_dimensions]
  261. 2) iterate [i] over the range 0 ... [step]-1 {
  262. 3) vector [entry_temp] = read vector from packet using current codebook in VQ context
  263. 4) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
  264. 5) vector [v] element ([offset]+[i]+[j]*[step]) =
  265. vector [v] element ([offset]+[i]+[j]*[step]) +
  266. vector [entry_temp] element [j]
  267. }
  268. }
  269. 6) done
  270. \end{programlisting}
  271. \subsubsection{format 1 specifics}
  272. Format 1 decodes partitions exactly as described earlier in the
  273. 'Residue Format: residue 1' section. The following pseudocode
  274. presents the same algorithm. Assume:
  275. \begin{itemize}
  276. \item \varname{[n]} is the value in
  277. \varname{[residue_partition_size]}
  278. \item \varname{[v]} is the residue vector
  279. \item \varname{[offset]} is the beginning read offset in [v]
  280. \end{itemize}
  281. \begin{programlisting}
  282. 1) [i] = 0
  283. 2) vector [entry_temp] = read vector from packet using current codebook in VQ context
  284. 3) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
  285. 4) vector [v] element ([offset]+[i]) =
  286. vector [v] element ([offset]+[i]) +
  287. vector [entry_temp] element [j]
  288. 5) increment [i]
  289. }
  290. 6) if ( [i] is less than [n] ) continue at step 2
  291. 7) done
  292. \end{programlisting}
  293. \subsubsection{format 2 specifics}
  294. Format 2 is reducible to format 1. It may be implemented as an additional step prior to and an additional post-decode step after a normal format 1 decode.
  295. Format 2 handles 'do not decode' vectors differently than residue 0 or
  296. 1; if all vectors are marked 'do not decode', no decode occurrs.
  297. However, if at least one vector is to be decoded, all the vectors are
  298. decoded. We then request normal format 1 to decode a single vector
  299. representing all output channels, rather than a vector for each
  300. channel. After decode, deinterleave the vector into independent vectors, one for each output channel. That is:
  301. \begin{enumerate}
  302. \item If all vectors 0 through \emph{ch}-1 are marked 'do not decode', allocate and clear a single vector \varname{[v]}of length \emph{ch*n} and skip step 2 below; proceed directly to the post-decode step.
  303. \item Rather than performing format 1 decode to produce \emph{ch} vectors of length \emph{n} each, call format 1 decode to produce a single vector \varname{[v]} of length \emph{ch*n}.
  304. \item Post decode: Deinterleave the single vector \varname{[v]} returned by format 1 decode as described above into \emph{ch} independent vectors, one for each outputchannel, according to:
  305. \begin{programlisting}
  306. 1) iterate [i] over the range 0 ... [n]-1 {
  307. 2) iterate [j] over the range 0 ... [ch]-1 {
  308. 3) output vector number [j] element [i] = vector [v] element ([i] * [ch] + [j])
  309. }
  310. }
  311. 4) done
  312. \end{programlisting}
  313. \end{enumerate}