Skip to main content

BoolEncoder

Struct BoolEncoder 

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

VP8 boolean (range) encoder per RFC 6386 §7.3.

The structure mirrors the spec’s bool_encoder C type one-for-one: range, bottom, and a per-position bit_count of remaining shifts before the next high-byte output. Encoded bytes accumulate in out.

Implements §7.3 of RFC 6386 (the init_bool_encoder / write_bool / add_one_to_output / flush_bool_encoder C listing the RFC embeds inline).

Implementations§

Source§

impl BoolEncoder

Source

pub fn new() -> Self

Construct an encoder with the §7.3 init_bool_encoder initial state (range = 255, bottom = 0, bit_count = 24).

Source

pub fn write_bool(&mut self, prob: u8, value: bool)

Encode one boolean against the §7.3 probability split formula. prob is the (approximate) probability that value is false, expressed as prob / 256.

The §7.3 listing renormalises one bit at a time: while range < 128 it doubles range, propagates a carry if bit 31 of bottom is set, doubles bottom, and emits the top byte every time bit_count reaches zero. This routine takes the whole renormalisation in one step. The shift count is the number of leading zeros of the 8-bit range (1..=7), and at most one byte boundary falls inside it. Bit 31 can only be set at the doubling that completes a byte: bottom is below 2^24 after a byte is emitted (below 2^8 initially) and gains less than 2^8 between doublings (the splits added while range stays ≥ 128 sum to less than 128), so after k doublings it is below (2^24 + 2^8)·2^k, which stays under 2^31 until the eighth. The carry test is therefore needed exactly once, on the value shifted to just before the boundary. write_bool_matches_bitwise_listing pins the byte stream and the final state against the one-bit loop.

Source

pub fn write_literal(&mut self, value: u32, num_bits: u32)

Encode num_bits flag bits MSB-first at the flat probability of 128 (i.e. the §7.3 L(n) macro), exactly as num_bits iterations of [write_bool](128, …) would.

Specialised register-local loop: at the fixed probability 128 the §7.3 interval split collapses to a pure shift — 1 + (((range - 1) * 128) >> 8) is exactly 1 + ((range - 1) >> 1) (* 2⁷ >> 8 ≡ >> 1, an algebraic identity on the 1..=255 range domain) — and hoisting range / bottom / bit_count into locals lets the whole loop run without touching self except to emit bytes / propagate a carry. State-for-state equality with the generic loop is pinned by write_literal_fast_matches_generic_loop (the decoder-side twin of this specialisation landed in round 306).

Source

pub fn write_signed_literal(&mut self, value: i32, num_bits: u32)

Encode a signed value as L(n) magnitude followed by L(1) sign, the §9.3 / §9.4 / §9.6 idiom. Panics in debug builds if value does not fit in num_bits magnitude bits.

Source

pub fn write_treed<F>(&mut self, tree: &[i8], prob_lookup: F, leaf: u8)
where F: Fn(usize) -> u8,

Write a tree-coded leaf by walking tree to find the path of bits that lands on -leaf, encoding each path bit against prob_lookup(node_index >> 1). This is the §8.1 treed_read walk run in reverse: the decoder’s crate::macroblock treed_read recovers exactly the leaf this method writes, against the same tree and probability lookup.

Used by the §11 macroblock-mode layer (kf_ymode_tree / uv_mode_tree / bmode_tree). Panics in debug builds if leaf is not reachable in tree.

Source

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

Finalise the encoder per §7.3 flush_bool_encoder and return the encoded bytes. Always writes 4 tail bytes (the spec’s c = 4; while (--c >= 0) loop), so the smallest possible partition is 4 bytes — comfortably above the 2-byte minimum crate::bool_decoder::BoolDecoder::init requires.

Source

pub fn bytes_written(&self) -> usize

Number of bytes the encoder has already committed (not counting the four trailing bytes finish will append). Useful for diagnostics; not otherwise consumed by the writer helpers.

Trait Implementations§

Source§

impl Debug for BoolEncoder

Source§

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

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

impl Default for BoolEncoder

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Forget for BoolEncoder

§

impl Freeze for BoolEncoder
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for BoolEncoder

§

impl RefUnwindSafe for BoolEncoder

§

impl Send for BoolEncoder
where RawVecInner: Send,

§

impl Sync for BoolEncoder
where RawVecInner: Sync,

§

impl Unpin for BoolEncoder
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for BoolEncoder

§

impl UnwindSafe for BoolEncoder
where RawVecInner: UnwindSafe,

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.