Skip to content

Commit

Permalink
enum Rav1dAdaptiveBool: make a real enum
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 6, 2024
1 parent d6e36fa commit 5e16328
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 45 deletions.
48 changes: 37 additions & 11 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
65 changes: 31 additions & 34 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 Expand Up @@ -2014,8 +2011,8 @@ unsafe fn parse_frame_hdr(
showable_frame,
error_resilient_mode,
disable_cdf_update,
allow_screen_content_tools,
force_integer_mv,
allow_screen_content_tools: allow_screen_content_tools as c_int,
force_integer_mv: force_integer_mv as c_int,
frame_size_override,
primary_ref_frame,
buffer_removal_time_present,
Expand All @@ -2024,7 +2021,7 @@ unsafe fn parse_frame_hdr(
allow_intrabc,
frame_ref_short_signaling,
refidx,
hp,
hp: hp as c_int,
subpel_filter_mode,
switchable_motion_mode,
use_ref_frame_mvs,
Expand Down

0 comments on commit 5e16328

Please sign in to comment.