Skip to content

Commit fb56c0e

Browse files
authored
enum Rav1dError: rename fields to more Rusty versions and add docs (#1442)
Pulled out the `Rav1dError` changes from #1439 into a separate PR.
2 parents e71e7aa + bbfa211 commit fb56c0e

File tree

9 files changed

+216
-134
lines changed

9 files changed

+216
-134
lines changed

src/data.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use std::ptr::NonNull;
55

66
use crate::c_arc::CArc;
77
use crate::c_box::{CBox, FnFree, Free};
8-
use crate::error::Rav1dError::EINVAL;
9-
use crate::error::Rav1dResult;
8+
use crate::error::{Rav1dError, Rav1dResult};
109
use crate::include::common::validate::validate_input;
1110
use crate::include::dav1d::common::Rav1dDataProps;
1211
use crate::include::dav1d::data::Rav1dData;
@@ -39,7 +38,7 @@ impl Rav1dData {
3938
free_callback: Option<FnFree>,
4039
cookie: Option<SendSyncNonNull<c_void>>,
4140
) -> Rav1dResult<Self> {
42-
let free = validate_input!(free_callback.ok_or(EINVAL))?;
41+
let free = validate_input!(free_callback.ok_or(Rav1dError::InvalidArgument))?;
4342
let free = Free { free, cookie };
4443
// SAFETY: Preconditions delegate to `CBox::from_c`'s safety.
4544
let data = unsafe { CBox::from_c(data, free) };
@@ -56,7 +55,7 @@ impl Rav1dData {
5655
free_callback: Option<FnFree>,
5756
cookie: Option<SendSyncNonNull<c_void>>,
5857
) -> Rav1dResult {
59-
let free = validate_input!(free_callback.ok_or(EINVAL))?;
58+
let free = validate_input!(free_callback.ok_or(Rav1dError::InvalidArgument))?;
6059
let free = Free { free, cookie };
6160
// SAFETY: Preconditions delegate to `CBox::from_c`'s safety.
6261
let user_data = unsafe { CBox::from_c(user_data, free) };

src/decode.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use crate::env::{
2222
get_cur_frame_segid, get_drl_context, get_filter_ctx, get_gmv_2d, get_intra_ctx,
2323
get_jnt_comp_ctx, get_mask_comp_ctx, get_partition_ctx, get_poc_diff, get_tx_ctx, BlockContext,
2424
};
25-
use crate::error::Rav1dError::{EINVAL, ENOPROTOOPT};
26-
use crate::error::Rav1dResult;
25+
use crate::error::{Rav1dError, Rav1dResult};
2726
use crate::extensions::OptionError as _;
2827
use crate::include::common::attributes::ctz;
2928
use crate::include::common::bitdepth::BPC;
@@ -4594,7 +4593,7 @@ pub(crate) fn rav1d_decode_frame_init_cdf(
45944593
data.len()
45954594
} else {
45964595
if n_bytes > data.len() {
4597-
return Err(EINVAL);
4596+
return Err(Rav1dError::InvalidArgument);
45984597
}
45994598
let (cur_data, rest_data) = CArc::split_at(data, n_bytes);
46004599
let tile_sz = cur_data
@@ -4605,7 +4604,7 @@ pub(crate) fn rav1d_decode_frame_init_cdf(
46054604
+ 1;
46064605
data = rest_data;
46074606
if tile_sz > data.len() {
4608-
return Err(EINVAL);
4607+
return Err(Rav1dError::InvalidArgument);
46094608
}
46104609
tile_sz
46114610
};
@@ -4709,7 +4708,7 @@ fn rav1d_decode_frame_main(c: &Rav1dContext, f: &mut Rav1dFrameData) -> Rav1dRes
47094708
}
47104709
for col in 0..cols {
47114710
t.ts = tile_row * cols + col;
4712-
rav1d_decode_tile_sbrow(c, &mut t, f).map_err(|()| EINVAL)?;
4711+
rav1d_decode_tile_sbrow(c, &mut t, f).map_err(|()| Rav1dError::InvalidArgument)?;
47134712
}
47144713
if f.frame_hdr().frame_type.is_inter_or_switch() {
47154714
c.dsp
@@ -4748,7 +4747,7 @@ pub(crate) fn rav1d_decode_frame_exit(
47484747
rf.p.frame_hdr.is_some()
47494748
&& rf.progress.as_ref().unwrap()[1].load(Ordering::SeqCst) == FRAME_ERROR
47504749
}) {
4751-
retval = Err(EINVAL);
4750+
retval = Err(Rav1dError::InvalidArgument);
47524751
task_thread.error.store(1, Ordering::SeqCst);
47534752
f.sr_cur.progress.as_mut().unwrap()[1].store(FRAME_ERROR, Ordering::SeqCst);
47544753
}
@@ -4932,7 +4931,7 @@ pub fn rav1d_submit_frame(c: &Rav1dContext, state: &mut Rav1dState) -> Rav1dResu
49324931
&mut state.cached_error_props,
49334932
&state.in_0.m,
49344933
);
4935-
return Err(ENOPROTOOPT);
4934+
return Err(Rav1dError::UnsupportedBitstream);
49364935
}
49374936
};
49384937

@@ -4953,7 +4952,7 @@ pub fn rav1d_submit_frame(c: &Rav1dContext, state: &mut Rav1dState) -> Rav1dResu
49534952
&mut state.cached_error_props,
49544953
&state.in_0.m,
49554954
);
4956-
return Err(EINVAL);
4955+
return Err(Rav1dError::InvalidArgument);
49574956
}
49584957
}
49594958
for i in 0..7 {
@@ -4976,7 +4975,7 @@ pub fn rav1d_submit_frame(c: &Rav1dContext, state: &mut Rav1dState) -> Rav1dResu
49764975
&mut state.cached_error_props,
49774976
&state.in_0.m,
49784977
);
4979-
return Err(EINVAL);
4978+
return Err(Rav1dError::InvalidArgument);
49804979
}
49814980
f.refp[i] = state.refs[refidx].p.clone();
49824981
ref_coded_width[i] = state.refs[refidx]

src/error.rs

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,82 @@
1+
use std::error::Error;
12
use std::ffi::{c_int, c_uint};
3+
use std::fmt::{self, Display, Formatter};
24

35
use strum::FromRepr;
46

7+
/// Error enum return by various `rav1d` operations.
58
#[derive(Clone, Copy, PartialEq, Eq, FromRepr, Debug)]
69
#[repr(u8)]
710
#[non_exhaustive]
811
pub enum Rav1dError {
912
/// This represents a generic `rav1d` error.
10-
/// It has nothing to do with the other `errno`-based ones
11-
/// (and that's why it's not all caps like the other ones).
13+
/// It has nothing to do with the other `errno`-based ones.
1214
///
1315
/// Normally `EPERM = 1`, but `dav1d` never uses `EPERM`,
1416
/// but does use `-1`, as opposed to the normal `DAV1D_ERR(E*)`.
1517
///
16-
/// Also Note that this forces `0` to be the niche,
18+
/// Also note that this forces `0` to be the niche,
1719
/// which is more optimal since `0` is no error for [`Dav1dResult`].
18-
EGeneric = 1,
19-
20-
ENOENT = libc::ENOENT as u8,
21-
EIO = libc::EIO as u8,
22-
EAGAIN = libc::EAGAIN as u8,
23-
ENOMEM = libc::ENOMEM as u8,
24-
EINVAL = libc::EINVAL as u8,
25-
ERANGE = libc::ERANGE as u8,
26-
ENOPROTOOPT = libc::ENOPROTOOPT as u8,
20+
Other = 1,
21+
22+
/// No entity.
23+
///
24+
/// No Sequence Header OBUs were found in the buffer.
25+
NoEntity = libc::ENOENT as u8,
26+
27+
/// Try again.
28+
///
29+
/// If this is returned by [`rav1d_send_data`], then there
30+
/// are decoded frames pending that first have to be retrieved via [`rav1d_get_picture`]
31+
/// before processing any further pending data.
32+
///
33+
/// If this is returned by [`rav1d_get_picture`], then no decoded frames are pending
34+
/// currently and more data needs to be sent to the decoder.
35+
TryAgain = libc::EAGAIN as u8,
36+
37+
/// Out of memory.
38+
///
39+
/// Not enough memory is currently available for performing this operation.
40+
OutOfMemory = libc::ENOMEM as u8,
41+
42+
/// Invalid argument.
43+
///
44+
/// One of the arguments passed to the function, including the bitstream, is invalid.
45+
InvalidArgument = libc::EINVAL as u8,
46+
47+
/// Out of range.
48+
///
49+
/// The frame size is larger than the limit.
50+
OutOfRange = libc::ERANGE as u8,
51+
52+
/// Unsupported bitstream.
53+
///
54+
/// The provided bitstream is not supported by `rav1d`.
55+
UnsupportedBitstream = libc::ENOPROTOOPT as u8,
56+
}
57+
58+
impl Rav1dError {
59+
pub const fn as_str(&self) -> &'static str {
60+
match self {
61+
Rav1dError::TryAgain => "Try again",
62+
Rav1dError::InvalidArgument => "Invalid argument",
63+
Rav1dError::OutOfMemory => "Not enough memory available",
64+
Rav1dError::UnsupportedBitstream => "Unsupported bitstream",
65+
Rav1dError::Other => "Other error",
66+
Rav1dError::NoEntity => "No sequence header found",
67+
Rav1dError::OutOfRange => "Out of range",
68+
}
69+
}
2770
}
2871

72+
impl Display for Rav1dError {
73+
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
74+
write!(fmt, "{}", self.as_str())
75+
}
76+
}
77+
78+
impl Error for Rav1dError {}
79+
2980
pub type Rav1dResult<T = ()> = Result<T, Rav1dError>;
3081

3182
#[derive(Clone, Copy, PartialEq, Eq, Debug)]

src/include/dav1d/dav1d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl TryFrom<Dav1dDecodeFrameType> for Rav1dDecodeFrameType {
8080
type Error = Rav1dError;
8181

8282
fn try_from(value: Dav1dDecodeFrameType) -> Result<Self, Self::Error> {
83-
Self::from_repr(value as usize).ok_or(Rav1dError::EINVAL)
83+
Self::from_repr(value as usize).ok_or(Rav1dError::InvalidArgument)
8484
}
8585
}
8686

src/include/dav1d/picture.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::c_arc::RawArc;
1414
use crate::disjoint_mut::{
1515
AsMutPtr, DisjointImmutGuard, DisjointMut, DisjointMutGuard, SliceBounds,
1616
};
17-
use crate::error::Rav1dError::EINVAL;
1817
use crate::error::{Dav1dResult, Rav1dError, Rav1dResult};
1918
use crate::ffi_safe::FFISafe;
2019
use crate::include::common::bitdepth::BitDepth;
@@ -719,8 +718,12 @@ impl TryFrom<Dav1dPicAllocator> for Rav1dPicAllocator {
719718
} = value;
720719
Ok(Self {
721720
cookie,
722-
alloc_picture_callback: validate_input!(alloc_picture_callback.ok_or(EINVAL))?,
723-
release_picture_callback: validate_input!(release_picture_callback.ok_or(EINVAL))?,
721+
alloc_picture_callback: validate_input!(
722+
alloc_picture_callback.ok_or(Rav1dError::InvalidArgument)
723+
)?,
724+
release_picture_callback: validate_input!(
725+
release_picture_callback.ok_or(Rav1dError::InvalidArgument)
726+
)?,
724727
})
725728
}
726729
}

0 commit comments

Comments
 (0)