Skip to main content

BoolDecoder

Struct BoolDecoder 

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

VP8 boolean entropy decoder state per RFC 6386 §7.3 (bool_decoder).

The decoder owns the input slice for its partition and walks it from front to back. Eight bits at a time are shifted into the value register during renormalisation; consumers never touch the underlying bytes directly.

§Invariants

After BoolDecoder::init returns Ok(_) and between any pair of successful BoolDecoder::read_bool / BoolDecoder::read_literal calls:

  • range is in 128..=255 (RFC 6386 §7.2);
  • value is < (range << 8) interpreted with the unread tail of the partition appended on the right.

Implementations§

Source§

impl<'a> BoolDecoder<'a>

Source

pub fn init(input: &'a [u8]) -> Result<Self, BoolDecoderError>

Initialise the decoder from the start of a partition.

Per RFC 6386 §7.3 init_bool_decoder the first two bytes of the partition are loaded into value, range starts at 255, and the bit-shift counter is zero.

Source

pub fn init_partition(input: &'a [u8]) -> Self

Initialise the decoder from a possibly-undersized partition.

Mirrors the §20 reference’s init_bool_decoder, which tolerates sz < 2 by zero-initialising the value register and presenting an empty input. Tiny VP8 frames where the DCT partition rounds down to a 0- or 1-byte slice can land here: the surrounding macroblock layer reads only mb_skip = 1 flags from this partition, which is satisfiable from the value = 0 initial state.

Callers reading the §19.2 control partition or the §9.1 frame header should keep using BoolDecoder::init, which rejects truncated input as an error (every valid VP8 frame’s control partition is at least 2 bytes).

Source

pub fn read_bool(&mut self, prob: u8) -> Result<bool, BoolDecoderError>

Read one boolean with probability prob / 256 of being 0.

Returns Ok(false) for a coded zero and Ok(true) for a coded one. prob must lie in 1..=255 per the encoder’s contract (RFC 6386 §7.2); a probability of zero or 256 has no meaning because split would degenerate.

Source

pub fn read_literal(&mut self, num_bits: u32) -> Result<u32, BoolDecoderError>

Read an unsigned num_bits-wide literal — num_bits flag bits at fixed probability 128, MSB first.

Per the RFC 6386 §7.3 read_literal convenience function, the returned value is the integer assembled left-to-right from the individual flag reads. num_bits must be in 0..=32; values outside that range are rejected via the bit_count field’s natural width.

Source

pub fn read_signed_literal( &mut self, num_bits: u32, ) -> Result<i32, BoolDecoderError>

Read a signed num_bits-wide literal. A leading sign bit (probability 128) is followed by num_bits - 1 magnitude bits when num_bits > 0; num_bits == 0 returns 0. Per the reference pseudocode the sign is realised by initialising the accumulator to -1 when the sign bit is set.

Source

pub fn range(&self) -> u32

Current range register. Exposed for testing / auditing only.

Source

pub fn value(&self) -> u32

Current value register. Exposed for testing / auditing only.

Source

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

Bytes of the partition not yet shifted into value.

Trait Implementations§

Source§

impl<'a> Clone for BoolDecoder<'a>

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<'a> Debug for BoolDecoder<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for BoolDecoder<'a>

§

impl<'a> Freeze for BoolDecoder<'a>
where &'a [u8]: Freeze,

§

impl<'a> Move for BoolDecoder<'a>

§

impl<'a> RefUnwindSafe for BoolDecoder<'a>

§

impl<'a> Send for BoolDecoder<'a>
where &'a [u8]: Send,

§

impl<'a> Sync for BoolDecoder<'a>

§

impl<'a> Unpin for BoolDecoder<'a>
where &'a [u8]: Unpin,

§

impl<'a> UnsafeUnpin for BoolDecoder<'a>
where &'a [u8]: UnsafeUnpin,

§

impl<'a> UnwindSafe for BoolDecoder<'a>
where &'a [u8]: 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> 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 = !

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.