h264enc: honour threads: 1, reuse MbInfo buffers, add raw-YUV bench

With threads: 1 the encoder built no pool, so the lookahead, half-pel and
slice loops fell through to rayon's global pool on every core. Build the
pool for every thread count. Reuse the per-picture MbInfo arrays like the
frame buffers. Add an h264enc_bench example that encodes a raw yuv420p
file with export-like settings, and drop the oracle output that was
committed under crates/h264enc/target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Opus 5.5 committed 2026-10-24 20:43:10 +02:00
1 parent bfc23840b6
commit 7b97aa7cd8
17 files changed
+89 -2

No files matched your search

+1
View File
@@ -1,6 +1,7 @@
*~
plan/
/target
/crates/*/target/
/dist
/log
.DS_Store
+64
View File
@@ -0,0 +1,64 @@
//! Development tool: encode a raw yuv420p file (30 fps) with export-like settings and report the speed.
//!
//! `cargo run --release -p filmcraft-h264enc --example h264enc_bench -- IN.yuv W H FRAMES OUT.h264 [preset] [rate] [slices] [bframes] [threads]`
//! preset: speed|balanced|quality, rate: qpN | crfN | cbrN | vbrN (max = 1.5 x target), slices: -1 = export rule
//! (one per four macroblock rows), threads: 0 = all cores. Defaults match an export: balanced vbr20000 -1 2 0.
use filmcraft_h264enc::*;
use std::time::Instant;
fn main() {
let a: Vec<String> = std::env::args().collect();
let arg = |i: usize, d: &str| a.get(i).cloned().unwrap_or_else(|| d.to_string());
if a.len() < 6 {
eprintln!("usage: h264enc_bench IN.yuv W H FRAMES OUT.h264 [preset] [rate] [slices] [bframes] [threads]");
std::process::exit(2);
}
let w: usize = arg(2, "").parse().expect("width");
let h: usize = arg(3, "").parse().expect("height");
let frames: usize = arg(4, "").parse().expect("frame count");
let raw = std::fs::read(arg(1, "")).expect("read input");
let fs = w * h * 3 / 2;
let n = frames.min(raw.len() / fs);
let mut cfg = EncoderConfig::new(w as u32, h as u32, 30, 1);
cfg.preset = match arg(6, "balanced").as_str() {
"speed" => Preset::Speed,
"quality" => Preset::Quality,
_ => Preset::Balanced,
};
let rate = arg(7, "vbr20000");
cfg.rate = if let Some(q) = rate.strip_prefix("qp") {
RateControl::Qp(q.parse().expect("qp"))
} else if let Some(q) = rate.strip_prefix("crf") {
RateControl::Crf(q.parse().expect("crf"))
} else if let Some(k) = rate.strip_prefix("cbr") {
RateControl::Cbr { kbps: k.parse().expect("kbps") }
} else {
let k: u32 = rate.strip_prefix("vbr").unwrap_or(&rate).parse().expect("kbps");
RateControl::Vbr { target_kbps: k, max_kbps: k * 3 / 2 }
};
let slices: i64 = arg(8, "-1").parse().expect("slices");
cfg.slices = usize::try_from(slices).unwrap_or_else(|_| h.div_ceil(16).div_ceil(4).max(1));
cfg.bframes = arg(9, "2").parse().expect("bframes");
cfg.threads = arg(10, "0").parse().expect("threads");
cfg.keyint = 60;
cfg.format = PacketFormat::AnnexB;
let mut enc = Encoder::new(cfg.clone()).expect("encoder");
let mut out = Vec::new();
let t0 = Instant::now();
for (i, f) in raw.chunks_exact(fs).take(n).enumerate() {
let (y, rest) = f.split_at(w * h);
let (u, v) = rest.split_at(w * h / 4);
let fr = YuvFrame { y, u, v, y_stride: w, uv_stride: w / 2 };
for p in enc.encode(&fr, i as i64).expect("encode") {
out.extend_from_slice(&p.data);
}
}
for p in enc.flush() {
out.extend_from_slice(&p.data);
}
let el = t0.elapsed().as_secs_f64();
std::fs::write(arg(5, ""), &out).expect("write output");
let kbps = out.len() as f64 * 8.0 / (n as f64 / 30.0) / 1000.0;
println!("frames={n} wall={el:.3}s fps={:.2} bytes={} kbps={kbps:.0} slices={} threads={}", n as f64 / el, out.len(), cfg.slices, cfg.threads);
}
+24 -2
View File
@@ -244,6 +244,7 @@ struct Input {
}
struct Pool {
/// Built for every thread count, also 1: rayon calls outside `install` run on the global pool (all cores).
#[cfg(feature = "threads")]
pool: Option<rayon::ThreadPool>,
}
@@ -286,6 +287,7 @@ pub struct Encoder {
held: Vec<Packet>,
frame_pool: Vec<Frame>,
hpel_pool: Vec<[picture::Plane; 3]>,
mbs_pool: Vec<Vec<MbInfo>>,
sei_sent: bool,
slices: usize,
pool: Pool,
@@ -483,7 +485,7 @@ impl Encoder {
let threads = if cfg.threads == 0 { std::thread::available_parallelism().map_or(1, |n| n.get()) } else { cfg.threads };
let slices = if cfg.slices > 0 { cfg.slices.min(mbh) } else { threads.min(mbh.div_ceil(4)).max(1) };
#[cfg(feature = "threads")]
let pool = Pool { pool: if threads > 1 { rayon::ThreadPoolBuilder::new().num_threads(threads).build().ok() } else { None } };
let pool = Pool { pool: rayon::ThreadPoolBuilder::new().num_threads(threads).build().ok() };
#[cfg(not(feature = "threads"))]
let pool = Pool {};
let record_pass1 = cfg.pass == Pass::First;
@@ -514,6 +516,7 @@ impl Encoder {
held: Vec::new(),
frame_pool: Vec::new(),
hpel_pool: Vec::new(),
mbs_pool: Vec::new(),
sei_sent: false,
slices,
pool,
@@ -677,6 +680,17 @@ impl Encoder {
self.frame_pool.pop().unwrap_or_else(|| Frame::new(self.params.mbw * 16, self.params.mbh * 16))
}
fn new_mbs(&mut self) -> Vec<MbInfo> {
let n = self.params.mbw * self.params.mbh;
match self.mbs_pool.pop() {
Some(mut m) => {
m.fill(MbInfo::default());
m
}
None => vec![MbInfo::default(); n],
}
}
fn recycle(&mut self, f: Frame) {
if self.frame_pool.len() < 8 {
self.frame_pool.push(f);
@@ -768,7 +782,7 @@ impl Encoder {
beta_offset_div2: 0,
};
let rec = self.new_frame();
let mbs = vec![MbInfo::default(); self.params.mbw * self.params.mbh];
let mbs = self.new_mbs();
jobs.push(Job { input, st, idr, qpf, qp, cplx, aq, hdr, nal_ref_idc, l0, l1, poc, rec: Some(rec), mbs, results: Vec::new() });
}
@@ -937,6 +951,7 @@ impl Encoder {
self.next_frame_num = (self.next_frame_num + 1) % (1 << LOG2_MAX_FRAME_NUM);
} else {
self.recycle(rec);
self.recycle_mbs(mbs);
}
if idr {
self.idr_count += 1;
@@ -965,6 +980,13 @@ impl Encoder {
if self.hpel_pool.len() < 4 {
self.hpel_pool.push(r.hpel);
}
self.recycle_mbs(r.mbs);
}
fn recycle_mbs(&mut self, m: Vec<MbInfo>) {
if self.mbs_pool.len() < 8 {
self.mbs_pool.push(m);
}
}
}