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:
rangeis in128..=255(RFC 6386 §7.2);valueis< (range << 8)interpreted with the unread tail of the partition appended on the right.
Implementations§
Source§impl<'a> BoolDecoder<'a>
impl<'a> BoolDecoder<'a>
Sourcepub fn init(input: &'a [u8]) -> Result<Self, BoolDecoderError>
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.
Sourcepub fn init_partition(input: &'a [u8]) -> Self
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).
Sourcepub fn read_bool(&mut self, prob: u8) -> Result<bool, BoolDecoderError>
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.
Sourcepub fn read_literal(&mut self, num_bits: u32) -> Result<u32, BoolDecoderError>
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.
Sourcepub fn read_signed_literal(
&mut self,
num_bits: u32,
) -> Result<i32, BoolDecoderError>
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.
Sourcepub fn remaining_input(&self) -> &[u8] ⓘ
pub fn remaining_input(&self) -> &[u8] ⓘ
Bytes of the partition not yet shifted into value.