diff --git a/crates/ec-vp8/src/frame.rs b/crates/ec-vp8/src/frame.rs index 14809dd2..6cb6a471 100644 --- a/crates/ec-vp8/src/frame.rs +++ b/crates/ec-vp8/src/frame.rs @@ -101,6 +101,11 @@ pub fn parse_keyframe_dims(data: &[u8]) -> Result { }) } +/// Clamp an integer sample into the 8-bit pixel range (RFC 6386 `clamp255`). +pub(crate) fn clamp255(v: i32) -> i32 { + v.clamp(0, 255) +} + /// Number of 16×16 macroblock columns/rows covering `width × height` /// pixels (the encoded frame is padded up to whole macroblocks). pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) { diff --git a/crates/ec-vp8/src/intra.rs b/crates/ec-vp8/src/intra.rs index 7419377c..cfc2d835 100644 --- a/crates/ec-vp8/src/intra.rs +++ b/crates/ec-vp8/src/intra.rs @@ -30,6 +30,7 @@ //! row); on the top frame row they are 127. [`predict4`] only consumes //! `A[0..8]` as given. +use crate::frame::clamp255; /// Weighted average of three adjacent pixels centered at `y` /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`). @@ -48,13 +49,6 @@ fn avg2(x: u8, y: u8) -> u8 { ((u32::from(x) + u32::from(y) + 1) >> 1) as u8 } -/// `clamp255(v)`: clamp an integer pixel difference into the valid `u8` range -/// (RFC 6386 Section 12.2 `TMpred` / Section 12.3 `B_TM_PRED`). -#[inline] -fn clamp255(v: i32) -> u8 { - v.clamp(0, 255) as u8 -} - /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is /// "essentially identical" with 16-pixel edges). @@ -106,7 +100,7 @@ fn predict_block( 3 => { for (row, &l) in out.as_chunks_mut::().0.iter_mut().zip(left.iter()) { for (o, &a) in row.iter_mut().zip(above.iter()) { - *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)); + *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)) as u8; } } } @@ -215,7 +209,7 @@ pub fn predict4( for r in 0..4 { for c in 0..4 { b[r * 4 + c] = - clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)); + clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)) as u8; } } } diff --git a/crates/ec-vp8/src/mc.rs b/crates/ec-vp8/src/mc.rs index 10a9707f..e1851822 100644 --- a/crates/ec-vp8/src/mc.rs +++ b/crates/ec-vp8/src/mc.rs @@ -23,6 +23,7 @@ //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386 use archmage::SimdToken; use multiversion::target::match_target; +use crate::frame::clamp255; use crate::modes; /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes. @@ -55,11 +56,6 @@ const BILINEAR_FILTERS: [[i32; 2]; 8] = [ [16, 112], ]; -#[inline] -fn clamp255(v: i32) -> i32 { - v.clamp(0, 255) -} - /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border` /// ring on all four sides by edge replication: every out-of-image sample /// becomes the nearest in-image sample.