ec-hw golden_hw: add VA-direct decode pass over the same 8 fixtures; zero-pad frame indices to 4 digits
Browse files at this commit79e4a4f65729676baf9fbcbf48288f94874ff4ccParent: 74a46ce1 changed file +99 -5
crates/ec-hw/examples/golden_hw.rsmodified+99 -5
| @@ -2,9 +2,9 @@ use std::fs; | |||
| 2 | use std::panic::{self, AssertUnwindSafe}; | 2 | use std::panic::{self, AssertUnwindSafe}; |
| 3 | 3 | ||
| 4 | use ec_hw::{ | 4 | use ec_hw::{ |
| 5 | - Backend, Capability, Codec, CodecProfile, CodedFrame, Direction, EncCodec, EncodeInput, | 5 | + Backend, Capability, Codec, CodecProfile, CodedFrame, Decoder as VaDecoder, Direction, |
| 6 | - EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, HwPicture, I420, I420_16, RateControlMode, | 6 | + EncCodec, EncodeInput, EncoderConfig, Frame, FrameMetadata, HwDecoder, HwEncoder, HwPicture, |
| 7 | - RgbFrame, RgbOrder, capabilities, | 7 | + I420, I420_16, RateControlMode, RgbFrame, RgbOrder, capabilities, |
| 8 | }; | 8 | }; |
| 9 | use sha2::{Digest, Sha256}; | 9 | use sha2::{Digest, Sha256}; |
| 10 | 10 | ||
| @@ -252,7 +252,7 @@ fn encode_case( | |||
| 252 | .map_err(|e| e.to_string())?; | 252 | .map_err(|e| e.to_string())?; |
| 253 | Ok(coded | 253 | Ok(coded |
| 254 | .iter() | 254 | .iter() |
| 255 | - .map(|c| format!("{label}/{}\t{}", c.timestamp, sha_hex(&c.data))) | 255 | + .map(|c| format!("{label}/{:04}\t{}", c.timestamp, sha_hex(&c.data))) |
| 256 | .collect()) | 256 | .collect()) |
| 257 | } | 257 | } |
| 258 | 258 | ||
| @@ -310,13 +310,105 @@ fn drain(decoder: &mut HwDecoder, prefix: &str, lines: &mut Vec<String>) -> Resu | |||
| 310 | while let Some(picture) = decoder.next_picture().map_err(|e| e.to_string())? { | 310 | while let Some(picture) = decoder.next_picture().map_err(|e| e.to_string())? { |
| 311 | let index = lines.len(); | 311 | let index = lines.len(); |
| 312 | lines.push(format!( | 312 | lines.push(format!( |
| 313 | - "{prefix}/{index}\t{}", | 313 | + "{prefix}/{index:04}\t{}", |
| 314 | sha_hex(&picture_bytes(&picture)?) | 314 | sha_hex(&picture_bytes(&picture)?) |
| 315 | )); | 315 | )); |
| 316 | } | 316 | } |
| 317 | Ok(()) | 317 | Ok(()) |
| 318 | } | 318 | } |
| 319 | 319 | ||
| 320 | +fn split_access_units(data: &[u8], codec: Codec) -> Vec<&[u8]> { | ||
| 321 | + let mut starts: Vec<usize> = Vec::new(); | ||
| 322 | + let mut seen_slice = false; | ||
| 323 | + let mut i = 0usize; | ||
| 324 | + while i + 3 < data.len() { | ||
| 325 | + if data[i] != 0 || data[i + 1] != 0 || data[i + 2] != 1 { | ||
| 326 | + i += 1; | ||
| 327 | + continue; | ||
| 328 | + } | ||
| 329 | + let nal = i + 3; | ||
| 330 | + let (is_vcl, first_slice) = match codec { | ||
| 331 | + Codec::H264 => { | ||
| 332 | + let t = data[nal] & 0x1f; | ||
| 333 | + let first = data.get(nal + 1).is_some_and(|&b| b & 0x80 != 0); | ||
| 334 | + ((1..=5).contains(&t), first) | ||
| 335 | + } | ||
| 336 | + _ => { | ||
| 337 | + let t = (data[nal] >> 1) & 0x3f; | ||
| 338 | + let first = data.get(nal + 2).is_some_and(|&b| b & 0x80 != 0); | ||
| 339 | + (t <= 31, first) | ||
| 340 | + } | ||
| 341 | + }; | ||
| 342 | + if is_vcl { | ||
| 343 | + if seen_slice && first_slice { | ||
| 344 | + starts.push(i); | ||
| 345 | + seen_slice = false; | ||
| 346 | + } | ||
| 347 | + if first_slice { | ||
| 348 | + seen_slice = true; | ||
| 349 | + } | ||
| 350 | + } | ||
| 351 | + i = nal + 1; | ||
| 352 | + } | ||
| 353 | + let mut out = Vec::new(); | ||
| 354 | + let mut prev = 0usize; | ||
| 355 | + for &start in &starts { | ||
| 356 | + out.push(&data[prev..start]); | ||
| 357 | + prev = start; | ||
| 358 | + } | ||
| 359 | + if prev < data.len() { | ||
| 360 | + out.push(&data[prev..]); | ||
| 361 | + } | ||
| 362 | + out | ||
| 363 | +} | ||
| 364 | + | ||
| 365 | +fn va_access_units(codec: Codec, data: &[u8]) -> Vec<&[u8]> { | ||
| 366 | + match codec { | ||
| 367 | + Codec::H264 | Codec::H265 => split_access_units(data, codec), | ||
| 368 | + Codec::Vp9 | Codec::Av1 => ivf_units(data), | ||
| 369 | + } | ||
| 370 | +} | ||
| 371 | + | ||
| 372 | +fn frame_bytes(frame: &Frame) -> Result<Vec<u8>, String> { | ||
| 373 | + if frame.bit_depth > 8 { | ||
| 374 | + let f = frame.to_i420_16().map_err(|e| e.to_string())?; | ||
| 375 | + Ok([f.y, f.u, f.v] | ||
| 376 | + .concat() | ||
| 377 | + .iter() | ||
| 378 | + .flat_map(|s| s.to_le_bytes()) | ||
| 379 | + .collect()) | ||
| 380 | + } else { | ||
| 381 | + let f = frame.to_i420().map_err(|e| e.to_string())?; | ||
| 382 | + Ok([f.y, f.u, f.v].concat()) | ||
| 383 | + } | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +fn va_drain(decoder: &mut VaDecoder, prefix: &str, lines: &mut Vec<String>) -> Result<(), String> { | ||
| 387 | + while let Some(frame) = decoder.next_frame() { | ||
| 388 | + let index = lines.len(); | ||
| 389 | + lines.push(format!( | ||
| 390 | + "{prefix}/{index:04}\t{}", | ||
| 391 | + sha_hex(&frame_bytes(&frame)?) | ||
| 392 | + )); | ||
| 393 | + } | ||
| 394 | + Ok(()) | ||
| 395 | +} | ||
| 396 | + | ||
| 397 | +fn va_decode_case(name: &str, codec: Codec, rel: &str) -> Outcome { | ||
| 398 | + let display = ec_va::open().map_err(|e| e.to_string())?; | ||
| 399 | + let data = fs::read(ec_testkit::fixture(rel)).map_err(|e| e.to_string())?; | ||
| 400 | + let mut decoder = VaDecoder::new(&display, codec).map_err(|e| e.to_string())?; | ||
| 401 | + let prefix = format!("hw/dec/va/{name}/{rel}"); | ||
| 402 | + let mut lines = Vec::new(); | ||
| 403 | + for (ts, unit) in va_access_units(codec, &data).into_iter().enumerate() { | ||
| 404 | + decoder.decode(unit, ts as i64).map_err(|e| e.to_string())?; | ||
| 405 | + va_drain(&mut decoder, &prefix, &mut lines)?; | ||
| 406 | + } | ||
| 407 | + decoder.flush().map_err(|e| e.to_string())?; | ||
| 408 | + va_drain(&mut decoder, &prefix, &mut lines)?; | ||
| 409 | + Ok(lines) | ||
| 410 | +} | ||
| 411 | + | ||
| 320 | fn decode_case(name: &str, codec: Codec, rel: &str) -> Outcome { | 412 | 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())?; | 413 | 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())?; | 414 | let mut decoder = HwDecoder::open(codec).map_err(|e| e.to_string())?; |
| @@ -376,6 +468,8 @@ fn main() { | |||
| 376 | for (name, codec, rel) in DECODERS { | 468 | for (name, codec, rel) in DECODERS { |
| 377 | let label = format!("hw/dec/{name}/{rel}"); | 469 | let label = format!("hw/dec/{name}/{rel}"); |
| 378 | guard(&label, &|| decode_case(name, codec, rel), &mut out); | 470 | guard(&label, &|| decode_case(name, codec, rel), &mut out); |
| 471 | + let va_label = format!("hw/dec/va/{name}/{rel}"); | ||
| 472 | + guard(&va_label, &|| va_decode_case(name, codec, rel), &mut out); | ||
| 379 | } | 473 | } |
| 380 | out.sort(); | 474 | out.sort(); |
| 381 | for line in out { | 475 | for line in out { |