Blame doc/02-bitpacking.tex

Packit 06404a
% -*- mode: latex; TeX-master: "Vorbis_I_spec"; -*-
Packit 06404a
%!TEX root = Vorbis_I_spec.tex
Packit 06404a
% $Id$
Packit 06404a
\section{Bitpacking Convention} \label{vorbis:spec:bitpacking}
Packit 06404a
Packit 06404a
\subsection{Overview}
Packit 06404a
Packit 06404a
The Vorbis codec uses relatively unstructured raw packets containing
Packit 06404a
arbitrary-width binary integer fields.  Logically, these packets are a
Packit 06404a
bitstream in which bits are coded one-by-one by the encoder and then
Packit 06404a
read one-by-one in the same monotonically increasing order by the
Packit 06404a
decoder.  Most current binary storage arrangements group bits into a
Packit 06404a
native word size of eight bits (octets), sixteen bits, thirty-two bits
Packit 06404a
or, less commonly other fixed word sizes.  The Vorbis bitpacking
Packit 06404a
convention specifies the correct mapping of the logical packet
Packit 06404a
bitstream into an actual representation in fixed-width words.
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{octets, bytes and words}
Packit 06404a
Packit 06404a
In most contemporary architectures, a 'byte' is synonymous with an
Packit 06404a
'octet', that is, eight bits.  This has not always been the case;
Packit 06404a
seven, ten, eleven and sixteen bit 'bytes' have been used.  For
Packit 06404a
purposes of the bitpacking convention, a byte implies the native,
Packit 06404a
smallest integer storage representation offered by a platform.  On
Packit 06404a
modern platforms, this is generally assumed to be eight bits (not
Packit 06404a
necessarily because of the processor but because of the
Packit 06404a
filesystem/memory architecture.  Modern filesystems invariably offer
Packit 06404a
bytes as the fundamental atom of storage).  A 'word' is an integer
Packit 06404a
size that is a grouped multiple of this smallest size.
Packit 06404a
Packit 06404a
The most ubiquitous architectures today consider a 'byte' to be an
Packit 06404a
octet (eight bits) and a word to be a group of two, four or eight
Packit 06404a
bytes (16, 32 or 64 bits).  Note however that the Vorbis bitpacking
Packit 06404a
convention is still well defined for any native byte size; Vorbis uses
Packit 06404a
the native bit-width of a given storage system. This document assumes
Packit 06404a
that a byte is one octet for purposes of example.
Packit 06404a
Packit 06404a
\subsubsection{bit order}
Packit 06404a
Packit 06404a
A byte has a well-defined 'least significant' bit (LSb), which is the
Packit 06404a
only bit set when the byte is storing the two's complement integer
Packit 06404a
value +1.  A byte's 'most significant' bit (MSb) is at the opposite
Packit 06404a
end of the byte. Bits in a byte are numbered from zero at the LSb to
Packit 06404a
$n$ ($n=7$ in an octet) for the
Packit 06404a
MSb.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{byte order}
Packit 06404a
Packit 06404a
Words are native groupings of multiple bytes.  Several byte orderings
Packit 06404a
are possible in a word; the common ones are 3-2-1-0 ('big endian' or
Packit 06404a
'most significant byte first' in which the highest-valued byte comes
Packit 06404a
first), 0-1-2-3 ('little endian' or 'least significant byte first' in
Packit 06404a
which the lowest value byte comes first) and less commonly 3-1-2-0 and
Packit 06404a
0-2-1-3 ('mixed endian').
Packit 06404a
Packit 06404a
The Vorbis bitpacking convention specifies storage and bitstream
Packit 06404a
manipulation at the byte, not word, level, thus host word ordering is
Packit 06404a
of a concern only during optimization when writing high performance
Packit 06404a
code that operates on a word of storage at a time rather than by byte.
Packit 06404a
Logically, bytes are always coded and decoded in order from byte zero
Packit 06404a
through byte $n$.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{coding bits into byte sequences}
Packit 06404a
Packit 06404a
The Vorbis codec has need to code arbitrary bit-width integers, from
Packit 06404a
zero to 32 bits wide, into packets.  These integer fields are not
Packit 06404a
aligned to the boundaries of the byte representation; the next field
Packit 06404a
is written at the bit position at which the previous field ends.
Packit 06404a
Packit 06404a
The encoder logically packs integers by writing the LSb of a binary
Packit 06404a
integer to the logical bitstream first, followed by next least
Packit 06404a
significant bit, etc, until the requested number of bits have been
Packit 06404a
coded.  When packing the bits into bytes, the encoder begins by
Packit 06404a
placing the LSb of the integer to be written into the least
Packit 06404a
significant unused bit position of the destination byte, followed by
Packit 06404a
the next-least significant bit of the source integer and so on up to
Packit 06404a
the requested number of bits.  When all bits of the destination byte
Packit 06404a
have been filled, encoding continues by zeroing all bits of the next
Packit 06404a
byte and writing the next bit into the bit position 0 of that byte.
Packit 06404a
Decoding follows the same process as encoding, but by reading bits
Packit 06404a
from the byte stream and reassembling them into integers.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{signedness}
Packit 06404a
Packit 06404a
The signedness of a specific number resulting from decode is to be
Packit 06404a
interpreted by the decoder given decode context.  That is, the three
Packit 06404a
bit binary pattern 'b111' can be taken to represent either 'seven' as
Packit 06404a
an unsigned integer, or '-1' as a signed, two's complement integer.
Packit 06404a
The encoder and decoder are responsible for knowing if fields are to
Packit 06404a
be treated as signed or unsigned.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{coding example}
Packit 06404a
Packit 06404a
Code the 4 bit integer value '12' [b1100] into an empty bytestream.
Packit 06404a
Bytestream result:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
              |
Packit 06404a
              V
Packit 06404a
Packit 06404a
        7 6 5 4 3 2 1 0
Packit 06404a
byte 0 [0 0 0 0 1 1 0 0]  <-
Packit 06404a
byte 1 [               ]
Packit 06404a
byte 2 [               ]
Packit 06404a
byte 3 [               ]
Packit 06404a
             ...
Packit 06404a
byte n [               ]  bytestream length == 1 byte
Packit 06404a
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Packit 06404a
Continue by coding the 3 bit integer value '-1' [b111]:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
        |
Packit 06404a
        V
Packit 06404a
Packit 06404a
        7 6 5 4 3 2 1 0
Packit 06404a
byte 0 [0 1 1 1 1 1 0 0]  <-
Packit 06404a
byte 1 [               ]
Packit 06404a
byte 2 [               ]
Packit 06404a
byte 3 [               ]
Packit 06404a
             ...
Packit 06404a
byte n [               ]  bytestream length == 1 byte
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Packit 06404a
Continue by coding the 7 bit integer value '17' [b0010001]:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
          |
Packit 06404a
          V
Packit 06404a
Packit 06404a
        7 6 5 4 3 2 1 0
Packit 06404a
byte 0 [1 1 1 1 1 1 0 0]
Packit 06404a
byte 1 [0 0 0 0 1 0 0 0]  <-
Packit 06404a
byte 2 [               ]
Packit 06404a
byte 3 [               ]
Packit 06404a
             ...
Packit 06404a
byte n [               ]  bytestream length == 2 bytes
Packit 06404a
                          bit cursor == 6
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Packit 06404a
Continue by coding the 13 bit integer value '6969' [b110 11001110 01]:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
                |
Packit 06404a
                V
Packit 06404a
Packit 06404a
        7 6 5 4 3 2 1 0
Packit 06404a
byte 0 [1 1 1 1 1 1 0 0]
Packit 06404a
byte 1 [0 1 0 0 1 0 0 0]
Packit 06404a
byte 2 [1 1 0 0 1 1 1 0]
Packit 06404a
byte 3 [0 0 0 0 0 1 1 0]  <-
Packit 06404a
             ...
Packit 06404a
byte n [               ]  bytestream length == 4 bytes
Packit 06404a
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{decoding example}
Packit 06404a
Packit 06404a
Reading from the beginning of the bytestream encoded in the above example:
Packit 06404a
Packit 06404a
\begin{Verbatim}[commandchars=\\\{\}]
Packit 06404a
                      |
Packit 06404a
                      V
Packit 06404a
Packit 06404a
        7 6 5 4 3 2 1 0
Packit 06404a
byte 0 [1 1 1 1 1 1 0 0]  <-
Packit 06404a
byte 1 [0 1 0 0 1 0 0 0]
Packit 06404a
byte 2 [1 1 0 0 1 1 1 0]
Packit 06404a
byte 3 [0 0 0 0 0 1 1 0]  bytestream length == 4 bytes
Packit 06404a
Packit 06404a
\end{Verbatim}
Packit 06404a
Packit 06404a
Packit 06404a
We read two, two-bit integer fields, resulting in the returned numbers
Packit 06404a
'b00' and 'b11'.  Two things are worth noting here:
Packit 06404a
Packit 06404a
\begin{itemize}
Packit 06404a
\item Although these four bits were originally written as a single
Packit 06404a
four-bit integer, reading some other combination of bit-widths from the
Packit 06404a
bitstream is well defined.  There are no artificial alignment
Packit 06404a
boundaries maintained in the bitstream.
Packit 06404a
Packit 06404a
\item The second value is the
Packit 06404a
two-bit-wide integer 'b11'.  This value may be interpreted either as
Packit 06404a
the unsigned value '3', or the signed value '-1'.  Signedness is
Packit 06404a
dependent on decode context.
Packit 06404a
\end{itemize}
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{end-of-packet alignment}
Packit 06404a
Packit 06404a
The typical use of bitpacking is to produce many independent
Packit 06404a
byte-aligned packets which are embedded into a larger byte-aligned
Packit 06404a
container structure, such as an Ogg transport bitstream.  Externally,
Packit 06404a
each bytestream (encoded bitstream) must begin and end on a byte
Packit 06404a
boundary.  Often, the encoded bitstream is not an integer number of
Packit 06404a
bytes, and so there is unused (uncoded) space in the last byte of a
Packit 06404a
packet.
Packit 06404a
Packit 06404a
Unused space in the last byte of a bytestream is always zeroed during
Packit 06404a
the coding process.  Thus, should this unused space be read, it will
Packit 06404a
return binary zeroes.
Packit 06404a
Packit 06404a
Attempting to read past the end of an encoded packet results in an
Packit 06404a
'end-of-packet' condition.  End-of-packet is not to be considered an
Packit 06404a
error; it is merely a state indicating that there is insufficient
Packit 06404a
remaining data to fulfill the desired read size.  Vorbis uses truncated
Packit 06404a
packets as a normal mode of operation, and as such, decoders must
Packit 06404a
handle reading past the end of a packet as a typical mode of
Packit 06404a
operation. Any further read operations after an 'end-of-packet'
Packit 06404a
condition shall also return 'end-of-packet'.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
\subsubsection{reading zero bits}
Packit 06404a
Packit 06404a
Reading a zero-bit-wide integer returns the value '0' and does not
Packit 06404a
increment the stream cursor.  Reading to the end of the packet (but
Packit 06404a
not past, such that an 'end-of-packet' condition has not triggered)
Packit 06404a
and then reading a zero bit integer shall succeed, returning 0, and
Packit 06404a
not trigger an end-of-packet condition.  Reading a zero-bit-wide
Packit 06404a
integer after a previous read sets 'end-of-packet' shall also fail
Packit 06404a
with 'end-of-packet'.
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a