From 31976a111588a980679372e31c09b375d3a3a297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 18:43:27 +0300 Subject: [PATCH] wip(hbd): r1's u16 widen in flight at the cap (does not compile; error count is the progress metric) --- crates/ec-av1/src/decode.rs | 151 ++++++++++++++++++------------- crates/ec-av1/src/intra.rs | 54 +++++------ crates/ec-av1/src/mc.rs | 28 +++--- crates/ec-av1/src/restoration.rs | 36 ++++---- crates/ec-av1/src/warp.rs | 4 +- 5 files changed, 150 insertions(+), 123 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d85fadde..29e4d1d3 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -76,6 +76,27 @@ pub(crate) fn filter_intra_hits() -> usize { thread_local! { static CDEF_BITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } +// lane-realworld r8: the sequence header's `color_config.bit_depth`, set +// once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs` +// call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still +// blocks every non-8-bit stream from reaching a decode call, so this is `8` +// on every path that runs today -- threading it now (rather than the literal +// `8`) is stage 1 of widening past that refusal without re-deriving every +// clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of +// depth (this decoder's own internal choice, matches dav1d/libaom). +thread_local! { + static BIT_DEPTH: std::cell::Cell = const { std::cell::Cell::new(8) }; +} +/// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current +/// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`. +pub(crate) fn sample_max() -> i32 { + (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 1 +} +/// Sets [`BIT_DEPTH`] for the current thread, called once per frame from +/// [`crate::stream`]'s decode loop. +pub(crate) fn set_bit_depth(bit_depth: u8) { + BIT_DEPTH.with(|c| c.set(bit_depth)); +} // Whether this superblock's single CDEF unit (this decoder has no 128x128 // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal // read -- reset to `false` at the top of each superblock in the two tile @@ -496,7 +517,7 @@ thread_local! { /// alternative this avoids). Always empty again once that call consumes /// it (`.take()`), so a non-palette block's own `reconstruct` call never /// sees stale state. - static PALETTE_PRED: std::cell::RefCell>> = + static PALETTE_PRED: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; } @@ -3578,7 +3599,7 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. #[derive(Clone)] struct PlaneBuf { - data: Vec, + data: Vec, width: usize, height: usize, /// The frame's true, decodable extent in this plane's own units — past @@ -3631,7 +3652,7 @@ impl PlaneBuf { y: usize, side: usize, reach: Reach, - ) -> (Option>, Option>, Option) { + ) -> (Option>, Option>, Option) { let own_across = x + side.min(self.true_width.saturating_sub(x)); let across = if reach.above_right { own_across + side.min(self.true_width.saturating_sub(own_across)) @@ -3670,7 +3691,7 @@ impl PlaneBuf { bw: usize, bh: usize, reach: Reach, - ) -> (Option>, Option>, Option) { + ) -> (Option>, Option>, Option) { let own_across = x + bw.min(self.true_width.saturating_sub(x)); let across = if reach.above_right { own_across + bw.min(self.true_width.saturating_sub(own_across)) @@ -3718,7 +3739,7 @@ impl PlaneBuf { smooth_neighbor: bool, ) { let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); - let mut prediction = vec![0u8; bw * bh]; + let mut prediction = vec![0u16; bw * bh]; if let Some(fi_mode) = filter_intra { crate::intra::predict_filter_intra( fi_mode, @@ -3748,9 +3769,9 @@ impl PlaneBuf { let idx = row * bw + col; let mut base = i32::from(prediction[idx]); if let Some((alpha_q3, ac_q3)) = cfl { - base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255); + base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); } - let sample = (base + residual[idx]).clamp(0, 255) as u8; + let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; self.data[(y + row) * self.width + x + col] = sample; } } @@ -3790,7 +3811,7 @@ impl PlaneBuf { buf } else { let (above, left, corner) = self.edges(x, y, side, reach); - let mut prediction = vec![0u8; side * side]; + let mut prediction = vec![0u16; side * side]; if let Some(fi_mode) = filter_intra { crate::intra::predict_filter_intra( fi_mode, @@ -3829,9 +3850,9 @@ impl PlaneBuf { let idx = row * side + col; let mut base = i32::from(prediction[idx]); if let Some((alpha_q3, ac_q3)) = cfl { - base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255); + base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); } - let sample = (base + residual[idx]).clamp(0, 255) as u8; + let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; self.data[(y + row) * self.width + x + col] = sample; } } @@ -4062,7 +4083,11 @@ fn decode_block( "a palette block with a split luma transform (round 1)", )); } - let buf: Vec = py.map.iter().map(|&idx| py.colors[idx as usize] as u8).collect(); + let buf: Vec = py + .map + .iter() + .map(|&idx| py.colors[idx as usize]) + .collect(); PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); } // A UV-palette block's own chroma prediction override (lane-palette2 r1), @@ -4070,10 +4095,10 @@ fn decode_block( // plane's own `reconstruct`/`read_plane` call below (never both planes at // once), since Y's own set-then-take already emptied the slot by the time // either of these run. - let palette_uv_bufs: Option<(Vec, Vec)> = palette_uv.as_ref().map(|puv| { + let palette_uv_bufs: Option<(Vec, Vec)> = palette_uv.as_ref().map(|puv| { ( - puv.map.iter().map(|&idx| puv.u_colors[idx as usize] as u8).collect(), - puv.map.iter().map(|&idx| puv.v_colors[idx as usize] as u8).collect(), + puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), + puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), ) }); if skip { @@ -4851,7 +4876,7 @@ fn cdef_filter_block( if clipping_required { y = y.clamp(min, max); } - dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, 255) as u8; + dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, sample_max()) as u16; } } } @@ -5286,7 +5311,7 @@ fn filter14( /// the next along the edge's own filtering direction. #[allow(clippy::too_many_arguments)] fn filter_edge( - data: &mut [u8], + data: &mut [u16], base: usize, outer_stride: isize, tap_stride: isize, @@ -5306,10 +5331,10 @@ fn filter_edge( && (q1 - q0).abs() <= limit && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1); - data[idx(-2)] = op1.clamp(0, 255) as u8; - data[idx(-1)] = op0.clamp(0, 255) as u8; - data[idx(0)] = oq0.clamp(0, 255) as u8; - data[idx(1)] = oq1.clamp(0, 255) as u8; + data[idx(-2)] = op1.clamp(0, sample_max()) as u16; + data[idx(-1)] = op0.clamp(0, sample_max()) as u16; + data[idx(0)] = oq0.clamp(0, sample_max()) as u16; + data[idx(1)] = oq1.clamp(0, sample_max()) as u16; } 6 => { let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2)); @@ -5323,10 +5348,10 @@ fn filter_edge( && (p2 - p0).abs() <= 1 && (q2 - q0).abs() <= 1; let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2); - data[idx(-2)] = op1.clamp(0, 255) as u8; - data[idx(-1)] = op0.clamp(0, 255) as u8; - data[idx(0)] = oq0.clamp(0, 255) as u8; - data[idx(1)] = oq1.clamp(0, 255) as u8; + data[idx(-2)] = op1.clamp(0, sample_max()) as u16; + data[idx(-1)] = op0.clamp(0, sample_max()) as u16; + data[idx(0)] = oq0.clamp(0, sample_max()) as u16; + data[idx(1)] = oq1.clamp(0, sample_max()) as u16; } 8 => { let (p3, p2, p1, p0, q0, q1, q2, q3) = @@ -5346,12 +5371,12 @@ fn filter_edge( && (q3 - q0).abs() <= 1; let [op2, op1, op0, oq0, oq1, oq2] = filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3); - data[idx(-3)] = op2.clamp(0, 255) as u8; - data[idx(-2)] = op1.clamp(0, 255) as u8; - data[idx(-1)] = op0.clamp(0, 255) as u8; - data[idx(0)] = oq0.clamp(0, 255) as u8; - data[idx(1)] = oq1.clamp(0, 255) as u8; - data[idx(2)] = oq2.clamp(0, 255) as u8; + data[idx(-3)] = op2.clamp(0, sample_max()) as u16; + data[idx(-2)] = op1.clamp(0, sample_max()) as u16; + data[idx(-1)] = op0.clamp(0, sample_max()) as u16; + data[idx(0)] = oq0.clamp(0, sample_max()) as u16; + data[idx(1)] = oq1.clamp(0, sample_max()) as u16; + data[idx(2)] = oq2.clamp(0, sample_max()) as u16; } _ => { let (p6, p5, p4, p3, p2, p1, p0) = @@ -5382,7 +5407,7 @@ fn filter_edge( q6, ); for (k, v) in (-6..=5isize).zip(out) { - data[idx(k)] = v.clamp(0, 255) as u8; + data[idx(k)] = v.clamp(0, sample_max()) as u16; } } } @@ -5778,7 +5803,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); let mut y = PlaneBuf { - data: vec![0u8; width * height], + data: vec![0u16; width * height], width, height, true_width, @@ -5789,7 +5814,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u8; width * height / 4], + data: vec![0u16; width * height / 4], width: width / 2, height: height / 2, true_width: true_width / 2, @@ -5800,7 +5825,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u8; width * height / 4], + data: vec![0u16; width * height / 4], width: width / 2, height: height / 2, true_width: true_width / 2, @@ -6320,9 +6345,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( use std::io::Write; let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let _ = f.write_all(&y.data); - let _ = f.write_all(&u.data); - let _ = f.write_all(&v.data); + let narrow = |p: &PlaneBuf| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; + let _ = f.write_all(&narrow(&y)); + let _ = f.write_all(&narrow(&u)); + let _ = f.write_all(&narrow(&v)); } } else { PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); @@ -6334,7 +6360,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let crop_wide = |plane: &PlaneBuf| -> Vec { let mut out = Vec::with_capacity(plane.true_width * plane.true_height); for row in 0..plane.true_height { - out.extend_from_slice(&plane.data[row * plane.width..][..plane.true_width]); + out.extend(plane.data[row * plane.width..][..plane.true_width].iter().map(|&s| s as u8)); } out }; @@ -6364,9 +6390,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( Picture { width, height, - y: y.data, - u: u.data, - v: v.data, + y: y.data.into_iter().map(|s| s as u8).collect(), + u: u.data.into_iter().map(|s| s as u8).collect(), + v: v.data.into_iter().map(|s| s as u8).collect(), }, result_cdfs, )); @@ -6374,7 +6400,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { - out.extend_from_slice(&plane.data[row * plane.width..][..w]); + out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8)); } out }; @@ -6633,7 +6659,7 @@ impl PlaneBuf { x: usize, y: usize, side: usize, - prediction: &[u8], + prediction: &[u16], residual: &[i32], ) { self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual); @@ -6654,13 +6680,13 @@ impl PlaneBuf { side: usize, w: usize, h: usize, - prediction: &[u8], + prediction: &[u16], residual: &[i32], ) { for row in 0..h { for col in 0..w { let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) - .clamp(0, 255) as u8; + .clamp(0, crate::decode::sample_max()) as u16; self.data[(y + row) * self.width + x + col] = sample; } } @@ -10670,7 +10696,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); let ref_y = PlaneBuf { - data: reference.y.clone(), + data: reference.y.iter().map(|&s| s as u16).collect(), width: reference.width, height: reference.height, true_width: reference.width, @@ -10681,7 +10707,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: reference.height, }; let ref_u = PlaneBuf { - data: reference.u.clone(), + data: reference.u.iter().map(|&s| s as u16).collect(), width: reference.width / 2, height: reference.height / 2, true_width: reference.width / 2, @@ -10692,7 +10718,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: reference.height / 2, }; let ref_v = PlaneBuf { - data: reference.v.clone(), + data: reference.v.iter().map(|&s| s as u16).collect(), width: reference.width / 2, height: reference.height / 2, true_width: reference.width / 2, @@ -10715,7 +10741,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( .map(|g| { ( PlaneBuf { - data: g.y.clone(), + data: g.y.iter().map(|&s| s as u16).collect(), width: g.width, height: g.height, true_width: g.width, @@ -10726,7 +10752,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: g.height, }, PlaneBuf { - data: g.u.clone(), + data: g.u.iter().map(|&s| s as u16).collect(), width: g.width / 2, height: g.height / 2, true_width: g.width / 2, @@ -10737,7 +10763,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: g.height / 2, }, PlaneBuf { - data: g.v.clone(), + data: g.v.iter().map(|&s| s as u16).collect(), width: g.width / 2, height: g.height / 2, true_width: g.width / 2, @@ -10754,7 +10780,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); let mut y = PlaneBuf { - data: vec![0u8; width * height], + data: vec![0u16; width * height], width, height, true_width, @@ -10765,7 +10791,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u8; width * height / 4], + data: vec![0u16; width * height / 4], width: width / 2, height: height / 2, true_width: true_width / 2, @@ -10776,7 +10802,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u8; width * height / 4], + data: vec![0u16; width * height / 4], width: width / 2, height: height / 2, true_width: true_width / 2, @@ -12099,9 +12125,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( use std::io::Write; let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let _ = f.write_all(&y.data); - let _ = f.write_all(&u.data); - let _ = f.write_all(&v.data); + let narrow = |p: &PlaneBuf| -> Vec { p.data.iter().map(|&s| s as u8).collect() }; + let _ = f.write_all(&narrow(&y)); + let _ = f.write_all(&narrow(&u)); + let _ = f.write_all(&narrow(&v)); } } else { PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); @@ -12133,9 +12160,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( Picture { width, height, - y: y.data, - u: u.data, - v: v.data, + y: y.data.into_iter().map(|s| s as u8).collect(), + u: u.data.into_iter().map(|s| s as u8).collect(), + v: v.data.into_iter().map(|s| s as u8).collect(), }, result_cdfs, motion_field, @@ -12144,7 +12171,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { - out.extend_from_slice(&plane.data[row * plane.width..][..w]); + out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8)); } out }; @@ -12843,7 +12870,7 @@ mod tests { let ref_v = ref_plane(2); let mut y = PlaneBuf { - data: vec![0u8; width * height], + data: vec![0u16; width * height], width, height, true_width: width, diff --git a/crates/ec-av1/src/intra.rs b/crates/ec-av1/src/intra.rs index a6bbc698..6c2c07e6 100644 --- a/crates/ec-av1/src/intra.rs +++ b/crates/ec-av1/src/intra.rs @@ -145,14 +145,14 @@ impl Edges { /// and `z3_c`'s `max_base_y` use (libaom `reconintra.c`) -- above and /// left both extend that far regardless of which of `bw`/`bh` is larger. fn build( - above: Option<&[u8]>, - left: Option<&[u8]>, - corner: Option, + above: Option<&[u16]>, + left: Option<&[u16]>, + corner: Option, bw: usize, bh: usize, ) -> Self { let want = bw + bh; - let extend = |samples: &[u8]| { + let extend = |samples: &[u16]| { let mut row: Vec = samples.iter().map(|&s| i32::from(s)).collect(); let last = *row.last().expect("an edge that exists has samples"); row.resize(want, last); @@ -230,14 +230,14 @@ impl Edges { pub fn predict( mode: u8, angle_delta: i32, - above: Option<&[u8]>, - left: Option<&[u8]>, - corner: Option, + above: Option<&[u16]>, + left: Option<&[u16]>, + corner: Option, bw: usize, bh: usize, enable_edge_filter: bool, smooth_neighbor: bool, - dst: &mut [u8], + dst: &mut [u16], ) { assert_eq!(dst.len(), bw * bh, "the destination is the block"); let edges = Edges::build(above, left, corner, bw, bh); @@ -304,7 +304,7 @@ pub fn predict( PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)), other => panic!("intra mode {other} is not one this module predicts"), }; - dst[row * bw + col] = value.clamp(0, 255) as u8; + dst[row * bw + col] = value.clamp(0, crate::decode::sample_max()) as u16; } } } @@ -431,7 +431,7 @@ fn upsample_intra_edge(buf: &[i32]) -> Vec { out[0] = inp[0]; for i in 0..sz { let s = -inp[i] + 9 * inp[i + 1] + 9 * inp[i + 2] - inp[i + 3]; - out[2 * i + 1] = ((s + 8) >> 4).clamp(0, 255); + out[2 * i + 1] = ((s + 8) >> 4).clamp(0, crate::decode::sample_max()); out[2 * i + 2] = inp[i + 2]; } out @@ -452,7 +452,7 @@ fn directional( smooth_neighbor: bool, n_top: usize, n_left: usize, - dst: &mut [u8], + dst: &mut [u16], ) { let (w, h) = (bw as i32, bh as i32); let reach = w + h; // `av1_dr_prediction_z{1,3}_c`'s `max_base_{x,y}` reach. @@ -565,7 +565,7 @@ fn directional( blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1) } }; - dst[(row * w + col) as usize] = value.clamp(0, 255) as u8; + dst[(row * w + col) as usize] = value.clamp(0, crate::decode::sample_max()) as u16; } } } @@ -579,8 +579,8 @@ fn directional( /// before `dc_predictor` averages, so a slice truncated short by the true /// frame edge must be extended by repetition here too, not averaged over its /// own shorter length. -fn dc(above: Option<&[u8]>, left: Option<&[u8]>, bw: usize, bh: usize) -> i32 { - let extend = |samples: &[u8], want: usize| -> Vec { +fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 { + let extend = |samples: &[u16], want: usize| -> Vec { if samples.len() >= want { samples[..want].to_vec() } else { @@ -592,7 +592,7 @@ fn dc(above: Option<&[u8]>, left: Option<&[u8]>, bw: usize, bh: usize) -> i32 { }; let above = above.map(|a| extend(a, bw)); let left = left.map(|l| extend(l, bh)); - let average = |samples: &[u8]| { + let average = |samples: &[u16]| { let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 }; @@ -723,11 +723,11 @@ const FILTER_INTRA_SCALE_BITS: u32 = 4; /// 32x32, and only on square blocks: 4, 8, 16 or 32). pub fn predict_filter_intra( mode: usize, - above: Option<&[u8]>, - left: Option<&[u8]>, - corner: Option, + above: Option<&[u16]>, + left: Option<&[u16]>, + corner: Option, side: usize, - dst: &mut [u8], + dst: &mut [u16], ) { assert_eq!(dst.len(), side * side, "the destination is the block"); assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32"); @@ -759,7 +759,7 @@ pub fn predict_filter_intra( for k in 0..8 { let (r_off, c_off) = (k >> 2, k & 3); let pr: i32 = taps[k].iter().zip(&p).map(|(t, s)| t * s).sum(); - let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, 255); + let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, crate::decode::sample_max()); let idx = (r + r_off) * (side + 1) + c + c_off; buffer[idx] = value; } @@ -769,7 +769,7 @@ pub fn predict_filter_intra( } for row in 0..side { for col in 0..side { - dst[row * side + col] = at(&buffer, row + 1, col + 1) as u8; + dst[row * side + col] = at(&buffer, row + 1, col + 1) as u16; } } } @@ -799,15 +799,15 @@ mod tests { /// Same deterministic synthetic ramp `lanes/intrarect_dump.c`'s `fill` /// generates. - fn fill(n: usize, seed: i32) -> Vec { + fn fill(n: usize, seed: i32) -> Vec { (0..n) - .map(|i| ((i as i32 * 7 + seed * 13 + (i as i32 % 5) * 3).rem_euclid(256)) as u8) + .map(|i| ((i as i32 * 7 + seed * 13 + (i as i32 % 5) * 3).rem_euclid(256)) as u16) .collect() } /// `lanes/intrarect_dump.c`'s `checksum`: a position-weighted sum so a /// wrong value anywhere in the block, not just at index 0, moves it. - fn checksum(dst: &[u8]) -> u64 { + fn checksum(dst: &[u16]) -> u64 { dst.iter() .enumerate() .map(|(i, &v)| u64::from(v) * (i as u64 + 1)) @@ -897,10 +897,10 @@ mod tests { let reach = bw + bh; let above = fill(reach, 1); let left = fill(reach, 2); - let corner = ((bw * 3 + bh * 5) % 256) as u8; + let corner = ((bw * 3 + bh * 5) % 256) as u16; for (name, mode) in [("DC", DC_PRED), ("SMOOTH", SMOOTH_PRED), ("PAETH", PAETH_PRED)] { - let mut dst = vec![0u8; bw * bh]; + let mut dst = vec![0u16; bw * bh]; predict( mode, 0, @@ -928,7 +928,7 @@ mod tests { for &(angle, mode, delta) in &directional_cases { for ef in [false, true] { for sn in [false, true] { - let mut dst = vec![0u8; bw * bh]; + let mut dst = vec![0u16; bw * bh]; predict( mode, delta, diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index 30ad0e42..91de2acf 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -228,7 +228,7 @@ impl InterpFilterKind { /// whatever this encoder's own uncoded padding columns/rows hold. #[allow(clippy::too_many_arguments)] fn sample( - reference: &[u8], + reference: &[u16], stride: usize, true_width: usize, true_height: usize, @@ -258,7 +258,7 @@ fn sample( /// empty. #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape pub fn predict( - reference: &[u8], + reference: &[u16], stride: usize, true_width: usize, true_height: usize, @@ -266,7 +266,7 @@ pub fn predict( y_q4: i32, block_w: usize, block_h: usize, - dst: &mut [u8], + dst: &mut [u16], ) { predict_with_filter( reference, @@ -287,7 +287,7 @@ pub fn predict( /// kernel both directions. #[allow(clippy::too_many_arguments)] pub fn predict_with_filter( - reference: &[u8], + reference: &[u16], stride: usize, true_width: usize, true_height: usize, @@ -296,7 +296,7 @@ pub fn predict_with_filter( block_w: usize, block_h: usize, filter_kind: InterpFilterKind, - dst: &mut [u8], + dst: &mut [u16], ) { predict_with_filters( reference, @@ -319,7 +319,7 @@ pub fn predict_with_filter( /// so the two passes can genuinely differ. #[allow(clippy::too_many_arguments)] pub fn predict_with_filters( - reference: &[u8], + reference: &[u16], stride: usize, true_width: usize, true_height: usize, @@ -329,7 +329,7 @@ pub fn predict_with_filters( block_h: usize, h_kind: InterpFilterKind, v_kind: InterpFilterKind, - dst: &mut [u8], + dst: &mut [u16], ) { assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); @@ -360,7 +360,7 @@ pub fn predict_with_filters( for col in 0..block_w { let x = x0 + col as i32; dst[row * block_w + col] = - sample(reference, stride, true_width, true_height, x, y) as u8; + sample(reference, stride, true_width, true_height, x, y) as u16; } } #[cfg(test)] @@ -403,7 +403,7 @@ pub fn predict_with_filters( for (t, &tap) in v_filter.iter().enumerate() { sum += tap * intermediate[(row + t) * block_w + col]; } - dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; + dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16; } } @@ -445,7 +445,7 @@ const DIST_PRECISION_BITS: u32 = 4; /// empty. #[allow(clippy::too_many_arguments)] pub fn predict_compound_intermediate( - reference: &[u8], + reference: &[u16], stride: usize, true_width: usize, true_height: usize, @@ -515,13 +515,13 @@ pub fn combine_compound( pred1: &[i32], fwd_weight: i32, bck_weight: i32, - dst: &mut [u8], + dst: &mut [u16], ) { assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); assert_eq!(dst.len(), pred0.len(), "the destination is the block"); for i in 0..dst.len() { let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight; - dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, 255) as u8; + dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, crate::decode::sample_max()) as u16; } } @@ -564,7 +564,7 @@ pub fn blend_masked_compound( w: usize, h: usize, subsampled: bool, - dst: &mut [u8], + dst: &mut [u16], ) { assert_eq!(pred0.len(), w * h, "pred0 is the destination-sized block"); assert_eq!(pred1.len(), w * h, "pred1 is the destination-sized block"); @@ -584,7 +584,7 @@ pub fn blend_masked_compound( i32::from(mask[i * mask_stride + j]) }; let res = (m * pred0[i * w + j] + (64 - m) * pred1[i * w + j]) >> 6; - dst[i * w + j] = round2(res, INTER_POST_ROUND).clamp(0, 255) as u8; + dst[i * w + j] = round2(res, INTER_POST_ROUND).clamp(0, crate::decode::sample_max()) as u16; } } } diff --git a/crates/ec-av1/src/restoration.rs b/crates/ec-av1/src/restoration.rs index 05301fda..ac35db2a 100644 --- a/crates/ec-av1/src/restoration.rs +++ b/crates/ec-av1/src/restoration.rs @@ -454,8 +454,8 @@ fn round2(value: i64, shift: u32) -> i64 { /// boundaries do. #[allow(clippy::too_many_arguments)] fn lr_sample( - cdef: &[u8], - deblocked: &[u8], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -512,9 +512,9 @@ fn lr_sample( /// reproduces the same output byte-for-byte. #[allow(clippy::too_many_arguments)] fn apply_wiener_stripe( - out: &mut [u8], - cdef: &[u8], - deblocked: &[u8], + out: &mut [u16], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -551,7 +551,7 @@ fn apply_wiener_stripe( for (t, &tap) in info.vfilter.iter().enumerate() { sum += tap as i64 * inter[(r + t) * w + c] as i64; } - out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, 255) as u8; + out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, crate::decode::sample_max() as i64) as u16; } } } @@ -613,8 +613,8 @@ const SGRPROJ_RST_BITS: u32 = 4; /// reproduces the same values without porting the sliding-window trick. #[allow(clippy::too_many_arguments)] fn compute_ab( - cdef: &[u8], - deblocked: &[u8], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -676,9 +676,9 @@ fn compute_ab( /// combined with the decoded `xqd` weights via `av1_decode_xq`. #[allow(clippy::too_many_arguments)] fn apply_sgrproj_stripe( - out: &mut [u8], - cdef: &[u8], - deblocked: &[u8], + out: &mut [u16], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -807,7 +807,7 @@ fn apply_sgrproj_stripe( ); } } - let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, 255) as u8; + let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, crate::decode::sample_max() as i64) as u16; out[(v_start as i64 + i) as usize * stride + (h_start as i64 + j) as usize] = out_v; } } @@ -822,9 +822,9 @@ fn apply_sgrproj_stripe( /// [`lr_sample`]. #[allow(clippy::too_many_arguments)] fn filter_restoration_unit( - out: &mut [u8], - cdef: &[u8], - deblocked: &[u8], + out: &mut [u16], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -872,8 +872,8 @@ fn filter_restoration_unit( /// place. #[allow(clippy::too_many_arguments)] pub(crate) fn apply_loop_restoration_plane( - cdef: &[u8], - deblocked: &[u8], + cdef: &[u16], + deblocked: &[u16], stride: usize, plane_w: usize, plane_h: usize, @@ -882,7 +882,7 @@ pub(crate) fn apply_loop_restoration_plane( unit_size: u32, grid: &RestorationGrid, plane: usize, -) -> Vec { +) -> Vec { let mut out = cdef.to_vec(); if ftype == RestorationType::None || unit_size == 0 { return out; diff --git a/crates/ec-av1/src/warp.rs b/crates/ec-av1/src/warp.rs index e3228046..3ea87ce8 100644 --- a/crates/ec-av1/src/warp.rs +++ b/crates/ec-av1/src/warp.rs @@ -415,8 +415,8 @@ pub fn global_warp_params(wmmat: [i32; 6]) -> Option { Some(WarpParams { wmmat, alpha, beta, gamma, delta }) } -fn clip_pixel(v: i32) -> u8 { - v.clamp(0, 255) as u8 +fn clip_pixel(v: i32) -> u16 { + v.clamp(0, crate::decode::sample_max()) as u16 } /// `av1_warp_affine_c` (`warped_motion.c`), 8-bit, non-compound only: