03-codebook.xml 15 KB

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