diff --git a/src/cpu.rs b/src/cpu.rs index ef7353640..ed30728dc 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -1,7 +1,5 @@ use crate::src::const_fn::const_for; -use crate::src::internal::Rav1dContext; use bitflags::bitflags; -use std::ffi::c_int; use std::ffi::c_uint; use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; @@ -208,6 +206,6 @@ pub extern "C" fn dav1d_set_cpu_flags_mask(mask: c_uint) { } #[cold] -pub(crate) fn rav1d_num_logical_processors(_c: *mut Rav1dContext) -> c_int { - num_cpus::get() as c_int +pub(crate) fn rav1d_num_logical_processors() -> usize { + num_cpus::get() } diff --git a/src/lib.rs b/src/lib.rs index 386f309d2..4d652829a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ use crate::include::common::bitdepth::DynCoef; -use crate::include::common::intops::iclip; use crate::include::common::validate::validate_input; use crate::include::dav1d::common::Dav1dDataProps; use crate::include::dav1d::common::Rav1dDataProps; @@ -204,40 +203,31 @@ unsafe fn get_stack_size_internal(_thread_attr: *const pthread_attr_t) -> usize return 0; } +struct NumThreads { + n_tc: usize, + n_fc: usize, +} + #[cold] -unsafe fn get_num_threads( - c: *mut Rav1dContext, - s: *const Rav1dSettings, - n_tc: *mut c_uint, - n_fc: *mut c_uint, -) { - static fc_lut: [u8; 49] = [ - 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - ]; - *n_tc = (if (*s).n_threads != 0 { - (*s).n_threads +fn get_num_threads(s: &Rav1dSettings) -> NumThreads { + let n_tc = if s.n_threads != 0 { + s.n_threads as usize } else { - iclip(rav1d_num_logical_processors(c), 1 as c_int, 256 as c_int) - }) as c_uint; - *n_fc = if (*s).max_frame_delay != 0 { - cmp::min((*s).max_frame_delay as c_uint, *n_tc) + rav1d_num_logical_processors().clamp(1, 256) + }; + let n_fc = if s.max_frame_delay != 0 { + cmp::min(s.max_frame_delay as usize, n_tc) } else { - (if *n_tc < 50 as c_uint { - fc_lut[(*n_tc).wrapping_sub(1 as c_int as c_uint) as usize] as c_int - } else { - 8 as c_int - }) as c_uint + cmp::min((n_tc as f64).sqrt().ceil() as usize, 8) }; + NumThreads { n_fc, n_tc } } #[cold] -pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult { - let mut n_tc: c_uint = 0; - let mut n_fc: c_uint = 0; +pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult { validate_input!((s.n_threads >= 0 && s.n_threads <= 256, EINVAL))?; validate_input!((s.max_frame_delay >= 0 && s.max_frame_delay <= 256, EINVAL))?; - get_num_threads(0 as *mut Rav1dContext, s, &mut n_tc, &mut n_fc); + let NumThreads { n_tc: _, n_fc } = get_num_threads(s); Ok(n_fc) } @@ -246,7 +236,7 @@ pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult Dav1dResult { (|| { validate_input!((!s.is_null(), EINVAL))?; - rav1d_get_frame_delay(&s.read().into()) + rav1d_get_frame_delay(&s.read().into()).map(|frame_delay| frame_delay as c_uint) })() .into() } @@ -342,7 +332,9 @@ pub(crate) unsafe fn rav1d_open(c_out: &mut *mut Rav1dContext, s: &Rav1dSettings } (*c).flush = &mut (*c).flush_mem; *(*c).flush = 0 as c_int; - get_num_threads(c, s, &mut (*c).n_tc, &mut (*c).n_fc); + let NumThreads { n_tc, n_fc } = get_num_threads(s); + (*c).n_tc = n_tc as c_uint; + (*c).n_fc = n_fc as c_uint; (*c).fc = rav1d_alloc_aligned( ::core::mem::size_of::().wrapping_mul((*c).n_fc as usize), 32 as c_int as usize,