From 78da9b7f43125b1580e4422bbdc1bb7bfe9fe7ce Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 9 Nov 2024 14:34:40 +0100 Subject: [PATCH] core/codebook: switch {Full,Short}CodebookDescReader to use slice reference This finally allows using them with static code definitions instead of always constructing a vector. --- nihav-core/src/compr/deflate.rs | 12 ++++---- nihav-core/src/io/codebook.rs | 42 ++++++++++++++-------------- nihav-duck/src/codecs/truemotion2.rs | 2 +- nihav-game/src/codecs/midivid3.rs | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/nihav-core/src/compr/deflate.rs b/nihav-core/src/compr/deflate.rs index 052043d..bb47c5b 100644 --- a/nihav-core/src/compr/deflate.rs +++ b/nihav-core/src/compr/deflate.rs @@ -600,7 +600,7 @@ impl Inflate { } let mut len_codes = [ShortCodebookDesc { code: 0, bits: 0 }; 19]; lengths_to_codes(&self.len_lengths, &mut len_codes)?; - let mut cr = ShortCodebookDescReader::new(len_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&len_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { self.state = InflateState::End; @@ -642,14 +642,14 @@ impl Inflate { let mut lit_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_LITERALS]; lengths_to_codes(lit_lengths, &mut lit_codes)?; - let mut cr = ShortCodebookDescReader::new(lit_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&lit_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { return Err(DecompressError::InvalidHeader); } self.dyn_lit_cb = Some(ret.unwrap()); let mut dist_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_DISTS]; lengths_to_codes(&dist_lengths[..self.hdist], &mut dist_codes)?; - let mut cr = ShortCodebookDescReader::new(dist_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&dist_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { return Err(DecompressError::InvalidHeader); } self.dyn_dist_cb = Some(ret.unwrap()); @@ -895,7 +895,7 @@ impl Inflate { } let mut len_codes = [ShortCodebookDesc { code: 0, bits: 0 }; 19]; lengths_to_codes(&len_lengths, &mut len_codes)?; - let mut cr = ShortCodebookDescReader::new(len_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&len_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { return Err(DecompressError::InvalidHeader); @@ -937,14 +937,14 @@ impl Inflate { let mut lit_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_LITERALS]; lengths_to_codes(lit_lengths, &mut lit_codes)?; - let mut cr = ShortCodebookDescReader::new(lit_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&lit_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { return Err(DecompressError::InvalidHeader); } let dyn_lit_cb = ret.unwrap(); let mut dist_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_DISTS]; lengths_to_codes(&dist_lengths[..hdist], &mut dist_codes)?; - let mut cr = ShortCodebookDescReader::new(dist_codes.to_vec()); + let mut cr = ShortCodebookDescReader::new(&dist_codes); let ret = Codebook::new(&mut cr, CodebookMode::LSB); if ret.is_err() { return Err(DecompressError::InvalidHeader); } let dyn_dist_cb = ret.unwrap(); diff --git a/nihav-core/src/io/codebook.rs b/nihav-core/src/io/codebook.rs index bd7bb1e..625009f 100644 --- a/nihav-core/src/io/codebook.rs +++ b/nihav-core/src/io/codebook.rs @@ -10,11 +10,11 @@ //! ``` //! use nihav_core::io::codebook::{ShortCodebookDesc, ShortCodebookDescReader, Codebook, CodebookMode}; //! -//! let cb_desc: Vec = vec!( +//! let cb_desc: &[ShortCodebookDesc] = &[ //! ShortCodebookDesc { code: 0b00, bits: 2 }, //! ShortCodebookDesc { code: 0, bits: 0 }, //! ShortCodebookDesc { code: 0b01, bits: 2 }, -//! ShortCodebookDesc { code: 0b1, bits: 1 }); +//! ShortCodebookDesc { code: 0b1, bits: 1 }]; //! let mut cr = ShortCodebookDescReader::new(cb_desc); //! let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap(); //! ``` @@ -39,7 +39,7 @@ //! # use nihav_core::io::codebook::{ShortCodebookDesc, ShortCodebookDescReader, CodebookDescReader, CodebookResult}; //! //! # fn foo(br: &mut BitReader) -> CodebookResult<()> { -//! # let mut cr = ShortCodebookDescReader::new(vec![ShortCodebookDesc { code: 0b00, bits: 2 }]); +//! # let mut cr = ShortCodebookDescReader::new(&[ShortCodebookDesc { code: 0b00, bits: 2 }]); //! let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap(); //! let value = br.read_cb(&cb)?; //! # Ok(()) @@ -381,38 +381,38 @@ impl<'a, S: Copy> CodebookReader for BitReader<'a> { } } -/// Codebook description that stores a list of codewords and their values. -pub struct FullCodebookDescReader { - data: Vec>, +/// Codebook description that stores a reference to the list of codewords and their values. +pub struct FullCodebookDescReader<'a, S> { + data: &'a [FullCodebookDesc], } -impl FullCodebookDescReader { +impl<'a, S> FullCodebookDescReader<'a, S> { /// Constructs a new `FullCodebookDescReader` instance. - pub fn new(data: Vec>) -> Self { + pub fn new(data: &'a [FullCodebookDesc]) -> Self { FullCodebookDescReader { data } } } -impl CodebookDescReader for FullCodebookDescReader { +impl<'a, S: Copy> CodebookDescReader for FullCodebookDescReader<'a, S> { fn bits(&mut self, idx: usize) -> u8 { self.data[idx].bits } fn code(&mut self, idx: usize) -> u32 { self.data[idx].code } fn sym (&mut self, idx: usize) -> S { self.data[idx].sym } fn len(&mut self) -> usize { self.data.len() } } -/// Codebook description that stores a list of codewords and their value is equal to the index. -pub struct ShortCodebookDescReader { - data: Vec, +/// Codebook description that stores a reference to the list of codewords (their value is equal to the index). +pub struct ShortCodebookDescReader<'a> { + data: &'a [ShortCodebookDesc], } -impl ShortCodebookDescReader { +impl<'a> ShortCodebookDescReader<'a> { /// Constructs a new `ShortCodebookDescReader` instance. - pub fn new(data: Vec>) -> Self { + pub fn new(data: &'a [ShortCodebookDesc]) -> Self { ShortCodebookDescReader { data } } } -impl CodebookDescReader for ShortCodebookDescReader { +impl<'a> CodebookDescReader for ShortCodebookDescReader<'a> { fn bits(&mut self, idx: usize) -> u8 { self.data[idx].bits } fn code(&mut self, idx: usize) -> u32 { self.data[idx].code } fn sym (&mut self, idx: usize) -> u32 { idx as u32 } @@ -448,12 +448,12 @@ mod test { #[test] fn test_cb() { const BITS: [u8; 2] = [0b01011011, 0b10111100]; - let cb_desc: Vec> = vec!( + let cb_desc = &[ FullCodebookDesc { code: 0b0, bits: 1, sym: 16 }, FullCodebookDesc { code: 0b10, bits: 2, sym: -3 }, FullCodebookDesc { code: 0b110, bits: 3, sym: 42 }, FullCodebookDesc { code: 0b1110, bits: 4, sym: -42 } - ); + ]; let buf = &BITS; let mut br = BitReader::new(buf, BitReaderMode::BE); let mut cfr = FullCodebookDescReader::new(cb_desc); @@ -469,7 +469,7 @@ mod test { assert_eq!(0, 1); } - let scb_desc: Vec = vec!( + let scb_desc = &[ ShortCodebookDesc { code: 0b0, bits: 1 }, ShortCodebookDesc { code: 0, bits: 0 }, ShortCodebookDesc { code: 0b10, bits: 2 }, @@ -483,7 +483,7 @@ mod test { ShortCodebookDesc { code: 0b1111011, bits: 7 }, ShortCodebookDesc { code: 0b1111110, bits: 7 }, ShortCodebookDesc { code: 0b11111111, bits: 8 } - ); + ]; let mut br2 = BitReader::new(buf, BitReaderMode::BE); let mut cfr = ShortCodebookDescReader::new(scb_desc); let cb = Codebook::new(&mut cfr, CodebookMode::MSB).unwrap(); @@ -497,7 +497,7 @@ mod test { const BITS_LE: [u8; 3] = [0b11101111, 0b01110010, 0b01]; let buf = &BITS_LE; - let scble_desc: Vec = vec!( + let scble_desc = &[ ShortCodebookDesc { code: 0b00, bits: 2 }, ShortCodebookDesc { code: 0, bits: 0 }, ShortCodebookDesc { code: 0b01, bits: 2 }, @@ -510,7 +510,7 @@ mod test { ShortCodebookDesc { code: 0b0101111, bits: 7 }, ShortCodebookDesc { code: 0b0111111, bits: 7 }, ShortCodebookDesc { code: 0b1011101111, bits: 10 } - ); + ]; let mut brl = BitReader::new(buf, BitReaderMode::LE); let mut cfr = ShortCodebookDescReader::new(scble_desc); let cb = Codebook::new(&mut cfr, CodebookMode::LSB).unwrap(); diff --git a/nihav-duck/src/codecs/truemotion2.rs b/nihav-duck/src/codecs/truemotion2.rs index 08115f6..281b7b7 100644 --- a/nihav-duck/src/codecs/truemotion2.rs +++ b/nihav-duck/src/codecs/truemotion2.rs @@ -184,7 +184,7 @@ impl TM2Stream { hdef.read(&mut br, &mut codes, 0, 0)?; htree.sym0 = codes[0].sym; if nelems > 1 { - let mut cr = FullCodebookDescReader::new(codes); + let mut cr = FullCodebookDescReader::new(&codes); htree.cb = Some(Codebook::new(&mut cr, CodebookMode::MSB)?); } diff --git a/nihav-game/src/codecs/midivid3.rs b/nihav-game/src/codecs/midivid3.rs index aebd118..910688b 100644 --- a/nihav-game/src/codecs/midivid3.rs +++ b/nihav-game/src/codecs/midivid3.rs @@ -66,7 +66,7 @@ struct Midivid3Decoder { impl Midivid3Decoder { fn new() -> Self { - let mut cr = ShortCodebookDescReader::new(vec![ + let mut cr = ShortCodebookDescReader::new(&[ ShortCodebookDesc { code: 0b00, bits: 2 }, ShortCodebookDesc { code: 0b01, bits: 2 }, ShortCodebookDesc { code: 0b100, bits: 3 }, -- 2.39.5