X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;ds=sidebyside;f=nihav-core%2Fsrc%2Fframe.rs;h=c6e392011cbd067abc3c77655228ab6ca39e6bb5;hb=8ee4352b1fad134b2fc3f137f9cfc94804446bfd;hp=b1a81b72874b40bcf2a73656fd6daf10ea8a855d;hpb=266da7b9db18727dc065a4e8971ecb0ac0b1e45f;p=nihav.git diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index b1a81b7..c6e3920 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -146,6 +146,7 @@ pub struct NAAudioBuffer { info: NAAudioInfo, data: NABufferRef>, offs: Vec, + stride: usize, chmap: NAChannelMap, len: usize, } @@ -155,8 +156,9 @@ impl NAAudioBuffer { if idx >= self.offs.len() { 0 } else { self.offs[idx] } } + pub fn get_stride(&self) -> usize { self.stride } pub fn get_info(&self) -> NAAudioInfo { self.info } - pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() } + pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap } pub fn get_data(&self) -> &Vec { self.data.as_ref() } pub fn get_data_mut(&mut self) -> Option<&mut Vec> { self.data.as_mut() } pub fn copy_buffer(&mut self) -> Self { @@ -164,7 +166,7 @@ impl NAAudioBuffer { data.clone_from(self.data.as_ref()); let mut offs: Vec = Vec::with_capacity(self.offs.len()); offs.clone_from(&self.offs); - NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap(), len: self.len } + NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride } } pub fn get_length(&self) -> usize { self.len } } @@ -172,7 +174,7 @@ impl NAAudioBuffer { impl NAAudioBuffer { pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef>, chmap: NAChannelMap) -> Self { let len = data.len(); - NAAudioBuffer { info, data, chmap, offs: Vec::new(), len } + NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0 } } } @@ -424,13 +426,14 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM let len = nsamples.checked_mul(ainfo.channels as usize); if len == None { return Err(AllocatorError::TooLargeDimensions); } let length = len.unwrap(); + let stride = nsamples; for i in 0..ainfo.channels { - offs.push((i as usize) * nsamples); + offs.push((i as usize) * stride); } if ainfo.format.is_float() { if ainfo.format.get_bits() == 32 { let data: Vec = vec![0.0; length]; - let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; Ok(NABufferType::AudioF32(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -438,11 +441,11 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM } else { if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { let data: Vec = vec![0; length]; - let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; Ok(NABufferType::AudioU8(buf)) } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { let data: Vec = vec![0; length]; - let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; Ok(NABufferType::AudioI16(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -453,7 +456,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM if len == None { return Err(AllocatorError::TooLargeDimensions); } let length = ainfo.format.get_audio_size(len.unwrap() as u64); let data: Vec = vec![0; length]; - let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0 }; Ok(NABufferType::AudioPacked(buf)) } } @@ -685,6 +688,26 @@ impl NATimeInfo { } } } + pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 { + let tb_num = tb_num as u64; + let tb_den = tb_den as u64; + let tmp = ts.checked_mul(base); + if let Some(tmp) = tmp { + let tmp2 = tmp.checked_mul(tb_num); + if let Some(tmp2) = tmp2 { + tmp2 / tb_den + } else { + (tmp / tb_den) * tb_num + } + } else { + let tmp = ts.checked_mul(tb_num); + if let Some(tmp) = tmp { + (tmp / tb_den) * base + } else { + (ts / tb_den) * base * tb_num + } + } + } } #[allow(dead_code)] @@ -751,7 +774,7 @@ impl fmt::Display for NAFrame { } /// Possible stream types. -#[derive(Debug,Clone,Copy)] +#[derive(Debug,Clone,Copy,PartialEq)] #[allow(dead_code)] pub enum StreamType { /// video stream @@ -812,6 +835,7 @@ impl NAStream { NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d } } pub fn get_id(&self) -> u32 { self.id } + pub fn get_media_type(&self) -> StreamType { self.media_type } pub fn get_num(&self) -> usize { self.num } pub fn set_num(&mut self, num: usize) { self.num = num; } pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }