X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fframe.rs;h=af7b498e92811b1acd1c3697482b32e14aacd46f;hb=01613464323864a655c994820d3c43df1954e3b2;hp=ea9f2031b5b7eacc4e6da36653399a0420bf22b3;hpb=1a967e6bad5f17943b4de0607078eb940ad5adfe;p=nihav.git diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index ea9f203..af7b498 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -1,8 +1,7 @@ use std::cmp::max; 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::*; @@ -135,8 +134,13 @@ impl NAVideoBuffer { pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { get_plane_size(&self.info, idx) } + pub fn into_ref(self) -> NABufferRef { + NABufferRef::new(self) + } } +pub type NAVideoBufferRef = NABufferRef>; + #[derive(Clone)] pub struct NAAudioBuffer { info: NAAudioInfo, @@ -174,10 +178,10 @@ impl NAAudioBuffer { #[derive(Clone)] pub enum NABufferType { - Video (NAVideoBuffer), - Video16 (NAVideoBuffer), - Video32 (NAVideoBuffer), - VideoPacked(NAVideoBuffer), + Video (NAVideoBufferRef), + Video16 (NAVideoBufferRef), + Video32 (NAVideoBufferRef), + VideoPacked(NAVideoBufferRef), AudioU8 (NAAudioBuffer), AudioI16 (NAAudioBuffer), AudioI32 (NAAudioBuffer), @@ -210,20 +214,20 @@ impl NABufferType { _ => None, } } - pub fn get_vbuf(&self) -> Option> { + 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(&self) -> Option> { + pub fn get_vbuf16(&self) -> Option> { match *self { NABufferType::Video16(ref vb) => Some(vb.clone()), _ => None, } } - pub fn get_vbuf32(&self) -> Option> { + pub fn get_vbuf32(&self) -> Option> { match *self { NABufferType::Video32(ref vb) => Some(vb.clone()), _ => None, @@ -256,6 +260,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, @@ -309,7 +355,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: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; - Ok(NABufferType::Video(buf)) + Ok(NABufferType::Video(buf.into_ref())) } else if !all_packed { for i in 0..fmt.get_num_comp() { let ochr = fmt.get_chromaton(i); @@ -334,17 +380,17 @@ 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: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; - Ok(NABufferType::Video(buf)) + Ok(NABufferType::Video(buf.into_ref())) } else if max_depth <= 16 { 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::Video16(buf)) + Ok(NABufferType::Video16(buf.into_ref())) } 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)) + Ok(NABufferType::Video32(buf.into_ref())) } } else if all_bytealigned || unfit_elem_size { let elem_sz = fmt.get_elem_size(); @@ -357,7 +403,7 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; - Ok(NABufferType::VideoPacked(buf)) + Ok(NABufferType::VideoPacked(buf.into_ref())) } else { let elem_sz = fmt.get_elem_size(); let new_sz = width.checked_mul(height); @@ -369,14 +415,14 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides }; - Ok(NABufferType::Video16(buf)) + Ok(NABufferType::Video16(buf.into_ref())) }, 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)) + Ok(NABufferType::Video32(buf.into_ref())) }, _ => unreachable!(), } @@ -438,49 +484,89 @@ pub fn copy_buffer(buf: NABufferType) -> NABufferType { buf.clone() } -pub struct NABufferPool { - pool: Vec>, +pub struct NAVideoBufferPool { + pool: Vec>, max_len: usize, + add_len: usize, } -impl NABufferPool { +impl NAVideoBufferPool { pub fn new(max_len: usize) -> Self { Self { pool: Vec::with_capacity(max_len), max_len, + add_len: 0, } } + pub fn set_dec_bufs(&mut self, add_len: usize) { + self.add_len = add_len; + } + pub fn get_free(&mut self) -> Option> { + for e in self.pool.iter() { + if e.get_num_refs() == 1 { + return Some(e.clone()); + } + } + None + } + pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef) -> Option> { + let res = self.get_free(); + if res.is_none() { + return None; + } + let mut dbuf = res.unwrap(); + dbuf.data.copy_from_slice(&rbuf.data); + Some(dbuf) + } + pub fn reset(&mut self) { + self.pool.truncate(0); + } +} + +impl NAVideoBufferPool { pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { - let nbufs = self.max_len - self.pool.len(); + let nbufs = self.max_len + self.add_len - self.pool.len(); for _ in 0..nbufs { - let buf = alloc_video_buffer(vinfo.clone(), align)?; - self.pool.push(NABufferRef::new(buf)); + let vbuf = alloc_video_buffer(vinfo.clone(), align)?; + if let NABufferType::Video(buf) = vbuf { + self.pool.push(buf); + } else if let NABufferType::VideoPacked(buf) = vbuf { + self.pool.push(buf); + } else { + return Err(AllocatorError::FormatError); + } } Ok(()) } - pub fn prealloc_audio(&mut self, ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<(), AllocatorError> { - let nbufs = self.max_len - self.pool.len(); +} + +impl NAVideoBufferPool { + pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { + let nbufs = self.max_len + self.add_len - self.pool.len(); for _ in 0..nbufs { - let buf = alloc_audio_buffer(ainfo.clone(), nsamples, chmap.clone())?; - self.pool.push(NABufferRef::new(buf)); + let vbuf = alloc_video_buffer(vinfo.clone(), align)?; + if let NABufferType::Video16(buf) = vbuf { + self.pool.push(buf); + } else { + return Err(AllocatorError::FormatError); + } } 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()); +} + +impl NAVideoBufferPool { + pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { + let nbufs = self.max_len + self.add_len - self.pool.len(); + for _ in 0..nbufs { + let vbuf = alloc_video_buffer(vinfo.clone(), align)?; + if let NABufferType::Video32(buf) = vbuf { + self.pool.push(buf); + } else { + return Err(AllocatorError::FormatError); } } - None + Ok(()) } } @@ -489,22 +575,25 @@ impl NABufferPool { 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 } @@ -517,11 +606,11 @@ 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() }) } } @@ -550,7 +639,7 @@ pub enum NAValue { Int(i32), Long(i64), String(String), - Data(Rc>), + Data(Arc>), } #[derive(Debug,Clone,Copy,PartialEq)] @@ -601,13 +690,13 @@ impl NATimeInfo { pub struct NAFrame { ts: NATimeInfo, buffer: NABufferType, - info: Rc, + info: NACodecInfoRef, ftype: FrameType, key: bool, options: HashMap, } -pub type NAFrameRef = Rc>; +pub type NAFrameRef = Arc; fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { let chromaton = info.get_format().get_chromaton(idx); @@ -622,12 +711,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; } @@ -641,6 +730,8 @@ impl NAFrame { pub fn set_duration(&mut self, dur: Option) { self.ts.set_duration(dur); } pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } + + pub fn into_ref(self) -> NAFrameRef { Arc::new(self) } } impl fmt::Display for NAFrame { @@ -688,11 +779,13 @@ pub struct NAStream { media_type: StreamType, id: u32, num: usize, - info: Rc, + info: NACodecInfoRef, tb_num: u32, tb_den: u32, } +pub type NAStreamRef = Arc; + pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { if tb_num == 0 { return (tb_num, tb_den); } if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); } @@ -711,18 +804,19 @@ 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); self.tb_num = n; self.tb_den = d; } + pub fn into_ref(self) -> NAStreamRef { Arc::new(self) } } impl fmt::Display for NAStream { @@ -733,7 +827,7 @@ impl fmt::Display for NAStream { #[allow(dead_code)] pub struct NAPacket { - stream: Rc, + stream: NAStreamRef, ts: NATimeInfo, buffer: NABufferRef>, keyframe: bool, @@ -741,12 +835,12 @@ pub struct NAPacket { } impl NAPacket { - pub fn new(str: Rc, ts: NATimeInfo, kf: bool, vec: Vec) -> Self { + pub fn new(str: NAStreamRef, 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: NABufferRef::new(vec) } } - pub fn get_stream(&self) -> Rc { self.stream.clone() } + pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() } 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() } @@ -772,12 +866,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) {