X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-commonfmt%2Fsrc%2Fcodecs%2Fpcm.rs;h=c0ef5d3bc1a2ab284aaa694a5557718c53831f7c;hb=e6aaad5c5273cd814b5748b7faf3751835a37217;hp=46b19928b1436ec1496696356404570277917b28;hpb=a480a0de101483d802a11e72d758dae00fa4860a;p=nihav.git diff --git a/nihav-commonfmt/src/codecs/pcm.rs b/nihav-commonfmt/src/codecs/pcm.rs index 46b1992..c0ef5d3 100644 --- a/nihav-commonfmt/src/codecs/pcm.rs +++ b/nihav-commonfmt/src/codecs/pcm.rs @@ -1,19 +1,23 @@ use nihav_core::formats::*; use nihav_core::codecs::*; +#[cfg(feature="encoder_pcm")] use nihav_core::io::byteio::*; #[derive(Clone,Copy,Debug,PartialEq)] +#[cfg(feature="decoder_pcm")] enum PCMMode { Infinity, ALaw, MuLaw, } +#[cfg(feature="decoder_pcm")] struct PCMDecoder { chmap: NAChannelMap, mode: PCMMode, } +#[cfg(feature="decoder_pcm")] fn cvt_alaw(val: u8) -> i16 { let val = val ^ 0x55; let sign = (val & 0x80) != 0; @@ -23,6 +27,7 @@ fn cvt_alaw(val: u8) -> i16 { if sign { aval } else { -aval } } +#[cfg(feature="decoder_pcm")] fn cvt_mulaw(val: u8) -> i16 { let val = !val; let sign = (val & 0x80) != 0; @@ -32,6 +37,7 @@ fn cvt_mulaw(val: u8) -> i16 { if !sign { aval } else { -aval } } +#[cfg(feature="decoder_pcm")] impl PCMDecoder { fn new(mode: PCMMode) -> Self { PCMDecoder { chmap: NAChannelMap::new(), mode } @@ -55,9 +61,12 @@ impl PCMDecoder { } } +#[cfg(feature="decoder_pcm")] const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C]; +#[cfg(feature="decoder_pcm")] const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R]; +#[cfg(feature="decoder_pcm")] fn get_default_chmap(nch: u8) -> NAChannelMap { let mut chmap = NAChannelMap::new(); match nch { @@ -68,16 +77,18 @@ fn get_default_chmap(nch: u8) -> NAChannelMap { chmap } +#[cfg(feature="decoder_pcm")] fn get_duration(ainfo: &NAAudioInfo, duration: Option, data_size: usize) -> u64 { - if duration == None { + if let Some(dur) = duration { + dur + } else { let size_bits = (data_size as u64) * 8; let blk_size = u64::from(ainfo.get_channels()) * u64::from(ainfo.get_format().get_bits()); size_bits / blk_size - } else { - duration.unwrap() } } +#[cfg(feature="decoder_pcm")] impl NADecoder for PCMDecoder { fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { @@ -109,29 +120,35 @@ impl NADecoder for PCMDecoder { } } +#[cfg(feature="decoder_pcm")] impl NAOptionHandler for PCMDecoder { fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } fn set_options(&mut self, _options: &[NAOption]) { } fn query_option_value(&self, _name: &str) -> Option { None } } +#[cfg(feature="decoder_pcm")] pub fn get_decoder() -> Box { Box::new(PCMDecoder::new(PCMMode::Infinity)) } +#[cfg(feature="decoder_pcm")] pub fn get_a_law_decoder() -> Box { Box::new(PCMDecoder::new(PCMMode::ALaw)) } +#[cfg(feature="decoder_pcm")] pub fn get_mu_law_decoder() -> Box { Box::new(PCMDecoder::new(PCMMode::MuLaw)) } +#[cfg(feature="encoder_pcm")] struct PCMEncoder { stream: Option, pkt: Option, } +#[cfg(feature="encoder_pcm")] impl PCMEncoder { fn new() -> Self { PCMEncoder { @@ -141,6 +158,7 @@ impl PCMEncoder { } } +#[allow(unused_macros)] macro_rules! write_buffer { ($abuf: expr, $dvec: expr, $write_be: ident, $write_le: ident, $dtype: tt) => { let info = $abuf.get_info(); @@ -166,13 +184,15 @@ macro_rules! write_buffer { } } +#[cfg(feature="encoder_pcm")] impl NAEncoder for PCMEncoder { fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult { match encinfo.format { NACodecTypeInfo::None => { - let mut ofmt = EncodeParameters::default(); - ofmt.format = NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16P_FORMAT, 0)); - Ok(ofmt) + Ok(EncodeParameters { + format: NACodecTypeInfo::Audio(NAAudioInfo::new(0, 0, SND_S16P_FORMAT, 0)), + ..Default::default() + }) }, NACodecTypeInfo::Video(_) => Err(EncoderError::FormatError), NACodecTypeInfo::Audio(_) => { @@ -180,6 +200,7 @@ impl NAEncoder for PCMEncoder { } } } + fn get_capabilities(&self) -> u64 { ENC_CAPS_CBR } fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult { match encinfo.format { NACodecTypeInfo::None => Err(EncoderError::FormatError), @@ -246,12 +267,14 @@ impl NAEncoder for PCMEncoder { } } +#[cfg(feature="encoder_pcm")] impl NAOptionHandler for PCMEncoder { fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } fn set_options(&mut self, _options: &[NAOption]) { } fn query_option_value(&self, _name: &str) -> Option { None } } +#[cfg(feature="encoder_pcm")] pub fn get_encoder() -> Box { Box::new(PCMEncoder::new()) }