From 6e90b9d2cff0b69ff1d1cf28810b424c6ee071db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 25 Aug 2026 04:44:05 +0300 Subject: [PATCH] fix(h265): bound the clip gate's decode to the pictures it samples `clip_sources` asked ffmpeg for the whole file as raw I420 and sliced the result afterwards. A 1080p feature is hundreds of gigabytes that way: a run on a full film grew the test binary to 29 GB of anon RSS and was killed by the kernel OOM killer, with the machine swapping hard. Short captures fit, so the gate had only ever been run on content that hid it. Trim inside ffmpeg instead -- `select=gte(n,SKIP)` drops the skipped pictures and `-frames:v WANT` stops the decode once enough have come out, so the process holds exactly the frames it measures. The same film run now finishes in 10 s inside an 8 GB cap, and the numbers on the short capture are unchanged. --- crates/ec-h265/tests/encode.rs | 36 +++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/ec-h265/tests/encode.rs b/crates/ec-h265/tests/encode.rs index 95fa4b7a..ae892d78 100644 --- a/crates/ec-h265/tests/encode.rs +++ b/crates/ec-h265/tests/encode.rs @@ -435,6 +435,9 @@ fn bd_psnr_vs_x265() { if let Ok(w) = std::env::var("EC_H265_CHROMA_W") { cfg.chroma_rd_weight = w.parse().expect("EC_H265_CHROMA_W must be a number"); } + if let Ok(v) = std::env::var("EC_H265_NXN") { + cfg.intra_nxn = v != "0"; + } if let Ok(k) = std::env::var("EC_H265_RDO_CANDIDATES") { cfg.rdo_candidates = k.parse().expect("EC_H265_RDO_CANDIDATES must be a number"); } @@ -705,10 +708,34 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec().ok()?, h.parse::().ok()?))) .expect("ffprobe returned dimensions"); + // Pictures to pass over before sampling: the head of a film is titles and + // fades, which are not what a codec is measured on. + let skip: usize = std::env::var("EC_H265_CLIP_SKIP") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(0); + // Trim on ffmpeg's side, not ours. Asking for the whole file and slicing + // afterwards buffers the entire decoded clip in memory: a 1080p feature is + // hundreds of gigabytes of raw I420, which the kernel answers with the OOM + // killer. `select` drops the skipped pictures inside ffmpeg and + // `-frames:v` stops the decode once `want` have come out, so what reaches + // this process is exactly `want` frames. let raw = std::process::Command::new("ffmpeg") .args(["-v", "error", "-i"]) .arg(path) - .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "pipe:1"]) + .args([ + "-vf", + &format!("select=gte(n\\,{skip})"), + "-fps_mode", + "passthrough", + "-frames:v", + &want.to_string(), + "-f", + "rawvideo", + "-pix_fmt", + "yuv420p", + "pipe:1", + ]) .output() .expect("ffmpeg raw extract"); assert!( @@ -719,18 +746,13 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec= want, - "clip {} yielded {} bytes, need {want}x{frame}", + "clip {} yielded {} bytes after skipping {skip}, need {want}x{frame}", path.to_string_lossy(), raw.stdout.len() ); - let skip: usize = std::env::var("EC_H265_CLIP_SKIP") - .ok() - .and_then(|v| v.parse().ok()) - .unwrap_or(0); let sources: Vec = raw .stdout .chunks(frame) - .skip(skip) .take(want) .map(|f| { let (y, rest) = f.split_at(w * h);