examples/sfexamples/oggvorbiscodec/src/libvorbis/doc/xml/02-bitpacking.xml

00001 <?xml version="1.0" standalone="no"?>
00002 <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
00003                 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
00004 
00005 ]>
00006 
00007 <section id="vorbis-spec-bitpacking">
00008 <sectioninfo>
00009 <releaseinfo>
00010  $Id: 02-bitpacking.xml 7186 2004-07-20 07:19:25Z xiphmont $
00011 </releaseinfo>
00012 </sectioninfo>
00013 <title>Bitpacking Convention</title>
00014 
00015 
00016 <section>
00017 <title>Overview</title>
00018 
00019 <para>
00020 The Vorbis codec uses relatively unstructured raw packets containing
00021 arbitrary-width binary integer fields.  Logically, these packets are a
00022 bitstream in which bits are coded one-by-one by the encoder and then
00023 read one-by-one in the same monotonically increasing order by the
00024 decoder.  Most current binary storage arrangements group bits into a
00025 native word size of eight bits (octets), sixteen bits, thirty-two bits
00026 or, less commonly other fixed word sizes.  The Vorbis bitpacking
00027 convention specifies the correct mapping of the logical packet
00028 bitstream into an actual representation in fixed-width words.
00029 </para>
00030 
00031 <section><title>octets, bytes and words</title>
00032 
00033 <para>
00034 In most contemporary architectures, a 'byte' is synonymous with an
00035 'octet', that is, eight bits.  This has not always been the case;
00036 seven, ten, eleven and sixteen bit 'bytes' have been used.  For
00037 purposes of the bitpacking convention, a byte implies the native,
00038 smallest integer storage representation offered by a platform.  On
00039 modern platforms, this is generally assumed to be eight bits (not
00040 necessarily because of the processor but because of the
00041 filesystem/memory architecture.  Modern filesystems invariably offer
00042 bytes as the fundamental atom of storage).  A 'word' is an integer
00043 size that is a grouped multiple of this smallest size.</para>
00044 
00045 <para>
00046 The most ubiquitous architectures today consider a 'byte' to be an
00047 octet (eight bits) and a word to be a group of two, four or eight
00048 bytes (16, 32 or 64 bits).  Note however that the Vorbis bitpacking
00049 convention is still well defined for any native byte size; Vorbis uses
00050 the native bit-width of a given storage system. This document assumes
00051 that a byte is one octet for purposes of example.</para>
00052 
00053 </section><section><title>bit order</title>
00054 
00055 <para>
00056 A byte has a well-defined 'least significant' bit (LSb), which is the
00057 only bit set when the byte is storing the two's complement integer
00058 value +1.  A byte's 'most significant' bit (MSb) is at the opposite
00059 end of the byte. Bits in a byte are numbered from zero at the LSb to
00060 <emphasis>n</emphasis> (<emphasis>n</emphasis>=7 in an octet) for the
00061 MSb.</para>
00062 
00063 </section>
00064 
00065 <section><title>byte order</title>
00066 
00067 <para>
00068 Words are native groupings of multiple bytes.  Several byte orderings
00069 are possible in a word; the common ones are 3-2-1-0 ('big endian' or
00070 'most significant byte first' in which the highest-valued byte comes
00071 first), 0-1-2-3 ('little endian' or 'least significant byte first' in
00072 which the lowest value byte comes first) and less commonly 3-1-2-0 and
00073 0-2-1-3 ('mixed endian').</para>
00074 
00075 <para>
00076 The Vorbis bitpacking convention specifies storage and bitstream
00077 manipulation at the byte, not word, level, thus host word ordering is
00078 of a concern only during optimization when writing high performance
00079 code that operates on a word of storage at a time rather than by byte.
00080 Logically, bytes are always coded and decoded in order from byte zero
00081 through byte <emphasis>n</emphasis>.</para>
00082 
00083 </section>
00084 
00085 <section><title>coding bits into byte sequences</title>
00086 
00087 <para>
00088 The Vorbis codec has need to code arbitrary bit-width integers, from
00089 zero to 32 bits wide, into packets.  These integer fields are not
00090 aligned to the boundaries of the byte representation; the next field
00091 is written at the bit position at which the previous field ends.</para>
00092 
00093 <para>
00094 The encoder logically packs integers by writing the LSb of a binary
00095 integer to the logical bitstream first, followed by next least
00096 significant bit, etc, until the requested number of bits have been
00097 coded.  When packing the bits into bytes, the encoder begins by
00098 placing the LSb of the integer to be written into the least
00099 significant unused bit position of the destination byte, followed by
00100 the next-least significant bit of the source integer and so on up to
00101 the requested number of bits.  When all bits of the destination byte
00102 have been filled, encoding continues by zeroing all bits of the next
00103 byte and writing the next bit into the bit position 0 of that byte.
00104 Decoding follows the same process as encoding, but by reading bits
00105 from the byte stream and reassembling them into integers.</para>
00106 
00107 </section>
00108 
00109 <section><title>signedness</title>
00110 
00111 <para>
00112 The signedness of a specific number resulting from decode is to be
00113 interpreted by the decoder given decode context.  That is, the three
00114 bit binary pattern 'b111' can be taken to represent either 'seven' as
00115 an unsigned integer, or '-1' as a signed, two's complement integer.
00116 The encoder and decoder are responsible for knowing if fields are to
00117 be treated as signed or unsigned.</para>
00118 
00119 </section>
00120 
00121 <section><title>coding example</title>
00122 
00123 <para>
00124 Code the 4 bit integer value '12' [b1100] into an empty bytestream.
00125 Bytestream result:
00126 
00127 <screen>  
00128               |
00129               V
00130 
00131         7 6 5 4 3 2 1 0
00132 byte 0 [0 0 0 0 1 1 0 0]  &lt;-
00133 byte 1 [               ]
00134 byte 2 [               ]
00135 byte 3 [               ]
00136              ...
00137 byte n [               ]  bytestream length == 1 byte
00138 
00139 </screen>
00140 </para>
00141 
00142 <para>
00143 Continue by coding the 3 bit integer value '-1' [b111]:
00144 
00145 <screen>
00146         |
00147         V
00148 
00149         7 6 5 4 3 2 1 0
00150 byte 0 [0 1 1 1 1 1 0 0]  &lt;-
00151 byte 1 [               ]
00152 byte 2 [               ]
00153 byte 3 [               ]
00154              ... 
00155 byte n [               ]  bytestream length == 1 byte
00156 </screen>
00157 </para>
00158 
00159 <para>
00160 Continue by coding the 7 bit integer value '17' [b0010001]:
00161 
00162 <screen>
00163           |
00164           V    
00165 
00166         7 6 5 4 3 2 1 0
00167 byte 0 [1 1 1 1 1 1 0 0]
00168 byte 1 [0 0 0 0 1 0 0 0]  &lt;-
00169 byte 2 [               ]
00170 byte 3 [               ]
00171              ...
00172 byte n [               ]  bytestream length == 2 bytes
00173                           bit cursor == 6
00174 </screen>
00175 </para>
00176 
00177 <para>
00178 Continue by coding the 13 bit integer value '6969' [b110 11001110 01]:
00179 
00180 <screen>
00181                 |
00182                 V
00183 
00184         7 6 5 4 3 2 1 0
00185 byte 0 [1 1 1 1 1 1 0 0]
00186 byte 1 [0 1 0 0 1 0 0 0]
00187 byte 2 [1 1 0 0 1 1 1 0]
00188 byte 3 [0 0 0 0 0 1 1 0]  &lt;-
00189              ...
00190 byte n [               ]  bytestream length == 4 bytes
00191 
00192 </screen>
00193 </para>
00194 
00195 </section>
00196 
00197 <section><title>decoding example</title>
00198 
00199 <para>
00200 Reading from the beginning of the bytestream encoded in the above example:
00201 
00202 <screen>
00203                       |
00204                       V
00205                       
00206         7 6 5 4 3 2 1 0
00207 byte 0 [1 1 1 1 1 1 0 0]  &lt;-
00208 byte 1 [0 1 0 0 1 0 0 0]
00209 byte 2 [1 1 0 0 1 1 1 0]
00210 byte 3 [0 0 0 0 0 1 1 0]  bytestream length == 4 bytes
00211 
00212 </screen>
00213 </para>
00214 
00215 <para>
00216 We read two, two-bit integer fields, resulting in the returned numbers
00217 'b00' and 'b11'.  Two things are worth noting here:
00218 
00219 <itemizedlist>
00220 <listitem>
00221 <para>Although these four bits were originally written as a single
00222 four-bit integer, reading some other combination of bit-widths from the
00223 bitstream is well defined.  There are no artificial alignment
00224 boundaries maintained in the bitstream.</para>
00225 </listitem>
00226 <listitem>
00227 <para>The second value is the
00228 two-bit-wide integer 'b11'.  This value may be interpreted either as
00229 the unsigned value '3', or the signed value '-1'.  Signedness is
00230 dependent on decode context.</para>
00231 </listitem>
00232 </itemizedlist>
00233 </para>
00234 
00235 </section>
00236 
00237 <section><title>end-of-packet alignment</title>
00238 
00239 <para>
00240 The typical use of bitpacking is to produce many independent
00241 byte-aligned packets which are embedded into a larger byte-aligned
00242 container structure, such as an Ogg transport bitstream.  Externally,
00243 each bytestream (encoded bitstream) must begin and end on a byte
00244 boundary.  Often, the encoded bitstream is not an integer number of
00245 bytes, and so there is unused (uncoded) space in the last byte of a
00246 packet.</para>
00247 
00248 <para>
00249 Unused space in the last byte of a bytestream is always zeroed during
00250 the coding process.  Thus, should this unused space be read, it will
00251 return binary zeroes.</para>
00252 
00253 <para>
00254 Attempting to read past the end of an encoded packet results in an
00255 'end-of-packet' condition.  End-of-packet is not to be considered an
00256 error; it is merely a state indicating that there is insufficient
00257 remaining data to fulfill the desired read size.  Vorbis uses truncated
00258 packets as a normal mode of operation, and as such, decoders must
00259 handle reading past the end of a packet as a typical mode of
00260 operation. Any further read operations after an 'end-of-packet'
00261 condition shall also return 'end-of-packet'.</para>
00262 
00263 </section>
00264 
00265 <section><title> reading zero bits</title>
00266 
00267 <para>
00268 Reading a zero-bit-wide integer returns the value '0' and does not
00269 increment the stream cursor.  Reading to the end of the packet (but
00270 not past, such that an 'end-of-packet' condition has not triggered)
00271 and then reading a zero bit integer shall succeed, returning 0, and
00272 not trigger an end-of-packet condition.  Reading a zero-bit-wide
00273 integer after a previous read sets 'end-of-packet' shall also fail
00274 with 'end-of-packet'.</para>
00275 
00276 </section>
00277 
00278 </section>
00279 </section>
00280 

Generated by  doxygen 1.6.2