ec-hw golden_hw: CQP enc H.264/HEVC/AV1 on Vk+VA, i420/i420_16/rgba/bgra, encode+submit; H.264 CBR sizes; decode h264/hevc/vp9/av1 fixtures
Browse files at this commit74a46ce6cb040a0f1fe4969e8fb034207c2e6f7dParent: 2f8f1003 changed files +386 -0
- Cargo.lock +1 -0
- crates/ec-hw/Cargo.toml +1 -0
- crates/ec-hw/examples/golden_hw.rs +384 -0
Cargo.lockmodified+1 -0
| @@ -597,6 +597,7 @@ dependencies = [ | ||
| 597 | 597 | "ec-va", |
| 598 | 598 | "ec-vk", |
| 599 | 599 | "ec-vp9-syntax", |
| 600 | + "sha2", | |
| 600 | 601 | ] |
| 601 | 602 | |
| 602 | 603 | [[package]] |
crates/ec-hw/Cargo.tomlmodified+1 -0
| @@ -21,3 +21,4 @@ ec-va.workspace = true | ||
| 21 | 21 | |
| 22 | 22 | [dev-dependencies] |
| 23 | 23 | ec-testkit.workspace = true |
| 24 | +sha2 = "0.11.3" |
crates/ec-hw/examples/golden_hw.rsadded+384 -0
| @@ -0,0 +1,384 @@ | ||
| 1 | +use std::fs; | |
| 2 | +use std::panic::{self, AssertUnwindSafe}; | |
| 3 | + | |
| 4 | +use ec_hw::{ | |
| 5 | + Backend, Capability, Codec, CodecProfile, CodedFrame, Direction, EncCodec, EncodeInput, | |
| 6 | + EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, HwPicture, I420, I420_16, RateControlMode, | |
| 7 | + RgbFrame, RgbOrder, capabilities, | |
| 8 | +}; | |
| 9 | +use sha2::{Digest, Sha256}; | |
| 10 | + | |
| 11 | +type Outcome = Result<Vec<String>, String>; | |
| 12 | + | |
| 13 | +const FRAMES: usize = 8; | |
| 14 | +const SIZES: [(usize, usize); 2] = [(320, 240), (322, 242)]; | |
| 15 | +const BACKENDS: [Backend; 2] = [Backend::Vk, Backend::Va]; | |
| 16 | +const APIS: [&str; 2] = ["encode", "submit"]; | |
| 17 | +const ENCODERS: [(&str, Codec, EncCodec, u32); 3] = [ | |
| 18 | + ("h264", Codec::H264, EncCodec::H264, 27), | |
| 19 | + ("hevc", Codec::H265, EncCodec::H265, 27), | |
| 20 | + ("av1", Codec::Av1, EncCodec::Av1, 135), | |
| 21 | +]; | |
| 22 | +const FORMATS: [Fmt; 4] = [Fmt::Yuv8, Fmt::Yuv10, Fmt::Rgba, Fmt::Bgra]; | |
| 23 | +const DECODERS: [(&str, Codec, &str); 8] = [ | |
| 24 | + ("h264", Codec::H264, "bitstreams/bf1280.h264"), | |
| 25 | + ("h264", Codec::H264, "bitstreams/h264-bframes-320.264"), | |
| 26 | + ("hevc", Codec::H265, "bitstreams/h265idr720.hevc"), | |
| 27 | + ("hevc", Codec::H265, "bitstreams/hevc-bframes-320.265"), | |
| 28 | + ("vp9", Codec::Vp9, "bitstreams/vp9_640.ivf"), | |
| 29 | + ("vp9", Codec::Vp9, "bitstreams/vp9_720.ivf"), | |
| 30 | + ("av1", Codec::Av1, "bitstreams/av1aom720.ivf"), | |
| 31 | + ("av1", Codec::Av1, "bitstreams/av1-altref.ivf"), | |
| 32 | +]; | |
| 33 | + | |
| 34 | +#[derive(Clone, Copy)] | |
| 35 | +enum Fmt { | |
| 36 | + Yuv8, | |
| 37 | + Yuv10, | |
| 38 | + Rgba, | |
| 39 | + Bgra, | |
| 40 | +} | |
| 41 | + | |
| 42 | +impl Fmt { | |
| 43 | + fn name(self) -> &'static str { | |
| 44 | + match self { | |
| 45 | + Fmt::Yuv8 => "i420", | |
| 46 | + Fmt::Yuv10 => "i420_16", | |
| 47 | + Fmt::Rgba => "rgba", | |
| 48 | + Fmt::Bgra => "bgra", | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + fn order(self) -> RgbOrder { | |
| 53 | + match self { | |
| 54 | + Fmt::Bgra => RgbOrder::Bgra, | |
| 55 | + _ => RgbOrder::Rgba, | |
| 56 | + } | |
| 57 | + } | |
| 58 | +} | |
| 59 | + | |
| 60 | +fn backend_name(backend: Backend) -> &'static str { | |
| 61 | + match backend { | |
| 62 | + Backend::Vk => "vk", | |
| 63 | + Backend::Va => "va", | |
| 64 | + } | |
| 65 | +} | |
| 66 | + | |
| 67 | +fn sha_hex(data: &[u8]) -> String { | |
| 68 | + Sha256::digest(data) | |
| 69 | + .iter() | |
| 70 | + .map(|b| format!("{b:02x}")) | |
| 71 | + .collect() | |
| 72 | +} | |
| 73 | + | |
| 74 | +fn is_ten_bit(profile: CodecProfile) -> bool { | |
| 75 | + matches!(profile, CodecProfile::H265Main10 | CodecProfile::Av1Main10) | |
| 76 | +} | |
| 77 | + | |
| 78 | +fn supported(caps: &[Capability], backend: Backend, codec: Codec, ten: bool) -> bool { | |
| 79 | + caps.iter().any(|c| { | |
| 80 | + c.backend == backend | |
| 81 | + && c.codec == codec | |
| 82 | + && c.direction == Direction::Encode | |
| 83 | + && (!ten || is_ten_bit(c.profile)) | |
| 84 | + }) | |
| 85 | +} | |
| 86 | + | |
| 87 | +fn in_square(col: usize, row: usize, t: usize, w: usize, h: usize) -> bool { | |
| 88 | + let x = (t * 12) % (w - 40); | |
| 89 | + let y = (t * 7) % (h - 40); | |
| 90 | + (x..x + 40).contains(&col) && (y..y + 40).contains(&row) | |
| 91 | +} | |
| 92 | + | |
| 93 | +struct Planes { | |
| 94 | + y: Vec<u16>, | |
| 95 | + u: Vec<u16>, | |
| 96 | + v: Vec<u16>, | |
| 97 | +} | |
| 98 | + | |
| 99 | +fn planes10(w: usize, h: usize, t: usize) -> Planes { | |
| 100 | + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); | |
| 101 | + let mut y = Vec::with_capacity(w * h); | |
| 102 | + for row in 0..h { | |
| 103 | + for col in 0..w { | |
| 104 | + let value = if in_square(col, row, t, w, h) { | |
| 105 | + 940 | |
| 106 | + } else { | |
| 107 | + ((col * 1023 / w + row * 1023 / h) / 2 + t * 5) & 1023 | |
| 108 | + }; | |
| 109 | + y.push(value as u16); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + let mut u = Vec::with_capacity(cw * ch); | |
| 113 | + let mut v = Vec::with_capacity(cw * ch); | |
| 114 | + for row in 0..ch { | |
| 115 | + for col in 0..cw { | |
| 116 | + u.push(((col * 1023 / cw + t * 3) & 1023) as u16); | |
| 117 | + v.push(((row * 1023 / ch + t * 2) & 1023) as u16); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + Planes { y, u, v } | |
| 121 | +} | |
| 122 | + | |
| 123 | +fn i420_frame(w: usize, h: usize, t: usize) -> I420 { | |
| 124 | + let p = planes10(w, h, t); | |
| 125 | + I420 { | |
| 126 | + y: p.y.iter().map(|&s| (s >> 2) as u8).collect(), | |
| 127 | + u: p.u.iter().map(|&s| (s >> 2) as u8).collect(), | |
| 128 | + v: p.v.iter().map(|&s| (s >> 2) as u8).collect(), | |
| 129 | + width: w as u32, | |
| 130 | + height: h as u32, | |
| 131 | + } | |
| 132 | +} | |
| 133 | + | |
| 134 | +fn i420_16_frame(w: usize, h: usize, t: usize) -> I420_16 { | |
| 135 | + let p = planes10(w, h, t); | |
| 136 | + I420_16 { | |
| 137 | + y: p.y, | |
| 138 | + u: p.u, | |
| 139 | + v: p.v, | |
| 140 | + width: w as u32, | |
| 141 | + height: h as u32, | |
| 142 | + bit_depth: 10, | |
| 143 | + } | |
| 144 | +} | |
| 145 | + | |
| 146 | +fn rgb_pixels(w: usize, h: usize, t: usize, order: RgbOrder) -> Vec<u8> { | |
| 147 | + let mut out = Vec::with_capacity(w * h * 4); | |
| 148 | + for row in 0..h { | |
| 149 | + for col in 0..w { | |
| 150 | + let (r, g, b) = if in_square(col, row, t, w, h) { | |
| 151 | + (235, 16, 128) | |
| 152 | + } else { | |
| 153 | + ( | |
| 154 | + (col * 255 / w + t * 3) & 255, | |
| 155 | + (row * 255 / h) & 255, | |
| 156 | + (col + row + t * 5) & 255, | |
| 157 | + ) | |
| 158 | + }; | |
| 159 | + let pixel = match order { | |
| 160 | + RgbOrder::Bgra => [b, g, r, 0], | |
| 161 | + RgbOrder::Rgba => [r, g, b, 0], | |
| 162 | + }; | |
| 163 | + out.extend(pixel.map(|x| x as u8)); | |
| 164 | + } | |
| 165 | + } | |
| 166 | + out | |
| 167 | +} | |
| 168 | + | |
| 169 | +fn rgb_frame(pixels: &[u8], w: usize, h: usize, order: RgbOrder) -> RgbFrame<'_> { | |
| 170 | + RgbFrame { | |
| 171 | + data: pixels, | |
| 172 | + width: w as u32, | |
| 173 | + height: h as u32, | |
| 174 | + stride: (w * 4) as u32, | |
| 175 | + order, | |
| 176 | + } | |
| 177 | +} | |
| 178 | + | |
| 179 | +fn meta(t: usize) -> FrameMetadata { | |
| 180 | + FrameMetadata { | |
| 181 | + timestamp: t as i64, | |
| 182 | + force_keyframe: false, | |
| 183 | + } | |
| 184 | +} | |
| 185 | + | |
| 186 | +fn encode_plain( | |
| 187 | + enc: &mut HwEncoder, | |
| 188 | + fmt: Fmt, | |
| 189 | + w: usize, | |
| 190 | + h: usize, | |
| 191 | +) -> ec_hw::Result<Vec<CodedFrame>> { | |
| 192 | + let mut out = Vec::new(); | |
| 193 | + for t in 0..FRAMES { | |
| 194 | + let coded = match fmt { | |
| 195 | + Fmt::Yuv8 => enc.encode(&i420_frame(w, h, t), meta(t))?, | |
| 196 | + Fmt::Yuv10 => enc.encode_16(&i420_16_frame(w, h, t), meta(t))?, | |
| 197 | + Fmt::Rgba | Fmt::Bgra => { | |
| 198 | + let pixels = rgb_pixels(w, h, t, fmt.order()); | |
| 199 | + enc.encode_rgb(&rgb_frame(&pixels, w, h, fmt.order()), meta(t))? | |
| 200 | + } | |
| 201 | + }; | |
| 202 | + out.push(coded); | |
| 203 | + } | |
| 204 | + Ok(out) | |
| 205 | +} | |
| 206 | + | |
| 207 | +fn encode_submit( | |
| 208 | + enc: &mut HwEncoder, | |
| 209 | + fmt: Fmt, | |
| 210 | + w: usize, | |
| 211 | + h: usize, | |
| 212 | +) -> ec_hw::Result<Vec<CodedFrame>> { | |
| 213 | + let mut out = Vec::new(); | |
| 214 | + for t in 0..FRAMES { | |
| 215 | + let coded = match fmt { | |
| 216 | + Fmt::Yuv8 => enc.submit(EncodeInput::I420(&i420_frame(w, h, t)), meta(t))?, | |
| 217 | + Fmt::Yuv10 => enc.submit(EncodeInput::I420_16(&i420_16_frame(w, h, t)), meta(t))?, | |
| 218 | + Fmt::Rgba | Fmt::Bgra => { | |
| 219 | + let pixels = rgb_pixels(w, h, t, fmt.order()); | |
| 220 | + enc.submit( | |
| 221 | + EncodeInput::Rgb(&rgb_frame(&pixels, w, h, fmt.order())), | |
| 222 | + meta(t), | |
| 223 | + )? | |
| 224 | + } | |
| 225 | + }; | |
| 226 | + out.extend(coded); | |
| 227 | + } | |
| 228 | + out.extend(enc.flush()?); | |
| 229 | + Ok(out) | |
| 230 | +} | |
| 231 | + | |
| 232 | +fn encode_case( | |
| 233 | + label: &str, | |
| 234 | + backend: Backend, | |
| 235 | + codec: EncCodec, | |
| 236 | + qp: u32, | |
| 237 | + fmt: Fmt, | |
| 238 | + (w, h): (usize, usize), | |
| 239 | + api: &str, | |
| 240 | +) -> Outcome { | |
| 241 | + let mut config = EncoderConfig::new(codec, w as u32, h as u32); | |
| 242 | + config.rate_control = RateControlMode::ConstantQp { qp }; | |
| 243 | + if matches!(fmt, Fmt::Yuv10) { | |
| 244 | + config = config.ten_bit(); | |
| 245 | + } | |
| 246 | + let mut enc = HwEncoder::open(config, Some(backend)).map_err(|e| e.to_string())?; | |
| 247 | + let coded = if api == "encode" { | |
| 248 | + encode_plain(&mut enc, fmt, w, h) | |
| 249 | + } else { | |
| 250 | + encode_submit(&mut enc, fmt, w, h) | |
| 251 | + } | |
| 252 | + .map_err(|e| e.to_string())?; | |
| 253 | + Ok(coded | |
| 254 | + .iter() | |
| 255 | + .map(|c| format!("{label}/{}\t{}", c.timestamp, sha_hex(&c.data))) | |
| 256 | + .collect()) | |
| 257 | +} | |
| 258 | + | |
| 259 | +fn cbr_case(label: &str, backend: Backend) -> Outcome { | |
| 260 | + let mut config = EncoderConfig::new(EncCodec::H264, 320, 240); | |
| 261 | + config.rate_control = RateControlMode::ConstantBitrate; | |
| 262 | + config.bitrate = 2_000_000; | |
| 263 | + let mut enc = HwEncoder::open(config, Some(backend)).map_err(|e| e.to_string())?; | |
| 264 | + let mut sizes = Vec::new(); | |
| 265 | + for t in 0..30 { | |
| 266 | + let coded = enc | |
| 267 | + .encode(&i420_frame(320, 240, t), meta(t)) | |
| 268 | + .map_err(|e| e.to_string())?; | |
| 269 | + sizes.push(coded.data.len().to_string()); | |
| 270 | + } | |
| 271 | + Ok(vec![format!( | |
| 272 | + "{label}\t{}", | |
| 273 | + sha_hex(sizes.join(",").as_bytes()) | |
| 274 | + )]) | |
| 275 | +} | |
| 276 | + | |
| 277 | +fn ivf_units(ivf: &[u8]) -> Vec<&[u8]> { | |
| 278 | + let mut pos = usize::from(u16::from_le_bytes([ivf[6], ivf[7]])); | |
| 279 | + let mut units = Vec::new(); | |
| 280 | + while pos + 12 <= ivf.len() { | |
| 281 | + let len = u32::from_le_bytes(ivf[pos..pos + 4].try_into().expect("frame size")) as usize; | |
| 282 | + units.push(&ivf[pos + 12..(pos + 12 + len).min(ivf.len())]); | |
| 283 | + pos += 12 + len; | |
| 284 | + } | |
| 285 | + units | |
| 286 | +} | |
| 287 | + | |
| 288 | +fn access_units(codec: Codec, data: &[u8]) -> Vec<&[u8]> { | |
| 289 | + match codec { | |
| 290 | + Codec::H264 | Codec::H265 => vec![data], | |
| 291 | + Codec::Vp9 | Codec::Av1 => ivf_units(data), | |
| 292 | + } | |
| 293 | +} | |
| 294 | + | |
| 295 | +fn picture_bytes(picture: &HwPicture) -> Result<Vec<u8>, String> { | |
| 296 | + if picture.bit_depth > 8 { | |
| 297 | + let f = picture.to_i420_16().map_err(|e| e.to_string())?; | |
| 298 | + Ok([f.y, f.u, f.v] | |
| 299 | + .concat() | |
| 300 | + .iter() | |
| 301 | + .flat_map(|s| s.to_le_bytes()) | |
| 302 | + .collect()) | |
| 303 | + } else { | |
| 304 | + let f = picture.to_i420().map_err(|e| e.to_string())?; | |
| 305 | + Ok([f.y, f.u, f.v].concat()) | |
| 306 | + } | |
| 307 | +} | |
| 308 | + | |
| 309 | +fn drain(decoder: &mut HwDecoder, prefix: &str, lines: &mut Vec<String>) -> Result<(), String> { | |
| 310 | + while let Some(picture) = decoder.next_picture().map_err(|e| e.to_string())? { | |
| 311 | + let index = lines.len(); | |
| 312 | + lines.push(format!( | |
| 313 | + "{prefix}/{index}\t{}", | |
| 314 | + sha_hex(&picture_bytes(&picture)?) | |
| 315 | + )); | |
| 316 | + } | |
| 317 | + Ok(()) | |
| 318 | +} | |
| 319 | + | |
| 320 | +fn decode_case(name: &str, codec: Codec, rel: &str) -> Outcome { | |
| 321 | + let data = fs::read(ec_testkit::fixture(rel)).map_err(|e| e.to_string())?; | |
| 322 | + let mut decoder = HwDecoder::open(codec).map_err(|e| e.to_string())?; | |
| 323 | + let prefix = format!("hw/dec/{}/{name}/{rel}", backend_name(decoder.backend())); | |
| 324 | + let mut lines = Vec::new(); | |
| 325 | + for (ts, unit) in access_units(codec, &data).into_iter().enumerate() { | |
| 326 | + decoder.decode(unit, ts as i64).map_err(|e| e.to_string())?; | |
| 327 | + drain(&mut decoder, &prefix, &mut lines)?; | |
| 328 | + } | |
| 329 | + decoder.flush().map_err(|e| e.to_string())?; | |
| 330 | + drain(&mut decoder, &prefix, &mut lines)?; | |
| 331 | + Ok(lines) | |
| 332 | +} | |
| 333 | + | |
| 334 | +fn guard(label: &str, case: &dyn Fn() -> Outcome, out: &mut Vec<String>) { | |
| 335 | + match panic::catch_unwind(AssertUnwindSafe(case)) { | |
| 336 | + Ok(Ok(lines)) => out.extend(lines), | |
| 337 | + Ok(Err(e)) => out.push(format!("{label}\tERR {}", e.replace('\n', " "))), | |
| 338 | + Err(_) => out.push(format!("{label}\tERR panic")), | |
| 339 | + } | |
| 340 | +} | |
| 341 | + | |
| 342 | +fn main() { | |
| 343 | + panic::set_hook(Box::new(|_| {})); | |
| 344 | + let caps = capabilities(); | |
| 345 | + let mut out = Vec::new(); | |
| 346 | + for backend in BACKENDS { | |
| 347 | + let bname = backend_name(backend); | |
| 348 | + for (name, codec, enc_codec, qp) in ENCODERS { | |
| 349 | + for fmt in FORMATS { | |
| 350 | + let ten = matches!(fmt, Fmt::Yuv10); | |
| 351 | + for (w, h) in SIZES { | |
| 352 | + for api in APIS { | |
| 353 | + let label = format!("hw/enc/{bname}/{name}/{}/{w}x{h}/{api}", fmt.name()); | |
| 354 | + if !supported(&caps, backend, codec, ten) { | |
| 355 | + out.push(format!("{label}\tSKIP unsupported")); | |
| 356 | + continue; | |
| 357 | + } | |
| 358 | + guard( | |
| 359 | + &label, | |
| 360 | + &|| encode_case(&label, backend, enc_codec, qp, fmt, (w, h), api), | |
| 361 | + &mut out, | |
| 362 | + ); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + } | |
| 366 | + } | |
| 367 | + } | |
| 368 | + for backend in BACKENDS { | |
| 369 | + let label = format!("hw/cbr/{}/h264/320x240", backend_name(backend)); | |
| 370 | + if !supported(&caps, backend, Codec::H264, false) { | |
| 371 | + out.push(format!("{label}\tSKIP unsupported")); | |
| 372 | + continue; | |
| 373 | + } | |
| 374 | + guard(&label, &|| cbr_case(&label, backend), &mut out); | |
| 375 | + } | |
| 376 | + for (name, codec, rel) in DECODERS { | |
| 377 | + let label = format!("hw/dec/{name}/{rel}"); | |
| 378 | + guard(&label, &|| decode_case(name, codec, rel), &mut out); | |
| 379 | + } | |
| 380 | + out.sort(); | |
| 381 | + for line in out { | |
| 382 | + println!("{line}"); | |
| 383 | + } | |
| 384 | +} |