Programming with Xiph.org libvorbis

Description

Libvorbis is the Xiph.org Foundation's portable Ogg Vorbis CODEC implemented as a programmatic library. Libvorbis provides primitives to handle framing and manipulation of Ogg bitstreams (used by the Vorbis for streaming), a full analysis (encoding) interface as well as packet decoding and synthesis for playback.

The libvorbis library does not provide any system interface; a full-featured demonstration player included with the library distribtion provides example code for a variety of system interfaces as well as a working example of using libvorbis in production code.

Encoding Overview

Decoding Overview

Decoding a bitstream with libvorbis follows roughly the following steps:

  1. Frame the incoming bitstream into pages
  2. Sort the pages by logical bitstream and buffer then into logical streams
  3. Decompose the logical streams into raw packets
  4. Reconstruct segments of the original data from each packet
  5. Glue the reconstructed segments back into a decoded stream

Framing

An Ogg bitstream is logically arranged into pages, but to decode the pages, we have to find them first. The raw bitstream is first fed into an ogg_sync_state buffer using ogg_sync_buffer() and ogg_sync_wrote(). After each block we submit to the sync buffer, we should check to see if we can frame and extract a complete page or pages using ogg_sync_pageout(). Extra pages are buffered; allowing them to build up in the ogg_sync_state buffer will eventually exhaust memory.

The Ogg pages returned from ogg_sync_pageout need not be decoded further to be used as landmarks in seeking; seeking can be either a rough process of simply jumping to approximately intuited portions of the bitstream, or it can be a precise bisection process that captures pages and inspects data position. When seeking, however, sequential multiplexing (chaining) must be accounted for; beginning play in a new logical bitstream requires initializing a synthesis engine with the headers from that bitstream. Vorbis bitstreams do not make use of concurent multiplexing (grouping).

Sorting

The pages produced by ogg_sync_pageout are then sorted by serial number to seperate logical bitstreams. Initialize logical bitstream buffers (og_stream_state) using ogg_stream_init(). Pages are submitted to the matching logical bitstream buffer using ogg_stream_pagein; the serial number of the page and the stream buffer must match, or the page will be rejected. A page submitted out of sequence will simply be noted, and in the course of outputting packets, the hole will be flagged (ogg_sync_pageout and ogg_stream_packetout will return a negative value at positions where they had to recapture the stream).

Extracting packets

After submitting page[s] to a logical stream, read available packets using ogg_stream_packetout.

Decoding packets

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling data segments

Ogg Bitstream Manipulation Structures

Two of the Ogg bitstream data structures are intended to be transparent to the developer; the fields should be used directly.

ogg_packet

typed/h3>

Reassembling