Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum Rav1dProfile: make a real enum #791

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,31 @@ pub struct Dav1dSequenceHeader {
[Dav1dSequenceHeaderOperatingParameterInfo; DAV1D_MAX_OPERATING_POINTS],
}

#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub enum Rav1dProfile {
Main = 0,
High = 1,
Professional = 2,
}

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

impl TryFrom<c_int> for Rav1dProfile {
type Error = ();

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

#[derive(Clone)]
#[repr(C)]
pub struct Rav1dSequenceHeader {
pub profile: c_int,
pub profile: Rav1dProfile,
pub max_width: c_int,
pub max_height: c_int,
pub layout: Rav1dPixelLayout,
Expand Down Expand Up @@ -1032,7 +1053,7 @@ impl From<Dav1dSequenceHeader> for Rav1dSequenceHeader {
operating_parameter_info,
} = value;
Self {
profile,
profile: profile.try_into().unwrap(),
max_width,
max_height,
layout: layout.try_into().unwrap(),
Expand Down Expand Up @@ -1147,7 +1168,7 @@ impl From<Rav1dSequenceHeader> for Dav1dSequenceHeader {
operating_parameter_info,
} = value;
Self {
profile,
profile: profile.into(),
max_width,
max_height,
layout: layout.into(),
Expand Down
19 changes: 8 additions & 11 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::include::dav1d::headers::Rav1dMasteringDisplay;
use crate::include::dav1d::headers::Rav1dMatrixCoefficients;
use crate::include::dav1d::headers::Rav1dObuType;
use crate::include::dav1d::headers::Rav1dPixelLayout;
use crate::include::dav1d::headers::Rav1dProfile;
use crate::include::dav1d::headers::Rav1dRestorationType;
use crate::include::dav1d::headers::Rav1dSegmentationData;
use crate::include::dav1d::headers::Rav1dSegmentationDataSet;
Expand Down Expand Up @@ -139,10 +140,7 @@ impl Debug {
fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSequenceHeader> {
let debug = Debug::new(false, "SEQHDR", gb);

let profile = gb.get_bits(3) as c_int;
if profile > 2 {
return Err(EINVAL);
}
let profile = Rav1dProfile::from_repr(gb.get_bits(3) as usize).ok_or(EINVAL)?;
debug.post(gb, "post-profile");

let still_picture = gb.get_bit() as c_int;
Expand Down Expand Up @@ -380,13 +378,13 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq

let hbd = {
let mut hbd = gb.get_bit() as c_int;
if profile == 2 && hbd != 0 {
if profile == Rav1dProfile::Professional && hbd != 0 {
hbd += gb.get_bit() as c_int;
}
hbd
};
let monochrome;
if profile != 1 {
if profile != Rav1dProfile::High {
monochrome = gb.get_bit() as c_int;
} else {
// Default initialization.
Expand Down Expand Up @@ -419,7 +417,7 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
} else if pri == RAV1D_COLOR_PRI_BT709 && trc == RAV1D_TRC_SRGB && mtrx == RAV1D_MC_IDENTITY {
layout = Rav1dPixelLayout::I444;
color_range = 1;
if profile != 1 && !(profile == 2 && hbd == 2) {
if profile != Rav1dProfile::High && !(profile == Rav1dProfile::Professional && hbd == 2) {
return Err(EINVAL);
}

Expand All @@ -430,19 +428,19 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
} else {
color_range = gb.get_bit() as c_int;
match profile {
0 => {
Rav1dProfile::Main => {
layout = Rav1dPixelLayout::I420;
ss_ver = 1;
ss_hor = ss_ver;
}
1 => {
Rav1dProfile::High => {
layout = Rav1dPixelLayout::I444;

// Default initialization.
ss_hor = Default::default();
ss_ver = Default::default();
}
2 => {
Rav1dProfile::Professional => {
if hbd == 2 {
ss_hor = gb.get_bit() as c_int;
if ss_hor != 0 {
Expand All @@ -467,7 +465,6 @@ fn parse_seq_hdr(c: &mut Rav1dContext, gb: &mut GetBits) -> Rav1dResult<Rav1dSeq
Rav1dPixelLayout::I444
};
}
_ => unreachable!(), // TODO(kkysen) Make `profile` an `enum` so this isn't needed.
}
chr = if ss_hor & ss_ver != 0 {
gb.get_bits(2) as Rav1dChromaSamplePosition
Expand Down
Loading