From 5c69ccaf76eb74b6c11726bed2e63568efa2c5ac Mon Sep 17 00:00:00 2001
From: Luca Barbato <lu_zero@gentoo.org>
Date: Sat, 29 Sep 2018 03:10:18 +0200
Subject: [PATCH] Move cli-only arguments out

---
 src/bin/common.rs   | 159 +++++++++++++++++++++-----------------------
 src/bin/rav1e.rs    |   4 +-
 src/bin/rav1repl.rs |   2 +-
 src/encoder.rs      |   2 -
 4 files changed, 77 insertions(+), 90 deletions(-)

diff --git a/src/bin/common.rs b/src/bin/common.rs
index 75dd0d6a..22c571e0 100644
--- a/src/bin/common.rs
+++ b/src/bin/common.rs
@@ -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.
diff --git a/src/bin/rav1e.rs b/src/bin/rav1e.rs
index fc8927a9..5d4f1498 100644
--- a/src/bin/rav1e.rs
+++ b/src/bin/rav1e.rs
@@ -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;
     }
 
diff --git a/src/bin/rav1repl.rs b/src/bin/rav1repl.rs
index 230805e9..fb2f7fb3 100644
--- a/src/bin/rav1repl.rs
+++ b/src/bin/rav1repl.rs
@@ -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();
diff --git a/src/encoder.rs b/src/encoder.rs
index b680cad3..0e2734ec 100644
--- a/src/encoder.rs
+++ b/src/encoder.rs
@@ -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,
-- 
GitLab