annotate src/libvorbis-1.3.3/doc/08-residue.tex @ 124:e3d5853d5918

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