forge
Explore
Log in

Don't you dare go Hollow

The bit-target mode and the batch helper

Browse files at this commit

4a5570047400efd39c3aa9f5fdb49701290fd9bdAhmet Kaan GümüşParent: 138d2a4
  • test
    • the bit-target mode and the batch helper
Show full message
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.tomlmodified+3 -0
@@ -9,3 +9,6 @@ authors.workspace = true
99 [dependencies]
1010 ec-core.workspace = true
1111 ec-h265-syntax.workspace = true
12+
13+[dev-dependencies]
14+ec-h265-syntax.workspace = true
crates/ec-h265/tests/conformance.rsmodified+54 -0
@@ -231,3 +231,57 @@ fn threads_do_not_change_the_bitstream() {
231231 let b = many.encode_idr(&frame).unwrap();
232232 assert_eq!(a.au, b.au, "wavefront changed the bitstream");
233233 }
234+
235+#[test]
236+fn a_bit_target_lands_near_its_target() {
237+ // The model picks a QP from bits-per-pixel; a picture that comes out more
238+ // than a quarter off is coded once more at a corrected QP. What is asserted
239+ // is the contract that mode offers — near the target, monotonic in it — not
240+ // a bitrate to the byte, which no single-picture encoder can promise.
241+ let mut previous = 0usize;
242+ for target in [400_000u64, 1_200_000, 3_000_000] {
243+ let mut cfg = EncoderConfig::new(640, 360);
244+ cfg.rate_control = RateControl::TargetBits(target);
245+ let encoder = Encoder::new(cfg).expect("encoder");
246+ let frame = test_frame(640, 360, 5);
247+ let coded = encoder.encode_idr(&frame).expect("encode");
248+ let bits = coded.au.len() * 8;
249+ assert!(
250+ bits > previous,
251+ "target {target}: {bits} bits did not grow with the target"
252+ );
253+ previous = bits;
254+ let ratio = bits as f64 / target as f64;
255+ assert!(
256+ (0.4..2.5).contains(&ratio),
257+ "target {target}: got {bits} bits (x{ratio:.2}) at QP {}",
258+ coded.qp
259+ );
260+ }
261+}
262+
263+#[test]
264+fn the_batch_helper_codes_every_picture_as_its_own_access_unit() {
265+ let cfg = EncoderConfig::new(128, 96);
266+ let encoder = Encoder::new(cfg).expect("encoder");
267+ let frames: Vec<VideoFrame> = (0..3).map(|i| test_frame(128, 96, i * 11)).collect();
268+ let coded = encoder.encode_batch(frames.iter()).expect("batch encode");
269+ assert_eq!(coded.len(), 3);
270+ for picture in &coded {
271+ // Every access unit stands alone: parameter sets, then an IDR slice.
272+ let nals = ec_h265_syntax::split_annex_b(&picture.au);
273+ let types: Vec<_> = nals.iter().map(|n| n.header.nal_type).collect();
274+ assert_eq!(
275+ &types[..4],
276+ &[
277+ ec_h265_syntax::NalUnitType::Vps,
278+ ec_h265_syntax::NalUnitType::Sps,
279+ ec_h265_syntax::NalUnitType::Pps,
280+ ec_h265_syntax::NalUnitType::IdrWRadl,
281+ ],
282+ "access unit is not self-contained"
283+ );
284+ }
285+ // Different pictures, different bitstreams.
286+ assert_ne!(coded[0].au, coded[1].au);
287+}