[Vorbis-dev] Vorbis bistream definition / separation from ogg

craig duncan craig-duncan at earthlink.net
Thu Apr 28 11:13:57 PDT 2005


Ralph Giles wrote:

> Unfortunately it is some work to get the number of samples per frame 
> (packet in vorbis terminology). The two possibly lengths are given as a 
> simple field in the info header, but which on a given data packet uses 
> is looked up through a table from the setup header, using the mode 
> number at the start of the packet as an index. So you have to do a fast 
> parse of the setup header to be able to determine this without doing a 
> full decode.
> 
> Hope that helps,
>  -r

Here's the function i used to get the number of samples in a vorbis packet
(as an aside, i see that the vorbis "frame" being referred to is being
taken as synonymous with a vorbis packet... which isn't how i was
thinking of it).

typedef struct _vStream
{
    ogg_stream_state *ogg_stream;
...
    vorbis_info *vi;
} vStream;


static long
_packet_blocksize( vStream *stream, ogg_packet *op )
{
    // This returns (approximately) the number of samples in the packet.
    //
    //   From vorbis_synthesis_blockin():
    //   codec_setup_info *ci = vi->codec_setup;
    //     if ( vd->granulepos != -1 )
    //        vd->granulepos += ci->blocksizes[vd->lW]/4 + ci->blocksizes[vd->W]/4;

    long ret, thisW = vorbis_packet_blocksize( stream->vi, op );
    if ( thisW <= 0 )  // Probably returned OV_ENOTAUDIO
       return 0;

    if ( stream->prevW == 0 )
       stream->prevW = thisW;

    ret = ( thisW + stream->prevW ) / 4;
    stream->prevW = thisW;

    if ( ret == 0 )
       printf("blocksize of 0\n");
    return ret;
}


And you have to call `vorbis_info_init' on the vorbis_info structure
first to get that structure properly initialized.
i.e.
    _vStream *s = ...

    s->vi = malloc( sizeof(vorbis_info) );
    vorbis_info_init( s->vi );


More information about the Vorbis-dev mailing list