| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::process::Command; |
| 9 | |
| 10 | use ec_core::Error; |
| 11 | use ec_vp9_syntax::{FrameType, Vp9Parser, superframe}; |
| 12 | |
| 13 | fn bitstreams() -> PathBuf { |
| 14 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/bitstreams") |
| 15 | } |
| 16 | |
| 17 | |
| 18 | fn ivf_frames(data: &[u8]) -> Vec<&[u8]> { |
| 19 | let mut out = Vec::new(); |
| 20 | let mut pos = 32; |
| 21 | while pos + 12 <= data.len() { |
| 22 | let size = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize; |
| 23 | pos += 12; |
| 24 | if pos + size > data.len() { |
| 25 | break; |
| 26 | } |
| 27 | out.push(&data[pos..pos + size]); |
| 28 | pos += size; |
| 29 | } |
| 30 | out |
| 31 | } |
| 32 | |
| 33 | fn ffprobe(path: &Path, entries: &str) -> Vec<String> { |
| 34 | let out = Command::new("ffprobe") |
| 35 | .args(["-v", "error", "-select_streams", "v:0", "-show_entries"]) |
| 36 | .arg(entries) |
| 37 | .args(["-of", "csv=p=0"]) |
| 38 | .arg(path) |
| 39 | .output() |
| 40 | .expect("ffprobe runs"); |
| 41 | String::from_utf8_lossy(&out.stdout) |
| 42 | .lines() |
| 43 | .map(|l| l.trim_end_matches(',').to_string()) |
| 44 | .collect() |
| 45 | } |
| 46 | |
| 47 | |
| 48 | fn probe_stream(path: &Path) -> (u32, u32, String, u8) { |
| 49 | let line = ffprobe(path, "stream=profile,width,height,pix_fmt") |
| 50 | .into_iter() |
| 51 | .next() |
| 52 | .expect("one video stream"); |
| 53 | let f: Vec<&str> = line.split(',').collect(); |
| 54 | let profile = f[0] |
| 55 | .rsplit(' ') |
| 56 | .next() |
| 57 | .and_then(|p| p.parse().ok()) |
| 58 | .unwrap_or_else(|| panic!("unexpected VP9 profile {:?}", f[0])); |
| 59 | ( |
| 60 | f[1].parse().unwrap(), |
| 61 | f[2].parse().unwrap(), |
| 62 | f[3].to_string(), |
| 63 | profile, |
| 64 | ) |
| 65 | } |
| 66 | |
| 67 | |
| 68 | fn pix_fmt_geometry(pix_fmt: &str) -> (u8, u8, u8) { |
| 69 | let depth = if pix_fmt.contains("10le") { |
| 70 | 10 |
| 71 | } else if pix_fmt.contains("12le") { |
| 72 | 12 |
| 73 | } else { |
| 74 | 8 |
| 75 | }; |
| 76 | let (sx, sy) = if pix_fmt.starts_with("yuv444") { |
| 77 | (0, 0) |
| 78 | } else if pix_fmt.starts_with("yuv422") { |
| 79 | (1, 0) |
| 80 | } else { |
| 81 | (1, 1) |
| 82 | }; |
| 83 | (depth, sx, sy) |
| 84 | } |
| 85 | |
| 86 | fn fixtures() -> Vec<PathBuf> { |
| 87 | let dir = bitstreams(); |
| 88 | let Ok(entries) = std::fs::read_dir(&dir) else { |
| 89 | return Vec::new(); |
| 90 | }; |
| 91 | let mut files: Vec<PathBuf> = entries |
| 92 | .filter_map(|e| e.ok().map(|e| e.path())) |
| 93 | .filter(|p| { |
| 94 | p.extension().is_some_and(|e| e == "ivf") |
| 95 | && p.file_name() |
| 96 | .is_some_and(|n| n.to_string_lossy().starts_with("vp9-")) |
| 97 | }) |
| 98 | .collect(); |
| 99 | files.sort(); |
| 100 | files |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn every_fixture_matches_ffprobe() { |
| 105 | let files = fixtures(); |
| 106 | if files.is_empty() { |
| 107 | eprintln!( |
| 108 | "skipped: no fixtures/bitstreams/vp9-*.ivf — run scripts/gen-bitstream-fixtures.sh" |
| 109 | ); |
| 110 | return; |
| 111 | } |
| 112 | for path in files { |
| 113 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 114 | let (width, height, pix_fmt, profile) = probe_stream(&path); |
| 115 | let (bit_depth, sx, sy) = pix_fmt_geometry(&pix_fmt); |
| 116 | let ffprobe_keys: Vec<bool> = ffprobe(&path, "frame=key_frame") |
| 117 | .iter() |
| 118 | .map(|l| l == "1") |
| 119 | .collect(); |
| 120 | |
| 121 | let data = std::fs::read(&path).unwrap(); |
| 122 | let mut parser = Vp9Parser::new(); |
| 123 | let mut shown = Vec::new(); |
| 124 | let mut coded = 0usize; |
| 125 | let mut superframes = 0usize; |
| 126 | let mut show_existing = 0usize; |
| 127 | for chunk in ivf_frames(&data) { |
| 128 | let frames = superframe::split(chunk) |
| 129 | .unwrap_or_else(|e| panic!("{name}: superframe split: {e}")); |
| 130 | if frames.len() > 1 { |
| 131 | superframes += 1; |
| 132 | } |
| 133 | for frame in frames { |
| 134 | let h = parser |
| 135 | .parse_frame(frame) |
| 136 | .unwrap_or_else(|e| panic!("{name}: frame {coded}: {e}")); |
| 137 | coded += 1; |
| 138 | if h.show_existing_frame { |
| 139 | show_existing += 1; |
| 140 | shown.push(false); |
| 141 | continue; |
| 142 | } |
| 143 | assert_eq!(h.profile, profile, "{name}: profile"); |
| 144 | assert_eq!(h.bit_depth, bit_depth, "{name}: bit depth ({pix_fmt})"); |
| 145 | assert_eq!( |
| 146 | (h.subsampling_x, h.subsampling_y), |
| 147 | (sx, sy), |
| 148 | "{name}: subsampling" |
| 149 | ); |
| 150 | assert_eq!((h.width, h.height), (width, height), "{name}: coded size"); |
| 151 | assert_eq!( |
| 152 | (h.render_width, h.render_height), |
| 153 | (width, height), |
| 154 | "{name}: render size" |
| 155 | ); |
| 156 | assert!( |
| 157 | h.header_size_in_bytes > 0, |
| 158 | "{name}: empty compressed header" |
| 159 | ); |
| 160 | assert!( |
| 161 | (h.uncompressed_header_size as usize) < frame.len(), |
| 162 | "{name}: header longer than the frame" |
| 163 | ); |
| 164 | if name.contains("tiles") { |
| 165 | |
| 166 | |
| 167 | assert_eq!(h.tile_info.cols_log2, 2, "{name}: tile columns"); |
| 168 | } |
| 169 | if h.show_frame { |
| 170 | shown.push(h.frame_type == FrameType::Key); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | assert_eq!( |
| 176 | shown.len(), |
| 177 | ffprobe_keys.len(), |
| 178 | "{name}: shown frame count ({coded} coded, {superframes} superframes, {show_existing} show_existing)" |
| 179 | ); |
| 180 | assert_eq!(shown, ffprobe_keys, "{name}: key frame flags"); |
| 181 | println!( |
| 182 | "{name}: {coded} coded / {} shown, {superframes} superframes, {show_existing} show_existing, \ |
| 183 | profile {profile}, {bit_depth}-bit, {width}x{height}", |
| 184 | shown.len() |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | #[test] |
| 199 | fn altref_fixture_has_superframes_and_hidden_frames() { |
| 200 | let path = bitstreams().join("vp9-superframe-altref.ivf"); |
| 201 | if !path.exists() { |
| 202 | eprintln!("skipped: no vp9-superframe-altref.ivf"); |
| 203 | return; |
| 204 | } |
| 205 | let data = std::fs::read(&path).unwrap(); |
| 206 | let mut parser = Vp9Parser::new(); |
| 207 | let (mut superframes, mut hidden) = (0, 0); |
| 208 | for chunk in ivf_frames(&data) { |
| 209 | let frames = superframe::split(chunk).unwrap(); |
| 210 | if frames.len() > 1 { |
| 211 | superframes += 1; |
| 212 | } |
| 213 | for frame in frames { |
| 214 | let h = parser.parse_frame(frame).unwrap(); |
| 215 | if !h.show_frame && !h.show_existing_frame { |
| 216 | hidden += 1; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | assert!(superframes > 0, "libvpx altref did not produce superframes"); |
| 221 | assert!(hidden > 0, "no hidden ALTREF frame"); |
| 222 | |
| 223 | |
| 224 | let h = parser.parse_frame(&[0b1000_1101]).unwrap(); |
| 225 | assert!(h.show_existing_frame); |
| 226 | assert_eq!(h.frame_to_show_map_idx, 5); |
| 227 | assert_eq!(h.uncompressed_header_size, 1); |
| 228 | let slot = parser.reference_slots()[5]; |
| 229 | assert!(slot.valid, "slot 5 was never refreshed by the fixture"); |
| 230 | assert_eq!((h.width, h.height), (slot.width, slot.height)); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | #[test] |
| 235 | fn fuzz_10k_mutations_never_panics() { |
| 236 | let seeds: Vec<Vec<u8>> = fixtures() |
| 237 | .iter() |
| 238 | .take(3) |
| 239 | .filter_map(|p| std::fs::read(p).ok()) |
| 240 | .flat_map(|data| { |
| 241 | ivf_frames(&data) |
| 242 | .into_iter() |
| 243 | .take(4) |
| 244 | .map(|f| f[..f.len().min(512)].to_vec()) |
| 245 | .collect::<Vec<_>>() |
| 246 | }) |
| 247 | .collect(); |
| 248 | let seeds = if seeds.is_empty() { |
| 249 | |
| 250 | vec![vec![0x82u8; 128]] |
| 251 | } else { |
| 252 | seeds |
| 253 | }; |
| 254 | |
| 255 | let mut state = 0x2545_f491_4f6c_dd1du64; |
| 256 | let mut rng = move || { |
| 257 | state ^= state << 13; |
| 258 | state ^= state >> 7; |
| 259 | state ^= state << 17; |
| 260 | state |
| 261 | }; |
| 262 | let mut errors = 0usize; |
| 263 | for i in 0..10_000 { |
| 264 | let mut buf = seeds[(rng() as usize) % seeds.len()].clone(); |
| 265 | let mutations = 1 + (rng() as usize) % 8; |
| 266 | for _ in 0..mutations { |
| 267 | let at = (rng() as usize) % buf.len(); |
| 268 | buf[at] ^= (rng() % 256) as u8; |
| 269 | } |
| 270 | if i % 3 == 0 { |
| 271 | buf.truncate((rng() as usize) % buf.len().max(1)); |
| 272 | } |
| 273 | let mut parser = Vp9Parser::new(); |
| 274 | for frame in superframe::split(&buf).unwrap_or_else(|_| vec![&buf]) { |
| 275 | match parser.parse_frame(frame) { |
| 276 | Ok(_) => {} |
| 277 | Err(Error::NeedMore | Error::Corrupt { .. }) => errors += 1, |
| 278 | Err(e) => panic!("unexpected error kind: {e}"), |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | println!("fuzz: 10000 mutations, {errors} rejected"); |
| 283 | } |