Skip to content

Commit

Permalink
enum Rav1dWarpedMotionType: make a real enum (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Mar 11, 2024
2 parents 929ca31 + d64894b commit 6835c15
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 66 deletions.
42 changes: 25 additions & 17 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,22 @@ pub enum Rav1dRestorationType {
}

pub type Dav1dWarpedMotionType = c_uint;
pub const DAV1D_WM_TYPE_AFFINE: Dav1dWarpedMotionType = 3;
pub const DAV1D_WM_TYPE_ROT_ZOOM: Dav1dWarpedMotionType = 2;
pub const DAV1D_WM_TYPE_TRANSLATION: Dav1dWarpedMotionType = 1;
pub const DAV1D_WM_TYPE_IDENTITY: Dav1dWarpedMotionType = 0;

pub type Rav1dWarpedMotionType = c_uint;
pub const RAV1D_WM_TYPE_AFFINE: Rav1dWarpedMotionType = DAV1D_WM_TYPE_AFFINE;
pub const RAV1D_WM_TYPE_ROT_ZOOM: Rav1dWarpedMotionType = DAV1D_WM_TYPE_ROT_ZOOM;
pub const RAV1D_WM_TYPE_TRANSLATION: Rav1dWarpedMotionType = DAV1D_WM_TYPE_TRANSLATION;
pub const RAV1D_WM_TYPE_IDENTITY: Rav1dWarpedMotionType = DAV1D_WM_TYPE_IDENTITY;
pub const DAV1D_WM_TYPE_IDENTITY: Dav1dWarpedMotionType =
Rav1dWarpedMotionType::Identity as Dav1dWarpedMotionType;
pub const DAV1D_WM_TYPE_TRANSLATION: Dav1dWarpedMotionType =
Rav1dWarpedMotionType::Translation as Dav1dWarpedMotionType;
pub const DAV1D_WM_TYPE_ROT_ZOOM: Dav1dWarpedMotionType =
Rav1dWarpedMotionType::RotZoom as Dav1dWarpedMotionType;
pub const DAV1D_WM_TYPE_AFFINE: Dav1dWarpedMotionType =
Rav1dWarpedMotionType::Affine as Dav1dWarpedMotionType;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromRepr)]
pub enum Rav1dWarpedMotionType {
Identity = 0,
Translation = 1,
RotZoom = 2,
Affine = 3,
}

#[derive(Clone)]
#[repr(C)]
Expand Down Expand Up @@ -298,18 +304,20 @@ impl Rav1dWarpedMotionParams {
}
}

impl From<Dav1dWarpedMotionParams> for Rav1dWarpedMotionParams {
fn from(value: Dav1dWarpedMotionParams) -> Self {
impl TryFrom<Dav1dWarpedMotionParams> for Rav1dWarpedMotionParams {
type Error = ();

fn try_from(value: Dav1dWarpedMotionParams) -> Result<Self, Self::Error> {
let Dav1dWarpedMotionParams {
r#type,
matrix,
abcd,
} = value;
Self {
r#type,
Ok(Self {
r#type: Rav1dWarpedMotionType::from_repr(r#type as usize).ok_or(())?,
matrix,
abcd: Abcd::new(abcd),
}
})
}
}

Expand All @@ -321,7 +329,7 @@ impl From<Rav1dWarpedMotionParams> for Dav1dWarpedMotionParams {
abcd,
} = value;
Self {
r#type,
r#type: r#type as Dav1dWarpedMotionType,
matrix,
abcd: abcd.get(),
}
Expand Down Expand Up @@ -2418,7 +2426,7 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
},
warp_motion,
reduced_txtp_set,
gmv: gmv.map(|c| c.into()),
gmv: gmv.map(|c| c.try_into().unwrap()),
}
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ 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::Rav1dWarpedMotionType;
use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS;
use crate::include::dav1d::headers::RAV1D_PRIMARY_REF_NONE;
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;
use crate::src::align::Align16;
use crate::src::cdef::rav1d_cdef_dsp_init;
use crate::src::cdf::rav1d_cdf_thread_alloc;
Expand Down Expand Up @@ -650,9 +648,9 @@ unsafe fn derive_warpmv(
wmp.r#type = if !rav1d_find_affine_int(&pts, ret, bw4, bh4, mv, &mut wmp, t.bx, t.by)
&& !rav1d_get_shear_params(&mut wmp)
{
RAV1D_WM_TYPE_AFFINE
Rav1dWarpedMotionType::Affine
} else {
RAV1D_WM_TYPE_IDENTITY
Rav1dWarpedMotionType::Identity
};
wmp
}
Expand Down Expand Up @@ -1526,9 +1524,9 @@ unsafe fn decode_b_inner(
&& b.motion_mode() as MotionMode == MM_WARP
{
if b.matrix()[0] == i16::MIN {
t.warpmv.r#type = RAV1D_WM_TYPE_IDENTITY;
t.warpmv.r#type = Rav1dWarpedMotionType::Identity;
} else {
t.warpmv.r#type = RAV1D_WM_TYPE_AFFINE;
t.warpmv.r#type = Rav1dWarpedMotionType::Affine;
t.warpmv.matrix[2] = b.matrix()[0] as i32 + 0x10000;
t.warpmv.matrix[3] = b.matrix()[1] as i32;
t.warpmv.matrix[4] = b.matrix()[2] as i32;
Expand Down Expand Up @@ -2635,8 +2633,8 @@ unsafe fn decode_b_inner(
fix_mv_precision(frame_hdr, &mut b.mv_mut()[idx]);
}
GLOBALMV => {
has_subpel_filter |=
frame_hdr.gmv[b.r#ref()[idx] as usize].r#type == RAV1D_WM_TYPE_TRANSLATION;
has_subpel_filter |= frame_hdr.gmv[b.r#ref()[idx] as usize].r#type
== Rav1dWarpedMotionType::Translation;
b.mv_mut()[idx] = get_gmv_2d(
&frame_hdr.gmv[b.r#ref()[idx] as usize],
t.bx,
Expand Down Expand Up @@ -2851,7 +2849,8 @@ unsafe fn decode_b_inner(
frame_hdr,
);
has_subpel_filter = cmp::min(bw4, bh4) == 1
|| frame_hdr.gmv[b.r#ref()[0] as usize].r#type == RAV1D_WM_TYPE_TRANSLATION;
|| frame_hdr.gmv[b.r#ref()[0] as usize].r#type
== Rav1dWarpedMotionType::Translation;
} else {
has_subpel_filter = true;
if rav1d_msac_decode_bool_adapt(
Expand Down Expand Up @@ -3006,7 +3005,7 @@ unsafe fn decode_b_inner(
// is not warped global motion
&& !(!frame_hdr.force_integer_mv
&& b.inter_mode() == GLOBALMV
&& frame_hdr.gmv[b.r#ref()[0] as usize].r#type > RAV1D_WM_TYPE_TRANSLATION)
&& frame_hdr.gmv[b.r#ref()[0] as usize].r#type > Rav1dWarpedMotionType::Translation)
// has overlappable neighbours
&& (have_left && findoddzero(&t.l.intra.0[by4 as usize..][..h4 as usize])
|| have_top && findoddzero(&(*t.a).intra.0[bx4 as usize..][..w4 as usize]))
Expand Down Expand Up @@ -3063,7 +3062,7 @@ unsafe fn decode_b_inner(
);
}
if t.frame_thread.pass != 0 {
if t.warpmv.r#type == RAV1D_WM_TYPE_AFFINE {
if t.warpmv.r#type == Rav1dWarpedMotionType::Affine {
b.matrix_mut()[0] = (t.warpmv.matrix[2] - 0x10000) as i16;
b.matrix_mut()[1] = t.warpmv.matrix[3] as i16;
b.matrix_mut()[2] = t.warpmv.matrix[4] as i16;
Expand Down Expand Up @@ -3275,7 +3274,7 @@ unsafe fn decode_b_inner(
if cmp::min(bw4, bh4) > 1
&& (b.inter_mode() == GLOBALMV && f.gmv_warp_allowed[b.r#ref()[0] as usize] != 0
|| b.motion_mode() == MM_WARP as u8
&& t.warpmv.r#type > RAV1D_WM_TYPE_TRANSLATION)
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
{
affine_lowest_px_luma(
t,
Expand Down Expand Up @@ -3373,7 +3372,7 @@ unsafe fn decode_b_inner(
&& (b.inter_mode() == GLOBALMV
&& f.gmv_warp_allowed[b.r#ref()[0] as usize] != 0
|| b.motion_mode() == MM_WARP as u8
&& t.warpmv.r#type > RAV1D_WM_TYPE_TRANSLATION)
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
{
affine_lowest_px_chroma(
t,
Expand Down Expand Up @@ -5044,7 +5043,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
f.svc[i][1].scale = 0;
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
f.gmv_warp_allowed[i] = (frame_hdr.gmv[i].r#type > Rav1dWarpedMotionType::Translation
&& !frame_hdr.force_integer_mv
&& !rav1d_get_shear_params(&frame_hdr.gmv[i])
&& f.svc[i][0].scale == 0) as u8;
Expand Down
9 changes: 5 additions & 4 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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::Rav1dWarpedMotionType;
use crate::src::align::Align8;
use crate::src::levels::mv;
use crate::src::levels::BlockLevel;
Expand Down Expand Up @@ -662,11 +663,11 @@ pub(crate) fn get_gmv_2d(
hdr: &Rav1dFrameHeader,
) -> mv {
match gmv.r#type {
2 => {
Rav1dWarpedMotionType::RotZoom => {
assert!(gmv.matrix[5] == gmv.matrix[2]);
assert!(gmv.matrix[4] == -gmv.matrix[3]);
}
1 => {
Rav1dWarpedMotionType::Translation => {
let mut res = mv {
y: (gmv.matrix[0] >> 13) as i16,
x: (gmv.matrix[1] >> 13) as i16,
Expand All @@ -676,10 +677,10 @@ pub(crate) fn get_gmv_2d(
}
return res;
}
0 => {
Rav1dWarpedMotionType::Identity => {
return mv::ZERO;
}
3 | _ => {}
Rav1dWarpedMotionType::Affine => {}
}
let x = bx4 * 4 + bw4 * 2 - 1;
let y = by4 * 4 + bh4 * 2 - 1;
Expand Down
19 changes: 8 additions & 11 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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::Rav1dWarpedMotionType;
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 All @@ -54,10 +55,6 @@ use crate::include::dav1d::headers::RAV1D_PRIMARY_REF_NONE;
use crate::include::dav1d::headers::RAV1D_REFS_PER_FRAME;
use crate::include::dav1d::headers::RAV1D_TRC_SRGB;
use crate::include::dav1d::headers::RAV1D_TRC_UNKNOWN;
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_ROT_ZOOM;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
use crate::src::c_arc::CArc;
use crate::src::cdf::rav1d_cdf_thread_ref;
use crate::src::cdf::rav1d_cdf_thread_unref;
Expand Down Expand Up @@ -1482,15 +1479,15 @@ unsafe fn parse_gmv(
if frame_type.is_inter_or_switch() {
for (i, gmv) in gmv.iter_mut().enumerate() {
gmv.r#type = if !gb.get_bit() {
RAV1D_WM_TYPE_IDENTITY
Rav1dWarpedMotionType::Identity
} else if gb.get_bit() {
RAV1D_WM_TYPE_ROT_ZOOM
Rav1dWarpedMotionType::RotZoom
} else if gb.get_bit() {
RAV1D_WM_TYPE_TRANSLATION
Rav1dWarpedMotionType::Translation
} else {
RAV1D_WM_TYPE_AFFINE
Rav1dWarpedMotionType::Affine
};
if gmv.r#type == RAV1D_WM_TYPE_IDENTITY {
if gmv.r#type == Rav1dWarpedMotionType::Identity {
continue;
}

Expand All @@ -1512,7 +1509,7 @@ unsafe fn parse_gmv(
let bits;
let shift;

if gmv.r#type >= RAV1D_WM_TYPE_ROT_ZOOM {
if gmv.r#type >= Rav1dWarpedMotionType::RotZoom {
mat[2] = (1 << 16) + 2 * gb.get_bits_subexp(ref_mat[2] - (1 << 16) >> 1, 12);
mat[3] = 2 * gb.get_bits_subexp(ref_mat[3] >> 1, 12);

Expand All @@ -1523,7 +1520,7 @@ unsafe fn parse_gmv(
shift = 13 + !hp as c_int;
}

if gmv.r#type as c_uint == RAV1D_WM_TYPE_AFFINE as c_int as c_uint {
if gmv.r#type == Rav1dWarpedMotionType::Affine {
mat[4] = 2 * gb.get_bits_subexp(ref_mat[4] >> 1, 12);
mat[5] = (1 << 16) + 2 * gb.get_bits_subexp(ref_mat[5] - (1 << 16) >> 1, 12);
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/recon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::include::common::intops::iclip;
use crate::include::dav1d::dav1d::Rav1dInloopFilterType;
use crate::include::dav1d::headers::Rav1dPixelLayout;
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
use crate::include::dav1d::picture::RAV1D_PICTURE_ALIGNMENT;
use crate::src::cdef_apply::rav1d_cdef_brow;
use crate::src::ctx::CaseSet;
Expand Down Expand Up @@ -3377,7 +3377,7 @@ pub(crate) unsafe fn rav1d_recon_b_inter<BD: BitDepth>(
&& f.gmv_warp_allowed[b.c2rust_unnamed.c2rust_unnamed_0.r#ref[0] as usize] as c_int
!= 0
|| b.c2rust_unnamed.c2rust_unnamed_0.motion_mode as c_int == MM_WARP as c_int
&& t.warpmv.r#type as c_uint > RAV1D_WM_TYPE_TRANSLATION as c_int as c_uint)
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
{
res = warp_affine::<BD>(
f,
Expand Down Expand Up @@ -3762,8 +3762,7 @@ pub(crate) unsafe fn rav1d_recon_b_inter<BD: BitDepth>(
!= 0
|| b.c2rust_unnamed.c2rust_unnamed_0.motion_mode as c_int
== MM_WARP as c_int
&& t.warpmv.r#type as c_uint
> RAV1D_WM_TYPE_TRANSLATION as c_int as c_uint)
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
{
let mut pl = 0;
while pl < 2 {
Expand Down
29 changes: 16 additions & 13 deletions src/refmvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::include::common::intops::apply_sign;
use crate::include::common::intops::iclip;
use crate::include::dav1d::headers::Rav1dFrameHeader;
use crate::include::dav1d::headers::Rav1dSequenceHeader;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
use crate::src::env::fix_mv_precision;
use crate::src::env::get_gmv_2d;
use crate::src::env::get_poc_diff;
Expand Down Expand Up @@ -710,12 +710,14 @@ pub(crate) unsafe fn rav1d_refmvs_find(
bh4,
&*rf.frm_hdr,
);
gmv[0] =
if (*rf.frm_hdr).gmv[r#ref.r#ref[0] as usize - 1].r#type > RAV1D_WM_TYPE_TRANSLATION {
tgmv[0]
} else {
mv::INVALID
};

gmv[0] = if (*rf.frm_hdr).gmv[r#ref.r#ref[0] as usize - 1].r#type
> Rav1dWarpedMotionType::Translation
{
tgmv[0]
} else {
mv::INVALID
};
} else {
tgmv[0] = mv::ZERO;
gmv[0] = mv::INVALID;
Expand All @@ -729,12 +731,13 @@ pub(crate) unsafe fn rav1d_refmvs_find(
bh4,
&*rf.frm_hdr,
);
gmv[1] =
if (*rf.frm_hdr).gmv[r#ref.r#ref[1] as usize - 1].r#type > RAV1D_WM_TYPE_TRANSLATION {
tgmv[1]
} else {
mv::INVALID
};
gmv[1] = if (*rf.frm_hdr).gmv[r#ref.r#ref[1] as usize - 1].r#type
> Rav1dWarpedMotionType::Translation
{
tgmv[1]
} else {
mv::INVALID
};
}

// top
Expand Down
4 changes: 2 additions & 2 deletions src/tables.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::Rav1dWarpedMotionParams;
use crate::include::dav1d::headers::RAV1D_WM_TYPE_IDENTITY;
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
use crate::src::align::Align16;
use crate::src::align::Align4;
use crate::src::align::Align64;
Expand Down Expand Up @@ -721,7 +721,7 @@ pub const interintra_allowed_mask: c_uint = 0
impl Default for Rav1dWarpedMotionParams {
fn default() -> Self {
Self {
r#type: RAV1D_WM_TYPE_IDENTITY,
r#type: Rav1dWarpedMotionType::Identity,
matrix: [0, 0, 1 << 16, 0, 0, 1 << 16],
abcd: Default::default(),
}
Expand Down

0 comments on commit 6835c15

Please sign in to comment.