ec-hw: reference picture selection for Vulkan AV1 — HwEncoder::ack(timestamp) and recover(); a recovery frame references the newest acked frame still in the 8-slot DPB (2 chain + 6 anchors every 16 frames), else a keyframe; CodedFrame::recovery
Browse files at this commit8737b14cc020ad92c7f8ce4a2b73684069c1d0ffParent: c86cfc112 changed files +300 -96
- crates/ec-hw/src/enc/mod.rs +1 -0
- crates/ec-hw/src/hw.rs +63 -37
- crates/ec-hw/src/types.rs +2 -0
- crates/ec-vk/src/av1.rs +38 -22
- crates/ec-vk/src/h265.rs +17 -5
- crates/ec-vk/src/history.rs +127 -0
- crates/ec-vk/src/lib.rs +33 -9
- shims/vk-video-ffi/src/av1_encode.rs +3 -6
- shims/vk-video-ffi/src/h264_encode.rs +3 -6
- shims/vk-video-ffi/src/h265_encode.rs +3 -6
- shims/vk-video-ffi/src/lib.rs +1 -0
- shims/vk-video-ffi/src/session.rs +9 -5
crates/ec-hw/src/enc/mod.rsmodified+1 -0
| @@ -661,6 +661,7 @@ impl Encoder { | ||
| 661 | 661 | data, |
| 662 | 662 | timestamp, |
| 663 | 663 | is_keyframe: keyframe, |
| 664 | + recovery: false, | |
| 664 | 665 | }) |
| 665 | 666 | } |
| 666 | 667 |
crates/ec-hw/src/hw.rsmodified+63 -37
| @@ -402,13 +402,7 @@ enum VkEncoder { | ||
| 402 | 402 | Av1(ec_vk::Av1Encoder), |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | -fn stamp(pending: &mut VecDeque<i64>, coded: ec_vk::CodedFrame) -> CodedFrame { | |
| 406 | - CodedFrame { | |
| 407 | - data: coded.data, | |
| 408 | - timestamp: pending.pop_front().unwrap_or_default(), | |
| 409 | - is_keyframe: coded.keyframe, | |
| 410 | - } | |
| 411 | -} | |
| 405 | +const ID_HISTORY: usize = 512; | |
| 412 | 406 | |
| 413 | 407 | impl VkEncoder { |
| 414 | 408 | fn submit( |
| @@ -579,6 +573,8 @@ pub struct HwEncoder { | ||
| 579 | 573 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] |
| 580 | 574 | size: (usize, usize), |
| 581 | 575 | pending: VecDeque<i64>, |
| 576 | + ids: VecDeque<(i64, u64)>, | |
| 577 | + keyframe_next: bool, | |
| 582 | 578 | tunings: Tunings, |
| 583 | 579 | rc: RateFeedback, |
| 584 | 580 | } |
| @@ -602,6 +598,8 @@ impl HwEncoder { | ||
| 602 | 598 | config, |
| 603 | 599 | size, |
| 604 | 600 | pending: VecDeque::new(), |
| 601 | + ids: VecDeque::new(), | |
| 602 | + keyframe_next: false, | |
| 605 | 603 | tunings: Tunings { |
| 606 | 604 | bitrate: config.bitrate, |
| 607 | 605 | framerate: config.framerate, |
| @@ -645,15 +643,17 @@ impl HwEncoder { | ||
| 645 | 643 | |
| 646 | 644 | /// Submit one picture without waiting; the oldest coded picture comes back once two are in flight. Call [`HwEncoder::flush`] at the end. |
| 647 | 645 | pub fn submit(&mut self, input: EncodeInput, meta: FrameMetadata) -> Result<Option<CodedFrame>> { |
| 646 | + let meta = self.with_recovery(meta); | |
| 648 | 647 | match &mut self.inner { |
| 649 | 648 | EncInner::Vk(enc) => { |
| 650 | 649 | vk_session_alive(self.born_epoch)?; |
| 651 | 650 | self.pending.push_back(meta.timestamp); |
| 652 | - let out = enc.submit(input, meta.force_keyframe)?; | |
| 653 | - if let Some(coded) = &out { | |
| 654 | - self.steer(coded.data.len())?; | |
| 655 | - } | |
| 656 | - Ok(out.map(|coded| stamp(&mut self.pending, coded))) | |
| 651 | + let Some(coded) = enc.submit(input, meta.force_keyframe)? else { | |
| 652 | + return Ok(None); | |
| 653 | + }; | |
| 654 | + self.steer(coded.data.len())?; | |
| 655 | + let timestamp = self.pending.pop_front().unwrap_or_default(); | |
| 656 | + Ok(Some(self.vk_frame(coded, timestamp))) | |
| 657 | 657 | } |
| 658 | 658 | #[cfg(target_os = "linux")] |
| 659 | 659 | EncInner::Va(enc) => match input { |
| @@ -674,13 +674,13 @@ impl HwEncoder { | ||
| 674 | 674 | EncInner::Vk(enc) => { |
| 675 | 675 | vk_session_alive(self.born_epoch)?; |
| 676 | 676 | let frames = enc.flush()?; |
| 677 | - for coded in &frames { | |
| 677 | + let mut out = Vec::with_capacity(frames.len()); | |
| 678 | + for coded in frames { | |
| 678 | 679 | self.steer(coded.data.len())?; |
| 680 | + let timestamp = self.pending.pop_front().unwrap_or_default(); | |
| 681 | + out.push(self.vk_frame(coded, timestamp)); | |
| 679 | 682 | } |
| 680 | - Ok(frames | |
| 681 | - .into_iter() | |
| 682 | - .map(|coded| stamp(&mut self.pending, coded)) | |
| 683 | - .collect()) | |
| 683 | + Ok(out) | |
| 684 | 684 | } |
| 685 | 685 | #[cfg(target_os = "linux")] |
| 686 | 686 | EncInner::Va(enc) => enc.flush(), |
| @@ -695,6 +695,44 @@ impl HwEncoder { | ||
| 695 | 695 | } |
| 696 | 696 | } |
| 697 | 697 | |
| 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. | |
| 699 | + pub fn ack(&mut self, timestamp: i64) { | |
| 700 | + let Some(&(_, id)) = self.ids.iter().rev().find(|(ts, _)| *ts == timestamp) else { | |
| 701 | + return; | |
| 702 | + }; | |
| 703 | + if let EncInner::Vk(VkEncoder::Av1(enc)) = &mut self.inner { | |
| 704 | + enc.ack(id); | |
| 705 | + } | |
| 706 | + } | |
| 707 | + | |
| 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. | |
| 709 | + pub fn recover(&mut self) { | |
| 710 | + match &mut self.inner { | |
| 711 | + EncInner::Vk(VkEncoder::Av1(enc)) => enc.recover(), | |
| 712 | + _ => self.keyframe_next = true, | |
| 713 | + } | |
| 714 | + } | |
| 715 | + | |
| 716 | + fn with_recovery(&mut self, meta: FrameMetadata) -> FrameMetadata { | |
| 717 | + FrameMetadata { | |
| 718 | + force_keyframe: meta.force_keyframe || std::mem::take(&mut self.keyframe_next), | |
| 719 | + ..meta | |
| 720 | + } | |
| 721 | + } | |
| 722 | + | |
| 723 | + fn vk_frame(&mut self, coded: ec_vk::CodedFrame, timestamp: i64) -> CodedFrame { | |
| 724 | + if self.ids.len() == ID_HISTORY { | |
| 725 | + self.ids.pop_front(); | |
| 726 | + } | |
| 727 | + self.ids.push_back((timestamp, coded.id)); | |
| 728 | + CodedFrame { | |
| 729 | + data: coded.data, | |
| 730 | + timestamp, | |
| 731 | + is_keyframe: coded.keyframe, | |
| 732 | + recovery: coded.recovery, | |
| 733 | + } | |
| 734 | + } | |
| 735 | + | |
| 698 | 736 | fn steer(&mut self, bytes: usize) -> Result<()> { |
| 699 | 737 | if !self.rc.track(bytes, self.config.rate_control, self.tunings) { |
| 700 | 738 | return Ok(()); |
| @@ -710,17 +748,14 @@ impl HwEncoder { | ||
| 710 | 748 | |
| 711 | 749 | /// Encode one frame; `meta.force_keyframe` starts a new GOP. |
| 712 | 750 | pub fn encode(&mut self, frame: &I420, meta: FrameMetadata) -> Result<CodedFrame> { |
| 751 | + let meta = self.with_recovery(meta); | |
| 713 | 752 | self.idle()?; |
| 714 | 753 | match &mut self.inner { |
| 715 | 754 | EncInner::Vk(enc) => { |
| 716 | 755 | vk_session_alive(self.born_epoch)?; |
| 717 | 756 | let coded = enc.encode(frame, meta.force_keyframe)?; |
| 718 | 757 | self.steer(coded.data.len())?; |
| 719 | - Ok(CodedFrame { | |
| 720 | - data: coded.data, | |
| 721 | - timestamp: meta.timestamp, | |
| 722 | - is_keyframe: coded.keyframe, | |
| 723 | - }) | |
| 758 | + Ok(self.vk_frame(coded, meta.timestamp)) | |
| 724 | 759 | } |
| 725 | 760 | #[cfg(target_os = "linux")] |
| 726 | 761 | EncInner::Va(enc) => enc.encode(frame, meta), |
| @@ -729,16 +764,13 @@ impl HwEncoder { | ||
| 729 | 764 | |
| 730 | 765 | /// Encode one 10-bit frame of a 10-bit encoder (H.265 and AV1), the [`HwEncoder::encode`] of a 10-bit encoder. |
| 731 | 766 | pub fn encode_16(&mut self, frame: &I420_16, meta: FrameMetadata) -> Result<CodedFrame> { |
| 767 | + let meta = self.with_recovery(meta); | |
| 732 | 768 | self.idle()?; |
| 733 | 769 | match &mut self.inner { |
| 734 | 770 | EncInner::Vk(enc) => { |
| 735 | 771 | let coded = enc.encode_16(frame, meta.force_keyframe)?; |
| 736 | 772 | self.steer(coded.data.len())?; |
| 737 | - Ok(CodedFrame { | |
| 738 | - data: coded.data, | |
| 739 | - timestamp: meta.timestamp, | |
| 740 | - is_keyframe: coded.keyframe, | |
| 741 | - }) | |
| 773 | + Ok(self.vk_frame(coded, meta.timestamp)) | |
| 742 | 774 | } |
| 743 | 775 | #[cfg(target_os = "linux")] |
| 744 | 776 | EncInner::Va(enc) => enc.encode_16(frame, meta), |
| @@ -747,17 +779,14 @@ impl HwEncoder { | ||
| 747 | 779 | |
| 748 | 780 | /// 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. |
| 749 | 781 | pub fn encode_rgb(&mut self, frame: &RgbFrame, meta: FrameMetadata) -> Result<CodedFrame> { |
| 782 | + let meta = self.with_recovery(meta); | |
| 750 | 783 | self.idle()?; |
| 751 | 784 | match &mut self.inner { |
| 752 | 785 | EncInner::Vk(enc) => { |
| 753 | 786 | vk_session_alive(self.born_epoch)?; |
| 754 | 787 | let coded = enc.encode_rgb(frame, meta.force_keyframe)?; |
| 755 | 788 | self.steer(coded.data.len())?; |
| 756 | - Ok(CodedFrame { | |
| 757 | - data: coded.data, | |
| 758 | - timestamp: meta.timestamp, | |
| 759 | - is_keyframe: coded.keyframe, | |
| 760 | - }) | |
| 789 | + Ok(self.vk_frame(coded, meta.timestamp)) | |
| 761 | 790 | } |
| 762 | 791 | #[cfg(target_os = "linux")] |
| 763 | 792 | EncInner::Va(enc) => enc.encode(&rgb_to_i420(frame, self.size)?, meta), |
| @@ -811,17 +840,14 @@ impl HwEncoder { | ||
| 811 | 840 | |
| 812 | 841 | #[cfg(target_os = "linux")] |
| 813 | 842 | fn encode_dma(&mut self, src: &DmaBuf, meta: FrameMetadata) -> Result<CodedFrame> { |
| 843 | + let meta = self.with_recovery(meta); | |
| 814 | 844 | self.idle()?; |
| 815 | 845 | match &mut self.inner { |
| 816 | 846 | EncInner::Vk(enc) => { |
| 817 | 847 | vk_session_alive(self.born_epoch)?; |
| 818 | 848 | let coded = enc.encode_dmabuf(src, meta.force_keyframe)?; |
| 819 | 849 | self.steer(coded.data.len())?; |
| 820 | - Ok(CodedFrame { | |
| 821 | - data: coded.data, | |
| 822 | - timestamp: meta.timestamp, | |
| 823 | - is_keyframe: coded.keyframe, | |
| 824 | - }) | |
| 850 | + Ok(self.vk_frame(coded, meta.timestamp)) | |
| 825 | 851 | } |
| 826 | 852 | EncInner::Va(_) => Err(Error::unsupported( |
| 827 | 853 | "DMA-BUF encode", |
crates/ec-hw/src/types.rsmodified+2 -0
| @@ -196,6 +196,8 @@ pub struct CodedFrame { | ||
| 196 | 196 | pub timestamp: i64, |
| 197 | 197 | /// True for an IDR / key frame. |
| 198 | 198 | pub is_keyframe: bool, |
| 199 | + /// True for an inter frame that references an acknowledged older frame after [`crate::HwEncoder::recover`]. | |
| 200 | + pub recovery: bool, | |
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | /// The codecs this crate decodes in hardware. |
crates/ec-vk/src/av1.rsmodified+38 -22
| @@ -2,7 +2,8 @@ use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; | ||
| 2 | 2 | |
| 3 | 3 | #[cfg(target_os = "linux")] |
| 4 | 4 | use crate::DmaBuf; |
| 5 | -use crate::{CodedFrame, Colour, Error, Flight, Gpu, RateControl, last_frame}; | |
| 5 | +use crate::history::History; | |
| 6 | +use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; | |
| 6 | 7 | |
| 7 | 8 | const TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00]; |
| 8 | 9 | |
| @@ -36,7 +37,7 @@ pub struct Av1Encoder { | ||
| 36 | 37 | gop_size: u32, |
| 37 | 38 | position: u32, |
| 38 | 39 | order_hint: u8, |
| 39 | - reference: Option<Av1Ref>, | |
| 40 | + history: History<u8>, | |
| 40 | 41 | uv: Vec<u8>, |
| 41 | 42 | uv16: Vec<u16>, |
| 42 | 43 | flight: Flight, |
| @@ -69,7 +70,7 @@ impl Av1Encoder { | ||
| 69 | 70 | gop_size: cfg.gop_size, |
| 70 | 71 | position: 0, |
| 71 | 72 | order_hint: 0, |
| 72 | - reference: None, | |
| 73 | + history: History::new(), | |
| 73 | 74 | uv: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 74 | 75 | uv16: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 75 | 76 | flight: Flight::default(), |
| @@ -212,36 +213,51 @@ impl Av1Encoder { | ||
| 212 | 213 | } |
| 213 | 214 | |
| 214 | 215 | fn next_picture(&mut self, force_keyframe: bool) -> Av1Picture { |
| 215 | - let keyframe = force_keyframe || self.reference.is_none() || self.position >= self.gop_size; | |
| 216 | - self.order_hint = if keyframe { | |
| 216 | + let step = self | |
| 217 | + .history | |
| 218 | + .next(force_keyframe || self.position >= self.gop_size); | |
| 219 | + self.order_hint = if step.key { | |
| 217 | 220 | 0 |
| 218 | 221 | } else { |
| 219 | 222 | self.order_hint.wrapping_add(1) |
| 220 | 223 | }; |
| 221 | - let reference = if keyframe { None } else { self.reference }; | |
| 222 | - let slot = reference.map_or(0, |r| 1 - r.slot); | |
| 223 | - let pic = Av1Picture { | |
| 224 | - key: keyframe, | |
| 225 | - order_hint: self.order_hint, | |
| 226 | - slot, | |
| 227 | - reference, | |
| 228 | - }; | |
| 229 | - self.reference = Some(Av1Ref { | |
| 230 | - slot: pic.slot, | |
| 231 | - order_hint: pic.order_hint, | |
| 224 | + self.history.store(&step, self.order_hint); | |
| 225 | + self.position = if step.key { 1 } else { self.position + 1 }; | |
| 226 | + self.flight.push(Meta { | |
| 227 | + keyframe: step.key, | |
| 228 | + recovery: step.recovery, | |
| 229 | + id: step.id, | |
| 232 | 230 | }); |
| 233 | - self.position = if keyframe { 1 } else { self.position + 1 }; | |
| 234 | - self.flight.push(keyframe); | |
| 235 | - pic | |
| 231 | + Av1Picture { | |
| 232 | + key: step.key, | |
| 233 | + order_hint: self.order_hint, | |
| 234 | + slot: step.slot, | |
| 235 | + reference: step.reference.map(|(slot, order_hint)| Av1Ref { slot, order_hint }), | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + /// The receiver decoded frame `id` ([`CodedFrame::id`]); a recovery frame may reference it while it stays in the DPB. | |
| 240 | + pub fn ack(&mut self, id: u64) { | |
| 241 | + self.history.ack(id); | |
| 242 | + } | |
| 243 | + | |
| 244 | + /// Make the next frame reference the newest acknowledged frame still held, or a KEY frame when none is. | |
| 245 | + pub fn recover(&mut self) { | |
| 246 | + self.history.arm(); | |
| 236 | 247 | } |
| 237 | 248 | |
| 238 | 249 | fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { |
| 239 | - let keyframe = self.flight.pop(); | |
| 250 | + let meta = self.flight.pop(); | |
| 240 | 251 | let mut data = TEMPORAL_DELIMITER.to_vec(); |
| 241 | - if keyframe { | |
| 252 | + if meta.keyframe { | |
| 242 | 253 | data.extend_from_slice(&self.sequence_header); |
| 243 | 254 | } |
| 244 | 255 | data.extend_from_slice(&bytes); |
| 245 | - CodedFrame { data, keyframe } | |
| 256 | + CodedFrame { | |
| 257 | + data, | |
| 258 | + keyframe: meta.keyframe, | |
| 259 | + recovery: meta.recovery, | |
| 260 | + id: meta.id, | |
| 261 | + } | |
| 246 | 262 | } |
| 247 | 263 | } |
crates/ec-vk/src/h265.rsmodified+17 -5
| @@ -2,7 +2,7 @@ use vk_video_ffi::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; | ||
| 2 | 2 | |
| 3 | 3 | #[cfg(target_os = "linux")] |
| 4 | 4 | use crate::DmaBuf; |
| 5 | -use crate::{CodedFrame, Colour, Error, Flight, Gpu, RateControl, last_frame}; | |
| 5 | +use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; | |
| 6 | 6 | |
| 7 | 7 | /// H.265 encoder settings. |
| 8 | 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -38,6 +38,7 @@ pub struct H265Encoder { | ||
| 38 | 38 | uv: Vec<u8>, |
| 39 | 39 | uv16: Vec<u16>, |
| 40 | 40 | flight: Flight, |
| 41 | + next_id: u64, | |
| 41 | 42 | } |
| 42 | 43 | |
| 43 | 44 | impl H265Encoder { |
| @@ -71,6 +72,7 @@ impl H265Encoder { | ||
| 71 | 72 | uv: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 72 | 73 | uv16: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 73 | 74 | flight: Flight::default(), |
| 75 | + next_id: 0, | |
| 74 | 76 | }) |
| 75 | 77 | } |
| 76 | 78 | |
| @@ -225,17 +227,27 @@ impl H265Encoder { | ||
| 225 | 227 | poc: pic.poc, |
| 226 | 228 | }); |
| 227 | 229 | self.position = if keyframe { 1 } else { self.position + 1 }; |
| 228 | - self.flight.push(keyframe); | |
| 230 | + self.flight.push(Meta { | |
| 231 | + keyframe, | |
| 232 | + recovery: false, | |
| 233 | + id: self.next_id, | |
| 234 | + }); | |
| 235 | + self.next_id += 1; | |
| 229 | 236 | pic |
| 230 | 237 | } |
| 231 | 238 | |
| 232 | 239 | fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { |
| 233 | - let keyframe = self.flight.pop(); | |
| 234 | - let data = if keyframe { | |
| 240 | + let meta = self.flight.pop(); | |
| 241 | + let data = if meta.keyframe { | |
| 235 | 242 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 236 | 243 | } else { |
| 237 | 244 | bytes |
| 238 | 245 | }; |
| 239 | - CodedFrame { data, keyframe } | |
| 246 | + CodedFrame { | |
| 247 | + data, | |
| 248 | + keyframe: meta.keyframe, | |
| 249 | + recovery: meta.recovery, | |
| 250 | + id: meta.id, | |
| 251 | + } | |
| 240 | 252 | } |
| 241 | 253 | } |
crates/ec-vk/src/history.rsadded+127 -0
| @@ -0,0 +1,127 @@ | ||
| 1 | +use vk_video_ffi::DPB_SLOTS; | |
| 2 | + | |
| 3 | +const CHAIN: usize = 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 | +/// Where the next picture goes and what it references. | |
| 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 | +/// 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. | |
| 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 | + let slot = if key || id % ANCHOR_EVERY == 0 { | |
| 79 | + self.anchor_slot(reference_slot) | |
| 80 | + } else { | |
| 81 | + self.chain_slot(reference_slot) | |
| 82 | + }; | |
| 83 | + Step { | |
| 84 | + id, | |
| 85 | + key, | |
| 86 | + recovery: !key && acked.is_some(), | |
| 87 | + slot: slot as u32, | |
| 88 | + reference: reference.map(|(slot, data)| (slot as u32, data)), | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + pub(crate) fn store(&mut self, step: &Step<T>, data: T) { | |
| 93 | + let slot = step.slot as usize; | |
| 94 | + self.slots[slot] = Some(Held { | |
| 95 | + id: step.id, | |
| 96 | + acked: false, | |
| 97 | + data, | |
| 98 | + }); | |
| 99 | + self.last = Some((slot, data)); | |
| 100 | + } | |
| 101 | + | |
| 102 | + fn newest_acked(&self, id: u64) -> Option<(usize, T)> { | |
| 103 | + (0..DPB_SLOTS) | |
| 104 | + .filter_map(|i| self.slots[i].map(|h| (i, h))) | |
| 105 | + .filter(|(_, h)| h.acked && id - h.id < MAX_AGE) | |
| 106 | + .max_by_key(|(_, h)| h.id) | |
| 107 | + .map(|(i, h)| (i, h.data)) | |
| 108 | + } | |
| 109 | + | |
| 110 | + fn anchor_slot(&mut self, reference: Option<usize>) -> usize { | |
| 111 | + let anchors = DPB_SLOTS - CHAIN; | |
| 112 | + loop { | |
| 113 | + let slot = CHAIN + self.next_anchor; | |
| 114 | + self.next_anchor = (self.next_anchor + 1) % anchors; | |
| 115 | + if Some(slot) != reference { | |
| 116 | + return slot; | |
| 117 | + } | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + fn chain_slot(&self, reference: Option<usize>) -> usize { | |
| 122 | + (0..CHAIN) | |
| 123 | + .filter(|&i| Some(i) != reference) | |
| 124 | + .min_by_key(|&i| self.slots[i].map_or(0, |h| h.id + 1)) | |
| 125 | + .unwrap_or(0) | |
| 126 | + } | |
| 127 | +} |
crates/ec-vk/src/lib.rsmodified+33 -9
| @@ -8,6 +8,7 @@ use vk_video_ffi::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref}; | ||
| 8 | 8 | |
| 9 | 9 | mod av1; |
| 10 | 10 | mod h265; |
| 11 | +mod history; | |
| 11 | 12 | |
| 12 | 13 | pub use av1::{Av1Encoder, Av1EncoderConfig}; |
| 13 | 14 | pub use h265::{H265Encoder, H265EncoderConfig}; |
| @@ -54,18 +55,29 @@ pub struct CodedFrame { | ||
| 54 | 55 | pub data: Vec<u8>, |
| 55 | 56 | /// True when the frame is an IDR picture. |
| 56 | 57 | pub keyframe: bool, |
| 58 | + /// True when the frame references an acknowledged older picture instead of the previous one. | |
| 59 | + pub recovery: bool, | |
| 60 | + /// Submission index of the frame, the handle [`Av1Encoder::ack`] takes. | |
| 61 | + pub id: u64, | |
| 62 | +} | |
| 63 | + | |
| 64 | +#[derive(Debug, Clone, Copy, Default)] | |
| 65 | +pub(crate) struct Meta { | |
| 66 | + pub(crate) keyframe: bool, | |
| 67 | + pub(crate) recovery: bool, | |
| 68 | + pub(crate) id: u64, | |
| 57 | 69 | } |
| 58 | 70 | |
| 59 | 71 | #[derive(Default)] |
| 60 | -pub(crate) struct Flight(VecDeque<bool>); | |
| 72 | +pub(crate) struct Flight(VecDeque<Meta>); | |
| 61 | 73 | |
| 62 | 74 | impl Flight { |
| 63 | - pub(crate) fn push(&mut self, keyframe: bool) { | |
| 64 | - self.0.push_back(keyframe); | |
| 75 | + pub(crate) fn push(&mut self, meta: Meta) { | |
| 76 | + self.0.push_back(meta); | |
| 65 | 77 | } |
| 66 | 78 | |
| 67 | - pub(crate) fn pop(&mut self) -> bool { | |
| 68 | - self.0.pop_front().unwrap_or(false) | |
| 79 | + pub(crate) fn pop(&mut self) -> Meta { | |
| 80 | + self.0.pop_front().unwrap_or_default() | |
| 69 | 81 | } |
| 70 | 82 | } |
| 71 | 83 | |
| @@ -93,6 +105,7 @@ pub struct H264Encoder { | ||
| 93 | 105 | reference: Option<H264Ref>, |
| 94 | 106 | uv: Vec<u8>, |
| 95 | 107 | flight: Flight, |
| 108 | + next_id: u64, | |
| 96 | 109 | } |
| 97 | 110 | |
| 98 | 111 | impl H264Encoder { |
| @@ -127,6 +140,7 @@ impl H264Encoder { | ||
| 127 | 140 | reference: None, |
| 128 | 141 | uv: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 129 | 142 | flight: Flight::default(), |
| 143 | + next_id: 0, | |
| 130 | 144 | }) |
| 131 | 145 | } |
| 132 | 146 | |
| @@ -259,17 +273,27 @@ impl H264Encoder { | ||
| 259 | 273 | if keyframe { |
| 260 | 274 | self.idr_pic_id = self.idr_pic_id.wrapping_add(1); |
| 261 | 275 | } |
| 262 | - self.flight.push(keyframe); | |
| 276 | + self.flight.push(Meta { | |
| 277 | + keyframe, | |
| 278 | + recovery: false, | |
| 279 | + id: self.next_id, | |
| 280 | + }); | |
| 281 | + self.next_id += 1; | |
| 263 | 282 | pic |
| 264 | 283 | } |
| 265 | 284 | |
| 266 | 285 | fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { |
| 267 | - let keyframe = self.flight.pop(); | |
| 268 | - let data = if keyframe { | |
| 286 | + let meta = self.flight.pop(); | |
| 287 | + let data = if meta.keyframe { | |
| 269 | 288 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 270 | 289 | } else { |
| 271 | 290 | bytes |
| 272 | 291 | }; |
| 273 | - CodedFrame { data, keyframe } | |
| 292 | + CodedFrame { | |
| 293 | + data, | |
| 294 | + keyframe: meta.keyframe, | |
| 295 | + recovery: meta.recovery, | |
| 296 | + id: meta.id, | |
| 297 | + } | |
| 274 | 298 | } |
| 275 | 299 | } |
shims/vk-video-ffi/src/av1_encode.rsmodified+3 -6
| @@ -41,7 +41,7 @@ pub struct Av1Picture { | ||
| 41 | 41 | pub key: bool, |
| 42 | 42 | /// order_hint of this frame. |
| 43 | 43 | pub order_hint: u8, |
| 44 | - /// DPB slot this picture is written to, 0 or 1. | |
| 44 | + /// DPB slot this picture is written to, below `DPB_SLOTS`. | |
| 45 | 45 | pub slot: u32, |
| 46 | 46 | /// Reference for an INTER frame, None for KEY. |
| 47 | 47 | pub reference: Option<Av1Ref>, |
| @@ -50,7 +50,7 @@ pub struct Av1Picture { | ||
| 50 | 50 | /// A picture held in the DPB. |
| 51 | 51 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 52 | 52 | pub struct Av1Ref { |
| 53 | - /// DPB slot, 0 or 1. | |
| 53 | + /// DPB slot, below `DPB_SLOTS`. | |
| 54 | 54 | pub slot: u32, |
| 55 | 55 | /// order_hint of the reference. |
| 56 | 56 | pub order_hint: u8, |
| @@ -300,10 +300,7 @@ impl Av1EncodeSession { | ||
| 300 | 300 | let extent = self.enc.extent; |
| 301 | 301 | |
| 302 | 302 | let refresh: u8 = if pic.key { 0xff } else { 1 << pic.slot }; |
| 303 | - let dpb_pr = [ | |
| 304 | - picture_resource(self.enc.dpb[0].view, extent), | |
| 305 | - picture_resource(self.enc.dpb[1].view, extent), | |
| 306 | - ]; | |
| 303 | + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); | |
| 307 | 304 | let src_pr = picture_resource(self.enc.src_view(), extent); |
| 308 | 305 | |
| 309 | 306 | let key_of = self.slot_key; |
shims/vk-video-ffi/src/h264_encode.rsmodified+3 -6
| @@ -121,7 +121,7 @@ pub struct H264Picture { | ||
| 121 | 121 | pub frame_num: u32, |
| 122 | 122 | /// PicOrderCnt. |
| 123 | 123 | pub poc: i32, |
| 124 | - /// DPB slot this picture is written to, 0 or 1. | |
| 124 | + /// DPB slot this picture is written to, below `DPB_SLOTS`. | |
| 125 | 125 | pub slot: u32, |
| 126 | 126 | /// Reference for a P picture, None for IDR. |
| 127 | 127 | pub reference: Option<H264Ref>, |
| @@ -130,7 +130,7 @@ pub struct H264Picture { | ||
| 130 | 130 | /// A picture held in the DPB. |
| 131 | 131 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 132 | 132 | pub struct H264Ref { |
| 133 | - /// DPB slot, 0 or 1. | |
| 133 | + /// DPB slot, below `DPB_SLOTS`. | |
| 134 | 134 | pub slot: u32, |
| 135 | 135 | /// frame_num of the reference. |
| 136 | 136 | pub frame_num: u32, |
| @@ -381,10 +381,7 @@ impl H264EncodeSession { | ||
| 381 | 381 | } else { |
| 382 | 382 | vk::video::STD_VIDEO_H264_PICTURE_TYPE_P |
| 383 | 383 | }; |
| 384 | - let dpb_pr = [ | |
| 385 | - picture_resource(self.enc.dpb[0].view, extent), | |
| 386 | - picture_resource(self.enc.dpb[1].view, extent), | |
| 387 | - ]; | |
| 384 | + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); | |
| 388 | 385 | let src_pr = picture_resource(self.enc.src_view(), extent); |
| 389 | 386 | |
| 390 | 387 | let new_std = reference_info(primary, pic.frame_num, pic.poc); |
shims/vk-video-ffi/src/h265_encode.rsmodified+3 -6
| @@ -41,7 +41,7 @@ pub struct H265Picture { | ||
| 41 | 41 | pub idr: bool, |
| 42 | 42 | /// PicOrderCntVal. |
| 43 | 43 | pub poc: i32, |
| 44 | - /// DPB slot this picture is written to, 0 or 1. | |
| 44 | + /// DPB slot this picture is written to, below `DPB_SLOTS`. | |
| 45 | 45 | pub slot: u32, |
| 46 | 46 | /// Reference for a P picture, None for IDR. |
| 47 | 47 | pub reference: Option<H265Ref>, |
| @@ -50,7 +50,7 @@ pub struct H265Picture { | ||
| 50 | 50 | /// A picture held in the DPB. |
| 51 | 51 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 52 | 52 | pub struct H265Ref { |
| 53 | - /// DPB slot, 0 or 1. | |
| 53 | + /// DPB slot, below `DPB_SLOTS`. | |
| 54 | 54 | pub slot: u32, |
| 55 | 55 | /// PicOrderCntVal of the reference, lower than the picture's. |
| 56 | 56 | pub poc: i32, |
| @@ -344,10 +344,7 @@ impl H265EncodeSession { | ||
| 344 | 344 | } else { |
| 345 | 345 | vk::video::STD_VIDEO_H265_PICTURE_TYPE_P |
| 346 | 346 | }; |
| 347 | - let dpb_pr = [ | |
| 348 | - picture_resource(self.enc.dpb[0].view, extent), | |
| 349 | - picture_resource(self.enc.dpb[1].view, extent), | |
| 350 | - ]; | |
| 347 | + let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); | |
| 351 | 348 | let src_pr = picture_resource(self.enc.src_view(), extent); |
| 352 | 349 | |
| 353 | 350 | let new_std = reference_info(primary, pic.poc); |
shims/vk-video-ffi/src/lib.rsmodified+1 -0
| @@ -26,6 +26,7 @@ pub use av1_decode::{ | ||
| 26 | 26 | Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, |
| 27 | 27 | }; |
| 28 | 28 | pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 29 | +pub use session::DPB_SLOTS; | |
| 29 | 30 | #[cfg(target_os = "linux")] |
| 30 | 31 | pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, read_dmabuf}; |
| 31 | 32 | pub use vp9_decode::{ |
shims/vk-video-ffi/src/session.rsmodified+9 -5
| @@ -16,7 +16,8 @@ use crate::{Error, Gpu, RateControl}; | ||
| 16 | 16 | |
| 17 | 17 | pub(crate) const NV12: vk::Format = vk::Format::G8_B8R8_2PLANE_420_UNORM; |
| 18 | 18 | pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; |
| 19 | -pub(crate) const DPB_SLOTS: usize = 2; | |
| 19 | +/// DPB slots of every encode session. | |
| 20 | +pub const DPB_SLOTS: usize = 8; | |
| 20 | 21 | pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0; |
| 21 | 22 | pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1; |
| 22 | 23 | #[cfg(target_os = "linux")] |
| @@ -958,10 +959,13 @@ impl Encoder { | ||
| 958 | 959 | .profiles(std::slice::from_ref(profile)) |
| 959 | 960 | .build(); |
| 960 | 961 | let family = vs.family; |
| 961 | - let dpb = [ | |
| 962 | - vs.image(extent, usage, usage, &[family], &mut list)?, | |
| 963 | - vs.image(extent, usage, usage, &[family], &mut list)?, | |
| 964 | - ]; | |
| 962 | + let mut images = Vec::with_capacity(DPB_SLOTS); | |
| 963 | + for _ in 0..DPB_SLOTS { | |
| 964 | + images.push(vs.image(extent, usage, usage, &[family], &mut list)?); | |
| 965 | + } | |
| 966 | + let Ok(dpb) = <[Image; DPB_SLOTS]>::try_from(images) else { | |
| 967 | + return Err(Error::Invalid("DPB image count".into())); | |
| 968 | + }; | |
| 965 | 969 | let slots = [ |
| 966 | 970 | Slot::new(&mut vs, profile, upload_family, input, extent, frame_bytes, bitstream_len)?, |
| 967 | 971 | Slot::new(&mut vs, profile, upload_family, input, extent, frame_bytes, bitstream_len)?, |