Skip to content

Commit

Permalink
fn Rav1dFrameData::bd_fn: Replace field with a method doing the loo…
Browse files Browse the repository at this point in the history
…kup through the existing `bitdepth_max` field, avoiding `.clone()`s and memory.
  • Loading branch information
kkysen committed Feb 16, 2024
1 parent 17abdc1 commit dbfbf5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
22 changes: 5 additions & 17 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ use crate::src::error::Rav1dResult;
use crate::src::filmgrain::Rav1dFilmGrainDSPContext;
use crate::src::internal::Rav1dContext;
use crate::src::internal::Rav1dContextTaskType;
use crate::src::internal::Rav1dFrameContext_bd_fn;
use crate::src::internal::Rav1dFrameData;
use crate::src::internal::Rav1dTaskContext;
use crate::src::internal::Rav1dTaskContext_scratch_pal;
Expand Down Expand Up @@ -1474,7 +1473,7 @@ unsafe fn decode_b_inner(
}

let ts = &mut *t.ts;
let bd_fn = f.bd_fn.clone();
let bd_fn = f.bd_fn();
let b_dim = &dav1d_block_dimensions[bs as usize];
let bx4 = t.bx & 31;
let by4 = t.by & 31;
Expand Down Expand Up @@ -4115,7 +4114,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
t.a = (t.a).offset(1);
}
}
(f.bd_fn.backup_ipred_edge)(f, t);
(f.bd_fn().backup_ipred_edge)(f, t);
return Ok(());
}

Expand Down Expand Up @@ -4241,7 +4240,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(

// backup pre-loopfilter pixels for intra prediction of the next sbrow
if t.frame_thread.pass != 1 {
(f.bd_fn.backup_ipred_edge)(f, t);
(f.bd_fn().backup_ipred_edge)(f, t);
}

// backup t->a/l.tx_lpf_y/uv at tile boundaries to use them to "fix"
Expand Down Expand Up @@ -4831,7 +4830,7 @@ unsafe fn rav1d_decode_frame_main(c: &Rav1dContext, f: &mut Rav1dFrameData) -> R
// and post-filtering, so that the full process runs in-line
let Rav1dFrameHeader_tiling { rows, cols, .. } = frame_hdr.tiling;
let [rows, cols] = [rows, cols].map(|it| it.try_into().unwrap());
// Need to clone this because `(f.bd_fn.filter_sbrow)(f, sby);` takes a `&mut` to `f` within the loop.
// Need to clone this because `(f.bd_fn().filter_sbrow)(f, sby);` takes a `&mut` to `f` within the loop.
let row_start_sb = frame_hdr.tiling.row_start_sb.clone();
for (tile_row, (sbh_start_end, ts)) in iter::zip(
row_start_sb[..rows + 1].windows(2),
Expand Down Expand Up @@ -4868,7 +4867,7 @@ unsafe fn rav1d_decode_frame_main(c: &Rav1dContext, f: &mut Rav1dFrameData) -> R
}

// loopfilter + cdef + restoration
(f.bd_fn.filter_sbrow)(c, f, &mut t, sby);
(f.bd_fn().filter_sbrow)(c, f, &mut t, sby);
}
}

Expand Down Expand Up @@ -5092,17 +5091,6 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
}
}
}
if seq_hdr.hbd == 0 {
#[cfg(feature = "bitdepth_8")]
{
f.bd_fn = Rav1dFrameContext_bd_fn::new::<BitDepth8>();
}
} else {
#[cfg(feature = "bitdepth_16")]
{
f.bd_fn = Rav1dFrameContext_bd_fn::new::<BitDepth16>();
}
}

fn scale_fac(ref_sz: i32, this_sz: i32) -> i32 {
((ref_sz << 14) + (this_sz >> 1)) / this_sz
Expand Down
8 changes: 5 additions & 3 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ pub struct ScalableMotionParams {
pub step: c_int,
}

#[repr(C)]
#[derive(Clone)]
pub(crate) struct Rav1dFrameContext_bd_fn {
pub recon_b_intra: recon_b_intra_fn,
pub recon_b_inter: recon_b_inter_fn,
Expand Down Expand Up @@ -554,7 +552,6 @@ pub(crate) struct Rav1dFrameData {
pub ts: *mut Rav1dTileState,
pub n_ts: c_int,
pub dsp: *const Rav1dDSPContext,
pub bd_fn: Rav1dFrameContext_bd_fn,

pub ipred_edge_sz: c_int,
pub ipred_edge: [*mut DynPixel; 3],
Expand Down Expand Up @@ -585,6 +582,11 @@ pub(crate) struct Rav1dFrameData {
}

impl Rav1dFrameData {
pub fn bd_fn(&self) -> &'static Rav1dFrameContext_bd_fn {
let bpc = BPC::from_bitdepth_max(self.bitdepth_max);
Rav1dFrameContext_bd_fn::get(bpc)
}

pub fn frame_hdr(&self) -> &Rav1dFrameHeader {
self.frame_hdr.as_ref().unwrap()
}
Expand Down
10 changes: 5 additions & 5 deletions src/thread_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
}
RAV1D_TASK_TYPE_DEBLOCK_COLS => {
if f.task_thread.error.load(Ordering::SeqCst) == 0 {
(f.bd_fn.filter_sbrow_deblock_cols)(c, f, &mut tc, sby);
(f.bd_fn().filter_sbrow_deblock_cols)(c, f, &mut tc, sby);
}
if ensure_progress(
ttd,
Expand All @@ -1241,7 +1241,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
}
RAV1D_TASK_TYPE_DEBLOCK_ROWS => {
if f.task_thread.error.load(Ordering::SeqCst) == 0 {
(f.bd_fn.filter_sbrow_deblock_rows)(c, f, &mut tc, sby);
(f.bd_fn().filter_sbrow_deblock_rows)(c, f, &mut tc, sby);
}
// signal deblock progress
let seq_hdr = &***f.seq_hdr.as_ref().unwrap();
Expand Down Expand Up @@ -1288,7 +1288,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
let seq_hdr = &***f.seq_hdr.as_ref().unwrap();
if seq_hdr.cdef != 0 {
if f.task_thread.error.load(Ordering::SeqCst) == 0 {
(f.bd_fn.filter_sbrow_cdef)(c, f, &mut tc, sby);
(f.bd_fn().filter_sbrow_cdef)(c, f, &mut tc, sby);
}
reset_task_cur_async(ttd, t.frame_idx, c.n_fc);
if ttd.cond_signaled.fetch_or(1, Ordering::SeqCst) == 0 {
Expand All @@ -1302,7 +1302,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
let frame_hdr = &***f.frame_hdr.as_ref().unwrap();
if frame_hdr.size.width[0] != frame_hdr.size.width[1] {
if f.task_thread.error.load(Ordering::SeqCst) == 0 {
(f.bd_fn.filter_sbrow_resize)(c, f, &mut tc, sby);
(f.bd_fn().filter_sbrow_resize)(c, f, &mut tc, sby);
}
}
task_type = RAV1D_TASK_TYPE_LOOP_RESTORATION;
Expand All @@ -1312,7 +1312,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
if f.task_thread.error.load(Ordering::SeqCst) == 0
&& f.lf.restore_planes != 0
{
(f.bd_fn.filter_sbrow_lr)(c, f, &mut tc, sby);
(f.bd_fn().filter_sbrow_lr)(c, f, &mut tc, sby);
}
task_type = RAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS;
continue 'fallthrough;
Expand Down

0 comments on commit dbfbf5c

Please sign in to comment.