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.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
openh264's MP4s say they are BT.709: both encoders (io/soft h264_encoder and GopEncoder) write the colour description in the stream's VUI (BT.709 primaries, transfer and matrix, limited range), as frames are converted and as the Mac's encoder tags them. Untagged, ffmpeg and other players read Windows and Linux renders as BT.601 and shifted reds and greens. io/soft's colour test reads the SPS each encoder writes. (closes #25)
shinyobjectz08a4b7df02cd
diff --git a/io/soft/src/h264.rs b/io/soft/src/h264.rsindex 0e97b9e1..ec08bab0 100644--- a/io/soft/src/h264.rs+++ b/io/soft/src/h264.rs@@ -248,7 +248,9 @@ impl GopEncoder {.intra_frame_period(openh264::encoder::IntraFramePeriod::from_num_frames(100_000,))- .num_threads(1);+ .num_threads(1)+ // BT.709 limited range, as h264_encoder tags its stream+ .vui(openh264::encoder::VuiConfig::bt709());let enc = H264Encoder::with_api_config(OpenH264API::from_source(), config).map_err(|e| format!("openh264: {e}"))?;Ok(GopEncoder {diff --git a/io/soft/src/lib.rs b/io/soft/src/lib.rsindex f806fa4b..f56f5eea 100644--- a/io/soft/src/lib.rs+++ b/io/soft/src/lib.rs@@ -258,7 +258,10 @@ pub fn h264_encoder(out: &Path, o: &EncodeOpts, intra_qp: Option<u8>) -> R<Box<d.max_frame_rate(FrameRate::from_hz(o.fps as f32)).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);+ .skip_frames(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());let config = match intra_qp {Some(qp) => config.rate_control_mode(RateControlMode::Off).qp(openh264::encoder::QpRange::new(qp, qp)).intra_frame_period(openh264::encoder::IntraFramePeriod::from_num_frames(1)),None => config,diff --git a/io/soft/tests/colour.rs b/io/soft/tests/colour.rsnew file mode 100644index 00000000..ec6bce90--- /dev/null+++ b/io/soft/tests/colour.rs@@ -0,0 +1,145 @@+//! openh264's streams say how their pictures were converted (BT.709, limited range, as raea_io_codec::yuv converts+//! them): an untagged stream reads as BT.601 in ffmpeg and other players, which shifts reds and greens (#25). Both+//! encoders are checked, from the SPS each one writes.+#![cfg(feature = "h264")]++use raea_io_codec::EncodeOpts;+use raea_io_soft::{GopEncoder, h264_encoder};++const W: u32 = 64;+const H: u32 = 64;++/// A picture half green, half red, as the issue's repro draws.+fn frame() -> Vec<u8> {+ (0..W * H).flat_map(|i| if i % W < W / 2 { [0x22, 0xb5, 0x73, 255] } else { [0xe8, 0x17, 0x4a, 255] }).collect()+}++/// Reads an RBSP's bits, Exp-Golomb codes among them.+struct Bits<'a> {+ b: &'a [u8],+ at: usize,+}++impl Bits<'_> {+ fn bit(&mut self) -> u32 {+ let v = (self.b[self.at / 8] >> (7 - self.at % 8)) & 1;+ self.at += 1;+ v as u32+ }+ fn u(&mut self, n: u32) -> u32 {+ (0..n).fold(0, |v, _| v << 1 | self.bit())+ }+ fn ue(&mut self) -> u32 {+ let mut zeros = 0;+ while self.bit() == 0 {+ zeros += 1;+ }+ (1 << zeros) - 1 + self.u(zeros)+ }+}++/// An SPS's colour description (primaries, transfer, matrix, full range), or None when its VUI doesn't carry one.+/// `sps` is the NAL unit, header byte first. Enough of H.264's 7.3.2.1.1 for openh264's Constrained Baseline streams.+fn colour(sps: &[u8]) -> Option<(u32, u32, u32, bool)> {+ // emulation prevention bytes (00 00 03) out+ let mut rbsp = Vec::with_capacity(sps.len());+ for &x in &sps[1..] {+ if x == 3 && rbsp.ends_with(&[0, 0]) {+ continue;+ }+ rbsp.push(x);+ }+ let mut r = Bits { b: &rbsp, at: 0 };+ let profile = r.u(8);+ assert!(matches!(profile, 66 | 77 | 88), "profile {profile} has chroma fields this reader skips");+ r.u(16); // constraint flags, level+ r.ue(); // seq_parameter_set_id+ r.ue(); // log2_max_frame_num_minus4+ match r.ue() {+ 0 => {+ r.ue();+ }+ 1 => panic!("pic_order_cnt_type 1 isn't read here"),+ _ => {}+ }+ r.ue(); // max_num_ref_frames+ r.bit(); // gaps_in_frame_num_value_allowed_flag+ r.ue();+ r.ue(); // the size in macroblocks+ if r.bit() == 0 {+ r.bit(); // mb_adaptive_frame_field_flag+ }+ r.bit(); // direct_8x8_inference_flag+ if r.bit() == 1 {+ (0..4).for_each(|_| {+ r.ue();+ });+ }+ if r.bit() == 0 {+ return None; // no VUI+ }+ if r.bit() == 1 && r.u(8) == 255 {+ r.u(32); // sar_width, sar_height+ }+ if r.bit() == 1 {+ r.bit(); // overscan_appropriate_flag+ }+ if r.bit() == 0 {+ return None; // no video_signal_type+ }+ r.u(3); // video_format+ let full = r.bit() == 1;+ if r.bit() == 0 {+ return None; // no colour_description+ }+ Some((r.u(8), r.u(8), r.u(8), full))+}++/// The SPS among Annex B NAL units (00 00 01 or 00 00 00 01 before each).+fn sps_in_annex_b(s: &[u8]) -> Vec<u8> {+ let starts: Vec<usize> = (0..s.len().saturating_sub(3)).filter(|&i| s[i..i + 3] == [0, 0, 1]).map(|i| i + 3).collect();+ for (k, &a) in starts.iter().enumerate() {+ if s[a] & 0x1f == 7 {+ let mut end = starts.get(k + 1).map_or(s.len(), |&n| n - 3);+ while end > a && s[end - 1] == 0 {+ end -= 1;+ }+ return s[a..end].to_vec();+ }+ }+ panic!("no SPS in the stream")+}++/// The first SPS in an MP4's avcC box.+fn sps_in_mp4(mp4: &[u8]) -> Vec<u8> {+ let at = mp4.windows(4).position(|w| w == b"avcC").expect("no avcC box") + 4;+ let c = &mp4[at..];+ assert!(c[5] & 0x1f >= 1, "avcC holds no SPS");+ let n = u16::from_be_bytes([c[6], c[7]]) as usize;+ c[8..8 + n].to_vec()+}++/// BT.709 primaries, transfer and matrix (H.273 code 1 each), limited range.+const BT709: Option<(u32, u32, u32, bool)> = Some((1, 1, 1, false));++#[test]+fn an_exports_stream_is_tagged_bt709() {+ let out = std::env::temp_dir().join(format!("raea-io-soft-colour-{}.mp4", std::process::id()));+ let o = EncodeOpts { width: W, height: H, fps: 30.0, kbps: 2000, quality: None, nv12: false, realtime: false };+ let mut enc = h264_encoder(&out, &o, None).unwrap();+ for _ in 0..3 {+ enc.push(&frame()).unwrap();+ }+ enc.finish(None).unwrap();+ let mp4 = std::fs::read(&out).unwrap();+ let _ = std::fs::remove_file(&out);+ assert_eq!(colour(&sps_in_mp4(&mp4)), BT709);+}++#[test]+fn a_gops_stream_is_tagged_bt709() {+ let mut enc = GopEncoder::new(W, H, 30.0, 24).unwrap();+ let (first, key) = enc.push(&frame()).unwrap();+ assert!(key);+ assert_eq!(colour(&sps_in_annex_b(&first)), BT709);+}