Skip to content

Commit 6835c15

Browse files
authored
enum Rav1dWarpedMotionType: make a real enum (#789)
2 parents 929ca31 + d64894b commit 6835c15

File tree

7 files changed

+73
-66
lines changed

7 files changed

+73
-66
lines changed

include/dav1d/headers.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,22 @@ pub enum Rav1dRestorationType {
203203
}
204204

205205
pub type Dav1dWarpedMotionType = c_uint;
206-
pub const DAV1D_WM_TYPE_AFFINE: Dav1dWarpedMotionType = 3;
207-
pub const DAV1D_WM_TYPE_ROT_ZOOM: Dav1dWarpedMotionType = 2;
208-
pub const DAV1D_WM_TYPE_TRANSLATION: Dav1dWarpedMotionType = 1;
209-
pub const DAV1D_WM_TYPE_IDENTITY: Dav1dWarpedMotionType = 0;
210-
211-
pub type Rav1dWarpedMotionType = c_uint;
212-
pub const RAV1D_WM_TYPE_AFFINE: Rav1dWarpedMotionType = DAV1D_WM_TYPE_AFFINE;
213-
pub const RAV1D_WM_TYPE_ROT_ZOOM: Rav1dWarpedMotionType = DAV1D_WM_TYPE_ROT_ZOOM;
214-
pub const RAV1D_WM_TYPE_TRANSLATION: Rav1dWarpedMotionType = DAV1D_WM_TYPE_TRANSLATION;
215-
pub const RAV1D_WM_TYPE_IDENTITY: Rav1dWarpedMotionType = DAV1D_WM_TYPE_IDENTITY;
206+
pub const DAV1D_WM_TYPE_IDENTITY: Dav1dWarpedMotionType =
207+
Rav1dWarpedMotionType::Identity as Dav1dWarpedMotionType;
208+
pub const DAV1D_WM_TYPE_TRANSLATION: Dav1dWarpedMotionType =
209+
Rav1dWarpedMotionType::Translation as Dav1dWarpedMotionType;
210+
pub const DAV1D_WM_TYPE_ROT_ZOOM: Dav1dWarpedMotionType =
211+
Rav1dWarpedMotionType::RotZoom as Dav1dWarpedMotionType;
212+
pub const DAV1D_WM_TYPE_AFFINE: Dav1dWarpedMotionType =
213+
Rav1dWarpedMotionType::Affine as Dav1dWarpedMotionType;
214+
215+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromRepr)]
216+
pub enum Rav1dWarpedMotionType {
217+
Identity = 0,
218+
Translation = 1,
219+
RotZoom = 2,
220+
Affine = 3,
221+
}
216222

217223
#[derive(Clone)]
218224
#[repr(C)]
@@ -298,18 +304,20 @@ impl Rav1dWarpedMotionParams {
298304
}
299305
}
300306

301-
impl From<Dav1dWarpedMotionParams> for Rav1dWarpedMotionParams {
302-
fn from(value: Dav1dWarpedMotionParams) -> Self {
307+
impl TryFrom<Dav1dWarpedMotionParams> for Rav1dWarpedMotionParams {
308+
type Error = ();
309+
310+
fn try_from(value: Dav1dWarpedMotionParams) -> Result<Self, Self::Error> {
303311
let Dav1dWarpedMotionParams {
304312
r#type,
305313
matrix,
306314
abcd,
307315
} = value;
308-
Self {
309-
r#type,
316+
Ok(Self {
317+
r#type: Rav1dWarpedMotionType::from_repr(r#type as usize).ok_or(())?,
310318
matrix,
311319
abcd: Abcd::new(abcd),
312-
}
320+
})
313321
}
314322
}
315323

@@ -321,7 +329,7 @@ impl From<Rav1dWarpedMotionParams> for Dav1dWarpedMotionParams {
321329
abcd,
322330
} = value;
323331
Self {
324-
r#type,
332+
r#type: r#type as Dav1dWarpedMotionType,
325333
matrix,
326334
abcd: abcd.get(),
327335
}
@@ -2418,7 +2426,7 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
24182426
},
24192427
warp_motion,
24202428
reduced_txtp_set,
2421-
gmv: gmv.map(|c| c.into()),
2429+
gmv: gmv.map(|c| c.try_into().unwrap()),
24222430
}
24232431
}
24242432
}

src/decode.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ use crate::include::dav1d::headers::Rav1dRestorationType;
1717
use crate::include::dav1d::headers::Rav1dSequenceHeader;
1818
use crate::include::dav1d::headers::Rav1dTxfmMode;
1919
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
20+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
2021
use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS;
2122
use crate::include::dav1d::headers::RAV1D_PRIMARY_REF_NONE;
22-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_AFFINE;
23-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_IDENTITY;
24-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
2523
use crate::src::align::Align16;
2624
use crate::src::cdef::rav1d_cdef_dsp_init;
2725
use crate::src::cdf::rav1d_cdf_thread_alloc;
@@ -650,9 +648,9 @@ unsafe fn derive_warpmv(
650648
wmp.r#type = if !rav1d_find_affine_int(&pts, ret, bw4, bh4, mv, &mut wmp, t.bx, t.by)
651649
&& !rav1d_get_shear_params(&mut wmp)
652650
{
653-
RAV1D_WM_TYPE_AFFINE
651+
Rav1dWarpedMotionType::Affine
654652
} else {
655-
RAV1D_WM_TYPE_IDENTITY
653+
Rav1dWarpedMotionType::Identity
656654
};
657655
wmp
658656
}
@@ -1526,9 +1524,9 @@ unsafe fn decode_b_inner(
15261524
&& b.motion_mode() as MotionMode == MM_WARP
15271525
{
15281526
if b.matrix()[0] == i16::MIN {
1529-
t.warpmv.r#type = RAV1D_WM_TYPE_IDENTITY;
1527+
t.warpmv.r#type = Rav1dWarpedMotionType::Identity;
15301528
} else {
1531-
t.warpmv.r#type = RAV1D_WM_TYPE_AFFINE;
1529+
t.warpmv.r#type = Rav1dWarpedMotionType::Affine;
15321530
t.warpmv.matrix[2] = b.matrix()[0] as i32 + 0x10000;
15331531
t.warpmv.matrix[3] = b.matrix()[1] as i32;
15341532
t.warpmv.matrix[4] = b.matrix()[2] as i32;
@@ -2635,8 +2633,8 @@ unsafe fn decode_b_inner(
26352633
fix_mv_precision(frame_hdr, &mut b.mv_mut()[idx]);
26362634
}
26372635
GLOBALMV => {
2638-
has_subpel_filter |=
2639-
frame_hdr.gmv[b.r#ref()[idx] as usize].r#type == RAV1D_WM_TYPE_TRANSLATION;
2636+
has_subpel_filter |= frame_hdr.gmv[b.r#ref()[idx] as usize].r#type
2637+
== Rav1dWarpedMotionType::Translation;
26402638
b.mv_mut()[idx] = get_gmv_2d(
26412639
&frame_hdr.gmv[b.r#ref()[idx] as usize],
26422640
t.bx,
@@ -2851,7 +2849,8 @@ unsafe fn decode_b_inner(
28512849
frame_hdr,
28522850
);
28532851
has_subpel_filter = cmp::min(bw4, bh4) == 1
2854-
|| frame_hdr.gmv[b.r#ref()[0] as usize].r#type == RAV1D_WM_TYPE_TRANSLATION;
2852+
|| frame_hdr.gmv[b.r#ref()[0] as usize].r#type
2853+
== Rav1dWarpedMotionType::Translation;
28552854
} else {
28562855
has_subpel_filter = true;
28572856
if rav1d_msac_decode_bool_adapt(
@@ -3006,7 +3005,7 @@ unsafe fn decode_b_inner(
30063005
// is not warped global motion
30073006
&& !(!frame_hdr.force_integer_mv
30083007
&& b.inter_mode() == GLOBALMV
3009-
&& frame_hdr.gmv[b.r#ref()[0] as usize].r#type > RAV1D_WM_TYPE_TRANSLATION)
3008+
&& frame_hdr.gmv[b.r#ref()[0] as usize].r#type > Rav1dWarpedMotionType::Translation)
30103009
// has overlappable neighbours
30113010
&& (have_left && findoddzero(&t.l.intra.0[by4 as usize..][..h4 as usize])
30123011
|| have_top && findoddzero(&(*t.a).intra.0[bx4 as usize..][..w4 as usize]))
@@ -3063,7 +3062,7 @@ unsafe fn decode_b_inner(
30633062
);
30643063
}
30653064
if t.frame_thread.pass != 0 {
3066-
if t.warpmv.r#type == RAV1D_WM_TYPE_AFFINE {
3065+
if t.warpmv.r#type == Rav1dWarpedMotionType::Affine {
30673066
b.matrix_mut()[0] = (t.warpmv.matrix[2] - 0x10000) as i16;
30683067
b.matrix_mut()[1] = t.warpmv.matrix[3] as i16;
30693068
b.matrix_mut()[2] = t.warpmv.matrix[4] as i16;
@@ -3275,7 +3274,7 @@ unsafe fn decode_b_inner(
32753274
if cmp::min(bw4, bh4) > 1
32763275
&& (b.inter_mode() == GLOBALMV && f.gmv_warp_allowed[b.r#ref()[0] as usize] != 0
32773276
|| b.motion_mode() == MM_WARP as u8
3278-
&& t.warpmv.r#type > RAV1D_WM_TYPE_TRANSLATION)
3277+
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
32793278
{
32803279
affine_lowest_px_luma(
32813280
t,
@@ -3373,7 +3372,7 @@ unsafe fn decode_b_inner(
33733372
&& (b.inter_mode() == GLOBALMV
33743373
&& f.gmv_warp_allowed[b.r#ref()[0] as usize] != 0
33753374
|| b.motion_mode() == MM_WARP as u8
3376-
&& t.warpmv.r#type > RAV1D_WM_TYPE_TRANSLATION)
3375+
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
33773376
{
33783377
affine_lowest_px_chroma(
33793378
t,
@@ -5044,7 +5043,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
50445043
f.svc[i][1].scale = 0;
50455044
f.svc[i][0].scale = f.svc[i][1].scale;
50465045
}
5047-
f.gmv_warp_allowed[i] = (frame_hdr.gmv[i].r#type > RAV1D_WM_TYPE_TRANSLATION
5046+
f.gmv_warp_allowed[i] = (frame_hdr.gmv[i].r#type > Rav1dWarpedMotionType::Translation
50485047
&& !frame_hdr.force_integer_mv
50495048
&& !rav1d_get_shear_params(&frame_hdr.gmv[i])
50505049
&& f.svc[i][0].scale == 0) as u8;

src/env.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::include::common::intops::apply_sign;
22
use crate::include::dav1d::headers::Rav1dFilterMode;
33
use crate::include::dav1d::headers::Rav1dFrameHeader;
44
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
5+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
56
use crate::src::align::Align8;
67
use crate::src::levels::mv;
78
use crate::src::levels::BlockLevel;
@@ -662,11 +663,11 @@ pub(crate) fn get_gmv_2d(
662663
hdr: &Rav1dFrameHeader,
663664
) -> mv {
664665
match gmv.r#type {
665-
2 => {
666+
Rav1dWarpedMotionType::RotZoom => {
666667
assert!(gmv.matrix[5] == gmv.matrix[2]);
667668
assert!(gmv.matrix[4] == -gmv.matrix[3]);
668669
}
669-
1 => {
670+
Rav1dWarpedMotionType::Translation => {
670671
let mut res = mv {
671672
y: (gmv.matrix[0] >> 13) as i16,
672673
x: (gmv.matrix[1] >> 13) as i16,
@@ -676,10 +677,10 @@ pub(crate) fn get_gmv_2d(
676677
}
677678
return res;
678679
}
679-
0 => {
680+
Rav1dWarpedMotionType::Identity => {
680681
return mv::ZERO;
681682
}
682-
3 | _ => {}
683+
Rav1dWarpedMotionType::Affine => {}
683684
}
684685
let x = bx4 * 4 + bw4 * 2 - 1;
685686
let y = by4 * 4 + bh4 * 2 - 1;

src/obu.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::include::dav1d::headers::Rav1dSequenceHeaderOperatingPoint;
4141
use crate::include::dav1d::headers::Rav1dTransferCharacteristics;
4242
use crate::include::dav1d::headers::Rav1dTxfmMode;
4343
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
44+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
4445
use crate::include::dav1d::headers::RAV1D_CHR_UNKNOWN;
4546
use crate::include::dav1d::headers::RAV1D_COLOR_PRI_BT709;
4647
use crate::include::dav1d::headers::RAV1D_COLOR_PRI_UNKNOWN;
@@ -54,10 +55,6 @@ use crate::include::dav1d::headers::RAV1D_PRIMARY_REF_NONE;
5455
use crate::include::dav1d::headers::RAV1D_REFS_PER_FRAME;
5556
use crate::include::dav1d::headers::RAV1D_TRC_SRGB;
5657
use crate::include::dav1d::headers::RAV1D_TRC_UNKNOWN;
57-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_AFFINE;
58-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_IDENTITY;
59-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_ROT_ZOOM;
60-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
6158
use crate::src::c_arc::CArc;
6259
use crate::src::cdf::rav1d_cdf_thread_ref;
6360
use crate::src::cdf::rav1d_cdf_thread_unref;
@@ -1482,15 +1479,15 @@ unsafe fn parse_gmv(
14821479
if frame_type.is_inter_or_switch() {
14831480
for (i, gmv) in gmv.iter_mut().enumerate() {
14841481
gmv.r#type = if !gb.get_bit() {
1485-
RAV1D_WM_TYPE_IDENTITY
1482+
Rav1dWarpedMotionType::Identity
14861483
} else if gb.get_bit() {
1487-
RAV1D_WM_TYPE_ROT_ZOOM
1484+
Rav1dWarpedMotionType::RotZoom
14881485
} else if gb.get_bit() {
1489-
RAV1D_WM_TYPE_TRANSLATION
1486+
Rav1dWarpedMotionType::Translation
14901487
} else {
1491-
RAV1D_WM_TYPE_AFFINE
1488+
Rav1dWarpedMotionType::Affine
14921489
};
1493-
if gmv.r#type == RAV1D_WM_TYPE_IDENTITY {
1490+
if gmv.r#type == Rav1dWarpedMotionType::Identity {
14941491
continue;
14951492
}
14961493

@@ -1512,7 +1509,7 @@ unsafe fn parse_gmv(
15121509
let bits;
15131510
let shift;
15141511

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

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

1526-
if gmv.r#type as c_uint == RAV1D_WM_TYPE_AFFINE as c_int as c_uint {
1523+
if gmv.r#type == Rav1dWarpedMotionType::Affine {
15271524
mat[4] = 2 * gb.get_bits_subexp(ref_mat[4] >> 1, 12);
15281525
mat[5] = (1 << 16) + 2 * gb.get_bits_subexp(ref_mat[5] - (1 << 16) >> 1, 12);
15291526
} else {

src/recon.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::include::common::intops::iclip;
1010
use crate::include::dav1d::dav1d::Rav1dInloopFilterType;
1111
use crate::include::dav1d::headers::Rav1dPixelLayout;
1212
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
13-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
13+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
1414
use crate::include::dav1d::picture::RAV1D_PICTURE_ALIGNMENT;
1515
use crate::src::cdef_apply::rav1d_cdef_brow;
1616
use crate::src::ctx::CaseSet;
@@ -3377,7 +3377,7 @@ pub(crate) unsafe fn rav1d_recon_b_inter<BD: BitDepth>(
33773377
&& f.gmv_warp_allowed[b.c2rust_unnamed.c2rust_unnamed_0.r#ref[0] as usize] as c_int
33783378
!= 0
33793379
|| b.c2rust_unnamed.c2rust_unnamed_0.motion_mode as c_int == MM_WARP as c_int
3380-
&& t.warpmv.r#type as c_uint > RAV1D_WM_TYPE_TRANSLATION as c_int as c_uint)
3380+
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
33813381
{
33823382
res = warp_affine::<BD>(
33833383
f,
@@ -3762,8 +3762,7 @@ pub(crate) unsafe fn rav1d_recon_b_inter<BD: BitDepth>(
37623762
!= 0
37633763
|| b.c2rust_unnamed.c2rust_unnamed_0.motion_mode as c_int
37643764
== MM_WARP as c_int
3765-
&& t.warpmv.r#type as c_uint
3766-
> RAV1D_WM_TYPE_TRANSLATION as c_int as c_uint)
3765+
&& t.warpmv.r#type > Rav1dWarpedMotionType::Translation)
37673766
{
37683767
let mut pl = 0;
37693768
while pl < 2 {

src/refmvs.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::include::common::intops::apply_sign;
22
use crate::include::common::intops::iclip;
33
use crate::include::dav1d::headers::Rav1dFrameHeader;
44
use crate::include::dav1d::headers::Rav1dSequenceHeader;
5-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_TRANSLATION;
5+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
66
use crate::src::env::fix_mv_precision;
77
use crate::src::env::get_gmv_2d;
88
use crate::src::env::get_poc_diff;
@@ -710,12 +710,14 @@ pub(crate) unsafe fn rav1d_refmvs_find(
710710
bh4,
711711
&*rf.frm_hdr,
712712
);
713-
gmv[0] =
714-
if (*rf.frm_hdr).gmv[r#ref.r#ref[0] as usize - 1].r#type > RAV1D_WM_TYPE_TRANSLATION {
715-
tgmv[0]
716-
} else {
717-
mv::INVALID
718-
};
713+
714+
gmv[0] = if (*rf.frm_hdr).gmv[r#ref.r#ref[0] as usize - 1].r#type
715+
> Rav1dWarpedMotionType::Translation
716+
{
717+
tgmv[0]
718+
} else {
719+
mv::INVALID
720+
};
719721
} else {
720722
tgmv[0] = mv::ZERO;
721723
gmv[0] = mv::INVALID;
@@ -729,12 +731,13 @@ pub(crate) unsafe fn rav1d_refmvs_find(
729731
bh4,
730732
&*rf.frm_hdr,
731733
);
732-
gmv[1] =
733-
if (*rf.frm_hdr).gmv[r#ref.r#ref[1] as usize - 1].r#type > RAV1D_WM_TYPE_TRANSLATION {
734-
tgmv[1]
735-
} else {
736-
mv::INVALID
737-
};
734+
gmv[1] = if (*rf.frm_hdr).gmv[r#ref.r#ref[1] as usize - 1].r#type
735+
> Rav1dWarpedMotionType::Translation
736+
{
737+
tgmv[1]
738+
} else {
739+
mv::INVALID
740+
};
738741
}
739742

740743
// top

src/tables.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::include::dav1d::headers::Rav1dFilterMode;
22
use crate::include::dav1d::headers::Rav1dWarpedMotionParams;
3-
use crate::include::dav1d::headers::RAV1D_WM_TYPE_IDENTITY;
3+
use crate::include::dav1d::headers::Rav1dWarpedMotionType;
44
use crate::src::align::Align16;
55
use crate::src::align::Align4;
66
use crate::src::align::Align64;
@@ -721,7 +721,7 @@ pub const interintra_allowed_mask: c_uint = 0
721721
impl Default for Rav1dWarpedMotionParams {
722722
fn default() -> Self {
723723
Self {
724-
r#type: RAV1D_WM_TYPE_IDENTITY,
724+
r#type: Rav1dWarpedMotionType::Identity,
725725
matrix: [0, 0, 1 << 16, 0, 0, 1 << 16],
726726
abcd: Default::default(),
727727
}

0 commit comments

Comments
 (0)