03-codebook.tex 17 KB

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