forge
Explore
Log in

Don't you dare go Hollow

Bound the clip gate's decode to the pictures it samples

Browse files at this commit

6e90b9d2cff0b69ff1d1cf28810b424c6ee071dbAhmet Kaan GümüşParent: fea923b
  • fix
    • bound the clip gate's decode to the pictures it samples
Show full message
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.rsmodified+29 -7
@@ -435,6 +435,9 @@ fn bd_psnr_vs_x265() {
435435 if let Ok(w) = std::env::var("EC_H265_CHROMA_W") {
436436 cfg.chroma_rd_weight = w.parse().expect("EC_H265_CHROMA_W must be a number");
437437 }
438+ if let Ok(v) = std::env::var("EC_H265_NXN") {
439+ cfg.intra_nxn = v != "0";
440+ }
438441 if let Ok(k) = std::env::var("EC_H265_RDO_CANDIDATES") {
439442 cfg.rdo_candidates = k.parse().expect("EC_H265_RDO_CANDIDATES must be a number");
440443 }
@@ -705,10 +708,34 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Plane
705708 .split_once(',')
706709 .and_then(|(w, h)| Some((w.parse::<usize>().ok()?, h.parse::<usize>().ok()?)))
707710 .expect("ffprobe returned dimensions");
711+ // Pictures to pass over before sampling: the head of a film is titles and
712+ // fades, which are not what a codec is measured on.
713+ let skip: usize = std::env::var("EC_H265_CLIP_SKIP")
714+ .ok()
715+ .and_then(|v| v.parse().ok())
716+ .unwrap_or(0);
717+ // Trim on ffmpeg's side, not ours. Asking for the whole file and slicing
718+ // afterwards buffers the entire decoded clip in memory: a 1080p feature is
719+ // hundreds of gigabytes of raw I420, which the kernel answers with the OOM
720+ // killer. `select` drops the skipped pictures inside ffmpeg and
721+ // `-frames:v` stops the decode once `want` have come out, so what reaches
722+ // this process is exactly `want` frames.
708723 let raw = std::process::Command::new("ffmpeg")
709724 .args(["-v", "error", "-i"])
710725 .arg(path)
711- .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "pipe:1"])
726+ .args([
727+ "-vf",
728+ &format!("select=gte(n\\,{skip})"),
729+ "-fps_mode",
730+ "passthrough",
731+ "-frames:v",
732+ &want.to_string(),
733+ "-f",
734+ "rawvideo",
735+ "-pix_fmt",
736+ "yuv420p",
737+ "pipe:1",
738+ ])
712739 .output()
713740 .expect("ffmpeg raw extract");
714741 assert!(
@@ -719,18 +746,13 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Plane
719746 let frame = w * h * 3 / 2;
720747 assert!(
721748 raw.stdout.len().is_multiple_of(frame) && raw.stdout.len() / frame >= want,
722- "clip {} yielded {} bytes, need {want}x{frame}",
749+ "clip {} yielded {} bytes after skipping {skip}, need {want}x{frame}",
723750 path.to_string_lossy(),
724751 raw.stdout.len()
725752 );
726- let skip: usize = std::env::var("EC_H265_CLIP_SKIP")
727- .ok()
728- .and_then(|v| v.parse().ok())
729- .unwrap_or(0);
730753 let sources: Vec<Planes> = raw
731754 .stdout
732755 .chunks(frame)
733- .skip(skip)
734756 .take(want)
735757 .map(|f| {
736758 let (y, rest) = f.split_at(w * h);