[Vorbis-dev] How can I seek in Ogg Vorbis file, but not using Vorbisfile library?

Ralph Giles giles at xiph.org
Tue Aug 29 10:04:26 PDT 2006


On Tue, Aug 29, 2006 at 07:01:24PM +0800, 隽 徐 wrote:

>   Because I am supposed not to use file, so I can't use the ov_pcm_seek() or ov_raw_seek() functions to seek in the file. For decoding an Ogg Vorbis file, I first put it in a buffer, then use API provided by libogg and libvorbis to decode it, and put the PCM in ouput buffer.

You can use ov_open_callbacks() to supply your own read/write/seek 
functions for reading from memory instead of a file. This is the 
simplest way to do what you want.

>   Now I have a problem, how to seek in the Ogg Vorbis file not using Vorbisfile library? That means, I can only use API provided by libogg and libvorbis libraries, and I didn't find an example.

Well, vorbisfile.c in the Tremor source is obviously such an example. 
You could see what it does. :)

The seek algorithm is a little complicated, but basically:

The stream is divided into Ogg pages, each has a serial number 
indicating which logical stream it belongs to, and a granulepos
which can be mapped to a timestamp if you understand the format
of the stream data. So you binary search for the time you want,
using a table of known sign posts, and known mappings from 
granulepos to time for each known stream serial number. Generally 
to build the known mappings table for each serial number you also 
have to binary search for the chain boundaries. Vorbisfile does
this when it opens the file, but in theory you could do it on the
fly.

Hope that helps,
 -r


More information about the Vorbis-dev mailing list