03-codebook.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
  2. %!TEX root = Vorbis_I_spec.tex
  3. % $Id$
  4. \section{Probability Model and Codebooks} \label{vorbis:spec:codebook}
  5. \subsection{Overview}
  6. Unlike practically every other mainstream audio codec, Vorbis has no
  7. statically configured probability model, instead packing all entropy
  8. decoding configuration, VQ and Huffman, into the bitstream itself in
  9. the third header, the codec setup header. This packed configuration
  10. consists of multiple 'codebooks', each containing a specific
  11. Huffman-equivalent representation for decoding compressed codewords as
  12. well as an optional lookup table of output vector values to which a
  13. decoded Huffman value is applied as an offset, generating the final
  14. decoded output corresponding to a given compressed codeword.
  15. \subsubsection{Bitwise operation}
  16. The codebook mechanism is built on top of the vorbis bitpacker. Both
  17. the codebooks themselves and the codewords they decode are unrolled
  18. from a packet as a series of arbitrary-width values read from the
  19. stream according to \xref{vorbis:spec:bitpacking}.
  20. \subsection{Packed codebook format}
  21. For purposes of the examples below, we assume that the storage
  22. system's native byte width is eight bits. This is not universally
  23. true; see \xref{vorbis:spec:bitpacking} for discussion
  24. relating to non-eight-bit bytes.
  25. \subsubsection{codebook decode}
  26. A codebook begins with a 24 bit sync pattern, 0x564342:
  27. \begin{Verbatim}[commandchars=\\\{\}]
  28. byte 0: [ 0 1 0 0 0 0 1 0 ] (0x42)
  29. byte 1: [ 0 1 0 0 0 0 1 1 ] (0x43)
  30. byte 2: [ 0 1 0 1 0 1 1 0 ] (0x56)
  31. \end{Verbatim}
  32. 16 bit \varname{[codebook_dimensions]} and 24 bit \varname{[codebook_entries]} fields:
  33. \begin{Verbatim}[commandchars=\\\{\}]
  34. byte 3: [ X X X X X X X X ]
  35. byte 4: [ X X X X X X X X ] [codebook_dimensions] (16 bit unsigned)
  36. byte 5: [ X X X X X X X X ]
  37. byte 6: [ X X X X X X X X ]
  38. byte 7: [ X X X X X X X X ] [codebook_entries] (24 bit unsigned)
  39. \end{Verbatim}
  40. Next is the \varname{[ordered]} bit flag:
  41. \begin{Verbatim}[commandchars=\\\{\}]
  42. byte 8: [ X ] [ordered] (1 bit)
  43. \end{Verbatim}
  44. Each entry, numbering a
  45. total of \varname{[codebook_entries]}, is assigned a codeword length.
  46. We now read the list of codeword lengths and store these lengths in
  47. the array \varname{[codebook_codeword_lengths]}. Decode of lengths is
  48. according to whether the \varname{[ordered]} flag is set or unset.
  49. \begin{itemize}
  50. \item
  51. If the \varname{[ordered]} flag is unset, the codeword list is not
  52. length ordered and the decoder needs to read each codeword length
  53. one-by-one.
  54. The decoder first reads one additional bit flag, the
  55. \varname{[sparse]} flag. This flag determines whether or not the
  56. codebook contains unused entries that are not to be included in the
  57. codeword decode tree:
  58. \begin{Verbatim}[commandchars=\\\{\}]
  59. byte 8: [ X 1 ] [sparse] flag (1 bit)
  60. \end{Verbatim}
  61. The decoder now performs for each of the \varname{[codebook_entries]}
  62. codebook entries:
  63. \begin{Verbatim}[commandchars=\\\{\}]
  64. 1) if([sparse] is set) \{
  65. 2) [flag] = read one bit;
  66. 3) if([flag] is set) \{
  67. 4) [length] = read a five bit unsigned integer;
  68. 5) codeword length for this entry is [length]+1;
  69. \} else \{
  70. 6) this entry is unused. mark it as such.
  71. \}
  72. \} else the sparse flag is not set \{
  73. 7) [length] = read a five bit unsigned integer;
  74. 8) the codeword length for this entry is [length]+1;
  75. \}
  76. \end{Verbatim}
  77. \item
  78. If the \varname{[ordered]} flag is set, the codeword list for this
  79. codebook is encoded in ascending length order. Rather than reading
  80. a length for every codeword, the encoder reads the number of
  81. codewords per length. That is, beginning at entry zero:
  82. \begin{Verbatim}[commandchars=\\\{\}]
  83. 1) [current_entry] = 0;
  84. 2) [current_length] = read a five bit unsigned integer and add 1;
  85. 3) [number] = read \link{vorbis:spec:ilog}{ilog}([codebook_entries] - [current_entry]) bits as an unsigned integer
  86. 4) set the entries [current_entry] through [current_entry]+[number]-1, inclusive,
  87. of the [codebook_codeword_lengths] array to [current_length]
  88. 5) set [current_entry] to [number] + [current_entry]
  89. 6) increment [current_length] by 1
  90. 7) if [current_entry] is greater than [codebook_entries] ERROR CONDITION;
  91. the decoder will not be able to read this stream.
  92. 8) if [current_entry] is less than [codebook_entries], repeat process starting at 3)
  93. 9) done.
  94. \end{Verbatim}
  95. \end{itemize}
  96. After all codeword lengths have been decoded, the decoder reads the
  97. vector lookup table. Vorbis I supports three lookup types:
  98. \begin{enumerate}
  99. \item
  100. No lookup
  101. \item
  102. Implicitly populated value mapping (lattice VQ)
  103. \item
  104. Explicitly populated value mapping (tessellated or 'foam'
  105. VQ)
  106. \end{enumerate}
  107. The lookup table type is read as a four bit unsigned integer:
  108. \begin{Verbatim}[commandchars=\\\{\}]
  109. 1) [codebook_lookup_type] = read four bits as an unsigned integer
  110. \end{Verbatim}
  111. Codebook decode precedes according to \varname{[codebook_lookup_type]}:
  112. \begin{itemize}
  113. \item
  114. Lookup type zero indicates no lookup to be read. Proceed past
  115. lookup decode.
  116. \item
  117. Lookup types one and two are similar, differing only in the
  118. number of lookup values to be read. Lookup type one reads a list of
  119. values that are permuted in a set pattern to build a list of vectors,
  120. each vector of order \varname{[codebook_dimensions]} scalars. Lookup
  121. type two builds the same vector list, but reads each scalar for each
  122. vector explicitly, rather than building vectors from a smaller list of
  123. possible scalar values. Lookup decode proceeds as follows:
  124. \begin{Verbatim}[commandchars=\\\{\}]
  125. 1) [codebook_minimum_value] = \link{vorbis:spec:float32:unpack}{float32_unpack}( read 32 bits as an unsigned integer)
  126. 2) [codebook_delta_value] = \link{vorbis:spec:float32:unpack}{float32_unpack}( read 32 bits as an unsigned integer)
  127. 3) [codebook_value_bits] = read 4 bits as an unsigned integer and add 1
  128. 4) [codebook_sequence_p] = read 1 bit as a boolean flag
  129. if ( [codebook_lookup_type] is 1 ) \{
  130. 5) [codebook_lookup_values] = \link{vorbis:spec:lookup1:values}{lookup1_values}(\varname{[codebook_entries]}, \varname{[codebook_dimensions]} )
  131. \} else \{
  132. 6) [codebook_lookup_values] = \varname{[codebook_entries]} * \varname{[codebook_dimensions]}
  133. \}
  134. 7) read a total of [codebook_lookup_values] unsigned integers of [codebook_value_bits] each;
  135. store these in order in the array [codebook_multiplicands]
  136. \end{Verbatim}
  137. \item
  138. A \varname{[codebook_lookup_type]} of greater than two is reserved
  139. and indicates a stream that is not decodable by the specification in this
  140. document.
  141. \end{itemize}
  142. An 'end of packet' during any read operation in the above steps is
  143. considered an error condition rendering the stream undecodable.
  144. \paragraph{Huffman decision tree representation}
  145. The \varname{[codebook_codeword_lengths]} array and
  146. \varname{[codebook_entries]} value uniquely define the Huffman decision
  147. tree used for entropy decoding.
  148. Briefly, each used codebook entry (recall that length-unordered
  149. codebooks support unused codeword entries) is assigned, in order, the
  150. lowest valued unused binary Huffman codeword possible. Assume the
  151. following codeword length list:
  152. \begin{Verbatim}[commandchars=\\\{\}]
  153. entry 0: length 2
  154. entry 1: length 4
  155. entry 2: length 4
  156. entry 3: length 4
  157. entry 4: length 4
  158. entry 5: length 2
  159. entry 6: length 3
  160. entry 7: length 3
  161. \end{Verbatim}
  162. Assigning codewords in order (lowest possible value of the appropriate
  163. length to highest) results in the following codeword list:
  164. \begin{Verbatim}[commandchars=\\\{\}]
  165. entry 0: length 2 codeword 00
  166. entry 1: length 4 codeword 0100
  167. entry 2: length 4 codeword 0101
  168. entry 3: length 4 codeword 0110
  169. entry 4: length 4 codeword 0111
  170. entry 5: length 2 codeword 10
  171. entry 6: length 3 codeword 110
  172. entry 7: length 3 codeword 111
  173. \end{Verbatim}
  174. \begin{note}
  175. Unlike most binary numerical values in this document, we
  176. intend the above codewords to be read and used bit by bit from left to
  177. right, thus the codeword '001' is the bit string 'zero, zero, one'.
  178. When determining 'lowest possible value' in the assignment definition
  179. above, the leftmost bit is the MSb.
  180. \end{note}
  181. It is clear that the codeword length list represents a Huffman
  182. decision tree with the entry numbers equivalent to the leaves numbered
  183. left-to-right:
  184. \begin{center}
  185. \includegraphics[width=10cm]{hufftree}
  186. \captionof{figure}{huffman tree illustration}
  187. \end{center}
  188. As we assign codewords in order, we see that each choice constructs a
  189. new leaf in the leftmost possible position.
  190. Note that it's possible to underspecify or overspecify a Huffman tree
  191. via the length list. In the above example, if codeword seven were
  192. eliminated, it's clear that the tree is unfinished:
  193. \begin{center}
  194. \includegraphics[width=10cm]{hufftree-under}
  195. \captionof{figure}{underspecified huffman tree illustration}
  196. \end{center}
  197. Similarly, in the original codebook, it's clear that the tree is fully
  198. populated and a ninth codeword is impossible. Both underspecified and
  199. overspecified trees are an error condition rendering the stream
  200. undecodable. Take special care that a codebook with a single used
  201. entry is handled properly; it consists of a single codework of zero
  202. bits and 'reading' a value out of such a codebook always returns the
  203. single used value and sinks zero bits.
  204. Codebook entries marked 'unused' are simply skipped in the assigning
  205. process. They have no codeword and do not appear in the decision
  206. tree, thus it's impossible for any bit pattern read from the stream to
  207. decode to that entry number.
  208. \paragraph{VQ lookup table vector representation}
  209. Unpacking the VQ lookup table vectors relies on the following values:
  210. \begin{programlisting}
  211. the [codebook_multiplicands] array
  212. [codebook_minimum_value]
  213. [codebook_delta_value]
  214. [codebook_sequence_p]
  215. [codebook_lookup_type]
  216. [codebook_entries]
  217. [codebook_dimensions]
  218. [codebook_lookup_values]
  219. \end{programlisting}
  220. \bigskip
  221. Decoding (unpacking) a specific vector in the vector lookup table
  222. proceeds according to \varname{[codebook_lookup_type]}. The unpacked
  223. vector values are what a codebook would return during audio packet
  224. decode in a VQ context.
  225. \paragraph{Vector value decode: Lookup type 1}
  226. Lookup type one specifies a lattice VQ lookup table built
  227. algorithmically from a list of scalar values. Calculate (unpack) the
  228. final values of a codebook entry vector from the entries in
  229. \varname{[codebook_multiplicands]} as follows (\varname{[value_vector]}
  230. is the output vector representing the vector of values for entry number
  231. \varname{[lookup_offset]} in this codebook):
  232. \begin{Verbatim}[commandchars=\\\{\}]
  233. 1) [last] = 0;
  234. 2) [index_divisor] = 1;
  235. 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) \{
  236. 4) [multiplicand_offset] = ( [lookup_offset] divided by [index_divisor] using integer
  237. division ) integer modulo [codebook_lookup_values]
  238. 5) vector [value_vector] element [i] =
  239. ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
  240. [codebook_delta_value] + [codebook_minimum_value] + [last];
  241. 6) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
  242. 7) [index_divisor] = [index_divisor] * [codebook_lookup_values]
  243. \}
  244. 8) vector calculation completed.
  245. \end{Verbatim}
  246. \paragraph{Vector value decode: Lookup type 2}
  247. Lookup type two specifies a VQ lookup table in which each scalar in
  248. each vector is explicitly set by the \varname{[codebook_multiplicands]}
  249. array in a one-to-one mapping. Calculate [unpack] the
  250. final values of a codebook entry vector from the entries in
  251. \varname{[codebook_multiplicands]} as follows (\varname{[value_vector]}
  252. is the output vector representing the vector of values for entry number
  253. \varname{[lookup_offset]} in this codebook):
  254. \begin{Verbatim}[commandchars=\\\{\}]
  255. 1) [last] = 0;
  256. 2) [multiplicand_offset] = [lookup_offset] * [codebook_dimensions]
  257. 3) iterate [i] over the range 0 ... [codebook_dimensions]-1 (once for each scalar value in the value vector) \{
  258. 4) vector [value_vector] element [i] =
  259. ( [codebook_multiplicands] array element number [multiplicand_offset] ) *
  260. [codebook_delta_value] + [codebook_minimum_value] + [last];
  261. 5) if ( [codebook_sequence_p] is set ) then set [last] = vector [value_vector] element [i]
  262. 6) increment [multiplicand_offset]
  263. \}
  264. 7) vector calculation completed.
  265. \end{Verbatim}
  266. \subsection{Use of the codebook abstraction}
  267. The decoder uses the codebook abstraction much as it does the
  268. bit-unpacking convention; a specific codebook reads a
  269. codeword from the bitstream, decoding it into an entry number, and then
  270. returns that entry number to the decoder (when used in a scalar
  271. entropy coding context), or uses that entry number as an offset into
  272. the VQ lookup table, returning a vector of values (when used in a context
  273. desiring a VQ value). Scalar or VQ context is always explicit; any call
  274. to the codebook mechanism requests either a scalar entry number or a
  275. lookup vector.
  276. Note that VQ lookup type zero indicates that there is no lookup table;
  277. requesting decode using a codebook of lookup type 0 in any context
  278. expecting a vector return value (even in a case where a vector of
  279. dimension one) is forbidden. If decoder setup or decode requests such
  280. an action, that is an error condition rendering the packet
  281. undecodable.
  282. Using a codebook to read from the packet bitstream consists first of
  283. reading and decoding the next codeword in the bitstream. The decoder
  284. reads bits until the accumulated bits match a codeword in the
  285. codebook. This process can be though of as logically walking the
  286. Huffman decode tree by reading one bit at a time from the bitstream,
  287. and using the bit as a decision boolean to take the 0 branch (left in
  288. the above examples) or the 1 branch (right in the above examples).
  289. Walking the tree finishes when the decode process hits a leaf in the
  290. decision tree; the result is the entry number corresponding to that
  291. leaf. Reading past the end of a packet propagates the 'end-of-stream'
  292. condition to the decoder.
  293. When used in a scalar context, the resulting codeword entry is the
  294. desired return value.
  295. When used in a VQ context, the codeword entry number is used as an
  296. offset into the VQ lookup table. The value returned to the decoder is
  297. the vector of scalars corresponding to this offset.