1 use nihav_core::muxers::*;
2 use nihav_registry::register::*;
5 bw: &'a mut ByteWriter<'a>,
9 impl<'a> WAVMuxer<'a> {
10 fn new(bw: &'a mut ByteWriter<'a>) -> Self {
18 fn patch_size(bw: &mut ByteWriter, pos: u64) -> MuxerResult<()> {
19 let size = bw.tell() - pos;
20 bw.seek(SeekFrom::Current(-((size + 4) as i64)))?;
21 bw.write_u32le(size as u32)?;
22 bw.seek(SeekFrom::End(0))?;
26 impl<'a> MuxCore<'a> for WAVMuxer<'a> {
27 fn create(&mut self, strmgr: &StreamManager) -> MuxerResult<()> {
28 if strmgr.get_num_streams() != 1 {
29 return Err(MuxerError::InvalidArgument);
32 let stream = strmgr.get_stream(0).unwrap();
34 if stream.get_info().get_properties().get_audio_info().is_none() {
35 return Err(MuxerError::InvalidArgument);
37 let ainfo = stream.get_info().get_properties().get_audio_info().unwrap();
39 let edata_len = if let Some(ref buf) = stream.get_info().get_extradata() { buf.len() } else { 0 };
40 if edata_len >= (1 << 16) {
41 return Err(MuxerError::UnsupportedFormat);
44 let twocc = find_wav_twocc(stream.get_info().get_name());
46 return Err(MuxerError::UnsupportedFormat);
48 let twocc = if stream.get_info().get_name() == "pcm" {
49 if !ainfo.format.float { 0x0001 } else { 0x0003 }
53 let avg_bytes_per_sec = if stream.get_info().get_name() == "pcm" {
54 (u32::from(ainfo.channels) * ainfo.sample_rate * u32::from(ainfo.format.bits)) >> 3
59 self.bw.write_buf(b"RIFF\0\0\0\0WAVEfmt ")?;
60 self.bw.write_u32le(if edata_len == 0 { 16 } else { 18 + edata_len } as u32)?;
61 self.bw.write_u16le(twocc)?;
62 self.bw.write_u16le(u16::from(ainfo.channels))?;
63 self.bw.write_u32le(ainfo.sample_rate)?;
64 self.bw.write_u32le(avg_bytes_per_sec)?;
65 self.bw.write_u16le(ainfo.block_len as u16)?;
66 self.bw.write_u16le(u16::from(ainfo.format.bits))?;
67 if let Some(ref buf) = stream.get_info().get_extradata() {
68 self.bw.write_u16le(edata_len as u16)?;
69 self.bw.write_buf(buf.as_slice())?;
71 self.bw.write_buf(b"data\0\0\0\0")?;
72 self.data_pos = self.bw.tell();
76 fn mux_frame(&mut self, _strmgr: &StreamManager, pkt: NAPacket) -> MuxerResult<()> {
77 if self.data_pos == 0 {
78 return Err(MuxerError::NotCreated);
81 let stream = pkt.get_stream();
82 if stream.get_num() != 0 {
83 return Err(MuxerError::UnsupportedFormat);
86 self.bw.write_buf(pkt.get_buffer().as_slice())?;
89 fn flush(&mut self) -> MuxerResult<()> {
92 fn end(&mut self) -> MuxerResult<()> {
93 patch_size(self.bw, self.data_pos)?;
94 patch_size(self.bw, 8)?;
95 // todo patch avg_bytes_per_second if calculated
96 // todo write fact value if calculated
101 impl<'a> NAOptionHandler for WAVMuxer<'a> {
102 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
103 fn set_options(&mut self, _options: &[NAOption]) { }
104 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
107 pub struct WAVMuxerCreator {}
109 impl MuxerCreator for WAVMuxerCreator {
110 fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box<dyn MuxCore<'a> + 'a> {
111 Box::new(WAVMuxer::new(bw))
113 fn get_name(&self) -> &'static str { "wav" }
114 fn get_capabilities(&self) -> MuxerCapabilities { MuxerCapabilities::SingleAudio("any") }
119 use nihav_core::codecs::*;
120 use nihav_core::demuxers::*;
121 use nihav_core::muxers::*;
122 use nihav_codec_support::test::enc_video::*;
126 fn test_wav_muxer() {
127 let mut dmx_reg = RegisteredDemuxers::new();
128 generic_register_all_demuxers(&mut dmx_reg);
129 // sample: https://samples.mplayerhq.hu/V-codecs/RT21/320x240/laser05.avi
130 let dec_config = DecoderTestParams {
132 in_name: "assets/Indeo/laser05.avi",
134 stream_type: StreamType::None,
135 dmx_reg, dec_reg: RegisteredDecoders::new(),
137 let mut mux_reg = RegisteredMuxers::new();
138 generic_register_all_muxers(&mut mux_reg);
139 /*let enc_config = EncoderTestParams {
142 out_name: "muxed.wav",
143 mux_reg, enc_reg: RegisteredEncoders::new(),
145 test_remuxing(&dec_config, &enc_config);*/
146 test_remuxing_md5(&dec_config, "wav", &mux_reg,
147 [0x1040ebe8, 0xe7a43e84, 0x49fbe234, 0xe870b6b3]);