use clap::Parser;
use tracing_subscriber::EnvFilter;
mod cli;
mod file_mode;
mod progress;
mod stream_mode;
mod y4m_format;
use cli::{Args, Command, InputSource, RunOptions, run_list_devices};
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
const DEFAULT_WORKERS: usize = 2;
fn run_input(opts: &RunOptions, input: &InputSource, workers: Option<usize>) -> Result<(), anyhow::Error> {
match input {
InputSource::File(path) => file_mode::run_file(opts, path, workers.unwrap_or(DEFAULT_WORKERS)),
stream @ (InputSource::Stdin | InputSource::Fd(_)) => {
if workers.is_some() {
tracing::warn!("--workers is ignored for piped input, which cannot be split by scene");
}
tracing::info!(input = %stream, "reading a y4m stream");
stream_mode::run_stream(&opts.planes, stream.open_reader()?)
},
}
}
fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_MIN_STACK").is_none() {
unsafe { std::env::set_var("RUST_MIN_STACK", "16777216") };
}
let args = Args::parse();
if std::env::var("RUST_LOG").is_err() {
let default = match args.command {
Command::ListDevices => "warn",
_ => "info",
};
unsafe { std::env::set_var("RUST_LOG", default) };
}
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_writer(progress::tracing_writer())
.init();
if matches!(args.command, Command::ListDevices) {
print!("{}", run_list_devices(&args.accelerators));
return Ok(());
}
match av_denoise::install_compilation_cache() {
Ok(Some(path)) => tracing::info!(?path, "caching compiled kernels"),
Ok(None) => tracing::info!(
"kernel caching is off, every run recompiles. Unset {} to turn it back on.",
av_denoise::COMPILATION_CACHE_ENV,
),
Err(_) => anyhow::bail!("unable to install the kernel cache, this is a bug."),
}
let (opts, input, workers) = match &args.command {
Command::Nlmeans(nlm) => (nlm.build_options(&args)?, &nlm.common.input, nlm.common.workers),
Command::Nl4d(nl4d) => (
nl4d.build_options(&args)?,
&nl4d.common.input,
nl4d.common.workers,
),
Command::ListDevices => unreachable!(),
};
run_input(&opts, input, workers)
}