| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::ivf; |
| 6 | |
| 7 | use ec_vp9::decode::Decoder; |
| 8 | |
| 9 | |
| 10 | |
| 11 | fn ensure_fixture(path: &std::path::Path) { |
| 12 | if path.exists() { |
| 13 | return; |
| 14 | } |
| 15 | if let Some(dir) = path.parent() { |
| 16 | std::fs::create_dir_all(dir).unwrap(); |
| 17 | } |
| 18 | let st = std::process::Command::new("ffmpeg") |
| 19 | .args([ |
| 20 | "-y", |
| 21 | "-f", |
| 22 | "lavfi", |
| 23 | "-i", |
| 24 | "testsrc2=size=320x240:rate=30:duration=1", |
| 25 | "-pix_fmt", |
| 26 | "yuv420p", |
| 27 | "-c:v", |
| 28 | "libvpx-vp9", |
| 29 | "-g", |
| 30 | "1", |
| 31 | "-cpu-used", |
| 32 | "4", |
| 33 | "-deadline", |
| 34 | "realtime", |
| 35 | "-frames:v", |
| 36 | "2", |
| 37 | ]) |
| 38 | .arg(path) |
| 39 | .output() |
| 40 | .expect("ffmpeg with libvpx-vp9 must be on PATH for the witnesses"); |
| 41 | assert!(st.status.success(), "fixture generation failed: {st:?}"); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec<u8> { |
| 46 | let out = std::process::Command::new("ffmpeg") |
| 47 | .args(["-v", "error", "-i"]) |
| 48 | .arg(path) |
| 49 | .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) |
| 50 | .output() |
| 51 | .expect("ffmpeg must be on PATH for the witnesses"); |
| 52 | assert!(out.status.success(), "ffmpeg failed on {path:?}"); |
| 53 | out.stdout |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | fn compare(path: &std::path::Path) { |
| 59 | let bytes = std::fs::read(path).unwrap(); |
| 60 | let (_fourcc, _w, _h, frames) = ivf::parse_ivf(&bytes); |
| 61 | let reference = ffmpeg_raw_yuv(path); |
| 62 | |
| 63 | let mut decoder = Decoder::new(); |
| 64 | let mut ref_pos = 0usize; |
| 65 | let mut shown = 0usize; |
| 66 | for frame in &frames { |
| 67 | let pic = decoder.decode(&frame.data).expect("decode must succeed"); |
| 68 | if let Some(pic) = pic { |
| 69 | let (w, h) = (pic.width as usize, pic.height as usize); |
| 70 | assert_eq!(w, 320, "fixture width"); |
| 71 | assert_eq!(h, 240, "fixture height"); |
| 72 | let uv = (w / 2) * (h / 2); |
| 73 | let fr = &reference[ref_pos..ref_pos + w * h + 2 * uv]; |
| 74 | ref_pos += w * h + 2 * uv; |
| 75 | let want_y = &fr[..w * h]; |
| 76 | let want_u = &fr[w * h..w * h + uv]; |
| 77 | let want_v = &fr[w * h + uv..]; |
| 78 | assert_eq!(pic.y.len(), want_y.len()); |
| 79 | assert_eq!(pic.u.len(), want_u.len()); |
| 80 | assert_eq!(pic.v.len(), want_v.len()); |
| 81 | for (i, (&a, &b)) in pic.y.iter().zip(want_y).enumerate() { |
| 82 | assert_eq!( |
| 83 | a as u8, |
| 84 | b, |
| 85 | "Y plane mismatch at pixel ({}, {}) frame {shown}", |
| 86 | i % w, |
| 87 | i / w |
| 88 | ); |
| 89 | } |
| 90 | for (i, (&a, &b)) in pic.u.iter().zip(want_u).enumerate() { |
| 91 | assert_eq!( |
| 92 | a as u8, |
| 93 | b, |
| 94 | "U plane mismatch at ({}, {}) frame {shown}", |
| 95 | i % (w / 2), |
| 96 | i / (w / 2) |
| 97 | ); |
| 98 | } |
| 99 | for (i, (&a, &b)) in pic.v.iter().zip(want_v).enumerate() { |
| 100 | assert_eq!( |
| 101 | a as u8, |
| 102 | b, |
| 103 | "V plane mismatch at ({}, {}) frame {shown}", |
| 104 | i % (w / 2), |
| 105 | i / (w / 2) |
| 106 | ); |
| 107 | } |
| 108 | shown += 1; |
| 109 | } |
| 110 | } |
| 111 | assert_eq!(shown, 2, "both keyframes are shown"); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn keyframes_match_ffmpeg() { |
| 116 | let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vp9"); |
| 117 | let path = dir.join("key-320.ivf"); |
| 118 | ensure_fixture(&path); |
| 119 | compare(&path); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn lossless_64_matches_ffmpeg() { |
| 124 | let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); |
| 125 | let path = dir.join("fixtures/vp9/lossless-64.ivf"); |
| 126 | let bytes = std::fs::read(&path).unwrap(); |
| 127 | let (_fourcc, w, h, frames) = ivf::parse_ivf(&bytes); |
| 128 | let reference = ffmpeg_raw_yuv(&path); |
| 129 | let mut decoder = Decoder::new(); |
| 130 | let pic = decoder |
| 131 | .decode(&frames[0].data) |
| 132 | .expect("decode must succeed") |
| 133 | .expect("frame is shown"); |
| 134 | let (w, h) = (w as usize, h as usize); |
| 135 | let uv = (w / 2) * (h / 2); |
| 136 | let want_y = &reference[..w * h]; |
| 137 | let want_u = &reference[w * h..w * h + uv]; |
| 138 | let want_v = &reference[w * h + uv..]; |
| 139 | assert!( |
| 140 | pic.y.iter().map(|&v| v as u8).eq(want_y.iter().copied()), |
| 141 | "lossless Y must be byte-exact" |
| 142 | ); |
| 143 | assert!( |
| 144 | pic.u.iter().map(|&v| v as u8).eq(want_u.iter().copied()), |
| 145 | "lossless U must be byte-exact" |
| 146 | ); |
| 147 | assert!( |
| 148 | pic.v.iter().map(|&v| v as u8).eq(want_v.iter().copied()), |
| 149 | "lossless V must be byte-exact" |
| 150 | ); |
| 151 | assert_eq!((pic.width, pic.height), (w as u16, h as u16)); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn inter_stream_decodes_and_matches_ffmpeg() { |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | let Some(dir) = ivf::fixture_dir() else { |
| 161 | return; |
| 162 | }; |
| 163 | let path = dir.join("vp9-superframe-altref.ivf"); |
| 164 | let bytes = std::fs::read(&path).unwrap(); |
| 165 | let (_, w, h, frames) = ivf::parse_ivf(&bytes); |
| 166 | let (w, h) = (w as usize, h as usize); |
| 167 | let expected = ffmpeg_raw_yuv(&path); |
| 168 | let frame_bytes = w * h + 2 * (w / 2) * (h / 2); |
| 169 | let mut decoder = Decoder::new(); |
| 170 | let mut shown = 0usize; |
| 171 | let mut hidden = 0usize; |
| 172 | for (i, frame) in frames.iter().enumerate() { |
| 173 | match decoder.decode(&frame.data).expect("every frame decodes") { |
| 174 | Some(pic) => { |
| 175 | let off = shown * frame_bytes; |
| 176 | let mut got = Vec::with_capacity(frame_bytes); |
| 177 | got.extend(pic.y.iter().map(|&v| v as u8)); |
| 178 | got.extend(pic.u.iter().map(|&v| v as u8)); |
| 179 | got.extend(pic.v.iter().map(|&v| v as u8)); |
| 180 | assert_eq!( |
| 181 | got, |
| 182 | expected[off..off + frame_bytes], |
| 183 | "frame {i}: pixel mismatch" |
| 184 | ); |
| 185 | shown += 1; |
| 186 | } |
| 187 | None => hidden += 1, |
| 188 | } |
| 189 | } |
| 190 | assert!(shown >= 2, "the stream shows frames"); |
| 191 | |
| 192 | |
| 193 | assert_eq!(hidden, 0, "superframe chunks show exactly one frame each"); |
| 194 | assert_eq!(shown * frame_bytes, expected.len()); |
| 195 | } |