| 1 | use vk_video_ffi::DPB_SLOTS; |
| 2 | |
| 3 | const ANCHORS: usize = DPB_SLOTS - 2; |
| 4 | const ANCHOR_EVERY: u64 = 16; |
| 5 | const MAX_AGE: u64 = 120; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy)] |
| 8 | struct Held<T> { |
| 9 | id: u64, |
| 10 | acked: bool, |
| 11 | data: T, |
| 12 | } |
| 13 | |
| 14 | |
| 15 | #[derive(Debug, Clone, Copy)] |
| 16 | pub(crate) struct Step<T> { |
| 17 | pub(crate) id: u64, |
| 18 | pub(crate) key: bool, |
| 19 | pub(crate) recovery: bool, |
| 20 | pub(crate) slot: u32, |
| 21 | pub(crate) reference: Option<(u32, T)>, |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | pub(crate) struct History<T> { |
| 28 | next_id: u64, |
| 29 | slots: [Option<Held<T>>; DPB_SLOTS], |
| 30 | last: Option<(usize, T)>, |
| 31 | next_anchor: usize, |
| 32 | armed: bool, |
| 33 | } |
| 34 | |
| 35 | impl<T: Copy> History<T> { |
| 36 | pub(crate) fn new() -> Self { |
| 37 | History { |
| 38 | next_id: 0, |
| 39 | slots: [None; DPB_SLOTS], |
| 40 | last: None, |
| 41 | next_anchor: 0, |
| 42 | armed: false, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub(crate) fn arm(&mut self) { |
| 47 | self.armed = true; |
| 48 | } |
| 49 | |
| 50 | pub(crate) fn ack(&mut self, id: u64) { |
| 51 | for held in self.slots.iter_mut().flatten() { |
| 52 | if held.id <= id { |
| 53 | held.acked = true; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub(crate) fn next(&mut self, want_key: bool) -> Step<T> { |
| 59 | let id = self.next_id; |
| 60 | self.next_id += 1; |
| 61 | let armed = std::mem::take(&mut self.armed); |
| 62 | let acked = if armed && !want_key { |
| 63 | self.newest_acked(id) |
| 64 | } else { |
| 65 | None |
| 66 | }; |
| 67 | let key = want_key || self.last.is_none() || (armed && acked.is_none()); |
| 68 | let reference = match (key, acked) { |
| 69 | (true, _) => None, |
| 70 | (false, Some(r)) => Some(r), |
| 71 | (false, None) => self.last, |
| 72 | }; |
| 73 | let reference_slot = reference.map(|(slot, _)| slot); |
| 74 | if key { |
| 75 | self.slots = [None; DPB_SLOTS]; |
| 76 | self.next_anchor = 0; |
| 77 | } |
| 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 | } |
| 86 | let slot = if key || id % ANCHOR_EVERY == 0 { |
| 87 | self.anchor_slot(reference_slot) |
| 88 | } else { |
| 89 | self.chain_slot(reference_slot) |
| 90 | }; |
| 91 | Step { |
| 92 | id, |
| 93 | key, |
| 94 | recovery: !key && acked.is_some(), |
| 95 | slot: slot as u32, |
| 96 | reference: reference.map(|(slot, data)| (slot as u32, data)), |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub(crate) fn store(&mut self, step: &Step<T>, data: T) { |
| 101 | let slot = step.slot as usize; |
| 102 | self.slots[slot] = Some(Held { |
| 103 | id: step.id, |
| 104 | acked: false, |
| 105 | data, |
| 106 | }); |
| 107 | self.last = Some((slot, data)); |
| 108 | } |
| 109 | |
| 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 | |
| 119 | fn newest_acked(&self, id: u64) -> Option<(usize, T)> { |
| 120 | (0..DPB_SLOTS) |
| 121 | .filter_map(|i| self.slots[i].map(|h| (i, h))) |
| 122 | .filter(|(_, h)| h.acked && id - h.id < MAX_AGE) |
| 123 | .max_by_key(|(_, h)| h.id) |
| 124 | .map(|(i, h)| (i, h.data)) |
| 125 | } |
| 126 | |
| 127 | fn anchor_slot(&mut self, reference: Option<usize>) -> usize { |
| 128 | loop { |
| 129 | let slot = self.next_anchor; |
| 130 | self.next_anchor = (self.next_anchor + 1) % ANCHORS; |
| 131 | if Some(slot) != reference { |
| 132 | return slot; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn chain_slot(&self, reference: Option<usize>) -> usize { |
| 138 | (ANCHORS..DPB_SLOTS) |
| 139 | .filter(|&i| Some(i) != reference) |
| 140 | .min_by_key(|&i| self.slots[i].map_or(0, |h| h.id + 1)) |
| 141 | .unwrap_or(ANCHORS) |
| 142 | } |
| 143 | } |