diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 53e0da51..e43dbdef 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) }; } @@ -3940,7 +3961,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 @@ -3993,7 +4014,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)) @@ -4032,7 +4053,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)) @@ -4080,7 +4101,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, @@ -4110,9 +4131,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; } } @@ -4152,7 +4173,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, @@ -4191,9 +4212,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; } } @@ -4425,7 +4446,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), @@ -4433,10 +4458,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 { @@ -5214,7 +5239,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; } } } @@ -5649,7 +5674,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, @@ -5669,10 +5694,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)); @@ -5686,10 +5711,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) = @@ -5709,12 +5734,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) = @@ -5745,7 +5770,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; } } } @@ -6141,7 +6166,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, @@ -6152,7 +6177,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, @@ -6163,7 +6188,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, @@ -6750,9 +6775,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); @@ -6764,7 +6790,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 }; @@ -6794,9 +6820,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, )); @@ -6804,7 +6830,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 }; @@ -7063,7 +7089,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); @@ -7084,13 +7110,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; } } @@ -7116,7 +7142,7 @@ fn read_inter_plane( y: usize, side: usize, base_q_idx: u8, - prediction: &[u8], + prediction: &[u16], // lane-chromau: this block's own luma transform's *actual coded* // `tx_type` -- `av1_get_tx_type` (libaom `blockd.h`): an inter block's // chroma plane never codes its own `tx_type` symbol, but (unlike an @@ -7548,8 +7574,8 @@ fn obmc_neighbour_pred( luma: bool, h_kind: mc::InterpFilterKind, v_kind: mc::InterpFilterKind, -) -> Vec { - let mut out = vec![0u8; w * h]; +) -> Vec { + let mut out = vec![0u16; w * h]; mc::predict_with_filters( &refplane.data, refplane.width, @@ -7596,7 +7622,7 @@ fn interintra_blend( side: usize, ii_mode: u8, wedge: Option<(&'static [u8], usize)>, - pred: &mut [u8], + pred: &mut [u16], ) { // `interintra_to_intra_mode`: II_DC/II_V/II_H/II_SMOOTH. let intra_mode = match ii_mode { @@ -7614,7 +7640,7 @@ fn interintra_blend( below_left: false, }, ); - let mut intra = vec![0u8; side * side]; + let mut intra = vec![0u16; side * side]; // Edge filtering never applies here: V/H at delta 0 are plain edge // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no // angle at all. @@ -7658,33 +7684,33 @@ fn interintra_blend( }); let idx = i * side + j; pred[idx] = - ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u8; + ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16; } } } -fn obmc_blend_v(dst: &mut [u8], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u8]) { +fn obmc_blend_v(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { let mask = obmc_mask(h); for row in 0..h { let m = u32::from(mask[row]); for col in 0..w { let d = &mut dst[(oy + row) * stride + ox + col]; let t = u32::from(tmp[row * w + col]); - *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u8; + *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; } } } /// Blends `tmp` into `dst` at `(ox, oy)`, mask varying by column (the /// left-neighbour pass, `aom_blend_a64_hmask`). -fn obmc_blend_h(dst: &mut [u8], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u8]) { +fn obmc_blend_h(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { let mask = obmc_mask(w); for row in 0..h { for col in 0..w { let m = u32::from(mask[col]); let d = &mut dst[(oy + row) * stride + ox + col]; let t = u32::from(tmp[row * w + col]); - *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u8; + *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; } } } @@ -7723,9 +7749,9 @@ fn obmc_blend( ref_v: &PlaneBuf, other_refs: &RefSlots, interp_fixed: Option, - pred_y: &mut [u8], - pred_u: &mut [u8], - pred_v: &mut [u8], + pred_y: &mut [u16], + pred_u: &mut [u16], + pred_v: &mut [u16], ) -> Result<()> { // lane-rect r2 (libaom av1_build_obmc_inter_prediction): each pass has // its OWN neighbour cap (width-log2 for above, height-log2 for left) and @@ -8787,7 +8813,7 @@ fn decode_inter_block( v_filter, &mut inter1_y, ); - let mut pred_y = vec![0u8; side * side]; + let mut pred_y = vec![0u16; side * side]; let mut diffwtd_mask_y = Vec::new(); // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR // the wedge codebook lookup (comp_group_idx==1's two mutually @@ -8836,7 +8862,7 @@ fn decode_inter_block( v_filter, &mut inter1_u, ); - let mut pred_u = vec![0u8; chroma_side * chroma_side]; + let mut pred_u = vec![0u16; chroma_side * chroma_side]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_u, @@ -8880,7 +8906,7 @@ fn decode_inter_block( v_filter, &mut inter1_v, ); - let mut pred_v = vec![0u8; chroma_side * chroma_side]; + let mut pred_v = vec![0u16; chroma_side * chroma_side]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_v, @@ -9433,7 +9459,7 @@ fn decode_inter_block( mode_for_tx = 0; uv_predict_mode = DC_PRED; - let mut pred_y = vec![0u8; side * side]; + let mut pred_y = vec![0u16; side * side]; mc::predict_with_filters( &py_ref.data, py_ref.width, @@ -9447,7 +9473,7 @@ fn decode_inter_block( v_filter, &mut pred_y, ); - let mut pred_u = vec![0u8; chroma_side * chroma_side]; + let mut pred_u = vec![0u16; chroma_side * chroma_side]; mc::predict_with_filters( &pu_ref.data, pu_ref.width, @@ -9461,7 +9487,7 @@ fn decode_inter_block( v_filter, &mut pred_u, ); - let mut pred_v = vec![0u8; chroma_side * chroma_side]; + let mut pred_v = vec![0u16; chroma_side * chroma_side]; mc::predict_with_filters( &pv_ref.data, pv_ref.width, @@ -10248,7 +10274,7 @@ fn decode_inter_block8( Regular, &mut inter1_y, ); - let mut pred_y = vec![0u8; SIDE * SIDE]; + let mut pred_y = vec![0u16; SIDE * SIDE]; let mut diffwtd_mask_y = Vec::new(); let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { diffwtd_mask_y = vec![0u8; SIDE * SIDE]; @@ -10293,7 +10319,7 @@ fn decode_inter_block8( Regular, &mut inter1_u, ); - let mut pred_u = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; + let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_u, @@ -10337,7 +10363,7 @@ fn decode_inter_block8( Regular, &mut inter1_v, ); - let mut pred_v = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; + let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_v, @@ -10604,7 +10630,7 @@ fn decode_inter_block8( } mode_for_tx = 0; - let mut pred_y = vec![0u8; SIDE * SIDE]; + let mut pred_y = vec![0u16; SIDE * SIDE]; mc::predict( &ref_y.data, ref_y.width, @@ -10616,7 +10642,7 @@ fn decode_inter_block8( SIDE, &mut pred_y, ); - let mut pred_u = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; + let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; mc::predict( &ref_u.data, ref_u.width, @@ -10628,7 +10654,7 @@ fn decode_inter_block8( CHROMA_SIDE, &mut pred_u, ); - let mut pred_v = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; + let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; mc::predict( &ref_v.data, ref_v.width, @@ -11101,7 +11127,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, @@ -11112,7 +11138,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, @@ -11123,7 +11149,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, @@ -11146,7 +11172,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, @@ -11157,7 +11183,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, @@ -11168,7 +11194,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, @@ -11185,7 +11211,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, @@ -11196,7 +11222,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, @@ -11207,7 +11233,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, @@ -12530,9 +12556,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); @@ -12564,9 +12591,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, @@ -12575,7 +12602,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 }; @@ -13242,7 +13269,7 @@ mod tests { let data = enc.finish(); let (width, height) = (64usize, 64usize); - let ref_pattern: Vec = (0..width * height).map(|i| (i % 251) as u8).collect(); + let ref_pattern: Vec = (0..width * height).map(|i| (i % 251) as u16).collect(); let ref_plane = |scale: usize| PlaneBuf { data: ref_pattern .iter() @@ -13274,7 +13301,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, @@ -13285,7 +13312,7 @@ mod tests { tile_y1: height, }; let mut u = PlaneBuf { - data: vec![0u8; (width / 2) * (height / 2)], + data: vec![0u16; (width / 2) * (height / 2)], width: width / 2, height: height / 2, true_width: width / 2, @@ -13296,7 +13323,7 @@ mod tests { tile_y1: height / 2, }; let mut v = PlaneBuf { - data: vec![0u8; (width / 2) * (height / 2)], + data: vec![0u16; (width / 2) * (height / 2)], width: width / 2, height: height / 2, true_width: width / 2, @@ -13361,7 +13388,7 @@ mod tests { .unwrap(); let (px, py) = (c * SUB, r * SUB); - let mut want_near = vec![0u8; side * side]; + let mut want_near = vec![0u16; side * side]; crate::mc::predict( &ref_y.data, ref_y.width, @@ -13373,7 +13400,7 @@ mod tests { side, &mut want_near, ); - let mut want_nearest = vec![0u8; side * side]; + let mut want_nearest = vec![0u16; side * side]; crate::mc::predict( &ref_y.data, ref_y.width, @@ -13390,7 +13417,7 @@ mod tests { "test setup: the two candidate MVs must predict differently" ); - let mut got = vec![0u8; side * side]; + let mut got = vec![0u16; side * side]; for row in 0..side { got[row * side..(row + 1) * side].copy_from_slice( &y.data[(py + row) * y.width + px..(py + row) * y.width + px + side], diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index efb73802..45194097 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -196,6 +196,45 @@ impl Picture { } } +/// lane-hbd r2: the encoder's own reconstruction/source buffers stay `u8` +/// (8-bit only, unchanged this round -- widening the encoder is out of this +/// round's scope, see `lanes/hbd-r2.report.md`); this is the local box that +/// lets it keep calling [`crate::intra::predict`] now that shape is `u16` +/// in and out. A block-sized round-trip through `u16` is exact for 8-bit +/// content (every value fits both ways), so this changes no encoder output. +#[allow(clippy::too_many_arguments)] +fn intra_predict_u8( + mode: u8, + angle_delta: i32, + above: Option<&[u8]>, + left: Option<&[u8]>, + corner: Option, + bw: usize, + bh: usize, + enable_edge_filter: bool, + smooth_neighbor: bool, + dst: &mut [u8], +) { + let above16: Option> = above.map(|s| s.iter().map(|&v| u16::from(v)).collect()); + let left16: Option> = left.map(|s| s.iter().map(|&v| u16::from(v)).collect()); + let mut dst16 = vec![0u16; dst.len()]; + crate::intra::predict( + mode, + angle_delta, + above16.as_deref(), + left16.as_deref(), + corner.map(u16::from), + bw, + bh, + enable_edge_filter, + smooth_neighbor, + &mut dst16, + ); + for (d, &s) in dst.iter_mut().zip(dst16.iter()) { + *d = s as u8; + } +} + /// Pads one plane to `(padded_width, padded_height)` by repeating its last /// row and column, so the block coder always sees a whole number of blocks. /// [`crop_plane`] undoes this on the way back out. @@ -597,7 +636,7 @@ impl Plane<'_> { } = at; let (above, left, corner) = self.edges(x, y, side, reach); let mut prediction = vec![0u8; side * side]; - crate::intra::predict( + intra_predict_u8( mode, 0, above.as_deref(), @@ -817,7 +856,7 @@ impl Plane<'_> { .iter() .map(|&mode| { let mut prediction = vec![0u8; side * side]; - crate::intra::predict( + intra_predict_u8( mode, 0, above.as_deref(), @@ -869,7 +908,7 @@ impl Plane<'_> { .iter() .map(|&mode| { let mut prediction = vec![0u8; side * side]; - crate::intra::predict( + intra_predict_u8( mode, 0, above.as_deref(), @@ -2136,9 +2175,13 @@ fn mc_trial( ) -> Trial { let x_q4 = mv_to_q4(x, mv.1, luma); let y_q4 = mv_to_q4(y, mv.0, luma); - let mut prediction = vec![0u8; side * side]; + // lane-hbd r2: `reference`/`prediction` stay `u8` here (encoder is + // 8-bit only this round, see `intra_predict_u8`'s doc comment) -- + // round-trip through `u16` locally to call the now-widened `mc::predict`. + let reference16: Vec = reference.iter().map(|&v| u16::from(v)).collect(); + let mut prediction16 = vec![0u16; side * side]; mc::predict( - reference, + &reference16, stride, ref_width, ref_height, @@ -2146,8 +2189,9 @@ fn mc_trial( y_q4, side, side, - &mut prediction, + &mut prediction16, ); + let prediction: Vec = prediction16.iter().map(|&v| v as u8).collect(); plane.code_from_prediction(x, y, side, &prediction, skip, base_q_idx, deadzone, set) } @@ -4518,7 +4562,7 @@ mod tests { let mut prediction = vec![0u8; side * side]; let t = Instant::now(); for _ in 0..N { - crate::intra::predict( + intra_predict_u8( D45_PRED, 0, Some(&above), 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..b8d37f11 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; } } } @@ -600,7 +600,7 @@ pub fn blend_masked_compound( /// off-by-one from double rounding. #[test] fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { - let reference = vec![100u8; 16 * 16]; + let reference = vec![100u16; 16 * 16]; let mut pred0 = vec![0i32; 16]; predict_compound_intermediate( &reference, @@ -618,7 +618,7 @@ fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { assert!(pred0.iter().all(|&v| v == 1600), "{pred0:?}"); let pred1 = pred0.clone(); - let mut dst = vec![0u8; 16]; + let mut dst = vec![0u16; 16]; combine_compound(&pred0, &pred1, 8, 8, &mut dst); assert!(dst.iter().all(|&v| v == 100), "{dst:?}"); } @@ -670,9 +670,11 @@ mod tests { // real (0, 0) coordinates -- clamping then reproduces the identical // extended border the dump already captured. let stride = 24; - let mut reference = vec![0u8; stride * 24]; + let mut reference = vec![0u16; stride * 24]; for (r, row) in window.iter().enumerate() { - reference[r * stride..r * stride + stride].copy_from_slice(row); + for (c, &v) in row.iter().enumerate() { + reference[r * stride + c] = u16::from(v); + } } // predict()'s own edge clamp starts at true (0,0); shift so the // dumped window's row/col 4 lands there by biasing x_q4/y_q4's @@ -682,7 +684,7 @@ mod tests { // whole-pel part of x_q4/y_q4 by +4 to land on real (0,0). let x_q4 = (4 + 0) * 16 + 15; // real block x0=0, xfrac=15 let y_q4 = (4 + 0) * 16 + 7; // real block y0=0, yfrac=7 - let mut dst = vec![0u8; 16 * 16]; + let mut dst = vec![0u16; 16 * 16]; predict_with_filter( &reference, stride, @@ -696,7 +698,7 @@ mod tests { &mut dst, ); #[rustfmt::skip] - let expected_row0: [u8; 16] = + let expected_row0: [u16; 16] = [114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116]; assert_eq!( &dst[0..16], @@ -704,7 +706,7 @@ mod tests { "row0 vs aomdec's real av1_convolve_2d_sr_c output" ); #[rustfmt::skip] - let expected_row15: [u8; 16] = + let expected_row15: [u16; 16] = [103,103,104,104,104,105,105,106,106,106,107,107,107,107,107,107]; assert_eq!( &dst[15 * 16..15 * 16 + 16], @@ -717,8 +719,8 @@ mod tests { fn integer_mv_is_identity() { let width = 12; let height = 12; - let reference: Vec = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); - let mut dst = vec![0u8; 6 * 6]; + let reference: Vec = (0..width * height).map(|i| (i * 7 % 251) as u16).collect(); + let mut dst = vec![0u16; 6 * 6]; predict( &reference, width, @@ -746,10 +748,10 @@ mod tests { // Step 2 so every half-pel position lands on an exact integer. let width = 16; let height = 16; - let reference: Vec = (0..width * height) - .map(|i| (2 * (i % width)) as u8) + let reference: Vec = (0..width * height) + .map(|i| (2 * (i % width)) as u16) .collect(); - let mut dst = vec![0u8; 4 * 4]; + let mut dst = vec![0u16; 4 * 4]; // Half-pel in x only: fraction 8/16, integer part at column 5. predict( &reference, @@ -778,10 +780,10 @@ mod tests { fn constant_plane_has_unit_dc_gain_at_every_subpel_position() { let width = 20; let height = 20; - let reference = vec![142u8; width * height]; + let reference = vec![142u16; width * height]; for yfrac in 0..16 { for xfrac in 0..16 { - let mut dst = vec![0u8; 5 * 5]; + let mut dst = vec![0u16; 5 * 5]; predict( &reference, width, @@ -810,11 +812,11 @@ mod tests { // it happened to produce two merely-different numbers. let width = 10; let height = 10; - let reference: Vec = (0..height) - .flat_map(|y| (0..width).map(move |x| (20 * x + 4 * y) as u8)) + let reference: Vec = (0..height) + .flat_map(|y| (0..width).map(move |x| (20 * x + 4 * y) as u16)) .collect(); - let mut horiz = vec![0u8; 4 * 4]; + let mut horiz = vec![0u16; 4 * 4]; predict( &reference, width, @@ -826,7 +828,7 @@ mod tests { 4, &mut horiz, ); - let mut vert = vec![0u8; 4 * 4]; + let mut vert = vec![0u16; 4 * 4]; predict( &reference, width, @@ -865,8 +867,8 @@ mod tests { fn mv_past_the_edge_clamps_instead_of_panicking() { let width = 8; let height = 8; - let reference: Vec = (0..width * height).map(|i| (i * 3 % 200) as u8).collect(); - let mut dst = vec![0u8; 4 * 4]; + let reference: Vec = (0..width * height).map(|i| (i * 3 % 200) as u16).collect(); + let mut dst = vec![0u16; 4 * 4]; // Whole-sample position far above and to the left of the plane, with // an odd fraction so both filter passes exercise the clamp. predict( @@ -886,7 +888,7 @@ mod tests { "an MV past the top-left edge must clamp to the corner sample, got {dst:?}" ); - let mut dst2 = vec![0u8; 4 * 4]; + let mut dst2 = vec![0u16; 4 * 4]; predict( &reference, width, diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index 9c9f066f..d3d5cad6 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -263,16 +263,22 @@ fn search_traced_from_step( assert_eq!(source.len(), block_w * block_h, "source is one block"); assert!(!reference.is_empty(), "a reference plane has samples"); - let mut dst = vec![0u8; block_w * block_h]; + // lane-hbd r2: this encoder-side search stays `u8` (see + // `encode::intra_predict_u8`'s doc comment) -- `reference` is converted + // once here, outside the per-candidate closure, since `mc::predict` now + // takes `u16`. + let reference16: Vec = reference.iter().map(|&v| u16::from(v)).collect(); + let mut dst16 = vec![0u16; block_w * block_h]; let mut cost_of = |mv: (i32, i32)| -> f64 { let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; predict( - reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst, + &reference16, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, + &mut dst16, ); let sad: f64 = source .iter() - .zip(dst.iter()) + .zip(dst16.iter()) .map(|(&a, &b)| f64::from((i32::from(a) - i32::from(b)).unsigned_abs())) .sum(); let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1); 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..96e7124b 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: @@ -427,11 +427,11 @@ fn clip_pixel(v: i32) -> u8 { #[allow(clippy::too_many_arguments)] pub fn warp_affine( params: &WarpParams, - reference: &[u8], + reference: &[u16], width: i32, height: i32, stride: i32, - dst: &mut [u8], + dst: &mut [u16], p_col: i32, p_row: i32, p_width: i32, diff --git a/lanes/hbd-r1.charter.md b/lanes/hbd-r1.charter.md new file mode 100644 index 00000000..67ce21f3 --- /dev/null +++ b/lanes/hbd-r1.charter.md @@ -0,0 +1,51 @@ +# lane-hbd r1 — widen the sample type + +Fresh worktree off main (71a2624), suite green at 263/0. This lane replaces +lane-realworld, whose decode work is all merged; only the high-bit-depth job is +left, and it now has a clean branch instead of a stale one. Read +`lanes/realworld-r8.report.md` (site inventory) and `lanes/realworld-r10.report.md` +(the previous attempt) first. + +**Why this matters:** both AV1 films in the user's library are `yuv420p10le` and +stop at the `bit_depth != 8` refusal. This is the critical path to his own files. + +## Start from the saved diff, do not retype it +r10 got `PlaneBuf::data -> Vec` plus all of `intra.rs` type-correct before +its cap, then reverted, saving the 500-line diff at +`/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/baaa03f8-c4ff-4469-8ebb-83100429b150/scratchpad/r10-attempt.diff`. +`git apply` it first. If it no longer applies (main moved: UV palette landed), +re-apply what does and redo the rest by hand — but check before assuming. + +Then, in this order, per r10's own handoff: +1. `restoration.rs`'s `apply_loop_restoration_plane` (still `Vec` in/out, + 3 call sites at `decode.rs` ~5445/5457/5469), two debug-dump narrowing + closures, and `reconstruct_mc`'s clamp — all small and mechanical. +2. `mc.rs` LAST: 6 functions still `&[u8]`/`&mut [u8]`, ~24 `decode.rs` call + sites resolve for free once they widen. Use the `sample_max()` pattern + `intra.rs` proved. +3. `encode.rs`'s 3 call sites into `intra.rs` (~600, ~820, ~872) — the encoder + shares those predictors. r10 found these; they were NOT in the original + inventory. Decide deliberately: widen the encoder's buffers too, or box + u8<->u16 locally at those three sites. Say which and why. + +**8-bit output must stay bit-exact.** The 263-test suite is the regression gate; +run it before every commit. Do NOT lift the `bit_depth != 8` refusal and do not +build a 10-bit gate this round — `ffmpeg_decode_sequence` hardcodes +`-pix_fmt yuv420p`, so a 10-bit gate needs a second helper with u16 LE parsing, +and that plus the first real 10-bit stream is the next round. + +Note from r10: `git checkout` and `git stash` were blocked in that session; +`git apply -R ` is the working revert mechanism if you need one. + +## Budget discipline +75 turns, no reset on resume. At about turn 55, stop starting new work: commit +what compiles and passes, and write your report. Do NOT revert working partial +work to make the report tidier — a compiling, green, partially widened crate is +a fine place to stop. + +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-hbd`; foreground +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; +`EC_AV1_REQUIRE_AOMENC=1`. Do NOT open the user's media in a GUI app — ffmpeg +and `decode_probe` only. Sibling worktrees have live agents — never build in or +edit them. Never push, never merge into main; I handle merges. End with +`lanes/hbd-r1.report.md`, VERDICT on line 1. diff --git a/lanes/hbd-r2.charter.md b/lanes/hbd-r2.charter.md new file mode 100644 index 00000000..042f594a --- /dev/null +++ b/lanes/hbd-r2.charter.md @@ -0,0 +1,47 @@ +# lane-hbd r2 — 42 errors to zero + +At a86872b. r1 applied the saved diff and widened well past it before its cap; +I committed its tree verbatim. **It does not compile: `cargo check -p ec-av1 +--lib` reports 42 errors, 41 of them E0308 type mismatches.** There are no +conflict markers — I checked. + +That number is your progress metric. Drive it to zero. + +## Checkpoint policy for this round — read this, it is different +A half-widened crate cannot compile, so "commit only when green" would mean +committing nothing until the very end, which is exactly how two rounds have died +here. Instead: **commit a `wip(hbd):` checkpoint whenever the error count drops +meaningfully** (say every 10 errors), with the count in the message. A +non-compiling commit is acceptable ONLY on this branch, ONLY with that prefix, +and ONLY while the count is falling — it is never merged in that state. Once the +count hits zero, run the full suite and make a real commit. + +## Order (r10's, still valid) +`restoration.rs`'s `apply_loop_restoration_plane` and its 3 `decode.rs` call +sites; the debug-dump narrowing closures; `reconstruct_mc`'s clamp; then `mc.rs` +LAST — its 6 `&[u8]`/`&mut [u8]` functions unlock ~24 `decode.rs` call sites at +once, using the `sample_max()` pattern `intra.rs` already proves. Then +`encode.rs`'s 3 call sites into `intra.rs`: decide deliberately whether the +encoder's buffers widen too or get a local u8<->u16 box, and say which and why — +the encoder shares those predictors and was not in the original inventory. + +**8-bit output must stay bit-exact.** The 263-test suite is the gate once it +compiles; a single pixel of drift there means a clamp bound or a rounding step +was widened wrong. + +Do NOT lift the `bit_depth != 8` refusal and do not build a 10-bit gate — that +is the next round, and it needs a second `ffmpeg_decode_sequence` helper emitting +`yuv420p10le` with u16 LE parsing. + +`git checkout` and `git stash` have been blocked in these sessions; use +`git apply -R ` if you need to undo something. + +## Budget discipline +75 turns, no reset on resume. At about turn 55, stop starting new work, commit +your checkpoint, and write the report with the current error count in it. + +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-hbd`; foreground +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; +`EC_AV1_REQUIRE_AOMENC=1`. Do NOT open the user's media in a GUI app. Sibling +worktrees have live agents — never build in or edit them. Never push, never merge +into main; I handle merges. End with `lanes/hbd-r2.report.md`, VERDICT on line 1. diff --git a/lanes/hbd-r2.report.md b/lanes/hbd-r2.report.md new file mode 100644 index 00000000..74bf69e0 --- /dev/null +++ b/lanes/hbd-r2.report.md @@ -0,0 +1,102 @@ +VERDICT: Done. `cargo check -p ec-av1 --lib --tests` reports 0 errors (42 -> 0), +and `cargo test -p ec-av1` (`EC_AV1_REQUIRE_AOMENC=1`) is 263 passed, 0 failed, +18 ignored -- 8-bit output is bit-exact. Real commit 213eeac on top of two +`wip(hbd):` checkpoints (177aa1c at 42->5, 6e6dbbb at --lib clean/17 test +errors left). + +## What actually needed doing + +r1's saved diff had already widened `PlaneBuf::data`, intra prediction, and +all six of `mc.rs`'s prediction/blend functions to `u16` -- the charter's own +"mc.rs last, it unlocks ~24 call sites" order was already moot; `mc.rs` was +done. The 42 errors were all downstream call sites that hadn't caught up: + +- `warp.rs`'s `warp_affine` (`reference`/`dst` still `&[u8]`/`&mut [u8]`, + only caller is 3 `decode.rs` sites) -- widened. +- `decode.rs`'s OBMC chain (`obmc_neighbour_pred`, `obmc_blend_v`, + `obmc_blend_h`, `obmc_blend`) and interintra chain (`interintra_blend`, + its local `intra` scratch buffer) -- both fed by the same `pred_y`/ + `pred_u`/`pred_v` buffers `read_inter_plane`'s call sites already needed + widened, so one `vec![0u8; ...]` -> `vec![0u16; ...]` sweep across those + buffer declarations (12 sites) plus the two function chains closed 37 of + the 42 lib errors. +- `read_inter_plane`'s own `prediction: &[u8]` parameter -> `&[u16]`. +- `encode.rs` (3 call sites into `intra::predict`) and `motion.rs` (1 call + site into `mc::predict`) -- see decision below. + +## The encoder decision (charter asked for one) + +The encoder's own buffers (`Plane::source`, `Plane::reconstruction`, the +motion-search `reference`/`source`) stay `u8` -- this round does not widen +the encoder, and nothing forces it to: the encoder is 8-bit-only by design +(`Picture`'s `y`/`u`/`v` are still `Vec`, unchanged this round), so there +is no wider content for it to ever hold. Widening those buffers to `u16` +would have added real diff for zero behavioural change and speculative +10-bit-encode plumbing nobody asked for. + +Instead, each of the 4 sites gets a local `u8<->u16` round-trip box right at +the call into the now-widened shared `intra::predict`/`mc::predict`: +- `encode.rs`: `intra_predict_u8` (new free fn, `crates/ec-av1/src/encode.rs` + near `pad_plane`) wraps `intra::predict`, used at all 4 of encode.rs's + `crate::intra::predict` call sites (`trial`, `intra_scores`, + `search_block`, and a `#[cfg(test)]` micro-bench). +- `encode.rs::mc_trial`: inline conversion around its one `mc::predict` call. +- `motion.rs::search_traced_from_step`: `reference` converted once, outside + the per-MV-candidate closure (not per-call), so the search's inner loop + cost is unchanged. + +A block-sized `u8`->`u16`->`u8` round-trip is exact for 8-bit content (every +value fits both ways with no rounding), so none of this changes encoder +output -- and the RD-trial/motion-search test suite (`encoder::tests::*`, +`motion` module) stayed green with no changes needed. + +## Test-only fixes (not caught by `--lib`) + +`--lib` hit 0 errors after the above; `--tests` still had 17, all fixture +buffers in `mc.rs`'s and `decode.rs`'s own unit tests declared `Vec`/ +`vec![0u8; ...]` and calling the now-`u16` `predict`/`predict_with_filter`/ +`combine_compound`. Mechanical widen of the fixture types (one hand-written +`u8` window array in `mc.rs`'s +`smooth_filter_matches_aomdec_chroma_inter_block` stays `u8` as the aomdec +dump's own literal shape, copied element-wise into a `u16` reference plane +instead of `copy_from_slice`). No test assertion changed shape, only types. + +## Files touched + +- `crates/ec-av1/src/warp.rs` -- `warp_affine` signature. +- `crates/ec-av1/src/decode.rs` -- `read_inter_plane` signature; OBMC chain; + interintra chain; 12 `pred_y`/`pred_u`/`pred_v` buffer declarations across + 4 near-identical inter-block-decode sites; one unit test's fixture types. +- `crates/ec-av1/src/mc.rs` -- unit test fixture types only (functions were + already widened). +- `crates/ec-av1/src/encode.rs` -- new `intra_predict_u8` helper + 4 call + sites; `mc_trial`'s inline conversion. +- `crates/ec-av1/src/motion.rs` -- `search_traced_from_step`'s inline + conversion. + +## Checks run + +- `cargo check -p ec-av1 --lib -j4` -- 0 errors (was 42 at round start). +- `cargo check -p ec-av1 --tests -j4` -- 0 errors. +- `cargo check -p ec-hw --lib -j4` -- 0 errors (only other in-tree crate + depending on ec-av1; no `PlaneBuf`/`mc`/`intra` internals are exported + across that boundary, confirmed by a clean check with no `ec-av1` diff + visible to it). +- `cargo test -p ec-av1 -j4` (`EC_AV1_REQUIRE_AOMENC=1`): **263 passed, 0 + failed, 18 ignored**, 177.58s. Every real-aomenc gate (obmc, masked + compound, reference-select, restoration, temporal MVs, warp, superres) + still fires and still matches pixel-exact -- 8-bit decode/encode output is + unchanged. + +## Scope respected + +`bit_depth != 8` refusal untouched (not searched for/touched this round); +no 10-bit gate, no second `ffmpeg_decode_sequence` helper -- both explicitly +next round's job per the charter. + +## Budget + +Landed well inside the 75-turn budget (checkpoint-then-converge discipline +wasn't needed this round; the crate reached 0 errors and a green suite in +one pass). Two `wip(hbd):` checkpoints exist on the branch per the round's +policy even though the whole thing closed in one sitting. diff --git a/lanes/realworld-r10.report.md b/lanes/realworld-r10.report.md new file mode 100644 index 00000000..b4b8dd9e --- /dev/null +++ b/lanes/realworld-r10.report.md @@ -0,0 +1,127 @@ +VERDICT: No commit this round. The `u16` widen of `PlaneBuf::data` was attempted, +got substantially further than r9 (67 -> ~35 remaining `cargo check` errors, +`decode.rs`'s own internal sites plus all of `intra.rs` fully widened and +type-correct on their own), but did not reach a compiling state inside the +turn budget. Per the charter's own rule ("a non-compiling [crate] is not [a +fine place to stop]"), the attempt was reversed with `git apply -R` against a +saved diff rather than committed half-wired. Working tree is clean, at +b8ad140 + the r10 charter commit c7b3fa0, `cargo check -p ec-av1 --lib` +clean, matching the round-start state (262/0 suite, unexercised this round +since nothing changed). + +## What was reached this round (and reverted) + +`PlaneBuf::data: Vec` -> `Vec` (`decode.rs:3428`), `sample_max()` +made `pub(crate)` for cross-module reuse, then the checker-driven sweep: + +- `PlaneBuf::edges`/`edges_rect` return types -> `(Option>, + Option>, Option)`. +- `PlaneBuf::reconstruct`/`reconstruct_rect`: prediction buffer `vec![0u16; + ..]`, both clamp-and-write sites -> `.clamp(0, sample_max()) as u16`. +- `PALETTE_PRED` thread-local widened to `Vec` (its `colors` source was + already `u16`, this dropped a stale `as u8` truncation). +- CDEF's single write-back site (`decode.rs:4687`) -> `sample_max()`/`u16`. +- The whole deblock `filter_edge` function (`decode.rs:5121`, + 4/6/8/14-tap taps, ~14 write sites) -> `data: &mut [u16]`, every + `.clamp(0, 255) as u8` -> `.clamp(0, sample_max()) as u16`. +- Every `PlaneBuf { data: vec![0u8; ...], ... }` zero-init (key frame, inter + frame, and one more decode entry point — 3 sites) -> `vec![0u16; ...]`. +- Reference-frame ingestion (`Picture.y/u/v` -> `PlaneBuf.data`, 6 struct + literals: `reference.{y,u,v}` and the per-slot `g.{y,u,v}` loop) -> + `.iter().map(|&s| s as u16).collect()` (widening in from `Picture`, which + stays `u8` this stage). +- The two `PlaneBuf -> Picture` narrowing points (both `y/u/v: y.data, ...` + struct literals) -> `.into_iter().map(|s| s as u8).collect()` (narrowing + out to `Picture`, which stays `u8` this stage — this is the exact site r8's + report named as still open, now closed). +- `intra.rs` fully widened end to end: `Edges::build`, `predict`, + `directional`, `dc`, `predict_filter_intra` all take/return `&[u16]`/ + `Option`/`&mut [u16]` now, every `.clamp(0, 255) as u8` (5 sites, + including 2 internal accumulator clamps that were previously untouched) + -> `.clamp(0, crate::decode::sample_max()) as u16`. Its own `#[cfg(test)]` + module (`fill`/`checksum`/the two `predict()` call sites) updated in step; + this file alone type-checked clean in isolation before the wider sweep + continued. + +That diff (500 lines) is saved at +`/tmp/claude-1000/.../scratchpad/r10-attempt.diff` for the next round to +replay rather than redo by hand — it is NOT applied to the tree right now. + +## Exact remaining error clusters (the live checklist, ~35 sites when this +round stopped, `cargo check -p ec-av1 --lib` against the PlaneBuf-widened +tree) + +- `crates/ec-av1/src/restoration.rs`'s `apply_loop_restoration_plane` still + takes/returns `Vec` (`decode.rs:5445/5457/5469`, the 3 call sites that + assign its result into `y.data`/`u.data`/`v.data`) — untouched this round, + not inspected past its signature. +- The two remaining debug-dump `write_all(&y.data)` blocks + (`decode.rs:6165-6167` and `12109-12111`, `EC_AV1_PREFILT_DUMP`) and the + `crop_wide`/`crop` closures (`6174`-ish and `6209`/`12151`-ish, both build + a cropped `Vec` from `plane.data`) still assume `u8` — these are + debug/superres-crop paths, need the same `.iter().map(|&s| s as u8)` + narrowing pattern already used at the two `Picture` literals. +- `decode.rs:6520` — `reconstruct_mc`'s own `prediction: &[u8]` param + its + `.clamp(0, 255) as u8` write, the same pattern as `reconstruct`/ + `reconstruct_rect` above it, not yet touched. +- `decode.rs:6996`/`7071` and `8242` through `10202` (~24 call sites, + `mc::predict`/`predict_with_filter(s)`/`predict_compound_intermediate`/ + `combine_compound`/`blend_masked_compound`, plus `interintra_blend`'s own + `pred: &mut [u8]` and its internal `vec![0u8; side*side]` intra buffer at + `~7040`) — `mc.rs` itself (907 lines, `predict`/`predict_with_filter(s)`/ + `predict_compound_intermediate`/`combine_compound`/ + `blend_masked_compound`, all six still `&[u8]`/`&mut [u8]`) is the one + big remaining piece; the charter's own note that a prior round widened + `mc.rs` alone in ~10 tool calls (using the same `sample_max()` pattern + just proven out in `intra.rs` this round) makes it the right next step — + do it FIRST next round since PlaneBuf is already committed by then and + `cargo check` will point at exactly these call sites. +- `crates/ec-av1/src/encode.rs:600/820/872` — three call sites into + `intra::predict`/`predict_filter_intra` from the ENCODER side, not + inventoried by r8 or the charter at all (a new site class this round's + attempt surfaced: the encoder shares `intra.rs` with the decoder, so + widening `intra.rs` fans out into `encode.rs` too, still on `u8` sample + buffers there — untouched, not even read this round; next round needs to + either widen `encode.rs`'s own picture buffers in step or box the u8<->u16 + conversion at those three call sites specifically since the encoder isn't + in this charter's scope). +- `warp.rs`, `superres.rs` — never reached (errors upstream of them all still + open); charter's own inventory (1 clamp-and-cast site each) is presumably + still accurate but unverified this round. + +## Why this round stopped short + +Turn-budget math: intra.rs's full widen (13 signature/body edits across a +970-line file) and decode.rs's own ~20-site internal sweep both landed +clean and are real, reusable progress — verified by literally getting the +error count from 67 to ~35 with `cargo check` as the checklist, exactly as +charter directed. What remained (`restoration.rs`, `mc.rs`, the newly +discovered `encode.rs` fan-out, two debug-dump closures) was correctly +estimated as too large to land AND leave green before the ~turn-55 stop +point, so the attempt was reversed rather than committed non-compiling. + +## Recommendation for r11 + +1. Re-apply `/tmp/claude-1000/.../scratchpad/r10-attempt.diff` (git apply, + not by hand) as the starting point — it is the PlaneBuf widen + + `intra.rs` widen, both proven type-correct in isolation. +2. Fix `restoration.rs`'s `apply_loop_restoration_plane` (small, self + contained — likely one function). +3. Fix the two debug-dump narrowing closures + `reconstruct_mc`'s clamp + (small, same pattern as everything already done). +4. Widen `mc.rs` LAST as the charter said, following the exact pattern this + round proved out in `intra.rs` (`sample_max()`, no other new machinery + needed) — expect ~24 decode.rs call sites to resolve for free once its + six functions take `&[u16]`/`&mut [u16]`. +5. New this round: budget a pass over `encode.rs`'s 3 call sites into + `intra.rs` — decide whether the encoder's own picture buffers widen too + (likely no, out of this charter's scope) or whether those 3 sites get a + local u8<->u16 box. +6. Run the 262-test suite before committing (per charter); it was NOT run + this round since nothing new landed to test. + +## Merge note + +`git status` clean; nothing to merge. No refusal strings touched. `intra.rs` +and `decode.rs` are byte-identical to c7b3fa0 (`git apply -R` verified with +a clean `cargo check -p ec-av1 --lib`, matching the round-start baseline). diff --git a/lanes/realworld-r8.report.md b/lanes/realworld-r8.report.md new file mode 100644 index 00000000..aa479251 --- /dev/null +++ b/lanes/realworld-r8.report.md @@ -0,0 +1,98 @@ +VERDICT: Stage 1 partially landed and committed at bd91ad0 (probe + BIT_DEPTH +thread-local + dequant call-site threading). The `u16` widening of +`PlaneBuf`/prediction buffers (item 2 of the charter) was attempted this round, +proved larger than the remaining turn budget, and was REVERTED rather than +left half-wired — this is the charter's own explicitly-sanctioned outcome +("half-wired is the one unacceptable outcome; incomplete-but-named is fine"). + +## What's landed (bd91ad0, prior round) + +- Probed both real films: `ReelB...mkv` -> 3840x1608, `yuv420p10le`, + HDR10 mastering-display metadata present; `ReelC...mkv` -> 1920x792, + `yuv420p10le`, bt709. Neither ffmpeg header dump showed film grain. Both + still stop at the `bit_depth != 8` refusal, as expected. +- `BIT_DEPTH` thread-local + `sample_max()` helper (decode.rs), set once per + frame from the sequence header's real `color_config.bit_depth` in + `stream.rs`'s decode loop. +- The real `bit_depth` threaded into the `dequant_and_inverse_typed*`/`_wh` + call sites that hardcoded `8`. +- Refusal is still in place, so behaviour is provably unchanged for every + existing (8-bit) stream — this increment is inert until PlaneBuf is + widened and the refusal comes off. + +## What was attempted and reverted this round + +Widened `mc.rs`'s prediction/reference functions (`sample`, `predict`, +`predict_with_filter(s)`, `predict_compound_intermediate`, `combine_compound`, +`blend_masked_compound`) from `&[u8]`/`&mut [u8]` to `&[u16]`/`&mut [u16]`, +clamped against `crate::decode::sample_max()` instead of the literal `255`, +and made `sample_max()` `pub(crate)` so `mc.rs` could call it. That much is a +clean, self-contained, mechanically-correct sub-step (`mc.rs`'s own unit test +was updated in step with it). + +It does NOT compile standing alone, because `PlaneBuf::data` (decode.rs:3425) +— every caller of `mc::predict`/`combine_compound`/etc.'s actual argument +type — is still `Vec`. Starting the PlaneBuf widen (`data: Vec`) +immediately fans out: `edges()` returns `(Option>, Option>, +Option)` consumed by `intra::predict`, `reconstruct`/`reconstruct_rect`/ +`reconstruct_mc`/`reconstruct_mc_rect` all take/write `&[u8]`/`u8` and +`.clamp(0, 255) as u8`, CDEF and ~14 deblock taps do the same, `warp.rs` and +`superres.rs` each have one more clamp-and-cast site, and the final +`PlaneBuf -> Picture` conversion needs an explicit `as u8` narrowing added +back in (Picture's own `y`/`u`/`v` stay `Vec` in this stage — lifting +that too is stage 2's job, tied to the pixel-format helper `stream.rs`'s own +gate needs). `cargo check -p ec-av1 --lib --tests` on the attempt: **34 +library errors + 40 additional test errors**, all `E0308` mismatched-types / +wrong-argument-type from that one root cause (`PlaneBuf::data` untouched) +propagating through every call site, plus 2 `u16` vs `u8` comparison errors +in existing tests. + +Given the turn budget remaining when this was discovered (partway through a +55-tool-call-deep session), finishing all ~30+ remaining sites blind risked +landing exactly the state the charter calls out by name as unacceptable — a +compiling-looking but subtly wrong half-widen, or an out-of-budget commit +with the crate not compiling at all. Reverted with `git checkout -- +crates/ec-av1/src/decode.rs crates/ec-av1/src/mc.rs`, back to the clean, +green bd91ad0. No uncommitted work lost invisibly: the attempt was stashed +first, popped back, inspected, and only then discarded once its scope was +confirmed too large for the remaining budget — worth stating plainly rather +than silently. + +## Exact sites still assuming 8 bits (for the next round) + +- `decode.rs:3425` `PlaneBuf::data: Vec` — the root type, must go first. +- `decode.rs` `PlaneBuf::edges()` (~3472) — returns `(Option>, + Option>, Option)`, consumed by `intra::predict`. +- `decode.rs` `PlaneBuf::reconstruct`/`reconstruct_rect` and + `reconstruct_mc`/`reconstruct_mc_rect` (~6404 onward) — `prediction: &[u8]` + params, `.clamp(0, 255) as u8` writes (this is the ~30-site sweep the + charter named; 22 `Vec`/`&[u8]`/`&mut [u8]` occurrences counted in + decode.rs alone this round, plus 4 in `intra.rs`, 1 each in `warp.rs` and + `superres.rs`). +- CDEF's own reconstruction write and the ~14 deblock taps (decode.rs, + un-enumerated this round — same `.clamp(0, 255) as u8` pattern, grep it). +- `mc.rs` — DONE this round in the attempt (stashed then reverted along with + everything else since it doesn't compile alone); the diff is not preserved + anywhere durable, so the next round redoes this specific file, but it is + the smallest, most mechanical piece and took under 10 tool calls the first + time. +- The final `PlaneBuf -> Picture` narrowing point (wherever a finished + `PlaneBuf` is copied into `Picture.y`/`.u`/`.v`) needs an explicit `as u8` + added, since `Picture` itself stays 8-bit output this stage. + +## Recommendation for the next round + +Do the PlaneBuf widen (`data: Vec`) FIRST, in its own commit-sized step, +before touching `mc.rs` again — the errors it produces are exactly the map +of every remaining site (`cargo check` becomes the sweep's own checklist). +Budget the whole stage 1 as its own round with nothing else assigned, since +one afternoon's attempt (this round, ~20 tool calls) got through `mc.rs` +alone before running short. + +## Merge note + +No refusal strings changed this round (the bit_depth refusal from r7 is +untouched). `crates/ec-av1/src/decode.rs` and `crates/ec-av1/src/mc.rs` are +both back at their bd91ad0 committed content — `git status` clean, nothing +new to merge from this session beyond bd91ad0 (already committed) and this +report.