More menial janitorial work on the documentation.
[platform/upstream/libvorbis.git] / doc / stereo.html
1 <HTML><HEAD><TITLE>xiph.org: Ogg Vorbis documentation</TITLE>
2 <BODY bgcolor="#ffffff" text="#202020" link="#006666" vlink="#000000">
3 <nobr><img src="white-ogg.png"><img src="vorbisword2.png"></nobr><p>
4
5
6 <h1><font color=#000070>
7 Stereo Channel Coupling in the Vorbis CODEC
8 </font></h1>
9
10 <em>Last update to this document: July 2, 2002</em><br> 
11
12 <h2>Abstract</h2> The Vorbis audio CODEC provides a channel coupling
13 mechanisms designed to reduce effective bitrate by both eliminating
14 interchannel redundancy and eliminating stereo image information
15 labeled inaudible or undesirable according to spatial psychoacoustic
16 models.  This document describes both the mechanical coupling
17 mechanisms available within the Vorbis specification, as well as the
18 specific stereo coupling models used by the reference
19 <tt>libvorbis</tt> CODEC provided by xiph.org.
20
21 <h2>Terminology</h2> Terminology as used in this document is based on
22 common terminology associated with contemporary CODECs such as MPEG I
23 audio layer 3 (mp3).  However, some differences in terminology are
24 useful in the context of Vorbis as Vorbis functions somewhat
25 differently than most current formats.  For clarity, a few terms are
26 defined beforehand here, and others will be defined where they first
27 appear in context.<p>
28
29 <h3>Subjective and Objective</h3>
30
31 <em>Objective</em> fidelity is a measure, based on a computable,
32 mechanical metric, of how carefully an output matches an input.  For
33 example, a stereo amplifier may claim to introduce less that .01%
34 total harmonic distortion when amplifying an input signal; this claim
35 is easy to verify given proper equipment, and any number of testers are
36 likely to arrive at the same, exact results.  One need not listen to
37 the equipment to make this measurement.<p>
38
39 However, given two amplifiers with identical, verifiable objective
40 specifications, listeners may strongly prefer the sound quality of one
41 over the other.  This is actually the case in the decades old debate
42 [some would say jihad] among audiophiles involving vacuum tube versus
43 solid state amplifiers.  There are people who can tell the difference,
44 and strongly prefer one over the other despite seemingly identical,
45 measurable quality.  This preference is <em>subjective</em> and
46 difficult to measure but nonetheless real.
47
48 Individual elements of subjective differences often can be qualified,
49 but overall subjective quality generally is not measurable.  Different
50 observers are likely to disagree on the exact results of a subjective
51 test as each observer's perspective differs.  When measuring
52 subjective qualities, the best one can hope for is average, empirical
53 results that show statistical significance across a group.<p>
54
55 Perceptual codecs are most concerned with subjective, not objective,
56 quality.  This is why evaluating a perceptual codec via distortion
57 measures and sonograms alone is useless; these objective measures may
58 provide insight into the quality or functioning of a codec, but cannot
59 answer the much squishier subjective question, "Does it sound
60 good?". The tube amplifier example is perhaps not the best as very few
61 people can hear, or care to hear, the minute differences between tubes
62 and transistors, whereas the subjective differences in perceptual
63 codecs tend to be quite large even when objective differences are
64 not.<p>
65
66 <h3>Fidelity, Artifacts and Differences</h3> Audio <em>artifacts</em>
67 and loss of fidelity or more simply put, audio <em>differences</em>
68 are not the same thing.<p>
69
70 A loss of fidelity implies differences between the perceived input and
71 output signal; it does not necessarily imply that the differences in
72 output are displeasing or that the output sounds poor (although this
73 is often the case).  Tube amplifiers are <em>not</em> higher fidelity
74 than modern solid state and digital systems.  They simply produce a
75 form of distortion and coloring that is either unnoticeable or actually
76 pleasing to many ears.<p>
77
78 As compared to an original signal using hard metrics, all perceptual
79 codecs [ASPEC, ATRAC, MP3, WMA, AAC, TwinVQ, AC3 and Vorbis included]
80 lose objective fidelity in order to reduce bitrate.  This is fact. The
81 idea is to lose fidelity in ways that cannot be perceived.  However,
82 most current streaming applications demand bitrates lower than what
83 can be achieved by sacrificing only objective fidelity; this is also
84 fact, despite whatever various company press releases might claim.
85 Subjective fidelity eventually must suffer in one way or another.<p>
86
87 The goal is to choose the best possible tradeoff such that the
88 fidelity loss is graceful and not obviously noticeable.  Most listeners
89 of FM radio do not realize how much lower fidelity that medium is as
90 compared to compact discs or DAT.  However, when compared directly to
91 source material, the difference is obvious.  A cassette tape is lower
92 fidelity still, and yet the degredation, relatively speaking, is
93 graceful and generally easy not to notice.  Compare this graceful loss
94 of quality to an average 44.1kHz stereo mp3 encoded at 80 or 96kbps.
95 The mp3 might actually be higher objective fidelity but subjectively
96 sounds much worse.<p>
97
98 Thus, when a CODEC <em>must</em> sacrifice subjective quality in order
99 to satisfy a user's requirements, the result should be a
100 <em>difference</em> that is generally either difficult to notice
101 without comparison, or easy to ignore.  An <em>artifact</em>, on the
102 other hand, is an element introduced into the output that is
103 immediately noticeable, obviously foreign, and undesired.  The famous
104 'underwater' or 'twinkling' effect synonymous with low bitrate (or
105 poorly encoded) mp3 is an example of an <em>artifact</em>.  This
106 working definition differs slightly from common usage, but the coined
107 distinction between differences and artifacts is useful for our
108 discussion.<p>
109
110 The goal, when it is absolutely necessary to sacrifice subjective
111 fidelity, is obviously to strive for differences and not artifacts.
112 The vast majority of CODECs today fail at this task miserably,
113 predictably, and regularly in one way or another.  Avoiding such
114 failures when it is necessary to sacrifice subjective quality is a
115 fundamental design objective of Vorbis and that objective is reflected
116 in Vorbis's channel coupling design.<p>
117
118 <h2>Mechanisms</h2>
119
120 In encoder release beta 4 and earlier, Vorbis supported multiple
121 channel encoding, but the channels were encoded entirely separately
122 with no cross-analysis or redundancy elimination between channels.
123 This multichannel strategy is very similar to the mp3's <em>dual
124 stereo</em> mode and Vorbis uses the same name for its analogous
125 uncoupled multichannel modes.
126
127 However, the Vorbis spec provides for, and Vorbis release 1.0 rc1 and
128 later implement a coupled channel strategy.  Vorbis has two specific
129 mechanisms that may be used alone or in conjunction to implement
130 channel coupling.  The first is <em>channel interleaving</em> via
131 residue backend #2, and the second is <em>square polar mapping</em>.
132 These two general mechanisms are particularly well suited to coupling
133 due to the structure of Vorbis encoding, as we'll explore below, and
134 using both we can implement both totally <em>lossless stereo image
135 coupling</em> [bit-for-bit decode-identical to uncoupled modes], as
136 well as various lossy models that seek to eliminate inaudible or
137 unimportant aspects of the stereo image in order to enhance
138 bitrate. The exact coupling implementation is generalized to allow the
139 encoder a great deal of flexibility in implementation of a stereo
140 model without requiring any significant complexity increase over the
141 combinatorically simpler mid/side joint stereo of mp3 and other
142 current audio codecs.<p>
143
144 An encoder may apply channel coupling directly to more than a single
145 channel and polar mapping is hierarchical such that polar coupling may be
146 extrapolated to an arbitrary number of channels and is not restricted
147 to only stereo, quadriphonics, ambisonics or 5.1 surround.  However,
148 the scope of this document restricts itself to the stereo coupling
149 case.<p>
150
151 <h3>Square Polar Mapping</h3>
152
153 <h4>maximal correlation</h4>
154  
155 Recall that the basic structure of a a Vorbis I stream first generates
156 from input audio a spectral 'floor' function that serves as an
157 MDCT-domain whitening filter.  This floor is meant to represent the
158 rough envelope of the frequency spectrum, using whatever metric the
159 encoder cares to define.  This floor is subtracted from the log
160 frequency spectrum, effectively normalizing the spectrum by frequency.
161 Each input channel is associated with a unique floor function.<p>
162
163 The basic idea behind any stereo coupling is that the left and right
164 channels usually correlate.  This correlation is even stronger if one
165 first accounts for energy differences in any given frequency band
166 across left and right; think for example of individual instruments
167 mixed into different portions of the stereo image, or a stereo
168 recording with a dominant feature not perfectly in the center.  The
169 floor functions, each specific to a channel, provide the perfect means
170 of normalizing left and right energies across the spectrum to maximize
171 correlation before coupling. This feature of the Vorbis format is not
172 a convenient accident.<p>
173
174 Because we strive to maximally correlate the left and right channels
175 and generally succeed in doing so, left and right residue is typically
176 nearly identical.  We could use channel interleaving (discussed below)
177 alone to efficiently remove the redundancy between the left and right
178 channels as a side effect of entropy encoding, but a polar
179 representation gives benefits when left/right correlation is
180 strong. <p>
181
182 <h4>point and diffuse imaging</h4>
183
184 The first advantage of a polar representation is that it effectively
185 seperates the spatial audio information into a 'point image'
186 (magnitude) at a given frequency and located somewhere in the sound
187 field, and a 'diffuse image' (angle) that fills a large amount of
188 space simultaneously.  Even if we preserve only the magnitude (point)
189 data, a detailed and carefully chosen floor function in each channel
190 provides us with a free, fine-grained, frequency relative intensity
191 stereo*.  Angle information represents diffuse sound fields, such as
192 reverberation that fills the entire space simultaneously.<p>
193
194 *<em>Because the Vorbis model supports a number of different possible
195 stereo models and these models may be mixed, we do not use the term
196 'intensity stereo' talking about Vorbis; instead we use the terms
197 'point stereo', 'phase stereo' and subcategories of each.</em><p>
198
199 The majority of a stereo image is representable by polar magnitude
200 alone, as strong sounds tend to be produced at near-point sources;
201 even non-diffuse, fast, sharp echoes track very accurately using
202 magnitude representation almost alone (for those experimenting with
203 Vorbis tuning, this strategy works much better with the precise,
204 piecewise control of floor 1; the continuous approximation of floor 0
205 results in unstable imaging).  Reverberation and diffuse sounds tend
206 to contain less energy and be psychoacoustically dominated by the
207 point sources embedded in them.  Thus, we again tend to concentrate
208 more represented energy into a predictably smaller number of numbers.
209 Separating representation of point and diffuse imaging also allows us
210 to model and manipulate point and diffuse qualities separately.<p>
211
212 <h4>controlling bit leakage and symbol crosstalk</h4> Because polar
213 representation concentrates represented energy into fewer large
214 values, we reduce bit 'leakage' during cascading (multistage VQ
215 encoding) as a secondary benefit.  A single large, monolithic VQ
216 codebook is more efficient than a cascaded book due to entropy
217 'crosstalk' among symbols between different stages of a multistage cascade.
218 Polar representation is a way of further concentrating entropy into
219 predictable locations so that codebook design can take steps to
220 improve multistage codebook efficiency.  It also allows us to cascade
221 various elements of the stereo image independently.<p>
222
223 <h4>eliminating trigonometry and rounding</h4>
224
225 Rounding and computational complexity are potential problems with a
226 polar representation. As our encoding process involves quantization,
227 mixing a polar representation and quantization makes it potentially
228 impossible, depending on implementation, to construct a coupled stereo
229 mechanism that results in bit-identical decompressed output compared
230 to an uncoupled encoding should the encoder desire it.<p>
231
232 Vorbis uses a mapping that preserves the most useful qualities of
233 polar representation, relies only on addition/subtraction (during
234 decode; high quality encoding still requires some trig), and makes it
235 trivial before or after quantization to represent an angle/magnitude
236 through a one-to-one mapping from possible left/right value
237 permutations.  We do this by basing our polar representation on the
238 unit square rather than the unit-circle.<p>
239
240 Given a magnitude and angle, we recover left and right using the
241 following function (note that A/B may be left/right or right/left
242 depending on the coupling definition used by the encoder):<p>
243
244 <pre>
245       if(magnitude>0)
246         if(angle>0){
247           A=magnitude;
248           B=magnitude-angle;
249         }else{
250           B=magnitude;
251           A=magnitude+angle;
252         }
253       else
254         if(angle>0){
255           A=magnitude;
256           B=magnitude+angle;
257         }else{
258           B=magnitude;
259           A=magnitude-angle;
260         }
261     }
262 </pre>
263
264 The function is antisymmetric for positive and negative magnitudes in
265 order to eliminate a redundant value when quantizing.  For example, if
266 we're quantizing to integer values, we can visualize a magnitude of 5
267 and an angle of -2 as follows:<p>
268
269 <img src="squarepolar.png">
270
271 <p>
272 This representation loses or replicates no values; if the range of A
273 and B are integral -5 through 5, the number of possible Cartesian
274 permutations is 121.  Represented in square polar notation, the
275 possible values are:
276
277 <pre>
278  0, 0
279
280 -1,-2  -1,-1  -1, 0  -1, 1
281
282  1,-2   1,-1   1, 0   1, 1
283
284 -2,-4  -2,-3  -2,-2  -2,-1  -2, 0  -2, 1  -2, 2  -2, 3  
285
286  2,-4   2,-3   ... following the pattern ...
287
288  ...    5, 1   5, 2   5, 3   5, 4   5, 5   5, 6   5, 7   5, 8   5, 9
289
290 </pre>
291
292 ...for a grand total of 121 possible values, the same number as in
293 Cartesian representation (note that, for example, <tt>5,-10</tt> is
294 the same as <tt>-5,10</tt>, so there's no reason to represent
295 both. 2,10 cannot happen, and there's no reason to account for it.)
296 It's also obvious that this mapping is exactly reversible.<p>
297
298 <h3>Channel interleaving</h3>
299
300 We can remap and A/B vector using polar mapping into a magnitude/angle
301 vector, and it's clear that, in general, this concentrates energy in
302 the magnitude vector and reduces the amount of information to encode
303 in the angle vector.  Encoding these vectors independently with
304 residue backend #0 or residue backend #1 will result in bitrate
305 savings.  However, there are still implicit correlations between the
306 magnitude and angle vectors.  The most obvious is that the amplitude
307 of the angle is bounded by its corresponding magnitude value.<p>
308
309 Entropy coding the results, then, further benefits from the entropy
310 model being able to compress magnitude and angle simultaneously.  For
311 this reason, Vorbis implements residuebackend #2 which preinterleaves
312 a number of input vectors (in the stereo case, two, A and B) into a
313 single output vector (with the elements in the order of
314 A_0, B_0, A_1, B_1, A_2 ... A_n-1, B_n-1) before entropy encoding.  Thus
315 each vector to be coded by the vector quantization backend consists of
316 matching magnitude and angle values.<p>
317
318 The astute reader, at this point, will notice that in the theoretical
319 case in which we can use monolithic codebooks of arbitrarily large
320 size, we can directly interleave and encode left and right without
321 polar mapping; in fact, the polar mapping does not appear to lend any
322 benefit whatsoever to the efficiency of the entropy coding.  In fact,
323 it is perfectly possible and reasonable to build a Vorbis encoder that
324 dispenses with polar mapping entirely and merely interleaves the
325 channel.  Libvorbis based encoders may configure such an encoding and
326 it will work as intended.<p>
327
328 However, when we leave the ideal/theoretical domain, we notice that
329 polar mapping does give additional practical benefits, as discussed in
330 the above section on polar mapping and summarized again here:<p>
331 <ul>
332 <li>Polar mapping aids in controlling entropy 'leakage' between stages
333 of a cascaded codebook.  <li>Polar mapping separates the stereo image
334 into point and diffuse components which may be analyzed and handled
335 differently.
336 </ul>
337
338 <h2>Stereo Models</h2>
339
340 <h3>Dual Stereo</h3>
341
342 Dual stereo refers to stereo encoding where the channels are entirely
343 separate; they are analyzed and encoded as entirely distinct entities.
344 This terminology is familiar from mp3.<p>
345
346 <h3>Lossless Stereo</h3>
347
348 Using polar mapping and/or channel interleaving, it's possible to
349 couple Vorbis channels losslessly, that is, construct a stereo
350 coupling encoding that both saves space but also decodes
351 bit-identically to dual stereo.  OggEnc 1.0 and later uses this
352 mode in all high-bitrate encoding.<p>
353
354 Overall, this stereo mode is overkill; however, it offers a safe
355 alternative to users concerned about the slightest possible
356 degredation to the stereo image or archival quality audio.<p>
357
358 <h3>Phase Stereo</h3>
359
360 Phase stereo is the least aggressive means of gracefully dropping
361 resolution from the stereo image; it affects only diffuse imaging.<p>
362
363 It's often quoted that the human ear is deaf to signal phase above
364 about 4kHz; this is nearly true and a passable rule of thumb, but it
365 can be demonstrated that even an average user can tell the difference
366 between high frequency in-phase and out-of-phase noise.  Obviously
367 then, the statement is not entirely true.  However, it's also the case
368 that one must resort to nearly such an extreme demostration before
369 finding the counterexample.<p>
370
371 'Phase stereo' is simply a more aggressive quantization of the polar
372 angle vector; above 4kHz it's generally quite safe to quantize noise
373 and noisy elements to only a handful of allowed phases, or to thin the
374 phase with respect to the magnitude.  The phases of high amplitude
375 pure tones may or may not be preserved more carefully (they are
376 relatively rare and L/R tend to be in phase, so there is generally
377 little reason not to spend a few more bits on them) <p>
378
379 <h4>example: eight phase stereo</h4>
380
381 Vorbis may implement phase stereo coupling by preserving the entirety
382 of the magnitude vector (essential to fine amplitude and energy
383 resolution overall) and quantizing the angle vector to one of only
384 four possible values. Given that the magnitude vector may be positive
385 or negative, this results in left and right phase having eight
386 possible permutation, thus 'eight phase stereo':<p>
387
388 <img src="eightphase.png"><p>
389
390 Left and right may be in phase (positive or negative), the most common
391 case by far, or out of phase by 90 or 180 degrees.<p>
392
393 <h4>example: four phase stereo</h4>
394
395 Similarly, four phase stereo takes the quantization one step further;
396 it allows only in-phase and 180 degree out-out-phase signals:<p>
397
398 <img src="fourphase.png"><p>
399
400 <h3>example: point stereo</h3>
401
402 Point stereo eliminates the possibility of out-of-phase signal
403 entirely.  Any diffuse quality to a sound source tends to collapse
404 inward to a point somewhere within the stereo image.  A practical
405 example would be balanced reverberations within a large, live space;
406 normally the sound is diffuse and soft, giving a sonic impression of
407 volume.  In point-stereo, the reverberations would still exist, but
408 sound fairly firmly centered within the image (assuming the
409 reverberation was centered overall; if the reverberation is stronger
410 to the left, then the point of localization in point stereo would be
411 to the left).  This effect is most noticeable at low and mid
412 frequencies and using headphones (which grant perfect stereo
413 separation). Point stereo is is a graceful but generally easy to
414 detect degrdation to the sound quality and is thus used in frequency
415 ranges where it is least noticeable.<p>
416
417 <h3>Mixed Stereo</h3>
418
419 Mixed stereo is the simultaneous use of more than one of the above
420 stereo encoding models, generally using more aggressive modes in
421 higher frequencies, lower amplitudes or 'nearly' in-phase sound.<p>
422
423 It is also the case that near-DC frequencies should be encoded using
424 lossless coupling to avoid frame blocking artifacts.<p>
425
426 <h3>Vorbis Stereo Modes</h3>
427
428 Vorbis, as of 1.0, uses lossless stereo and a number of mixed modes
429 constructed out of lossless and point stereo.  Phase stereo was used
430 in the rc2 encoder, but is not currently used for simplicity's sake.  It
431 will likely be readded to the stereo model in the future.
432
433 <p>
434 <hr>
435 <a href="http://www.xiph.org/">
436 <img src="white-xifish.png" align=left border=0>
437 </a>
438 <font size=-2 color=#505050>
439
440 Ogg is a <a href="http://www.xiph.org">Xiph.org Foundation</a> effort
441 to protect essential tenets of Internet multimedia from corporate
442 hostage-taking; Open Source is the net's greatest tool to keep
443 everyone honest. See <a href="http://www.xiph.org/about.html">About
444 the Xiph.org Foundation</a> for details.
445 <p>
446
447 Ogg Vorbis is the first Ogg audio CODEC.  Anyone may freely use and
448 distribute the Ogg and Vorbis specification, whether in a private,
449 public or corporate capacity.  However, the Xiph.org Foundation and
450 the Ogg project (xiph.org) reserve the right to set the Ogg Vorbis
451 specification and certify specification compliance.<p>
452
453 Xiph.org's Vorbis software CODEC implementation is distributed under a
454 BSD-like license.  This does not restrict third parties from
455 distributing independent implementations of Vorbis software under
456 other licenses.<p>
457
458 Ogg, Vorbis, Xiph.org Foundation and their logos are trademarks (tm)
459 of the <a href="http://www.xiph.org/">Xiph.org Foundation</a>.  These
460 pages are copyright (C) 1994-2002 Xiph.org Foundation. All rights
461 reserved.<p>
462 </body>
463
464
465
466
467
468