Files
craft-libs/crates/video/tests/h264_encode/pinned_decisions.rs

92 lines
3.9 KiB
Rust

//! The encoder's decisions are pinned: the coded slices of synthetic clips hash to fixed values,
//! so a change that alters any decision (a mode, a motion vector, a quantiser), and not only one
//! that breaks decoding, fails here. Only slice NAL units are hashed: the SEI names the version.
use crate::common::{annexb_nals, synth};
use craft_video::h264::*;
/// FNV-1a over the slice NAL units of `n` frames of [`synth`] encoded with `c`.
fn slices_hash(c: EncoderConfig, n: usize) -> u64 {
slices_hash_with_starts(c, n, MotionSearchStarts::Extended)
}
fn slices_hash_with_starts(c: EncoderConfig, n: usize, starts: MotionSearchStarts) -> u64 {
let (w, h) = (c.width as usize, c.height as usize);
let mut enc = Encoder::new_with_motion_search_starts(c, starts).unwrap();
let mut stream = Vec::new();
for t in 0..n {
let f = synth(w, h, t);
for p in enc.encode(&f.frame(), t as i64).unwrap() {
stream.extend(p.data);
}
}
for p in enc.flush() {
stream.extend(p.data);
}
let mut hash = 0xcbf2_9ce4_8422_2325u64;
for r in annexb_nals(&stream) {
let nal = &stream[r];
if matches!(nal.get(3).map(|b| b & 31), Some(1 | 5)) {
for &b in nal {
hash = (hash ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3);
}
}
}
hash
}
fn cfg(profile: Profile, preset: Preset, rate: RateControl) -> EncoderConfig {
let mut c = EncoderConfig::new(320, 192, 24, 1);
c.profile = profile;
c.preset = preset;
c.rate = rate;
c.slices = 3;
c.threads = 3;
c
}
/// Pinned on this crate's encoder. FilmCraft's copy, which these tests come from, pinned
/// 0x6223db8241eb7da1, 0x721d6e683f965bf1, 0x777291d54c75c320 and 0x72ff5e78a3f0e46e; this encoder
/// gives exactly those with the 16x16 search's extra start candidates (skip, zero and co-located
/// vectors) left out and FilmCraft's writer name in the SEI (whose size the bitrate modes count).
#[test]
fn coded_slices_are_pinned() {
let cases = [
("high balanced crf", cfg(Profile::High, Preset::Balanced, RateControl::Crf(23.0)), 0x1641_6fcb_1180_8977u64),
("high quality vbr", cfg(Profile::High, Preset::Quality, RateControl::Vbr { target_kbps: 800, max_kbps: 1200 }), 0x4d7f_6862_4134_8043),
("main speed qp", cfg(Profile::Main, Preset::Speed, RateControl::Qp(30)), 0x6dae_85ee_6ec3_7bf7),
("baseline balanced cbr", cfg(Profile::Baseline, Preset::Balanced, RateControl::Cbr { kbps: 600 }), 0x47a8_004e_fb1a_6a17),
];
let got: Vec<u64> = cases.iter().map(|(_, c, _)| slices_hash(c.clone(), 20)).collect();
for ((name, _, _), got) in cases.iter().zip(&got) {
eprintln!("{name}: {got:#018x}");
}
for ((name, _, want), got) in cases.iter().zip(&got) {
assert_eq!(*got, *want, "{name}: the coded slices changed");
}
}
#[test]
fn filmcraft_writer_keeps_shared_search_decisions_pinned() {
let cases = [
("high balanced crf", cfg(Profile::High, Preset::Balanced, RateControl::Crf(23.0)), 0x6223_db82_41eb_7da1u64),
("high quality vbr", cfg(Profile::High, Preset::Quality, RateControl::Vbr { target_kbps: 800, max_kbps: 1200 }), 0x721d_6e68_3f96_5bf1),
("main speed qp", cfg(Profile::Main, Preset::Speed, RateControl::Qp(30)), 0x7772_91d5_4c75_c320),
("baseline balanced cbr", cfg(Profile::Baseline, Preset::Balanced, RateControl::Cbr { kbps: 600 }), 0x72ff_5e78_a3f0_e46e),
];
let got: Vec<u64> = cases
.iter()
.map(|(_, c, _)| {
let mut c = c.clone();
c.writer = "filmcraft-h264enc 0.20.1".into();
slices_hash_with_starts(c, 20, MotionSearchStarts::SeedAndNeighbour)
})
.collect();
for ((name, _, _), got) in cases.iter().zip(&got) {
eprintln!("FilmCraft {name}: {got:#018x}");
}
for ((name, _, want), got) in cases.iter().zip(&got) {
assert_eq!(*got, *want, "{name}: FilmCraft's coded slices changed");
}
}