[vorbis-dev] Uncompressed size in bytes + memory buffer

Andrew Csikvari csiki at mail.datanet.hu
Tue Jul 29 23:38:19 PDT 2003



>I think he means a predefined set of callbacks for reading from a
memory 
>buffer, just like there are predefined callbacks currently for reading 
>from a file.
Yes.

>Also, speaking of stream length, make sure you implement the full
'seek' 
>and 'tell' callbacks -- the library needs to be able to know where the 
>compressed stream ends in order to figure out where the uncompressed 
>stream ends (although it goes through a little more work than that to 
>handle e.g. chained bitstreams).

Yes, I made it:

    struct MemBuffer {
        uint8*  data;
        uint32  pos;
        uint32  size;
    };

    void MBInit(MemBuffer* pb, uint8* pdata, uint32 psize)
    {
        pb->data=pdata; pb->size=psize; pb->pos=0;
    }

    size_t MBRead(void *ptr, size_t size, size_t nmemb, void
*datasource)
    { 
        MemBuffer& mb=*(MemBuffer*) datasource;
        uint32 toread=size*nmemb;
        if (mb.pos+toread<=mb.size) {
            memcpy(ptr, mb.data+mb.pos, toread); mb.pos+=toread;
            return nmemb;
        }
        uint32 count=(mb.size-mb.pos)/size;
        toread=size*count;
        memcpy(ptr, mb.data+mb.pos, toread); mb.pos+=toread;
        return count;
    }

    int MBSeek(void *datasource, ogg_int64_t offset, int whence)
    {
        MemBuffer& mb=*(MemBuffer*) datasource;
        uint32 npos;
        if (whence==SEEK_SET) npos=(uint32) offset; else
        if (whence=SEEK_CUR)  npos=mb.pos+(int32) offset; else
        if (whence==SEEK_END) npos=mb.size+(int32) offset; else return
-1;
        if (npos>mb.size) return -1;
        mb.pos=npos;
        return 0;
    }

    int MBClose(void *datasource)
    {
        return 0;
    }

    long MBTell(void *datasource)
    {
        return ((MemBuffer*) datasource)->pos;
    }

    ov_callbacks MemBufferCallbacks = { MBRead, MBSeek, MBClose, MBTell
};

I hope, this is good. :)
This is a C++ code, but can be converted to C easily.

Another question:
ov_open and ov_open_callbacks support to give them the beginning of the
file from memory (char* initial, long ibytes).
Is it possible to give the whole file through this (the memory buffer
and it's size) and make a more simplier ov_callback
(just to tell that we are at the end of the file), or there is/will be a
limit to the ibytes parameter?
Will the vorbis think from it, that the file is unseekable?

Thanks,
   Csiki

<p>--- >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-dev-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-dev mailing list