#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[expect(
clippy::struct_excessive_bools,
reason = "the int flags of the C structure, which it uses as predicates"
)]
pub struct VP8Io {
pub width: i32,
pub height: i32,
pub mb_y: i32,
pub mb_w: i32,
pub mb_h: i32,
pub y_stride: i32,
pub uv_stride: i32,
pub fancy_upsampling: bool,
pub bypass_filtering: bool,
pub use_cropping: bool,
pub crop_left: i32,
pub crop_right: i32,
pub crop_top: i32,
pub crop_bottom: i32,
pub use_scaling: bool,
pub scaled_width: i32,
pub scaled_height: i32,
}
#[derive(Debug)]
pub struct VP8IoRows<'r> {
pub y: &'r mut [u8],
pub u: &'r [u8],
pub v: &'r [u8],
pub a: Option<&'r [u8]>,
pub a_offset: usize,
}
pub trait VP8IoHooks {
fn setup(&mut self, io: &mut VP8Io) -> bool;
fn put(&mut self, io: &VP8Io, rows: &mut VP8IoRows<'_>) -> bool;
fn teardown(&mut self, io: &VP8Io);
fn has_setup(&self) -> bool {
true
}
fn has_put(&self) -> bool {
true
}
fn has_teardown(&self) -> bool {
true
}
}
use std::sync::LazyLock;
use crate::dec::alpha_dec::webp_deallocate_alpha_memory;
use crate::dec::common_dec::{
B_DC_PRED, MB_FEATURE_TREE_PROBS, NUM_BANDS, NUM_MB_SEGMENTS, NUM_MODE_LF_DELTAS,
NUM_REF_LF_DELTAS,
};
use crate::dec::frame_dec::{
FINISH_ROW_HOOK, row_job, vp8_enter_critical, vp8_exit_critical, vp8_init_frame,
vp8_process_row, vp8_sync_rows,
};
use crate::dec::quant_dec::vp8_parse_quant;
use crate::dec::tree_dec::{vp8_parse_intra_mode_row, vp8_parse_proba, vp8_reset_proba};
use crate::dec::vp8i_dec::{
DEC_MAJ_VERSION, DEC_MIN_VERSION, DEC_REV_VERSION, VP8BandProbas, VP8Decoder, VP8RowJob,
VP8SegmentHeader, quant_t,
};
use crate::dsp::cpu::{CPUFeature, vp8_get_cpu_info};
use crate::dsp::dec::vp8_transform_wht;
use crate::utils::bit_reader_inl_utils::{vp8_get_bit, vp8_get_bit_alt, vp8_get_signed};
use crate::utils::bit_reader_utils::{
VP8BitReader, vp8_get, vp8_get_signed_value, vp8_get_value, vp8_init_bit_reader,
vp8_remap_bit_reader,
};
use crate::webp::decode::{VP8StatusCode, WEBP_DECODER_ABI_VERSION};
use crate::webp::format_constants::VP8_FRAME_HEADER_SIZE;
use crate::webp::types::webp_abi_is_incompatible;
#[must_use]
pub fn webp_get_decoder_version() -> i32 {
(DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION
}
type GetCoeffsFunc = fn(
&mut VP8BitReader,
&[u8],
&[VP8BandProbas; NUM_BANDS as usize],
&[usize; 16 + 1],
i32,
quant_t,
i32,
&mut [i16],
) -> i32;
static GET_COEFFS: LazyLock<GetCoeffsFunc> = LazyLock::new(init_get_coeffs);
fn set_ok(dec: &mut VP8Decoder) {
dec.status = VP8StatusCode::VP8_STATUS_OK;
dec.error_msg = Some("OK");
}
pub fn vp8_init_io_internal(io: &mut VP8Io, version: i32) -> bool {
if webp_abi_is_incompatible(version, WEBP_DECODER_ABI_VERSION) {
return false; }
*io = VP8Io::default();
true
}
#[must_use]
pub fn vp8_init_io(io: &mut VP8Io) -> bool {
vp8_init_io_internal(io, WEBP_DECODER_ABI_VERSION)
}
#[must_use]
pub fn vp8_new() -> Box<VP8Decoder> {
let mut dec = Box::<VP8Decoder>::default();
set_ok(&mut dec);
dec.worker
.set_job(FINISH_ROW_HOOK, VP8RowJob::default(), ());
dec.ready = false;
dec.num_parts_minus_one = 0;
dec
}
pub fn vp8_status(dec: &VP8Decoder) -> VP8StatusCode {
dec.status
}
#[must_use]
pub fn vp8_status_message(dec: &VP8Decoder) -> &'static str {
dec.error_msg.unwrap_or("OK")
}
pub fn vp8_set_error(
dec: &mut VP8Decoder,
error: VP8StatusCode,
msg: &'static str,
) -> VP8StatusCode {
debug_assert!(dec.incremental || error != VP8StatusCode::VP8_STATUS_SUSPENDED);
if dec.status == VP8StatusCode::VP8_STATUS_OK {
dec.status = error;
dec.error_msg = Some(msg);
dec.ready = false;
}
dec.status
}
#[must_use]
pub fn vp8_check_signature(data: &[u8]) -> bool {
data.len() >= 3 && data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a
}
#[must_use]
pub fn vp8_get_info(data: &[u8], chunk_size: usize) -> Option<(i32, i32)> {
if data.len() < VP8_FRAME_HEADER_SIZE.unsigned_abs() as usize {
return None; }
if !vp8_check_signature(&data[3..]) {
return None; }
let bits = u32::from(data[0]) | (u32::from(data[1]) << 8) | (u32::from(data[2]) << 16);
let key_frame = (bits & 1) == 0;
let w = ((i32::from(data[7]) << 8) | i32::from(data[6])) & 0x3fff;
let h = ((i32::from(data[9]) << 8) | i32::from(data[8])) & 0x3fff;
if !key_frame {
return None;
}
if ((bits >> 1) & 7) > 3 {
return None; }
if ((bits >> 4) & 1) == 0 {
return None; }
if (bits >> 5) as usize >= chunk_size {
return None; }
if w == 0 || h == 0 {
return None; }
Some((w, h))
}
fn init_bit_reader_at(br: &mut VP8BitReader, data: &[u8], start: usize, size: usize) {
vp8_init_bit_reader(br, &data[start..start + size]);
vp8_remap_bit_reader(
br,
isize::try_from(start).expect("a position in the input fits in isize"),
);
}
fn reset_segment_header(hdr: &mut VP8SegmentHeader) {
hdr.use_segment = false;
hdr.update_map = false;
hdr.absolute_delta = true;
hdr.quantizer = [0; NUM_MB_SEGMENTS as usize];
hdr.filter_strength = [0; NUM_MB_SEGMENTS as usize];
}
#[expect(
clippy::cast_possible_truncation,
reason = "C assigns the int value of at most 7 bits to an int8_t"
)]
fn value_i8(v: i32) -> i8 {
v as i8
}
#[allow(clippy::needless_range_loop, reason = "the C index loops")]
fn parse_segment_header(dec: &mut VP8Decoder, data: &[u8]) -> bool {
let VP8Decoder {
br,
segment_hdr: hdr,
proba,
..
} = dec;
hdr.use_segment = vp8_get(br, data) != 0;
if hdr.use_segment {
hdr.update_map = vp8_get(br, data) != 0;
if vp8_get(br, data) != 0 {
hdr.absolute_delta = vp8_get(br, data) != 0;
for s in 0..NUM_MB_SEGMENTS as usize {
hdr.quantizer[s] = if vp8_get(br, data) != 0 {
value_i8(vp8_get_signed_value(br, data, 7))
} else {
0
};
}
for s in 0..NUM_MB_SEGMENTS as usize {
hdr.filter_strength[s] = if vp8_get(br, data) != 0 {
value_i8(vp8_get_signed_value(br, data, 6))
} else {
0
};
}
}
if hdr.update_map {
for s in 0..MB_FEATURE_TREE_PROBS as usize {
proba.segments[s] = if vp8_get(br, data) != 0 {
#[expect(
clippy::cast_possible_truncation,
reason = "C assigns the 8-bit value to a uint8_t"
)]
let value = vp8_get_value(br, data, 8) as u8;
value
} else {
255
};
}
}
} else {
hdr.update_map = false;
}
!br.eof
}
#[allow(clippy::needless_range_loop, reason = "the C index loops")]
fn parse_partitions(
dec: &mut VP8Decoder,
data: &[u8],
buf: usize,
size: usize,
) -> Result<(), VP8StatusCode> {
let br = &mut dec.br;
let mut sz = buf;
let buf_end = buf + size;
let mut size_left = size;
dec.num_parts_minus_one = (1 << vp8_get_value(br, data, 2)) - 1;
let last_part = dec.num_parts_minus_one as usize;
if size < 3 * last_part {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
let mut part_start = buf + last_part * 3;
size_left -= last_part * 3;
for p in 0..last_part {
let mut psize = usize::from(data[sz])
| (usize::from(data[sz + 1]) << 8)
| (usize::from(data[sz + 2]) << 16);
if psize > size_left {
psize = size_left;
}
init_bit_reader_at(&mut dec.parts[p], data, part_start, psize);
part_start += psize;
size_left -= psize;
sz += 3;
}
init_bit_reader_at(&mut dec.parts[last_part], data, part_start, size_left);
if part_start < buf_end {
return Ok(());
}
Err(if dec.incremental {
VP8StatusCode::VP8_STATUS_SUSPENDED } else {
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA
})
}
#[expect(
clippy::cast_possible_wrap,
reason = "C assigns the unsigned values of at most 6 bits to ints"
)]
#[allow(clippy::needless_range_loop, reason = "the C index loops")]
fn parse_filter_header(dec: &mut VP8Decoder, data: &[u8]) -> bool {
let VP8Decoder {
br,
filter_hdr: hdr,
..
} = dec;
hdr.simple = vp8_get(br, data) != 0;
hdr.level = vp8_get_value(br, data, 6) as i32;
hdr.sharpness = vp8_get_value(br, data, 3) as i32;
hdr.use_lf_delta = vp8_get(br, data) != 0;
if hdr.use_lf_delta && vp8_get(br, data) != 0 {
for i in 0..NUM_REF_LF_DELTAS as usize {
if vp8_get(br, data) != 0 {
hdr.ref_lf_delta[i] = vp8_get_signed_value(br, data, 6);
}
}
for i in 0..NUM_MODE_LF_DELTAS as usize {
if vp8_get(br, data) != 0 {
hdr.mode_lf_delta[i] = vp8_get_signed_value(br, data, 6);
}
}
}
dec.filter_type = if hdr.level == 0 {
0
} else if hdr.simple {
1
} else {
2
};
!br.eof
}
pub fn vp8_get_headers(
dec: &mut VP8Decoder,
io: &mut VP8Io,
data: &[u8],
) -> Result<(), VP8StatusCode> {
vp8_get_headers_at(dec, io, data, 0)
}
#[expect(
clippy::cast_possible_truncation,
reason = "C assigns the bit fields of the header to their uint8_t fields"
)]
#[expect(clippy::too_many_lines, reason = "the C function")]
pub(crate) fn vp8_get_headers_at(
dec: &mut VP8Decoder,
io: &mut VP8Io,
data: &[u8],
start: usize,
) -> Result<(), VP8StatusCode> {
set_ok(dec);
let mut buf = start;
let mut buf_size = data.len() - start;
if buf_size < 4 {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA,
"Truncated header.",
));
}
{
let bits = u32::from(data[buf])
| (u32::from(data[buf + 1]) << 8)
| (u32::from(data[buf + 2]) << 16);
let frm_hdr = &mut dec.frm_hdr;
frm_hdr.key_frame = u8::from((bits & 1) == 0);
frm_hdr.profile = ((bits >> 1) & 7) as u8;
frm_hdr.show = ((bits >> 4) & 1) as u8;
frm_hdr.partition_length = bits >> 5;
if frm_hdr.profile > 3 {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR,
"Incorrect keyframe parameters.",
));
}
if frm_hdr.show == 0 {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_UNSUPPORTED_FEATURE,
"Frame not displayable.",
));
}
buf += 3;
buf_size -= 3;
}
if dec.frm_hdr.key_frame != 0 {
if buf_size < 7 {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA,
"cannot parse picture header",
));
}
if !vp8_check_signature(&data[buf..buf + buf_size]) {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR,
"Bad code word",
));
}
let pic_hdr = &mut dec.pic_hdr;
pic_hdr.width = ((u16::from(data[buf + 4]) << 8) | u16::from(data[buf + 3])) & 0x3fff;
pic_hdr.xscale = data[buf + 4] >> 6; pic_hdr.height = ((u16::from(data[buf + 6]) << 8) | u16::from(data[buf + 5])) & 0x3fff;
pic_hdr.yscale = data[buf + 6] >> 6;
buf += 7;
buf_size -= 7;
dec.mb_w = (i32::from(pic_hdr.width) + 15) >> 4;
dec.mb_h = (i32::from(pic_hdr.height) + 15) >> 4;
io.width = i32::from(pic_hdr.width);
io.height = i32::from(pic_hdr.height);
io.use_cropping = false;
io.crop_top = 0;
io.crop_left = 0;
io.crop_right = io.width;
io.crop_bottom = io.height;
io.use_scaling = false;
io.scaled_width = io.width;
io.scaled_height = io.height;
io.mb_w = io.width; io.mb_h = io.height;
vp8_reset_proba(&mut dec.proba);
reset_segment_header(&mut dec.segment_hdr);
}
if dec.frm_hdr.partition_length as usize > buf_size {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA,
"bad partition length",
));
}
let partition_length = dec.frm_hdr.partition_length as usize;
init_bit_reader_at(&mut dec.br, data, buf, partition_length);
buf += partition_length;
buf_size -= partition_length;
if dec.frm_hdr.key_frame != 0 {
dec.pic_hdr.colorspace = vp8_get(&mut dec.br, data) as u8;
dec.pic_hdr.clamp_type = vp8_get(&mut dec.br, data) as u8;
}
if !parse_segment_header(dec, data) {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR,
"cannot parse segment header",
));
}
if !parse_filter_header(dec, data) {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR,
"cannot parse filter header",
));
}
if let Err(status) = parse_partitions(dec, data, buf, buf_size) {
return Err(vp8_set_error(dec, status, "cannot parse partitions"));
}
vp8_parse_quant(dec, data);
if dec.frm_hdr.key_frame == 0 {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_UNSUPPORTED_FEATURE,
"Not a key frame.",
));
}
vp8_get(&mut dec.br, data);
vp8_parse_proba(dec, data);
dec.ready = true;
Ok(())
}
const CAT3: [u8; 4] = [173, 148, 140, 0];
const CAT4: [u8; 5] = [176, 155, 140, 135, 0];
const CAT5: [u8; 6] = [180, 157, 141, 134, 130, 0];
const CAT6: [u8; 12] = [254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0];
const CAT3456: [&[u8]; 4] = [&CAT3, &CAT4, &CAT5, &CAT6];
const ZIGZAG: [u8; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];
#[expect(
clippy::misrefactored_assign_op,
reason = "the C expression v += v + bit"
)]
fn get_large_value(br: &mut VP8BitReader, data: &[u8], p: &[u8; 11]) -> i32 {
let prob = |i: usize| i32::from(p[i]);
let v;
if vp8_get_bit(br, data, prob(3)) == 0 {
if vp8_get_bit(br, data, prob(4)) == 0 {
v = 2;
} else {
v = 3 + vp8_get_bit(br, data, prob(5));
}
} else if vp8_get_bit(br, data, prob(6)) == 0 {
if vp8_get_bit(br, data, prob(7)) == 0 {
v = 5 + vp8_get_bit(br, data, 159);
} else {
let mut w = 7 + 2 * vp8_get_bit(br, data, 165);
w += vp8_get_bit(br, data, 145);
v = w;
}
} else {
let bit1 = vp8_get_bit(br, data, prob(8));
let bit0 = vp8_get_bit(br, data, prob(9 + usize::from(bit1 != 0)));
let cat = 2 * bit1 + bit0;
let mut w = 0;
for &tab in CAT3456[usize::try_from(cat).expect("two bits")] {
if tab == 0 {
break;
}
w += w + vp8_get_bit(br, data, i32::from(tab));
}
w += 3 + (8 << cat);
v = w;
}
v
}
#[expect(
clippy::cast_possible_truncation,
reason = "C stores the int product in an int16_t"
)]
fn coeff(v: i32) -> i16 {
v as i16
}
fn position(n: usize) -> i32 {
i32::try_from(n).expect("a position in [0, 16]")
}
#[expect(clippy::too_many_arguments, reason = "the C parameters with the input")]
fn get_coeffs_fast(
br: &mut VP8BitReader,
data: &[u8],
bands: &[VP8BandProbas; NUM_BANDS as usize],
prob: &[usize; 16 + 1],
ctx: i32,
dq: quant_t,
n: i32,
out: &mut [i16],
) -> i32 {
let ctx = usize::try_from(ctx).expect("a context in [0, 2]");
let mut n = usize::try_from(n).expect("a position in [0, 15]");
let mut p = &bands[prob[n]].probas[ctx];
while n < 16 {
if vp8_get_bit(br, data, i32::from(p[0])) == 0 {
return position(n); }
while vp8_get_bit(br, data, i32::from(p[1])) == 0 {
n += 1;
p = &bands[prob[n]].probas[0];
if n == 16 {
return 16;
}
}
{
let p_ctx = &bands[prob[n + 1]].probas;
let v;
if vp8_get_bit(br, data, i32::from(p[2])) == 0 {
v = 1;
p = &p_ctx[1];
} else {
v = get_large_value(br, data, p);
p = &p_ctx[2];
}
out[usize::from(ZIGZAG[n])] =
coeff(vp8_get_signed(br, data, v) * dq[usize::from(n > 0)]);
}
n += 1;
}
16
}
#[expect(clippy::too_many_arguments, reason = "the C parameters with the input")]
fn get_coeffs_alt(
br: &mut VP8BitReader,
data: &[u8],
bands: &[VP8BandProbas; NUM_BANDS as usize],
prob: &[usize; 16 + 1],
ctx: i32,
dq: quant_t,
n: i32,
out: &mut [i16],
) -> i32 {
let ctx = usize::try_from(ctx).expect("a context in [0, 2]");
let mut n = usize::try_from(n).expect("a position in [0, 15]");
let mut p = &bands[prob[n]].probas[ctx];
while n < 16 {
if vp8_get_bit_alt(br, data, i32::from(p[0])) == 0 {
return position(n); }
while vp8_get_bit_alt(br, data, i32::from(p[1])) == 0 {
n += 1;
p = &bands[prob[n]].probas[0];
if n == 16 {
return 16;
}
}
{
let p_ctx = &bands[prob[n + 1]].probas;
let v;
if vp8_get_bit_alt(br, data, i32::from(p[2])) == 0 {
v = 1;
p = &p_ctx[1];
} else {
v = get_large_value(br, data, p);
p = &p_ctx[2];
}
out[usize::from(ZIGZAG[n])] =
coeff(vp8_get_signed(br, data, v) * dq[usize::from(n > 0)]);
}
n += 1;
}
16
}
fn init_get_coeffs() -> GetCoeffsFunc {
if vp8_get_cpu_info(CPUFeature::kSlowSSSE3) {
get_coeffs_alt
} else {
get_coeffs_fast
}
}
fn nz_code_bits(nz_coeffs: u32, nz: i32, dc_nz: bool) -> u32 {
let mut nz_coeffs = nz_coeffs << 2;
nz_coeffs |= if nz > 3 {
3
} else if nz > 1 {
2
} else {
u32::from(dc_nz)
};
nz_coeffs
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C stores the int or uint32_t value in a uint8_t"
)]
fn low_u8(v: i64) -> u8 {
v as u8
}
#[expect(clippy::too_many_lines, reason = "the C function")]
#[expect(clippy::similar_names, reason = "the C names out_t_nz and out_l_nz")]
fn parse_residuals(dec: &mut VP8Decoder, mb: usize, token_br: usize, data: &[u8]) -> bool {
let VP8Decoder {
parts,
proba,
mb_data,
mb_x,
dqm,
mb_info,
..
} = dec;
let get_coeffs = *GET_COEFFS;
let token_br = &mut parts[token_br];
let bands = &proba.bands;
let bands_ptr = &proba.bands_ptr;
let block = &mut mb_data[usize::try_from(*mb_x).expect("a non-negative position")];
let q = &dqm[usize::from(block.segment)];
let dst = &mut block.coeffs;
let mut dst_pos = 0;
let mut non_zero_y: u32 = 0;
let mut non_zero_uv: u32 = 0;
*dst = [0; 384];
let (first, ac_proba) = if block.is_i4x4 == 0 {
let mut dc = [0i16; 16];
let ctx = i32::from(mb_info[mb].nz_dc) + i32::from(mb_info[0].nz_dc);
let nz = get_coeffs(
token_br,
data,
&bands[1],
&bands_ptr[1],
ctx,
q.y2_mat,
0,
&mut dc,
);
let nz_dc = u8::from(nz > 0);
mb_info[mb].nz_dc = nz_dc;
mb_info[0].nz_dc = nz_dc;
if nz > 1 {
vp8_transform_wht(&dc, dst);
} else {
let dc0 = coeff((i32::from(dc[0]) + 3) >> 3);
for i in (0..16 * 16).step_by(16) {
dst[i] = dc0;
}
}
(1, 0)
} else {
(0, 3)
};
let mut tnz: u8 = mb_info[mb].nz & 0x0f;
let mut lnz: u8 = mb_info[0].nz & 0x0f;
for _y in 0..4 {
let mut l = i32::from(lnz & 1);
let mut nz_coeffs: u32 = 0;
for _x in 0..4 {
let ctx = l + i32::from(tnz & 1);
let nz = get_coeffs(
token_br,
data,
&bands[ac_proba],
&bands_ptr[ac_proba],
ctx,
q.y1_mat,
first,
&mut dst[dst_pos..],
);
l = i32::from(nz > first);
tnz = low_u8(i64::from((i32::from(tnz) >> 1) | (l << 7)));
nz_coeffs = nz_code_bits(nz_coeffs, nz, dst[dst_pos] != 0);
dst_pos += 16;
}
tnz >>= 4;
lnz = low_u8(i64::from((i32::from(lnz) >> 1) | (l << 7)));
non_zero_y = (non_zero_y << 8) | nz_coeffs;
}
let mut out_t_nz = u32::from(tnz);
let mut out_l_nz = u32::from(lnz >> 4);
for ch in (0..4).step_by(2) {
let mut nz_coeffs: u32 = 0;
tnz = mb_info[mb].nz >> (4 + ch);
lnz = mb_info[0].nz >> (4 + ch);
for _y in 0..2 {
let mut l = i32::from(lnz & 1);
for _x in 0..2 {
let ctx = l + i32::from(tnz & 1);
let nz = get_coeffs(
token_br,
data,
&bands[2],
&bands_ptr[2],
ctx,
q.uv_mat,
0,
&mut dst[dst_pos..],
);
l = i32::from(nz > 0);
tnz = low_u8(i64::from((i32::from(tnz) >> 1) | (l << 3)));
nz_coeffs = nz_code_bits(nz_coeffs, nz, dst[dst_pos] != 0);
dst_pos += 16;
}
tnz >>= 2;
lnz = low_u8(i64::from((i32::from(lnz) >> 1) | (l << 5)));
}
non_zero_uv |= nz_coeffs << (4 * ch);
out_t_nz |= (u32::from(tnz) << 4) << ch;
out_l_nz |= (u32::from(lnz) & 0xf0) << ch;
}
mb_info[mb].nz = low_u8(i64::from(out_t_nz));
mb_info[0].nz = low_u8(i64::from(out_l_nz));
block.non_zero_y = non_zero_y;
block.non_zero_uv = non_zero_uv;
block.dither = if (non_zero_uv & 0xaaaa) != 0 {
0
} else {
low_u8(i64::from(q.dither))
};
(non_zero_y | non_zero_uv) == 0 }
pub fn vp8_decode_mb(dec: &mut VP8Decoder, token_br: usize, data: &[u8]) -> bool {
let mb_x = usize::try_from(dec.mb_x).expect("a non-negative position");
let mb = mb_x + 1; let block = &dec.mb_data[mb_x];
let mut skip = if dec.use_skip_proba {
block.skip != 0
} else {
false
};
if skip {
let is_i4x4 = block.is_i4x4 != 0;
dec.mb_info[0].nz = 0;
dec.mb_info[mb].nz = 0;
if !is_i4x4 {
dec.mb_info[0].nz_dc = 0;
dec.mb_info[mb].nz_dc = 0;
}
let block = &mut dec.mb_data[mb_x];
block.non_zero_y = 0;
block.non_zero_uv = 0;
block.dither = 0;
} else {
skip = parse_residuals(dec, mb, token_br, data);
}
if dec.filter_type > 0 {
let block = &dec.mb_data[mb_x];
let mut finfo = dec.fstrengths[usize::from(block.segment)][usize::from(block.is_i4x4)];
finfo.f_inner |= u8::from(!skip);
dec.f_info[mb_x] = finfo;
}
!dec.parts[token_br].eof
}
pub fn vp8_init_scanline(dec: &mut VP8Decoder) {
let left = &mut dec.mb_info[0];
left.nz = 0;
left.nz_dc = 0;
#[expect(
clippy::cast_possible_truncation,
reason = "memset with the mode B_DC_PRED"
)]
let mode = B_DC_PRED as u8;
dec.intra_l = [mode; 4];
dec.mb_x = 0;
}
#[allow(clippy::needless_bool, reason = "the C early return")]
fn parse_frame(
dec: &mut VP8Decoder,
io: &mut VP8Io,
data: &[u8],
alpha_data: Option<&[u8]>,
hooks: Option<&mut (dyn VP8IoHooks + '_)>,
) -> bool {
let mut hooks = hooks;
dec.mb_y = 0;
while dec.mb_y < dec.br_mb_y {
let token_br = (dec.mb_y.cast_unsigned() & dec.num_parts_minus_one) as usize;
if !vp8_parse_intra_mode_row(dec, data) {
let _ = vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA,
"Premature end-of-partition0 encountered.",
);
return false;
}
while dec.mb_x < dec.mb_w {
if !vp8_decode_mb(dec, token_br, data) {
let _ = vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA,
"Premature end-of-file encountered.",
);
return false;
}
dec.mb_x += 1;
}
vp8_init_scanline(dec);
if !vp8_process_row(dec, io, alpha_data, hooks.as_deref_mut()) {
let _ = vp8_set_error(dec, VP8StatusCode::VP8_STATUS_USER_ABORT, "Output aborted.");
return false;
}
dec.mb_y += 1;
}
if dec.mt_method > 0 && !vp8_sync_rows(dec, alpha_data, hooks) {
return false;
}
true
}
pub fn vp8_decode(
dec: &mut VP8Decoder,
io: &mut VP8Io,
data: &[u8],
alpha_data: Option<&[u8]>,
hooks: Option<&mut (dyn VP8IoHooks + '_)>,
) -> bool {
let mut hooks = hooks;
if !dec.ready && vp8_get_headers(dec, io, data).is_err() {
return false;
}
debug_assert!(dec.ready);
let mut ok = vp8_enter_critical(dec, io, hooks.as_deref_mut()).is_ok();
if ok {
ok = vp8_init_frame(dec, io, alpha_data);
if ok {
ok = parse_frame(dec, io, data, alpha_data, hooks.as_deref_mut());
}
ok &= vp8_exit_critical(dec, io, alpha_data, hooks);
}
if !ok {
vp8_clear(dec);
return false;
}
dec.ready = false;
ok
}
pub fn vp8_clear(dec: &mut VP8Decoder) {
dec.worker.end();
webp_deallocate_alpha_memory(dec);
dec.intra_t = Vec::new();
dec.mb_info = Vec::new();
dec.f_info = Vec::new();
dec.mb_data = Vec::new();
dec.frame_alpha = Vec::new();
{
let job = row_job(&mut dec.worker);
job.yuv_t = Vec::new();
job.yuv_b = Vec::new();
job.cache_y = Vec::new();
job.cache_u = Vec::new();
job.cache_v = Vec::new();
job.thread_ctx.f_info = Vec::new();
job.thread_ctx.mb_data = Vec::new();
}
dec.mem_size = 0;
dec.br = VP8BitReader::default();
dec.ready = false;
dec.put_pending = false;
}