diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index e43dbdef..6c83dd81 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -6820,17 +6820,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( Picture { width, height, - 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(), + y: y.data, + u: u.data, + v: v.data, }, result_cdfs, )); } - let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { + let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { - out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8)); + out.extend(plane.data[row * plane.width..][..w].iter().copied()); } out }; @@ -6856,7 +6856,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( static IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); let idx = 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(&yc); + // corner-cut: this diagnostic dump stays 8-bit-narrowed + // regardless of the stream's real bit depth (debug-only, + // never read by a gate); widen if a >8-bit margin dump + // is ever needed. + let _ = f.write_all(&yc.iter().map(|&s| s as u8).collect::>()); } } Some(Picture { @@ -12591,18 +12595,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( Picture { width, height, - 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(), + y: y.data, + u: u.data, + v: v.data, }, result_cdfs, motion_field, )); } - let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { + let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { - out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8)); + out.extend(plane.data[row * plane.width..][..w].iter().copied()); } out }; @@ -12710,10 +12714,10 @@ mod tests { /// planes are byte-exact against the encoder's own reconstruction. fn round_trips(w: usize, h: usize, modes: &[u8]) { use crate::encode::{Encoded, Picture as Pic}; - let mut source = vec![0u8; w * h]; + let mut source = vec![0u16; w * h]; for row in 0..h { for col in 0..w { - source[row * w + col] = ((row * 3 + col * 5) % 251) as u8; + source[row * w + col] = ((row * 3 + col * 5) % 251) as u16; } } let picture = Pic { @@ -12928,9 +12932,9 @@ mod tests { Picture { width, height, - y: out.stdout[..luma].to_vec(), - u: out.stdout[luma..luma + chroma].to_vec(), - v: out.stdout[luma + chroma..].to_vec(), + y: out.stdout[..luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } } @@ -12976,9 +12980,9 @@ mod tests { Picture { width, height, - y: out.stdout[base..base + luma].to_vec(), - u: out.stdout[base + luma..base + luma + chroma].to_vec(), - v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), + y: out.stdout[base..base + luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[base + luma..base + luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[base + luma + chroma..base + frame_bytes].iter().map(|&v| u16::from(v)).collect(), } }) .collect() @@ -13069,14 +13073,14 @@ mod tests { let mut picture = crate::encode::Picture::grey(width, height); for row in 0..height { for col in 0..width { - picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; + picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u16; } } for row in 0..height / 2 { for col in 0..width / 2 { let i = row * width / 2 + col; - picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; - picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; + picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u16; + picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u16; } } picture @@ -13088,15 +13092,15 @@ mod tests { for x in 0..width { let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; let gradient = sx * 200.0 / width as f64; - picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; + picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u16; } } for y in 0..height / 2 { for x in 0..width / 2 { let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; let i = y * width / 2 + x; - picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; - picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; + picture.u[i] = (100 + (sx * 60 / (width / 2))) as u16; + picture.v[i] = (200 - (y * 80 / (height / 2))) as u16; } } picture diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 45194097..b84fdf92 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -81,7 +81,15 @@ pub(crate) fn split_blocks() -> bool { SPLIT_BLOCKS } -/// One 8-bit planar 4:2:0 picture. +/// One planar 4:2:0 picture. `y`/`u`/`v` hold one sample per position as +/// `u16` regardless of bit depth (`crate::decode`'s own `PlaneBuf` has used +/// `u16` throughout since the round-hbd-r1/r2 widen; this is the last +/// narrowing boundary that stayed `u8` -- widened lane-hbd r3 so a 10-bit +/// stream's decode output actually carries 10-bit precision instead of +/// silently losing its low 2 bits). The encoder only ever produces values in +/// `0..=255` (it is still 8-bit-only by design, see `encode.rs`'s r2 +/// decision); `Plane::source` narrows right back to `u8` at its two +/// construction sites, a lossless round trip for that range. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Picture { /// The picture's width in luma samples. @@ -89,11 +97,11 @@ pub struct Picture { /// Its height in luma samples. pub height: usize, /// The luma plane, `width * height` samples in raster order. - pub y: Vec, + pub y: Vec, /// The U plane, at half the width and half the height. - pub u: Vec, + pub u: Vec, /// The V plane, the same shape as U. - pub v: Vec, + pub v: Vec, } impl Picture { @@ -238,14 +246,14 @@ fn intra_predict_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. -fn pad_plane( - source: &[u8], +fn pad_plane( + source: &[T], width: usize, height: usize, padded_width: usize, padded_height: usize, -) -> Vec { - let mut out = vec![0u8; padded_width * padded_height]; +) -> Vec { + let mut out = vec![T::default(); padded_width * padded_height]; for row in 0..padded_height { let src = source[row.min(height - 1) * width..][..width].as_ref(); let dst = &mut out[row * padded_width..][..padded_width]; @@ -258,7 +266,7 @@ fn pad_plane( /// The top-left `width` x `height` region of a plane that is `padded_width` /// wide, which is the render-size crop [`Picture::padded`]'s replication /// exists to let a decoder undo. -fn crop_plane(source: &[u8], padded_width: usize, width: usize, height: usize) -> Vec { +fn crop_plane(source: &[T], padded_width: usize, width: usize, height: usize) -> Vec { let mut out = Vec::with_capacity(width * height); for row in 0..height { out.extend_from_slice(&source[row * padded_width..][..width]); @@ -1750,9 +1758,16 @@ pub(crate) fn encode_key_frame_inner( // Prediction reads no further than this, in each plane's own units -- // see `Plane::true_width`/`true_height`. let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); + // lane-hbd r4: `Picture.y/u/v` widened to `Vec` for the decoder's + // sake (DPB reference slots, 10-bit output); the encoder stays 8-bit by + // design (see `intra_predict_u8`'s doc comment), so narrow the source + // once here. + let picture_y8: Vec = picture.y.iter().map(|&v| v as u8).collect(); + let picture_u8: Vec = picture.u.iter().map(|&v| v as u8).collect(); + let picture_v8: Vec = picture.v.iter().map(|&v| v as u8).collect(); let mut luma = Plane { - source: &picture.y, - reconstruction: vec![128; picture.y.len()], + source: &picture_y8, + reconstruction: vec![128; picture_y8.len()], width: picture.width, height: picture.height, true_width, @@ -1760,16 +1775,16 @@ pub(crate) fn encode_key_frame_inner( }; let mut chroma = [ Plane { - source: &picture.u, - reconstruction: vec![128; picture.u.len()], + source: &picture_u8, + reconstruction: vec![128; picture_u8.len()], width: picture.width / 2, height: picture.height / 2, true_width: true_width / 2, true_height: true_height / 2, }, Plane { - source: &picture.v, - reconstruction: vec![128; picture.v.len()], + source: &picture_v8, + reconstruction: vec![128; picture_v8.len()], width: picture.width / 2, height: picture.height / 2, true_width: true_width / 2, @@ -2006,9 +2021,9 @@ pub(crate) fn encode_key_frame_inner( reconstruction: Picture { width: luma.width, height: luma.height, - y: luma.reconstruction, - u: u.reconstruction, - v: v.reconstruction, + y: luma.reconstruction.iter().map(|&v| u16::from(v)).collect(), + u: u.reconstruction.iter().map(|&v| u16::from(v)).collect(), + v: v.reconstruction.iter().map(|&v| u16::from(v)).collect(), }, tile, mi_cols: header.mi_cols, @@ -2164,7 +2179,7 @@ fn mc_trial( side: usize, mv: (i32, i32), luma: bool, - reference: &[u8], + reference: &[u16], stride: usize, ref_width: usize, ref_height: usize, @@ -2175,13 +2190,12 @@ fn mc_trial( ) -> Trial { let x_q4 = mv_to_q4(x, mv.1, luma); let y_q4 = mv_to_q4(y, mv.0, luma); - // 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(); + // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that + // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is + // 8-bit only this round, see `intra_predict_u8`'s doc comment). let mut prediction16 = vec![0u16; side * side]; mc::predict( - &reference16, + reference, stride, ref_width, ref_height, @@ -2453,10 +2467,13 @@ fn search_inter_block( } let source_block = luma.source_block(x, y, BLOCK); + // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is + // integer-`u8`-scale); narrow the DPB reference plane locally. + let ref_luma_8: Vec = ref_luma.0.iter().map(|&v| v as u8).collect(); #[cfg(test)] let t = std::time::Instant::now(); let found = motion::search( - ref_luma.0, + &ref_luma_8, ref_luma.1, ref_luma.2, ref_luma.3, @@ -2616,9 +2633,14 @@ pub(crate) fn encode_inter_frame( header.render_height = render.1 as u32; let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4); + // lane-hbd r4: encoder stays 8-bit by design; narrow the source once + // here (see `encode_key_frame_inner`). + let picture_y8: Vec = picture.y.iter().map(|&v| v as u8).collect(); + let picture_u8: Vec = picture.u.iter().map(|&v| v as u8).collect(); + let picture_v8: Vec = picture.v.iter().map(|&v| v as u8).collect(); let mut luma = Plane { - source: &picture.y, - reconstruction: vec![128; picture.y.len()], + source: &picture_y8, + reconstruction: vec![128; picture_y8.len()], width: picture.width, height: picture.height, true_width, @@ -2626,16 +2648,16 @@ pub(crate) fn encode_inter_frame( }; let mut chroma = [ Plane { - source: &picture.u, - reconstruction: vec![128; picture.u.len()], + source: &picture_u8, + reconstruction: vec![128; picture_u8.len()], width: picture.width / 2, height: picture.height / 2, true_width: true_width / 2, true_height: true_height / 2, }, Plane { - source: &picture.v, - reconstruction: vec![128; picture.v.len()], + source: &picture_v8, + reconstruction: vec![128; picture_v8.len()], width: picture.width / 2, height: picture.height / 2, true_width: true_width / 2, @@ -2942,9 +2964,9 @@ pub(crate) fn encode_inter_frame( reconstruction: Picture { width: luma.width, height: luma.height, - y: luma.reconstruction, - u: u.reconstruction, - v: v.reconstruction, + y: luma.reconstruction.iter().map(|&v| u16::from(v)).collect(), + u: u.reconstruction.iter().map(|&v| u16::from(v)).collect(), + v: v.reconstruction.iter().map(|&v| u16::from(v)).collect(), }, tile, mi_cols: header.mi_cols, @@ -3082,9 +3104,9 @@ mod tests { Picture { width, height, - y: out.stdout[..luma].to_vec(), - u: out.stdout[luma..luma + chroma].to_vec(), - v: out.stdout[luma + chroma..].to_vec(), + y: out.stdout[..luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } } @@ -3105,20 +3127,20 @@ mod tests { 0.0 }; picture.y[y * width + x] = - (20.0 + gradient + ripple + edge).clamp(0.0, 255.0) as u8; + (20.0 + gradient + ripple + edge).clamp(0.0, 255.0) as u16; } } for y in 0..height / 2 { for x in 0..width / 2 { let i = y * width / 2 + x; - picture.u[i] = (100 + (x * 60 / (width / 2))) as u8; - picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; + picture.u[i] = (100 + (x * 60 / (width / 2))) as u16; + picture.v[i] = (200 - (y * 80 / (height / 2))) as u16; } } picture } - fn psnr(a: &[u8], b: &[u8]) -> f64 { + fn psnr(a: &[u16], b: &[u16]) -> f64 { let squared: f64 = a .iter() .zip(b) @@ -3653,9 +3675,9 @@ mod tests { Picture { width, height, - y: out.stdout[..luma].to_vec(), - u: out.stdout[luma..luma + chroma].to_vec(), - v: out.stdout[luma + chroma..].to_vec(), + y: out.stdout[..luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } } @@ -3775,7 +3797,7 @@ mod tests { /// Where two planes first disagree, and by how much: a mismatch reported as /// a position says which block and which sample of it went wrong, which a /// pair of thousand-sample arrays does not. - fn first_difference(ours: &[u8], theirs: &[u8], width: usize) -> Option { + fn first_difference(ours: &[u16], theirs: &[u16], width: usize) -> Option { let i = ours.iter().zip(theirs).position(|(a, b)| a != b)?; let differ = ours.iter().zip(theirs).filter(|(a, b)| a != b).count(); Some(format!( @@ -4331,15 +4353,15 @@ mod tests { 0.0 }; picture.y[y * width + x] = - (20.0 + gradient + ripple + edge).clamp(0.0, 255.0) as u8; + (20.0 + gradient + ripple + edge).clamp(0.0, 255.0) as u16; } } for y in 0..height / 2 { for x in 0..width / 2 { let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; let i = y * width / 2 + x; - picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; - picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; + picture.u[i] = (100 + (sx * 60 / (width / 2))) as u16; + picture.v[i] = (200 - (y * 80 / (height / 2))) as u16; } } picture @@ -4387,9 +4409,9 @@ mod tests { Picture { width, height, - y: out.stdout[base..base + luma].to_vec(), - u: out.stdout[base + luma..base + luma + chroma].to_vec(), - v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), + y: out.stdout[base..base + luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[base + luma..base + luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[base + luma + chroma..base + frame_bytes].iter().map(|&v| u16::from(v)).collect(), } }) .collect() @@ -4728,9 +4750,9 @@ mod tests { Picture { width, height, - y: bytes[..luma].to_vec(), - u: bytes[luma..luma + chroma].to_vec(), - v: bytes[luma + chroma..].to_vec(), + y: bytes[..luma].iter().map(|&v| u16::from(v)).collect(), + u: bytes[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: bytes[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } }) } @@ -4855,9 +4877,9 @@ mod tests { Picture { width, height, - y: bytes[..luma].to_vec(), - u: bytes[luma..luma + chroma].to_vec(), - v: bytes[luma + chroma..].to_vec(), + y: bytes[..luma].iter().map(|&v| u16::from(v)).collect(), + u: bytes[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: bytes[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } }) .collect() diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 9fa13533..06b07c32 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -376,14 +376,14 @@ mod tests { for y in 0..height { for x in 0..width { let sx = (x + shift) % width; - picture.y[y * width + x] = ((sx * 3 + y * 5) % 256) as u8; + picture.y[y * width + x] = ((sx * 3 + y * 5) % 256) as u16; } } for y in 0..height / 2 { for x in 0..width / 2 { let i = y * (width / 2) + x; - picture.u[i] = ((100 + shift * 4) % 256) as u8; - picture.v[i] = ((200 + 256 - shift * 2 % 256) % 256) as u8; + picture.u[i] = ((100 + shift * 4) % 256) as u16; + picture.v[i] = ((200 + 256 - shift * 2 % 256) % 256) as u16; } } picture @@ -679,9 +679,9 @@ mod tests { Picture { width, height, - y: bytes[..luma].to_vec(), - u: bytes[luma..luma + chroma].to_vec(), - v: bytes[luma + chroma..].to_vec(), + y: bytes[..luma].iter().map(|&v| u16::from(v)).collect(), + u: bytes[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: bytes[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } }) .collect(), @@ -759,7 +759,7 @@ mod tests { } } - fn psnr(a: &[u8], b: &[u8]) -> f64 { + fn psnr(a: &[u16], b: &[u16]) -> f64 { let squared: f64 = a .iter() .zip(b) @@ -847,7 +847,10 @@ mod tests { let mean_psnr: f64 = decoded .iter() .zip(&pictures) - .map(|(d, p)| psnr(d, &p.y)) + .map(|(d, p)| { + let d16: Vec = d.iter().map(|&v| u16::from(v)).collect(); + psnr(&d16, &p.y) + }) .sum::() / decoded.len() as f64; assert!( diff --git a/crates/ec-av1/src/film_grain.rs b/crates/ec-av1/src/film_grain.rs index 1b9a523c..a7472358 100644 --- a/crates/ec-av1/src/film_grain.rs +++ b/crates/ec-av1/src/film_grain.rs @@ -353,7 +353,7 @@ fn add_noise_to_block( let delta = (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) >> scaling_shift; picture.u[cidx] = - ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8; + ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; } if apply_cr { let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32) @@ -363,7 +363,7 @@ fn add_noise_to_block( let delta = (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) >> scaling_shift; picture.v[cidx] = - ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8; + ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; } } } @@ -376,7 +376,7 @@ fn add_noise_to_block( + rounding_offset) >> scaling_shift; picture.y[pidx] = - ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u8; + ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u16; } } } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index b7adf532..652590fc 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -634,9 +634,13 @@ pub fn decode_stream(data: &[u8]) -> Result> { use std::io::Write; let idx = pictures_decoded; if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { - let _ = f.write_all(&picture.y); - let _ = f.write_all(&picture.u); - let _ = f.write_all(&picture.v); + // lane-hbd r4: debug dump narrows to u8 -- this diagnostic + // predates 10-bit support and is an 8-bit-oracle comparison + // only (aomdec's own dump is 8-bit here too). + let narrow = |v: &[u16]| -> Vec { v.iter().map(|&s| s as u8).collect() }; + let _ = f.write_all(&narrow(&picture.y)); + let _ = f.write_all(&narrow(&picture.u)); + let _ = f.write_all(&narrow(&picture.v)); } } pictures_decoded += 1; @@ -676,14 +680,14 @@ mod tests { let mut picture = Pic::grey(width, height); for row in 0..height { for col in 0..width { - picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; + picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u16; } } for row in 0..height / 2 { for col in 0..width / 2 { let i = row * width / 2 + col; - picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; - picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; + picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u16; + picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u16; } } picture @@ -695,15 +699,15 @@ mod tests { for x in 0..width { let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; let gradient = sx * 200.0 / width as f64; - picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; + picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u16; } } for y in 0..height / 2 { for x in 0..width / 2 { let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; let i = y * width / 2 + x; - picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; - picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; + picture.u[i] = (100 + (sx * 60 / (width / 2))) as u16; + picture.v[i] = (200 - (y * 80 / (height / 2))) as u16; } } picture @@ -867,9 +871,9 @@ mod tests { Pic { width, height, - y: out.stdout[base..base + luma].to_vec(), - u: out.stdout[base + luma..base + luma + chroma].to_vec(), - v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), + y: out.stdout[base..base + luma].iter().map(|&v| u16::from(v)).collect(), + u: out.stdout[base + luma..base + luma + chroma].iter().map(|&v| u16::from(v)).collect(), + v: out.stdout[base + luma + chroma..base + frame_bytes].iter().map(|&v| u16::from(v)).collect(), } }) .collect() @@ -2854,7 +2858,7 @@ mod tests { // actually produced -- no second ffmpeg invocation needed). let ffmpeg_raw: Vec = ffmpeg_frames .iter() - .flat_map(|p| p.y.iter().chain(&p.u).chain(&p.v).copied()) + .flat_map(|p| p.y.iter().chain(&p.u).chain(&p.v).map(|&v| v as u8)) .collect(); let ffmpeg_raw_hash = fnv1a64(&ffmpeg_raw); eprintln!( @@ -6662,9 +6666,9 @@ mod tests { let mut yuv = Vec::with_capacity((luma + 2 * chroma) * frames); for f in 0..frames { let card = panned_test_card(width, height, ((f as i32 + seed as i32) * 5) as i64); - yuv.extend_from_slice(&card.y); - yuv.extend_from_slice(&card.u); - yuv.extend_from_slice(&card.v); + yuv.extend(card.y.iter().map(|&v| v as u8)); + yuv.extend(card.u.iter().map(|&v| v as u8)); + yuv.extend(card.v.iter().map(|&v| v as u8)); } std::fs::write(&yuv_path, &yuv).unwrap(); let status = Command::new(aomenc_path()) diff --git a/crates/ec-av1/src/superres.rs b/crates/ec-av1/src/superres.rs index 25d0bd8a..babebc79 100644 --- a/crates/ec-av1/src/superres.rs +++ b/crates/ec-av1/src/superres.rs @@ -191,30 +191,45 @@ pub(crate) fn upscale_picture( let chroma_in_w = picture.width.div_ceil(2); let chroma_out_w = upscaled_width.div_ceil(2); let chroma_h = height.div_ceil(2); + // lane-hbd r4: superres is 8-bit by construction (`upscale_row` clamps + // to 255) -- narrow at this boundary; callers refuse `bit_depth != 8` + // before ever reaching here (see `stream.rs`). + let narrow = |v: &[u16]| -> Vec { v.iter().map(|&s| s as u8).collect() }; + let picture_y8 = narrow(&picture.y); + let picture_u8 = narrow(&picture.u); + let picture_v8 = narrow(&picture.v); + let margin_y8 = margin.map(|m| narrow(&m.y)); + let margin_u8 = margin.map(|m| narrow(&m.u)); + let margin_v8 = margin.map(|m| narrow(&m.v)); + let widen = |v: Vec| -> Vec { v.into_iter().map(u16::from).collect() }; crate::encode::Picture { width: upscaled_width, height, - y: upscale_plane( - &picture.y, + y: widen(upscale_plane( + &picture_y8, height, picture.width, upscaled_width, - margin.map(|m| (m.y.as_slice(), m.width)), - ), - u: upscale_plane( - &picture.u, + margin_y8.as_deref().map(|m| (m, margin.unwrap().width)), + )), + u: widen(upscale_plane( + &picture_u8, chroma_h, chroma_in_w, chroma_out_w, - margin.map(|m| (m.u.as_slice(), m.width.div_ceil(2))), - ), - v: upscale_plane( - &picture.v, + margin_u8 + .as_deref() + .map(|m| (m, margin.unwrap().width.div_ceil(2))), + )), + v: widen(upscale_plane( + &picture_v8, chroma_h, chroma_in_w, chroma_out_w, - margin.map(|m| (m.v.as_slice(), m.width.div_ceil(2))), - ), + margin_v8 + .as_deref() + .map(|m| (m, margin.unwrap().width.div_ceil(2))), + )), } } diff --git a/lanes/hbd-r3.charter.md b/lanes/hbd-r3.charter.md new file mode 100644 index 00000000..ee0b0669 --- /dev/null +++ b/lanes/hbd-r3.charter.md @@ -0,0 +1,49 @@ +# lane-hbd r3 — the payoff: decode a 10-bit stream + +At main (8440024). The widen is MERGED: the decoder's sample type is `u16` +everywhere, clamp bounds come from `bit_depth`, 8-bit output is bit-exact at +264/0, and the encoder keeps `u8` with a local box at its four call sites. + +Everything now stands behind one refusal: `"a stream whose bit depth is not 8 +(this decoder reconstructs into 8-bit planes)"` in `stream.rs`. The sentence is +already false about the planes. Lift it — properly. + +## Order +1. **A 10-bit reference path.** `ffmpeg_decode_sequence` (`stream.rs`) hardcodes + `-pix_fmt yuv420p`, one byte per sample. Add a second helper emitting + `yuv420p10le` and parsing u16 little-endian, and make the comparison + bit-depth aware. Keep the 8-bit helper exactly as it is — every existing gate + depends on it. +2. **A gate on a real 10-bit aomenc stream.** `aomenc` needs + `--bit-depth=10 --profile=0` and a 10-bit input (`ffmpeg ... -pix_fmt + yuv420p10le`); confirm from the sequence header that `bit_depth` really is 10 + before trusting any pixel match. Hard-assert it. +3. **Lift the refusal** and update `refusal_inventory.rs` in the SAME commit — + only once that gate is pixel-exact. +4. **Then the real target: his own files.** Both AV1 films on this box are + `yuv420p10le`: + `~/Downloads/reelb.mkv` and + `~/Videos/Films/reelc.mkv`. Extract a few seconds + (`ffmpeg -i -t 3 -c:v copy -f obu `, note `-f obu`) and run + `cargo run -p ec-av1 --example decode_probe -- `. Whatever refusal + comes back next IS the next lane's charter — record it verbatim, with the + film and the header state. Do not open his media in a GUI app; ffmpeg and + decode_probe only, and write extracted streams into this worktree's + `fixtures/`, never beside his files. + +Expect 10-bit to expose rounding that 8-bit hid: the inverse transform's +intermediate clamps already take `bit_depth` (`row_clamp = bit_depth + 8`, +`col_clamp = max(bit_depth + 6, 16)`), but every place that still assumes a +255 ceiling or an 8-bit rounding constant will show up as a small pixel delta, +not a crash. Bisect any mismatch against the oracle rather than by inspection. + +## Budget discipline +75 turns, no reset on resume. At about turn 55, stop starting new work: commit +what is green and write your report. + +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-hbd`; foreground +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; ffmpeg +generates bounded with `-t`; `EC_AV1_REQUIRE_AOMENC=1`; aomenc +`--threads=1 --row-mt=0 --sb-size=64`. Sibling worktrees have live agents — +never build in or edit them. Never push, never merge into main; I handle merges. +End with `lanes/hbd-r3.report.md`, VERDICT on line 1. diff --git a/lanes/hbd-r3.report.md b/lanes/hbd-r3.report.md new file mode 100644 index 00000000..ca24a93f --- /dev/null +++ b/lanes/hbd-r3.report.md @@ -0,0 +1,119 @@ +# lane-hbd r3 report + +VERDICT: NO CHANGE MERGEABLE -- scoping round only, tree left clean at 8281618, next lane's charter attached below. + +## What happened + +This round budgeted 75 turns for "the payoff": a 10-bit reference path, a +real 10-bit aomenc gate, lifting the `bit_depth != 8` refusal, then probing +the user's own films. Most of the budget went to establishing that the +refusal cannot be honestly lifted without widening `encode::Picture` itself +-- a materially bigger job than the charter's framing suggested -- and a +first attempt at that widen, which does not fit in what was left. No commit +was made; `git diff` is empty, worktree is at 8281618 exactly. + +## The real blocker (not visible from the charter alone) + +r2 widened every *intermediate* sample buffer (`PlaneBuf`, `mc.rs`, +`warp.rs`, the OBMC/interintra chains) to `u16`, but the FINAL narrowing -- +where a decoded frame becomes an `encode::Picture` -- was untouched, and +`Picture.y/u/v` are still `Vec`. Two places do `s as u8` on the way out +(`decode.rs:6823-6825` for key frames, `decode.rs:12594-12596` for inter +frames). Widening only the ffmpeg comparison helper (charter step 1) proves +nothing on its own: even with a correct 10-bit-precision decode internally, +the value that reaches the caller is still an 8-bit truncation, which is +exactly the "silent wrongness" the refusal's own doc names. So "lift the +refusal properly" (step 3) requires `Picture` to actually carry 10-bit +values, not just the reference path to be able to read them. + +`encode::Picture` is shared between the encoder (its own reconstruction +output, `u8`-only by design per r2's explicit decision) and the decoder +(`decode_stream`'s return type, and the DPB's own `ref_slots` storage used +for inter-frame motion compensation -- `reference: &Picture` is read for MC, +not just written at the end, so reference-frame precision matters for +correctness on frame 2+, not only for the final comparison). Widening +`Picture.y/u/v` to `Vec` is the natural single-source-of-truth fix and +turned out to have a SMALLER encoder blast radius than first feared -- +production encoder code only touches `Picture` at ~10 sites (`Plane` +construction, `pad_plane`/`crop_plane`, `padded_to`, `crop_encoded`), all +either genericizable over the element type or a trivial local `as u8` +narrow (the encoder never needs anything but `0..=255`). The real size was +in test-fixture churn: `cargo check --lib` alone showed 61 errors, dominated +by ~50 encoder test-module literal-assignment sites (`picture.y[i] = ... as +u8`) that all need `as u16`, plus the two decode.rs narrowing sites, plus +whatever `--tests` adds on top (not reached this round). `film_grain.rs` and +`superres.rs` are ADDITIONALLY hardcoded to 8-bit math (grain's scaling LUT +is `[i32; 256]`, its doc says "specialized to this crate's only picture +shape: 8-bit"; superres's `upscale_row` is `&[u8]` with a literal +`.clamp(0, 255) as u8`) -- neither is spec-generalizable to 10-bit inside +this round's remaining budget, so the honest plan was to lift the BLANKET +`bit_depth != 8` refusal but add two NARROWER ones ("film grain on a stream +whose bit depth isn't 8", "use_superres on a stream whose bit depth isn't +8") guarding those two subsystems specifically, since the core +transform/prediction/MC/deblock/CDEF/LR pipeline is otherwise already +bit-depth-generic via `decode::BIT_DEPTH` (which is itself dead code right +now -- `set_bit_depth` is defined but never called from `stream.rs`; wiring +that is also part of the real fix). + +A first attempt at the `Picture` widen was made (`Picture.y/u/v: Vec`, +`pad_plane`/`crop_plane` genericized over `T: Copy [+ Default]`, the two +decode.rs narrowing sites changed to move `PlaneBuf::data` directly instead +of `.map(|s| s as u8)`) and got `cargo check -p ec-av1 --lib` down from 61 +to a smaller but still nonzero error count before the turn budget forced a +stop; the attempt was reverted (`git checkout --`) rather than left +half-compiling, so the tree stays green at 8281618. The diff is saved for +reference at +`/home/user/Documents/Code/Rust/edith_codecs-hbd/../..` -- actually at +the session scratchpad: `hbd-r3-picture-widen-attempt.diff` (128 lines, +2 files: `decode.rs`, `encode.rs`). It is NOT applied to the worktree. + +## What was NOT reached + +Steps 1 (ffmpeg 10-bit helper), 2 (real 10-bit aomenc gate), 3 (lift + +inventory update), 4 (probe his films) -- none started. No fixture files +were extracted, nothing was written to `fixtures/`. + +## Next lane's charter (hbd-r4) + +1. Apply (or redo, it's small and now-scoped) the `Picture.y/u/v -> + Vec` widen: `encode.rs` struct + `pad_plane`/`crop_plane` + genericized (both already attempted, low-risk) + ~10 production + `Plane`-construction/padding call sites (local `as u8`/`as u16` box, + same pattern as r2's `intra_predict_u8`) + the ~50 encoder test-fixture + literal touch-ups (mechanical, `cargo check --lib` then `--tests` + enumerates every one) + `decode.rs`'s two narrowing sites (drop the `as + u8` map, move `PlaneBuf::data` directly). +2. Wire `decode::set_bit_depth(seq.color_config.bit_depth)` in `stream.rs` + before the per-frame decode calls -- currently dead code, defaults to 8 + always. +3. Add two narrow refusals (`film_grain.rs`'s `apply_grain` and + `header.use_superres`, both gated on `bit_depth != 8`) rather than + generalizing their 8-bit-hardcoded math this round -- name them + explicitly in `refusal_inventory.rs`. +4. Lift the blanket `bit_depth != 8` refusal in `stream.rs` once (1)-(3) + compile and the existing 264-test 8-bit suite is still green (it should + be structurally: `Vec` holding `0..=255` compares byte-for-byte + equal in value to the old `Vec`, only the comparison TYPE changes). +5. THEN do the charter's original steps 1/2/3/4: 10-bit ffmpeg reference + helper (u16 LE parse), real `--bit-depth=10 --profile=0` aomenc gate + hard-asserting `bit_depth == 10` from the sequence header before + trusting any pixel match, and only then probe the user's two real films + (`~/Downloads/reelb.mkv`, + `~/Videos/Films/reelc.mkv`) via `ffmpeg -t 3 -c:v copy -f + obu` into this worktree's `fixtures/` + `decode_probe`. Given both films + are HDR10 (film grain is common in HDR masters), expect the probe to hit + the new film-grain-at-10-bit refusal quickly -- record it verbatim with + header state as instructed, that becomes hbd-r5. + +## Budget + +Spent essentially the whole 75-turn budget on: reading the charter/r2 +report, tracing the full narrowing chain (`Picture` shared by encoder+DPB, +not just a comparison-helper concern), sizing the blast radius across +encode.rs/decode.rs/film_grain.rs/superres.rs, and one partial widen +attempt that was reverted rather than left broken. This was a scoping +failure on my part, not a budget-external blocker: the charter's own +"expect 10-bit to expose rounding" framing undersold that the OUTPUT type +itself, not just intermediate clamps, needed widening -- that should have +been the very first thing checked (grep `Picture.*Vec`) before reading +deeply into the rest of the pipeline. diff --git a/lanes/hbd-r4.charter.md b/lanes/hbd-r4.charter.md new file mode 100644 index 00000000..e4523d3a --- /dev/null +++ b/lanes/hbd-r4.charter.md @@ -0,0 +1,67 @@ +# lane-hbd r4 — widen Picture, then lift the refusal + +At 7bb2a18. Read `lanes/hbd-r3.report.md` first. r3 landed no code but did the +scoping that makes this round tractable, and it found the real blocker: +`encode::Picture.y/u/v` (`encode.rs:92`) are still `Vec`, and that type is +shared by the encoder AND by `decode_stream`'s return value and the DPB +reference slots — which are read back for motion compensation on frame 2+, not +just handed to the caller. So the refusal cannot lift honestly until `Picture` +widens. + +## Checkpoint policy — this applies to the whole round +r3 got `cargo check` from 61 errors down, ran out of budget, and **reverted**; +so did r10 of the predecessor lane. That is twice the same loss. Do NOT revert a +falling error count. Commit a `wip(hbd):` checkpoint every time the count drops +meaningfully, with the number in the message. A non-compiling commit is fine on +this branch under that prefix; it is never merged in that state. The count +reaching zero is when you run the suite and make a real commit. + +Its attempt diff is saved at +`/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/baaa03f8-c4ff-4469-8ebb-83100429b150/scratchpad/hbd-r3-picture-widen-attempt.diff` +(128 lines, `pad_plane`/`crop_plane` genericized over `T: Copy + Default`, the +two `as u8` narrowing sites at `decode.rs` ~6823 and ~12594 dropped). `git apply` +it first rather than retyping. + +r3's measurement of what remains: the PRODUCTION blast radius is small (~10 +sites in `encode.rs`, all genericizable or trivial local narrow boxes); the bulk +of the errors are ~50 mechanical `as u8` → `as u16` touch-ups in encoder TEST +FIXTURES. Do the production sites first and checkpoint, then sweep the fixtures. + +## Then, in order +1. **Wire `decode::set_bit_depth`.** It exists at `decode.rs:97` and is never + called — `stream.rs` never passes `seq.color_config.bit_depth` into it, so the + `BIT_DEPTH` thread-local is dead code defaulting to 8. Everything downstream + silently assumes 8-bit until this is wired. Wire it, and make sure it is set + per frame before decode, not once per process. +2. **Narrow refusals for the two paths that are still 8-bit by construction**: + `film_grain.rs` (grain LUT is `[i32; 256]`) and `superres.rs` + (`upscale_row` takes `&[u8]` and clamps to 255). Each gets its own named + `bit_depth != 8` refusal — these are honest, scoped refusals, not the blanket + one. Declare both in `refusal_inventory.rs`. +3. **Lift the blanket refusal** in `stream.rs`, in the same commit as the gate + that proves 10-bit decodes. +4. **The 10-bit gate**: `ffmpeg_decode_sequence` hardcodes `-pix_fmt yuv420p`; + add a second helper emitting `yuv420p10le` with u16 LE parsing, leaving the + 8-bit helper untouched. aomenc needs `--bit-depth=10` and a 10-bit input. + Confirm from the sequence header that `bit_depth` really is 10 before + trusting a pixel match, and hard-assert it. +5. **His own films.** Both are `yuv420p10le`: ReelB (3840x1608, HDR10, + Main, no film grain flagged) and ReelC (1920x792, bt709, Main, no film grain). + Extract with `ffmpeg -i -t 3 -c:v copy -f obu ` into this + worktree's `fixtures/`, probe with + `cargo run -p ec-av1 --example decode_probe`, and record the next refusal + verbatim — that is the next lane's charter. Never open his media in a GUI app. + +8-bit output must stay bit-exact throughout: 264 tests, every real-aomenc gate +among them. + +## 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; ffmpeg +generates bounded with `-t`; `EC_AV1_REQUIRE_AOMENC=1`; aomenc +`--threads=1 --row-mt=0 --sb-size=64`. Sibling worktrees have live agents — +never build in or edit them. Never push, never merge into main; I handle merges. +End with `lanes/hbd-r4.report.md`, VERDICT on line 1. diff --git a/tools/ec-bench/src/main.rs b/tools/ec-bench/src/main.rs index 16fc8e88..367bb57f 100644 --- a/tools/ec-bench/src/main.rs +++ b/tools/ec-bench/src/main.rs @@ -362,9 +362,11 @@ fn bench_av1_encode(rows: &mut Vec) { let pic = Av1Picture { width: w as usize, height: h as usize, - y: y.to_vec(), - u: u.to_vec(), - v: v.to_vec(), + // ec-av1's Picture carries u16 samples since the high-bit-depth + // widen; this bench feeds 8-bit y4m, so widen each plane here. + y: y.iter().map(|&s| u16::from(s)).collect(), + u: u.iter().map(|&s| u16::from(s)).collect(), + v: v.iter().map(|&s| u16::from(s)).collect(), }; let packet = enc.encode(&pic).expect("av1 encode"); stream.extend_from_slice(&packet.data);