Skip to main content

BitReader

Struct BitReader 

Source
pub struct BitReader<'a> { /* private fields */ }
Expand description

MSB-first bit reader over a borrowed byte slice.

Implementations§

Source§

impl<'a> BitReader<'a>

Source

pub fn new(bytes: &'a [u8]) -> Self

Create a fresh reader positioned at bit 0 of bytes.

Source

pub fn total_bits(&self) -> usize

Total number of payload bits in the reader.

Source

pub fn bit_pos(&self) -> usize

Current absolute bit offset.

Source

pub fn bits_remaining(&self) -> usize

Number of unread bits (saturating at zero).

Source

pub fn at_end(&self) -> bool

True if the next read would fall past the end of the buffer.

Source

pub fn byte_aligned(&self) -> bool

True if the reader is currently byte-aligned.

Source

pub fn skip(&mut self, n: usize)

Skip forward by n bits.

Source

pub fn align_to_byte(&mut self)

Skip forward to the next byte boundary if not already aligned.

Source

pub fn read_bit(&mut self) -> u32

Read one bit. Returns 0 past the end.

Source

pub fn u(&mut self, n: u32) -> u32

Read n bits MSB first into a u32. Past-the-end bits are 0. n must be ≤ 32 (the upper limit covers every fixed-width field used by H.264 / HEVC / AV1 OBU headers).

Source

pub fn u64(&mut self, n: u32) -> u64

Read n bits MSB first into a u64 (for AV1’s 64-bit reference_frame_id-class fields).

Source

pub fn peek_bits(&self, n: u32) -> u32

Peek the next n bits MSB-first without advancing bit_pos. n must be ≤ 32. Past-the-end bits are zero, identical to BitReader::u’s contract. Useful for codec parsers that need to inspect a marker bit before deciding whether to commit to a branch.

Source

pub fn more_rbsp_data(&self) -> bool

H.264 §7.2 / H.265 §7.2 / H.266 §7.2 more_rbsp_data().

Returns true if there is at least one more RBSP data bit before the rbsp_trailing_bits() marker. The marker is a 1 bit (the rbsp_stop_one_bit) followed by zero or more 0 bits up to the next byte boundary, sitting at the end of the RBSP. The algorithm: search forward from the current position for any 1 bit after the next 1 — if we find one, the next 1 was not the stop marker and there is more data.

Returns false once positioned at or past the stop bit, and at end-of-stream.

Source

pub fn read_rbsp_trailing_bits(&mut self) -> Result<(), BitstreamError>

H.264 §7.3.2.11 / H.265 §7.3.2.11 / H.266 §7.3.10 rbsp_trailing_bits(). Consumes a 1 (rbsp_stop_one_bit) followed by zero or more 0 bits up to the next byte boundary. Returns Err(InvalidData) if the next bit is not 1, if any of the padding bits before the boundary is not 0, or if the stream ends before the marker is found.

Source

pub fn ue(&mut self) -> Result<u32, BitstreamError>

Unsigned Exp-Golomb (ue(v)). H.264 9.1 / H.265 9.2. Returns an error if 32 or more leading zeros are seen: the largest representable ue(v) value is u32::MAX - 1 (31 leading zeros), so 32 leading zeros has no valid u32 value and the 1 << leadingZeros term would overflow.

Source

pub fn se(&mut self) -> Result<i32, BitstreamError>

Signed Exp-Golomb (se(v)). H.264 9.1.1 / H.265 9.2.2.

Auto Trait Implementations§

§

impl<'a> Freeze for BitReader<'a>

§

impl<'a> RefUnwindSafe for BitReader<'a>

§

impl<'a> Send for BitReader<'a>

§

impl<'a> Sync for BitReader<'a>

§

impl<'a> Unpin for BitReader<'a>

§

impl<'a> UnsafeUnpin for BitReader<'a>

§

impl<'a> UnwindSafe for BitReader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.