From 8737b14cc020ad92c7f8ce4a2b73684069c1d0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 26 Nov 2026 10:53:16 +0300 Subject: [PATCH] =?UTF-8?q?ec-hw:=20reference=20picture=20selection=20for?= =?UTF-8?q?=20Vulkan=20AV1=20=E2=80=94=20HwEncoder::ack(timestamp)=20and?= =?UTF-8?q?=20recover();=20a=20recovery=20frame=20references=20the=20newes?= =?UTF-8?q?t=20acked=20frame=20still=20in=20the=208-slot=20DPB=20(2=20chai?= =?UTF-8?q?n=20+=206=20anchors=20every=2016=20frames),=20else=20a=20keyfra?= =?UTF-8?q?me;=20CodedFrame::recovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ec-hw/src/enc/mod.rs | 1 + crates/ec-hw/src/hw.rs | 100 ++++++++++++-------- crates/ec-hw/src/types.rs | 2 + crates/ec-vk/src/av1.rs | 60 +++++++----- crates/ec-vk/src/h265.rs | 22 ++++- crates/ec-vk/src/history.rs | 127 ++++++++++++++++++++++++++ crates/ec-vk/src/lib.rs | 42 +++++++-- shims/vk-video-ffi/src/av1_encode.rs | 9 +- shims/vk-video-ffi/src/h264_encode.rs | 9 +- shims/vk-video-ffi/src/h265_encode.rs | 9 +- shims/vk-video-ffi/src/lib.rs | 1 + shims/vk-video-ffi/src/session.rs | 14 ++- 12 files changed, 300 insertions(+), 96 deletions(-) create mode 100644 crates/ec-vk/src/history.rs diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index 3949ddbf..4c8c0730 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -661,6 +661,7 @@ impl Encoder { data, timestamp, is_keyframe: keyframe, + recovery: false, }) } diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index c81875de..fff6d710 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -402,13 +402,7 @@ enum VkEncoder { Av1(ec_vk::Av1Encoder), } -fn stamp(pending: &mut VecDeque, coded: ec_vk::CodedFrame) -> CodedFrame { - CodedFrame { - data: coded.data, - timestamp: pending.pop_front().unwrap_or_default(), - is_keyframe: coded.keyframe, - } -} +const ID_HISTORY: usize = 512; impl VkEncoder { fn submit( @@ -579,6 +573,8 @@ pub struct HwEncoder { #[cfg_attr(not(target_os = "linux"), allow(dead_code))] size: (usize, usize), pending: VecDeque, + ids: VecDeque<(i64, u64)>, + keyframe_next: bool, tunings: Tunings, rc: RateFeedback, } @@ -602,6 +598,8 @@ impl HwEncoder { config, size, pending: VecDeque::new(), + ids: VecDeque::new(), + keyframe_next: false, tunings: Tunings { bitrate: config.bitrate, framerate: config.framerate, @@ -645,15 +643,17 @@ impl HwEncoder { /// Submit one picture without waiting; the oldest coded picture comes back once two are in flight. Call [`HwEncoder::flush`] at the end. pub fn submit(&mut self, input: EncodeInput, meta: FrameMetadata) -> Result> { + let meta = self.with_recovery(meta); match &mut self.inner { EncInner::Vk(enc) => { 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))) + let Some(coded) = enc.submit(input, meta.force_keyframe)? else { + return Ok(None); + }; + self.steer(coded.data.len())?; + let timestamp = self.pending.pop_front().unwrap_or_default(); + Ok(Some(self.vk_frame(coded, timestamp))) } #[cfg(target_os = "linux")] EncInner::Va(enc) => match input { @@ -674,13 +674,13 @@ impl HwEncoder { EncInner::Vk(enc) => { vk_session_alive(self.born_epoch)?; let frames = enc.flush()?; - for coded in &frames { + let mut out = Vec::with_capacity(frames.len()); + for coded in frames { self.steer(coded.data.len())?; + let timestamp = self.pending.pop_front().unwrap_or_default(); + out.push(self.vk_frame(coded, timestamp)); } - Ok(frames - .into_iter() - .map(|coded| stamp(&mut self.pending, coded)) - .collect()) + Ok(out) } #[cfg(target_os = "linux")] EncInner::Va(enc) => enc.flush(), @@ -695,6 +695,44 @@ impl HwEncoder { } } + /// Frame `timestamp` was decoded by every receiver. A frame after [`HwEncoder::recover`] may then reference it instead of being a keyframe. Vulkan AV1 only; elsewhere a no-op. + pub fn ack(&mut self, timestamp: i64) { + let Some(&(_, id)) = self.ids.iter().rev().find(|(ts, _)| *ts == timestamp) else { + return; + }; + if let EncInner::Vk(VkEncoder::Av1(enc)) = &mut self.inner { + enc.ack(id); + } + } + + /// Recover the receivers from loss: the next frame references the newest acknowledged frame still held ([`CodedFrame::recovery`]), or is a keyframe when none is. Codecs without reference selection always send a keyframe. + pub fn recover(&mut self) { + match &mut self.inner { + EncInner::Vk(VkEncoder::Av1(enc)) => enc.recover(), + _ => self.keyframe_next = true, + } + } + + fn with_recovery(&mut self, meta: FrameMetadata) -> FrameMetadata { + FrameMetadata { + force_keyframe: meta.force_keyframe || std::mem::take(&mut self.keyframe_next), + ..meta + } + } + + fn vk_frame(&mut self, coded: ec_vk::CodedFrame, timestamp: i64) -> CodedFrame { + if self.ids.len() == ID_HISTORY { + self.ids.pop_front(); + } + self.ids.push_back((timestamp, coded.id)); + CodedFrame { + data: coded.data, + timestamp, + is_keyframe: coded.keyframe, + recovery: coded.recovery, + } + } + fn steer(&mut self, bytes: usize) -> Result<()> { if !self.rc.track(bytes, self.config.rate_control, self.tunings) { return Ok(()); @@ -710,17 +748,14 @@ impl HwEncoder { /// Encode one frame; `meta.force_keyframe` starts a new GOP. pub fn encode(&mut self, frame: &I420, meta: FrameMetadata) -> Result { + let meta = self.with_recovery(meta); self.idle()?; match &mut self.inner { 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, - is_keyframe: coded.keyframe, - }) + Ok(self.vk_frame(coded, meta.timestamp)) } #[cfg(target_os = "linux")] EncInner::Va(enc) => enc.encode(frame, meta), @@ -729,16 +764,13 @@ impl HwEncoder { /// Encode one 10-bit frame of a 10-bit encoder (H.265 and AV1), the [`HwEncoder::encode`] of a 10-bit encoder. pub fn encode_16(&mut self, frame: &I420_16, meta: FrameMetadata) -> Result { + let meta = self.with_recovery(meta); self.idle()?; 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, - is_keyframe: coded.keyframe, - }) + Ok(self.vk_frame(coded, meta.timestamp)) } #[cfg(target_os = "linux")] EncInner::Va(enc) => enc.encode_16(frame, meta), @@ -747,17 +779,14 @@ impl HwEncoder { /// Encode one packed RGB frame of the source size; it is box-downscaled to the encoder size. Vulkan does this and the NV12 conversion on the GPU, VA-API on the CPU. pub fn encode_rgb(&mut self, frame: &RgbFrame, meta: FrameMetadata) -> Result { + let meta = self.with_recovery(meta); self.idle()?; match &mut self.inner { 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, - is_keyframe: coded.keyframe, - }) + Ok(self.vk_frame(coded, meta.timestamp)) } #[cfg(target_os = "linux")] EncInner::Va(enc) => enc.encode(&rgb_to_i420(frame, self.size)?, meta), @@ -811,17 +840,14 @@ impl HwEncoder { #[cfg(target_os = "linux")] fn encode_dma(&mut self, src: &DmaBuf, meta: FrameMetadata) -> Result { + let meta = self.with_recovery(meta); self.idle()?; match &mut self.inner { 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, - is_keyframe: coded.keyframe, - }) + Ok(self.vk_frame(coded, meta.timestamp)) } EncInner::Va(_) => Err(Error::unsupported( "DMA-BUF encode", diff --git a/crates/ec-hw/src/types.rs b/crates/ec-hw/src/types.rs index e6843d85..7a6feab1 100644 --- a/crates/ec-hw/src/types.rs +++ b/crates/ec-hw/src/types.rs @@ -196,6 +196,8 @@ pub struct CodedFrame { pub timestamp: i64, /// True for an IDR / key frame. pub is_keyframe: bool, + /// True for an inter frame that references an acknowledged older frame after [`crate::HwEncoder::recover`]. + pub recovery: bool, } /// The codecs this crate decodes in hardware. diff --git a/crates/ec-vk/src/av1.rs b/crates/ec-vk/src/av1.rs index 80271047..5282c899 100644 --- a/crates/ec-vk/src/av1.rs +++ b/crates/ec-vk/src/av1.rs @@ -2,7 +2,8 @@ use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; #[cfg(target_os = "linux")] use crate::DmaBuf; -use crate::{CodedFrame, Colour, Error, Flight, Gpu, RateControl, last_frame}; +use crate::history::History; +use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; const TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00]; @@ -36,7 +37,7 @@ pub struct Av1Encoder { gop_size: u32, position: u32, order_hint: u8, - reference: Option, + history: History, uv: Vec, uv16: Vec, flight: Flight, @@ -69,7 +70,7 @@ impl Av1Encoder { gop_size: cfg.gop_size, position: 0, order_hint: 0, - reference: None, + history: History::new(), uv: vec![0; cfg.width as usize * cfg.height as usize / 2], uv16: vec![0; cfg.width as usize * cfg.height as usize / 2], flight: Flight::default(), @@ -212,36 +213,51 @@ impl Av1Encoder { } fn next_picture(&mut self, force_keyframe: bool) -> Av1Picture { - let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size; - self.order_hint = if keyframe { + let step = self + .history + .next(force_keyframe || self.position >= self.gop_size); + self.order_hint = if step.key { 0 } else { self.order_hint.wrapping_add(1) }; - let reference = if keyframe { None } else { self.reference }; - let slot = reference.map_or(0, |r| 1 - r.slot); - let pic = Av1Picture { - key: keyframe, - order_hint: self.order_hint, - slot, - reference, - }; - self.reference = Some(Av1Ref { - slot: pic.slot, - order_hint: pic.order_hint, + self.history.store(&step, self.order_hint); + self.position = if step.key { 1 } else { self.position + 1 }; + self.flight.push(Meta { + keyframe: step.key, + recovery: step.recovery, + id: step.id, }); - self.position = if keyframe { 1 } else { self.position + 1 }; - self.flight.push(keyframe); - pic + Av1Picture { + key: step.key, + order_hint: self.order_hint, + slot: step.slot, + reference: step.reference.map(|(slot, order_hint)| Av1Ref { slot, order_hint }), + } + } + + /// The receiver decoded frame `id` ([`CodedFrame::id`]); a recovery frame may reference it while it stays in the DPB. + pub fn ack(&mut self, id: u64) { + self.history.ack(id); + } + + /// Make the next frame reference the newest acknowledged frame still held, or a KEY frame when none is. + pub fn recover(&mut self) { + self.history.arm(); } fn coded(&mut self, bytes: Vec) -> CodedFrame { - let keyframe = self.flight.pop(); + let meta = self.flight.pop(); let mut data = TEMPORAL_DELIMITER.to_vec(); - if keyframe { + if meta.keyframe { data.extend_from_slice(&self.sequence_header); } data.extend_from_slice(&bytes); - CodedFrame { data, keyframe } + CodedFrame { + data, + keyframe: meta.keyframe, + recovery: meta.recovery, + id: meta.id, + } } } diff --git a/crates/ec-vk/src/h265.rs b/crates/ec-vk/src/h265.rs index 0a62d4af..4cd32a86 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, Colour, Error, Flight, Gpu, RateControl, last_frame}; +use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; /// H.265 encoder settings. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -38,6 +38,7 @@ pub struct H265Encoder { uv: Vec, uv16: Vec, flight: Flight, + next_id: u64, } impl H265Encoder { @@ -71,6 +72,7 @@ impl H265Encoder { uv: vec![0; cfg.width as usize * cfg.height as usize / 2], uv16: vec![0; cfg.width as usize * cfg.height as usize / 2], flight: Flight::default(), + next_id: 0, }) } @@ -225,17 +227,27 @@ impl H265Encoder { poc: pic.poc, }); self.position = if keyframe { 1 } else { self.position + 1 }; - self.flight.push(keyframe); + self.flight.push(Meta { + keyframe, + recovery: false, + id: self.next_id, + }); + self.next_id += 1; pic } fn coded(&mut self, bytes: Vec) -> CodedFrame { - let keyframe = self.flight.pop(); - let data = if keyframe { + let meta = self.flight.pop(); + let data = if meta.keyframe { [self.parameter_sets.as_slice(), bytes.as_slice()].concat() } else { bytes }; - CodedFrame { data, keyframe } + CodedFrame { + data, + keyframe: meta.keyframe, + recovery: meta.recovery, + id: meta.id, + } } } diff --git a/crates/ec-vk/src/history.rs b/crates/ec-vk/src/history.rs new file mode 100644 index 00000000..474310a9 --- /dev/null +++ b/crates/ec-vk/src/history.rs @@ -0,0 +1,127 @@ +use vk_video_ffi::DPB_SLOTS; + +const CHAIN: usize = 2; +const ANCHOR_EVERY: u64 = 16; +const MAX_AGE: u64 = 120; + +#[derive(Debug, Clone, Copy)] +struct Held { + id: u64, + acked: bool, + data: T, +} + +/// Where the next picture goes and what it references. +#[derive(Debug, Clone, Copy)] +pub(crate) struct Step { + pub(crate) id: u64, + pub(crate) key: bool, + pub(crate) recovery: bool, + pub(crate) slot: u32, + pub(crate) reference: Option<(u32, T)>, +} + +/// DPB slot bookkeeping: two slots chain each picture to the previous one, the +/// rest keep every sixteenth picture as an anchor a recovery picture can +/// reference once the receiver acknowledged it. +pub(crate) struct History { + next_id: u64, + slots: [Option>; DPB_SLOTS], + last: Option<(usize, T)>, + next_anchor: usize, + armed: bool, +} + +impl History { + pub(crate) fn new() -> Self { + History { + next_id: 0, + slots: [None; DPB_SLOTS], + last: None, + next_anchor: 0, + armed: false, + } + } + + pub(crate) fn arm(&mut self) { + self.armed = true; + } + + pub(crate) fn ack(&mut self, id: u64) { + for held in self.slots.iter_mut().flatten() { + if held.id == id { + held.acked = true; + } + } + } + + pub(crate) fn next(&mut self, want_key: bool) -> Step { + let id = self.next_id; + self.next_id += 1; + let armed = std::mem::take(&mut self.armed); + let acked = if armed && !want_key { + self.newest_acked(id) + } else { + None + }; + let key = want_key || self.last.is_none() || (armed && acked.is_none()); + let reference = match (key, acked) { + (true, _) => None, + (false, Some(r)) => Some(r), + (false, None) => self.last, + }; + let reference_slot = reference.map(|(slot, _)| slot); + if key { + self.slots = [None; DPB_SLOTS]; + self.next_anchor = 0; + } + let slot = if key || id % ANCHOR_EVERY == 0 { + self.anchor_slot(reference_slot) + } else { + self.chain_slot(reference_slot) + }; + Step { + id, + key, + recovery: !key && acked.is_some(), + slot: slot as u32, + reference: reference.map(|(slot, data)| (slot as u32, data)), + } + } + + pub(crate) fn store(&mut self, step: &Step, data: T) { + let slot = step.slot as usize; + self.slots[slot] = Some(Held { + id: step.id, + acked: false, + data, + }); + self.last = Some((slot, data)); + } + + fn newest_acked(&self, id: u64) -> Option<(usize, T)> { + (0..DPB_SLOTS) + .filter_map(|i| self.slots[i].map(|h| (i, h))) + .filter(|(_, h)| h.acked && id - h.id < MAX_AGE) + .max_by_key(|(_, h)| h.id) + .map(|(i, h)| (i, h.data)) + } + + fn anchor_slot(&mut self, reference: Option) -> usize { + let anchors = DPB_SLOTS - CHAIN; + loop { + let slot = CHAIN + self.next_anchor; + self.next_anchor = (self.next_anchor + 1) % anchors; + if Some(slot) != reference { + return slot; + } + } + } + + fn chain_slot(&self, reference: Option) -> usize { + (0..CHAIN) + .filter(|&i| Some(i) != reference) + .min_by_key(|&i| self.slots[i].map_or(0, |h| h.id + 1)) + .unwrap_or(0) + } +} diff --git a/crates/ec-vk/src/lib.rs b/crates/ec-vk/src/lib.rs index a76a8ba4..9d350492 100644 --- a/crates/ec-vk/src/lib.rs +++ b/crates/ec-vk/src/lib.rs @@ -8,6 +8,7 @@ use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref}; mod av1; mod h265; +mod history; pub use av1::{Av1Encoder, Av1EncoderConfig}; pub use h265::{H265Encoder, H265EncoderConfig}; @@ -54,18 +55,29 @@ pub struct CodedFrame { pub data: Vec, /// True when the frame is an IDR picture. pub keyframe: bool, + /// True when the frame references an acknowledged older picture instead of the previous one. + pub recovery: bool, + /// Submission index of the frame, the handle [`Av1Encoder::ack`] takes. + pub id: u64, +} + +#[derive(Debug, Clone, Copy, Default)] +pub(crate) struct Meta { + pub(crate) keyframe: bool, + pub(crate) recovery: bool, + pub(crate) id: u64, } #[derive(Default)] -pub(crate) struct Flight(VecDeque); +pub(crate) struct Flight(VecDeque); impl Flight { - pub(crate) fn push(&mut self, keyframe: bool) { - self.0.push_back(keyframe); + pub(crate) fn push(&mut self, meta: Meta) { + self.0.push_back(meta); } - pub(crate) fn pop(&mut self) -> bool { - self.0.pop_front().unwrap_or(false) + pub(crate) fn pop(&mut self) -> Meta { + self.0.pop_front().unwrap_or_default() } } @@ -93,6 +105,7 @@ pub struct H264Encoder { reference: Option, uv: Vec, flight: Flight, + next_id: u64, } impl H264Encoder { @@ -127,6 +140,7 @@ impl H264Encoder { reference: None, uv: vec![0; cfg.width as usize * cfg.height as usize / 2], flight: Flight::default(), + next_id: 0, }) } @@ -259,17 +273,27 @@ impl H264Encoder { if keyframe { self.idr_pic_id = self.idr_pic_id.wrapping_add(1); } - self.flight.push(keyframe); + self.flight.push(Meta { + keyframe, + recovery: false, + id: self.next_id, + }); + self.next_id += 1; pic } fn coded(&mut self, bytes: Vec) -> CodedFrame { - let keyframe = self.flight.pop(); - let data = if keyframe { + let meta = self.flight.pop(); + let data = if meta.keyframe { [self.parameter_sets.as_slice(), bytes.as_slice()].concat() } else { bytes }; - CodedFrame { data, keyframe } + CodedFrame { + data, + keyframe: meta.keyframe, + recovery: meta.recovery, + id: meta.id, + } } } diff --git a/shims/vk-video-ffi/src/av1_encode.rs b/shims/vk-video-ffi/src/av1_encode.rs index 3d5f8d6d..905a04a2 100644 --- a/shims/vk-video-ffi/src/av1_encode.rs +++ b/shims/vk-video-ffi/src/av1_encode.rs @@ -41,7 +41,7 @@ pub struct Av1Picture { pub key: bool, /// order_hint of this frame. pub order_hint: u8, - /// DPB slot this picture is written to, 0 or 1. + /// DPB slot this picture is written to, below `DPB_SLOTS`. pub slot: u32, /// Reference for an INTER frame, None for KEY. pub reference: Option, @@ -50,7 +50,7 @@ pub struct Av1Picture { /// A picture held in the DPB. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Av1Ref { - /// DPB slot, 0 or 1. + /// DPB slot, below `DPB_SLOTS`. pub slot: u32, /// order_hint of the reference. pub order_hint: u8, @@ -300,10 +300,7 @@ impl Av1EncodeSession { let extent = self.enc.extent; let refresh: u8 = if pic.key { 0xff } else { 1 << pic.slot }; - let dpb_pr = [ - picture_resource(self.enc.dpb[0].view, extent), - picture_resource(self.enc.dpb[1].view, extent), - ]; + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); let src_pr = picture_resource(self.enc.src_view(), extent); let key_of = self.slot_key; diff --git a/shims/vk-video-ffi/src/h264_encode.rs b/shims/vk-video-ffi/src/h264_encode.rs index d2bb0b17..340522f0 100644 --- a/shims/vk-video-ffi/src/h264_encode.rs +++ b/shims/vk-video-ffi/src/h264_encode.rs @@ -121,7 +121,7 @@ pub struct H264Picture { pub frame_num: u32, /// PicOrderCnt. pub poc: i32, - /// DPB slot this picture is written to, 0 or 1. + /// DPB slot this picture is written to, below `DPB_SLOTS`. pub slot: u32, /// Reference for a P picture, None for IDR. pub reference: Option, @@ -130,7 +130,7 @@ pub struct H264Picture { /// A picture held in the DPB. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct H264Ref { - /// DPB slot, 0 or 1. + /// DPB slot, below `DPB_SLOTS`. pub slot: u32, /// frame_num of the reference. pub frame_num: u32, @@ -381,10 +381,7 @@ impl H264EncodeSession { } else { vk::video::STD_VIDEO_H264_PICTURE_TYPE_P }; - let dpb_pr = [ - picture_resource(self.enc.dpb[0].view, extent), - picture_resource(self.enc.dpb[1].view, extent), - ]; + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); let src_pr = picture_resource(self.enc.src_view(), extent); let new_std = reference_info(primary, pic.frame_num, pic.poc); diff --git a/shims/vk-video-ffi/src/h265_encode.rs b/shims/vk-video-ffi/src/h265_encode.rs index 9e55a919..23a9892a 100644 --- a/shims/vk-video-ffi/src/h265_encode.rs +++ b/shims/vk-video-ffi/src/h265_encode.rs @@ -41,7 +41,7 @@ pub struct H265Picture { pub idr: bool, /// PicOrderCntVal. pub poc: i32, - /// DPB slot this picture is written to, 0 or 1. + /// DPB slot this picture is written to, below `DPB_SLOTS`. pub slot: u32, /// Reference for a P picture, None for IDR. pub reference: Option, @@ -50,7 +50,7 @@ pub struct H265Picture { /// A picture held in the DPB. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct H265Ref { - /// DPB slot, 0 or 1. + /// DPB slot, below `DPB_SLOTS`. pub slot: u32, /// PicOrderCntVal of the reference, lower than the picture's. pub poc: i32, @@ -344,10 +344,7 @@ impl H265EncodeSession { } else { vk::video::STD_VIDEO_H265_PICTURE_TYPE_P }; - let dpb_pr = [ - picture_resource(self.enc.dpb[0].view, extent), - picture_resource(self.enc.dpb[1].view, extent), - ]; + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); let src_pr = picture_resource(self.enc.src_view(), extent); let new_std = reference_info(primary, pic.poc); diff --git a/shims/vk-video-ffi/src/lib.rs b/shims/vk-video-ffi/src/lib.rs index 90d5104a..0cd8e75d 100644 --- a/shims/vk-video-ffi/src/lib.rs +++ b/shims/vk-video-ffi/src/lib.rs @@ -26,6 +26,7 @@ pub use av1_decode::{ Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, }; pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; +pub use session::DPB_SLOTS; #[cfg(target_os = "linux")] pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; pub use vp9_decode::{ diff --git a/shims/vk-video-ffi/src/session.rs b/shims/vk-video-ffi/src/session.rs index 930b7c49..fa418851 100644 --- a/shims/vk-video-ffi/src/session.rs +++ b/shims/vk-video-ffi/src/session.rs @@ -16,7 +16,8 @@ use crate::{Error, Gpu, RateControl}; pub(crate) const NV12: vk::Format = vk::Format::G8_B8R8_2PLANE_420_UNORM; pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; -pub(crate) const DPB_SLOTS: usize = 2; +/// DPB slots of every encode session. +pub const DPB_SLOTS: usize = 8; pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0; pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1; #[cfg(target_os = "linux")] @@ -958,10 +959,13 @@ impl Encoder { .profiles(std::slice::from_ref(profile)) .build(); let family = vs.family; - let dpb = [ - vs.image(extent, usage, usage, &[family], &mut list)?, - vs.image(extent, usage, usage, &[family], &mut list)?, - ]; + let mut images = Vec::with_capacity(DPB_SLOTS); + for _ in 0..DPB_SLOTS { + images.push(vs.image(extent, usage, usage, &[family], &mut list)?); + } + let Ok(dpb) = <[Image; DPB_SLOTS]>::try_from(images) else { + return Err(Error::Invalid("DPB image count".into())); + }; let slots = [ Slot::new(&mut vs, profile, upload_family, input, extent, frame_bytes, bitstream_len)?, Slot::new(&mut vs, profile, upload_family, input, extent, frame_bytes, bitstream_len)?,