1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-15"/>
6 <title>Ogg Vorbis Documentation
</title>
8 <style type=
"text/css">
10 margin: 0 18px 0 18px;
12 font-family: Verdana
, Arial
, Helvetica
, sans-serif
;
26 margin: 30px 0 16px 0;
33 h1
, h1 a
, h2
, h2 a
, h3
, h3 a
{
36 margin: 1.3em 0 8px 0;
70 <a href=
"http://www.xiph.org/"><img src=
"fish_xiph_org.png" alt=
"Fish Logo and Xiph.org"/></a>
73 <h1>Programming with Xiph.org
<tt>libvorbis
</tt></h1>
77 <p>Libvorbis is the Xiph.org Foundation's portable Ogg Vorbis CODEC
78 implemented as a programmatic library. Libvorbis provides primitives
79 to handle framing and manipulation of Ogg bitstreams (used by the
80 Vorbis for streaming), a full analysis (encoding) interface as well as
81 packet decoding and synthesis for playback.
</p>
83 <p>The libvorbis library does not provide any system interface; a
84 full-featured demonstration player included with the library
85 distribtion provides example code for a variety of system interfaces
86 as well as a working example of using libvorbis in production code.
</p>
88 <h2>Encoding Overview
</h2>
90 <h2>Decoding Overview
</h2>
92 <p>Decoding a bitstream with libvorbis follows roughly the following
96 <li>Frame the incoming bitstream into pages
</li>
97 <li>Sort the pages by logical bitstream and buffer then into logical streams
</li>
98 <li>Decompose the logical streams into raw packets
</li>
99 <li>Reconstruct segments of the original data from each packet
</li>
100 <li>Glue the reconstructed segments back into a decoded stream
</li>
105 <p>An Ogg bitstream is logically arranged into pages, but to decode
106 the pages, we have to find them first. The raw bitstream is first fed
107 into an
<tt>ogg_sync_state
</tt> buffer using
<tt>ogg_sync_buffer()
</tt>
108 and
<tt>ogg_sync_wrote()
</tt>. After each block we submit to the sync
109 buffer, we should check to see if we can frame and extract a complete
110 page or pages using
<tt>ogg_sync_pageout()
</tt>. Extra pages are
111 buffered; allowing them to build up in the
<tt>ogg_sync_state
</tt>
112 buffer will eventually exhaust memory.
</p>
114 <p>The Ogg pages returned from
<tt>ogg_sync_pageout
</tt> need not be
115 decoded further to be used as landmarks in seeking; seeking can be
116 either a rough process of simply jumping to approximately intuited
117 portions of the bitstream, or it can be a precise bisection process
118 that captures pages and inspects data position. When seeking,
119 however, sequential multiplexing (chaining) must be accounted for;
120 beginning play in a new logical bitstream requires initializing a
121 synthesis engine with the headers from that bitstream. Vorbis
122 bitstreams do not make use of concurent multiplexing (grouping).
</p>
126 <p>The pages produced by
<tt>ogg_sync_pageout
</tt> are then sorted by
127 serial number to seperate logical bitstreams. Initialize logical
128 bitstream buffers (
<tt>og_stream_state
</tt>) using
129 <tt>ogg_stream_init()
</tt>. Pages are submitted to the matching
130 logical bitstream buffer using
<tt>ogg_stream_pagein
</tt>; the serial
131 number of the page and the stream buffer must match, or the page will
132 be rejected. A page submitted out of sequence will simply be noted,
133 and in the course of outputting packets, the hole will be flagged
134 (
<tt>ogg_sync_pageout
</tt> and
<tt>ogg_stream_packetout
</tt> will
135 return a negative value at positions where they had to recapture the
138 <h3>Extracting packets
</h3>
140 <p>After submitting page[s] to a logical stream, read available packets
141 using
<tt>ogg_stream_packetout
</tt>.
</p>
143 <h3>Decoding packets
</h3>
145 <h3>Reassembling data segments
</h3>
147 <h2>Ogg Bitstream Manipulation Structures
</h2>
149 <p>Two of the Ogg bitstream data structures are intended to be
150 transparent to the developer; the fields should be used directly.
</p>
156 unsigned char *packet;
168 <dd>a pointer to the byte data of the raw packet
</dd>
170 <dd>the size of the packet' raw data
</dd>
172 <dd>beginning of stream; nonzero if this is the first packet of
173 the logical bitstream
</dd>
175 <dd>end of stream; nonzero if this is the last packet of the
176 logical bitstream
</dd>
178 <dd>the absolute position of this packet in the original
179 uncompressed data stream.
</dd>
182 <h4>encoding notes
</h4>
184 <p>The encoder is responsible for setting all of
185 the fields of the packet to appropriate values before submission to
186 <tt>ogg_stream_packetin()
</tt>; however, it is noted that the value in
187 <tt>b_o_s
</tt> is ignored; the first page produced from a given
188 <tt>ogg_stream_state
</tt> structure will be stamped as the initial
189 page.
<tt>e_o_s
</tt>, however, must be set; this is the means by
190 which the stream encoding primitives handle end of stream and cleanup.
</p>
192 <h4>decoding notes
</h4>
194 <p><tt>ogg_stream_packetout()
</tt> sets the fields
195 to appropriate values. Note that granulepos will be
>=
0 only in the
196 case that the given packet actually represents that position (ie, only
197 the last packet completed on any page will have a meaningful
198 <tt>granulepos
</tt>). Intervening frames will see
<tt>granulepos
</tt> set
205 unsigned char *header;
214 <dd>pointer to the page header data
</dd>
216 <dd>length of the page header in bytes
</dd>
218 <dd>pointer to the page body
</dd>
220 <dd>length of the page body
</dd>
223 <p>Note that although the
<tt>header
</tt> and
<tt>body
</tt> pointers do
224 not necessarily point into a single contiguous page vector, the page
225 body must immediately follow the header in the bitstream.
</p>
227 <h2>Ogg Bitstream Manipulation Functions
</h2>
230 int ogg_page_bos(ogg_page *og);
233 <p>Returns the 'beginning of stream' flag for the given Ogg page. The
234 beginning of stream flag is set on the initial page of a logical
237 <p>Zero indicates the flag is cleared (this is not the initial page of a
238 logical bitstream). Nonzero indicates the flag is set (this is the
239 initial page of a logical bitstream).
</p>
242 int ogg_page_continued(ogg_page *og);
245 <p>Returns the 'packet continued' flag for the given Ogg page. The packet
246 continued flag indicates whether or not the body data of this page
247 begins with packet continued from a preceeding page.
</p>
249 <p>Zero (unset) indicates that the body data begins with a new packet.
250 Nonzero (set) indicates that the first packet data on the page is a
251 continuation from the preceeding page.
</p>
254 int ogg_page_eos(ogg_page *og);
257 <p>Returns the 'end of stream' flag for a give Ogg page. The end of page
258 flag is set on the last (terminal) page of a logical bitstream.
</p>
260 <p>Zero (unset) indicates that this is not the last page of a logical
261 bitstream. Nonzero (set) indicates that this is the last page of a
262 logical bitstream and that no addiitonal pages belonging to this
263 bitstream may follow.
</p>
266 size64 ogg_page_granulepos(ogg_page *og);
269 <p>Returns the position of this page as an absolute position within the
270 original uncompressed data. The position, as returned, is 'frames
271 encoded to date up to and including the last whole packet on this
272 page'. Partial packets begun on this page but continued to the
273 following page are not included. If no packet ends on this page, the
274 frame position value will be equal to the frame position value of the
275 preceeding page. If none of the original uncompressed data is yet
276 represented in the logical bitstream (for example, the first page of a
277 bitstream consists only of a header packet; this packet encodes only
278 metadata), the value shall be zero.
</p>
280 <p>The units of the framenumber are determined by media mapping. A
281 vorbis audio bitstream, for example, defines one frame to be the
282 channel values from a single sampling period (eg, a
16 bit stereo
283 bitstream consists of two samples of two bytes for a total of four
284 bytes, thus a frame would be four bytes). A video stream defines one
285 frame to be a single frame of video.
</p>
288 int ogg_page_pageno(ogg_page *og);
291 <p>Returns the sequential page number of the given Ogg page. The first
292 page in a logical bitstream is numbered zero; following pages are
293 numbered in increasing monotonic order.
</p>
296 int ogg_page_serialno(ogg_page *og);
299 <p>Returns the serial number of the given Ogg page. The serial number is
300 used as a handle to distinguish various logical bitstreams in a
301 physical Ogg bitstresm. Every logical bitstream within a
302 physical bitstream must use a unique (within the scope of the physical
303 bitstream) serial number, which is stamped on all bitstream pages.
</p>
306 int ogg_page_version(ogg_page *og);
309 <p>Returns the revision of the Ogg bitstream structure of the given page.
310 Currently, the only permitted number is zero. Later revisions of the
311 bitstream spec will increment this version should any changes be
315 int ogg_stream_clear(ogg_stream_state *os);
318 <p>Clears and deallocates the internal storage of the given Ogg stream.
319 After clearing, the stream structure is not initialized for use;
320 <tt>ogg_stream_init
</tt> must be called to reinitialize for use.
321 Use
<tt>ogg_stream_reset
</tt> to reset the stream state
322 to a fresh, intiialized state.
</p>
324 <p><tt>ogg_stream_clear
</tt> does not call
<tt>free()
</tt> on the pointer
325 <tt>os
</tt>, allowing use of this call on stream structures in static
326 or automatic storage.
<tt>ogg_stream_destroy
</tt>is a complimentary
327 function that frees the pointer as well.
</p>
329 <p>Returns zero on success and non-zero on failure. This function always
333 int ogg_stream_destroy(ogg_stream_state *os);
336 <p>Clears and deallocates the internal storage of the given Ogg stream,
337 then frees the storage associated with the pointer
<tt>os
</tt>.
</p>
339 <p><tt>ogg_stream_clear
</tt> does not call
<tt>free()
</tt> on the pointer
340 <tt>os
</tt>, allowing use of that call on stream structures in static
341 or automatic storage.
</p>
343 <p>Returns zero on success and non-zero on failure. This function always
347 int ogg_stream_init(ogg_stream_state *os,int serialno);
350 <p>Initialize the storage associated with
<tt>os
</tt> for use as an Ogg
351 stream. This call is used to initialize a stream for both encode and
352 decode. The given serial number is the serial number that will be
353 stamped on pages of the produced bitstream (during encode), or used as
354 a check that pages match (during decode).
</p>
356 <p>Returns zero on success, nonzero on failure.
</p>
359 int ogg_stream_packetin(ogg_stream_state *os, ogg_packet *op);
362 <p>Used during encoding to add the given raw packet to the given Ogg
363 bitstream. The contents of
<tt>op
</tt> are copied;
364 <tt>ogg_stream_packetin
</tt> does not retain any pointers into
365 <tt>op
</tt>'s storage. The encoding proccess buffers incoming packets
366 until enough packets have been assembled to form an entire page;
367 <tt>ogg_stream_pageout
</tt> is used to read complete pages.
</p>
369 <p>Returns zero on success, nonzero on failure.
</p>
372 int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op);
375 <p>Used during decoding to read raw packets from the given logical
376 bitstream.
<tt>ogg_stream_packetout
</tt> will only return complete
377 packets for which checksumming indicates no corruption. The size and
378 contents of the packet exactly match those given in the encoding
381 <p>Returns zero if the next packet is not ready to be read (not buffered
382 or incomplete), positive if it returned a complete packet in
383 <tt>op
</tt> and negative if there is a gap, extra bytes or corruption
384 at this position in the bitstream (essentially that the bitstream had
385 to be recaptured). A negative value is not necessarily an error. It
386 would be a common occurence when seeking, for example, which requires
387 recapture of the bitstream at the position decoding continued.
</p>
389 <p>If the return value is positive,
<tt>ogg_stream_packetout
</tt> placed
390 a packet in
<tt>op
</tt>. The data in
<tt>op
</tt> points to static
391 storage that is valid until the next call to
392 <tt>ogg_stream_pagein
</tt>,
<tt>ogg_stream_clear
</tt>,
393 <tt>ogg_stream_reset
</tt>, or
<tt>ogg_stream_destroy
</tt>. The
394 pointers are not invalidated by more calls to
395 <tt>ogg_stream_packetout
</tt>.
</p>
398 int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og);
401 <p>Used during decoding to buffer the given complete, pre-verified page
402 for decoding into raw Ogg packets. The given page must be framed,
403 normally produced by
<tt>ogg_sync_pageout
</tt>, and from the logical
404 bitstream associated with
<tt>os
</tt> (the serial numbers must match).
405 The contents of the given page are copied;
<tt>ogg_stream_pagein
</tt>
406 retains no pointers into
<tt>og
</tt> storage.
</p>
408 <p>Returns zero on success and non-zero on failure.
</p>
411 int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og);
414 <p>Used during encode to read complete pages from the stream buffer. The
415 returned page is ready for sending out to the real world.
</p>
417 <p>Returns zero if there is no complete page ready for reading. Returns
418 nonzero when it has placed data for a complete page into
419 <tt>og
</tt>. Note that the storage returned in og points into internal
420 storage; the pointers in
<tt>og
</tt> are valid until the next call to
421 <tt>ogg_stream_pageout
</tt>,
<tt>ogg_stream_packetin
</tt>,
422 <tt>ogg_stream_reset
</tt>,
<tt>ogg_stream_clear
</tt> or
423 <tt>ogg_stream_destroy
</tt>.
</p>
426 int ogg_stream_reset(ogg_stream_state *os);
429 <p>Resets the given stream's state to that of a blank, unused stream;
430 this may be used during encode or decode.
</p>
432 <p>Note that if used during encode, it does not alter the stream's serial
433 number. In addition, the next page produced during encoding will be
434 marked as the 'initial' page of the logical bitstream.
</p>
436 <p>When used during decode, this simply clears the data buffer of any
437 pending pages. Beginning and end of stream cues are read from the
438 bitstream and are unaffected by reset.
</p>
440 <p>Returns zero on success and non-zero on failure. This function always
444 char *ogg_sync_buffer(ogg_sync_state *oy, long size);
447 <p>This call is used to buffer a raw bitstream for framing and
448 verification.
<tt>ogg_sync_buffer
</tt> handles stream capture and
449 recapture, checksumming, and division into Ogg pages (as required by
450 <tt>ogg_stream_pagein
</tt>).
</p>
452 <p><tt>ogg_sync_buffer
</tt> exposes a buffer area into which the decoder
453 copies the next (up to)
<tt>size
</tt> bytes. We expose the buffer
454 (rather than taking a buffer) in order to avoid an extra copy many
455 uses; this way, for example,
<tt>read()
</tt> can transfer data
456 directly into the stream buffer without first needing to place it in
457 temporary storage.
</p>
459 <p>Returns a pointer into
<tt>oy
</tt>'s internal bitstream sync buffer;
460 the remaining space in the sync buffer is at least
<tt>size
</tt>
461 bytes. The decoder need not write all of
<tt>size
</tt> bytes;
462 <tt>ogg_sync_wrote
</tt> is used to inform the engine how many bytes
463 were actually written. Use of
<tt>ogg_sync_wrote
</tt> after writing
464 into the exposed buffer is mandantory.
</p>
467 int ogg_sync_clear(ogg_sync_state *oy);
470 <p><tt>ogg_sync_clear
</tt>
471 clears and deallocates the internal storage of the given Ogg sync
472 buffer. After clearing, the sync structure is not initialized for
473 use;
<tt>ogg_sync_init
</tt> must be called to reinitialize for use.
474 Use
<tt>ogg_sync_reset
</tt> to reset the sync state and buffer to a
475 fresh, intiialized state.
</p>
477 <p><tt>ogg_sync_clear
</tt> does not call
<tt>free()
</tt> on the pointer
478 <tt>oy
</tt>, allowing use of this call on sync structures in static
479 or automatic storage.
<tt>ogg_sync_destroy
</tt>is a complimentary
480 function that frees the pointer as well.
</p>
482 <p>Returns zero on success and non-zero on failure. This function always
486 int ogg_sync_destroy(ogg_sync_state *oy);
489 <p>Clears and deallocates the internal storage of the given Ogg sync
490 buffer, then frees the storage associated with the pointer
493 <p><tt>ogg_sync_clear
</tt> does not call
<tt>free()
</tt> on the pointer
494 <tt>oy
</tt>, allowing use of that call on stream structures in static
495 or automatic storage.
</p>
497 <p>Returns zero on success and non-zero on failure. This function always
501 int ogg_sync_init(ogg_sync_state *oy);
504 <p>Initializes the sync buffer
<tt>oy
</tt> for use.
</p>
506 <p>Returns zero on success and non-zero on failure. This function always
510 int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og);
513 <p>Reads complete, framed, verified Ogg pages from the sync buffer,
514 placing the page data in
<tt>og
</tt>.
</p>
516 <p>Returns zero when there's no complete pages buffered for
517 retrieval. Returns negative when a loss of sync or recapture occurred
518 (this is not necessarily an error; recapture would be required after
519 seeking, for example). Returns positive when a page is returned in
520 <tt>og
</tt>. Note that the data in
<tt>og
</tt> points into the sync
521 buffer storage; the pointers are valid until the next call to
522 <tt>ogg_sync_buffer
</tt>,
<tt>ogg_sync_clear
</tt>,
523 <tt>ogg_sync_destroy
</tt> or
<tt>ogg_sync_reset
</tt>.
</p>
526 int ogg_sync_reset(ogg_sync_state *oy);
529 <p><tt>ogg_sync_reset
</tt> resets the sync state in
<tt>oy
</tt> to a
530 clean, empty state. This is useful, for example, when seeking to a
531 new location in a bitstream.
</p>
533 <p>Returns zero on success, nonzero on failure.
</p>
536 int ogg_sync_wrote(ogg_sync_state *oy, long bytes);
539 <p>Used to inform the sync state as to how many bytes were actually
540 written into the exposed sync buffer. It must be equal to or less
541 than the size of the buffer requested.
</p>
543 <p>Returns zero on success and non-zero on failure; failure occurs only
544 when the number of bytes written were larger than the buffer.
</p>
547 The Xiph Fish Logo is a
548 trademark (
™) of Xiph.Org.
<br/>
550 These pages
© 1994 -
2005 Xiph.Org. All rights reserved.