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

Ian Malone ibmalone at gmail.com
Fri Sep 1 09:25:55 PDT 2006


On 01/09/06, 隽 徐 <china_xujun at yahoo.com.cn> wrote:
>
> Hello, All.
>
> Thanks, Ian Malone, thank you for your help. I have a better understanding
> about the Ogg Vorbis
> decoding now.
>
> You are so kind to give me the example about callbacks, but unfortunately, I
> am not allowed to use the
> file like operations. So, I can only use the API provided by libogg and
> libvorbis.
>

I'm not quite sure what you mean by "not allowed to use file like operations."
If you mean you can't use fread etc. then that's what I meant when I said
you can simply provide callbacks to use your buffer. You'd have a struct like
this:
struct data_buffer {
    char *start; // Start of your buffer
    char *end;  // Last element of the buffer
    char *current; // Current read position, initially = start.
};

size_t buffer_read(void *target, size_t SIZE, size_t COUNT,
                          struct data_buffer *buffer) {
    char *p;
    size_t items = 0;
    for( p = buffer->current;
         p <= end && p - buffer->current < COUNT*SIZE; p++, target++)
        *target = *p;
    items = (p - buffer->current)/SIZE;
    buffer->current = p;  /* NB, this can end up with current pointing to the
                                   * element past end. */
    return items;
}

This doesn't involve any file operations, it's just a way of making your
buffer act like a file for vorbisfile's sake.  The above is slightly flaky
(but could be improved), both COUNT and SIZE could be big enough
that their product overflows a size_t, but you couldn't allocate a buffer
that big anyway.  If for some other reason you can't use vorbisfile then
see below.

> I put part of the Ogg Voribis file in a buffer, then seek in the buffer, and
> I can find the page with a
> complete packet, but I don't know how to get the exactly location of the
> page or the packet. I want to
> use the ogg_page_granulepos() function, but according to the documentation
> provided by xiph.org, it is
> an absolute position within the original uncompressed data.
>

I don't believe there's an easy way to do what you seem to want.
I think this is roughly what you need to do:
Find out using ogg_page_granulepos which pages sit around the
sample (granulepos) you want to go to, this involves doing some
jumping around in the data (bisection search) and checking the
granulepos.  Once you've got approximately the right place start
pulling packets out (possibly keeping a one packet buffer to make
sure you don't overshoot your sample), until you find the packet
with the right granulepos to start decoding from.

> If I can get bytes number before this page, then how can I get the packet
> location?
>

You can't really get the offset to a particular page unless you've
been keeping track of them.  Once you've found the page your
packet starts on (or even one or two before it), it's time to start
pulling out packets sequentially until you've found the one you
want.

-- 
imalone


More information about the Vorbis-dev mailing list