annotate src/libvorbis-1.3.3/doc/04-codec.tex @ 22:b07fe9e906dc

Portaudio: add missed file
author Chris Cannam
date Tue, 26 Mar 2013 12:14:11 +0000
parents 05aa0afa9217
children
rev   line source
Chris@1 1
Chris@1 2 % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Chris@1 3 %!TEX root = Vorbis_I_spec.tex
Chris@1 4 % $Id$
Chris@1 5 \section{Codec Setup and Packet Decode} \label{vorbis:spec:codec}
Chris@1 6
Chris@1 7 \subsection{Overview}
Chris@1 8
Chris@1 9 This document serves as the top-level reference document for the
Chris@1 10 bit-by-bit decode specification of Vorbis I. This document assumes a
Chris@1 11 high-level understanding of the Vorbis decode process, which is
Chris@1 12 provided in \xref{vorbis:spec:intro}. \xref{vorbis:spec:bitpacking} covers reading and writing bit fields from
Chris@1 13 and to bitstream packets.
Chris@1 14
Chris@1 15
Chris@1 16
Chris@1 17 \subsection{Header decode and decode setup}
Chris@1 18
Chris@1 19 A Vorbis bitstream begins with three header packets. The header
Chris@1 20 packets are, in order, the identification header, the comments header,
Chris@1 21 and the setup header. All are required for decode compliance. An
Chris@1 22 end-of-packet condition during decoding the first or third header
Chris@1 23 packet renders the stream undecodable. End-of-packet decoding the
Chris@1 24 comment header is a non-fatal error condition.
Chris@1 25
Chris@1 26 \subsubsection{Common header decode}
Chris@1 27
Chris@1 28 Each header packet begins with the same header fields.
Chris@1 29
Chris@1 30
Chris@1 31 \begin{Verbatim}[commandchars=\\\{\}]
Chris@1 32 1) [packet\_type] : 8 bit value
Chris@1 33 2) 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73: the characters 'v','o','r','b','i','s' as six octets
Chris@1 34 \end{Verbatim}
Chris@1 35
Chris@1 36 Decode continues according to packet type; the identification header
Chris@1 37 is type 1, the comment header type 3 and the setup header type 5
Chris@1 38 (these types are all odd as a packet with a leading single bit of '0'
Chris@1 39 is an audio packet). The packets must occur in the order of
Chris@1 40 identification, comment, setup.
Chris@1 41
Chris@1 42
Chris@1 43
Chris@1 44 \subsubsection{Identification header}
Chris@1 45
Chris@1 46 The identification header is a short header of only a few fields used
Chris@1 47 to declare the stream definitively as Vorbis, and provide a few externally
Chris@1 48 relevant pieces of information about the audio stream. The
Chris@1 49 identification header is coded as follows:
Chris@1 50
Chris@1 51 \begin{Verbatim}[commandchars=\\\{\}]
Chris@1 52 1) [vorbis\_version] = read 32 bits as unsigned integer
Chris@1 53 2) [audio\_channels] = read 8 bit integer as unsigned
Chris@1 54 3) [audio\_sample\_rate] = read 32 bits as unsigned integer
Chris@1 55 4) [bitrate\_maximum] = read 32 bits as signed integer
Chris@1 56 5) [bitrate\_nominal] = read 32 bits as signed integer
Chris@1 57 6) [bitrate\_minimum] = read 32 bits as signed integer
Chris@1 58 7) [blocksize\_0] = 2 exponent (read 4 bits as unsigned integer)
Chris@1 59 8) [blocksize\_1] = 2 exponent (read 4 bits as unsigned integer)
Chris@1 60 9) [framing\_flag] = read one bit
Chris@1 61 \end{Verbatim}
Chris@1 62
Chris@1 63 \varname{[vorbis\_version]} is to read '0' in order to be compatible
Chris@1 64 with this document. Both \varname{[audio\_channels]} and
Chris@1 65 \varname{[audio\_sample\_rate]} must read greater than zero. Allowed final
Chris@1 66 blocksize values are 64, 128, 256, 512, 1024, 2048, 4096 and 8192 in
Chris@1 67 Vorbis I. \varname{[blocksize\_0]} must be less than or equal to
Chris@1 68 \varname{[blocksize\_1]}. The framing bit must be nonzero. Failure to
Chris@1 69 meet any of these conditions renders a stream undecodable.
Chris@1 70
Chris@1 71 The bitrate fields above are used only as hints. The nominal bitrate
Chris@1 72 field especially may be considerably off in purely VBR streams. The
Chris@1 73 fields are meaningful only when greater than zero.
Chris@1 74
Chris@1 75 \begin{itemize}
Chris@1 76 \item All three fields set to the same value implies a fixed rate, or tightly bounded, nearly fixed-rate bitstream
Chris@1 77 \item Only nominal set implies a VBR or ABR stream that averages the nominal bitrate
Chris@1 78 \item Maximum and or minimum set implies a VBR bitstream that obeys the bitrate limits
Chris@1 79 \item None set indicates the encoder does not care to speculate.
Chris@1 80 \end{itemize}
Chris@1 81
Chris@1 82
Chris@1 83
Chris@1 84
Chris@1 85 \subsubsection{Comment header}
Chris@1 86 Comment header decode and data specification is covered in
Chris@1 87 \xref{vorbis:spec:comment}.
Chris@1 88
Chris@1 89
Chris@1 90 \subsubsection{Setup header}
Chris@1 91
Chris@1 92 Vorbis codec setup is configurable to an extreme degree:
Chris@1 93
Chris@1 94 \begin{center}
Chris@1 95 \includegraphics[width=\textwidth]{components}
Chris@1 96 \captionof{figure}{decoder pipeline configuration}
Chris@1 97 \end{center}
Chris@1 98
Chris@1 99
Chris@1 100 The setup header contains the bulk of the codec setup information
Chris@1 101 needed for decode. The setup header contains, in order, the lists of
Chris@1 102 codebook configurations, time-domain transform configurations
Chris@1 103 (placeholders in Vorbis I), floor configurations, residue
Chris@1 104 configurations, channel mapping configurations and mode
Chris@1 105 configurations. It finishes with a framing bit of '1'. Header decode
Chris@1 106 proceeds in the following order:
Chris@1 107
Chris@1 108 \paragraph{Codebooks}
Chris@1 109
Chris@1 110 \begin{enumerate}
Chris@1 111 \item \varname{[vorbis\_codebook\_count]} = read eight bits as unsigned integer and add one
Chris@1 112 \item Decode \varname{[vorbis\_codebook\_count]} codebooks in order as defined
Chris@1 113 in \xref{vorbis:spec:codebook}. Save each configuration, in
Chris@1 114 order, in an array of
Chris@1 115 codebook configurations \varname{[vorbis\_codebook\_configurations]}.
Chris@1 116 \end{enumerate}
Chris@1 117
Chris@1 118
Chris@1 119
Chris@1 120 \paragraph{Time domain transforms}
Chris@1 121
Chris@1 122 These hooks are placeholders in Vorbis I. Nevertheless, the
Chris@1 123 configuration placeholder values must be read to maintain bitstream
Chris@1 124 sync.
Chris@1 125
Chris@1 126 \begin{enumerate}
Chris@1 127 \item \varname{[vorbis\_time\_count]} = read 6 bits as unsigned integer and add one
Chris@1 128 \item read \varname{[vorbis\_time\_count]} 16 bit values; each value should be zero. If any value is nonzero, this is an error condition and the stream is undecodable.
Chris@1 129 \end{enumerate}
Chris@1 130
Chris@1 131
Chris@1 132
Chris@1 133 \paragraph{Floors}
Chris@1 134
Chris@1 135 Vorbis uses two floor types; header decode is handed to the decode
Chris@1 136 abstraction of the appropriate type.
Chris@1 137
Chris@1 138 \begin{enumerate}
Chris@1 139 \item \varname{[vorbis\_floor\_count]} = read 6 bits as unsigned integer and add one
Chris@1 140 \item For each \varname{[i]} of \varname{[vorbis\_floor\_count]} floor numbers:
Chris@1 141 \begin{enumerate}
Chris@1 142 \item read the floor type: vector \varname{[vorbis\_floor\_types]} element \varname{[i]} =
Chris@1 143 read 16 bits as unsigned integer
Chris@1 144 \item If the floor type is zero, decode the floor
Chris@1 145 configuration as defined in \xref{vorbis:spec:floor0}; save
Chris@1 146 this
Chris@1 147 configuration in slot \varname{[i]} of the floor configuration array \varname{[vorbis\_floor\_configurations]}.
Chris@1 148 \item If the floor type is one,
Chris@1 149 decode the floor configuration as defined in \xref{vorbis:spec:floor1}; save this configuration in slot \varname{[i]} of the floor configuration array \varname{[vorbis\_floor\_configurations]}.
Chris@1 150 \item If the the floor type is greater than one, this stream is undecodable; ERROR CONDITION
Chris@1 151 \end{enumerate}
Chris@1 152
Chris@1 153 \end{enumerate}
Chris@1 154
Chris@1 155
Chris@1 156
Chris@1 157 \paragraph{Residues}
Chris@1 158
Chris@1 159 Vorbis uses three residue types; header decode of each type is identical.
Chris@1 160
Chris@1 161
Chris@1 162 \begin{enumerate}
Chris@1 163 \item \varname{[vorbis\_residue\_count]} = read 6 bits as unsigned integer and add one
Chris@1 164
Chris@1 165 \item For each of \varname{[vorbis\_residue\_count]} residue numbers:
Chris@1 166 \begin{enumerate}
Chris@1 167 \item read the residue type; vector \varname{[vorbis\_residue\_types]} element \varname{[i]} = read 16 bits as unsigned integer
Chris@1 168 \item If the residue type is zero,
Chris@1 169 one or two, decode the residue configuration as defined in \xref{vorbis:spec:residue}; save this configuration in slot \varname{[i]} of the residue configuration array \varname{[vorbis\_residue\_configurations]}.
Chris@1 170 \item If the the residue type is greater than two, this stream is undecodable; ERROR CONDITION
Chris@1 171 \end{enumerate}
Chris@1 172
Chris@1 173 \end{enumerate}
Chris@1 174
Chris@1 175
Chris@1 176
Chris@1 177 \paragraph{Mappings}
Chris@1 178
Chris@1 179 Mappings are used to set up specific pipelines for encoding
Chris@1 180 multichannel audio with varying channel mapping applications. Vorbis I
Chris@1 181 uses a single mapping type (0), with implicit PCM channel mappings.
Chris@1 182
Chris@1 183 % FIXME/TODO: LaTeX cannot nest enumerate that deeply, so I have to use
Chris@1 184 % itemize at the innermost level. However, it would be much better to
Chris@1 185 % rewrite this pseudocode using listings or algoritmicx or some other
Chris@1 186 % package geared towards this.
Chris@1 187 \begin{enumerate}
Chris@1 188 \item \varname{[vorbis\_mapping\_count]} = read 6 bits as unsigned integer and add one
Chris@1 189 \item For each \varname{[i]} of \varname{[vorbis\_mapping\_count]} mapping numbers:
Chris@1 190 \begin{enumerate}
Chris@1 191 \item read the mapping type: 16 bits as unsigned integer. There's no reason to save the mapping type in Vorbis I.
Chris@1 192 \item If the mapping type is nonzero, the stream is undecodable
Chris@1 193 \item If the mapping type is zero:
Chris@1 194 \begin{enumerate}
Chris@1 195 \item read 1 bit as a boolean flag
Chris@1 196 \begin{enumerate}
Chris@1 197 \item if set, \varname{[vorbis\_mapping\_submaps]} = read 4 bits as unsigned integer and add one
Chris@1 198 \item if unset, \varname{[vorbis\_mapping\_submaps]} = 1
Chris@1 199 \end{enumerate}
Chris@1 200
Chris@1 201
Chris@1 202 \item read 1 bit as a boolean flag
Chris@1 203 \begin{enumerate}
Chris@1 204 \item if set, square polar channel mapping is in use:
Chris@1 205 \begin{itemize}
Chris@1 206 \item \varname{[vorbis\_mapping\_coupling\_steps]} = read 8 bits as unsigned integer and add one
Chris@1 207 \item for \varname{[j]} each of \varname{[vorbis\_mapping\_coupling\_steps]} steps:
Chris@1 208 \begin{itemize}
Chris@1 209 \item vector \varname{[vorbis\_mapping\_magnitude]} element \varname{[j]}= read \link{vorbis:spec:ilog}{ilog}(\varname{[audio\_channels]} - 1) bits as unsigned integer
Chris@1 210 \item vector \varname{[vorbis\_mapping\_angle]} element \varname{[j]}= read \link{vorbis:spec:ilog}{ilog}(\varname{[audio\_channels]} - 1) bits as unsigned integer
Chris@1 211 \item the numbers read in the above two steps are channel numbers representing the channel to treat as magnitude and the channel to treat as angle, respectively. If for any coupling step the angle channel number equals the magnitude channel number, the magnitude channel number is greater than \varname{[audio\_channels]}-1, or the angle channel is greater than \varname{[audio\_channels]}-1, the stream is undecodable.
Chris@1 212 \end{itemize}
Chris@1 213
Chris@1 214
Chris@1 215 \end{itemize}
Chris@1 216
Chris@1 217
Chris@1 218 \item if unset, \varname{[vorbis\_mapping\_coupling\_steps]} = 0
Chris@1 219 \end{enumerate}
Chris@1 220
Chris@1 221
Chris@1 222 \item read 2 bits (reserved field); if the value is nonzero, the stream is undecodable
Chris@1 223 \item if \varname{[vorbis\_mapping\_submaps]} is greater than one, we read channel multiplex settings. For each \varname{[j]} of \varname{[audio\_channels]} channels:
Chris@1 224 \begin{enumerate}
Chris@1 225 \item vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} = read 4 bits as unsigned integer
Chris@1 226 \item if the value is greater than the highest numbered submap (\varname{[vorbis\_mapping\_submaps]} - 1), this in an error condition rendering the stream undecodable
Chris@1 227 \end{enumerate}
Chris@1 228
Chris@1 229 \item for each submap \varname{[j]} of \varname{[vorbis\_mapping\_submaps]} submaps, read the floor and residue numbers for use in decoding that submap:
Chris@1 230 \begin{enumerate}
Chris@1 231 \item read and discard 8 bits (the unused time configuration placeholder)
Chris@1 232 \item read 8 bits as unsigned integer for the floor number; save in vector \varname{[vorbis\_mapping\_submap\_floor]} element \varname{[j]}
Chris@1 233 \item verify the floor number is not greater than the highest number floor configured for the bitstream. If it is, the bitstream is undecodable
Chris@1 234 \item read 8 bits as unsigned integer for the residue number; save in vector \varname{[vorbis\_mapping\_submap\_residue]} element \varname{[j]}
Chris@1 235 \item verify the residue number is not greater than the highest number residue configured for the bitstream. If it is, the bitstream is undecodable
Chris@1 236 \end{enumerate}
Chris@1 237
Chris@1 238 \item save this mapping configuration in slot \varname{[i]} of the mapping configuration array \varname{[vorbis\_mapping\_configurations]}.
Chris@1 239 \end{enumerate}
Chris@1 240
Chris@1 241 \end{enumerate}
Chris@1 242
Chris@1 243 \end{enumerate}
Chris@1 244
Chris@1 245
Chris@1 246
Chris@1 247 \paragraph{Modes}
Chris@1 248
Chris@1 249 \begin{enumerate}
Chris@1 250 \item \varname{[vorbis\_mode\_count]} = read 6 bits as unsigned integer and add one
Chris@1 251 \item For each of \varname{[vorbis\_mode\_count]} mode numbers:
Chris@1 252 \begin{enumerate}
Chris@1 253 \item \varname{[vorbis\_mode\_blockflag]} = read 1 bit
Chris@1 254 \item \varname{[vorbis\_mode\_windowtype]} = read 16 bits as unsigned integer
Chris@1 255 \item \varname{[vorbis\_mode\_transformtype]} = read 16 bits as unsigned integer
Chris@1 256 \item \varname{[vorbis\_mode\_mapping]} = read 8 bits as unsigned integer
Chris@1 257 \item verify ranges; zero is the only legal value in Vorbis I for
Chris@1 258 \varname{[vorbis\_mode\_windowtype]}
Chris@1 259 and \varname{[vorbis\_mode\_transformtype]}. \varname{[vorbis\_mode\_mapping]} must not be greater than the highest number mapping in use. Any illegal values render the stream undecodable.
Chris@1 260 \item save this mode configuration in slot \varname{[i]} of the mode configuration array
Chris@1 261 \varname{[vorbis\_mode\_configurations]}.
Chris@1 262 \end{enumerate}
Chris@1 263
Chris@1 264 \item read 1 bit as a framing flag. If unset, a framing error occurred and the stream is not
Chris@1 265 decodable.
Chris@1 266 \end{enumerate}
Chris@1 267
Chris@1 268 After reading mode descriptions, setup header decode is complete.
Chris@1 269
Chris@1 270
Chris@1 271
Chris@1 272
Chris@1 273
Chris@1 274
Chris@1 275
Chris@1 276
Chris@1 277 \subsection{Audio packet decode and synthesis}
Chris@1 278
Chris@1 279 Following the three header packets, all packets in a Vorbis I stream
Chris@1 280 are audio. The first step of audio packet decode is to read and
Chris@1 281 verify the packet type. \emph{A non-audio packet when audio is expected
Chris@1 282 indicates stream corruption or a non-compliant stream. The decoder
Chris@1 283 must ignore the packet and not attempt decoding it to audio}.
Chris@1 284
Chris@1 285
Chris@1 286 \subsubsection{packet type, mode and window decode}
Chris@1 287
Chris@1 288 \begin{enumerate}
Chris@1 289 \item read 1 bit \varname{[packet\_type]}; check that packet type is 0 (audio)
Chris@1 290 \item read \link{vorbis:spec:ilog}{ilog}([vorbis\_mode\_count]-1) bits
Chris@1 291 \varname{[mode\_number]}
Chris@1 292 \item decode blocksize \varname{[n]} is equal to \varname{[blocksize\_0]} if
Chris@1 293 \varname{[vorbis\_mode\_blockflag]} is 0, else \varname{[n]} is equal to \varname{[blocksize\_1]}.
Chris@1 294 \item perform window selection and setup; this window is used later by the inverse MDCT:
Chris@1 295 \begin{enumerate}
Chris@1 296 \item if this is a long window (the \varname{[vorbis\_mode\_blockflag]} flag of this mode is
Chris@1 297 set):
Chris@1 298 \begin{enumerate}
Chris@1 299 \item read 1 bit for \varname{[previous\_window\_flag]}
Chris@1 300 \item read 1 bit for \varname{[next\_window\_flag]}
Chris@1 301 \item if \varname{[previous\_window\_flag]} is not set, the left half
Chris@1 302 of the window will be a hybrid window for lapping with a
Chris@1 303 short block. See \xref{vorbis:spec:window} for an illustration of overlapping
Chris@1 304 dissimilar
Chris@1 305 windows. Else, the left half window will have normal long
Chris@1 306 shape.
Chris@1 307 \item if \varname{[next\_window\_flag]} is not set, the right half of
Chris@1 308 the window will be a hybrid window for lapping with a short
Chris@1 309 block. See \xref{vorbis:spec:window} for an
Chris@1 310 illustration of overlapping dissimilar
Chris@1 311 windows. Else, the left right window will have normal long
Chris@1 312 shape.
Chris@1 313 \end{enumerate}
Chris@1 314
Chris@1 315 \item if this is a short window, the window is always the same
Chris@1 316 short-window shape.
Chris@1 317 \end{enumerate}
Chris@1 318
Chris@1 319 \end{enumerate}
Chris@1 320
Chris@1 321 Vorbis windows all use the slope function $y=\sin(\frac{\pi}{2} * \sin^2((x+0.5)/n * \pi))$,
Chris@1 322 where $n$ is window size and $x$ ranges $0 \ldots n-1$, but dissimilar
Chris@1 323 lapping requirements can affect overall shape. Window generation
Chris@1 324 proceeds as follows:
Chris@1 325
Chris@1 326 \begin{enumerate}
Chris@1 327 \item \varname{[window\_center]} = \varname{[n]} / 2
Chris@1 328 \item if (\varname{[vorbis\_mode\_blockflag]} is set and \varname{[previous\_window\_flag]} is
Chris@1 329 not set) then
Chris@1 330 \begin{enumerate}
Chris@1 331 \item \varname{[left\_window\_start]} = \varname{[n]}/4 -
Chris@1 332 \varname{[blocksize\_0]}/4
Chris@1 333 \item \varname{[left\_window\_end]} = \varname{[n]}/4 + \varname{[blocksize\_0]}/4
Chris@1 334 \item \varname{[left\_n]} = \varname{[blocksize\_0]}/2
Chris@1 335 \end{enumerate}
Chris@1 336 else
Chris@1 337 \begin{enumerate}
Chris@1 338 \item \varname{[left\_window\_start]} = 0
Chris@1 339 \item \varname{[left\_window\_end]} = \varname{[window\_center]}
Chris@1 340 \item \varname{[left\_n]} = \varname{[n]}/2
Chris@1 341 \end{enumerate}
Chris@1 342
Chris@1 343 \item if (\varname{[vorbis\_mode\_blockflag]} is set and \varname{[next\_window\_flag]} is not
Chris@1 344 set) then
Chris@1 345 \begin{enumerate}
Chris@1 346 \item \varname{[right\_window\_start]} = \varname{[n]*3}/4 -
Chris@1 347 \varname{[blocksize\_0]}/4
Chris@1 348 \item \varname{[right\_window\_end]} = \varname{[n]*3}/4 +
Chris@1 349 \varname{[blocksize\_0]}/4
Chris@1 350 \item \varname{[right\_n]} = \varname{[blocksize\_0]}/2
Chris@1 351 \end{enumerate}
Chris@1 352 else
Chris@1 353 \begin{enumerate}
Chris@1 354 \item \varname{[right\_window\_start]} = \varname{[window\_center]}
Chris@1 355 \item \varname{[right\_window\_end]} = \varname{[n]}
Chris@1 356 \item \varname{[right\_n]} = \varname{[n]}/2
Chris@1 357 \end{enumerate}
Chris@1 358
Chris@1 359 \item window from range 0 ... \varname{[left\_window\_start]}-1 inclusive is zero
Chris@1 360 \item for \varname{[i]} in range \varname{[left\_window\_start]} ...
Chris@1 361 \varname{[left\_window\_end]}-1, window(\varname{[i]}) = $\sin(\frac{\pi}{2} * \sin^2($ (\varname{[i]}-\varname{[left\_window\_start]}+0.5) / \varname{[left\_n]} $* \frac{\pi}{2})$ )
Chris@1 362 \item window from range \varname{[left\_window\_end]} ... \varname{[right\_window\_start]}-1
Chris@1 363 inclusive is one\item for \varname{[i]} in range \varname{[right\_window\_start]} ... \varname{[right\_window\_end]}-1, window(\varname{[i]}) = $\sin(\frac{\pi}{2} * \sin^2($ (\varname{[i]}-\varname{[right\_window\_start]}+0.5) / \varname{[right\_n]} $ * \frac{\pi}{2} + \frac{\pi}{2})$ )
Chris@1 364 \item window from range \varname{[right\_window\_start]} ... \varname{[n]}-1 is
Chris@1 365 zero
Chris@1 366 \end{enumerate}
Chris@1 367
Chris@1 368 An end-of-packet condition up to this point should be considered an
Chris@1 369 error that discards this packet from the stream. An end of packet
Chris@1 370 condition past this point is to be considered a possible nominal
Chris@1 371 occurrence.
Chris@1 372
Chris@1 373
Chris@1 374
Chris@1 375 \subsubsection{floor curve decode}
Chris@1 376
Chris@1 377 From this point on, we assume out decode context is using mode number
Chris@1 378 \varname{[mode\_number]} from configuration array
Chris@1 379 \varname{[vorbis\_mode\_configurations]} and the map number
Chris@1 380 \varname{[vorbis\_mode\_mapping]} (specified by the current mode) taken
Chris@1 381 from the mapping configuration array
Chris@1 382 \varname{[vorbis\_mapping\_configurations]}.
Chris@1 383
Chris@1 384 Floor curves are decoded one-by-one in channel order.
Chris@1 385
Chris@1 386 For each floor \varname{[i]} of \varname{[audio\_channels]}
Chris@1 387 \begin{enumerate}
Chris@1 388 \item \varname{[submap\_number]} = element \varname{[i]} of vector [vorbis\_mapping\_mux]
Chris@1 389 \item \varname{[floor\_number]} = element \varname{[submap\_number]} of vector
Chris@1 390 [vorbis\_submap\_floor]
Chris@1 391 \item if the floor type of this
Chris@1 392 floor (vector \varname{[vorbis\_floor\_types]} element
Chris@1 393 \varname{[floor\_number]}) is zero then decode the floor for
Chris@1 394 channel \varname{[i]} according to the
Chris@1 395 \xref{vorbis:spec:floor0-decode}
Chris@1 396 \item if the type of this floor
Chris@1 397 is one then decode the floor for channel \varname{[i]} according
Chris@1 398 to the \xref{vorbis:spec:floor1-decode}
Chris@1 399 \item save the needed decoded floor information for channel for later synthesis
Chris@1 400 \item if the decoded floor returned 'unused', set vector \varname{[no\_residue]} element
Chris@1 401 \varname{[i]} to true, else set vector \varname{[no\_residue]} element \varname{[i]} to
Chris@1 402 false
Chris@1 403 \end{enumerate}
Chris@1 404
Chris@1 405
Chris@1 406 An end-of-packet condition during floor decode shall result in packet
Chris@1 407 decode zeroing all channel output vectors and skipping to the
Chris@1 408 add/overlap output stage.
Chris@1 409
Chris@1 410
Chris@1 411
Chris@1 412 \subsubsection{nonzero vector propagate}
Chris@1 413
Chris@1 414 A possible result of floor decode is that a specific vector is marked
Chris@1 415 'unused' which indicates that that final output vector is all-zero
Chris@1 416 values (and the floor is zero). The residue for that vector is not
Chris@1 417 coded in the stream, save for one complication. If some vectors are
Chris@1 418 used and some are not, channel coupling could result in mixing a
Chris@1 419 zeroed and nonzeroed vector to produce two nonzeroed vectors.
Chris@1 420
Chris@1 421 for each \varname{[i]} from 0 ... \varname{[vorbis\_mapping\_coupling\_steps]}-1
Chris@1 422
Chris@1 423 \begin{enumerate}
Chris@1 424 \item if either \varname{[no\_residue]} entry for channel
Chris@1 425 (\varname{[vorbis\_mapping\_magnitude]} element \varname{[i]})
Chris@1 426 or channel
Chris@1 427 (\varname{[vorbis\_mapping\_angle]} element \varname{[i]})
Chris@1 428 are set to false, then both must be set to false. Note that an 'unused'
Chris@1 429 floor has no decoded floor information; it is important that this is
Chris@1 430 remembered at floor curve synthesis time.
Chris@1 431 \end{enumerate}
Chris@1 432
Chris@1 433
Chris@1 434
Chris@1 435
Chris@1 436 \subsubsection{residue decode}
Chris@1 437
Chris@1 438 Unlike floors, which are decoded in channel order, the residue vectors
Chris@1 439 are decoded in submap order.
Chris@1 440
Chris@1 441 for each submap \varname{[i]} in order from 0 ... \varname{[vorbis\_mapping\_submaps]}-1
Chris@1 442
Chris@1 443 \begin{enumerate}
Chris@1 444 \item \varname{[ch]} = 0
Chris@1 445 \item for each channel \varname{[j]} in order from 0 ... \varname{[audio\_channels]} - 1
Chris@1 446 \begin{enumerate}
Chris@1 447 \item if channel \varname{[j]} in submap \varname{[i]} (vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} is equal to \varname{[i]})
Chris@1 448 \begin{enumerate}
Chris@1 449 \item if vector \varname{[no\_residue]} element \varname{[j]} is true
Chris@1 450 \begin{enumerate}
Chris@1 451 \item vector \varname{[do\_not\_decode\_flag]} element \varname{[ch]} is set
Chris@1 452 \end{enumerate}
Chris@1 453 else
Chris@1 454 \begin{enumerate}
Chris@1 455 \item vector \varname{[do\_not\_decode\_flag]} element \varname{[ch]} is unset
Chris@1 456 \end{enumerate}
Chris@1 457
Chris@1 458 \item increment \varname{[ch]}
Chris@1 459 \end{enumerate}
Chris@1 460
Chris@1 461 \end{enumerate}
Chris@1 462 \item \varname{[residue\_number]} = vector \varname{[vorbis\_mapping\_submap\_residue]} element \varname{[i]}
Chris@1 463 \item \varname{[residue\_type]} = vector \varname{[vorbis\_residue\_types]} element \varname{[residue\_number]}
Chris@1 464 \item decode \varname{[ch]} vectors using residue \varname{[residue\_number]}, according to type \varname{[residue\_type]}, also passing vector \varname{[do\_not\_decode\_flag]} to indicate which vectors in the bundle should not be decoded. Correct per-vector decode length is \varname{[n]}/2.
Chris@1 465 \item \varname{[ch]} = 0
Chris@1 466 \item for each channel \varname{[j]} in order from 0 ... \varname{[audio\_channels]}
Chris@1 467 \begin{enumerate}
Chris@1 468 \item if channel \varname{[j]} is in submap \varname{[i]} (vector \varname{[vorbis\_mapping\_mux]} element \varname{[j]} is equal to \varname{[i]})
Chris@1 469 \begin{enumerate}
Chris@1 470 \item residue vector for channel \varname{[j]} is set to decoded residue vector \varname{[ch]}
Chris@1 471 \item increment \varname{[ch]}
Chris@1 472 \end{enumerate}
Chris@1 473
Chris@1 474 \end{enumerate}
Chris@1 475
Chris@1 476 \end{enumerate}
Chris@1 477
Chris@1 478
Chris@1 479
Chris@1 480 \subsubsection{inverse coupling}
Chris@1 481
Chris@1 482 for each \varname{[i]} from \varname{[vorbis\_mapping\_coupling\_steps]}-1 descending to 0
Chris@1 483
Chris@1 484 \begin{enumerate}
Chris@1 485 \item \varname{[magnitude\_vector]} = the residue vector for channel
Chris@1 486 (vector \varname{[vorbis\_mapping\_magnitude]} element \varname{[i]})
Chris@1 487 \item \varname{[angle\_vector]} = the residue vector for channel (vector
Chris@1 488 \varname{[vorbis\_mapping\_angle]} element \varname{[i]})
Chris@1 489 \item for each scalar value \varname{[M]} in vector \varname{[magnitude\_vector]} and the corresponding scalar value \varname{[A]} in vector \varname{[angle\_vector]}:
Chris@1 490 \begin{enumerate}
Chris@1 491 \item if (\varname{[M]} is greater than zero)
Chris@1 492 \begin{enumerate}
Chris@1 493 \item if (\varname{[A]} is greater than zero)
Chris@1 494 \begin{enumerate}
Chris@1 495 \item \varname{[new\_M]} = \varname{[M]}
Chris@1 496 \item \varname{[new\_A]} = \varname{[M]}-\varname{[A]}
Chris@1 497 \end{enumerate}
Chris@1 498 else
Chris@1 499 \begin{enumerate}
Chris@1 500 \item \varname{[new\_A]} = \varname{[M]}
Chris@1 501 \item \varname{[new\_M]} = \varname{[M]}+\varname{[A]}
Chris@1 502 \end{enumerate}
Chris@1 503
Chris@1 504 \end{enumerate}
Chris@1 505 else
Chris@1 506 \begin{enumerate}
Chris@1 507 \item if (\varname{[A]} is greater than zero)
Chris@1 508 \begin{enumerate}
Chris@1 509 \item \varname{[new\_M]} = \varname{[M]}
Chris@1 510 \item \varname{[new\_A]} = \varname{[M]}+\varname{[A]}
Chris@1 511 \end{enumerate}
Chris@1 512 else
Chris@1 513 \begin{enumerate}
Chris@1 514 \item \varname{[new\_A]} = \varname{[M]}
Chris@1 515 \item \varname{[new\_M]} = \varname{[M]}-\varname{[A]}
Chris@1 516 \end{enumerate}
Chris@1 517
Chris@1 518 \end{enumerate}
Chris@1 519
Chris@1 520 \item set scalar value \varname{[M]} in vector \varname{[magnitude\_vector]} to \varname{[new\_M]}
Chris@1 521 \item set scalar value \varname{[A]} in vector \varname{[angle\_vector]} to \varname{[new\_A]}
Chris@1 522 \end{enumerate}
Chris@1 523
Chris@1 524 \end{enumerate}
Chris@1 525
Chris@1 526
Chris@1 527
Chris@1 528
Chris@1 529 \subsubsection{dot product}
Chris@1 530
Chris@1 531 For each channel, synthesize the floor curve from the decoded floor
Chris@1 532 information, according to packet type. Note that the vector synthesis
Chris@1 533 length for floor computation is \varname{[n]}/2.
Chris@1 534
Chris@1 535 For each channel, multiply each element of the floor curve by each
Chris@1 536 element of that channel's residue vector. The result is the dot
Chris@1 537 product of the floor and residue vectors for each channel; the produced
Chris@1 538 vectors are the length \varname{[n]}/2 audio spectrum for each
Chris@1 539 channel.
Chris@1 540
Chris@1 541 % TODO/FIXME: The following two paragraphs have identical twins
Chris@1 542 % in section 1 (under "compute floor/residue dot product")
Chris@1 543 One point is worth mentioning about this dot product; a common mistake
Chris@1 544 in a fixed point implementation might be to assume that a 32 bit
Chris@1 545 fixed-point representation for floor and residue and direct
Chris@1 546 multiplication of the vectors is sufficient for acceptable spectral
Chris@1 547 depth in all cases because it happens to mostly work with the current
Chris@1 548 Xiph.Org reference encoder.
Chris@1 549
Chris@1 550 However, floor vector values can span \~140dB (\~24 bits unsigned), and
Chris@1 551 the audio spectrum vector should represent a minimum of 120dB (\~21
Chris@1 552 bits with sign), even when output is to a 16 bit PCM device. For the
Chris@1 553 residue vector to represent full scale if the floor is nailed to
Chris@1 554 $-140$dB, it must be able to span 0 to $+140$dB. For the residue vector
Chris@1 555 to reach full scale if the floor is nailed at 0dB, it must be able to
Chris@1 556 represent $-140$dB to $+0$dB. Thus, in order to handle full range
Chris@1 557 dynamics, a residue vector may span $-140$dB to $+140$dB entirely within
Chris@1 558 spec. A 280dB range is approximately 48 bits with sign; thus the
Chris@1 559 residue vector must be able to represent a 48 bit range and the dot
Chris@1 560 product must be able to handle an effective 48 bit times 24 bit
Chris@1 561 multiplication. This range may be achieved using large (64 bit or
Chris@1 562 larger) integers, or implementing a movable binary point
Chris@1 563 representation.
Chris@1 564
Chris@1 565
Chris@1 566
Chris@1 567 \subsubsection{inverse MDCT}
Chris@1 568
Chris@1 569 Convert the audio spectrum vector of each channel back into time
Chris@1 570 domain PCM audio via an inverse Modified Discrete Cosine Transform
Chris@1 571 (MDCT). A detailed description of the MDCT is available in \cite{Sporer/Brandenburg/Edler}. The window
Chris@1 572 function used for the MDCT is the function described earlier.
Chris@1 573
Chris@1 574
Chris@1 575
Chris@1 576 \subsubsection{overlap\_add}
Chris@1 577
Chris@1 578 Windowed MDCT output is overlapped and added with the right hand data
Chris@1 579 of the previous window such that the 3/4 point of the previous window
Chris@1 580 is aligned with the 1/4 point of the current window (as illustrated in
Chris@1 581 \xref{vorbis:spec:window}). The overlapped portion
Chris@1 582 produced from overlapping the previous and current frame data is
Chris@1 583 finished data to be returned by the decoder. This data spans from the
Chris@1 584 center of the previous window to the center of the current window. In
Chris@1 585 the case of same-sized windows, the amount of data to return is
Chris@1 586 one-half block consisting of and only of the overlapped portions. When
Chris@1 587 overlapping a short and long window, much of the returned range does not
Chris@1 588 actually overlap. This does not damage transform orthogonality. Pay
Chris@1 589 attention however to returning the correct data range; the amount of
Chris@1 590 data to be returned is:
Chris@1 591
Chris@1 592 \begin{programlisting}
Chris@1 593 window\_blocksize(previous\_window)/4+window\_blocksize(current\_window)/4
Chris@1 594 \end{programlisting}
Chris@1 595
Chris@1 596 from the center (element windowsize/2) of the previous window to the
Chris@1 597 center (element windowsize/2-1, inclusive) of the current window.
Chris@1 598
Chris@1 599 Data is not returned from the first frame; it must be used to 'prime'
Chris@1 600 the decode engine. The encoder accounts for this priming when
Chris@1 601 calculating PCM offsets; after the first frame, the proper PCM output
Chris@1 602 offset is '0' (as no data has been returned yet).
Chris@1 603
Chris@1 604
Chris@1 605
Chris@1 606 \subsubsection{output channel order}
Chris@1 607
Chris@1 608 Vorbis I specifies only a channel mapping type 0. In mapping type 0,
Chris@1 609 channel mapping is implicitly defined as follows for standard audio
Chris@1 610 applications. As of revision 16781 (20100113), the specification adds
Chris@1 611 defined channel locations for 6.1 and 7.1 surround. Ordering/location
Chris@1 612 for greater-than-eight channels remains 'left to the implementation'.
Chris@1 613
Chris@1 614 These channel orderings refer to order within the encoded stream. It
Chris@1 615 is naturally possible for a decoder to produce output with channels in
Chris@1 616 any order. Any such decoder should explicitly document channel
Chris@1 617 reordering behavior.
Chris@1 618
Chris@1 619 \begin{description} %[style=nextline]
Chris@1 620 \item[one channel]
Chris@1 621 the stream is monophonic
Chris@1 622
Chris@1 623 \item[two channels]
Chris@1 624 the stream is stereo. channel order: left, right
Chris@1 625
Chris@1 626 \item[three channels]
Chris@1 627 the stream is a 1d-surround encoding. channel order: left,
Chris@1 628 center, right
Chris@1 629
Chris@1 630 \item[four channels]
Chris@1 631 the stream is quadraphonic surround. channel order: front left,
Chris@1 632 front right, rear left, rear right
Chris@1 633
Chris@1 634 \item[five channels]
Chris@1 635 the stream is five-channel surround. channel order: front left,
Chris@1 636 center, front right, rear left, rear right
Chris@1 637
Chris@1 638 \item[six channels]
Chris@1 639 the stream is 5.1 surround. channel order: front left, center,
Chris@1 640 front right, rear left, rear right, LFE
Chris@1 641
Chris@1 642 \item[seven channels]
Chris@1 643 the stream is 6.1 surround. channel order: front left, center,
Chris@1 644 front right, side left, side right, rear center, LFE
Chris@1 645
Chris@1 646 \item[eight channels]
Chris@1 647 the stream is 7.1 surround. channel order: front left, center,
Chris@1 648 front right, side left, side right, rear left, rear right,
Chris@1 649 LFE
Chris@1 650
Chris@1 651 \item[greater than eight channels]
Chris@1 652 channel use and order is defined by the application
Chris@1 653
Chris@1 654 \end{description}
Chris@1 655
Chris@1 656 Applications using Vorbis for dedicated purposes may define channel
Chris@1 657 mapping as seen fit. Future channel mappings (such as three and four
Chris@1 658 channel \href{http://www.ambisonic.net/}{Ambisonics}) will
Chris@1 659 make use of channel mappings other than mapping 0.
Chris@1 660
Chris@1 661