forge
Explore
Log in

Don't you dare go Hollow

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

Browse files at this commit

c41118999fb42403cf9a01e109f8f9a010992ec3Ahmet Kaan GümüşParent: 8737b14
crates/ec-hw/src/dpb264.rsmodified+1 -0
@@ -356,6 +356,7 @@ impl<S: Clone> Dpb<S> {
356356 };
357357 self.number_short_term(unused, sps);
358358 self.sliding_window(sps);
359+ self.frames.retain(|p| p.stored());
359360 let id = self.next_id();
360361 self.frames.push(Picture {
361362 id,
crates/ec-hw/src/hw.rsmodified+13 -6
@@ -695,21 +695,28 @@ impl HwEncoder {
695695 }
696696 }
697697
698- /// 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.
698+ /// 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.
699699 pub fn ack(&mut self, timestamp: i64) {
700- let Some(&(_, id)) = self.ids.iter().rev().find(|(ts, _)| *ts == timestamp) else {
700+ let Some(&(_, id)) = self.ids.iter().rev().find(|(ts, _)| *ts <= timestamp) else {
701701 return;
702702 };
703- if let EncInner::Vk(VkEncoder::Av1(enc)) = &mut self.inner {
704- enc.ack(id);
703+ match &mut self.inner {
704+ EncInner::Vk(VkEncoder::Av1(enc)) => enc.ack(id),
705+ EncInner::Vk(VkEncoder::H265(enc)) => enc.ack(id),
706+ EncInner::Vk(VkEncoder::H264(enc)) => enc.ack(id),
707+ #[cfg(target_os = "linux")]
708+ EncInner::Va(_) => {}
705709 }
706710 }
707711
708- /// 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.
712+ /// 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.
709713 pub fn recover(&mut self) {
710714 match &mut self.inner {
711715 EncInner::Vk(VkEncoder::Av1(enc)) => enc.recover(),
712- _ => self.keyframe_next = true,
716+ EncInner::Vk(VkEncoder::H265(enc)) => enc.recover(),
717+ EncInner::Vk(VkEncoder::H264(enc)) => enc.recover(),
718+ #[cfg(target_os = "linux")]
719+ EncInner::Va(_) => self.keyframe_next = true,
713720 }
714721 }
715722
crates/ec-hw/src/vkdec264.rsmodified+5 -3
@@ -299,10 +299,12 @@ impl VkH264Decoder {
299299 }
300300
301301 fn fill_gap(&mut self, sps: &Sps, frame_num: u32) -> Result<()> {
302- let before = self.dpb.frames.len();
302+ let before: Vec<i32> = self.dpb.frames.iter().map(|p| p.id).collect();
303303 self.dpb.fill_frame_num_gap(sps, frame_num)?;
304- for i in before..self.dpb.frames.len() {
305- self.dpb.frames[i].surface = self.free_slot()?;
304+ for i in 0..self.dpb.frames.len() {
305+ if !before.contains(&self.dpb.frames[i].id) {
306+ self.dpb.frames[i].surface = self.free_slot()?;
307+ }
306308 }
307309 Ok(())
308310 }
crates/ec-vk/src/av1.rsmodified+1 -1
@@ -236,7 +236,7 @@ impl Av1Encoder {
236236 }
237237 }
238238
239- /// The receiver decoded frame `id` ([`CodedFrame::id`]); a recovery frame may reference it while it stays in the DPB.
239+ /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one while it stays in the DPB.
240240 pub fn ack(&mut self, id: u64) {
241241 self.history.ack(id);
242242 }
crates/ec-vk/src/h265.rsmodified+30 -24
@@ -2,6 +2,7 @@ use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref};
22
33 #[cfg(target_os = "linux")]
44 use crate::DmaBuf;
5+use crate::history::History;
56 use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame};
67
78 /// H.265 encoder settings.
@@ -34,11 +35,10 @@ pub struct H265Encoder {
3435 gop_size: u32,
3536 position: u32,
3637 poc: i32,
37- reference: Option<H265Ref>,
38+ history: History<i32>,
3839 uv: Vec<u8>,
3940 uv16: Vec<u16>,
4041 flight: Flight,
41- next_id: u64,
4242 }
4343
4444 impl H265Encoder {
@@ -68,11 +68,10 @@ impl H265Encoder {
6868 gop_size: cfg.gop_size,
6969 position: 0,
7070 poc: 0,
71- reference: None,
71+ history: History::new(),
7272 uv: vec![0; cfg.width as usize * cfg.height as usize / 2],
7373 uv16: vec![0; cfg.width as usize * cfg.height as usize / 2],
7474 flight: Flight::default(),
75- next_id: 0,
7675 })
7776 }
7877
@@ -212,28 +211,35 @@ impl H265Encoder {
212211 }
213212
214213 fn next_picture(&mut self, force_keyframe: bool) -> H265Picture {
215- let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size;
216- self.poc = if keyframe { 0 } else { self.poc + 1 };
217- let reference = if keyframe { None } else { self.reference };
218- let slot = reference.map_or(0, |r| 1 - r.slot);
219- let pic = H265Picture {
220- idr: keyframe,
221- poc: self.poc,
222- slot,
223- reference,
224- };
225- self.reference = Some(H265Ref {
226- slot: pic.slot,
227- poc: pic.poc,
228- });
229- self.position = if keyframe { 1 } else { self.position + 1 };
214+ let step = self
215+ .history
216+ .next(force_keyframe || self.position >= self.gop_size);
217+ self.poc = if step.key { 0 } else { self.poc + 1 };
218+ let keep = self.history.kept(&step);
219+ self.history.store(&step, self.poc);
220+ self.position = if step.key { 1 } else { self.position + 1 };
230221 self.flight.push(Meta {
231- keyframe,
232- recovery: false,
233- id: self.next_id,
222+ keyframe: step.key,
223+ recovery: step.recovery,
224+ id: step.id,
234225 });
235- self.next_id += 1;
236- pic
226+ H265Picture {
227+ idr: step.key,
228+ poc: self.poc,
229+ slot: step.slot,
230+ reference: step.reference.map(|(slot, poc)| H265Ref { slot, poc }),
231+ keep,
232+ }
233+ }
234+
235+ /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one while it stays in the DPB.
236+ pub fn ack(&mut self, id: u64) {
237+ self.history.ack(id);
238+ }
239+
240+ /// Make the next frame reference the newest acknowledged frame still held, or an IDR when none is.
241+ pub fn recover(&mut self) {
242+ self.history.arm();
237243 }
238244
239245 fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame {
crates/ec-vk/src/history.rsmodified+26 -10
@@ -1,6 +1,6 @@
11 use vk_video_ffi::DPB_SLOTS;
22
3-const CHAIN: usize = 2;
3+const ANCHORS: usize = DPB_SLOTS - 2;
44 const ANCHOR_EVERY: u64 = 16;
55 const MAX_AGE: u64 = 120;
66
@@ -21,9 +21,9 @@ pub(crate) struct Step<T> {
2121 pub(crate) reference: Option<(u32, T)>,
2222 }
2323
24-/// DPB slot bookkeeping: two slots chain each picture to the previous one, the
25-/// rest keep every sixteenth picture as an anchor a recovery picture can
26-/// reference once the receiver acknowledged it.
24+/// DPB slot bookkeeping: the first slots keep the keyframe and every sixteenth
25+/// picture as anchors a recovery picture can reference once the receiver
26+/// acknowledged them, the last two chain each picture to the previous one.
2727 pub(crate) struct History<T> {
2828 next_id: u64,
2929 slots: [Option<Held<T>>; DPB_SLOTS],
@@ -49,7 +49,7 @@ impl<T: Copy> History<T> {
4949
5050 pub(crate) fn ack(&mut self, id: u64) {
5151 for held in self.slots.iter_mut().flatten() {
52- if held.id == id {
52+ if held.id <= id {
5353 held.acked = true;
5454 }
5555 }
@@ -75,6 +75,14 @@ impl<T: Copy> History<T> {
7575 self.slots = [None; DPB_SLOTS];
7676 self.next_anchor = 0;
7777 }
78+ if let Some((slot, _)) = acked {
79+ let base = self.slots[slot].map_or(0, |h| h.id);
80+ for held in &mut self.slots {
81+ if held.is_some_and(|h| h.id > base) {
82+ *held = None;
83+ }
84+ }
85+ }
7886 let slot = if key || id % ANCHOR_EVERY == 0 {
7987 self.anchor_slot(reference_slot)
8088 } else {
@@ -99,6 +107,15 @@ impl<T: Copy> History<T> {
99107 self.last = Some((slot, data));
100108 }
101109
110+ pub(crate) fn kept(&self, step: &Step<T>) -> [Option<T>; DPB_SLOTS] {
111+ let reference = step.reference.map(|(slot, _)| slot as usize);
112+ std::array::from_fn(|i| {
113+ self.slots[i]
114+ .filter(|_| i != step.slot as usize && Some(i) != reference)
115+ .map(|h| h.data)
116+ })
117+ }
118+
102119 fn newest_acked(&self, id: u64) -> Option<(usize, T)> {
103120 (0..DPB_SLOTS)
104121 .filter_map(|i| self.slots[i].map(|h| (i, h)))
@@ -108,10 +125,9 @@ impl<T: Copy> History<T> {
108125 }
109126
110127 fn anchor_slot(&mut self, reference: Option<usize>) -> usize {
111- let anchors = DPB_SLOTS - CHAIN;
112128 loop {
113- let slot = CHAIN + self.next_anchor;
114- self.next_anchor = (self.next_anchor + 1) % anchors;
129+ let slot = self.next_anchor;
130+ self.next_anchor = (self.next_anchor + 1) % ANCHORS;
115131 if Some(slot) != reference {
116132 return slot;
117133 }
@@ -119,9 +135,9 @@ impl<T: Copy> History<T> {
119135 }
120136
121137 fn chain_slot(&self, reference: Option<usize>) -> usize {
122- (0..CHAIN)
138+ (ANCHORS..DPB_SLOTS)
123139 .filter(|&i| Some(i) != reference)
124140 .min_by_key(|&i| self.slots[i].map_or(0, |h| h.id + 1))
125- .unwrap_or(0)
141+ .unwrap_or(ANCHORS)
126142 }
127143 }
crates/ec-vk/src/lib.rsmodified+28 -24
@@ -4,7 +4,8 @@
44
55 use std::collections::VecDeque;
66
7-use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref};
7+use history::History;
8+use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, LOG2_MAX_FRAME_NUM};
89
910 mod av1;
1011 mod h265;
@@ -102,10 +103,9 @@ pub struct H264Encoder {
102103 frame_num: u32,
103104 poc: i32,
104105 idr_pic_id: u16,
105- reference: Option<H264Ref>,
106+ history: History<(u32, i32)>,
106107 uv: Vec<u8>,
107108 flight: Flight,
108- next_id: u64,
109109 }
110110
111111 impl H264Encoder {
@@ -137,10 +137,9 @@ impl H264Encoder {
137137 frame_num: 0,
138138 poc: 0,
139139 idr_pic_id: 0,
140- reference: None,
140+ history: History::new(),
141141 uv: vec![0; cfg.width as usize * cfg.height as usize / 2],
142142 flight: Flight::default(),
143- next_id: 0,
144143 })
145144 }
146145
@@ -246,42 +245,47 @@ impl H264Encoder {
246245 }
247246
248247 fn next_picture(&mut self, force_keyframe: bool) -> H264Picture {
249- let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size;
250- if keyframe {
248+ let step = self
249+ .history
250+ .next(force_keyframe || self.position >= self.gop_size);
251+ if step.key {
251252 self.frame_num = 0;
252253 self.poc = 0;
253254 } else {
254- self.frame_num = (self.frame_num + 1) % 16;
255+ self.frame_num = (self.frame_num + 1) % (1 << LOG2_MAX_FRAME_NUM);
255256 self.poc += 2;
256257 }
257- let reference = if keyframe { None } else { self.reference };
258- let slot = reference.map_or(0, |r| 1 - r.slot);
259258 let pic = H264Picture {
260- idr: keyframe,
259+ idr: step.key,
261260 idr_pic_id: self.idr_pic_id,
262261 frame_num: self.frame_num,
263262 poc: self.poc,
264- slot,
265- reference,
263+ slot: step.slot,
264+ reference: step.reference.map(|(slot, (frame_num, poc))| H264Ref { slot, frame_num, poc }),
266265 };
267- self.reference = Some(H264Ref {
268- slot: pic.slot,
269- frame_num: pic.frame_num,
270- poc: pic.poc,
271- });
272- self.position = if keyframe { 1 } else { self.position + 1 };
273- if keyframe {
266+ self.history.store(&step, (self.frame_num, self.poc));
267+ self.position = if step.key { 1 } else { self.position + 1 };
268+ if step.key {
274269 self.idr_pic_id = self.idr_pic_id.wrapping_add(1);
275270 }
276271 self.flight.push(Meta {
277- keyframe,
278- recovery: false,
279- id: self.next_id,
272+ keyframe: step.key,
273+ recovery: step.recovery,
274+ id: step.id,
280275 });
281- self.next_id += 1;
282276 pic
283277 }
284278
279+ /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one while it stays in the DPB.
280+ pub fn ack(&mut self, id: u64) {
281+ self.history.ack(id);
282+ }
283+
284+ /// Make the next frame reference the newest acknowledged frame still held, or an IDR when none is.
285+ pub fn recover(&mut self) {
286+ self.history.arm();
287+ }
288+
285289 fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame {
286290 let meta = self.flight.pop();
287291 let data = if meta.keyframe {
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+1 -0
@@ -165,6 +165,7 @@ fn h265_picture(i: usize) -> H265Picture {
165165 slot: ((i - 1) % 2) as u32,
166166 poc: 2 * (i - 1) as i32,
167167 }),
168+ keep: [None; vk_video_ffi::DPB_SLOTS],
168169 }
169170 }
170171
shims/vk-video-ffi/src/h264_encode.rsmodified+48 -10
@@ -110,6 +110,10 @@ pub struct H264EncodeConfig {
110110 pub colour: Option<Colour>,
111111 }
112112
113+/// frame_num wraps at 1 << LOG2_MAX_FRAME_NUM.
114+pub const LOG2_MAX_FRAME_NUM: u8 = 8;
115+const MAX_REF_FRAMES: u8 = DPB_SLOTS as u8 + 1;
116+
113117 /// One picture to encode.
114118 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
115119 pub struct H264Picture {
@@ -121,13 +125,13 @@ pub struct H264Picture {
121125 pub frame_num: u32,
122126 /// PicOrderCnt.
123127 pub poc: i32,
124- /// DPB slot this picture is written to, below `DPB_SLOTS`.
128+ /// DPB slot this picture is written to, below `DPB_SLOTS`, 0 for IDR; it becomes long-term reference `slot`.
125129 pub slot: u32,
126130 /// Reference for a P picture, None for IDR.
127131 pub reference: Option<H264Ref>,
128132 }
129133
130-/// A picture held in the DPB.
134+/// A picture held in the DPB as a long-term reference, LongTermFrameIdx = slot.
131135 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
132136 pub struct H264Ref {
133137 /// DPB slot, below `DPB_SLOTS`.
@@ -239,6 +243,7 @@ impl RateChain {
239243 /// An H.264 Vulkan Video encode session: IDR and P pictures, one reference, NV12 uploaded from CPU.
240244 pub struct H264EncodeSession {
241245 enc: Encoder,
246+ long_term_open: bool,
242247 }
243248
244249 impl H264EncodeSession {
@@ -298,7 +303,10 @@ impl H264EncodeSession {
298303 )?;
299304 let params = create_parameters(&enc.vs, cfg, level)?;
300305 enc.vs.set_params(params);
301- Ok(H264EncodeSession { enc })
306+ Ok(H264EncodeSession {
307+ enc,
308+ long_term_open: false,
309+ })
302310 }
303311
304312 /// Annex-B SPS and PPS of this session.
@@ -384,10 +392,10 @@ impl H264EncodeSession {
384392 let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent));
385393 let src_pr = picture_resource(self.enc.src_view(), extent);
386394
387- let new_std = reference_info(primary, pic.frame_num, pic.poc);
388- let ref_std = pic
389- .reference
390- .map(|r| reference_info(vk::video::STD_VIDEO_H264_PICTURE_TYPE_P, r.frame_num, r.poc));
395+ let new_std = reference_info(primary, pic.frame_num, pic.poc, pic.slot);
396+ let ref_std = pic.reference.map(|r| {
397+ reference_info(vk::video::STD_VIDEO_H264_PICTURE_TYPE_P, r.frame_num, r.poc, r.slot)
398+ });
391399 let mut new_dpb = vk::VideoEncodeH264DpbSlotInfoKHR::builder()
392400 .std_reference_info(&new_std)
393401 .build();
@@ -435,13 +443,37 @@ impl H264EncodeSession {
435443 if let Some(r) = pic.reference {
436444 list0[0] = r.slot as u8;
437445 }
446+ let mut modification = zeroed::<vk::video::StdVideoEncodeH264RefListModEntry>();
447+ modification.modification_of_pic_nums_idc =
448+ vk::video::STD_VIDEO_H264_MODIFICATION_OF_PIC_NUMS_IDC_LONG_TERM;
449+ modification.long_term_pic_num = pic.reference.map_or(0, |r| r.slot as u16);
450+ let mut marking = [zeroed::<vk::video::StdVideoEncodeH264RefPicMarkingEntry>(); 3];
451+ marking[0].memory_management_control_operation =
452+ vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_SET_MAX_LONG_TERM_INDEX;
453+ marking[0].max_long_term_frame_idx_plus1 = DPB_SLOTS as u16;
454+ marking[1].memory_management_control_operation =
455+ vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_MARK_CURRENT_AS_LONG_TERM;
456+ marking[1].long_term_frame_idx = pic.slot as u16;
457+ marking[2].memory_management_control_operation =
458+ vk::video::STD_VIDEO_H264_MEM_MGMT_CONTROL_OP_END;
438459 let mut lists = zeroed::<vk::video::StdVideoEncodeH264ReferenceListsInfo>();
439460 lists.RefPicList0 = list0;
440461 lists.RefPicList1 = [0xff; 32];
462+ if !pic.idr {
463+ let first = usize::from(self.long_term_open);
464+ lists.flags.set_ref_pic_list_modification_flag_l0(1);
465+ lists.refList0ModOpCount = 1;
466+ lists.pRefList0ModOperations = &modification;
467+ lists.refPicMarkingOpCount = (marking.len() - first) as u8;
468+ lists.pRefPicMarkingOperations = marking[first..].as_ptr();
469+ }
470+ self.long_term_open = !pic.idr;
441471
442472 let mut pic_std = zeroed::<vk::video::StdVideoEncodeH264PictureInfo>();
443473 pic_std.flags.set_IdrPicFlag(pic.idr as u32);
444474 pic_std.flags.set_is_reference(1);
475+ pic_std.flags.set_long_term_reference_flag(pic.idr as u32);
476+ pic_std.flags.set_adaptive_ref_pic_marking_mode_flag(!pic.idr as u32);
445477 pic_std.idr_pic_id = pic.idr_pic_id;
446478 pic_std.primary_pic_type = primary;
447479 pic_std.frame_num = pic.frame_num;
@@ -523,7 +555,7 @@ impl H264EncodeSession {
523555 return Err(Error::Invalid("DPB slot".into()));
524556 }
525557 match pic.reference {
526- None if pic.idr => Ok(()),
558+ None if pic.idr && pic.slot == 0 => Ok(()),
527559 Some(r) if !pic.idr && (r.slot as usize) < DPB_SLOTS && r.slot != pic.slot => Ok(()),
528560 _ => Err(Error::Invalid("picture and reference do not match".into())),
529561 }
@@ -552,7 +584,7 @@ fn create_parameters(
552584 vui.num_units_in_tick = cfg.fps.1;
553585 vui.time_scale = cfg.fps.0 * 2;
554586 vui.max_num_reorder_frames = 0;
555- vui.max_dec_frame_buffering = 1;
587+ vui.max_dec_frame_buffering = MAX_REF_FRAMES;
556588 if let Some(c) = cfg.colour {
557589 vui.flags.set_video_signal_type_present_flag(1);
558590 vui.flags.set_video_full_range_flag(c.full_range as u32);
@@ -574,7 +606,9 @@ fn create_parameters(
574606 sps.level_idc = level;
575607 sps.chroma_format_idc = vk::video::STD_VIDEO_H264_CHROMA_FORMAT_IDC_420;
576608 sps.pic_order_cnt_type = vk::video::STD_VIDEO_H264_POC_TYPE_2;
577- sps.max_num_ref_frames = 1;
609+ sps.max_num_ref_frames = MAX_REF_FRAMES;
610+ sps.log2_max_frame_num_minus4 = LOG2_MAX_FRAME_NUM - 4;
611+ sps.flags.set_gaps_in_frame_num_value_allowed_flag(1);
578612 sps.pic_width_in_mbs_minus1 = aw / 16 - 1;
579613 sps.pic_height_in_map_units_minus1 = ah / 16 - 1;
580614 sps.frame_crop_right_offset = (aw - cfg.width) / 2;
@@ -645,11 +679,15 @@ fn reference_info(
645679 primary: vk::video::StdVideoH264PictureType,
646680 frame_num: u32,
647681 poc: i32,
682+ slot: u32,
648683 ) -> vk::video::StdVideoEncodeH264ReferenceInfo {
649684 let mut info = zeroed::<vk::video::StdVideoEncodeH264ReferenceInfo>();
685+ info.flags.set_used_for_long_term_reference(1);
650686 info.primary_pic_type = primary;
651687 info.FrameNum = frame_num;
652688 info.PicOrderCnt = poc;
689+ info.long_term_pic_num = slot as u16;
690+ info.long_term_frame_idx = slot as u16;
653691 info
654692 }
655693
shims/vk-video-ffi/src/h265_encode.rsmodified+19 -8
@@ -45,6 +45,8 @@ pub struct H265Picture {
4545 pub slot: u32,
4646 /// Reference for a P picture, None for IDR.
4747 pub reference: Option<H265Ref>,
48+ /// PicOrderCntVal of every other picture the decoder keeps for a later reference, none for IDR.
49+ pub keep: [Option<i32>; DPB_SLOTS],
4850 }
4951
5052 /// A picture held in the DPB.
@@ -402,7 +404,7 @@ impl H265EncodeSession {
402404 lists.RefPicList0 = list0;
403405 lists.RefPicList1 = [0xff; 15];
404406
405- let rps = pic.reference.map(|r| short_term_rps(pic.poc - r.poc));
407+ let rps = pic.reference.map(|r| short_term_rps(pic.poc, r.poc, &pic.keep));
406408
407409 let mut pic_std = zeroed::<vk::video::StdVideoEncodeH265PictureInfo>();
408410 pic_std.flags.set_IrapPicFlag(pic.idr as u32);
@@ -495,12 +497,13 @@ impl H265EncodeSession {
495497 return Err(Error::Invalid("DPB slot".into()));
496498 }
497499 match pic.reference {
498- None if pic.idr && pic.poc == 0 => Ok(()),
500+ None if pic.idr && pic.poc == 0 && pic.keep.iter().all(Option::is_none) => Ok(()),
499501 Some(r)
500502 if !pic.idr
501503 && (r.slot as usize) < DPB_SLOTS
502504 && r.slot != pic.slot
503- && (1..=65536).contains(&(pic.poc - r.poc)) =>
505+ && pic.keep.iter().flatten().chain([&r.poc]).all(|&p| (1..=32768).contains(&(pic.poc - p)))
506+ && pic.keep.iter().flatten().filter(|&&p| p == r.poc).count() == 0 =>
504507 {
505508 Ok(())
506509 }
@@ -523,7 +526,7 @@ fn create_parameters(
523526 ptl.flags.set_general_frame_only_constraint_flag(1);
524527
525528 let mut dpb = zeroed::<vk::video::StdVideoH265DecPicBufMgr>();
526- dpb.max_dec_pic_buffering_minus1[0] = 1;
529+ dpb.max_dec_pic_buffering_minus1[0] = (DPB_SLOTS - 1) as u8;
527530
528531 let mut vui = zeroed::<vk::video::StdVideoH265SequenceParameterSetVui>();
529532 vui.flags.set_vui_timing_info_present_flag(1);
@@ -711,10 +714,18 @@ fn reference_info(
711714 info
712715 }
713716
714-fn short_term_rps(delta: i32) -> vk::video::StdVideoH265ShortTermRefPicSet {
717+fn short_term_rps(poc: i32, used: i32, keep: &[Option<i32>]) -> vk::video::StdVideoH265ShortTermRefPicSet {
718+ let mut pocs: Vec<i32> = keep.iter().flatten().copied().chain([used]).collect();
719+ pocs.sort_unstable_by(|a, b| b.cmp(a));
715720 let mut rps = zeroed::<vk::video::StdVideoH265ShortTermRefPicSet>();
716- rps.num_negative_pics = 1;
717- rps.delta_poc_s0_minus1[0] = (delta - 1) as u16;
718- rps.used_by_curr_pic_s0_flag = 1;
721+ rps.num_negative_pics = pocs.len() as u8;
722+ let mut prev = poc;
723+ for (i, &p) in pocs.iter().enumerate() {
724+ rps.delta_poc_s0_minus1[i] = (prev - p - 1) as u16;
725+ if p == used {
726+ rps.used_by_curr_pic_s0_flag |= 1 << i;
727+ }
728+ prev = p;
729+ }
719730 rps
720731 }
shims/vk-video-ffi/src/lib.rsmodified+2 -1
@@ -38,7 +38,8 @@ pub use h264_decode::{
3838 ScalingLists,
3939 };
4040 pub use h264_encode::{
41- H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, RateControl,
41+ H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, LOG2_MAX_FRAME_NUM,
42+ RateControl,
4243 };
4344 pub use h265_decode::{
4445 H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, H265LongTermRefPicSps,