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