X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fframe.rs;h=c086339bf3dd0299f7696cadd3a09798f0711010;hb=88c03b61673e1b5e7496433b621710a38134c588;hp=449512c2a10e027dd488898caf2a1263e6e81d4a;hpb=b1be9318bde52e93b833ef418bffcbdcb14e9d79;p=nihav.git diff --git a/src/frame.rs b/src/frame.rs index 449512c..c086339 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::fmt; use std::rc::Rc; +use std::cell::*; use formats::*; #[allow(dead_code)] @@ -71,12 +72,13 @@ impl fmt::Display for NACodecTypeInfo { } } +pub type BufferRef = Rc>>; #[allow(dead_code)] #[derive(Clone)] pub struct NABuffer { id: u64, - data: Rc>, + data: BufferRef, } impl Drop for NABuffer { @@ -84,10 +86,12 @@ impl Drop for NABuffer { } impl NABuffer { - pub fn get_data(&self) -> Rc> { self.data.clone() } - pub fn get_data_mut(&mut self) -> Option<&mut Vec> { Rc::get_mut(&mut self.data) } + pub fn get_data(&self) -> Ref> { self.data.borrow() } + pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } } +pub type NABufferRef = Rc>; + #[allow(dead_code)] #[derive(Clone)] pub struct NACodecInfo { @@ -171,7 +175,7 @@ fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec, offs: &mut Vec } } -pub fn alloc_buf(info: &NACodecInfo) -> (Rc, Vec) { +pub fn alloc_buf(info: &NACodecInfo) -> (NABufferRef, Vec) { let mut data: Vec = Vec::new(); let mut offs: Vec = Vec::new(); match info.properties { @@ -179,13 +183,13 @@ pub fn alloc_buf(info: &NACodecInfo) -> (Rc, Vec) { NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs), _ => (), } - (Rc::new(NABuffer { id: 0, data: Rc::new(data) }), offs) + (Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) })), offs) } -pub fn copy_buf(buf: &NABuffer) -> Rc { +pub fn copy_buf(buf: &NABuffer) -> NABufferRef { let mut data: Vec = Vec::new(); data.clone_from(buf.get_data().as_ref()); - Rc::new(NABuffer { id: 0, data: Rc::new(data) }) + Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) })) } #[derive(Debug,Clone)] @@ -197,14 +201,36 @@ pub enum NAValue { Data(Rc>), } +#[derive(Debug,Clone,Copy,PartialEq)] +#[allow(dead_code)] +pub enum FrameType { + I, + P, + B, + Other, +} + +impl fmt::Display for FrameType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + FrameType::I => write!(f, "I"), + FrameType::P => write!(f, "P"), + FrameType::B => write!(f, "B"), + FrameType::Other => write!(f, "x"), + } + } +} + #[allow(dead_code)] #[derive(Clone)] pub struct NAFrame { pts: Option, dts: Option, duration: Option, - buffer: Rc, + buffer: NABufferRef, info: Rc, + ftype: FrameType, + key: bool, offsets: Vec, options: HashMap, } @@ -222,27 +248,33 @@ impl NAFrame { pub fn new(pts: Option, dts: Option, duration: Option, + ftype: FrameType, + keyframe: bool, info: Rc, options: HashMap) -> Self { let (buf, offs) = alloc_buf(&info); - NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, options: options } + NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, ftype: ftype, key: keyframe, options: options } } pub fn from_copy(src: &NAFrame) -> Self { - let buf = copy_buf(src.get_buffer().as_ref()); + let buf = copy_buf(&src.get_buffer()); let mut offs: Vec = Vec::new(); offs.clone_from(&src.offsets); - NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), options: src.options.clone() } + NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), ftype: src.ftype, key: src.key, options: src.options.clone() } } pub fn get_pts(&self) -> Option { self.pts } pub fn get_dts(&self) -> Option { self.dts } pub fn get_duration(&self) -> Option { self.duration } + pub fn get_frame_type(&self) -> FrameType { self.ftype } + pub fn is_keyframe(&self) -> bool { self.key } 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 set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } + pub fn set_keyframe(&mut self, key: bool) { self.key = key; } pub fn get_offset(&self, idx: usize) -> usize { self.offsets[idx] } - pub fn get_buffer(&self) -> Rc { self.buffer.clone() } - pub fn get_buffer_mut(&mut self) -> Option<&mut NABuffer> { Rc::get_mut(&mut self.buffer) } + pub fn get_buffer(&self) -> Ref { self.buffer.borrow() } + pub fn get_buffer_mut(&mut self) -> RefMut { self.buffer.borrow_mut() } pub fn get_stride(&self, idx: usize) -> usize { if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() { if idx >= vinfo.get_format().get_num_comp() { return 0; } @@ -259,6 +291,8 @@ impl NAFrame { } } +pub type NAFrameRef = Rc>; + /// Possible stream types. #[derive(Debug,Clone,Copy)] #[allow(dead_code)] @@ -360,7 +394,7 @@ pub trait FrameFromPacket { impl FrameFromPacket for NAFrame { fn new_from_pkt(pkt: &NAPacket, info: Rc) -> NAFrame { - NAFrame::new(pkt.pts, pkt.dts, pkt.duration, info, HashMap::new()) + NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new()) } fn fill_timestamps(&mut self, pkt: &NAPacket) { self.set_pts(pkt.pts);