From 5076115b9a0c4ef344802af408b0e8fe7f7d95fa Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Thu, 15 Jun 2017 12:50:28 +0200 Subject: [PATCH] frame: more utility functions --- src/frame.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/frame.rs b/src/frame.rs index e739559..b6cc9eb 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -75,6 +75,18 @@ impl NACodecTypeInfo { _ => None, } } + pub fn is_video(&self) -> bool { + match *self { + NACodecTypeInfo::Video(_) => true, + _ => false, + } + } + pub fn is_audio(&self) -> bool { + match *self { + NACodecTypeInfo::Audio(_) => true, + _ => false, + } + } } impl fmt::Display for NACodecTypeInfo { @@ -130,6 +142,7 @@ pub struct NAAudioBuffer { data: NABufferRefT, offs: Vec, chmap: NAChannelMap, + len: usize, } impl NAAudioBuffer { @@ -146,13 +159,15 @@ impl NAAudioBuffer { data.clone_from(self.data.borrow().as_ref()); let mut offs: Vec = Vec::with_capacity(self.offs.len()); offs.clone_from(&self.offs); - NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap() } + NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len } } + pub fn get_length(&self) -> usize { self.len } } impl NAAudioBuffer { pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT, chmap: NAChannelMap) -> Self { - NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new() } + let len = data.borrow().len(); + NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len } } } @@ -330,7 +345,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM if ainfo.format.get_bits() == 32 { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0.0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioF32(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -339,12 +354,12 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioU8(buf)) } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioI16(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -356,7 +371,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM let length = ainfo.format.get_audio_size(len.unwrap() as u64); let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioPacked(buf)) } } @@ -405,6 +420,12 @@ impl NACodecInfo { if let NACodecTypeInfo::Audio(_) = self.properties { return true; } false } + pub fn new_dummy() -> Rc { + Rc::new(DUMMY_CODEC_INFO) + } + pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc { + Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() }) + } } impl fmt::Display for NACodecInfo { @@ -505,6 +526,7 @@ impl NAFrame { buffer: NABufferType) -> Self { NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } } + pub fn get_info(&self) -> Rc { self.info.clone() } pub fn get_frame_type(&self) -> FrameType { self.ftype } pub fn is_keyframe(&self) -> bool { self.key } pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } -- 2.39.5