Vk runtime rate change: set_rate in the shim sessions (rate-control re-sent inside the coding scope, no new IDR), ec-vk set_rate, HwEncoder::tune for Vk and VA; hw_tune example
Browse files at this commit0c81983d348de729daa62ad92b670450e7bbd0e5Parent: a26af318 changed files +266 -44
- crates/ec-hw/examples/hw_tune.rs +102 -0
- crates/ec-hw/src/hw.rs +39 -2
- crates/ec-vk/src/av1.rs +5 -0
- crates/ec-vk/src/h265.rs +5 -0
- crates/ec-vk/src/lib.rs +5 -0
- shims/vk-video-ffi/src/av1_encode.rs +36 -19
- shims/vk-video-ffi/src/h264_encode.rs +38 -11
- shims/vk-video-ffi/src/h265_encode.rs +36 -12
crates/ec-hw/examples/hw_tune.rsadded+102 -0
| @@ -0,0 +1,102 @@ | |||
| 1 | +use ec_hw::{ | ||
| 2 | + Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, RateControlMode, RgbFrame, | ||
| 3 | + RgbOrder, Tunings, | ||
| 4 | +}; | ||
| 5 | + | ||
| 6 | +const W: u32 = 1280; | ||
| 7 | +const H: u32 = 720; | ||
| 8 | +const FPS: u32 = 30; | ||
| 9 | +const SEGMENT: u32 = 90; | ||
| 10 | + | ||
| 11 | +fn main() { | ||
| 12 | + for codec in [EncCodec::H264, EncCodec::H265, EncCodec::Av1] { | ||
| 13 | + if let Err(e) = run(codec) { | ||
| 14 | + println!("{codec:?}: error: {e}"); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +fn run(codec: EncCodec) -> ec_hw::Result<()> { | ||
| 20 | + let config = EncoderConfig { | ||
| 21 | + bitrate: 4_000_000, | ||
| 22 | + rate_control: RateControlMode::ConstantBitrate, | ||
| 23 | + framerate: (FPS, 1), | ||
| 24 | + gop_size: 1000, | ||
| 25 | + ..EncoderConfig::new(codec, W, H) | ||
| 26 | + }; | ||
| 27 | + let mut encoder = HwEncoder::open(config, None)?; | ||
| 28 | + let mut coded = Vec::new(); | ||
| 29 | + for index in 0..3 * SEGMENT { | ||
| 30 | + if index == SEGMENT { | ||
| 31 | + encoder.tune(Tunings { | ||
| 32 | + bitrate: 1_000_000, | ||
| 33 | + framerate: (FPS, 1), | ||
| 34 | + })?; | ||
| 35 | + } | ||
| 36 | + if index == 2 * SEGMENT { | ||
| 37 | + encoder.tune(Tunings { | ||
| 38 | + bitrate: 3_000_000, | ||
| 39 | + framerate: (FPS, 1), | ||
| 40 | + })?; | ||
| 41 | + } | ||
| 42 | + let pixels = bgra_gradient(index); | ||
| 43 | + let frame = RgbFrame { | ||
| 44 | + data: &pixels, | ||
| 45 | + width: W, | ||
| 46 | + height: H, | ||
| 47 | + stride: W * 4, | ||
| 48 | + order: RgbOrder::Bgra, | ||
| 49 | + }; | ||
| 50 | + let meta = FrameMetadata { | ||
| 51 | + timestamp: index as i64, | ||
| 52 | + force_keyframe: index == 0, | ||
| 53 | + }; | ||
| 54 | + coded.push(encoder.encode_rgb(&frame, meta)?); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + let mut line = format!("{codec:?} {:?} kb/s per segment:", encoder.backend()); | ||
| 58 | + for chunk in coded.chunks(SEGMENT as usize) { | ||
| 59 | + let bytes: usize = chunk.iter().map(|c| c.data.len()).sum(); | ||
| 60 | + let kbps = bytes as f64 * 8.0 * FPS as f64 / SEGMENT as f64 / 1000.0; | ||
| 61 | + line += &format!(" {kbps:.0}"); | ||
| 62 | + } | ||
| 63 | + let keyframes = coded.iter().filter(|c| c.is_keyframe).count(); | ||
| 64 | + println!("{line}, keyframes {keyframes}"); | ||
| 65 | + | ||
| 66 | + let mut decoder = HwDecoder::open(decode_codec(codec))?; | ||
| 67 | + let mut decoded = 0; | ||
| 68 | + for c in &coded { | ||
| 69 | + decoder.decode(&c.data, c.timestamp)?; | ||
| 70 | + while decoder.next_frame()?.is_some() { | ||
| 71 | + decoded += 1; | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + decoder.flush()?; | ||
| 75 | + while decoder.next_frame()?.is_some() { | ||
| 76 | + decoded += 1; | ||
| 77 | + } | ||
| 78 | + println!(" decoded {decoded} of {}", coded.len()); | ||
| 79 | + Ok(()) | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +fn decode_codec(codec: EncCodec) -> Codec { | ||
| 83 | + match codec { | ||
| 84 | + EncCodec::H264 => Codec::H264, | ||
| 85 | + EncCodec::H265 => Codec::H265, | ||
| 86 | + EncCodec::Av1 => Codec::Av1, | ||
| 87 | + } | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +fn bgra_gradient(index: u32) -> Vec<u8> { | ||
| 91 | + let shift = index as usize * 4; | ||
| 92 | + let mut out = Vec::with_capacity(W as usize * H as usize * 4); | ||
| 93 | + for y in 0..H as usize { | ||
| 94 | + for x in 0..W as usize { | ||
| 95 | + let r = (x + shift) & 0xff; | ||
| 96 | + let g = (y + 2 * shift) & 0xff; | ||
| 97 | + let b = ((x + y) / 2 + 3 * shift) & 0xff; | ||
| 98 | + out.extend_from_slice(&[b as u8, g as u8, r as u8, 0xff]); | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + out | ||
| 102 | +} | ||
crates/ec-hw/src/hw.rsmodified+39 -2
| @@ -2,7 +2,9 @@ | |||
| 2 | 2 | ||
| 3 | use crate::error::{Error, Result}; | 3 | use crate::error::{Error, Result}; |
| 4 | use crate::picture::I420; | 4 | use crate::picture::I420; |
| 5 | -use crate::types::{Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, RateControlMode}; | 5 | +use crate::types::{ |
| 6 | + Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, RateControlMode, Tunings, | ||
| 7 | +}; | ||
| 6 | use crate::vkdec264::VkH264Decoder; | 8 | use crate::vkdec264::VkH264Decoder; |
| 7 | use crate::vkdec265::VkH265Decoder; | 9 | use crate::vkdec265::VkH265Decoder; |
| 8 | use crate::vkdec_av1::VkAv1Decoder; | 10 | use crate::vkdec_av1::VkAv1Decoder; |
| @@ -246,6 +248,18 @@ impl VkEncoder { | |||
| 246 | } | 248 | } |
| 247 | } | 249 | } |
| 248 | 250 | ||
| 251 | + fn set_rate( | ||
| 252 | + &mut self, | ||
| 253 | + rate: ec_vk::RateControl, | ||
| 254 | + fps: (u32, u32), | ||
| 255 | + ) -> std::result::Result<(), ec_vk::Error> { | ||
| 256 | + match self { | ||
| 257 | + VkEncoder::H264(e) => e.set_rate(rate, fps), | ||
| 258 | + VkEncoder::H265(e) => e.set_rate(rate, fps), | ||
| 259 | + VkEncoder::Av1(e) => e.set_rate(rate, fps), | ||
| 260 | + } | ||
| 261 | + } | ||
| 262 | + | ||
| 249 | fn encode_rgb( | 263 | fn encode_rgb( |
| 250 | &mut self, | 264 | &mut self, |
| 251 | frame: &RgbFrame, | 265 | frame: &RgbFrame, |
| @@ -270,6 +284,7 @@ enum EncInner { | |||
| 270 | /// A hardware encoder on whichever backend opened. | 284 | /// A hardware encoder on whichever backend opened. |
| 271 | pub struct HwEncoder { | 285 | pub struct HwEncoder { |
| 272 | inner: EncInner, | 286 | inner: EncInner, |
| 287 | + config: EncoderConfig, | ||
| 273 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] | 288 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] |
| 274 | size: (usize, usize), | 289 | size: (usize, usize), |
| 275 | } | 290 | } |
| @@ -286,7 +301,11 @@ impl HwEncoder { | |||
| 286 | match open_backend(backend, config) { | 301 | match open_backend(backend, config) { |
| 287 | Ok(inner) => { | 302 | Ok(inner) => { |
| 288 | let size = (config.width as usize, config.height as usize); | 303 | let size = (config.width as usize, config.height as usize); |
| 289 | - return Ok(HwEncoder { inner, size }); | 304 | + return Ok(HwEncoder { |
| 305 | + inner, | ||
| 306 | + config, | ||
| 307 | + size, | ||
| 308 | + }); | ||
| 290 | } | 309 | } |
| 291 | Err(e) => reasons.push(format!("{backend:?}: {e}")), | 310 | Err(e) => reasons.push(format!("{backend:?}: {e}")), |
| 292 | } | 311 | } |
| @@ -306,6 +325,24 @@ impl HwEncoder { | |||
| 306 | } | 325 | } |
| 307 | } | 326 | } |
| 308 | 327 | ||
| 328 | + /// Change bitrate or frame rate from the next frame; a constant-QP encoder keeps its QP. | ||
| 329 | + pub fn tune(&mut self, tunings: Tunings) -> Result<()> { | ||
| 330 | + match &mut self.inner { | ||
| 331 | + EncInner::Vk(enc) => { | ||
| 332 | + let rate = match self.config.rate_control { | ||
| 333 | + RateControlMode::ConstantQp { .. } => vk_rate(&self.config), | ||
| 334 | + _ => ec_vk::RateControl::Cbr { | ||
| 335 | + bitrate: tunings.bitrate, | ||
| 336 | + }, | ||
| 337 | + }; | ||
| 338 | + enc.set_rate(rate, tunings.framerate)?; | ||
| 339 | + } | ||
| 340 | + #[cfg(target_os = "linux")] | ||
| 341 | + EncInner::Va(enc) => enc.tune(tunings), | ||
| 342 | + } | ||
| 343 | + Ok(()) | ||
| 344 | + } | ||
| 345 | + | ||
| 309 | /// Encode one frame; `meta.force_keyframe` starts a new GOP. | 346 | /// Encode one frame; `meta.force_keyframe` starts a new GOP. |
| 310 | pub fn encode(&mut self, frame: &I420, meta: FrameMetadata) -> Result<CodedFrame> { | 347 | pub fn encode(&mut self, frame: &I420, meta: FrameMetadata) -> Result<CodedFrame> { |
| 311 | match &mut self.inner { | 348 | match &mut self.inner { |
crates/ec-vk/src/av1.rsmodified+5 -0
| @@ -82,6 +82,11 @@ impl Av1Encoder { | |||
| 82 | Ok(self.finish(&pic, frame)) | 82 | Ok(self.finish(&pic, frame)) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | + /// Changes bitrate or frame rate; takes effect on the next frame. | ||
| 86 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 87 | + Ok(self.session.set_rate(rate, fps)?) | ||
| 88 | + } | ||
| 89 | + | ||
| 85 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. | 90 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| 86 | pub fn encode_rgb( | 91 | pub fn encode_rgb( |
| 87 | &mut self, | 92 | &mut self, |
crates/ec-vk/src/h265.rsmodified+5 -0
| @@ -80,6 +80,11 @@ impl H265Encoder { | |||
| 80 | Ok(self.finish(&pic, slice)) | 80 | Ok(self.finish(&pic, slice)) |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | + /// Changes bitrate or frame rate; takes effect on the next frame. | ||
| 84 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 85 | + Ok(self.session.set_rate(rate, fps)?) | ||
| 86 | + } | ||
| 87 | + | ||
| 83 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. | 88 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| 84 | pub fn encode_rgb( | 89 | pub fn encode_rgb( |
| 85 | &mut self, | 90 | &mut self, |
crates/ec-vk/src/lib.rsmodified+5 -0
| @@ -112,6 +112,11 @@ impl H264Encoder { | |||
| 112 | Ok(self.finish(&pic, slice)) | 112 | Ok(self.finish(&pic, slice)) |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | + /// Changes bitrate or frame rate; takes effect on the next frame. | ||
| 116 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 117 | + Ok(self.session.set_rate(rate, fps)?) | ||
| 118 | + } | ||
| 119 | + | ||
| 115 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. | 120 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| 116 | pub fn encode_rgb( | 121 | pub fn encode_rgb( |
| 117 | &mut self, | 122 | &mut self, |
shims/vk-video-ffi/src/av1_encode.rsmodified+36 -19
| @@ -6,6 +6,7 @@ use vulkanalia::vk::{ | |||
| 6 | KhrVideoQueueExtensionInstanceCommands, | 6 | KhrVideoQueueExtensionInstanceCommands, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| 9 | +use crate::h264_encode::validate_rate; | ||
| 9 | use crate::rgb_convert::{Input, RgbConverter, check_rgb}; | 10 | use crate::rgb_convert::{Input, RgbConverter, check_rgb}; |
| 10 | use crate::{Error, Gpu, RateControl}; | 11 | use crate::{Error, Gpu, RateControl}; |
| 11 | 12 | ||
| @@ -160,6 +161,8 @@ pub struct Av1EncodeSession { | |||
| 160 | height: u32, | 161 | height: u32, |
| 161 | fps: (u32, u32), | 162 | fps: (u32, u32), |
| 162 | rate: RateControl, | 163 | rate: RateControl, |
| 164 | + applied: (RateControl, (u32, u32)), | ||
| 165 | + dirty: bool, | ||
| 163 | sb128: bool, | 166 | sb128: bool, |
| 164 | seq: Vec<u8>, | 167 | seq: Vec<u8>, |
| 165 | bitstream_len: u64, | 168 | bitstream_len: u64, |
| @@ -233,6 +236,8 @@ impl Av1EncodeSession { | |||
| 233 | height: cfg.height, | 236 | height: cfg.height, |
| 234 | fps: cfg.fps, | 237 | fps: cfg.fps, |
| 235 | rate: cfg.rate, | 238 | rate: cfg.rate, |
| 239 | + applied: (cfg.rate, cfg.fps), | ||
| 240 | + dirty: false, | ||
| 236 | sb128, | 241 | sb128, |
| 237 | seq: sequence_header_obu(cfg.width, cfg.height, sb128), | 242 | seq: sequence_header_obu(cfg.width, cfg.height, sb128), |
| 238 | bitstream_len: 0, | 243 | bitstream_len: 0, |
| @@ -423,11 +428,13 @@ impl Av1EncodeSession { | |||
| 423 | .push_next(&mut av1_pic) | 428 | .push_next(&mut av1_pic) |
| 424 | .build(); | 429 | .build(); |
| 425 | 430 | ||
| 431 | + let mut applied = RateChain::new(self.applied.0, self.applied.1); | ||
| 426 | let mut chain = RateChain::new(self.rate, self.fps); | 432 | let mut chain = RateChain::new(self.rate, self.fps); |
| 427 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | 433 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() |
| 428 | .quality_level(0) | 434 | .quality_level(0) |
| 429 | .build(); | 435 | .build(); |
| 430 | let reset = !self.coded; | 436 | let reset = !self.coded; |
| 437 | + let update = reset || self.dirty; | ||
| 431 | 438 | ||
| 432 | unsafe { | 439 | unsafe { |
| 433 | let begin = vk::CommandBufferBeginInfo::builder() | 440 | let begin = vk::CommandBufferBeginInfo::builder() |
| @@ -439,20 +446,23 @@ impl Av1EncodeSession { | |||
| 439 | .video_session(self.video_session) | 446 | .video_session(self.video_session) |
| 440 | .video_session_parameters(self.params) | 447 | .video_session_parameters(self.params) |
| 441 | .reference_slots(&begin_slots) | 448 | .reference_slots(&begin_slots) |
| 442 | - .push_next(&mut chain.rc) | 449 | + .push_next(&mut applied.rc) |
| 443 | .build(); | 450 | .build(); |
| 444 | d.cmd_begin_video_coding_khr(cb, &begin_info); | 451 | d.cmd_begin_video_coding_khr(cb, &begin_info); |
| 445 | 452 | ||
| 446 | - if reset { | 453 | + if update { |
| 447 | - let control = vk::VideoCodingControlInfoKHR::builder() | 454 | + let flags = if reset { |
| 448 | - .flags( | 455 | + vk::VideoCodingControlFlagsKHR::RESET |
| 449 | - vk::VideoCodingControlFlagsKHR::RESET | 456 | + | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL |
| 450 | - | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL | 457 | + | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 451 | - | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL, | 458 | + } else { |
| 452 | - ) | 459 | + vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 453 | - .push_next(&mut quality) | 460 | + }; |
| 454 | - .push_next(&mut chain.rc) | 461 | + let mut control = vk::VideoCodingControlInfoKHR::builder().flags(flags); |
| 455 | - .build(); | 462 | + if reset { |
| 463 | + control = control.push_next(&mut quality); | ||
| 464 | + } | ||
| 465 | + let control = control.push_next(&mut chain.rc).build(); | ||
| 456 | d.cmd_control_video_coding_khr(cb, &control); | 466 | d.cmd_control_video_coding_khr(cb, &control); |
| 457 | } | 467 | } |
| 458 | 468 | ||
| @@ -466,6 +476,8 @@ impl Av1EncodeSession { | |||
| 466 | 476 | ||
| 467 | self.submit(self.queue, cb)?; | 477 | self.submit(self.queue, cb)?; |
| 468 | self.coded = true; | 478 | self.coded = true; |
| 479 | + self.applied = (self.rate, self.fps); | ||
| 480 | + self.dirty = false; | ||
| 469 | for i in 0..DPB_SLOTS { | 481 | for i in 0..DPB_SLOTS { |
| 470 | if refresh >> i & 1 == 1 { | 482 | if refresh >> i & 1 == 1 { |
| 471 | self.slot_key[i] = pic.key; | 483 | self.slot_key[i] = pic.key; |
| @@ -479,6 +491,18 @@ impl Av1EncodeSession { | |||
| 479 | self.read_bitstream() | 491 | self.read_bitstream() |
| 480 | } | 492 | } |
| 481 | 493 | ||
| 494 | + /// Changes bitrate or frame rate; the next encode applies it. The rate control mode cannot change. | ||
| 495 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 496 | + validate_rate(fps, rate)?; | ||
| 497 | + if std::mem::discriminant(&rate) != std::mem::discriminant(&self.rate) { | ||
| 498 | + return Err(Error::Invalid("rate control mode change".into())); | ||
| 499 | + } | ||
| 500 | + self.rate = rate; | ||
| 501 | + self.fps = fps; | ||
| 502 | + self.dirty = true; | ||
| 503 | + Ok(()) | ||
| 504 | + } | ||
| 505 | + | ||
| 482 | fn check(&self, pic: &Av1Picture) -> Result<(), Error> { | 506 | fn check(&self, pic: &Av1Picture) -> Result<(), Error> { |
| 483 | if pic.slot as usize >= DPB_SLOTS { | 507 | if pic.slot as usize >= DPB_SLOTS { |
| 484 | return Err(Error::Invalid("DPB slot".into())); | 508 | return Err(Error::Invalid("DPB slot".into())); |
| @@ -1056,14 +1080,7 @@ fn validate(cfg: &Av1EncodeConfig) -> Result<(), Error> { | |||
| 1056 | "size must be a non-zero multiple of 8".into(), | 1080 | "size must be a non-zero multiple of 8".into(), |
| 1057 | )); | 1081 | )); |
| 1058 | } | 1082 | } |
| 1059 | - if cfg.fps.0 == 0 || cfg.fps.1 == 0 { | 1083 | + validate_rate(cfg.fps, cfg.rate) |
| 1060 | - return Err(Error::Invalid("frame rate".into())); | ||
| 1061 | - } | ||
| 1062 | - match cfg.rate { | ||
| 1063 | - RateControl::ConstantQp { qp } if qp > 51 => Err(Error::Invalid("qp above 51".into())), | ||
| 1064 | - RateControl::Cbr { bitrate: 0 } => Err(Error::Invalid("bitrate is zero".into())), | ||
| 1065 | - _ => Ok(()), | ||
| 1066 | - } | ||
| 1067 | } | 1084 | } |
| 1068 | 1085 | ||
| 1069 | fn q_index(rate: RateControl) -> u32 { | 1086 | fn q_index(rate: RateControl) -> u32 { |
shims/vk-video-ffi/src/h264_encode.rsmodified+38 -11
| @@ -184,6 +184,8 @@ pub struct H264EncodeSession { | |||
| 184 | height: u32, | 184 | height: u32, |
| 185 | fps: (u32, u32), | 185 | fps: (u32, u32), |
| 186 | rate: RateControl, | 186 | rate: RateControl, |
| 187 | + applied: (RateControl, (u32, u32)), | ||
| 188 | + dirty: bool, | ||
| 187 | bitstream_len: u64, | 189 | bitstream_len: u64, |
| 188 | video_session: vk::VideoSessionKHR, | 190 | video_session: vk::VideoSessionKHR, |
| 189 | params: vk::VideoSessionParametersKHR, | 191 | params: vk::VideoSessionParametersKHR, |
| @@ -252,6 +254,8 @@ impl H264EncodeSession { | |||
| 252 | height, | 254 | height, |
| 253 | fps: cfg.fps, | 255 | fps: cfg.fps, |
| 254 | rate: cfg.rate, | 256 | rate: cfg.rate, |
| 257 | + applied: (cfg.rate, cfg.fps), | ||
| 258 | + dirty: false, | ||
| 255 | bitstream_len: 0, | 259 | bitstream_len: 0, |
| 256 | video_session: Default::default(), | 260 | video_session: Default::default(), |
| 257 | params: Default::default(), | 261 | params: Default::default(), |
| @@ -443,11 +447,13 @@ impl H264EncodeSession { | |||
| 443 | .push_next(&mut h264_pic) | 447 | .push_next(&mut h264_pic) |
| 444 | .build(); | 448 | .build(); |
| 445 | 449 | ||
| 450 | + let mut applied = RateChain::new(self.applied.0, self.applied.1); | ||
| 446 | let mut chain = RateChain::new(self.rate, self.fps); | 451 | let mut chain = RateChain::new(self.rate, self.fps); |
| 447 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | 452 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() |
| 448 | .quality_level(0) | 453 | .quality_level(0) |
| 449 | .build(); | 454 | .build(); |
| 450 | let reset = !self.coded; | 455 | let reset = !self.coded; |
| 456 | + let update = reset || self.dirty; | ||
| 451 | 457 | ||
| 452 | unsafe { | 458 | unsafe { |
| 453 | let begin = vk::CommandBufferBeginInfo::builder() | 459 | let begin = vk::CommandBufferBeginInfo::builder() |
| @@ -459,20 +465,23 @@ impl H264EncodeSession { | |||
| 459 | .video_session(self.video_session) | 465 | .video_session(self.video_session) |
| 460 | .video_session_parameters(self.params) | 466 | .video_session_parameters(self.params) |
| 461 | .reference_slots(&begin_slots) | 467 | .reference_slots(&begin_slots) |
| 462 | - .push_next(&mut chain.rc) | 468 | + .push_next(&mut applied.rc) |
| 463 | .build(); | 469 | .build(); |
| 464 | d.cmd_begin_video_coding_khr(cb, &begin_info); | 470 | d.cmd_begin_video_coding_khr(cb, &begin_info); |
| 465 | 471 | ||
| 466 | - if reset { | 472 | + if update { |
| 467 | - let control = vk::VideoCodingControlInfoKHR::builder() | 473 | + let flags = if reset { |
| 468 | - .flags( | 474 | + vk::VideoCodingControlFlagsKHR::RESET |
| 469 | - vk::VideoCodingControlFlagsKHR::RESET | 475 | + | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL |
| 470 | - | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL | 476 | + | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 471 | - | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL, | 477 | + } else { |
| 472 | - ) | 478 | + vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 473 | - .push_next(&mut quality) | 479 | + }; |
| 474 | - .push_next(&mut chain.rc) | 480 | + let mut control = vk::VideoCodingControlInfoKHR::builder().flags(flags); |
| 475 | - .build(); | 481 | + if reset { |
| 482 | + control = control.push_next(&mut quality); | ||
| 483 | + } | ||
| 484 | + let control = control.push_next(&mut chain.rc).build(); | ||
| 476 | d.cmd_control_video_coding_khr(cb, &control); | 485 | d.cmd_control_video_coding_khr(cb, &control); |
| 477 | } | 486 | } |
| 478 | 487 | ||
| @@ -486,9 +495,23 @@ impl H264EncodeSession { | |||
| 486 | 495 | ||
| 487 | self.submit(self.queue, cb)?; | 496 | self.submit(self.queue, cb)?; |
| 488 | self.coded = true; | 497 | self.coded = true; |
| 498 | + self.applied = (self.rate, self.fps); | ||
| 499 | + self.dirty = false; | ||
| 489 | self.read_bitstream() | 500 | self.read_bitstream() |
| 490 | } | 501 | } |
| 491 | 502 | ||
| 503 | + /// Changes bitrate or frame rate; the next encode applies it. The rate control mode cannot change. | ||
| 504 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 505 | + validate_rate(fps, rate)?; | ||
| 506 | + if std::mem::discriminant(&rate) != std::mem::discriminant(&self.rate) { | ||
| 507 | + return Err(Error::Invalid("rate control mode change".into())); | ||
| 508 | + } | ||
| 509 | + self.rate = rate; | ||
| 510 | + self.fps = fps; | ||
| 511 | + self.dirty = true; | ||
| 512 | + Ok(()) | ||
| 513 | + } | ||
| 514 | + | ||
| 492 | fn check(&self, pic: &H264Picture) -> Result<(), Error> { | 515 | fn check(&self, pic: &H264Picture) -> Result<(), Error> { |
| 493 | if pic.slot as usize >= DPB_SLOTS { | 516 | if pic.slot as usize >= DPB_SLOTS { |
| 494 | return Err(Error::Invalid("DPB slot".into())); | 517 | return Err(Error::Invalid("DPB slot".into())); |
| @@ -1004,6 +1027,10 @@ pub(crate) fn validate( | |||
| 1004 | if width == 0 || height == 0 || width % 2 != 0 || height % 2 != 0 { | 1027 | if width == 0 || height == 0 || width % 2 != 0 || height % 2 != 0 { |
| 1005 | return Err(Error::Invalid("size must be even and non-zero".into())); | 1028 | return Err(Error::Invalid("size must be even and non-zero".into())); |
| 1006 | } | 1029 | } |
| 1030 | + validate_rate(fps, rate) | ||
| 1031 | +} | ||
| 1032 | + | ||
| 1033 | +pub(crate) fn validate_rate(fps: (u32, u32), rate: RateControl) -> Result<(), Error> { | ||
| 1007 | if fps.0 == 0 || fps.1 == 0 { | 1034 | if fps.0 == 0 || fps.1 == 0 { |
| 1008 | return Err(Error::Invalid("frame rate".into())); | 1035 | return Err(Error::Invalid("frame rate".into())); |
| 1009 | } | 1036 | } |
shims/vk-video-ffi/src/h265_encode.rsmodified+36 -12
| @@ -8,7 +8,8 @@ use vulkanalia::vk::{ | |||
| 8 | 8 | ||
| 9 | use crate::h264_encode::{ | 9 | use crate::h264_encode::{ |
| 10 | BufferRes, DPB_SLOTS, ImageRes, NV12, PLANE_0, PLANE_1, align, alloc_cb, barrier, color_range, | 10 | BufferRes, DPB_SLOTS, ImageRes, NV12, PLANE_0, PLANE_1, align, alloc_cb, barrier, color_range, |
| 11 | - copy_region, create_pool, fill_nv12, memory_type, picture_resource, validate, vk, zeroed, | 11 | + copy_region, create_pool, fill_nv12, memory_type, picture_resource, validate, validate_rate, |
| 12 | + vk, zeroed, | ||
| 12 | }; | 13 | }; |
| 13 | use crate::rgb_convert::{Input, RgbConverter, check_rgb}; | 14 | use crate::rgb_convert::{Input, RgbConverter, check_rgb}; |
| 14 | use crate::{Error, Gpu, RateControl}; | 15 | use crate::{Error, Gpu, RateControl}; |
| @@ -160,6 +161,8 @@ pub struct H265EncodeSession { | |||
| 160 | height: u32, | 161 | height: u32, |
| 161 | fps: (u32, u32), | 162 | fps: (u32, u32), |
| 162 | rate: RateControl, | 163 | rate: RateControl, |
| 164 | + applied: (RateControl, (u32, u32)), | ||
| 165 | + dirty: bool, | ||
| 163 | bitstream_len: u64, | 166 | bitstream_len: u64, |
| 164 | video_session: vk::VideoSessionKHR, | 167 | video_session: vk::VideoSessionKHR, |
| 165 | params: vk::VideoSessionParametersKHR, | 168 | params: vk::VideoSessionParametersKHR, |
| @@ -229,6 +232,8 @@ impl H265EncodeSession { | |||
| 229 | height: coding.height, | 232 | height: coding.height, |
| 230 | fps: cfg.fps, | 233 | fps: cfg.fps, |
| 231 | rate: cfg.rate, | 234 | rate: cfg.rate, |
| 235 | + applied: (cfg.rate, cfg.fps), | ||
| 236 | + dirty: false, | ||
| 232 | bitstream_len: 0, | 237 | bitstream_len: 0, |
| 233 | video_session: Default::default(), | 238 | video_session: Default::default(), |
| 234 | params: Default::default(), | 239 | params: Default::default(), |
| @@ -425,11 +430,13 @@ impl H265EncodeSession { | |||
| 425 | .push_next(&mut h265_pic) | 430 | .push_next(&mut h265_pic) |
| 426 | .build(); | 431 | .build(); |
| 427 | 432 | ||
| 433 | + let mut applied = RateChain::new(self.applied.0, self.applied.1); | ||
| 428 | let mut chain = RateChain::new(self.rate, self.fps); | 434 | let mut chain = RateChain::new(self.rate, self.fps); |
| 429 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() | 435 | let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() |
| 430 | .quality_level(0) | 436 | .quality_level(0) |
| 431 | .build(); | 437 | .build(); |
| 432 | let reset = !self.coded; | 438 | let reset = !self.coded; |
| 439 | + let update = reset || self.dirty; | ||
| 433 | 440 | ||
| 434 | unsafe { | 441 | unsafe { |
| 435 | let begin = vk::CommandBufferBeginInfo::builder() | 442 | let begin = vk::CommandBufferBeginInfo::builder() |
| @@ -441,20 +448,23 @@ impl H265EncodeSession { | |||
| 441 | .video_session(self.video_session) | 448 | .video_session(self.video_session) |
| 442 | .video_session_parameters(self.params) | 449 | .video_session_parameters(self.params) |
| 443 | .reference_slots(&begin_slots) | 450 | .reference_slots(&begin_slots) |
| 444 | - .push_next(&mut chain.rc) | 451 | + .push_next(&mut applied.rc) |
| 445 | .build(); | 452 | .build(); |
| 446 | d.cmd_begin_video_coding_khr(cb, &begin_info); | 453 | d.cmd_begin_video_coding_khr(cb, &begin_info); |
| 447 | 454 | ||
| 448 | - if reset { | 455 | + if update { |
| 449 | - let control = vk::VideoCodingControlInfoKHR::builder() | 456 | + let flags = if reset { |
| 450 | - .flags( | 457 | + vk::VideoCodingControlFlagsKHR::RESET |
| 451 | - vk::VideoCodingControlFlagsKHR::RESET | 458 | + | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL |
| 452 | - | vk::VideoCodingControlFlagsKHR::ENCODE_QUALITY_LEVEL | 459 | + | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 453 | - | vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL, | 460 | + } else { |
| 454 | - ) | 461 | + vk::VideoCodingControlFlagsKHR::ENCODE_RATE_CONTROL |
| 455 | - .push_next(&mut quality) | 462 | + }; |
| 456 | - .push_next(&mut chain.rc) | 463 | + let mut control = vk::VideoCodingControlInfoKHR::builder().flags(flags); |
| 457 | - .build(); | 464 | + if reset { |
| 465 | + control = control.push_next(&mut quality); | ||
| 466 | + } | ||
| 467 | + let control = control.push_next(&mut chain.rc).build(); | ||
| 458 | d.cmd_control_video_coding_khr(cb, &control); | 468 | d.cmd_control_video_coding_khr(cb, &control); |
| 459 | } | 469 | } |
| 460 | 470 | ||
| @@ -468,9 +478,23 @@ impl H265EncodeSession { | |||
| 468 | 478 | ||
| 469 | self.submit(self.queue, cb)?; | 479 | self.submit(self.queue, cb)?; |
| 470 | self.coded = true; | 480 | self.coded = true; |
| 481 | + self.applied = (self.rate, self.fps); | ||
| 482 | + self.dirty = false; | ||
| 471 | self.read_bitstream() | 483 | self.read_bitstream() |
| 472 | } | 484 | } |
| 473 | 485 | ||
| 486 | + /// Changes bitrate or frame rate; the next encode applies it. The rate control mode cannot change. | ||
| 487 | + pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { | ||
| 488 | + validate_rate(fps, rate)?; | ||
| 489 | + if std::mem::discriminant(&rate) != std::mem::discriminant(&self.rate) { | ||
| 490 | + return Err(Error::Invalid("rate control mode change".into())); | ||
| 491 | + } | ||
| 492 | + self.rate = rate; | ||
| 493 | + self.fps = fps; | ||
| 494 | + self.dirty = true; | ||
| 495 | + Ok(()) | ||
| 496 | + } | ||
| 497 | + | ||
| 474 | fn check(&self, pic: &H265Picture) -> Result<(), Error> { | 498 | fn check(&self, pic: &H265Picture) -> Result<(), Error> { |
| 475 | if pic.slot as usize >= DPB_SLOTS { | 499 | if pic.slot as usize >= DPB_SLOTS { |
| 476 | return Err(Error::Invalid("DPB slot".into())); | 500 | return Err(Error::Invalid("DPB slot".into())); |