From: Kostya Shishkov Date: Mon, 18 Aug 2025 16:38:38 +0000 (+0200) Subject: nihav_ms: switch to ByteIO X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=7dfb60de825b83500321527bacb1a380194b43e2;p=nihav.git nihav_ms: switch to ByteIO --- diff --git a/nihav-ms/src/codecs/imaadpcm.rs b/nihav-ms/src/codecs/imaadpcm.rs index e0606e9..bd9f42f 100644 --- a/nihav-ms/src/codecs/imaadpcm.rs +++ b/nihav-ms/src/codecs/imaadpcm.rs @@ -49,8 +49,7 @@ impl NADecoder for IMAADPCMDecoder { let mut off = [adata.get_offset(0), adata.get_offset(1)]; let dst = adata.get_data_mut().unwrap(); - let mut mr = MemoryReader::new_read(pktbuf.as_slice()); - let mut br = ByteReader::new(&mut mr); + let mut br = MemoryReader::new_read(pktbuf.as_slice()); for ch in 0..channels { let pred = br.read_u16le()? as i16; let step = br.read_byte()?; diff --git a/nihav-ms/src/codecs/imaadpcmenc.rs b/nihav-ms/src/codecs/imaadpcmenc.rs index eee51b8..581ea04 100644 --- a/nihav-ms/src/codecs/imaadpcmenc.rs +++ b/nihav-ms/src/codecs/imaadpcmenc.rs @@ -3,13 +3,13 @@ use nihav_core::io::byteio::*; use nihav_codec_support::codecs::imaadpcm::*; struct NibbleWriter<'a> { - bw: ByteWriter<'a>, + bw: &'a mut dyn ByteIO, val: u8, first: bool, } impl<'a> NibbleWriter<'a> { - fn new(bw: ByteWriter<'a>) -> Self { + fn new(bw: &'a mut dyn ByteIO) -> Self { Self { bw, val: 0, first: true } } fn write(&mut self, nib: u8) -> EncoderResult<()> { @@ -74,8 +74,7 @@ impl IMAADPCMEncoder { } let mut dbuf = vec![0u8; self.block_size]; - let mut mw = MemoryWriter::new_write(dbuf.as_mut_slice()); - let mut bw = ByteWriter::new(&mut mw); + let mut bw = MemoryWriter::new_write(dbuf.as_mut_slice()); for ch in 0..self.channels { self.first[ch].predictor = i32::from(self.samples[ch]); @@ -88,7 +87,7 @@ impl IMAADPCMEncoder { bw.write_byte(self.first[ch].step as u8)?; bw.write_byte(0)?; } - let mut nw = NibbleWriter::new(bw); + let mut nw = NibbleWriter::new(&mut bw); for samples in self.samples.chunks(self.channels).take(self.block_len).skip(1) { for (state, &samp) in self.first.iter_mut().zip(samples.iter()) { let nib = state.compress_sample(samp); @@ -157,7 +156,7 @@ impl IMAADPCMEncoder { bw.write_byte(self.nibs[ch][0])?; bw.write_byte(0)?; } - let mut nw = NibbleWriter::new(bw); + let mut nw = NibbleWriter::new(&mut bw); for i in 1..self.block_len { for ch in 0..self.channels { nw.write(self.nibs[ch][i])?; diff --git a/nihav-ms/src/codecs/msadpcm.rs b/nihav-ms/src/codecs/msadpcm.rs index c1e32f6..7cb6a78 100644 --- a/nihav-ms/src/codecs/msadpcm.rs +++ b/nihav-ms/src/codecs/msadpcm.rs @@ -75,8 +75,7 @@ impl NADecoder for MSADPCMDecoder { if let Some(ref buf) = info.get_extradata() { validate!(buf.len() >= 6); validate!((buf.len() & 3) == 0); - let mut mr = MemoryReader::new_read(buf.as_slice()); - let mut br = ByteReader::new(&mut mr); + let mut br = MemoryReader::new_read(buf.as_slice()); let _smth = br.read_u16le()?; let ncoeffs = br.read_u16le()? as usize; validate!(buf.len() == ncoeffs * 4 + 4); @@ -111,8 +110,7 @@ impl NADecoder for MSADPCMDecoder { let mut pred = [Predictor::default(), Predictor::default()]; for blk in pktbuf.chunks(self.block_len) { - let mut mr = MemoryReader::new_read(blk); - let mut br = ByteReader::new(&mut mr); + let mut br = MemoryReader::new_read(blk); for p in pred[..channels].iter_mut() { let coef_idx = br.read_byte()? as usize; validate!(coef_idx < self.adapt_coeffs.len()); @@ -208,8 +206,7 @@ impl MSADPCMEncoder { } let mut dbuf = vec![0u8; Self::calc_block_size(len, self.channels)]; - let mut mw = MemoryWriter::new_write(dbuf.as_mut_slice()); - let mut bw = ByteWriter::new(&mut mw); + let mut bw = MemoryWriter::new_write(dbuf.as_mut_slice()); let mut best_idx = [0usize; 2]; for (ch, best_idx) in best_idx[..self.channels].iter_mut().enumerate() { diff --git a/nihav-ms/src/codecs/msrle.rs b/nihav-ms/src/codecs/msrle.rs index 5953a37..9d2712c 100644 --- a/nihav-ms/src/codecs/msrle.rs +++ b/nihav-ms/src/codecs/msrle.rs @@ -15,7 +15,7 @@ impl RleDecoder { fn new() -> Self { Self::default() } - fn decode_8bit(&self, br: &mut ByteReader, frm: &mut NASimpleVideoFrame) -> DecoderResult { + fn decode_8bit(&self, br: &mut dyn ByteIO, frm: &mut NASimpleVideoFrame) -> DecoderResult { let mut has_skips = false; if (br.left() as usize) == self.width * self.height { @@ -86,7 +86,7 @@ impl RleDecoder { Ok(if has_skips { FrameType::P } else { FrameType::I }) } - fn decode_4bit(&self, br: &mut ByteReader, frm: &mut NASimpleVideoFrame) -> DecoderResult { + fn decode_4bit(&self, br: &mut dyn ByteIO, frm: &mut NASimpleVideoFrame) -> DecoderResult { let mut has_skips = false; if (br.left() as usize) == (self.width + 1) / 2 * self.height { @@ -197,8 +197,7 @@ impl NADecoder for RleDecoder { fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { let src = pkt.get_buffer(); validate!(src.len() >= 2); - let mut mr = MemoryReader::new_read(src.as_slice()); - let mut br = ByteReader::new(&mut mr); + let mut br = MemoryReader::new_read(src.as_slice()); let bufret = self.hams.clone_ref(); let mut buf; diff --git a/nihav-ms/src/codecs/msvideo1.rs b/nihav-ms/src/codecs/msvideo1.rs index 424eb3b..2795d0a 100644 --- a/nihav-ms/src/codecs/msvideo1.rs +++ b/nihav-ms/src/codecs/msvideo1.rs @@ -25,7 +25,7 @@ impl Video1Decoder { fn new() -> Self { Self::default() } - fn decode_frame(&self, br: &mut ByteReader, frm: &mut NASimpleVideoFrame) -> DecoderResult { + fn decode_frame(&self, br: &mut dyn ByteIO, frm: &mut NASimpleVideoFrame) -> DecoderResult { let off = frm.offset[0]; let stride = frm.stride[0]; let mut skip_count = 0; @@ -89,7 +89,7 @@ impl Video1Decoder { Ok(FrameType::Skip) } } - fn decode_frame16(&self, br: &mut ByteReader, frm: &mut NASimpleVideoFrame) -> DecoderResult { + fn decode_frame16(&self, br: &mut dyn ByteIO, frm: &mut NASimpleVideoFrame) -> DecoderResult { let off = frm.offset[0]; let stride = frm.stride[0]; let mut skip_count = 0; @@ -178,8 +178,7 @@ impl NADecoder for Video1Decoder { fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { let src = pkt.get_buffer(); validate!(src.len() >= 2); - let mut mr = MemoryReader::new_read(src.as_slice()); - let mut br = ByteReader::new(&mut mr); + let mut br = MemoryReader::new_read(src.as_slice()); let buftype; let ftype; diff --git a/nihav-ms/src/codecs/msvideo1enc.rs b/nihav-ms/src/codecs/msvideo1enc.rs index b3cde3a..94c1cee 100644 --- a/nihav-ms/src/codecs/msvideo1enc.rs +++ b/nihav-ms/src/codecs/msvideo1enc.rs @@ -402,18 +402,18 @@ impl<'a> BlockPainterPal<'a> { struct BlockWriterPal {} impl BlockWriterPal { - fn write_fill(bw: &mut ByteWriter, fill_val: u8) -> EncoderResult<()> { + fn write_fill(bw: &mut dyn ByteIO, fill_val: u8) -> EncoderResult<()> { bw.write_byte(fill_val)?; bw.write_byte(0x80)?; Ok(()) } - fn write_clr2(bw: &mut ByteWriter, clr2_flags: u16, clr2: [u8; 2]) -> EncoderResult<()> { + fn write_clr2(bw: &mut dyn ByteIO, clr2_flags: u16, clr2: [u8; 2]) -> EncoderResult<()> { bw.write_u16le(clr2_flags)?; bw.write_byte(clr2[0])?; bw.write_byte(clr2[1])?; Ok(()) } - fn write_clr8(bw: &mut ByteWriter, mut clr8_flags: u16, mut clr8: [[u8; 4]; 4]) -> EncoderResult<()> { + fn write_clr8(bw: &mut dyn ByteIO, mut clr8_flags: u16, mut clr8: [[u8; 4]; 4]) -> EncoderResult<()> { if (clr8_flags & 0x8000) == 0 { clr8_flags ^= 0xFF00; clr8[2].swap(0, 1); @@ -482,17 +482,17 @@ impl BlockPainter15 { struct BlockWriter15 {} impl BlockWriter15 { - fn write_fill(bw: &mut ByteWriter, fill_val: u16) -> EncoderResult<()> { + fn write_fill(bw: &mut dyn ByteIO, fill_val: u16) -> EncoderResult<()> { bw.write_u16le(fill_val | 0x8000)?; Ok(()) } - fn write_clr2(bw: &mut ByteWriter, clr2_flags: u16, clr2: [u16; 2]) -> EncoderResult<()> { + fn write_clr2(bw: &mut dyn ByteIO, clr2_flags: u16, clr2: [u16; 2]) -> EncoderResult<()> { bw.write_u16le(clr2_flags)?; bw.write_u16le(clr2[0])?; bw.write_u16le(clr2[1])?; Ok(()) } - fn write_clr8(bw: &mut ByteWriter, mut clr8_flags: u16, mut clr8: [[u16; 4]; 4]) -> EncoderResult<()> { + fn write_clr8(bw: &mut dyn ByteIO, mut clr8_flags: u16, mut clr8: [[u16; 4]; 4]) -> EncoderResult<()> { if (clr8_flags & 0x8000) != 0 { clr8_flags ^= 0xFF00; clr8[2].swap(0, 1); @@ -559,11 +559,11 @@ impl MSVideo1Encoder { } } } - fn write_skips(bw: &mut ByteWriter, skips: usize) -> EncoderResult<()> { + fn write_skips(bw: &mut dyn ByteIO, skips: usize) -> EncoderResult<()> { bw.write_u16le((skips as u16) | 0x8400)?; Ok(()) } - fn encode_inter_rgb555(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, prev_frm: &NAVideoBuffer, quality: u8) -> EncoderResult { + fn encode_inter_rgb555(bw: &mut dyn ByteIO, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, prev_frm: &NAVideoBuffer, quality: u8) -> EncoderResult { let (skip_threshold, fill_threshold) = map_quality_15bit(quality); let mut is_intra = true; let src = in_frm.get_data(); @@ -649,7 +649,7 @@ impl MSVideo1Encoder { } //xxx: something for inter? Ok(is_intra) } - fn encode_intra_rgb555(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, quality: u8) -> EncoderResult { + fn encode_intra_rgb555(bw: &mut dyn ByteIO, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, quality: u8) -> EncoderResult { let (_, fill_threshold) = map_quality_15bit(quality); let src = in_frm.get_data(); let sstride = in_frm.get_stride(0); @@ -683,7 +683,7 @@ impl MSVideo1Encoder { bw.write_u16le(0)?; Ok(true) } - fn encode_inter_pal(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, prev_frm: &NAVideoBuffer, quality: u8, pal: &[UnpackedPixel; 256], ls: &LocalSearch) -> EncoderResult { + fn encode_inter_pal(bw: &mut dyn ByteIO, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, prev_frm: &NAVideoBuffer, quality: u8, pal: &[UnpackedPixel; 256], ls: &LocalSearch) -> EncoderResult { let (skip_threshold, fill_threshold) = map_quality_pal(quality); let mut is_intra = true; let src = in_frm.get_data(); @@ -769,7 +769,7 @@ impl MSVideo1Encoder { } //xxx: something for inter? Ok(is_intra) } - fn encode_intra_pal(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, quality: u8, pal: &[UnpackedPixel; 256], ls: &LocalSearch) -> EncoderResult { + fn encode_intra_pal(bw: &mut dyn ByteIO, cur_frm: &mut NAVideoBuffer, in_frm: &NAVideoBuffer, quality: u8, pal: &[UnpackedPixel; 256], ls: &LocalSearch) -> EncoderResult { let (_, fill_threshold) = map_quality_pal(quality); let src = in_frm.get_data(); let sstride = in_frm.get_stride(0); @@ -875,8 +875,7 @@ impl NAEncoder for MSVideo1Encoder { if frm.frame_type == FrameType::Skip { if let Some(ref stream) = self.stream { let mut dbuf = Vec::with_capacity(4); - let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); - let mut bw = ByteWriter::new(&mut gw); + let mut bw = GrowableMemoryWriter::new_write(&mut dbuf); let vinfo = stream.get_info().get_properties().get_video_info().unwrap(); let mut nskips = ((vinfo.get_width() + 3) / 4) * ((vinfo.get_height() + 3) / 4); @@ -903,8 +902,7 @@ impl NAEncoder for MSVideo1Encoder { } let mut cur_frm = self.pool15.get_free().unwrap(); let mut dbuf = Vec::with_capacity(4); - let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); - let mut bw = ByteWriter::new(&mut gw); + let mut bw = GrowableMemoryWriter::new_write(&mut dbuf); if self.frmcount == 0 { self.lastfrm15 = None; } @@ -943,8 +941,7 @@ impl NAEncoder for MSVideo1Encoder { let mut cur_frm = self.pool8.get_free().unwrap(); let mut dbuf = Vec::with_capacity(4); - let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); - let mut bw = ByteWriter::new(&mut gw); + let mut bw = GrowableMemoryWriter::new_write(&mut dbuf); if self.frmcount == 0 { self.lastfrm8 = None; }