04-codec.tex 27 KB

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