The word-buffered bit accumulator no longer shifts by 64
Browse files at this commit82ba265691018540707d8b2483d160ea80bc4aadParent: a050448- fix
- the word-buffered bit accumulator no longer shifts by 64
1 changed file +75 -2
- crates/ec-mp3/src/huffman.rs +75 -2
crates/ec-mp3/src/huffman.rsmodified+75 -2
| @@ -205,11 +205,40 @@ impl BitBuf { | ||
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /// Appends the low `n` bits of `value` (`n <= 64`), most significant first. |
| 208 | + /// | |
| 209 | + /// The run is placed after the bits already buffered; when it would spill | |
| 210 | + /// past the accumulator's 64 bits the leading bits complete the buffered | |
| 211 | + /// bytes, those bytes are emitted, and the remainder starts a fresh word. | |
| 212 | + /// Since `used < 8`, the free space is always at least 57 bits, so the | |
| 213 | + /// remainder is under eight bits and the trailing drain below is the same | |
| 214 | + /// one the un-split path runs. A zero-length run is a no-op: the | |
| 215 | + /// scalefactor coder emits one for every band whose `slen` is zero. | |
| 208 | 216 | pub(crate) fn push_bits(&mut self, value: u64, n: u32) { |
| 209 | 217 | debug_assert!(n <= 64, "push_bits: n = {n} > 64"); |
| 218 | + if n == 0 { | |
| 219 | + return; | |
| 220 | + } | |
| 210 | 221 | let mask = if n >= 64 { u64::MAX } else { (1u64 << n) - 1 }; |
| 211 | - self.acc |= (value & mask) << (64 - self.used - n); | |
| 212 | - self.used += n; | |
| 222 | + let value = value & mask; | |
| 223 | + let free = 64 - self.used; | |
| 224 | + if n <= free { | |
| 225 | + self.acc |= value << (free - n); | |
| 226 | + self.used += n; | |
| 227 | + } else { | |
| 228 | + // Spill: the run's top `free` bits fill the accumulator exactly, | |
| 229 | + // flush it, then the low `n - free` bits sit at the top of an | |
| 230 | + // empty word. | |
| 231 | + self.acc |= value >> (n - free); | |
| 232 | + self.used = 64; | |
| 233 | + while self.used >= 8 { | |
| 234 | + self.bytes.push((self.acc >> 56) as u8); | |
| 235 | + self.acc <<= 8; | |
| 236 | + self.used -= 8; | |
| 237 | + } | |
| 238 | + let tail = n - free; | |
| 239 | + self.acc |= (value & ((1u64 << tail) - 1)) << (64 - tail); | |
| 240 | + self.used = tail; | |
| 241 | + } | |
| 213 | 242 | while self.used >= 8 { |
| 214 | 243 | self.bytes.push((self.acc >> 56) as u8); |
| 215 | 244 | self.acc <<= 8; |
| @@ -378,6 +407,50 @@ mod tests { | ||
| 378 | 407 | } |
| 379 | 408 | } |
| 380 | 409 | |
| 410 | + /// `push_bits` appends any run up to 64 bits, even one that straddles the | |
| 411 | + /// accumulator's own width, and a zero-length run is a no-op. Byte-for-byte | |
| 412 | + /// against a plain MSB-first reference. The cases that bite: a zero-length | |
| 413 | + /// run at `used == 0` (which the scalefactor path hits whenever `slen` is | |
| 414 | + /// zero) shifted by the full 64, and a run whose leading bits complete the | |
| 415 | + /// buffered bytes plus a remainder. | |
| 416 | + #[test] | |
| 417 | + fn bits_survive_the_accumulator_boundary() { | |
| 418 | + let mut writer = BitBuf::new(); | |
| 419 | + let mut bits: Vec<bool> = Vec::new(); | |
| 420 | + let push = |writer: &mut BitBuf, bits: &mut Vec<bool>, value: u64, n: u32| { | |
| 421 | + for i in (0..n).rev() { | |
| 422 | + bits.push((value >> i) & 1 == 1); | |
| 423 | + } | |
| 424 | + writer.push_bits(value, n); | |
| 425 | + }; | |
| 426 | + for &(value, n) in &[ | |
| 427 | + (0u64, 0u32), // zero-length run, empty buffer | |
| 428 | + (0b101, 3), // part-fill | |
| 429 | + (0xABCD_EF01_2345_6789, 64), // a full word, straddling | |
| 430 | + (0b1_0110, 5), | |
| 431 | + (0x00FF_00FF_00FF_00FF, 60), // 5 + 60 > 64: a spill | |
| 432 | + (0, 0), // zero-length mid-stream | |
| 433 | + (0xDEAD_BEEF, 32), | |
| 434 | + (0b111, 3), | |
| 435 | + (0x5555_5555_5555_5555, 57), // another spill | |
| 436 | + ] { | |
| 437 | + push(&mut writer, &mut bits, value, n); | |
| 438 | + } | |
| 439 | + let mut want = Vec::new(); | |
| 440 | + let mut byte = 0u8; | |
| 441 | + for (i, bit) in bits.iter().enumerate() { | |
| 442 | + byte = (byte << 1) | u8::from(*bit); | |
| 443 | + if i % 8 == 7 { | |
| 444 | + want.push(byte); | |
| 445 | + byte = 0; | |
| 446 | + } | |
| 447 | + } | |
| 448 | + if bits.len() % 8 != 0 { | |
| 449 | + want.push(byte << (8 - bits.len() % 8)); | |
| 450 | + } | |
| 451 | + assert_eq!(writer.into_bytes(), want); | |
| 452 | + } | |
| 453 | + | |
| 381 | 454 | #[test] |
| 382 | 455 | fn quads_round_trip() { |
| 383 | 456 | for select in [false, true] { |