support scale options
[nihav-encoder.git] / src / main.rs
index 6b9086f08913a4a15023ffcdb51fb31342c7185c..0a36cc88da1c09579c9ec8da73ee75e675ce04c0 100644 (file)
@@ -56,6 +56,7 @@ enum OutputConvert {
     None,
 }
 
+#[allow(clippy::large_enum_variant)]
 enum OutputMode {
     Drop,
     Copy(u32),
@@ -63,6 +64,7 @@ enum OutputMode {
 }
 
 #[derive(Default)]
+#[allow(clippy::type_complexity)]
 struct Transcoder {
     input_name:     String,
     input_fmt:      Option<String>,
@@ -72,6 +74,7 @@ struct Transcoder {
     mux_opts:       Vec<OptionArgs>,
     istr_opts:      Vec<InputStreamOptions>,
     ostr_opts:      Vec<OutputStreamOptions>,
+    scale_opts:     Vec<(String, String)>,
     decoders:       Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>, Box<dyn FrameReorderer>)>>,
     encoders:       Vec<OutputMode>,
     no_video:       bool,
@@ -87,14 +90,18 @@ macro_rules! parse_and_apply_options {
         for opt in $in_opts.iter() {
             let mut found = false;
             for opt_def in opt_def.iter() {
-                if opt.name == opt_def.name {
+                let mut matches = opt.name == opt_def.name;
+                if !matches && opt.name.starts_with("no") {
+                    let (_, name) = opt.name.split_at(2);
+                    matches = name == opt_def.name;
+                }
+                if matches {
                     let arg = if let Some(ref str) = opt.value { Some(str) } else { None };
                     let ret = opt_def.parse(&opt.name, arg);
-                    if ret.is_err() {
-                        println!("invalid option {} for {}", opt.name, $name);
-                    } else {
-                        let (val, _) = ret.unwrap();
+                    if let Ok((val, _)) = ret {
                         opts.push(val);
+                    } else {
+                        println!("invalid option {} for {}", opt.name, $name);
                     }
                     found = true;
                 }
@@ -399,7 +406,21 @@ impl Transcoder {
         }
         true
     }
-    fn apply_decoder_options(&self, dec: &mut NADecoder, str_id: u32) {
+    fn parse_scale_options(&mut self, opts: &str) -> bool {
+        for opt in opts.split(',') {
+            let oval: Vec<_> = opt.split('=').collect();
+            if oval.len() == 1 {
+                self.scale_opts.push((oval[0].to_string(), "".to_string()));
+            } else if oval.len() == 2 {
+                self.scale_opts.push((oval[0].to_string(), oval[1].to_string()));
+            } else {
+                println!("unrecognized option '{}'", opt);
+                return false;
+            }
+        }
+        true
+    }
+    fn apply_decoder_options(&self, dec: &mut dyn NADecoder, str_id: u32) {
         if let Some(str_idx) = self.istr_opts.iter().position(|str| str.id == str_id) {
             let dec_opts = dec.get_supported_options();
             if dec_opts.is_empty() { return; }
@@ -419,7 +440,7 @@ impl Transcoder {
         if let Some(str_idx) = self.ostr_opts.iter().position(|str| str.id == out_id) {
             let oopts = &mut self.ostr_opts[str_idx];
             if oopts.enc_name.as_str() == "copy" && (cname == "any" || istr.get_info().get_name() == cname) {
-                out_sm.add_stream_ref(istr.clone());
+                out_sm.add_stream_ref(istr);
                 self.encoders.push(OutputMode::Copy(out_id));
             } else if cname == "any" || oopts.enc_name.as_str() == cname {
                 let enc_create = enc_reg.find_encoder(oopts.enc_name.as_str());
@@ -450,7 +471,7 @@ impl Transcoder {
                                 OutputConvert::None
                             } else {
                                 let ofmt = ScaleInfo { fmt: dvinfo.format, width: dvinfo.width, height: dvinfo.height };
-                                let ret = NAScale::new(ofmt, ofmt);
+                                let ret = NAScale::new_with_options(ofmt, ofmt, &self.scale_opts);
                                 if ret.is_err() {
                                     println!("cannot create scaler");
                                     return false;
@@ -498,15 +519,13 @@ println!("can't generate default channel map for {} channels", dainfo.channels);
 println!("encoder {} is not supported by output (expected {})", istr.id, istr.get_info().get_name());
                 return false;
             }
+        } else if cname == "any" || istr.get_info().get_name() == cname {
+            out_sm.add_stream_ref(istr);
+            self.encoders.push(OutputMode::Copy(out_id));
         } else {
-            if cname == "any" || istr.get_info().get_name() == cname {
-                out_sm.add_stream_ref(istr.clone());
-                self.encoders.push(OutputMode::Copy(out_id));
-            } else {
 println!("stream {} ({}) can't be handled", istr.id, istr.get_info().get_name());
 // todo autoselect encoder?
-                return false;
-            }
+            return false;
         }
         true
     }
@@ -603,7 +622,7 @@ println!("stream {} ({}) can't be handled", istr.id, istr.get_info().get_name())
     }
 }
 
-fn encode_frame(dst_id: u32, encoder: &mut Box<NAEncoder>, cvt: &mut OutputConvert, frm: NAFrameRef) -> bool {
+fn encode_frame(dst_id: u32, encoder: &mut Box<dyn NAEncoder>, cvt: &mut OutputConvert, frm: NAFrameRef, scale_opts: &[(String, String)]) -> bool {
     let buf = frm.get_buffer();
     let cbuf = match cvt {
             OutputConvert::None => buf,
@@ -612,7 +631,7 @@ fn encode_frame(dst_id: u32, encoder: &mut Box<NAEncoder>, cvt: &mut OutputConve
                 let last_ifmt = scaler.get_in_fmt();
                 if cur_ifmt != last_ifmt {
                     let ofmt = scaler.get_out_fmt();
-                    let ret = NAScale::new(cur_ifmt, ofmt);
+                    let ret = NAScale::new_with_options(cur_ifmt, ofmt, scale_opts);
                     if ret.is_err() {
                         println!("error re-initialising scaler for {} -> {}", cur_ifmt, ofmt);
                         return false;
@@ -649,6 +668,7 @@ macro_rules! next_arg {
     }
 }
 
+#[allow(clippy::single_match)]
 fn main() {
     let args: Vec<_> = env::args().collect();
 
@@ -666,6 +686,7 @@ fn main() {
         println!("  --input inputfile           - set input file");
         println!("  --input-format fmt          - force input format");
         println!("  --demuxer-options options   - set input demuxer options");
+        println!("  --scale-options options     - set scaler options");
         println!("  --output outputfile         - set output file");
         println!("  --output-format fmt         - force output format");
         println!("  --muxer-options options     - set output muxer options");
@@ -816,6 +837,13 @@ fn main() {
                     return;
                 }
             },
+            "--scale-options" => {
+                next_arg!(args, arg_idx);
+                if !transcoder.parse_scale_options(&args[arg_idx]) {
+                    println!("invalid scale option syntax");
+                    return;
+                }
+            },
             "--no-video" | "-vn" => {
                 transcoder.no_video = true;
             },
@@ -878,11 +906,11 @@ fn main() {
         return;
     }
 
-    if transcoder.input_name.len() == 0 {
+    if transcoder.input_name.is_empty() {
         println!("no input name provided");
         return;
     }
-    if transcoder.output_name.len() == 0 {
+    if transcoder.output_name.is_empty() {
         println!("no output name provided");
         return;
     }
@@ -898,14 +926,12 @@ fn main() {
 
     let dmx_name = if let Some(ref str) = transcoder.input_fmt {
             str.as_str()
+        } else if let Some((dmx_name, score)) = detect::detect_format(transcoder.input_name.as_str(), &mut br) {
+            println!("detected {} with score {:?}", dmx_name, score);
+            dmx_name
         } else {
-            if let Some((dmx_name, score)) = detect::detect_format(transcoder.input_name.as_str(), &mut br) {
-                println!("detected {} with score {:?}", dmx_name, score);
-                dmx_name
-            } else {
-                println!("cannot detect input format");
-                return;
-            }
+            println!("cannot detect input format");
+            return;
         };
     let ret = dmx_reg.find_demuxer(dmx_name);
     if ret.is_none() {
@@ -954,13 +980,11 @@ println!("stream {} - {} {}", i, s, info.get_name());
 
     let output_fmt = if let Some(ref str) = transcoder.output_fmt {
             str
+        } else if let Some(str) = detect::detect_format_by_name(transcoder.output_name.as_str()) {
+            str
         } else {
-            if let Some(str) = detect::detect_format_by_name(transcoder.output_name.as_str()) {
-                str
-            } else {
-                println!("Cannot guess muxer for output");
-                return;
-            }
+            println!("Cannot guess muxer for output");
+            return;
         };
     let ret = mux_reg.find_muxer(output_fmt);
     let ofmt = output_fmt.to_string();
@@ -1031,7 +1055,7 @@ println!("stream {} - {} {}", i, s, info.get_name());
                     let frm = ret.unwrap();
                     reorderer.add_frame(frm);
                     while let Some(frm) = reorderer.get_frame() {
-                        if !encode_frame(dst_id, encoder, cvt, frm) {
+                        if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) {
                             break;
                         }
                         while let Ok(Some(pkt)) = encoder.get_packet() {
@@ -1051,7 +1075,7 @@ println!("stream {} - {} {}", i, s, info.get_name());
         if let OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) = transcoder.encoders[src_id] {
             if let Some((_, _, ref mut reorderer)) = transcoder.decoders[src_id] {
                 while let Some(frm) = reorderer.get_last_frames() {
-                    if !encode_frame(dst_id, encoder, cvt, frm) {
+                    if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) {
                         break;
                     }
                     while let Ok(Some(pkt)) = encoder.get_packet() {