Boost logo

Boost :

Subject: [boost] Any interest in bitstream class?
From: Paul Long (plong_at_[hidden])
Date: 2013-06-27 21:44:19


Same semantics as, e.g., stringstream, but it operates on binary data. I
already have the analogues to istringstream/stringbuf (you can take a
look at ibitstream/bitbuf at https://github.com/dplong/bstream) There
are some interesting features, but I won't go into them here. If there
is interest, I would implement obitstream and bitstream and possibly
support Boost::dynamic_bitset.

The rationale is _succinct_ expression of predictive parsing of binary data.

I've used ibitstream in production code for decoding RTP headers and
frames of various video encodings. Below is a simple function that
decodes an RTP header using ibitstream; I also have a GitHub repo that
contains more complex code for high-level parsing of H.264 frames at
https://github.com/dplong/rtspudph264

struct RtpHeader {
     bool padding, marker;
     bitset<7> payloadType;
     WORD sequenceNumber;
     DWORD timestamp, ssrcIdentifier;
     vector<DWORD> csrcIdentifier;
     struct {
         bool present;
         WORD identifier;
         vector<BYTE> contents;
     } extension;
};

bool ParseRtpHeader(const BYTE *buffer, RtpHeader &rtp)
{
     ibitstream bin(buffer);
     static const bitset<2> version(0x2);
     bitset<4> csrcCount;
     WORD extensionLength;

     bin >> version >> rtp.padding >> rtp.extension.present
>> csrcCount >> rtp.marker >> rtp.payloadType
>> rtp.sequenceNumber >> rtp.timestamp >> rtp.ssrcIdentifier
>> setrepeat(csrcCount.to_ulong()) >> rtp.csrcIdentifier;

     if (rtp.extension.present) {
         bin >> rtp.extension.identifier >> extensionLength
>> setrepeat(extensionLength * sizeof(DWORD))
>> rtp.extension.contents;
     }

     return bin.good();
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk