Skip to content

Commit

Permalink
use bool for two values
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 3, 2024
1 parent 9adf7e8 commit d21fd1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
30 changes: 10 additions & 20 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::src::enum_map::EnumKey;
use crate::src::error::Rav1dError;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ops::BitAnd;
Expand Down Expand Up @@ -60,16 +59,16 @@ pub(crate) const RAV1D_REFS_PER_FRAME: usize = DAV1D_REFS_PER_FRAME;
pub(crate) const RAV1D_TOTAL_REFS_PER_FRAME: usize = DAV1D_TOTAL_REFS_PER_FRAME;

pub type Dav1dObuType = c_uint;
pub const DAV1D_OBU_PADDING: Dav1dObuType = Rav1dObuType::Padding as _;
pub const DAV1D_OBU_REDUNDANT_FRAME_HDR: Dav1dObuType = Rav1dObuType::RedundantFrameHdr as _;
pub const DAV1D_OBU_FRAME: Dav1dObuType = Rav1dObuType::Frame as _;
pub const DAV1D_OBU_METADATA: Dav1dObuType = Rav1dObuType::Metadata as _;
pub const DAV1D_OBU_TILE_GRP: Dav1dObuType = Rav1dObuType::TileGrp as _;
pub const DAV1D_OBU_FRAME_HDR: Dav1dObuType = Rav1dObuType::FrameHdr as _;
pub const DAV1D_OBU_TD: Dav1dObuType = Rav1dObuType::Td as _;
pub const DAV1D_OBU_SEQ_HDR: Dav1dObuType = Rav1dObuType::SeqHdr as _;

#[repr(u8)]
pub const DAV1D_OBU_PADDING: Dav1dObuType = Rav1dObuType::Padding as Dav1dObuType;
pub const DAV1D_OBU_REDUNDANT_FRAME_HDR: Dav1dObuType =
Rav1dObuType::RedundantFrameHdr as Dav1dObuType;
pub const DAV1D_OBU_FRAME: Dav1dObuType = Rav1dObuType::Frame as Dav1dObuType;
pub const DAV1D_OBU_METADATA: Dav1dObuType = Rav1dObuType::Metadata as Dav1dObuType;
pub const DAV1D_OBU_TILE_GRP: Dav1dObuType = Rav1dObuType::TileGrp as Dav1dObuType;
pub const DAV1D_OBU_FRAME_HDR: Dav1dObuType = Rav1dObuType::FrameHdr as Dav1dObuType;
pub const DAV1D_OBU_TD: Dav1dObuType = Rav1dObuType::Td as Dav1dObuType;
pub const DAV1D_OBU_SEQ_HDR: Dav1dObuType = Rav1dObuType::SeqHdr as Dav1dObuType;

#[derive(Clone, Copy, PartialEq, Eq, FromRepr)]
pub enum Rav1dObuType {
SeqHdr = 1,
Expand All @@ -82,15 +81,6 @@ pub enum Rav1dObuType {
Padding = 15,
}

impl TryFrom<Dav1dObuType> for Rav1dObuType {
type Error = Rav1dError;

fn try_from(value: Dav1dObuType) -> Result<Self, Self::Error> {
let value = u8::try_from(value).map_err(|_| Rav1dError::EINVAL)?;
Self::from_repr(value).ok_or(Rav1dError::EINVAL)
}
}

pub type Dav1dTxfmMode = c_uint;
pub const DAV1D_N_TX_MODES: usize = 3;
pub const DAV1D_TX_SWITCHABLE: Dav1dTxfmMode = 2;
Expand Down
38 changes: 19 additions & 19 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,21 +2125,21 @@ unsafe fn parse_obus(
// obu header
gb.get_bit(); // obu_forbidden_bit
let raw_type = gb.get_bits(4);
let r#type = Rav1dObuType::try_from(raw_type);
let has_extension = gb.get_bit() as c_int;
let has_length_field = gb.get_bit() as c_int;
let r#type = Rav1dObuType::from_repr(raw_type as usize);
let has_extension = gb.get_bit();
let has_length_field = gb.get_bit();
gb.get_bit(); // reserved

let mut temporal_id = 0;
let mut spatial_id = 0;
if has_extension != 0 {
if has_extension {
temporal_id = gb.get_bits(3) as c_int;
spatial_id = gb.get_bits(2) as c_int;
gb.get_bits(3); // reserved
}

// obu length field
let len = if has_length_field != 0 {
let len = if has_length_field {
gb.get_uleb128() as usize
} else {
r#in.len() - 1 - has_extension as usize
Expand All @@ -2163,8 +2163,8 @@ unsafe fn parse_obus(
}

// skip obu not belonging to the selected temporal/spatial layer
if !matches!(r#type, Ok(Rav1dObuType::SeqHdr | Rav1dObuType::Td))
&& has_extension != 0
if !matches!(r#type, Some(Rav1dObuType::SeqHdr | Rav1dObuType::Td))
&& has_extension
&& c.operating_point_idc != 0
{
let in_temporal_layer = (c.operating_point_idc >> temporal_id & 1) as c_int;
Expand Down Expand Up @@ -2224,7 +2224,7 @@ unsafe fn parse_obus(
}

match r#type {
Ok(Rav1dObuType::SeqHdr) => {
Some(Rav1dObuType::SeqHdr) => {
let seq_hdr = parse_seq_hdr(c, &mut gb).inspect_err(|_| {
writeln!(c.logger, "Error parsing sequence header");
})?;
Expand Down Expand Up @@ -2269,10 +2269,10 @@ unsafe fn parse_obus(
}
c.seq_hdr = Some(Arc::new(DRav1d::from_rav1d(seq_hdr))); // TODO(kkysen) fallible allocation
}
Ok(Rav1dObuType::RedundantFrameHdr) if c.frame_hdr.is_some() => {}
Ok(Rav1dObuType::RedundantFrameHdr | Rav1dObuType::Frame | Rav1dObuType::FrameHdr)
Some(Rav1dObuType::RedundantFrameHdr) if c.frame_hdr.is_some() => {}
Some(Rav1dObuType::RedundantFrameHdr | Rav1dObuType::Frame | Rav1dObuType::FrameHdr)
if global => {}
Ok(Rav1dObuType::RedundantFrameHdr | Rav1dObuType::Frame | Rav1dObuType::FrameHdr) => {
Some(Rav1dObuType::RedundantFrameHdr | Rav1dObuType::Frame | Rav1dObuType::FrameHdr) => {
c.frame_hdr = None;
// TODO(kkysen) C originally re-used this allocation,
// but it was also pooling, which we've dropped for now.
Expand All @@ -2288,7 +2288,7 @@ unsafe fn parse_obus(

c.tiles.clear();
c.n_tiles = 0;
if r#type != Ok(Rav1dObuType::Frame) {
if r#type != Some(Rav1dObuType::Frame) {
// This is actually a frame header OBU,
// so read the trailing bit and check for overrun.
gb.get_bit();
Expand All @@ -2309,7 +2309,7 @@ unsafe fn parse_obus(
return Err(ERANGE);
}

if r#type == Ok(Rav1dObuType::Frame) {
if r#type == Some(Rav1dObuType::Frame) {
// OBU_FRAMEs shouldn't be signaled with `show_existing_frame`.
if frame_hdr.show_existing_frame != 0 {
return Err(EINVAL);
Expand All @@ -2318,7 +2318,7 @@ unsafe fn parse_obus(

c.frame_hdr = Some(Arc::new(DRav1d::from_rav1d(frame_hdr))); // TODO(kkysen) fallible allocation

if r#type == Ok(Rav1dObuType::Frame) {
if r#type == Some(Rav1dObuType::Frame) {
// This is the frame header at the start of a frame OBU.
// There's no trailing bit at the end to skip,
// but we do need to align to the next byte.
Expand All @@ -2328,12 +2328,12 @@ unsafe fn parse_obus(
}
}
}
Ok(Rav1dObuType::TileGrp) => {
Some(Rav1dObuType::TileGrp) => {
if !global {
parse_tile_grp(c, r#in, props, &mut gb, init_bit_pos, init_byte_pos, len)?;
}
}
Ok(Rav1dObuType::Metadata) => {
Some(Rav1dObuType::Metadata) => {
let debug = Debug::new(false, "OBU", &gb);

// obu metadata type field
Expand Down Expand Up @@ -2441,12 +2441,12 @@ unsafe fn parse_obus(
}
}
}
Ok(Rav1dObuType::Td) => {
Some(Rav1dObuType::Td) => {
c.frame_flags
.fetch_or(PictureFlags::NEW_TEMPORAL_UNIT, Ordering::Relaxed);
}
Ok(Rav1dObuType::Padding) => {} // Ignore OBUs we don't care about.
Err(_) => {
Some(Rav1dObuType::Padding) => {} // Ignore OBUs we don't care about.
None => {
// Print a warning, but don't fail for unknown types.
writeln!(c.logger, "Unknown OBU type {raw_type} of size {len}");
}
Expand Down

0 comments on commit d21fd1b

Please sign in to comment.