]>
Commit | Line | Data |
---|---|---|
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; | |
7 | use std::io::BufReader; | |
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::*; | |
14 | use nihav_core::reorder::*; | |
15 | use nihav_core::scale::*; | |
16 | use nihav_core::soundcvt::*; | |
17 | use nihav_registry::detect; | |
18 | use nihav_registry::register; | |
19 | use std::env; | |
20 | ||
21 | mod demux; | |
22 | use crate::demux::*; | |
23 | mod null; | |
24 | ||
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 | ||
60 | #[allow(clippy::large_enum_variant)] | |
61 | enum OutputMode { | |
62 | Drop, | |
63 | Copy(u32), | |
64 | Encode(u32, Box<dyn NAEncoder>, OutputConvert), | |
65 | } | |
66 | ||
67 | #[derive(Default)] | |
68 | #[allow(clippy::type_complexity)] | |
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>, | |
78 | scale_opts: Vec<(String, String)>, | |
79 | decoders: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>, Box<dyn FrameReorderer>)>>, | |
80 | encoders: Vec<OutputMode>, | |
81 | no_video: bool, | |
82 | no_audio: bool, | |
83 | start: NATimePoint, | |
84 | end: NATimePoint, | |
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() { | |
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 { | |
100 | let arg = if let Some(ref str) = opt.value { Some(str) } else { None }; | |
101 | let ret = opt_def.parse(&opt.name, arg); | |
102 | if let Ok((val, _)) = ret { | |
103 | opts.push(val); | |
104 | } else { | |
105 | println!("invalid option {} for {}", opt.name, $name); | |
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]; | |
133 | ||
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]; | |
164 | ||
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] { | |
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 | }, | |
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]); | |
218 | ostr.enc_name = oval[1].to_string(); | |
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 | }, | |
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 | }, | |
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 | }, | |
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 | },*/ | |
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 | } | |
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 | } | |
424 | fn apply_decoder_options(&self, dec: &mut dyn NADecoder, str_id: u32) { | |
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]; | |
443 | if oopts.enc_name.as_str() == "copy" && (cname == "any" || istr.get_info().get_name() == cname) { | |
444 | out_sm.add_stream_ref(istr); | |
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())(); | |
453 | let forced_out = oopts.enc_params.format != NACodecTypeInfo::None; | |
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)) => { | |
471 | if svinfo == dvinfo && !forced_out { | |
472 | OutputConvert::None | |
473 | } else { | |
474 | let ofmt = ScaleInfo { fmt: dvinfo.format, width: dvinfo.width, height: dvinfo.height }; | |
475 | let ret = NAScale::new_with_options(ofmt, ofmt, &self.scale_opts); | |
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 | } | |
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)); | |
526 | } else { | |
527 | let mut oopts = OutputStreamOptions {id: out_id, enc_name: cname.to_owned(), enc_params: EncodeParameters::default(), enc_opts: Vec::new() }; | |
528 | ||
529 | let enc_create = enc_reg.find_encoder(cname); | |
530 | if enc_create.is_none() { | |
531 | println!("encoder '{}' not found", oopts.enc_name.as_str()); | |
532 | return false; | |
533 | } | |
534 | let mut encoder = (enc_create.unwrap())(); | |
535 | oopts.enc_params.format = istr.get_info().get_properties(); | |
536 | oopts.enc_params.tb_num = istr.tb_num; | |
537 | oopts.enc_params.tb_den = istr.tb_den; | |
538 | let ret_eparams = encoder.negotiate_format(&oopts.enc_params); | |
539 | if ret_eparams.is_err() { | |
540 | println!("cannot negotiate encoding parameters"); | |
541 | return false; | |
542 | } | |
543 | let ret_eparams = ret_eparams.unwrap(); | |
544 | ||
545 | //todo check for params mismatch | |
546 | let cvt = match (&oopts.enc_params.format, &ret_eparams.format) { | |
547 | (NACodecTypeInfo::Video(svinfo), NACodecTypeInfo::Video(dvinfo)) => { | |
548 | if svinfo == dvinfo { | |
549 | OutputConvert::None | |
550 | } else { | |
551 | let ofmt = ScaleInfo { fmt: dvinfo.format, width: dvinfo.width, height: dvinfo.height }; | |
552 | let ret = NAScale::new_with_options(ofmt, ofmt, &self.scale_opts); | |
553 | if ret.is_err() { | |
554 | println!("cannot create scaler"); | |
555 | return false; | |
556 | } | |
557 | let scaler = ret.unwrap(); | |
558 | let ret = alloc_video_buffer(*dvinfo, 4); | |
559 | if ret.is_err() { | |
560 | println!("cannot create scaler buffer"); | |
561 | return false; | |
562 | } | |
563 | let cvt_buf = ret.unwrap(); | |
564 | OutputConvert::Video(scaler, cvt_buf) | |
565 | } | |
566 | }, | |
567 | (NACodecTypeInfo::Audio(sainfo), NACodecTypeInfo::Audio(dainfo)) => { | |
568 | if sainfo == dainfo { | |
569 | OutputConvert::None | |
570 | } else { | |
571 | let dchmap = match dainfo.channels { | |
572 | 1 => NAChannelMap::from_ms_mapping(0x4), | |
573 | 2 => NAChannelMap::from_ms_mapping(0x3), | |
574 | _ => { | |
575 | println!("can't generate default channel map for {} channels", dainfo.channels); | |
576 | return false; | |
577 | }, | |
578 | }; | |
579 | //todo channelmap | |
580 | OutputConvert::Audio(*dainfo, dchmap) | |
581 | } | |
582 | }, | |
583 | _ => OutputConvert::None, | |
584 | }; | |
585 | let ret = encoder.init(out_id, ret_eparams); | |
586 | if ret.is_err() { | |
587 | println!("error initialising encoder"); | |
588 | return false; | |
589 | } | |
590 | out_sm.add_stream_ref(ret.unwrap()); | |
591 | self.encoders.push(OutputMode::Encode(out_id, encoder, cvt)); | |
592 | self.ostr_opts.push(oopts); | |
593 | } | |
594 | true | |
595 | } | |
596 | fn map_single(&mut self, cname: &str, ctype: StreamType, src_sm: &StreamManager, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool { | |
597 | let mut found_stream = false; | |
598 | for istr in src_sm.iter() { | |
599 | if istr.get_media_type() != ctype || found_stream { | |
600 | self.encoders.push(OutputMode::Drop); | |
601 | } else { | |
602 | if !self.register_output_stream(cname, istr, out_sm, enc_reg) { | |
603 | return false; | |
604 | } | |
605 | found_stream = true; | |
606 | } | |
607 | } | |
608 | found_stream | |
609 | } | |
610 | fn negotiate_stream_map(&mut self, src_sm: &StreamManager, mux_caps: MuxerCapabilities, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool { | |
611 | match mux_caps { | |
612 | MuxerCapabilities::SingleVideo(cname) => { | |
613 | if self.no_video { return false; } | |
614 | self.map_single(cname, StreamType::Video, src_sm, out_sm, enc_reg) | |
615 | }, | |
616 | MuxerCapabilities::SingleAudio(cname) => { | |
617 | if self.no_audio { return false; } | |
618 | self.map_single(cname, StreamType::Audio, src_sm, out_sm, enc_reg) | |
619 | }, | |
620 | MuxerCapabilities::SingleVideoAndAudio(vname, aname) => { | |
621 | let mut found_vid = false; | |
622 | let mut found_aud = false; | |
623 | for istr in src_sm.iter() { | |
624 | if istr.get_media_type() == StreamType::Video && !found_vid && !self.no_video { | |
625 | if !self.register_output_stream(vname, istr, out_sm, enc_reg) { | |
626 | return false; | |
627 | } | |
628 | found_vid = true; | |
629 | } else if istr.get_media_type() == StreamType::Audio && !found_aud && !self.no_audio { | |
630 | if !self.register_output_stream(aname, istr, out_sm, enc_reg) { | |
631 | return false; | |
632 | } | |
633 | found_aud = true; | |
634 | } else { | |
635 | self.encoders.push(OutputMode::Drop); | |
636 | } | |
637 | } | |
638 | found_vid | found_aud | |
639 | }, | |
640 | MuxerCapabilities::OnlyVideo => { | |
641 | if self.no_video { return false; } | |
642 | ||
643 | let mut found_vid = false; | |
644 | for istr in src_sm.iter() { | |
645 | if istr.get_media_type() == StreamType::Video && !found_vid { | |
646 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
647 | return false; | |
648 | } | |
649 | found_vid = true; | |
650 | } else { | |
651 | self.encoders.push(OutputMode::Drop); | |
652 | } | |
653 | } | |
654 | found_vid | |
655 | }, | |
656 | MuxerCapabilities::OnlyAudio => { | |
657 | if self.no_audio { return false; } | |
658 | ||
659 | let mut found_aud = false; | |
660 | for istr in src_sm.iter() { | |
661 | if istr.get_media_type() == StreamType::Audio && !found_aud { | |
662 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
663 | return false; | |
664 | } | |
665 | found_aud = true; | |
666 | } else { | |
667 | self.encoders.push(OutputMode::Drop); | |
668 | } | |
669 | } | |
670 | found_aud | |
671 | }, | |
672 | MuxerCapabilities::Universal => { | |
673 | for istr in src_sm.iter() { | |
674 | if (istr.get_media_type() == StreamType::Video && self.no_video) || | |
675 | (istr.get_media_type() == StreamType::Audio && self.no_audio) { | |
676 | self.encoders.push(OutputMode::Drop); | |
677 | continue; | |
678 | } | |
679 | if !self.register_output_stream("any", istr, out_sm, enc_reg) { | |
680 | return false; | |
681 | } | |
682 | } | |
683 | true | |
684 | }, | |
685 | } | |
686 | } | |
687 | } | |
688 | ||
689 | fn encode_frame(dst_id: u32, encoder: &mut Box<dyn NAEncoder>, cvt: &mut OutputConvert, frm: NAFrameRef, scale_opts: &[(String, String)]) -> bool { | |
690 | let buf = frm.get_buffer(); | |
691 | let cbuf = match cvt { | |
692 | OutputConvert::None => buf, | |
693 | OutputConvert::Video(ref mut scaler, ref mut dbuf) => { | |
694 | let cur_ifmt = get_scale_fmt_from_pic(&buf); | |
695 | let last_ifmt = scaler.get_in_fmt(); | |
696 | if cur_ifmt != last_ifmt { | |
697 | let ofmt = scaler.get_out_fmt(); | |
698 | let ret = NAScale::new_with_options(cur_ifmt, ofmt, scale_opts); | |
699 | if ret.is_err() { | |
700 | println!("error re-initialising scaler for {} -> {}", cur_ifmt, ofmt); | |
701 | return false; | |
702 | } | |
703 | *scaler = ret.unwrap(); | |
704 | } | |
705 | let ret = scaler.convert(&buf, dbuf); | |
706 | if ret.is_err() { | |
707 | println!("error converting frame for encoding"); | |
708 | return false; | |
709 | } | |
710 | dbuf.clone() | |
711 | }, | |
712 | OutputConvert::Audio(ref dinfo, ref dchmap) => { | |
713 | let ret = convert_audio_frame(&buf, dinfo, dchmap); | |
714 | if ret.is_err() { | |
715 | println!("error converting audio for stream {}", dst_id); | |
716 | return false; | |
717 | } | |
718 | ret.unwrap() | |
719 | }, | |
720 | }; | |
721 | let cfrm = NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cbuf); | |
722 | encoder.encode(&cfrm).unwrap(); | |
723 | true | |
724 | } | |
725 | ||
726 | macro_rules! next_arg { | |
727 | ($args: expr, $arg_idx: expr) => { | |
728 | if $arg_idx + 1 >= $args.len() { | |
729 | println!("codec name is required"); | |
730 | } | |
731 | $arg_idx += 1; | |
732 | } | |
733 | } | |
734 | ||
735 | #[allow(clippy::single_match)] | |
736 | fn main() { | |
737 | let args: Vec<_> = env::args().collect(); | |
738 | ||
739 | if args.len() == 1 { | |
740 | println!("usage: nihav-encoder [options] --input inputfile --output outputfile"); | |
741 | println!(" use nihav-encoder --help to list all available options"); | |
742 | return; | |
743 | } | |
744 | if args.len() == 2 && (args[1] == "--help" || args[1] == "-h") { | |
745 | println!("usage: nihav-encoder [options] --input inputfile --output outputfile"); | |
746 | println!(" query options:"); | |
747 | println!(" --list-{{decoders,encoders,demuxers,muxers}} - lists all available decoders/encoders/demuxers/muxers"); | |
748 | println!(" --query-{{decoder,encoder,demuxer,muxer}}-options name - lists all options recognized by that decoder/encoder/demuxer/muxer"); | |
749 | println!(" processing options:"); | |
750 | println!(" --input inputfile - set input file"); | |
751 | println!(" --input-format fmt - force input format"); | |
752 | println!(" --demuxer-options options - set input demuxer options"); | |
753 | println!(" --scale-options options - set scaler options"); | |
754 | println!(" --output outputfile - set output file"); | |
755 | println!(" --output-format fmt - force output format"); | |
756 | println!(" --muxer-options options - set output muxer options"); | |
757 | println!(" --no-audio - do not decode audio streams"); | |
758 | println!(" --no-video - do not decode video streams"); | |
759 | println!(" --start starttime - start decoding from given position"); | |
760 | println!(" --end endtime - end decoding at given position"); | |
761 | println!(" --istreamX options - set options for input stream X"); | |
762 | println!(" --ostreamX options - set options for output stream X"); | |
763 | println!(); | |
764 | println!(" (de)muxer and stream options are passed as comma-separated list e.g. --ostream0 width=320,height=240,flip"); | |
765 | return; | |
766 | } | |
767 | ||
768 | let full_reg = FullRegister::new(); | |
769 | ||
770 | let mut transcoder = Transcoder::new(); | |
771 | ||
772 | let mut arg_idx = 1; | |
773 | let mut printed_info = false; | |
774 | while arg_idx < args.len() { | |
775 | match args[arg_idx].as_str() { | |
776 | "--list-decoders" => { | |
777 | if full_reg.dec_reg.iter().len() > 0 { | |
778 | println!("Registered decoders:"); | |
779 | for dec in full_reg.dec_reg.iter() { | |
780 | let cdesc = register::get_codec_description(dec.name); | |
781 | let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" }; | |
782 | println!(" {} ({})", dec.name, full_name); | |
783 | } | |
784 | } else { | |
785 | println!("No registered decoders."); | |
786 | } | |
787 | printed_info = true; | |
788 | }, | |
789 | "--list-encoders" => { | |
790 | if full_reg.enc_reg.iter().len() > 0 { | |
791 | println!("Registered encoders:"); | |
792 | for enc in full_reg.enc_reg.iter() { | |
793 | let cdesc = register::get_codec_description(enc.name); | |
794 | let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" }; | |
795 | println!(" {} ({})", enc.name, full_name); | |
796 | } | |
797 | } else { | |
798 | println!("No registered encoders."); | |
799 | } | |
800 | printed_info = true; | |
801 | }, | |
802 | "--list-demuxers" => { | |
803 | print!("Registered demuxers:"); | |
804 | for dmx in full_reg.dmx_reg.iter() { | |
805 | print!(" {}", dmx.get_name()); | |
806 | } | |
807 | println!(); | |
808 | printed_info = true; | |
809 | }, | |
810 | "--list-muxers" => { | |
811 | print!("Registered muxers:"); | |
812 | for mux in full_reg.mux_reg.iter() { | |
813 | print!(" {}", mux.get_name()); | |
814 | } | |
815 | println!(); | |
816 | printed_info = true; | |
817 | }, | |
818 | "--query-decoder-options" => { | |
819 | next_arg!(args, arg_idx); | |
820 | let cname = args[arg_idx].as_str(); | |
821 | if let Some(decfunc) = full_reg.dec_reg.find_decoder(cname) { | |
822 | let dec = (decfunc)(); | |
823 | let opts = dec.get_supported_options(); | |
824 | print_options(cname, opts); | |
825 | } else { | |
826 | println!("codec {} is not found", cname); | |
827 | } | |
828 | printed_info = true; | |
829 | }, | |
830 | "--query-demuxer-options" => { | |
831 | next_arg!(args, arg_idx); | |
832 | let dname = args[arg_idx].as_str(); | |
833 | let mut mr = MemoryReader::new_read(&[]); | |
834 | let mut br = ByteReader::new(&mut mr); | |
835 | if let Some(dmx_creator) = full_reg.dmx_reg.find_demuxer(dname) { | |
836 | let dmx = dmx_creator.new_demuxer(&mut br); | |
837 | let opts = dmx.get_supported_options(); | |
838 | print_options(dname, opts); | |
839 | } else { | |
840 | println!("demuxer {} is not found", dname); | |
841 | } | |
842 | printed_info = true; | |
843 | }, | |
844 | "--query-encoder-options" => { | |
845 | next_arg!(args, arg_idx); | |
846 | let cname = args[arg_idx].as_str(); | |
847 | if let Some(encfunc) = full_reg.enc_reg.find_encoder(cname) { | |
848 | let enc = (encfunc)(); | |
849 | let opts = enc.get_supported_options(); | |
850 | print_options(cname, opts); | |
851 | } else { | |
852 | println!("codec {} is not found", cname); | |
853 | } | |
854 | printed_info = true; | |
855 | }, | |
856 | "--query-muxer-options" => { | |
857 | next_arg!(args, arg_idx); | |
858 | let name = args[arg_idx].as_str(); | |
859 | let mut data = []; | |
860 | let mut mw = MemoryWriter::new_write(&mut data); | |
861 | let mut bw = ByteWriter::new(&mut mw); | |
862 | if let Some(mux_creator) = full_reg.mux_reg.find_muxer(name) { | |
863 | let mux = mux_creator.new_muxer(&mut bw); | |
864 | let opts = mux.get_supported_options(); | |
865 | print_options(name, opts); | |
866 | } else { | |
867 | println!("muxer {} is not found", name); | |
868 | } | |
869 | printed_info = true; | |
870 | }, | |
871 | "--input" | "-i" => { | |
872 | next_arg!(args, arg_idx); | |
873 | transcoder.input_name = args[arg_idx].clone(); | |
874 | }, | |
875 | "--input-format" => { | |
876 | next_arg!(args, arg_idx); | |
877 | transcoder.input_fmt = Some(args[arg_idx].clone()); | |
878 | }, | |
879 | "--output" | "-o" => { | |
880 | next_arg!(args, arg_idx); | |
881 | transcoder.output_name = args[arg_idx].clone(); | |
882 | }, | |
883 | "--output-format" => { | |
884 | next_arg!(args, arg_idx); | |
885 | transcoder.output_fmt = Some(args[arg_idx].clone()); | |
886 | }, | |
887 | "--demuxer-options" => { | |
888 | next_arg!(args, arg_idx); | |
889 | if !transcoder.parse_demuxer_options(&args[arg_idx], &full_reg.dmx_reg) { | |
890 | println!("invalid demuxer option syntax"); | |
891 | return; | |
892 | } | |
893 | }, | |
894 | "--scale-options" => { | |
895 | next_arg!(args, arg_idx); | |
896 | if !transcoder.parse_scale_options(&args[arg_idx]) { | |
897 | println!("invalid scale option syntax"); | |
898 | return; | |
899 | } | |
900 | }, | |
901 | "--no-video" | "-vn" => { | |
902 | transcoder.no_video = true; | |
903 | }, | |
904 | "--no-audio" | "-an" => { | |
905 | transcoder.no_audio = true; | |
906 | }, | |
907 | "--start" => { | |
908 | next_arg!(args, arg_idx); | |
909 | let ret = args[arg_idx].parse::<NATimePoint>(); | |
910 | if let Ok(val) = ret { | |
911 | transcoder.start = val; | |
912 | } else { | |
913 | println!("invalid start time"); | |
914 | return; | |
915 | } | |
916 | }, | |
917 | "--end" => { | |
918 | next_arg!(args, arg_idx); | |
919 | let ret = args[arg_idx].parse::<NATimePoint>(); | |
920 | if let Ok(val) = ret { | |
921 | transcoder.end = val; | |
922 | } else { | |
923 | println!("invalid end time"); | |
924 | return; | |
925 | } | |
926 | }, | |
927 | "--muxer-options" => { | |
928 | next_arg!(args, arg_idx); | |
929 | if !transcoder.parse_muxer_options(&args[arg_idx], &full_reg.mux_reg) { | |
930 | println!("invalid muxer option syntax"); | |
931 | return; | |
932 | } | |
933 | }, | |
934 | _ => { | |
935 | if args[arg_idx].starts_with("--istream") { | |
936 | let opt0 = &args[arg_idx]; | |
937 | next_arg!(args, arg_idx); | |
938 | if !transcoder.parse_istream_options(opt0, &args[arg_idx]) { | |
939 | println!("invalid input stream option syntax"); | |
940 | return; | |
941 | } | |
942 | } else if args[arg_idx].starts_with("--ostream") { | |
943 | let opt0 = &args[arg_idx]; | |
944 | next_arg!(args, arg_idx); | |
945 | if !transcoder.parse_ostream_options(opt0, &args[arg_idx], &full_reg.enc_reg) { | |
946 | println!("invalid output stream option syntax"); | |
947 | return; | |
948 | } | |
949 | } else if args[arg_idx].starts_with("--") { | |
950 | println!("unknown option"); | |
951 | } else { | |
952 | println!("unrecognized argument"); | |
953 | } | |
954 | }, | |
955 | }; | |
956 | arg_idx += 1; | |
957 | } | |
958 | ||
959 | if printed_info { | |
960 | return; | |
961 | } | |
962 | ||
963 | if transcoder.input_name.is_empty() { | |
964 | println!("no input name provided"); | |
965 | return; | |
966 | } | |
967 | if transcoder.output_name.is_empty() { | |
968 | println!("no output name provided"); | |
969 | return; | |
970 | } | |
971 | ||
972 | let res = File::open(transcoder.input_name.as_str()); | |
973 | if res.is_err() { | |
974 | println!("error opening input"); | |
975 | return; | |
976 | } | |
977 | let file = res.unwrap(); | |
978 | let file = BufReader::new(file); | |
979 | let mut fr = FileReader::new_read(file); | |
980 | let mut br = ByteReader::new(&mut fr); | |
981 | let (is_raw, start, end) = if transcoder.input_fmt.is_none() { | |
982 | detect_tags(&mut br) | |
983 | } else { | |
984 | (false, 0, None) | |
985 | }; | |
986 | ||
987 | let mut nfr: Box<dyn ByteIO>; | |
988 | if start != 0 || end.is_some() { | |
989 | let file = fr.finish(); | |
990 | nfr = Box::new(BoundedFileReader::new_read(file, start, end).unwrap()); | |
991 | } else { | |
992 | nfr = Box::new(fr); | |
993 | } | |
994 | let mut br = ByteReader::new(nfr.as_mut()); | |
995 | ||
996 | let mut dmx = DemuxerObject::create(&mut br, &full_reg, transcoder.input_name.as_str(), &transcoder.input_fmt, is_raw); | |
997 | if dmx.is_none() { | |
998 | println!("cannot find demuxer for '{}'", transcoder.input_name.as_str()); | |
999 | return; | |
1000 | } | |
1001 | parse_and_apply_options!(dmx, &transcoder.demux_opts, "input"); | |
1002 | for i in 0..dmx.get_num_streams() { | |
1003 | let s = dmx.get_stream(i).unwrap(); | |
1004 | let info = s.get_info(); | |
1005 | let decfunc = full_reg.dec_reg.find_decoder(info.get_name()); | |
1006 | println!("stream {} - {} {}", i, s, info.get_name()); | |
1007 | let str_id = s.get_num() as u32; | |
1008 | if let Some(create_dec) = decfunc { | |
1009 | let mut dec = (create_dec)(); | |
1010 | let mut dsupp = Box::new(NADecoderSupport::new()); | |
1011 | let ret = dec.init(&mut dsupp, info.clone()); | |
1012 | if ret.is_err() { | |
1013 | println!("Error initialising decoder '{}' for stream {}", info.get_name(), str_id); | |
1014 | return; | |
1015 | } | |
1016 | transcoder.apply_decoder_options(dec.as_mut(), str_id); | |
1017 | let desc = register::get_codec_description(info.get_name()); | |
1018 | let has_b = if let Some(desc) = desc { | |
1019 | desc.has_reorder() | |
1020 | } else { | |
1021 | println!("No codec description found, using B-frame reorderer."); | |
1022 | true | |
1023 | }; | |
1024 | let reord: Box<dyn FrameReorderer> = if has_b { Box::new(IPBReorderer::new()) } else { Box::new(NoReorderer::new()) }; | |
1025 | transcoder.decoders.push(Some((dsupp, dec, reord))); | |
1026 | } else { | |
1027 | println!("No decoder for stream {} ({}) is found", str_id, info.get_name()); | |
1028 | transcoder.decoders.push(None); | |
1029 | } | |
1030 | } | |
1031 | if transcoder.start != NATimePoint::None { | |
1032 | let ret = dmx.seek(transcoder.start); | |
1033 | if ret.is_err() { | |
1034 | println!(" failed to seek to {} error {:?}", transcoder.start, ret.err().unwrap()); | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | let output_fmt = if let Some(ref str) = transcoder.output_fmt { | |
1039 | str | |
1040 | } else if let Some(str) = detect::detect_format_by_name(transcoder.output_name.as_str()) { | |
1041 | str | |
1042 | } else { | |
1043 | println!("Cannot guess muxer for output"); | |
1044 | return; | |
1045 | }; | |
1046 | let ret = full_reg.mux_reg.find_muxer(output_fmt); | |
1047 | let ofmt = output_fmt.to_string(); | |
1048 | ||
1049 | if ret.is_none() { | |
1050 | println!("cannot find muxer '{}'", output_fmt); | |
1051 | } | |
1052 | let mux_creator = ret.unwrap(); | |
1053 | ||
1054 | let mux_caps = mux_creator.get_capabilities(); | |
1055 | let mut out_sm = StreamManager::new(); | |
1056 | if !transcoder.negotiate_stream_map(dmx.get_stream_manager(), mux_caps, &mut out_sm, &full_reg.enc_reg) { | |
1057 | println!("cannot determine stream map"); | |
1058 | return; | |
1059 | } | |
1060 | ||
1061 | let ret = File::create(transcoder.output_name.as_str()); | |
1062 | if ret.is_err() { | |
1063 | println!("cannot open output file"); | |
1064 | return; | |
1065 | } | |
1066 | let mut fw = FileWriter::new_write(ret.unwrap()); | |
1067 | let mut bw = ByteWriter::new(&mut fw); | |
1068 | let ret = create_muxer(mux_creator, out_sm, &mut bw); | |
1069 | if let Err(err) = ret { | |
1070 | println!("cannot create muxer instance {:?}", err); | |
1071 | return; | |
1072 | } | |
1073 | let mut mux = ret.unwrap(); | |
1074 | parse_and_apply_options!(mux, &transcoder.mux_opts, "output"); | |
1075 | ||
1076 | println!("Output {} muxer {}", transcoder.output_name, ofmt); | |
1077 | for ostr in mux.get_streams() { | |
1078 | println!(" #{}: {} {}", ostr.get_num(), ostr, ostr.get_info().get_name()); | |
1079 | } | |
1080 | ||
1081 | 'main_loop: loop { | |
1082 | let pktres = dmx.get_frame(); | |
1083 | if let Err(DemuxerError::EOF) = pktres { break; } | |
1084 | if pktres.is_err() { | |
1085 | println!("demuxing error"); | |
1086 | break; | |
1087 | } | |
1088 | let mut pkt = pktres.unwrap(); | |
1089 | if transcoder.start != NATimePoint::None && pkt.ts.less_than(transcoder.start) { continue; } | |
1090 | let src_id = pkt.get_stream().get_num(); | |
1091 | match transcoder.encoders[src_id] { | |
1092 | OutputMode::Drop => {}, | |
1093 | OutputMode::Copy(dst_id) => { | |
1094 | let dstr = mux.get_stream(dst_id as usize).unwrap(); | |
1095 | pkt.reassign(dstr, pkt.get_time_information()); | |
1096 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; } | |
1097 | if mux.mux_frame(pkt).is_err() { | |
1098 | println!("error muxing packet"); | |
1099 | break; | |
1100 | } | |
1101 | }, | |
1102 | OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) => { | |
1103 | if let Some((ref mut dsupp, ref mut decoder, ref mut reorderer)) = transcoder.decoders[src_id] { | |
1104 | let ret = decoder.decode(dsupp, &pkt); | |
1105 | if let (true, Err(DecoderError::MissingReference)) = (transcoder.start != NATimePoint::None, &ret) { | |
1106 | continue; | |
1107 | } | |
1108 | if ret.is_err() { | |
1109 | println!("error decoding stream {}", src_id); | |
1110 | break; | |
1111 | } | |
1112 | let frm = ret.unwrap(); | |
1113 | reorderer.add_frame(frm); | |
1114 | while let Some(frm) = reorderer.get_frame() { | |
1115 | if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) { | |
1116 | break; | |
1117 | } | |
1118 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1119 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; } | |
1120 | mux.mux_frame(pkt).unwrap(); | |
1121 | } | |
1122 | } | |
1123 | } else { | |
1124 | println!("no decoder for stream {}", src_id); | |
1125 | break; | |
1126 | } | |
1127 | }, | |
1128 | }; | |
1129 | } | |
1130 | 'reord_flush_loop: for str in dmx.get_streams() { | |
1131 | let src_id = str.get_num(); | |
1132 | if let OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) = transcoder.encoders[src_id] { | |
1133 | if let Some((_, _, ref mut reorderer)) = transcoder.decoders[src_id] { | |
1134 | while let Some(frm) = reorderer.get_last_frames() { | |
1135 | if !encode_frame(dst_id, encoder, cvt, frm, &transcoder.scale_opts) { | |
1136 | break; | |
1137 | } | |
1138 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1139 | if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'reord_flush_loop; } | |
1140 | mux.mux_frame(pkt).unwrap(); | |
1141 | } | |
1142 | } | |
1143 | } | |
1144 | } | |
1145 | } | |
1146 | 'flush_loop: for enc in transcoder.encoders.iter_mut() { | |
1147 | match enc { | |
1148 | OutputMode::Encode(str_id, ref mut encoder, _) => { | |
1149 | let ret = encoder.flush(); | |
1150 | if ret.is_err() { | |
1151 | println!("error flushing encoder for stream {}", str_id); | |
1152 | break; | |
1153 | } else { | |
1154 | while let Ok(Some(pkt)) = encoder.get_packet() { | |
1155 | if mux.mux_frame(pkt).is_err() { | |
1156 | println!("error muxing packet"); | |
1157 | break 'flush_loop; | |
1158 | } | |
1159 | } | |
1160 | } | |
1161 | }, | |
1162 | _ => {}, | |
1163 | }; | |
1164 | } | |
1165 | ||
1166 | let ret = mux.end(); | |
1167 | if ret.is_err() { | |
1168 | println!("error at finalising muxing"); | |
1169 | } | |
1170 | } |