From 83638a5952754415bbb52300ccaade55f92e406a Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sun, 16 Jun 2024 02:18:12 -0700 Subject: [PATCH] `fn mc`: Make `r#ref` var a `Rav1dPictureDataComponentOffset`, using `fn wrap_buf` for `emu_edge_buf`, and passing it to `fn mc{,t}{,_scaled}::Fn::call`. To accomodate `emu_edge_buf`, which is `320 * (256 + 7)`, this also introduces `const RAV1D_PICTURE_GUARANTEED_MULTIPLE = 64` (as opposed to `64 * 64`), which is what's checked and `assume`d always. `RAV1D_PICTURE_MULTIPLE` is still checked for actual `Rav1dPictureData`s. --- include/dav1d/picture.rs | 13 ++++++++--- src/mc.rs | 34 ++++++++++++++-------------- src/recon.rs | 48 ++++++++++++++++++---------------------- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/include/dav1d/picture.rs b/include/dav1d/picture.rs index 36f971940..48d1a9deb 100644 --- a/include/dav1d/picture.rs +++ b/include/dav1d/picture.rs @@ -120,6 +120,13 @@ pub struct AlignedPixelChunk([u8; RAV1D_PICTURE_ALIGNMENT]); const _: () = assert!(mem::align_of::() == RAV1D_PICTURE_ALIGNMENT); const _: () = assert!(mem::size_of::() == RAV1D_PICTURE_ALIGNMENT); +/// The guaranteed length multiple of [`Rav1dPictureDataComponentInner`]s. +/// This is checked and [`assume`]d. +const RAV1D_PICTURE_GUARANTEED_MULTIPLE: usize = 64; + +/// Actual [`Rav1dPictureData`]'s components should be multiples of this, +/// as this is guaranteed by [`Rav1dPicAllocator::alloc_picture_callback`], +/// though wrapped buffers may only be [`RAV1D_PICTURE_GUARANTEED_MULTIPLE`]. const RAV1D_PICTURE_MULTIPLE: usize = 64 * 64; pub struct Rav1dPictureDataComponentInner { @@ -131,7 +138,7 @@ pub struct Rav1dPictureDataComponentInner { /// The length of [`Self::ptr`] in [`u8`] bytes. /// - /// It is a multiple of [`RAV1D_PICTURE_MULTIPLE`]. + /// It is a multiple of [`RAV1D_PICTURE_GUARANTEED_MULTIPLE`]. len: usize, /// The stride of [`Self::ptr`] in [`u8`] bytes. @@ -185,7 +192,7 @@ impl Rav1dPictureDataComponentInner { let ptr = NonNull::new(buf.as_mut_ptr()).unwrap(); assert!(ptr.cast::().is_aligned()); let len = buf.len(); - assert!(len % RAV1D_PICTURE_MULTIPLE == 0); + assert!(len % RAV1D_PICTURE_GUARANTEED_MULTIPLE == 0); let stride = (stride * mem::size_of::()) as isize; Self { ptr, len, stride } } @@ -213,7 +220,7 @@ unsafe impl AsMutPtr for Rav1dPictureDataComponentInner { #[inline] // Inline so callers can see the assume. fn len(&self) -> usize { // SAFETY: We already checked this in `Self::new`. - unsafe { assume(self.len % RAV1D_PICTURE_MULTIPLE == 0) }; + unsafe { assume(self.len % RAV1D_PICTURE_GUARANTEED_MULTIPLE == 0) }; self.len } } diff --git a/src/mc.rs b/src/mc.rs index 322e4507c..49f17f9d3 100644 --- a/src/mc.rs +++ b/src/mc.rs @@ -1106,8 +1106,7 @@ impl mc::Fn { &self, dst: *mut BD::Pixel, dst_stride: isize, - src: *const BD::Pixel, - src_stride: isize, + src: Rav1dPictureDataComponentOffset, w: i32, h: i32, mx: i32, @@ -1115,9 +1114,10 @@ impl mc::Fn { bd: BD, ) { let dst = dst.cast(); - let src = src.cast(); + let src_ptr = src.as_ptr::().cast(); + let src_stride = src.stride(); let bd = bd.into_c(); - self.get()(dst, dst_stride, src, src_stride, w, h, mx, my, bd) + self.get()(dst, dst_stride, src_ptr, src_stride, w, h, mx, my, bd) } } @@ -1140,8 +1140,7 @@ impl mc_scaled::Fn { &self, dst: *mut BD::Pixel, dst_stride: isize, - src: *const BD::Pixel, - src_stride: isize, + src: Rav1dPictureDataComponentOffset, w: i32, h: i32, mx: i32, @@ -1151,9 +1150,12 @@ impl mc_scaled::Fn { bd: BD, ) { let dst = dst.cast(); - let src = src.cast(); + let src_ptr = src.as_ptr::().cast(); + let src_stride = src.stride(); let bd = bd.into_c(); - self.get()(dst, dst_stride, src, src_stride, w, h, mx, my, dx, dy, bd) + self.get()( + dst, dst_stride, src_ptr, src_stride, w, h, mx, my, dx, dy, bd, + ) } } @@ -1202,8 +1204,7 @@ impl mct::Fn { pub unsafe fn call( &self, tmp: &mut [i16], - src: *const BD::Pixel, - src_stride: isize, + src: Rav1dPictureDataComponentOffset, w: i32, h: i32, mx: i32, @@ -1211,9 +1212,10 @@ impl mct::Fn { bd: BD, ) { let tmp = tmp[..(w * h) as usize].as_mut_ptr(); - let src = src.cast(); + let src_ptr = src.as_ptr::().cast(); + let src_stride = src.stride(); let bd = bd.into_c(); - self.get()(tmp, src, src_stride, w, h, mx, my, bd) + self.get()(tmp, src_ptr, src_stride, w, h, mx, my, bd) } } @@ -1234,8 +1236,7 @@ impl mct_scaled::Fn { pub unsafe fn call( &self, tmp: &mut [i16], - src: *const BD::Pixel, - src_stride: isize, + src: Rav1dPictureDataComponentOffset, w: i32, h: i32, mx: i32, @@ -1245,9 +1246,10 @@ impl mct_scaled::Fn { bd: BD, ) { let tmp = tmp[..(w * h) as usize].as_mut_ptr(); - let src = src.cast(); + let src_ptr = src.as_ptr::().cast(); + let src_stride = src.stride(); let bd = bd.into_c(); - self.get()(tmp, src, src_stride, w, h, mx, my, dx, dy, bd) + self.get()(tmp, src_ptr, src_stride, w, h, mx, my, dx, dy, bd) } } diff --git a/src/recon.rs b/src/recon.rs index 91a16dab9..8d0832356 100644 --- a/src/recon.rs +++ b/src/recon.rs @@ -2060,8 +2060,6 @@ unsafe fn mc( let mvy = mv.y as c_int; let mx = mvx & 15 >> (ss_hor == 0) as c_int; let my = mvy & 15 >> (ss_ver == 0) as c_int; - let mut ref_stride = refp.p.stride[(pl != 0) as usize]; - let r#ref; if refp.p.p.w == f.cur.p.w && refp.p.p.h == f.cur.p.h { let dx = bx * h_mul + (mvx >> 3 + ss_hor); @@ -2076,7 +2074,7 @@ unsafe fn mc( w = f.bw * 4 >> ss_hor; h = f.bh * 4 >> ss_ver; } - if dx < (mx != 0) as c_int * 3 + let r#ref = if dx < (mx != 0) as c_int * 3 || dy < (my != 0) as c_int * 3 || dx + bw4 * h_mul + (mx != 0) as c_int * 4 > w || dy + bh4 * v_mul + (my != 0) as c_int * 4 > h @@ -2093,16 +2091,15 @@ unsafe fn mc( 192, &ref_data[pl], ); - r#ref = emu_edge_buf - .as_mut_ptr() - .add((192 * (my != 0) as c_int * 3 + (mx != 0) as c_int * 3) as usize); - ref_stride = 192 * ::core::mem::size_of::() as isize; + let stride = 192; + Rav1dPictureDataComponentOffset { + data: &Rav1dPictureDataComponent::wrap_buf::(emu_edge_buf, stride), + offset: stride * (my != 0) as usize * 3 + (mx != 0) as usize * 3, + } } else { - r#ref = ref_data[pl] - .as_strided_mut_ptr::() - .offset(BD::pxstride(ref_stride) * dy as isize) - .add(dx as usize); - } + let r#ref = &ref_data[pl]; + r#ref.with_offset::() + (dy as isize * r#ref.pixel_stride::()) + dx as usize + }; let w = bw4 * h_mul; let h = bh4 * v_mul; @@ -2110,11 +2107,10 @@ unsafe fn mc( let my = my << (ss_ver == 0) as u8; match dst { MaybeTempPixels::NonTemp { dst, dst_stride } => { - f.dsp.mc.mc[filter_2d] - .call::(dst, dst_stride, r#ref, ref_stride, w, h, mx, my, bd); + f.dsp.mc.mc[filter_2d].call::(dst, dst_stride, r#ref, w, h, mx, my, bd); } MaybeTempPixels::Temp { tmp, tmp_stride: _ } => { - f.dsp.mc.mct[filter_2d].call::(tmp, r#ref, ref_stride, w, h, mx, my, bd); + f.dsp.mc.mct[filter_2d].call::(tmp, r#ref, w, h, mx, my, bd); } } } else { @@ -2152,7 +2148,7 @@ unsafe fn mc( let w = refp.p.p.w + ss_hor >> ss_hor; let h = refp.p.p.h + ss_ver >> ss_ver; - if left < 3 || top < 3 || right + 4 > w || bottom + 4 > h { + let r#ref = if left < 3 || top < 3 || right + 4 > w || bottom + 4 > h { let emu_edge_buf = emu_edge.buf_mut::(); f.dsp.mc.emu_edge.call::( (right - left + 7) as intptr_t, @@ -2165,17 +2161,18 @@ unsafe fn mc( 320, &ref_data[pl], ); - r#ref = emu_edge_buf.as_mut_ptr().add((320 * 3 + 3) as usize); - ref_stride = 320 * ::core::mem::size_of::() as isize; if debug_block_info!(f, b) { println!("Emu"); } + let stride = 320; + Rav1dPictureDataComponentOffset { + data: &Rav1dPictureDataComponent::wrap_buf::(emu_edge_buf, stride), + offset: stride * 3 + 3, + } } else { - r#ref = ref_data[pl] - .as_strided_mut_ptr::() - .offset(BD::pxstride(ref_stride) * top as isize) - .offset(left as isize); - } + let r#ref = &ref_data[pl]; + r#ref.with_offset::() + (top as isize * r#ref.pixel_stride::()) + left as isize + }; let w = bw4 * h_mul; let h = bh4 * v_mul; @@ -2186,11 +2183,10 @@ unsafe fn mc( match dst { MaybeTempPixels::NonTemp { dst, dst_stride } => { f.dsp.mc.mc_scaled[filter_2d] - .call::(dst, dst_stride, r#ref, ref_stride, w, h, mx, my, dx, dy, bd); + .call::(dst, dst_stride, r#ref, w, h, mx, my, dx, dy, bd); } MaybeTempPixels::Temp { tmp, tmp_stride: _ } => { - f.dsp.mc.mct_scaled[filter_2d] - .call::(tmp, r#ref, ref_stride, w, h, mx, my, dx, dy, bd); + f.dsp.mc.mct_scaled[filter_2d].call::(tmp, r#ref, w, h, mx, my, dx, dy, bd); } } }