08-residue.tex 17 KB

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