From c41118999fb42403cf9a01e109f8f9a010992ec3 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 11:04:42 +0300 Subject: [PATCH] ec-hw: reference picture selection for Vulkan H.265 (explicit short-term RPS keeps all 8 slots) and H.264 (every picture long-term idx = slot via MMCO6, list modification picks the reference); acks are cumulative; recovery evicts frames newer than its base; H.264 Vk decoder prunes unused gap frames before giving them slots --- crates/ec-hw/src/dpb264.rs | 1 + crates/ec-hw/src/hw.rs | 19 ++++--- crates/ec-hw/src/vkdec264.rs | 8 +-- crates/ec-vk/src/av1.rs | 2 +- crates/ec-vk/src/h265.rs | 54 ++++++++++--------- crates/ec-vk/src/history.rs | 36 +++++++++---- crates/ec-vk/src/lib.rs | 52 +++++++++--------- shims/vk-video-ffi/examples/dmabuf_smoke.rs | 1 + shims/vk-video-ffi/src/h264_encode.rs | 58 +++++++++++++++++---- shims/vk-video-ffi/src/h265_encode.rs | 27 +++++++--- shims/vk-video-ffi/src/lib.rs | 3 +- 11 files changed, 174 insertions(+), 87 deletions(-) diff --git a/crates/ec-hw/src/dpb264.rs b/crates/ec-hw/src/dpb264.rs index ad51b747..1b534bda 100644 --- a/crates/ec-hw/src/dpb264.rs +++ b/crates/ec-hw/src/dpb264.rs @@ -356,6 +356,7 @@ impl Dpb { }; self.number_short_term(unused, sps); self.sliding_window(sps); + self.frames.retain(|p| p.stored()); let id = self.next_id(); self.frames.push(Picture { id, diff --git a/crates/ec-hw/src/hw.rs b/crates/ec-hw/src/hw.rs index fff6d710..cc997594 100644 --- a/crates/ec-hw/src/hw.rs +++ b/crates/ec-hw/src/hw.rs @@ -695,21 +695,28 @@ 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. + /// Every frame up to `timestamp` was decoded by every receiver. A frame after [`HwEncoder::recover`] may then reference one of them instead of being a keyframe. Vulkan only; a no-op on VA-API. pub fn ack(&mut self, timestamp: i64) { - let Some(&(_, id)) = self.ids.iter().rev().find(|(ts, _)| *ts == timestamp) else { + 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); + match &mut self.inner { + EncInner::Vk(VkEncoder::Av1(enc)) => enc.ack(id), + EncInner::Vk(VkEncoder::H265(enc)) => enc.ack(id), + EncInner::Vk(VkEncoder::H264(enc)) => enc.ack(id), + #[cfg(target_os = "linux")] + EncInner::Va(_) => {} } } - /// 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. + /// Recover the receivers from loss: the next frame references the newest acknowledged frame still held ([`CodedFrame::recovery`]), or is a keyframe when none is. VA-API always sends a keyframe. pub fn recover(&mut self) { match &mut self.inner { EncInner::Vk(VkEncoder::Av1(enc)) => enc.recover(), - _ => self.keyframe_next = true, + EncInner::Vk(VkEncoder::H265(enc)) => enc.recover(), + EncInner::Vk(VkEncoder::H264(enc)) => enc.recover(), + #[cfg(target_os = "linux")] + EncInner::Va(_) => self.keyframe_next = true, } } diff --git a/crates/ec-hw/src/vkdec264.rs b/crates/ec-hw/src/vkdec264.rs index ce991234..021b3400 100644 --- a/crates/ec-hw/src/vkdec264.rs +++ b/crates/ec-hw/src/vkdec264.rs @@ -299,10 +299,12 @@ impl VkH264Decoder { } fn fill_gap(&mut self, sps: &Sps, frame_num: u32) -> Result<()> { - let before = self.dpb.frames.len(); + let before: Vec = self.dpb.frames.iter().map(|p| p.id).collect(); self.dpb.fill_frame_num_gap(sps, frame_num)?; - for i in before..self.dpb.frames.len() { - self.dpb.frames[i].surface = self.free_slot()?; + for i in 0..self.dpb.frames.len() { + if !before.contains(&self.dpb.frames[i].id) { + self.dpb.frames[i].surface = self.free_slot()?; + } } Ok(()) } diff --git a/crates/ec-vk/src/av1.rs b/crates/ec-vk/src/av1.rs index 5282c899..47c8d851 100644 --- a/crates/ec-vk/src/av1.rs +++ b/crates/ec-vk/src/av1.rs @@ -236,7 +236,7 @@ impl Av1Encoder { } } - /// The receiver decoded frame `id` ([`CodedFrame::id`]); a recovery frame may reference it while it stays in the DPB. + /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one while it stays in the DPB. pub fn ack(&mut self, id: u64) { self.history.ack(id); } diff --git a/crates/ec-vk/src/h265.rs b/crates/ec-vk/src/h265.rs index 4cd32a86..98f5f315 100644 --- a/crates/ec-vk/src/h265.rs +++ b/crates/ec-vk/src/h265.rs @@ -2,6 +2,7 @@ use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; #[cfg(target_os = "linux")] use crate::DmaBuf; +use crate::history::History; use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; /// H.265 encoder settings. @@ -34,11 +35,10 @@ pub struct H265Encoder { gop_size: u32, position: u32, poc: i32, - reference: Option, + history: History, uv: Vec, uv16: Vec, flight: Flight, - next_id: u64, } impl H265Encoder { @@ -68,11 +68,10 @@ impl H265Encoder { gop_size: cfg.gop_size, position: 0, poc: 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(), - next_id: 0, }) } @@ -212,28 +211,35 @@ impl H265Encoder { } fn next_picture(&mut self, force_keyframe: bool) -> H265Picture { - let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size; - self.poc = if keyframe { 0 } else { self.poc + 1 }; - let reference = if keyframe { None } else { self.reference }; - let slot = reference.map_or(0, |r| 1 - r.slot); - let pic = H265Picture { - idr: keyframe, - poc: self.poc, - slot, - reference, - }; - self.reference = Some(H265Ref { - slot: pic.slot, - poc: pic.poc, - }); - self.position = if keyframe { 1 } else { self.position + 1 }; + let step = self + .history + .next(force_keyframe || self.position >= self.gop_size); + self.poc = if step.key { 0 } else { self.poc + 1 }; + let keep = self.history.kept(&step); + self.history.store(&step, self.poc); + self.position = if step.key { 1 } else { self.position + 1 }; self.flight.push(Meta { - keyframe, - recovery: false, - id: self.next_id, + keyframe: step.key, + recovery: step.recovery, + id: step.id, }); - self.next_id += 1; - pic + H265Picture { + idr: step.key, + poc: self.poc, + slot: step.slot, + reference: step.reference.map(|(slot, poc)| H265Ref { slot, poc }), + keep, + } + } + + /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one 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 an IDR when none is. + pub fn recover(&mut self) { + self.history.arm(); } fn coded(&mut self, bytes: Vec) -> CodedFrame { diff --git a/crates/ec-vk/src/history.rs b/crates/ec-vk/src/history.rs index 474310a9..65e13eae 100644 --- a/crates/ec-vk/src/history.rs +++ b/crates/ec-vk/src/history.rs @@ -1,6 +1,6 @@ use vk_video_ffi::DPB_SLOTS; -const CHAIN: usize = 2; +const ANCHORS: usize = DPB_SLOTS - 2; const ANCHOR_EVERY: u64 = 16; const MAX_AGE: u64 = 120; @@ -21,9 +21,9 @@ pub(crate) struct Step { 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. +/// DPB slot bookkeeping: the first slots keep the keyframe and every sixteenth +/// picture as anchors a recovery picture can reference once the receiver +/// acknowledged them, the last two chain each picture to the previous one. pub(crate) struct History { next_id: u64, slots: [Option>; DPB_SLOTS], @@ -49,7 +49,7 @@ impl History { pub(crate) fn ack(&mut self, id: u64) { for held in self.slots.iter_mut().flatten() { - if held.id == id { + if held.id <= id { held.acked = true; } } @@ -75,6 +75,14 @@ impl History { self.slots = [None; DPB_SLOTS]; self.next_anchor = 0; } + if let Some((slot, _)) = acked { + let base = self.slots[slot].map_or(0, |h| h.id); + for held in &mut self.slots { + if held.is_some_and(|h| h.id > base) { + *held = None; + } + } + } let slot = if key || id % ANCHOR_EVERY == 0 { self.anchor_slot(reference_slot) } else { @@ -99,6 +107,15 @@ impl History { self.last = Some((slot, data)); } + pub(crate) fn kept(&self, step: &Step) -> [Option; DPB_SLOTS] { + let reference = step.reference.map(|(slot, _)| slot as usize); + std::array::from_fn(|i| { + self.slots[i] + .filter(|_| i != step.slot as usize && Some(i) != reference) + .map(|h| h.data) + }) + } + fn newest_acked(&self, id: u64) -> Option<(usize, T)> { (0..DPB_SLOTS) .filter_map(|i| self.slots[i].map(|h| (i, h))) @@ -108,10 +125,9 @@ impl History { } 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; + let slot = self.next_anchor; + self.next_anchor = (self.next_anchor + 1) % ANCHORS; if Some(slot) != reference { return slot; } @@ -119,9 +135,9 @@ impl History { } fn chain_slot(&self, reference: Option) -> usize { - (0..CHAIN) + (ANCHORS..DPB_SLOTS) .filter(|&i| Some(i) != reference) .min_by_key(|&i| self.slots[i].map_or(0, |h| h.id + 1)) - .unwrap_or(0) + .unwrap_or(ANCHORS) } } diff --git a/crates/ec-vk/src/lib.rs b/crates/ec-vk/src/lib.rs index 9d350492..954d609d 100644 --- a/crates/ec-vk/src/lib.rs +++ b/crates/ec-vk/src/lib.rs @@ -4,7 +4,8 @@ use std::collections::VecDeque; -use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref}; +use history::History; +use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, LOG2_MAX_FRAME_NUM}; mod av1; mod h265; @@ -102,10 +103,9 @@ pub struct H264Encoder { frame_num: u32, poc: i32, idr_pic_id: u16, - reference: Option, + history: History<(u32, i32)>, uv: Vec, flight: Flight, - next_id: u64, } impl H264Encoder { @@ -137,10 +137,9 @@ impl H264Encoder { frame_num: 0, poc: 0, idr_pic_id: 0, - reference: None, + history: History::new(), uv: vec![0; cfg.width as usize * cfg.height as usize / 2], flight: Flight::default(), - next_id: 0, }) } @@ -246,42 +245,47 @@ impl H264Encoder { } fn next_picture(&mut self, force_keyframe: bool) -> H264Picture { - let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size; - if keyframe { + let step = self + .history + .next(force_keyframe || self.position >= self.gop_size); + if step.key { self.frame_num = 0; self.poc = 0; } else { - self.frame_num = (self.frame_num + 1) % 16; + self.frame_num = (self.frame_num + 1) % (1 << LOG2_MAX_FRAME_NUM); self.poc += 2; } - let reference = if keyframe { None } else { self.reference }; - let slot = reference.map_or(0, |r| 1 - r.slot); let pic = H264Picture { - idr: keyframe, + idr: step.key, idr_pic_id: self.idr_pic_id, frame_num: self.frame_num, poc: self.poc, - slot, - reference, + slot: step.slot, + reference: step.reference.map(|(slot, (frame_num, poc))| H264Ref { slot, frame_num, poc }), }; - self.reference = Some(H264Ref { - slot: pic.slot, - frame_num: pic.frame_num, - poc: pic.poc, - }); - self.position = if keyframe { 1 } else { self.position + 1 }; - if keyframe { + self.history.store(&step, (self.frame_num, self.poc)); + self.position = if step.key { 1 } else { self.position + 1 }; + if step.key { self.idr_pic_id = self.idr_pic_id.wrapping_add(1); } self.flight.push(Meta { - keyframe, - recovery: false, - id: self.next_id, + keyframe: step.key, + recovery: step.recovery, + id: step.id, }); - self.next_id += 1; pic } + /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one 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 an IDR when none is. + pub fn recover(&mut self) { + self.history.arm(); + } + fn coded(&mut self, bytes: Vec) -> CodedFrame { let meta = self.flight.pop(); let data = if meta.keyframe { diff --git a/shims/vk-video-ffi/examples/dmabuf_smoke.rs b/shims/vk-video-ffi/examples/dmabuf_smoke.rs index 9b377b02..0973976b 100644 --- a/shims/vk-video-ffi/examples/dmabuf_smoke.rs +++ b/shims/vk-video-ffi/examples/dmabuf_smoke.rs @@ -165,6 +165,7 @@ fn h265_picture(i: usize) -> H265Picture { slot: ((i - 1) % 2) as u32, poc: 2 * (i - 1) as i32, }), + keep: [None; vk_video_ffi::DPB_SLOTS], } } diff --git a/shims/vk-video-ffi/src/h264_encode.rs b/shims/vk-video-ffi/src/h264_encode.rs index 340522f0..8a3f6769 100644 --- a/shims/vk-video-ffi/src/h264_encode.rs +++ b/shims/vk-video-ffi/src/h264_encode.rs @@ -110,6 +110,10 @@ pub struct H264EncodeConfig { pub colour: Option, } +/// frame_num wraps at 1 << LOG2_MAX_FRAME_NUM. +pub const LOG2_MAX_FRAME_NUM: u8 = 8; +const MAX_REF_FRAMES: u8 = DPB_SLOTS as u8 + 1; + /// One picture to encode. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct H264Picture { @@ -121,13 +125,13 @@ pub struct H264Picture { pub frame_num: u32, /// PicOrderCnt. pub poc: i32, - /// DPB slot this picture is written to, below `DPB_SLOTS`. + /// DPB slot this picture is written to, below `DPB_SLOTS`, 0 for IDR; it becomes long-term reference `slot`. pub slot: u32, /// Reference for a P picture, None for IDR. pub reference: Option, } -/// A picture held in the DPB. +/// A picture held in the DPB as a long-term reference, LongTermFrameIdx = slot. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct H264Ref { /// DPB slot, below `DPB_SLOTS`. @@ -239,6 +243,7 @@ impl RateChain { /// An H.264 Vulkan Video encode session: IDR and P pictures, one reference, NV12 uploaded from CPU. pub struct H264EncodeSession { enc: Encoder, + long_term_open: bool, } impl H264EncodeSession { @@ -298,7 +303,10 @@ impl H264EncodeSession { )?; let params = create_parameters(&enc.vs, cfg, level)?; enc.vs.set_params(params); - Ok(H264EncodeSession { enc }) + Ok(H264EncodeSession { + enc, + long_term_open: false, + }) } /// Annex-B SPS and PPS of this session. @@ -384,10 +392,10 @@ impl H264EncodeSession { 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); - let ref_std = pic - .reference - .map(|r| reference_info(vk::video::STD_VIDEO_H264_PICTURE_TYPE_P, r.frame_num, r.poc)); + let new_std = reference_info(primary, pic.frame_num, pic.poc, pic.slot); + let ref_std = pic.reference.map(|r| { + reference_info(vk::video::STD_VIDEO_H264_PICTURE_TYPE_P, r.frame_num, r.poc, r.slot) + }); let mut new_dpb = vk::VideoEncodeH264DpbSlotInfoKHR::builder() .std_reference_info(&new_std) .build(); @@ -435,13 +443,37 @@ impl H264EncodeSession { if let Some(r) = pic.reference { list0[0] = r.slot as u8; } + let mut modification = zeroed::(); + modification.modification_of_pic_nums_idc = + vk::video::STD_VIDEO_H264_MODIFICATION_OF_PIC_NUMS_IDC_LONG_TERM; + modification.long_term_pic_num = pic.reference.map_or(0, |r| r.slot as u16); + let mut marking = [zeroed::(); 3]; + marking[0].memory_management_control_operation = + vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_SET_MAX_LONG_TERM_INDEX; + marking[0].max_long_term_frame_idx_plus1 = DPB_SLOTS as u16; + marking[1].memory_management_control_operation = + vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_MARK_CURRENT_AS_LONG_TERM; + marking[1].long_term_frame_idx = pic.slot as u16; + marking[2].memory_management_control_operation = + vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_END; let mut lists = zeroed::(); lists.RefPicList0 = list0; lists.RefPicList1 = [0xff; 32]; + if !pic.idr { + let first = usize::from(self.long_term_open); + lists.flags.set_ref_pic_list_modification_flag_l0(1); + lists.refList0ModOpCount = 1; + lists.pRefList0ModOperations = &modification; + lists.refPicMarkingOpCount = (marking.len() - first) as u8; + lists.pRefPicMarkingOperations = marking[first..].as_ptr(); + } + self.long_term_open = !pic.idr; let mut pic_std = zeroed::(); pic_std.flags.set_IdrPicFlag(pic.idr as u32); pic_std.flags.set_is_reference(1); + pic_std.flags.set_long_term_reference_flag(pic.idr as u32); + pic_std.flags.set_adaptive_ref_pic_marking_mode_flag(!pic.idr as u32); pic_std.idr_pic_id = pic.idr_pic_id; pic_std.primary_pic_type = primary; pic_std.frame_num = pic.frame_num; @@ -523,7 +555,7 @@ impl H264EncodeSession { return Err(Error::Invalid("DPB slot".into())); } match pic.reference { - None if pic.idr => Ok(()), + None if pic.idr && pic.slot == 0 => Ok(()), Some(r) if !pic.idr && (r.slot as usize) < DPB_SLOTS && r.slot != pic.slot => Ok(()), _ => Err(Error::Invalid("picture and reference do not match".into())), } @@ -552,7 +584,7 @@ fn create_parameters( vui.num_units_in_tick = cfg.fps.1; vui.time_scale = cfg.fps.0 * 2; vui.max_num_reorder_frames = 0; - vui.max_dec_frame_buffering = 1; + vui.max_dec_frame_buffering = MAX_REF_FRAMES; 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); @@ -574,7 +606,9 @@ fn create_parameters( 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; - sps.max_num_ref_frames = 1; + sps.max_num_ref_frames = MAX_REF_FRAMES; + sps.log2_max_frame_num_minus4 = LOG2_MAX_FRAME_NUM - 4; + sps.flags.set_gaps_in_frame_num_value_allowed_flag(1); sps.pic_width_in_mbs_minus1 = aw / 16 - 1; sps.pic_height_in_map_units_minus1 = ah / 16 - 1; sps.frame_crop_right_offset = (aw - cfg.width) / 2; @@ -645,11 +679,15 @@ fn reference_info( primary: vk::video::StdVideoH264PictureType, frame_num: u32, poc: i32, + slot: u32, ) -> vk::video::StdVideoEncodeH264ReferenceInfo { let mut info = zeroed::(); + info.flags.set_used_for_long_term_reference(1); info.primary_pic_type = primary; info.FrameNum = frame_num; info.PicOrderCnt = poc; + info.long_term_pic_num = slot as u16; + info.long_term_frame_idx = slot as u16; info } diff --git a/shims/vk-video-ffi/src/h265_encode.rs b/shims/vk-video-ffi/src/h265_encode.rs index 23a9892a..988e8dd8 100644 --- a/shims/vk-video-ffi/src/h265_encode.rs +++ b/shims/vk-video-ffi/src/h265_encode.rs @@ -45,6 +45,8 @@ pub struct H265Picture { pub slot: u32, /// Reference for a P picture, None for IDR. pub reference: Option, + /// PicOrderCntVal of every other picture the decoder keeps for a later reference, none for IDR. + pub keep: [Option; DPB_SLOTS], } /// A picture held in the DPB. @@ -402,7 +404,7 @@ impl H265EncodeSession { lists.RefPicList0 = list0; lists.RefPicList1 = [0xff; 15]; - let rps = pic.reference.map(|r| short_term_rps(pic.poc - r.poc)); + let rps = pic.reference.map(|r| short_term_rps(pic.poc, r.poc, &pic.keep)); let mut pic_std = zeroed::(); pic_std.flags.set_IrapPicFlag(pic.idr as u32); @@ -495,12 +497,13 @@ impl H265EncodeSession { return Err(Error::Invalid("DPB slot".into())); } match pic.reference { - None if pic.idr && pic.poc == 0 => Ok(()), + None if pic.idr && pic.poc == 0 && pic.keep.iter().all(Option::is_none) => Ok(()), Some(r) if !pic.idr && (r.slot as usize) < DPB_SLOTS && r.slot != pic.slot - && (1..=65536).contains(&(pic.poc - r.poc)) => + && pic.keep.iter().flatten().chain([&r.poc]).all(|&p| (1..=32768).contains(&(pic.poc - p))) + && pic.keep.iter().flatten().filter(|&&p| p == r.poc).count() == 0 => { Ok(()) } @@ -523,7 +526,7 @@ fn create_parameters( ptl.flags.set_general_frame_only_constraint_flag(1); let mut dpb = zeroed::(); - dpb.max_dec_pic_buffering_minus1[0] = 1; + dpb.max_dec_pic_buffering_minus1[0] = (DPB_SLOTS - 1) as u8; let mut vui = zeroed::(); vui.flags.set_vui_timing_info_present_flag(1); @@ -711,10 +714,18 @@ fn reference_info( info } -fn short_term_rps(delta: i32) -> vk::video::StdVideoH265ShortTermRefPicSet { +fn short_term_rps(poc: i32, used: i32, keep: &[Option]) -> vk::video::StdVideoH265ShortTermRefPicSet { + let mut pocs: Vec = keep.iter().flatten().copied().chain([used]).collect(); + pocs.sort_unstable_by(|a, b| b.cmp(a)); let mut rps = zeroed::(); - rps.num_negative_pics = 1; - rps.delta_poc_s0_minus1[0] = (delta - 1) as u16; - rps.used_by_curr_pic_s0_flag = 1; + rps.num_negative_pics = pocs.len() as u8; + let mut prev = poc; + for (i, &p) in pocs.iter().enumerate() { + rps.delta_poc_s0_minus1[i] = (prev - p - 1) as u16; + if p == used { + rps.used_by_curr_pic_s0_flag |= 1 << i; + } + prev = p; + } rps } diff --git a/shims/vk-video-ffi/src/lib.rs b/shims/vk-video-ffi/src/lib.rs index 0cd8e75d..9ea581ef 100644 --- a/shims/vk-video-ffi/src/lib.rs +++ b/shims/vk-video-ffi/src/lib.rs @@ -38,7 +38,8 @@ pub use h264_decode::{ ScalingLists, }; pub use h264_encode::{ - H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, RateControl, + H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, LOG2_MAX_FRAME_NUM, + RateControl, }; pub use h265_decode::{ H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, H265LongTermRefPicSps,