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() {
435 if let Ok(w) = std::env::var("EC_H265_CHROMA_W") {435 if let Ok(w) = std::env::var("EC_H265_CHROMA_W") {
436 cfg.chroma_rd_weight = w.parse().expect("EC_H265_CHROMA_W must be a number");436 cfg.chroma_rd_weight = w.parse().expect("EC_H265_CHROMA_W must be a number");
437 }437 }
438+ if let Ok(v) = std::env::var("EC_H265_NXN") {
439+ cfg.intra_nxn = v != "0";
440+ }
438 if let Ok(k) = std::env::var("EC_H265_RDO_CANDIDATES") {441 if let Ok(k) = std::env::var("EC_H265_RDO_CANDIDATES") {
439 cfg.rdo_candidates = k.parse().expect("EC_H265_RDO_CANDIDATES must be a number");442 cfg.rdo_candidates = k.parse().expect("EC_H265_RDO_CANDIDATES must be a number");
440 }443 }
@@ -705,10 +708,34 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Plane
705 .split_once(',')708 .split_once(',')
706 .and_then(|(w, h)| Some((w.parse::<usize>().ok()?, h.parse::<usize>().ok()?)))709 .and_then(|(w, h)| Some((w.parse::<usize>().ok()?, h.parse::<usize>().ok()?)))
707 .expect("ffprobe returned dimensions");710 .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.
708 let raw = std::process::Command::new("ffmpeg")723 let raw = std::process::Command::new("ffmpeg")
709 .args(["-v", "error", "-i"])724 .args(["-v", "error", "-i"])
710 .arg(path)725 .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+ ])
712 .output()739 .output()
713 .expect("ffmpeg raw extract");740 .expect("ffmpeg raw extract");
714 assert!(741 assert!(
@@ -719,18 +746,13 @@ fn clip_sources(path: &std::ffi::OsStr, want: usize) -> (usize, usize, Vec<Plane
719 let frame = w * h * 3 / 2;746 let frame = w * h * 3 / 2;
720 assert!(747 assert!(
721 raw.stdout.len().is_multiple_of(frame) && raw.stdout.len() / frame >= want,748 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}",
723 path.to_string_lossy(),750 path.to_string_lossy(),
724 raw.stdout.len()751 raw.stdout.len()
725 );752 );
726- let skip: usize = std::env::var("EC_H265_CLIP_SKIP")
727- .ok()
728- .and_then(|v| v.parse().ok())
729- .unwrap_or(0);
730 let sources: Vec<Planes> = raw753 let sources: Vec<Planes> = raw
731 .stdout754 .stdout
732 .chunks(frame)755 .chunks(frame)
733- .skip(skip)
734 .take(want)756 .take(want)
735 .map(|f| {757 .map(|f| {
736 let (y, rest) = f.split_at(w * h);758 let (y, rest) = f.split_at(w * h);