02-bitpacking.tex 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. % -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
  2. %!TEX root = Vorbis_I_spec.tex
  3. % $Id$
  4. \section{Bitpacking Convention} \label{vorbis:spec:bitpacking}
  5. \subsection{Overview}
  6. The Vorbis codec uses relatively unstructured raw packets containing
  7. arbitrary-width binary integer fields. Logically, these packets are a
  8. bitstream in which bits are coded one-by-one by the encoder and then
  9. read one-by-one in the same monotonically increasing order by the
  10. decoder. Most current binary storage arrangements group bits into a
  11. native word size of eight bits (octets), sixteen bits, thirty-two bits
  12. or, less commonly other fixed word sizes. The Vorbis bitpacking
  13. convention specifies the correct mapping of the logical packet
  14. bitstream into an actual representation in fixed-width words.
  15. \subsubsection{octets, bytes and words}
  16. In most contemporary architectures, a 'byte' is synonymous with an
  17. 'octet', that is, eight bits. This has not always been the case;
  18. seven, ten, eleven and sixteen bit 'bytes' have been used. For
  19. purposes of the bitpacking convention, a byte implies the native,
  20. smallest integer storage representation offered by a platform. On
  21. modern platforms, this is generally assumed to be eight bits (not
  22. necessarily because of the processor but because of the
  23. filesystem/memory architecture. Modern filesystems invariably offer
  24. bytes as the fundamental atom of storage). A 'word' is an integer
  25. size that is a grouped multiple of this smallest size.
  26. The most ubiquitous architectures today consider a 'byte' to be an
  27. octet (eight bits) and a word to be a group of two, four or eight
  28. bytes (16, 32 or 64 bits). Note however that the Vorbis bitpacking
  29. convention is still well defined for any native byte size; Vorbis uses
  30. the native bit-width of a given storage system. This document assumes
  31. that a byte is one octet for purposes of example.
  32. \subsubsection{bit order}
  33. A byte has a well-defined 'least significant' bit (LSb), which is the
  34. only bit set when the byte is storing the two's complement integer
  35. value +1. A byte's 'most significant' bit (MSb) is at the opposite
  36. end of the byte. Bits in a byte are numbered from zero at the LSb to
  37. $n$ ($n=7$ in an octet) for the
  38. MSb.
  39. \subsubsection{byte order}
  40. Words are native groupings of multiple bytes. Several byte orderings
  41. are possible in a word; the common ones are 3-2-1-0 ('big endian' or
  42. 'most significant byte first' in which the highest-valued byte comes
  43. first), 0-1-2-3 ('little endian' or 'least significant byte first' in
  44. which the lowest value byte comes first) and less commonly 3-1-2-0 and
  45. 0-2-1-3 ('mixed endian').
  46. The Vorbis bitpacking convention specifies storage and bitstream
  47. manipulation at the byte, not word, level, thus host word ordering is
  48. of a concern only during optimization when writing high performance
  49. code that operates on a word of storage at a time rather than by byte.
  50. Logically, bytes are always coded and decoded in order from byte zero
  51. through byte $n$.
  52. \subsubsection{coding bits into byte sequences}
  53. The Vorbis codec has need to code arbitrary bit-width integers, from
  54. zero to 32 bits wide, into packets. These integer fields are not
  55. aligned to the boundaries of the byte representation; the next field
  56. is written at the bit position at which the previous field ends.
  57. The encoder logically packs integers by writing the LSb of a binary
  58. integer to the logical bitstream first, followed by next least
  59. significant bit, etc, until the requested number of bits have been
  60. coded. When packing the bits into bytes, the encoder begins by
  61. placing the LSb of the integer to be written into the least
  62. significant unused bit position of the destination byte, followed by
  63. the next-least significant bit of the source integer and so on up to
  64. the requested number of bits. When all bits of the destination byte
  65. have been filled, encoding continues by zeroing all bits of the next
  66. byte and writing the next bit into the bit position 0 of that byte.
  67. Decoding follows the same process as encoding, but by reading bits
  68. from the byte stream and reassembling them into integers.
  69. \subsubsection{signedness}
  70. The signedness of a specific number resulting from decode is to be
  71. interpreted by the decoder given decode context. That is, the three
  72. bit binary pattern 'b111' can be taken to represent either 'seven' as
  73. an unsigned integer, or '-1' as a signed, two's complement integer.
  74. The encoder and decoder are responsible for knowing if fields are to
  75. be treated as signed or unsigned.
  76. \subsubsection{coding example}
  77. Code the 4 bit integer value '12' [b1100] into an empty bytestream.
  78. Bytestream result:
  79. \begin{Verbatim}[commandchars=\\\{\}]
  80. |
  81. V
  82. 7 6 5 4 3 2 1 0
  83. byte 0 [0 0 0 0 1 1 0 0] <-
  84. byte 1 [ ]
  85. byte 2 [ ]
  86. byte 3 [ ]
  87. ...
  88. byte n [ ] bytestream length == 1 byte
  89. \end{Verbatim}
  90. Continue by coding the 3 bit integer value '-1' [b111]:
  91. \begin{Verbatim}[commandchars=\\\{\}]
  92. |
  93. V
  94. 7 6 5 4 3 2 1 0
  95. byte 0 [0 1 1 1 1 1 0 0] <-
  96. byte 1 [ ]
  97. byte 2 [ ]
  98. byte 3 [ ]
  99. ...
  100. byte n [ ] bytestream length == 1 byte
  101. \end{Verbatim}
  102. Continue by coding the 7 bit integer value '17' [b0010001]:
  103. \begin{Verbatim}[commandchars=\\\{\}]
  104. |
  105. V
  106. 7 6 5 4 3 2 1 0
  107. byte 0 [1 1 1 1 1 1 0 0]
  108. byte 1 [0 0 0 0 1 0 0 0] <-
  109. byte 2 [ ]
  110. byte 3 [ ]
  111. ...
  112. byte n [ ] bytestream length == 2 bytes
  113. bit cursor == 6
  114. \end{Verbatim}
  115. Continue by coding the 13 bit integer value '6969' [b110 11001110 01]:
  116. \begin{Verbatim}[commandchars=\\\{\}]
  117. |
  118. V
  119. 7 6 5 4 3 2 1 0
  120. byte 0 [1 1 1 1 1 1 0 0]
  121. byte 1 [0 1 0 0 1 0 0 0]
  122. byte 2 [1 1 0 0 1 1 1 0]
  123. byte 3 [0 0 0 0 0 1 1 0] <-
  124. ...
  125. byte n [ ] bytestream length == 4 bytes
  126. \end{Verbatim}
  127. \subsubsection{decoding example}
  128. Reading from the beginning of the bytestream encoded in the above example:
  129. \begin{Verbatim}[commandchars=\\\{\}]
  130. |
  131. V
  132. 7 6 5 4 3 2 1 0
  133. byte 0 [1 1 1 1 1 1 0 0] <-
  134. byte 1 [0 1 0 0 1 0 0 0]
  135. byte 2 [1 1 0 0 1 1 1 0]
  136. byte 3 [0 0 0 0 0 1 1 0] bytestream length == 4 bytes
  137. \end{Verbatim}
  138. We read two, two-bit integer fields, resulting in the returned numbers
  139. 'b00' and 'b11'. Two things are worth noting here:
  140. \begin{itemize}
  141. \item Although these four bits were originally written as a single
  142. four-bit integer, reading some other combination of bit-widths from the
  143. bitstream is well defined. There are no artificial alignment
  144. boundaries maintained in the bitstream.
  145. \item The second value is the
  146. two-bit-wide integer 'b11'. This value may be interpreted either as
  147. the unsigned value '3', or the signed value '-1'. Signedness is
  148. dependent on decode context.
  149. \end{itemize}
  150. \subsubsection{end-of-packet alignment}
  151. The typical use of bitpacking is to produce many independent
  152. byte-aligned packets which are embedded into a larger byte-aligned
  153. container structure, such as an Ogg transport bitstream. Externally,
  154. each bytestream (encoded bitstream) must begin and end on a byte
  155. boundary. Often, the encoded bitstream is not an integer number of
  156. bytes, and so there is unused (uncoded) space in the last byte of a
  157. packet.
  158. Unused space in the last byte of a bytestream is always zeroed during
  159. the coding process. Thus, should this unused space be read, it will
  160. return binary zeroes.
  161. Attempting to read past the end of an encoded packet results in an
  162. 'end-of-packet' condition. End-of-packet is not to be considered an
  163. error; it is merely a state indicating that there is insufficient
  164. remaining data to fulfill the desired read size. Vorbis uses truncated
  165. packets as a normal mode of operation, and as such, decoders must
  166. handle reading past the end of a packet as a typical mode of
  167. operation. Any further read operations after an 'end-of-packet'
  168. condition shall also return 'end-of-packet'.
  169. \subsubsection{reading zero bits}
  170. Reading a zero-bit-wide integer returns the value '0' and does not
  171. increment the stream cursor. Reading to the end of the packet (but
  172. not past, such that an 'end-of-packet' condition has not triggered)
  173. and then reading a zero bit integer shall succeed, returning 0, and
  174. not trigger an end-of-packet condition. Reading a zero-bit-wide
  175. integer after a previous read sets 'end-of-packet' shall also fail
  176. with 'end-of-packet'.