]>
Commit | Line | Data |
---|---|---|
db2b1e6c KS |
1 | use nihav_core::muxers::*; |
2 | use nihav_registry::register::*; | |
3 | ||
4 | struct WAVMuxer<'a> { | |
5 | bw: &'a mut ByteWriter<'a>, | |
6 | data_pos: u64, | |
7 | } | |
8 | ||
9 | impl<'a> WAVMuxer<'a> { | |
10 | fn new(bw: &'a mut ByteWriter<'a>) -> Self { | |
11 | Self { | |
12 | bw, | |
13 | data_pos: 0, | |
14 | } | |
15 | } | |
16 | } | |
17 | ||
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))?; | |
23 | Ok(()) | |
24 | } | |
25 | ||
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); | |
30 | } | |
31 | ||
32 | let stream = strmgr.get_stream(0).unwrap(); | |
33 | ||
34 | if stream.get_info().get_properties().get_audio_info().is_none() { | |
35 | return Err(MuxerError::InvalidArgument); | |
36 | } | |
37 | let ainfo = stream.get_info().get_properties().get_audio_info().unwrap(); | |
38 | ||
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); | |
42 | } | |
43 | ||
44 | let twocc = find_wav_twocc(stream.get_info().get_name()); | |
45 | if twocc.is_none() { | |
46 | return Err(MuxerError::UnsupportedFormat); | |
47 | } | |
48 | let twocc = if stream.get_info().get_name() == "pcm" { | |
49 | if !ainfo.format.float { 0x0001 } else { 0x0003 } | |
50 | } else { | |
51 | twocc.unwrap_or(0) | |
52 | }; | |
53 | let avg_bytes_per_sec = if stream.get_info().get_name() == "pcm" { | |
61cab15b | 54 | (u32::from(ainfo.channels) * ainfo.sample_rate * u32::from(ainfo.format.bits)) >> 3 |
db2b1e6c KS |
55 | } else { |
56 | 0 | |
57 | }; | |
58 | ||
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)?; | |
61cab15b | 62 | self.bw.write_u16le(u16::from(ainfo.channels))?; |
db2b1e6c KS |
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)?; | |
61cab15b | 66 | self.bw.write_u16le(u16::from(ainfo.format.bits))?; |
db2b1e6c KS |
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())?; | |
70 | } | |
71 | self.bw.write_buf(b"data\0\0\0\0")?; | |
72 | self.data_pos = self.bw.tell(); | |
73 | ||
74 | Ok(()) | |
75 | } | |
76 | fn mux_frame(&mut self, _strmgr: &StreamManager, pkt: NAPacket) -> MuxerResult<()> { | |
77 | if self.data_pos == 0 { | |
78 | return Err(MuxerError::NotCreated); | |
79 | } | |
80 | ||
81 | let stream = pkt.get_stream(); | |
82 | if stream.get_num() != 0 { | |
83 | return Err(MuxerError::UnsupportedFormat); | |
84 | } | |
85 | ||
86 | self.bw.write_buf(pkt.get_buffer().as_slice())?; | |
87 | Ok(()) | |
88 | } | |
89 | fn flush(&mut self) -> MuxerResult<()> { | |
90 | Ok(()) | |
91 | } | |
92 | fn end(&mut self) -> MuxerResult<()> { | |
93 | patch_size(&mut self.bw, self.data_pos)?; | |
94 | patch_size(&mut self.bw, 8)?; | |
95 | // todo patch avg_bytes_per_second if calculated | |
96 | // todo write fact value if calculated | |
97 | Ok(()) | |
98 | } | |
99 | } | |
100 | ||
dc80f48e KS |
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 } | |
105 | } | |
106 | ||
db2b1e6c KS |
107 | pub struct WAVMuxerCreator {} |
108 | ||
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)) | |
112 | } | |
113 | fn get_name(&self) -> &'static str { "wav" } | |
f0081142 | 114 | fn get_capabilities(&self) -> MuxerCapabilities { MuxerCapabilities::SingleAudio("any") } |
db2b1e6c KS |
115 | } |
116 | ||
117 | #[cfg(test)] | |
118 | mod test { | |
68e5a4ca | 119 | use nihav_core::codecs::*; |
db2b1e6c | 120 | use nihav_core::demuxers::*; |
68e5a4ca KS |
121 | use nihav_core::muxers::*; |
122 | use nihav_codec_support::test::enc_video::*; | |
123 | use crate::*; | |
db2b1e6c KS |
124 | |
125 | #[test] | |
126 | fn test_wav_muxer() { | |
127 | let mut dmx_reg = RegisteredDemuxers::new(); | |
128 | generic_register_all_demuxers(&mut dmx_reg); | |
68e5a4ca KS |
129 | let dec_config = DecoderTestParams { |
130 | demuxer: "avi", | |
131 | in_name: "assets/Indeo/laser05.avi", | |
132 | limit: None, | |
133 | stream_type: StreamType::None, | |
134 | dmx_reg, dec_reg: RegisteredDecoders::new(), | |
135 | }; | |
136 | let mut mux_reg = RegisteredMuxers::new(); | |
137 | generic_register_all_muxers(&mut mux_reg); | |
138 | /*let enc_config = EncoderTestParams { | |
139 | muxer: "wav", | |
140 | enc_name: "", | |
141 | out_name: "muxed.wav", | |
142 | mux_reg, enc_reg: RegisteredEncoders::new(), | |
143 | }; | |
144 | test_remuxing(&dec_config, &enc_config);*/ | |
145 | test_remuxing_md5(&dec_config, "wav", &mux_reg, | |
146 | [0x1040ebe8, 0xe7a43e84, 0x49fbe234, 0xe870b6b3]); | |
db2b1e6c KS |
147 | } |
148 | } |