annotate src/libvorbis-1.3.3/doc/02-bitpacking.tex @ 36:55ece8862b6d

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