04-codec.tex 28 KB

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