Skip to content

Commit

Permalink
fn mc: Make r#ref var safe w/ Rav1dPictureDataComponentOffset (#…
Browse files Browse the repository at this point in the history
…1218)

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.
  • Loading branch information
kkysen authored Jun 21, 2024
2 parents 41b267d + 83638a5 commit 9ff6ac5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
13 changes: 10 additions & 3 deletions include/dav1d/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ pub struct AlignedPixelChunk([u8; RAV1D_PICTURE_ALIGNMENT]);
const _: () = assert!(mem::align_of::<AlignedPixelChunk>() == RAV1D_PICTURE_ALIGNMENT);
const _: () = assert!(mem::size_of::<AlignedPixelChunk>() == 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 {
Expand All @@ -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.
Expand Down Expand Up @@ -185,7 +192,7 @@ impl Rav1dPictureDataComponentInner {
let ptr = NonNull::new(buf.as_mut_ptr()).unwrap();
assert!(ptr.cast::<AlignedPixelChunk>().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::<BD::Pixel>()) as isize;
Self { ptr, len, stride }
}
Expand Down Expand Up @@ -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
}
}
Expand Down
34 changes: 18 additions & 16 deletions src/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,18 +1106,18 @@ 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,
my: i32,
bd: BD,
) {
let dst = dst.cast();
let src = src.cast();
let src_ptr = src.as_ptr::<BD>().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)
}
}

Expand All @@ -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,
Expand All @@ -1151,9 +1150,12 @@ impl mc_scaled::Fn {
bd: BD,
) {
let dst = dst.cast();
let src = src.cast();
let src_ptr = src.as_ptr::<BD>().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,
)
}
}

Expand Down Expand Up @@ -1202,18 +1204,18 @@ impl mct::Fn {
pub unsafe fn call<BD: BitDepth>(
&self,
tmp: &mut [i16],
src: *const BD::Pixel,
src_stride: isize,
src: Rav1dPictureDataComponentOffset,
w: i32,
h: i32,
mx: i32,
my: i32,
bd: BD,
) {
let tmp = tmp[..(w * h) as usize].as_mut_ptr();
let src = src.cast();
let src_ptr = src.as_ptr::<BD>().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)
}
}

Expand All @@ -1234,8 +1236,7 @@ impl mct_scaled::Fn {
pub unsafe fn call<BD: BitDepth>(
&self,
tmp: &mut [i16],
src: *const BD::Pixel,
src_stride: isize,
src: Rav1dPictureDataComponentOffset,
w: i32,
h: i32,
mx: i32,
Expand All @@ -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::<BD>().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)
}
}

Expand Down
48 changes: 22 additions & 26 deletions src/recon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,8 +2060,6 @@ unsafe fn mc<BD: BitDepth>(
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);
Expand All @@ -2076,7 +2074,7 @@ unsafe fn mc<BD: BitDepth>(
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
Expand All @@ -2093,28 +2091,26 @@ unsafe fn mc<BD: BitDepth>(
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::<BD::Pixel>() as isize;
let stride = 192;
Rav1dPictureDataComponentOffset {
data: &Rav1dPictureDataComponent::wrap_buf::<BD>(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::<BD>()
.offset(BD::pxstride(ref_stride) * dy as isize)
.add(dx as usize);
}
let r#ref = &ref_data[pl];
r#ref.with_offset::<BD>() + (dy as isize * r#ref.pixel_stride::<BD>()) + dx as usize
};

let w = bw4 * h_mul;
let h = bh4 * v_mul;
let mx = mx << (ss_hor == 0) as u8;
let my = my << (ss_ver == 0) as u8;
match dst {
MaybeTempPixels::NonTemp { dst, dst_stride } => {
f.dsp.mc.mc[filter_2d]
.call::<BD>(dst, dst_stride, r#ref, ref_stride, w, h, mx, my, bd);
f.dsp.mc.mc[filter_2d].call::<BD>(dst, dst_stride, r#ref, w, h, mx, my, bd);
}
MaybeTempPixels::Temp { tmp, tmp_stride: _ } => {
f.dsp.mc.mct[filter_2d].call::<BD>(tmp, r#ref, ref_stride, w, h, mx, my, bd);
f.dsp.mc.mct[filter_2d].call::<BD>(tmp, r#ref, w, h, mx, my, bd);
}
}
} else {
Expand Down Expand Up @@ -2152,7 +2148,7 @@ unsafe fn mc<BD: BitDepth>(

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::<BD>();
f.dsp.mc.emu_edge.call::<BD>(
(right - left + 7) as intptr_t,
Expand All @@ -2165,17 +2161,18 @@ unsafe fn mc<BD: BitDepth>(
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::<BD::Pixel>() as isize;
if debug_block_info!(f, b) {
println!("Emu");
}
let stride = 320;
Rav1dPictureDataComponentOffset {
data: &Rav1dPictureDataComponent::wrap_buf::<BD>(emu_edge_buf, stride),
offset: stride * 3 + 3,
}
} else {
r#ref = ref_data[pl]
.as_strided_mut_ptr::<BD>()
.offset(BD::pxstride(ref_stride) * top as isize)
.offset(left as isize);
}
let r#ref = &ref_data[pl];
r#ref.with_offset::<BD>() + (top as isize * r#ref.pixel_stride::<BD>()) + left as isize
};

let w = bw4 * h_mul;
let h = bh4 * v_mul;
Expand All @@ -2186,11 +2183,10 @@ unsafe fn mc<BD: BitDepth>(
match dst {
MaybeTempPixels::NonTemp { dst, dst_stride } => {
f.dsp.mc.mc_scaled[filter_2d]
.call::<BD>(dst, dst_stride, r#ref, ref_stride, w, h, mx, my, dx, dy, bd);
.call::<BD>(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::<BD>(tmp, r#ref, ref_stride, w, h, mx, my, dx, dy, bd);
f.dsp.mc.mct_scaled[filter_2d].call::<BD>(tmp, r#ref, w, h, mx, my, dx, dy, bd);
}
}
}
Expand Down

0 comments on commit 9ff6ac5

Please sign in to comment.