From f90936b12185ce34e696192f76f6e7cdf7f600e6 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 27 Feb 2024 15:20:23 -0800 Subject: [PATCH] `Rav1dFrameContext_lf::tx_lpf_right_edge`: Split and make into `Vec`s --- src/decode.rs | 32 ++++++++------------------------ src/internal.rs | 15 +++++++++++++-- src/lf_apply.rs | 16 +++++++--------- src/lib.rs | 2 +- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index 1a11c8ff8..9cee3628a 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -4237,23 +4237,15 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow( // backup t->a/l.tx_lpf_y/uv at tile boundaries to use them to "fix" // up the initial value in neighbour tiles when running the loopfilter let mut align_h = f.bh + 31 & !31; - slice::from_raw_parts_mut( - f.lf.tx_lpf_right_edge[0], - (align_h * tile_col + t.by + sb_step).try_into().unwrap(), - )[(align_h * tile_col + t.by).try_into().unwrap()..][..sb_step.try_into().unwrap()] - .copy_from_slice(&t.l.tx_lpf_y.0[(t.by & 16) as usize..][..sb_step.try_into().unwrap()]); + let (tx_lpf_right_edge_y, tx_lpf_right_edge_uv) = f.lf.tx_lpf_right_edge_mut(); + tx_lpf_right_edge_y[(align_h * tile_col + t.by) as usize..][..sb_step as usize] + .copy_from_slice(&t.l.tx_lpf_y.0[(t.by & 16) as usize..][..sb_step as usize]); let ss_ver = (f.cur.p.layout == Rav1dPixelLayout::I420) as c_int; align_h >>= ss_ver; - slice::from_raw_parts_mut( - f.lf.tx_lpf_right_edge[1], - (align_h * tile_col + (t.by >> ss_ver) + (sb_step >> ss_ver)) - .try_into() - .unwrap(), - )[(align_h * tile_col + (t.by >> ss_ver)).try_into().unwrap()..] - [..(sb_step >> ss_ver).try_into().unwrap()] + tx_lpf_right_edge_uv[(align_h * tile_col + (t.by >> ss_ver)) as usize..] + [..(sb_step >> ss_ver) as usize] .copy_from_slice( - &t.l.tx_lpf_uv.0[((t.by & 16) >> ss_ver) as usize..] - [..(sb_step >> ss_ver).try_into().unwrap()], + &t.l.tx_lpf_uv.0[((t.by & 16) >> ss_ver) as usize..][..(sb_step >> ss_ver) as usize], ); Ok(()) @@ -4562,16 +4554,8 @@ pub(crate) unsafe fn rav1d_decode_frame_init( } let re_sz = f.sb128h * frame_hdr.tiling.cols; - if re_sz != f.lf.re_sz { - freep(&mut *f.lf.tx_lpf_right_edge.as_mut_ptr().offset(0) as *mut *mut u8 as *mut c_void); - f.lf.tx_lpf_right_edge[0] = malloc(re_sz as usize * 32 * 2) as *mut u8; - if f.lf.tx_lpf_right_edge[0].is_null() { - f.lf.re_sz = 0; - return Err(ENOMEM); - } - f.lf.tx_lpf_right_edge[1] = f.lf.tx_lpf_right_edge[0].offset((re_sz * 32) as isize); - f.lf.re_sz = re_sz; - } + // TODO: Fallible allocation + f.lf.tx_lpf_right_edge.resize(re_sz as usize * 32 * 2, 0); // init ref mvs if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc != 0 { diff --git a/src/internal.rs b/src/internal.rs index c3e5ec475..426f78761 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -459,11 +459,10 @@ pub struct Rav1dFrameContext_lf { pub cdef_buf_plane_sz: [c_int; 2], /* stride*sbh*4 */ pub cdef_buf_sbh: c_int, pub lr_buf_plane_sz: [c_int; 2], /* (stride*sbh*4) << sb128 if n_tc > 1, else stride*4 */ - pub re_sz: c_int, /* h */ pub lim_lut: Align16, pub last_sharpness: c_int, pub lvl: [[[[u8; 2]; 8]; 4]; 8], /* [8 seg_id][4 dir][8 ref][2 is_gmv] */ - pub tx_lpf_right_edge: [*mut u8; 2], + pub tx_lpf_right_edge: Vec, /* len = h*2 */ pub cdef_line_buf: *mut u8, pub lr_line_buf: *mut u8, pub cdef_line: [[*mut DynPixel; 3]; 2], /* [2 pre/post][3 plane] */ @@ -479,6 +478,18 @@ pub struct Rav1dFrameContext_lf { pub restore_planes: c_int, // enum LrRestorePlanes } +impl Rav1dFrameContext_lf { + pub fn tx_lpf_right_edge(&self) -> (&[u8], &[u8]) { + let mid = self.tx_lpf_right_edge.len() / 2; + self.tx_lpf_right_edge.split_at(mid) + } + + pub fn tx_lpf_right_edge_mut(&mut self) -> (&mut [u8], &mut [u8]) { + let mid = self.tx_lpf_right_edge.len() / 2; + self.tx_lpf_right_edge.split_at_mut(mid) + } +} + #[repr(C)] pub struct Rav1dFrameContext_task_thread_pending_tasks { pub head: *mut Rav1dTask, diff --git a/src/lf_apply.rs b/src/lf_apply.rs index f976d5166..2b4ce3808 100644 --- a/src/lf_apply.rs +++ b/src/lf_apply.rs @@ -560,10 +560,9 @@ pub(crate) unsafe fn rav1d_loopfilter_sbrow_cols( let hmax = (1 as c_uint) << hmask; let endy4 = (starty4 + cmp::min(f.h4 - sby * sbsz, sbsz)) as c_uint; let uv_endy4: c_uint = endy4.wrapping_add(ss_ver as c_uint) >> ss_ver; - let mut lpf_y: *const u8 = &mut *(*(f.lf.tx_lpf_right_edge).as_ptr().offset(0)) - .offset((sby << sbl2) as isize) as *mut u8; - let mut lpf_uv: *const u8 = &mut *(*(f.lf.tx_lpf_right_edge).as_ptr().offset(1)) - .offset((sby << sbl2 - ss_ver) as isize) as *mut u8; + let (lpf_y, lpf_uv) = f.lf.tx_lpf_right_edge(); + let mut lpf_y = &lpf_y[(sby << sbl2) as usize..]; + let mut lpf_uv = &lpf_uv[(sby << sbl2 - ss_ver) as usize..]; let frame_hdr = &***f.frame_hdr.as_ref().unwrap(); let mut tile_col = 1; loop { @@ -592,7 +591,7 @@ pub(crate) unsafe fn rav1d_loopfilter_sbrow_cols( *fresh2 = (*fresh2 as c_uint & !smask) as u16; let ref mut fresh3 = (*y_hmask.offset(cmp::min( idx, - *lpf_y.offset(y.wrapping_sub(starty4 as c_uint) as isize) as c_int, + lpf_y[y.wrapping_sub(starty4 as c_uint) as usize] as c_int, ) as isize))[sidx as usize]; *fresh3 = (*fresh3 as c_uint | smask) as u16; y = y.wrapping_add(1); @@ -614,16 +613,15 @@ pub(crate) unsafe fn rav1d_loopfilter_sbrow_cols( *fresh5 = (*fresh5 as c_uint & !smask_0) as u16; let ref mut fresh6 = (*uv_hmask.offset(cmp::min( idx_0, - *lpf_uv.offset(y_0.wrapping_sub((starty4 >> ss_ver) as c_uint) as isize) - as c_int, + lpf_uv[y_0.wrapping_sub((starty4 >> ss_ver) as c_uint) as usize] as c_int, ) as isize))[sidx_0 as usize]; *fresh6 = (*fresh6 as c_uint | smask_0) as u16; y_0 = y_0.wrapping_add(1); uv_mask <<= 1; } } - lpf_y = lpf_y.offset(halign as isize); - lpf_uv = lpf_uv.offset((halign >> ss_ver) as isize); + lpf_y = &lpf_y[halign as usize..]; + lpf_uv = &lpf_uv[(halign >> ss_ver) as usize..]; tile_col += 1; } if start_of_tile_row != 0 { diff --git a/src/lib.rs b/src/lib.rs index b0efb8d26..a9ae58ae1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -918,7 +918,7 @@ impl Drop for Rav1dContext { let _ = mem::take(&mut f.lf.mask); // TODO: remove when context is owned free(f.lf.lr_mask as *mut c_void); let _ = mem::take(&mut f.lf.level); - free(f.lf.tx_lpf_right_edge[0] as *mut c_void); + let _ = mem::take(&mut f.lf.tx_lpf_right_edge); // TODO: remove when context is owned free(f.lf.start_of_tile_row as *mut c_void); rav1d_refmvs_clear(&mut f.rf); rav1d_free_aligned(f.lf.cdef_line_buf as *mut c_void);