Skip to content

Commit

Permalink
struct {D,R}av1d{Sequence,Frame}Header: Replace Rav1dRefs with `O…
Browse files Browse the repository at this point in the history
…ption<Arc<DRav1d<_, _>>>`s (#663)

As these types are heavily used unlike the other `Rav1dRef`ed types,
`impl Deref for DRav1d` is added, `deref`ing to `R` as that's the type
used primarily in Rust.

And to simplify uses and avoid triple derefs (including an `Arc` deref
and `Option::unwrap`) at use sites, a `let {frame,seq}_hdr =
&***{f,c}.{frame,seq}_hdr.as_ref().unwrap();` is refactored out in each
`fn` where it's used.

Furthermore, similarly to #661, `.update_rav1d()`s are skipped. Although
these types are indeed read, they are meant to be read-only, so this
should be okay to do.

Unlike the previous `Arc`ified types in #659 and #661, these `Rav1dRef`s
were pooled, which the `Arc` replacement is not. However, those pools
were not thread-local, so it's not clear if it is a perf regression to
remove them (we'll check). Either way, the API of `Arc` should be
similar enough to a pooled `Arc` (e.x.
[`refpool`](https://docs.rs/refpool)) that we can switch to it later if
perf demands it.
  • Loading branch information
kkysen authored Jan 11, 2024
2 parents 126be73 + ca294df commit 120ae32
Show file tree
Hide file tree
Showing 17 changed files with 623 additions and 712 deletions.
65 changes: 34 additions & 31 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::src::enum_map::EnumKey;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ops::BitAnd;
use std::ops::Deref;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use strum::EnumCount;
Expand All @@ -15,15 +16,6 @@ pub struct DRav1d<R, D> {
pub dav1d: D,
}

impl<R, D> DRav1d<R, D>
where
D: Clone + Into<R>,
{
pub fn update_rav1d(&mut self) {
self.rav1d = self.dav1d.clone().into();
}
}

impl<R, D> DRav1d<R, D>
where
R: Clone + Into<D>,
Expand All @@ -34,6 +26,17 @@ where
}
}

/// Since the `D`/`Dav1d*` type is only used externally by C,
/// it's reasonable to `.deref()`
/// to the `R`/`Rav1d*` type used everywhere internally.
impl<R, D> Deref for DRav1d<R, D> {
type Target = R;

fn deref(&self) -> &Self::Target {
&self.rav1d
}
}

// Constants from Section 3. "Symbols and abbreviated terms"
pub const DAV1D_MAX_CDEF_STRENGTHS: usize = 8;
pub const DAV1D_MAX_OPERATING_POINTS: usize = 32;
Expand Down Expand Up @@ -346,7 +349,7 @@ impl From<Rav1dPixelLayoutSubSampled> for Rav1dPixelLayout {
}

#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub(crate) enum Rav1dFrameType {
pub enum Rav1dFrameType {
Key = 0,
Inter = 1,
Intra = 2,
Expand Down Expand Up @@ -569,7 +572,7 @@ pub struct Dav1dSequenceHeaderOperatingPoint {

#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub(crate) struct Rav1dSequenceHeaderOperatingPoint {
pub struct Rav1dSequenceHeaderOperatingPoint {
pub major_level: c_int,
pub minor_level: c_int,
pub initial_display_delay: c_int,
Expand Down Expand Up @@ -635,7 +638,7 @@ pub struct Dav1dSequenceHeaderOperatingParameterInfo {

#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[repr(C)]
pub(crate) struct Rav1dSequenceHeaderOperatingParameterInfo {
pub struct Rav1dSequenceHeaderOperatingParameterInfo {
pub decoder_buffer_delay: c_int,
pub encoder_buffer_delay: c_int,
pub low_delay_mode: c_int,
Expand Down Expand Up @@ -732,7 +735,7 @@ pub struct Dav1dSequenceHeader {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dSequenceHeader {
pub struct Rav1dSequenceHeader {
pub profile: c_int,
pub max_width: c_int,
pub max_height: c_int,
Expand Down Expand Up @@ -1151,7 +1154,7 @@ pub struct Dav1dSegmentationData {

#[derive(Clone, Default)]
#[repr(C)]
pub(crate) struct Rav1dSegmentationData {
pub struct Rav1dSegmentationData {
pub delta_q: c_int,
pub delta_lf_y_v: c_int,
pub delta_lf_y_h: c_int,
Expand Down Expand Up @@ -1222,7 +1225,7 @@ pub struct Dav1dSegmentationDataSet {

#[derive(Clone, Default)]
#[repr(C)]
pub(crate) struct Rav1dSegmentationDataSet {
pub struct Rav1dSegmentationDataSet {
pub d: [Rav1dSegmentationData; RAV1D_MAX_SEGMENTS as usize],
pub preskip: c_int,
pub last_active_segid: c_int,
Expand Down Expand Up @@ -1267,7 +1270,7 @@ pub struct Dav1dLoopfilterModeRefDeltas {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dLoopfilterModeRefDeltas {
pub struct Rav1dLoopfilterModeRefDeltas {
pub mode_delta: [c_int; 2],
pub ref_delta: [c_int; RAV1D_TOTAL_REFS_PER_FRAME],
}
Expand Down Expand Up @@ -1475,7 +1478,7 @@ pub struct Dav1dFrameHeader_film_grain {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_film_grain {
pub struct Rav1dFrameHeader_film_grain {
pub data: Rav1dFilmGrainData,
pub present: c_int,
pub update: c_int,
Expand Down Expand Up @@ -1519,7 +1522,7 @@ pub struct Dav1dFrameHeaderOperatingPoint {

#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeaderOperatingPoint {
pub struct Rav1dFrameHeaderOperatingPoint {
pub buffer_removal_time: c_int,
}

Expand Down Expand Up @@ -1554,7 +1557,7 @@ pub struct Dav1dFrameHeader_super_res {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_super_res {
pub struct Rav1dFrameHeader_super_res {
pub width_scale_denominator: c_int,
pub enabled: c_int,
}
Expand Down Expand Up @@ -1605,7 +1608,7 @@ pub struct Dav1dFrameHeader_tiling {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_tiling {
pub struct Rav1dFrameHeader_tiling {
pub uniform: c_int,
pub n_bytes: c_uint,
pub min_log2_cols: c_int,
Expand Down Expand Up @@ -1708,7 +1711,7 @@ pub struct Dav1dFrameHeader_quant {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_quant {
pub struct Rav1dFrameHeader_quant {
pub yac: c_int,
pub ydc_delta: c_int,
pub udc_delta: c_int,
Expand Down Expand Up @@ -1793,7 +1796,7 @@ pub struct Dav1dFrameHeader_segmentation {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_segmentation {
pub struct Rav1dFrameHeader_segmentation {
pub enabled: c_int,
pub update_map: c_int,
pub temporal: c_int,
Expand Down Expand Up @@ -1858,7 +1861,7 @@ pub struct Dav1dFrameHeader_delta_q {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_delta_q {
pub struct Rav1dFrameHeader_delta_q {
pub present: c_int,
pub res_log2: c_int,
}
Expand Down Expand Up @@ -1887,7 +1890,7 @@ pub struct Dav1dFrameHeader_delta_lf {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_delta_lf {
pub struct Rav1dFrameHeader_delta_lf {
pub present: c_int,
pub res_log2: c_int,
pub multi: c_int,
Expand Down Expand Up @@ -1932,7 +1935,7 @@ pub struct Dav1dFrameHeader_delta {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_delta {
pub struct Rav1dFrameHeader_delta {
pub q: Rav1dFrameHeader_delta_q,
pub lf: Rav1dFrameHeader_delta_lf,
}
Expand Down Expand Up @@ -1971,7 +1974,7 @@ pub struct Dav1dFrameHeader_loopfilter {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_loopfilter {
pub struct Rav1dFrameHeader_loopfilter {
pub level_y: [c_int; 2],
pub level_u: c_int,
pub level_v: c_int,
Expand Down Expand Up @@ -2038,7 +2041,7 @@ pub struct Dav1dFrameHeader_cdef {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_cdef {
pub struct Rav1dFrameHeader_cdef {
pub damping: c_int,
pub n_bits: c_int,
pub y_strength: [c_int; RAV1D_MAX_CDEF_STRENGTHS],
Expand Down Expand Up @@ -2088,7 +2091,7 @@ pub struct Dav1dFrameHeader_restoration {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader_restoration {
pub struct Rav1dFrameHeader_restoration {
pub r#type: [Rav1dRestorationType; 3],
pub unit_size: [c_int; 2],
}
Expand Down Expand Up @@ -2164,7 +2167,7 @@ pub struct Dav1dFrameHeader {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameSize {
pub struct Rav1dFrameSize {
pub width: [c_int; 2],
pub height: c_int,
pub render_width: c_int,
Expand All @@ -2175,15 +2178,15 @@ pub(crate) struct Rav1dFrameSize {

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameSkipMode {
pub struct Rav1dFrameSkipMode {
pub allowed: c_int,
pub enabled: c_int,
pub refs: [c_int; 2],
}

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader {
pub struct Rav1dFrameHeader {
pub size: Rav1dFrameSize,
pub film_grain: Rav1dFrameHeader_film_grain,
pub frame_type: Rav1dFrameType,
Expand Down
Loading

0 comments on commit 120ae32

Please sign in to comment.