| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::common::{self, ffmpeg_raw_yuv}; |
| 6 | use ec_vp8::decode::Decoder; |
| 7 | use ec_vp8::stream::decode_stream; |
| 8 | |
| 9 | |
| 10 | |
| 11 | const FIXTURES: &[&str] = &[ |
| 12 | "kf-16x16-black-q0.ivf", |
| 13 | "kf-32x16-q35.ivf", |
| 14 | "kf-76x52-q30.ivf", |
| 15 | "kf-96x80-q63.ivf", |
| 16 | "kf-160x96-q20.ivf", |
| 17 | "kf-160x96-q40.ivf", |
| 18 | "kf-172x144-q25.ivf", |
| 19 | "gop-96x80-v1.ivf", |
| 20 | "gop-160x96.ivf", |
| 21 | "mparts-160x96.ivf", |
| 22 | "altref-160x96.ivf", |
| 23 | "clip-obs-320x192.ivf", |
| 24 | ]; |
| 25 | |
| 26 | #[test] |
| 27 | fn decode_stream_matches_ffmpeg_on_every_fixture() { |
| 28 | let Some(dir) = common::fixture_dir() else { |
| 29 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 30 | }; |
| 31 | for name in FIXTURES { |
| 32 | let path = dir.join(name); |
| 33 | let bytes = std::fs::read(&path).unwrap(); |
| 34 | let (_, w, h, frames) = common::parse_ivf(&bytes); |
| 35 | let all = ffmpeg_raw_yuv(&path); |
| 36 | let frame_len = usize::from(w) * usize::from(h) * 3 / 2; |
| 37 | let shown = frames.iter().filter(|f| common::show_frame(&f.data)).count(); |
| 38 | assert_eq!(all.len(), shown * frame_len, "{name}: ffmpeg output size"); |
| 39 | assert!(!frames[0].data.is_empty(), "{name}: empty first frame"); |
| 40 | |
| 41 | let pictures = |
| 42 | decode_stream(&bytes).unwrap_or_else(|e| panic!("{name}: decode failed: {e}")); |
| 43 | assert_eq!(pictures.len(), shown, "{name}: picture count (shown only)"); |
| 44 | for (i, pic) in pictures.iter().enumerate() { |
| 45 | let expect = &all[i * frame_len..(i + 1) * frame_len]; |
| 46 | let (cw, ch) = (usize::from(w + 1) / 2, usize::from(h + 1) / 2); |
| 47 | assert_eq!(pic.y, &expect[..usize::from(w) * usize::from(h)], "{name} frame {i}: Y"); |
| 48 | assert_eq!(pic.u, &expect[usize::from(w) * usize::from(h)..][..cw * ch], "{name} frame {i}: U"); |
| 49 | assert_eq!(pic.v, &expect[usize::from(w) * usize::from(h) + cw * ch..], "{name} frame {i}: V"); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | #[test] |
| 60 | fn hidden_frames_update_references_and_produce_no_picture() { |
| 61 | let Some(dir) = common::fixture_dir() else { |
| 62 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 63 | }; |
| 64 | let path = dir.join("altref-160x96.ivf"); |
| 65 | let bytes = std::fs::read(&path).unwrap(); |
| 66 | let (_, w, h, frames) = common::parse_ivf(&bytes); |
| 67 | let hidden_at: Vec<usize> = frames |
| 68 | .iter() |
| 69 | .enumerate() |
| 70 | .filter(|(_, f)| !common::show_frame(&f.data)) |
| 71 | .map(|(i, _)| i) |
| 72 | .collect(); |
| 73 | assert!( |
| 74 | hidden_at.len() >= 6, |
| 75 | "altref-160x96.ivf lost its hidden frames ({}) — regenerate", |
| 76 | hidden_at.len() |
| 77 | ); |
| 78 | |
| 79 | |
| 80 | let all = ffmpeg_raw_yuv(&path); |
| 81 | let frame_len = usize::from(w) * usize::from(h) * 3 / 2; |
| 82 | let mut dec = Decoder::new(); |
| 83 | let mut shown_seen = 0; |
| 84 | for (i, f) in frames.iter().enumerate() { |
| 85 | let pic = dec.decode(&f.data).expect("hidden frame decodes"); |
| 86 | if common::show_frame(&f.data) { |
| 87 | let expect = &all[shown_seen * frame_len..(shown_seen + 1) * frame_len]; |
| 88 | let y = &pic.as_ref().expect("shown frame has picture").y; |
| 89 | assert_eq!(y, &expect[..usize::from(w) * usize::from(h)], "frame {i}: Y"); |
| 90 | |
| 91 | |
| 92 | if i == hidden_at[0] + 1 { |
| 93 | assert!( |
| 94 | y[..32] == expect[..32] && y == &expect[..y.len()], |
| 95 | "frame after hidden altref diverges" |
| 96 | ); |
| 97 | } |
| 98 | shown_seen += 1; |
| 99 | } else { |
| 100 | assert!(pic.is_none(), "hidden frame {i} produced a picture"); |
| 101 | } |
| 102 | } |
| 103 | assert_eq!(shown_seen * frame_len, all.len()); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn decode_stream_is_error_not_panic_on_bad_containers() { |
| 108 | let Some(dir) = common::fixture_dir() else { |
| 109 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 110 | }; |
| 111 | let bytes = std::fs::read(dir.join("gop-160x96.ivf")).unwrap(); |
| 112 | |
| 113 | |
| 114 | let mut bad = bytes.clone(); |
| 115 | bad[0..4].copy_from_slice(b"JUNK"); |
| 116 | assert!(decode_stream(&bad).is_err(), "non-DKIF accepted"); |
| 117 | |
| 118 | |
| 119 | let mut bad = bytes.clone(); |
| 120 | bad[8..12].copy_from_slice(b"VP8L"); |
| 121 | assert!(decode_stream(&bad).is_err(), "non-VP80 fourcc accepted"); |
| 122 | |
| 123 | |
| 124 | let mut bad = bytes.clone(); |
| 125 | bad[32..36].copy_from_slice(&u32::MAX.to_le_bytes()); |
| 126 | assert!(decode_stream(&bad).is_err(), "truncated frame accepted"); |
| 127 | } |
| 128 | |