Ogg page
An Ogg page is a unit of data in an Ogg bitstream, usually between 4 kB and 8 kB, with a maximum size of 65,307 bytes.[1]
Purpose
One of the purposes of multimedia container formats is to allow multiple codecs to be muxed into a single file or stream. For example, to combine audio, video, and subtitles into a single file.
The viewpoint held by Christopher Montgomery, creator of the Ogg format, is that the units of muxed codec data should be a separate abstraction layer from the units of data used by the codec in order to limit the amount of buffering necessary when decoding the contained data. In his opinion, which is supported by other developers working with the Xiph.org Foundation, this gives Ogg a technical advantage over alternative bitstream formats such as AVI, QuickTime, and MPEG.
Each Ogg page also provides the time offset of the contained data, which allows efficient seeking that is both accurate and compatible with streaming. In contrast, many other formats seek to byte positions in the stream or rely on a table of contents for seeking information.
Page structure
Every Ogg page begins with the four-byte magic string "OggS". If sync is lost a decoder can look for the next occurrence of this sequence to begin decoding again. These four bytes are followed by a zero byte for Ogg version 0. The version field had originally been intended to allow multiple Ogg page types tuned for different payloads to coexist in the same stream. In the 2000s it became clear that only one page version will be used.[2]
The sixth byte of each page specifies type flags. The value 1 specifies that the page contains data of a packet continued from the previous page. The value 2 specifies that this is the first page of the stream, and the value 4 specifies that this is the last page of the stream. These values can be combined with addition or logical OR.
The next 8 bytes, or 64 bits, is called the absolute granule position which is a synthetic value that encodes the Decode Timestamp, the Presentation time stamp and distance to first-needed reference. The exact encoding of the granule position is up to a specific codec.[2][3]
The following 4 bytes are the stream serial number to which this page belongs. Each logical stream must have a unique serial number within a physical stream. It is also intended to be used like a weak hash so that a collision is very unlikely when multiplexing different streams, which eliminates the need for continuous recalculation of page headers at every multiplexing step.
The following 4 bytes are the page sequence number within the stream. This is a page counter that allows detection of lost pages. In comparison to other container formats, the large number of bits also allows direct UDP unicast/multicast with Ogg handling reordering and reassembly.[2]
The next 4 bytes, starting at the 23rd byte of the page, is the CRC checksum of the page. Because the value of this field changes, the result of the check is computed with this field equal to zero.
Next, the 27th byte of each page specifies the number of segments it contains, ranging from 0 to 255. This is also the size of the following segment table in bytes. Each byte of the segment table provides the length of a segment.
Each segment can be up to 255 bytes in length and is bounded by the page. A segment smaller than 255 bytes marks the end of a packet; the next segment will begin a new packet. A packet with a length that is a multiple of 255 bytes will end with a segment of length 0. If the last segment of the page is 255 bytes then the last packet is continued on the following page.
Editing with pages
One of the common uses of Ogg pages is to allow the editing of Ogg files without a reduction in quality or the need to transcode files.
Any series of pages within a stream can be preceded by the stream's header pages (pages with an absolute granule position of 0) and be decoded properly by media players. This technique can be used to losslessly crop a media file.
Multiple cropped segments can be appended to each other with a process known as chaining provided that the serial numbers of these segments are modified to unique numbers. New content can also be inserted between these segments.
References
- "RFC 3533 - The Ogg Encapsulation Format Version 0".
- Montgomery, Christopher (2010-04-27). "Monty - In Defense of Ogg's Good Name". people.xiph.org/~xiphmont. Archived from the original on 2010-04-29. Retrieved 2010-05-02.
- "ogg-multiplex". Retrieved 2011-03-02.