FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#1450: table-driven CRC-32 for PSI sections
vangel76da13964a820d
diff --git a/crates/mpegts/src/lib.rs b/crates/mpegts/src/lib.rsindex 4092b6b51..cb62b66e7 100644--- a/crates/mpegts/src/lib.rs+++ b/crates/mpegts/src/lib.rs@@ -345,3 +345,5 @@ impl Unwrap {#[cfg(test)]mod tests;+#[cfg(test)]+mod work_tests;diff --git a/crates/mpegts/src/ts.rs b/crates/mpegts/src/ts.rsindex 98b092f72..b77691bb0 100644--- a/crates/mpegts/src/ts.rs+++ b/crates/mpegts/src/ts.rs@@ -6,14 +6,29 @@ use crate::es::Splitter;use crate::pes::{self, PesHeader};use crate::{ByteSource, Codec, Error, File, Format, Result, Stream, StreamId, Unit, Unwrap, read_some};+/// Byte-wise table for the MSB-first CRC-32 (polynomial 0x04C11DB7): entry `i` is the CRC register+/// after shifting the byte `i` through eight bit steps.+const CRC_TABLE: [u32; 256] = {+ let mut t = [0u32; 256];+ let mut i = 0;+ while i < 256 {+ let mut crc = (i as u32) << 24;+ let mut bit = 0;+ while bit < 8 {+ crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 };+ bit += 1;+ }+ t[i] = crc;+ i += 1;+ }+ t+};+/// CRC-32 of PSI sections (MSB-first, polynomial 0x04C11DB7, initial value all ones; Annex A).pub(crate) fn crc32_mpeg(data: &[u8]) -> u32 {let mut crc = 0xFFFF_FFFFu32;for &b in data {- crc ^= (b as u32) << 24;- for _ in 0..8 {- crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 };- }+ crc = (crc << 8) ^ CRC_TABLE[((crc >> 24) as u8 ^ b) as usize];}crc}diff --git a/crates/mpegts/src/work_tests.rs b/crates/mpegts/src/work_tests.rsnew file mode 100644index 000000000..01d2244ad--- /dev/null+++ b/crates/mpegts/src/work_tests.rs@@ -0,0 +1,50 @@+//! Equality and counting tests for the redundant-work removals: the old implementation is kept+//! here as the reference, the new one must give the same results.++use super::*;++/// The bitwise CRC-32 that `ts::crc32_mpeg` replaced.+fn crc32_bitwise(data: &[u8]) -> u32 {+ let mut crc = 0xFFFF_FFFFu32;+ for &b in data {+ crc ^= (b as u32) << 24;+ for _ in 0..8 {+ crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 };+ }+ }+ crc+}++/// Deterministic xorshift bytes.+fn noise(seed: u32, n: usize) -> Vec<u8> {+ let mut x = seed | 1;+ (0..n)+ .map(|_| {+ x ^= x << 13;+ x ^= x >> 17;+ x ^= x << 5;+ (x >> 8) as u8+ })+ .collect()+}++#[test]+fn table_crc_equals_the_bitwise_crc() {+ // the standard check value of CRC-32/MPEG-2+ assert_eq!(ts::crc32_mpeg(b"123456789"), 0x0376_E6E7);+ assert_eq!(ts::crc32_mpeg(&[]), 0xFFFF_FFFF);+ for b in 0..=255u8 {+ assert_eq!(ts::crc32_mpeg(&[b]), crc32_bitwise(&[b]));+ }+ for n in [0usize, 1, 2, 3, 4, 5, 15, 16, 17, 183, 184, 1023, 1024, 4096] {+ for seed in 1..6u32 {+ let d = noise(seed * 7919 + n as u32, n);+ assert_eq!(ts::crc32_mpeg(&d), crc32_bitwise(&d), "len {n} seed {seed}");+ }+ }+ // a section followed by its own CRC checks to zero+ let mut sec = noise(5, 40);+ let crc = ts::crc32_mpeg(&sec);+ sec.extend(crc.to_be_bytes());+ assert_eq!(ts::crc32_mpeg(&sec), 0);+}