From 4a5570047400efd39c3aa9f5fdb49701290fd9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 13:19:40 +0300 Subject: [PATCH] test(h265): the bit-target mode and the batch helper Both are surface a caller reaches for and neither had a test. The target mode is asserted on the contract it can keep - monotonic in the target and within a factor either side - rather than on a bitrate to the byte, which no single-picture encoder can promise. --- crates/ec-h265/Cargo.toml | 3 ++ crates/ec-h265/tests/conformance.rs | 54 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/crates/ec-h265/Cargo.toml b/crates/ec-h265/Cargo.toml index b1299a49..ce5e1f69 100644 --- a/crates/ec-h265/Cargo.toml +++ b/crates/ec-h265/Cargo.toml @@ -9,3 +9,6 @@ authors.workspace = true [dependencies] ec-core.workspace = true ec-h265-syntax.workspace = true + +[dev-dependencies] +ec-h265-syntax.workspace = true diff --git a/crates/ec-h265/tests/conformance.rs b/crates/ec-h265/tests/conformance.rs index c83fa4f3..538776cd 100644 --- a/crates/ec-h265/tests/conformance.rs +++ b/crates/ec-h265/tests/conformance.rs @@ -231,3 +231,57 @@ fn threads_do_not_change_the_bitstream() { let b = many.encode_idr(&frame).unwrap(); assert_eq!(a.au, b.au, "wavefront changed the bitstream"); } + +#[test] +fn a_bit_target_lands_near_its_target() { + // The model picks a QP from bits-per-pixel; a picture that comes out more + // than a quarter off is coded once more at a corrected QP. What is asserted + // is the contract that mode offers — near the target, monotonic in it — not + // a bitrate to the byte, which no single-picture encoder can promise. + let mut previous = 0usize; + for target in [400_000u64, 1_200_000, 3_000_000] { + let mut cfg = EncoderConfig::new(640, 360); + cfg.rate_control = RateControl::TargetBits(target); + let encoder = Encoder::new(cfg).expect("encoder"); + let frame = test_frame(640, 360, 5); + let coded = encoder.encode_idr(&frame).expect("encode"); + let bits = coded.au.len() * 8; + assert!( + bits > previous, + "target {target}: {bits} bits did not grow with the target" + ); + previous = bits; + let ratio = bits as f64 / target as f64; + assert!( + (0.4..2.5).contains(&ratio), + "target {target}: got {bits} bits (x{ratio:.2}) at QP {}", + coded.qp + ); + } +} + +#[test] +fn the_batch_helper_codes_every_picture_as_its_own_access_unit() { + let cfg = EncoderConfig::new(128, 96); + let encoder = Encoder::new(cfg).expect("encoder"); + let frames: Vec = (0..3).map(|i| test_frame(128, 96, i * 11)).collect(); + let coded = encoder.encode_batch(frames.iter()).expect("batch encode"); + assert_eq!(coded.len(), 3); + for picture in &coded { + // Every access unit stands alone: parameter sets, then an IDR slice. + let nals = ec_h265_syntax::split_annex_b(&picture.au); + let types: Vec<_> = nals.iter().map(|n| n.header.nal_type).collect(); + assert_eq!( + &types[..4], + &[ + ec_h265_syntax::NalUnitType::Vps, + ec_h265_syntax::NalUnitType::Sps, + ec_h265_syntax::NalUnitType::Pps, + ec_h265_syntax::NalUnitType::IdrWRadl, + ], + "access unit is not self-contained" + ); + } + // Different pictures, different bitstreams. + assert_ne!(coded[0].au, coded[1].au); +}