X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-commonfmt%2Fsrc%2Fdemuxers%2Fwav.rs;h=193623eea2f3c72359fd236ddf215fac329dcbcc;hb=bc22bba650c0ad4cd84d748468539b5dae982dc5;hp=1a2ead772e6ab85235d2f95b4f9f2f8543b30c85;hpb=817e487223b28379176a533f09485c27a68443f8;p=nihav.git diff --git a/nihav-commonfmt/src/demuxers/wav.rs b/nihav-commonfmt/src/demuxers/wav.rs index 1a2ead7..193623e 100644 --- a/nihav-commonfmt/src/demuxers/wav.rs +++ b/nihav-commonfmt/src/demuxers/wav.rs @@ -20,6 +20,9 @@ struct WAVDemuxer<'a> { is_pcm: bool, avg_bytes: u32, duration: u64, + + force_tb_num: u32, + force_tb_den: u32, } impl<'a> DemuxCore<'a> for WAVDemuxer<'a> { @@ -86,13 +89,28 @@ impl<'a> DemuxCore<'a> for WAVDemuxer<'a> { }; let ts = NATimeInfo::new(pts, None, None, 1, self.srate); if self.is_pcm { - let mut bsize = self.block_size; - while bsize < 256 { - bsize <<= 1; - } + let bsize = if self.force_tb_num != 0 && self.force_tb_den != 0 { + let nbsize = u64::from(self.avg_bytes) * u64::from(self.force_tb_num) / u64::from(self.force_tb_den); + let mut nbsize = nbsize as usize + self.block_size - 1; + nbsize /= self.block_size; + nbsize *= self.block_size; + nbsize + } else { + let mut bsize = self.block_size; + while bsize < 256 { + bsize <<= 1; + } + bsize + }; let mut buf = vec![0; bsize]; - let size = self.src.read_buf_some(buf.as_mut_slice())?; - buf.truncate(size); + let mut tot_size = 0; + while let Ok(psize) = self.src.read_buf_some(&mut buf[tot_size..]) { + tot_size += psize; + if tot_size == buf.len() { + break; + } + } + buf.truncate(tot_size); Ok(NAPacket::new(stream, ts, true, buf)) } else { self.src.read_packet(stream, ts, true, self.block_size) @@ -119,10 +137,37 @@ impl<'a> DemuxCore<'a> for WAVDemuxer<'a> { fn get_duration(&self) -> u64 { self.duration } } +const WAV_OPTIONS: &[NAOptionDefinition] = &[ + NAOptionDefinition { + name: "force_tb_num", description: "force timebase numerator for PCM blocks", + opt_type: NAOptionDefinitionType::Int(Some(1), None) }, + NAOptionDefinition { + name: "force_tb_den", description: "force timebase denominator for PCM blocks", + opt_type: NAOptionDefinitionType::Int(Some(1), None) }, +]; + impl<'a> NAOptionHandler for WAVDemuxer<'a> { - fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } - fn set_options(&mut self, _options: &[NAOption]) { } - fn query_option_value(&self, _name: &str) -> Option { None } + fn get_supported_options(&self) -> &[NAOptionDefinition] { WAV_OPTIONS } + fn set_options(&mut self, options: &[NAOption]) { + for option in options.iter() { + match (option.name, &option.value) { + ("force_tb_num", NAValue::Int(ref ival)) => { + self.force_tb_num = *ival as u32; + }, + ("force_tb_den", NAValue::Int(ref ival)) => { + self.force_tb_den = *ival as u32; + }, + _ => {}, + }; + } + } + fn query_option_value(&self, name: &str) -> Option { + match name { + "force_tb_num" => Some(NAValue::Int(i64::from(self.force_tb_num))), + "force_tb_den" => Some(NAValue::Int(i64::from(self.force_tb_den))), + _ => None, + } + } } impl<'a> WAVDemuxer<'a> { @@ -136,6 +181,8 @@ impl<'a> WAVDemuxer<'a> { is_pcm: false, avg_bytes: 0, duration: 0, + force_tb_num: 0, + force_tb_den: 0, } } fn parse_fmt(&mut self, strmgr: &mut StreamManager, csize: usize) -> DemuxerResult<()> {