| 1 | |
| 2 | |
| 3 | |
| 4 | use crate::common::{self, ffmpeg_raw_yuv}; |
| 5 | use ec_vp8::decode::Decoder; |
| 6 | |
| 7 | fn raw<'a>(pos: &mut usize, data: &'a [u8], len: usize) -> &'a [u8] { |
| 8 | let s = &data[*pos..*pos + len]; |
| 9 | *pos += len; |
| 10 | s |
| 11 | } |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | fn compare(name: &str, skip_if_missing: bool) { |
| 17 | let Some(dir) = common::fixture_dir() else { |
| 18 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 19 | }; |
| 20 | let path = dir.join(name); |
| 21 | if skip_if_missing && !path.exists() { |
| 22 | panic!("{name} missing (regenerate fixtures)"); |
| 23 | } |
| 24 | let bytes = std::fs::read(&path).unwrap(); |
| 25 | let (_, w, h, frames) = common::parse_ivf(&bytes); |
| 26 | let all = ffmpeg_raw_yuv(&path); |
| 27 | let frame_len = usize::from(w) * usize::from(h) * 3 / 2; |
| 28 | let shown: Vec<&common::IvfFrame> = |
| 29 | frames.iter().filter(|f| common::show_frame(&f.data)).collect(); |
| 30 | assert_eq!( |
| 31 | all.len(), |
| 32 | shown.len() * frame_len, |
| 33 | "{name}: ffmpeg output {}/{} not {} whole shown frames", |
| 34 | all.len(), |
| 35 | frame_len, |
| 36 | shown.len() |
| 37 | ); |
| 38 | |
| 39 | let mut dec = Decoder::new(); |
| 40 | let (cw, ch) = (usize::from(w + 1) / 2, usize::from(h + 1) / 2); |
| 41 | let mut shown_idx = 0; |
| 42 | for (i, f) in frames.iter().enumerate() { |
| 43 | match dec.decode(&f.data).expect("decode succeeds") { |
| 44 | Some(pic) => { |
| 45 | assert!(common::show_frame(&f.data), "{name} frame {i}: picture for hidden frame"); |
| 46 | assert_eq!(pic.width, w); |
| 47 | assert_eq!(pic.height, h); |
| 48 | let mut pos = shown_idx * frame_len; |
| 49 | shown_idx += 1; |
| 50 | let exp_y = raw(&mut pos, &all, usize::from(w) * usize::from(h)); |
| 51 | let exp_u = raw(&mut pos, &all, cw * ch); |
| 52 | let exp_v = raw(&mut pos, &all, cw * ch); |
| 53 | assert_eq!(pic.y, exp_y, "{name} frame {i}: luma mismatch"); |
| 54 | assert_eq!(pic.u, exp_u, "{name} frame {i}: U mismatch"); |
| 55 | assert_eq!(pic.v, exp_v, "{name} frame {i}: V mismatch"); |
| 56 | } |
| 57 | None => assert!( |
| 58 | !common::show_frame(&f.data), |
| 59 | "{name} frame {i}: shown frame produced no picture" |
| 60 | ), |
| 61 | } |
| 62 | } |
| 63 | assert_eq!(shown_idx, shown.len(), "{name}: shown picture count"); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn keyframes_match_ffmpeg() { |
| 68 | for (name, _, _) in [ |
| 69 | ("kf-160x96-q20.ivf", 160, 96), |
| 70 | ("kf-160x96-q40.ivf", 160, 96), |
| 71 | ("kf-76x52-q30.ivf", 76, 52), |
| 72 | ("kf-32x16-q35.ivf", 32, 16), |
| 73 | ("kf-172x144-q25.ivf", 172, 144), |
| 74 | ("kf-96x80-q63.ivf", 96, 80), |
| 75 | ] { |
| 76 | compare(name, false); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn multi_partition_keyframe_matches_ffmpeg() { |
| 82 | compare("mparts-160x96.ivf", false); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[test] |
| 91 | fn interframe_streams_match_ffmpeg() { |
| 92 | for name in [ |
| 93 | "gop-160x96.ivf", |
| 94 | "clip-obs-320x192.ivf", |
| 95 | "altref-160x96.ivf", |
| 96 | ] { |
| 97 | if name == "altref-160x96.ivf" { |
| 98 | let dir = common::fixture_dir().expect("fixtures dir"); |
| 99 | let bytes = std::fs::read(dir.join(name)).unwrap(); |
| 100 | let hidden = census_hidden(&bytes); |
| 101 | assert!( |
| 102 | hidden >= 6, |
| 103 | "{name}: only {hidden} hidden frames — regenerate with \ |
| 104 | scripts/gen_vp8_fixtures.sh (--auto-alt-ref=1 --passes=2)" |
| 105 | ); |
| 106 | } |
| 107 | compare(name, false); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | fn census_hidden(bytes: &[u8]) -> usize { |
| 113 | let (_, _, _, frames) = common::parse_ivf(bytes); |
| 114 | frames.iter().filter(|f| !common::show_frame(&f.data)).count() |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn vp8_in_webp_still_matches_ffmpeg() { |
| 119 | let Some(dir) = common::fixture_dir() else { |
| 120 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 121 | }; |
| 122 | let bytes = std::fs::read(dir.join("still.webp")).unwrap(); |
| 123 | let vp8 = common::webp_vp8_chunk(&bytes); |
| 124 | let reference = std::process::Command::new("ffmpeg") |
| 125 | .args(["-v", "error", "-i"]) |
| 126 | .arg(dir.join("still.webp")) |
| 127 | .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) |
| 128 | .output() |
| 129 | .unwrap(); |
| 130 | assert!(reference.status.success()); |
| 131 | let mut dec = Decoder::new(); |
| 132 | let pic = dec.decode(&vp8).expect("decode").expect("shown"); |
| 133 | assert_eq!( |
| 134 | pic.y.len() + pic.u.len() + pic.v.len(), |
| 135 | reference.stdout.len() |
| 136 | ); |
| 137 | let mut exp = &reference.stdout[..]; |
| 138 | assert_eq!(pic.y, &exp[..pic.y.len()]); |
| 139 | exp = &exp[pic.y.len()..]; |
| 140 | assert_eq!(pic.u, &exp[..pic.u.len()]); |
| 141 | exp = &exp[pic.u.len()..]; |
| 142 | assert_eq!(pic.v, exp); |
| 143 | } |