Bound the clip gate's decode to the pictures it samples
Browse files at this commit6e90b9d2cff0b69ff1d1cf28810b424c6ee071dbParent: fea923b- fix
- bound the clip gate's decode to the pictures it samples
1 changed file +29 -7
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> = raw | 753 | let sources: Vec<Planes> = raw |
| 731 | .stdout | 754 | .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); |