Commit 5c69ccaf authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Move cli-only arguments out

parent f1f1025d
......@@ -13,93 +13,82 @@ pub struct EncoderIO {
pub rec: Option<Box<dyn Write>>
}
pub trait FromCli {
fn from_cli() -> (EncoderIO, EncoderConfig);
}
pub fn parse_cli() -> (EncoderIO, EncoderConfig, usize) {
let matches = App::new("rav1e")
.version("0.1.0")
.about("AV1 video encoder")
.arg(
Arg::with_name("INPUT")
.help("Uncompressed YUV4MPEG2 video input")
.required(true)
.index(1)
).arg(
Arg::with_name("OUTPUT")
.help("Compressed AV1 in IVF video output")
.short("o")
.long("output")
.required(true)
.takes_value(true)
).arg(Arg::with_name("RECONSTRUCTION").short("r").takes_value(true))
.arg(
Arg::with_name("LIMIT")
.help("Maximum number of frames to encode")
.short("l")
.long("limit")
.takes_value(true)
.default_value("0")
).arg(
Arg::with_name("QP")
.help("Quantizer (0-255)")
.long("quantizer")
.takes_value(true)
.default_value("100")
).arg(
Arg::with_name("SPEED")
.help("Speed level (0(slow)-10(fast))")
.short("s")
.long("speed")
.takes_value(true)
.default_value("3")
).arg(
Arg::with_name("TUNE")
.help("Quality tuning (Will enforce partition sizes >= 8x8)")
.long("tune")
.possible_values(&Tune::variants())
.default_value("psnr")
.case_insensitive(true)
).get_matches();
let io = EncoderIO {
input: match matches.value_of("INPUT").unwrap() {
"-" => Box::new(io::stdin()) as Box<dyn Read>,
f => Box::new(File::open(&f).unwrap()) as Box<dyn Read>
},
output: match matches.value_of("OUTPUT").unwrap() {
"-" => Box::new(io::stdout()) as Box<dyn Write>,
f => Box::new(File::create(&f).unwrap()) as Box<dyn Write>
},
rec: matches
.value_of("RECONSTRUCTION")
.map(|f| Box::new(File::create(&f).unwrap()) as Box<dyn Write>)
};
let config = EncoderConfig {
quantizer: matches.value_of("QP").unwrap().parse().unwrap(),
speed: matches.value_of("SPEED").unwrap().parse().unwrap(),
tune: matches.value_of("TUNE").unwrap().parse().unwrap()
};
// Validate arguments
if config.quantizer == 0 {
unimplemented!();
} else if config.quantizer > 255 || config.speed > 10 {
panic!("argument out of range");
}
impl FromCli for EncoderConfig {
fn from_cli() -> (EncoderIO, EncoderConfig) {
let matches = App::new("rav1e")
.version("0.1.0")
.about("AV1 video encoder")
.arg(
Arg::with_name("INPUT")
.help("Uncompressed YUV4MPEG2 video input")
.required(true)
.index(1)
)
.arg(
Arg::with_name("OUTPUT")
.help("Compressed AV1 in IVF video output")
.short("o")
.long("output")
.required(true)
.takes_value(true)
)
.arg(Arg::with_name("RECONSTRUCTION").short("r").takes_value(true))
.arg(
Arg::with_name("LIMIT")
.help("Maximum number of frames to encode")
.short("l")
.long("limit")
.takes_value(true)
.default_value("0")
)
.arg(
Arg::with_name("QP")
.help("Quantizer (0-255)")
.long("quantizer")
.takes_value(true)
.default_value("100")
)
.arg(
Arg::with_name("SPEED")
.help("Speed level (0(slow)-10(fast))")
.short("s")
.long("speed")
.takes_value(true)
.default_value("3")
)
.arg(
Arg::with_name("TUNE")
.help("Quality tuning (Will enforce partition sizes >= 8x8)")
.long("tune")
.possible_values(&Tune::variants())
.default_value("psnr")
.case_insensitive(true)
)
.get_matches();
let io = EncoderIO {
input: match matches.value_of("INPUT").unwrap() {
"-" => Box::new(io::stdin()) as Box<dyn Read>,
f => Box::new(File::open(&f).unwrap()) as Box<dyn Read>
},
output: match matches.value_of("OUTPUT").unwrap() {
"-" => Box::new(io::stdout()) as Box<dyn Write>,
f => Box::new(File::create(&f).unwrap()) as Box<dyn Write>
},
rec: matches
.value_of("RECONSTRUCTION")
.map(|f| Box::new(File::create(&f).unwrap()) as Box<dyn Write>)
};
let config = EncoderConfig {
limit: matches.value_of("LIMIT").unwrap().parse().unwrap(),
quantizer: matches.value_of("QP").unwrap().parse().unwrap(),
speed: matches.value_of("SPEED").unwrap().parse().unwrap(),
tune: matches.value_of("TUNE").unwrap().parse().unwrap()
};
// Validate arguments
if config.quantizer == 0 {
unimplemented!();
} else if config.quantizer > 255 || config.speed > 10 {
panic!("argument out of range");
}
let limit = matches.value_of("LIMIT").unwrap().parse().unwrap();
(io, config)
}
(io, config, limit)
}
/// Encode and write a frame.
......
......@@ -17,7 +17,7 @@ use common::*;
use rav1e::*;
fn main() {
let (mut io, enc) = EncoderConfig::from_cli();
let (mut io, enc, limit) = parse_cli();
let mut y4m_dec = y4m::decode(&mut io.input).unwrap();
let width = y4m_dec.get_width();
let height = y4m_dec.get_height();
......@@ -80,7 +80,7 @@ fn main() {
count += 1;
if enc.limit != 0 && count >= enc.limit {
if limit != 0 && count >= limit {
break;
}
......
......@@ -22,7 +22,7 @@ use rustyline::error::ReadlineError;
use rustyline::Editor;
fn main() {
let (mut io, enc) = EncoderConfig::from_cli();
let (mut io, enc, _) = parse_cli();
let mut y4m_dec = y4m::decode(&mut io.input).unwrap();
let width = y4m_dec.get_width();
let height = y4m_dec.get_height();
......
......@@ -477,7 +477,6 @@ impl fmt::Display for FrameType{
#[derive(Copy, Clone, Debug)]
pub struct EncoderConfig {
pub limit: u64,
pub quantizer: usize,
pub speed: usize,
pub tune: Tune
......@@ -486,7 +485,6 @@ pub struct EncoderConfig {
impl Default for EncoderConfig {
fn default() -> Self {
EncoderConfig {
limit: 0,
quantizer: 100,
speed: 0,
tune: Tune::Psnr,
......
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