Commit | Line | Data |
---|---|---|
98b31ef7 | 1 | extern crate nihav_core; |
7d08c359 | 2 | extern crate nihav_registry; |
98b31ef7 | 3 | extern crate nihav_allstuff; |
019f9f9c | 4 | |
019f9f9c KS |
5 | use std::io::SeekFrom; |
6 | use std::fs::File; | |
7 | use std::path::Path; | |
98b31ef7 KS |
8 | use nihav_core::io::byteio::{FileReader, ByteReader}; |
9 | use nihav_core::frame::*; | |
10 | use nihav_core::codecs::*; | |
11 | use nihav_core::demuxers::*; | |
7d08c359 | 12 | use nihav_registry::detect; |
98b31ef7 | 13 | use nihav_allstuff::*; |
019f9f9c KS |
14 | use std::env; |
15 | ||
3660c127 | 16 | mod frmwriter; |
1e596ff3 | 17 | use crate::frmwriter::*; |
3660c127 KS |
18 | |
19 | mod wavwriter; | |
1e596ff3 | 20 | use crate::wavwriter::WavWriter; |
019f9f9c | 21 | |
44939c84 KS |
22 | #[derive(Clone,Copy,PartialEq)] |
23 | enum NumberMode { | |
24 | Counter, | |
25 | PktPTS, | |
26 | FrmPTS, | |
27 | } | |
28 | ||
3660c127 | 29 | struct FrameOutput { |
7437145d | 30 | prefix: String, |
3660c127 | 31 | streamno: usize, |
44939c84 KS |
32 | frameno: u64, |
33 | nmode: NumberMode, | |
3660c127 | 34 | } |
019f9f9c | 35 | |
3660c127 | 36 | impl FrameOutput { |
125ba9b3 | 37 | fn output_frame(&mut self, pkt: &NAPacket, frm: NAFrameRef) { |
3660c127 | 38 | if frm.get_frame_type() != FrameType::Skip { |
44939c84 KS |
39 | let pts = match self.nmode { |
40 | NumberMode::Counter => { self.frameno }, | |
41 | NumberMode::PktPTS => { pkt.get_pts().unwrap() }, | |
42 | NumberMode::FrmPTS => { if let Some(pt) = frm.get_pts() { pt } else { pkt.get_pts().unwrap() } }, | |
43 | }; | |
ffe69577 KS |
44 | let vinfo = frm.get_buffer().get_video_info().unwrap(); |
45 | if vinfo.get_format().is_paletted() { | |
7437145d | 46 | write_palppm(&self.prefix, self.streamno, pts, frm); |
ffe69577 | 47 | } else if vinfo.get_format().get_model().is_yuv() { |
7437145d | 48 | write_pgmyuv(&self.prefix, self.streamno, pts, frm); |
ffe69577 | 49 | } else if vinfo.get_format().get_model().is_rgb() { |
7437145d | 50 | write_ppm(&self.prefix, self.streamno, pts, frm); |
ffe69577 KS |
51 | } else { |
52 | panic!(" unknown format"); | |
3660c127 KS |
53 | } |
54 | } | |
44939c84 | 55 | self.frameno += 1; |
019f9f9c KS |
56 | } |
57 | } | |
58 | ||
3660c127 KS |
59 | struct AudioOutput { |
60 | wwr: WavWriter<'static>, | |
61 | wrote_header: bool, | |
62 | } | |
63 | ||
64 | impl AudioOutput { | |
e47ee411 | 65 | fn new(name: &str) -> Self { Self { wwr: WavWriter::new(name), wrote_header: false } } |
3660c127 KS |
66 | fn output_frame(&mut self, _pkt: &NAPacket, frm: NAFrameRef) { |
67 | if !self.wrote_header { | |
125ba9b3 | 68 | self.wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); |
3660c127 | 69 | self.wrote_header = true; |
019f9f9c | 70 | } |
125ba9b3 | 71 | self.wwr.write_frame(frm.get_buffer()).unwrap(); |
019f9f9c KS |
72 | } |
73 | } | |
74 | ||
3660c127 KS |
75 | enum Outputter { |
76 | Video(FrameOutput), | |
77 | Audio(AudioOutput), | |
78 | None, | |
79 | } | |
80 | ||
019f9f9c KS |
81 | fn main() { |
82 | let args: Vec<_> = env::args().collect(); | |
83 | ||
3660c127 KS |
84 | if args.len() == 1 { |
85 | println!("usage: nihav-tool [-noout] [-vn] [-an] input [lastpts]"); | |
86 | return; | |
87 | } | |
88 | let mut lastpts: Option<u64> = None; | |
89 | let mut cur_arg: usize = 1; | |
90 | let mut noout = false; | |
91 | let mut decode_video = true; | |
92 | let mut decode_audio = true; | |
44939c84 | 93 | let mut nmode = NumberMode::FrmPTS; |
fde9f7c3 | 94 | let mut seek_time = 0u64; |
7437145d KS |
95 | let mut vpfx: Option<String> = None; |
96 | let mut apfx: Option<&str> = None; | |
3660c127 | 97 | |
e47ee411 | 98 | while (cur_arg < args.len()) && args[cur_arg].starts_with('-') { |
3660c127 KS |
99 | match args[cur_arg].as_str() { |
100 | "--" => { break; }, | |
101 | "-noout" => { noout = true; }, | |
102 | "-an" => { decode_audio = false; }, | |
103 | "-vn" => { decode_video = false; }, | |
44939c84 KS |
104 | "-nm=count" => { nmode = NumberMode::Counter; }, |
105 | "-nm=pktpts" => { nmode = NumberMode::PktPTS; }, | |
106 | "-nm=frmpts" => { nmode = NumberMode::FrmPTS; }, | |
fde9f7c3 KS |
107 | "-seek" => { |
108 | cur_arg += 1; | |
109 | if cur_arg == args.len() { | |
110 | println!("seek time missing"); | |
111 | return; | |
112 | } | |
113 | let ret = args[cur_arg].parse::<u64>(); | |
114 | if ret.is_err() { | |
115 | println!("wrong seek time"); | |
116 | return; | |
117 | } | |
118 | seek_time = ret.unwrap(); | |
119 | }, | |
7437145d KS |
120 | "-apfx" => { |
121 | cur_arg += 1; | |
122 | if cur_arg == args.len() { | |
123 | println!("name missing"); | |
124 | return; | |
125 | } | |
126 | apfx = Some(&args[cur_arg]); | |
127 | }, | |
128 | "-vpfx" => { | |
129 | cur_arg += 1; | |
130 | if cur_arg == args.len() { | |
131 | println!("name missing"); | |
132 | return; | |
133 | } | |
134 | vpfx = Some(args[cur_arg].clone()); | |
135 | }, | |
3660c127 KS |
136 | _ => { println!("unknown option {}", args[cur_arg]); return; }, |
137 | } | |
138 | cur_arg += 1; | |
139 | } | |
140 | let name = args[cur_arg].as_str(); | |
141 | cur_arg += 1; | |
3660c127 KS |
142 | if cur_arg < args.len() { |
143 | lastpts = Some(u64::from_str_radix(args[cur_arg].as_str(), 10).unwrap()); | |
144 | } | |
019f9f9c | 145 | |
019f9f9c KS |
146 | let path = Path::new(name); |
147 | let mut file = File::open(path).unwrap(); | |
148 | let dmx_fact; | |
149 | let mut fr = FileReader::new_read(&mut file); | |
150 | let mut br = ByteReader::new(&mut fr); | |
151 | let res = detect::detect_format(name, &mut br); | |
152 | if res.is_none() { | |
153 | println!("cannot detect format for {}", name); | |
154 | return; | |
155 | } | |
156 | let (dmx_name, _) = res.unwrap(); | |
3660c127 | 157 | println!("trying demuxer {} on {}", dmx_name, name); |
98b31ef7 KS |
158 | |
159 | let mut dmx_reg = RegisteredDemuxers::new(); | |
160 | nihav_register_all_demuxers(&mut dmx_reg); | |
161 | let mut dec_reg = RegisteredDecoders::new(); | |
162 | nihav_register_all_codecs(&mut dec_reg); | |
163 | ||
164 | dmx_fact = dmx_reg.find_demuxer(dmx_name).unwrap(); | |
019f9f9c | 165 | br.seek(SeekFrom::Start(0)).unwrap(); |
3660c127 | 166 | let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap(); |
019f9f9c | 167 | |
fde9f7c3 KS |
168 | if seek_time > 0 { |
169 | let ret = dmx.seek(seek_time); | |
170 | if ret.is_err() { | |
171 | println!(" seek error {:?}", ret.err().unwrap()); | |
172 | } | |
173 | } | |
174 | ||
e47ee411 | 175 | let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new(); |
417ba15e | 176 | let mut sids: Vec<u32> = Vec::new(); |
3660c127 | 177 | let mut writers: Vec<Outputter> = Vec::new(); |
019f9f9c KS |
178 | for i in 0..dmx.get_num_streams() { |
179 | let s = dmx.get_stream(i).unwrap(); | |
180 | let info = s.get_info(); | |
98b31ef7 | 181 | let decfunc = dec_reg.find_decoder(info.get_name()); |
3660c127 | 182 | println!("stream {} - {} {}", i, s, info.get_name()); |
417ba15e | 183 | let str_id = s.get_id(); |
3660c127 | 184 | let mut has_out = false; |
417ba15e | 185 | sids.push(str_id); |
3660c127 KS |
186 | if info.is_video() { |
187 | if decode_video { | |
44939c84 KS |
188 | if decfunc.is_none() { |
189 | println!("no video decoder found!"); | |
190 | return; | |
191 | } | |
3660c127 | 192 | let mut dec = (decfunc.unwrap())(); |
4b6a29ce KS |
193 | let mut dsupp = Box::new(NADecoderSupport::new()); |
194 | dec.init(&mut dsupp, info).unwrap(); | |
195 | decs.push(Some((dsupp, dec))); | |
3660c127 | 196 | if !noout { |
7437145d | 197 | 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 |
198 | has_out = true; |
199 | } | |
200 | } else { | |
201 | decs.push(None); | |
202 | } | |
203 | } else if info.is_audio() { | |
204 | if decode_audio { | |
44939c84 KS |
205 | if decfunc.is_none() { |
206 | println!("no audio decoder found!"); | |
207 | return; | |
208 | } | |
3660c127 | 209 | let mut dec = (decfunc.unwrap())(); |
4b6a29ce KS |
210 | let mut dsupp = Box::new(NADecoderSupport::new()); |
211 | dec.init(&mut dsupp, info).unwrap(); | |
212 | decs.push(Some((dsupp, dec))); | |
3660c127 | 213 | if !noout { |
7437145d KS |
214 | let name = if let Some(apfx) = apfx { |
215 | format!("{}{:02}.wav", apfx, i) | |
216 | } else { | |
217 | format!("out{:02}.wav", i) | |
218 | }; | |
3660c127 KS |
219 | writers.push(Outputter::Audio(AudioOutput::new(&name))); |
220 | has_out = true; | |
221 | } | |
222 | } else { | |
223 | decs.push(None); | |
224 | } | |
019f9f9c | 225 | } else { |
3660c127 | 226 | decs.push(None); |
019f9f9c KS |
227 | panic!("decoder {} not found", info.get_name()); |
228 | } | |
3660c127 KS |
229 | if !has_out { |
230 | writers.push(Outputter::None); | |
231 | } | |
019f9f9c KS |
232 | } |
233 | ||
234 | loop { | |
235 | let pktres = dmx.get_frame(); | |
236 | if let Err(e) = pktres { | |
237 | if e == DemuxerError::EOF { break; } | |
238 | } | |
239 | let pkt = pktres.unwrap(); | |
417ba15e KS |
240 | let streamno = pkt.get_stream().get_id(); |
241 | let sr = sids.iter().position(|x| *x == streamno); | |
242 | let idx = sr.unwrap(); | |
4b6a29ce | 243 | if let Some((ref mut dsupp, ref mut dec)) = decs[idx] { |
251d9950 KS |
244 | match dec.decode(dsupp, &pkt) { |
245 | Ok(frm) => { | |
246 | if !noout { | |
247 | match writers[idx] { | |
248 | Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); }, | |
249 | Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); }, | |
250 | _ => {}, | |
251 | }; | |
252 | } | |
253 | }, | |
254 | Err(DecoderError::MissingReference) if seek_time > 0 => { | |
255 | println!("ignoring missing ref"); | |
256 | }, | |
257 | Err(reason) => { | |
258 | println!("error decoding frame {:?}", reason); | |
259 | break; | |
260 | }, | |
261 | }; | |
a319c93c | 262 | if pkt.get_pts() != None && lastpts.is_some() && pkt.get_pts() >= lastpts { break; } |
019f9f9c | 263 | } |
019f9f9c KS |
264 | } |
265 | //panic!("end"); | |
266 | } |