| 1 |
use alloc::vec::Vec; |
| 2 |
use core::cmp; |
| 3 |
|
| 4 |
use crate::{ |
| 5 |
lz77::LongestMatch, |
| 6 |
util::{ZOPFLI_CACHE_LENGTH, ZOPFLI_MAX_MATCH, ZOPFLI_MIN_MATCH}, |
| 7 |
}; |
| 8 |
|
| 9 |
// Cache used by ZopfliFindLongestMatch to remember previously found length/dist |
| 10 |
// values. |
| 11 |
// This is needed because the squeeze runs will ask these values multiple times for |
| 12 |
// the same position. |
| 13 |
// Uses large amounts of memory, since it has to remember the distance belonging |
| 14 |
// to every possible shorter-than-the-best length (the so called "sublen" array). |
| 15 |
pub struct ZopfliLongestMatchCache { |
| 16 |
length: Vec<u16>, |
| 17 |
dist: Vec<u16>, |
| 18 |
sublen: Vec<u8>, |
| 19 |
} |
| 20 |
|
| 21 |
impl ZopfliLongestMatchCache { |
| 22 |
pub fn new(blocksize: usize) -> Self { |
| 23 |
Self { |
| 24 |
/* length > 0 and dist 0 is invalid combination, which indicates on purpose |
| 25 |
that this cache value is not filled in yet. */ |
| 26 |
length: vec![1; blocksize], |
| 27 |
dist: vec![0; blocksize], |
| 28 |
/* Rather large amount of memory. */ |
| 29 |
sublen: vec![0; ZOPFLI_CACHE_LENGTH * blocksize * 3], |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
fn length_at(&self, pos: usize) -> u16 { |
| 34 |
self.length[pos] |
| 35 |
} |
| 36 |
|
| 37 |
fn dist_at(&self, pos: usize) -> u16 { |
| 38 |
self.dist[pos] |
| 39 |
} |
| 40 |
|
| 41 |
fn store_length_at(&mut self, pos: usize, val: u16) { |
| 42 |
self.length[pos] = val; |
| 43 |
} |
| 44 |
|
| 45 |
fn store_dist_at(&mut self, pos: usize, val: u16) { |
| 46 |
self.dist[pos] = val; |
| 47 |
} |
| 48 |
|
| 49 |
/// Returns the length up to which could be stored in the cache. |
| 50 |
fn max_sublen(&self, pos: usize) -> u32 { |
| 51 |
let start = ZOPFLI_CACHE_LENGTH * pos * 3; |
| 52 |
if self.sublen[start + 1] == 0 && self.sublen[start + 2] == 0 { |
| 53 |
return 0; // No sublen cached. |
| 54 |
} |
| 55 |
u32::from(self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)]) + 3 |
| 56 |
} |
| 57 |
|
| 58 |
/// Stores sublen array in the cache. |
| 59 |
fn store_sublen(&mut self, sublen: &[u16], pos: usize, length: usize) { |
| 60 |
if length < 3 { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
let start = ZOPFLI_CACHE_LENGTH * pos * 3; |
| 65 |
let mut i = 3; |
| 66 |
let mut j = 0; |
| 67 |
let mut bestlength = 0; |
| 68 |
while i <= length { |
| 69 |
if i == length || sublen[i] != sublen[i + 1] { |
| 70 |
self.sublen[start + (j * 3)] = (i - 3) as u8; |
| 71 |
self.sublen[start + (j * 3 + 1)] = sublen[i].wrapping_rem(256) as u8; |
| 72 |
self.sublen[start + (j * 3 + 2)] = (sublen[i] >> 8).wrapping_rem(256) as u8; |
| 73 |
bestlength = i as u32; |
| 74 |
j += 1; |
| 75 |
if j >= ZOPFLI_CACHE_LENGTH { |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
i += 1; |
| 80 |
} |
| 81 |
|
| 82 |
if j < ZOPFLI_CACHE_LENGTH { |
| 83 |
debug_assert_eq!(bestlength, length as u32); |
| 84 |
self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] = (bestlength - 3) as u8; |
| 85 |
} else { |
| 86 |
debug_assert!(bestlength <= length as u32); |
| 87 |
} |
| 88 |
debug_assert_eq!(bestlength, self.max_sublen(pos)); |
| 89 |
} |
| 90 |
|
| 91 |
/// Extracts sublen array from the cache. |
| 92 |
fn fetch_sublen(&self, pos: usize, length: usize, sublen: &mut [u16]) { |
| 93 |
if length < 3 { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
let start = ZOPFLI_CACHE_LENGTH * pos * 3; |
| 98 |
let maxlength = self.max_sublen(pos) as usize; |
| 99 |
let mut prevlength = 0; |
| 100 |
|
| 101 |
for j in 0..ZOPFLI_CACHE_LENGTH { |
| 102 |
let length = self.sublen[start + (j * 3)] as usize + 3; |
| 103 |
let dist = u16::from(self.sublen[start + (j * 3 + 1)]) |
| 104 |
+ 256 * u16::from(self.sublen[start + (j * 3 + 2)]); |
| 105 |
|
| 106 |
let mut i = prevlength; |
| 107 |
while i <= length { |
| 108 |
sublen[i] = dist; |
| 109 |
i += 1; |
| 110 |
} |
| 111 |
if length == maxlength { |
| 112 |
break; |
| 113 |
} |
| 114 |
prevlength = length + 1; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
pub trait Cache { |
| 120 |
fn try_get( |
| 121 |
&self, |
| 122 |
pos: usize, |
| 123 |
limit: usize, |
| 124 |
sublen: &mut Option<&mut [u16]>, |
| 125 |
blockstart: usize, |
| 126 |
) -> LongestMatch; |
| 127 |
fn store( |
| 128 |
&mut self, |
| 129 |
pos: usize, |
| 130 |
limit: usize, |
| 131 |
sublen: &mut Option<&mut [u16]>, |
| 132 |
distance: u16, |
| 133 |
length: u16, |
| 134 |
blockstart: usize, |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
pub struct NoCache; |
| 139 |
|
| 140 |
impl Cache for NoCache { |
| 141 |
fn try_get( |
| 142 |
&self, |
| 143 |
_: usize, |
| 144 |
limit: usize, |
| 145 |
_: &mut Option<&mut [u16]>, |
| 146 |
_: usize, |
| 147 |
) -> LongestMatch { |
| 148 |
LongestMatch::new(limit) |
| 149 |
} |
| 150 |
|
| 151 |
fn store(&mut self, _: usize, _: usize, _: &mut Option<&mut [u16]>, _: u16, _: u16, _: usize) { |
| 152 |
// Nowhere to store |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl Cache for ZopfliLongestMatchCache { |
| 157 |
fn try_get( |
| 158 |
&self, |
| 159 |
pos: usize, |
| 160 |
mut limit: usize, |
| 161 |
sublen: &mut Option<&mut [u16]>, |
| 162 |
blockstart: usize, |
| 163 |
) -> LongestMatch { |
| 164 |
let mut longest_match = LongestMatch::new(limit); |
| 165 |
|
| 166 |
/* The LMC cache starts at the beginning of the block rather than the |
| 167 |
beginning of the whole array. */ |
| 168 |
let lmcpos = pos - blockstart; |
| 169 |
|
| 170 |
/* Length > 0 and dist 0 is invalid combination, which indicates on purpose |
| 171 |
that this cache value is not filled in yet. */ |
| 172 |
let length_lmcpos = self.length_at(lmcpos); |
| 173 |
let dist_lmcpos = self.dist_at(lmcpos); |
| 174 |
let cache_available = length_lmcpos == 0 || dist_lmcpos != 0; |
| 175 |
let max_sublen = self.max_sublen(lmcpos); |
| 176 |
let limit_ok_for_cache = limit == ZOPFLI_MAX_MATCH |
| 177 |
|| length_lmcpos <= limit as u16 |
| 178 |
|| (sublen.is_some() && max_sublen >= limit as u32); |
| 179 |
|
| 180 |
if limit_ok_for_cache && cache_available { |
| 181 |
if sublen.is_none() || u32::from(length_lmcpos) <= max_sublen { |
| 182 |
let length = cmp::min(length_lmcpos, limit as u16); |
| 183 |
let distance; |
| 184 |
if let Some(ref mut subl) = *sublen { |
| 185 |
self.fetch_sublen(lmcpos, length as usize, subl); |
| 186 |
distance = subl[length as usize]; |
| 187 |
|
| 188 |
if limit == ZOPFLI_MAX_MATCH && length >= ZOPFLI_MIN_MATCH as u16 { |
| 189 |
debug_assert_eq!(subl[length as usize], dist_lmcpos); |
| 190 |
} |
| 191 |
} else { |
| 192 |
distance = dist_lmcpos; |
| 193 |
} |
| 194 |
longest_match.distance = distance; |
| 195 |
longest_match.length = length; |
| 196 |
longest_match.from_cache = true; |
| 197 |
return longest_match; |
| 198 |
} |
| 199 |
/* Can't use much of the cache, since the "sublens" need to be calculated, |
| 200 |
but at least we already know when to stop. */ |
| 201 |
limit = length_lmcpos as usize; |
| 202 |
longest_match.limit = limit; |
| 203 |
} |
| 204 |
|
| 205 |
longest_match |
| 206 |
} |
| 207 |
|
| 208 |
fn store( |
| 209 |
&mut self, |
| 210 |
pos: usize, |
| 211 |
limit: usize, |
| 212 |
sublen: &mut Option<&mut [u16]>, |
| 213 |
distance: u16, |
| 214 |
length: u16, |
| 215 |
blockstart: usize, |
| 216 |
) { |
| 217 |
if let Some(ref mut subl) = *sublen { |
| 218 |
/* Length > 0 and dist 0 is invalid combination, which indicates on purpose |
| 219 |
that this cache value is not filled in yet. */ |
| 220 |
let lmcpos = pos - blockstart; |
| 221 |
let cache_available = self.length_at(lmcpos) == 0 || self.dist_at(lmcpos) != 0; |
| 222 |
|
| 223 |
if limit == ZOPFLI_MAX_MATCH && !cache_available { |
| 224 |
debug_assert!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0); |
| 225 |
if length < ZOPFLI_MIN_MATCH as u16 { |
| 226 |
self.store_dist_at(lmcpos, 0); |
| 227 |
self.store_length_at(lmcpos, 0); |
| 228 |
} else { |
| 229 |
self.store_dist_at(lmcpos, distance); |
| 230 |
self.store_length_at(lmcpos, length); |
| 231 |
} |
| 232 |
debug_assert!(!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0)); |
| 233 |
self.store_sublen(subl, lmcpos, length as usize); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |