From 34219db3e2b43a690418e3d157b59a9f052ad343 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Fri, 25 Aug 2023 18:54:44 +0200 Subject: [PATCH] wav: allow demuxing custom block sizes --- nihav-commonfmt/src/demuxers/wav.rs | 55 +++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/nihav-commonfmt/src/demuxers/wav.rs b/nihav-commonfmt/src/demuxers/wav.rs index 1a2ead7..9019617 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,10 +89,19 @@ 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); @@ -119,10 +131,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 +175,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<()> { -- 2.30.2