From 1a967e6bad5f17943b4de0607078eb940ad5adfe Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 28 Apr 2019 15:15:50 +0200 Subject: [PATCH] switch to refcounted buffers --- nihav-commonfmt/src/codecs/aac.rs | 2 +- nihav-commonfmt/src/codecs/atrac3.rs | 2 +- nihav-commonfmt/src/codecs/clearvideo.rs | 8 +- nihav-commonfmt/src/codecs/pcm.rs | 4 +- nihav-commonfmt/src/codecs/sipro.rs | 2 +- nihav-commonfmt/src/codecs/ts102366.rs | 2 +- nihav-core/src/codecs/blockdsp.rs | 8 +- nihav-core/src/codecs/h263/code.rs | 4 +- nihav-core/src/demuxers/mod.rs | 4 +- nihav-core/src/frame.rs | 109 ++++++++++++++++------- nihav-core/src/lib.rs | 1 + nihav-core/src/refs.rs | 87 ++++++++++++++++++ nihav-duck/src/codecs/dkadpcm.rs | 2 +- nihav-duck/src/codecs/truemotion1.rs | 4 +- nihav-duck/src/codecs/truemotion2.rs | 2 +- nihav-duck/src/codecs/truemotion2x.rs | 2 +- nihav-duck/src/codecs/truemotionrt.rs | 2 +- nihav-game/src/codecs/bmv.rs | 4 +- nihav-game/src/codecs/bmv3.rs | 4 +- nihav-game/src/codecs/gremlinvideo.rs | 4 +- nihav-game/src/codecs/vmd.rs | 6 +- nihav-indeo/src/codecs/imc.rs | 2 +- nihav-indeo/src/codecs/indeo2.rs | 4 +- nihav-indeo/src/codecs/indeo3.rs | 2 +- nihav-indeo/src/codecs/ivibr.rs | 4 +- nihav-rad/src/codecs/bink2.rs | 2 +- nihav-rad/src/codecs/binkaud.rs | 2 +- nihav-rad/src/codecs/binkvid.rs | 4 +- nihav-rad/src/codecs/smacker.rs | 8 +- nihav-realmedia/src/codecs/cook.rs | 2 +- nihav-realmedia/src/codecs/ra144.rs | 2 +- nihav-realmedia/src/codecs/ra288.rs | 2 +- nihav-realmedia/src/codecs/ralf.rs | 2 +- nihav-realmedia/src/codecs/rv3040.rs | 10 +-- nihav-realmedia/src/codecs/rv30dsp.rs | 6 +- nihav-realmedia/src/codecs/rv40dsp.rs | 6 +- nihav-realmedia/src/codecs/rv60.rs | 30 +++---- nihav-realmedia/src/codecs/rv60dsp.rs | 14 +-- 38 files changed, 248 insertions(+), 117 deletions(-) create mode 100644 nihav-core/src/refs.rs diff --git a/nihav-commonfmt/src/codecs/aac.rs b/nihav-commonfmt/src/codecs/aac.rs index 02223c8..8232e07 100644 --- a/nihav-commonfmt/src/codecs/aac.rs +++ b/nihav-commonfmt/src/codecs/aac.rs @@ -966,7 +966,7 @@ impl ChannelPair { } fn synth_audio(&mut self, dsp: &mut DSP, abuf: &mut NABufferType, srate_idx: usize) { let mut adata = abuf.get_abuf_f32().unwrap(); - let mut output = adata.get_data_mut(); + let output = adata.get_data_mut().unwrap(); let off0 = abuf.get_offset(self.channel); let off1 = abuf.get_offset(self.channel + 1); self.ics[0].synth_channel(dsp, &mut output[off0..], srate_idx); diff --git a/nihav-commonfmt/src/codecs/atrac3.rs b/nihav-commonfmt/src/codecs/atrac3.rs index 66751b3..7b543bf 100644 --- a/nihav-commonfmt/src/codecs/atrac3.rs +++ b/nihav-commonfmt/src/codecs/atrac3.rs @@ -681,7 +681,7 @@ impl NADecoder for Atrac3Decoder { let abuf = alloc_audio_buffer(ainfo, ATRAC3_FRAME_SIZE, self.chmap.clone())?; let mut adata = abuf.get_abuf_f32().unwrap(); - let mut output = adata.get_data_mut(); + let output = adata.get_data_mut().unwrap(); for ch in 0..self.channels { let dpos = abuf.get_offset(ch); diff --git a/nihav-commonfmt/src/codecs/clearvideo.rs b/nihav-commonfmt/src/codecs/clearvideo.rs index 71b82f6..3c3ae9a 100644 --- a/nihav-commonfmt/src/codecs/clearvideo.rs +++ b/nihav-commonfmt/src/codecs/clearvideo.rs @@ -270,7 +270,7 @@ fn put_blocks(buf: &mut NAVideoBuffer, xpos: usize, ypos: usize, blk: &[[i32 let mut idxu = buf.get_offset(1) + xpos * 8 + ypos * 8 * strideu; let mut idxv = buf.get_offset(2) + xpos * 8 + ypos * 8 * stridev; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); for j in 0..8 { @@ -328,7 +328,7 @@ fn copy_block(dst: &mut NAVideoBuffer, src: &NAVideoBuffer, let sbuf: &[u8] = sdta.as_slice(); let dstride = dst.get_stride(plane); let mut doff = dst.get_offset(plane) + x + y * dstride; - let mut ddta = dst.get_data_mut(); + let ddta = dst.get_data_mut().unwrap(); let dbuf: &mut [u8] = ddta.as_mut_slice(); for _ in 0..size { let dst = &mut dbuf[doff..][..size]; @@ -351,7 +351,7 @@ fn copyadd_block(dst: &mut NAVideoBuffer, src: &NAVideoBuffer, let sbuf: &[u8] = sdta.as_slice(); let dstride = dst.get_stride(plane); let mut doff = dst.get_offset(plane) + x + y * dstride; - let mut ddta = dst.get_data_mut(); + let ddta = dst.get_data_mut().unwrap(); let dbuf: &mut [u8] = ddta.as_mut_slice(); for _ in 0..size { let dst = &mut dbuf[doff..][..size]; @@ -404,7 +404,7 @@ fn extend_edges(buf: &mut NAVideoBuffer, tile_size: usize) { let size = if comp == 0 { tile_size } else { tile_size >> 1 }; let stride = buf.get_stride(comp); let planeoff = buf.get_offset(comp); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); let right = size - (w & (size - 1)); diff --git a/nihav-commonfmt/src/codecs/pcm.rs b/nihav-commonfmt/src/codecs/pcm.rs index b718b67..edc8124 100644 --- a/nihav-commonfmt/src/codecs/pcm.rs +++ b/nihav-commonfmt/src/codecs/pcm.rs @@ -49,9 +49,7 @@ impl NADecoder for PCMDecoder { if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { let duration = get_duration(&ainfo, pkt.get_duration(), pkt.get_buffer().len()); let pktbuf = pkt.get_buffer(); - let mut buf: Vec = Vec::with_capacity(pktbuf.len()); - buf.clone_from(&pktbuf); - let abuf = NAAudioBuffer::new_from_buf(ainfo, Rc::new(RefCell::new(buf)), self.chmap.clone()); + let abuf = NAAudioBuffer::new_from_buf(ainfo, pktbuf, self.chmap.clone()); let mut frm = NAFrame::new_from_pkt(pkt, info, NABufferType::AudioPacked(abuf)); frm.set_duration(Some(duration)); frm.set_keyframe(true); diff --git a/nihav-commonfmt/src/codecs/sipro.rs b/nihav-commonfmt/src/codecs/sipro.rs index b6cab62..1b8d701 100644 --- a/nihav-commonfmt/src/codecs/sipro.rs +++ b/nihav-commonfmt/src/codecs/sipro.rs @@ -682,7 +682,7 @@ impl NADecoder for SiproDecoder { let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?; let mut adata = abuf.get_abuf_f32().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let frame_len = self.mode.subframe_len * self.mode.subframes; for (input, output) in pktbuf.chunks(frm_size).zip(dst.chunks_mut(out_frm_size)) { diff --git a/nihav-commonfmt/src/codecs/ts102366.rs b/nihav-commonfmt/src/codecs/ts102366.rs index a82624d..5d1d66c 100644 --- a/nihav-commonfmt/src/codecs/ts102366.rs +++ b/nihav-commonfmt/src/codecs/ts102366.rs @@ -1201,7 +1201,7 @@ impl NADecoder for AudioDecoder { let abuf = alloc_audio_buffer(ainfo, duration, bsi.acmod.get_channel_map(bsi.lfeon))?; let mut adata = abuf.get_abuf_f32().unwrap(); - let mut output = adata.get_data_mut(); + let output = adata.get_data_mut().unwrap(); self.ablk = AudioBlock::new(); for blk in 0..NBLOCKS { diff --git a/nihav-core/src/codecs/blockdsp.rs b/nihav-core/src/codecs/blockdsp.rs index 33d682a..ccd44cb 100644 --- a/nihav-core/src/codecs/blockdsp.rs +++ b/nihav-core/src/codecs/blockdsp.rs @@ -8,7 +8,7 @@ pub fn put_blocks(buf: &mut NAVideoBuffer, xpos: usize, ypos: usize, blk: &[ let mut idxu = buf.get_offset(1) + xpos * 8 + ypos * 8 * strideu; let mut idxv = buf.get_offset(2) + xpos * 8 + ypos * 8 * stridev; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); for j in 0..8 { @@ -62,7 +62,7 @@ pub fn add_blocks(buf: &mut NAVideoBuffer, xpos: usize, ypos: usize, blk: &[ let mut idxu = buf.get_offset(1) + xpos * 8 + ypos * 8 * strideu; let mut idxv = buf.get_offset(2) + xpos * 8 + ypos * 8 * stridev; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); for j in 0..8 { @@ -149,7 +149,7 @@ pub fn copy_blocks(dst: &mut NAVideoBuffer, src: &NAVideoBuffer, for comp in 0..3 { let dstride = dst.get_stride(comp); let doff = dst.get_offset(comp); - let mut ddta = dst.get_data_mut(); + let ddta = dst.get_data_mut().unwrap(); let dbuf: &mut [u8] = ddta.as_mut_slice(); let x = if comp > 0 { dx/2 } else { dx }; let y = if comp > 0 { dy/2 } else { dy }; @@ -171,7 +171,7 @@ pub fn copy_blocks(dst: &mut NAVideoBuffer, src: &NAVideoBuffer, let sbuf: &[u8] = sdta.as_slice(); let dstride = dst.get_stride(comp); let doff = dst.get_offset(comp); - let mut ddta = dst.get_data_mut(); + let ddta = dst.get_data_mut().unwrap(); let dbuf: &mut [u8] = ddta.as_mut_slice(); let x = if comp > 0 { dx/2 } else { dx }; let y = if comp > 0 { dy/2 } else { dy }; diff --git a/nihav-core/src/codecs/h263/code.rs b/nihav-core/src/codecs/h263/code.rs index dd1279e..c18a9ea 100644 --- a/nihav-core/src/codecs/h263/code.rs +++ b/nihav-core/src/codecs/h263/code.rs @@ -347,7 +347,7 @@ impl H263BlockDSP { fn deblock_hor(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { let stride = buf.get_stride(comp); - let mut dptr = buf.get_data_mut(); + let dptr = buf.get_data_mut().unwrap(); let buf = dptr.as_mut_slice(); for x in 0..8 { let a = buf[off - 2 * stride + x] as i16; @@ -377,7 +377,7 @@ fn deblock_hor(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { fn deblock_ver(buf: &mut NAVideoBuffer, comp: usize, q: u8, off: usize) { let stride = buf.get_stride(comp); - let mut dptr = buf.get_data_mut(); + let dptr = buf.get_data_mut().unwrap(); let buf = dptr.as_mut_slice(); for y in 0..8 { let a = buf[off - 2 + y * stride] as i16; diff --git a/nihav-core/src/demuxers/mod.rs b/nihav-core/src/demuxers/mod.rs index 201e163..48529d4 100644 --- a/nihav-core/src/demuxers/mod.rs +++ b/nihav-core/src/demuxers/mod.rs @@ -39,7 +39,7 @@ impl<'a> NAPacketReader for ByteReader<'a> { } fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { let mut refbuf = pkt.get_buffer(); - let buf = Rc::make_mut(&mut refbuf); + let buf = refbuf.as_mut().unwrap(); let res = self.read_buf(buf.as_mut_slice()); if let Err(_) = res { return Err(DemuxerError::IOError); } Ok(()) @@ -226,4 +226,4 @@ impl RegisteredDemuxers { } None } -} \ No newline at end of file +} diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index 253842a..ea9f203 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -4,6 +4,7 @@ use std::fmt; pub use std::rc::Rc; pub use std::cell::*; pub use crate::formats::*; +pub use crate::refs::*; #[allow(dead_code)] #[derive(Clone,Copy,PartialEq)] @@ -102,12 +103,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 +117,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 +140,7 @@ impl NAVideoBuffer { #[derive(Clone)] pub struct NAAudioBuffer { info: NAAudioInfo, - data: NABufferRefT, + data: NABufferRef>, offs: Vec, chmap: NAChannelMap, len: usize, @@ -154,21 +153,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 } } } @@ -184,7 +183,7 @@ pub enum NABufferType { AudioI32 (NAAudioBuffer), AudioF32 (NAAudioBuffer), AudioPacked(NAAudioBuffer), - Data (NABufferRefT), + Data (NABufferRef>), None, } @@ -309,7 +308,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() { @@ -334,17 +333,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: 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 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: 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::Video32(buf)) } } else if all_bytealigned || unfit_elem_size { @@ -357,7 +356,7 @@ 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(); @@ -369,14 +368,14 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = 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 }; + 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: 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::Video32(buf)) }, _ => unreachable!(), @@ -397,7 +396,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) @@ -406,12 +405,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) @@ -423,7 +422,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)) } } @@ -431,7 +430,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)) } @@ -439,6 +438,52 @@ 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 { @@ -690,7 +735,7 @@ impl fmt::Display for NAStream { pub struct NAPacket { stream: Rc, ts: NATimeInfo, - buffer: Rc>, + buffer: NABufferRef>, keyframe: bool, // options: HashMap>, } @@ -699,7 +744,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 } @@ -707,7 +752,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 { diff --git a/nihav-core/src/lib.rs b/nihav-core/src/lib.rs index 05b7ad9..615a0cf 100644 --- a/nihav-core/src/lib.rs +++ b/nihav-core/src/lib.rs @@ -7,6 +7,7 @@ pub mod demuxers; pub mod formats; pub mod frame; pub mod io; +pub mod refs; pub mod register; pub mod detect; diff --git a/nihav-core/src/refs.rs b/nihav-core/src/refs.rs new file mode 100644 index 0000000..f2b0577 --- /dev/null +++ b/nihav-core/src/refs.rs @@ -0,0 +1,87 @@ +use std::ops::{Deref, DerefMut}; +use std::sync::atomic::*; + +struct NABufferData { + data: T, + refs: AtomicUsize, +} + +impl NABufferData { + fn new(data: T) -> Self { + Self { + data: data, + refs: AtomicUsize::new(1), + } + } + fn inc_refs(obj: &mut Self) { + obj.refs.fetch_add(1, Ordering::SeqCst); + } + fn dec_refs(obj: &mut Self) { + if obj.refs.fetch_sub(1, Ordering::SeqCst) == 0 { + std::mem::forget(obj); + } + } + fn get_num_refs(obj: &Self) -> usize { + obj.refs.load(Ordering::Relaxed) + } + fn get_read_ptr(obj: &Self) -> &T { + &obj.data + } + fn get_write_ptr(obj: &mut Self) -> Option<&mut T> { + Some(&mut obj.data) + } +} + +pub struct NABufferRef { + ptr: *mut NABufferData, +} + +impl NABufferRef { + pub fn new(val: T) -> Self { + let bdata = NABufferData::new(val); + let nbox: Box<_> = Box::new(bdata); + Self { ptr: Box::into_raw(nbox) } + } + pub fn get_num_refs(&self) -> usize { + unsafe { + NABufferData::get_num_refs(self.ptr.as_mut().unwrap()) + } + } + pub fn as_ref(&self) -> &T { + unsafe { + NABufferData::get_read_ptr(self.ptr.as_mut().unwrap()) + } + } + pub fn as_mut(&mut self) -> Option<&mut T> { + unsafe { + NABufferData::get_write_ptr(self.ptr.as_mut().unwrap()) + } + } +} + +impl Deref for NABufferRef { + type Target = T; + fn deref(&self) -> &T { self.as_ref() } +} + +impl DerefMut for NABufferRef { + fn deref_mut(&mut self) -> &mut T { self.as_mut().unwrap() } +} + +impl Clone for NABufferRef { + fn clone(&self) -> Self { + unsafe { + NABufferData::inc_refs(self.ptr.as_mut().unwrap()); + } + Self { ptr: self.ptr } + } +} + +impl Drop for NABufferRef { + fn drop(&mut self) { + unsafe { + NABufferData::dec_refs(self.ptr.as_mut().unwrap()); + } + } +} + diff --git a/nihav-duck/src/codecs/dkadpcm.rs b/nihav-duck/src/codecs/dkadpcm.rs index 68969da..0410957 100644 --- a/nihav-duck/src/codecs/dkadpcm.rs +++ b/nihav-duck/src/codecs/dkadpcm.rs @@ -81,7 +81,7 @@ impl NADecoder for DuckADPCMDecoder { let mut adata = abuf.get_abuf_i16().unwrap(); let mut off0 = adata.get_offset(0); let mut off1 = adata.get_offset(1); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); for blk in pktbuf.chunks_exact(self.block_len) { let mut mr = MemoryReader::new_read(blk); diff --git a/nihav-duck/src/codecs/truemotion1.rs b/nihav-duck/src/codecs/truemotion1.rs index eae23d4..a156705 100644 --- a/nihav-duck/src/codecs/truemotion1.rs +++ b/nihav-duck/src/codecs/truemotion1.rs @@ -625,7 +625,7 @@ impl NADecoder for TM1Decoder { if let Some(mut buf) = self.lastframe.get16() { let stride = buf.get_stride(0); { - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); if !is_sprite { self.decode_16bit(data.as_mut_slice(), stride, out_width, height, &mut mask, &mut index)?; } else { @@ -644,7 +644,7 @@ impl NADecoder for TM1Decoder { if let Some(mut buf) = self.lastframe.get24() { let stride = buf.get_stride(0); { - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); self.decode_24bit(data.as_mut_slice(), stride, out_width, height, &mut mask, &mut index)?; } bufinfo = NABufferType::VideoPacked(buf); diff --git a/nihav-duck/src/codecs/truemotion2.rs b/nihav-duck/src/codecs/truemotion2.rs index 3419d93..54f0d30 100644 --- a/nihav-duck/src/codecs/truemotion2.rs +++ b/nihav-duck/src/codecs/truemotion2.rs @@ -520,7 +520,7 @@ impl TM2Decoder { fmt.get_chromaton(1).unwrap().get_offset() as usize, fmt.get_chromaton(2).unwrap().get_offset() as usize]; let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let mut off = 0; diff --git a/nihav-duck/src/codecs/truemotion2x.rs b/nihav-duck/src/codecs/truemotion2x.rs index 4fa1b44..6eb3559 100644 --- a/nihav-duck/src/codecs/truemotion2x.rs +++ b/nihav-duck/src/codecs/truemotion2x.rs @@ -180,7 +180,7 @@ impl TM2XDecoder { fn output_frame(&mut self, buf: &mut NAVideoBuffer) { let mut offs = [ buf.get_offset(0), buf.get_offset(1), buf.get_offset(2) ]; let strides = [ buf.get_stride(0), buf.get_stride(1), buf.get_stride(2) ]; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let mut pos = self.cur_frame.stride; diff --git a/nihav-duck/src/codecs/truemotionrt.rs b/nihav-duck/src/codecs/truemotionrt.rs index a4ea0ac..df17047 100644 --- a/nihav-duck/src/codecs/truemotionrt.rs +++ b/nihav-duck/src/codecs/truemotionrt.rs @@ -72,7 +72,7 @@ impl NADecoder for TMRTDecoder { let (w, h) = buf.get_dimensions(plane); let off = buf.get_offset(plane); let stride = buf.get_stride(plane); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?; } diff --git a/nihav-game/src/codecs/bmv.rs b/nihav-game/src/codecs/bmv.rs index 0bdb624..46e5f65 100644 --- a/nihav-game/src/codecs/bmv.rs +++ b/nihav-game/src/codecs/bmv.rs @@ -118,7 +118,7 @@ impl BMVVideoDecoder { let mut buf = bufo.unwrap(); let paloff = buf.get_offset(1); let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let fwd = (line <= -640) || (line >= 0); @@ -278,7 +278,7 @@ impl NADecoder for BMVAudioDecoder { let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; let mut adata = abuf.get_abuf_i16().unwrap(); let off1 = adata.get_offset(1); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let psrc = &pktbuf[1..]; for (n, src) in psrc.chunks_exact(65).enumerate() { let code = src[0].rotate_right(1); diff --git a/nihav-game/src/codecs/bmv3.rs b/nihav-game/src/codecs/bmv3.rs index 66afc4f..099f070 100644 --- a/nihav-game/src/codecs/bmv3.rs +++ b/nihav-game/src/codecs/bmv3.rs @@ -491,7 +491,7 @@ impl NADecoder for BMV3VideoDecoder { { let mut buf = bufinfo.get_vbuf16().unwrap(); let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let refbuf = &self.frame[self.stride..]; @@ -571,7 +571,7 @@ impl NADecoder for BMV3AudioDecoder { let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; let mut adata = abuf.get_abuf_i16().unwrap(); let off1 = adata.get_offset(1); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let mut first = pktbuf[0] == 0; let psrc = &pktbuf[1..]; for (n, src) in psrc.chunks_exact(41).enumerate() { diff --git a/nihav-game/src/codecs/gremlinvideo.rs b/nihav-game/src/codecs/gremlinvideo.rs index 65d5ed1..9dd67ca 100644 --- a/nihav-game/src/codecs/gremlinvideo.rs +++ b/nihav-game/src/codecs/gremlinvideo.rs @@ -141,7 +141,7 @@ impl GremlinVideoDecoder { let mut buf = bufo.unwrap(); let paloff = buf.get_offset(1); let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let mut sidx = PREAMBLE_SIZE; let mut didx = 0; @@ -531,7 +531,7 @@ impl NADecoder for GremlinAudioDecoder { let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; let mut adata = abuf.get_abuf_i16().unwrap(); let off1 = adata.get_offset(1); - let mut buf = adata.get_data_mut(); + let buf = adata.get_data_mut().unwrap(); if self.chmap.num_channels() == 2 { for (i, e) in pktbuf.chunks(2).enumerate() { self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]); diff --git a/nihav-game/src/codecs/vmd.rs b/nihav-game/src/codecs/vmd.rs index a70ef60..bbe5218 100644 --- a/nihav-game/src/codecs/vmd.rs +++ b/nihav-game/src/codecs/vmd.rs @@ -169,7 +169,7 @@ impl VMDVideoDecoder { fn decode_frame(&mut self, br: &mut ByteReader, buf: &mut NAVideoBuffer) -> DecoderResult { let paloff = buf.get_offset(1); let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let frame_x = br.read_u16le()? as usize; @@ -398,11 +398,11 @@ impl NADecoder for VMDAudioDecoder { if self.is16bit { let mut adata = abuf.get_abuf_i16().unwrap(); let off1 = adata.get_offset(1); - let mut dst = adata.get_data_mut(); + let mut dst = adata.get_data_mut().unwrap(); self.decode_16bit(&mut dst, off1, &mut br, nblocks, mask)?; } else { let mut adata = abuf.get_abuf_u8().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let mut doff = 0; let mut mask = mask; let channels = self.chmap.num_channels(); diff --git a/nihav-indeo/src/codecs/imc.rs b/nihav-indeo/src/codecs/imc.rs index 53ea686..2d65a7c 100644 --- a/nihav-indeo/src/codecs/imc.rs +++ b/nihav-indeo/src/codecs/imc.rs @@ -898,7 +898,7 @@ impl NADecoder for IMCDecoder { let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?; let mut adata = abuf.get_abuf_f32().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let mut start: usize = 0; let channels = self.ainfo.get_channels() as usize; diff --git a/nihav-indeo/src/codecs/indeo2.rs b/nihav-indeo/src/codecs/indeo2.rs index 49b8e9b..c3a8a47 100644 --- a/nihav-indeo/src/codecs/indeo2.rs +++ b/nihav-indeo/src/codecs/indeo2.rs @@ -207,7 +207,7 @@ impl Indeo2Decoder { let stride = buf.get_stride(planeno); let cb = &self.cb; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); let table = &INDEO2_DELTA_TABLE[tableno]; @@ -270,7 +270,7 @@ impl Indeo2Decoder { let stride = buf.get_stride(planeno); let cb = &self.cb; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); let table = &INDEO2_DELTA_TABLE[tableno]; diff --git a/nihav-indeo/src/codecs/indeo3.rs b/nihav-indeo/src/codecs/indeo3.rs index 3571a30..a99db74 100644 --- a/nihav-indeo/src/codecs/indeo3.rs +++ b/nihav-indeo/src/codecs/indeo3.rs @@ -65,7 +65,7 @@ impl Buffers { let width = if planeno == 0 { self.width } else { self.width >> 2 }; let height = if planeno == 0 { self.height } else { self.height >> 2 }; let src = if self.fbuf { &self.buf1[0..] } else { &self.buf2[0..] }; - let mut dst = fbuf.get_data_mut(); + let dst = fbuf.get_data_mut().unwrap(); for _ in 0..height { for x in 0..width { dst[doff + x] = src[soff + x] * 2; diff --git a/nihav-indeo/src/codecs/ivibr.rs b/nihav-indeo/src/codecs/ivibr.rs index 7f26143..b5fa291 100644 --- a/nihav-indeo/src/codecs/ivibr.rs +++ b/nihav-indeo/src/codecs/ivibr.rs @@ -393,7 +393,7 @@ impl FrameData { let (w, h) = vb.get_dimensions(dplane); let mut didx = vb.get_offset(dplane); let dstride = vb.get_stride(dplane); - let mut dst = vb.get_data_mut(); + let dst = vb.get_data_mut().unwrap(); let src = &self.plane_buf[plane]; let mut sidx = 0; let sstride = self.plane_stride[plane]; @@ -883,7 +883,7 @@ br.skip(skip_part as u32)?; let (w, h) = vb.get_dimensions(dplane); let dstride = vb.get_stride(dplane); let off = vb.get_offset(dplane); - let mut dst = vb.get_data_mut(); + let dst = vb.get_data_mut().unwrap(); dec.recombine_plane(&frame.plane_buf[plane], frame.plane_stride[plane], &mut dst[off..], dstride, w, h); } } diff --git a/nihav-rad/src/codecs/bink2.rs b/nihav-rad/src/codecs/bink2.rs index ac81835..719608d 100644 --- a/nihav-rad/src/codecs/bink2.rs +++ b/nihav-rad/src/codecs/bink2.rs @@ -1027,7 +1027,7 @@ impl Bink2Decoder { let (mut off_y, mut off_u, mut off_v, mut off_a) = (buf.get_offset(0), buf.get_offset(1), buf.get_offset(2), buf.get_offset(3)); let (ooff_y, ooff_u, ooff_v, ooff_a) = (off_y, off_u, off_v, off_a); let (width, height) = buf.get_dimensions(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let bw = (width + 31) >> 5; let bheight = (height + 31) >> 5; diff --git a/nihav-rad/src/codecs/binkaud.rs b/nihav-rad/src/codecs/binkaud.rs index 2bf93ff..c38f6d4 100644 --- a/nihav-rad/src/codecs/binkaud.rs +++ b/nihav-rad/src/codecs/binkaud.rs @@ -242,7 +242,7 @@ impl NADecoder for BinkAudioDecoder { let mut adata = abuf.get_abuf_f32().unwrap(); let mut off0 = adata.get_offset(0); let mut off1 = adata.get_offset(1); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); let num_subframes = nsamples / self.duration / self.chmap.num_channels() / 2; diff --git a/nihav-rad/src/codecs/binkvid.rs b/nihav-rad/src/codecs/binkvid.rs index 90d1d53..dc9a469 100644 --- a/nihav-rad/src/codecs/binkvid.rs +++ b/nihav-rad/src/codecs/binkvid.rs @@ -719,7 +719,7 @@ println!(" copy from {}.{} + {},{}({})", bx*8, by*8, xoff, yoff, ybias); let stride = buf.get_stride(plane_no); let mut off = buf.get_offset(plane_no); let (width, height) = buf.get_dimensions(plane_no); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let bw = (width + 7) >> 3; let bh = (height + 7) >> 3; @@ -928,7 +928,7 @@ println!(" blk {}.{} type {}", bx,by,btype); let stride = buf.get_stride(plane_no); let mut off = buf.get_offset(plane_no); let (width, height) = buf.get_dimensions(plane_no); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let bw = (width + 7) >> 3; let bh = (height + 7) >> 3; diff --git a/nihav-rad/src/codecs/smacker.rs b/nihav-rad/src/codecs/smacker.rs index c77b14f..bbf4846 100644 --- a/nihav-rad/src/codecs/smacker.rs +++ b/nihav-rad/src/codecs/smacker.rs @@ -361,7 +361,7 @@ impl SmackerVideoDecoder { } fn output_frame(&self, buf: &mut NAVideoBuffer) { let stride = buf.get_stride(0); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let is_scaled = (self.flags & SMK_FLAG_SCALED) != 0; let is_interlaced = (self.flags & SMK_FLAG_INTERLACED) != 0; @@ -453,7 +453,7 @@ impl NADecoder for SmackerVideoDecoder { is_intra = self.decode_frame(&mut br)?; self.output_frame(&mut buf); let paloff = buf.get_offset(1); - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst = data.as_mut_slice(); let palout = &mut dst[paloff..][..PAL_SIZE]; palout.copy_from_slice(&src[0..PAL_SIZE]); @@ -543,7 +543,7 @@ impl NADecoder for SmackerAudioDecoder { abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; let mut adata = abuf.get_abuf_i16().unwrap(); let offs: [usize; 2] = [0, adata.get_offset(1)]; - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); for ch in 0..nch { dst[offs[ch]] = pred[ch]; } @@ -559,7 +559,7 @@ impl NADecoder for SmackerAudioDecoder { samples = unp_size / nch; abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; let mut adata = abuf.get_abuf_u8().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); if stereo { let mut trees: [SmackerTree8; 2] = [SmackerTree8::new(), SmackerTree8::new()]; trees[0].decode(&mut br)?; diff --git a/nihav-realmedia/src/codecs/cook.rs b/nihav-realmedia/src/codecs/cook.rs index 5f61d2e..6d7877b 100644 --- a/nihav-realmedia/src/codecs/cook.rs +++ b/nihav-realmedia/src/codecs/cook.rs @@ -433,7 +433,7 @@ impl CookChannelPair { for ch in 0..self.mode.get_channels() { let off = abuf.get_offset(ch + self.start_ch); let mut adata = abuf.get_abuf_f32().unwrap(); - let mut output = adata.get_data_mut(); + let output = adata.get_data_mut().unwrap(); let dst = &mut output[off..]; dsp.imdct.imdct(&self.block[ch], &mut dsp.out); diff --git a/nihav-realmedia/src/codecs/ra144.rs b/nihav-realmedia/src/codecs/ra144.rs index 3587382..19cf38b 100644 --- a/nihav-realmedia/src/codecs/ra144.rs +++ b/nihav-realmedia/src/codecs/ra144.rs @@ -266,7 +266,7 @@ impl NADecoder for RA144Decoder { let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?; let mut adata = abuf.get_abuf_i16().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); for (input, output) in pktbuf.chunks(FRAME_SIZE).zip(dst.chunks_mut(NBLOCKS * BLOCKSIZE)) { let mut br = BitReader::new(input, input.len(), BitReaderMode::BE); diff --git a/nihav-realmedia/src/codecs/ra288.rs b/nihav-realmedia/src/codecs/ra288.rs index a5acc29..8538f85 100644 --- a/nihav-realmedia/src/codecs/ra288.rs +++ b/nihav-realmedia/src/codecs/ra288.rs @@ -174,7 +174,7 @@ impl NADecoder for RA288Decoder { let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?; let mut adata = abuf.get_abuf_f32().unwrap(); - let mut dst = adata.get_data_mut(); + let dst = adata.get_data_mut().unwrap(); for (input, output) in pktbuf.chunks(FRAME_SIZE).zip(dst.chunks_mut(NBLOCKS * BLOCKSIZE)) { let mut br = BitReader::new(input, input.len(), BitReaderMode::LE); diff --git a/nihav-realmedia/src/codecs/ralf.rs b/nihav-realmedia/src/codecs/ralf.rs index e114dd0..76bf3df 100644 --- a/nihav-realmedia/src/codecs/ralf.rs +++ b/nihav-realmedia/src/codecs/ralf.rs @@ -303,7 +303,7 @@ impl RALFDecoder { let ch0 = abuf.get_offset(0) + self.sample_offset; let ch1 = abuf.get_offset(1) + self.sample_offset; let mut adata = abuf.get_abuf_i16().unwrap(); - let mut output = adata.get_data_mut(); + let output = adata.get_data_mut().unwrap(); match dmode { 0 => { for i in 0..length { diff --git a/nihav-realmedia/src/codecs/rv3040.rs b/nihav-realmedia/src/codecs/rv3040.rs index d90e59d..d517cac 100644 --- a/nihav-realmedia/src/codecs/rv3040.rs +++ b/nihav-realmedia/src/codecs/rv3040.rs @@ -735,7 +735,7 @@ fn do_avg(cdsp: &RV34CommonDSP, buf: &mut NAVideoBuffer, avg_buf: &NAVideoBu let csize = if comp == 0 { size } else { size >> 1 }; let dstride = buf.get_stride(comp); let doffset = buf.get_offset(comp) + xoff + yoff * dstride; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let sstride = avg_buf.get_stride(comp); @@ -839,7 +839,7 @@ impl RV34Decoder { } let stride = buf.get_stride(0); let mut offset = buf.get_offset(0) + sstate.mb_x * 16 + sstate.mb_y * 16 * stride; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); if is_16 { @@ -890,7 +890,7 @@ impl RV34Decoder { for comp in 1..3 { let stride = buf.get_stride(comp); let mut offset = buf.get_offset(comp) + sstate.mb_x * 8 + sstate.mb_y * 8 * stride; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); if is_16 { let im8 = imode.get_pred8_type(sstate.has_top, sstate.has_left); @@ -1015,7 +1015,7 @@ impl RV34Decoder { } let stride = buf.get_stride(0); let mut offset = buf.get_offset(0) + sstate.mb_x * 16 + sstate.mb_y * 16 * stride; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); for y in 0..4 { @@ -1050,7 +1050,7 @@ impl RV34Decoder { for comp in 1..3 { let stride = buf.get_stride(comp); let mut offset = buf.get_offset(comp) + sstate.mb_x * 8 + sstate.mb_y * 8 * stride; - let mut data = buf.get_data_mut(); + let data = buf.get_data_mut().unwrap(); let framebuf: &mut [u8] = data.as_mut_slice(); for _ in 0..2 { for x in 0..2 { diff --git a/nihav-realmedia/src/codecs/rv30dsp.rs b/nihav-realmedia/src/codecs/rv30dsp.rs index dc7ed09..9271916 100644 --- a/nihav-realmedia/src/codecs/rv30dsp.rs +++ b/nihav-realmedia/src/codecs/rv30dsp.rs @@ -248,7 +248,7 @@ impl RV34DSP for RV30DSP { offs[comp] = frame.get_offset(comp) + start * stride[comp]; } - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); // vertical filter @@ -381,7 +381,7 @@ impl RV34DSP for RV30DSP { let size: usize = if use16 { 16 } else { 8 }; let dstride = frame.get_stride(0); let doffset = frame.get_offset(0) + (if !avg { x + y * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let (w_, h_) = prev_frame.get_dimensions(0); @@ -409,7 +409,7 @@ impl RV34DSP for RV30DSP { let size: usize = if use8 { 8 } else { 4 }; let dstride = frame.get_stride(comp); let doffset = frame.get_offset(comp) + (if !avg { x + y * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let (w_, h_) = prev_frame.get_dimensions(comp); diff --git a/nihav-realmedia/src/codecs/rv40dsp.rs b/nihav-realmedia/src/codecs/rv40dsp.rs index 030321c..c2841df 100644 --- a/nihav-realmedia/src/codecs/rv40dsp.rs +++ b/nihav-realmedia/src/codecs/rv40dsp.rs @@ -577,7 +577,7 @@ impl RV34DSP for RV40DSP { offs[comp] = frame.get_offset(comp) + start * stride[comp]; } - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let mut mb_pos: usize = row * mb_w; @@ -810,7 +810,7 @@ impl RV34DSP for RV40DSP { let size: usize = if use16 { 16 } else { 8 }; let dstride = frame.get_stride(0); let doffset = frame.get_offset(0) + (if !avg { x + y * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let (w_, h_) = prev_frame.get_dimensions(0); @@ -840,7 +840,7 @@ impl RV34DSP for RV40DSP { let size: usize = if use8 { 8 } else { 4 }; let dstride = frame.get_stride(comp); let doffset = frame.get_offset(comp) + (if !avg { x + y * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let (w_, h_) = prev_frame.get_dimensions(comp); diff --git a/nihav-realmedia/src/codecs/rv60.rs b/nihav-realmedia/src/codecs/rv60.rs index cf09845..007450a 100644 --- a/nihav-realmedia/src/codecs/rv60.rs +++ b/nihav-realmedia/src/codecs/rv60.rs @@ -740,7 +740,7 @@ println!(" left {} bits", br.left()); if !split_i4x4 { let dstride = buf.get_stride(0); let off = xpos + ypos * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let dst = &mut data; self.populate_ipred(hdr, dst, 0, dstride, 0, 0, size, true); self.ipred.pred_angle(dst, off, dstride, size, itype as usize, true); @@ -749,7 +749,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(comp); let soff = buf.get_offset(comp); let off = soff + (xpos >> 1) + (ypos >> 1) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.populate_ipred(hdr, dst, soff, dstride, 0, 0, size >> 1, false); self.ipred.pred_angle(&mut dst, off, dstride, size >> 1, itype as usize, false); @@ -848,7 +848,7 @@ println!(" left {} bits", br.left()); self.dsp.transform4x4(&mut self.y_coeffs[i * 16..][..16]); let dstride = buf.get_stride(0); let off = xpos + x * 4 + (ypos + y * 4) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs[i*16..][..16], 4); } @@ -863,7 +863,7 @@ println!(" left {} bits", br.left()); self.dsp.transform4x4(&mut self.u_coeffs[i * 16..][..16]); let dstride = buf.get_stride(1); let off = buf.get_offset(1) + xoff + yoff * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs[i * 16..][..16], 4); } @@ -871,7 +871,7 @@ println!(" left {} bits", br.left()); self.dsp.transform4x4(&mut self.v_coeffs[i * 16..][..16]); let dstride = buf.get_stride(2); let off = buf.get_offset(2) + xoff + yoff * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs[i * 16..][..16], 4); } @@ -890,7 +890,7 @@ println!(" left {} bits", br.left()); if split_i4x4 { let dstride = buf.get_stride(0); let off = xpos + xoff + (ypos + yoff) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.populate_ipred(hdr, dst, 0, dstride, xoff, yoff, 4, true); let itype = self.blk_info[self.blk_pos + (i & 1) + (i >> 1) * self.blk_stride].imode; @@ -902,7 +902,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(0); let soff = buf.get_offset(0); let off = soff + xpos + xoff + (ypos + yoff) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, blk, 4); } @@ -912,7 +912,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(1); let soff = buf.get_offset(1); let off = soff + (xpos >> 1) + (ypos >> 1) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4); } @@ -921,7 +921,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(2); let soff = buf.get_offset(2); let off = soff + (xpos >> 1) + (ypos >> 1) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4); } @@ -937,7 +937,7 @@ println!(" left {} bits", br.left()); self.dsp.transform8x8(&mut self.y_coeffs); let dstride = buf.get_stride(0); let off = xpos + ypos * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 8); } @@ -946,7 +946,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(1); let soff = buf.get_offset(1); let off = soff + (xpos >> 1) + (ypos >> 1) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 4); } @@ -955,7 +955,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(2); let soff = buf.get_offset(2); let off = soff + (xpos >> 1) + (ypos >> 1) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 4); } @@ -982,7 +982,7 @@ println!(" left {} bits", br.left()); self.dsp.transform16x16(&mut self.y_coeffs); let dstride = buf.get_stride(0); let off = xpos + x * 16 + (ypos + y * 16) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.y_coeffs, 16); } @@ -991,7 +991,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(1); let soff = buf.get_offset(1); let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.u_coeffs, 8); } @@ -1000,7 +1000,7 @@ println!(" left {} bits", br.left()); let dstride = buf.get_stride(2); let soff = buf.get_offset(2); let off = soff + (xpos >> 1) + x * 8 + ((ypos >> 1) + y * 8) * dstride; - let mut data = buf.get_data_mut(); + let mut data = buf.get_data_mut().unwrap(); let mut dst = &mut data; self.dsp.add_block(&mut dst, off, dstride, &self.v_coeffs, 8); } diff --git a/nihav-realmedia/src/codecs/rv60dsp.rs b/nihav-realmedia/src/codecs/rv60dsp.rs index 14e3fdb..0fa8beb 100644 --- a/nihav-realmedia/src/codecs/rv60dsp.rs +++ b/nihav-realmedia/src/codecs/rv60dsp.rs @@ -456,7 +456,7 @@ impl RV60DSP { let sstride = prev_frame.get_stride(comp); let doff = if comp == 0 { x + y * dstride } else { frame.get_offset(comp) + (x >> 1) + (y >> 1) * dstride }; let soff = prev_frame.get_offset(comp); - let mut ddata = frame.get_data_mut(); + let ddata = frame.get_data_mut().unwrap(); let dst: &mut [u8] = ddata.as_mut_slice(); let sdata = prev_frame.get_data(); let src: &[u8] = sdata.as_slice(); @@ -472,7 +472,7 @@ impl RV60DSP { { // luma let dstride = frame.get_stride(0); let doffset = frame.get_offset(0) + (if !avg { x + y * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); let (w_, h_) = prev_frame.get_dimensions(0); @@ -512,7 +512,7 @@ impl RV60DSP { for comp in 1..3 { // chroma let dstride = frame.get_stride(comp); let doffset = frame.get_offset(comp) + (if !avg { (x >> 1) + (y >> 1) * dstride } else { 0 }); - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); if check_pos(x >> 1, y >> 1, cw, ch, fw, fh, dx, dy, 0, 1, 0, 1) { let sstride = prev_frame.get_stride(comp); @@ -543,7 +543,7 @@ impl RV60DSP { { let stride = frame.get_stride(0); let offset = frame.get_offset(0) + xpos + ypos * stride; - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); filter_luma_edge(dst, offset, 1, stride, mode_l, mode_r, lim1, lim2); } @@ -551,7 +551,7 @@ impl RV60DSP { for comp in 1..2 { let stride = frame.get_stride(comp); let offset = frame.get_offset(comp) + (xpos >> 1) + (ypos >> 1) * stride; - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); filter_chroma_edge(dst, offset, 1, stride, mode_l, mode_r, lim1, lim2); } @@ -572,7 +572,7 @@ impl RV60DSP { { let stride = frame.get_stride(0); let offset = frame.get_offset(0) + xpos + ypos * stride; - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); filter_luma_edge(dst, offset, stride, 1, mode_t, mode_d, lim1, lim2); } @@ -580,7 +580,7 @@ impl RV60DSP { for comp in 1..2 { let stride = frame.get_stride(comp); let offset = frame.get_offset(comp) + (xpos >> 1) + (ypos >> 1) * stride; - let mut data = frame.get_data_mut(); + let data = frame.get_data_mut().unwrap(); let dst: &mut [u8] = data.as_mut_slice(); filter_chroma_edge(dst, offset, stride, 1, mode_t, mode_d, lim1, lim2); } -- 2.39.5