X-Git-Url: https://git.nihav.org/?p=nihav.git;a=blobdiff_plain;f=nihav-core%2Fsrc%2Fframe.rs;h=bc3216bd2b3a37a9247f4414caf951be723bf64e;hp=9756dfbaded9a3f3402cd3b029f46af926d11600;hb=2422d9699cd56cbb86ac32b3e8dd026e20a89db5;hpb=4e8b4f31bc2ef2b22b4c725aa07dfc776664a97b diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index 9756dfb..bc3216b 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; use std::fmt; pub use std::rc::Rc; pub use std::cell::*; +use std::sync::Arc; pub use crate::formats::*; +pub use crate::refs::*; #[allow(dead_code)] #[derive(Clone,Copy,PartialEq)] @@ -102,12 +104,10 @@ impl fmt::Display for NACodecTypeInfo { } } -pub type NABufferRefT = Rc>>; - #[derive(Clone)] pub struct NAVideoBuffer { info: NAVideoInfo, - data: NABufferRefT, + data: NABufferRef>, offs: Vec, strides: Vec, } @@ -118,16 +118,16 @@ impl NAVideoBuffer { else { self.offs[idx] } } pub fn get_info(&self) -> NAVideoInfo { self.info } - pub fn get_data(&self) -> Ref> { self.data.borrow() } - pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } + 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 { - let mut data: Vec = Vec::with_capacity(self.data.borrow().len()); - data.clone_from(self.data.borrow().as_ref()); + let mut data: Vec = Vec::with_capacity(self.data.len()); + data.clone_from(self.data.as_ref()); let mut offs: Vec = Vec::with_capacity(self.offs.len()); offs.clone_from(&self.offs); let mut strides: Vec = Vec::with_capacity(self.strides.len()); strides.clone_from(&self.strides); - NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides } + NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, strides: strides } } pub fn get_stride(&self, idx: usize) -> usize { if idx >= self.strides.len() { return 0; } @@ -141,7 +141,7 @@ impl NAVideoBuffer { #[derive(Clone)] pub struct NAAudioBuffer { info: NAAudioInfo, - data: NABufferRefT, + data: NABufferRef>, offs: Vec, chmap: NAChannelMap, len: usize, @@ -154,21 +154,21 @@ impl NAAudioBuffer { } pub fn get_info(&self) -> NAAudioInfo { self.info } pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() } - pub fn get_data(&self) -> Ref> { self.data.borrow() } - pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } + 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 { - let mut data: Vec = Vec::with_capacity(self.data.borrow().len()); - data.clone_from(self.data.borrow().as_ref()); + let mut data: Vec = Vec::with_capacity(self.data.len()); + 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: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len } + NAAudioBuffer { info: self.info, data: NABufferRef::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 { - let len = data.borrow().len(); + pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef>, chmap: NAChannelMap) -> Self { + let len = data.len(); NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len } } } @@ -177,13 +177,14 @@ impl NAAudioBuffer { pub enum NABufferType { Video (NAVideoBuffer), Video16 (NAVideoBuffer), + Video32 (NAVideoBuffer), VideoPacked(NAVideoBuffer), AudioU8 (NAAudioBuffer), AudioI16 (NAAudioBuffer), AudioI32 (NAAudioBuffer), AudioF32 (NAAudioBuffer), AudioPacked(NAAudioBuffer), - Data (NABufferRefT), + Data (NABufferRef>), None, } @@ -192,6 +193,7 @@ impl NABufferType { match *self { NABufferType::Video(ref vb) => vb.get_offset(idx), NABufferType::Video16(ref vb) => vb.get_offset(idx), + NABufferType::Video32(ref vb) => vb.get_offset(idx), NABufferType::VideoPacked(ref vb) => vb.get_offset(idx), NABufferType::AudioU8(ref ab) => ab.get_offset(idx), NABufferType::AudioI16(ref ab) => ab.get_offset(idx), @@ -200,39 +202,54 @@ impl NABufferType { _ => 0, } } - pub fn get_vbuf(&mut self) -> Option> { + pub fn get_video_info(&self) -> Option { + match *self { + NABufferType::Video(ref vb) => Some(vb.get_info()), + NABufferType::Video16(ref vb) => Some(vb.get_info()), + NABufferType::Video32(ref vb) => Some(vb.get_info()), + NABufferType::VideoPacked(ref vb) => Some(vb.get_info()), + _ => None, + } + } + pub fn get_vbuf(&self) -> Option> { match *self { NABufferType::Video(ref vb) => Some(vb.clone()), NABufferType::VideoPacked(ref vb) => Some(vb.clone()), _ => None, } } - pub fn get_vbuf16(&mut self) -> Option> { + pub fn get_vbuf16(&self) -> Option> { match *self { NABufferType::Video16(ref vb) => Some(vb.clone()), _ => None, } } - pub fn get_abuf_u8(&mut self) -> Option> { + pub fn get_vbuf32(&self) -> Option> { + match *self { + NABufferType::Video32(ref vb) => Some(vb.clone()), + _ => None, + } + } + pub fn get_abuf_u8(&self) -> Option> { match *self { NABufferType::AudioU8(ref ab) => Some(ab.clone()), NABufferType::AudioPacked(ref ab) => Some(ab.clone()), _ => None, } } - pub fn get_abuf_i16(&mut self) -> Option> { + pub fn get_abuf_i16(&self) -> Option> { match *self { NABufferType::AudioI16(ref ab) => Some(ab.clone()), _ => None, } } - pub fn get_abuf_i32(&mut self) -> Option> { + pub fn get_abuf_i32(&self) -> Option> { match *self { NABufferType::AudioI32(ref ab) => Some(ab.clone()), _ => None, } } - pub fn get_abuf_f32(&mut self) -> Option> { + pub fn get_abuf_f32(&self) -> Option> { match *self { NABufferType::AudioF32(ref ab) => Some(ab.clone()), _ => None, @@ -240,6 +257,48 @@ impl NABufferType { } } +const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4; +pub struct NASimpleVideoFrame<'a, T: Copy> { + pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS], + pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS], + pub flip: bool, + pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS], + pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS], + pub components: usize, + pub data: &'a mut Vec, +} + +impl<'a, T:Copy> NASimpleVideoFrame<'a, T> { + pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer) -> Option { + let vinfo = vbuf.get_info(); + let components = vinfo.format.components as usize; + if components > NA_SIMPLE_VFRAME_COMPONENTS { + return None; + } + let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; + let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; + let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; + let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; + for comp in 0..components { + let (width, height) = vbuf.get_dimensions(comp); + w[comp] = width; + h[comp] = height; + s[comp] = vbuf.get_stride(comp); + o[comp] = vbuf.get_offset(comp); + } + let flip = vinfo.flipped; + Some(NASimpleVideoFrame { + width: w, + height: h, + flip, + stride: s, + offset: o, + components, + data: vbuf.data.as_mut().unwrap(), + }) + } +} + #[derive(Debug,Clone,Copy,PartialEq)] pub enum AllocatorError { TooLargeDimensions, @@ -261,16 +320,22 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result false, + _ => true, + }; //todo semi-packed like NV12 if fmt.is_paletted() { @@ -286,7 +351,7 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = Vec::with_capacity(new_size.unwrap()); data.resize(new_size.unwrap(), 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::Video(buf)) } else if !all_packed { for i in 0..fmt.get_num_comp() { @@ -311,15 +376,20 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = Vec::with_capacity(new_size); data.resize(new_size, 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::Video(buf)) - } else { + } else if max_depth <= 16 { let mut data: Vec = Vec::with_capacity(new_size); data.resize(new_size, 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::Video16(buf)) + } else { + let mut data: Vec = Vec::with_capacity(new_size); + data.resize(new_size, 0); + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; + Ok(NABufferType::Video32(buf)) } - } else { + } else if all_bytealigned || unfit_elem_size { let elem_sz = fmt.get_elem_size(); let line_sz = width.checked_mul(elem_sz as usize); if line_sz == None { return Err(AllocatorError::TooLargeDimensions); } @@ -329,8 +399,30 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = Vec::with_capacity(new_size); data.resize(new_size, 0); strides.push(line_sz.unwrap()); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::VideoPacked(buf)) + } else { + let elem_sz = fmt.get_elem_size(); + let new_sz = width.checked_mul(height); + if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } + new_size = new_sz.unwrap(); + match elem_sz { + 2 => { + let mut data: Vec = Vec::with_capacity(new_size); + data.resize(new_size, 0); + strides.push(width); + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; + Ok(NABufferType::Video16(buf)) + }, + 4 => { + let mut data: Vec = Vec::with_capacity(new_size); + data.resize(new_size, 0); + strides.push(width); + let buf: NAVideoBuffer = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; + Ok(NABufferType::Video32(buf)) + }, + _ => unreachable!(), + } } } @@ -347,7 +439,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, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioF32(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -356,12 +448,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, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::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, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioI16(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -373,7 +465,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, len: nsamples }; + let buf: NAAudioBuffer = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioPacked(buf)) } } @@ -381,7 +473,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM pub fn alloc_data_buffer(size: usize) -> Result { let mut data: Vec = Vec::with_capacity(size); data.resize(size, 0); - let buf: NABufferRefT = Rc::new(RefCell::new(data)); + let buf: NABufferRef> = NABufferRef::new(data); Ok(NABufferType::Data(buf)) } @@ -389,27 +481,76 @@ pub fn copy_buffer(buf: NABufferType) -> NABufferType { buf.clone() } +pub struct NABufferPool { + pool: Vec>, + max_len: usize, +} + +impl NABufferPool { + pub fn new(max_len: usize) -> Self { + Self { + pool: Vec::with_capacity(max_len), + max_len, + } + } + pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { + let nbufs = self.max_len - self.pool.len(); + for _ in 0..nbufs { + let buf = alloc_video_buffer(vinfo.clone(), align)?; + self.pool.push(NABufferRef::new(buf)); + } + Ok(()) + } + pub fn prealloc_audio(&mut self, ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<(), AllocatorError> { + let nbufs = self.max_len - self.pool.len(); + for _ in 0..nbufs { + let buf = alloc_audio_buffer(ainfo.clone(), nsamples, chmap.clone())?; + self.pool.push(NABufferRef::new(buf)); + } + Ok(()) + } + pub fn add(&mut self, buf: NABufferType) -> bool { + if self.pool.len() < self.max_len { + self.pool.push(NABufferRef::new(buf)); + true + } else { + false + } + } + pub fn get_free(&mut self) -> Option> { + for e in self.pool.iter() { + if e.get_num_refs() == 1 { + return Some(e.clone()); + } + } + None + } +} + #[allow(dead_code)] #[derive(Clone)] pub struct NACodecInfo { name: &'static str, properties: NACodecTypeInfo, - extradata: Option>>, + extradata: Option>>, } +pub type NACodecInfoRef = Arc; + impl NACodecInfo { pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option>) -> Self { let extradata = match edata { None => None, - Some(vec) => Some(Rc::new(vec)), + Some(vec) => Some(Arc::new(vec)), }; NACodecInfo { name: name, properties: p, extradata: extradata } } - pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option>>) -> Self { + pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option>>) -> Self { NACodecInfo { name: name, properties: p, extradata: edata } } + pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) } pub fn get_properties(&self) -> NACodecTypeInfo { self.properties } - pub fn get_extradata(&self) -> Option>> { + pub fn get_extradata(&self) -> Option>> { if let Some(ref vec) = self.extradata { return Some(vec.clone()); } None } @@ -422,14 +563,18 @@ impl NACodecInfo { if let NACodecTypeInfo::Audio(_) = self.properties { return true; } false } - pub fn new_dummy() -> Rc { - Rc::new(DUMMY_CODEC_INFO) + pub fn new_dummy() -> Arc { + Arc::new(DUMMY_CODEC_INFO) } - pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc { - Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() }) + pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc { + Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() }) } } +impl Default for NACodecInfo { + fn default() -> Self { DUMMY_CODEC_INFO } +} + impl fmt::Display for NACodecInfo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let edata = match self.extradata.clone() { @@ -451,7 +596,7 @@ pub enum NAValue { Int(i32), Long(i64), String(String), - Data(Rc>), + Data(Arc>), } #[derive(Debug,Clone,Copy,PartialEq)] @@ -502,7 +647,7 @@ impl NATimeInfo { pub struct NAFrame { ts: NATimeInfo, buffer: NABufferType, - info: Rc, + info: NACodecInfoRef, ftype: FrameType, key: bool, options: HashMap, @@ -523,12 +668,12 @@ impl NAFrame { pub fn new(ts: NATimeInfo, ftype: FrameType, keyframe: bool, - info: Rc, + info: NACodecInfoRef, options: HashMap, 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_info(&self) -> NACodecInfoRef { 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; } @@ -589,7 +734,7 @@ pub struct NAStream { media_type: StreamType, id: u32, num: usize, - info: Rc, + info: NACodecInfoRef, tb_num: u32, tb_den: u32, } @@ -612,12 +757,12 @@ pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { impl NAStream { pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self { let (n, d) = reduce_timebase(tb_num, tb_den); - NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d } + NAStream { media_type: mt, id: id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d } } pub fn get_id(&self) -> u32 { self.id } pub fn get_num(&self) -> usize { self.num } pub fn set_num(&mut self, num: usize) { self.num = num; } - pub fn get_info(&self) -> Rc { self.info.clone() } + pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() } pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) } pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) { let (n, d) = reduce_timebase(tb_num, tb_den); @@ -636,7 +781,7 @@ impl fmt::Display for NAStream { pub struct NAPacket { stream: Rc, ts: NATimeInfo, - buffer: Rc>, + buffer: NABufferRef>, keyframe: bool, // options: HashMap>, } @@ -645,7 +790,7 @@ impl NAPacket { pub fn new(str: Rc, ts: NATimeInfo, kf: bool, vec: Vec) -> Self { // let mut vec: Vec = Vec::new(); // vec.resize(size, 0); - NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) } + NAPacket { stream: str, ts: ts, keyframe: kf, buffer: NABufferRef::new(vec) } } pub fn get_stream(&self) -> Rc { self.stream.clone() } pub fn get_time_information(&self) -> NATimeInfo { self.ts } @@ -653,7 +798,7 @@ impl NAPacket { pub fn get_dts(&self) -> Option { self.ts.get_dts() } pub fn get_duration(&self) -> Option { self.ts.get_duration() } pub fn is_keyframe(&self) -> bool { self.keyframe } - pub fn get_buffer(&self) -> Rc> { self.buffer.clone() } + pub fn get_buffer(&self) -> NABufferRef> { self.buffer.clone() } } impl Drop for NAPacket { @@ -673,12 +818,12 @@ impl fmt::Display for NAPacket { } pub trait FrameFromPacket { - fn new_from_pkt(pkt: &NAPacket, info: Rc, buf: NABufferType) -> NAFrame; + fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame; fn fill_timestamps(&mut self, pkt: &NAPacket); } impl FrameFromPacket for NAFrame { - fn new_from_pkt(pkt: &NAPacket, info: Rc, buf: NABufferType) -> NAFrame { + fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame { NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) } fn fill_timestamps(&mut self, pkt: &NAPacket) {