1 ********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *
9 * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: discussion of Vorbis framing
15 author: Monty <xiphmont@mit.edu>, <monty@xiph.org>
16 modifications by: Monty
17 last modification date: Jun 30 1999
19 ********************************************************************
21 Vorbis encodes short-time blocks of PCM data into raw packets of
22 bit-packed data. These raw packets may be used directly by transport
23 mechanisms that provide their own framing and packet-seperation
24 mechanisms (such as UDP datagrams). For stream based storage (such as
25 files) and transport (such as TCP streams or pipes), Vorbis also
26 specifies an additional layer of bitstream structure to provide
27 framing/sync, sync recapture after error, landmarks during seeking,
28 and enough information to properly seperate data back into packets at
29 the original packet boundaries without relying on decoding to find
34 1) True streaming; we must not need to seek to build a 100% complete
37 2) Use no more than approximately 1-2% of bitstream bandwidth for packet
38 boundary marking, high-level framing, sync and seeking.
40 3) Specification of absolute position within the original sample stream.
42 4) Simple mechanism to ease limited editing, such as a simplified
43 concatenation mechanism.
45 5) Detection of corruption and recapture after error.
47 A vorbis stream is structured by dividing packets into segments of up
48 to 255 bytes and then wrapping a group of contiguous packet segments
49 into a variable length page preceeded by a page header. Both the
50 header size and page size are variable; the page header contains
51 sizing information and checksum data to determine header/page size and
54 The bitstream is captured (or recaptured) by looking for the beginning
55 of a page, specifically the capture pattern. Once the capture pattern
56 is found, the decoder verifies page sync and integrity by computing
57 and comparing the checksum. At that point, the decoder can extract the
60 **** Packet segmentation
62 Packets are logically divided into multiple segments before encoding
63 into a page. Note that the segmentation and fragmentation process is a
64 logical one; it's used to compute page header values and the original
65 page data need not be disturbed, even when a packet spans page
68 The raw packet is logically divided into [n] 255 byte segments and a
69 last fractional segment of < 255 bytes. A packet size may well
70 consist only of the trailing fractional segment, and a fractional
71 segment may be zero length. These values, called "lacing values" are
72 then saved and placed into the header segment table.
74 An example should make the basic concept clear:
77 ___________________________________________
78 |______________packet data__________________| 753 bytes
80 lacing values for page header segment table: 255,255,243
82 We simply add the lacing values for the total size; the last lacing
83 value for a packet is always the value that is less than 255. Note
84 that this encoding both avoids imposing a maximum packet size as well
85 as imposing minimum overhead on small packets (as opposed to, eg,
86 simply using two bytes at the head of every packet and having a max
87 packet size of 32k. Small packets (<255, the typical case) are
88 penalized with twice the segmentation overhead). Using the lacing
89 values as suggested, small packets see the minimum possible
90 byte-aligned overheade (1 byte) and large packets, over 512 bytes or
91 so, see a fairly constant ~.5% overhead on encoding space.
93 Clarification of boundary cases:
95 Note that a lacing value of 255 implies that a second lacing value
96 follows in the packet, and a value of < 255 marks the end of the
97 packet after that many additional bytes. A packet of 255 bytes (or a
98 multiple of 255 bytes) is terminated by a lacing value of 0:
101 _______________________________
102 |________packet data____________| 255 bytes
104 lacing values: 255, 0
106 Note also that a 'nil' (zero length) packet is not an error; it
107 consists of nothing more than a lacing value of zero in the header.
109 **** Packets spanning pages:
111 Packets are not resticted to beginning and ending within a page,
112 although individual segments are, by definition, required to do so.
113 Packets are not restricted to a maximum size, although excessively
114 large packets in the data stream are discouraged; the Vorbis
115 specification strongly recommend nominal page size of approximately
116 4-8kB (large packets are forseen as being useful for initialization
117 data at the beginning of a logical bitstream).
119 After segmenting a packet, the encoder may decide not to place all the
120 resulting segments into the current page; to do so, the encoder places
121 the lacing values of the segments it wishes to belong to the current
122 page into the current segment table, then finishes the page. The next
123 page is begun with the first value in the segment table belonging to
124 the next packet segment, thus continuing the packet (data in the
125 packet body must also correspond properly to the lacing values in the
126 spanned pages. The segment data in the first packet corresponding to the
127 lacing values of the first page belong in that page; packet segments listed in the segment table of the following page must begin the page body of the subsequent page).
129 The last mechanic to spanning a page boundary is to set the header
130 flag in the new page to indicate that the first lacing value in the
131 segment table continues rather than begins a packet; a header flag of
132 0x02 is used to indicate a continued packet. Although mandatory, it
133 is not actually algorithmically necessary; one could inspect the
134 preceeding segment table to determine if the packet is new or
135 continued. Adding the information to the packet_header flag allows a
136 simpler design (with no overhead) that needs only inspect the current
137 page header after frame capture. This also allows faster error
138 recovery in the event that the packet originates in a corrupt
139 preceeding page, implying that the previous page's segment table
142 Note that a packet can span an arbitrary number of pages; the above
143 spanning process is repeated for each spanned page boundary. Also a
144 'zero termination' on a packet size that is an even multiple of 255
145 must appear even if the lacing value appears in the next page as a
146 zero-length continuation of the current packet. The header flag
147 should be set to 0x02 to indicate that the packet spanned, even though
148 the span is a nil case as far as data is concerned.
150 The encoding looks odd, but is properly optimized for speed and the
151 expected case of the majority of packets being between 50 and 200
152 bytes (note that it is designed such that packets of wildly different
153 sizes can be handled within the model; placing packet size
154 restrictions on the encoder would have only slightly simplified design
155 in page generation and increased overall encoder complexity).
157 The main point behind tracking individual packets (and packet
158 segments) is to allow more flexible encoding tricks that requiring
159 explicit knowledge of packet size. An example is simple bandwidth
160 limiting, implemented by simply truncating packets in the nominal case
161 (the packet is arranged so that the least sensitive portion of the
166 The headering mechanism is designed to avoid copying and re-assembly
167 of the packet data; the header can be generated directly from incoming
168 packet data. The encoder buffers packet data until it finishes a
169 complete page at which point it writes the header followed by the
170 buffered packet segments.
174 A header begins with a capture pattern that simplifies identifying
175 pages; once the decoder has found the capture pattern it can do a more
176 intensive job of verifying that it has in fact found a page boundary
177 (as opposed to an inadvertant coincidence in the byte stream).
185 stream_structure_version:
187 The capture pattern is followed by the stream structure revision:
194 The header type flag identifies this page's context in the bitstream:
196 5 0x00 (beginning of bitstream)
197 0x01 (bitstream continued, fresh packet)
198 0x02 (bitstream continued, continued packet)
200 PCM absolute position
202 (This is packed in the same way the rest of Vorbis packet data is
203 packed; LSb of LSB first. Note that the 'position' data specifies a
204 sample number (eg, in a CD quality sample is four octets, 16 bits for
205 left and 16 bits for right). The position specified is the total
206 samples encoded after including all packets begun in this page. The
207 rationale here is that the position specified in the frame header of
208 the last page tells how long the PCM data coded by the bitstream is.
219 stream serial number:
221 Vorbis allows for seperate logical bitstreams to be mixed at page
222 granularity in a physical bitstream. The most common case would be
223 sequential arrangement, but it is possible to interleave pages for
224 two seperate bitstreams to be decoded concurrently. Right now, the
225 standard code doesn't use the serial number (sets it to zero), but it
226 will eventually. Each logical stream must have a unique serial
227 number within a physical stream:
236 Page counter; lets us know if a page is lost (useful where packets
237 span page boundaries).
246 32 bit CRC value (direct algorithm, initial val and final XOR = 0,
247 generator polynomial=0x04c11db7). The value is computed over the
248 entire header (with the CRC field in the header set to zero) and then
249 continued over the page. The CRC field is then filled with the
252 (A thorough discussion of CRC algorithms can be found in "A Painless
253 Guide to CRC Error Detection Algorithms" by Ross Williams
254 (ross@guest.adelaide.edu.au). The document is available from
255 ftp://ftp.adelaide.edu.au/pub/rocksoft)
264 The number of segment entries to appear in the segment table. The
265 maximum number of 255 segments (255 bytes each) sets the maximum
266 possible physical page size at 65307 bytes or just under 64kB (thus
267 we know that a header corrupted so as destroy sizing/alignment
268 information will not cause a runaway bitstream. We'll read in the
269 page according to the corrupted size information that's guaranteed to
270 be a reasonable size regardless, notice the checksum mismatch, drop
271 sync and then look for recapture).
275 segment_table (containing packet lacing values)
277 The lacing values for each packet segment physically appearing in
278 this page are listed in contiguous order.
282 n 0x00-0xff (0-255, n=page_segments+26)
284 Total page size is calculated directly from the known header size and
285 lacing values in the segment table. Packet data segments follow
286 immediately after the header.
288 Page headers typically impose a flat .25-.5% space overhead assuming
289 nominal ~8k page sizes. The segmentation table needed for exact
290 packet recovery in the streaming layer adds approximately .5-1%
291 nominal assuming expected encoder behavior in the 44.1kHz, 128kbps