1 //! Auxiliary data structures for codecs.
5 /// In the decoding process of many codecs there is a need to store some previously decoded information and only immediate top neighbours are used.
6 /// This can be done by storing either the full information for the whole frame or just the top line and move information for last decoded row to the top every time when row decoding is done.
7 /// `GenericCache` implements the second approach.
11 /// Create a cache for one line and use top pixel for prediction:
13 /// use nihav_codec_support::data::GenericCache;
15 /// # let width = 640;
16 /// # let height = 480;
17 /// # let mut dst: Vec<u8> = vec![0; width];
18 /// let mut linecache: GenericCache<u8> = GenericCache::new(1, width, 0x80);
19 /// for _ in 0..height {
20 /// for x in 0..width {
22 /// dst[x] = linecache.data[linecache.xpos + x - linecache.stride] + delta;
24 /// linecache.update_row();
27 pub struct GenericCache<T: Copy> {
28 /// Number of rows that are processed at once.
30 /// Number of elements in one row.
32 /// Start of curent data.
36 /// Default value to fill the cache with.
40 impl<T:Copy> GenericCache<T> {
41 /// Constructs a new instance of `GenericCache`.
42 pub fn new(height: usize, stride: usize, default: T) -> Self {
47 data: Vec::with_capacity((height + 1) * stride + 1),
53 /// Reports the total amount of elements stored.
54 pub fn full_size(&self) -> usize { self.stride * (self.height + 1) + 1 }
55 /// Resets the cache state.
56 pub fn reset(&mut self) {
58 let size = self.full_size();
59 self.data.resize(size, self.default);
60 self.xpos = self.stride + 1;
62 /// Updates cache state for the next line.
63 pub fn update_row(&mut self) {
64 for i in 0..self.stride {
65 self.data[i] = self.data[self.height * self.stride + i];
67 self.data.truncate(self.stride);
68 let size = self.full_size();
69 self.data.resize(size, self.default);
70 self.xpos = self.stride + 1;