02-bitpacking.tex 8.3 KB

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