From 6655344742b675c98abbc793bbc9df35ef2a62a3 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sun, 10 Dec 2023 20:08:48 -0800 Subject: [PATCH 1/2] `enum Rav1dFrameType`: Make a real `enum` and make `fn is_{inter_or_switch,key_or_intra}` methods instead. Notably, this allows these methods to be used before a `Rav1dFrameHeader` is constructed. --- include/common/frame.rs | 14 ---------- include/dav1d/headers.rs | 55 +++++++++++++++++++++++++++++-------- lib.rs | 1 - src/decode.rs | 43 ++++++++++++++--------------- src/obu.rs | 58 +++++++++++++++++++--------------------- 5 files changed, 94 insertions(+), 77 deletions(-) delete mode 100644 include/common/frame.rs diff --git a/include/common/frame.rs b/include/common/frame.rs deleted file mode 100644 index ebf88ab81..000000000 --- a/include/common/frame.rs +++ /dev/null @@ -1,14 +0,0 @@ -use crate::include::dav1d::headers::Rav1dFrameHeader; - -/// Checks whether the frame type is `INTER` or `SWITCH`. -pub(crate) fn is_inter_or_switch(frame_header: &Rav1dFrameHeader) -> bool { - // Both are defined as odd numbers {1, 3} and therefore have the LSB set. - // See also: AV1 spec 6.8.2 - frame_header.frame_type & 1 != 0 -} - -/// Checks whether Dav1dFrameType == KEY || == INTRA -/// See also: AV1 spec 6.8.2 -pub(crate) fn is_key_or_intra(frame_header: &Rav1dFrameHeader) -> bool { - !is_inter_or_switch(frame_header) -} diff --git a/include/dav1d/headers.rs b/include/dav1d/headers.rs index 1029f6970..b9b881ea5 100644 --- a/include/dav1d/headers.rs +++ b/include/dav1d/headers.rs @@ -3,6 +3,7 @@ use std::ffi::c_int; use std::ffi::c_uint; use std::ops::BitAnd; use strum::EnumCount; +use strum::FromRepr; /// This is so we can store both `*mut D` and `*mut R` /// for maintaining `dav1d` ABI compatibility, @@ -319,17 +320,49 @@ impl From for Rav1dPixelLayout { } } +#[derive(Clone, Copy, PartialEq, Eq, FromRepr)] +pub(crate) enum Rav1dFrameType { + Key = 0, + Inter = 1, + Intra = 2, + Switch = 3, +} + +impl Rav1dFrameType { + pub const fn into_rav1d(self) -> Dav1dFrameType { + self as Dav1dFrameType + } +} + pub type Dav1dFrameType = c_uint; -pub const DAV1D_FRAME_TYPE_SWITCH: Dav1dFrameType = 3; -pub const DAV1D_FRAME_TYPE_INTRA: Dav1dFrameType = 2; -pub const DAV1D_FRAME_TYPE_INTER: Dav1dFrameType = 1; -pub const DAV1D_FRAME_TYPE_KEY: Dav1dFrameType = 0; +pub const DAV1D_FRAME_TYPE_KEY: Dav1dFrameType = Rav1dFrameType::Key.into_rav1d(); +pub const DAV1D_FRAME_TYPE_INTER: Dav1dFrameType = Rav1dFrameType::Inter.into_rav1d(); +pub const DAV1D_FRAME_TYPE_INTRA: Dav1dFrameType = Rav1dFrameType::Intra.into_rav1d(); +pub const DAV1D_FRAME_TYPE_SWITCH: Dav1dFrameType = Rav1dFrameType::Switch.into_rav1d(); + +impl From for Dav1dFrameType { + fn from(value: Rav1dFrameType) -> Self { + value.into_rav1d() + } +} + +impl TryFrom for Rav1dFrameType { + type Error = (); -pub(crate) type Rav1dFrameType = c_uint; -pub(crate) const RAV1D_FRAME_TYPE_SWITCH: Rav1dFrameType = DAV1D_FRAME_TYPE_SWITCH; -pub(crate) const RAV1D_FRAME_TYPE_INTRA: Rav1dFrameType = DAV1D_FRAME_TYPE_INTRA; -pub(crate) const RAV1D_FRAME_TYPE_INTER: Rav1dFrameType = DAV1D_FRAME_TYPE_INTER; -pub(crate) const RAV1D_FRAME_TYPE_KEY: Rav1dFrameType = DAV1D_FRAME_TYPE_KEY; + fn try_from(value: Dav1dFrameType) -> Result { + Self::from_repr(value as usize).ok_or(()) + } +} + +impl Rav1dFrameType { + pub const fn is_inter_or_switch(&self) -> bool { + matches!(self, Self::Inter | Self::Switch) + } + + pub const fn is_key_or_intra(&self) -> bool { + matches!(self, Self::Key | Self::Intra) + } +} pub type Dav1dColorPrimaries = c_uint; pub const DAV1D_COLOR_PRI_RESERVED: Dav1dColorPrimaries = 255; @@ -2247,7 +2280,7 @@ impl From for Rav1dFrameHeader { have_render_size, }, film_grain: film_grain.into(), - frame_type, + frame_type: frame_type.try_into().unwrap(), frame_offset, temporal_id, spatial_id, @@ -2353,7 +2386,7 @@ impl From for Dav1dFrameHeader { } = value; Self { film_grain: film_grain.into(), - frame_type, + frame_type: frame_type.into(), width, height, frame_offset, diff --git a/lib.rs b/lib.rs index 76ad01e89..968a0d636 100644 --- a/lib.rs +++ b/lib.rs @@ -14,7 +14,6 @@ pub mod include { pub(crate) mod attributes; pub(crate) mod bitdepth; pub(crate) mod dump; - pub mod frame; pub(crate) mod intops; pub(crate) mod validate; } // mod common diff --git a/src/decode.rs b/src/decode.rs index cb8104df4..0c54ba9f1 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,8 +1,6 @@ use crate::include::common::attributes::ctz; use crate::include::common::bitdepth::DynCoef; use crate::include::common::bitdepth::DynPixel; -use crate::include::common::frame::is_inter_or_switch; -use crate::include::common::frame::is_key_or_intra; use crate::include::common::intops::apply_sign64; use crate::include::common::intops::iclip; use crate::include::common::intops::iclip_u8; @@ -1513,7 +1511,7 @@ unsafe fn decode_b( case.set(&mut dir.intra.0, 1); }, ); - if is_inter_or_switch(frame_hdr) { + if frame_hdr.frame_type.is_inter_or_switch() { let r = t.rt.r[((t.by & 31) + 5 + bh4 - 1) as usize].offset(t.bx as isize); for x in 0..bw4 { let block = &mut *r.offset(x as isize); @@ -1539,7 +1537,7 @@ unsafe fn decode_b( ); } } else { - if is_inter_or_switch(frame_hdr) /* not intrabc */ + if frame_hdr.frame_type.is_inter_or_switch() /* not intrabc */ && b.comp_type() == COMP_INTER_NONE && b.motion_mode() as MotionMode == MM_WARP { @@ -1587,7 +1585,7 @@ unsafe fn decode_b( }, ); - if is_inter_or_switch(frame_hdr) { + if frame_hdr.frame_type.is_inter_or_switch() { let r = t.rt.r[((t.by & 31) + 5 + bh4 - 1) as usize].offset(t.bx as isize); let r = std::slice::from_raw_parts_mut(r, bw4 as usize); for r in r { @@ -1902,7 +1900,7 @@ unsafe fn decode_b( if b.skip_mode != 0 { b.intra = 0; - } else if is_inter_or_switch(frame_hdr) { + } else if frame_hdr.frame_type.is_inter_or_switch() { if let Some(seg) = seg.filter(|seg| seg.r#ref >= 0 || seg.globalmv != 0) { b.intra = (seg.r#ref == 0) as u8; } else { @@ -1925,7 +1923,7 @@ unsafe fn decode_b( // intra/inter-specific stuff if b.intra != 0 { - let ymode_cdf = if frame_hdr.frame_type & 1 != 0 { + let ymode_cdf = if frame_hdr.frame_type.is_inter_or_switch() { &mut ts.cdf.m.y_mode[dav1d_ymode_size_context[bs as usize] as usize] } else { &mut ts.cdf.kfym @@ -2220,7 +2218,7 @@ unsafe fn decode_b( &mut t.pal_sz_uv[dir_index], if has_chroma { b.pal_sz()[1] } else { 0 }, ); - if is_inter_or_switch(frame_hdr) { + if frame_hdr.frame_type.is_inter_or_switch() { case.set(&mut dir.comp_type.0, COMP_INTER_NONE); case.set(&mut dir.r#ref[0], -1); case.set(&mut dir.r#ref[1], -1); @@ -2272,10 +2270,10 @@ unsafe fn decode_b( } } } - if is_inter_or_switch(frame_hdr) || frame_hdr.allow_intrabc != 0 { + if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc != 0 { splat_intraref(&*f.c, t, bs, bw4 as usize, bh4 as usize); } - } else if is_key_or_intra(frame_hdr) { + } else if frame_hdr.frame_type.is_key_or_intra() { // intra block copy let mut mvstack = [Default::default(); 8]; let mut n_mvs = 0; @@ -3262,7 +3260,7 @@ unsafe fn decode_b( } } - if t.frame_thread.pass == 1 && b.intra == 0 && frame_hdr.frame_type & 1 != 0 { + if t.frame_thread.pass == 1 && b.intra == 0 && frame_hdr.frame_type.is_inter_or_switch() { let sby = t.by - ts.tiling.row_start >> f.sb_shift; let lowest_px = &mut *ts.lowest_pixel.offset(sby as isize); // keep track of motion vectors for each reference @@ -4045,7 +4043,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(t: &mut Rav1dTaskContext) -> Result let col_sb_start = (*f.frame_hdr).tiling.col_start_sb[tile_col as usize] as c_int; let col_sb128_start = col_sb_start >> ((*f.seq_hdr).sb128 == 0) as c_int; - if is_inter_or_switch(&*f.frame_hdr) || (*f.frame_hdr).allow_intrabc != 0 { + if (*f.frame_hdr).frame_type.is_inter_or_switch() || (*f.frame_hdr).allow_intrabc != 0 { rav1d_refmvs_tile_sbrow_init( &mut t.rt, &f.rf, @@ -4059,14 +4057,14 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(t: &mut Rav1dTaskContext) -> Result ); } - if is_inter_or_switch(&*f.frame_hdr) && c.n_fc > 1 { + if (*f.frame_hdr).frame_type.is_inter_or_switch() && c.n_fc > 1 { let sby = t.by - ts.tiling.row_start >> f.sb_shift; *ts.lowest_pixel.offset(sby as isize) = [[i32::MIN; 2]; 7]; } reset_context( &mut t.l, - is_key_or_intra(&*f.frame_hdr), + (*f.frame_hdr).frame_type.is_key_or_intra(), t.frame_thread.pass, ); if t.frame_thread.pass == 2 { @@ -4199,7 +4197,10 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(t: &mut Rav1dTaskContext) -> Result } } - if (*f.seq_hdr).ref_frame_mvs != 0 && (*f.c).n_tc > 1 && (*f.frame_hdr).frame_type & 1 != 0 { + if (*f.seq_hdr).ref_frame_mvs != 0 + && (*f.c).n_tc > 1 + && (*f.frame_hdr).frame_type.is_inter_or_switch() + { rav1d_refmvs_save_tmvs( &(*f.c).refmvs_dsp, &mut t.rt, @@ -4602,7 +4603,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(f: &mut Rav1dFrameContext) -> Rav1d } // init ref mvs - if is_inter_or_switch(&*f.frame_hdr) || (*f.frame_hdr).allow_intrabc != 0 { + if (*f.frame_hdr).frame_type.is_inter_or_switch() || (*f.frame_hdr).allow_intrabc != 0 { let ret = rav1d_refmvs_init_frame( &mut f.rf, f.seq_hdr, @@ -4768,7 +4769,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init_cdf(f: &mut Rav1dFrameContext) -> R { reset_context( ctx, - is_key_or_intra(&*f.frame_hdr), + (*f.frame_hdr).frame_type.is_key_or_intra(), if uses_2pass { 1 + (n >= sb128w * rows) as c_int } else { @@ -4794,7 +4795,7 @@ unsafe fn rav1d_decode_frame_main(f: &mut Rav1dFrameContext) -> Rav1dResult { f.a, (f.sb128w * (*f.frame_hdr).tiling.rows).try_into().unwrap(), ) { - reset_context(ctx, is_key_or_intra(&*f.frame_hdr), 0); + reset_context(ctx, (*f.frame_hdr).frame_type.is_key_or_intra(), 0); } // no threading - we explicitly interleave tile/sbrow decoding @@ -4829,7 +4830,7 @@ unsafe fn rav1d_decode_frame_main(f: &mut Rav1dFrameContext) -> Rav1dResult { t.ts = tile; rav1d_decode_tile_sbrow(t).map_err(|()| EINVAL)?; } - if is_inter_or_switch(&*f.frame_hdr) { + if (*f.frame_hdr).frame_type.is_inter_or_switch() { rav1d_refmvs_save_tmvs( &(*f.c).refmvs_dsp, &mut t.rt, @@ -5097,7 +5098,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult { } let mut ref_coded_width = <[i32; 7]>::default(); - if is_inter_or_switch(&*f.frame_hdr) { + if (*f.frame_hdr).frame_type.is_inter_or_switch() { if (*f.frame_hdr).primary_ref_frame != RAV1D_PRIMARY_REF_NONE { let pri_ref = (*f.frame_hdr).refidx[(*f.frame_hdr).primary_ref_frame as usize] as usize; if c.refs[pri_ref].p.p.data[0].is_null() { @@ -5218,7 +5219,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult { ); // ref_mvs - if is_inter_or_switch(&*f.frame_hdr) || (*f.frame_hdr).allow_intrabc != 0 { + if (*f.frame_hdr).frame_type.is_inter_or_switch() || (*f.frame_hdr).allow_intrabc != 0 { f.mvs_ref = rav1d_ref_create_using_pool( c.refmvs_pool, ::core::mem::size_of::() diff --git a/src/obu.rs b/src/obu.rs index 7e4b5c84d..1da36648a 100644 --- a/src/obu.rs +++ b/src/obu.rs @@ -1,5 +1,3 @@ -use crate::include::common::frame::is_inter_or_switch; -use crate::include::common::frame::is_key_or_intra; use crate::include::common::intops::iclip_u8; use crate::include::common::intops::ulog2; use crate::include::dav1d::data::Rav1dData; @@ -37,10 +35,6 @@ 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; use crate::include::dav1d::headers::RAV1D_FILTER_SWITCHABLE; -use crate::include::dav1d::headers::RAV1D_FRAME_TYPE_INTER; -use crate::include::dav1d::headers::RAV1D_FRAME_TYPE_INTRA; -use crate::include::dav1d::headers::RAV1D_FRAME_TYPE_KEY; -use crate::include::dav1d::headers::RAV1D_FRAME_TYPE_SWITCH; use crate::include::dav1d::headers::RAV1D_MAX_OPERATING_POINTS; use crate::include::dav1d::headers::RAV1D_MAX_SEGMENTS; use crate::include::dav1d::headers::RAV1D_MAX_TILE_COLS; @@ -748,9 +742,9 @@ unsafe fn parse_frame_hdr( } hdr.frame_type = if seqhdr.reduced_still_picture_header != 0 { - RAV1D_FRAME_TYPE_KEY + Rav1dFrameType::Key } else { - rav1d_get_bits(gb, 2) as Rav1dFrameType + Rav1dFrameType::from_repr(rav1d_get_bits(gb, 2) as usize).unwrap() }; hdr.show_frame = (seqhdr.reduced_still_picture_header != 0 || rav1d_get_bit(gb) != 0) as c_int; if hdr.show_frame != 0 { @@ -758,12 +752,12 @@ unsafe fn parse_frame_hdr( hdr.frame_presentation_delay = rav1d_get_bits(gb, seqhdr.frame_presentation_delay_length) as c_int; } - hdr.showable_frame = (hdr.frame_type != RAV1D_FRAME_TYPE_KEY) as c_int; + hdr.showable_frame = (hdr.frame_type != Rav1dFrameType::Key) as c_int; } else { hdr.showable_frame = rav1d_get_bit(gb) as c_int; } - hdr.error_resilient_mode = (hdr.frame_type == RAV1D_FRAME_TYPE_KEY && hdr.show_frame != 0 - || hdr.frame_type == RAV1D_FRAME_TYPE_SWITCH + hdr.error_resilient_mode = (hdr.frame_type == Rav1dFrameType::Key && hdr.show_frame != 0 + || hdr.frame_type == Rav1dFrameType::Switch || seqhdr.reduced_still_picture_header != 0 || rav1d_get_bit(gb) != 0) as c_int; if DEBUG_FRAME_HDR { @@ -788,7 +782,7 @@ unsafe fn parse_frame_hdr( hdr.force_integer_mv = 0; } - if is_key_or_intra(&hdr) { + if hdr.frame_type.is_key_or_intra() { hdr.force_integer_mv = 1; } @@ -798,7 +792,7 @@ unsafe fn parse_frame_hdr( hdr.frame_size_override = (if seqhdr.reduced_still_picture_header != 0 { 0 - } else if hdr.frame_type == RAV1D_FRAME_TYPE_SWITCH { + } else if hdr.frame_type == Rav1dFrameType::Switch { 1 } else { rav1d_get_bit(gb) @@ -814,7 +808,8 @@ unsafe fn parse_frame_hdr( } else { 0 }; - hdr.primary_ref_frame = if hdr.error_resilient_mode == 0 && is_inter_or_switch(&hdr) { + hdr.primary_ref_frame = if hdr.error_resilient_mode == 0 && hdr.frame_type.is_inter_or_switch() + { rav1d_get_bits(gb, 3) as c_int } else { RAV1D_PRIMARY_REF_NONE @@ -838,8 +833,8 @@ unsafe fn parse_frame_hdr( } } - if is_key_or_intra(&hdr) { - hdr.refresh_frame_flags = if hdr.frame_type == RAV1D_FRAME_TYPE_KEY && hdr.show_frame != 0 { + if hdr.frame_type.is_key_or_intra() { + hdr.refresh_frame_flags = if hdr.frame_type == Rav1dFrameType::Key && hdr.show_frame != 0 { 0xff } else { rav1d_get_bits(gb, 8) as c_int @@ -853,7 +848,7 @@ unsafe fn parse_frame_hdr( } } if c.strict_std_compliance - && hdr.frame_type == RAV1D_FRAME_TYPE_INTRA + && hdr.frame_type == Rav1dFrameType::Intra && hdr.refresh_frame_flags == 0xff { return Err(EINVAL); @@ -865,7 +860,7 @@ unsafe fn parse_frame_hdr( hdr.use_ref_frame_mvs = 0; } else { hdr.allow_intrabc = 0; - hdr.refresh_frame_flags = if hdr.frame_type == RAV1D_FRAME_TYPE_SWITCH { + hdr.refresh_frame_flags = if hdr.frame_type == Rav1dFrameType::Switch { 0xff } else { rav1d_get_bits(gb, 8) as c_int @@ -1011,7 +1006,7 @@ unsafe fn parse_frame_hdr( hdr.use_ref_frame_mvs = (hdr.error_resilient_mode == 0 && seqhdr.ref_frame_mvs != 0 && seqhdr.order_hint != 0 - && is_inter_or_switch(&hdr) + && hdr.frame_type.is_inter_or_switch() && rav1d_get_bit(gb) != 0) as c_int; } if DEBUG_FRAME_HDR { @@ -1486,7 +1481,7 @@ unsafe fn parse_frame_hdr( gb.ptr.offset_from(init_ptr) * 8 - gb.bits_left as isize ); } - hdr.switchable_comp_refs = if is_inter_or_switch(&hdr) { + hdr.switchable_comp_refs = if hdr.frame_type.is_inter_or_switch() { rav1d_get_bit(gb) as c_int } else { 0 @@ -1498,7 +1493,10 @@ unsafe fn parse_frame_hdr( ); } hdr.skip_mode_allowed = 0; - if hdr.switchable_comp_refs != 0 && is_inter_or_switch(&hdr) && seqhdr.order_hint != 0 { + if hdr.switchable_comp_refs != 0 + && hdr.frame_type.is_inter_or_switch() + && seqhdr.order_hint != 0 + { let poc = hdr.frame_offset as c_uint; let mut off_before = 0xffffffff; let mut off_after = -1; @@ -1588,7 +1586,7 @@ unsafe fn parse_frame_hdr( ); } hdr.warp_motion = (hdr.error_resilient_mode == 0 - && is_inter_or_switch(&hdr) + && hdr.frame_type.is_inter_or_switch() && seqhdr.warped_motion != 0 && rav1d_get_bit(gb) != 0) as c_int; if DEBUG_FRAME_HDR { @@ -1609,7 +1607,7 @@ unsafe fn parse_frame_hdr( hdr.gmv[i as usize] = dav1d_default_wm_params.clone(); } - if is_inter_or_switch(&hdr) { + if hdr.frame_type.is_inter_or_switch() { for i in 0..7 { hdr.gmv[i as usize].r#type = if rav1d_get_bit(gb) == 0 { RAV1D_WM_TYPE_IDENTITY @@ -1677,7 +1675,7 @@ unsafe fn parse_frame_hdr( if hdr.film_grain.present != 0 { let seed = rav1d_get_bits(gb, 16); hdr.film_grain.update = - (hdr.frame_type != RAV1D_FRAME_TYPE_INTER || rav1d_get_bit(gb) != 0) as c_int; + (hdr.frame_type != Rav1dFrameType::Inter || rav1d_get_bit(gb) != 0) as c_int; if hdr.film_grain.update == 0 { let refidx = rav1d_get_bits(gb, 3) as c_int; let mut found = false; @@ -2343,14 +2341,14 @@ unsafe fn parse_obus( .p .p .frame_hdr) - .frame_type as c_uint + .frame_type { - RAV1D_FRAME_TYPE_INTER | RAV1D_FRAME_TYPE_SWITCH => { + Rav1dFrameType::Inter | Rav1dFrameType::Switch => { if c.decode_frame_type > RAV1D_DECODEFRAMETYPE_REFERENCE { return Ok(skip(c, len, init_byte_pos)); } } - RAV1D_FRAME_TYPE_INTRA => { + Rav1dFrameType::Intra => { if c.decode_frame_type > RAV1D_DECODEFRAMETYPE_INTRA { return Ok(skip(c, len, init_byte_pos)); } @@ -2468,7 +2466,7 @@ unsafe fn parse_obus( .p .frame_hdr) .frame_type - == RAV1D_FRAME_TYPE_KEY + == Rav1dFrameType::Key { let r = (*c.frame_hdr).existing_frame_idx; c.refs[r as usize].p.showable = false; @@ -2496,7 +2494,7 @@ unsafe fn parse_obus( c.frame_hdr = 0 as *mut Rav1dFrameHeader; } else if c.n_tiles == (*c.frame_hdr).tiling.cols * (*c.frame_hdr).tiling.rows { match (*c.frame_hdr).frame_type { - RAV1D_FRAME_TYPE_INTER | RAV1D_FRAME_TYPE_SWITCH => { + Rav1dFrameType::Inter | Rav1dFrameType::Switch => { if c.decode_frame_type > RAV1D_DECODEFRAMETYPE_REFERENCE || c.decode_frame_type == RAV1D_DECODEFRAMETYPE_REFERENCE && (*c.frame_hdr).refresh_frame_flags == 0 @@ -2504,7 +2502,7 @@ unsafe fn parse_obus( return Ok(skip(c, len, init_byte_pos)); } } - RAV1D_FRAME_TYPE_INTRA => { + Rav1dFrameType::Intra => { if c.decode_frame_type > RAV1D_DECODEFRAMETYPE_INTRA || c.decode_frame_type == RAV1D_DECODEFRAMETYPE_REFERENCE && (*c.frame_hdr).refresh_frame_flags == 0 From fd0bddf60a9b62525f052177db91c099122d8e3d Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sun, 10 Dec 2023 20:15:18 -0800 Subject: [PATCH 2/2] `enum Rav1dPixelLayout`: `#[derive(FromRepr)]` and define the `Dav1d*` versions in terms of the `Rav1d*` versions like for `enum Rav1dFrameType`. --- include/dav1d/headers.rs | 67 ++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/include/dav1d/headers.rs b/include/dav1d/headers.rs index b9b881ea5..9d0ee1c70 100644 --- a/include/dav1d/headers.rs +++ b/include/dav1d/headers.rs @@ -223,33 +223,29 @@ impl From for Dav1dWarpedMotionParams { } } -pub type Dav1dPixelLayout = c_uint; -pub const DAV1D_PIXEL_LAYOUT_I444: Dav1dPixelLayout = 3; -pub const DAV1D_PIXEL_LAYOUT_I422: Dav1dPixelLayout = 2; -pub const DAV1D_PIXEL_LAYOUT_I420: Dav1dPixelLayout = 1; -pub const DAV1D_PIXEL_LAYOUT_I400: Dav1dPixelLayout = 0; - -#[derive(Clone, Copy, PartialEq, Eq, EnumCount)] +#[derive(Clone, Copy, PartialEq, Eq, EnumCount, FromRepr)] pub(crate) enum Rav1dPixelLayout { - I400, - I420, - I422, - I444, + I400 = 0, + I420 = 1, + I422 = 2, + I444 = 3, } -impl EnumKey<{ Self::COUNT }> for Rav1dPixelLayout { - const VALUES: [Self; Self::COUNT] = [Self::I400, Self::I420, Self::I422, Self::I444]; - - fn as_usize(self) -> usize { - self as usize +impl Rav1dPixelLayout { + pub const fn into_rav1d(self) -> Dav1dPixelLayout { + self as Dav1dPixelLayout } } -impl BitAnd for Rav1dPixelLayout { - type Output = bool; +pub type Dav1dPixelLayout = c_uint; +pub const DAV1D_PIXEL_LAYOUT_I400: Dav1dPixelLayout = Rav1dPixelLayout::I400.into_rav1d(); +pub const DAV1D_PIXEL_LAYOUT_I420: Dav1dPixelLayout = Rav1dPixelLayout::I420.into_rav1d(); +pub const DAV1D_PIXEL_LAYOUT_I422: Dav1dPixelLayout = Rav1dPixelLayout::I422.into_rav1d(); +pub const DAV1D_PIXEL_LAYOUT_I444: Dav1dPixelLayout = Rav1dPixelLayout::I444.into_rav1d(); - fn bitand(self, rhs: Self) -> Self::Output { - (self as usize & rhs as usize) != 0 +impl From for Dav1dPixelLayout { + fn from(value: Rav1dPixelLayout) -> Self { + value.into_rav1d() } } @@ -257,26 +253,23 @@ impl TryFrom for Rav1dPixelLayout { type Error = (); fn try_from(value: Dav1dPixelLayout) -> Result { - use Rav1dPixelLayout::*; - Ok(match value { - DAV1D_PIXEL_LAYOUT_I400 => I400, - DAV1D_PIXEL_LAYOUT_I420 => I420, - DAV1D_PIXEL_LAYOUT_I422 => I422, - DAV1D_PIXEL_LAYOUT_I444 => I444, - _ => return Err(()), - }) + Self::from_repr(value as usize).ok_or(()) } } -impl From for Dav1dPixelLayout { - fn from(value: Rav1dPixelLayout) -> Self { - use Rav1dPixelLayout::*; - match value { - I400 => DAV1D_PIXEL_LAYOUT_I400, - I420 => DAV1D_PIXEL_LAYOUT_I420, - I422 => DAV1D_PIXEL_LAYOUT_I422, - I444 => DAV1D_PIXEL_LAYOUT_I444, - } +impl EnumKey<{ Self::COUNT }> for Rav1dPixelLayout { + const VALUES: [Self; Self::COUNT] = [Self::I400, Self::I420, Self::I422, Self::I444]; + + fn as_usize(self) -> usize { + self as usize + } +} + +impl BitAnd for Rav1dPixelLayout { + type Output = bool; + + fn bitand(self, rhs: Self) -> Self::Output { + (self as usize & rhs as usize) != 0 } }