Skip to content

Commit ab750d8

Browse files
authored
struct Rav1dSettings: change n_threads and max_frame_delay to be u32 (#1443)
Pulled out type changes from here #1439 into its own PR.
2 parents fb56c0e + 4863a8a commit ab750d8

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

src/include/dav1d/dav1d.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use strum::FromRepr;
55

66
use crate::c_arc::RawArc;
77
use crate::error::Rav1dError;
8+
use crate::in_range::InRange;
89
use crate::include::dav1d::picture::{Dav1dPicAllocator, Rav1dPicAllocator};
910
use crate::internal::Rav1dContext;
1011
pub use crate::log::Dav1dLogger;
1112
use crate::log::Rav1dLogger;
13+
use crate::validate_input;
1214

1315
pub type Dav1dContext = RawArc<Rav1dContext>;
1416

@@ -142,10 +144,10 @@ pub struct Dav1dSettings {
142144

143145
#[repr(C)]
144146
pub(crate) struct Rav1dSettings {
145-
pub n_threads: c_int,
146-
pub max_frame_delay: c_int,
147+
pub n_threads: InRange<u16, 0, 256>,
148+
pub max_frame_delay: InRange<u16, 0, 256>,
147149
pub apply_grain: bool,
148-
pub operating_point: u8,
150+
pub operating_point: InRange<u8, 0, 31>,
149151
pub all_layers: bool,
150152
pub frame_size_limit: c_uint,
151153
pub allocator: Rav1dPicAllocator,
@@ -175,11 +177,20 @@ impl TryFrom<Dav1dSettings> for Rav1dSettings {
175177
decode_frame_type,
176178
reserved: _,
177179
} = value;
180+
validate_input!(((0..=256).contains(&n_threads), Rav1dError::InvalidArgument))?;
181+
validate_input!((
182+
(0..=256).contains(&max_frame_delay),
183+
Rav1dError::InvalidArgument
184+
))?;
185+
validate_input!((
186+
(0..32).contains(&operating_point),
187+
Rav1dError::InvalidArgument
188+
))?;
178189
Ok(Self {
179-
n_threads,
180-
max_frame_delay,
190+
n_threads: InRange::new(n_threads.try_into().unwrap()).unwrap(),
191+
max_frame_delay: InRange::new(max_frame_delay.try_into().unwrap()).unwrap(),
181192
apply_grain: apply_grain != 0,
182-
operating_point: operating_point.try_into().unwrap(),
193+
operating_point: InRange::new(operating_point.try_into().unwrap()).unwrap(),
183194
all_layers: all_layers != 0,
184195
frame_size_limit,
185196
allocator: allocator.try_into()?,
@@ -209,10 +220,10 @@ impl From<Rav1dSettings> for Dav1dSettings {
209220
decode_frame_type,
210221
} = value;
211222
Self {
212-
n_threads,
213-
max_frame_delay,
223+
n_threads: n_threads.get().into(),
224+
max_frame_delay: max_frame_delay.get().into(),
214225
apply_grain: apply_grain as c_int,
215-
operating_point: operating_point.into(),
226+
operating_point: operating_point.get().into(),
216227
all_layers: all_layers as c_int,
217228
frame_size_limit,
218229
allocator: allocator.into(),

src/lib.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ use crate::decode::rav1d_decode_frame_exit;
161161
pub use crate::error::Dav1dResult;
162162
use crate::error::{Rav1dError, Rav1dResult};
163163
use crate::extensions::OptionError as _;
164+
use crate::in_range::InRange;
164165
#[cfg(feature = "bitdepth_16")]
165166
use crate::include::common::bitdepth::BitDepth16;
166167
#[cfg(feature = "bitdepth_8")]
@@ -229,10 +230,10 @@ pub extern "C" fn dav1d_version_api() -> c_uint {
229230
impl Default for Rav1dSettings {
230231
fn default() -> Self {
231232
Self {
232-
n_threads: 0,
233-
max_frame_delay: 0,
233+
n_threads: InRange::<u16, 0, 256>::new(0).unwrap(),
234+
max_frame_delay: InRange::<u16, 0, 256>::new(0).unwrap(),
234235
apply_grain: true,
235-
operating_point: 0,
236+
operating_point: InRange::<u8, 0, 31>::new(0).unwrap(),
236237
all_layers: true,
237238
frame_size_limit: 0,
238239
allocator: Default::default(),
@@ -264,13 +265,13 @@ struct NumThreads {
264265

265266
#[cold]
266267
fn get_num_threads(s: &Rav1dSettings) -> NumThreads {
267-
let n_tc = if s.n_threads != 0 {
268-
s.n_threads as usize
268+
let n_tc = if s.n_threads.get() != 0 {
269+
s.n_threads.get() as usize // TODO propagate `InRange`
269270
} else {
270271
rav1d_num_logical_processors().get().clamp(1, 256)
271272
};
272-
let n_fc = if s.max_frame_delay != 0 {
273-
cmp::min(s.max_frame_delay as usize, n_tc)
273+
let n_fc = if s.max_frame_delay.get() != 0 {
274+
cmp::min(s.max_frame_delay.get() as usize, n_tc) // TODO propagate `InRange`
274275
} else {
275276
cmp::min((n_tc as f64).sqrt().ceil() as usize, 8)
276277
};
@@ -279,14 +280,6 @@ fn get_num_threads(s: &Rav1dSettings) -> NumThreads {
279280

280281
#[cold]
281282
pub(crate) fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult<usize> {
282-
validate_input!((
283-
s.n_threads >= 0 && s.n_threads <= 256,
284-
Rav1dError::InvalidArgument
285-
))?;
286-
validate_input!((
287-
s.max_frame_delay >= 0 && s.max_frame_delay <= 256,
288-
Rav1dError::InvalidArgument
289-
))?;
290283
let NumThreads { n_tc: _, n_fc } = get_num_threads(s);
291284
Ok(n_fc)
292285
}
@@ -312,15 +305,6 @@ pub(crate) fn rav1d_open(s: &Rav1dSettings) -> Rav1dResult<Arc<Rav1dContext>> {
312305
static INITTED: Once = Once::new();
313306
INITTED.call_once(|| init_internal());
314307

315-
validate_input!((
316-
s.n_threads >= 0 && s.n_threads <= 256,
317-
Rav1dError::InvalidArgument
318-
))?;
319-
validate_input!((
320-
s.max_frame_delay >= 0 && s.max_frame_delay <= 256,
321-
Rav1dError::InvalidArgument
322-
))?;
323-
validate_input!((s.operating_point <= 31, Rav1dError::InvalidArgument))?;
324308
validate_input!((
325309
!s.allocator.is_default() || s.allocator.cookie.is_none(),
326310
Rav1dError::InvalidArgument
@@ -405,7 +389,7 @@ pub(crate) fn rav1d_open(s: &Rav1dSettings) -> Rav1dResult<Arc<Rav1dContext>> {
405389
allocator: s.allocator.clone(),
406390
logger: s.logger.clone(),
407391
apply_grain: s.apply_grain,
408-
operating_point: s.operating_point,
392+
operating_point: s.operating_point.get(), // TODO propagate `InRange`
409393
all_layers: s.all_layers,
410394
frame_size_limit,
411395
strict_std_compliance: s.strict_std_compliance,

0 commit comments

Comments
 (0)