Skip to content

Commit

Permalink
struct {Rav1dEvent,Picture}Flag: Make bitflags! and replace `unsa…
Browse files Browse the repository at this point in the history
…fe fn rav1d_{picture_,}get_event_flags` with `.into()` and `mem::take` (#653)
  • Loading branch information
kkysen authored Dec 21, 2023
2 parents c73587a + bb2e75a commit a088a9e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
43 changes: 37 additions & 6 deletions include/dav1d/dav1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::src::internal::Rav1dContext;
pub use crate::src::log::Dav1dLogger;
use crate::src::log::Rav1dLogger;
use crate::src::r#ref::Rav1dRef;
use bitflags::bitflags;
use std::ffi::c_int;
use std::ffi::c_uint;

Expand Down Expand Up @@ -39,13 +40,43 @@ pub(crate) const RAV1D_DECODEFRAMETYPE_REFERENCE: Rav1dDecodeFrameType =
pub(crate) const RAV1D_DECODEFRAMETYPE_ALL: Rav1dDecodeFrameType = DAV1D_DECODEFRAMETYPE_ALL;

pub type Dav1dEventFlags = c_uint;
pub const DAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO: Dav1dEventFlags = 2;
pub const DAV1D_EVENT_FLAG_NEW_SEQUENCE: Dav1dEventFlags = 1;
pub const DAV1D_EVENT_FLAG_NEW_SEQUENCE: Dav1dEventFlags =
Rav1dEventFlags::NEW_SEQUENCE.bits() as Dav1dEventFlags;
pub const DAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO: Dav1dEventFlags =
Rav1dEventFlags::NEW_OP_PARAMS_INFO.bits() as Dav1dEventFlags;

pub(crate) type Rav1dEventFlags = c_uint;
pub(crate) const RAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO: Rav1dEventFlags =
DAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO;
pub(crate) const RAV1D_EVENT_FLAG_NEW_SEQUENCE: Rav1dEventFlags = DAV1D_EVENT_FLAG_NEW_SEQUENCE;
bitflags! {
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub(crate) struct Rav1dEventFlags: u8 {
/// The last returned picture contains a reference
/// to a new [`Rav1dSequenceHeader`],
/// either because it's the start of a new coded sequence,
/// or the decoder was flushed before it was generated.
///
/// [`Rav1dSequenceHeader`]: crate::include::dav1d::headers::Rav1dSequenceHeader
const NEW_SEQUENCE = 1 << 0;

/// The last returned picture contains a reference to a
/// [`Rav1dSequenceHeader`] with new [`Rav1dSequenceHeaderOperatingParameterInfo`]
/// for the current coded sequence.
///
/// [`Rav1dSequenceHeader`]: crate::include::dav1d::headers::Rav1dSequenceHeader
/// [`Rav1dSequenceHeaderOperatingParameterInfo`]: crate::include::dav1d::headers::Rav1dSequenceHeaderOperatingParameterInfo
const NEW_OP_PARAMS_INFO = 1 << 1;
}
}

impl From<Rav1dEventFlags> for Dav1dEventFlags {
fn from(value: Rav1dEventFlags) -> Self {
value.bits().into()
}
}

impl From<Dav1dEventFlags> for Rav1dEventFlags {
fn from(value: Dav1dEventFlags) -> Self {
Self::from_bits_retain(value as u8)
}
}

#[repr(C)]
pub struct Dav1dSettings {
Expand Down
5 changes: 2 additions & 3 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ use crate::src::msac::rav1d_msac_decode_symbol_adapt8;
use crate::src::msac::rav1d_msac_decode_uniform;
use crate::src::msac::rav1d_msac_init;
use crate::src::picture::rav1d_picture_alloc_copy;
use crate::src::picture::rav1d_picture_get_event_flags;
use crate::src::picture::rav1d_picture_ref;
use crate::src::picture::rav1d_picture_unref_internal;
use crate::src::picture::rav1d_thread_picture_alloc;
Expand Down Expand Up @@ -4985,7 +4984,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
);
if (out_delayed.visible || c.output_invisible_frames) && progress != FRAME_ERROR {
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.event_flags |= rav1d_picture_get_event_flags(out_delayed);
c.event_flags |= out_delayed.flags.into();
}
rav1d_thread_picture_unref(out_delayed);
}
Expand Down Expand Up @@ -5192,7 +5191,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
if c.n_fc == 1 {
if (*f.frame_hdr).show_frame != 0 || c.output_invisible_frames {
rav1d_thread_picture_ref(&mut c.out, &mut f.sr_cur);
c.event_flags |= rav1d_picture_get_event_flags(&mut f.sr_cur);
c.event_flags |= f.sr_cur.flags.into();
}
} else {
rav1d_thread_picture_ref(out_delayed, &mut f.sr_cur);
Expand Down
23 changes: 6 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::include::dav1d::data::Rav1dData;
use crate::include::dav1d::dav1d::Dav1dContext;
use crate::include::dav1d::dav1d::Dav1dEventFlags;
use crate::include::dav1d::dav1d::Dav1dSettings;
use crate::include::dav1d::dav1d::Rav1dEventFlags;
use crate::include::dav1d::dav1d::Rav1dSettings;
use crate::include::dav1d::dav1d::RAV1D_DECODEFRAMETYPE_ALL;
use crate::include::dav1d::dav1d::RAV1D_DECODEFRAMETYPE_KEY;
Expand Down Expand Up @@ -71,15 +70,14 @@ use crate::src::obu::rav1d_parse_obus;
use crate::src::picture::dav1d_default_picture_alloc;
use crate::src::picture::dav1d_default_picture_release;
use crate::src::picture::rav1d_picture_alloc_copy;
use crate::src::picture::rav1d_picture_get_event_flags;
use crate::src::picture::rav1d_picture_move_ref;
use crate::src::picture::rav1d_picture_ref;
use crate::src::picture::rav1d_picture_unref_internal;
use crate::src::picture::rav1d_thread_picture_move_ref;
use crate::src::picture::rav1d_thread_picture_ref;
use crate::src::picture::rav1d_thread_picture_unref;
use crate::src::picture::PictureFlags;
use crate::src::picture::Rav1dThreadPicture;
use crate::src::picture::PICTURE_FLAG_NEW_TEMPORAL_UNIT;
use crate::src::r#ref::rav1d_ref_dec;
use crate::src::r#ref::Rav1dRef;
use crate::src::refmvs::rav1d_refmvs_clear;
Expand Down Expand Up @@ -114,6 +112,7 @@ use std::ffi::c_int;
use std::ffi::c_uint;
use std::ffi::c_ulong;
use std::ffi::c_void;
use std::mem;
use std::mem::MaybeUninit;
use std::process::abort;
use std::ptr::NonNull;
Expand Down Expand Up @@ -625,7 +624,7 @@ unsafe extern "C" fn output_picture_ready(c: *mut Rav1dContext, drain: c_int) ->
if !(*c).all_layers && (*c).max_spatial_id {
if !((*c).out.p.data[0]).is_null() && !((*c).cache.p.data[0]).is_null() {
if (*c).max_spatial_id == ((*(*c).cache.p.frame_hdr).spatial_id != 0)
|| (*c).out.flags as c_uint & PICTURE_FLAG_NEW_TEMPORAL_UNIT as c_int as c_uint != 0
|| (*c).out.flags.contains(PictureFlags::NEW_TEMPORAL_UNIT)
{
return 1 as c_int;
}
Expand Down Expand Up @@ -710,9 +709,7 @@ unsafe fn drain_picture(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRe
);
if ((*out_delayed).visible || c.output_invisible_frames) && progress != FRAME_ERROR {
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.event_flags = ::core::mem::transmute::<c_uint, Dav1dEventFlags>(
c.event_flags as c_uint | rav1d_picture_get_event_flags(out_delayed) as c_uint,
);
c.event_flags |= (*out_delayed).flags.into();
}
rav1d_thread_picture_unref(out_delayed);
if output_picture_ready(c, 0 as c_int) != 0 {
Expand Down Expand Up @@ -1161,15 +1158,6 @@ unsafe fn close_internal(c_out: &mut *mut Rav1dContext, flush: c_int) {
rav1d_freep_aligned(c_out as *mut _ as *mut c_void);
}

pub(crate) unsafe fn rav1d_get_event_flags(
c: &mut Rav1dContext,
flags: &mut Rav1dEventFlags,
) -> Rav1dResult {
*flags = c.event_flags;
c.event_flags = 0 as Dav1dEventFlags;
Ok(())
}

#[no_mangle]
pub unsafe extern "C" fn dav1d_get_event_flags(
c: *mut Dav1dContext,
Expand All @@ -1178,7 +1166,8 @@ pub unsafe extern "C" fn dav1d_get_event_flags(
(|| {
validate_input!((!c.is_null(), EINVAL))?;
validate_input!((!flags.is_null(), EINVAL))?;
rav1d_get_event_flags(&mut *c, &mut *flags)
flags.write(mem::take(&mut (*c).event_flags).into());
Ok(())
})()
.into()
}
Expand Down
22 changes: 10 additions & 12 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ use crate::src::levels::OBU_META_SCALABILITY;
use crate::src::levels::OBU_META_TIMECODE;
use crate::src::log::Rav1dLog as _;
use crate::src::picture::rav1d_picture_copy_props;
use crate::src::picture::rav1d_picture_get_event_flags;
use crate::src::picture::rav1d_thread_picture_ref;
use crate::src::picture::rav1d_thread_picture_unref;
use crate::src::picture::PICTURE_FLAG_NEW_OP_PARAMS_INFO;
use crate::src::picture::PICTURE_FLAG_NEW_SEQUENCE;
use crate::src::picture::PICTURE_FLAG_NEW_TEMPORAL_UNIT;
use crate::src::picture::PictureFlags;
use crate::src::r#ref::rav1d_ref_create;
use crate::src::r#ref::rav1d_ref_create_using_pool;
use crate::src::r#ref::rav1d_ref_dec;
Expand Down Expand Up @@ -2015,7 +2012,7 @@ unsafe fn parse_obus(

if c.seq_hdr.is_null() {
c.frame_hdr = 0 as *mut Rav1dFrameHeader;
c.frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
c.frame_flags |= PictureFlags::NEW_SEQUENCE;
} else if !(*seq_hdr).eq_without_operating_parameter_info(&*c.seq_hdr) {
// See 7.5, `operating_parameter_info` is allowed to change in
// sequence headers of a single sequence.
Expand All @@ -2032,10 +2029,10 @@ unsafe fn parse_obus(
rav1d_ref_dec(&mut c.refs[i as usize].refmvs);
rav1d_cdf_thread_unref(&mut c.cdf[i as usize]);
}
c.frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
c.frame_flags |= PictureFlags::NEW_SEQUENCE;
} else if (*seq_hdr).operating_parameter_info != (*c.seq_hdr).operating_parameter_info {
// If operating_parameter_info changed, signal it
c.frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
c.frame_flags |= PictureFlags::NEW_OP_PARAMS_INFO;
}
rav1d_ref_dec(&mut c.seq_hdr_ref);
c.seq_hdr_ref = r#ref;
Expand Down Expand Up @@ -2315,7 +2312,7 @@ unsafe fn parse_obus(
}
}
}
RAV1D_OBU_TD => c.frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT,
RAV1D_OBU_TD => c.frame_flags |= PictureFlags::NEW_TEMPORAL_UNIT,
RAV1D_OBU_PADDING => {} // Ignore OBUs we don't care about.
_ => {
// Print a warning, but don't fail for unknown types.
Expand Down Expand Up @@ -2383,9 +2380,10 @@ unsafe fn parse_obus(
// Must be removed from the context after being attached to the frame
rav1d_ref_dec(&mut c.itut_t35_ref);
c.itut_t35 = 0 as *mut Rav1dITUTT35;
c.event_flags |= rav1d_picture_get_event_flags(
&mut c.refs[(*c.frame_hdr).existing_frame_idx as usize].p,
);
c.event_flags |= c.refs[(*c.frame_hdr).existing_frame_idx as usize]
.p
.flags
.into();
} else {
pthread_mutex_lock(&mut c.task_thread.lock);
// Need to append this to the frame output queue.
Expand Down Expand Up @@ -2437,7 +2435,7 @@ unsafe fn parse_obus(
&& progress != FRAME_ERROR
{
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.event_flags |= rav1d_picture_get_event_flags(out_delayed);
c.event_flags |= out_delayed.flags.into();
}
rav1d_thread_picture_unref(out_delayed);
}
Expand Down
53 changes: 21 additions & 32 deletions src/picture.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::include::common::validate::validate_input;
use crate::include::dav1d::common::Rav1dDataProps;
use crate::include::dav1d::dav1d::Dav1dEventFlags;
use crate::include::dav1d::dav1d::Rav1dEventFlags;
use crate::include::dav1d::dav1d::RAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO;
use crate::include::dav1d::dav1d::RAV1D_EVENT_FLAG_NEW_SEQUENCE;
use crate::include::dav1d::headers::Rav1dContentLightLevel;
use crate::include::dav1d::headers::Rav1dFrameHeader;
use crate::include::dav1d::headers::Rav1dITUTT35;
Expand Down Expand Up @@ -31,6 +28,7 @@ use crate::src::r#ref::rav1d_ref_dec;
use crate::src::r#ref::rav1d_ref_inc;
use crate::src::r#ref::rav1d_ref_wrap;
use crate::src::r#ref::Rav1dRef;
use bitflags::bitflags;
use libc::free;
use libc::malloc;
use libc::memset;
Expand All @@ -42,10 +40,23 @@ use std::ffi::c_void;
use std::io;
use std::ptr;

pub type PictureFlags = c_uint;
pub const PICTURE_FLAG_NEW_TEMPORAL_UNIT: PictureFlags = 4;
pub const PICTURE_FLAG_NEW_OP_PARAMS_INFO: PictureFlags = 2;
pub const PICTURE_FLAG_NEW_SEQUENCE: PictureFlags = 1;
bitflags! {
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct PictureFlags: u8 {
const NEW_SEQUENCE = 1 << 0;
const NEW_OP_PARAMS_INFO = 1 << 1;
const NEW_TEMPORAL_UNIT = 1 << 2;
}
}

impl From<PictureFlags> for Rav1dEventFlags {
fn from(value: PictureFlags) -> Self {
// [`Rav1dEventFlags`] just has one extra flag vs. [`PictureFlags`],
// which this just truncates off.
// Otherwise the values are the same so we can convert the bits.
Self::from_bits_truncate(value.bits())
}
}

#[repr(C)]
pub(crate) struct Rav1dThreadPicture {
Expand Down Expand Up @@ -303,14 +314,12 @@ pub(crate) unsafe fn rav1d_thread_picture_alloc(
rav1d_ref_dec(&mut (*c).itut_t35_ref);
(*c).itut_t35 = 0 as *mut Rav1dITUTT35;
let flags_mask = if (*(*f).frame_hdr).show_frame != 0 || (*c).output_invisible_frames {
0 as c_int
PictureFlags::empty()
} else {
PICTURE_FLAG_NEW_SEQUENCE as c_int | PICTURE_FLAG_NEW_OP_PARAMS_INFO as c_int
PictureFlags::NEW_SEQUENCE | PictureFlags::NEW_OP_PARAMS_INFO
};
(*p).flags = (*c).frame_flags;
(*c).frame_flags = ::core::mem::transmute::<c_uint, PictureFlags>(
(*c).frame_flags as c_uint & flags_mask as c_uint,
);
(*c).frame_flags &= flags_mask;
(*p).visible = (*(*f).frame_hdr).show_frame != 0;
(*p).showable = (*(*f).frame_hdr).showable_frame != 0;
if have_frame_mt != 0 {
Expand Down Expand Up @@ -451,23 +460,3 @@ pub(crate) unsafe fn rav1d_thread_picture_unref(p: *mut Rav1dThreadPicture) {
rav1d_picture_unref_internal(&mut (*p).p);
(*p).progress = 0 as *mut atomic_uint;
}

pub(crate) unsafe fn rav1d_picture_get_event_flags(
p: *const Rav1dThreadPicture,
) -> Rav1dEventFlags {
if (*p).flags as u64 == 0 {
return 0 as Dav1dEventFlags;
}
let mut flags: Dav1dEventFlags = 0 as Dav1dEventFlags;
if (*p).flags as c_uint & PICTURE_FLAG_NEW_SEQUENCE as c_int as c_uint != 0 {
flags = ::core::mem::transmute::<c_uint, Dav1dEventFlags>(
flags as c_uint | RAV1D_EVENT_FLAG_NEW_SEQUENCE as c_int as c_uint,
);
}
if (*p).flags as c_uint & PICTURE_FLAG_NEW_OP_PARAMS_INFO as c_int as c_uint != 0 {
flags = ::core::mem::transmute::<c_uint, Dav1dEventFlags>(
flags as c_uint | RAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO as c_int as c_uint,
);
}
return flags;
}

0 comments on commit a088a9e

Please sign in to comment.