]> git.nihav.org Git - nihav-tool.git/blame - src/main.rs
introduce various output frame numbering modes and some messages
[nihav-tool.git] / src / main.rs
CommitLineData
019f9f9c
KS
1extern crate nihav;
2
019f9f9c
KS
3use std::io::SeekFrom;
4use std::fs::File;
5use std::path::Path;
6use nihav::io::byteio::{FileReader, ByteReader};
7use nihav::frame::*;
8use nihav::codecs::*;
9use nihav::demuxers::*;
10use nihav::detect;
11use std::env;
12
3660c127
KS
13mod frmwriter;
14use frmwriter::*;
15
16mod wavwriter;
17use wavwriter::WavWriter;
019f9f9c 18
44939c84
KS
19#[derive(Clone,Copy,PartialEq)]
20enum NumberMode {
21 Counter,
22 PktPTS,
23 FrmPTS,
24}
25
3660c127
KS
26struct FrameOutput {
27 prefix: &'static str,
28 streamno: usize,
44939c84
KS
29 frameno: u64,
30 nmode: NumberMode,
3660c127 31}
019f9f9c 32
3660c127
KS
33impl FrameOutput {
34 fn output_frame(&mut self, pkt: &NAPacket, frmref: NAFrameRef) {
35 let frm = frmref.borrow();
36 if frm.get_frame_type() != FrameType::Skip {
44939c84
KS
37 let pts = match self.nmode {
38 NumberMode::Counter => { self.frameno },
39 NumberMode::PktPTS => { pkt.get_pts().unwrap() },
40 NumberMode::FrmPTS => { if let Some(pt) = frm.get_pts() { pt } else { pkt.get_pts().unwrap() } },
41 };
3660c127
KS
42 let frmbuf = frm.get_buffer().get_vbuf().unwrap();
43 if frmbuf.get_info().get_format().is_paletted() {
44 write_palppm(self.prefix, self.streamno, pts, frm);
45 } else {
46 write_pgmyuv(self.prefix, self.streamno, pts, frm);
47 }
48 }
44939c84 49 self.frameno += 1;
019f9f9c
KS
50 }
51}
52
3660c127
KS
53struct AudioOutput {
54 wwr: WavWriter<'static>,
55 wrote_header: bool,
56}
57
58impl AudioOutput {
59 fn new(name: &String) -> Self { Self { wwr: WavWriter::new(name), wrote_header: false } }
60 fn output_frame(&mut self, _pkt: &NAPacket, frm: NAFrameRef) {
61 if !self.wrote_header {
62 self.wwr.write_header(frm.borrow().get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap();
63 self.wrote_header = true;
019f9f9c 64 }
3660c127 65 self.wwr.write_frame(frm.borrow().get_buffer()).unwrap();
019f9f9c
KS
66 }
67}
68
3660c127
KS
69enum Outputter {
70 Video(FrameOutput),
71 Audio(AudioOutput),
72 None,
73}
74
019f9f9c
KS
75fn main() {
76 let args: Vec<_> = env::args().collect();
77
3660c127
KS
78 if args.len() == 1 {
79 println!("usage: nihav-tool [-noout] [-vn] [-an] input [lastpts]");
80 return;
81 }
82 let mut lastpts: Option<u64> = None;
83 let mut cur_arg: usize = 1;
84 let mut noout = false;
85 let mut decode_video = true;
86 let mut decode_audio = true;
44939c84 87 let mut nmode = NumberMode::FrmPTS;
3660c127
KS
88
89 while (cur_arg < args.len()) && (args[cur_arg].chars().next().unwrap() == '-') {
90 match args[cur_arg].as_str() {
91 "--" => { break; },
92 "-noout" => { noout = true; },
93 "-an" => { decode_audio = false; },
94 "-vn" => { decode_video = false; },
44939c84
KS
95 "-nm=count" => { nmode = NumberMode::Counter; },
96 "-nm=pktpts" => { nmode = NumberMode::PktPTS; },
97 "-nm=frmpts" => { nmode = NumberMode::FrmPTS; },
3660c127
KS
98 _ => { println!("unknown option {}", args[cur_arg]); return; },
99 }
100 cur_arg += 1;
101 }
102 let name = args[cur_arg].as_str();
103 cur_arg += 1;
3660c127
KS
104 if cur_arg < args.len() {
105 lastpts = Some(u64::from_str_radix(args[cur_arg].as_str(), 10).unwrap());
106 }
019f9f9c 107
019f9f9c
KS
108 let path = Path::new(name);
109 let mut file = File::open(path).unwrap();
110 let dmx_fact;
111 let mut fr = FileReader::new_read(&mut file);
112 let mut br = ByteReader::new(&mut fr);
113 let res = detect::detect_format(name, &mut br);
114 if res.is_none() {
115 println!("cannot detect format for {}", name);
116 return;
117 }
118 let (dmx_name, _) = res.unwrap();
3660c127 119println!("trying demuxer {} on {}", dmx_name, name);
019f9f9c
KS
120 dmx_fact = find_demuxer(dmx_name).unwrap();
121 br.seek(SeekFrom::Start(0)).unwrap();
3660c127 122 let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap();
019f9f9c
KS
123
124 let mut decs: Vec<Option<Box<NADecoder>>> = Vec::new();
3660c127 125 let mut writers: Vec<Outputter> = Vec::new();
019f9f9c
KS
126 for i in 0..dmx.get_num_streams() {
127 let s = dmx.get_stream(i).unwrap();
128 let info = s.get_info();
129 let decfunc = find_decoder(info.get_name());
3660c127
KS
130println!("stream {} - {} {}", i, s, info.get_name());
131 let mut has_out = false;
132 if info.is_video() {
133 if decode_video {
44939c84
KS
134 if decfunc.is_none() {
135 println!("no video decoder found!");
136 return;
137 }
3660c127
KS
138 let mut dec = (decfunc.unwrap())();
139 dec.init(info).unwrap();
140 decs.push(Some(dec));
141 if !noout {
44939c84 142 writers.push(Outputter::Video(FrameOutput{prefix: "", streamno: i, frameno: 1, nmode: nmode}));
3660c127
KS
143 has_out = true;
144 }
145 } else {
146 decs.push(None);
147 }
148 } else if info.is_audio() {
149 if decode_audio {
44939c84
KS
150 if decfunc.is_none() {
151 println!("no audio decoder found!");
152 return;
153 }
3660c127
KS
154 let mut dec = (decfunc.unwrap())();
155 dec.init(info).unwrap();
156 decs.push(Some(dec));
157 if !noout {
158 let name = format!("out{:02}.wav", i);
159 writers.push(Outputter::Audio(AudioOutput::new(&name)));
160 has_out = true;
161 }
162 } else {
163 decs.push(None);
164 }
019f9f9c 165 } else {
3660c127 166 decs.push(None);
019f9f9c
KS
167panic!("decoder {} not found", info.get_name());
168 }
3660c127
KS
169 if !has_out {
170 writers.push(Outputter::None);
171 }
019f9f9c
KS
172 }
173
174 loop {
175 let pktres = dmx.get_frame();
176 if let Err(e) = pktres {
177 if e == DemuxerError::EOF { break; }
178 }
179 let pkt = pktres.unwrap();
180 let streamno = pkt.get_stream().get_id() as usize;
181 if let Some(ref mut dec) = decs[streamno] {
182 let frm = dec.decode(&pkt).unwrap();
3660c127
KS
183 if !noout {
184 match writers[streamno] {
185 Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); },
186 Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); },
187 _ => {},
188 };
019f9f9c
KS
189 }
190 }
cdaf5445 191 if pkt.get_pts() != None && lastpts.is_some() && pkt.get_pts() >= lastpts { break; }
019f9f9c
KS
192 }
193//panic!("end");
194}