Skip to content

Commit

Permalink
enum Rav1dFilterMode: make a real enum (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Mar 6, 2024
2 parents c988a43 + 7db01e0 commit d6e36fa
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 146 deletions.
57 changes: 39 additions & 18 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,43 @@ impl TryFrom<Dav1dTxfmMode> for Rav1dTxfmMode {
}

pub type Dav1dFilterMode = u8;
pub const DAV1D_FILTER_SWITCHABLE: Dav1dFilterMode = 4;
pub const DAV1D_N_FILTERS: usize = 4;
pub const DAV1D_FILTER_BILINEAR: Dav1dFilterMode = 3;
pub const DAV1D_N_SWITCHABLE_FILTERS: usize = 3;
pub const DAV1D_FILTER_8TAP_SHARP: Dav1dFilterMode = 2;
pub const DAV1D_FILTER_8TAP_SMOOTH: Dav1dFilterMode = 1;
pub const DAV1D_FILTER_8TAP_REGULAR: Dav1dFilterMode = 0;

pub type Rav1dFilterMode = u8;
pub const RAV1D_FILTER_SWITCHABLE: Rav1dFilterMode = DAV1D_FILTER_SWITCHABLE;
pub const RAV1D_N_FILTERS: usize = DAV1D_N_FILTERS;
pub const RAV1D_FILTER_BILINEAR: Rav1dFilterMode = DAV1D_FILTER_BILINEAR;
pub const RAV1D_N_SWITCHABLE_FILTERS: usize = DAV1D_N_SWITCHABLE_FILTERS;
pub const RAV1D_FILTER_8TAP_SHARP: Rav1dFilterMode = DAV1D_FILTER_8TAP_SHARP;
pub const RAV1D_FILTER_8TAP_SMOOTH: Rav1dFilterMode = DAV1D_FILTER_8TAP_SMOOTH;
pub const RAV1D_FILTER_8TAP_REGULAR: Rav1dFilterMode = DAV1D_FILTER_8TAP_REGULAR;
pub const DAV1D_N_SWITCHABLE_FILTERS: usize = Rav1dFilterMode::N_SWITCHABLE_FILTERS as usize;
pub const DAV1D_N_FILTERS: usize = Rav1dFilterMode::N_FILTERS as usize;
pub const DAV1D_FILTER_SWITCHABLE: Dav1dFilterMode = Rav1dFilterMode::Switchable as Dav1dFilterMode;
pub const DAV1D_FILTER_BILINEAR: Dav1dFilterMode = Rav1dFilterMode::Bilinear as Dav1dFilterMode;
pub const DAV1D_FILTER_8TAP_SHARP: Dav1dFilterMode = Rav1dFilterMode::Sharp8Tap as Dav1dFilterMode;
pub const DAV1D_FILTER_8TAP_SMOOTH: Dav1dFilterMode =
Rav1dFilterMode::Smooth8Tap as Dav1dFilterMode;
pub const DAV1D_FILTER_8TAP_REGULAR: Dav1dFilterMode =
Rav1dFilterMode::Regular8Tap as Dav1dFilterMode;

#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub enum Rav1dFilterMode {
Regular8Tap = 0,
Smooth8Tap = 1,
Sharp8Tap = 2,
Bilinear = 3,
Switchable = 4,
}

impl Rav1dFilterMode {
pub const N_FILTERS: usize = 4;
pub const N_SWITCHABLE_FILTERS: u8 = 3;
}

impl From<Rav1dFilterMode> for Dav1dFilterMode {
fn from(value: Rav1dFilterMode) -> Self {
value as Dav1dFilterMode
}
}

impl TryFrom<Dav1dFilterMode> for Rav1dFilterMode {
type Error = ();

fn try_from(value: Dav1dFilterMode) -> Result<Self, Self::Error> {
Self::from_repr(value as usize).ok_or(())
}
}

pub type Dav1dAdaptiveBoolean = c_uint;
pub const DAV1D_ADAPTIVE: Dav1dAdaptiveBoolean = 2;
Expand Down Expand Up @@ -2337,7 +2358,7 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
frame_ref_short_signaling,
refidx,
hp,
subpel_filter_mode,
subpel_filter_mode: subpel_filter_mode.try_into().unwrap(),
switchable_motion_mode,
use_ref_frame_mvs,
refresh_context,
Expand Down Expand Up @@ -2454,7 +2475,7 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
frame_ref_short_signaling,
refidx,
hp,
subpel_filter_mode,
subpel_filter_mode: subpel_filter_mode.into(),
switchable_motion_mode,
use_ref_frame_mvs,
refresh_context,
Expand Down
9 changes: 7 additions & 2 deletions src/cdf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::include::dav1d::headers::Rav1dFilterMode;
use crate::include::dav1d::headers::Rav1dFrameHeader;
use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS;
use crate::include::dav1d::headers::RAV1D_N_SWITCHABLE_FILTERS;
use crate::src::align::Align16;
use crate::src::align::Align32;
use crate::src::align::Align4;
Expand Down Expand Up @@ -5019,7 +5019,12 @@ pub(crate) fn rav1d_cdf_thread_update(

update_bit_1d!(3, m.skip_mode);
update_cdf_2d!(4, N_INTRA_PRED_MODES - 1, m.y_mode);
update_cdf_3d!(2, 8, RAV1D_N_SWITCHABLE_FILTERS - 1, m.filter);
update_cdf_3d!(
2,
8,
Rav1dFilterMode::N_SWITCHABLE_FILTERS as usize - 1,
m.filter
);
update_bit_1d!(6, m.newmv_mode);
update_bit_1d!(2, m.globalmv_mode);
update_bit_1d!(6, m.refmv_mode);
Expand Down
24 changes: 11 additions & 13 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ use crate::include::common::intops::iclip;
use crate::include::common::intops::iclip_u8;
use crate::include::common::intops::ulog2;
use crate::include::dav1d::headers::Dav1dFilterMode;
use crate::include::dav1d::headers::Rav1dFilterMode;
use crate::include::dav1d::headers::Rav1dFrameHeader;
use crate::include::dav1d::headers::Rav1dFrameHeader_tiling;
use crate::include::dav1d::headers::Rav1dPixelLayout;
use crate::include::dav1d::headers::Rav1dRestorationType;
use crate::include::dav1d::headers::Rav1dSequenceHeader;
use crate::include::dav1d::headers::Rav1dTxfmMode;
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_FILTER_8TAP_REGULAR;
use crate::include::dav1d::headers::RAV1D_FILTER_SWITCHABLE;
use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS;
use crate::include::dav1d::headers::RAV1D_N_SWITCHABLE_FILTERS;
use crate::include::dav1d::headers::RAV1D_PRIMARY_REF_NONE;
use crate::include::dav1d::headers::RAV1D_RESTORATION_NONE;
use crate::include::dav1d::headers::RAV1D_RESTORATION_SGRPROJ;
Expand Down Expand Up @@ -1569,8 +1567,8 @@ unsafe fn decode_b_inner(
[bh4 as usize, bw4 as usize],
[by4 as usize, bx4 as usize],
|case, dir| {
case.set(&mut dir.filter.0[0], filter[0]);
case.set(&mut dir.filter.0[1], filter[1]);
case.set(&mut dir.filter.0[0], filter[0].into());
case.set(&mut dir.filter.0[1], filter[1].into());
case.set(&mut dir.intra.0, 0);
},
);
Expand Down Expand Up @@ -2222,8 +2220,8 @@ unsafe fn decode_b_inner(
case.set(&mut dir.comp_type.0, COMP_INTER_NONE);
case.set(&mut dir.r#ref[0], -1);
case.set(&mut dir.r#ref[1], -1);
case.set(&mut dir.filter.0[0], RAV1D_N_SWITCHABLE_FILTERS as u8);
case.set(&mut dir.filter.0[1], RAV1D_N_SWITCHABLE_FILTERS as u8);
case.set(&mut dir.filter.0[0], Rav1dFilterMode::N_SWITCHABLE_FILTERS);
case.set(&mut dir.filter.0[1], Rav1dFilterMode::N_SWITCHABLE_FILTERS);
}
},
);
Expand Down Expand Up @@ -3095,14 +3093,14 @@ unsafe fn decode_b_inner(
}

// subpel filter
let filter = if frame_hdr.subpel_filter_mode == RAV1D_FILTER_SWITCHABLE {
let filter = if frame_hdr.subpel_filter_mode == Rav1dFilterMode::Switchable {
if has_subpel_filter {
let comp = b.comp_type() != COMP_INTER_NONE;
let ctx1 = get_filter_ctx(&*t.a, &t.l, comp, false, b.r#ref()[0], by4, bx4);
let filter0 = rav1d_msac_decode_symbol_adapt4(
&mut ts.msac,
&mut ts.cdf.m.filter.0[0][ctx1 as usize],
RAV1D_N_SWITCHABLE_FILTERS as usize - 1,
Rav1dFilterMode::N_SWITCHABLE_FILTERS as usize - 1,
) as Dav1dFilterMode;
if seq_hdr.dual_filter != 0 {
let ctx2 = get_filter_ctx(&*t.a, &t.l, comp, true, b.r#ref()[0], by4, bx4);
Expand All @@ -3115,7 +3113,7 @@ unsafe fn decode_b_inner(
let filter1 = rav1d_msac_decode_symbol_adapt4(
&mut ts.msac,
&mut ts.cdf.m.filter.0[1][ctx2 as usize],
RAV1D_N_SWITCHABLE_FILTERS as usize - 1,
Rav1dFilterMode::N_SWITCHABLE_FILTERS as usize - 1,
) as Dav1dFilterMode;
if debug_block_info!(f, t) {
println!(
Expand All @@ -3134,10 +3132,10 @@ unsafe fn decode_b_inner(
[filter0; 2]
}
} else {
[RAV1D_FILTER_8TAP_REGULAR; 2]
[Rav1dFilterMode::Regular8Tap as u8; 2]
}
} else {
[frame_hdr.subpel_filter_mode; 2]
[frame_hdr.subpel_filter_mode as u8; 2]
};
*b.filter2d_mut() = dav1d_filter_2d[filter[1] as usize][filter[0] as usize];

Expand Down Expand Up @@ -3837,7 +3835,7 @@ fn reset_context(ctx: &mut BlockContext, keyframe: bool, pass: c_int) {
ccoef.fill(0x40);
}
for filter in &mut ctx.filter.0 {
filter.fill(RAV1D_N_SWITCHABLE_FILTERS as u8);
filter.fill(Rav1dFilterMode::N_SWITCHABLE_FILTERS as u8);
}
ctx.seg_pred.0.fill(0);
ctx.pal_sz.0.fill(0);
Expand Down
10 changes: 5 additions & 5 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::include::common::intops::apply_sign;
use crate::include::dav1d::headers::Rav1dFilterMode;
use crate::include::dav1d::headers::Rav1dFrameHeader;
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_N_SWITCHABLE_FILTERS;
use crate::src::align::Align8;
use crate::src::levels::mv;
use crate::src::levels::BlockLevel;
Expand Down Expand Up @@ -167,19 +167,19 @@ pub fn get_filter_ctx(
if al.r#ref[0][b4 as usize] == r#ref || al.r#ref[1][b4 as usize] == r#ref {
al.filter[dir as usize][b4 as usize]
} else {
RAV1D_N_SWITCHABLE_FILTERS as u8
Rav1dFilterMode::N_SWITCHABLE_FILTERS
}
});

(comp as u8) * 4
+ if a_filter == l_filter {
a_filter
} else if a_filter == RAV1D_N_SWITCHABLE_FILTERS as u8 {
} else if a_filter == Rav1dFilterMode::N_SWITCHABLE_FILTERS {
l_filter
} else if l_filter == RAV1D_N_SWITCHABLE_FILTERS as u8 {
} else if l_filter == Rav1dFilterMode::N_SWITCHABLE_FILTERS {
a_filter
} else {
RAV1D_N_SWITCHABLE_FILTERS as u8
Rav1dFilterMode::N_SWITCHABLE_FILTERS
}
}

Expand Down
Loading

0 comments on commit d6e36fa

Please sign in to comment.