Skip to main content

BitWriter

Struct BitWriter 

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

Packs bits MSB-first into a growable byte buffer.

The first bit written lands in bit 7 (the most-significant bit) of byte 0, matching the bit order every video/image bitstream uses. Partial trailing bits stay in an internal accumulator until a byte fills or the writer is byte-aligned.

Implementations§

Source§

impl BitWriter

Source

pub fn new() -> Self

A new, empty writer.

Source

pub fn with_capacity(bytes: usize) -> Self

A writer preallocated for roughly bytes bytes of output.

Source

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

Append a single bit (only the low bit of bit is used).

Source

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

Append the low n bits of value, MSB-first. n must be <= 32.

Source

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

Append the low n bits of a 64-bit value, MSB-first. n must be <= 64.

Source

pub fn flag(&mut self, b: bool)

Append a boolean as a u(1) flag.

Source

pub fn write_byte(&mut self, b: u8)

Append a whole byte. Fast when byte-aligned (the common case for raw PCM samples); falls back to bit-by-bit otherwise.

Source

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

Unsigned Exp-Golomb, ue(v) (H.265 §9.2). Encodes value as leadingZeros 0s, a 1, then the leadingZeros-bit suffix of value + 1.

Source

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

Signed Exp-Golomb, se(v) (H.265 §9.2). Maps value to a ue(v) codeNum via the standard zig-zag (0→0, 1→1, -1→2, 2→3, -2→4, …).

Source

pub fn bit_len(&self) -> usize

Total number of bits written so far.

Source

pub fn is_byte_aligned(&self) -> bool

Whether the next bit would start a fresh byte.

Source

pub fn align_zero(&mut self)

Pad to the next byte boundary with 0 bits (no-op if already aligned).

Source

pub fn finish(self) -> Vec<u8> ⓘ

Consume the writer, padding any partial final byte with zeros, and return the bytes.

Source

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

Borrow the completed bytes; panics unless byte-aligned. Prefer finish unless you must keep writing.

Trait Implementations§

Source§

impl Clone for BitWriter

Source§

fn clone(&self) -> Self

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() -> Self

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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.