Files
craft-libs/crates/video/src/mpeg2/bits.rs
Dylan M. Taylor 5e892217ed craft-video: MPEG-2 and MPEG-1 video decoder from FilmCraft (mpeg2) (#864)
* craft-video: MPEG-2 and MPEG-1 video decoder from FilmCraft (`mpeg2`)

FilmCraft's crates/mpeg2v at 4bec22bd moves to craft_video::mpeg2 behind the
decoder-only feature `mpeg2`: sources, README and tests (unit, synthetic
streams checked by ffmpeg, the ffmpeg oracle fixtures, hostile sweeps).

Changes in the move: craft-media-testkit instead of filmcraft-testkit, one
test target (tests/mpeg2), crate paths, rustfmt at this repo's width.

New: a memory budget (Decoder::set_memory_budget, craft_video::budget) checked
when a sequence becomes active, and a mutation test over the synthetic
field-picture, 16x8 and dual-prime streams, which ffmpeg's encoder cannot
produce and the hostile sweep therefore never reached.

* craft-video mpeg2: write the restart check without a negated conjunction (clippy nonminimal_bool)
2026-11-11 17:42:02 -05:00

94 lines
2.4 KiB
Rust

//! Big-endian bit reader over one start-code unit. Reads past the end return zero bits; callers
//! check [`Bits::overrun`] to detect truncated data.
#[derive(Clone)]
pub(crate) struct Bits<'a> {
data: &'a [u8],
pos: usize,
}
impl<'a> Bits<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self { data, pos: 0 }
}
/// The next 32 bits, MSB first (zeros past the end).
#[inline(always)]
pub fn peek32(&self) -> u32 {
let byte = self.pos >> 3;
let w = if let Some(&b) = self.data.get(byte..).and_then(|d| d.first_chunk::<8>()) {
u64::from_be_bytes(b)
} else {
let mut b = [0u8; 8];
if byte < self.data.len() {
let n = self.data.len() - byte;
b[..n].copy_from_slice(&self.data[byte..]);
}
u64::from_be_bytes(b)
};
((w << (self.pos & 7)) >> 32) as u32
}
/// The next `n` (1..=32) bits without consuming them.
#[inline(always)]
pub fn peek(&self, n: u32) -> u32 {
debug_assert!((1..=32).contains(&n));
((self.peek32() as u64) >> (32 - n)) as u32
}
#[inline(always)]
pub fn skip(&mut self, n: u32) {
self.pos += n as usize;
}
#[inline(always)]
pub fn read(&mut self, n: u32) -> u32 {
if n == 0 {
return 0;
}
let v = self.peek(n);
self.pos += n as usize;
v
}
#[inline(always)]
pub fn bit(&mut self) -> bool {
self.read(1) == 1
}
/// A `n`-bit two's complement value.
#[inline(always)]
pub fn read_signed(&mut self, n: u32) -> i32 {
let v = self.read(n) as i32;
(v << (32 - n)) >> (32 - n)
}
pub fn bits_left(&self) -> isize {
(self.data.len() * 8) as isize - self.pos as isize
}
/// More than a few bytes have been read past the end: the data is truncated or corrupt.
pub fn overrun(&self) -> bool {
self.bits_left() < 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_across_bytes_and_past_the_end() {
let d = [0b1010_1100, 0xFF, 0x01];
let mut b = Bits::new(&d);
assert_eq!(b.read(3), 0b101);
assert_eq!(b.read(7), 0b011_0011);
assert_eq!(b.peek(4), 0xF);
assert_eq!(b.read_signed(4), -1);
assert_eq!(b.read(10), 0b11_0000_0001);
assert!(!b.overrun());
assert_eq!(b.read(8), 0);
assert!(b.overrun());
}
}