Commit | Line | Data |
---|---|---|
98b31ef7 | 1 | extern crate nihav_core; |
b4a0ee1d | 2 | extern crate nihav_codec_support; |
7d08c359 | 3 | extern crate nihav_registry; |
98b31ef7 | 4 | extern crate nihav_allstuff; |
019f9f9c | 5 | |
f7e9662a | 6 | use std::io::{SeekFrom, Write, BufReader}; |
019f9f9c KS |
7 | use std::fs::File; |
8 | use std::path::Path; | |
98b31ef7 KS |
9 | use nihav_core::io::byteio::{FileReader, ByteReader}; |
10 | use nihav_core::frame::*; | |
11 | use nihav_core::codecs::*; | |
12 | use nihav_core::demuxers::*; | |
b4a0ee1d | 13 | use nihav_codec_support::imgwrite::*; |
7d08c359 | 14 | use nihav_registry::detect; |
98b31ef7 | 15 | use nihav_allstuff::*; |
019f9f9c KS |
16 | use std::env; |
17 | ||
3660c127 | 18 | mod wavwriter; |
1e596ff3 | 19 | use crate::wavwriter::WavWriter; |
019f9f9c | 20 | |
44939c84 KS |
21 | #[derive(Clone,Copy,PartialEq)] |
22 | enum NumberMode { | |
23 | Counter, | |
24 | PktPTS, | |
25 | FrmPTS, | |
26 | } | |
27 | ||
3660c127 | 28 | struct FrameOutput { |
7437145d | 29 | prefix: String, |
3660c127 | 30 | streamno: usize, |
44939c84 KS |
31 | frameno: u64, |
32 | nmode: NumberMode, | |
3660c127 | 33 | } |
019f9f9c | 34 | |
3660c127 | 35 | impl FrameOutput { |
125ba9b3 | 36 | fn output_frame(&mut self, pkt: &NAPacket, frm: NAFrameRef) { |
3660c127 | 37 | if frm.get_frame_type() != FrameType::Skip { |
44939c84 KS |
38 | let pts = match self.nmode { |
39 | NumberMode::Counter => { self.frameno }, | |
40 | NumberMode::PktPTS => { pkt.get_pts().unwrap() }, | |
41 | NumberMode::FrmPTS => { if let Some(pt) = frm.get_pts() { pt } else { pkt.get_pts().unwrap() } }, | |
42 | }; | |
b4a0ee1d KS |
43 | if let Err(_) = write_pnm(&self.prefix, self.streamno, pts, frm) { |
44 | println!("error writing output picture"); | |
3660c127 KS |
45 | } |
46 | } | |
44939c84 | 47 | self.frameno += 1; |
019f9f9c KS |
48 | } |
49 | } | |
50 | ||
3660c127 KS |
51 | struct AudioOutput { |
52 | wwr: WavWriter<'static>, | |
53 | wrote_header: bool, | |
54 | } | |
55 | ||
56 | impl AudioOutput { | |
e47ee411 | 57 | fn new(name: &str) -> Self { Self { wwr: WavWriter::new(name), wrote_header: false } } |
3660c127 KS |
58 | fn output_frame(&mut self, _pkt: &NAPacket, frm: NAFrameRef) { |
59 | if !self.wrote_header { | |
125ba9b3 | 60 | self.wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); |
3660c127 | 61 | self.wrote_header = true; |
019f9f9c | 62 | } |
125ba9b3 | 63 | self.wwr.write_frame(frm.get_buffer()).unwrap(); |
019f9f9c KS |
64 | } |
65 | } | |
66 | ||
3660c127 KS |
67 | enum Outputter { |
68 | Video(FrameOutput), | |
69 | Audio(AudioOutput), | |
70 | None, | |
71 | } | |
72 | ||
019f9f9c KS |
73 | fn main() { |
74 | let args: Vec<_> = env::args().collect(); | |
75 | ||
3660c127 | 76 | if args.len() == 1 { |
f700bcdb KS |
77 | println!("usage: nihav-tool [-noout] [-vn] [-an] input [endtime]"); |
78 | println!(" or invoke nihav-tool --help for more detailed information"); | |
79 | return; | |
80 | } | |
81 | if args.len() == 2 && args[1] == "--help" { | |
82 | println!("usage: nihav-tool [options] input [endtime]"); | |
83 | println!("available options:"); | |
84 | println!(" -noout - decode but do not write output"); | |
85 | println!(" -an - do not decode audio streams"); | |
86 | println!(" -vn - do not decode video streams"); | |
87 | println!(" -nm={{count,pktpts,frmpts}} - use counter/frame PTS/decoded PTS as output image number"); | |
88 | println!(" -skip={{key,intra}} - decode only reference frames (I-/P-) or intra frames only"); | |
89 | println!(" -seek time - try seeking to the given time before starting decoding"); | |
90 | println!(" -apfx/-vpfx prefix - use given prefix when creating output audio/video files instead of default 'out'"); | |
91 | println!(" -ignerr - keep decoding even if decoding error is encountered"); | |
92 | println!(" -dumpfrm - dump raw frame data for all streams"); | |
93 | println!(" endtime - decoding end time, can be given either as time (hh:mm:ss.ms) or as a timestamp (e.g. 42pts)"); | |
3660c127 KS |
94 | return; |
95 | } | |
7cd3b41e | 96 | let mut lastpts = NATimePoint::None; |
3660c127 KS |
97 | let mut cur_arg: usize = 1; |
98 | let mut noout = false; | |
99 | let mut decode_video = true; | |
100 | let mut decode_audio = true; | |
44939c84 | 101 | let mut nmode = NumberMode::FrmPTS; |
69524874 | 102 | let mut smode = FrameSkipMode::None; |
7cd3b41e | 103 | let mut seek_time = NATimePoint::None; |
7437145d KS |
104 | let mut vpfx: Option<String> = None; |
105 | let mut apfx: Option<&str> = None; | |
e9c93f5f | 106 | let mut ignore_errors = false; |
2299da07 | 107 | let mut dump_frames = false; |
3660c127 | 108 | |
e47ee411 | 109 | while (cur_arg < args.len()) && args[cur_arg].starts_with('-') { |
3660c127 KS |
110 | match args[cur_arg].as_str() { |
111 | "--" => { break; }, | |
112 | "-noout" => { noout = true; }, | |
113 | "-an" => { decode_audio = false; }, | |
114 | "-vn" => { decode_video = false; }, | |
44939c84 KS |
115 | "-nm=count" => { nmode = NumberMode::Counter; }, |
116 | "-nm=pktpts" => { nmode = NumberMode::PktPTS; }, | |
117 | "-nm=frmpts" => { nmode = NumberMode::FrmPTS; }, | |
69524874 KS |
118 | "-skip=key" => { smode = FrameSkipMode::KeyframesOnly; }, |
119 | "-skip=intra" => { smode = FrameSkipMode::IntraOnly; }, | |
fde9f7c3 KS |
120 | "-seek" => { |
121 | cur_arg += 1; | |
122 | if cur_arg == args.len() { | |
123 | println!("seek time missing"); | |
124 | return; | |
125 | } | |
7cd3b41e | 126 | let ret = args[cur_arg].parse::<NATimePoint>(); |
fde9f7c3 KS |
127 | if ret.is_err() { |
128 | println!("wrong seek time"); | |
129 | return; | |
130 | } | |
131 | seek_time = ret.unwrap(); | |
132 | }, | |
7437145d KS |
133 | "-apfx" => { |
134 | cur_arg += 1; | |
135 | if cur_arg == args.len() { | |
136 | println!("name missing"); | |
137 | return; | |
138 | } | |
139 | apfx = Some(&args[cur_arg]); | |
140 | }, | |
141 | "-vpfx" => { | |
142 | cur_arg += 1; | |
143 | if cur_arg == args.len() { | |
144 | println!("name missing"); | |
145 | return; | |
146 | } | |
147 | vpfx = Some(args[cur_arg].clone()); | |
148 | }, | |
e9c93f5f | 149 | "-ignerr" => { ignore_errors = true; }, |
2299da07 | 150 | "-dumpfrm" => { dump_frames = true; }, |
3660c127 KS |
151 | _ => { println!("unknown option {}", args[cur_arg]); return; }, |
152 | } | |
153 | cur_arg += 1; | |
154 | } | |
155 | let name = args[cur_arg].as_str(); | |
156 | cur_arg += 1; | |
3660c127 | 157 | if cur_arg < args.len() { |
7cd3b41e KS |
158 | let ret = args[cur_arg].parse::<NATimePoint>(); |
159 | if ret.is_err() { | |
160 | println!("cannot parse end time"); | |
161 | return; | |
162 | } | |
163 | lastpts = ret.unwrap(); | |
3660c127 | 164 | } |
019f9f9c | 165 | |
019f9f9c | 166 | let path = Path::new(name); |
f7e9662a KS |
167 | let file = File::open(path).unwrap(); |
168 | let mut file = BufReader::new(file); | |
019f9f9c KS |
169 | let dmx_fact; |
170 | let mut fr = FileReader::new_read(&mut file); | |
171 | let mut br = ByteReader::new(&mut fr); | |
172 | let res = detect::detect_format(name, &mut br); | |
173 | if res.is_none() { | |
174 | println!("cannot detect format for {}", name); | |
175 | return; | |
176 | } | |
177 | let (dmx_name, _) = res.unwrap(); | |
3660c127 | 178 | println!("trying demuxer {} on {}", dmx_name, name); |
98b31ef7 KS |
179 | |
180 | let mut dmx_reg = RegisteredDemuxers::new(); | |
181 | nihav_register_all_demuxers(&mut dmx_reg); | |
182 | let mut dec_reg = RegisteredDecoders::new(); | |
1e83105a | 183 | nihav_register_all_decoders(&mut dec_reg); |
98b31ef7 KS |
184 | |
185 | dmx_fact = dmx_reg.find_demuxer(dmx_name).unwrap(); | |
019f9f9c | 186 | br.seek(SeekFrom::Start(0)).unwrap(); |
3660c127 | 187 | let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap(); |
019f9f9c | 188 | |
7cd3b41e | 189 | if seek_time != NATimePoint::None { |
fde9f7c3 KS |
190 | let ret = dmx.seek(seek_time); |
191 | if ret.is_err() { | |
192 | println!(" seek error {:?}", ret.err().unwrap()); | |
193 | } | |
194 | } | |
195 | ||
e47ee411 | 196 | let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new(); |
417ba15e | 197 | let mut sids: Vec<u32> = Vec::new(); |
3660c127 | 198 | let mut writers: Vec<Outputter> = Vec::new(); |
69524874 | 199 | let dec_opts = [NAOption{name: FRAME_SKIP_OPTION, value: NAValue::String(smode.to_string())}]; |
a4c97adf KS |
200 | let duration = dmx.get_duration(); |
201 | if duration != 0 { | |
202 | let s = duration / 1000; | |
203 | let m = s / 60; | |
204 | let h = m / 60; | |
205 | println!(" total duration {}:{:02}:{:02}.{}", h, m % 60, s % 60, (duration / 100) % 10); | |
206 | } | |
019f9f9c KS |
207 | for i in 0..dmx.get_num_streams() { |
208 | let s = dmx.get_stream(i).unwrap(); | |
209 | let info = s.get_info(); | |
98b31ef7 | 210 | let decfunc = dec_reg.find_decoder(info.get_name()); |
3660c127 | 211 | println!("stream {} - {} {}", i, s, info.get_name()); |
417ba15e | 212 | let str_id = s.get_id(); |
3660c127 | 213 | let mut has_out = false; |
417ba15e | 214 | sids.push(str_id); |
3660c127 KS |
215 | if info.is_video() { |
216 | if decode_video { | |
44939c84 KS |
217 | if decfunc.is_none() { |
218 | println!("no video decoder found!"); | |
219 | return; | |
220 | } | |
3660c127 | 221 | let mut dec = (decfunc.unwrap())(); |
4b6a29ce KS |
222 | let mut dsupp = Box::new(NADecoderSupport::new()); |
223 | dec.init(&mut dsupp, info).unwrap(); | |
69524874 | 224 | dec.set_options(&dec_opts); |
4b6a29ce | 225 | decs.push(Some((dsupp, dec))); |
3660c127 | 226 | if !noout { |
7437145d | 227 | writers.push(Outputter::Video(FrameOutput{prefix: if let Some(ref str) = vpfx { str.clone() } else { "out".to_string() }, streamno: i, frameno: 1, nmode})); |
3660c127 KS |
228 | has_out = true; |
229 | } | |
230 | } else { | |
231 | decs.push(None); | |
232 | } | |
233 | } else if info.is_audio() { | |
234 | if decode_audio { | |
44939c84 KS |
235 | if decfunc.is_none() { |
236 | println!("no audio decoder found!"); | |
237 | return; | |
238 | } | |
3660c127 | 239 | let mut dec = (decfunc.unwrap())(); |
4b6a29ce KS |
240 | let mut dsupp = Box::new(NADecoderSupport::new()); |
241 | dec.init(&mut dsupp, info).unwrap(); | |
242 | decs.push(Some((dsupp, dec))); | |
3660c127 | 243 | if !noout { |
7437145d KS |
244 | let name = if let Some(apfx) = apfx { |
245 | format!("{}{:02}.wav", apfx, i) | |
246 | } else { | |
247 | format!("out{:02}.wav", i) | |
248 | }; | |
3660c127 KS |
249 | writers.push(Outputter::Audio(AudioOutput::new(&name))); |
250 | has_out = true; | |
251 | } | |
252 | } else { | |
253 | decs.push(None); | |
254 | } | |
019f9f9c | 255 | } else { |
3660c127 | 256 | decs.push(None); |
019f9f9c | 257 | } |
3660c127 KS |
258 | if !has_out { |
259 | writers.push(Outputter::None); | |
260 | } | |
019f9f9c KS |
261 | } |
262 | ||
2299da07 | 263 | let mut frmnum = 0; |
019f9f9c KS |
264 | loop { |
265 | let pktres = dmx.get_frame(); | |
266 | if let Err(e) = pktres { | |
267 | if e == DemuxerError::EOF { break; } | |
268 | } | |
269 | let pkt = pktres.unwrap(); | |
417ba15e KS |
270 | let streamno = pkt.get_stream().get_id(); |
271 | let sr = sids.iter().position(|x| *x == streamno); | |
272 | let idx = sr.unwrap(); | |
2299da07 KS |
273 | if dump_frames { |
274 | let name = format!("out{:02}_{:08}.frm", streamno, pkt.get_pts().unwrap_or(frmnum)); | |
275 | let mut ofile = File::create(name).unwrap(); | |
276 | ofile.write(pkt.get_buffer().as_slice()).unwrap(); | |
277 | frmnum += 1; | |
278 | } | |
4b6a29ce | 279 | if let Some((ref mut dsupp, ref mut dec)) = decs[idx] { |
251d9950 KS |
280 | match dec.decode(dsupp, &pkt) { |
281 | Ok(frm) => { | |
282 | if !noout { | |
283 | match writers[idx] { | |
284 | Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); }, | |
285 | Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); }, | |
286 | _ => {}, | |
287 | }; | |
288 | } | |
289 | }, | |
7cd3b41e | 290 | Err(DecoderError::MissingReference) if seek_time != NATimePoint::None => { |
251d9950 KS |
291 | println!("ignoring missing ref"); |
292 | }, | |
293 | Err(reason) => { | |
294 | println!("error decoding frame {:?}", reason); | |
e9c93f5f KS |
295 | if !ignore_errors { |
296 | break; | |
297 | } | |
251d9950 KS |
298 | }, |
299 | }; | |
7cd3b41e | 300 | if pkt.get_pts() != None && lastpts != NATimePoint::None && !pkt.ts.less_than(lastpts) { break; } |
019f9f9c | 301 | } |
019f9f9c KS |
302 | } |
303 | //panic!("end"); | |
304 | } |