TSDuck Version 3.16-1064 (TSDuck - The MPEG Transport Stream Toolkit)
ts::BitStream Class Reference

Class to analyze a bit-stream in memory. More...

Public Member Functions

 BitStream ()
 Default constructor. More...
 
 BitStream (const void *data, size_t size_in_bits, size_t bit_offset_in_first_byte=0)
 Constructor using a memory area which must remain valid as long as the BitStream object is used. More...
 
 BitStream (const BitStream &bs)
 Copy constructor. More...
 
void back (size_t n)
 Back n bits. More...
 
bool byteAligned () const
 Check if the current bit pointer is on a byte boundary. More...
 
size_t currentBitOffset () const
 Get current bit position. More...
 
bool endOfStream () const
 Check end of stream. More...
 
bool isAssociated () const
 Check if this object is currently associated with a memory area. More...
 
BitStreamoperator= (const BitStream &bs)
 Assignment (use the same buffer). More...
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value >::type * = nullptr>
INT read (size_t n, INT def=0)
 Read the next n bits as an integer value and advance the bitstream pointer. More...
 
uint8_t readBit (uint8_t def=0)
 Read the next bit and advance the bitstream pointer. More...
 
size_t remainingBitCount () const
 Get number of remaining bits. More...
 
void reset (const void *data, size_t size_in_bits, size_t bit_offset_in_first_byte=0)
 Reset with a memory area which must remain valid as long as the BitStream object is used. More...
 
void seek (size_t bit_offset)
 Reset parsing at the specified bit offset. More...
 
void skip (size_t n)
 Skip bits. More...
 
void skipToNextByte ()
 Advance pointer to next byte boundary.
 

Detailed Description

Class to analyze a bit-stream in memory.

An instance of this class is used to analyze a continuous bit stream in memory, ignoring byte boundaries. The bit-stream can be read bit by bit. Integer values of any size can be read, regardless of alignment.

The order of which the bits are read is the following: The bytes are read in increasing order of address. The bits within each byte are read from the most significant one to the least significant one.

When multi-bit integer values are extracted, they are interpreted in most significant bit first order (MSB first). But the integer value is returned in native byte order.

An instance of BitStream is an analyzer, it does not contain the bit stream data. An instance must be associated to a memory area. This association can be performed in a constructor or using the reset() method.

Constructor & Destructor Documentation

◆ BitStream() [1/3]

ts::BitStream::BitStream ( )
inline

Default constructor.

Object is unusable as long as reset is not invoked.

◆ BitStream() [2/3]

ts::BitStream::BitStream ( const void *  data,
size_t  size_in_bits,
size_t  bit_offset_in_first_byte = 0 
)
inline

Constructor using a memory area which must remain valid as long as the BitStream object is used.

Parameters
[in]dataAddress of data area to analyze.
[in]size_in_bitsSize in bits of the data area.
[in]bit_offset_in_first_byteThe number of initial bits to ignore in the first byte. Default: zero.

◆ BitStream() [3/3]

ts::BitStream::BitStream ( const BitStream bs)
inline

Copy constructor.

The same data area will be analyzed, starting at the current point.

Parameters
[in]bsOther bitstream to copy.

Member Function Documentation

◆ operator=()

BitStream& ts::BitStream::operator= ( const BitStream bs)
inline

Assignment (use the same buffer).

The same data area will be analyzed, starting at the current point.

Parameters
[in]bsOther bitstream to copy.
Returns
A reference to this object.

◆ isAssociated()

bool ts::BitStream::isAssociated ( ) const
inline

Check if this object is currently associated with a memory area.

Returns
True if this object is currently associated with a memory area.

◆ reset()

void ts::BitStream::reset ( const void *  data,
size_t  size_in_bits,
size_t  bit_offset_in_first_byte = 0 
)
inline

Reset with a memory area which must remain valid as long as the BitStream object is used.

Parameters
[in]dataAddress of data area to analyze.
[in]size_in_bitsSize in bits of the data area.
[in]bit_offset_in_first_byteThe number of initial bits to ignore in the first byte. Default: zero.

◆ seek()

void ts::BitStream::seek ( size_t  bit_offset)
inline

Reset parsing at the specified bit offset.

Parameters
[in]bit_offsetOffset of next bit to analyze.

◆ currentBitOffset()

size_t ts::BitStream::currentBitOffset ( ) const
inline

Get current bit position.

Returns
The offset of the current bit from the starting one.

◆ remainingBitCount()

size_t ts::BitStream::remainingBitCount ( ) const
inline

Get number of remaining bits.

Returns
The number of remaining bits.

◆ endOfStream()

bool ts::BitStream::endOfStream ( ) const
inline

Check end of stream.

Returns
True if the end of stream is reached.

◆ byteAligned()

bool ts::BitStream::byteAligned ( ) const
inline

Check if the current bit pointer is on a byte boundary.

Returns
True if the next byte to read is at the beginning of a byte.

◆ skip()

void ts::BitStream::skip ( size_t  n)
inline

Skip bits.

Parameters
[in]nNumber of bits to skip.

◆ back()

void ts::BitStream::back ( size_t  n)
inline

Back n bits.

Parameters
[in]nNumber of bits to skip back.

◆ readBit()

uint8_t ts::BitStream::readBit ( uint8_t  def = 0)
inline

Read the next bit and advance the bitstream pointer.

Parameters
[in]defDefault value to return if already at end of stream.
Returns
The value of the next bit.

◆ read()

template<typename INT , typename std::enable_if< std::is_integral< INT >::value >::type * = nullptr>
INT ts::BitStream::read ( size_t  n,
INT  def = 0 
)
inline

Read the next n bits as an integer value and advance the bitstream pointer.

Template Parameters
INTAn integer type for the result.
Parameters
[in]nNumber of bits to read.
[in]defDefault value to return if less than n bits before end of stream.
Returns
The value of the next n bits.

The documentation for this class was generated from the following file: