Dali: Streams -- C API

[ Header Files | Types | Constants | BitStream | BitParser | BitStreamFilter | Input/Output | See Also ]

Header Files

#include <dvmbasic.h>

Type Definitions

BitStream

A BitStream is a memory buffer containing raw binary data. It is most often used as an I/O buffer.

    typedef struct BitStream {
        unsigned char *buffer;
        unsigned char *endDataPtr;
        unsigned char *endBufPtr;
        unsigned char isVirtual;
        int size;
    } BitStream;
  
buffer
pointer to the first byte in the BitStream memory buffer.
endBufPtr
pointer to the last allocated byte in the BitStream memory buffer.
endDataPtr
pointer to the last data byte in the memory buffer. (If we have a 100 byte buffer, but store only 80 bytes of data inside, endDataPtr will point to the 80-th byte while endBufPtr will point to the 100-th byte.)
isVirtual
1 iff the BitStream is created by casting from another type.
0 otherwise.
size
the size of the BitStream, equivalent to endBufPtr - buffer.

BitParser

A BitParser maintains a cursor into a BitStream. Reading and writing to and from a BitStream are usually done via a BitParser.

    typedef struct BitParser {
        BitStream *bs;
        unsigned char *offsetPtr;
        int currentBits;
        int bitCount;
    } BitParser;
  
bs
pointer to the BitStream the BitParser is attached to.
offsetPtr
pointer to the next unconsumed byte inside the BitStream.
currentBits
32-bits data in the BitStream just before offsetPtr.
bitCount
number of unconsumed bits in the currentBits.

FilterEntry

A FilterEntry is a pair (offset, length) identifying a segment of a BitStream.

    typedef struct FilterEntry {
        unsigned int offset;
        unsigned int length;
    } FilterEntry;
  
offset
offset of a segment from the beginning of the input.
length
length of a segment in the input.

BitStreamFilter

A BitStreamFilter consists of an array of FilterEntrys which defines a sequence of segments in a BitStream (i.e., it's a scatter/gather vector). It also maintains enough state to perform a scan over these segments using multiple function calls. This is an essential feature for large BitStreams.

For example, a BitStreamFilter can be used to define the video stream within an MPEG system stream.

    typedef struct BitStreamFilter {
        int maxEntry;
        int lastEntry;
        int currEntry;
        int currOffset;
        int currLength;
        FilterEntry *table;
    } BitStreamFilter;
  
maxEntry
The number of entries allocated in the filter (i.e., the size of table).
lastEntry
The index (in table) of the last entry added to the filter.
table
The array of filter entries.
currEntry
The next three slots are needed when a filter is used to copy data from a source over multiple calls. This can get tricky because only part of a segment may get copied during one call. These 3 slots maintain sufficient state so that the next call works correctly.

The following simplified pseudo-code shows how a filter is used to read N bytes from source (assuming the source is a file):

         while (currLength < N) {
             Seek to position currOffset within source;
             read currLength bytes from source into a BitStream;
             N = N - currLength;
             currEntry++;
             currLength = table[currEntry].length;
             currOffset = table[currEntry].offset;
         }
         if (N != 0) {
             Seek to position currOffset within source;
             read N bytes from source into a BitStream;
             currLength -= N;
             currOffset += N;
         }
         

CurrLength bytes are copied from the source, starting at offset currOffset from the beginning of the source. For example, if currLength is 32,currOffset is 2500, and the source is a file, the next call will first copy 32 bytes, starting at 2500 bytes into the file.

CurrEntry is the index (in table) of the entry that will be first accessed during the next call to copy data. If part of that entry was accessed during the previous call, the rest of it will be read during the next call.

currOffset
If the last call using the filter resulted in a partial read, currOffset stores the offset from the beginning of the source where data should be taken from.
currLength
If the last call using the filter resulted in a partial read, currLength stores the amount of data (in bytes) that should be copied from the source during the next read to complete this segment.

Constants

Return Codes

DVM_STREAMS_OK indicates that a bitstream operation is successful. DVM_STREAM_FILTER_FULL is returned if we are trying to add entries into a full BitStreamFilter.

    #define DVM_STREAMS_OK 0
    #define DVM_STREAMS_FILTER_FULL -1
  

Operators

BitStream

BitStream *BitStreamNew(int size)

BitStream *BitStreamMmapReadNew (char *fileName)

void BitStreamMmapReadFree (BitStream *bs)

void BitStreamFree(BitStream *bs)

void BitStreamShift(BitStream *bs, int offset)

void BitStreamResize(BitStream *bs, int size)

void BitStreamShareBuffer (BitStream *src, BitStream *dest)

int BitStreamBytesLeft(BitStream *bs, int off)

int BitStreamDump (BitStream *inbs, int inOff, BitStream *outbs, int outOff, int len)

int BitStreamDumpSegments (BitStream *inbs, int inOff, BitStream *outbs, int outOff, int len, int skip, int count)


BitParser

BitParser *BitParserNew ()

void BitParserFree (BitParser *bp)

void BitParserWrap (BitParser *bp, BitStream *bs)

int BitParserTell(BitParser *bp)

void BitParserSeek(BitParser *bp, int off)


BitStreamFilter

BitStreamFilter *BitStreamFilterNew(int size)

void BitStreamFilterResize(BitStreamFilter *filter, unsigned int size)

void BitStreamFilterFree(BitStreamFilter *filter)

int BitStreamFilterAdd(BitStreamFilter *filter, unsigned int offset, unsigned int length)

void BitStreamFilterRead(FILE *file, BitStreamFilter *filter)
void BitStreamFilterWrite(FILE *file, BitStreamFilter *filter)

void BitStreamFilterStartScan(BitStreamFilter *filter)

int BitStreamDumpUsingFilter(BitStream *src, int srcOff, BitStream *dest, int destOff, int length, BitStreamFilter *index)


Input/Output

int BitStreamFileRead ( BitStream *bs, FILE *f, int offset)

int BitStreamFileReadSegment ( BitStream *bs, FILE *f, int offset, int length)

int BitStreamFileReadSegments ( BitStream *bs, FILE *f, int offset, int size, int skip, int times)

int BitStreamFileFilterIn ( BitStream *bs, FILE *f, int offset, BitStreamFilter *filter)

int BitStreamFileWrite (BitStream *bs, FILE *f, int offset)

int BitStreamFileWriteSegment (BitStream *bs, FILE*f, int offset, int size)

int BitStreamFileWriteSegments ( BitStream *bs, FILE *f, int offset, int length, int skip, int times)


See Also

BitParser API


Last updated : Saturday, November 14, 1998, 07:50 PM