| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use crate::reallib; |
| 12 | |
| 13 | use std::io::Read; |
| 14 | use std::path::Path; |
| 15 | |
| 16 | use ec_vp8::decode::Decoder as Vp8Decoder; |
| 17 | use ec_vp9::decode::Decoder as Vp9Decoder; |
| 18 | use engine::demux::Demuxer; |
| 19 | use reallib::{ffmpeg_pipe, fill, have}; |
| 20 | |
| 21 | |
| 22 | fn i420<T: Copy>( |
| 23 | y: &[T], |
| 24 | u: &[T], |
| 25 | v: &[T], |
| 26 | (w, h): (usize, usize), |
| 27 | (stride, uv_stride): (usize, usize), |
| 28 | ) -> Vec<u8> |
| 29 | where |
| 30 | u16: From<T>, |
| 31 | { |
| 32 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 33 | let mut out = Vec::with_capacity(w * h + 2 * cw * ch); |
| 34 | let mut plane = |src: &[T], pitch: usize, pw: usize, ph: usize| { |
| 35 | for row in 0..ph { |
| 36 | out.extend( |
| 37 | src[row * pitch..row * pitch + pw] |
| 38 | .iter() |
| 39 | .map(|s| u16::from(*s) as u8), |
| 40 | ); |
| 41 | } |
| 42 | }; |
| 43 | plane(y, stride, w, h); |
| 44 | plane(u, uv_stride, cw, ch); |
| 45 | plane(v, uv_stride, cw, ch); |
| 46 | out |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | fn compare_with_ffmpeg( |
| 52 | key: &str, |
| 53 | path: &Path, |
| 54 | mut next: impl FnMut() -> Option<(Vec<u8>, (usize, usize))>, |
| 55 | ) -> usize { |
| 56 | let (mut child, mut ff) = ffmpeg_pipe( |
| 57 | &[ |
| 58 | "-map", |
| 59 | "0:v:0", |
| 60 | "-f", |
| 61 | "rawvideo", |
| 62 | "-pix_fmt", |
| 63 | "yuv420p", |
| 64 | "-fps_mode", |
| 65 | "passthrough", |
| 66 | "-", |
| 67 | ], |
| 68 | path, |
| 69 | ); |
| 70 | let mut frames = 0usize; |
| 71 | let mut want = Vec::new(); |
| 72 | while let Some((got, (w, h))) = next() { |
| 73 | let size = w * h + 2 * w.div_ceil(2) * h.div_ceil(2); |
| 74 | want.resize(size, 0); |
| 75 | let n = fill(&mut ff, &mut want); |
| 76 | assert_eq!( |
| 77 | n, size, |
| 78 | "{key}: ffmpeg ended before frame {frames} ({n} of {size} bytes)" |
| 79 | ); |
| 80 | assert!( |
| 81 | got == want, |
| 82 | "{key}: frame {frames} ({w}x{h}) differs from ffmpeg's rawvideo" |
| 83 | ); |
| 84 | frames += 1; |
| 85 | } |
| 86 | let mut rest = Vec::new(); |
| 87 | ff.read_to_end(&mut rest).expect("drain ffmpeg"); |
| 88 | assert!( |
| 89 | rest.is_empty(), |
| 90 | "{key}: ffmpeg has {} bytes of frames ec did not decode", |
| 91 | rest.len() |
| 92 | ); |
| 93 | let status = child.wait().expect("wait for ffmpeg"); |
| 94 | assert!(status.success(), "{key}: ffmpeg failed"); |
| 95 | frames |
| 96 | } |
| 97 | |
| 98 | fn vp8_frames(path: &Path) -> impl FnMut() -> Option<(Vec<u8>, (usize, usize))> { |
| 99 | let (_, mut demuxer) = Demuxer::open(path).expect("open the VP8 file"); |
| 100 | let mut decoder = Vp8Decoder::new(); |
| 101 | move || loop { |
| 102 | let au = demuxer.next_access_unit().expect("demux")?; |
| 103 | if let Some(pic) = decoder.decode(&au).expect("ec-vp8 decode") { |
| 104 | let dims = (usize::from(pic.width), usize::from(pic.height)); |
| 105 | let bytes = i420(&pic.y, &pic.u, &pic.v, dims, (pic.stride, pic.uv_stride)); |
| 106 | return Some((bytes, dims)); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | fn vp9_frames(path: &Path) -> impl FnMut() -> Option<(Vec<u8>, (usize, usize))> { |
| 112 | let (_, mut demuxer) = Demuxer::open(path).expect("open the VP9 file"); |
| 113 | let mut decoder = Vp9Decoder::new(); |
| 114 | move || loop { |
| 115 | let au = demuxer.next_access_unit().expect("demux")?; |
| 116 | if let Some(pic) = decoder.decode(&au).expect("ec-vp9 decode") { |
| 117 | let dims = (usize::from(pic.width), usize::from(pic.height)); |
| 118 | let bytes = i420(&pic.y, &pic.u, &pic.v, dims, (pic.stride, pic.uv_stride)); |
| 119 | return Some((bytes, dims)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | #[test] |
| 127 | fn real_vp8_webm_frames_match_ffmpeg() { |
| 128 | let Some(path) = engine::real_library::film("vp8_webm") else { |
| 129 | return; |
| 130 | }; |
| 131 | if !have("ffmpeg") { |
| 132 | eprintln!("skipped: no ffmpeg to decode the reference frames with"); |
| 133 | return; |
| 134 | } |
| 135 | let frames = compare_with_ffmpeg("vp8_webm", &path, vp8_frames(&path)); |
| 136 | eprintln!("vp8_webm: {frames} frames byte-identical to ffmpeg's rawvideo"); |
| 137 | assert!(frames > 0, "no frames decoded"); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | #[test] |
| 143 | fn real_vp9_opus_mp4_frames_match_ffmpeg() { |
| 144 | let Some(path) = engine::real_library::film("vp9_opus_mp4") else { |
| 145 | return; |
| 146 | }; |
| 147 | if !have("ffmpeg") { |
| 148 | eprintln!("skipped: no ffmpeg to decode the reference frames with"); |
| 149 | return; |
| 150 | } |
| 151 | let frames = compare_with_ffmpeg("vp9_opus_mp4", &path, vp9_frames(&path)); |
| 152 | eprintln!("vp9_opus_mp4: {frames} frames byte-identical to ffmpeg's rawvideo"); |
| 153 | assert!(frames > 0, "no frames decoded"); |
| 154 | } |