S7: vp8 one clamp255 (frame.rs) shared by intra and mc; same clamp, u8 casts at intra call sites
Browse files at this commiteb7a1988e87bc11c50b9fe5a91cd5c2c4056a687Parent: 91fc6e63 changed files +9 -14
crates/ec-vp8/src/frame.rsmodified+5 -0
| @@ -101,6 +101,11 @@ pub fn parse_keyframe_dims(data: &[u8]) -> Result<KeyFrameDims> { | ||
| 101 | 101 | }) |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | +/// Clamp an integer sample into the 8-bit pixel range (RFC 6386 `clamp255`). | |
| 105 | +pub(crate) fn clamp255(v: i32) -> i32 { | |
| 106 | + v.clamp(0, 255) | |
| 107 | +} | |
| 108 | + | |
| 104 | 109 | /// Number of 16×16 macroblock columns/rows covering `width × height` |
| 105 | 110 | /// pixels (the encoded frame is padded up to whole macroblocks). |
| 106 | 111 | pub fn mb_geometry(width: u16, height: u16) -> (usize, usize) { |
crates/ec-vp8/src/intra.rsmodified+3 -9
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | //! row); on the top frame row they are 127. [`predict4`] only consumes |
| 31 | 31 | //! `A[0..8]` as given. |
| 32 | 32 | |
| 33 | +use crate::frame::clamp255; | |
| 33 | 34 | |
| 34 | 35 | /// Weighted average of three adjacent pixels centered at `y` |
| 35 | 36 | /// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`). |
| @@ -48,13 +49,6 @@ fn avg2(x: u8, y: u8) -> u8 { | ||
| 48 | 49 | ((u32::from(x) + u32::from(y) + 1) >> 1) as u8 |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | -/// `clamp255(v)`: clamp an integer pixel difference into the valid `u8` range | |
| 52 | -/// (RFC 6386 Section 12.2 `TMpred` / Section 12.3 `B_TM_PRED`). | |
| 53 | -#[inline] | |
| 54 | -fn clamp255(v: i32) -> u8 { | |
| 55 | - v.clamp(0, 255) as u8 | |
| 56 | -} | |
| 57 | - | |
| 58 | 52 | /// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks |
| 59 | 53 | /// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is |
| 60 | 54 | /// "essentially identical" with 16-pixel edges). |
| @@ -106,7 +100,7 @@ fn predict_block<const N: usize>( | ||
| 106 | 100 | 3 => { |
| 107 | 101 | for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) { |
| 108 | 102 | for (o, &a) in row.iter_mut().zip(above.iter()) { |
| 109 | - *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)); | |
| 103 | + *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left)) as u8; | |
| 110 | 104 | } |
| 111 | 105 | } |
| 112 | 106 | } |
| @@ -215,7 +209,7 @@ pub fn predict4( | ||
| 215 | 209 | for r in 0..4 { |
| 216 | 210 | for c in 0..4 { |
| 217 | 211 | b[r * 4 + c] = |
| 218 | - clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)); | |
| 212 | + clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left)) as u8; | |
| 219 | 213 | } |
| 220 | 214 | } |
| 221 | 215 | } |
crates/ec-vp8/src/mc.rsmodified+1 -5
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | //! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386 |
| 24 | 24 | use archmage::SimdToken; |
| 25 | 25 | use multiversion::target::match_target; |
| 26 | +use crate::frame::clamp255; | |
| 26 | 27 | use crate::modes; |
| 27 | 28 | |
| 28 | 29 | /// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes. |
| @@ -55,11 +56,6 @@ const BILINEAR_FILTERS: [[i32; 2]; 8] = [ | ||
| 55 | 56 | [16, 112], |
| 56 | 57 | ]; |
| 57 | 58 | |
| 58 | -#[inline] | |
| 59 | -fn clamp255(v: i32) -> i32 { | |
| 60 | - v.clamp(0, 255) | |
| 61 | -} | |
| 62 | - | |
| 63 | 59 | /// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border` |
| 64 | 60 | /// ring on all four sides by edge replication: every out-of-image sample |
| 65 | 61 | /// becomes the nearest in-image sample. |