forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust375.8 MiB5c8eff8Tahinli2 months ago2949 commits

probe.rs86 lines, 3.5 KiBHistoryRawDownload
1//! The BD gate's source arming, shared with `examples/enc_probe` (lane-probe).
2//!
3//! The probe used to build its own ffmpeg command line and dropped the gate's
4//! `-ss` seek, so it read film B's black leader and every census taken through
5//! it was probe-relative (59 kB where the gate's own ladder point coded 89 kB
6//! of the same title). One definition now serves both: the gate's
7//! `clip_frames_vf` and the probe both call [`source`], so the two can only
8//! differ in the arguments they pass, which
9//! `encode::tests::the_probe_arms_the_gates_source` pins.
10
11use crate::encode::Picture;
12use std::process::Command;
13
14/// The video stream's coded size, from ffprobe.
15#[must_use]
16pub fn dims(clip: &str) -> Option<(usize, usize)> {
17 let out = Command::new("ffprobe")
18 .args(["-v", "error", "-select_streams", "v:0"])
19 .args(["-show_entries", "stream=width,height", "-of", "csv=p=0"])
20 .arg(clip)
21 .output()
22 .ok()?;
23 let text = String::from_utf8_lossy(&out.stdout);
24 let mut f = text.trim().split(',');
25 Some((f.next()?.trim().parse().ok()?, f.next()?.trim().parse().ok()?))
26}
27
28/// The native BD gate's window on `clip`: a whole number of 128-wide
29/// superblocks, at most 1920x1024, out of the MIDDLE of the coded frame, with
30/// even offsets for 4:2:0 -- as an ffmpeg `crop` filter plus its size.
31///
32/// This lives here because the probe used to be handed the crop by hand, and
33/// a hand-written `crop=1920:1024:960:568` (a 2160-high frame that film B,
34/// coded 3840x1608, is not) put the instrument on different pixels than the
35/// gate: 60 kB where the gate's own q=150 point coded 90 kB.
36///
37/// # Panics
38/// When ffprobe cannot read the size, or the frame is under one superblock.
39#[must_use]
40pub fn gate_crop(clip: &str) -> (String, usize, usize) {
41 let (nw, nh) = dims(clip).expect("ffprobe gave no size");
42 let cw = nw.min(1920) / 128 * 128;
43 let ch = nh.min(1024) / 128 * 128;
44 assert!(cw >= 128 && ch >= 128, "{nw}x{nh} is smaller than a superblock");
45 let (x, y) = ((nw - cw) / 2 & !1, (nh - ch) / 2 & !1);
46 (format!("crop={cw}:{ch}:{x}:{y}"), cw, ch)
47}
48
49/// `frames` pictures of `clip` from `skip` through the filter chain `vf`, as
50/// planar 8-bit 4:2:0 at `width`x`height` -- the BD gate's loader recipe.
51///
52/// # Panics
53/// When ffmpeg cannot be run, fails, or delivers a different number of frames.
54#[must_use]
55pub fn source(
56 clip: &str,
57 skip: &str,
58 vf: &str,
59 width: usize,
60 height: usize,
61 frames: usize,
62) -> Vec<Picture> {
63 let out = Command::new("ffmpeg")
64 .args(["-v", "error", "-ss", skip, "-i", clip])
65 .args(["-frames:v", &frames.to_string()])
66 .args(["-vf", vf])
67 .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
68 .output()
69 .expect("ffmpeg failed to run");
70 assert!(out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr));
71 let (luma, chroma) = (width * height, width * height / 4);
72 let frame_len = luma + 2 * chroma;
73 assert_eq!(out.stdout.len(), frame_len * frames, "expected {frames} 4:2:0 frames");
74 (0..frames)
75 .map(|i| {
76 let b = &out.stdout[i * frame_len..][..frame_len];
77 Picture {
78 width,
79 height,
80 y: b[..luma].iter().map(|&v| u16::from(v)).collect(),
81 u: b[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(),
82 v: b[luma + chroma..].iter().map(|&v| u16::from(v)).collect(),
83 }
84 })
85 .collect()
86}