edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Damaged and hostile input: nothing may panic, and decoding is deterministic. |
| 2 | use ec_dv::{DvVideoDecoder, PROFILES, extract_audio}; |
| 3 | |
| 4 | struct Rng(u64); |
| 5 | impl Rng { |
| 6 | fn next(&mut self) -> u64 { |
| 7 | self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); |
| 8 | self.0 >> 33 |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | fn frame_for(p: &ec_dv::Profile, rng: &mut Rng, header_bytes: bool) -> Vec<u8> { |
| 13 | let mut f: Vec<u8> = (0..p.frame_size).map(|_| rng.next() as u8).collect(); |
| 14 | if header_bytes { |
| 15 | f[3] = (f[3] & 0x7f) | (p.dsf << 7); |
| 16 | f[80 * 5 + 48 + 3] = p.video_stype; |
| 17 | f[4] &= 0xf8; |
| 18 | for b in 0..p.frame_size / 80 { |
| 19 | // plausible DIF headers keep the segment status nibbles equal, as real streams do |
| 20 | f[b * 80 + 3] = (f[b * 80 + 3] & 0x0f) | 0xf0; |
| 21 | } |
| 22 | f[3] = (f[3] & 0x7f) | (p.dsf << 7); |
| 23 | f[80 * 5 + 48 + 3] = p.video_stype; |
| 24 | } |
| 25 | f |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn random_frames_of_every_profile_do_not_panic() { |
| 30 | let mut rng = Rng(7); |
| 31 | for (i, p) in PROFILES.iter().enumerate() { |
| 32 | if i == 2 || i == 9 { |
| 33 | continue; // PAL 4:1:1 / IEC 61883-5 share their header bytes with other entries |
| 34 | } |
| 35 | for round in 0..6 { |
| 36 | let f = frame_for(p, &mut rng, round % 2 == 0); |
| 37 | let mut d = DvVideoDecoder::new(); |
| 38 | if round % 2 == 1 { |
| 39 | let _ = d.decode(&f); // random header: an error is fine, a panic is not |
| 40 | let _ = extract_audio(&f); |
| 41 | continue; |
| 42 | } |
| 43 | let a = d.decode(&f).unwrap_or_else(|e| panic!("profile {i} round {round}: {e:?}")); |
| 44 | let b = DvVideoDecoder::new().decode(&f).unwrap(); |
| 45 | assert_eq!(a.planes, b.planes, "decode must be deterministic"); |
| 46 | assert_eq!((a.width, a.height), (p.width, p.height)); |
| 47 | let _ = extract_audio(&f); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn bit_flips_and_truncations_do_not_panic() { |
| 54 | let mut rng = Rng(99); |
| 55 | let p = &PROFILES[0]; |
| 56 | let base = frame_for(p, &mut rng, true); |
| 57 | let mut d = DvVideoDecoder::new(); |
| 58 | for n in 0..200 { |
| 59 | let mut f = base.clone(); |
| 60 | for _ in 0..(1 + n % 20) { |
| 61 | let at = rng.next() as usize % f.len(); |
| 62 | f[at] ^= 1 << (rng.next() % 8); |
| 63 | } |
| 64 | let _ = d.decode(&f); |
| 65 | let cut = rng.next() as usize % f.len(); |
| 66 | let _ = d.decode(&f[..cut]); |
| 67 | let _ = extract_audio(&f[..cut]); |
| 68 | let _ = extract_audio(&f); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn all_headers_with_hostile_audio_packs() { |
| 74 | let mut rng = Rng(5); |
| 75 | for p in PROFILES.iter() { |
| 76 | for _ in 0..20 { |
| 77 | let mut f = frame_for(p, &mut rng, true); |
| 78 | for c in 0..10usize { |
| 79 | let offs = if c & 1 == 1 { 80 * 6 + 3 + c * 12000 } else { 80 * 6 + 80 * 16 * 3 + 3 + c * 12000 }; |
| 80 | if offs + 5 < f.len() { |
| 81 | f[offs] = 0x50; |
| 82 | f[offs + 1] = rng.next() as u8; |
| 83 | f[offs + 3] = rng.next() as u8; |
| 84 | f[offs + 4] = rng.next() as u8; |
| 85 | } |
| 86 | } |
| 87 | let _ = extract_audio(&f); |
| 88 | let _ = DvVideoDecoder::new().decode(&f); |
| 89 | } |
| 90 | } |
| 91 | } |