Skip to main content

BitWriter

Struct BitWriter 

Source
pub struct BitWriter { /* private fields */ }
Expand description

A growable, MSB-first bit buffer.

Uses a bit cache: bits accumulate in the low nbits positions of a 64-bit cache (the most-recently-written bit is least-significant within the valid region), and whole bytes are flushed off the top. A multi-bit write is a shift

  • OR + a short byte-flush loop — not one operation per bit. After every public write the invariant nbits < 8 and “bits above nbits are zero” hold.

Implementations§

Source§

impl BitWriter

Source

pub fn new() -> Self

Creates an empty writer.

Source

pub fn with_capacity(bytes: usize) -> Self

Creates a writer with bytes pre-reserved — for the per-frame slice writer, so the CAVLC hot loop never pays a Vec realloc mid-frame.

Source

pub fn bit_len(&self) -> usize

Number of bits written so far.

Source

pub fn is_byte_aligned(&self) -> bool

true if the writer is on a byte boundary.

Source

pub fn write_bits(&mut self, value: u32, n: u32)

Writes the low n bits of value, most-significant first (u(n)). n <= 32.

Keeps up to 31 pending bits in cache and flushes a whole u32 word (4 bytes) at a time, like openh264’s BsWriteBits (WRITE_BE_32) — a quarter the Vec writes of byte-at-a-time flushing.

Source

pub fn write_bit(&mut self, bit: bool)

Writes a single bit (true => 1).

Source

pub fn append(&mut self, other: &BitWriter)

Appends every bit of other, in order, at this writer’s current position.

Neither writer need be byte-aligned. This is what lets a macroblock be encoded into a scratch writer before the syntax that must precede it is known (mb_skip_run is only decided once the macroblock’s own cost is), so the encoder can commit one encode rather than trialing and repeating it. Sound for CAVLC because a macroblock’s Exp-Golomb/VLC syntax does not depend on its bit position; an arithmetic coder (CABAC) could NOT be spliced this way, since its state is carried across the whole slice.

Source

pub fn write_ue(&mut self, value: u32)

Unsigned Exp-Golomb code ue(v).

Source

pub fn write_se(&mut self, value: i32)

Signed Exp-Golomb code se(v): 0->0, 1->1, -1->2, 2->3, -2->4, ....

Source

pub fn rbsp_trailing_bits(&mut self)

Writes the rbsp_trailing_bits(): a stop bit 1 then zero-pad to a byte.

Source

pub fn align_zero(&mut self)

Pads with zero bits to the next byte boundary (no stop bit), then flushes the pending whole bytes from the cache (the word-flush in write_bits can leave up to 31 pending bits).

Source

pub fn into_bytes(self) -> Vec<u8>

Consumes the writer, returning the byte buffer. Flushes any whole bytes still pending in the cache; panics if a sub-byte remainder is left (call rbsp_trailing_bits or align_zero first).

Source

pub fn as_bytes(&self) -> &[u8]

Borrows the completed bytes (excludes bits still pending in the cache).

Trait Implementations§

Source§

impl Clone for BitWriter

Source§

fn clone(&self) -> BitWriter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitWriter

Source§

fn default() -> BitWriter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.