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
impl BoolEncoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct an encoder with the §7.3 init_bool_encoder initial
state (range = 255, bottom = 0, bit_count = 24).
Sourcepub fn write_bool(&mut self, prob: u8, value: bool)
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.
Sourcepub fn write_literal(&mut self, value: u32, num_bits: u32)
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).
Sourcepub fn write_signed_literal(&mut self, value: i32, num_bits: u32)
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.
Sourcepub fn write_treed<F>(&mut self, tree: &[i8], prob_lookup: F, leaf: u8)
pub fn write_treed<F>(&mut self, tree: &[i8], prob_lookup: F, leaf: 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.
Sourcepub fn finish(self) -> Vec<u8> ⓘ
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.
Sourcepub fn bytes_written(&self) -> usize
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.