X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fframe.rs;h=3af0d1d1af3ba09c11272b4d50702f11695779cf;hb=e64739f87a35f29be0bbbce366876180ba3eb57e;hp=6dfc5eb98d50a665963d388e75d99ae2c6163be4;hpb=dc45d8ce2269e638c10a18f99aed3294fd5de827;p=nihav.git diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index 6dfc5eb..3af0d1d 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -8,10 +8,10 @@ pub use crate::refs::*; #[allow(dead_code)] #[derive(Clone,Copy,PartialEq)] pub struct NAAudioInfo { - sample_rate: u32, - channels: u8, - format: NASoniton, - block_len: usize, + pub sample_rate: u32, + pub channels: u8, + pub format: NASoniton, + pub block_len: usize, } impl NAAudioInfo { @@ -118,6 +118,7 @@ impl NAVideoBuffer { pub fn get_info(&self) -> NAVideoInfo { self.info } 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 get_num_components(&self) -> usize { self.offs.len() } pub fn copy_buffer(&mut self) -> Self { let mut data: Vec = Vec::with_capacity(self.data.len()); data.clone_from(self.data.as_ref()); @@ -146,6 +147,7 @@ pub struct NAAudioBuffer { info: NAAudioInfo, data: NABufferRef>, offs: Vec, + stride: usize, chmap: NAChannelMap, len: usize, } @@ -155,8 +157,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 +167,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 +175,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 } } } @@ -233,6 +236,46 @@ impl NABufferType { _ => None, } } + pub fn get_audio_info(&self) -> Option { + match *self { + NABufferType::AudioU8(ref ab) => Some(ab.get_info()), + NABufferType::AudioI16(ref ab) => Some(ab.get_info()), + NABufferType::AudioI32(ref ab) => Some(ab.get_info()), + NABufferType::AudioF32(ref ab) => Some(ab.get_info()), + NABufferType::AudioPacked(ref ab) => Some(ab.get_info()), + _ => None, + } + } + pub fn get_chmap(&self) -> Option<&NAChannelMap> { + match *self { + NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()), + NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()), + NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()), + NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()), + NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()), + _ => None, + } + } + pub fn get_audio_length(&self) -> usize { + match *self { + NABufferType::AudioU8(ref ab) => ab.get_length(), + NABufferType::AudioI16(ref ab) => ab.get_length(), + NABufferType::AudioI32(ref ab) => ab.get_length(), + NABufferType::AudioF32(ref ab) => ab.get_length(), + NABufferType::AudioPacked(ref ab) => ab.get_length(), + _ => 0, + } + } + pub fn get_audio_stride(&self) -> usize { + match *self { + NABufferType::AudioU8(ref ab) => ab.get_stride(), + NABufferType::AudioI16(ref ab) => ab.get_stride(), + NABufferType::AudioI32(ref ab) => ab.get_stride(), + NABufferType::AudioF32(ref ab) => ab.get_stride(), + NABufferType::AudioPacked(ref ab) => ab.get_stride(), + _ => 0, + } + } pub fn get_abuf_u8(&self) -> Option> { match *self { NABufferType::AudioU8(ref ab) => Some(ab.clone()), @@ -360,9 +403,7 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result Result = 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) @@ -443,11 +482,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) @@ -458,7 +497,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)) } } @@ -668,12 +707,55 @@ impl NATimeInfo { pub fn set_pts(&mut self, pts: Option) { self.pts = pts; } pub fn set_dts(&mut self, dts: Option) { self.dts = dts; } pub fn set_duration(&mut self, dur: Option) { self.duration = dur; } + + pub fn time_to_ts(time: 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 = time.checked_mul(tb_num); + if let Some(tmp) = tmp { + tmp / base / tb_den + } else { + let tmp = time.checked_mul(tb_num); + if let Some(tmp) = tmp { + tmp / base / tb_den + } else { + let coarse = time / base; + let tmp = coarse.checked_mul(tb_num); + if let Some(tmp) = tmp { + tmp / tb_den + } else { + (coarse / tb_den) * tb_num + } + } + } + } + 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)] #[derive(Clone)] pub struct NAFrame { ts: NATimeInfo, + id: i64, buffer: NABufferType, info: NACodecInfoRef, ftype: FrameType, @@ -699,7 +781,7 @@ impl NAFrame { info: NACodecInfoRef, options: HashMap, buffer: NABufferType) -> Self { - NAFrame { ts, buffer, info, ftype, key: keyframe, options } + NAFrame { ts, id: 0, buffer, info, ftype, key: keyframe, options } } pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() } pub fn get_frame_type(&self) -> FrameType { self.ftype } @@ -709,9 +791,11 @@ impl NAFrame { pub fn get_time_information(&self) -> NATimeInfo { self.ts } pub fn get_pts(&self) -> Option { self.ts.get_pts() } pub fn get_dts(&self) -> Option { self.ts.get_dts() } + pub fn get_id(&self) -> i64 { self.id } pub fn get_duration(&self) -> Option { self.ts.get_duration() } pub fn set_pts(&mut self, pts: Option) { self.ts.set_pts(pts); } pub fn set_dts(&mut self, dts: Option) { self.ts.set_dts(dts); } + pub fn set_id(&mut self, id: i64) { self.id = id; } pub fn set_duration(&mut self, dur: Option) { self.ts.set_duration(dur); } pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } @@ -731,7 +815,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 @@ -792,6 +876,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() }