forge
Explore
Log in

Don't you dare go Hollow

wip(hbd): Picture.y/u/v widened to u16, --lib compiles (0 errors)

Browse files at this commit

502402c82ba1ac2d2e7ffee38c3bd52b9282db69Ahmet Kaan GümüşParent: 7ae2956
crates/ec-av1/src/decode.rsmodified+15 -11
@@ -6820,17 +6820,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
68206820 Picture {
68216821 width,
68226822 height,
6823- y: y.data.into_iter().map(|s| s as u8).collect(),
6824- u: u.data.into_iter().map(|s| s as u8).collect(),
6825- v: v.data.into_iter().map(|s| s as u8).collect(),
6823+ y: y.data,
6824+ u: u.data,
6825+ v: v.data,
68266826 },
68276827 result_cdfs,
68286828 ));
68296829 }
6830- let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {
6830+ let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u16> {
68316831 let mut out = Vec::with_capacity(w * h);
68326832 for row in 0..h {
6833- out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8));
6833+ out.extend(plane.data[row * plane.width..][..w].iter().copied());
68346834 }
68356835 out
68366836 };
@@ -6856,7 +6856,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
68566856 static IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
68576857 let idx = IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
68586858 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {
6859- let _ = f.write_all(&yc);
6859+ // corner-cut: this diagnostic dump stays 8-bit-narrowed
6860+ // regardless of the stream's real bit depth (debug-only,
6861+ // never read by a gate); widen if a >8-bit margin dump
6862+ // is ever needed.
6863+ let _ = f.write_all(&yc.iter().map(|&s| s as u8).collect::<Vec<u8>>());
68606864 }
68616865 }
68626866 Some(Picture {
@@ -12591,18 +12595,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1259112595 Picture {
1259212596 width,
1259312597 height,
12594- y: y.data.into_iter().map(|s| s as u8).collect(),
12595- u: u.data.into_iter().map(|s| s as u8).collect(),
12596- v: v.data.into_iter().map(|s| s as u8).collect(),
12598+ y: y.data,
12599+ u: u.data,
12600+ v: v.data,
1259712601 },
1259812602 result_cdfs,
1259912603 motion_field,
1260012604 ));
1260112605 }
12602- let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {
12606+ let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u16> {
1260312607 let mut out = Vec::with_capacity(w * h);
1260412608 for row in 0..h {
12605- out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8));
12609+ out.extend(plane.data[row * plane.width..][..w].iter().copied());
1260612610 }
1260712611 out
1260812612 };
crates/ec-av1/src/encode.rsmodified+56 -34
@@ -81,7 +81,15 @@ pub(crate) fn split_blocks() -> bool {
8181 SPLIT_BLOCKS
8282 }
8383
84-/// One 8-bit planar 4:2:0 picture.
84+/// One planar 4:2:0 picture. `y`/`u`/`v` hold one sample per position as
85+/// `u16` regardless of bit depth (`crate::decode`'s own `PlaneBuf` has used
86+/// `u16` throughout since the round-hbd-r1/r2 widen; this is the last
87+/// narrowing boundary that stayed `u8` -- widened lane-hbd r3 so a 10-bit
88+/// stream's decode output actually carries 10-bit precision instead of
89+/// silently losing its low 2 bits). The encoder only ever produces values in
90+/// `0..=255` (it is still 8-bit-only by design, see `encode.rs`'s r2
91+/// decision); `Plane::source` narrows right back to `u8` at its two
92+/// construction sites, a lossless round trip for that range.
8593 #[derive(Clone, Debug, PartialEq, Eq)]
8694 pub struct Picture {
8795 /// The picture's width in luma samples.
@@ -89,11 +97,11 @@ pub struct Picture {
8997 /// Its height in luma samples.
9098 pub height: usize,
9199 /// The luma plane, `width * height` samples in raster order.
92- pub y: Vec<u8>,
100+ pub y: Vec<u16>,
93101 /// The U plane, at half the width and half the height.
94- pub u: Vec<u8>,
102+ pub u: Vec<u16>,
95103 /// The V plane, the same shape as U.
96- pub v: Vec<u8>,
104+ pub v: Vec<u16>,
97105 }
98106
99107 impl Picture {
@@ -238,14 +246,14 @@ fn intra_predict_u8(
238246 /// Pads one plane to `(padded_width, padded_height)` by repeating its last
239247 /// row and column, so the block coder always sees a whole number of blocks.
240248 /// [`crop_plane`] undoes this on the way back out.
241-fn pad_plane(
242- source: &[u8],
249+fn pad_plane<T: Copy + Default>(
250+ source: &[T],
243251 width: usize,
244252 height: usize,
245253 padded_width: usize,
246254 padded_height: usize,
247-) -> Vec<u8> {
248- let mut out = vec![0u8; padded_width * padded_height];
255+) -> Vec<T> {
256+ let mut out = vec![T::default(); padded_width * padded_height];
249257 for row in 0..padded_height {
250258 let src = source[row.min(height - 1) * width..][..width].as_ref();
251259 let dst = &mut out[row * padded_width..][..padded_width];
@@ -258,7 +266,7 @@ fn pad_plane(
258266 /// The top-left `width` x `height` region of a plane that is `padded_width`
259267 /// wide, which is the render-size crop [`Picture::padded`]'s replication
260268 /// exists to let a decoder undo.
261-fn crop_plane(source: &[u8], padded_width: usize, width: usize, height: usize) -> Vec<u8> {
269+fn crop_plane<T: Copy>(source: &[T], padded_width: usize, width: usize, height: usize) -> Vec<T> {
262270 let mut out = Vec::with_capacity(width * height);
263271 for row in 0..height {
264272 out.extend_from_slice(&source[row * padded_width..][..width]);
@@ -1750,9 +1758,16 @@ pub(crate) fn encode_key_frame_inner(
17501758 // Prediction reads no further than this, in each plane's own units --
17511759 // see `Plane::true_width`/`true_height`.
17521760 let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
1761+ // lane-hbd r4: `Picture.y/u/v` widened to `Vec<u16>` for the decoder's
1762+ // sake (DPB reference slots, 10-bit output); the encoder stays 8-bit by
1763+ // design (see `intra_predict_u8`'s doc comment), so narrow the source
1764+ // once here.
1765+ let picture_y8: Vec<u8> = picture.y.iter().map(|&v| v as u8).collect();
1766+ let picture_u8: Vec<u8> = picture.u.iter().map(|&v| v as u8).collect();
1767+ let picture_v8: Vec<u8> = picture.v.iter().map(|&v| v as u8).collect();
17531768 let mut luma = Plane {
1754- source: &picture.y,
1755- reconstruction: vec![128; picture.y.len()],
1769+ source: &picture_y8,
1770+ reconstruction: vec![128; picture_y8.len()],
17561771 width: picture.width,
17571772 height: picture.height,
17581773 true_width,
@@ -1760,16 +1775,16 @@ pub(crate) fn encode_key_frame_inner(
17601775 };
17611776 let mut chroma = [
17621777 Plane {
1763- source: &picture.u,
1764- reconstruction: vec![128; picture.u.len()],
1778+ source: &picture_u8,
1779+ reconstruction: vec![128; picture_u8.len()],
17651780 width: picture.width / 2,
17661781 height: picture.height / 2,
17671782 true_width: true_width / 2,
17681783 true_height: true_height / 2,
17691784 },
17701785 Plane {
1771- source: &picture.v,
1772- reconstruction: vec![128; picture.v.len()],
1786+ source: &picture_v8,
1787+ reconstruction: vec![128; picture_v8.len()],
17731788 width: picture.width / 2,
17741789 height: picture.height / 2,
17751790 true_width: true_width / 2,
@@ -2006,9 +2021,9 @@ pub(crate) fn encode_key_frame_inner(
20062021 reconstruction: Picture {
20072022 width: luma.width,
20082023 height: luma.height,
2009- y: luma.reconstruction,
2010- u: u.reconstruction,
2011- v: v.reconstruction,
2024+ y: luma.reconstruction.iter().map(|&v| u16::from(v)).collect(),
2025+ u: u.reconstruction.iter().map(|&v| u16::from(v)).collect(),
2026+ v: v.reconstruction.iter().map(|&v| u16::from(v)).collect(),
20122027 },
20132028 tile,
20142029 mi_cols: header.mi_cols,
@@ -2164,7 +2179,7 @@ fn mc_trial(
21642179 side: usize,
21652180 mv: (i32, i32),
21662181 luma: bool,
2167- reference: &[u8],
2182+ reference: &[u16],
21682183 stride: usize,
21692184 ref_width: usize,
21702185 ref_height: usize,
@@ -2175,13 +2190,12 @@ fn mc_trial(
21752190 ) -> Trial {
21762191 let x_q4 = mv_to_q4(x, mv.1, luma);
21772192 let y_q4 = mv_to_q4(y, mv.0, luma);
2178- // lane-hbd r2: `reference`/`prediction` stay `u8` here (encoder is
2179- // 8-bit only this round, see `intra_predict_u8`'s doc comment) --
2180- // round-trip through `u16` locally to call the now-widened `mc::predict`.
2181- let reference16: Vec<u16> = reference.iter().map(|&v| u16::from(v)).collect();
2193+ // lane-hbd r4: `reference` (a DPB `Picture`'s plane) is `u16` now that
2194+ // `Picture` is widened; `prediction`/the encoder stay `u8` (encoder is
2195+ // 8-bit only this round, see `intra_predict_u8`'s doc comment).
21822196 let mut prediction16 = vec![0u16; side * side];
21832197 mc::predict(
2184- &reference16,
2198+ reference,
21852199 stride,
21862200 ref_width,
21872201 ref_height,
@@ -2453,10 +2467,13 @@ fn search_inter_block(
24532467 }
24542468
24552469 let source_block = luma.source_block(x, y, BLOCK);
2470+ // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is
2471+ // integer-`u8`-scale); narrow the DPB reference plane locally.
2472+ let ref_luma_8: Vec<u8> = ref_luma.0.iter().map(|&v| v as u8).collect();
24562473 #[cfg(test)]
24572474 let t = std::time::Instant::now();
24582475 let found = motion::search(
2459- ref_luma.0,
2476+ &ref_luma_8,
24602477 ref_luma.1,
24612478 ref_luma.2,
24622479 ref_luma.3,
@@ -2616,9 +2633,14 @@ pub(crate) fn encode_inter_frame(
26162633 header.render_height = render.1 as u32;
26172634
26182635 let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
2636+ // lane-hbd r4: encoder stays 8-bit by design; narrow the source once
2637+ // here (see `encode_key_frame_inner`).
2638+ let picture_y8: Vec<u8> = picture.y.iter().map(|&v| v as u8).collect();
2639+ let picture_u8: Vec<u8> = picture.u.iter().map(|&v| v as u8).collect();
2640+ let picture_v8: Vec<u8> = picture.v.iter().map(|&v| v as u8).collect();
26192641 let mut luma = Plane {
2620- source: &picture.y,
2621- reconstruction: vec![128; picture.y.len()],
2642+ source: &picture_y8,
2643+ reconstruction: vec![128; picture_y8.len()],
26222644 width: picture.width,
26232645 height: picture.height,
26242646 true_width,
@@ -2626,16 +2648,16 @@ pub(crate) fn encode_inter_frame(
26262648 };
26272649 let mut chroma = [
26282650 Plane {
2629- source: &picture.u,
2630- reconstruction: vec![128; picture.u.len()],
2651+ source: &picture_u8,
2652+ reconstruction: vec![128; picture_u8.len()],
26312653 width: picture.width / 2,
26322654 height: picture.height / 2,
26332655 true_width: true_width / 2,
26342656 true_height: true_height / 2,
26352657 },
26362658 Plane {
2637- source: &picture.v,
2638- reconstruction: vec![128; picture.v.len()],
2659+ source: &picture_v8,
2660+ reconstruction: vec![128; picture_v8.len()],
26392661 width: picture.width / 2,
26402662 height: picture.height / 2,
26412663 true_width: true_width / 2,
@@ -2942,9 +2964,9 @@ pub(crate) fn encode_inter_frame(
29422964 reconstruction: Picture {
29432965 width: luma.width,
29442966 height: luma.height,
2945- y: luma.reconstruction,
2946- u: u.reconstruction,
2947- v: v.reconstruction,
2967+ y: luma.reconstruction.iter().map(|&v| u16::from(v)).collect(),
2968+ u: u.reconstruction.iter().map(|&v| u16::from(v)).collect(),
2969+ v: v.reconstruction.iter().map(|&v| u16::from(v)).collect(),
29482970 },
29492971 tile,
29502972 mi_cols: header.mi_cols,
crates/ec-av1/src/film_grain.rsmodified+3 -3
@@ -353,7 +353,7 @@ fn add_noise_to_block(
353353 let delta = (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset)
354354 >> scaling_shift;
355355 picture.u[cidx] =
356- ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8;
356+ ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
357357 }
358358 if apply_cr {
359359 let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32)
@@ -363,7 +363,7 @@ fn add_noise_to_block(
363363 let delta = (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset)
364364 >> scaling_shift;
365365 picture.v[cidx] =
366- ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8;
366+ ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
367367 }
368368 }
369369 }
@@ -376,7 +376,7 @@ fn add_noise_to_block(
376376 + rounding_offset)
377377 >> scaling_shift;
378378 picture.y[pidx] =
379- ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u8;
379+ ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u16;
380380 }
381381 }
382382 }
crates/ec-av1/src/stream.rsmodified+7 -3
@@ -634,9 +634,13 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
634634 use std::io::Write;
635635 let idx = pictures_decoded;
636636 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {
637- let _ = f.write_all(&picture.y);
638- let _ = f.write_all(&picture.u);
639- let _ = f.write_all(&picture.v);
637+ // lane-hbd r4: debug dump narrows to u8 -- this diagnostic
638+ // predates 10-bit support and is an 8-bit-oracle comparison
639+ // only (aomdec's own dump is 8-bit here too).
640+ let narrow = |v: &[u16]| -> Vec<u8> { v.iter().map(|&s| s as u8).collect() };
641+ let _ = f.write_all(&narrow(&picture.y));
642+ let _ = f.write_all(&narrow(&picture.u));
643+ let _ = f.write_all(&narrow(&picture.v));
640644 }
641645 }
642646 pictures_decoded += 1;
crates/ec-av1/src/superres.rsmodified+27 -12
@@ -191,30 +191,45 @@ pub(crate) fn upscale_picture(
191191 let chroma_in_w = picture.width.div_ceil(2);
192192 let chroma_out_w = upscaled_width.div_ceil(2);
193193 let chroma_h = height.div_ceil(2);
194+ // lane-hbd r4: superres is 8-bit by construction (`upscale_row` clamps
195+ // to 255) -- narrow at this boundary; callers refuse `bit_depth != 8`
196+ // before ever reaching here (see `stream.rs`).
197+ let narrow = |v: &[u16]| -> Vec<u8> { v.iter().map(|&s| s as u8).collect() };
198+ let picture_y8 = narrow(&picture.y);
199+ let picture_u8 = narrow(&picture.u);
200+ let picture_v8 = narrow(&picture.v);
201+ let margin_y8 = margin.map(|m| narrow(&m.y));
202+ let margin_u8 = margin.map(|m| narrow(&m.u));
203+ let margin_v8 = margin.map(|m| narrow(&m.v));
204+ let widen = |v: Vec<u8>| -> Vec<u16> { v.into_iter().map(u16::from).collect() };
194205 crate::encode::Picture {
195206 width: upscaled_width,
196207 height,
197- y: upscale_plane(
198- &picture.y,
208+ y: widen(upscale_plane(
209+ &picture_y8,
199210 height,
200211 picture.width,
201212 upscaled_width,
202- margin.map(|m| (m.y.as_slice(), m.width)),
203- ),
204- u: upscale_plane(
205- &picture.u,
213+ margin_y8.as_deref().map(|m| (m, margin.unwrap().width)),
214+ )),
215+ u: widen(upscale_plane(
216+ &picture_u8,
206217 chroma_h,
207218 chroma_in_w,
208219 chroma_out_w,
209- margin.map(|m| (m.u.as_slice(), m.width.div_ceil(2))),
210- ),
211- v: upscale_plane(
212- &picture.v,
220+ margin_u8
221+ .as_deref()
222+ .map(|m| (m, margin.unwrap().width.div_ceil(2))),
223+ )),
224+ v: widen(upscale_plane(
225+ &picture_v8,
213226 chroma_h,
214227 chroma_in_w,
215228 chroma_out_w,
216- margin.map(|m| (m.v.as_slice(), m.width.div_ceil(2))),
217- ),
229+ margin_v8
230+ .as_deref()
231+ .map(|m| (m, margin.unwrap().width.div_ceil(2))),
232+ )),
218233 }
219234 }
220235