diff --git a/crates/ec-hw/examples/hw_config.rs b/crates/ec-hw/examples/hw_config.rs new file mode 100644 index 00000000..267c080c --- /dev/null +++ b/crates/ec-hw/examples/hw_config.rs @@ -0,0 +1,139 @@ +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use ec_hw::{ + Backend, EncCodec, EncoderConfig, FrameMetadata, H264Profile, HwEncoder, I420, RateControlMode, +}; + +const WIDTH: u32 = 640; +const HEIGHT: u32 = 360; +const FRAMES: usize = 30; +const FPS: u32 = 30; +const GOP: u32 = 15; +const FORCE_AT: usize = 7; +const BITRATE: u32 = 2_000_000; +const FIELDS: [&str; 7] = [ + "profile", + "width", + "height", + "color_space", + "color_transfer", + "color_primaries", + "color_range", +]; + +fn config() -> EncoderConfig { + EncoderConfig { + bitrate: BITRATE, + framerate: (FPS, 1), + gop_size: GOP, + rate_control: RateControlMode::ConstantBitrate, + ..EncoderConfig::new(EncCodec::H264, WIDTH, HEIGHT) + } + .h264_profile(H264Profile::Main) + .colour(6, 6, 6, true) +} + +fn frame(t: usize) -> I420 { + let (w, h) = (WIDTH as usize, HEIGHT as usize); + let y = (0..w * h) + .map(|i| ((i % w + i / w + 3 * t) & 0xff) as u8) + .collect(); + let chroma = w / 2 * h / 2; + I420 { + y, + u: vec![(96 + t % 64) as u8; chroma], + v: vec![160; chroma], + width: WIDTH, + height: HEIGHT, + } +} + +fn encode(dir: &Path, name: &str, backend: Backend) -> Option { + let mut encoder = match HwEncoder::open(config(), Some(backend)) { + Ok(encoder) => encoder, + Err(e) => { + println!("{name}: {e}"); + return None; + } + }; + let mut stream = Vec::new(); + for t in 0..FRAMES { + let meta = FrameMetadata { + timestamp: t as i64, + force_keyframe: t == FORCE_AT, + }; + let coded = encoder.encode(&frame(t), meta).expect("encode frame"); + stream.extend_from_slice(&coded.data); + } + let path = dir.join(format!("{name}.h264")); + fs::write(&path, stream).expect("write stream"); + Some(path) +} + +fn probe(path: &Path) -> Vec<(String, String)> { + let out = Command::new("ffprobe") + .args(["-v", "error", "-select_streams", "v:0", "-show_entries"]) + .arg(format!("stream={}", FIELDS.join(","))) + .args(["-of", "default=nw=1"]) + .arg(path) + .output() + .expect("run ffprobe"); + String::from_utf8_lossy(&out.stdout) + .lines() + .filter_map(|l| l.split_once('=')) + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect() +} + +fn keyframes(path: &Path) -> Vec { + let out = Command::new("ffprobe") + .args([ + "-v", + "error", + "-select_streams", + "v:0", + "-show_entries", + "frame=key_frame", + ]) + .args(["-of", "csv=p=0"]) + .arg(path) + .output() + .expect("run ffprobe"); + String::from_utf8_lossy(&out.stdout) + .lines() + .enumerate() + .filter(|(_, l)| l.trim() == "1") + .map(|(i, _)| i) + .collect() +} + +fn value(info: &[(String, String)], key: &str) -> String { + info.iter() + .find(|(k, _)| k == key) + .map_or("-".to_string(), |(_, v)| v.clone()) +} + +fn main() { + let dir = PathBuf::from(std::env::args().nth(1).expect("scratch dir argument")); + let Some(vk) = encode(&dir, "vk", Backend::Vk) else { + return; + }; + let Some(va) = encode(&dir, "va", Backend::Va) else { + return; + }; + let (vk_info, va_info) = (probe(&vk), probe(&va)); + let mut same = true; + println!("{:<16}{:<16}{:<16}", "field", "vk", "va"); + for key in FIELDS { + let (a, b) = (value(&vk_info, key), value(&va_info, key)); + same &= a == b; + println!("{key:<16}{a:<16}{b:<16}"); + } + let (kv, ka) = (keyframes(&vk), keyframes(&va)); + same &= kv == ka; + let list = |k: &[usize]| k.iter().map(usize::to_string).collect::>().join(","); + println!("{:<16}{:<16}{:<16}", "keyframes", list(&kv), list(&ka)); + println!("match: {same}"); +} diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index 8251414d..9622c15e 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -53,6 +53,7 @@ use ec_va::{CapReport, Config, ConfigAttrib, Context, Display, Surface, SurfaceS use crate::error::{Error, Result}; use crate::frame::{Frame, I420, I420_16}; use crate::pool::{PooledSurface, SurfacePool}; +use crate::rate::{RATE_WINDOW_MS, RateFeedback, hrd_buffer_ms}; pub use crate::types::{ CodedFrame, Colour, EncCodec, EncoderConfig, FrameMetadata, H264Profile, RateControlMode, Tunings, @@ -78,64 +79,9 @@ impl H264Profile { } } -/// The rate window the request is spread over, in milliseconds (`window_size`). -const RATE_WINDOW_MS: u32 = 1000; - /// Pictures the pipelined path keeps in flight before the oldest is read back. const DEPTH: usize = 3; -/// The HRD buffer for a GOP of `gop_size` pictures, in milliseconds of the -/// requested bitrate. -/// -/// The buffer is what radeonsi's constant-bitrate loop works against, and it -/// must be short when pictures are mostly key frames. Measured at 720p, -/// testsrc2, 240 pictures, HEVC, requested 1, 3 and 8 Mb/s: -/// -/// | GOP | buffer | delivered (3 Mb/s request) | -/// |---|---|---| -/// | 1 | 1000 ms | 7.63 Mb/s (+154%) | -/// | 1 | 33 ms | 3.29 Mb/s (+9.5%) | -/// | 1 | 20 ms | 3.02 Mb/s (+0.7%) | -/// | 2 | 1000 ms | +9.4% | -/// | 5 | 1000 ms | +4.3% | -/// | 10 | 1000 ms | +1.8% | -/// | 30 | 1000 ms | +1.0%, 43.5 dB | -/// | 30 | 20 ms | +0.0%, 40.1 dB | -/// -/// So a GOP shorter than ten pictures gets a 20 ms buffer. A GOP of thirty keeps -/// the full rate window: the short buffer lands on the request there too, but -/// costs 3.4 dB at the same rate. -fn hrd_buffer_ms(gop_size: u32) -> u32 { - if gop_size < 10 { 20 } else { RATE_WINDOW_MS } -} - -/// Closed-loop correction of the driver's rate control, which lands off the ask -/// on detailed content (testsrc2: +12..19% on mesa 26.2.6, ffmpeg's VA-API -/// encoders too): once a second the bitrate sent is rescaled by the measured -/// bias, plus whatever was over- or under-spent so far. -#[derive(Debug, Clone, Copy)] -struct RateFeedback { - scale: f64, - spent: f64, - frames: u32, - window: f64, - window_frames: u32, - steered: bool, -} - -impl Default for RateFeedback { - fn default() -> Self { - RateFeedback { - scale: 1.0, - spent: 0.0, - frames: 0, - window: 0.0, - window_frames: 0, - steered: false, - } - } -} - impl RateControlMode { /// The `VAConfigAttribRateControl` value this mode is configured with. /// @@ -718,38 +664,11 @@ impl Encoder { } fn steered_bitrate(&self) -> u32 { - (f64::from(self.tunings.bitrate) * self.rc.scale) as u32 + self.rc.steered_bitrate(self.tunings.bitrate) } fn track_rate(&mut self, bytes: usize) { - if !matches!( - self.config.rate_control, - RateControlMode::ConstantBitrate | RateControlMode::VariableBitrate - ) { - return; - } - let (num, den) = self.tunings.framerate; - let fps = f64::from(num.max(1)) / f64::from(den.max(1)); - let rate = f64::from(self.tunings.bitrate); - let per_frame = rate / fps; - let rc = &mut self.rc; - let bits = bytes as f64 * 8.0; - rc.spent += bits; - rc.frames += 1; - rc.window += bits; - rc.window_frames += 1; - if f64::from(rc.window_frames) < fps.round().max(1.0) { - return; - } - let asked = rc.scale * per_frame * f64::from(rc.window_frames); - let bias = rc.window / asked; - let debt = f64::from(rc.frames) * per_frame - rc.spent; - let scale = ((rate + debt) / rate / bias).clamp(0.5, 1.5); - rc.window = 0.0; - rc.window_frames = 0; - if (scale / rc.scale - 1.0).abs() > 0.02 { - rc.scale = scale; - rc.steered = true; + if self.rc.track(bytes, self.config.rate_control, self.tunings) { self.tunings_dirty = true; } } @@ -799,7 +718,7 @@ impl Encoder { out.push(BufferType::EncMiscParameter(EncMiscParameter::FrameRate( EncMiscParameterFrameRate::new(packed, 0), ))); - if bits > 0 && !std::mem::take(&mut self.rc.steered) { + if bits > 0 && !self.rc.take_steered() { // The buffer starts full. Starting at three quarters full cost a // quarter of a window of budget: radeonsi then spent 12.5% under the // ask on a 2 s clip, and the gap is exactly that quarter over the diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index 722ed988..a99e5e84 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -15,8 +15,10 @@ use crate::picture::{Decoded, I420, I420_16, RgbPicture}; use crate::picture::{DmaBufPicture, i420_to_rgb}; #[cfg(target_os = "linux")] use ec_core::color::{ColorDescription, Matrix, Tags}; +use crate::rate::{RateFeedback, hrd_buffer_ms}; use crate::types::{ - Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, RateControlMode, Tunings, + Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, H264Profile, RateControlMode, + Tunings, }; use crate::vkdec_av1::VkAv1Decoder; use crate::vkdec_vp9::VkVp9Decoder; @@ -564,6 +566,8 @@ pub struct HwEncoder { #[cfg_attr(not(target_os = "linux"), allow(dead_code))] size: (usize, usize), pending: VecDeque, + tunings: Tunings, + rc: RateFeedback, } impl HwEncoder { @@ -585,6 +589,11 @@ impl HwEncoder { config, size, pending: VecDeque::new(), + tunings: Tunings { + bitrate: config.bitrate, + framerate: config.framerate, + }, + rc: RateFeedback::default(), }); } Err(e) => reasons.push(format!("{backend:?}: {e}")), @@ -610,12 +619,9 @@ impl HwEncoder { match &mut self.inner { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; - let rate = match self.config.rate_control { - RateControlMode::ConstantQp { .. } => vk_rate(&self.config), - _ => ec_vk::RateControl::Cbr { - bitrate: tunings.bitrate, - }, - }; + self.tunings = tunings; + self.rc = RateFeedback::default(); + let rate = vk_rate(&self.config, tunings.bitrate); enc.set_rate(rate, tunings.framerate)?; } #[cfg(target_os = "linux")] @@ -631,6 +637,9 @@ impl HwEncoder { vk_session_alive(self.born_epoch)?; self.pending.push_back(meta.timestamp); let out = enc.submit(input, meta.force_keyframe)?; + if let Some(coded) = &out { + self.steer(coded.data.len())?; + } Ok(out.map(|coded| stamp(&mut self.pending, coded))) } #[cfg(target_os = "linux")] @@ -652,6 +661,9 @@ impl HwEncoder { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; let frames = enc.flush()?; + for coded in &frames { + self.steer(coded.data.len())?; + } Ok(frames .into_iter() .map(|coded| stamp(&mut self.pending, coded)) @@ -670,6 +682,19 @@ impl HwEncoder { } } + fn steer(&mut self, bytes: usize) -> Result<()> { + if !self.rc.track(bytes, self.config.rate_control, self.tunings) { + return Ok(()); + } + let rate = vk_rate(&self.config, self.rc.steered_bitrate(self.tunings.bitrate)); + match &mut self.inner { + EncInner::Vk(enc) => enc.set_rate(rate, self.tunings.framerate)?, + #[cfg(target_os = "linux")] + EncInner::Va(_) => {} + } + Ok(()) + } + /// Encode one frame; `meta.force_keyframe` starts a new GOP. pub fn encode(&mut self, frame: &I420, meta: FrameMetadata) -> Result { self.idle()?; @@ -677,6 +702,7 @@ impl HwEncoder { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; let coded = enc.encode(frame, meta.force_keyframe)?; + self.steer(coded.data.len())?; Ok(CodedFrame { data: coded.data, timestamp: meta.timestamp, @@ -694,6 +720,7 @@ impl HwEncoder { match &mut self.inner { EncInner::Vk(enc) => { let coded = enc.encode_16(frame, meta.force_keyframe)?; + self.steer(coded.data.len())?; Ok(CodedFrame { data: coded.data, timestamp: meta.timestamp, @@ -712,6 +739,7 @@ impl HwEncoder { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; let coded = enc.encode_rgb(frame, meta.force_keyframe)?; + self.steer(coded.data.len())?; Ok(CodedFrame { data: coded.data, timestamp: meta.timestamp, @@ -775,6 +803,7 @@ impl HwEncoder { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; let coded = enc.encode_dmabuf(src, meta.force_keyframe)?; + self.steer(coded.data.len())?; Ok(CodedFrame { data: coded.data, timestamp: meta.timestamp, @@ -812,13 +841,19 @@ fn open_vk(config: EncoderConfig) -> Result { } let gpu = vk_gpu() .ok_or_else(|| Error::unsupported("Vulkan Video encode", "no Vulkan Video device"))?; - let rate = vk_rate(&config); + let rate = vk_rate(&config, config.bitrate); let (width, height, fps, gop_size) = ( config.width, config.height, config.framerate, config.gop_size, ); + let colour = config.colour.map(|c| ec_vk::Colour { + primaries: c.primaries, + transfer: c.transfer, + matrix: c.matrix, + full_range: c.full_range, + }); Ok(match config.codec { EncCodec::H264 => VkEncoder::H264(ec_vk::H264Encoder::new( &gpu, @@ -828,6 +863,8 @@ fn open_vk(config: EncoderConfig) -> Result { fps, gop_size, rate, + profile: vk_h264_profile(config.h264_profile), + colour, }, )?), EncCodec::H265 => VkEncoder::H265(ec_vk::H265Encoder::new( @@ -839,6 +876,7 @@ fn open_vk(config: EncoderConfig) -> Result { gop_size, rate, bit_depth: config.bit_depth, + colour, }, )?), EncCodec::Av1 => VkEncoder::Av1(ec_vk::Av1Encoder::new( @@ -850,12 +888,21 @@ fn open_vk(config: EncoderConfig) -> Result { gop_size, rate, bit_depth: config.bit_depth, + colour, }, )?), }) } -fn vk_rate(config: &EncoderConfig) -> ec_vk::RateControl { +fn vk_h264_profile(profile: H264Profile) -> ec_vk::H264Profile { + match profile { + H264Profile::ConstrainedBaseline => ec_vk::H264Profile::ConstrainedBaseline, + H264Profile::Main => ec_vk::H264Profile::Main, + H264Profile::High => ec_vk::H264Profile::High, + } +} + +fn vk_rate(config: &EncoderConfig, bitrate: u32) -> ec_vk::RateControl { match (config.rate_control, config.codec) { (RateControlMode::ConstantQp { qp }, EncCodec::Av1) => ec_vk::RateControl::ConstantQp { qp: (qp / 5).min(51) as u8, @@ -864,7 +911,8 @@ fn vk_rate(config: &EncoderConfig) -> ec_vk::RateControl { qp: qp.min(51) as u8, }, _ => ec_vk::RateControl::Cbr { - bitrate: config.bitrate, + bitrate, + buffer_ms: hrd_buffer_ms(config.gop_size), }, } } diff --git a/crates/ec-hw/src/lib.rs b/crates/ec-hw/src/lib.rs index 3e10669a..eccb604d 100644 --- a/crates/ec-hw/src/lib.rs +++ b/crates/ec-hw/src/lib.rs @@ -63,6 +63,7 @@ pub mod error; pub(crate) mod dpb264; #[cfg_attr(not(target_os = "linux"), allow(dead_code))] pub(crate) mod h264_common; +pub(crate) mod rate; pub(crate) mod ref_slots; pub(crate) mod vkdec_vp9; pub(crate) mod hevc_dpb; diff --git a/crates/ec-hw/src/rate.rs b/crates/ec-hw/src/rate.rs new file mode 100644 index 00000000..35e24982 --- /dev/null +++ b/crates/ec-hw/src/rate.rs @@ -0,0 +1,100 @@ +use crate::types::{RateControlMode, Tunings}; + +pub(crate) const RATE_WINDOW_MS: u32 = 1000; + +/// The HRD buffer for a GOP of `gop_size` pictures, in milliseconds of the +/// requested bitrate. +/// +/// The buffer is what radeonsi's constant-bitrate loop works against, and it +/// must be short when pictures are mostly key frames. Measured at 720p, +/// testsrc2, 240 pictures, HEVC, requested 1, 3 and 8 Mb/s: +/// +/// | GOP | buffer | delivered (3 Mb/s request) | +/// |---|---|---| +/// | 1 | 1000 ms | 7.63 Mb/s (+154%) | +/// | 1 | 33 ms | 3.29 Mb/s (+9.5%) | +/// | 1 | 20 ms | 3.02 Mb/s (+0.7%) | +/// | 2 | 1000 ms | +9.4% | +/// | 5 | 1000 ms | +4.3% | +/// | 10 | 1000 ms | +1.8% | +/// | 30 | 1000 ms | +1.0%, 43.5 dB | +/// | 30 | 20 ms | +0.0%, 40.1 dB | +/// +/// So a GOP shorter than ten pictures gets a 20 ms buffer. A GOP of thirty keeps +/// the full rate window: the short buffer lands on the request there too, but +/// costs 3.4 dB at the same rate. +pub(crate) fn hrd_buffer_ms(gop_size: u32) -> u32 { + if gop_size < 10 { 20 } else { RATE_WINDOW_MS } +} + +/// Closed-loop correction of the driver's rate control, which lands off the ask +/// on detailed content (testsrc2: +12..19% on mesa 26.2.6, ffmpeg's VA-API +/// encoders too): once a second the bitrate sent is rescaled by the measured +/// bias, plus whatever was over- or under-spent so far. +#[derive(Debug, Clone, Copy)] +pub(crate) struct RateFeedback { + scale: f64, + spent: f64, + frames: u32, + window: f64, + window_frames: u32, + steered: bool, +} + +impl Default for RateFeedback { + fn default() -> Self { + RateFeedback { + scale: 1.0, + spent: 0.0, + frames: 0, + window: 0.0, + window_frames: 0, + steered: false, + } + } +} + +impl RateFeedback { + pub(crate) fn steered_bitrate(&self, bitrate: u32) -> u32 { + (f64::from(bitrate) * self.scale) as u32 + } + + #[cfg_attr(not(target_os = "linux"), allow(dead_code))] + pub(crate) fn take_steered(&mut self) -> bool { + std::mem::take(&mut self.steered) + } + + /// Account one coded picture of `bytes`; true when the bitrate sent has changed. + pub(crate) fn track(&mut self, bytes: usize, mode: RateControlMode, tunings: Tunings) -> bool { + if !matches!( + mode, + RateControlMode::ConstantBitrate | RateControlMode::VariableBitrate + ) { + return false; + } + let (num, den) = tunings.framerate; + let fps = f64::from(num.max(1)) / f64::from(den.max(1)); + let rate = f64::from(tunings.bitrate); + let per_frame = rate / fps; + let bits = bytes as f64 * 8.0; + self.spent += bits; + self.frames += 1; + self.window += bits; + self.window_frames += 1; + if f64::from(self.window_frames) < fps.round().max(1.0) { + return false; + } + let asked = self.scale * per_frame * f64::from(self.window_frames); + let bias = self.window / asked; + let debt = f64::from(self.frames) * per_frame - self.spent; + let scale = ((rate + debt) / rate / bias).clamp(0.5, 1.5); + self.window = 0.0; + self.window_frames = 0; + if (scale / self.scale - 1.0).abs() > 0.02 { + self.scale = scale; + self.steered = true; + return true; + } + false + } +} diff --git a/crates/ec-vk/examples/av1_encode.rs b/crates/ec-vk/examples/av1_encode.rs index 77ea138b..865c6d87 100644 --- a/crates/ec-vk/examples/av1_encode.rs +++ b/crates/ec-vk/examples/av1_encode.rs @@ -31,10 +31,12 @@ fn main() { rate: match std::env::args().nth(2) { Some(b) => RateControl::Cbr { bitrate: b.parse().expect("bitrate"), + buffer_ms: 1000, }, None => RateControl::ConstantQp { qp: 26 }, }, bit_depth: 8, + colour: None, }; let mut encoder = Av1Encoder::new(&gpu, cfg).expect("create encoder"); diff --git a/crates/ec-vk/examples/h264_encode.rs b/crates/ec-vk/examples/h264_encode.rs index d37dc3c9..1c588db4 100644 --- a/crates/ec-vk/examples/h264_encode.rs +++ b/crates/ec-vk/examples/h264_encode.rs @@ -1,4 +1,4 @@ -use ec_vk::{Gpu, H264Encoder, H264EncoderConfig, RateControl}; +use ec_vk::{Gpu, H264Encoder, H264EncoderConfig, H264Profile, RateControl}; const W: u32 = 1280; const H: u32 = 720; @@ -27,8 +27,13 @@ fn main() { height: H, fps: (30, 1), gop_size: 30, + profile: H264Profile::High, + colour: None, rate: match std::env::args().nth(2) { - Some(b) => RateControl::Cbr { bitrate: b.parse().expect("bitrate") }, + Some(b) => RateControl::Cbr { + bitrate: b.parse().expect("bitrate"), + buffer_ms: 1000, + }, None => RateControl::ConstantQp { qp: 26 }, }, }; diff --git a/crates/ec-vk/examples/h265_encode.rs b/crates/ec-vk/examples/h265_encode.rs index f09c679b..d7d7da0b 100644 --- a/crates/ec-vk/examples/h265_encode.rs +++ b/crates/ec-vk/examples/h265_encode.rs @@ -30,10 +30,12 @@ fn main() { rate: match std::env::args().nth(2) { Some(b) => RateControl::Cbr { bitrate: b.parse().expect("bitrate"), + buffer_ms: 1000, }, None => RateControl::ConstantQp { qp: 26 }, }, bit_depth: 8, + colour: None, }; let mut encoder = H265Encoder::new(&gpu, cfg).expect("create encoder"); let mut out = Vec::new(); diff --git a/crates/ec-vk/src/av1.rs b/crates/ec-vk/src/av1.rs index b597394d..b8cd96eb 100644 --- a/crates/ec-vk/src/av1.rs +++ b/crates/ec-vk/src/av1.rs @@ -2,7 +2,7 @@ use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; #[cfg(target_os = "linux")] use crate::DmaBuf; -use crate::{CodedFrame, Error, Flight, Gpu, RateControl, last_frame}; +use crate::{CodedFrame, Colour, Error, Flight, Gpu, RateControl, last_frame}; const TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00]; @@ -21,6 +21,8 @@ pub struct Av1EncoderConfig { pub rate: RateControl, /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`). pub bit_depth: u8, + /// Colour config of the sequence header, or none. + pub colour: Option, } /// AV1 encoder: order hint, DPB slot and LAST reference bookkeeping over [`Av1EncodeSession`]. @@ -52,6 +54,7 @@ impl Av1Encoder { fps: cfg.fps, rate: cfg.rate, bit_depth: cfg.bit_depth, + colour: cfg.colour, }, )?; let sequence_header = session.sequence_header(); diff --git a/crates/ec-vk/src/h265.rs b/crates/ec-vk/src/h265.rs index b9153e44..66cbdd43 100644 --- a/crates/ec-vk/src/h265.rs +++ b/crates/ec-vk/src/h265.rs @@ -2,7 +2,7 @@ use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; #[cfg(target_os = "linux")] use crate::DmaBuf; -use crate::{CodedFrame, Error, Flight, Gpu, RateControl, last_frame}; +use crate::{CodedFrame, Colour, Error, Flight, Gpu, RateControl, last_frame}; /// H.265 encoder settings. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -19,6 +19,8 @@ pub struct H265EncoderConfig { pub rate: RateControl, /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`). pub bit_depth: u8, + /// Colour description for the VUI, or none. + pub colour: Option, } /// H.265 encoder: GOP, POC and DPB slot bookkeeping over [`H265EncodeSession`]. @@ -50,6 +52,7 @@ impl H265Encoder { fps: cfg.fps, rate: cfg.rate, bit_depth: cfg.bit_depth, + colour: cfg.colour, }, )?; let parameter_sets = session.parameter_sets()?; diff --git a/crates/ec-vk/src/lib.rs b/crates/ec-vk/src/lib.rs index a2dc9bbf..0cbe2cb3 100644 --- a/crates/ec-vk/src/lib.rs +++ b/crates/ec-vk/src/lib.rs @@ -17,13 +17,13 @@ pub use vk_video_ffi::{ Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams, Av1FrameHeader, Av1FrameType, Av1GlobalMotion, Av1InterpolationFilter, Av1LoopFilter, Av1LoopRestoration, Av1Quantization, Av1RefSlot, Av1Restoration, Av1Segmentation, - Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, Codec, CodecProfile, Direction, - Error, Gpu, H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, H264RefSlot, - H264Sps, H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, - H265LongTermRefPicSps, H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, H265ScalingLists, - H265ShortTermRefPicSet, H265Sps, H265Vps, RateControl, ScalingLists, Vp9ColorConfig, - Vp9DecodeConfig, Vp9DecodePicture, Vp9DecodeSession, Vp9FrameHeader, Vp9InterpolationFilter, - Vp9LoopFilter, Vp9Segmentation, + Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, Codec, CodecProfile, Colour, + Direction, Error, Gpu, H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, + H264Profile, H264RefSlot, H264Sps, H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, + H265DecodeSession, H265LongTermRefPicSps, H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, + H265ScalingLists, H265ShortTermRefPicSet, H265Sps, H265Vps, RateControl, ScalingLists, + Vp9ColorConfig, Vp9DecodeConfig, Vp9DecodePicture, Vp9DecodeSession, Vp9FrameHeader, + Vp9InterpolationFilter, Vp9LoopFilter, Vp9Segmentation, }; /// Encoder settings. @@ -39,6 +39,10 @@ pub struct H264EncoderConfig { pub gop_size: u32, /// Rate control. pub rate: RateControl, + /// H.264 profile. + pub profile: H264Profile, + /// Colour description for the VUI, or none. + pub colour: Option, } /// One coded frame, Annex-B. Keyframes carry SPS and PPS before the slice. @@ -102,7 +106,8 @@ impl H264Encoder { height: cfg.height, fps: cfg.fps, rate: cfg.rate, - profile_high: true, + profile: cfg.profile, + colour: cfg.colour, }, )?; let parameter_sets = session.parameter_sets()?; diff --git a/shims/vk-video-ffi/examples/dmabuf_smoke.rs b/shims/vk-video-ffi/examples/dmabuf_smoke.rs index c5a8815f..3c48073a 100644 --- a/shims/vk-video-ffi/examples/dmabuf_smoke.rs +++ b/shims/vk-video-ffi/examples/dmabuf_smoke.rs @@ -3,7 +3,7 @@ use std::os::fd::{AsFd, FromRawFd, OwnedFd}; use vk_video_ffi::{ Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref, DmaBuf, DmaBufFormat, DmaBufPlane, Error, Gpu, H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, H265EncodeConfig, - H265EncodeSession, H265Picture, H265Ref, RateControl, + H265EncodeSession, H265Picture, H265Ref, H264Profile, RateControl, }; use vulkanalia::prelude::v1_3::*; use vulkanalia::vk::KhrExternalMemoryFdExtensionDeviceCommands; @@ -87,7 +87,8 @@ impl Encode for H264EncodeSession { height: size.1, fps: FPS, rate: RATE, - profile_high: true, + profile: H264Profile::High, + colour: None, }; H264EncodeSession::new(gpu, &cfg) } @@ -129,6 +130,7 @@ impl Encode for H265EncodeSession { fps: FPS, rate: RATE, bit_depth: bits, + colour: None, }; H265EncodeSession::new(gpu, &cfg) } @@ -172,6 +174,7 @@ impl Encode for Av1EncodeSession { fps: FPS, rate: RATE, bit_depth: bits, + colour: None, }; Av1EncodeSession::new(gpu, &cfg) } diff --git a/shims/vk-video-ffi/src/av1_encode.rs b/shims/vk-video-ffi/src/av1_encode.rs index 864b4337..39743122 100644 --- a/shims/vk-video-ffi/src/av1_encode.rs +++ b/shims/vk-video-ffi/src/av1_encode.rs @@ -7,10 +7,10 @@ use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands; use crate::dmabuf::DmaBuf; use crate::rgb_convert::{Input, check_rgb}; use crate::session::{ - DPB_SLOTS, Encoder, INITIAL_VIRTUAL_BUFFER_MS, VIRTUAL_BUFFER_MS, VideoSession, - component_bit_depth, picture_resource, validate_rate, video_caps, vk, zeroed, + DPB_SLOTS, Encoder, VideoSession, component_bit_depth, picture_resource, validate_rate, + video_caps, vk, zeroed, }; -use crate::{Error, Gpu, RateControl}; +use crate::{Colour, Error, Gpu, RateControl}; const AV1_SLOTS: usize = 8; const LEVEL: vk::video::StdVideoAV1Level = vk::video::STD_VIDEO_AV1_LEVEL_5_1; @@ -29,6 +29,8 @@ pub struct Av1EncodeConfig { pub rate: RateControl, /// Luma and chroma bit depth: 8 or 10 (Main profile, NV12 or P010 input). pub bit_depth: u8, + /// Colour config of the sequence header, or none (unspecified). + pub colour: Option, } /// One picture to encode. @@ -102,19 +104,22 @@ struct RateChain { impl RateChain { fn new(rate: RateControl, fps: (u32, u32)) -> Box { - let (mode, layers, bitrate) = match rate { + let (mode, layers, bitrate, buffer_ms) = match rate { RateControl::ConstantQp { .. } => { - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0) - } - RateControl::Cbr { bitrate } => { - (vk::VideoEncodeRateControlModeFlagsKHR::CBR, 1, bitrate) + (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) } + RateControl::Cbr { bitrate, buffer_ms } => ( + vk::VideoEncodeRateControlModeFlagsKHR::CBR, + 1, + bitrate, + buffer_ms, + ), }; let mut c = Box::new(RateChain { rc: vk::VideoEncodeRateControlInfoKHR::builder() .rate_control_mode(mode) - .virtual_buffer_size_in_ms(VIRTUAL_BUFFER_MS) - .initial_virtual_buffer_size_in_ms(INITIAL_VIRTUAL_BUFFER_MS) + .virtual_buffer_size_in_ms(buffer_ms) + .initial_virtual_buffer_size_in_ms(buffer_ms) .build(), rc_av1: vk::VideoEncodeAV1RateControlInfoKHR::builder() .flags(vk::VideoEncodeAV1RateControlFlagsKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_REGULAR_GOP | vk::VideoEncodeAV1RateControlFlagsKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_REFERENCE_PATTERN_FLAT) @@ -209,11 +214,11 @@ impl Av1EncodeSession { (cfg.rate, cfg.fps), caps.size_align, )?; - let params = create_parameters(&enc.vs, extent, sb128, cfg.bit_depth)?; + let params = create_parameters(&enc.vs, extent, sb128, cfg)?; enc.vs.set_params(params); Ok(Av1EncodeSession { enc, - seq: sequence_header_obu(cfg.width, cfg.height, sb128, cfg.bit_depth), + seq: sequence_header_obu(cfg, sb128), slot_key: [false; DPB_SLOTS], ref_order_hint: [0; AV1_SLOTS], }) @@ -492,16 +497,24 @@ fn create_parameters( vs: &VideoSession, extent: vk::Extent2D, sb128: bool, - bit_depth: u8, + cfg: &Av1EncodeConfig, ) -> Result { let mut color = zeroed::(); - color.BitDepth = bit_depth; + color.BitDepth = cfg.bit_depth; color.subsampling_x = 1; color.subsampling_y = 1; color.color_primaries = vk::video::STD_VIDEO_AV1_COLOR_PRIMARIES_UNSPECIFIED; color.transfer_characteristics = vk::video::STD_VIDEO_AV1_TRANSFER_CHARACTERISTICS_UNSPECIFIED; color.matrix_coefficients = vk::video::STD_VIDEO_AV1_MATRIX_COEFFICIENTS_UNSPECIFIED; color.chroma_sample_position = vk::video::STD_VIDEO_AV1_CHROMA_SAMPLE_POSITION_UNKNOWN; + if let Some(c) = cfg.colour { + color.flags.set_color_description_present_flag(1); + color.flags.set_color_range(c.full_range as u32); + color.color_primaries = vk::video::StdVideoAV1ColorPrimaries(i32::from(c.primaries)); + color.transfer_characteristics = + vk::video::StdVideoAV1TransferCharacteristics(i32::from(c.transfer)); + color.matrix_coefficients = vk::video::StdVideoAV1MatrixCoefficients(i32::from(c.matrix)); + } let mut seq = zeroed::(); seq.flags.set_enable_order_hint(1); @@ -578,7 +591,8 @@ fn obu(kind: u8, payload: &[u8]) -> Vec { out } -fn sequence_header_obu(width: u32, height: u32, sb128: bool, bit_depth: u8) -> Vec { +fn sequence_header_obu(cfg: &Av1EncodeConfig, sb128: bool) -> Vec { + let (width, height, bit_depth) = (cfg.width, cfg.height, cfg.bit_depth); let mut b = Bits(Vec::new()); b.put(0, 3); b.put(0, 1); @@ -612,8 +626,19 @@ fn sequence_header_obu(width: u32, height: u32, sb128: bool, bit_depth: u8) -> V b.put(0, 1); b.put((bit_depth == 10) as u32, 1); b.put(0, 1); - b.put(0, 1); - b.put(0, 1); + match cfg.colour { + Some(c) => { + b.put(1, 1); + b.put(u32::from(c.primaries), 8); + b.put(u32::from(c.transfer), 8); + b.put(u32::from(c.matrix), 8); + b.put(c.full_range as u32, 1); + } + None => { + b.put(0, 1); + b.put(0, 1); + } + } b.put(0, 2); b.put(0, 1); b.put(0, 1); diff --git a/shims/vk-video-ffi/src/h264_encode.rs b/shims/vk-video-ffi/src/h264_encode.rs index 3988048f..6b08b66c 100644 --- a/shims/vk-video-ffi/src/h264_encode.rs +++ b/shims/vk-video-ffi/src/h264_encode.rs @@ -9,10 +9,10 @@ use vulkanalia::vk::{ use crate::dmabuf::DmaBuf; use crate::rgb_convert::{Input, check_rgb}; use crate::session::{ - DPB_SLOTS, Encoder, INITIAL_VIRTUAL_BUFFER_MS, VIRTUAL_BUFFER_MS, VideoSession, align, - picture_resource, validate_rate, video_caps, vk, zeroed, + DPB_SLOTS, Encoder, VideoSession, align, picture_resource, validate_rate, video_caps, vk, + zeroed, }; -use crate::{Error, Gpu}; +use crate::{Colour, Error, Gpu}; /// Rate control of an encode session. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -26,9 +26,22 @@ pub enum RateControl { Cbr { /// Target bitrate in bit/s. bitrate: u32, + /// HRD virtual buffer in milliseconds of `bitrate`, also the initial fullness. + buffer_ms: u32, }, } +/// H.264 profile written into the SPS and PPS. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum H264Profile { + /// `profile_idc` 66 with `constraint_set1_flag`; CAVLC. + ConstrainedBaseline, + /// `profile_idc` 77; CABAC, no 8x8 transform. + Main, + /// `profile_idc` 100; CABAC with the 8x8 transform. + High, +} + /// Settings fixed for the life of a session. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct H264EncodeConfig { @@ -40,8 +53,10 @@ pub struct H264EncodeConfig { pub fps: (u32, u32), /// Rate control. pub rate: RateControl, - /// High profile when true, Main otherwise. - pub profile_high: bool, + /// H.264 profile. + pub profile: H264Profile, + /// Colour description for the VUI, or none. + pub colour: Option, } /// One picture to encode. @@ -87,11 +102,11 @@ struct Profile { } impl Profile { - fn new(high: bool) -> Box { - let idc = if high { - vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH - } else { - vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN + fn new(profile: H264Profile) -> Box { + let idc = match profile { + H264Profile::ConstrainedBaseline => vk::video::STD_VIDEO_H264_PROFILE_IDC_BASELINE, + H264Profile::Main => vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN, + H264Profile::High => vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH, }; let mut p = Box::new(Profile { info: vk::VideoProfileInfoKHR::builder() @@ -124,19 +139,22 @@ struct RateChain { impl RateChain { fn new(rate: RateControl, fps: (u32, u32)) -> Box { - let (mode, layers, bitrate) = match rate { + let (mode, layers, bitrate, buffer_ms) = match rate { RateControl::ConstantQp { .. } => { - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0) - } - RateControl::Cbr { bitrate } => { - (vk::VideoEncodeRateControlModeFlagsKHR::CBR, 1, bitrate) + (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) } + RateControl::Cbr { bitrate, buffer_ms } => ( + vk::VideoEncodeRateControlModeFlagsKHR::CBR, + 1, + bitrate, + buffer_ms, + ), }; let mut c = Box::new(RateChain { rc: vk::VideoEncodeRateControlInfoKHR::builder() .rate_control_mode(mode) - .virtual_buffer_size_in_ms(VIRTUAL_BUFFER_MS) - .initial_virtual_buffer_size_in_ms(INITIAL_VIRTUAL_BUFFER_MS) + .virtual_buffer_size_in_ms(buffer_ms) + .initial_virtual_buffer_size_in_ms(buffer_ms) .build(), rc_h264: vk::VideoEncodeH264RateControlInfoKHR::builder() .flags( @@ -186,8 +204,11 @@ impl H264EncodeSession { let width = align(cfg.width, 16); let height = align(cfg.height, 16); let level = level_for(width, height, cfg.fps)?; - let profile = Profile::new(cfg.profile_high); - let caps = query_caps(gpu, &profile.info)?; + let profile = Profile::new(cfg.profile); + let caps = query_caps(gpu, &profile.info).map_err(|e| match e { + Error::Vk(..) => Error::Unsupported(format!("H.264 {:?} encode", cfg.profile)), + other => other, + })?; if width > caps.max_extent.width || height > caps.max_extent.height { return Err(Error::Unsupported(format!("H.264 encode {width}x{height}"))); } @@ -472,10 +493,10 @@ fn create_parameters( let aw = align(cfg.width, 16); let ah = align(cfg.height, 16); let cropped = aw != cfg.width || ah != cfg.height; - let profile_idc = if cfg.profile_high { - vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH - } else { - vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN + let profile_idc = match cfg.profile { + H264Profile::ConstrainedBaseline => vk::video::STD_VIDEO_H264_PROFILE_IDC_BASELINE, + H264Profile::Main => vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN, + H264Profile::High => vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH, }; let mut vui = zeroed::(); @@ -487,6 +508,15 @@ fn create_parameters( vui.time_scale = cfg.fps.0 * 2; vui.max_num_reorder_frames = 0; vui.max_dec_frame_buffering = 1; + if let Some(c) = cfg.colour { + vui.flags.set_video_signal_type_present_flag(1); + vui.flags.set_video_full_range_flag(c.full_range as u32); + vui.flags.set_color_description_present_flag(1); + vui.video_format = 5; + vui.colour_primaries = c.primaries; + vui.transfer_characteristics = c.transfer; + vui.matrix_coefficients = c.matrix; + } let mut sps = zeroed::(); sps.flags.set_direct_8x8_inference_flag(1); @@ -494,6 +524,8 @@ fn create_parameters( sps.flags.set_frame_cropping_flag(cropped as u32); sps.flags.set_vui_parameters_present_flag(1); sps.profile_idc = profile_idc; + sps.flags + .set_constraint_set1_flag((cfg.profile == H264Profile::ConstrainedBaseline) as u32); sps.level_idc = level; sps.chroma_format_idc = vk::video::STD_VIDEO_H264_CHROMA_FORMAT_IDC_420; sps.pic_order_cnt_type = vk::video::STD_VIDEO_H264_POC_TYPE_2; @@ -506,9 +538,10 @@ fn create_parameters( let mut pps = zeroed::(); pps.flags - .set_transform_8x8_mode_flag((cfg.profile_high) as u32); + .set_transform_8x8_mode_flag((cfg.profile == H264Profile::High) as u32); pps.flags.set_deblocking_filter_control_present_flag(1); - pps.flags.set_entropy_coding_mode_flag(1); + pps.flags + .set_entropy_coding_mode_flag((cfg.profile != H264Profile::ConstrainedBaseline) as u32); pps.weighted_bipred_idc = vk::video::STD_VIDEO_H264_WEIGHTED_BIPRED_IDC_DEFAULT; let add = vk::VideoEncodeH264SessionParametersAddInfoKHR::builder() diff --git a/shims/vk-video-ffi/src/h265_encode.rs b/shims/vk-video-ffi/src/h265_encode.rs index 9fb0d5d3..8b61a669 100644 --- a/shims/vk-video-ffi/src/h265_encode.rs +++ b/shims/vk-video-ffi/src/h265_encode.rs @@ -10,10 +10,10 @@ use crate::dmabuf::DmaBuf; use crate::h264_encode::validate; use crate::rgb_convert::{Input, check_rgb}; use crate::session::{ - DPB_SLOTS, Encoder, INITIAL_VIRTUAL_BUFFER_MS, VIRTUAL_BUFFER_MS, VideoSession, align, - component_bit_depth, picture_resource, video_caps, vk, zeroed, + DPB_SLOTS, Encoder, VideoSession, align, component_bit_depth, picture_resource, video_caps, vk, + zeroed, }; -use crate::{Error, Gpu, RateControl}; +use crate::{Colour, Error, Gpu, RateControl}; /// Settings fixed for the life of a session. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -28,6 +28,8 @@ pub struct H265EncodeConfig { pub rate: RateControl, /// Luma and chroma bit depth: 8 (Main, NV12 input) or 10 (Main 10, P010 input). pub bit_depth: u8, + /// Colour description for the VUI, or none. + pub colour: Option, } /// One picture to encode. @@ -115,19 +117,22 @@ struct RateChain { impl RateChain { fn new(rate: RateControl, fps: (u32, u32)) -> Box { - let (mode, layers, bitrate) = match rate { + let (mode, layers, bitrate, buffer_ms) = match rate { RateControl::ConstantQp { .. } => { - (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0) - } - RateControl::Cbr { bitrate } => { - (vk::VideoEncodeRateControlModeFlagsKHR::CBR, 1, bitrate) + (vk::VideoEncodeRateControlModeFlagsKHR::DISABLED, 0, 0, 0) } + RateControl::Cbr { bitrate, buffer_ms } => ( + vk::VideoEncodeRateControlModeFlagsKHR::CBR, + 1, + bitrate, + buffer_ms, + ), }; let mut c = Box::new(RateChain { rc: vk::VideoEncodeRateControlInfoKHR::builder() .rate_control_mode(mode) - .virtual_buffer_size_in_ms(VIRTUAL_BUFFER_MS) - .initial_virtual_buffer_size_in_ms(INITIAL_VIRTUAL_BUFFER_MS) + .virtual_buffer_size_in_ms(buffer_ms) + .initial_virtual_buffer_size_in_ms(buffer_ms) .build(), rc_h265: vk::VideoEncodeH265RateControlInfoKHR::builder() .flags( @@ -529,6 +534,15 @@ fn create_parameters( vui.flags.set_vui_poc_proportional_to_timing_flag(1); vui.vui_num_units_in_tick = cfg.fps.1; vui.vui_time_scale = cfg.fps.0; + if let Some(c) = cfg.colour { + vui.flags.set_video_signal_type_present_flag(1); + vui.flags.set_video_full_range_flag(c.full_range as u32); + vui.flags.set_colour_description_present_flag(1); + vui.video_format = 5; + vui.colour_primaries = c.primaries; + vui.transfer_characteristics = c.transfer; + vui.matrix_coeffs = c.matrix; + } let mut vps = zeroed::(); vps.flags.set_vps_temporal_id_nesting_flag(1); diff --git a/shims/vk-video-ffi/src/lib.rs b/shims/vk-video-ffi/src/lib.rs index 2e169c43..e9cc175d 100644 --- a/shims/vk-video-ffi/src/lib.rs +++ b/shims/vk-video-ffi/src/lib.rs @@ -36,7 +36,9 @@ pub use h264_decode::{ H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, H264RefSlot, H264Sps, ScalingLists, }; -pub use h264_encode::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, RateControl}; +pub use h264_encode::{ + H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, RateControl, +}; pub use h265_decode::{ H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, H265LongTermRefPicSps, H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, H265ScalingLists, H265ShortTermRefPicSet, @@ -44,6 +46,19 @@ pub use h265_decode::{ }; pub use h265_encode::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; +/// Colour description written into the VUI (H.264, H.265) or the colour config (AV1). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Colour { + /// `colour_primaries` (H.273 code points). + pub primaries: u8, + /// `transfer_characteristics`. + pub transfer: u8, + /// `matrix_coefficients`. + pub matrix: u8, + /// `video_full_range_flag` / `color_range`. + pub full_range: bool, +} + use vk::KhrVideoQueueExtensionInstanceCommands; use vulkanalia::loader::LibloadingLoader; use vulkanalia::prelude::v1_3::*; diff --git a/shims/vk-video-ffi/src/session.rs b/shims/vk-video-ffi/src/session.rs index b5de7b75..96ad63e2 100644 --- a/shims/vk-video-ffi/src/session.rs +++ b/shims/vk-video-ffi/src/session.rs @@ -19,8 +19,6 @@ pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNOR pub(crate) const DPB_SLOTS: usize = 2; pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0; pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1; -pub(crate) const VIRTUAL_BUFFER_MS: u32 = 25; -pub(crate) const INITIAL_VIRTUAL_BUFFER_MS: u32 = 12; pub(crate) type Stage = (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2); @@ -67,7 +65,8 @@ pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Er } match rate { RateControl::ConstantQp { qp } if qp > 51 => Err(Error::Invalid("qp above 51".into())), - RateControl::Cbr { bitrate: 0 } => Err(Error::Invalid("bitrate is zero".into())), + RateControl::Cbr { bitrate: 0, .. } => Err(Error::Invalid("bitrate is zero".into())), + RateControl::Cbr { buffer_ms: 0, .. } => Err(Error::Invalid("buffer is zero".into())), _ => Ok(()), } }