pub struct RangeEncoder { /* private fields */ }Expand description
Bit-exact CELT/SILK range encoder state per RFC 6716 §5.1.
The state four-tuple (val, rng, rem, ext) from §5.1 is carried
directly: val is the low end of the current range, rng its size,
rem a single buffered non-propagating output byte (or -1 for
“none yet”), and ext a count of pending carry-propagating (255)
output bytes. Range-coder bytes accumulate front-to-back in buf;
raw bits (§5.1.3) accumulate back-to-front in end_bytes /
end_window and are appended as the buffer tail at Self::finish.
Implementations§
Source§impl RangeEncoder
impl RangeEncoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Initialize the range encoder per RFC 6716 §5.1.
The state vector is (val, rng, rem, ext) = (0, 2**31, -1, 0).
nbits_total starts at 33 so that Self::tell reports the
same value as a freshly-initialized decoder (which reaches
nbits_total == 33, rng == 2**31, tell() == 1 after its
§4.1.1 initialization normalize).
Sourcepub fn tell(&self) -> u32
pub fn tell(&self) -> u32
Whole-bit budget produced so far, per RFC 6716 §5.1.6 / §4.1.6.1.
Equal to nbits_total - ilog(rng) + nbits_raw; matches the
decoder’s tell() after the same symbols.
Sourcepub fn tell_frac(&self) -> u32
pub fn tell_frac(&self) -> u32
1/8th-bit-precision budget produced so far, per RFC 6716 §5.1.6 /
§4.1.6.2. Matches the decoder’s tell_frac() after the same
symbols.
Sourcepub fn encode(&mut self, fl: u32, fh: u32, ft: u32)
pub fn encode(&mut self, fl: u32, fh: u32, ft: u32)
Encode symbol k described by the three-tuple (fl, fh, ft),
per RFC 6716 §5.1.1 (ec_encode).
Requires 0 <= fl < fh <= ft and 1 <= ft <= 2**16. The §5.1.1
update narrows the range to the symbol’s [fl, fh) sub-interval
of [0, ft). The fl == 0 branch subtracts (rng/ft)*(ft - fh)
from rng so the top symbol absorbs the integer-division
remainder — the exact mirror of the decoder’s §4.1.2 update, which
is what keeps encoder rng equal to decoder rng.
Sourcepub fn encode_bin(&mut self, fl: u32, fh: u32, ftb: u32)
pub fn encode_bin(&mut self, fl: u32, fh: u32, ftb: u32)
Encode symbol k for a power-of-two total ft = 1<<ftb, per
RFC 6716 §5.1.2.1 (ec_encode_bin). Division-free equivalent of
Self::encode with ft = 1<<ftb.
Sourcepub fn enc_bit_logp(&mut self, bit: bool, logp: u32)
pub fn enc_bit_logp(&mut self, bit: bool, logp: u32)
Encode a single binary symbol whose “1” has probability
2**-logp, per RFC 6716 §5.1.2.2 (ec_enc_bit_logp).
Equivalent to ec_encode with (fl, fh, ft) equal to
(0, (1<<logp)-1, 1<<logp) for a 0 and
((1<<logp)-1, 1<<logp, 1<<logp) for a 1. Multiplication- and
division-free.
Sourcepub fn enc_icdf(&mut self, k: usize, icdf: &[u8], ftb: u32)
pub fn enc_icdf(&mut self, k: usize, icdf: &[u8], ftb: u32)
Encode symbol index k against an inverse-CDF table, per
RFC 6716 §5.1.2.3 (ec_enc_icdf). Uses the SAME icdf[] tables
as the decoder’s crate::range_decoder::RangeDecoder::dec_icdf:
icdf[j] stores (1<<ftb) - fh[j], terminated by a 0 entry.
Per §5.1.2.3, fl[k] = (1<<ftb) - icdf[k-1] (or 0 if k == 0),
fh[k] = (1<<ftb) - icdf[k], ft = 1<<ftb. The symbol update
then depends only on the icdf[] differences, so no total is
needed.
Sourcepub fn enc_bits(&mut self, value: u32, bits: u32)
pub fn enc_bits(&mut self, value: u32, bits: u32)
Encode bits raw bits (low bits of value), per RFC 6716
§5.1.3 (ec_enc_bits). Raw bits are packed at the END of the
output buffer, LSB-first, mirroring the decoder’s back-to-front
reader. The first bit emitted here is the one the decoder reads
first.
Sourcepub fn enc_uint(&mut self, t: u32, ft: u32)
pub fn enc_uint(&mut self, t: u32, ft: u32)
Encode one of ft equiprobable values t in 0..ft, per
RFC 6716 §5.1.4 (ec_enc_uint). ft may be as large as
2**32 - 1. Values ft <= 1 degenerate to a no-op (matching the
decoder returning the constant 0).
Sourcepub fn finish(self) -> Vec<u8> ⓘ
pub fn finish(self) -> Vec<u8> ⓘ
Finalize the stream (§5.1.5, ec_enc_done) and return the packed
output bytes.
Chooses the terminating code value end inside [val, val + rng)
with the most trailing zero bits b such that
end + (1<<b) - 1 is still in the interval, flushes it through
the carry buffer, then appends the raw-bit region (§5.1.3) as a
disjoint tail, separated from the range data by a zero pad so the
decoder’s forward range reader can zero-extend past the committed
bytes without consuming a raw byte.
Trait Implementations§
Source§impl Clone for RangeEncoder
impl Clone for RangeEncoder
Source§fn clone(&self) -> RangeEncoder
fn clone(&self) -> RangeEncoder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more