Commit | Line | Data |
---|---|---|
6dc69f51 KS |
1 | extern crate nihav_core; |
2 | extern crate nihav_codec_support; | |
3 | extern crate nihav_registry; | |
4 | extern crate nihav_allstuff; | |
5 | ||
6 | use std::fs::File; | |
91a15e39 | 7 | use std::io::BufReader; |
6dc69f51 KS |
8 | use nihav_core::io::byteio::{FileReader, ByteReader}; |
9 | use nihav_core::frame::*; | |
10 | use nihav_core::options::*; | |
11 | use nihav_core::codecs::*; | |
12 | use nihav_core::demuxers::*; | |
13 | use nihav_core::muxers::*; | |
d9fe2b71 | 14 | use nihav_core::reorder::*; |
6dc69f51 KS |
15 | use nihav_core::scale::*; |
16 | use nihav_core::soundcvt::*; | |
17 | use nihav_registry::detect; | |
e6cb09af | 18 | use nihav_registry::register; |
6dc69f51 KS |
19 | use std::env; |
20 | ||
91a15e39 KS |
21 | mod demux; |
22 | use crate::demux::*; | |
93521506 | 23 | mod null; |
93521506 | 24 | |
6dc69f51 KS |
25 | fn print_options(name: &str, options: &[NAOptionDefinition]) { |
26 | if options.is_empty() { | |
27 | println!("No custom options."); | |
28 | } else { | |
29 | println!("Options for '{}'", name); | |
30 | for opt in options.iter() { | |
31 | println!(" {}", opt); | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
36 | struct OptionArgs { | |
37 | name: String, | |
38 | value: Option<String>, | |
39 | } | |
40 | ||
41 | struct InputStreamOptions { | |
42 | id: u32, | |
43 | drop: bool, | |
44 | dec_opts: Vec<OptionArgs>, | |
45 | } | |
46 | ||
47 | struct OutputStreamOptions { | |
48 | id: u32, | |
49 | enc_params: EncodeParameters, | |
50 | enc_name: String, | |
51 | enc_opts: Vec<OptionArgs>, | |
52 | } | |
53 | ||
54 | enum OutputConvert { | |
55 | Video(NAScale, NABufferType), | |
56 | Audio(NAAudioInfo, NAChannelMap), | |
57 | None, | |
58 | } | |
59 | ||
86c54d88 | 60 | #[allow(clippy::large_enum_variant)] |
6dc69f51 KS |
61 | enum OutputMode { |
62 | Drop, | |
63 | Copy(u32), | |
64 | Encode(u32, Box<dyn NAEncoder>, OutputConvert), | |
65 | } | |
66 | ||
67 | #[derive(Default)] | |
86c54d88 | 68 | #[allow(clippy::type_complexity)] |
6dc69f51 KS |
69 | struct Transcoder { |
70 | input_name: String, | |
71 | input_fmt: Option<String>, | |
72 | output_name: String, | |
73 | output_fmt: Option<String>, | |
74 | demux_opts: Vec<OptionArgs>, | |
75 | mux_opts: Vec<OptionArgs>, | |
76 | istr_opts: Vec<InputStreamOptions>, | |
77 | ostr_opts: Vec<OutputStreamOptions>, | |
df37d3b1 | 78 | scale_opts: Vec<(String, String)>, |
d9fe2b71 | 79 | decoders: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>, Box<dyn FrameReorderer>)>>, |
6dc69f51 KS |
80 | encoders: Vec<OutputMode>, |
81 | no_video: bool, | |
82 | no_audio: bool, | |
951916e8 KS |
83 | start: NATimePoint, |
84 | end: NATimePoint, | |
6dc69f51 KS |
85 | } |
86 | ||
87 | macro_rules! parse_and_apply_options { | |
88 | ($obj: expr, $in_opts: expr, $name: expr) => { | |
89 | let mut opts = Vec::with_capacity($in_opts.len()); | |
90 | let opt_def = $obj.get_supported_options(); | |
91 | for opt in $in_opts.iter() { | |
92 | let mut found = false; | |
93 | for opt_def in opt_def.iter() { | |
3e2a4e88 KS |
94 | let mut matches = opt.name == opt_def.name; |
95 | if !matches && opt.name.starts_with("no") { | |
96 | let (_, name) = opt.name.split_at(2); | |
97 | matches = name == opt_def.name; | |
98 | } | |
99 | if matches { | |
6dc69f51 KS |
100 | let arg = if let Some(ref str) = opt.value { Some(str) } else { None }; |
101 | let ret = opt_def.parse(&opt.name, arg); | |
86c54d88 | 102 | if let Ok((val, _)) = ret { |
6dc69f51 | 103 | opts.push(val); |
86c54d88 KS |
104 | } else { |
105 | println!("invalid option {} for {}", opt.name, $name); | |
6dc69f51 KS |
106 | } |
107 | found = true; | |
108 | } | |
109 | } | |
110 | if !found { | |
111 | println!(" ignoring option '{}' for {}", opt.name, $name); | |
112 | } | |
113 | } | |
114 | $obj.set_options(opts.as_slice()); | |
115 | } | |
116 | } | |
117 | ||
118 | impl Transcoder { | |
119 | fn new() -> Self { Self::default() } | |
120 | fn parse_istream_options(&mut self, opt0: &str, opt1: &str) -> bool { | |
121 | let (_, strno) = opt0.split_at(9); | |
122 | let ret = strno.parse::<u32>(); | |
123 | if ret.is_err() { return false; } | |
124 | let streamno = ret.unwrap(); | |
125 | ||
126 | let sidx = if let Some(idx) = self.istr_opts.iter().position(|str| str.id == streamno) { | |
127 | idx | |
128 | } else { | |
129 | self.istr_opts.push(InputStreamOptions {id: streamno, drop: false, dec_opts: Vec::new() }); | |
130 | self.istr_opts.len() - 1 | |
131 | }; | |
132 | let istr = &mut self.istr_opts[sidx]; | |
830c03a1 | 133 | |
6dc69f51 KS |
134 | for opt in opt1.split(',') { |
135 | let oval: Vec<_> = opt.split('=').collect(); | |
136 | if oval.len() == 1 { | |
137 | match oval[0] { | |
138 | "drop" => { istr.drop = true; }, | |
139 | _ => { | |
140 | istr.dec_opts.push(OptionArgs{ name: oval[0].to_string(), value: None }); | |
141 | }, | |
142 | }; | |
143 | } else if oval.len() == 2 { | |
144 | istr.dec_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) }); | |
145 | } else { | |
146 | println!("unrecognized option '{}'", opt); | |
147 | } | |
148 | } | |
149 | true | |
150 | } | |
151 | fn parse_ostream_options(&mut self, opt0: &str, opt1: &str, enc_reg: &RegisteredEncoders) -> bool { | |
152 | let (_, strno) = opt0.split_at(9); | |
153 | let ret = strno.parse::<u32>(); | |
154 | if ret.is_err() { return false; } | |
155 | let streamno = ret.unwrap(); | |
156 | ||
157 | let sidx = if let Some(idx) = self.ostr_opts.iter().position(|str| str.id == streamno) { | |
158 | idx | |
159 | } else { | |
160 | self.ostr_opts.push(OutputStreamOptions {id: streamno, enc_name: String::new(), enc_params: EncodeParameters::default(), enc_opts: Vec::new() }); | |
161 | self.ostr_opts.len() - 1 | |
162 | }; | |
163 | let ostr = &mut self.ostr_opts[sidx]; | |
830c03a1 | 164 | |
6dc69f51 KS |
165 | for opt in opt1.split(',') { |
166 | let oval: Vec<_> = opt.split('=').collect(); | |
167 | if oval.len() == 1 { | |
168 | match oval[0] { | |
169 | "flip" => { | |
170 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
171 | ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)); | |
172 | } | |
173 | if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format { | |
174 | vinfo.flipped = true; | |
175 | } else { | |
176 | println!("video option for audio stream"); | |
177 | } | |
178 | }, | |
179 | "noflip" => { | |
180 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
181 | ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)); | |
182 | } | |
183 | if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format { | |
184 | vinfo.flipped = false; | |
185 | } else { | |
186 | println!("video option for audio stream"); | |
187 | } | |
188 | }, | |
189 | _ => { | |
190 | ostr.enc_opts.push(OptionArgs{ name: oval[0].to_string(), value: None }); | |
191 | }, | |
192 | }; | |
193 | } else if oval.len() == 2 { | |
194 | //todo parse encoder options, store, init later | |
195 | match oval[0] { | |
ca3b31d7 KS |
196 | "timebase" => { |
197 | let mut parts = oval[1].split('/'); | |
198 | let num = parts.next().unwrap(); | |
199 | let den = parts.next(); | |
200 | if let Some(den) = den { | |
201 | let rnum = num.parse::<u32>(); | |
202 | let rden = den.parse::<u32>(); | |
203 | if let (Ok(num), Ok(den)) = (rnum, rden) { | |
204 | ostr.enc_params.tb_num = num; | |
205 | ostr.enc_params.tb_den = den; | |
206 | } else { | |
207 | println!("invalid timebase value"); | |
208 | } | |
209 | } else { | |
210 | println!("invalid timebase format (should be num/den)"); | |
211 | } | |
212 | }, | |
6dc69f51 KS |
213 | "encoder" => { |
214 | if enc_reg.find_encoder(oval[1]).is_some() { | |
215 | ostr.enc_name = oval[1].to_string(); | |
216 | } else { | |
217 | println!("unknown encoder '{}'", oval[1]); | |
c0052668 | 218 | ostr.enc_name = oval[1].to_string(); |
6dc69f51 KS |
219 | } |
220 | }, | |
221 | "width" => { | |
222 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
223 | ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)); | |
224 | } | |
225 | if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format { | |
226 | let ret = oval[1].parse::<usize>(); | |
227 | if let Ok(val) = ret { | |
228 | vinfo.width = val; | |
229 | } else { | |
230 | println!("invalid width"); | |
231 | } | |
232 | } else { | |
233 | println!("video option for audio stream"); | |
234 | } | |
235 | }, | |
236 | "height" => { | |
237 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
238 | ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)); | |
239 | } | |
240 | if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format { | |
241 | let ret = oval[1].parse::<usize>(); | |
242 | if let Ok(val) = ret { | |
243 | vinfo.height = val; | |
244 | } else { | |
245 | println!("invalid height"); | |
246 | } | |
247 | } else { | |
248 | println!("video option for audio stream"); | |
249 | } | |
250 | }, | |
e176bfee KS |
251 | "pixfmt" => { |
252 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
253 | ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT)); | |
254 | } | |
255 | if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format { | |
256 | let ret = oval[1].parse::<NAPixelFormaton>(); | |
257 | if let Ok(val) = ret { | |
258 | vinfo.format = val; | |
259 | } else { | |
260 | println!("invalid pixel format"); | |
261 | } | |
262 | } else { | |
263 | println!("video option for audio stream"); | |
264 | } | |
265 | }, | |
6dc69f51 KS |
266 | "srate" => { |
267 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
268 | ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0)); | |
269 | } | |
270 | if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format { | |
271 | let ret = oval[1].parse::<u32>(); | |
272 | if let Ok(val) = ret { | |
273 | ainfo.sample_rate = val; | |
274 | } else { | |
275 | println!("invalid sampling rate"); | |
276 | } | |
277 | } else { | |
278 | println!("audio option for video stream"); | |
279 | } | |
280 | }, | |
281 | "channels" => { | |
282 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
283 | ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0)); | |
284 | } | |
285 | if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format { | |
286 | let ret = oval[1].parse::<u8>(); | |
287 | if let Ok(val) = ret { | |
288 | ainfo.channels = val; | |
289 | } else { | |
290 | println!("invalid number of channels"); | |
291 | } | |
292 | } else { | |
293 | println!("audio option for video stream"); | |
294 | } | |
295 | }, | |
296 | "block_len" => { | |
297 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
298 | ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0)); | |
299 | } | |
300 | if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format { | |
301 | let ret = oval[1].parse::<usize>(); | |
302 | if let Ok(val) = ret { | |
303 | ainfo.block_len = val; | |
304 | } else { | |
305 | println!("invalid block_length"); | |
306 | } | |
307 | } else { | |
308 | println!("audio option for video stream"); | |
309 | } | |
310 | }, | |
e176bfee KS |
311 | "sfmt" => { |
312 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
313 | ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0)); | |
314 | } | |
315 | if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format { | |
316 | let ret = oval[1].parse::<NASoniton>(); | |
317 | if let Ok(val) = ret { | |
318 | ainfo.format = val; | |
319 | } else { | |
320 | println!("invalid audio format"); | |
321 | } | |
322 | } else { | |
323 | println!("audio option for video stream"); | |
324 | } | |
325 | }, | |
326 | // todo channel map negotiation | |
327 | /*"chmap" => { | |
328 | if ostr.enc_params.format == NACodecTypeInfo::None { | |
329 | ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0)); | |
330 | } | |
331 | if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format { | |
332 | let ret = oval[1].parse::<NAChannelMap>(); | |
333 | if let Ok(val) = ret { | |
334 | ainfo.chmap = val; | |
335 | } else { | |
336 | println!("invalid channel map"); | |
337 | } | |
338 | } else { | |
339 | println!("audio option for video stream"); | |
340 | } | |
341 | },*/ | |
6dc69f51 KS |
342 | "bitrate" => { |
343 | let ret = oval[1].parse::<u32>(); | |
344 | if let Ok(val) = ret { | |
345 | ostr.enc_params.bitrate = val; | |
346 | } else { | |
347 | println!("invalid bitrate value"); | |
348 | } | |
349 | }, | |
350 | "quality" => { | |
351 | let ret = oval[1].parse::<u8>(); | |
352 | if let Ok(val) = ret { | |
353 | ostr.enc_params.quality = val; | |
354 | } else { | |
355 | println!("invalid quality value"); | |
356 | } | |
357 | }, | |
358 | _ => { | |
359 | ostr.enc_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) }); | |
360 | }, | |
361 | } | |
362 | } else { | |
363 | println!("unrecognized option '{}'", opt); | |
364 | } | |
365 | } | |
366 | true | |
367 | } | |
368 | fn parse_demuxer_options(&mut self, opts: &str, dmx_reg: &RegisteredDemuxers) -> bool { | |
369 | for opt in opts.split(',') { | |
370 | let oval: Vec<_> = opt.split('=').collect(); | |
371 | if oval.len() == 1 { | |
372 | self.demux_opts.push(OptionArgs{ name: oval[0].to_string(), value: None }); | |
373 | } else if oval.len() == 2 { | |
374 | if oval[0] == "format" { | |
375 | if dmx_reg.find_demuxer(oval[1]).is_some() { | |
376 | self.input_fmt = Some(oval[1].to_string()); | |
377 | } else { | |
378 | println!("unknown demuxer format '{}'", oval[1]); | |
379 | } | |
380 | } else { | |
381 | self.demux_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) }); | |
382 | } | |
383 | } else { | |
384 | println!("unrecognized option '{}'", opt); | |
385 | } | |
386 | } | |
387 | true | |
388 | } | |
389 | fn parse_muxer_options(&mut self, opts: &str, mux_reg: &RegisteredMuxers) -> bool { | |
390 | for opt in opts.split(',') { | |
391 | let oval: Vec<_> = opt.split('=').collect(); | |
392 | if oval.len() == 1 { | |
393 | self.mux_opts.push(OptionArgs{ name: oval[0].to_string(), value: None }); | |
394 | } else if oval.len() == 2 { | |
395 | if oval[0] == "format" { | |
396 | if mux_reg.find_muxer(oval[1]).is_some() { | |
397 | self.output_fmt = Some(oval[1].to_string()); | |
398 | } else { | |
399 | println!("unknown muxer format '{}'", oval[1]); | |
400 | } | |
401 | } else { | |
402 | self.mux_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) }); | |
403 | } | |
404 | } else { | |
405 | println!("unrecognized option '{}'", opt); | |
406 | } | |
407 | } | |
408 | true | |
409 | } | |
df37d3b1 KS |
410 | fn parse_scale_options(&mut self, opts: &str) -> bool { |
411 | for opt in opts.split(',') { | |
412 | let oval: Vec<_> = opt.split('=').collect(); | |
413 | if oval.len() == 1 { | |
414 | self.scale_opts.push((oval[0].to_string(), "".to_string())); | |
415 | } else if oval.len() == 2 { | |
416 | self.scale_opts.push((oval[0].to_string(), oval[1].to_string())); | |
417 | } else { | |
418 | println!("unrecognized option '{}'", opt); | |
419 | return false; | |
420 | } | |
421 | } | |
422 | true | |
423 | } | |
73f889bf | 424 | fn apply_decoder_options(&self, dec: &mut dyn NADecoder, str_id: u32) { |
6dc69f51 KS |
425 | if let Some(str_idx) = self.istr_opts.iter().position(|str| str.id == str_id) { |
426 | let dec_opts = dec.get_supported_options(); | |
427 | if dec_opts.is_empty() { return; } | |
428 | let name = format!("input stream {}", str_id); | |
429 | parse_and_apply_options!(dec, &self.istr_opts[str_idx].dec_opts, name); | |
430 | } | |
431 | } | |
432 | fn register_output_stream(&mut self, cname: &str, istr: NAStreamRef, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool { | |
433 | let out_id = out_sm.get_num_streams() as u32; | |
434 | if let Some(str_idx) = self.istr_opts.iter().position(|str| str.id == (istr.get_num() as u32)) { | |
435 | if self.istr_opts[str_idx].drop { | |
436 | self.encoders.push(OutputMode::Drop); | |
437 | return true; | |
438 | } | |
439 | } | |
440 | ||
441 | if let Some(str_idx) = self.ostr_opts.iter().position(|str| str.id == out_id) { | |
442 | let oopts = &mut self.ostr_opts[str_idx]; | |
a7b5f008 | 443 | if oopts.enc_name.as_str() == "copy" && (cname == "any" || istr.get_info().get_name() == cname) { |
86c54d88 | 444 | out_sm.add_stream_ref(istr); |
6dc69f51 KS |
445 | self.encoders.push(OutputMode::Copy(out_id)); |
446 | } else if cname == "any" || oopts.enc_name.as_str() == cname { | |
447 | let enc_create = enc_reg.find_encoder(oopts.enc_name.as_str()); | |
448 | if enc_create.is_none() { | |
449 | println!("encoder '{}' not found", oopts.enc_name.as_str()); | |
450 | return false; | |
451 | } | |
452 | let mut encoder = (enc_create.unwrap())(); | |
eb563e3d | 453 | let forced_out = oopts.enc_params.format != NACodecTypeInfo::None; |
6dc69f51 KS |
454 | if oopts.enc_params.format == NACodecTypeInfo::None { |
455 | oopts.enc_params.format = istr.get_info().get_properties(); | |
456 | } | |
457 | if oopts.enc_params.tb_num == 0 { | |
458 | oopts.enc_params.tb_num = istr.tb_num; | |
459 | oopts.enc_params.tb_den = istr.tb_den; | |
460 | } | |
461 | let ret_eparams = encoder.negotiate_format(&oopts.enc_params); | |
462 | if ret_eparams.is_err() { | |
463 | println!("cannot negotiate encoding parameters"); | |
464 | return false; | |
465 | } | |
466 | let ret_eparams = ret_eparams.unwrap(); | |
467 | ||
468 | //todo check for params mismatch | |
469 | let cvt = match (&oopts.enc_params.format, &ret_eparams.format) { | |
470 | (NACodecTypeInfo::Video(svinfo), NACodecTypeInfo::Video(dvinfo)) => { | |
eb563e3d | 471 | if svinfo == dvinfo && !forced_out { |
6dc69f51 KS |
472 | OutputConvert::None |
473 | } else { | |
474 | let ofmt = ScaleInfo { fmt: dvinfo.format, width: dvinfo.width, height: dvinfo.height }; | |
df37d3b1 | 475 | let ret = NAScale::new_with_options(ofmt, ofmt, &self.scale_opts); |
6dc69f51 KS |
476 | if ret.is_err() { |
477 | println!("cannot create scaler"); | |
478 | return false; | |
479 | } | |
480 | let scaler = ret.unwrap(); | |
481 | let ret = alloc_video_buffer(*dvinfo, 4); | |
482 | if ret.is_err() { | |
483 | println!("cannot create scaler buffer"); | |
484 | return false; | |
485 | } | |
486 | let cvt_buf = ret.unwrap(); | |
487 | OutputConvert::Video(scaler, cvt_buf) | |
488 | } | |
489 | }, | |
490 | (NACodecTypeInfo::Audio(sainfo), NACodecTypeInfo::Audio(dainfo)) => { | |
491 | if sainfo == dainfo { | |
492 | OutputConvert::None | |
493 | } else { | |
494 | let dchmap = match dainfo.channels { | |
495 | 1 => NAChannelMap::from_ms_mapping(0x4), | |
496 | 2 => NAChannelMap::from_ms_mapping(0x3), | |
497 | _ => { | |
498 | println!("can't generate default channel map for {} channels", dainfo.channels); | |
499 | return false; | |
500 | }, | |
501 | }; | |
502 | //todo channelmap | |
503 | OutputConvert::Audio(*dainfo, dchmap) | |
504 | } | |
505 | }, | |
506 | _ => OutputConvert::None, | |
507 | }; | |
508 | let ret = encoder.init(out_id, ret_eparams); | |
509 | if ret.is_err() { | |
510 | println!("error initialising encoder"); | |
511 | return false; | |
512 | } | |
513 | out_sm.add_stream_ref(ret.unwrap()); | |
514 | ||
515 | let name = format!("output stream {}", out_id); | |
516 | parse_and_apply_options!(encoder, &oopts.enc_opts, name); | |
517 | ||
518 | self.encoders.push(OutputMode::Encode(out_id, encoder, cvt)); | |
519 | } else { | |
520 | println!("encoder {} is not supported by output (expected {})", istr.id, istr.get_info().get_name()); | |
521 | return false; | |
522 | } | |
86c54d88 KS |
523 | } else if cname == "any" || istr.get_info().get_name() == cname { |
524 | out_sm.add_stream_ref(istr); | |
525 | self.encoders.push(OutputMode::Copy(out_id)); | |
6dc69f51 | 526 | } else { |
6dc69f51 KS |
527 | println!("stream {} ({}) can't be handled", istr.id, istr.get_info().get_name()); |
528 | // todo autoselect encoder? | |
86c54d88 | 529 | return false; |
6dc69f51 KS |
530 | } |
531 | true | |
532 | } | |
533 | fn map_single(&mut self, cname: &str, ctype: StreamType, src_sm: &StreamManager, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool { | |
534 | let mut found_stream = false; | |
535 | for istr in src_sm.iter() { | |
536 | if istr.get_media_type() != ctype || found_stream { | |
537 | self.encoders.push(OutputMode::Drop); | |
538 | } else { | |
539 | if !self.register_output_stream(cname, istr, out_sm, enc_reg) { | |
540 | return false; | |
541 | } | |
542 | found_stream = true; | |
543 | } | |
544 | } | |
545 | found_stream | |
546 | } | |
547 | fn negotiate_stream_map(&mut self, src_sm: &StreamManager, mux_caps: MuxerCapabilities, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool { | |
548 | match mux_caps { | |
549 | MuxerCapabilities::SingleVideo(cname) => { | |
550 | if self.no_video { return false; } | |
551 | self.map_single(cname, StreamType::Video, src_sm, out_sm, enc_reg) | |
552 | }, | |
553 | MuxerCapabilities::SingleAudio(cname) => { | |
554 | if self.no_audio { return false; } | |
555 | self.map_single(cname, StreamType::Audio, src_sm, out_sm, enc_reg) | |
556 | }, | |
557 | MuxerCapabilities::SingleVideoAndAudio(vname, aname) => { | |
558 | let mut found_vid = false; | |
559 | let mut found_aud = false; | |
560 | for istr in src_sm.iter() { | |
561 | if istr.get_media_type() == StreamType::Video && !found_vid && !self.no_video { | |
562 | if !self.register_output_stream(vname, istr, out_sm, enc_reg) { | |
563 | return false; | |
564 | } | |
565 | found_vid = true; | |
566 | } else if istr.get_media_type() == StreamType::Audio && !found_aud && !self.no_audio { | |
567 | if !self.register_output_stream(aname, istr, out_sm, enc_reg) { | |
568 | return false; | |
569 | } | |
570 | found_aud = true; | |
571 | } else { | |
572 | self.encoders.push(OutputMode::Drop); | |
573 | } | |
574 | } | |
575 | found_vid | found_aud | |
576 | }, | |
577 | MuxerCapabilities::OnlyVideo => { | |
578 | if self.no_video { return false; } | |
579 | ||
580 | let mut found_vid = false; | |
581 | for istr in src_sm.iter() { | |
582 | if istr.get_media_type() == StreamType::Video && !found_vid { | |
583 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
584 | return false; | |
585 | } | |
586 | found_vid = true; | |
587 | } else { | |
588 | self.encoders.push(OutputMode::Drop); | |
589 | } | |
590 | } | |
591 | found_vid | |
592 | }, | |
593 | MuxerCapabilities::OnlyAudio => { | |
594 | if self.no_audio { return false; } | |
595 | ||
596 | let mut found_aud = false; | |
597 | for istr in src_sm.iter() { | |
598 | if istr.get_media_type() == StreamType::Audio && !found_aud { | |
599 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
600 | return false; | |
601 | } | |
602 | found_aud = true; | |
603 | } else { | |
604 | self.encoders.push(OutputMode::Drop); | |
605 | } | |
606 | } | |
607 | found_aud | |
608 | }, | |
609 | MuxerCapabilities::Universal => { | |
610 | for istr in src_sm.iter() { | |
611 | if (istr.get_media_type() == StreamType::Video && self.no_video) || | |
612 | (istr.get_media_type() == StreamType::Audio && self.no_audio) { | |
613 | self.encoders.push(OutputMode::Drop); | |
614 | continue; | |
615 | } | |
616 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
617 | return false; | |
618 | } | |
619 | } | |
620 | true | |
621 | }, | |
622 | } | |
623 | } | |
624 | } | |
625 | ||
df37d3b1 | 626 | fn encode_frame(dst_id: u32, encoder: &mut Box<dyn NAEncoder>, cvt: &mut OutputConvert, frm: NAFrameRef, scale_opts: &[(String, String)]) -> bool { |
d9fe2b71 KS |
627 | let buf = frm.get_buffer(); |
628 | let cbuf = match cvt { | |
629 | OutputConvert::None => buf, | |
630 | OutputConvert::Video(ref mut scaler, ref mut dbuf) => { | |
631 | let cur_ifmt = get_scale_fmt_from_pic(&buf); | |
632 | let last_ifmt = scaler.get_in_fmt(); | |
633 | if cur_ifmt != last_ifmt { | |
634 | let ofmt = scaler.get_out_fmt(); | |
df37d3b1 | 635 | let ret = NAScale::new_with_options(cur_ifmt, ofmt, scale_opts); |
d9fe2b71 KS |
636 | if ret.is_err() { |
637 | println!("error re-initialising scaler for {} -> {}", cur_ifmt, ofmt); | |
638 | return false; | |
639 | } | |
640 | *scaler = ret.unwrap(); | |
641 | } | |
642 | let ret = scaler.convert(&buf, dbuf); | |
643 | if ret.is_err() { | |
644 | println!("error converting frame for encoding"); | |
645 | return false; | |
646 | } | |
647 | dbuf.clone() | |
648 | }, | |
649 | OutputConvert::Audio(ref dinfo, ref dchmap) => { | |
650 | let ret = convert_audio_frame(&buf, dinfo, dchmap); | |
651 | if ret.is_err() { | |
652 | println!("error converting audio for stream {}", dst_id); | |
653 | return false; | |
654 | } | |
655 | ret.unwrap() | |
656 | }, | |
657 | }; | |
658 | let cfrm = NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cbuf); | |
659 | encoder.encode(&cfrm).unwrap(); | |
660 | true | |
661 | } | |
662 | ||
6dc69f51 KS |
663 | macro_rules! next_arg { |
664 | ($args: expr, $arg_idx: expr) => { | |
665 | if $arg_idx + 1 >= $args.len() { | |
666 | println!("codec name is required"); | |
667 | } | |
668 | $arg_idx += 1; | |
669 | } | |
670 | } | |
671 | ||
86c54d88 | 672 | #[allow(clippy::single_match)] |
6dc69f51 KS |
673 | fn main() { |
674 | let args: Vec<_> = env::args().collect(); | |
675 | ||
676 | if args.len() == 1 { | |
071d353e KS |
677 | println!("usage: nihav-encoder [options] --input inputfile --output outputfile"); |
678 | println!(" use nihav-encoder --help to list all available options"); | |
679 | return; | |
680 | } | |
681 | if args.len() == 2 && (args[1] == "--help" || args[1] == "-h") { | |
682 | println!("usage: nihav-encoder [options] --input inputfile --output outputfile"); | |
683 | println!(" query options:"); | |
684 | println!(" --list-{{decoders,encoders,demuxers,muxers}} - lists all available decoders/encoders/demuxers/muxers"); | |
685 | println!(" --query-{{decoder,encoder,demuxer,muxer}}-options name - lists all options recognized by that decoder/encoder/demuxer/muxer"); | |
686 | println!(" processing options:"); | |
687 | println!(" --input inputfile - set input file"); | |
688 | println!(" --input-format fmt - force input format"); | |
689 | println!(" --demuxer-options options - set input demuxer options"); | |
df37d3b1 | 690 | println!(" --scale-options options - set scaler options"); |
071d353e KS |
691 | println!(" --output outputfile - set output file"); |
692 | println!(" --output-format fmt - force output format"); | |
693 | println!(" --muxer-options options - set output muxer options"); | |
694 | println!(" --no-audio - do not decode audio streams"); | |
695 | println!(" --no-video - do not decode video streams"); | |
696 | println!(" --start starttime - start decoding from given position"); | |
697 | println!(" --end endtime - end decoding at given position"); | |
698 | println!(" --istreamX options - set options for input stream X"); | |
699 | println!(" --ostreamX options - set options for output stream X"); | |
700 | println!(); | |
701 | println!(" (de)muxer and stream options are passed as comma-separated list e.g. --ostream0 width=320,height=240,flip"); | |
6dc69f51 KS |
702 | return; |
703 | } | |
704 | ||
91a15e39 | 705 | let full_reg = FullRegister::new(); |
6dc69f51 KS |
706 | |
707 | let mut transcoder = Transcoder::new(); | |
708 | ||
709 | let mut arg_idx = 1; | |
5a8a7cdb | 710 | let mut printed_info = false; |
6dc69f51 KS |
711 | while arg_idx < args.len() { |
712 | match args[arg_idx].as_str() { | |
e6cb09af | 713 | "--list-decoders" => { |
91a15e39 | 714 | if full_reg.dec_reg.iter().len() > 0 { |
e6cb09af | 715 | println!("Registered decoders:"); |
91a15e39 | 716 | for dec in full_reg.dec_reg.iter() { |
e6cb09af KS |
717 | let cdesc = register::get_codec_description(dec.name); |
718 | let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" }; | |
719 | println!(" {} ({})", dec.name, full_name); | |
720 | } | |
721 | } else { | |
722 | println!("No registered decoders."); | |
723 | } | |
5a8a7cdb | 724 | printed_info = true; |
e6cb09af KS |
725 | }, |
726 | "--list-encoders" => { | |
91a15e39 | 727 | if full_reg.enc_reg.iter().len() > 0 { |
e6cb09af | 728 | println!("Registered encoders:"); |
91a15e39 | 729 | for enc in full_reg.enc_reg.iter() { |
e6cb09af KS |
730 | let cdesc = register::get_codec_description(enc.name); |
731 | let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" }; | |
732 | println!(" {} ({})", enc.name, full_name); | |
733 | } | |
734 | } else { | |
735 | println!("No registered encoders."); | |
736 | } | |
5a8a7cdb | 737 | printed_info = true; |
e6cb09af KS |
738 | }, |
739 | "--list-demuxers" => { | |
740 | print!("Registered demuxers:"); | |
91a15e39 | 741 | for dmx in full_reg.dmx_reg.iter() { |
e6cb09af KS |
742 | print!(" {}", dmx.get_name()); |
743 | } | |
744 | println!(); | |
5a8a7cdb | 745 | printed_info = true; |
e6cb09af KS |
746 | }, |
747 | "--list-muxers" => { | |
748 | print!("Registered muxers:"); | |
91a15e39 | 749 | for mux in full_reg.mux_reg.iter() { |
e6cb09af KS |
750 | print!(" {}", mux.get_name()); |
751 | } | |
752 | println!(); | |
5a8a7cdb | 753 | printed_info = true; |
e6cb09af | 754 | }, |
6dc69f51 KS |
755 | "--query-decoder-options" => { |
756 | next_arg!(args, arg_idx); | |
757 | let cname = args[arg_idx].as_str(); | |
91a15e39 | 758 | if let Some(decfunc) = full_reg.dec_reg.find_decoder(cname) { |
6dc69f51 KS |
759 | let dec = (decfunc)(); |
760 | let opts = dec.get_supported_options(); | |
761 | print_options(cname, opts); | |
762 | } else { | |
763 | println!("codec {} is not found", cname); | |
764 | } | |
5a8a7cdb | 765 | printed_info = true; |
6dc69f51 KS |
766 | }, |
767 | "--query-demuxer-options" => { | |
768 | next_arg!(args, arg_idx); | |
769 | let dname = args[arg_idx].as_str(); | |
770 | let mut mr = MemoryReader::new_read(&[]); | |
771 | let mut br = ByteReader::new(&mut mr); | |
91a15e39 | 772 | if let Some(dmx_creator) = full_reg.dmx_reg.find_demuxer(dname) { |
6dc69f51 KS |
773 | let dmx = dmx_creator.new_demuxer(&mut br); |
774 | let opts = dmx.get_supported_options(); | |
775 | print_options(dname, opts); | |
776 | } else { | |
777 | println!("demuxer {} is not found", dname); | |
778 | } | |
5a8a7cdb | 779 | printed_info = true; |
6dc69f51 KS |
780 | }, |
781 | "--query-encoder-options" => { | |
782 | next_arg!(args, arg_idx); | |
783 | let cname = args[arg_idx].as_str(); | |
91a15e39 | 784 | if let Some(encfunc) = full_reg.enc_reg.find_encoder(cname) { |
6dc69f51 KS |
785 | let enc = (encfunc)(); |
786 | let opts = enc.get_supported_options(); | |
787 | print_options(cname, opts); | |
788 | } else { | |
789 | println!("codec {} is not found", cname); | |
790 | } | |
5a8a7cdb | 791 | printed_info = true; |
6dc69f51 KS |
792 | }, |
793 | "--query-muxer-options" => { | |
794 | next_arg!(args, arg_idx); | |
795 | let name = args[arg_idx].as_str(); | |
796 | let mut data = []; | |
797 | let mut mw = MemoryWriter::new_write(&mut data); | |
798 | let mut bw = ByteWriter::new(&mut mw); | |
91a15e39 | 799 | if let Some(mux_creator) = full_reg.mux_reg.find_muxer(name) { |
6dc69f51 KS |
800 | let mux = mux_creator.new_muxer(&mut bw); |
801 | let opts = mux.get_supported_options(); | |
802 | print_options(name, opts); | |
803 | } else { | |
804 | println!("muxer {} is not found", name); | |
805 | } | |
5a8a7cdb | 806 | printed_info = true; |
6dc69f51 | 807 | }, |
4b5c61e2 | 808 | "--input" | "-i" => { |
6dc69f51 KS |
809 | next_arg!(args, arg_idx); |
810 | transcoder.input_name = args[arg_idx].clone(); | |
811 | }, | |
812 | "--input-format" => { | |
813 | next_arg!(args, arg_idx); | |
814 | transcoder.input_fmt = Some(args[arg_idx].clone()); | |
815 | }, | |
4b5c61e2 | 816 | "--output" | "-o" => { |
6dc69f51 KS |
817 | next_arg!(args, arg_idx); |
818 | transcoder.output_name = args[arg_idx].clone(); | |
819 | }, | |
820 | "--output-format" => { | |
821 | next_arg!(args, arg_idx); | |
822 | transcoder.output_fmt = Some(args[arg_idx].clone()); | |
823 | }, | |
824 | "--demuxer-options" => { | |
825 | next_arg!(args, arg_idx); | |
91a15e39 | 826 | if !transcoder.parse_demuxer_options(&args[arg_idx], &full_reg.dmx_reg) { |
6dc69f51 KS |
827 | println!("invalid demuxer option syntax"); |
828 | return; | |
829 | } | |
830 | }, | |
df37d3b1 KS |
831 | "--scale-options" => { |
832 | next_arg!(args, arg_idx); | |
833 | if !transcoder.parse_scale_options(&args[arg_idx]) { | |
834 | println!("invalid scale option syntax"); | |
835 | return; | |
836 | } | |
837 | }, | |
4b5c61e2 | 838 | "--no-video" | "-vn" => { |
6dc69f51 KS |
839 | transcoder.no_video = true; |
840 | }, | |
4b5c61e2 | 841 | "--no-audio" | "-an" => { |
6dc69f51 KS |
842 | transcoder.no_audio = true; |
843 | }, | |
951916e8 KS |
844 | "--start" => { |
845 | next_arg!(args, arg_idx); | |
846 | let ret = args[arg_idx].parse::<NATimePoint>(); | |
847 | if let Ok(val) = ret { | |
848 | transcoder.start = val; | |
849 | } else { | |
850 | println!("invalid start time"); | |
851 | return; | |
852 | } | |
853 | }, | |
854 | "--end" => { | |
855 | next_arg!(args, arg_idx); | |
856 | let ret = args[arg_idx].parse::<NATimePoint>(); | |
857 | if let Ok(val) = ret { | |
858 | transcoder.end = val; | |
859 | } else { | |
b0c51548 | 860 | println!("invalid end time"); |
951916e8 KS |
861 | return; |
862 | } | |
863 | }, | |
6dc69f51 KS |
864 | "--muxer-options" => { |
865 | next_arg!(args, arg_idx); | |
91a15e39 | 866 | if !transcoder.parse_muxer_options(&args[arg_idx], &full_reg.mux_reg) { |
6dc69f51 KS |
867 | println!("invalid muxer option syntax"); |
868 | return; | |
869 | } | |
870 | }, | |
871 | _ => { | |
872 | if args[arg_idx].starts_with("--istream") { | |
873 | let opt0 = &args[arg_idx]; | |
874 | next_arg!(args, arg_idx); | |
875 | if !transcoder.parse_istream_options(opt0, &args[arg_idx]) { | |
876 | println!("invalid input stream option syntax"); | |
877 | return; | |
878 | } | |
879 | } else if args[arg_idx].starts_with("--ostream") { | |
880 | let opt0 = &args[arg_idx]; | |
881 | next_arg!(args, arg_idx); | |
91a15e39 | 882 | if !transcoder.parse_ostream_options(opt0, &args[arg_idx], &full_reg.enc_reg) { |
6dc69f51 KS |
883 | println!("invalid output stream option syntax"); |
884 | return; | |
885 | } | |
886 | } else if args[arg_idx].starts_with("--") { | |
887 | println!("unknown option"); | |
888 | } else { | |
889 | println!("unrecognized argument"); | |
890 | } | |
891 | }, | |
892 | }; | |
893 | arg_idx += 1; | |
894 | } | |
895 | ||
5a8a7cdb KS |
896 | if printed_info { |
897 | return; | |
898 | } | |
899 | ||
86c54d88 | 900 | if transcoder.input_name.is_empty() { |
6dc69f51 KS |
901 | println!("no input name provided"); |
902 | return; | |
903 | } | |
86c54d88 | 904 | if transcoder.output_name.is_empty() { |
6dc69f51 KS |
905 | println!("no output name provided"); |
906 | return; | |
907 | } | |
908 | ||
909 | let res = File::open(transcoder.input_name.as_str()); | |
910 | if res.is_err() { | |
911 | println!("error opening input"); | |
912 | return; | |
913 | } | |
91a15e39 KS |
914 | let file = res.unwrap(); |
915 | let file = BufReader::new(file); | |
916 | let mut fr = FileReader::new_read(file); | |
6dc69f51 | 917 | let mut br = ByteReader::new(&mut fr); |
91a15e39 KS |
918 | let (is_raw, start, end) = if transcoder.input_fmt.is_none() { |
919 | detect_tags(&mut br) | |
6dc69f51 | 920 | } else { |
91a15e39 | 921 | (false, 0, None) |
6dc69f51 | 922 | }; |
91a15e39 KS |
923 | |
924 | let mut nfr: Box<dyn ByteIO>; | |
925 | if start != 0 || end.is_some() { | |
926 | let file = fr.finish(); | |
927 | nfr = Box::new(BoundedFileReader::new_read(file, start, end).unwrap()); | |
928 | } else { | |
929 | nfr = Box::new(fr); | |
930 | } | |
931 | let mut br = ByteReader::new(nfr.as_mut()); | |
932 | ||
933 | let mut dmx = DemuxerObject::create(&mut br, &full_reg, transcoder.input_name.as_str(), &transcoder.input_fmt, is_raw); | |
934 | if dmx.is_none() { | |
935 | println!("cannot find demuxer for '{}'", transcoder.input_name.as_str()); | |
6dc69f51 KS |
936 | return; |
937 | } | |
6dc69f51 KS |
938 | parse_and_apply_options!(dmx, &transcoder.demux_opts, "input"); |
939 | for i in 0..dmx.get_num_streams() { | |
940 | let s = dmx.get_stream(i).unwrap(); | |
941 | let info = s.get_info(); | |
91a15e39 | 942 | let decfunc = full_reg.dec_reg.find_decoder(info.get_name()); |
6dc69f51 KS |
943 | println!("stream {} - {} {}", i, s, info.get_name()); |
944 | let str_id = s.get_num() as u32; | |
945 | if let Some(create_dec) = decfunc { | |
946 | let mut dec = (create_dec)(); | |
947 | let mut dsupp = Box::new(NADecoderSupport::new()); | |
948 | let ret = dec.init(&mut dsupp, info.clone()); | |
949 | if ret.is_err() { | |
950 | println!("Error initialising decoder '{}' for stream {}", info.get_name(), str_id); | |
951 | return; | |
952 | } | |
953 | transcoder.apply_decoder_options(dec.as_mut(), str_id); | |
d9fe2b71 KS |
954 | let desc = register::get_codec_description(info.get_name()); |
955 | let has_b = if let Some(desc) = desc { | |
956 | desc.has_reorder() | |
957 | } else { | |
958 | println!("No codec description found, using B-frame reorderer."); | |
959 | true | |
960 | }; | |
961 | let reord: Box<dyn FrameReorderer> = if has_b { Box::new(IPBReorderer::new()) } else { Box::new(NoReorderer::new()) }; | |
962 | transcoder.decoders.push(Some((dsupp, dec, reord))); | |
6dc69f51 KS |
963 | } else { |
964 | println!("No decoder for stream {} ({}) is found", str_id, info.get_name()); | |
965 | transcoder.decoders.push(None); | |
966 | } | |
967 | } | |
951916e8 KS |
968 | if transcoder.start != NATimePoint::None { |
969 | let ret = dmx.seek(transcoder.start); | |
970 | if ret.is_err() { | |
971 | println!(" failed to seek to {} error {:?}", transcoder.start, ret.err().unwrap()); | |
972 | } | |
973 | } | |
6dc69f51 KS |
974 | |
975 | let output_fmt = if let Some(ref str) = transcoder.output_fmt { | |
976 | str | |
86c54d88 KS |
977 | } else if let Some(str) = detect::detect_format_by_name(transcoder.output_name.as_str()) { |
978 | str | |
6dc69f51 | 979 | } else { |
86c54d88 KS |
980 | println!("Cannot guess muxer for output"); |
981 | return; | |
6dc69f51 | 982 | }; |
91a15e39 | 983 | let ret = full_reg.mux_reg.find_muxer(output_fmt); |
6dc69f51 KS |
984 | let ofmt = output_fmt.to_string(); |
985 | ||
986 | if ret.is_none() { | |
987 | println!("cannot find muxer '{}'", output_fmt); | |
988 | } | |
989 | let mux_creator = ret.unwrap(); | |
990 | ||
991 | let mux_caps = mux_creator.get_capabilities(); | |
992 | let mut out_sm = StreamManager::new(); | |
91a15e39 | 993 | if !transcoder.negotiate_stream_map(dmx.get_stream_manager(), mux_caps, &mut out_sm, &full_reg.enc_reg) { |
6dc69f51 KS |
994 | println!("cannot determine stream map"); |
995 | return; | |
996 | } | |
997 | ||
998 | let ret = File::create(transcoder.output_name.as_str()); | |
999 | if ret.is_err() { | |
1000 | println!("cannot open output file"); | |
1001 | return; | |
1002 | } | |
1003 | let mut fw = FileWriter::new_write(ret.unwrap()); | |
1004 | let mut bw = ByteWriter::new(&mut fw); | |
1005 | let ret = create_muxer(mux_creator, out_sm, &mut bw); | |
1006 | if let Err(err) = ret { | |
1007 | println!("cannot create muxer instance {:?}", err); | |
1008 | return; | |
1009 | } | |
1010 | let mut mux = ret.unwrap(); | |
1011 | parse_and_apply_options!(mux, &transcoder.mux_opts, "output"); | |
1012 | ||
1013 | println!("Output {} muxer {}", transcoder.output_name, ofmt); | |
1014 | for ostr in mux.get_streams() { | |
1015 | println!(" #{}: {} {}", ostr.get_num(), ostr, ostr.get_info().get_name()); | |
1016 | } | |
1017 | ||
951916e8 | 1018 | 'main_loop: loop { |
6dc69f51 KS |
1019 | let pktres = dmx.get_frame(); |
1020 | if let Err(DemuxerError::EOF) = pktres { break; } | |
1021 | if pktres.is_err() { | |
1022 | println!("demuxing error"); | |
1023 | break; | |
1024 | } | |
1025 | let mut pkt = pktres.unwrap(); | |
f88b8fb4 | 1026 | if transcoder.start != NATimePoint::None && pkt.ts.less_than(transcoder.start) { continue; } |
6dc69f51 KS |
1027 | let src_id = pkt.get_stream().get_num(); |
1028 | match transcoder.encoders[src_id] { | |
1029 | OutputMode::Drop => {}, | |
1030 | OutputMode::Copy(dst_id) => { | |
1031 | let dstr = mux.get_stream(dst_id as usize).unwrap(); | |
1032 | pkt.reassign(dstr, pkt.get_time_information()); | |
951916e8 | 1033 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; } |
6dc69f51 KS |
1034 | if mux.mux_frame(pkt).is_err() { |
1035 | println!("error muxing packet"); | |
1036 | break; | |
1037 | } | |
1038 | }, | |
1039 | OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) => { | |
d9fe2b71 | 1040 | if let Some((ref mut dsupp, ref mut decoder, ref mut reorderer)) = transcoder.decoders[src_id] { |
6dc69f51 | 1041 | let ret = decoder.decode(dsupp, &pkt); |
f88b8fb4 KS |
1042 | if let (true, Err(DecoderError::MissingReference)) = (transcoder.start != NATimePoint::None, &ret) { |
1043 | continue; | |
1044 | } | |
6dc69f51 KS |
1045 | if ret.is_err() { |
1046 | println!("error decoding stream {}", src_id); | |
1047 | break; | |
1048 | } | |
1049 | let frm = ret.unwrap(); | |
d9fe2b71 KS |
1050 | reorderer.add_frame(frm); |
1051 | while let Some(frm) = reorderer.get_frame() { | |
df37d3b1 | 1052 | if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) { |
d9fe2b71 KS |
1053 | break; |
1054 | } | |
1055 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1056 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; } | |
1057 | mux.mux_frame(pkt).unwrap(); | |
1058 | } | |
6dc69f51 KS |
1059 | } |
1060 | } else { | |
1061 | println!("no decoder for stream {}", src_id); | |
1062 | break; | |
1063 | } | |
1064 | }, | |
1065 | }; | |
1066 | } | |
d9fe2b71 KS |
1067 | 'reord_flush_loop: for str in dmx.get_streams() { |
1068 | let src_id = str.get_num(); | |
1069 | if let OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) = transcoder.encoders[src_id] { | |
1070 | if let Some((_, _, ref mut reorderer)) = transcoder.decoders[src_id] { | |
1071 | while let Some(frm) = reorderer.get_last_frames() { | |
df37d3b1 | 1072 | if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) { |
d9fe2b71 KS |
1073 | break; |
1074 | } | |
1075 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1076 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'reord_flush_loop; } | |
1077 | mux.mux_frame(pkt).unwrap(); | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | } | |
6dc69f51 KS |
1083 | 'flush_loop: for enc in transcoder.encoders.iter_mut() { |
1084 | match enc { | |
1085 | OutputMode::Encode(str_id, ref mut encoder, _) => { | |
1086 | let ret = encoder.flush(); | |
1087 | if ret.is_err() { | |
1088 | println!("error flushing encoder for stream {}", str_id); | |
1089 | break; | |
1090 | } else { | |
1091 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1092 | if mux.mux_frame(pkt).is_err() { | |
1093 | println!("error muxing packet"); | |
1094 | break 'flush_loop; | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | }, | |
1099 | _ => {}, | |
1100 | }; | |
1101 | } | |
1102 | ||
1103 | let ret = mux.end(); | |
1104 | if ret.is_err() { | |
1105 | println!("error at finalising muxing"); | |
1106 | } | |
1107 | } |