use crate::utils::bit_reader_inl_utils::{LBIT_BYTES, vp8_get_bit, vp8_load_new_bytes};
use crate::utils::endian_inl_utils::h_to_le32;
use crate::utils::utils::webp_mem_to_uint32;
pub const BITS: i32 = if cfg!(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32",
target_arch = "wasm64"
)) {
56
} else {
24
};
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32",
target_arch = "wasm64"
))]
#[expect(non_camel_case_types, reason = "C type name")]
pub type bit_t = u64;
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32",
target_arch = "wasm64"
)))]
#[expect(non_camel_case_types, reason = "C type name")]
pub type bit_t = u32;
#[expect(non_camel_case_types, reason = "C type name")]
pub type range_t = u32;
#[derive(Debug, Default)]
pub struct VP8BitReader {
pub value: bit_t,
pub range: range_t,
pub bits: i32,
pub has_buf: bool,
pub buf: usize,
pub buf_end: usize,
pub buf_max: usize,
pub eof: bool,
}
pub const VP8_LOG2_RANGE: [u8; 128] = [
7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
];
pub const VP8_NEW_RANGE: [u8; 128] = [
127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239, 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239, 247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179, 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 127,
];
pub fn vp8_bit_reader_set_buffer(br: &mut VP8BitReader, size: usize) {
br.has_buf = true;
br.buf = 0;
br.buf_end = size;
br.buf_max = if size >= LBIT_BYTES {
size + 1 - LBIT_BYTES
} else {
0
};
}
pub fn vp8_init_bit_reader(br: &mut VP8BitReader, data: &[u8]) {
debug_assert!(data.len() < (1 << 31)); br.range = 255 - 1;
br.value = 0;
br.bits = -8; br.eof = false;
vp8_bit_reader_set_buffer(br, data.len());
vp8_load_new_bytes(br, data);
}
pub fn vp8_remap_bit_reader(br: &mut VP8BitReader, offset: isize) {
if br.has_buf {
br.buf = br
.buf
.checked_add_signed(offset)
.expect("the remapped position stays in the input");
br.buf_end = br
.buf_end
.checked_add_signed(offset)
.expect("the remapped end stays in the input");
br.buf_max = br
.buf_max
.checked_add_signed(offset)
.expect("the remapped limit stays in the input");
}
}
pub fn vp8_load_final_bytes(br: &mut VP8BitReader, data: &[u8]) {
if br.buf < br.buf_end {
br.bits += 8;
br.value = bit_t::from(data[br.buf]) | (br.value << 8);
br.buf += 1;
} else if !br.eof {
br.value <<= 8;
br.bits += 8;
br.eof = true;
} else {
br.bits = 0; }
}
#[expect(clippy::cast_sign_loss, reason = "each bit is 0 or 1")]
pub fn vp8_get_value(br: &mut VP8BitReader, data: &[u8], num_bits: i32) -> u32 {
let mut v: u32 = 0;
let mut bits = num_bits;
while bits > 0 {
bits -= 1;
v |= (vp8_get_bit(br, data, 0x80) as u32) << bits;
}
v
}
pub fn vp8_get(br: &mut VP8BitReader, data: &[u8]) -> u32 {
vp8_get_value(br, data, 1)
}
#[expect(clippy::cast_possible_wrap, reason = "C converts the uint32_t to int")]
pub fn vp8_get_signed_value(br: &mut VP8BitReader, data: &[u8], num_bits: i32) -> i32 {
let value = vp8_get_value(br, data, num_bits) as i32;
if vp8_get(br, data) != 0 {
-value
} else {
value
}
}
pub const VP8L_LBITS: i32 = 64;
pub const VP8L_WBITS: i32 = 32;
pub const VP8L_MAX_NUM_BIT_READ: i32 = 24;
#[expect(non_camel_case_types, reason = "C type name")]
pub type vp8l_val_t = u64;
const VP8L_LOG8_WBITS: usize = 4;
pub const VP8L_USE_FAST_LOAD: bool = cfg!(any(
target_arch = "arm",
target_arch = "aarch64",
target_arch = "x86",
target_arch = "x86_64",
target_arch = "wasm32",
target_arch = "wasm64",
));
const BIT_MASK: [u32; 25] = [
0,
0x0000_0001,
0x0000_0003,
0x0000_0007,
0x0000_000f,
0x0000_001f,
0x0000_003f,
0x0000_007f,
0x0000_00ff,
0x0000_01ff,
0x0000_03ff,
0x0000_07ff,
0x0000_0fff,
0x0000_1fff,
0x0000_3fff,
0x0000_7fff,
0x0000_ffff,
0x0001_ffff,
0x0003_ffff,
0x0007_ffff,
0x000f_ffff,
0x001f_ffff,
0x003f_ffff,
0x007f_ffff,
0x00ff_ffff,
];
#[derive(Debug, Default)]
pub struct VP8LBitReader {
pub val: vp8l_val_t,
pub len: usize,
pub pos: usize,
pub bit_pos: i32,
pub eos: bool,
}
pub fn vp8l_init_bit_reader(br: &mut VP8LBitReader, data: &[u8]) {
debug_assert!(data.len() < 0xffff_fff8); let length = data.len().min(size_of::<vp8l_val_t>());
let mut value: vp8l_val_t = 0;
for (i, &byte) in data[..length].iter().enumerate() {
value |= vp8l_val_t::from(byte) << (8 * i);
}
br.len = data.len();
br.val = value;
br.pos = length;
br.bit_pos = 0;
br.eos = false;
}
pub fn vp8l_bit_reader_set_buffer(br: &mut VP8LBitReader, len: usize) {
debug_assert!(len < 0xffff_fff8); br.len = len;
br.eos = (br.pos > br.len) || vp8l_is_end_of_stream(br);
}
fn vp8l_set_end_of_stream(br: &mut VP8LBitReader) {
br.eos = true;
br.bit_pos = 0; }
fn shift_bytes(br: &mut VP8LBitReader, data: &[u8]) {
while br.bit_pos >= 8 && br.pos < br.len {
br.val >>= 8;
br.val |= vp8l_val_t::from(data[br.pos]) << (VP8L_LBITS - 8);
br.pos += 1;
br.bit_pos -= 8;
}
if vp8l_is_end_of_stream(br) {
vp8l_set_end_of_stream(br);
}
}
#[expect(
clippy::cast_sign_loss,
reason = "n_bits is checked not to be negative"
)]
pub fn vp8l_read_bits(br: &mut VP8LBitReader, data: &[u8], n_bits: i32) -> u32 {
debug_assert!(n_bits >= 0);
if !br.eos && n_bits <= VP8L_MAX_NUM_BIT_READ {
let val = vp8l_prefetch_bits(br) & BIT_MASK[n_bits as usize];
let new_bits = br.bit_pos + n_bits;
br.bit_pos = new_bits;
shift_bytes(br, data);
val
} else {
vp8l_set_end_of_stream(br);
0
}
}
#[must_use]
#[expect(clippy::cast_possible_truncation, reason = "C keeps the low 32 bits")]
pub fn vp8l_prefetch_bits(br: &VP8LBitReader) -> u32 {
(br.val >> (br.bit_pos & (VP8L_LBITS - 1))) as u32
}
#[must_use]
pub fn vp8l_is_end_of_stream(br: &VP8LBitReader) -> bool {
debug_assert!(br.pos <= br.len);
br.eos || ((br.pos == br.len) && (br.bit_pos > VP8L_LBITS))
}
pub fn vp8l_set_bit_pos(br: &mut VP8LBitReader, val: i32) {
br.bit_pos = val;
}
pub fn vp8l_do_fill_bit_window(br: &mut VP8LBitReader, data: &[u8]) {
debug_assert!(br.bit_pos >= VP8L_WBITS);
if VP8L_USE_FAST_LOAD && br.pos + size_of::<vp8l_val_t>() < br.len {
br.val >>= VP8L_WBITS;
br.bit_pos -= VP8L_WBITS;
let word = h_to_le32(webp_mem_to_uint32(&data[br.pos..]));
br.val |= vp8l_val_t::from(word) << (VP8L_LBITS - VP8L_WBITS);
br.pos += VP8L_LOG8_WBITS;
return;
}
shift_bytes(br, data); }
pub fn vp8l_fill_bit_window(br: &mut VP8LBitReader, data: &[u8]) {
if br.bit_pos >= VP8L_WBITS {
vp8l_do_fill_bit_window(br, data);
}
}