forge
Explore
Log in

Don't you dare go Hollow

The word-buffered bit accumulator no longer shifts by 64

Browse files at this commit

82ba265691018540707d8b2483d160ea80bc4aadAhmet Kaan GümüşParent: a050448
  • fix
    • the word-buffered bit accumulator no longer shifts by 64
Show full message
fix(mp3): the word-buffered bit accumulator no longer shifts by 64

push_bits computed `(value & mask) << (64 - used - n)`. For a ZERO-LENGTH run
on an empty buffer (used == 0, n == 0) that shift count is exactly 64: debug
builds panicked ("attempt to shift left with overflow") and release builds
wrapped it to a shift of 0, OR-ing the unmasked value into the accumulator.
The scalefactor coder emits such a run for every band whose slen is 0 - the
normal case when all scalefactors are zero. The debt's "used + n > 64" framing
was wrong: used < 8 always, so free >= 57 and the old subtraction never
underflowed.

Fix: n == 0 returns early; a run longer than the free space fills the word,
drains its complete bytes and starts the remainder in a fresh word (the
remainder is under eight bits, so the trailing drain is the one the un-split
path already ran). For n >= 1 with n <= free the arithmetic is unchanged and
the emitted bytes are identical, so previously working encodes are
byte-for-byte the same.

Evidence: ec-mp3 lib 22/22 (3 panicked before at the old huffman.rs:211);
edith `cargo test -p engine --test audio_export -- --test-threads=1` 14/14
(`exports_the_timeline_as_an_mp3` and `the_sound_is_written_at_the_rate_that_
was_asked_for` failed pre-fix with "export worker panicked: attempt to shift
left with overflow"); new boundary test compares byte-for-byte against a plain
MSB-first reference across n = 0, full-word and spill runs, and fails on the
old body. Both reviewed independently: SAFE TO COMMIT.
crates/ec-mp3/src/huffman.rsmodified+75 -2
@@ -205,11 +205,40 @@ impl BitBuf {
205 }205 }
206 206
207 /// Appends the low `n` bits of `value` (`n <= 64`), most significant first.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 pub(crate) fn push_bits(&mut self, value: u64, n: u32) {216 pub(crate) fn push_bits(&mut self, value: u64, n: u32) {
209 debug_assert!(n <= 64, "push_bits: n = {n} > 64");217 debug_assert!(n <= 64, "push_bits: n = {n} > 64");
218+ if n == 0 {
219+ return;
220+ }
210 let mask = if n >= 64 { u64::MAX } else { (1u64 << n) - 1 };221 let mask = if n >= 64 { u64::MAX } else { (1u64 << n) - 1 };
211- self.acc |= (value & mask) << (64 - self.used - n);222+ let value = value & mask;
212- self.used += n;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 while self.used >= 8 {242 while self.used >= 8 {
214 self.bytes.push((self.acc >> 56) as u8);243 self.bytes.push((self.acc >> 56) as u8);
215 self.acc <<= 8;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 #[test]454 #[test]
382 fn quads_round_trip() {455 fn quads_round_trip() {
383 for select in [false, true] {456 for select in [false, true] {