mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 07:35:02 +00:00
craft-video: FilmCraft's later H.264 encoder work (High 10, threads, sub-pel diamond) and its H.264 and ProRes tests * craft-video: H.264 High 10 and High 10 Intra in the encoder The encoder is generic over the sample type (u8 at 8 bits, u16 at 9-10 bits): QP' = QP + QpBdOffset for quantisation and lambdas, mb_qp_delta range and wrap, chroma QP mapping, deblocking thresholds scaled to the bit depth, clipping in prediction, interpolation and reconstruction. SPS bit depths, profile_idc 110, constraint_set3 for High 10 Intra, avcC high-profile fields, the High 10 cpbBrVclFactor for level choice. New YuvFrame16 / Encoder::encode16, EncoderConfig::bit_depth / intra_only. 8-bit output is unchanged. Merged onto craft-video's copy: its input stride checks, B-frame timestamp check, configuration validation, level 6.2 refusal, motion search start candidates and codec_params are kept. Ported from storytold/filmcraft#1580 (commit d24ce15e). * craft-video: the H.264 encoder honours threads: 1 and reuses MbInfo buffers; raw-YUV bench - A pool is built for every thread count, including 1: rayon calls outside install run on the global pool (all cores), so threads: 1 used to run on every core. A pool that cannot be built is an InvalidConfig error. - Up to eight macroblock-info buffers are recycled between pictures and cleared on reuse, instead of allocating one per picture. - examples/h264_h264enc_bench.rs encodes a raw YUV file and reports speed; its argument checks run as tests. Ported from storytold/filmcraft#3205 (merge 08f8075a). * craft-video: H.264 encoder sub-pel refinement on a 4-point diamond; 16x8 and 8x16 only after 8x8 beat 16x16 Sub-pel refinement tries the four axis neighbours instead of all eight, and the 16x8 / 8x16 partitions are searched only when 8x8 beat 16x16: fewer SATD evaluations for about the same decisions. craft-video's own motion search start candidates are kept. Ported from storytold/filmcraft#3207 (merge 8db5334c). * craft-video: FilmCraft's H.264 encoder tests; EncoderConfig::writer names the program in the SEI The encoder had no integration tests here. FilmCraft's come along as the h264_encode test target: ffmpeg/ffprobe oracle decodes (8-bit and High 10, bit-exact against the reconstruction), pinned decisions, CRF level choice and the settings SEI, thread counts and VBR overshoot. EncoderConfig::writer is the program and version the identification SEI names (craft-h264enc and this crate's version by default), so an application's files keep naming it. Two expectations differ from FilmCraft's copy: the pinned slice hashes (this encoder also starts the 16x16 search from the skip, zero and co-located vectors; with those left out and FilmCraft's writer name, it gives FilmCraft's four hashes exactly) and a CRF that is not a number, which this encoder refuses. * craft-video: FilmCraft's ProRes tests (oracle decode and encode, round trip, fuzz, perf) craft-video's ProRes copy came with only its hostile-input test. FilmCraft's ffmpeg oracle decode and encode tests, round trips, mutation fuzz and the ignored throughput tests come along; craft-video's own fuzz_hostile stays. Frame::new returns a Result here, so the tests unwrap it. --------- Co-authored-by: JordyUSA <75485565+JordyUSA@users.noreply.github.com> Co-authored-by: vangel76 <vangel1976@googlemail.com>
64 lines
2.6 KiB
Rust
64 lines
2.6 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 {
|
|
let (w, h) = (c.width as usize, c.height as usize);
|
|
let mut enc = Encoder::new(c).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");
|
|
}
|
|
}
|