From 3bba1c4a09266bf7bd0dfa7e7ca80f23015d83c8 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 10 Feb 2019 18:46:16 +0100 Subject: [PATCH] frame: add 32-bit packed video buffer and fix video buffer type allocation --- nihav-core/src/frame.rs | 56 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index cd25ca2..3ae8536 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -177,6 +177,7 @@ impl NAAudioBuffer { pub enum NABufferType { Video (NAVideoBuffer), Video16 (NAVideoBuffer), + Video32 (NAVideoBuffer), VideoPacked(NAVideoBuffer), AudioU8 (NAAudioBuffer), AudioI16 (NAAudioBuffer), @@ -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,6 +202,15 @@ impl NABufferType { _ => 0, } } + 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(&mut self) -> Option> { match *self { NABufferType::Video(ref vb) => Some(vb.clone()), @@ -213,6 +224,12 @@ impl NABufferType { _ => None, } } + pub fn get_vbuf32(&mut self) -> Option> { + match *self { + NABufferType::Video32(ref vb) => Some(vb.clone()), + _ => None, + } + } pub fn get_abuf_u8(&mut self) -> Option> { match *self { NABufferType::AudioU8(ref ab) => Some(ab.clone()), @@ -261,16 +278,22 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result false, + _ => true, + }; //todo semi-packed like NV12 if fmt.is_paletted() { @@ -313,13 +336,18 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = NAVideoBuffer { data: Rc::new(RefCell::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 }; Ok(NABufferType::Video16(buf)) + } else { + 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 }; + 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); } @@ -331,6 +359,28 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = NAVideoBuffer { data: Rc::new(RefCell::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: Rc::new(RefCell::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: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + Ok(NABufferType::Video32(buf)) + }, + _ => unreachable!(), + } } } -- 2.39.5