add option for not stopping on the first decoding error
[nihav-tool.git] / src / main.rs
index 784fccb8382db37051eb71d7b069b19ca6f40169..bb94c1218432969bcebead768e75deb3fbaa6b68 100644 (file)
@@ -1,4 +1,5 @@
 extern crate nihav_core;
+extern crate nihav_registry;
 extern crate nihav_allstuff;
 
 use std::io::SeekFrom;
@@ -8,7 +9,7 @@ use nihav_core::io::byteio::{FileReader, ByteReader};
 use nihav_core::frame::*;
 use nihav_core::codecs::*;
 use nihav_core::demuxers::*;
-use nihav_core::detect;
+use nihav_registry::detect;
 use nihav_allstuff::*;
 use std::env;
 
@@ -26,7 +27,7 @@ enum NumberMode {
 }
 
 struct FrameOutput {
-    prefix:     &'static str,
+    prefix:     String,
     streamno:   usize,
     frameno:    u64,
     nmode:      NumberMode,
@@ -42,11 +43,11 @@ impl FrameOutput {
                 };
             let vinfo = frm.get_buffer().get_video_info().unwrap();
             if vinfo.get_format().is_paletted() {
-                write_palppm(self.prefix, self.streamno, pts, frm);
+                write_palppm(&self.prefix, self.streamno, pts, frm);
             } else if vinfo.get_format().get_model().is_yuv() {
-                write_pgmyuv(self.prefix, self.streamno, pts, frm);
+                write_pgmyuv(&self.prefix, self.streamno, pts, frm);
             } else if vinfo.get_format().get_model().is_rgb() {
-                write_ppm(self.prefix, self.streamno, pts, frm);
+                write_ppm(&self.prefix, self.streamno, pts, frm);
             } else {
 panic!(" unknown format");
             }
@@ -91,6 +92,9 @@ fn main() {
     let mut decode_audio = true;
     let mut nmode = NumberMode::FrmPTS;
     let mut seek_time = 0u64;
+    let mut vpfx: Option<String> = None;
+    let mut apfx: Option<&str> = None;
+    let mut ignore_errors = false;
 
     while (cur_arg < args.len()) && args[cur_arg].starts_with('-') {
         match args[cur_arg].as_str() {
@@ -114,6 +118,23 @@ fn main() {
                 }
                 seek_time = ret.unwrap();
             },
+            "-apfx"     => {
+                cur_arg += 1;
+                if cur_arg == args.len() {
+                    println!("name missing");
+                    return;
+                }
+                apfx = Some(&args[cur_arg]);
+            },
+            "-vpfx"     => {
+                cur_arg += 1;
+                if cur_arg == args.len() {
+                    println!("name missing");
+                    return;
+                }
+                vpfx = Some(args[cur_arg].clone());
+            },
+            "-ignerr"   => { ignore_errors = true; },
             _           => { println!("unknown option {}", args[cur_arg]); return; },
         }
         cur_arg += 1;
@@ -175,7 +196,7 @@ println!("stream {} - {} {}", i, s, info.get_name());
                 dec.init(&mut dsupp, info).unwrap();
                 decs.push(Some((dsupp, dec)));
                 if !noout {
-                    writers.push(Outputter::Video(FrameOutput{prefix: "", streamno: i, frameno: 1, nmode}));
+                    writers.push(Outputter::Video(FrameOutput{prefix: if let Some(ref str) = vpfx { str.clone() } else { "out".to_string() }, streamno: i, frameno: 1, nmode}));
                     has_out = true;
                 }
             } else {
@@ -192,7 +213,11 @@ println!("stream {} - {} {}", i, s, info.get_name());
                 dec.init(&mut dsupp, info).unwrap();
                 decs.push(Some((dsupp, dec)));
                 if !noout {
-                    let name = format!("out{:02}.wav", i);
+                    let name = if let Some(apfx) = apfx {
+                            format!("{}{:02}.wav", apfx, i)
+                        } else {
+                            format!("out{:02}.wav", i)
+                        };
                     writers.push(Outputter::Audio(AudioOutput::new(&name)));
                     has_out = true;
                 }
@@ -218,16 +243,28 @@ panic!("decoder {} not found", info.get_name());
         let sr = sids.iter().position(|x| *x == streamno);
         let idx = sr.unwrap();
         if let Some((ref mut dsupp, ref mut dec)) = decs[idx] {
-            let frm = dec.decode(dsupp, &pkt).unwrap();
-            if !noout {
-                match writers[idx] {
-                    Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); },
-                    Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); },
-                    _ => {},
-                };
-            }
+            match dec.decode(dsupp, &pkt) {
+                Ok(frm) => {
+                    if !noout {
+                        match writers[idx] {
+                            Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); },
+                            Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); },
+                            _ => {},
+                        };
+                    }
+                },
+                Err(DecoderError::MissingReference) if seek_time > 0 => {
+                    println!("ignoring missing ref");
+                },
+                Err(reason) => {
+                    println!("error decoding frame {:?}", reason);
+                    if !ignore_errors {
+                        break;
+                    }
+                },
+            };
+            if pkt.get_pts() != None && lastpts.is_some() && pkt.get_pts() >= lastpts { break; }
         }
-        if pkt.get_pts() != None && lastpts.is_some() && pkt.get_pts() >= lastpts { break; }
     }
 //panic!("end");
 }