use crate::bool_decoder::{BoolDecoder, BoolDecoderError};
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodedHeaderError {
BoolDecoder(BoolDecoderError),
}
impl fmt::Display for CodedHeaderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CodedHeaderError::BoolDecoder(inner) => {
write!(f, "vp8 coded header: {inner}")
}
}
}
}
impl std::error::Error for CodedHeaderError {}
impl From<BoolDecoderError> for CodedHeaderError {
fn from(value: BoolDecoderError) -> Self {
CodedHeaderError::BoolDecoder(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UpdateSegmentation {
pub update_mb_segmentation_map: bool,
pub update_segment_feature_data: bool,
pub segment_feature_mode_absolute: bool,
pub quantizer_update: [Option<i16>; 4],
pub loop_filter_update: [Option<i16>; 4],
pub segment_prob: [Option<u8>; 3],
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct MbLfAdjustments {
pub loop_filter_adj_enable: bool,
pub mode_ref_lf_delta_update: bool,
pub ref_frame_delta_update: [Option<i16>; 4],
pub mb_mode_delta_update: [Option<i16>; 4],
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct QuantIndices {
pub y_ac_qi: u8,
pub y_dc_delta: Option<i8>,
pub y2_dc_delta: Option<i8>,
pub y2_ac_delta: Option<i8>,
pub uv_dc_delta: Option<i8>,
pub uv_ac_delta: Option<i8>,
}
pub type TokenProbUpdates = [[[[Option<u8>; 11]; 3]; 8]; 4];
pub const MV_PROB_COUNT: usize = 19;
pub type MvProbUpdates = [[Option<u8>; MV_PROB_COUNT]; 2];
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct Vp8CodedHeader {
pub color_space: Option<bool>,
pub clamping_type: Option<bool>,
pub segmentation_enabled: bool,
pub update_segmentation: Option<UpdateSegmentation>,
pub filter_type: bool,
pub loop_filter_level: u8,
pub sharpness_level: u8,
pub mb_lf_adjustments: MbLfAdjustments,
pub log2_nbr_of_dct_partitions: u8,
pub nbr_of_dct_partitions: u8,
pub quant_indices: QuantIndices,
pub refresh_entropy_probs: bool,
pub refresh_golden_frame: Option<bool>,
pub refresh_alternate_frame: Option<bool>,
pub copy_buffer_to_golden: Option<u8>,
pub copy_buffer_to_alternate: Option<u8>,
pub sign_bias_golden: Option<bool>,
pub sign_bias_alternate: Option<bool>,
pub refresh_last: Option<bool>,
pub token_prob_updates: TokenProbUpdates,
pub mb_no_skip_coeff: bool,
pub prob_skip_false: Option<u8>,
pub prob_intra: Option<u8>,
pub prob_last: Option<u8>,
pub prob_gf: Option<u8>,
pub intra_y_mode_prob_update: Option<[u8; 4]>,
pub intra_uv_mode_prob_update: Option<[u8; 3]>,
pub mv_prob_update: Option<MvProbUpdates>,
}
impl Vp8CodedHeader {
#[must_use]
pub fn with_color_space(mut self, v: Option<bool>) -> Self {
self.color_space = v;
self
}
#[must_use]
pub fn with_clamping_type(mut self, v: Option<bool>) -> Self {
self.clamping_type = v;
self
}
#[must_use]
pub fn with_segmentation_enabled(mut self, v: bool) -> Self {
self.segmentation_enabled = v;
self
}
#[must_use]
pub fn with_update_segmentation(mut self, v: Option<UpdateSegmentation>) -> Self {
self.update_segmentation = v;
self
}
#[must_use]
pub fn with_filter_type(mut self, v: bool) -> Self {
self.filter_type = v;
self
}
#[must_use]
pub fn with_loop_filter_level(mut self, v: u8) -> Self {
self.loop_filter_level = v;
self
}
#[must_use]
pub fn with_sharpness_level(mut self, v: u8) -> Self {
self.sharpness_level = v;
self
}
#[must_use]
pub fn with_mb_lf_adjustments(mut self, v: MbLfAdjustments) -> Self {
self.mb_lf_adjustments = v;
self
}
#[must_use]
pub fn with_log2_nbr_of_dct_partitions(mut self, v: u8) -> Self {
self.log2_nbr_of_dct_partitions = v;
self
}
#[must_use]
pub fn with_nbr_of_dct_partitions(mut self, v: u8) -> Self {
self.nbr_of_dct_partitions = v;
self
}
#[must_use]
pub fn with_quant_indices(mut self, v: QuantIndices) -> Self {
self.quant_indices = v;
self
}
#[must_use]
pub fn with_refresh_entropy_probs(mut self, v: bool) -> Self {
self.refresh_entropy_probs = v;
self
}
#[must_use]
pub fn with_refresh_golden_frame(mut self, v: Option<bool>) -> Self {
self.refresh_golden_frame = v;
self
}
#[must_use]
pub fn with_refresh_alternate_frame(mut self, v: Option<bool>) -> Self {
self.refresh_alternate_frame = v;
self
}
#[must_use]
pub fn with_copy_buffer_to_golden(mut self, v: Option<u8>) -> Self {
self.copy_buffer_to_golden = v;
self
}
#[must_use]
pub fn with_copy_buffer_to_alternate(mut self, v: Option<u8>) -> Self {
self.copy_buffer_to_alternate = v;
self
}
#[must_use]
pub fn with_sign_bias_golden(mut self, v: Option<bool>) -> Self {
self.sign_bias_golden = v;
self
}
#[must_use]
pub fn with_sign_bias_alternate(mut self, v: Option<bool>) -> Self {
self.sign_bias_alternate = v;
self
}
#[must_use]
pub fn with_refresh_last(mut self, v: Option<bool>) -> Self {
self.refresh_last = v;
self
}
#[must_use]
pub fn with_token_prob_updates(mut self, v: TokenProbUpdates) -> Self {
self.token_prob_updates = v;
self
}
#[must_use]
pub fn with_mb_no_skip_coeff(mut self, v: bool) -> Self {
self.mb_no_skip_coeff = v;
self
}
#[must_use]
pub fn with_prob_skip_false(mut self, v: Option<u8>) -> Self {
self.prob_skip_false = v;
self
}
#[must_use]
pub fn with_prob_intra(mut self, v: Option<u8>) -> Self {
self.prob_intra = v;
self
}
#[must_use]
pub fn with_prob_last(mut self, v: Option<u8>) -> Self {
self.prob_last = v;
self
}
#[must_use]
pub fn with_prob_gf(mut self, v: Option<u8>) -> Self {
self.prob_gf = v;
self
}
#[must_use]
pub fn with_intra_y_mode_prob_update(mut self, v: Option<[u8; 4]>) -> Self {
self.intra_y_mode_prob_update = v;
self
}
#[must_use]
pub fn with_intra_uv_mode_prob_update(mut self, v: Option<[u8; 3]>) -> Self {
self.intra_uv_mode_prob_update = v;
self
}
#[must_use]
pub fn with_mv_prob_update(mut self, v: Option<MvProbUpdates>) -> Self {
self.mv_prob_update = v;
self
}
}
impl Vp8CodedHeader {
pub fn parse(partition: &[u8], key_frame: bool) -> Result<Self, CodedHeaderError> {
Self::parse_with_decoder(partition, key_frame).map(|(hdr, _dec)| hdr)
}
pub fn parse_with_decoder<'a>(
partition: &'a [u8],
key_frame: bool,
) -> Result<(Self, BoolDecoder<'a>), CodedHeaderError> {
let mut dec = BoolDecoder::init(partition)?;
let hdr = Self::parse_from_decoder(&mut dec, key_frame)?;
Ok((hdr, dec))
}
fn parse_from_decoder(
dec: &mut BoolDecoder<'_>,
key_frame: bool,
) -> Result<Self, CodedHeaderError> {
let (color_space, clamping_type) = if key_frame {
let cs = dec.read_bool(128)?;
let ct = dec.read_bool(128)?;
(Some(cs), Some(ct))
} else {
(None, None)
};
let segmentation_enabled = dec.read_bool(128)?;
let update_segmentation = if segmentation_enabled {
Some(parse_update_segmentation(dec)?)
} else {
None
};
let filter_type = dec.read_bool(128)?;
let loop_filter_level = dec.read_literal(6)? as u8;
let sharpness_level = dec.read_literal(3)? as u8;
let mb_lf_adjustments = parse_mb_lf_adjustments(dec)?;
let log2_nbr_of_dct_partitions = dec.read_literal(2)? as u8;
let nbr_of_dct_partitions = 1u8 << log2_nbr_of_dct_partitions;
let quant_indices = parse_quant_indices(dec)?;
let (
refresh_golden_frame,
refresh_alternate_frame,
copy_buffer_to_golden,
copy_buffer_to_alternate,
sign_bias_golden,
sign_bias_alternate,
refresh_entropy_probs,
refresh_last,
) = if key_frame {
let refresh_entropy_probs = dec.read_bool(128)?;
(
None,
None,
None,
None,
None,
None,
refresh_entropy_probs,
None,
)
} else {
let refresh_golden_frame = dec.read_bool(128)?;
let refresh_alternate_frame = dec.read_bool(128)?;
let copy_buffer_to_golden = if !refresh_golden_frame {
Some(dec.read_literal(2)? as u8)
} else {
None
};
let copy_buffer_to_alternate = if !refresh_alternate_frame {
Some(dec.read_literal(2)? as u8)
} else {
None
};
let sign_bias_golden = dec.read_bool(128)?;
let sign_bias_alternate = dec.read_bool(128)?;
let refresh_entropy_probs = dec.read_bool(128)?;
let refresh_last = dec.read_bool(128)?;
(
Some(refresh_golden_frame),
Some(refresh_alternate_frame),
copy_buffer_to_golden,
copy_buffer_to_alternate,
Some(sign_bias_golden),
Some(sign_bias_alternate),
refresh_entropy_probs,
Some(refresh_last),
)
};
let token_prob_updates = parse_token_prob_update(dec)?;
let mb_no_skip_coeff = dec.read_bool(128)?;
let prob_skip_false = if mb_no_skip_coeff {
Some(dec.read_literal(8)? as u8)
} else {
None
};
let (
prob_intra,
prob_last,
prob_gf,
intra_y_mode_prob_update,
intra_uv_mode_prob_update,
mv_prob_update,
) = if key_frame {
(None, None, None, None, None, None)
} else {
let prob_intra = dec.read_literal(8)? as u8;
let prob_last = dec.read_literal(8)? as u8;
let prob_gf = dec.read_literal(8)? as u8;
let intra_y_mode_prob_update = if dec.read_bool(128)? {
let mut buf = [0u8; 4];
for slot in &mut buf {
*slot = dec.read_literal(8)? as u8;
}
Some(buf)
} else {
None
};
let intra_uv_mode_prob_update = if dec.read_bool(128)? {
let mut buf = [0u8; 3];
for slot in &mut buf {
*slot = dec.read_literal(8)? as u8;
}
Some(buf)
} else {
None
};
let mv_prob_update = Some(parse_mv_prob_update(dec)?);
(
Some(prob_intra),
Some(prob_last),
Some(prob_gf),
intra_y_mode_prob_update,
intra_uv_mode_prob_update,
mv_prob_update,
)
};
Ok(Vp8CodedHeader {
color_space,
clamping_type,
segmentation_enabled,
update_segmentation,
filter_type,
loop_filter_level,
sharpness_level,
mb_lf_adjustments,
log2_nbr_of_dct_partitions,
nbr_of_dct_partitions,
quant_indices,
refresh_entropy_probs,
refresh_golden_frame,
refresh_alternate_frame,
copy_buffer_to_golden,
copy_buffer_to_alternate,
sign_bias_golden,
sign_bias_alternate,
refresh_last,
token_prob_updates,
mb_no_skip_coeff,
prob_skip_false,
prob_intra,
prob_last,
prob_gf,
intra_y_mode_prob_update,
intra_uv_mode_prob_update,
mv_prob_update,
})
}
}
const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [
[
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255],
[249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255],
[234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255],
[250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255],
[254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
],
[
[
[217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255],
[234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255],
],
[
[255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
],
[
[
[186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255],
[234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255],
[251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255],
],
[
[255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
],
[
[
[248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255],
[248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255],
],
[
[255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255],
[248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255],
[250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
[
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
],
],
];
pub(crate) const MV_UPDATE_PROBS: [[u8; MV_PROB_COUNT]; 2] = [
[
237, 246, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 250, 250, 252, 254,
254,
],
[
231, 243, 245, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 251, 251, 254, 254,
254,
],
];
pub const DEFAULT_MV_CONTEXT: [[u8; MV_PROB_COUNT]; 2] = [
[
162, 128, 225, 146, 172, 147, 214, 39, 156, 128, 129, 132, 75, 145, 178, 206, 239, 254, 254,
],
[
164, 128, 204, 170, 119, 235, 140, 230, 228, 128, 130, 130, 74, 148, 180, 203, 236, 254,
254,
],
];
fn parse_mv_prob_update(dec: &mut BoolDecoder<'_>) -> Result<MvProbUpdates, BoolDecoderError> {
let mut out: MvProbUpdates = [[None; MV_PROB_COUNT]; 2];
for (i, ctx) in out.iter_mut().enumerate() {
for (j, slot) in ctx.iter_mut().enumerate() {
if dec.read_bool(MV_UPDATE_PROBS[i][j])? {
let x = dec.read_literal(7)? as u8;
*slot = Some(if x == 0 { 1 } else { x << 1 });
}
}
}
Ok(out)
}
fn parse_token_prob_update(
dec: &mut BoolDecoder<'_>,
) -> Result<TokenProbUpdates, BoolDecoderError> {
let mut out: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
for (out_plane, prob_plane) in out.iter_mut().zip(COEFF_UPDATE_PROBS.iter()) {
for (out_band, prob_band) in out_plane.iter_mut().zip(prob_plane.iter()) {
for (out_ctx, prob_ctx) in out_band.iter_mut().zip(prob_band.iter()) {
for (slot, &prob) in out_ctx.iter_mut().zip(prob_ctx.iter()) {
let present = dec.read_bool(prob)?;
*slot = if present {
Some(dec.read_literal(8)? as u8)
} else {
None
};
}
}
}
}
Ok(out)
}
#[doc(hidden)]
pub fn bench_parse_token_prob_update(
dec: &mut BoolDecoder<'_>,
) -> Result<TokenProbUpdates, BoolDecoderError> {
parse_token_prob_update(dec)
}
fn read_signed_delta(dec: &mut BoolDecoder<'_>, n: u32) -> Result<i16, BoolDecoderError> {
let magnitude = dec.read_literal(n)? as i16;
let sign = dec.read_bool(128)?;
Ok(if sign { -magnitude } else { magnitude })
}
fn parse_update_segmentation(
dec: &mut BoolDecoder<'_>,
) -> Result<UpdateSegmentation, BoolDecoderError> {
let update_mb_segmentation_map = dec.read_bool(128)?;
let update_segment_feature_data = dec.read_bool(128)?;
let mut segment_feature_mode_absolute = false;
let mut quantizer_update: [Option<i16>; 4] = [None; 4];
let mut loop_filter_update: [Option<i16>; 4] = [None; 4];
if update_segment_feature_data {
segment_feature_mode_absolute = dec.read_bool(128)?;
for slot in &mut quantizer_update {
let present = dec.read_bool(128)?;
*slot = if present {
Some(read_signed_delta(dec, 7)?)
} else {
None
};
}
for slot in &mut loop_filter_update {
let present = dec.read_bool(128)?;
*slot = if present {
Some(read_signed_delta(dec, 6)?)
} else {
None
};
}
}
let mut segment_prob: [Option<u8>; 3] = [None; 3];
if update_mb_segmentation_map {
for slot in &mut segment_prob {
let present = dec.read_bool(128)?;
*slot = if present {
Some(dec.read_literal(8)? as u8)
} else {
None
};
}
}
Ok(UpdateSegmentation {
update_mb_segmentation_map,
update_segment_feature_data,
segment_feature_mode_absolute,
quantizer_update,
loop_filter_update,
segment_prob,
})
}
fn parse_mb_lf_adjustments(dec: &mut BoolDecoder<'_>) -> Result<MbLfAdjustments, BoolDecoderError> {
let loop_filter_adj_enable = dec.read_bool(128)?;
let mut mode_ref_lf_delta_update = false;
let mut ref_frame_delta_update: [Option<i16>; 4] = [None; 4];
let mut mb_mode_delta_update: [Option<i16>; 4] = [None; 4];
if loop_filter_adj_enable {
mode_ref_lf_delta_update = dec.read_bool(128)?;
if mode_ref_lf_delta_update {
for slot in &mut ref_frame_delta_update {
let present = dec.read_bool(128)?;
*slot = if present {
Some(read_signed_delta(dec, 6)?)
} else {
None
};
}
for slot in &mut mb_mode_delta_update {
let present = dec.read_bool(128)?;
*slot = if present {
Some(read_signed_delta(dec, 6)?)
} else {
None
};
}
}
}
Ok(MbLfAdjustments {
loop_filter_adj_enable,
mode_ref_lf_delta_update,
ref_frame_delta_update,
mb_mode_delta_update,
})
}
fn parse_quant_indices(dec: &mut BoolDecoder<'_>) -> Result<QuantIndices, BoolDecoderError> {
let y_ac_qi = dec.read_literal(7)? as u8;
let mut deltas: [Option<i8>; 5] = [None; 5];
for slot in &mut deltas {
let present = dec.read_bool(128)?;
*slot = if present {
let mag = dec.read_literal(4)? as i8;
let sign = dec.read_bool(128)?;
Some(if sign { -mag } else { mag })
} else {
None
};
}
Ok(QuantIndices {
y_ac_qi,
y_dc_delta: deltas[0],
y2_dc_delta: deltas[1],
y2_ac_delta: deltas[2],
uv_dc_delta: deltas[3],
uv_ac_delta: deltas[4],
})
}
#[cfg(test)]
mod tests {
use super::*;
struct TestEncoder {
out: Vec<u8>,
range: u32,
bottom: u32,
bit_count: i32,
}
impl TestEncoder {
fn new() -> Self {
TestEncoder {
out: Vec::new(),
range: 255,
bottom: 0,
bit_count: 24,
}
}
fn write_bool(&mut self, prob: u8, val: bool) {
let split = 1 + (((self.range - 1) * prob as u32) >> 8);
if val {
self.bottom = self.bottom.wrapping_add(split);
self.range -= split;
} else {
self.range = split;
}
while self.range < 128 {
self.range <<= 1;
if (self.bottom >> 31) & 1 == 1 {
let mut i = self.out.len();
while i > 0 {
i -= 1;
if self.out[i] == 255 {
self.out[i] = 0;
} else {
self.out[i] = self.out[i].wrapping_add(1);
break;
}
}
}
self.bottom <<= 1;
self.bit_count -= 1;
if self.bit_count == 0 {
let byte = (self.bottom >> 24) as u8;
self.out.push(byte);
self.bottom &= (1 << 24) - 1;
self.bit_count = 8;
}
}
}
fn write_literal(&mut self, value: u32, num_bits: u32) {
for i in (0..num_bits).rev() {
let bit = ((value >> i) & 1) == 1;
self.write_bool(128, bit);
}
}
fn write_empty_token_prob_update(&mut self) {
for plane in &COEFF_UPDATE_PROBS {
for band in plane {
for ctx in band {
for &prob in ctx {
self.write_bool(prob, false);
}
}
}
}
}
fn write_empty_mv_prob_update(&mut self) {
for ctx in &MV_UPDATE_PROBS {
for &prob in ctx {
self.write_bool(prob, false);
}
}
}
fn finish(mut self) -> Vec<u8> {
let c = self.bit_count;
let mut v = self.bottom;
if v & (1u32 << (32 - c)) != 0 {
let mut i = self.out.len();
while i > 0 {
i -= 1;
if self.out[i] == 255 {
self.out[i] = 0;
} else {
self.out[i] = self.out[i].wrapping_add(1);
break;
}
}
}
v <<= c & 7;
let mut c_shift = c >> 3;
while c_shift > 0 {
v <<= 8;
c_shift -= 1;
}
for _ in 0..4 {
self.out.push((v >> 24) as u8);
v <<= 8;
}
self.out
}
}
fn build_minimal_key_frame() -> Vec<u8> {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(4, 7); for _ in 0..5 {
enc.write_bool(128, false); }
enc.write_bool(128, true); enc.write_empty_token_prob_update();
enc.write_bool(128, false); enc.finish()
}
#[test]
fn minimal_key_frame_round_trips() {
let buf = build_minimal_key_frame();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert_eq!(hdr.color_space, Some(false));
assert_eq!(hdr.clamping_type, Some(false));
assert!(!hdr.segmentation_enabled);
assert!(hdr.update_segmentation.is_none());
assert!(!hdr.filter_type);
assert_eq!(hdr.loop_filter_level, 0);
assert_eq!(hdr.sharpness_level, 0);
assert!(!hdr.mb_lf_adjustments.loop_filter_adj_enable);
assert!(!hdr.mb_lf_adjustments.mode_ref_lf_delta_update);
assert_eq!(hdr.log2_nbr_of_dct_partitions, 0);
assert_eq!(hdr.nbr_of_dct_partitions, 1);
assert_eq!(hdr.quant_indices.y_ac_qi, 4);
assert_eq!(hdr.quant_indices.y_dc_delta, None);
assert_eq!(hdr.quant_indices.uv_ac_delta, None);
assert!(hdr.refresh_entropy_probs);
assert_eq!(hdr.refresh_golden_frame, None);
assert_eq!(hdr.refresh_alternate_frame, None);
assert_eq!(hdr.refresh_last, None);
assert!(!hdr.mb_no_skip_coeff);
assert_eq!(hdr.prob_skip_false, None);
}
#[test]
fn key_frame_filter_and_partition_fields_round_trip() {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, true); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_literal(63, 6); enc.write_literal(7, 3); enc.write_bool(128, false); enc.write_literal(3, 2); enc.write_literal(127, 7); for _ in 0..5 {
enc.write_bool(128, false);
}
enc.write_bool(128, false); enc.write_empty_token_prob_update();
enc.write_bool(128, true); enc.write_literal(0xab, 8); let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert_eq!(hdr.clamping_type, Some(true));
assert!(hdr.filter_type);
assert_eq!(hdr.loop_filter_level, 63);
assert_eq!(hdr.sharpness_level, 7);
assert_eq!(hdr.log2_nbr_of_dct_partitions, 3);
assert_eq!(hdr.nbr_of_dct_partitions, 8);
assert_eq!(hdr.quant_indices.y_ac_qi, 127);
assert!(!hdr.refresh_entropy_probs);
assert!(hdr.mb_no_skip_coeff);
assert_eq!(hdr.prob_skip_false, Some(0xab));
}
#[test]
fn mb_lf_adjustments_full_block_round_trips() {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_literal(1, 6);
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true);
enc.write_literal(2, 6);
enc.write_bool(128, true); enc.write_bool(128, true);
enc.write_literal(5, 6);
enc.write_bool(128, false);
enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(3, 6);
enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(1, 6);
enc.write_bool(128, true);
enc.write_bool(128, false);
enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
enc.write_bool(128, false);
}
enc.write_bool(128, false); enc.write_empty_token_prob_update();
enc.write_bool(128, false); let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
let lf = hdr.mb_lf_adjustments;
assert!(lf.loop_filter_adj_enable);
assert!(lf.mode_ref_lf_delta_update);
assert_eq!(
lf.ref_frame_delta_update,
[Some(1), None, Some(-2), Some(5)]
);
assert_eq!(lf.mb_mode_delta_update, [None, Some(3), Some(-1), None]);
}
#[test]
fn update_segmentation_block_round_trips() {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true);
enc.write_literal(10, 7);
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true);
enc.write_literal(20, 7);
enc.write_bool(128, true); enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(5, 6);
enc.write_bool(128, false); enc.write_bool(128, true);
enc.write_literal(3, 6);
enc.write_bool(128, true); enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(255, 8);
enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(0, 8);
enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
enc.write_bool(128, false);
}
enc.write_bool(128, false); enc.write_empty_token_prob_update();
enc.write_bool(128, false); let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert!(hdr.segmentation_enabled);
let seg = hdr.update_segmentation.unwrap();
assert!(seg.update_mb_segmentation_map);
assert!(seg.update_segment_feature_data);
assert!(seg.segment_feature_mode_absolute);
assert_eq!(seg.quantizer_update, [Some(10), None, None, Some(-20)]);
assert_eq!(seg.loop_filter_update, [None, Some(5), Some(-3), None]);
assert_eq!(seg.segment_prob, [Some(255), None, Some(0)]);
}
#[test]
fn quant_indices_all_deltas_present() {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(64, 7); enc.write_bool(128, true);
enc.write_literal(1, 4);
enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(2, 4);
enc.write_bool(128, true);
enc.write_bool(128, true);
enc.write_literal(15, 4);
enc.write_bool(128, false);
enc.write_bool(128, true);
enc.write_literal(15, 4);
enc.write_bool(128, true);
enc.write_bool(128, true);
enc.write_literal(0, 4);
enc.write_bool(128, false);
enc.write_bool(128, false); enc.write_empty_token_prob_update();
enc.write_bool(128, false); let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert_eq!(hdr.quant_indices.y_ac_qi, 64);
assert_eq!(hdr.quant_indices.y_dc_delta, Some(1));
assert_eq!(hdr.quant_indices.y2_dc_delta, Some(-2));
assert_eq!(hdr.quant_indices.y2_ac_delta, Some(15));
assert_eq!(hdr.quant_indices.uv_dc_delta, Some(-15));
assert_eq!(hdr.quant_indices.uv_ac_delta, Some(0));
}
#[test]
fn interframe_refresh_block_full_path() {
let mut enc = TestEncoder::new();
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(10, 6); enc.write_literal(2, 3); enc.write_bool(128, false); enc.write_literal(1, 2); enc.write_literal(8, 7); for _ in 0..5 {
enc.write_bool(128, false);
}
enc.write_bool(128, false); enc.write_bool(128, true); enc.write_literal(2, 2); enc.write_bool(128, true); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_empty_token_prob_update();
enc.write_bool(128, true); enc.write_literal(200, 8); enc.write_literal(17, 8); enc.write_literal(130, 8); enc.write_literal(240, 8); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_mv_prob_update();
let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
assert_eq!(hdr.color_space, None);
assert_eq!(hdr.clamping_type, None);
assert_eq!(hdr.loop_filter_level, 10);
assert_eq!(hdr.sharpness_level, 2);
assert_eq!(hdr.nbr_of_dct_partitions, 2);
assert_eq!(hdr.refresh_golden_frame, Some(false));
assert_eq!(hdr.refresh_alternate_frame, Some(true));
assert_eq!(hdr.copy_buffer_to_golden, Some(2));
assert_eq!(hdr.copy_buffer_to_alternate, None);
assert_eq!(hdr.sign_bias_golden, Some(true));
assert_eq!(hdr.sign_bias_alternate, Some(false));
assert!(!hdr.refresh_entropy_probs);
assert_eq!(hdr.refresh_last, Some(true));
assert!(hdr.mb_no_skip_coeff);
assert_eq!(hdr.prob_skip_false, Some(200));
assert_eq!(hdr.prob_intra, Some(17));
assert_eq!(hdr.prob_last, Some(130));
assert_eq!(hdr.prob_gf, Some(240));
assert_eq!(hdr.intra_y_mode_prob_update, None);
assert_eq!(hdr.intra_uv_mode_prob_update, None);
let mv = hdr.mv_prob_update.unwrap();
assert!(mv[0].iter().all(|s| s.is_none()));
assert!(mv[1].iter().all(|s| s.is_none()));
}
#[test]
fn mb_no_skip_coeff_false_omits_prob_skip_false() {
let buf = build_minimal_key_frame();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert!(!hdr.mb_no_skip_coeff);
assert_eq!(hdr.prob_skip_false, None);
}
#[test]
fn input_too_short_surfaces_bool_decoder_error() {
let err = Vp8CodedHeader::parse(&[], true).unwrap_err();
assert!(matches!(
err,
CodedHeaderError::BoolDecoder(BoolDecoderError::InputTooShort)
));
let err = Vp8CodedHeader::parse(&[0x00], true).unwrap_err();
assert!(matches!(
err,
CodedHeaderError::BoolDecoder(BoolDecoderError::InputTooShort)
));
}
#[test]
fn parses_real_tiny_fixture_partition() {
let partition: [u8; 23] = [
0x00, 0x47, 0x08, 0x85, 0x85, 0x88, 0x85, 0x84, 0x88, 0x02, 0x02, 0x02, 0x75, 0xaa,
0x03, 0xf8, 0x03, 0xfa, 0x02, 0x0d, 0x4d, 0x18, 0x00,
];
let hdr = Vp8CodedHeader::parse(&partition, true).unwrap();
assert!(hdr.color_space.is_some());
assert!(hdr.clamping_type.is_some());
assert!(!hdr.segmentation_enabled);
assert!(!hdr.filter_type, "filter_simple=0 per trace");
assert_eq!(hdr.loop_filter_level, 1, "filter_level=1 per trace");
assert_eq!(hdr.sharpness_level, 0, "filter_sharpness=0 per trace");
assert!(
hdr.mb_lf_adjustments.loop_filter_adj_enable,
"lf_delta_enabled=1 per trace"
);
assert_eq!(
hdr.log2_nbr_of_dct_partitions, 0,
"num_partitions=1 per trace → log2=0"
);
assert_eq!(hdr.nbr_of_dct_partitions, 1);
assert_eq!(hdr.quant_indices.y_ac_qi, 4, "qi_y_ac=4 per trace");
assert_eq!(hdr.quant_indices.y_dc_delta, None, "qi_y_dc_d=0 per trace");
assert_eq!(hdr.quant_indices.y2_dc_delta, None);
assert_eq!(hdr.quant_indices.y2_ac_delta, None);
assert_eq!(hdr.quant_indices.uv_dc_delta, None);
assert_eq!(hdr.quant_indices.uv_ac_delta, None);
assert!(hdr.refresh_entropy_probs, "update_probs=1 per trace");
assert!(hdr.mb_no_skip_coeff, "mbskip_enabled=1 per trace");
assert_eq!(hdr.prob_skip_false, Some(255), "prob_skip=255 per trace");
assert_eq!(hdr.prob_intra, None);
assert_eq!(hdr.prob_last, None);
assert_eq!(hdr.prob_gf, None);
assert_eq!(hdr.intra_y_mode_prob_update, None);
assert_eq!(hdr.intra_uv_mode_prob_update, None);
assert!(hdr.mv_prob_update.is_none());
}
#[test]
fn key_frame_omits_section_9_10_tail() {
let buf = build_minimal_key_frame();
let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
assert_eq!(hdr.prob_intra, None);
assert_eq!(hdr.prob_last, None);
assert_eq!(hdr.prob_gf, None);
assert_eq!(hdr.intra_y_mode_prob_update, None);
assert_eq!(hdr.intra_uv_mode_prob_update, None);
assert!(hdr.mv_prob_update.is_none());
}
fn write_inter_prefix_through_skip_block(enc: &mut TestEncoder) {
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
enc.write_bool(128, false); }
enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 2); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_token_prob_update();
enc.write_bool(128, false); }
#[test]
fn interframe_prob_intra_last_gf_round_trip() {
let mut enc = TestEncoder::new();
write_inter_prefix_through_skip_block(&mut enc);
enc.write_literal(0x00, 8); enc.write_literal(0x80, 8); enc.write_literal(0xff, 8); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_mv_prob_update();
let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
assert_eq!(hdr.prob_intra, Some(0));
assert_eq!(hdr.prob_last, Some(128));
assert_eq!(hdr.prob_gf, Some(255));
}
#[test]
fn interframe_intra_y_mode_prob_update_full_block() {
let mut enc = TestEncoder::new();
write_inter_prefix_through_skip_block(&mut enc);
enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, true); for &p in &[200u8, 100, 33, 250] {
enc.write_literal(p as u32, 8);
}
enc.write_bool(128, false); enc.write_empty_mv_prob_update();
let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
assert_eq!(hdr.intra_y_mode_prob_update, Some([200, 100, 33, 250]));
assert_eq!(hdr.intra_uv_mode_prob_update, None);
}
#[test]
fn interframe_intra_uv_mode_prob_update_full_block() {
let mut enc = TestEncoder::new();
write_inter_prefix_through_skip_block(&mut enc);
enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, false); enc.write_bool(128, true); for &p in &[10u8, 20, 30] {
enc.write_literal(p as u32, 8);
}
enc.write_empty_mv_prob_update();
let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
assert_eq!(hdr.intra_y_mode_prob_update, None);
assert_eq!(hdr.intra_uv_mode_prob_update, Some([10, 20, 30]));
}
#[test]
fn interframe_mv_prob_update_round_trip() {
let mut enc = TestEncoder::new();
write_inter_prefix_through_skip_block(&mut enc);
enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, false); enc.write_bool(128, false);
for (j, &update_prob) in MV_UPDATE_PROBS[0].iter().enumerate() {
if j == 0 {
enc.write_bool(update_prob, true);
enc.write_literal(50, 7);
} else if j == 5 {
enc.write_bool(update_prob, true);
enc.write_literal(0, 7);
} else {
enc.write_bool(update_prob, false);
}
}
for (j, &update_prob) in MV_UPDATE_PROBS[1].iter().enumerate() {
if j == MV_PROB_COUNT - 1 {
enc.write_bool(update_prob, true);
enc.write_literal(127, 7);
} else {
enc.write_bool(update_prob, false);
}
}
let buf = enc.finish();
let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
let mv = hdr.mv_prob_update.unwrap();
assert_eq!(mv[0][0], Some(100));
assert_eq!(mv[0][1], None);
assert_eq!(mv[0][5], Some(1));
for (j, &slot) in mv[0].iter().enumerate() {
if j != 0 && j != 5 {
assert!(slot.is_none(), "row[{j}] should be None");
}
}
assert_eq!(mv[1][MV_PROB_COUNT - 1], Some(254));
for (j, &slot) in mv[1].iter().enumerate().take(MV_PROB_COUNT - 1) {
assert!(slot.is_none(), "col[{j}] should be None");
}
}
#[test]
fn mv_default_context_matches_spec_listing() {
assert_eq!(DEFAULT_MV_CONTEXT[0][0], 162); assert_eq!(DEFAULT_MV_CONTEXT[0][1], 128); assert_eq!(DEFAULT_MV_CONTEXT[0][2], 225); assert_eq!(DEFAULT_MV_CONTEXT[0][8], 156); assert_eq!(DEFAULT_MV_CONTEXT[0][9], 128); assert_eq!(DEFAULT_MV_CONTEXT[0][18], 254); assert_eq!(DEFAULT_MV_CONTEXT[1][0], 164); assert_eq!(DEFAULT_MV_CONTEXT[1][1], 128); assert_eq!(DEFAULT_MV_CONTEXT[1][2], 204); assert_eq!(DEFAULT_MV_CONTEXT[1][8], 228); assert_eq!(DEFAULT_MV_CONTEXT[1][9], 128); assert_eq!(DEFAULT_MV_CONTEXT[1][18], 254); assert_eq!(MV_UPDATE_PROBS[0][0], 237);
assert_eq!(MV_UPDATE_PROBS[0][18], 254);
assert_eq!(MV_UPDATE_PROBS[1][0], 231);
assert_eq!(MV_UPDATE_PROBS[1][18], 254);
}
fn reference_parse_token_prob_update_indexed(
dec: &mut BoolDecoder<'_>,
) -> Result<TokenProbUpdates, BoolDecoderError> {
let mut out: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
for (i, plane) in out.iter_mut().enumerate() {
for (j, band) in plane.iter_mut().enumerate() {
for (k, ctx) in band.iter_mut().enumerate() {
for (t, slot) in ctx.iter_mut().enumerate() {
let present = dec.read_bool(COEFF_UPDATE_PROBS[i][j][k][t])?;
*slot = if present {
Some(dec.read_literal(8)? as u8)
} else {
None
};
}
}
}
}
Ok(out)
}
#[test]
fn parse_token_prob_update_matches_indexed_reference() {
let mut intended: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
let mut salt: u32 = 1;
for (i, plane) in intended.iter_mut().enumerate() {
for (j, band) in plane.iter_mut().enumerate() {
for (k, ctx) in band.iter_mut().enumerate() {
for (t, slot) in ctx.iter_mut().enumerate() {
salt = salt.wrapping_mul(1103515245).wrapping_add(12345);
if (i + j + k + t + (salt >> 16) as usize).is_multiple_of(5) {
*slot = Some(((salt >> 8) & 0xFF) as u8);
}
}
}
}
}
let mut enc = TestEncoder::new();
for (i, plane) in intended.iter().enumerate() {
for (j, band) in plane.iter().enumerate() {
for (k, ctx) in band.iter().enumerate() {
for (t, slot) in ctx.iter().enumerate() {
let p = COEFF_UPDATE_PROBS[i][j][k][t];
match slot {
Some(prob) => {
enc.write_bool(p, true);
enc.write_literal(*prob as u32, 8);
}
None => enc.write_bool(p, false),
}
}
}
}
}
enc.write_bool(128, false);
let bytes = enc.finish();
let mut dec_prod = BoolDecoder::init(&bytes).unwrap();
let produced = parse_token_prob_update(&mut dec_prod).unwrap();
let mut dec_ref = BoolDecoder::init(&bytes).unwrap();
let reference = reference_parse_token_prob_update_indexed(&mut dec_ref).unwrap();
assert_eq!(
produced, reference,
"zipped loop must match the 4-level-indexed reference loop"
);
assert_eq!(
produced, intended,
"decode must recover the intended payload"
);
assert_eq!(
dec_prod.remaining_input(),
dec_ref.remaining_input(),
"both parsers must leave the decoder at the same stream position"
);
}
}