access_unit/
h264.rs

1pub fn is_nalu(data: &[u8]) -> bool {
2    if data.len() < 3 {
3        return false;
4    }
5
6    data.windows(3).any(|window| match window {
7        [0x00, 0x00, 0x01] => true,
8        [0x00, 0x00, 0x00] if data.len() >= 4 && data[3] == 0x01 => true,
9        _ => false,
10    })
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16
17    #[test]
18    fn test_nalu_detection() {
19        // Start codes at beginning
20        assert!(is_nalu(&[0x00, 0x00, 0x01, 0x09, 0xFF]));
21        assert!(is_nalu(&[0x00, 0x00, 0x00, 0x01, 0x09, 0xFF]));
22
23        // Start codes in middle
24        assert!(is_nalu(&[0xFF, 0xFF, 0x00, 0x00, 0x01, 0x09]));
25        assert!(is_nalu(&[0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x09]));
26
27        // Start codes at end
28        assert!(is_nalu(&[0xFF, 0xFF, 0x00, 0x00, 0x01]));
29        assert!(is_nalu(&[0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01]));
30
31        // Multiple start codes
32        assert!(is_nalu(&[
33            0xFF, 0x00, 0x00, 0x01, 0x09, 0x00, 0x00, 0x00, 0x01
34        ]));
35
36        // Invalid data
37        assert!(!is_nalu(&[0x00, 0x01, 0x00, 0x01]));
38        assert!(!is_nalu(&[0xFF, 0xFF, 0xFF, 0xFF]));
39
40        // Too short
41        assert!(!is_nalu(&[0x00, 0x00]));
42        assert!(!is_nalu(&[]));
43
44        // Partial start code at end
45        assert!(!is_nalu(&[0xFF, 0x00, 0x00]));
46        assert!(!is_nalu(&[0xFF, 0x00, 0x00, 0x00]));
47    }
48}