Skip to main content

RangeEncoder

Struct RangeEncoder 

Source
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

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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

Source§

fn clone(&self) -> RangeEncoder

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 Debug for RangeEncoder

Source§

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

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

impl Default for RangeEncoder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.