From 7e4c4a0a4bae21506761a5dc0d45483330e5e15a Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Fri, 1 Mar 2024 15:34:26 -0800 Subject: [PATCH] `Rav1dFrameContext_lf::cdef_lpf_line`: Make ptrs into offsets --- src/cdef_apply.rs | 20 ++++++++++++++------ src/decode.rs | 21 ++++++++------------- src/internal.rs | 6 +++--- src/lf_apply.rs | 24 +++++++++++++++++++++--- 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/cdef_apply.rs b/src/cdef_apply.rs index 9b197d422..ac0293abf 100644 --- a/src/cdef_apply.rs +++ b/src/cdef_apply.rs @@ -321,7 +321,10 @@ pub(crate) unsafe fn rav1d_cdef_brow( } else if sbrow_start && by == by_start { if resize { offset = ((sby - 1) * 4) as isize * y_stride + (bx * 4) as isize; - top = f.lf.cdef_lpf_line[0].cast::().offset(offset); + top = cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[0]) + .offset(offset); } else { offset = (sby * ((4 as c_int) << sb128) - 4) as isize * y_stride + (bx * 4) as isize; @@ -337,7 +340,10 @@ pub(crate) unsafe fn rav1d_cdef_brow( .offset(offset); if resize { offset = (sby * 4 + 2) as isize * y_stride + (bx * 4) as isize; - bot = f.lf.cdef_lpf_line[0].cast::().offset(offset); + bot = cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[0]) + .offset(offset); } else { let line = sby * ((4 as c_int) << sb128) + 4 * sb128 + 2; offset = line as isize * y_stride + (bx * 4) as isize; @@ -407,8 +413,9 @@ pub(crate) unsafe fn rav1d_cdef_brow( if resize { offset = ((sby - 1) * 4) as isize * uv_stride + (bx * 4 >> ss_hor) as isize; - top = f.lf.cdef_lpf_line[pl] - .cast::() + top = cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[pl]) .offset(offset); } else { let line_0 = sby * ((4 as c_int) << sb128) - 4; @@ -429,8 +436,9 @@ pub(crate) unsafe fn rav1d_cdef_brow( if resize { offset = (sby * 4 + 2) as isize * uv_stride + (bx * 4 >> ss_hor) as isize; - bot = f.lf.cdef_lpf_line[pl] - .cast::() + bot = cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[pl]) .offset(offset); } else { let line_1 = sby * ((4 as c_int) << sb128) + 4 * sb128 + 2; diff --git a/src/decode.rs b/src/decode.rs index ae8041d32..131bc56a1 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -4427,28 +4427,23 @@ pub(crate) unsafe fn rav1d_decode_frame_init( f.lf.cdef_line[1][2] = offset.wrapping_add_signed(uv_stride_px * 6); } - let mut ptr = - f.lf.cdef_line_buf - .as_mut_ptr() - .add(32) - .offset(y_stride.abs() * f.sbh as isize * 4); if need_cdef_lpf_copy != 0 { - ptr = ptr.offset(uv_stride.abs() * f.sbh as isize * 8); + offset = offset.wrapping_add_signed(uv_stride_px.abs() * f.sbh as isize * 8); if y_stride < 0 { f.lf.cdef_lpf_line[0] = - ptr.offset(-(y_stride * (f.sbh as isize * 4 - 1))) as *mut DynPixel; + offset.wrapping_add_signed(-(y_stride_px * (f.sbh as isize * 4 - 1))); } else { - f.lf.cdef_lpf_line[0] = ptr as *mut DynPixel; + f.lf.cdef_lpf_line[0] = offset; } - ptr = ptr.offset(y_stride.abs() * f.sbh as isize * 4); + offset = offset.wrapping_add_signed(y_stride_px.abs() * f.sbh as isize * 4); if uv_stride < 0 { f.lf.cdef_lpf_line[1] = - ptr.offset(-(uv_stride * (f.sbh as isize * 4 - 1))) as *mut DynPixel; + offset.wrapping_add_signed(-(uv_stride_px * (f.sbh as isize * 4 - 1))); f.lf.cdef_lpf_line[2] = - ptr.offset(-(uv_stride * (f.sbh as isize * 8 - 1))) as *mut DynPixel; + offset.wrapping_add_signed(-(uv_stride_px * (f.sbh as isize * 8 - 1))); } else { - f.lf.cdef_lpf_line[1] = ptr as *mut DynPixel; - f.lf.cdef_lpf_line[2] = ptr.offset(uv_stride * f.sbh as isize * 4) as *mut DynPixel; + f.lf.cdef_lpf_line[1] = offset; + f.lf.cdef_lpf_line[2] = offset.wrapping_add_signed(uv_stride_px * f.sbh as isize * 4); } } diff --git a/src/internal.rs b/src/internal.rs index 61fb0c87b..a61826c99 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -435,9 +435,9 @@ pub struct Rav1dFrameContext_lf { pub tx_lpf_right_edge: Vec, /* len = h*2 */ pub cdef_line_buf: AlignedVec32, /* AlignedVec32 */ pub lr_line_buf: *mut u8, - pub cdef_line: [[usize; 3]; 2], /* [2 pre/post][3 plane] */ - pub cdef_lpf_line: [*mut DynPixel; 3], /* plane */ - pub lr_lpf_line: [*mut DynPixel; 3], /* plane */ + pub cdef_line: [[usize; 3]; 2], /* [2 pre/post][3 plane] */ + pub cdef_lpf_line: [usize; 3], /* plane */ + pub lr_lpf_line: [*mut DynPixel; 3], /* plane */ // in-loop filter per-frame state keeping pub start_of_tile_row: *mut u8, diff --git a/src/lf_apply.rs b/src/lf_apply.rs index 2b4ce3808..9ecfb4b67 100644 --- a/src/lf_apply.rs +++ b/src/lf_apply.rs @@ -13,6 +13,7 @@ use libc::ptrdiff_t; use std::cmp; use std::ffi::c_int; use std::ffi::c_uint; +use std::mem; use std::slice; // The loop filter buffer stores 12 rows of pixels. A superblock block will @@ -191,6 +192,12 @@ pub(crate) unsafe fn rav1d_copy_lpf( .offset(tt_off as isize * BD::pxstride(*lr_stride.offset(1) as usize) as isize), ]; let restore_planes = f.lf.restore_planes; + + // Temporarily take ownership of `cdef_line_buf` so that we can mutate while + // still being able to take references to `f`. + let mut cdef_line_buf_tmp = mem::take(&mut f.lf.cdef_line_buf); + let cdef_line_buf = BD::cast_pixel_slice_mut(&mut cdef_line_buf_tmp); + if seq_hdr.cdef != 0 || restore_planes & LR_RESTORE_Y as c_int != 0 { let h = f.cur.p.h; let w = f.bw << 2; @@ -222,7 +229,10 @@ pub(crate) unsafe fn rav1d_copy_lpf( backup_lpf::( c, f, - (f.lf.cdef_lpf_line[0] as *mut BD::Pixel).offset(cdef_off_y as isize), + cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[0]) + .offset(cdef_off_y), *src_stride.offset(0), (*src.offset(0)).offset( -offset as isize * BD::pxstride(*src_stride.offset(0) as usize) as isize, @@ -276,7 +286,10 @@ pub(crate) unsafe fn rav1d_copy_lpf( backup_lpf::( c, f, - (f.lf.cdef_lpf_line[1] as *mut BD::Pixel).offset(cdef_off_uv as isize), + cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[1]) + .offset(cdef_off_uv), *src_stride.offset(1), (*src.offset(1)).offset( -offset_uv as isize * BD::pxstride(*src_stride.offset(1) as usize) as isize, @@ -318,7 +331,10 @@ pub(crate) unsafe fn rav1d_copy_lpf( backup_lpf::( c, f, - (f.lf.cdef_lpf_line[2] as *mut BD::Pixel).offset(cdef_off_uv as isize), + cdef_line_buf + .as_mut_ptr() + .add(f.lf.cdef_lpf_line[2]) + .offset(cdef_off_uv), *src_stride.offset(1), (*src.offset(2)).offset( -offset_uv as isize * BD::pxstride(*src_stride.offset(1) as usize) as isize, @@ -336,6 +352,8 @@ pub(crate) unsafe fn rav1d_copy_lpf( } } } + + f.lf.cdef_line_buf = cdef_line_buf_tmp; } /// Slice `[u8; 4]`s from `lvl`, but "unaligned",