forge
Explore
Log in

Don't you dare go Hollow

wip(hbd): r1's u16 widen in flight at the cap (does not compile; error count is the progress metric)

Browse files at this commit

31976a111588a980679372e31c09b375d3a3a297Ahmet Kaan GümüşParent: c3da14b
crates/ec-av1/src/decode.rsmodified+89 -62
@@ -76,6 +76,27 @@ pub(crate) fn filter_intra_hits() -> usize {
76 thread_local! {76 thread_local! {
77 static CDEF_BITS: std::cell::Cell<u8> = const { std::cell::Cell::new(0) };77 static CDEF_BITS: std::cell::Cell<u8> = const { std::cell::Cell::new(0) };
78 }78 }
79+// lane-realworld r8: the sequence header's `color_config.bit_depth`, set
80+// once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs`
81+// call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still
82+// blocks every non-8-bit stream from reaching a decode call, so this is `8`
83+// on every path that runs today -- threading it now (rather than the literal
84+// `8`) is stage 1 of widening past that refusal without re-deriving every
85+// clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of
86+// depth (this decoder's own internal choice, matches dav1d/libaom).
87+thread_local! {
88+ static BIT_DEPTH: std::cell::Cell<u8> = const { std::cell::Cell::new(8) };
89+}
90+/// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current
91+/// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`.
92+pub(crate) fn sample_max() -> i32 {
93+ (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 1
94+}
95+/// Sets [`BIT_DEPTH`] for the current thread, called once per frame from
96+/// [`crate::stream`]'s decode loop.
97+pub(crate) fn set_bit_depth(bit_depth: u8) {
98+ BIT_DEPTH.with(|c| c.set(bit_depth));
99+}
79 // Whether this superblock's single CDEF unit (this decoder has no 128x128100 // Whether this superblock's single CDEF unit (this decoder has no 128x128
80 // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal101 // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal
81 // read -- reset to `false` at the top of each superblock in the two tile102 // read -- reset to `false` at the top of each superblock in the two tile
@@ -496,7 +517,7 @@ thread_local! {
496 /// alternative this avoids). Always empty again once that call consumes517 /// alternative this avoids). Always empty again once that call consumes
497 /// it (`.take()`), so a non-palette block's own `reconstruct` call never518 /// it (`.take()`), so a non-palette block's own `reconstruct` call never
498 /// sees stale state.519 /// sees stale state.
499- static PALETTE_PRED: std::cell::RefCell<Option<Vec<u8>>> =520+ static PALETTE_PRED: std::cell::RefCell<Option<Vec<u16>>> =
500 const { std::cell::RefCell::new(None) };521 const { std::cell::RefCell::new(None) };
501 }522 }
502 523
@@ -3578,7 +3599,7 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 {
3578 /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone.3599 /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone.
3579 #[derive(Clone)]3600 #[derive(Clone)]
3580 struct PlaneBuf {3601 struct PlaneBuf {
3581- data: Vec<u8>,3602+ data: Vec<u16>,
3582 width: usize,3603 width: usize,
3583 height: usize,3604 height: usize,
3584 /// The frame's true, decodable extent in this plane's own units — past3605 /// The frame's true, decodable extent in this plane's own units — past
@@ -3631,7 +3652,7 @@ impl PlaneBuf {
3631 y: usize,3652 y: usize,
3632 side: usize,3653 side: usize,
3633 reach: Reach,3654 reach: Reach,
3634- ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {3655+ ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) {
3635 let own_across = x + side.min(self.true_width.saturating_sub(x));3656 let own_across = x + side.min(self.true_width.saturating_sub(x));
3636 let across = if reach.above_right {3657 let across = if reach.above_right {
3637 own_across + side.min(self.true_width.saturating_sub(own_across))3658 own_across + side.min(self.true_width.saturating_sub(own_across))
@@ -3670,7 +3691,7 @@ impl PlaneBuf {
3670 bw: usize,3691 bw: usize,
3671 bh: usize,3692 bh: usize,
3672 reach: Reach,3693 reach: Reach,
3673- ) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<u8>) {3694+ ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) {
3674 let own_across = x + bw.min(self.true_width.saturating_sub(x));3695 let own_across = x + bw.min(self.true_width.saturating_sub(x));
3675 let across = if reach.above_right {3696 let across = if reach.above_right {
3676 own_across + bw.min(self.true_width.saturating_sub(own_across))3697 own_across + bw.min(self.true_width.saturating_sub(own_across))
@@ -3718,7 +3739,7 @@ impl PlaneBuf {
3718 smooth_neighbor: bool,3739 smooth_neighbor: bool,
3719 ) {3740 ) {
3720 let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach);3741 let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach);
3721- let mut prediction = vec![0u8; bw * bh];3742+ let mut prediction = vec![0u16; bw * bh];
3722 if let Some(fi_mode) = filter_intra {3743 if let Some(fi_mode) = filter_intra {
3723 crate::intra::predict_filter_intra(3744 crate::intra::predict_filter_intra(
3724 fi_mode,3745 fi_mode,
@@ -3748,9 +3769,9 @@ impl PlaneBuf {
3748 let idx = row * bw + col;3769 let idx = row * bw + col;
3749 let mut base = i32::from(prediction[idx]);3770 let mut base = i32::from(prediction[idx]);
3750 if let Some((alpha_q3, ac_q3)) = cfl {3771 if let Some((alpha_q3, ac_q3)) = cfl {
3751- base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255);3772+ base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max());
3752 }3773 }
3753- let sample = (base + residual[idx]).clamp(0, 255) as u8;3774+ let sample = (base + residual[idx]).clamp(0, sample_max()) as u16;
3754 self.data[(y + row) * self.width + x + col] = sample;3775 self.data[(y + row) * self.width + x + col] = sample;
3755 }3776 }
3756 }3777 }
@@ -3790,7 +3811,7 @@ impl PlaneBuf {
3790 buf3811 buf
3791 } else {3812 } else {
3792 let (above, left, corner) = self.edges(x, y, side, reach);3813 let (above, left, corner) = self.edges(x, y, side, reach);
3793- let mut prediction = vec![0u8; side * side];3814+ let mut prediction = vec![0u16; side * side];
3794 if let Some(fi_mode) = filter_intra {3815 if let Some(fi_mode) = filter_intra {
3795 crate::intra::predict_filter_intra(3816 crate::intra::predict_filter_intra(
3796 fi_mode,3817 fi_mode,
@@ -3829,9 +3850,9 @@ impl PlaneBuf {
3829 let idx = row * side + col;3850 let idx = row * side + col;
3830 let mut base = i32::from(prediction[idx]);3851 let mut base = i32::from(prediction[idx]);
3831 if let Some((alpha_q3, ac_q3)) = cfl {3852 if let Some((alpha_q3, ac_q3)) = cfl {
3832- base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, 255);3853+ base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max());
3833 }3854 }
3834- let sample = (base + residual[idx]).clamp(0, 255) as u8;3855+ let sample = (base + residual[idx]).clamp(0, sample_max()) as u16;
3835 self.data[(y + row) * self.width + x + col] = sample;3856 self.data[(y + row) * self.width + x + col] = sample;
3836 }3857 }
3837 }3858 }
@@ -4062,7 +4083,11 @@ fn decode_block(
4062 "a palette block with a split luma transform (round 1)",4083 "a palette block with a split luma transform (round 1)",
4063 ));4084 ));
4064 }4085 }
4065- let buf: Vec<u8> = py.map.iter().map(|&idx| py.colors[idx as usize] as u8).collect();4086+ let buf: Vec<u16> = py
4087+ .map
4088+ .iter()
4089+ .map(|&idx| py.colors[idx as usize])
4090+ .collect();
4066 PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf));4091 PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf));
4067 }4092 }
4068 // A UV-palette block's own chroma prediction override (lane-palette2 r1),4093 // A UV-palette block's own chroma prediction override (lane-palette2 r1),
@@ -4070,10 +4095,10 @@ fn decode_block(
4070 // plane's own `reconstruct`/`read_plane` call below (never both planes at4095 // plane's own `reconstruct`/`read_plane` call below (never both planes at
4071 // once), since Y's own set-then-take already emptied the slot by the time4096 // once), since Y's own set-then-take already emptied the slot by the time
4072 // either of these run.4097 // either of these run.
4073- let palette_uv_bufs: Option<(Vec<u8>, Vec<u8>)> = palette_uv.as_ref().map(|puv| {4098+ let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| {
4074 (4099 (
4075- puv.map.iter().map(|&idx| puv.u_colors[idx as usize] as u8).collect(),4100+ puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(),
4076- puv.map.iter().map(|&idx| puv.v_colors[idx as usize] as u8).collect(),4101+ puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(),
4077 )4102 )
4078 });4103 });
4079 if skip {4104 if skip {
@@ -4851,7 +4876,7 @@ fn cdef_filter_block(
4851 if clipping_required {4876 if clipping_required {
4852 y = y.clamp(min, max);4877 y = y.clamp(min, max);
4853 }4878 }
4854- dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, 255) as u8;4879+ dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, sample_max()) as u16;
4855 }4880 }
4856 }4881 }
4857 }4882 }
@@ -5286,7 +5311,7 @@ fn filter14(
5286 /// the next along the edge's own filtering direction.5311 /// the next along the edge's own filtering direction.
5287 #[allow(clippy::too_many_arguments)]5312 #[allow(clippy::too_many_arguments)]
5288 fn filter_edge(5313 fn filter_edge(
5289- data: &mut [u8],5314+ data: &mut [u16],
5290 base: usize,5315 base: usize,
5291 outer_stride: isize,5316 outer_stride: isize,
5292 tap_stride: isize,5317 tap_stride: isize,
@@ -5306,10 +5331,10 @@ fn filter_edge(
5306 && (q1 - q0).abs() <= limit5331 && (q1 - q0).abs() <= limit
5307 && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit;5332 && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit;
5308 let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1);5333 let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1);
5309- data[idx(-2)] = op1.clamp(0, 255) as u8;5334+ data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
5310- data[idx(-1)] = op0.clamp(0, 255) as u8;5335+ data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
5311- data[idx(0)] = oq0.clamp(0, 255) as u8;5336+ data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
5312- data[idx(1)] = oq1.clamp(0, 255) as u8;5337+ data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
5313 }5338 }
5314 6 => {5339 6 => {
5315 let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2));5340 let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2));
@@ -5323,10 +5348,10 @@ fn filter_edge(
5323 && (p2 - p0).abs() <= 15348 && (p2 - p0).abs() <= 1
5324 && (q2 - q0).abs() <= 1;5349 && (q2 - q0).abs() <= 1;
5325 let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2);5350 let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2);
5326- data[idx(-2)] = op1.clamp(0, 255) as u8;5351+ data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
5327- data[idx(-1)] = op0.clamp(0, 255) as u8;5352+ data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
5328- data[idx(0)] = oq0.clamp(0, 255) as u8;5353+ data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
5329- data[idx(1)] = oq1.clamp(0, 255) as u8;5354+ data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
5330 }5355 }
5331 8 => {5356 8 => {
5332 let (p3, p2, p1, p0, q0, q1, q2, q3) =5357 let (p3, p2, p1, p0, q0, q1, q2, q3) =
@@ -5346,12 +5371,12 @@ fn filter_edge(
5346 && (q3 - q0).abs() <= 1;5371 && (q3 - q0).abs() <= 1;
5347 let [op2, op1, op0, oq0, oq1, oq2] =5372 let [op2, op1, op0, oq0, oq1, oq2] =
5348 filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3);5373 filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3);
5349- data[idx(-3)] = op2.clamp(0, 255) as u8;5374+ data[idx(-3)] = op2.clamp(0, sample_max()) as u16;
5350- data[idx(-2)] = op1.clamp(0, 255) as u8;5375+ data[idx(-2)] = op1.clamp(0, sample_max()) as u16;
5351- data[idx(-1)] = op0.clamp(0, 255) as u8;5376+ data[idx(-1)] = op0.clamp(0, sample_max()) as u16;
5352- data[idx(0)] = oq0.clamp(0, 255) as u8;5377+ data[idx(0)] = oq0.clamp(0, sample_max()) as u16;
5353- data[idx(1)] = oq1.clamp(0, 255) as u8;5378+ data[idx(1)] = oq1.clamp(0, sample_max()) as u16;
5354- data[idx(2)] = oq2.clamp(0, 255) as u8;5379+ data[idx(2)] = oq2.clamp(0, sample_max()) as u16;
5355 }5380 }
5356 _ => {5381 _ => {
5357 let (p6, p5, p4, p3, p2, p1, p0) =5382 let (p6, p5, p4, p3, p2, p1, p0) =
@@ -5382,7 +5407,7 @@ fn filter_edge(
5382 q6,5407 q6,
5383 );5408 );
5384 for (k, v) in (-6..=5isize).zip(out) {5409 for (k, v) in (-6..=5isize).zip(out) {
5385- data[idx(k)] = v.clamp(0, 255) as u8;5410+ data[idx(k)] = v.clamp(0, sample_max()) as u16;
5386 }5411 }
5387 }5412 }
5388 }5413 }
@@ -5778,7 +5803,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
5778 let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK);5803 let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK);
5779 5804
5780 let mut y = PlaneBuf {5805 let mut y = PlaneBuf {
5781- data: vec![0u8; width * height],5806+ data: vec![0u16; width * height],
5782 width,5807 width,
5783 height,5808 height,
5784 true_width,5809 true_width,
@@ -5789,7 +5814,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
5789 tile_y1: height,5814 tile_y1: height,
5790 };5815 };
5791 let mut u = PlaneBuf {5816 let mut u = PlaneBuf {
5792- data: vec![0u8; width * height / 4],5817+ data: vec![0u16; width * height / 4],
5793 width: width / 2,5818 width: width / 2,
5794 height: height / 2,5819 height: height / 2,
5795 true_width: true_width / 2,5820 true_width: true_width / 2,
@@ -5800,7 +5825,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
5800 tile_y1: height / 2,5825 tile_y1: height / 2,
5801 };5826 };
5802 let mut v = PlaneBuf {5827 let mut v = PlaneBuf {
5803- data: vec![0u8; width * height / 4],5828+ data: vec![0u16; width * height / 4],
5804 width: width / 2,5829 width: width / 2,
5805 height: height / 2,5830 height: height / 2,
5806 true_width: true_width / 2,5831 true_width: true_width / 2,
@@ -6320,9 +6345,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
6320 use std::io::Write;6345 use std::io::Write;
6321 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);6346 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
6322 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {6347 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {
6323- let _ = f.write_all(&y.data);6348+ let narrow = |p: &PlaneBuf| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() };
6324- let _ = f.write_all(&u.data);6349+ let _ = f.write_all(&narrow(&y));
6325- let _ = f.write_all(&v.data);6350+ let _ = f.write_all(&narrow(&u));
6351+ let _ = f.write_all(&narrow(&v));
6326 }6352 }
6327 } else {6353 } else {
6328 PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);6354 PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -6334,7 +6360,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
6334 let crop_wide = |plane: &PlaneBuf| -> Vec<u8> {6360 let crop_wide = |plane: &PlaneBuf| -> Vec<u8> {
6335 let mut out = Vec::with_capacity(plane.true_width * plane.true_height);6361 let mut out = Vec::with_capacity(plane.true_width * plane.true_height);
6336 for row in 0..plane.true_height {6362 for row in 0..plane.true_height {
6337- out.extend_from_slice(&plane.data[row * plane.width..][..plane.true_width]);6363+ out.extend(plane.data[row * plane.width..][..plane.true_width].iter().map(|&s| s as u8));
6338 }6364 }
6339 out6365 out
6340 };6366 };
@@ -6364,9 +6390,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
6364 Picture {6390 Picture {
6365 width,6391 width,
6366 height,6392 height,
6367- y: y.data,6393+ y: y.data.into_iter().map(|s| s as u8).collect(),
6368- u: u.data,6394+ u: u.data.into_iter().map(|s| s as u8).collect(),
6369- v: v.data,6395+ v: v.data.into_iter().map(|s| s as u8).collect(),
6370 },6396 },
6371 result_cdfs,6397 result_cdfs,
6372 ));6398 ));
@@ -6374,7 +6400,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
6374 let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {6400 let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {
6375 let mut out = Vec::with_capacity(w * h);6401 let mut out = Vec::with_capacity(w * h);
6376 for row in 0..h {6402 for row in 0..h {
6377- out.extend_from_slice(&plane.data[row * plane.width..][..w]);6403+ out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8));
6378 }6404 }
6379 out6405 out
6380 };6406 };
@@ -6633,7 +6659,7 @@ impl PlaneBuf {
6633 x: usize,6659 x: usize,
6634 y: usize,6660 y: usize,
6635 side: usize,6661 side: usize,
6636- prediction: &[u8],6662+ prediction: &[u16],
6637 residual: &[i32],6663 residual: &[i32],
6638 ) {6664 ) {
6639 self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual);6665 self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual);
@@ -6654,13 +6680,13 @@ impl PlaneBuf {
6654 side: usize,6680 side: usize,
6655 w: usize,6681 w: usize,
6656 h: usize,6682 h: usize,
6657- prediction: &[u8],6683+ prediction: &[u16],
6658 residual: &[i32],6684 residual: &[i32],
6659 ) {6685 ) {
6660 for row in 0..h {6686 for row in 0..h {
6661 for col in 0..w {6687 for col in 0..w {
6662 let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col])6688 let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col])
6663- .clamp(0, 255) as u8;6689+ .clamp(0, crate::decode::sample_max()) as u16;
6664 self.data[(y + row) * self.width + x + col] = sample;6690 self.data[(y + row) * self.width + x + col] = sample;
6665 }6691 }
6666 }6692 }
@@ -10670,7 +10696,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10670 let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK);10696 let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK);
10671 10697
10672 let ref_y = PlaneBuf {10698 let ref_y = PlaneBuf {
10673- data: reference.y.clone(),10699+ data: reference.y.iter().map(|&s| s as u16).collect(),
10674 width: reference.width,10700 width: reference.width,
10675 height: reference.height,10701 height: reference.height,
10676 true_width: reference.width,10702 true_width: reference.width,
@@ -10681,7 +10707,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10681 tile_y1: reference.height,10707 tile_y1: reference.height,
10682 };10708 };
10683 let ref_u = PlaneBuf {10709 let ref_u = PlaneBuf {
10684- data: reference.u.clone(),10710+ data: reference.u.iter().map(|&s| s as u16).collect(),
10685 width: reference.width / 2,10711 width: reference.width / 2,
10686 height: reference.height / 2,10712 height: reference.height / 2,
10687 true_width: reference.width / 2,10713 true_width: reference.width / 2,
@@ -10692,7 +10718,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10692 tile_y1: reference.height / 2,10718 tile_y1: reference.height / 2,
10693 };10719 };
10694 let ref_v = PlaneBuf {10720 let ref_v = PlaneBuf {
10695- data: reference.v.clone(),10721+ data: reference.v.iter().map(|&s| s as u16).collect(),
10696 width: reference.width / 2,10722 width: reference.width / 2,
10697 height: reference.height / 2,10723 height: reference.height / 2,
10698 true_width: reference.width / 2,10724 true_width: reference.width / 2,
@@ -10715,7 +10741,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10715 .map(|g| {10741 .map(|g| {
10716 (10742 (
10717 PlaneBuf {10743 PlaneBuf {
10718- data: g.y.clone(),10744+ data: g.y.iter().map(|&s| s as u16).collect(),
10719 width: g.width,10745 width: g.width,
10720 height: g.height,10746 height: g.height,
10721 true_width: g.width,10747 true_width: g.width,
@@ -10726,7 +10752,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10726 tile_y1: g.height,10752 tile_y1: g.height,
10727 },10753 },
10728 PlaneBuf {10754 PlaneBuf {
10729- data: g.u.clone(),10755+ data: g.u.iter().map(|&s| s as u16).collect(),
10730 width: g.width / 2,10756 width: g.width / 2,
10731 height: g.height / 2,10757 height: g.height / 2,
10732 true_width: g.width / 2,10758 true_width: g.width / 2,
@@ -10737,7 +10763,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10737 tile_y1: g.height / 2,10763 tile_y1: g.height / 2,
10738 },10764 },
10739 PlaneBuf {10765 PlaneBuf {
10740- data: g.v.clone(),10766+ data: g.v.iter().map(|&s| s as u16).collect(),
10741 width: g.width / 2,10767 width: g.width / 2,
10742 height: g.height / 2,10768 height: g.height / 2,
10743 true_width: g.width / 2,10769 true_width: g.width / 2,
@@ -10754,7 +10780,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10754 std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c)));10780 std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c)));
10755 10781
10756 let mut y = PlaneBuf {10782 let mut y = PlaneBuf {
10757- data: vec![0u8; width * height],10783+ data: vec![0u16; width * height],
10758 width,10784 width,
10759 height,10785 height,
10760 true_width,10786 true_width,
@@ -10765,7 +10791,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10765 tile_y1: height,10791 tile_y1: height,
10766 };10792 };
10767 let mut u = PlaneBuf {10793 let mut u = PlaneBuf {
10768- data: vec![0u8; width * height / 4],10794+ data: vec![0u16; width * height / 4],
10769 width: width / 2,10795 width: width / 2,
10770 height: height / 2,10796 height: height / 2,
10771 true_width: true_width / 2,10797 true_width: true_width / 2,
@@ -10776,7 +10802,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
10776 tile_y1: height / 2,10802 tile_y1: height / 2,
10777 };10803 };
10778 let mut v = PlaneBuf {10804 let mut v = PlaneBuf {
10779- data: vec![0u8; width * height / 4],10805+ data: vec![0u16; width * height / 4],
10780 width: width / 2,10806 width: width / 2,
10781 height: height / 2,10807 height: height / 2,
10782 true_width: true_width / 2,10808 true_width: true_width / 2,
@@ -12099,9 +12125,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
12099 use std::io::Write;12125 use std::io::Write;
12100 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);12126 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
12101 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {12127 if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) {
12102- let _ = f.write_all(&y.data);12128+ let narrow = |p: &PlaneBuf| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() };
12103- let _ = f.write_all(&u.data);12129+ let _ = f.write_all(&narrow(&y));
12104- let _ = f.write_all(&v.data);12130+ let _ = f.write_all(&narrow(&u));
12131+ let _ = f.write_all(&narrow(&v));
12105 }12132 }
12106 } else {12133 } else {
12107 PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);12134 PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
@@ -12133,9 +12160,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
12133 Picture {12160 Picture {
12134 width,12161 width,
12135 height,12162 height,
12136- y: y.data,12163+ y: y.data.into_iter().map(|s| s as u8).collect(),
12137- u: u.data,12164+ u: u.data.into_iter().map(|s| s as u8).collect(),
12138- v: v.data,12165+ v: v.data.into_iter().map(|s| s as u8).collect(),
12139 },12166 },
12140 result_cdfs,12167 result_cdfs,
12141 motion_field,12168 motion_field,
@@ -12144,7 +12171,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
12144 let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {12171 let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {
12145 let mut out = Vec::with_capacity(w * h);12172 let mut out = Vec::with_capacity(w * h);
12146 for row in 0..h {12173 for row in 0..h {
12147- out.extend_from_slice(&plane.data[row * plane.width..][..w]);12174+ out.extend(plane.data[row * plane.width..][..w].iter().map(|&s| s as u8));
12148 }12175 }
12149 out12176 out
12150 };12177 };
@@ -12843,7 +12870,7 @@ mod tests {
12843 let ref_v = ref_plane(2);12870 let ref_v = ref_plane(2);
12844 12871
12845 let mut y = PlaneBuf {12872 let mut y = PlaneBuf {
12846- data: vec![0u8; width * height],12873+ data: vec![0u16; width * height],
12847 width,12874 width,
12848 height,12875 height,
12849 true_width: width,12876 true_width: width,
crates/ec-av1/src/intra.rsmodified+27 -27
@@ -145,14 +145,14 @@ impl Edges {
145 /// and `z3_c`'s `max_base_y` use (libaom `reconintra.c`) -- above and145 /// and `z3_c`'s `max_base_y` use (libaom `reconintra.c`) -- above and
146 /// left both extend that far regardless of which of `bw`/`bh` is larger.146 /// left both extend that far regardless of which of `bw`/`bh` is larger.
147 fn build(147 fn build(
148- above: Option<&[u8]>,148+ above: Option<&[u16]>,
149- left: Option<&[u8]>,149+ left: Option<&[u16]>,
150- corner: Option<u8>,150+ corner: Option<u16>,
151 bw: usize,151 bw: usize,
152 bh: usize,152 bh: usize,
153 ) -> Self {153 ) -> Self {
154 let want = bw + bh;154 let want = bw + bh;
155- let extend = |samples: &[u8]| {155+ let extend = |samples: &[u16]| {
156 let mut row: Vec<i32> = samples.iter().map(|&s| i32::from(s)).collect();156 let mut row: Vec<i32> = samples.iter().map(|&s| i32::from(s)).collect();
157 let last = *row.last().expect("an edge that exists has samples");157 let last = *row.last().expect("an edge that exists has samples");
158 row.resize(want, last);158 row.resize(want, last);
@@ -230,14 +230,14 @@ impl Edges {
230 pub fn predict(230 pub fn predict(
231 mode: u8,231 mode: u8,
232 angle_delta: i32,232 angle_delta: i32,
233- above: Option<&[u8]>,233+ above: Option<&[u16]>,
234- left: Option<&[u8]>,234+ left: Option<&[u16]>,
235- corner: Option<u8>,235+ corner: Option<u16>,
236 bw: usize,236 bw: usize,
237 bh: usize,237 bh: usize,
238 enable_edge_filter: bool,238 enable_edge_filter: bool,
239 smooth_neighbor: bool,239 smooth_neighbor: bool,
240- dst: &mut [u8],240+ dst: &mut [u16],
241 ) {241 ) {
242 assert_eq!(dst.len(), bw * bh, "the destination is the block");242 assert_eq!(dst.len(), bw * bh, "the destination is the block");
243 let edges = Edges::build(above, left, corner, bw, bh);243 let edges = Edges::build(above, left, corner, bw, bh);
@@ -304,7 +304,7 @@ pub fn predict(
304 PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)),304 PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)),
305 other => panic!("intra mode {other} is not one this module predicts"),305 other => panic!("intra mode {other} is not one this module predicts"),
306 };306 };
307- dst[row * bw + col] = value.clamp(0, 255) as u8;307+ dst[row * bw + col] = value.clamp(0, crate::decode::sample_max()) as u16;
308 }308 }
309 }309 }
310 }310 }
@@ -431,7 +431,7 @@ fn upsample_intra_edge(buf: &[i32]) -> Vec<i32> {
431 out[0] = inp[0];431 out[0] = inp[0];
432 for i in 0..sz {432 for i in 0..sz {
433 let s = -inp[i] + 9 * inp[i + 1] + 9 * inp[i + 2] - inp[i + 3];433 let s = -inp[i] + 9 * inp[i + 1] + 9 * inp[i + 2] - inp[i + 3];
434- out[2 * i + 1] = ((s + 8) >> 4).clamp(0, 255);434+ out[2 * i + 1] = ((s + 8) >> 4).clamp(0, crate::decode::sample_max());
435 out[2 * i + 2] = inp[i + 2];435 out[2 * i + 2] = inp[i + 2];
436 }436 }
437 out437 out
@@ -452,7 +452,7 @@ fn directional(
452 smooth_neighbor: bool,452 smooth_neighbor: bool,
453 n_top: usize,453 n_top: usize,
454 n_left: usize,454 n_left: usize,
455- dst: &mut [u8],455+ dst: &mut [u16],
456 ) {456 ) {
457 let (w, h) = (bw as i32, bh as i32);457 let (w, h) = (bw as i32, bh as i32);
458 let reach = w + h; // `av1_dr_prediction_z{1,3}_c`'s `max_base_{x,y}` reach.458 let reach = w + h; // `av1_dr_prediction_z{1,3}_c`'s `max_base_{x,y}` reach.
@@ -565,7 +565,7 @@ fn directional(
565 blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)565 blend(&left_at, y2 >> frac_bits_y, ((y2 << up_l) & 0x3F) >> 1)
566 }566 }
567 };567 };
568- dst[(row * w + col) as usize] = value.clamp(0, 255) as u8;568+ dst[(row * w + col) as usize] = value.clamp(0, crate::decode::sample_max()) as u16;
569 }569 }
570 }570 }
571 }571 }
@@ -579,8 +579,8 @@ fn directional(
579 /// before `dc_predictor` averages, so a slice truncated short by the true579 /// before `dc_predictor` averages, so a slice truncated short by the true
580 /// frame edge must be extended by repetition here too, not averaged over its580 /// frame edge must be extended by repetition here too, not averaged over its
581 /// own shorter length.581 /// own shorter length.
582-fn dc(above: Option<&[u8]>, left: Option<&[u8]>, bw: usize, bh: usize) -> i32 {582+fn dc(above: Option<&[u16]>, left: Option<&[u16]>, bw: usize, bh: usize) -> i32 {
583- let extend = |samples: &[u8], want: usize| -> Vec<u8> {583+ let extend = |samples: &[u16], want: usize| -> Vec<u16> {
584 if samples.len() >= want {584 if samples.len() >= want {
585 samples[..want].to_vec()585 samples[..want].to_vec()
586 } else {586 } else {
@@ -592,7 +592,7 @@ fn dc(above: Option<&[u8]>, left: Option<&[u8]>, bw: usize, bh: usize) -> i32 {
592 };592 };
593 let above = above.map(|a| extend(a, bw));593 let above = above.map(|a| extend(a, bw));
594 let left = left.map(|l| extend(l, bh));594 let left = left.map(|l| extend(l, bh));
595- let average = |samples: &[u8]| {595+ let average = |samples: &[u16]| {
596 let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();596 let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();
597 ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32597 ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32
598 };598 };
@@ -723,11 +723,11 @@ const FILTER_INTRA_SCALE_BITS: u32 = 4;
723 /// 32x32, and only on square blocks: 4, 8, 16 or 32).723 /// 32x32, and only on square blocks: 4, 8, 16 or 32).
724 pub fn predict_filter_intra(724 pub fn predict_filter_intra(
725 mode: usize,725 mode: usize,
726- above: Option<&[u8]>,726+ above: Option<&[u16]>,
727- left: Option<&[u8]>,727+ left: Option<&[u16]>,
728- corner: Option<u8>,728+ corner: Option<u16>,
729 side: usize,729 side: usize,
730- dst: &mut [u8],730+ dst: &mut [u16],
731 ) {731 ) {
732 assert_eq!(dst.len(), side * side, "the destination is the block");732 assert_eq!(dst.len(), side * side, "the destination is the block");
733 assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32");733 assert_eq!(side % 4, 0, "filter intra only offers 4/8/16/32");
@@ -759,7 +759,7 @@ pub fn predict_filter_intra(
759 for k in 0..8 {759 for k in 0..8 {
760 let (r_off, c_off) = (k >> 2, k & 3);760 let (r_off, c_off) = (k >> 2, k & 3);
761 let pr: i32 = taps[k].iter().zip(&p).map(|(t, s)| t * s).sum();761 let pr: i32 = taps[k].iter().zip(&p).map(|(t, s)| t * s).sum();
762- let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, 255);762+ let value = round2(pr, FILTER_INTRA_SCALE_BITS).clamp(0, crate::decode::sample_max());
763 let idx = (r + r_off) * (side + 1) + c + c_off;763 let idx = (r + r_off) * (side + 1) + c + c_off;
764 buffer[idx] = value;764 buffer[idx] = value;
765 }765 }
@@ -769,7 +769,7 @@ pub fn predict_filter_intra(
769 }769 }
770 for row in 0..side {770 for row in 0..side {
771 for col in 0..side {771 for col in 0..side {
772- dst[row * side + col] = at(&buffer, row + 1, col + 1) as u8;772+ dst[row * side + col] = at(&buffer, row + 1, col + 1) as u16;
773 }773 }
774 }774 }
775 }775 }
@@ -799,15 +799,15 @@ mod tests {
799 799
800 /// Same deterministic synthetic ramp `lanes/intrarect_dump.c`'s `fill`800 /// Same deterministic synthetic ramp `lanes/intrarect_dump.c`'s `fill`
801 /// generates.801 /// generates.
802- fn fill(n: usize, seed: i32) -> Vec<u8> {802+ fn fill(n: usize, seed: i32) -> Vec<u16> {
803 (0..n)803 (0..n)
804- .map(|i| ((i as i32 * 7 + seed * 13 + (i as i32 % 5) * 3).rem_euclid(256)) as u8)804+ .map(|i| ((i as i32 * 7 + seed * 13 + (i as i32 % 5) * 3).rem_euclid(256)) as u16)
805 .collect()805 .collect()
806 }806 }
807 807
808 /// `lanes/intrarect_dump.c`'s `checksum`: a position-weighted sum so a808 /// `lanes/intrarect_dump.c`'s `checksum`: a position-weighted sum so a
809 /// wrong value anywhere in the block, not just at index 0, moves it.809 /// wrong value anywhere in the block, not just at index 0, moves it.
810- fn checksum(dst: &[u8]) -> u64 {810+ fn checksum(dst: &[u16]) -> u64 {
811 dst.iter()811 dst.iter()
812 .enumerate()812 .enumerate()
813 .map(|(i, &v)| u64::from(v) * (i as u64 + 1))813 .map(|(i, &v)| u64::from(v) * (i as u64 + 1))
@@ -897,10 +897,10 @@ mod tests {
897 let reach = bw + bh;897 let reach = bw + bh;
898 let above = fill(reach, 1);898 let above = fill(reach, 1);
899 let left = fill(reach, 2);899 let left = fill(reach, 2);
900- let corner = ((bw * 3 + bh * 5) % 256) as u8;900+ let corner = ((bw * 3 + bh * 5) % 256) as u16;
901 901
902 for (name, mode) in [("DC", DC_PRED), ("SMOOTH", SMOOTH_PRED), ("PAETH", PAETH_PRED)] {902 for (name, mode) in [("DC", DC_PRED), ("SMOOTH", SMOOTH_PRED), ("PAETH", PAETH_PRED)] {
903- let mut dst = vec![0u8; bw * bh];903+ let mut dst = vec![0u16; bw * bh];
904 predict(904 predict(
905 mode,905 mode,
906 0,906 0,
@@ -928,7 +928,7 @@ mod tests {
928 for &(angle, mode, delta) in &directional_cases {928 for &(angle, mode, delta) in &directional_cases {
929 for ef in [false, true] {929 for ef in [false, true] {
930 for sn in [false, true] {930 for sn in [false, true] {
931- let mut dst = vec![0u8; bw * bh];931+ let mut dst = vec![0u16; bw * bh];
932 predict(932 predict(
933 mode,933 mode,
934 delta,934 delta,
crates/ec-av1/src/mc.rsmodified+14 -14
@@ -228,7 +228,7 @@ impl InterpFilterKind {
228 /// whatever this encoder's own uncoded padding columns/rows hold.228 /// whatever this encoder's own uncoded padding columns/rows hold.
229 #[allow(clippy::too_many_arguments)]229 #[allow(clippy::too_many_arguments)]
230 fn sample(230 fn sample(
231- reference: &[u8],231+ reference: &[u16],
232 stride: usize,232 stride: usize,
233 true_width: usize,233 true_width: usize,
234 true_height: usize,234 true_height: usize,
@@ -258,7 +258,7 @@ fn sample(
258 /// empty.258 /// empty.
259 #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape259 #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape
260 pub fn predict(260 pub fn predict(
261- reference: &[u8],261+ reference: &[u16],
262 stride: usize,262 stride: usize,
263 true_width: usize,263 true_width: usize,
264 true_height: usize,264 true_height: usize,
@@ -266,7 +266,7 @@ pub fn predict(
266 y_q4: i32,266 y_q4: i32,
267 block_w: usize,267 block_w: usize,
268 block_h: usize,268 block_h: usize,
269- dst: &mut [u8],269+ dst: &mut [u16],
270 ) {270 ) {
271 predict_with_filter(271 predict_with_filter(
272 reference,272 reference,
@@ -287,7 +287,7 @@ pub fn predict(
287 /// kernel both directions.287 /// kernel both directions.
288 #[allow(clippy::too_many_arguments)]288 #[allow(clippy::too_many_arguments)]
289 pub fn predict_with_filter(289 pub fn predict_with_filter(
290- reference: &[u8],290+ reference: &[u16],
291 stride: usize,291 stride: usize,
292 true_width: usize,292 true_width: usize,
293 true_height: usize,293 true_height: usize,
@@ -296,7 +296,7 @@ pub fn predict_with_filter(
296 block_w: usize,296 block_w: usize,
297 block_h: usize,297 block_h: usize,
298 filter_kind: InterpFilterKind,298 filter_kind: InterpFilterKind,
299- dst: &mut [u8],299+ dst: &mut [u16],
300 ) {300 ) {
301 predict_with_filters(301 predict_with_filters(
302 reference,302 reference,
@@ -319,7 +319,7 @@ pub fn predict_with_filter(
319 /// so the two passes can genuinely differ.319 /// so the two passes can genuinely differ.
320 #[allow(clippy::too_many_arguments)]320 #[allow(clippy::too_many_arguments)]
321 pub fn predict_with_filters(321 pub fn predict_with_filters(
322- reference: &[u8],322+ reference: &[u16],
323 stride: usize,323 stride: usize,
324 true_width: usize,324 true_width: usize,
325 true_height: usize,325 true_height: usize,
@@ -329,7 +329,7 @@ pub fn predict_with_filters(
329 block_h: usize,329 block_h: usize,
330 h_kind: InterpFilterKind,330 h_kind: InterpFilterKind,
331 v_kind: InterpFilterKind,331 v_kind: InterpFilterKind,
332- dst: &mut [u8],332+ dst: &mut [u16],
333 ) {333 ) {
334 assert_eq!(dst.len(), block_w * block_h, "the destination is the block");334 assert_eq!(dst.len(), block_w * block_h, "the destination is the block");
335 assert!(!reference.is_empty(), "a reference plane has samples");335 assert!(!reference.is_empty(), "a reference plane has samples");
@@ -360,7 +360,7 @@ pub fn predict_with_filters(
360 for col in 0..block_w {360 for col in 0..block_w {
361 let x = x0 + col as i32;361 let x = x0 + col as i32;
362 dst[row * block_w + col] =362 dst[row * block_w + col] =
363- sample(reference, stride, true_width, true_height, x, y) as u8;363+ sample(reference, stride, true_width, true_height, x, y) as u16;
364 }364 }
365 }365 }
366 #[cfg(test)]366 #[cfg(test)]
@@ -403,7 +403,7 @@ pub fn predict_with_filters(
403 for (t, &tap) in v_filter.iter().enumerate() {403 for (t, &tap) in v_filter.iter().enumerate() {
404 sum += tap * intermediate[(row + t) * block_w + col];404 sum += tap * intermediate[(row + t) * block_w + col];
405 }405 }
406- dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8;406+ dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16;
407 }407 }
408 }408 }
409 409
@@ -445,7 +445,7 @@ const DIST_PRECISION_BITS: u32 = 4;
445 /// empty.445 /// empty.
446 #[allow(clippy::too_many_arguments)]446 #[allow(clippy::too_many_arguments)]
447 pub fn predict_compound_intermediate(447 pub fn predict_compound_intermediate(
448- reference: &[u8],448+ reference: &[u16],
449 stride: usize,449 stride: usize,
450 true_width: usize,450 true_width: usize,
451 true_height: usize,451 true_height: usize,
@@ -515,13 +515,13 @@ pub fn combine_compound(
515 pred1: &[i32],515 pred1: &[i32],
516 fwd_weight: i32,516 fwd_weight: i32,
517 bck_weight: i32,517 bck_weight: i32,
518- dst: &mut [u8],518+ dst: &mut [u16],
519 ) {519 ) {
520 assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block");520 assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block");
521 assert_eq!(dst.len(), pred0.len(), "the destination is the block");521 assert_eq!(dst.len(), pred0.len(), "the destination is the block");
522 for i in 0..dst.len() {522 for i in 0..dst.len() {
523 let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight;523 let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight;
524- dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, 255) as u8;524+ dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, crate::decode::sample_max()) as u16;
525 }525 }
526 }526 }
527 527
@@ -564,7 +564,7 @@ pub fn blend_masked_compound(
564 w: usize,564 w: usize,
565 h: usize,565 h: usize,
566 subsampled: bool,566 subsampled: bool,
567- dst: &mut [u8],567+ dst: &mut [u16],
568 ) {568 ) {
569 assert_eq!(pred0.len(), w * h, "pred0 is the destination-sized block");569 assert_eq!(pred0.len(), w * h, "pred0 is the destination-sized block");
570 assert_eq!(pred1.len(), w * h, "pred1 is the destination-sized block");570 assert_eq!(pred1.len(), w * h, "pred1 is the destination-sized block");
@@ -584,7 +584,7 @@ pub fn blend_masked_compound(
584 i32::from(mask[i * mask_stride + j])584 i32::from(mask[i * mask_stride + j])
585 };585 };
586 let res = (m * pred0[i * w + j] + (64 - m) * pred1[i * w + j]) >> 6;586 let res = (m * pred0[i * w + j] + (64 - m) * pred1[i * w + j]) >> 6;
587- dst[i * w + j] = round2(res, INTER_POST_ROUND).clamp(0, 255) as u8;587+ dst[i * w + j] = round2(res, INTER_POST_ROUND).clamp(0, crate::decode::sample_max()) as u16;
588 }588 }
589 }589 }
590 }590 }
crates/ec-av1/src/restoration.rsmodified+18 -18
@@ -454,8 +454,8 @@ fn round2(value: i64, shift: u32) -> i64 {
454 /// boundaries do.454 /// boundaries do.
455 #[allow(clippy::too_many_arguments)]455 #[allow(clippy::too_many_arguments)]
456 fn lr_sample(456 fn lr_sample(
457- cdef: &[u8],457+ cdef: &[u16],
458- deblocked: &[u8],458+ deblocked: &[u16],
459 stride: usize,459 stride: usize,
460 plane_w: usize,460 plane_w: usize,
461 plane_h: usize,461 plane_h: usize,
@@ -512,9 +512,9 @@ fn lr_sample(
512 /// reproduces the same output byte-for-byte.512 /// reproduces the same output byte-for-byte.
513 #[allow(clippy::too_many_arguments)]513 #[allow(clippy::too_many_arguments)]
514 fn apply_wiener_stripe(514 fn apply_wiener_stripe(
515- out: &mut [u8],515+ out: &mut [u16],
516- cdef: &[u8],516+ cdef: &[u16],
517- deblocked: &[u8],517+ deblocked: &[u16],
518 stride: usize,518 stride: usize,
519 plane_w: usize,519 plane_w: usize,
520 plane_h: usize,520 plane_h: usize,
@@ -551,7 +551,7 @@ fn apply_wiener_stripe(
551 for (t, &tap) in info.vfilter.iter().enumerate() {551 for (t, &tap) in info.vfilter.iter().enumerate() {
552 sum += tap as i64 * inter[(r + t) * w + c] as i64;552 sum += tap as i64 * inter[(r + t) * w + c] as i64;
553 }553 }
554- out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, 255) as u8;554+ out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, crate::decode::sample_max() as i64) as u16;
555 }555 }
556 }556 }
557 }557 }
@@ -613,8 +613,8 @@ const SGRPROJ_RST_BITS: u32 = 4;
613 /// reproduces the same values without porting the sliding-window trick.613 /// reproduces the same values without porting the sliding-window trick.
614 #[allow(clippy::too_many_arguments)]614 #[allow(clippy::too_many_arguments)]
615 fn compute_ab(615 fn compute_ab(
616- cdef: &[u8],616+ cdef: &[u16],
617- deblocked: &[u8],617+ deblocked: &[u16],
618 stride: usize,618 stride: usize,
619 plane_w: usize,619 plane_w: usize,
620 plane_h: usize,620 plane_h: usize,
@@ -676,9 +676,9 @@ fn compute_ab(
676 /// combined with the decoded `xqd` weights via `av1_decode_xq`.676 /// combined with the decoded `xqd` weights via `av1_decode_xq`.
677 #[allow(clippy::too_many_arguments)]677 #[allow(clippy::too_many_arguments)]
678 fn apply_sgrproj_stripe(678 fn apply_sgrproj_stripe(
679- out: &mut [u8],679+ out: &mut [u16],
680- cdef: &[u8],680+ cdef: &[u16],
681- deblocked: &[u8],681+ deblocked: &[u16],
682 stride: usize,682 stride: usize,
683 plane_w: usize,683 plane_w: usize,
684 plane_h: usize,684 plane_h: usize,
@@ -807,7 +807,7 @@ fn apply_sgrproj_stripe(
807 );807 );
808 }808 }
809 }809 }
810- let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, 255) as u8;810+ let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, crate::decode::sample_max() as i64) as u16;
811 out[(v_start as i64 + i) as usize * stride + (h_start as i64 + j) as usize] = out_v;811 out[(v_start as i64 + i) as usize * stride + (h_start as i64 + j) as usize] = out_v;
812 }812 }
813 }813 }
@@ -822,9 +822,9 @@ fn apply_sgrproj_stripe(
822 /// [`lr_sample`].822 /// [`lr_sample`].
823 #[allow(clippy::too_many_arguments)]823 #[allow(clippy::too_many_arguments)]
824 fn filter_restoration_unit(824 fn filter_restoration_unit(
825- out: &mut [u8],825+ out: &mut [u16],
826- cdef: &[u8],826+ cdef: &[u16],
827- deblocked: &[u8],827+ deblocked: &[u16],
828 stride: usize,828 stride: usize,
829 plane_w: usize,829 plane_w: usize,
830 plane_h: usize,830 plane_h: usize,
@@ -872,8 +872,8 @@ fn filter_restoration_unit(
872 /// place.872 /// place.
873 #[allow(clippy::too_many_arguments)]873 #[allow(clippy::too_many_arguments)]
874 pub(crate) fn apply_loop_restoration_plane(874 pub(crate) fn apply_loop_restoration_plane(
875- cdef: &[u8],875+ cdef: &[u16],
876- deblocked: &[u8],876+ deblocked: &[u16],
877 stride: usize,877 stride: usize,
878 plane_w: usize,878 plane_w: usize,
879 plane_h: usize,879 plane_h: usize,
@@ -882,7 +882,7 @@ pub(crate) fn apply_loop_restoration_plane(
882 unit_size: u32,882 unit_size: u32,
883 grid: &RestorationGrid,883 grid: &RestorationGrid,
884 plane: usize,884 plane: usize,
885-) -> Vec<u8> {885+) -> Vec<u16> {
886 let mut out = cdef.to_vec();886 let mut out = cdef.to_vec();
887 if ftype == RestorationType::None || unit_size == 0 {887 if ftype == RestorationType::None || unit_size == 0 {
888 return out;888 return out;
crates/ec-av1/src/warp.rsmodified+2 -2
@@ -415,8 +415,8 @@ pub fn global_warp_params(wmmat: [i32; 6]) -> Option<WarpParams> {
415 Some(WarpParams { wmmat, alpha, beta, gamma, delta })415 Some(WarpParams { wmmat, alpha, beta, gamma, delta })
416 }416 }
417 417
418-fn clip_pixel(v: i32) -> u8 {418+fn clip_pixel(v: i32) -> u16 {
419- v.clamp(0, 255) as u8419+ v.clamp(0, crate::decode::sample_max()) as u16
420 }420 }
421 421
422 /// `av1_warp_affine_c` (`warped_motion.c`), 8-bit, non-compound only:422 /// `av1_warp_affine_c` (`warped_motion.c`), 8-bit, non-compound only: