Skip to content

Commit

Permalink
enum Rav1dAdaptiveBool: make a real enum (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Mar 7, 2024
2 parents f6d4770 + 6fa550a commit a78273c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 66 deletions.
66 changes: 46 additions & 20 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,40 @@ impl TryFrom<Dav1dFilterMode> for Rav1dFilterMode {
}

pub type Dav1dAdaptiveBoolean = c_uint;
pub const DAV1D_ADAPTIVE: Dav1dAdaptiveBoolean = 2;
pub const DAV1D_ON: Dav1dAdaptiveBoolean = 1;
pub const DAV1D_OFF: Dav1dAdaptiveBoolean = 0;
pub const DAV1D_OFF: Dav1dAdaptiveBoolean = Rav1dAdaptiveBoolean::Off as Dav1dAdaptiveBoolean;
pub const DAV1D_ON: Dav1dAdaptiveBoolean = Rav1dAdaptiveBoolean::On as Dav1dAdaptiveBoolean;
pub const DAV1D_ADAPTIVE: Dav1dAdaptiveBoolean =
Rav1dAdaptiveBoolean::Adaptive as Dav1dAdaptiveBoolean;

pub(crate) type Rav1dAdaptiveBoolean = c_uint;
pub(crate) const RAV1D_ADAPTIVE: Rav1dAdaptiveBoolean = DAV1D_ADAPTIVE;
pub(crate) const _RAV1D_ON: Rav1dAdaptiveBoolean = DAV1D_ON;
pub(crate) const _RAV1D_OFF: Rav1dAdaptiveBoolean = DAV1D_OFF;
#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub enum Rav1dAdaptiveBoolean {
Off = 0,
On = 1,
Adaptive = 2,
}

impl From<bool> for Rav1dAdaptiveBoolean {
fn from(value: bool) -> Self {
match value {
true => Self::On,
false => Self::Off,
}
}
}

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

impl TryFrom<Dav1dAdaptiveBoolean> for Rav1dAdaptiveBoolean {
type Error = ();

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

pub type Dav1dRestorationType = u8;
pub const DAV1D_RESTORATION_SGRPROJ: Dav1dRestorationType = 3;
Expand Down Expand Up @@ -1046,8 +1072,8 @@ impl From<Dav1dSequenceHeader> for Rav1dSequenceHeader {
order_hint,
jnt_comp,
ref_frame_mvs,
screen_content_tools,
force_integer_mv,
screen_content_tools: screen_content_tools.try_into().unwrap(),
force_integer_mv: force_integer_mv.try_into().unwrap(),
order_hint_n_bits,
super_res,
cdef,
Expand Down Expand Up @@ -1161,8 +1187,8 @@ impl From<Rav1dSequenceHeader> for Dav1dSequenceHeader {
order_hint,
jnt_comp,
ref_frame_mvs,
screen_content_tools,
force_integer_mv,
screen_content_tools: screen_content_tools.into(),
force_integer_mv: force_integer_mv.into(),
order_hint_n_bits,
super_res,
cdef,
Expand Down Expand Up @@ -2240,8 +2266,8 @@ pub struct Rav1dFrameHeader {
pub showable_frame: c_int,
pub error_resilient_mode: c_int,
pub disable_cdf_update: c_int,
pub allow_screen_content_tools: c_int,
pub force_integer_mv: c_int,
pub allow_screen_content_tools: bool,
pub force_integer_mv: bool,
pub frame_size_override: c_int,
pub primary_ref_frame: c_int,
pub buffer_removal_time_present: c_int,
Expand All @@ -2250,7 +2276,7 @@ pub struct Rav1dFrameHeader {
pub allow_intrabc: c_int,
pub frame_ref_short_signaling: c_int,
pub refidx: [c_int; RAV1D_REFS_PER_FRAME],
pub hp: c_int,
pub hp: bool,
pub subpel_filter_mode: Rav1dFilterMode,
pub switchable_motion_mode: c_int,
pub use_ref_frame_mvs: c_int,
Expand Down Expand Up @@ -2347,8 +2373,8 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
showable_frame,
error_resilient_mode,
disable_cdf_update,
allow_screen_content_tools,
force_integer_mv,
allow_screen_content_tools: allow_screen_content_tools != 0,
force_integer_mv: force_integer_mv != 0,
frame_size_override,
primary_ref_frame,
buffer_removal_time_present,
Expand All @@ -2357,7 +2383,7 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
allow_intrabc,
frame_ref_short_signaling,
refidx,
hp,
hp: hp != 0,
subpel_filter_mode: subpel_filter_mode.try_into().unwrap(),
switchable_motion_mode,
use_ref_frame_mvs,
Expand Down Expand Up @@ -2460,8 +2486,8 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
showable_frame,
error_resilient_mode,
disable_cdf_update,
allow_screen_content_tools,
force_integer_mv,
allow_screen_content_tools: allow_screen_content_tools.into(),
force_integer_mv: force_integer_mv.into(),
frame_size_override,
primary_ref_frame,
buffer_removal_time_present,
Expand All @@ -2474,7 +2500,7 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
allow_intrabc,
frame_ref_short_signaling,
refidx,
hp,
hp: hp.into(),
subpel_filter_mode: subpel_filter_mode.into(),
switchable_motion_mode,
use_ref_frame_mvs,
Expand Down
16 changes: 8 additions & 8 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ unsafe fn read_mv_component_diff(
have_fp: bool,
) -> c_int {
let ts = &mut *t.ts;
let have_hp = f.frame_hdr.as_ref().unwrap().hp != 0;
let have_hp = f.frame_hdr.as_ref().unwrap().hp;
let sign = rav1d_msac_decode_bool_adapt(&mut ts.msac, &mut mv_comp.sign.0);
let cl = rav1d_msac_decode_symbol_adapt16(&mut ts.msac, &mut mv_comp.classes.0, 10);
let mut up;
Expand Down Expand Up @@ -2015,7 +2015,7 @@ unsafe fn decode_b_inner(
}

*b.pal_sz_mut() = [0, 0];
if frame_hdr.allow_screen_content_tools != 0 && cmp::max(bw4, bh4) <= 16 && bw4 + bh4 >= 4 {
if frame_hdr.allow_screen_content_tools && cmp::max(bw4, bh4) <= 16 && bw4 + bh4 >= 4 {
let sz_ctx = b_dim[2] + b_dim[3] - 2;
if b.y_mode() == DC_PRED {
let pal_ctx = ((*t.a).pal_sz.0[bx4 as usize] > 0) as usize
Expand Down Expand Up @@ -2657,7 +2657,7 @@ unsafe fn decode_b_inner(
f,
&mut b.mv_mut()[idx],
&mut ts.cdf.mv,
frame_hdr.force_integer_mv == 0,
!frame_hdr.force_integer_mv,
);
}
_ => {}
Expand Down Expand Up @@ -2948,7 +2948,7 @@ unsafe fn decode_b_inner(
f,
&mut *b.mv_mut().as_mut_ptr().offset(0),
&mut ts.cdf.mv,
frame_hdr.force_integer_mv == 0,
!frame_hdr.force_integer_mv,
);
if debug_block_info!(f, t) {
println!(
Expand Down Expand Up @@ -3008,7 +3008,7 @@ unsafe fn decode_b_inner(
&& b.interintra_type() == INTER_INTRA_NONE
&& cmp::min(bw4, bh4) >= 2
// is not warped global motion
&& !(frame_hdr.force_integer_mv == 0
&& !(!frame_hdr.force_integer_mv
&& b.inter_mode() == GLOBALMV
&& frame_hdr.gmv[b.r#ref()[0] as usize].r#type > RAV1D_WM_TYPE_TRANSLATION)
// has overlappable neighbours
Expand All @@ -3031,7 +3031,7 @@ unsafe fn decode_b_inner(
&mut mask,
);
let allow_warp = (f.svc[b.r#ref()[0] as usize][0].scale == 0
&& frame_hdr.force_integer_mv == 0
&& !frame_hdr.force_integer_mv
&& frame_hdr.warp_motion != 0
&& mask[0] | mask[1] != 0) as c_int;

Expand Down Expand Up @@ -4363,7 +4363,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(
// TODO: Fallible allocation
f.frame_thread.cf.resize(cf_sz as usize * 128 * 128 / 2, 0);

if frame_hdr.allow_screen_content_tools != 0 {
if frame_hdr.allow_screen_content_tools {
// TODO: Fallible allocation
f.frame_thread
.pal
Expand Down Expand Up @@ -5049,7 +5049,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
f.svc[i][0].scale = f.svc[i][1].scale;
}
f.gmv_warp_allowed[i] = (frame_hdr.gmv[i].r#type > RAV1D_WM_TYPE_TRANSLATION
&& frame_hdr.force_integer_mv == 0
&& !frame_hdr.force_integer_mv
&& !rav1d_get_shear_params(&frame_hdr.gmv[i])
&& f.svc[i][0].scale == 0) as u8;
}
Expand Down
14 changes: 7 additions & 7 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,9 @@ fn fix_int_mv_precision(mv: &mut mv) {

#[inline]
pub(crate) fn fix_mv_precision(hdr: &Rav1dFrameHeader, mv: &mut mv) {
if hdr.force_integer_mv != 0 {
if hdr.force_integer_mv {
fix_int_mv_precision(mv);
} else if (*hdr).hp == 0 {
} else if !(*hdr).hp {
mv.x = (mv.x - (mv.x >> 15)) & !1;
mv.y = (mv.y - (mv.y >> 15)) & !1;
}
Expand All @@ -671,7 +671,7 @@ pub(crate) fn get_gmv_2d(
y: (gmv.matrix[0] >> 13) as i16,
x: (gmv.matrix[1] >> 13) as i16,
};
if hdr.force_integer_mv != 0 {
if hdr.force_integer_mv {
fix_int_mv_precision(&mut res);
}
return res;
Expand All @@ -685,13 +685,13 @@ pub(crate) fn get_gmv_2d(
let y = by4 * 4 + bh4 * 2 - 1;
let xc = (gmv.matrix[2] - (1 << 16)) * x + gmv.matrix[3] * y + gmv.matrix[0];
let yc = (gmv.matrix[5] - (1 << 16)) * y + gmv.matrix[4] * x + gmv.matrix[1];
let shift = 16 - (3 - (hdr.hp == 0) as c_int);
let shift = 16 - (3 - !hdr.hp as c_int);
let round = 1 << shift >> 1;
let mut res = mv {
y: apply_sign(yc.abs() + round >> shift << (hdr.hp == 0) as c_int, yc) as i16,
x: apply_sign(xc.abs() + round >> shift << (hdr.hp == 0) as c_int, xc) as i16,
y: apply_sign(yc.abs() + round >> shift << !hdr.hp as c_int, yc) as i16,
x: apply_sign(xc.abs() + round >> shift << !hdr.hp as c_int, xc) as i16,
};
if hdr.force_integer_mv != 0 {
if hdr.force_integer_mv {
fix_int_mv_precision(&mut res);
}
return res;
Expand Down
59 changes: 28 additions & 31 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use crate::include::dav1d::headers::Rav1dSequenceHeaderOperatingPoint;
use crate::include::dav1d::headers::Rav1dTransferCharacteristics;
use crate::include::dav1d::headers::Rav1dTxfmMode;
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_ADAPTIVE;
use crate::include::dav1d::headers::RAV1D_CHR_UNKNOWN;
use crate::include::dav1d::headers::RAV1D_COLOR_PRI_BT709;
use crate::include::dav1d::headers::RAV1D_COLOR_PRI_UNKNOWN;
Expand Down Expand Up @@ -326,8 +325,8 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
let ref_frame_mvs;
let order_hint_n_bits;
if reduced_still_picture_header != 0 {
screen_content_tools = RAV1D_ADAPTIVE;
force_integer_mv = RAV1D_ADAPTIVE;
screen_content_tools = Rav1dAdaptiveBoolean::Adaptive;
force_integer_mv = Rav1dAdaptiveBoolean::Adaptive;

// Default initialization.
inter_intra = Default::default();
Expand All @@ -353,19 +352,19 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
ref_frame_mvs = Default::default();
}
screen_content_tools = if gb.get_bit() {
RAV1D_ADAPTIVE
Rav1dAdaptiveBoolean::Adaptive
} else {
gb.get_bit() as Rav1dAdaptiveBoolean
gb.get_bit().into()
};
debug.post(gb, "screentools");
force_integer_mv = if screen_content_tools as c_uint != 0 {
force_integer_mv = if screen_content_tools != Rav1dAdaptiveBoolean::Off {
if gb.get_bit() {
RAV1D_ADAPTIVE
Rav1dAdaptiveBoolean::Adaptive
} else {
gb.get_bit() as Rav1dAdaptiveBoolean
gb.get_bit().into()
}
} else {
2
Rav1dAdaptiveBoolean::Adaptive
};
if order_hint != 0 {
order_hint_n_bits = gb.get_bits(3) as c_int + 1;
Expand Down Expand Up @@ -1464,7 +1463,7 @@ unsafe fn parse_gmv(
frame_type: Rav1dFrameType,
primary_ref_frame: c_int,
refidx: &[c_int; RAV1D_REFS_PER_FRAME],
hp: c_int,
hp: bool,
debug: &Debug,
gb: &mut GetBits,
) -> Rav1dResult<[Rav1dWarpedMotionParams; RAV1D_REFS_PER_FRAME]> {
Expand Down Expand Up @@ -1510,8 +1509,8 @@ unsafe fn parse_gmv(
bits = 12;
shift = 10;
} else {
bits = 9 - (hp == 0) as c_int;
shift = 13 + (hp == 0) as c_int;
bits = 9 - !hp as c_int;
shift = 13 + !hp as c_int;
}

if gmv.r#type as c_uint == RAV1D_WM_TYPE_AFFINE as c_int as c_uint {
Expand Down Expand Up @@ -1772,24 +1771,23 @@ unsafe fn parse_frame_hdr(
|| gb.get_bit()) as c_int;
debug.post(gb, "frametype_bits");
let disable_cdf_update = gb.get_bit() as c_int;
let allow_screen_content_tools = (if seqhdr.screen_content_tools == RAV1D_ADAPTIVE {
gb.get_bit()
} else {
seqhdr.screen_content_tools != 0
}) as c_int;
let mut force_integer_mv;
if allow_screen_content_tools != 0 {
force_integer_mv = (if seqhdr.force_integer_mv == RAV1D_ADAPTIVE {
gb.get_bit()
} else {
seqhdr.force_integer_mv != 0
}) as c_int;
let allow_screen_content_tools = match seqhdr.screen_content_tools {
Rav1dAdaptiveBoolean::Adaptive => gb.get_bit(),
Rav1dAdaptiveBoolean::On => true,
Rav1dAdaptiveBoolean::Off => false,
};
let mut force_integer_mv = if allow_screen_content_tools {
match seqhdr.force_integer_mv {
Rav1dAdaptiveBoolean::Adaptive => gb.get_bit(),
Rav1dAdaptiveBoolean::On => true,
Rav1dAdaptiveBoolean::Off => false,
}
} else {
force_integer_mv = 0;
}
false
};

if frame_type.is_key_or_intra() {
force_integer_mv = 1;
force_integer_mv = true;
}

let frame_id;
Expand Down Expand Up @@ -1870,9 +1868,8 @@ unsafe fn parse_frame_hdr(
return Err(EINVAL);
}
size = parse_frame_size(c, seqhdr, None, frame_size_override, gb)?;
allow_intrabc = (allow_screen_content_tools != 0
&& size.super_res.enabled == 0
&& gb.get_bit()) as c_int;
allow_intrabc =
(allow_screen_content_tools && size.super_res.enabled == 0 && gb.get_bit()) as c_int;
use_ref_frame_mvs = 0;

// Default initialization.
Expand Down Expand Up @@ -1910,7 +1907,7 @@ unsafe fn parse_frame_hdr(
frame_size_override,
gb,
)?;
hp = (force_integer_mv == 0 && gb.get_bit()) as c_int;
hp = !force_integer_mv && gb.get_bit();
subpel_filter_mode = if gb.get_bit() {
Rav1dFilterMode::Switchable
} else {
Expand Down

0 comments on commit a78273c

Please sign in to comment.