Ogg leaves the meaning of a page’s 64-bit granule position to the codec. It marks a point in time at the end of the last packet finished on the page, and a page where no packet ends has the value −1 (all bits set), so a reader can seek without decoding.
By kind of stream
| Stream | Meaning |
|---|---|
| Vorbis | the number of PCM samples (per channel) decoded by the end of the page |
| Opus | the number of samples at 48 kHz decoded by the end of the page, whatever the input rate was. The header’s pre-skip must be subtracted to get the time |
| Speex | the number of samples, at the sample rate of the header |
| FLAC | the number of the sample at the end of the last frame finished on the page |
| Theora | two parts: (keyframe << KFGSHIFT) + frames since it, where KFGSHIFT comes from the Theora identification header. Both parts count frames rather than index them: the high part is the number of frames up to and including the last key frame, and high + low is the number of frames up to the end of the page, so the first frame of a stream has granule position 1 << KFGSHIFT. Streams made before Theora 3.2.1 (VREV 0 in the header) index frames from 0 instead: a reader adds 1 to their high part |
| Skeleton, Kate and others | Skeleton’s pages have granule position 0. Other mappings give their own rule; Skeleton’s fisbone gives each stream’s granule rate and shift |
Working out time
A stream need not start at 0: one cut from a longer stream keeps its granule positions, so its first audio page says a later time. Its start position is worked out from the first page that finishes an audio packet: that page’s granule position, less the samples its packets decode to. For Vorbis the samples are none for the first audio packet, then for each packet a quarter of the previous block size plus a quarter of its own. The playing time is the last granule position less the start position (and less the pre-skip for Opus), divided by the sample rate.
- Start later than 0. The first audio page’s granule position is larger than its packets’ samples. Vorbis I (A.2) and RFC 7845 both allow it: the audio is the same, and only its place on the timeline changes.
- Start trimming (Vorbis). The first audio page’s granule position is smaller than its packets’ samples: the difference is a count of samples to drop from the start of the decoded audio (they come “before time zero”). Vorbis I A.2 allows no more than the first two audio packets overlap to give. Opus does not allow this: there the stream is invalid, and samples at the start are dropped only through pre-skip.
- End trimming. The last page (with the EOS flag) may give a granule position smaller than what its packets decode to: the spare samples at the end are dropped. This is how Vorbis and Opus files end on an exact sample. For Opus it may drop no more than the last packet decodes.
- A stream on one page. When the first audio page is also the last, its granule position trims the end, and the stream starts at 0.
- Chained files. Each link starts its own timeline; a file’s playing time is the sum of its links’.
Pages without a position
A granule position of −1 can only be on a page where no packet ends, such as the middle of a very long packet. Readers use the nearest page either side of it when seeking.
Links
- RFC 3533 – the Ogg encapsulation format
- RFC 7845 – Ogg encapsulation for Opus, including its granule rule
- Vorbis I specification – appendix A.2, the Vorbis granule rule with start and end trimming
- Theora I specification – appendix A.2.3, the Theora granule position
Source: RFC 3533 section 6 (granule_position, -1 when no packet finishes); Vorbis I specification appendix A.2; RFC 7845 section 4 (Opus); the Speex manual section on Ogg encapsulation (“granulepos is the number of the last sample encoded in that packet”); Ogg FLAC mapping; Ogg Skeleton 3 and 4 specifications. The Theora rule is read from the Theora specification as cited on the Other streams page.