]> git.nihav.org Git - nihav.git/commitdiff
core/codebook: switch {Full,Short}CodebookDescReader to use slice reference
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 9 Nov 2024 13:34:40 +0000 (14:34 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 9 Nov 2024 14:34:05 +0000 (15:34 +0100)
This finally allows using them with static code definitions instead of
always constructing a vector.

nihav-core/src/compr/deflate.rs
nihav-core/src/io/codebook.rs
nihav-duck/src/codecs/truemotion2.rs
nihav-game/src/codecs/midivid3.rs

index 052043d8833ea40c5df8c4846b53520df382a4d6..bb47c5bd9789c537a901286cb9821881f4d04a99 100644 (file)
@@ -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();
index bd7bb1e8ce24f90111cfc480d0c6d9f1fa88c193..625009f9784b48e7b836ba245aececa2ecba5ec8 100644 (file)
 //! ```
 //! use nihav_core::io::codebook::{ShortCodebookDesc, ShortCodebookDescReader, Codebook, CodebookMode};
 //!
-//! let cb_desc: Vec<ShortCodebookDesc> = 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<S> for BitReader<'a> {
     }
 }
 
-/// Codebook description that stores a list of codewords and their values.
-pub struct FullCodebookDescReader<S> {
-    data: Vec<FullCodebookDesc<S>>,
+/// Codebook description that stores a reference to the list of codewords and their values.
+pub struct FullCodebookDescReader<'a, S> {
+    data: &'a [FullCodebookDesc<S>],
 }
 
-impl<S> FullCodebookDescReader<S> {
+impl<'a, S> FullCodebookDescReader<'a, S> {
     /// Constructs a new `FullCodebookDescReader` instance.
-    pub fn new(data: Vec<FullCodebookDesc<S>>) -> Self {
+    pub fn new(data: &'a [FullCodebookDesc<S>]) -> Self {
         FullCodebookDescReader { data }
     }
 }
 
-impl<S: Copy> CodebookDescReader<S> for FullCodebookDescReader<S> {
+impl<'a, S: Copy> CodebookDescReader<S> 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<ShortCodebookDesc>,
+/// 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<ShortCodebookDesc<>>) -> Self {
+    pub fn new(data: &'a [ShortCodebookDesc]) -> Self {
         ShortCodebookDescReader { data }
     }
 }
 
-impl CodebookDescReader<u32> for ShortCodebookDescReader {
+impl<'a> CodebookDescReader<u32> 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<FullCodebookDesc<i8>> = 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<ShortCodebookDesc> = 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<ShortCodebookDesc> = 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();
index 08115f6a27e932deca0c440cc9460b1336fa6b96..281b7b7c79267e0c3f3a4611282da0f795449b60 100644 (file)
@@ -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)?);
         }
 
index aebd11814ecfa364a4bd5d98614f890179f3c47e..910688b8cdedba6f1df4c00c1408460461b7aa82 100644 (file)
@@ -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 },