1 extern crate nihav_core;
2 extern crate nihav_codec_support;
3 extern crate nihav_registry;
4 extern crate nihav_allstuff;
7 use nihav_core::io::byteio::{FileReader, ByteReader};
8 use nihav_core::frame::*;
9 use nihav_core::options::*;
10 use nihav_core::codecs::*;
11 use nihav_core::demuxers::*;
12 use nihav_core::muxers::*;
13 use nihav_core::scale::*;
14 use nihav_core::soundcvt::*;
15 use nihav_registry::detect;
16 use nihav_registry::register;
17 use nihav_allstuff::*;
23 fn print_options(name: &str, options: &[NAOptionDefinition]) {
24 if options.is_empty() {
25 println!("No custom options.");
27 println!("Options for '{}'", name);
28 for opt in options.iter() {
36 value: Option<String>,
39 struct InputStreamOptions {
42 dec_opts: Vec<OptionArgs>,
45 struct OutputStreamOptions {
47 enc_params: EncodeParameters,
49 enc_opts: Vec<OptionArgs>,
53 Video(NAScale, NABufferType),
54 Audio(NAAudioInfo, NAChannelMap),
61 Encode(u32, Box<dyn NAEncoder>, OutputConvert),
67 input_fmt: Option<String>,
69 output_fmt: Option<String>,
70 demux_opts: Vec<OptionArgs>,
71 mux_opts: Vec<OptionArgs>,
72 istr_opts: Vec<InputStreamOptions>,
73 ostr_opts: Vec<OutputStreamOptions>,
74 decoders: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>>,
75 encoders: Vec<OutputMode>,
82 macro_rules! parse_and_apply_options {
83 ($obj: expr, $in_opts: expr, $name: expr) => {
84 let mut opts = Vec::with_capacity($in_opts.len());
85 let opt_def = $obj.get_supported_options();
86 for opt in $in_opts.iter() {
87 let mut found = false;
88 for opt_def in opt_def.iter() {
89 if opt.name == opt_def.name {
90 let arg = if let Some(ref str) = opt.value { Some(str) } else { None };
91 let ret = opt_def.parse(&opt.name, arg);
93 println!("invalid option {} for {}", opt.name, $name);
95 let (val, _) = ret.unwrap();
102 println!(" ignoring option '{}' for {}", opt.name, $name);
105 $obj.set_options(opts.as_slice());
110 fn new() -> Self { Self::default() }
111 fn parse_istream_options(&mut self, opt0: &str, opt1: &str) -> bool {
112 let (_, strno) = opt0.split_at(9);
113 let ret = strno.parse::<u32>();
114 if ret.is_err() { return false; }
115 let streamno = ret.unwrap();
117 let sidx = if let Some(idx) = self.istr_opts.iter().position(|str| str.id == streamno) {
120 self.istr_opts.push(InputStreamOptions {id: streamno, drop: false, dec_opts: Vec::new() });
121 self.istr_opts.len() - 1
123 let istr = &mut self.istr_opts[sidx];
125 for opt in opt1.split(',') {
126 let oval: Vec<_> = opt.split('=').collect();
129 "drop" => { istr.drop = true; },
131 istr.dec_opts.push(OptionArgs{ name: oval[0].to_string(), value: None });
134 } else if oval.len() == 2 {
135 istr.dec_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) });
137 println!("unrecognized option '{}'", opt);
142 fn parse_ostream_options(&mut self, opt0: &str, opt1: &str, enc_reg: &RegisteredEncoders) -> bool {
143 let (_, strno) = opt0.split_at(9);
144 let ret = strno.parse::<u32>();
145 if ret.is_err() { return false; }
146 let streamno = ret.unwrap();
148 let sidx = if let Some(idx) = self.ostr_opts.iter().position(|str| str.id == streamno) {
151 self.ostr_opts.push(OutputStreamOptions {id: streamno, enc_name: String::new(), enc_params: EncodeParameters::default(), enc_opts: Vec::new() });
152 self.ostr_opts.len() - 1
154 let ostr = &mut self.ostr_opts[sidx];
156 for opt in opt1.split(',') {
157 let oval: Vec<_> = opt.split('=').collect();
161 if ostr.enc_params.format == NACodecTypeInfo::None {
162 ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
164 if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format {
165 vinfo.flipped = true;
167 println!("video option for audio stream");
171 if ostr.enc_params.format == NACodecTypeInfo::None {
172 ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
174 if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format {
175 vinfo.flipped = false;
177 println!("video option for audio stream");
181 ostr.enc_opts.push(OptionArgs{ name: oval[0].to_string(), value: None });
184 } else if oval.len() == 2 {
185 //todo parse encoder options, store, init later
188 if enc_reg.find_encoder(oval[1]).is_some() {
189 ostr.enc_name = oval[1].to_string();
191 println!("unknown encoder '{}'", oval[1]);
192 ostr.enc_name = oval[1].to_string();
196 if ostr.enc_params.format == NACodecTypeInfo::None {
197 ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
199 if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format {
200 let ret = oval[1].parse::<usize>();
201 if let Ok(val) = ret {
204 println!("invalid width");
207 println!("video option for audio stream");
211 if ostr.enc_params.format == NACodecTypeInfo::None {
212 ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
214 if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format {
215 let ret = oval[1].parse::<usize>();
216 if let Ok(val) = ret {
219 println!("invalid height");
222 println!("video option for audio stream");
226 if ostr.enc_params.format == NACodecTypeInfo::None {
227 ostr.enc_params.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
229 if let NACodecTypeInfo::Video(ref mut vinfo) = ostr.enc_params.format {
230 let ret = oval[1].parse::<NAPixelFormaton>();
231 if let Ok(val) = ret {
234 println!("invalid pixel format");
237 println!("video option for audio stream");
241 if ostr.enc_params.format == NACodecTypeInfo::None {
242 ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0));
244 if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format {
245 let ret = oval[1].parse::<u32>();
246 if let Ok(val) = ret {
247 ainfo.sample_rate = val;
249 println!("invalid sampling rate");
252 println!("audio option for video stream");
256 if ostr.enc_params.format == NACodecTypeInfo::None {
257 ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0));
259 if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format {
260 let ret = oval[1].parse::<u8>();
261 if let Ok(val) = ret {
262 ainfo.channels = val;
264 println!("invalid number of channels");
267 println!("audio option for video stream");
271 if ostr.enc_params.format == NACodecTypeInfo::None {
272 ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0));
274 if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format {
275 let ret = oval[1].parse::<usize>();
276 if let Ok(val) = ret {
277 ainfo.block_len = val;
279 println!("invalid block_length");
282 println!("audio option for video stream");
286 if ostr.enc_params.format == NACodecTypeInfo::None {
287 ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0));
289 if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format {
290 let ret = oval[1].parse::<NASoniton>();
291 if let Ok(val) = ret {
294 println!("invalid audio format");
297 println!("audio option for video stream");
300 // todo channel map negotiation
302 if ostr.enc_params.format == NACodecTypeInfo::None {
303 ostr.enc_params.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16_FORMAT, 0));
305 if let NACodecTypeInfo::Audio(ref mut ainfo) = ostr.enc_params.format {
306 let ret = oval[1].parse::<NAChannelMap>();
307 if let Ok(val) = ret {
310 println!("invalid channel map");
313 println!("audio option for video stream");
317 let ret = oval[1].parse::<u32>();
318 if let Ok(val) = ret {
319 ostr.enc_params.bitrate = val;
321 println!("invalid bitrate value");
325 let ret = oval[1].parse::<u8>();
326 if let Ok(val) = ret {
327 ostr.enc_params.quality = val;
329 println!("invalid quality value");
333 ostr.enc_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) });
337 println!("unrecognized option '{}'", opt);
342 fn parse_demuxer_options(&mut self, opts: &str, dmx_reg: &RegisteredDemuxers) -> bool {
343 for opt in opts.split(',') {
344 let oval: Vec<_> = opt.split('=').collect();
346 self.demux_opts.push(OptionArgs{ name: oval[0].to_string(), value: None });
347 } else if oval.len() == 2 {
348 if oval[0] == "format" {
349 if dmx_reg.find_demuxer(oval[1]).is_some() {
350 self.input_fmt = Some(oval[1].to_string());
352 println!("unknown demuxer format '{}'", oval[1]);
355 self.demux_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) });
358 println!("unrecognized option '{}'", opt);
363 fn parse_muxer_options(&mut self, opts: &str, mux_reg: &RegisteredMuxers) -> bool {
364 for opt in opts.split(',') {
365 let oval: Vec<_> = opt.split('=').collect();
367 self.mux_opts.push(OptionArgs{ name: oval[0].to_string(), value: None });
368 } else if oval.len() == 2 {
369 if oval[0] == "format" {
370 if mux_reg.find_muxer(oval[1]).is_some() {
371 self.output_fmt = Some(oval[1].to_string());
373 println!("unknown muxer format '{}'", oval[1]);
376 self.mux_opts.push(OptionArgs{ name: oval[0].to_string(), value: Some(oval[1].to_string()) });
379 println!("unrecognized option '{}'", opt);
384 fn apply_decoder_options(&self, dec: &mut NADecoder, str_id: u32) {
385 if let Some(str_idx) = self.istr_opts.iter().position(|str| str.id == str_id) {
386 let dec_opts = dec.get_supported_options();
387 if dec_opts.is_empty() { return; }
388 let name = format!("input stream {}", str_id);
389 parse_and_apply_options!(dec, &self.istr_opts[str_idx].dec_opts, name);
392 fn register_output_stream(&mut self, cname: &str, istr: NAStreamRef, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool {
393 let out_id = out_sm.get_num_streams() as u32;
394 if let Some(str_idx) = self.istr_opts.iter().position(|str| str.id == (istr.get_num() as u32)) {
395 if self.istr_opts[str_idx].drop {
396 self.encoders.push(OutputMode::Drop);
401 if let Some(str_idx) = self.ostr_opts.iter().position(|str| str.id == out_id) {
402 let oopts = &mut self.ostr_opts[str_idx];
403 if oopts.enc_name.as_str() == "copy" && (cname == "any" || istr.get_info().get_name() == cname) {
404 out_sm.add_stream_ref(istr.clone());
405 self.encoders.push(OutputMode::Copy(out_id));
406 } else if cname == "any" || oopts.enc_name.as_str() == cname {
407 let enc_create = enc_reg.find_encoder(oopts.enc_name.as_str());
408 if enc_create.is_none() {
409 println!("encoder '{}' not found", oopts.enc_name.as_str());
412 let mut encoder = (enc_create.unwrap())();
413 let forced_out = oopts.enc_params.format != NACodecTypeInfo::None;
414 if oopts.enc_params.format == NACodecTypeInfo::None {
415 oopts.enc_params.format = istr.get_info().get_properties();
417 if oopts.enc_params.tb_num == 0 {
418 oopts.enc_params.tb_num = istr.tb_num;
419 oopts.enc_params.tb_den = istr.tb_den;
421 let ret_eparams = encoder.negotiate_format(&oopts.enc_params);
422 if ret_eparams.is_err() {
423 println!("cannot negotiate encoding parameters");
426 let ret_eparams = ret_eparams.unwrap();
428 //todo check for params mismatch
429 let cvt = match (&oopts.enc_params.format, &ret_eparams.format) {
430 (NACodecTypeInfo::Video(svinfo), NACodecTypeInfo::Video(dvinfo)) => {
431 if svinfo == dvinfo && !forced_out {
434 let ofmt = ScaleInfo { fmt: dvinfo.format, width: dvinfo.width, height: dvinfo.height };
435 let ret = NAScale::new(ofmt, ofmt);
437 println!("cannot create scaler");
440 let scaler = ret.unwrap();
441 let ret = alloc_video_buffer(*dvinfo, 4);
443 println!("cannot create scaler buffer");
446 let cvt_buf = ret.unwrap();
447 OutputConvert::Video(scaler, cvt_buf)
450 (NACodecTypeInfo::Audio(sainfo), NACodecTypeInfo::Audio(dainfo)) => {
451 if sainfo == dainfo {
454 let dchmap = match dainfo.channels {
455 1 => NAChannelMap::from_ms_mapping(0x4),
456 2 => NAChannelMap::from_ms_mapping(0x3),
458 println!("can't generate default channel map for {} channels", dainfo.channels);
463 OutputConvert::Audio(*dainfo, dchmap)
466 _ => OutputConvert::None,
468 let ret = encoder.init(out_id, ret_eparams);
470 println!("error initialising encoder");
473 out_sm.add_stream_ref(ret.unwrap());
475 let name = format!("output stream {}", out_id);
476 parse_and_apply_options!(encoder, &oopts.enc_opts, name);
478 self.encoders.push(OutputMode::Encode(out_id, encoder, cvt));
480 println!("encoder {} is not supported by output (expected {})", istr.id, istr.get_info().get_name());
484 if cname == "any" || istr.get_info().get_name() == cname {
485 out_sm.add_stream_ref(istr.clone());
486 self.encoders.push(OutputMode::Copy(out_id));
488 println!("stream {} ({}) can't be handled", istr.id, istr.get_info().get_name());
489 // todo autoselect encoder?
495 fn map_single(&mut self, cname: &str, ctype: StreamType, src_sm: &StreamManager, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool {
496 let mut found_stream = false;
497 for istr in src_sm.iter() {
498 if istr.get_media_type() != ctype || found_stream {
499 self.encoders.push(OutputMode::Drop);
501 if !self.register_output_stream(cname, istr, out_sm, enc_reg) {
509 fn negotiate_stream_map(&mut self, src_sm: &StreamManager, mux_caps: MuxerCapabilities, out_sm: &mut StreamManager, enc_reg: &RegisteredEncoders) -> bool {
511 MuxerCapabilities::SingleVideo(cname) => {
512 if self.no_video { return false; }
513 self.map_single(cname, StreamType::Video, src_sm, out_sm, enc_reg)
515 MuxerCapabilities::SingleAudio(cname) => {
516 if self.no_audio { return false; }
517 self.map_single(cname, StreamType::Audio, src_sm, out_sm, enc_reg)
519 MuxerCapabilities::SingleVideoAndAudio(vname, aname) => {
520 let mut found_vid = false;
521 let mut found_aud = false;
522 for istr in src_sm.iter() {
523 if istr.get_media_type() == StreamType::Video && !found_vid && !self.no_video {
524 if !self.register_output_stream(vname, istr, out_sm, enc_reg) {
528 } else if istr.get_media_type() == StreamType::Audio && !found_aud && !self.no_audio {
529 if !self.register_output_stream(aname, istr, out_sm, enc_reg) {
534 self.encoders.push(OutputMode::Drop);
537 found_vid | found_aud
539 MuxerCapabilities::OnlyVideo => {
540 if self.no_video { return false; }
542 let mut found_vid = false;
543 for istr in src_sm.iter() {
544 if istr.get_media_type() == StreamType::Video && !found_vid {
545 if !self.register_output_stream("any", istr, out_sm, enc_reg) {
550 self.encoders.push(OutputMode::Drop);
555 MuxerCapabilities::OnlyAudio => {
556 if self.no_audio { return false; }
558 let mut found_aud = false;
559 for istr in src_sm.iter() {
560 if istr.get_media_type() == StreamType::Audio && !found_aud {
561 if !self.register_output_stream("any", istr, out_sm, enc_reg) {
566 self.encoders.push(OutputMode::Drop);
571 MuxerCapabilities::Universal => {
572 for istr in src_sm.iter() {
573 if (istr.get_media_type() == StreamType::Video && self.no_video) ||
574 (istr.get_media_type() == StreamType::Audio && self.no_audio) {
575 self.encoders.push(OutputMode::Drop);
578 if !self.register_output_stream("any", istr, out_sm, enc_reg) {
588 macro_rules! next_arg {
589 ($args: expr, $arg_idx: expr) => {
590 if $arg_idx + 1 >= $args.len() {
591 println!("codec name is required");
598 let args: Vec<_> = env::args().collect();
601 println!("usage: nihav-encoder [-noout] [-vn] [-an] input [lastpts]");
605 let mut dmx_reg = RegisteredDemuxers::new();
606 nihav_register_all_demuxers(&mut dmx_reg);
607 let mut dec_reg = RegisteredDecoders::new();
608 nihav_register_all_decoders(&mut dec_reg);
610 let mut mux_reg = RegisteredMuxers::new();
611 nihav_register_all_muxers(&mut mux_reg);
612 mux_reg.add_muxer(NULL_MUXER);
613 let mut enc_reg = RegisteredEncoders::new();
614 nihav_register_all_encoders(&mut enc_reg);
615 enc_reg.add_encoder(NULL_ENCODER);
617 let mut transcoder = Transcoder::new();
620 let mut printed_info = false;
621 while arg_idx < args.len() {
622 match args[arg_idx].as_str() {
623 "--list-decoders" => {
624 if dec_reg.iter().len() > 0 {
625 println!("Registered decoders:");
626 for dec in dec_reg.iter() {
627 let cdesc = register::get_codec_description(dec.name);
628 let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" };
629 println!(" {} ({})", dec.name, full_name);
632 println!("No registered decoders.");
636 "--list-encoders" => {
637 if enc_reg.iter().len() > 0 {
638 println!("Registered encoders:");
639 for enc in enc_reg.iter() {
640 let cdesc = register::get_codec_description(enc.name);
641 let full_name = if let Some(cd) = cdesc { cd.get_full_name() } else { "???" };
642 println!(" {} ({})", enc.name, full_name);
645 println!("No registered encoders.");
649 "--list-demuxers" => {
650 print!("Registered demuxers:");
651 for dmx in dmx_reg.iter() {
652 print!(" {}", dmx.get_name());
658 print!("Registered muxers:");
659 for mux in mux_reg.iter() {
660 print!(" {}", mux.get_name());
665 "--query-decoder-options" => {
666 next_arg!(args, arg_idx);
667 let cname = args[arg_idx].as_str();
668 if let Some(decfunc) = dec_reg.find_decoder(cname) {
669 let dec = (decfunc)();
670 let opts = dec.get_supported_options();
671 print_options(cname, opts);
673 println!("codec {} is not found", cname);
677 "--query-demuxer-options" => {
678 next_arg!(args, arg_idx);
679 let dname = args[arg_idx].as_str();
680 let mut mr = MemoryReader::new_read(&[]);
681 let mut br = ByteReader::new(&mut mr);
682 if let Some(dmx_creator) = dmx_reg.find_demuxer(dname) {
683 let dmx = dmx_creator.new_demuxer(&mut br);
684 let opts = dmx.get_supported_options();
685 print_options(dname, opts);
687 println!("demuxer {} is not found", dname);
691 "--query-encoder-options" => {
692 next_arg!(args, arg_idx);
693 let cname = args[arg_idx].as_str();
694 if let Some(encfunc) = enc_reg.find_encoder(cname) {
695 let enc = (encfunc)();
696 let opts = enc.get_supported_options();
697 print_options(cname, opts);
699 println!("codec {} is not found", cname);
703 "--query-muxer-options" => {
704 next_arg!(args, arg_idx);
705 let name = args[arg_idx].as_str();
707 let mut mw = MemoryWriter::new_write(&mut data);
708 let mut bw = ByteWriter::new(&mut mw);
709 if let Some(mux_creator) = mux_reg.find_muxer(name) {
710 let mux = mux_creator.new_muxer(&mut bw);
711 let opts = mux.get_supported_options();
712 print_options(name, opts);
714 println!("muxer {} is not found", name);
718 "--input" | "-i" => {
719 next_arg!(args, arg_idx);
720 transcoder.input_name = args[arg_idx].clone();
722 "--input-format" => {
723 next_arg!(args, arg_idx);
724 transcoder.input_fmt = Some(args[arg_idx].clone());
726 "--output" | "-o" => {
727 next_arg!(args, arg_idx);
728 transcoder.output_name = args[arg_idx].clone();
730 "--output-format" => {
731 next_arg!(args, arg_idx);
732 transcoder.output_fmt = Some(args[arg_idx].clone());
734 "--demuxer-options" => {
735 next_arg!(args, arg_idx);
736 if !transcoder.parse_demuxer_options(&args[arg_idx], &dmx_reg) {
737 println!("invalid demuxer option syntax");
741 "--no-video" | "-vn" => {
742 transcoder.no_video = true;
744 "--no-audio" | "-an" => {
745 transcoder.no_audio = true;
748 next_arg!(args, arg_idx);
749 let ret = args[arg_idx].parse::<NATimePoint>();
750 if let Ok(val) = ret {
751 transcoder.start = val;
753 println!("invalid start time");
758 next_arg!(args, arg_idx);
759 let ret = args[arg_idx].parse::<NATimePoint>();
760 if let Ok(val) = ret {
761 transcoder.end = val;
763 println!("invalid end time");
767 "--muxer-options" => {
768 next_arg!(args, arg_idx);
769 if !transcoder.parse_muxer_options(&args[arg_idx], &mux_reg) {
770 println!("invalid muxer option syntax");
775 if args[arg_idx].starts_with("--istream") {
776 let opt0 = &args[arg_idx];
777 next_arg!(args, arg_idx);
778 if !transcoder.parse_istream_options(opt0, &args[arg_idx]) {
779 println!("invalid input stream option syntax");
782 } else if args[arg_idx].starts_with("--ostream") {
783 let opt0 = &args[arg_idx];
784 next_arg!(args, arg_idx);
785 if !transcoder.parse_ostream_options(opt0, &args[arg_idx], &enc_reg) {
786 println!("invalid output stream option syntax");
789 } else if args[arg_idx].starts_with("--") {
790 println!("unknown option");
792 println!("unrecognized argument");
803 if transcoder.input_name.len() == 0 {
804 println!("no input name provided");
807 if transcoder.output_name.len() == 0 {
808 println!("no output name provided");
812 let res = File::open(transcoder.input_name.as_str());
814 println!("error opening input");
817 let mut file = res.unwrap();
818 let mut fr = FileReader::new_read(&mut file);
819 let mut br = ByteReader::new(&mut fr);
821 let dmx_name = if let Some(ref str) = transcoder.input_fmt {
824 if let Some((dmx_name, score)) = detect::detect_format(transcoder.input_name.as_str(), &mut br) {
825 println!("detected {} with score {:?}", dmx_name, score);
828 println!("cannot detect input format");
832 let ret = dmx_reg.find_demuxer(dmx_name);
834 println!("cannot find demuxer for '{}'", dmx_name);
837 let dmx_fact = ret.unwrap();
838 br.seek(SeekFrom::Start(0)).unwrap();
839 let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap();
840 parse_and_apply_options!(dmx, &transcoder.demux_opts, "input");
841 for i in 0..dmx.get_num_streams() {
842 let s = dmx.get_stream(i).unwrap();
843 let info = s.get_info();
844 let decfunc = dec_reg.find_decoder(info.get_name());
845 println!("stream {} - {} {}", i, s, info.get_name());
846 let str_id = s.get_num() as u32;
847 if let Some(create_dec) = decfunc {
848 let mut dec = (create_dec)();
849 let mut dsupp = Box::new(NADecoderSupport::new());
850 let ret = dec.init(&mut dsupp, info.clone());
852 println!("Error initialising decoder '{}' for stream {}", info.get_name(), str_id);
855 transcoder.apply_decoder_options(dec.as_mut(), str_id);
856 transcoder.decoders.push(Some((dsupp, dec)));
858 println!("No decoder for stream {} ({}) is found", str_id, info.get_name());
859 transcoder.decoders.push(None);
862 if transcoder.start != NATimePoint::None {
863 let ret = dmx.seek(transcoder.start);
865 println!(" failed to seek to {} error {:?}", transcoder.start, ret.err().unwrap());
869 let output_fmt = if let Some(ref str) = transcoder.output_fmt {
872 if let Some(str) = detect::detect_format_by_name(transcoder.output_name.as_str()) {
875 println!("Cannot guess muxer for output");
879 let ret = mux_reg.find_muxer(output_fmt);
880 let ofmt = output_fmt.to_string();
883 println!("cannot find muxer '{}'", output_fmt);
885 let mux_creator = ret.unwrap();
887 let mux_caps = mux_creator.get_capabilities();
888 let mut out_sm = StreamManager::new();
889 if !transcoder.negotiate_stream_map(dmx.get_stream_manager(), mux_caps, &mut out_sm, &enc_reg) {
890 println!("cannot determine stream map");
894 let ret = File::create(transcoder.output_name.as_str());
896 println!("cannot open output file");
899 let mut fw = FileWriter::new_write(ret.unwrap());
900 let mut bw = ByteWriter::new(&mut fw);
901 let ret = create_muxer(mux_creator, out_sm, &mut bw);
902 if let Err(err) = ret {
903 println!("cannot create muxer instance {:?}", err);
906 let mut mux = ret.unwrap();
907 parse_and_apply_options!(mux, &transcoder.mux_opts, "output");
909 println!("Output {} muxer {}", transcoder.output_name, ofmt);
910 for ostr in mux.get_streams() {
911 println!(" #{}: {} {}", ostr.get_num(), ostr, ostr.get_info().get_name());
915 let pktres = dmx.get_frame();
916 if let Err(DemuxerError::EOF) = pktres { break; }
918 println!("demuxing error");
921 let mut pkt = pktres.unwrap();
922 if transcoder.start != NATimePoint::None && pkt.ts.less_than(transcoder.start) { continue; }
923 let src_id = pkt.get_stream().get_num();
924 match transcoder.encoders[src_id] {
925 OutputMode::Drop => {},
926 OutputMode::Copy(dst_id) => {
927 let dstr = mux.get_stream(dst_id as usize).unwrap();
928 pkt.reassign(dstr, pkt.get_time_information());
929 if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; }
930 if mux.mux_frame(pkt).is_err() {
931 println!("error muxing packet");
935 OutputMode::Encode(dst_id, ref mut encoder, ref mut cvt) => {
936 if let Some((ref mut dsupp, ref mut decoder)) = transcoder.decoders[src_id] {
937 let ret = decoder.decode(dsupp, &pkt);
938 if let (true, Err(DecoderError::MissingReference)) = (transcoder.start != NATimePoint::None, &ret) {
942 println!("error decoding stream {}", src_id);
945 let frm = ret.unwrap();
946 let buf = frm.get_buffer();
947 let cbuf = match cvt {
948 OutputConvert::None => buf,
949 OutputConvert::Video(ref mut scaler, ref mut dbuf) => {
950 let cur_ifmt = get_scale_fmt_from_pic(&buf);
951 let last_ifmt = scaler.get_in_fmt();
952 if cur_ifmt != last_ifmt {
953 let ofmt = scaler.get_out_fmt();
954 let ret = NAScale::new(cur_ifmt, ofmt);
956 println!("error re-initialising scaler for {} -> {}", cur_ifmt, ofmt);
959 *scaler = ret.unwrap();
961 let ret = scaler.convert(&buf, dbuf);
963 println!("error converting frame for encoding");
968 OutputConvert::Audio(ref dinfo, ref dchmap) => {
969 let ret = convert_audio_frame(&buf, dinfo, dchmap);
971 println!("error converting audio for stream {}", dst_id);
977 let cfrm = NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cbuf);
978 encoder.encode(&cfrm).unwrap();
979 while let Ok(Some(pkt)) = encoder.get_packet() {
980 if transcoder.end != NATimePoint::None && !pkt.ts.less_than(transcoder.end) { break 'main_loop; }
981 mux.mux_frame(pkt).unwrap();
984 println!("no decoder for stream {}", src_id);
990 'flush_loop: for enc in transcoder.encoders.iter_mut() {
992 OutputMode::Encode(str_id, ref mut encoder, _) => {
993 let ret = encoder.flush();
995 println!("error flushing encoder for stream {}", str_id);
998 while let Ok(Some(pkt)) = encoder.get_packet() {
999 if mux.mux_frame(pkt).is_err() {
1000 println!("error muxing packet");
1010 let ret = mux.end();
1012 println!("error at finalising muxing");