Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn Rav1dFrameContext_bd_fn::get: Replace field with lookups to static versions #752

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ pub enum BPC {
BPC16,
}

impl BPC {
pub fn from_bitdepth_max(bitdepth_max: c_int) -> Self {
if bitdepth_max == BitDepth8::new(()).bitdepth_max().into() {
Self::BPC8
} else {
Self::BPC16
}
}
}

pub trait BitDepth: Clone + Copy {
const BPC: BPC;
const BITDEPTH: u8;
Expand Down
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
22 changes: 18 additions & 4 deletions src/internal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::include::common::bitdepth::BitDepth;
use crate::include::common::bitdepth::BitDepth16;
use crate::include::common::bitdepth::BitDepth8;
use crate::include::common::bitdepth::BitDepthDependentType;
use crate::include::common::bitdepth::BitDepthUnion;
use crate::include::common::bitdepth::DynCoef;
use crate::include::common::bitdepth::DynPixel;
use crate::include::common::bitdepth::BPC;
use crate::include::dav1d::common::Rav1dDataProps;
use crate::include::dav1d::data::Rav1dData;
use crate::include::dav1d::dav1d::Rav1dDecodeFrameType;
Expand Down Expand Up @@ -328,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 All @@ -345,7 +346,7 @@ pub(crate) struct Rav1dFrameContext_bd_fn {
}

impl Rav1dFrameContext_bd_fn {
pub fn new<BD: BitDepth>() -> Self {
pub const fn new<BD: BitDepth>() -> Self {
Self {
recon_b_inter: rav1d_recon_b_inter::<BD>,
recon_b_intra: rav1d_recon_b_intra::<BD>,
Expand All @@ -360,6 +361,15 @@ impl Rav1dFrameContext_bd_fn {
}
}

pub const fn get(bpc: BPC) -> &'static Self {
const BPC8: Rav1dFrameContext_bd_fn = Rav1dFrameContext_bd_fn::new::<BitDepth8>();
const BPC16: Rav1dFrameContext_bd_fn = Rav1dFrameContext_bd_fn::new::<BitDepth16>();
match bpc {
BPC::BPC8 => &BPC8,
BPC::BPC16 => &BPC16,
}
}

pub unsafe fn recon_b_intra(
&self,
f: &Rav1dFrameData,
Expand Down Expand Up @@ -542,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 @@ -573,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);
kkysen marked this conversation as resolved.
Show resolved Hide resolved
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
Loading