| 1 | use std::fs; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | use std::process::Command; |
| 4 | |
| 5 | use ec_hw::{ |
| 6 | Backend, EncCodec, EncoderConfig, FrameMetadata, H264Profile, HwEncoder, I420, RateControlMode, |
| 7 | }; |
| 8 | |
| 9 | const WIDTH: u32 = 640; |
| 10 | const HEIGHT: u32 = 360; |
| 11 | const FRAMES: usize = 30; |
| 12 | const FPS: u32 = 30; |
| 13 | const GOP: u32 = 15; |
| 14 | const FORCE_AT: usize = 7; |
| 15 | const BITRATE: u32 = 2_000_000; |
| 16 | const FIELDS: [&str; 7] = [ |
| 17 | "profile", |
| 18 | "width", |
| 19 | "height", |
| 20 | "color_space", |
| 21 | "color_transfer", |
| 22 | "color_primaries", |
| 23 | "color_range", |
| 24 | ]; |
| 25 | |
| 26 | fn config(profile: H264Profile) -> EncoderConfig { |
| 27 | EncoderConfig { |
| 28 | bitrate: BITRATE, |
| 29 | framerate: (FPS, 1), |
| 30 | gop_size: GOP, |
| 31 | rate_control: RateControlMode::ConstantBitrate, |
| 32 | ..EncoderConfig::new(EncCodec::H264, WIDTH, HEIGHT) |
| 33 | } |
| 34 | .h264_profile(profile) |
| 35 | .colour(6, 6, 6, true) |
| 36 | } |
| 37 | |
| 38 | fn frame(t: usize) -> I420 { |
| 39 | let (w, h) = (WIDTH as usize, HEIGHT as usize); |
| 40 | let y = (0..w * h) |
| 41 | .map(|i| ((i % w + i / w + 3 * t) & 0xff) as u8) |
| 42 | .collect(); |
| 43 | let chroma = w / 2 * h / 2; |
| 44 | I420 { |
| 45 | y, |
| 46 | u: vec![(96 + t % 64) as u8; chroma], |
| 47 | v: vec![160; chroma], |
| 48 | width: WIDTH, |
| 49 | height: HEIGHT, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn encode(dir: &Path, name: &str, profile: H264Profile, backend: Backend) -> Option<PathBuf> { |
| 54 | let mut encoder = match HwEncoder::open(config(profile), Some(backend)) { |
| 55 | Ok(encoder) => encoder, |
| 56 | Err(e) => { |
| 57 | println!("{name}: {e}"); |
| 58 | return None; |
| 59 | } |
| 60 | }; |
| 61 | let mut stream = Vec::new(); |
| 62 | for t in 0..FRAMES { |
| 63 | let meta = FrameMetadata { |
| 64 | timestamp: t as i64, |
| 65 | force_keyframe: t == FORCE_AT, |
| 66 | }; |
| 67 | let coded = encoder.encode(&frame(t), meta).expect("encode frame"); |
| 68 | stream.extend_from_slice(&coded.data); |
| 69 | } |
| 70 | let path = dir.join(format!("{name}.h264")); |
| 71 | fs::write(&path, stream).expect("write stream"); |
| 72 | Some(path) |
| 73 | } |
| 74 | |
| 75 | fn probe(path: &Path) -> Vec<(String, String)> { |
| 76 | let out = Command::new("ffprobe") |
| 77 | .args(["-v", "error", "-select_streams", "v:0", "-show_entries"]) |
| 78 | .arg(format!("stream={}", FIELDS.join(","))) |
| 79 | .args(["-of", "default=nw=1"]) |
| 80 | .arg(path) |
| 81 | .output() |
| 82 | .expect("run ffprobe"); |
| 83 | String::from_utf8_lossy(&out.stdout) |
| 84 | .lines() |
| 85 | .filter_map(|l| l.split_once('=')) |
| 86 | .map(|(k, v)| (k.to_string(), v.to_string())) |
| 87 | .collect() |
| 88 | } |
| 89 | |
| 90 | fn keyframes(path: &Path) -> Vec<usize> { |
| 91 | let out = Command::new("ffprobe") |
| 92 | .args([ |
| 93 | "-v", |
| 94 | "error", |
| 95 | "-select_streams", |
| 96 | "v:0", |
| 97 | "-show_entries", |
| 98 | "frame=key_frame", |
| 99 | ]) |
| 100 | .args(["-of", "csv=p=0"]) |
| 101 | .arg(path) |
| 102 | .output() |
| 103 | .expect("run ffprobe"); |
| 104 | String::from_utf8_lossy(&out.stdout) |
| 105 | .lines() |
| 106 | .enumerate() |
| 107 | .filter(|(_, l)| l.trim() == "1") |
| 108 | .map(|(i, _)| i) |
| 109 | .collect() |
| 110 | } |
| 111 | |
| 112 | fn value(info: &[(String, String)], key: &str) -> String { |
| 113 | info.iter() |
| 114 | .find(|(k, _)| k == key) |
| 115 | .map_or("-".to_string(), |(_, v)| v.clone()) |
| 116 | } |
| 117 | |
| 118 | fn main() { |
| 119 | let dir = PathBuf::from(std::env::args().nth(1).expect("scratch dir argument")); |
| 120 | for (label, profile) in [("main", H264Profile::Main), ("cb", H264Profile::ConstrainedBaseline)] { |
| 121 | compare(&dir, label, profile); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn compare(dir: &Path, label: &str, profile: H264Profile) { |
| 126 | let Some(vk) = encode(dir, &format!("vk_{label}"), profile, Backend::Vk) else { |
| 127 | return; |
| 128 | }; |
| 129 | let Some(va) = encode(dir, &format!("va_{label}"), profile, Backend::Va) else { |
| 130 | return; |
| 131 | }; |
| 132 | let (vk_info, va_info) = (probe(&vk), probe(&va)); |
| 133 | let mut same = true; |
| 134 | println!("{:<16}{:<16}{:<16}", "field", "vk", "va"); |
| 135 | for key in FIELDS { |
| 136 | let (a, b) = (value(&vk_info, key), value(&va_info, key)); |
| 137 | same &= a == b; |
| 138 | println!("{key:<16}{a:<16}{b:<16}"); |
| 139 | } |
| 140 | let (kv, ka) = (keyframes(&vk), keyframes(&va)); |
| 141 | same &= kv == ka; |
| 142 | let list = |k: &[usize]| k.iter().map(usize::to_string).collect::<Vec<_>>().join(","); |
| 143 | println!("{:<16}{:<16}{:<16}", "keyframes", list(&kv), list(&ka)); |
| 144 | println!("{label} match: {same}"); |
| 145 | } |