Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

openh264 keeps every block of a slowly changing dark ground at --quality master (#169)

shinyobjectz3a08d33f37be
diff --git a/io/soft/src/lib.rs b/io/soft/src/lib.rs
index 3802771c..0c500668 100644
--- a/io/soft/src/lib.rs
+++ b/io/soft/src/lib.rs
@@ -282,6 +282,12 @@ pub fn h264_encoder(out: &Path, o: &EncodeOpts, intra_qp: Option<u8>) -> R<Box<d
.rate_control_mode(RateControlMode::Bitrate)
// every frame is a sample: rate control may not drop one (the edit has a frame for every tick)
.skip_frames(false)
+ // no background detection: it takes a slowly changing dark ground (a night scene's sky, a glow drifting over
+ // black) for still and copies its old blocks forward, which undoes the bits adaptive quantization gives flat
+ // darks. A dim glow drifting at --quality master blotched in every sampled frame (6-8% of dark blocks
+ // stepping, raea check) at any bitrate or quantiser; without it, 0.1%, as a lossless encode of the same
+ // frames (#169). Adaptive quantization stays on: off, the darks blotch with or without this.
+ .background_detection(false)
// BT.709 limited range, as raea_io_codec::yuv converts the frames and as the Mac's encoder tags them (vt.rs):
// untagged, ffmpeg and other players read it as BT.601 and shift reds and greens
.vui(openh264::encoder::VuiConfig::bt709());
diff --git a/io/soft/tests/quality.rs b/io/soft/tests/quality.rs
index dcbc0c90..d3bc487f 100644
--- a/io/soft/tests/quality.rs
+++ b/io/soft/tests/quality.rs
@@ -75,3 +75,44 @@ fn a_quantiser_below_openh264s_floor_is_its_floor() {
// openh264 refuses a pinned QP under 14 with rate control on (Native:3); asked for less, it gets 14
assert!(gop(4) == gop(14));
}
+
+/// A dim glow drifting over near-black, frame `k` of a slow move: the flat darks of a night scene, which openh264's
+/// background detection took for still ground and copied forward from earlier frames (#169, "the encoder starved the
+/// near-blacks" at --quality master on Windows)
+fn glow(w: u32, h: u32, k: u32) -> Vec<u8> {
+ let (cx, cy, r) = (w as f32 * (0.3 + k as f32 * 0.004), h as f32 * 0.45, w as f32 * 0.8);
+ (0..w * h).flat_map(|i| {
+ let (dx, dy) = ((i % w) as f32 - cx, (i / w) as f32 - cy);
+ let v = (2.0 + 36.0 * (1.0 - (dx * dx + dy * dy).sqrt() / r).max(0.0).powi(2)).round() as u8;
+ [v, v, v, 255]
+ }).collect()
+}
+
+/// The share of 8x8 blocks whose mean red is more than a level off between two RGBA pictures
+fn blocks_off(a: &[u8], b: &[u8], w: u32, h: u32) -> f64 {
+ let (bw, bh) = (w as usize / 8, h as usize / 8);
+ let mean = |p: &[u8], bx: usize, by: usize| (0..64).map(|k| p[((by * 8 + k / 8) * w as usize + bx * 8 + k % 8) * 4] as f32).sum::<f32>() / 64.0;
+ let off = (0..bw * bh).filter(|i| (mean(a, i % bw, i / bw) - mean(b, i % bw, i / bw)).abs() > 1.0).count();
+ off as f64 / (bw * bh) as f64
+}
+
+#[test]
+fn a_dark_glow_drifting_keeps_every_block_at_master() {
+ use raea_io_codec::Backend;
+ let (w, h, n) = (640, 360, 45);
+ let o = EncodeOpts { width: w, height: h, fps: 30.0, kbps: 10_000, quality: Some(0.95), nv12: false, realtime: false };
+ let out = std::env::temp_dir().join(format!("raea-soft-darks-{}.mp4", std::process::id()));
+ let mut enc = h264_encoder(&out, &o, None).unwrap();
+ for k in 0..n {
+ enc.push(&glow(w, h, k)).unwrap();
+ }
+ enc.finish(None).unwrap();
+ let mut dec = raea_io_soft::Soft.open_video(&out).unwrap();
+ let last = dec.frame_at((n - 1) as f64 / 30.0).unwrap();
+ let _ = std::fs::remove_file(&out);
+ let off = blocks_off(&last.rgba, &glow(w, h, n - 1), w, h);
+ eprintln!("blocks off by more than a level: {:.2}%", off * 100.0);
+ // with background detection on, the ground lagged the glow: blocks stood where earlier frames had left them, 5.3% of
+ // them more than a level off by frame 44; without it 1.6% (the rest is the quantiser's)
+ assert!(off < 0.03, "{:.1}% of 8x8 blocks more than a level off the picture at frame {}", off * 100.0, n - 1);
+}