01-introduction.tex 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
  2. %!TEX root = Vorbis_I_spec.tex
  3. \section{Introduction and Description} \label{vorbis:spec:intro}
  4. \subsection{Overview}
  5. This document provides a high level description of the Vorbis codec's
  6. construction. A bit-by-bit specification appears beginning in
  7. \xref{vorbis:spec:codec}.
  8. The later sections assume a high-level
  9. understanding of the Vorbis decode process, which is
  10. provided here.
  11. \subsubsection{Application}
  12. Vorbis is a general purpose perceptual audio CODEC intended to allow
  13. maximum encoder flexibility, thus allowing it to scale competitively
  14. over an exceptionally wide range of bitrates. At the high
  15. quality/bitrate end of the scale (CD or DAT rate stereo, 16/24 bits)
  16. it is in the same league as MPEG-2 and MPC. Similarly, the 1.0
  17. encoder can encode high-quality CD and DAT rate stereo at below 48kbps
  18. without resampling to a lower rate. Vorbis is also intended for
  19. lower and higher sample rates (from 8kHz telephony to 192kHz digital
  20. masters) and a range of channel representations (monaural,
  21. polyphonic, stereo, quadraphonic, 5.1, ambisonic, or up to 255
  22. discrete channels).
  23. \subsubsection{Classification}
  24. Vorbis I is a forward-adaptive monolithic transform CODEC based on the
  25. Modified Discrete Cosine Transform. The codec is structured to allow
  26. addition of a hybrid wavelet filterbank in Vorbis II to offer better
  27. transient response and reproduction using a transform better suited to
  28. localized time events.
  29. \subsubsection{Assumptions}
  30. The Vorbis CODEC design assumes a complex, psychoacoustically-aware
  31. encoder and simple, low-complexity decoder. Vorbis decode is
  32. computationally simpler than mp3, although it does require more
  33. working memory as Vorbis has no static probability model; the vector
  34. codebooks used in the first stage of decoding from the bitstream are
  35. packed in their entirety into the Vorbis bitstream headers. In
  36. packed form, these codebooks occupy only a few kilobytes; the extent
  37. to which they are pre-decoded into a cache is the dominant factor in
  38. decoder memory usage.
  39. Vorbis provides none of its own framing, synchronization or protection
  40. against errors; it is solely a method of accepting input audio,
  41. dividing it into individual frames and compressing these frames into
  42. raw, unformatted 'packets'. The decoder then accepts these raw
  43. packets in sequence, decodes them, synthesizes audio frames from
  44. them, and reassembles the frames into a facsimile of the original
  45. audio stream. Vorbis is a free-form variable bit rate (VBR) codec and packets have no
  46. minimum size, maximum size, or fixed/expected size. Packets
  47. are designed that they may be truncated (or padded) and remain
  48. decodable; this is not to be considered an error condition and is used
  49. extensively in bitrate management in peeling. Both the transport
  50. mechanism and decoder must allow that a packet may be any size, or
  51. end before or after packet decode expects.
  52. Vorbis packets are thus intended to be used with a transport mechanism
  53. that provides free-form framing, sync, positioning and error correction
  54. in accordance with these design assumptions, such as Ogg (for file
  55. transport) or RTP (for network multicast). For purposes of a few
  56. examples in this document, we will assume that Vorbis is to be
  57. embedded in an Ogg stream specifically, although this is by no means a
  58. requirement or fundamental assumption in the Vorbis design.
  59. The specification for embedding Vorbis into
  60. an Ogg transport stream is in \xref{vorbis:over:ogg}.
  61. \subsubsection{Codec Setup and Probability Model}
  62. Vorbis' heritage is as a research CODEC and its current design
  63. reflects a desire to allow multiple decades of continuous encoder
  64. improvement before running out of room within the codec specification.
  65. For these reasons, configurable aspects of codec setup intentionally
  66. lean toward the extreme of forward adaptive.
  67. The single most controversial design decision in Vorbis (and the most
  68. unusual for a Vorbis developer to keep in mind) is that the entire
  69. probability model of the codec, the Huffman and VQ codebooks, is
  70. packed into the bitstream header along with extensive CODEC setup
  71. parameters (often several hundred fields). This makes it impossible,
  72. as it would be with MPEG audio layers, to embed a simple frame type
  73. flag in each audio packet, or begin decode at any frame in the stream
  74. without having previously fetched the codec setup header.
  75. \begin{note}
  76. Vorbis \emph{can} initiate decode at any arbitrary packet within a
  77. bitstream so long as the codec has been initialized/setup with the
  78. setup headers.
  79. \end{note}
  80. Thus, Vorbis headers are both required for decode to begin and
  81. relatively large as bitstream headers go. The header size is
  82. unbounded, although for streaming a rule-of-thumb of 4kB or less is
  83. recommended (and Xiph.Org's Vorbis encoder follows this suggestion).
  84. Our own design work indicates the primary liability of the
  85. required header is in mindshare; it is an unusual design and thus
  86. causes some amount of complaint among engineers as this runs against
  87. current design trends (and also points out limitations in some
  88. existing software/interface designs, such as Windows' ACM codec
  89. framework). However, we find that it does not fundamentally limit
  90. Vorbis' suitable application space.
  91. \subsubsection{Format Specification}
  92. The Vorbis format is well-defined by its decode specification; any
  93. encoder that produces packets that are correctly decoded by the
  94. reference Vorbis decoder described below may be considered a proper
  95. Vorbis encoder. A decoder must faithfully and completely implement
  96. the specification defined below (except where noted) to be considered
  97. a proper Vorbis decoder.
  98. \subsubsection{Hardware Profile}
  99. Although Vorbis decode is computationally simple, it may still run
  100. into specific limitations of an embedded design. For this reason,
  101. embedded designs are allowed to deviate in limited ways from the
  102. `full' decode specification yet still be certified compliant. These
  103. optional omissions are labelled in the spec where relevant.
  104. \subsection{Decoder Configuration}
  105. Decoder setup consists of configuration of multiple, self-contained
  106. component abstractions that perform specific functions in the decode
  107. pipeline. Each different component instance of a specific type is
  108. semantically interchangeable; decoder configuration consists both of
  109. internal component configuration, as well as arrangement of specific
  110. instances into a decode pipeline. Componentry arrangement is roughly
  111. as follows:
  112. \begin{center}
  113. \includegraphics[width=\textwidth]{components}
  114. \captionof{figure}{decoder pipeline configuration}
  115. \end{center}
  116. \subsubsection{Global Config}
  117. Global codec configuration consists of a few audio related fields
  118. (sample rate, channels), Vorbis version (always '0' in Vorbis I),
  119. bitrate hints, and the lists of component instances. All other
  120. configuration is in the context of specific components.
  121. \subsubsection{Mode}
  122. Each Vorbis frame is coded according to a master 'mode'. A bitstream
  123. may use one or many modes.
  124. The mode mechanism is used to encode a frame according to one of
  125. multiple possible methods with the intention of choosing a method best
  126. suited to that frame. Different modes are, e.g. how frame size
  127. is changed from frame to frame. The mode number of a frame serves as a
  128. top level configuration switch for all other specific aspects of frame
  129. decode.
  130. A 'mode' configuration consists of a frame size setting, window type
  131. (always 0, the Vorbis window, in Vorbis I), transform type (always
  132. type 0, the MDCT, in Vorbis I) and a mapping number. The mapping
  133. number specifies which mapping configuration instance to use for
  134. low-level packet decode and synthesis.
  135. \subsubsection{Mapping}
  136. A mapping contains a channel coupling description and a list of
  137. 'submaps' that bundle sets of channel vectors together for grouped
  138. encoding and decoding. These submaps are not references to external
  139. components; the submap list is internal and specific to a mapping.
  140. A 'submap' is a configuration/grouping that applies to a subset of
  141. floor and residue vectors within a mapping. The submap functions as a
  142. last layer of indirection such that specific special floor or residue
  143. settings can be applied not only to all the vectors in a given mode,
  144. but also specific vectors in a specific mode. Each submap specifies
  145. the proper floor and residue instance number to use for decoding that
  146. submap's spectral floor and spectral residue vectors.
  147. As an example:
  148. Assume a Vorbis stream that contains six channels in the standard 5.1
  149. format. The sixth channel, as is normal in 5.1, is bass only.
  150. Therefore it would be wasteful to encode a full-spectrum version of it
  151. as with the other channels. The submapping mechanism can be used to
  152. apply a full range floor and residue encoding to channels 0 through 4,
  153. and a bass-only representation to the bass channel, thus saving space.
  154. In this example, channels 0-4 belong to submap 0 (which indicates use
  155. of a full-range floor) and channel 5 belongs to submap 1, which uses a
  156. bass-only representation.
  157. \subsubsection{Floor}
  158. Vorbis encodes a spectral 'floor' vector for each PCM channel. This
  159. vector is a low-resolution representation of the audio spectrum for
  160. the given channel in the current frame, generally used akin to a
  161. whitening filter. It is named a 'floor' because the Xiph.Org
  162. reference encoder has historically used it as a unit-baseline for
  163. spectral resolution.
  164. A floor encoding may be of two types. Floor 0 uses a packed LSP
  165. representation on a dB amplitude scale and Bark frequency scale.
  166. Floor 1 represents the curve as a piecewise linear interpolated
  167. representation on a dB amplitude scale and linear frequency scale.
  168. The two floors are semantically interchangeable in
  169. encoding/decoding. However, floor type 1 provides more stable
  170. inter-frame behavior, and so is the preferred choice in all
  171. coupled-stereo and high bitrate modes. Floor 1 is also considerably
  172. less expensive to decode than floor 0.
  173. Floor 0 is not to be considered deprecated, but it is of limited
  174. modern use. No known Vorbis encoder past Xiph.Org's own beta 4 makes
  175. use of floor 0.
  176. The values coded/decoded by a floor are both compactly formatted and
  177. make use of entropy coding to save space. For this reason, a floor
  178. configuration generally refers to multiple codebooks in the codebook
  179. component list. Entropy coding is thus provided as an abstraction,
  180. and each floor instance may choose from any and all available
  181. codebooks when coding/decoding.
  182. \subsubsection{Residue}
  183. The spectral residue is the fine structure of the audio spectrum
  184. once the floor curve has been subtracted out. In simplest terms, it
  185. is coded in the bitstream using cascaded (multi-pass) vector
  186. quantization according to one of three specific packing/coding
  187. algorithms numbered 0 through 2. The packing algorithm details are
  188. configured by residue instance. As with the floor components, the
  189. final VQ/entropy encoding is provided by external codebook instances
  190. and each residue instance may choose from any and all available
  191. codebooks.
  192. \subsubsection{Codebooks}
  193. Codebooks are a self-contained abstraction that perform entropy
  194. decoding and, optionally, use the entropy-decoded integer value as an
  195. offset into an index of output value vectors, returning the indicated
  196. vector of values.
  197. The entropy coding in a Vorbis I codebook is provided by a standard
  198. Huffman binary tree representation. This tree is tightly packed using
  199. one of several methods, depending on whether codeword lengths are
  200. ordered or unordered, or the tree is sparse.
  201. The codebook vector index is similarly packed according to index
  202. characteristic. Most commonly, the vector index is encoded as a
  203. single list of values of possible values that are then permuted into
  204. a list of n-dimensional rows (lattice VQ).
  205. \subsection{High-level Decode Process}
  206. \subsubsection{Decode Setup}
  207. Before decoding can begin, a decoder must initialize using the
  208. bitstream headers matching the stream to be decoded. Vorbis uses
  209. three header packets; all are required, in-order, by this
  210. specification. Once set up, decode may begin at any audio packet
  211. belonging to the Vorbis stream. In Vorbis I, all packets after the
  212. three initial headers are audio packets.
  213. The header packets are, in order, the identification
  214. header, the comments header, and the setup header.
  215. \paragraph{Identification Header}
  216. The identification header identifies the bitstream as Vorbis, Vorbis
  217. version, and the simple audio characteristics of the stream such as
  218. sample rate and number of channels.
  219. \paragraph{Comment Header}
  220. The comment header includes user text comments (``tags'') and a vendor
  221. string for the application/library that produced the bitstream. The
  222. encoding and proper use of the comment header is described in \xref{vorbis:spec:comment}.
  223. \paragraph{Setup Header}
  224. The setup header includes extensive CODEC setup information as well as
  225. the complete VQ and Huffman codebooks needed for decode.
  226. \subsubsection{Decode Procedure}
  227. The decoding and synthesis procedure for all audio packets is
  228. fundamentally the same.
  229. \begin{enumerate}
  230. \item decode packet type flag
  231. \item decode mode number
  232. \item decode window shape (long windows only)
  233. \item decode floor
  234. \item decode residue into residue vectors
  235. \item inverse channel coupling of residue vectors
  236. \item generate floor curve from decoded floor data
  237. \item compute dot product of floor and residue, producing audio spectrum vector
  238. \item inverse monolithic transform of audio spectrum vector, always an MDCT in Vorbis I
  239. \item overlap/add left-hand output of transform with right-hand output of previous frame
  240. \item store right hand-data from transform of current frame for future lapping
  241. \item if not first frame, return results of overlap/add as audio result of current frame
  242. \end{enumerate}
  243. Note that clever rearrangement of the synthesis arithmetic is
  244. possible; as an example, one can take advantage of symmetries in the
  245. MDCT to store the right-hand transform data of a partial MDCT for a
  246. 50\% inter-frame buffer space savings, and then complete the transform
  247. later before overlap/add with the next frame. This optimization
  248. produces entirely equivalent output and is naturally perfectly legal.
  249. The decoder must be \emph{entirely mathematically equivalent} to the
  250. specification, it need not be a literal semantic implementation.
  251. \paragraph{Packet type decode}
  252. Vorbis I uses four packet types. The first three packet types mark each
  253. of the three Vorbis headers described above. The fourth packet type
  254. marks an audio packet. All other packet types are reserved; packets
  255. marked with a reserved type should be ignored.
  256. Following the three header packets, all packets in a Vorbis I stream
  257. are audio. The first step of audio packet decode is to read and
  258. verify the packet type; \emph{a non-audio packet when audio is expected
  259. indicates stream corruption or a non-compliant stream. The decoder
  260. must ignore the packet and not attempt decoding it to
  261. audio}.
  262. \paragraph{Mode decode}
  263. Vorbis allows an encoder to set up multiple, numbered packet 'modes',
  264. as described earlier, all of which may be used in a given Vorbis
  265. stream. The mode is encoded as an integer used as a direct offset into
  266. the mode instance index.
  267. \paragraph{Window shape decode (long windows only)} \label{vorbis:spec:window}
  268. Vorbis frames may be one of two PCM sample sizes specified during
  269. codec setup. In Vorbis I, legal frame sizes are powers of two from 64
  270. to 8192 samples. Aside from coupling, Vorbis handles channels as
  271. independent vectors and these frame sizes are in samples per channel.
  272. Vorbis uses an overlapping transform, namely the MDCT, to blend one
  273. frame into the next, avoiding most inter-frame block boundary
  274. artifacts. The MDCT output of one frame is windowed according to MDCT
  275. requirements, overlapped 50\% with the output of the previous frame and
  276. added. The window shape assures seamless reconstruction.
  277. This is easy to visualize in the case of equal sized-windows:
  278. \begin{center}
  279. \includegraphics[width=\textwidth]{window1}
  280. \captionof{figure}{overlap of two equal-sized windows}
  281. \end{center}
  282. And slightly more complex in the case of overlapping unequal sized
  283. windows:
  284. \begin{center}
  285. \includegraphics[width=\textwidth]{window2}
  286. \captionof{figure}{overlap of a long and a short window}
  287. \end{center}
  288. In the unequal-sized window case, the window shape of the long window
  289. must be modified for seamless lapping as above. It is possible to
  290. correctly infer window shape to be applied to the current window from
  291. knowing the sizes of the current, previous and next window. It is
  292. legal for a decoder to use this method. However, in the case of a long
  293. window (short windows require no modification), Vorbis also codes two
  294. flag bits to specify pre- and post- window shape. Although not
  295. strictly necessary for function, this minor redundancy allows a packet
  296. to be fully decoded to the point of lapping entirely independently of
  297. any other packet, allowing easier abstraction of decode layers as well
  298. as allowing a greater level of easy parallelism in encode and
  299. decode.
  300. A description of valid window functions for use with an inverse MDCT
  301. can be found in \cite{Sporer/Brandenburg/Edler}. Vorbis windows
  302. all use the slope function
  303. \[ y = \sin(.5*\pi \, \sin^2((x+.5)/n*\pi)) . \]
  304. \paragraph{floor decode}
  305. Each floor is encoded/decoded in channel order, however each floor
  306. belongs to a 'submap' that specifies which floor configuration to
  307. use. All floors are decoded before residue decode begins.
  308. \paragraph{residue decode}
  309. Although the number of residue vectors equals the number of channels,
  310. channel coupling may mean that the raw residue vectors extracted
  311. during decode do not map directly to specific channels. When channel
  312. coupling is in use, some vectors will correspond to coupled magnitude
  313. or angle. The coupling relationships are described in the codec setup
  314. and may differ from frame to frame, due to different mode numbers.
  315. Vorbis codes residue vectors in groups by submap; the coding is done
  316. in submap order from submap 0 through n-1. This differs from floors
  317. which are coded using a configuration provided by submap number, but
  318. are coded individually in channel order.
  319. \paragraph{inverse channel coupling}
  320. A detailed discussion of stereo in the Vorbis codec can be found in
  321. the document \href{stereo.html}{Stereo Channel Coupling in the
  322. Vorbis CODEC}. Vorbis is not limited to only stereo coupling, but
  323. the stereo document also gives a good overview of the generic coupling
  324. mechanism.
  325. Vorbis coupling applies to pairs of residue vectors at a time;
  326. decoupling is done in-place a pair at a time in the order and using
  327. the vectors specified in the current mapping configuration. The
  328. decoupling operation is the same for all pairs, converting square
  329. polar representation (where one vector is magnitude and the second
  330. angle) back to Cartesian representation.
  331. After decoupling, in order, each pair of vectors on the coupling list,
  332. the resulting residue vectors represent the fine spectral detail
  333. of each output channel.
  334. \paragraph{generate floor curve}
  335. The decoder may choose to generate the floor curve at any appropriate
  336. time. It is reasonable to generate the output curve when the floor
  337. data is decoded from the raw packet, or it can be generated after
  338. inverse coupling and applied to the spectral residue directly,
  339. combining generation and the dot product into one step and eliminating
  340. some working space.
  341. Both floor 0 and floor 1 generate a linear-range, linear-domain output
  342. vector to be multiplied (dot product) by the linear-range,
  343. linear-domain spectral residue.
  344. \paragraph{compute floor/residue dot product}
  345. This step is straightforward; for each output channel, the decoder
  346. multiplies the floor curve and residue vectors element by element,
  347. producing the finished audio spectrum of each channel.
  348. % TODO/FIXME: The following two paragraphs have identical twins
  349. % in section 4 (under "dot product")
  350. One point is worth mentioning about this dot product; a common mistake
  351. in a fixed point implementation might be to assume that a 32 bit
  352. fixed-point representation for floor and residue and direct
  353. multiplication of the vectors is sufficient for acceptable spectral
  354. depth in all cases because it happens to mostly work with the current
  355. Xiph.Org reference encoder.
  356. However, floor vector values can span \~{}140dB (\~{}24 bits unsigned), and
  357. the audio spectrum vector should represent a minimum of 120dB (\~{}21
  358. bits with sign), even when output is to a 16 bit PCM device. For the
  359. residue vector to represent full scale if the floor is nailed to
  360. $-140$dB, it must be able to span 0 to $+140$dB. For the residue vector
  361. to reach full scale if the floor is nailed at 0dB, it must be able to
  362. represent $-140$dB to $+0$dB. Thus, in order to handle full range
  363. dynamics, a residue vector may span $-140$dB to $+140$dB entirely within
  364. spec. A 280dB range is approximately 48 bits with sign; thus the
  365. residue vector must be able to represent a 48 bit range and the dot
  366. product must be able to handle an effective 48 bit times 24 bit
  367. multiplication. This range may be achieved using large (64 bit or
  368. larger) integers, or implementing a movable binary point
  369. representation.
  370. \paragraph{inverse monolithic transform (MDCT)}
  371. The audio spectrum is converted back into time domain PCM audio via an
  372. inverse Modified Discrete Cosine Transform (MDCT). A detailed
  373. description of the MDCT is available in \cite{Sporer/Brandenburg/Edler}.
  374. Note that the PCM produced directly from the MDCT is not yet finished
  375. audio; it must be lapped with surrounding frames using an appropriate
  376. window (such as the Vorbis window) before the MDCT can be considered
  377. orthogonal.
  378. \paragraph{overlap/add data}
  379. Windowed MDCT output is overlapped and added with the right hand data
  380. of the previous window such that the 3/4 point of the previous window
  381. is aligned with the 1/4 point of the current window (as illustrated in
  382. the window overlap diagram). At this point, the audio data between the
  383. center of the previous frame and the center of the current frame is
  384. now finished and ready to be returned.
  385. \paragraph{cache right hand data}
  386. The decoder must cache the right hand portion of the current frame to
  387. be lapped with the left hand portion of the next frame.
  388. \paragraph{return finished audio data}
  389. The overlapped portion produced from overlapping the previous and
  390. current frame data is finished data to be returned by the decoder.
  391. This data spans from the center of the previous window to the center
  392. of the current window. In the case of same-sized windows, the amount
  393. of data to return is one-half block consisting of and only of the
  394. overlapped portions. When overlapping a short and long window, much of
  395. the returned range is not actually overlap. This does not damage
  396. transform orthogonality. Pay attention however to returning the
  397. correct data range; the amount of data to be returned is:
  398. \begin{Verbatim}[commandchars=\\\{\}]
  399. window\_blocksize(previous\_window)/4+window\_blocksize(current\_window)/4
  400. \end{Verbatim}
  401. from the center of the previous window to the center of the current
  402. window.
  403. Data is not returned from the first frame; it must be used to 'prime'
  404. the decode engine. The encoder accounts for this priming when
  405. calculating PCM offsets; after the first frame, the proper PCM output
  406. offset is '0' (as no data has been returned yet).