Skip to content

Commit

Permalink
enum Rav1dRestorationType: make a real enum (#788)
Browse files Browse the repository at this point in the history
and turn some fields into `bool`.
  • Loading branch information
kkysen authored Mar 11, 2024
2 parents a78273c + 1fd3ad7 commit 929ca31
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 102 deletions.
61 changes: 37 additions & 24 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,23 @@ impl TryFrom<Dav1dAdaptiveBoolean> for Rav1dAdaptiveBoolean {
}

pub type Dav1dRestorationType = u8;
pub const DAV1D_RESTORATION_SGRPROJ: Dav1dRestorationType = 3;
pub const DAV1D_RESTORATION_WIENER: Dav1dRestorationType = 2;
pub const DAV1D_RESTORATION_SWITCHABLE: Dav1dRestorationType = 1;
pub const DAV1D_RESTORATION_NONE: Dav1dRestorationType = 0;

pub type Rav1dRestorationType = u8;
pub const RAV1D_RESTORATION_SGRPROJ: Rav1dRestorationType = DAV1D_RESTORATION_SGRPROJ;
pub const RAV1D_RESTORATION_WIENER: Rav1dRestorationType = DAV1D_RESTORATION_WIENER;
pub const RAV1D_RESTORATION_SWITCHABLE: Rav1dRestorationType = DAV1D_RESTORATION_SWITCHABLE;
pub const RAV1D_RESTORATION_NONE: Rav1dRestorationType = DAV1D_RESTORATION_NONE;
pub const DAV1D_RESTORATION_NONE: Dav1dRestorationType =
Rav1dRestorationType::None as Dav1dRestorationType;
pub const DAV1D_RESTORATION_SWITCHABLE: Dav1dRestorationType =
Rav1dRestorationType::Switchable as Dav1dRestorationType;
pub const DAV1D_RESTORATION_WIENER: Dav1dRestorationType =
Rav1dRestorationType::Wiener as Dav1dRestorationType;
pub const DAV1D_RESTORATION_SGRPROJ: Dav1dRestorationType =
Rav1dRestorationType::SgrProj as Dav1dRestorationType;

#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr, Default)]
pub enum Rav1dRestorationType {
#[default]
None = 0,
Switchable = 1,
Wiener = 2,
SgrProj = 3,
}

pub type Dav1dWarpedMotionType = c_uint;
pub const DAV1D_WM_TYPE_AFFINE: Dav1dWarpedMotionType = 3;
Expand Down Expand Up @@ -1624,7 +1631,7 @@ pub struct Dav1dFrameHeader_super_res {
#[repr(C)]
pub struct Rav1dFrameHeader_super_res {
pub width_scale_denominator: c_int,
pub enabled: c_int,
pub enabled: bool,
}

impl From<Dav1dFrameHeader_super_res> for Rav1dFrameHeader_super_res {
Expand All @@ -1635,7 +1642,7 @@ impl From<Dav1dFrameHeader_super_res> for Rav1dFrameHeader_super_res {
} = value;
Self {
width_scale_denominator,
enabled,
enabled: enabled != 0,
}
}
}
Expand All @@ -1648,7 +1655,7 @@ impl From<Rav1dFrameHeader_super_res> for Dav1dFrameHeader_super_res {
} = value;
Self {
width_scale_denominator,
enabled,
enabled: enabled as c_int,
}
}
}
Expand Down Expand Up @@ -2164,14 +2171,20 @@ pub struct Rav1dFrameHeader_restoration {
impl From<Dav1dFrameHeader_restoration> for Rav1dFrameHeader_restoration {
fn from(value: Dav1dFrameHeader_restoration) -> Self {
let Dav1dFrameHeader_restoration { r#type, unit_size } = value;
Self { r#type, unit_size }
Self {
r#type: r#type.map(|e| Rav1dRestorationType::from_repr(e as usize).unwrap()),
unit_size,
}
}
}

impl From<Rav1dFrameHeader_restoration> for Dav1dFrameHeader_restoration {
fn from(value: Rav1dFrameHeader_restoration) -> Self {
let Rav1dFrameHeader_restoration { r#type, unit_size } = value;
Self { r#type, unit_size }
Self {
r#type: r#type.map(|e| e as u8),
unit_size,
}
}
}

Expand Down Expand Up @@ -2268,12 +2281,12 @@ pub struct Rav1dFrameHeader {
pub disable_cdf_update: c_int,
pub allow_screen_content_tools: bool,
pub force_integer_mv: bool,
pub frame_size_override: c_int,
pub frame_size_override: bool,
pub primary_ref_frame: c_int,
pub buffer_removal_time_present: c_int,
pub operating_points: [Rav1dFrameHeaderOperatingPoint; RAV1D_MAX_OPERATING_POINTS],
pub refresh_frame_flags: c_int,
pub allow_intrabc: c_int,
pub allow_intrabc: bool,
pub frame_ref_short_signaling: c_int,
pub refidx: [c_int; RAV1D_REFS_PER_FRAME],
pub hp: bool,
Expand All @@ -2285,7 +2298,7 @@ pub struct Rav1dFrameHeader {
pub quant: Rav1dFrameHeader_quant,
pub segmentation: Rav1dFrameHeader_segmentation,
pub delta: Rav1dFrameHeader_delta,
pub all_lossless: c_int,
pub all_lossless: bool,
pub loopfilter: Rav1dFrameHeader_loopfilter,
pub cdef: Rav1dFrameHeader_cdef,
pub restoration: Rav1dFrameHeader_restoration,
Expand Down Expand Up @@ -2375,12 +2388,12 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
disable_cdf_update,
allow_screen_content_tools: allow_screen_content_tools != 0,
force_integer_mv: force_integer_mv != 0,
frame_size_override,
frame_size_override: frame_size_override != 0,
primary_ref_frame,
buffer_removal_time_present,
operating_points: operating_points.map(|c| c.into()),
refresh_frame_flags,
allow_intrabc,
allow_intrabc: allow_intrabc != 0,
frame_ref_short_signaling,
refidx,
hp: hp != 0,
Expand All @@ -2392,7 +2405,7 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
quant: quant.into(),
segmentation: segmentation.into(),
delta: delta.into(),
all_lossless,
all_lossless: all_lossless != 0,
loopfilter: loopfilter.into(),
cdef: cdef.into(),
restoration: restoration.into(),
Expand Down Expand Up @@ -2488,7 +2501,7 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
disable_cdf_update,
allow_screen_content_tools: allow_screen_content_tools.into(),
force_integer_mv: force_integer_mv.into(),
frame_size_override,
frame_size_override: frame_size_override.into(),
primary_ref_frame,
buffer_removal_time_present,
operating_points: operating_points.map(|rust| rust.into()),
Expand All @@ -2497,7 +2510,7 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
render_height,
super_res: super_res.into(),
have_render_size,
allow_intrabc,
allow_intrabc: allow_intrabc.into(),
frame_ref_short_signaling,
refidx,
hp: hp.into(),
Expand All @@ -2509,7 +2522,7 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
quant: quant.into(),
segmentation: segmentation.into(),
delta: delta.into(),
all_lossless,
all_lossless: all_lossless.into(),
loopfilter: loopfilter.into(),
cdef: cdef.into(),
restoration: restoration.into(),
Expand Down
36 changes: 16 additions & 20 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ use crate::include::dav1d::headers::Rav1dTxfmMode;
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS;
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;
use crate::include::dav1d::headers::RAV1D_RESTORATION_SWITCHABLE;
use crate::include::dav1d::headers::RAV1D_RESTORATION_WIENER;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_AFFINE;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_IDENTITY;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
Expand Down Expand Up @@ -1909,7 +1905,7 @@ unsafe fn decode_b_inner(
println!("Post-intra[{}]: r={}", b.intra, ts.msac.rng);
}
}
} else if frame_hdr.allow_intrabc != 0 {
} else if frame_hdr.allow_intrabc {
b.intra = (!rav1d_msac_decode_bool_adapt(&mut ts.msac, &mut ts.cdf.m.intrabc.0)) as u8;
if debug_block_info!(f, t) {
println!("Post-intrabcflag[{}]: r={}", b.intra, ts.msac.rng);
Expand Down Expand Up @@ -2268,7 +2264,7 @@ unsafe fn decode_b_inner(
}
}
}
if f.frame_hdr().frame_type.is_inter_or_switch() || f.frame_hdr().allow_intrabc != 0 {
if f.frame_hdr().frame_type.is_inter_or_switch() || f.frame_hdr().allow_intrabc {
splat_intraref(c, t, bs, bw4 as usize, bh4 as usize);
}
} else if f.frame_hdr().frame_type.is_key_or_intra() {
Expand Down Expand Up @@ -3969,22 +3965,22 @@ unsafe fn read_restoration_info(
) {
let lr_ref = ts.lr_ref[p];

if frame_type == RAV1D_RESTORATION_SWITCHABLE {
if frame_type == Rav1dRestorationType::Switchable {
let filter =
rav1d_msac_decode_symbol_adapt4(&mut ts.msac, &mut ts.cdf.m.restore_switchable.0, 2);
lr.r#type = if filter != 0 {
if filter == 2 {
RAV1D_RESTORATION_SGRPROJ
Rav1dRestorationType::SgrProj
} else {
RAV1D_RESTORATION_WIENER
Rav1dRestorationType::Wiener
}
} else {
RAV1D_RESTORATION_NONE
Rav1dRestorationType::None
};
} else {
let r#type = rav1d_msac_decode_bool_adapt(
&mut ts.msac,
if frame_type == RAV1D_RESTORATION_WIENER {
if frame_type == Rav1dRestorationType::Wiener {
&mut ts.cdf.m.restore_wiener.0
} else {
&mut ts.cdf.m.restore_sgrproj.0
Expand All @@ -3993,7 +3989,7 @@ unsafe fn read_restoration_info(
lr.r#type = if r#type {
frame_type
} else {
RAV1D_RESTORATION_NONE
Rav1dRestorationType::None
};
}

Expand All @@ -4002,7 +3998,7 @@ unsafe fn read_restoration_info(
- adjustment as c_int) as i8
}

if lr.r#type == RAV1D_RESTORATION_WIENER {
if lr.r#type == Rav1dRestorationType::Wiener {
lr.filter_v[0] = if p != 0 {
0
} else {
Expand Down Expand Up @@ -4033,7 +4029,7 @@ unsafe fn read_restoration_info(
ts.msac.rng,
);
}
} else if lr.r#type == RAV1D_RESTORATION_SGRPROJ {
} else if lr.r#type == Rav1dRestorationType::SgrProj {
let idx = rav1d_msac_decode_bools(&mut ts.msac, 4) as u8;
let sgr_params = &dav1d_sgr_params[idx.into()];
lr.sgr_idx = idx;
Expand Down Expand Up @@ -4078,7 +4074,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
let col_sb_start = frame_hdr.tiling.col_start_sb[tile_col as usize] as c_int;
let col_sb128_start = col_sb_start >> (seq_hdr.sb128 == 0) as c_int;

if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc != 0 {
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc {
rav1d_refmvs_tile_sbrow_init(
&mut t.rt,
&f.rf,
Expand Down Expand Up @@ -4519,7 +4515,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(
.r#type
.iter()
.enumerate()
.map(|(i, &r#type)| ((r#type != RAV1D_RESTORATION_NONE) as u8) << i)
.map(|(i, &r#type)| ((r#type != Rav1dRestorationType::None) as u8) << i)
.sum::<u8>()
.into();
if frame_hdr.loopfilter.sharpness != f.lf.last_sharpness {
Expand Down Expand Up @@ -4550,7 +4546,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(
f.lf.tx_lpf_right_edge.resize(re_sz as usize, 0);

// init ref mvs
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc != 0 {
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc {
let ret = rav1d_refmvs_init_frame(
&mut f.rf,
seq_hdr,
Expand Down Expand Up @@ -5137,7 +5133,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
.store(cols * rows + f.sbh << uses_2pass, Ordering::SeqCst);

// ref_mvs
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc != 0 {
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc {
f.mvs_ref = rav1d_ref_create_using_pool(
c.refmvs_pool,
::core::mem::size_of::<refmvs_temporal_block>()
Expand All @@ -5150,7 +5146,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
return Err(ENOMEM);
}
f.mvs = (*f.mvs_ref).data.cast::<refmvs_temporal_block>();
if frame_hdr.allow_intrabc == 0 {
if !frame_hdr.allow_intrabc {
for i in 0..7 {
f.refpoc[i] = f.refp[i].p.frame_hdr.as_ref().unwrap().frame_offset as c_uint;
}
Expand Down Expand Up @@ -5264,7 +5260,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
rav1d_ref_inc(f.cur_segmap_ref);
}
rav1d_ref_dec(&mut c.refs[i].refmvs);
if frame_hdr.allow_intrabc == 0 {
if !frame_hdr.allow_intrabc {
c.refs[i].refmvs = f.mvs_ref;
if !f.mvs_ref.is_null() {
rav1d_ref_inc(f.mvs_ref);
Expand Down
2 changes: 1 addition & 1 deletion src/lf_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ unsafe fn backup_lpf<BD: BitDepth>(
bitdepth_max: c_int,
) {
let cdef_backup = (lr_backup == 0) as c_int;
let dst_w = if frame_hdr.size.super_res.enabled != 0 {
let dst_w = if frame_hdr.size.super_res.enabled {
frame_hdr.size.width[1] + ss_hor >> ss_hor
} else {
src_w
Expand Down
12 changes: 5 additions & 7 deletions src/lr_apply.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::include::common::bitdepth::BitDepth;
use crate::include::dav1d::headers::Rav1dPixelLayout;
use crate::include::dav1d::headers::RAV1D_RESTORATION_NONE;
use crate::include::dav1d::headers::RAV1D_RESTORATION_SGRPROJ;
use crate::include::dav1d::headers::RAV1D_RESTORATION_WIENER;
use crate::include::dav1d::headers::Rav1dRestorationType;
use crate::src::align::Align16;
use crate::src::internal::Rav1dContext;
use crate::src::internal::Rav1dDSPContext;
Expand Down Expand Up @@ -62,7 +60,7 @@ unsafe fn lr_stripe<BD: BitDepth>(
let mut params: LooprestorationParams = LooprestorationParams {
filter: [[0; 8]; 2].into(),
};
if lr.r#type as c_int == RAV1D_RESTORATION_WIENER as c_int {
if lr.r#type == Rav1dRestorationType::Wiener {
let filter = &mut params.filter.0;
filter[0][0] = lr.filter_h[0] as i16;
filter[0][1] = lr.filter_h[1] as i16;
Expand All @@ -87,7 +85,7 @@ unsafe fn lr_stripe<BD: BitDepth>(

lr_fn = dsp.lr.wiener[((filter[0][0] | filter[1][0]) == 0) as usize];
} else {
assert_eq!(lr.r#type, RAV1D_RESTORATION_SGRPROJ);
assert_eq!(lr.r#type, Rav1dRestorationType::SgrProj);
let sgr_params = &dav1d_sgr_params[lr.sgr_idx as usize];
params.sgr.s0 = sgr_params[0] as u32;
params.sgr.s1 = sgr_params[1] as u32;
Expand Down Expand Up @@ -196,7 +194,7 @@ unsafe fn lr_sbrow<BD: BitDepth>(
let sb_idx = (aligned_unit_pos >> 7) * f.sr_sb128w;
let unit_idx = (aligned_unit_pos >> 6 & 1) << 1;
lr[0] = f.lf.lr_mask[sb_idx as usize].lr[plane as usize][unit_idx as usize].get();
let mut restore = lr[0].r#type != RAV1D_RESTORATION_NONE;
let mut restore = lr[0].r#type != Rav1dRestorationType::None;
let mut x = 0;
let mut bit = false;
while x + max_unit_size <= w {
Expand All @@ -205,7 +203,7 @@ unsafe fn lr_sbrow<BD: BitDepth>(
lr[!bit as usize] = f.lf.lr_mask[(sb_idx + (next_x >> shift_hor)) as usize].lr
[plane as usize][next_u_idx as usize]
.get();
let restore_next = lr[!bit as usize].r#type != RAV1D_RESTORATION_NONE;
let restore_next = lr[!bit as usize].r#type != Rav1dRestorationType::None;
if restore_next {
backup4xU::<BD>(
&mut pre_lr_border[bit as usize],
Expand Down
Loading

0 comments on commit 929ca31

Please sign in to comment.