03-codebook.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.
  201. Codebook entries marked 'unused' are simply skipped in the assigning
  202. process. They have no codeword and do not appear in the decision
  203. tree, thus it's impossible for any bit pattern read from the stream to
  204. decode to that entry number.
  205. \paragraph{Errata 20150226: Single entry codebooks}
  206. A 'single-entry codebook' is a codebook with one active codeword
  207. entry. A single-entry codebook may be either a fully populated
  208. codebook with only one declared entry, or a sparse codebook with only
  209. one entry marked used. The Vorbis I spec provides no means to specify
  210. a codeword length of zero, and as a result, a single-entry codebook is
  211. inherently malformed because it is underpopulated. The original
  212. specification did not address directly the matter of single-entry
  213. codebooks; they were implicitly illegal as it was not possible to
  214. write such a codebook with a valid tree structure.
  215. In r14811 of the libvorbis reference implementation, Xiph added an
  216. additional check to the codebook implementation to reject
  217. underpopulated Huffman trees. This change led to the discovery of
  218. single-entry books used 'in the wild' when the new, stricter checks
  219. rejected a number of apparently working streams.
  220. In order to minimize breakage of deployed (if technically erroneous)
  221. streams, r16073 of the reference implementation explicitly
  222. special-cased single-entry codebooks to tolerate the single-entry
  223. case. Commit r16073 also added the following to the specification:
  224. \blockquote{\sout{Take special care that a codebook with a single used
  225. entry is handled properly; it consists of a single codework of
  226. zero bits and ’reading’ a value out of such a codebook always
  227. returns the single used value and sinks zero bits.
  228. }}
  229. The intent was to clarify the spec and codify current practice.
  230. However, this addition is erroneously at odds with the intent of preserving
  231. usability of existing streams using single-entry codebooks, disagrees
  232. with the code changes that reinstated decoding, and does not address how
  233. single-entry codebooks should be encoded.
  234. As such, the above addition made in r16037 is struck from the
  235. specification and replaced by the following:
  236. \blockquote{It is possible to declare a Vorbis codebook containing a
  237. single codework entry. A single-entry codebook may be either a
  238. fully populated codebook with \varname{[codebook\_entries]} set to
  239. 1, or a sparse codebook marking only one entry used. Note that it
  240. is not possible to also encode a \varname{[codeword\_length]} of
  241. zero for the single used codeword, as the unsigned value written to
  242. the stream is \varname{[codeword\_length]-1}. Instead, encoder
  243. implementations should indicate a \varname{[codeword\_length]} of 1
  244. and 'write' the codeword to a stream during audio encoding by
  245. writing a single zero bit.
  246. Decoder implementations shall reject a codebook if it contains only
  247. one used entry and the encoded \varname{[codeword\_length]} of that
  248. entry is not 1. 'Reading' a value from single-entry codebook always
  249. returns the single used codeword value and sinks one bit. Decoders
  250. should tolerate that the bit read from the stream be '1' instead of
  251. '0'; both values shall return the single used codeword.}
  252. \paragraph{VQ lookup table vector representation}
  253. Unpacking the VQ lookup table vectors relies on the following values:
  254. \begin{programlisting}
  255. the [codebook\_multiplicands] array
  256. [codebook\_minimum\_value]
  257. [codebook\_delta\_value]
  258. [codebook\_sequence\_p]
  259. [codebook\_lookup\_type]
  260. [codebook\_entries]
  261. [codebook\_dimensions]
  262. [codebook\_lookup\_values]
  263. \end{programlisting}
  264. \bigskip
  265. Decoding (unpacking) a specific vector in the vector lookup table
  266. proceeds according to \varname{[codebook\_lookup\_type]}. The unpacked
  267. vector values are what a codebook would return during audio packet
  268. decode in a VQ context.
  269. \paragraph{Vector value decode: Lookup type 1}
  270. Lookup type one specifies a lattice VQ lookup table built
  271. algorithmically from a list of scalar values. Calculate (unpack) the
  272. final values of a codebook entry vector from the entries in
  273. \varname{[codebook\_multiplicands]} as follows (\varname{[value\_vector]}
  274. is the output vector representing the vector of values for entry number
  275. \varname{[lookup\_offset]} in this codebook):
  276. \begin{Verbatim}[commandchars=\\\{\}]
  277. 1) [last] = 0;
  278. 2) [index\_divisor] = 1;
  279. 3) iterate [i] over the range 0 ... [codebook\_dimensions]-1 (once for each scalar value in the value vector) \{
  280. 4) [multiplicand\_offset] = ( [lookup\_offset] divided by [index\_divisor] using integer
  281. division ) integer modulo [codebook\_lookup\_values]
  282. 5) vector [value\_vector] element [i] =
  283. ( [codebook\_multiplicands] array element number [multiplicand\_offset] ) *
  284. [codebook\_delta\_value] + [codebook\_minimum\_value] + [last];
  285. 6) if ( [codebook\_sequence\_p] is set ) then set [last] = vector [value\_vector] element [i]
  286. 7) [index\_divisor] = [index\_divisor] * [codebook\_lookup\_values]
  287. \}
  288. 8) vector calculation completed.
  289. \end{Verbatim}
  290. \paragraph{Vector value decode: Lookup type 2}
  291. Lookup type two specifies a VQ lookup table in which each scalar in
  292. each vector is explicitly set by the \varname{[codebook\_multiplicands]}
  293. array in a one-to-one mapping. Calculate [unpack] the
  294. final values of a codebook entry vector from the entries in
  295. \varname{[codebook\_multiplicands]} as follows (\varname{[value\_vector]}
  296. is the output vector representing the vector of values for entry number
  297. \varname{[lookup\_offset]} in this codebook):
  298. \begin{Verbatim}[commandchars=\\\{\}]
  299. 1) [last] = 0;
  300. 2) [multiplicand\_offset] = [lookup\_offset] * [codebook\_dimensions]
  301. 3) iterate [i] over the range 0 ... [codebook\_dimensions]-1 (once for each scalar value in the value vector) \{
  302. 4) vector [value\_vector] element [i] =
  303. ( [codebook\_multiplicands] array element number [multiplicand\_offset] ) *
  304. [codebook\_delta\_value] + [codebook\_minimum\_value] + [last];
  305. 5) if ( [codebook\_sequence\_p] is set ) then set [last] = vector [value\_vector] element [i]
  306. 6) increment [multiplicand\_offset]
  307. \}
  308. 7) vector calculation completed.
  309. \end{Verbatim}
  310. \subsection{Use of the codebook abstraction}
  311. The decoder uses the codebook abstraction much as it does the
  312. bit-unpacking convention; a specific codebook reads a
  313. codeword from the bitstream, decoding it into an entry number, and then
  314. returns that entry number to the decoder (when used in a scalar
  315. entropy coding context), or uses that entry number as an offset into
  316. the VQ lookup table, returning a vector of values (when used in a context
  317. desiring a VQ value). Scalar or VQ context is always explicit; any call
  318. to the codebook mechanism requests either a scalar entry number or a
  319. lookup vector.
  320. Note that VQ lookup type zero indicates that there is no lookup table;
  321. requesting decode using a codebook of lookup type 0 in any context
  322. expecting a vector return value (even in a case where a vector of
  323. dimension one) is forbidden. If decoder setup or decode requests such
  324. an action, that is an error condition rendering the packet
  325. undecodable.
  326. Using a codebook to read from the packet bitstream consists first of
  327. reading and decoding the next codeword in the bitstream. The decoder
  328. reads bits until the accumulated bits match a codeword in the
  329. codebook. This process can be though of as logically walking the
  330. Huffman decode tree by reading one bit at a time from the bitstream,
  331. and using the bit as a decision boolean to take the 0 branch (left in
  332. the above examples) or the 1 branch (right in the above examples).
  333. Walking the tree finishes when the decode process hits a leaf in the
  334. decision tree; the result is the entry number corresponding to that
  335. leaf. Reading past the end of a packet propagates the 'end-of-stream'
  336. condition to the decoder.
  337. When used in a scalar context, the resulting codeword entry is the
  338. desired return value.
  339. When used in a VQ context, the codeword entry number is used as an
  340. offset into the VQ lookup table. The value returned to the decoder is
  341. the vector of scalars corresponding to this offset.