1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
7 <section id="vorbis-spec-residue">
10 $Id: 08-residue.xml,v 1.5 2003/03/11 11:02:17 xiphmont Exp $
11 <emphasis>Last update to this document: March 11, 2003</emphasis>
14 <title>Residue setup and decode</title>
18 <title>Overview</title>
21 A residue vector represents the fine detail of the audio spectrum of
22 one channel in an audio frame after the encoder subtracts the floor
23 curve and performs any channel coupling. A residue vector may
24 represent spectral lines, spectral magnitude, spectral phase or
25 hybrids as mixed by channel coupling. The exact semantic content of
26 the vector does not matter to the residue abstraction.</para>
29 Whatever the exact qualities, the Vorbis residue abstraction codes the
30 residue vectors into the bitstream packet, and then reconstructs the
31 vectors during decode. Vorbis makes use of three different encoding
32 variants (numbered 0, 1 and 2) of the same basic vector encoding
38 <title>Residue format</title>
41 Reside format partitions each vector in the vector bundle into chunks,
42 classifies each chunk, encodes the chunk classifications and finally
43 encodes the chunks themselves using the the specific VQ arrangement
44 defined for each selected selected classification. The exact
45 interleaving and partitioning vary by residue encoding number, however
46 the high-level process used to classify and encode the residue vector
47 is the same in all three variants.</para>
50 A set of coded residue vectors are all of the same length. High level
51 coding structure, ignoring for the moment exactly how a partition is
52 encoded and simply trusting that it is, is as follows:</para>
55 <listitem><para>Each vector is partitioned into multiple equal sized chunks
56 according to configuration specified. If we have a vector size of
57 <emphasis>n</emphasis>, a partition size <emphasis>residue_partition_size</emphasis>, and a total
58 of <emphasis>ch</emphasis> residue vectors, the total number of partitioned chunks
59 coded is <emphasis>n</emphasis>/<emphasis>residue_partition_size</emphasis>*<emphasis>ch</emphasis>. It is
60 important to note that the integer division truncates. In the below
61 example, we assume an example <emphasis>residue_partition_size</emphasis> of 8.</para></listitem>
63 <listitem><para>Each partition in each vector has a classification number that
64 specifies which of multiple configured VQ codebook setups are used to
65 decode that partition. The classification numbers of each partition
66 can be thought of as forming a vector in their own right, as in the
67 illustration below. Just as the residue vectors are coded in grouped
68 partitions to increase encoding efficiency, the classification vector
69 is also partitioned into chunks. The integer elements of each scalar
70 in a classification chunk are built into a single scalar that
71 represents the classification numbers in that chunk. In the below
72 example, the classification codeword encodes two classification
73 numbers.</para></listitem>
75 <listitem><para>The values in a residue vector may be encoded monolithically in a
76 single pass through the residue vector, but more often efficient
77 codebook design dictates that each vector is encoded as the additive
78 sum of several passes through the residue vector using more than one
79 VQ codebook. Thus, each residue value potentially accumulates values
80 from multiple decode passes. The classification value associated with
81 a partition is the same in each pass, thus the classification codeword
82 is coded only in the first pass.</para></listitem>
88 <imagedata fileref="residue-pack.png" format="PNG"/>
91 <phrase>[illustration of residue vector format]</phrase>
97 <section><title>residue 0</title>
100 Residue 0 and 1 differ only in the way the values within a residue
101 partition are interleaved during partition encoding (visually treated
102 as a black box--or cyan box or brown box--in the above figure).</para>
105 Residue encoding 0 interleaves VQ encoding according to the
106 dimension of the codebook used to encode a partition in a specific
107 pass. The dimension of the codebook need not be the same in multiple
108 passes, however the partition size must be an even multiple of the
109 codebook dimension.</para>
112 As an example, assume a partition vector of size eight, to be encoded
113 by residue 0 using codebook sizes of 8, 4, 2 and 1:</para>
117 original residue vector: [ 0 1 2 3 4 5 6 7 ]
119 codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
121 codebook dimensions = 4 encoded as: [ 0 2 4 6 ], [ 1 3 5 7 ]
123 codebook dimensions = 2 encoded as: [ 0 4 ], [ 1 5 ], [ 2 6 ], [ 3 7 ]
125 codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
130 It is worth mentioning at this point that no configurable value in the
131 residue coding setup is restricted to a power of two.</para>
135 <section><title>residue 1</title>
138 Residue 1 does not interleave VQ encoding. It represents partition
139 vector scalars in order. As with residue 0, however, partition length
140 must be an integer multiple of the codebook dimension, although
141 dimension may vary from pass to pass.</para>
144 As an example, assume a partition vector of size eight, to be encoded
145 by residue 0 using codebook sizes of 8, 4, 2 and 1:</para>
149 original residue vector: [ 0 1 2 3 4 5 6 7 ]
151 codebook dimensions = 8 encoded as: [ 0 1 2 3 4 5 6 7 ]
153 codebook dimensions = 4 encoded as: [ 0 1 2 3 ], [ 4 5 6 7 ]
155 codebook dimensions = 2 encoded as: [ 0 1 ], [ 2 3 ], [ 4 5 ], [ 6 7 ]
157 codebook dimensions = 1 encoded as: [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ]
163 <section><title>residue 2</title>
166 Residue type two can be thought of as a variant of residue type 1.
167 Rather than encoding multiple passed-in vectors as in residue type 1,
168 the <emphasis>ch</emphasis> passed in vectors of length <emphasis>n</emphasis> are first
169 interleaved and flattened into a single vector of length
170 <emphasis>ch</emphasis>*<emphasis>n</emphasis>. Encoding then proceeds as in type 1. Decoding is
171 as in type 1 with decode interleave reversed. If operating on a single
172 vector to begin with, residue type 1 and type 2 are equivalent.</para>
176 <imagedata fileref="residue2.png" format="PNG"/>
179 <phrase>[illustration of residue type 2]</phrase>
186 <title>Residue decode</title>
188 <section><title>header decode</title>
191 Header decode for all three residue types is identical.</para>
193 1) [residue_begin] = read 24 bits as unsigned integer
194 2) [residue_end] = read 24 bits as unsigned integer
195 3) [residue_partition_size] = read 24 bits as unsigned integer and add one
196 4) [residue_classifications] = read 6 bits as unsigned integer and add one
197 5) [residue_classbook] = read 8 bits as unsigned integer
201 <varname>[residue_begin]</varname> and <varname>[residue_end]</varname> select the specific
202 sub-portion of each vector that is actually coded; it implements akin
203 to a bandpass where, for coding purposes, the vector effectively
204 begins at element <varname>[residue_begin]</varname> and ends at
205 <varname>[residue_end]</varname>. Preceding and following values in the unpacked
206 vectors are zeroed. Note that for residue type 2, these values as
207 well as <varname>[residue_partition_size]</varname>apply to the interleaved
208 vector, not the individual vectors before interleave.
209 <varname>[residue_partition_size]</varname> is as explained above,
210 <varname>[residue_classifications]</varname> is the number of possible
211 classification to which a partition can belong and
212 <varname>[residue_classbook]</varname> is the codebook number used to code
213 classification codewords. The number of dimensions in book
214 <varname>[residue_classbook]</varname> determines how many classification values
215 are grouped into a single classification codeword.</para>
218 Next we read a bitmap pattern that specifies which partition classes
219 code values in which passes.</para>
222 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
225 3) [low_bits] = read 3 bits as unsigned integer
226 4) [bitflag] = read one bit as boolean
227 5) if ( [bitflag] is set ) then [high_bits] = read five bits as unsigned integer
228 6) vector [residue_cascade] element [i] = [high_bits] * 8 + [low_bits]
234 Finally, we read in a list of book numbers, each corresponding to
235 specific bit set in the cascade bitmap. We loop over the possible
236 codebook classifications and the maximum possible number of encoding
237 stages (8 in Vorbis I, as constrained by the elements of the cascade
238 bitmap being eight bits):</para>
241 1) iterate [i] over the range 0 ... [residue_classifications]-1 {
243 2) iterate [j] over the range 0 ... 7 {
245 3) if ( vector [residue_cascade] element [i] bit [j] is set ) {
247 4) array [residue_books] element [i][j] = read 8 bits as unsigned integer
251 5) array [residue_books] element [i][j] = unused
261 An end-of-packet condition at any point in header decode renders the
262 stream undecodable. In addition, any codebook number greater than the
263 maximum numbered codebook set up in this stream also renders the
264 stream undecodable.</para>
268 <section><title>packet decode</title>
271 Format 0 and 1 packet decode is identical except for specific
272 partition interleave. Format 2 packet decode can be built out of the
273 format 1 decode process. Thus we describe first the decode
274 infrastructure identical to all three formats.</para>
277 In addition to configuration information, the residue decode process
278 is passed the number of vectors in the submap bundle and a vector of
279 flags indicating if any of the vectors are not to be decoded. If the
280 passed in number of vectors is 3 and vector number 1 is marked 'do not
281 decode', decode skips vector 1 during the decode loop. However, even
282 'do not decode' vectors are allocated and zeroed.</para>
285 The following convenience values are conceptually useful to clarifying
286 the decode process:</para>
289 1) [classwords_per_codeword] = [codebook_dimensions] value of codebook [residue_classbook]
290 2) [n_to_read] = [residue_end] - [residue_begin]
291 3) [partitions_to_read] = [n_to_read] / [residue_partition_size]
295 Packet decode proceeds as follows, matching the description offered earlier in the document. We assume that the number of vectors being encoded, <varname>[ch]</varname> is provided by the higher level decoding process.</para>
297 1) allocate and zero all vectors that will be returned.
298 2) iterate [pass] over the range 0 ... 7 {
300 3) [partition_count] = 0
302 4) if ([pass] is zero) {
304 5) iterate [j] over the range 0 .. [ch]-1 {
306 6) if vector [j] is not marked 'do not decode' {
308 7) [temp] = read from packet using codebook [residue_classbook] in scalar context
309 8) iterate [i] descending over the range [classwords_per_codeword]-1 ... 0 {
311 9) array [classifications] element [j],([i]+[partition_count]) =
312 [temp] integer modulo [residue_classifications]
313 10) [temp] = [temp] / [residue_classifications] using integer division
323 11) iterate [i] over the range 0 .. ([classwords_per_codeword] - 1) while [partition_count]
324 is also less than [partitions_to_read] {
326 12) iterate [j] over the range 0 .. [ch]-1 {
328 13) if vector [j] is not marked 'do not decode' {
330 14) [vqclass] = array [classifications] element [j],[partition_count]
331 15) [vqbook] = array [residue_books] element [vqclass],[pass]
332 16) if ([vqbook] is not 'unused') {
334 17) decode partition into output vector number [j], starting at scalar
335 offset [residue_begin]+[partition_count]*[residue_partition_size] using
336 codebook number [vqbook] in VQ context
340 18) increment [partition_count] by one
350 An end-of-packet condition during packet decode is to be considered a
351 nominal occurrence. Decode returns the result of vector decode up to
356 <section><title>format 0 specifics</title>
359 Format zero decodes partitions exactly as described earlier in the
360 'Residue Format: residue 0' section. The following pseudocode
361 presents the same algorithm. Assume:</para>
364 <listitem><simpara> <varname>[n]</varname> is the value in <varname>[residue_partition_size]</varname></simpara></listitem>
365 <listitem><simpara><varname>[v]</varname> is the residue vector</simpara></listitem>
366 <listitem><simpara><varname>[offset]</varname> is the beginning read offset in [v]</simpara></listitem>
370 1) [step] = [n] / [codebook_dimensions]
371 2) iterate [i] over the range 0 ... [step]-1 {
373 3) vector [entry_temp] = read vector from packet using current codebook in VQ context
374 4) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
376 5) vector [v] element ([offset]+[i]+[j]*[step]) =
377 vector [v] element ([offset]+[i]+[j]*[step]) +
378 vector [entry_temp] element [j]
390 <section><title>format 1 specifics</title>
393 Format 1 decodes partitions exactly as described earlier in the
394 'Residue Format: residue 1' section. The following pseudocode
395 presents the same algorithm. Assume:</para>
398 <listitem><simpara> <varname>[n]</varname> is the value in
399 <varname>[residue_partition_size]</varname></simpara></listitem>
400 <listitem><simpara><varname>[v]</varname> is the residue vector</simpara></listitem>
401 <listitem><simpara><varname>[offset]</varname> is the beginning read offset in [v]</simpara></listitem>
406 2) vector [entry_temp] = read vector from packet using current codebook in VQ context
407 3) iterate [j] over the range 0 ... [codebook_dimensions]-1 {
409 5) vector [v] element ([offset]+[i]) =
410 vector [v] element ([offset]+[i]) +
411 vector [entry_temp] element [j]
416 4) if ( [i] is less than [n] ) continue at step 2
422 <section><title>format 2 specifics</title>
425 Format 2 is reducible to format 1. It may be implemented as an additional stepprior to and an additional post-decode step after a normal format 1 decode.
429 Format 2 handles 'do not decode' vectors differently than residue 0 or
430 1; if all vectors are marked 'do not decode', no decode occurrs.
431 However, if at least one vector is to be decoded, all the vectors are
432 decoded. We then request normal format 1 to decode a single vector
433 representing all output channels, rather than a vector for each
434 channel. After decode, deinterleave the vector into independent vectors, one for each output channel. That is:</para>
437 <listitem><simpara>If all vectors 0 through <emphasis>ch</emphasis>-1 are marked 'do not decode', allocate and clear a single vector <varname>[v]</varname>of length <emphasis>ch*n</emphasis> and skip step 2 below; proceed directly to the post-decode step.</simpara></listitem>
438 <listitem><simpara>Rather than performing format 1 decode to produce <emphasis>ch</emphasis> vectors of length <emphasis>n</emphasis> each, call format 1 decode to produce a single vector <varname>[v]</varname> of length <emphasis>ch*n</emphasis>. </simpara></listitem>
439 <listitem><para>Post decode: Deinterleave the single vector <varname>[v]</varname> returned by format 1 decode as described above into <emphasis>ch</emphasis> independent vectors, one for each outputchannel, according to:
441 1) iterate [i] over the range 0 ... [n]-1 {
443 2) iterate [j] over the range 0 ... [ch]-1 {
445 3) output vector number [j] element [i] = vector [v] element ([i] * [ch] + [j])