All-vector sweep, 51-QP and geometry matrices, avcC entry
Browse files at this commit3e1c1779732461623ee3add301014804465893c6Parent: 19f3903- test
- all-vector sweep, 51-QP and geometry matrices, avcC entry
1 changed file +436 -73
- crates/ec-h264/tests/conformance.rs +436 -73
crates/ec-h264/tests/conformance.rsmodified+436 -73
| @@ -18,7 +18,9 @@ use std::path::{Path, PathBuf}; | |||
| 18 | use std::process::Command; | 18 | use std::process::Command; |
| 19 | use std::sync::atomic::{AtomicU64, Ordering}; | 19 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 20 | 20 | ||
| 21 | -use ec_h264::{Decoder, Error}; | 21 | +use ec_core::registry::{CodecId, CodecParameters, Decoder as _}; |
| 22 | +use ec_core::{Buf, Frame, Packet, TimeBase}; | ||
| 23 | +use ec_h264::{Decoder, Error, H264Decoder}; | ||
| 22 | 24 | ||
| 23 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
| 24 | // Counting allocator: proves the steady-state decode loop allocates nothing. | 26 | // Counting allocator: proves the steady-state decode loop allocates nothing. |
| @@ -66,41 +68,24 @@ fn vectors_dir() -> PathBuf { | |||
| 66 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/h264-jvt") | 68 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/h264-jvt") |
| 67 | } | 69 | } |
| 68 | 70 | ||
| 69 | -/// The CAVLC, progressive, single-slice-group streams whose first IDR this | 71 | +/// Every vector directory `scripts/fetch-vectors.sh` has populated, in name |
| 70 | -/// decoder must reproduce bit-exactly. | 72 | +/// order. |
| 71 | -const CAVLC_STREAMS: &[&str] = &[ | 73 | +/// |
| 72 | - "AUD_MW_E", | 74 | +/// A hand-written list would only ever record what passed on the day it was |
| 73 | - "BA1_Sony_D", | 75 | +/// written: a vector added later would be silently untested, and one that |
| 74 | - "BA3_SVA_C", | 76 | +/// regressed from bit-exact to refused would be invisible. Walking the |
| 75 | - "BA_MW_D", | 77 | +/// directory makes every fixture on disk a claim this test has to account for. |
| 76 | - "BANM_MW_D", | 78 | +fn all_vectors(base: &Path) -> Vec<String> { |
| 77 | - "BASQP1_Sony_C", | 79 | + let mut names: Vec<String> = std::fs::read_dir(base) |
| 78 | - "CI_MW_D", | 80 | + .into_iter() |
| 79 | - "MIDR_MW_D", | 81 | + .flatten() |
| 80 | - "NL1_Sony_D", | 82 | + .flatten() |
| 81 | - "NL3_SVA_E", | 83 | + .filter(|e| e.path().is_dir()) |
| 82 | - "NRF_MW_E", | 84 | + .map(|e| e.file_name().to_string_lossy().into_owned()) |
| 83 | - "SVA_BA2_D", | 85 | + .collect(); |
| 84 | - "SVA_CL1_E", | 86 | + names.sort(); |
| 85 | - "SVA_NL2_E", | 87 | + names |
| 86 | - "CVPCMNL2_SVA_C", | 88 | +} |
| 87 | - // First PPS/picture of these carries no FMO; the first IDR is plain | ||
| 88 | - // CAVLC baseline and must be bit-exact like the rest. | ||
| 89 | - "SVA_FM1_E", | ||
| 90 | - "SL1_SVA_B", | ||
| 91 | - "MR1_BT_A", | ||
| 92 | -]; | ||
| 93 | - | ||
| 94 | -/// Streams that must fail with a named Unsupported error (wrong output is | ||
| 95 | -/// forbidden; refusal strings are claims backed by these tests). | ||
| 96 | -const UNSUPPORTED_STREAMS: &[(&str, &str)] = &[ | ||
| 97 | - ("CABA1_SVA_B", "CABAC"), // entropy_coding_mode_flag = 1 | ||
| 98 | - ("MR9_BT_B", "CABAC"), // Main profile, CABAC | ||
| 99 | - ("CAMACI3_Sony_C", "CABAC"), // CABAC + interlace tools | ||
| 100 | - ("FM2_SVA_C", "FMO"), // num_slice_groups > 1 | ||
| 101 | - ("CVFI1_SVA_C", "interlaced"), // field pictures | ||
| 102 | - ("FI1_Sony_E", "interlaced"), // field pictures | ||
| 103 | -]; | ||
| 104 | 89 | ||
| 105 | fn find_stream(dir: &Path) -> Option<PathBuf> { | 90 | fn find_stream(dir: &Path) -> Option<PathBuf> { |
| 106 | let mut fallback = None; | 91 | let mut fallback = None; |
| @@ -184,7 +169,9 @@ fn jvt_cavlc_first_idr_bit_exact() { | |||
| 184 | } | 169 | } |
| 185 | let mut passed = Vec::new(); | 170 | let mut passed = Vec::new(); |
| 186 | let mut failed = Vec::new(); | 171 | let mut failed = Vec::new(); |
| 187 | - for name in CAVLC_STREAMS { | 172 | + let mut refused = Vec::new(); |
| 173 | + for name in all_vectors(&base) { | ||
| 174 | + let name = name.as_str(); | ||
| 188 | let dir = base.join(name); | 175 | let dir = base.join(name); |
| 189 | let Some(stream) = find_stream(&dir) else { | 176 | let Some(stream) = find_stream(&dir) else { |
| 190 | eprintln!("SKIP {name}: no bitstream file"); | 177 | eprintln!("SKIP {name}: no bitstream file"); |
| @@ -225,58 +212,39 @@ fn jvt_cavlc_first_idr_bit_exact() { | |||
| 225 | } | 212 | } |
| 226 | } | 213 | } |
| 227 | if ok { | 214 | if ok { |
| 228 | - passed.push(*name); | 215 | + passed.push(name.to_string()); |
| 229 | } else { | 216 | } else { |
| 230 | - failed.push(*name); | 217 | + failed.push(name.to_string()); |
| 231 | } | 218 | } |
| 232 | } | 219 | } |
| 220 | + // Outside this release's scope is allowed, but only when the | ||
| 221 | + // decoder says so by name. Corrupt or NeedMore on a conformant | ||
| 222 | + // stream is a bug here, not a capability statement. | ||
| 223 | + Err(e @ Error::Unsupported { .. }) => { | ||
| 224 | + eprintln!("REFUSED {name}: {e}"); | ||
| 225 | + refused.push(name.to_string()); | ||
| 226 | + } | ||
| 233 | Err(e) => { | 227 | Err(e) => { |
| 234 | eprintln!("FAIL {name}: decode error: {e}"); | 228 | eprintln!("FAIL {name}: decode error: {e}"); |
| 235 | - failed.push(*name); | 229 | + failed.push(name.to_string()); |
| 236 | } | 230 | } |
| 237 | } | 231 | } |
| 238 | } | 232 | } |
| 239 | eprintln!( | 233 | eprintln!( |
| 240 | - "PASS table ({} streams bit-exact): {passed:?}", | 234 | + "PASS table ({} bit-exact, {} refused by name): {passed:?}", |
| 241 | - passed.len() | 235 | + passed.len(), |
| 236 | + refused.len() | ||
| 237 | + ); | ||
| 238 | + assert!( | ||
| 239 | + failed.is_empty(), | ||
| 240 | + "streams neither bit-exact nor refused by name: {failed:?}" | ||
| 242 | ); | 241 | ); |
| 243 | - assert!(failed.is_empty(), "streams not bit-exact: {failed:?}"); | ||
| 244 | assert!( | 242 | assert!( |
| 245 | passed.len() >= 5, | 243 | passed.len() >= 5, |
| 246 | "fewer than 5 CAVLC conformance streams verified: {passed:?}" | 244 | "fewer than 5 CAVLC conformance streams verified: {passed:?}" |
| 247 | ); | 245 | ); |
| 248 | } | 246 | } |
| 249 | 247 | ||
| 250 | -#[test] | ||
| 251 | -fn unsupported_streams_refuse_with_named_errors() { | ||
| 252 | - let base = vectors_dir(); | ||
| 253 | - if !base.is_dir() { | ||
| 254 | - eprintln!("SKIP: fixtures missing"); | ||
| 255 | - return; | ||
| 256 | - } | ||
| 257 | - for (name, what) in UNSUPPORTED_STREAMS { | ||
| 258 | - let dir = base.join(name); | ||
| 259 | - let Some(stream) = find_stream(&dir) else { | ||
| 260 | - eprintln!("SKIP {name}: no bitstream file"); | ||
| 261 | - continue; | ||
| 262 | - }; | ||
| 263 | - let bytes = std::fs::read(&stream).unwrap(); | ||
| 264 | - match decode_first(&bytes) { | ||
| 265 | - Err(Error::Unsupported { what: w, .. }) => { | ||
| 266 | - let w_lower = w.to_lowercase(); | ||
| 267 | - let expect = what.to_lowercase(); | ||
| 268 | - assert!( | ||
| 269 | - w_lower.contains(&expect) | ||
| 270 | - || (expect == "interlaced" && w_lower.contains("interlace")), | ||
| 271 | - "{name}: expected Unsupported({what}), got Unsupported({w})" | ||
| 272 | - ); | ||
| 273 | - } | ||
| 274 | - Ok(_) => panic!("{name}: expected a named Unsupported error, decode succeeded"), | ||
| 275 | - Err(e) => panic!("{name}: expected Unsupported({what}), got: {e}"), | ||
| 276 | - } | ||
| 277 | - } | ||
| 278 | -} | ||
| 279 | - | ||
| 280 | /// Steady-state decode loop performs zero heap allocations: after one warm-up | 248 | /// Steady-state decode loop performs zero heap allocations: after one warm-up |
| 281 | /// picture, re-decoding the same slices into the reused picture context | 249 | /// picture, re-decoding the same slices into the reused picture context |
| 282 | /// (including deblocking) must not allocate. Frame emission (`frame()`) | 250 | /// (including deblocking) must not allocate. Frame emission (`frame()`) |
| @@ -413,3 +381,398 @@ fn corrupt_streams_never_panic() { | |||
| 413 | let _ = dec.decode_first_idr(&m); // must not panic; any Err is fine | 381 | let _ = dec.decode_first_idr(&m); // must not panic; any Err is fine |
| 414 | } | 382 | } |
| 415 | } | 383 | } |
| 384 | + | ||
| 385 | +// --------------------------------------------------------------------------- | ||
| 386 | +// ffmpeg-driven coverage: the JVT vectors are all QCIF, single-slice and coded | ||
| 387 | +// at a handful of quantisers, which leaves most of the deblocking tables, every | ||
| 388 | +// non-QCIF geometry and every refusal unexercised. x264 fills those gaps, with | ||
| 389 | +// ffmpeg's own decoder as the oracle. | ||
| 390 | +// --------------------------------------------------------------------------- | ||
| 391 | + | ||
| 392 | +/// Run a command, false when it fails or is not installed. | ||
| 393 | +fn run(command: &str, args: &[&str]) -> bool { | ||
| 394 | + Command::new(command) | ||
| 395 | + .args(args) | ||
| 396 | + .stdout(std::process::Stdio::null()) | ||
| 397 | + .stderr(std::process::Stdio::null()) | ||
| 398 | + .status() | ||
| 399 | + .map(|s| s.success()) | ||
| 400 | + .unwrap_or(false) | ||
| 401 | +} | ||
| 402 | + | ||
| 403 | +fn have_ffmpeg() -> bool { | ||
| 404 | + run( | ||
| 405 | + "ffmpeg", | ||
| 406 | + &["-hide_banner", "-loglevel", "error", "-version"], | ||
| 407 | + ) | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +/// A private scratch directory for one test. | ||
| 411 | +fn scratch(tag: &str) -> PathBuf { | ||
| 412 | + let dir = std::env::temp_dir().join(format!("ec-h264-{tag}-{}", std::process::id())); | ||
| 413 | + let _ = std::fs::create_dir_all(&dir); | ||
| 414 | + dir | ||
| 415 | +} | ||
| 416 | + | ||
| 417 | +/// Encode one all-intra picture with x264 and return the Annex B file. | ||
| 418 | +fn x264_encode(dir: &Path, tag: &str, size: &str, extra: &[&str]) -> Option<PathBuf> { | ||
| 419 | + let stream = dir.join(format!("{tag}.264")); | ||
| 420 | + let source = format!("testsrc=size={size}:rate=1:duration=1"); | ||
| 421 | + let mut args: Vec<String> = [ | ||
| 422 | + "-hide_banner", | ||
| 423 | + "-loglevel", | ||
| 424 | + "error", | ||
| 425 | + "-y", | ||
| 426 | + "-f", | ||
| 427 | + "lavfi", | ||
| 428 | + "-i", | ||
| 429 | + ] | ||
| 430 | + .iter() | ||
| 431 | + .map(|s| s.to_string()) | ||
| 432 | + .collect(); | ||
| 433 | + args.push(source); | ||
| 434 | + // The pixel format has to precede the encoder arguments that name a | ||
| 435 | + // profile depending on it. | ||
| 436 | + if !extra.contains(&"-pix_fmt") { | ||
| 437 | + args.extend(["-pix_fmt".into(), "yuv420p".into()]); | ||
| 438 | + } | ||
| 439 | + args.extend([ | ||
| 440 | + "-c:v".into(), | ||
| 441 | + "libx264".into(), | ||
| 442 | + "-frames:v".into(), | ||
| 443 | + "1".into(), | ||
| 444 | + ]); | ||
| 445 | + args.extend(extra.iter().map(|s| s.to_string())); | ||
| 446 | + args.extend([ | ||
| 447 | + "-f".into(), | ||
| 448 | + "h264".into(), | ||
| 449 | + stream.to_string_lossy().into_owned(), | ||
| 450 | + ]); | ||
| 451 | + let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); | ||
| 452 | + run("ffmpeg", &refs).then_some(stream) | ||
| 453 | +} | ||
| 454 | + | ||
| 455 | +/// Encode, decode both ways, return the number of differing bytes. | ||
| 456 | +fn round_trip(dir: &Path, tag: &str, size: &str, extra: &[&str]) -> Result<usize, String> { | ||
| 457 | + let stream = | ||
| 458 | + x264_encode(dir, tag, size, extra).ok_or(format!("{tag}: ffmpeg cannot encode"))?; | ||
| 459 | + let expected = | ||
| 460 | + ffmpeg_first_frame(&stream).ok_or(format!("{tag}: ffmpeg cannot decode its own output"))?; | ||
| 461 | + let (ours, w, h) = | ||
| 462 | + decode_first(&std::fs::read(&stream).unwrap()).map_err(|e| format!("{tag}: {e}"))?; | ||
| 463 | + if ours.len() > expected.len() { | ||
| 464 | + return Err(format!( | ||
| 465 | + "{tag}: {w}x{h} is {} bytes, ffmpeg gave {}", | ||
| 466 | + ours.len(), | ||
| 467 | + expected.len() | ||
| 468 | + )); | ||
| 469 | + } | ||
| 470 | + Ok(ours | ||
| 471 | + .iter() | ||
| 472 | + .zip(&expected[..ours.len()]) | ||
| 473 | + .filter(|(a, b)| a != b) | ||
| 474 | + .count()) | ||
| 475 | +} | ||
| 476 | + | ||
| 477 | +/// Every quantisation parameter, bit-exact against ffmpeg. | ||
| 478 | +/// | ||
| 479 | +/// The deblocking filter's alpha, beta and tC0 tables (8-16 and 8-17) are | ||
| 480 | +/// indexed by QP; the conformance vectors touch only a few of their 52 rows, | ||
| 481 | +/// and a whole wrong column there passes the vectors (it did, once). One | ||
| 482 | +/// all-intra Baseline picture per QP covers every row. | ||
| 483 | +#[test] | ||
| 484 | +fn every_quantiser_matches_ffmpeg() { | ||
| 485 | + if !have_ffmpeg() { | ||
| 486 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 487 | + return; | ||
| 488 | + } | ||
| 489 | + let dir = scratch("qp-sweep"); | ||
| 490 | + let mut checked = 0usize; | ||
| 491 | + let mut failures = Vec::new(); | ||
| 492 | + for qp in 1..=51u32 { | ||
| 493 | + // Baseline is CAVLC, 4:2:0, no 8x8 transform and no scaling matrices: | ||
| 494 | + // exactly this release's scope, with the loop filter on. | ||
| 495 | + let extra = ["-profile:v", "baseline", "-qp", &qp.to_string() as &str]; | ||
| 496 | + match round_trip(&dir, &format!("q{qp}"), "176x144", &extra) { | ||
| 497 | + Ok(0) => checked += 1, | ||
| 498 | + Ok(n) => failures.push(format!("qp {qp}: {n} bytes differ")), | ||
| 499 | + Err(e) => failures.push(e), | ||
| 500 | + } | ||
| 501 | + } | ||
| 502 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 503 | + assert!(failures.is_empty(), "{failures:#?}"); | ||
| 504 | + assert!(checked >= 40, "too few quantisers exercised: {checked}"); | ||
| 505 | + eprintln!("{checked} quantisers bit-exact against ffmpeg"); | ||
| 506 | +} | ||
| 507 | + | ||
| 508 | +/// Geometry and slice structure: cropping, odd sizes, multiple slices, the | ||
| 509 | +/// loop filter off and a chroma QP offset — what a real camera or encoder | ||
| 510 | +/// produces, none of which the QCIF single-slice vectors cover. | ||
| 511 | +#[test] | ||
| 512 | +fn geometry_and_slice_structure_match_ffmpeg() { | ||
| 513 | + if !have_ffmpeg() { | ||
| 514 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 515 | + return; | ||
| 516 | + } | ||
| 517 | + let dir = scratch("geometry"); | ||
| 518 | + let cases: &[(&str, &str, &[&str])] = &[ | ||
| 519 | + // 1080 lines are coded as 68 macroblock rows and cropped back to 1080. | ||
| 520 | + ("hd", "1920x1080", &["-profile:v", "baseline", "-qp", "26"]), | ||
| 521 | + // A width that is not a multiple of 16 crops horizontally as well. | ||
| 522 | + ("odd", "1916x1080", &["-profile:v", "baseline", "-qp", "26"]), | ||
| 523 | + // Four slices: neighbour availability stops at every slice boundary. | ||
| 524 | + ( | ||
| 525 | + "slices", | ||
| 526 | + "352x288", | ||
| 527 | + &[ | ||
| 528 | + "-profile:v", | ||
| 529 | + "baseline", | ||
| 530 | + "-qp", | ||
| 531 | + "24", | ||
| 532 | + "-x264-params", | ||
| 533 | + "slices=4", | ||
| 534 | + ], | ||
| 535 | + ), | ||
| 536 | + // No loop filter at all. | ||
| 537 | + ( | ||
| 538 | + "nodeblock", | ||
| 539 | + "352x288", | ||
| 540 | + &[ | ||
| 541 | + "-profile:v", | ||
| 542 | + "baseline", | ||
| 543 | + "-qp", | ||
| 544 | + "30", | ||
| 545 | + "-x264-params", | ||
| 546 | + "no-deblock=1", | ||
| 547 | + ], | ||
| 548 | + ), | ||
| 549 | + // A non-zero chroma QP offset moves the chroma deblock thresholds. | ||
| 550 | + ( | ||
| 551 | + "chromaqp", | ||
| 552 | + "352x288", | ||
| 553 | + &[ | ||
| 554 | + "-profile:v", | ||
| 555 | + "baseline", | ||
| 556 | + "-qp", | ||
| 557 | + "30", | ||
| 558 | + "-x264-params", | ||
| 559 | + "chroma_qp_offset=6", | ||
| 560 | + ], | ||
| 561 | + ), | ||
| 562 | + // One macroblock wide: every neighbour is unavailable. | ||
| 563 | + ("tiny", "16x16", &["-profile:v", "baseline", "-qp", "20"]), | ||
| 564 | + ]; | ||
| 565 | + let mut table = String::new(); | ||
| 566 | + let mut failures = Vec::new(); | ||
| 567 | + for (tag, size, extra) in cases { | ||
| 568 | + match round_trip(&dir, tag, size, extra) { | ||
| 569 | + Ok(0) => table.push_str(&format!("{tag:<10} {size:<10} bit-exact\n")), | ||
| 570 | + Ok(n) => { | ||
| 571 | + table.push_str(&format!("{tag:<10} {size:<10} MISMATCH {n} bytes\n")); | ||
| 572 | + failures.push(format!("{tag}: {n} bytes differ")); | ||
| 573 | + } | ||
| 574 | + Err(e) => { | ||
| 575 | + table.push_str(&format!("{tag:<10} {size:<10} {e}\n")); | ||
| 576 | + failures.push(e); | ||
| 577 | + } | ||
| 578 | + } | ||
| 579 | + } | ||
| 580 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 581 | + eprintln!("{table}"); | ||
| 582 | + assert!(failures.is_empty(), "{failures:#?}"); | ||
| 583 | +} | ||
| 584 | + | ||
| 585 | +/// Scaling matrices are decoded, not refused: a High-profile stream carrying | ||
| 586 | +/// the JVT default matrices (`cqm=jvt`) must come out bit-exact. | ||
| 587 | +/// | ||
| 588 | +/// Pinned because it is the one High-profile tool this release implements, and | ||
| 589 | +/// nothing else in the suite would notice it regressing into a refusal. | ||
| 590 | +#[test] | ||
| 591 | +fn jvt_scaling_matrices_decode_bit_exact() { | ||
| 592 | + if !have_ffmpeg() { | ||
| 593 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 594 | + return; | ||
| 595 | + } | ||
| 596 | + let dir = scratch("scaling"); | ||
| 597 | + let extra = [ | ||
| 598 | + "-profile:v", | ||
| 599 | + "high", | ||
| 600 | + "-qp", | ||
| 601 | + "26", | ||
| 602 | + "-x264-params", | ||
| 603 | + "cabac=0:8x8dct=0:cqm=jvt", | ||
| 604 | + ]; | ||
| 605 | + let result = round_trip(&dir, "cqm-jvt", "176x144", &extra); | ||
| 606 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 607 | + match result { | ||
| 608 | + Ok(0) => eprintln!("cqm=jvt scaling matrices bit-exact"), | ||
| 609 | + Ok(n) => panic!("cqm=jvt: {n} bytes differ from ffmpeg"), | ||
| 610 | + Err(e) => panic!("{e}"), | ||
| 611 | + } | ||
| 612 | +} | ||
| 613 | + | ||
| 614 | +/// Every refusal, proved against a stream that really uses the feature. | ||
| 615 | +/// | ||
| 616 | +/// A "not supported" string is a claim about this binary; without a stream | ||
| 617 | +/// that triggers it, it is just as likely to be a bug being hidden. | ||
| 618 | +#[test] | ||
| 619 | +fn refusals_name_a_feature_the_stream_really_uses() { | ||
| 620 | + if !have_ffmpeg() { | ||
| 621 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 622 | + return; | ||
| 623 | + } | ||
| 624 | + let dir = scratch("refusals"); | ||
| 625 | + // (tag, encoder arguments, the words the refusal must contain) | ||
| 626 | + let cases: &[(&str, &[&str], &str)] = &[ | ||
| 627 | + ( | ||
| 628 | + "cabac", | ||
| 629 | + &["-profile:v", "main", "-qp", "26", "-x264-params", "cabac=1"], | ||
| 630 | + "CABAC", | ||
| 631 | + ), | ||
| 632 | + ( | ||
| 633 | + "transform8x8", | ||
| 634 | + &[ | ||
| 635 | + "-profile:v", | ||
| 636 | + "high", | ||
| 637 | + "-qp", | ||
| 638 | + "26", | ||
| 639 | + "-x264-params", | ||
| 640 | + "cabac=0:8x8dct=1", | ||
| 641 | + ], | ||
| 642 | + "8x8 transform", | ||
| 643 | + ), | ||
| 644 | + ( | ||
| 645 | + "yuv422", | ||
| 646 | + &[ | ||
| 647 | + "-profile:v", | ||
| 648 | + "high422", | ||
| 649 | + "-qp", | ||
| 650 | + "26", | ||
| 651 | + "-pix_fmt", | ||
| 652 | + "yuv422p", | ||
| 653 | + "-x264-params", | ||
| 654 | + "cabac=0", | ||
| 655 | + ], | ||
| 656 | + "chroma_format_idc 2", | ||
| 657 | + ), | ||
| 658 | + ( | ||
| 659 | + "high10", | ||
| 660 | + &[ | ||
| 661 | + "-profile:v", | ||
| 662 | + "high10", | ||
| 663 | + "-qp", | ||
| 664 | + "26", | ||
| 665 | + "-pix_fmt", | ||
| 666 | + "yuv420p10le", | ||
| 667 | + "-x264-params", | ||
| 668 | + "cabac=0", | ||
| 669 | + ], | ||
| 670 | + "10-bit", | ||
| 671 | + ), | ||
| 672 | + ]; | ||
| 673 | + let mut table = String::new(); | ||
| 674 | + let mut failures = Vec::new(); | ||
| 675 | + let mut proved = 0usize; | ||
| 676 | + for (tag, extra, expected) in cases { | ||
| 677 | + let Some(stream) = x264_encode(&dir, tag, "176x144", extra) else { | ||
| 678 | + table.push_str(&format!( | ||
| 679 | + "{tag:<13} not encodable by this ffmpeg, skipped\n" | ||
| 680 | + )); | ||
| 681 | + continue; | ||
| 682 | + }; | ||
| 683 | + match decode_first(&std::fs::read(&stream).unwrap()) { | ||
| 684 | + Err(Error::Unsupported { what, why }) => { | ||
| 685 | + let message = format!("{what} ({why})"); | ||
| 686 | + if message.contains(expected) { | ||
| 687 | + proved += 1; | ||
| 688 | + table.push_str(&format!("{tag:<13} refused: {what}\n")); | ||
| 689 | + } else { | ||
| 690 | + failures.push(format!("{tag}: refused as {message}, expected {expected}")); | ||
| 691 | + } | ||
| 692 | + } | ||
| 693 | + Err(e) => failures.push(format!("{tag}: expected Unsupported({expected}), got {e}")), | ||
| 694 | + Ok(_) => failures.push(format!("{tag}: decoded a stream it does not support")), | ||
| 695 | + } | ||
| 696 | + } | ||
| 697 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 698 | + eprintln!("{table}"); | ||
| 699 | + assert!(failures.is_empty(), "{failures:#?}"); | ||
| 700 | + assert!(proved >= 2, "too few refusals proved against real streams"); | ||
| 701 | +} | ||
| 702 | + | ||
| 703 | +/// The `avcC` entry path: parameter sets out of band and NAL units length | ||
| 704 | +/// prefixed — how an MP4 or Matroska demuxer hands H.264 over. | ||
| 705 | +#[test] | ||
| 706 | +fn avcc_extradata_and_length_prefixed_packets() { | ||
| 707 | + if !have_ffmpeg() { | ||
| 708 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 709 | + return; | ||
| 710 | + } | ||
| 711 | + let dir = scratch("avcc"); | ||
| 712 | + let Some(annex_b) = x264_encode( | ||
| 713 | + &dir, | ||
| 714 | + "stream", | ||
| 715 | + "176x144", | ||
| 716 | + &["-profile:v", "baseline", "-qp", "26"], | ||
| 717 | + ) else { | ||
| 718 | + panic!("ffmpeg cannot encode a baseline picture"); | ||
| 719 | + }; | ||
| 720 | + let data = std::fs::read(&annex_b).unwrap(); | ||
| 721 | + let units: Vec<Vec<u8>> = ec_h264_syntax::AnnexBIter::new(&data) | ||
| 722 | + .map(<[u8]>::to_vec) | ||
| 723 | + .collect(); | ||
| 724 | + let sps = units.iter().find(|u| u[0] & 0x1F == 7).expect("an SPS"); | ||
| 725 | + let pps = units.iter().find(|u| u[0] & 0x1F == 8).expect("a PPS"); | ||
| 726 | + | ||
| 727 | + // avcC (ISO/IEC 14496-15 clause 5.3.3.1): version, profile, compatibility, | ||
| 728 | + // level, the NAL length size, then the parameter sets. | ||
| 729 | + let mut avcc = vec![1, sps[1], sps[2], sps[3], 0xFF, 0xE1]; | ||
| 730 | + avcc.extend_from_slice(&(sps.len() as u16).to_be_bytes()); | ||
| 731 | + avcc.extend_from_slice(sps); | ||
| 732 | + avcc.push(1); | ||
| 733 | + avcc.extend_from_slice(&(pps.len() as u16).to_be_bytes()); | ||
| 734 | + avcc.extend_from_slice(pps); | ||
| 735 | + | ||
| 736 | + let mut params = CodecParameters::new(CodecId::H264); | ||
| 737 | + params.extradata = Some(Buf::from_vec(avcc)); | ||
| 738 | + let mut decoder = H264Decoder::new(params).expect("avcC parses"); | ||
| 739 | + assert_eq!( | ||
| 740 | + ( | ||
| 741 | + decoder.codec_parameters().video().unwrap().width, | ||
| 742 | + decoder.codec_parameters().video().unwrap().height | ||
| 743 | + ), | ||
| 744 | + (176, 144), | ||
| 745 | + "the SPS inside the avcC sets the picture size, before any packet" | ||
| 746 | + ); | ||
| 747 | + | ||
| 748 | + // The slice NAL units, each behind a four byte length prefix. | ||
| 749 | + let mut sample = Vec::new(); | ||
| 750 | + for unit in units.iter().filter(|u| matches!(u[0] & 0x1F, 1 | 5)) { | ||
| 751 | + sample.extend_from_slice(&(unit.len() as u32).to_be_bytes()); | ||
| 752 | + sample.extend_from_slice(unit); | ||
| 753 | + } | ||
| 754 | + decoder | ||
| 755 | + .send_packet(&Packet::new(0, TimeBase::new(1, 25), sample).with_pts(7)) | ||
| 756 | + .expect("length prefixed packet decodes"); | ||
| 757 | + let Frame::Video(frame) = decoder.receive_frame().expect("a frame") else { | ||
| 758 | + panic!("video frame expected"); | ||
| 759 | + }; | ||
| 760 | + assert_eq!((frame.width, frame.height), (176, 144)); | ||
| 761 | + assert_eq!( | ||
| 762 | + frame.pts.map(|t| t.ticks), | ||
| 763 | + Some(7), | ||
| 764 | + "packet pts reaches the frame" | ||
| 765 | + ); | ||
| 766 | + | ||
| 767 | + // Same picture through the Annex B path: the two entry paths agree. | ||
| 768 | + let (annex_b_frame, _, _) = decode_first(&data).unwrap(); | ||
| 769 | + let mut avcc_frame = Vec::with_capacity(annex_b_frame.len()); | ||
| 770 | + for (index, plane) in frame.planes.iter().enumerate() { | ||
| 771 | + let (w, h) = if index == 0 { (176, 144) } else { (88, 72) }; | ||
| 772 | + for row in 0..h { | ||
| 773 | + avcc_frame.extend_from_slice(plane.row(row, w).expect("plane row")); | ||
| 774 | + } | ||
| 775 | + } | ||
| 776 | + assert_eq!(avcc_frame, annex_b_frame); | ||
| 777 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 778 | +} | ||