Skip to content

Commit

Permalink
enum Rav1dMatrixCoefficients: make a real enum
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 11, 2024
1 parent f7ddd24 commit 105694a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
103 changes: 67 additions & 36 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,38 +543,69 @@ pub(crate) const RAV1D_TRC_UNKNOWN: Rav1dTransferCharacteristics = DAV1D_TRC_UNK
pub(crate) const _RAV1D_TRC_BT709: Rav1dTransferCharacteristics = DAV1D_TRC_BT709;

pub type Dav1dMatrixCoefficients = c_uint;
pub const DAV1D_MC_RESERVED: Dav1dMatrixCoefficients = 255;
pub const DAV1D_MC_ICTCP: Dav1dMatrixCoefficients = 14;
pub const DAV1D_MC_CHROMAT_CL: Dav1dMatrixCoefficients = 13;
pub const DAV1D_MC_CHROMAT_NCL: Dav1dMatrixCoefficients = 12;
pub const DAV1D_MC_SMPTE2085: Dav1dMatrixCoefficients = 11;
pub const DAV1D_MC_BT2020_CL: Dav1dMatrixCoefficients = 10;
pub const DAV1D_MC_BT2020_NCL: Dav1dMatrixCoefficients = 9;
pub const DAV1D_MC_SMPTE_YCGCO: Dav1dMatrixCoefficients = 8;
pub const DAV1D_MC_SMPTE240: Dav1dMatrixCoefficients = 7;
pub const DAV1D_MC_BT601: Dav1dMatrixCoefficients = 6;
pub const DAV1D_MC_BT470BG: Dav1dMatrixCoefficients = 5;
pub const DAV1D_MC_FCC: Dav1dMatrixCoefficients = 4;
pub const DAV1D_MC_UNKNOWN: Dav1dMatrixCoefficients = 2;
pub const DAV1D_MC_BT709: Dav1dMatrixCoefficients = 1;
pub const DAV1D_MC_IDENTITY: Dav1dMatrixCoefficients = 0;

pub(crate) type Rav1dMatrixCoefficients = c_uint;
pub(crate) const _RAV1D_MC_RESERVED: Rav1dMatrixCoefficients = DAV1D_MC_RESERVED;
pub(crate) const _RAV1D_MC_ICTCP: Rav1dMatrixCoefficients = DAV1D_MC_ICTCP;
pub(crate) const _RAV1D_MC_CHROMAT_CL: Rav1dMatrixCoefficients = DAV1D_MC_CHROMAT_CL;
pub(crate) const _RAV1D_MC_CHROMAT_NCL: Rav1dMatrixCoefficients = DAV1D_MC_CHROMAT_NCL;
pub(crate) const _RAV1D_MC_SMPTE2085: Rav1dMatrixCoefficients = DAV1D_MC_SMPTE2085;
pub(crate) const _RAV1D_MC_BT2020_CL: Rav1dMatrixCoefficients = DAV1D_MC_BT2020_CL;
pub(crate) const _RAV1D_MC_BT2020_NCL: Rav1dMatrixCoefficients = DAV1D_MC_BT2020_NCL;
pub(crate) const _RAV1D_MC_SMPTE_YCGCO: Rav1dMatrixCoefficients = DAV1D_MC_SMPTE_YCGCO;
pub(crate) const _RAV1D_MC_SMPTE240: Rav1dMatrixCoefficients = DAV1D_MC_SMPTE240;
pub(crate) const _RAV1D_MC_BT601: Rav1dMatrixCoefficients = DAV1D_MC_BT601;
pub(crate) const _RAV1D_MC_BT470BG: Rav1dMatrixCoefficients = DAV1D_MC_BT470BG;
pub(crate) const _RAV1D_MC_FCC: Rav1dMatrixCoefficients = DAV1D_MC_FCC;
pub(crate) const RAV1D_MC_UNKNOWN: Rav1dMatrixCoefficients = DAV1D_MC_UNKNOWN;
pub(crate) const _RAV1D_MC_BT709: Rav1dMatrixCoefficients = DAV1D_MC_BT709;
pub(crate) const RAV1D_MC_IDENTITY: Rav1dMatrixCoefficients = DAV1D_MC_IDENTITY;
pub const DAV1D_MC_IDENTITY: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Identity as Dav1dMatrixCoefficients;
pub const DAV1D_MC_BT709: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Bt709 as Dav1dMatrixCoefficients;
pub const DAV1D_MC_UNKNOWN: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Unknown as Dav1dMatrixCoefficients;
pub const DAV1D_MC_FCC: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Fcc as Dav1dMatrixCoefficients;
pub const DAV1D_MC_BT470BG: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Bt470bg as Dav1dMatrixCoefficients;
pub const DAV1D_MC_BT601: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Bt601 as Dav1dMatrixCoefficients;
pub const DAV1D_MC_SMPTE240: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Smpte240 as Dav1dMatrixCoefficients;
pub const DAV1D_MC_SMPTE_YCGCO: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Smpte_ycgco as Dav1dMatrixCoefficients;
pub const DAV1D_MC_BT2020_NCL: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Bt2020_ncl as Dav1dMatrixCoefficients;
pub const DAV1D_MC_BT2020_CL: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Bt2020_cl as Dav1dMatrixCoefficients;
pub const DAV1D_MC_SMPTE2085: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Smpte2085 as Dav1dMatrixCoefficients;
pub const DAV1D_MC_CHROMAT_NCL: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Chromat_ncl as Dav1dMatrixCoefficients;
pub const DAV1D_MC_CHROMAT_CL: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Chromat_cl as Dav1dMatrixCoefficients;
pub const DAV1D_MC_ICTCP: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Ictcp as Dav1dMatrixCoefficients;
pub const DAV1D_MC_RESERVED: Dav1dMatrixCoefficients =
Rav1dMatrixCoefficients::Reserved as Dav1dMatrixCoefficients;

#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub enum Rav1dMatrixCoefficients {
Identity = 0,
Bt709 = 1,
Unknown = 2,
Fcc = 4,
Bt470bg = 5,
Bt601 = 6,
Smpte240 = 7,
Smpte_ycgco = 8,
Bt2020_ncl = 9,
Bt2020_cl = 10,
Smpte2085 = 11,
Chromat_ncl = 12,
Chromat_cl = 13,
Ictcp = 14,
Reserved = 255,
}

impl From<Rav1dMatrixCoefficients> for Dav1dMatrixCoefficients {
fn from(value: Rav1dMatrixCoefficients) -> Self {
value as Dav1dMatrixCoefficients
}
}

impl TryFrom<Dav1dMatrixCoefficients> for Rav1dMatrixCoefficients {
type Error = ();

fn try_from(value: Dav1dMatrixCoefficients) -> Result<Self, Self::Error> {
Self::from_repr(value as usize).ok_or(())
}
}

pub type Dav1dChromaSamplePosition = c_uint;
pub const DAV1D_CHR_COLOCATED: Dav1dChromaSamplePosition = 2;
Expand Down Expand Up @@ -1074,8 +1105,8 @@ impl From<Dav1dSequenceHeader> for Rav1dSequenceHeader {
layout: layout.try_into().unwrap(),
pri,
trc,
mtrx,
chr,
mtrx: mtrx.try_into().unwrap(),
chr: chr.try_into().unwrap(),
hbd,
color_range,
num_operating_points,
Expand Down Expand Up @@ -1189,8 +1220,8 @@ impl From<Rav1dSequenceHeader> for Dav1dSequenceHeader {
layout: layout.into(),
pri,
trc,
mtrx,
chr,
mtrx: mtrx.into(),
chr: chr.into(),
hbd,
color_range,
num_operating_points,
Expand Down
4 changes: 2 additions & 2 deletions src/fg_apply.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::include::common::bitdepth::BitDepth;
use crate::include::common::bitdepth::BPC;
use crate::include::dav1d::headers::Rav1dMatrixCoefficients;
use crate::include::dav1d::headers::Rav1dPixelLayout;
use crate::include::dav1d::headers::RAV1D_MC_IDENTITY;
use crate::include::dav1d::picture::Rav1dPicture;
use crate::src::align::ArrayDefault;
use crate::src::filmgrain::Rav1dFilmGrainDSPContext;
Expand Down Expand Up @@ -187,7 +187,7 @@ pub(crate) unsafe fn rav1d_apply_grain_row<BD: BitDepth>(
let ss_y = (r#in.p.layout == Rav1dPixelLayout::I420) as usize;
let ss_x = (r#in.p.layout != Rav1dPixelLayout::I444) as usize;
let cpw = out.p.w as usize + ss_x >> ss_x;
let is_id = seq_hdr.mtrx == RAV1D_MC_IDENTITY;
let is_id = seq_hdr.mtrx == Rav1dMatrixCoefficients::Identity;
let luma_src = (r#in.data.data[0] as *mut BD::Pixel)
.offset(((row * 32) as isize * BD::pxstride(r#in.stride[0])) as isize);
let bitdepth_max = (1 << out.p.bpc) - 1;
Expand Down
16 changes: 10 additions & 6 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ use crate::include::dav1d::headers::RAV1D_MAX_CDEF_STRENGTHS;
use crate::include::dav1d::headers::RAV1D_MAX_OPERATING_POINTS;
use crate::include::dav1d::headers::RAV1D_MAX_TILE_COLS;
use crate::include::dav1d::headers::RAV1D_MAX_TILE_ROWS;
use crate::include::dav1d::headers::RAV1D_MC_IDENTITY;
use crate::include::dav1d::headers::RAV1D_MC_UNKNOWN;
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;
Expand Down Expand Up @@ -393,11 +391,11 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
if color_description_present != 0 {
pri = gb.get_bits(8) as Rav1dColorPrimaries;
trc = gb.get_bits(8) as Rav1dTransferCharacteristics;
mtrx = gb.get_bits(8) as Rav1dMatrixCoefficients;
mtrx = Rav1dMatrixCoefficients::from_repr(gb.get_bits(8) as usize).unwrap();
} else {
pri = RAV1D_COLOR_PRI_UNKNOWN;
trc = RAV1D_TRC_UNKNOWN;
mtrx = RAV1D_MC_UNKNOWN;
mtrx = Rav1dMatrixCoefficients::Unknown;
}
let color_range;
let layout;
Expand All @@ -410,7 +408,10 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
ss_ver = 1;
ss_hor = ss_ver;
chr = RAV1D_CHR_UNKNOWN;
} else if pri == RAV1D_COLOR_PRI_BT709 && trc == RAV1D_TRC_SRGB && mtrx == RAV1D_MC_IDENTITY {
} else if pri == RAV1D_COLOR_PRI_BT709
&& trc == RAV1D_TRC_SRGB
&& mtrx == Rav1dMatrixCoefficients::Identity
{
layout = Rav1dPixelLayout::I444;
color_range = 1;
if profile != Rav1dProfile::High && !(profile == Rav1dProfile::Professional && hbd == 2) {
Expand Down Expand Up @@ -468,7 +469,10 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
RAV1D_CHR_UNKNOWN
};
}
if c.strict_std_compliance && mtrx == RAV1D_MC_IDENTITY && layout != Rav1dPixelLayout::I444 {
if c.strict_std_compliance
&& mtrx == Rav1dMatrixCoefficients::Identity
&& layout != Rav1dPixelLayout::I444
{
return Err(EINVAL);
}
let separate_uv_delta_q;
Expand Down

0 comments on commit 105694a

Please sign in to comment.