Commit cc1d494f authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Make the API threads-aware

parent 3450a2e1
......@@ -437,7 +437,9 @@ pub struct ContentLight {
/// Contain all the encoder configuration
#[derive(Clone, Debug)]
pub struct Config {
pub enc: EncoderConfig
pub enc: EncoderConfig,
/// The number of threads in the threadpool.
pub threads: usize
}
impl Config {
......@@ -449,6 +451,7 @@ impl Config {
"quantizer" => self.enc.quantizer = value.parse().map_err(|_e| ParseError)?,
"speed" => self.enc.speed_settings = SpeedSettings::from_preset(value.parse().map_err(|_e| ParseError)?),
"tune" => self.enc.tune = value.parse().map_err(|_e| ParseError)?,
"threads" => self.threads = value.parse().map_err(|_e| ParseError)?,
_ => return Err(InvalidKey)
}
......@@ -465,6 +468,8 @@ impl Config {
None
};
let pool = rayon::ThreadPoolBuilder::new().num_threads(self.threads).build().unwrap();
Context {
frame_count: 0,
limit: 0,
......@@ -474,6 +479,7 @@ impl Config {
frame_data: BTreeMap::new(),
keyframes: BTreeSet::new(),
packet_data,
pool,
segment_start_idx: 0,
segment_start_frame: 0,
keyframe_detector: SceneChangeDetector::new(self.enc.bit_depth),
......@@ -517,6 +523,7 @@ pub struct Context<T: Pixel> {
rc_state: RCState,
maybe_prev_log_base_q: Option<i64>,
pub first_pass_data: FirstPassData,
pool: rayon::ThreadPool,
}
#[derive(Clone, Copy, Debug)]
......@@ -754,7 +761,7 @@ impl<T: Pixel> Context<T> {
// TODO: Record the bits spent here against the original frame for rate
// control purposes, or add a new frame subtype?
let sef_data = encode_frame(fi, &mut fs);
let sef_data = self.pool.install(|| encode_frame(fi, &mut fs));
self.packet_data.extend(sef_data);
let rec = if fi.show_frame { Some(fs.rec) } else { None };
......@@ -773,7 +780,7 @@ impl<T: Pixel> Context<T> {
let mut fs = FrameState::new_with_frame(fi, frame.clone());
// TODO: Trial encoding for first frame of each type.
let data = encode_frame(fi, &mut fs);
let data = self.pool.install(||encode_frame(fi, &mut fs));
self.maybe_prev_log_base_q = Some(qps.log_base_q);
// TODO: Add support for dropping frames.
self.rc_state.update_state(
......
......@@ -30,6 +30,7 @@ pub struct CliOptions {
pub limit: usize,
pub skip: usize,
pub verbose: bool,
pub threads: usize,
}
pub fn parse_cli() -> CliOptions {
......@@ -268,9 +269,7 @@ pub fn parse_cli() -> CliOptions {
std::process::exit(0);
}
if let Some(threads) = matches.value_of("THREADS").map(|v| v.parse().expect("Threads must be an integer")) {
rayon::ThreadPoolBuilder::new().num_threads(threads).build_global().unwrap();
}
let threads = matches.value_of("THREADS").map(|v| v.parse().expect("Threads must be an integer")).unwrap();
if let Some(matches) = matches.subcommand_matches("advanced") {
if let Some(shell) = matches.value_of("SHELL").map(|v| v.parse().unwrap()) {
......@@ -299,6 +298,7 @@ pub fn parse_cli() -> CliOptions {
limit: matches.value_of("LIMIT").unwrap().parse().unwrap(),
skip: matches.value_of("SKIP").unwrap().parse().unwrap(),
verbose: matches.is_present("VERBOSE"),
threads,
}
}
......
......@@ -150,7 +150,8 @@ fn main() {
cli.enc.chroma_sample_position = video_info.chroma_sample_position;
cli.enc.time_base = video_info.time_base;
let cfg = Config {
enc: cli.enc
enc: cli.enc,
threads: cli.threads,
};
let stderr = io::stderr();
......
......@@ -128,7 +128,8 @@ pub(crate) fn setup_encoder<T: Pixel>(
enc.bitrate = bitrate;
let cfg = Config {
enc
enc,
threads: 0,
};
cfg.new_context()
......@@ -352,4 +353,4 @@ fn get_decoder<T: Pixel>(decoder: &str, w: usize, h: usize) -> Box<dyn TestDecod
"dav1d" => Box::new(Dav1dDecoder::<T>::setup_decoder(w, h)),
_ => unimplemented!()
}
}
\ No newline at end of file
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment