[vorbis] Extracting sample count from a Vorbis packet

Ralph Giles giles at xiph.org
Wed Nov 5 01:14:03 PST 2003



On Wed, Nov 05, 2003 at 12:31:30AM -0800, Aaron Colwell wrote:

> I was wondering if there was a way to determine the number of samples in a 
> Vorbis packet without having to decode the whole thing? The reason I'm 
> asking this is because I'm thinking about implementing the current Vorbis 
> RTP draft for Helix.

Oh yay!

>                       The problem is that the Ogg pages only contain a 
> single timestamp. If I want to split up the Vorbis packets on different 
> boundries than the Ogg page boundries, I'll need to know the number of 
> samples associated with each packet so I can generate proper timestamps. 
> Can anyone help me with this?

This isn't too bad. Each vorbis packet encodes either a short or long 
block of audio. The sizes of the two block types are stored in the info 
header (the first packet in the stream) as two 4-bit power-of-2 
exponents at byte offset 27. See section 4.2.1 and 4.2.2 of the spec 
for parsing details.

You can then look at the header of each audio packet to determine which 
block type it is. See section 4.3.1 for details. Unfortunately, you also 
need the mode count from near the end of the the setup header (the third 
packet in the stream, sections 4.2.4.6 and 4.2.4) to get the bit offset 
of the blocktype field.

While that gives you the length (in samples per channel) of each block 
type, that's not how far decoding advances with each packet because of 
the window overlaps (block lapping) with the next and previous packets. 
This is a little complicated to describe, so I'll just refer you to the 
picture at 1.3.2.3. Each packet advances you from the start of the 
overlap with the previous block to the start of the overlap with the 
next block. For convenience, the size of the next and previous blocks 
are also encoded in each audio packet header. (Again see section 
4.3.1.) This means that in seeking you can 'work backwards' from the 
granulepos of the current page pretty easily without having to look at 
the previous page.

Also note that the beginning and end of an ogg-encapsulated vorbis 
stream can be trimed by adjusting the sample numbers given in the page 
granulepos. You might want to take this into account as well. See 
appendix A.2 in the spec for some notes on this.

Anyway, hope that's enough to get you started. The spec is available 
online as http://xiph.org/ogg/vorbis/doc/Vorbis_I_spec.html (or .pdf) if 
you don't have a local copy. And please feel free to ask if you have any 
further questions. We'd really like to see a vorbis rtp implementation.

 -r

BTW, vorbis-dev might be more appropriate for technical questions of 
this sort.
--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'vorbis-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the Vorbis mailing list