HwDecoder::next_picture -> HwPicture (to_i420/to_i420_16/to_rgb/export_dmabuf per picture) on a reusable exportable output ring (stable fd inodes); old next_* as wrappers; CodecProfile H264ConstrainedBaseline/H264Main/Vp9Profile2
Browse files at this commite12d32fe3eccdd43c66cf72b83a8a47fe5cb36aaParent: dba774a20 changed files +1121 -739
- crates/ec-hw/examples/hw_decode.rs +58 -0
- crates/ec-hw/src/hevc_dpb.rs +1 -0
- crates/ec-hw/src/hw.rs +56 -127
- crates/ec-hw/src/lib.rs +2 -0
- crates/ec-hw/src/output.rs +381 -0
- crates/ec-hw/src/picture.rs +0 -24
- crates/ec-hw/src/vkdec264.rs +33 -76
- crates/ec-hw/src/vkdec265.rs +29 -81
- crates/ec-hw/src/vkdec_av1.rs +34 -129
- crates/ec-hw/src/vkdec_vp9.rs +24 -57
- shims/vk-video-ffi/examples/av1_decode_smoke.rs +2 -1
- shims/vk-video-ffi/examples/h264_decode_smoke.rs +2 -1
- shims/vk-video-ffi/examples/h265_decode_smoke.rs +2 -1
- shims/vk-video-ffi/src/av1_decode.rs +23 -5
- shims/vk-video-ffi/src/dmabuf.rs +46 -155
- shims/vk-video-ffi/src/h264_decode.rs +23 -5
- shims/vk-video-ffi/src/h265_decode.rs +23 -5
- shims/vk-video-ffi/src/lib.rs +23 -6
- shims/vk-video-ffi/src/session.rs +336 -61
- shims/vk-video-ffi/src/vp9_decode.rs +23 -5
crates/ec-hw/examples/hw_decode.rsmodified+58 -0
| @@ -5,7 +5,10 @@ | |||
| 5 | //! cargo run -p ec-hw --example hw_decode -- <scratch dir> | 5 | //! cargo run -p ec-hw --example hw_decode -- <scratch dir> |
| 6 | //! ``` | 6 | //! ``` |
| 7 | 7 | ||
| 8 | +use std::collections::HashSet; | ||
| 8 | use std::fs; | 9 | use std::fs; |
| 10 | +use std::os::fd::AsRawFd; | ||
| 11 | +use std::os::unix::fs::MetadataExt; | ||
| 9 | use std::path::{Path, PathBuf}; | 12 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::Command; | 13 | use std::process::Command; |
| 11 | 14 | ||
| @@ -112,9 +115,64 @@ fn main() { | |||
| 112 | own_av1(&dir); | 115 | own_av1(&dir); |
| 113 | vp9_cases(&dir); | 116 | vp9_cases(&dir); |
| 114 | dmabuf_cases(&dir); | 117 | dmabuf_cases(&dir); |
| 118 | + mixed_cases(&dir); | ||
| 115 | seek_case(&dir); | 119 | seek_case(&dir); |
| 116 | } | 120 | } |
| 117 | 121 | ||
| 122 | +const MIXED_PASSES: usize = 2; | ||
| 123 | + | ||
| 124 | +fn mixed_cases(dir: &Path) { | ||
| 125 | + let h264 = fs::read(dir.join("bf1280.h264")).expect("read bf1280"); | ||
| 126 | + let ten = fs::read(dir.join("ten.hevc")).expect("read ten.hevc"); | ||
| 127 | + let gpu = ec_vk::Gpu::open().expect("Vulkan device"); | ||
| 128 | + mixed_case("bf1280.h264", Codec::H264, &h264_units(&h264), false, &gpu); | ||
| 129 | + mixed_case("ten.hevc", Codec::H265, &nal_units(&ten), true, &gpu); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +fn mixed_case(label: &str, codec: Codec, units: &[&[u8]], ten: bool, gpu: &ec_vk::Gpu) { | ||
| 133 | + let mut reference = if ten { | ||
| 134 | + HwDecoder::open_16(codec) | ||
| 135 | + } else { | ||
| 136 | + HwDecoder::open(codec) | ||
| 137 | + } | ||
| 138 | + .expect("open decoder"); | ||
| 139 | + let expected = decoded_planes(&mut reference, units, ten); | ||
| 140 | + | ||
| 141 | + let mut decoder = HwDecoder::open(codec).expect("open mixed decoder"); | ||
| 142 | + assert_eq!(decoder.backend(), Backend::Vk, "{label}: mixed must decode on Vulkan"); | ||
| 143 | + let mut exports = 0usize; | ||
| 144 | + let mut inodes = HashSet::new(); | ||
| 145 | + for _ in 0..MIXED_PASSES { | ||
| 146 | + decoder.reset(); | ||
| 147 | + for (pts, unit) in units.iter().enumerate() { | ||
| 148 | + decoder.decode(unit, pts as i64).expect("decode mixed"); | ||
| 149 | + } | ||
| 150 | + decoder.flush().expect("flush mixed"); | ||
| 151 | + let mut index = 0usize; | ||
| 152 | + while let Some(picture) = decoder.next_picture().expect("next picture") { | ||
| 153 | + let planes = if ten && index % 2 == 0 { | ||
| 154 | + p010_planes(&picture.to_i420_16().expect("to_i420_16")) | ||
| 155 | + } else { | ||
| 156 | + let dmabuf = picture.export_dmabuf().expect("export DMA-BUF"); | ||
| 157 | + for fd in &dmabuf.fds { | ||
| 158 | + let path = format!("/proc/self/fd/{}", fd.as_raw_fd()); | ||
| 159 | + inodes.insert(fs::metadata(path).expect("fd inode").ino()); | ||
| 160 | + } | ||
| 161 | + exports += 1; | ||
| 162 | + read_back(gpu, dmabuf) | ||
| 163 | + }; | ||
| 164 | + assert!(planes == expected[index], "{label}: picture {index} differs"); | ||
| 165 | + index += 1; | ||
| 166 | + } | ||
| 167 | + assert_eq!(index, expected.len(), "{label}: picture count"); | ||
| 168 | + } | ||
| 169 | + assert!(inodes.len() < exports, "{label}: DMA-BUF slots are not reused"); | ||
| 170 | + println!( | ||
| 171 | + "{label} mixed: {exports} DMA-BUF exports, {} distinct fd inodes, all pictures match", | ||
| 172 | + inodes.len(), | ||
| 173 | + ); | ||
| 174 | +} | ||
| 175 | + | ||
| 118 | fn dmabuf_cases(dir: &Path) { | 176 | fn dmabuf_cases(dir: &Path) { |
| 119 | let h264 = fs::read(dir.join("bf1280.h264")).expect("read bf1280"); | 177 | let h264 = fs::read(dir.join("bf1280.h264")).expect("read bf1280"); |
| 120 | dmabuf_case("bf1280.h264", Codec::H264, &h264_units(&h264), false); | 178 | dmabuf_case("bf1280.h264", Codec::H264, &h264_units(&h264), false); |
crates/ec-hw/src/hevc_dpb.rsmodified+1 -0
| @@ -22,6 +22,7 @@ pub(crate) enum Mark { | |||
| 22 | pub(crate) struct StoredPicture<S> { | 22 | pub(crate) struct StoredPicture<S> { |
| 23 | pub(crate) surface: S, | 23 | pub(crate) surface: S, |
| 24 | pub(crate) poc: i32, | 24 | pub(crate) poc: i32, |
| 25 | + #[cfg_attr(not(target_os = "linux"), allow(dead_code))] | ||
| 25 | pub(crate) timestamp: i64, | 26 | pub(crate) timestamp: i64, |
| 26 | pub(crate) mark: Mark, | 27 | pub(crate) mark: Mark, |
| 27 | /// Still to be handed to the caller. | 28 | /// Still to be handed to the caller. |
crates/ec-hw/src/hw.rsmodified+56 -127
| @@ -10,11 +10,10 @@ use std::cell::RefCell; | |||
| 10 | use std::collections::VecDeque; | 10 | use std::collections::VecDeque; |
| 11 | 11 | ||
| 12 | use crate::error::{Error, Result}; | 12 | use crate::error::{Error, Result}; |
| 13 | -use crate::picture::{Decoded, I420, I420_16, RgbPicture}; | 13 | +use crate::output::HwPicture; |
| 14 | +use crate::picture::{I420, I420_16, RgbPicture}; | ||
| 14 | #[cfg(target_os = "linux")] | 15 | #[cfg(target_os = "linux")] |
| 15 | -use crate::picture::{DmaBufPicture, i420_to_rgb}; | 16 | +use crate::picture::DmaBufPicture; |
| 16 | -#[cfg(target_os = "linux")] | ||
| 17 | -use ec_core::color::{ColorDescription, Matrix, Tags}; | ||
| 18 | use crate::rate::{RateFeedback, hrd_buffer_ms}; | 17 | use crate::rate::{RateFeedback, hrd_buffer_ms}; |
| 19 | use crate::types::{ | 18 | use crate::types::{ |
| 20 | Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, H264Profile, RateControlMode, | 19 | Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, H264Profile, RateControlMode, |
| @@ -51,6 +50,10 @@ pub enum Direction { | |||
| 51 | /// Profile of a [`Capability`], 4:2:0 only. | 50 | /// Profile of a [`Capability`], 4:2:0 only. |
| 52 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 51 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 53 | pub enum CodecProfile { | 52 | pub enum CodecProfile { |
| 53 | + /// H.264 Constrained Baseline, 8-bit. | ||
| 54 | + H264ConstrainedBaseline, | ||
| 55 | + /// H.264 Main, 8-bit. | ||
| 56 | + H264Main, | ||
| 54 | /// H.264 High, 8-bit. | 57 | /// H.264 High, 8-bit. |
| 55 | H264High, | 58 | H264High, |
| 56 | /// H.265 Main, 8-bit. | 59 | /// H.265 Main, 8-bit. |
| @@ -63,6 +66,8 @@ pub enum CodecProfile { | |||
| 63 | Av1Main10, | 66 | Av1Main10, |
| 64 | /// VP9 Profile 0, 8-bit. | 67 | /// VP9 Profile 0, 8-bit. |
| 65 | Vp9Profile0, | 68 | Vp9Profile0, |
| 69 | + /// VP9 Profile 2, 10-bit. | ||
| 70 | + Vp9Profile2, | ||
| 66 | } | 71 | } |
| 67 | 72 | ||
| 68 | /// One codec, profile, direction and backend this machine offers. | 73 | /// One codec, profile, direction and backend this machine offers. |
| @@ -212,10 +217,12 @@ fn va_capabilities(out: &mut Vec<Capability>) { | |||
| 212 | impl CodecProfile { | 217 | impl CodecProfile { |
| 213 | fn codec(self) -> Codec { | 218 | fn codec(self) -> Codec { |
| 214 | match self { | 219 | match self { |
| 215 | - CodecProfile::H264High => Codec::H264, | 220 | + CodecProfile::H264ConstrainedBaseline |
| 221 | + | CodecProfile::H264Main | ||
| 222 | + | CodecProfile::H264High => Codec::H264, | ||
| 216 | CodecProfile::H265Main | CodecProfile::H265Main10 => Codec::H265, | 223 | CodecProfile::H265Main | CodecProfile::H265Main10 => Codec::H265, |
| 217 | CodecProfile::Av1Main | CodecProfile::Av1Main10 => Codec::Av1, | 224 | CodecProfile::Av1Main | CodecProfile::Av1Main10 => Codec::Av1, |
| 218 | - CodecProfile::Vp9Profile0 => Codec::Vp9, | 225 | + CodecProfile::Vp9Profile0 | CodecProfile::Vp9Profile2 => Codec::Vp9, |
| 219 | } | 226 | } |
| 220 | } | 227 | } |
| 221 | } | 228 | } |
| @@ -225,10 +232,13 @@ fn va_profiles(profile: ec_va::Profile, rt_formats: u32) -> Vec<CodecProfile> { | |||
| 225 | let yuv420 = ec_va::va::VA_RT_FORMAT_YUV420; | 232 | let yuv420 = ec_va::va::VA_RT_FORMAT_YUV420; |
| 226 | let yuv420_10 = ec_va::va::VA_RT_FORMAT_YUV420_10; | 233 | let yuv420_10 = ec_va::va::VA_RT_FORMAT_YUV420_10; |
| 227 | match profile { | 234 | match profile { |
| 235 | + ec_va::Profile::H264ConstrainedBaseline => vec![CodecProfile::H264ConstrainedBaseline], | ||
| 236 | + ec_va::Profile::H264Main => vec![CodecProfile::H264Main], | ||
| 228 | ec_va::Profile::H264High => vec![CodecProfile::H264High], | 237 | ec_va::Profile::H264High => vec![CodecProfile::H264High], |
| 229 | ec_va::Profile::HEVCMain => vec![CodecProfile::H265Main], | 238 | ec_va::Profile::HEVCMain => vec![CodecProfile::H265Main], |
| 230 | ec_va::Profile::HEVCMain10 => vec![CodecProfile::H265Main10], | 239 | ec_va::Profile::HEVCMain10 => vec![CodecProfile::H265Main10], |
| 231 | ec_va::Profile::VP9Profile0 => vec![CodecProfile::Vp9Profile0], | 240 | ec_va::Profile::VP9Profile0 => vec![CodecProfile::Vp9Profile0], |
| 241 | + ec_va::Profile::VP9Profile2 if rt_formats & yuv420_10 != 0 => vec![CodecProfile::Vp9Profile2], | ||
| 232 | ec_va::Profile::AV1Profile0 => [ | 242 | ec_va::Profile::AV1Profile0 => [ |
| 233 | (yuv420, CodecProfile::Av1Main), | 243 | (yuv420, CodecProfile::Av1Main), |
| 234 | (yuv420_10, CodecProfile::Av1Main10), | 244 | (yuv420_10, CodecProfile::Av1Main10), |
| @@ -249,6 +259,9 @@ fn vk_profile(profile: ec_vk::CodecProfile) -> CodecProfile { | |||
| 249 | ec_vk::CodecProfile::Av1Main => CodecProfile::Av1Main, | 259 | ec_vk::CodecProfile::Av1Main => CodecProfile::Av1Main, |
| 250 | ec_vk::CodecProfile::Av1Main10 => CodecProfile::Av1Main10, | 260 | ec_vk::CodecProfile::Av1Main10 => CodecProfile::Av1Main10, |
| 251 | ec_vk::CodecProfile::Vp9Profile0 => CodecProfile::Vp9Profile0, | 261 | ec_vk::CodecProfile::Vp9Profile0 => CodecProfile::Vp9Profile0, |
| 262 | + ec_vk::CodecProfile::Vp9Profile2 => CodecProfile::Vp9Profile2, | ||
| 263 | + ec_vk::CodecProfile::H264ConstrainedBaseline => CodecProfile::H264ConstrainedBaseline, | ||
| 264 | + ec_vk::CodecProfile::H264Main => CodecProfile::H264Main, | ||
| 252 | } | 265 | } |
| 253 | } | 266 | } |
| 254 | 267 | ||
| @@ -948,7 +961,7 @@ impl VkDecoder { | |||
| 948 | } | 961 | } |
| 949 | } | 962 | } |
| 950 | 963 | ||
| 951 | - fn next_frame(&mut self) -> Option<(i64, Decoded)> { | 964 | + fn next_frame(&mut self) -> Option<HwPicture> { |
| 952 | match self { | 965 | match self { |
| 953 | VkDecoder::H264(d) => d.next_frame(), | 966 | VkDecoder::H264(d) => d.next_frame(), |
| 954 | VkDecoder::H265(d) => d.next_frame(), | 967 | VkDecoder::H265(d) => d.next_frame(), |
| @@ -993,56 +1006,47 @@ pub struct RgbOutput { | |||
| 993 | pub max_width: Option<u32>, | 1006 | pub max_width: Option<u32>, |
| 994 | } | 1007 | } |
| 995 | 1008 | ||
| 996 | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 997 | -pub(crate) enum Mode { | ||
| 998 | - I420, | ||
| 999 | - I420_16, | ||
| 1000 | - Rgb(RgbOutput), | ||
| 1001 | - #[cfg(target_os = "linux")] | ||
| 1002 | - DmaBuf, | ||
| 1003 | -} | ||
| 1004 | - | ||
| 1005 | /// A hardware decoder on whichever backend opened. | 1009 | /// A hardware decoder on whichever backend opened. |
| 1006 | pub struct HwDecoder { | 1010 | pub struct HwDecoder { |
| 1007 | inner: DecInner, | 1011 | inner: DecInner, |
| 1008 | born_epoch: u64, | 1012 | born_epoch: u64, |
| 1009 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] | 1013 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] |
| 1010 | codec: Codec, | 1014 | codec: Codec, |
| 1011 | - mode: Mode, | 1015 | + rgb: Option<RgbOutput>, |
| 1012 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] | 1016 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] |
| 1013 | replay: Option<Vec<(Vec<u8>, i64)>>, | 1017 | replay: Option<Vec<(Vec<u8>, i64)>>, |
| 1014 | } | 1018 | } |
| 1015 | 1019 | ||
| 1016 | impl HwDecoder { | 1020 | impl HwDecoder { |
| 1017 | - /// Open a decoder for `codec` that hands out I420. Every codec tries Vulkan Video first, then VA-API (Linux). | 1021 | + /// Open a decoder for `codec`. Every codec tries Vulkan Video first, then VA-API (Linux). |
| 1018 | pub fn open(codec: Codec) -> Result<HwDecoder> { | 1022 | pub fn open(codec: Codec) -> Result<HwDecoder> { |
| 1019 | - HwDecoder::open_mode(codec, Mode::I420) | 1023 | + HwDecoder::open_with(codec, None) |
| 1020 | } | 1024 | } |
| 1021 | 1025 | ||
| 1022 | - /// Open a decoder for `codec` that hands out packed RGB, converted on the GPU by Vulkan and on the CPU by VA-API. | 1026 | + /// Open a decoder for `codec` whose [`HwDecoder::next_rgb`] hands out packed RGB, converted on the GPU by Vulkan and on the CPU by VA-API. |
| 1023 | pub fn open_rgb(codec: Codec, output: RgbOutput) -> Result<HwDecoder> { | 1027 | pub fn open_rgb(codec: Codec, output: RgbOutput) -> Result<HwDecoder> { |
| 1024 | - HwDecoder::open_mode(codec, Mode::Rgb(output)) | 1028 | + HwDecoder::open_with(codec, Some(output)) |
| 1025 | } | 1029 | } |
| 1026 | 1030 | ||
| 1027 | - /// Open a decoder for `codec` that hands out 10-bit I420 ([`HwDecoder::next_frame_16`]). 8-bit streams are widened to 10 bits. | 1031 | + /// Same as [`HwDecoder::open`]: every picture can be asked for 10-bit I420 with [`HwPicture::to_i420_16`]. |
| 1028 | pub fn open_16(codec: Codec) -> Result<HwDecoder> { | 1032 | pub fn open_16(codec: Codec) -> Result<HwDecoder> { |
| 1029 | - HwDecoder::open_mode(codec, Mode::I420_16) | 1033 | + HwDecoder::open(codec) |
| 1030 | } | 1034 | } |
| 1031 | 1035 | ||
| 1032 | - /// Open a decoder for `codec` that hands out each picture as DMA-BUF file descriptors ([`HwDecoder::next_dmabuf`]). Linux only. | 1036 | + /// Same as [`HwDecoder::open`]: every picture can be asked for DMA-BUF file descriptors with [`HwPicture::export_dmabuf`]. Linux only. |
| 1033 | #[cfg(target_os = "linux")] | 1037 | #[cfg(target_os = "linux")] |
| 1034 | pub fn open_dmabuf(codec: Codec) -> Result<HwDecoder> { | 1038 | pub fn open_dmabuf(codec: Codec) -> Result<HwDecoder> { |
| 1035 | - HwDecoder::open_mode(codec, Mode::DmaBuf) | 1039 | + HwDecoder::open(codec) |
| 1036 | } | 1040 | } |
| 1037 | 1041 | ||
| 1038 | - fn open_mode(codec: Codec, mode: Mode) -> Result<HwDecoder> { | 1042 | + fn open_with(codec: Codec, rgb: Option<RgbOutput>) -> Result<HwDecoder> { |
| 1039 | let mut reasons = Vec::new(); | 1043 | let mut reasons = Vec::new(); |
| 1040 | let born_epoch = crate::reset::gpu_reset_epoch(); | 1044 | let born_epoch = crate::reset::gpu_reset_epoch(); |
| 1041 | let vk = vk_grace().and_then(|()| match codec { | 1045 | let vk = vk_grace().and_then(|()| match codec { |
| 1042 | - Codec::H264 => VkH264Decoder::new(mode).map(VkDecoder::H264), | 1046 | + Codec::H264 => VkH264Decoder::new().map(VkDecoder::H264), |
| 1043 | - Codec::H265 => VkH265Decoder::new(mode).map(VkDecoder::H265), | 1047 | + Codec::H265 => VkH265Decoder::new().map(VkDecoder::H265), |
| 1044 | - Codec::Av1 => VkAv1Decoder::new(mode).map(VkDecoder::Av1), | 1048 | + Codec::Av1 => VkAv1Decoder::new().map(VkDecoder::Av1), |
| 1045 | - Codec::Vp9 => VkVp9Decoder::new(mode).map(VkDecoder::Vp9), | 1049 | + Codec::Vp9 => VkVp9Decoder::new().map(VkDecoder::Vp9), |
| 1046 | }); | 1050 | }); |
| 1047 | match vk { | 1051 | match vk { |
| 1048 | Ok(inner) => { | 1052 | Ok(inner) => { |
| @@ -1050,7 +1054,7 @@ impl HwDecoder { | |||
| 1050 | inner: DecInner::Vk(inner), | 1054 | inner: DecInner::Vk(inner), |
| 1051 | born_epoch, | 1055 | born_epoch, |
| 1052 | codec, | 1056 | codec, |
| 1053 | - mode, | 1057 | + rgb, |
| 1054 | replay: Some(Vec::new()), | 1058 | replay: Some(Vec::new()), |
| 1055 | }); | 1059 | }); |
| 1056 | } | 1060 | } |
| @@ -1064,7 +1068,7 @@ impl HwDecoder { | |||
| 1064 | inner: DecInner::Va(inner), | 1068 | inner: DecInner::Va(inner), |
| 1065 | born_epoch, | 1069 | born_epoch, |
| 1066 | codec, | 1070 | codec, |
| 1067 | - mode, | 1071 | + rgb, |
| 1068 | replay: None, | 1072 | replay: None, |
| 1069 | }); | 1073 | }); |
| 1070 | } | 1074 | } |
| @@ -1115,93 +1119,59 @@ impl HwDecoder { | |||
| 1115 | result | 1119 | result |
| 1116 | } | 1120 | } |
| 1117 | 1121 | ||
| 1118 | - /// The next picture in display order, as 8-bit I420 with its timestamp. An error on a decoder opened with [`HwDecoder::open_rgb`] or [`HwDecoder::open_16`]. | 1122 | + /// The next picture in display order, held in its decode slot until dropped. Convert it with the `to_*` methods of [`HwPicture`]. |
| 1119 | - pub fn next_frame(&mut self) -> Result<Option<(i64, I420)>> { | 1123 | + pub fn next_picture(&mut self) -> Result<Option<HwPicture>> { |
| 1120 | - if self.mode != Mode::I420 { | 1124 | + self.pull() |
| 1121 | - return Err(Error::config( | ||
| 1122 | - "next_frame on a decoder not opened as 8-bit I420", | ||
| 1123 | - )); | ||
| 1124 | } | 1125 | } |
| 1126 | + | ||
| 1127 | + /// The next picture in display order, as 8-bit I420 with its timestamp. | ||
| 1128 | + pub fn next_frame(&mut self) -> Result<Option<(i64, I420)>> { | ||
| 1125 | match self.pull()? { | 1129 | match self.pull()? { |
| 1126 | None => Ok(None), | 1130 | None => Ok(None), |
| 1127 | - Some((ts, Decoded::I420(frame))) => Ok(Some((ts, frame))), | 1131 | + Some(picture) => Ok(Some((picture.timestamp, picture.to_i420()?))), |
| 1128 | - Some(_) => Err(Error::config("non-I420 picture from an I420 decoder")), | ||
| 1129 | } | 1132 | } |
| 1130 | } | 1133 | } |
| 1131 | 1134 | ||
| 1132 | - /// The next picture in display order, as 10-bit I420 with its timestamp. An error on a decoder not opened with [`HwDecoder::open_16`]. | 1135 | + /// The next picture in display order, as 10-bit I420 with its timestamp. |
| 1133 | pub fn next_frame_16(&mut self) -> Result<Option<(i64, I420_16)>> { | 1136 | pub fn next_frame_16(&mut self) -> Result<Option<(i64, I420_16)>> { |
| 1134 | - if self.mode != Mode::I420_16 { | ||
| 1135 | - return Err(Error::config( | ||
| 1136 | - "next_frame_16 on a decoder not opened with open_16", | ||
| 1137 | - )); | ||
| 1138 | - } | ||
| 1139 | match self.pull()? { | 1137 | match self.pull()? { |
| 1140 | None => Ok(None), | 1138 | None => Ok(None), |
| 1141 | - Some((ts, Decoded::I420_16(frame))) => Ok(Some((ts, frame))), | 1139 | + Some(picture) => Ok(Some((picture.timestamp, picture.to_i420_16()?))), |
| 1142 | - Some(_) => Err(Error::config("non-16-bit picture from a 16-bit decoder")), | ||
| 1143 | } | 1140 | } |
| 1144 | } | 1141 | } |
| 1145 | 1142 | ||
| 1146 | - /// The next picture in display order, as DMA-BUF file descriptors with its timestamp. An error on a decoder not opened with [`HwDecoder::open_dmabuf`]. | 1143 | + /// The next picture in display order, as DMA-BUF file descriptors with its timestamp. Linux only. |
| 1147 | #[cfg(target_os = "linux")] | 1144 | #[cfg(target_os = "linux")] |
| 1148 | pub fn next_dmabuf(&mut self) -> Result<Option<(i64, DmaBufPicture)>> { | 1145 | pub fn next_dmabuf(&mut self) -> Result<Option<(i64, DmaBufPicture)>> { |
| 1149 | - if self.mode != Mode::DmaBuf { | ||
| 1150 | - return Err(Error::config( | ||
| 1151 | - "next_dmabuf on a decoder not opened with open_dmabuf", | ||
| 1152 | - )); | ||
| 1153 | - } | ||
| 1154 | match self.pull()? { | 1146 | match self.pull()? { |
| 1155 | None => Ok(None), | 1147 | None => Ok(None), |
| 1156 | - Some((ts, Decoded::DmaBuf(picture))) => Ok(Some((ts, picture))), | 1148 | + Some(picture) => Ok(Some((picture.timestamp, picture.export_dmabuf()?))), |
| 1157 | - Some(_) => Err(Error::config("non-DMA-BUF picture from a DMA-BUF decoder")), | ||
| 1158 | } | 1149 | } |
| 1159 | } | 1150 | } |
| 1160 | 1151 | ||
| 1161 | /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`]. | 1152 | /// The next picture in display order, as packed RGB with its timestamp. An error on a decoder not opened with [`HwDecoder::open_rgb`]. |
| 1162 | pub fn next_rgb(&mut self) -> Result<Option<(i64, RgbPicture)>> { | 1153 | pub fn next_rgb(&mut self) -> Result<Option<(i64, RgbPicture)>> { |
| 1163 | - if !matches!(self.mode, Mode::Rgb(_)) { | 1154 | + let output = self |
| 1164 | - return Err(Error::config( | 1155 | + .rgb |
| 1165 | - "next_rgb on a decoder not opened with open_rgb", | 1156 | + .ok_or_else(|| Error::config("next_rgb on a decoder not opened with open_rgb"))?; |
| 1166 | - )); | ||
| 1167 | - } | ||
| 1168 | match self.pull()? { | 1157 | match self.pull()? { |
| 1169 | None => Ok(None), | 1158 | None => Ok(None), |
| 1170 | - Some((ts, Decoded::Rgb(picture))) => Ok(Some((ts, picture))), | 1159 | + Some(picture) => Ok(Some((picture.timestamp, picture.to_rgb(&output)?))), |
| 1171 | - Some(_) => Err(Error::config("non-RGB picture from an RGB decoder")), | ||
| 1172 | } | 1160 | } |
| 1173 | } | 1161 | } |
| 1174 | 1162 | ||
| 1175 | - fn pull(&mut self) -> Result<Option<(i64, Decoded)>> { | 1163 | + fn pull(&mut self) -> Result<Option<HwPicture>> { |
| 1176 | match &mut self.inner { | 1164 | match &mut self.inner { |
| 1177 | DecInner::Vk(d) => { | 1165 | DecInner::Vk(d) => { |
| 1178 | vk_session_alive(self.born_epoch)?; | 1166 | vk_session_alive(self.born_epoch)?; |
| 1179 | - let frame = d.next_frame(); | 1167 | + let picture = d.next_frame(); |
| 1180 | - if frame.is_some() { | 1168 | + if picture.is_some() { |
| 1181 | self.replay = None; | 1169 | self.replay = None; |
| 1182 | } | 1170 | } |
| 1183 | - Ok(frame) | 1171 | + Ok(picture) |
| 1184 | } | 1172 | } |
| 1185 | #[cfg(target_os = "linux")] | 1173 | #[cfg(target_os = "linux")] |
| 1186 | - DecInner::Va(d) => match d.next_frame() { | 1174 | + DecInner::Va(d) => Ok(d.next_frame().map(HwPicture::va)), |
| 1187 | - Some(frame) => { | ||
| 1188 | - let decoded = match self.mode { | ||
| 1189 | - Mode::I420 => Decoded::I420(frame.to_i420()?), | ||
| 1190 | - Mode::I420_16 => Decoded::I420_16(frame.to_i420_16()?), | ||
| 1191 | - Mode::Rgb(output) => { | ||
| 1192 | - let i420 = frame.to_i420()?; | ||
| 1193 | - let matrix = frame | ||
| 1194 | - .colour | ||
| 1195 | - .map(|c| c.description.matrix) | ||
| 1196 | - .unwrap_or_else(|| default_matrix(i420.height)); | ||
| 1197 | - Decoded::Rgb(va_rgb(&i420, output, matrix)) | ||
| 1198 | - } | ||
| 1199 | - Mode::DmaBuf => Decoded::DmaBuf(dmabuf_from_prime(frame.export_prime()?)?), | ||
| 1200 | - }; | ||
| 1201 | - Ok(Some((frame.timestamp, decoded))) | ||
| 1202 | - } | ||
| 1203 | - None => Ok(None), | ||
| 1204 | - }, | ||
| 1205 | } | 1175 | } |
| 1206 | } | 1176 | } |
| 1207 | 1177 | ||
| @@ -1225,47 +1195,6 @@ impl HwDecoder { | |||
| 1225 | } | 1195 | } |
| 1226 | } | 1196 | } |
| 1227 | 1197 | ||
| 1228 | -#[cfg(target_os = "linux")] | ||
| 1229 | -fn dmabuf_from_prime(surface: ec_va::PrimeSurface) -> Result<DmaBufPicture> { | ||
| 1230 | - let planes: Vec<(u32, u32, u32)> = surface | ||
| 1231 | - .layers | ||
| 1232 | - .iter() | ||
| 1233 | - .flat_map(|layer| layer.planes.iter().copied()) | ||
| 1234 | - .collect(); | ||
| 1235 | - let [y, uv] = planes.as_slice() else { | ||
| 1236 | - return Err(Error::config("exported surface is not a two-plane 4:2:0 picture")); | ||
| 1237 | - }; | ||
| 1238 | - let plane = |&(object, offset, pitch): &(u32, u32, u32)| DmaBufPlane { | ||
| 1239 | - fd: object as usize, | ||
| 1240 | - offset, | ||
| 1241 | - pitch, | ||
| 1242 | - }; | ||
| 1243 | - let modifier = surface.objects.first().map_or(0, |o| o.modifier); | ||
| 1244 | - Ok(DmaBufPicture { | ||
| 1245 | - fds: surface.objects.into_iter().map(|o| o.fd).collect(), | ||
| 1246 | - fourcc: surface.fourcc, | ||
| 1247 | - modifier, | ||
| 1248 | - width: surface.width, | ||
| 1249 | - height: surface.height, | ||
| 1250 | - planes: [plane(y), plane(uv)], | ||
| 1251 | - }) | ||
| 1252 | -} | ||
| 1253 | - | ||
| 1254 | -#[cfg(target_os = "linux")] | ||
| 1255 | -fn default_matrix(height: u32) -> Matrix { | ||
| 1256 | - ColorDescription::resolve(Tags::default(), Tags::default(), height).matrix | ||
| 1257 | -} | ||
| 1258 | - | ||
| 1259 | -#[cfg(target_os = "linux")] | ||
| 1260 | -fn va_rgb(frame: &I420, output: RgbOutput, matrix: Matrix) -> RgbPicture { | ||
| 1261 | - i420_to_rgb( | ||
| 1262 | - frame, | ||
| 1263 | - output.max_width, | ||
| 1264 | - output.order == RgbOrder::Bgra, | ||
| 1265 | - matrix, | ||
| 1266 | - ) | ||
| 1267 | -} | ||
| 1268 | - | ||
| 1269 | #[cfg(target_os = "linux")] | 1198 | #[cfg(target_os = "linux")] |
| 1270 | fn open_va_decoder(codec: Codec) -> Result<VaDecoder> { | 1199 | fn open_va_decoder(codec: Codec) -> Result<VaDecoder> { |
| 1271 | let display = ec_va::open()?; | 1200 | let display = ec_va::open()?; |
crates/ec-hw/src/lib.rsmodified+2 -0
| @@ -70,6 +70,7 @@ pub(crate) mod hevc_dpb; | |||
| 70 | pub mod hw; | 70 | pub mod hw; |
| 71 | #[cfg(target_os = "linux")] | 71 | #[cfg(target_os = "linux")] |
| 72 | pub mod frame; | 72 | pub mod frame; |
| 73 | +pub(crate) mod output; | ||
| 73 | pub mod picture; | 74 | pub mod picture; |
| 74 | #[cfg(target_os = "linux")] | 75 | #[cfg(target_os = "linux")] |
| 75 | pub mod params; | 76 | pub mod params; |
| @@ -96,6 +97,7 @@ pub use hw::{ | |||
| 96 | }; | 97 | }; |
| 97 | #[cfg(target_os = "linux")] | 98 | #[cfg(target_os = "linux")] |
| 98 | pub use frame::Frame; | 99 | pub use frame::Frame; |
| 100 | +pub use output::HwPicture; | ||
| 99 | pub use picture::{Colour, I420, I420_16, RgbPicture, i420_to_nv12, nv12_to_i420}; | 101 | pub use picture::{Colour, I420, I420_16, RgbPicture, i420_to_nv12, nv12_to_i420}; |
| 100 | #[cfg(target_os = "linux")] | 102 | #[cfg(target_os = "linux")] |
| 101 | pub use picture::DmaBufPicture; | 103 | pub use picture::DmaBufPicture; |
crates/ec-hw/src/output.rsadded+381 -0
| @@ -0,0 +1,381 @@ | |||
| 1 | +//! What a decoder hands out: a picture held in its GPU ring slot (Vulkan) or | ||
| 2 | +//! a VA-API frame, converted on request to I420, 10-bit I420, packed RGB or a | ||
| 3 | +//! DMA-BUF. | ||
| 4 | + | ||
| 5 | +use std::cell::RefCell; | ||
| 6 | +use std::rc::Rc; | ||
| 7 | + | ||
| 8 | +use ec_av1_syntax::{FilmGrainParams, SequenceHeader}; | ||
| 9 | +use ec_core::color::Matrix; | ||
| 10 | +#[cfg(target_os = "linux")] | ||
| 11 | +use ec_core::color::{ColorDescription, Tags}; | ||
| 12 | +use ec_vk::{ | ||
| 13 | + Av1DecodeSession, H264DecodeSession, H265DecodeSession, Vp9DecodeSession, | ||
| 14 | +}; | ||
| 15 | + | ||
| 16 | +use crate::error::{Error, Result}; | ||
| 17 | +use crate::hw::{RgbOrder, RgbOutput}; | ||
| 18 | +use crate::picture::{ | ||
| 19 | + Colour, I420, I420_16, RgbPicture, i420_to_rgb, rgb_size, semi_planar_to_i420, | ||
| 20 | + semi_planar_to_i420_16, yuv_to_rgb, | ||
| 21 | +}; | ||
| 22 | +use crate::vkdec_av1::{synthesize, synthesize16}; | ||
| 23 | +#[cfg(target_os = "linux")] | ||
| 24 | +use crate::frame::Frame; | ||
| 25 | +#[cfg(target_os = "linux")] | ||
| 26 | +use crate::picture::DmaBufPicture; | ||
| 27 | + | ||
| 28 | +/// One decoded picture, as a handle on its ring slot in the decode session. | ||
| 29 | +pub struct HwPicture { | ||
| 30 | + /// Presentation timestamp of the access unit the picture came from. | ||
| 31 | + pub timestamp: i64, | ||
| 32 | + /// Display width in luma samples. | ||
| 33 | + pub width: u32, | ||
| 34 | + /// Display height in luma samples. | ||
| 35 | + pub height: u32, | ||
| 36 | + /// Luma bit depth: 8 or 10. | ||
| 37 | + pub bit_depth: u8, | ||
| 38 | + /// Colour metadata, when the decoder resolved some. | ||
| 39 | + pub colour: Option<Colour>, | ||
| 40 | + source: Source, | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +enum Source { | ||
| 44 | + Vk(VkPicture), | ||
| 45 | + #[cfg(target_os = "linux")] | ||
| 46 | + Va(Frame), | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +impl HwPicture { | ||
| 50 | + pub(crate) fn vk(timestamp: i64, picture: VkPicture) -> HwPicture { | ||
| 51 | + HwPicture { | ||
| 52 | + timestamp, | ||
| 53 | + width: picture.width, | ||
| 54 | + height: picture.height, | ||
| 55 | + bit_depth: picture.bit_depth, | ||
| 56 | + colour: None, | ||
| 57 | + source: Source::Vk(picture), | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + #[cfg(target_os = "linux")] | ||
| 62 | + pub(crate) fn va(frame: Frame) -> HwPicture { | ||
| 63 | + HwPicture { | ||
| 64 | + timestamp: frame.timestamp, | ||
| 65 | + width: frame.display_size.0, | ||
| 66 | + height: frame.display_size.1, | ||
| 67 | + bit_depth: frame.bit_depth, | ||
| 68 | + colour: frame.colour, | ||
| 69 | + source: Source::Va(frame), | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /// 8-bit I420 at display size. A 10-bit picture is truncated to 8 bits. | ||
| 74 | + pub fn to_i420(&self) -> Result<I420> { | ||
| 75 | + match &self.source { | ||
| 76 | + Source::Vk(picture) => picture.i420(), | ||
| 77 | + #[cfg(target_os = "linux")] | ||
| 78 | + Source::Va(frame) => frame.to_i420(), | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /// 10-bit I420 at display size. An 8-bit picture is widened to 10 bits. | ||
| 83 | + pub fn to_i420_16(&self) -> Result<I420_16> { | ||
| 84 | + match &self.source { | ||
| 85 | + Source::Vk(picture) => picture.i420_16(), | ||
| 86 | + #[cfg(target_os = "linux")] | ||
| 87 | + Source::Va(frame) => frame.to_i420_16(), | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /// Packed RGB, scaled and byte-ordered as `output` asks. | ||
| 92 | + pub fn to_rgb(&self, output: &RgbOutput) -> Result<RgbPicture> { | ||
| 93 | + match &self.source { | ||
| 94 | + Source::Vk(picture) => picture.rgb(output), | ||
| 95 | + #[cfg(target_os = "linux")] | ||
| 96 | + Source::Va(frame) => { | ||
| 97 | + let i420 = frame.to_i420()?; | ||
| 98 | + let matrix = frame | ||
| 99 | + .colour | ||
| 100 | + .map(|c| c.description.matrix) | ||
| 101 | + .unwrap_or_else(|| default_matrix(i420.height)); | ||
| 102 | + Ok(i420_to_rgb( | ||
| 103 | + &i420, | ||
| 104 | + output.max_width, | ||
| 105 | + output.order == RgbOrder::Bgra, | ||
| 106 | + matrix, | ||
| 107 | + )) | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /// One DMA-BUF of NV12 or P010 planes at display size. Not for a picture with film grain. | ||
| 113 | + #[cfg(target_os = "linux")] | ||
| 114 | + pub fn export_dmabuf(&self) -> Result<DmaBufPicture> { | ||
| 115 | + match &self.source { | ||
| 116 | + Source::Vk(picture) => picture.export(), | ||
| 117 | + Source::Va(frame) => Ok(dmabuf_from_prime(frame.export_prime()?)?), | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +/// The GPU ring of one decode session: a token per slot, held by every picture still using the slot. | ||
| 123 | +pub(crate) struct Ring { | ||
| 124 | + tokens: Vec<Rc<()>>, | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +impl Ring { | ||
| 128 | + pub(crate) fn new(len: u32) -> Ring { | ||
| 129 | + Ring { | ||
| 130 | + tokens: (0..len).map(|_| Rc::new(())).collect(), | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + /// The first free slot, or a new one from `grow` when every slot is held. | ||
| 135 | + pub(crate) fn take(&mut self, grow: impl FnOnce() -> Result<u32>) -> Result<Slot> { | ||
| 136 | + let index = match self | ||
| 137 | + .tokens | ||
| 138 | + .iter() | ||
| 139 | + .position(|token| Rc::strong_count(token) == 1) | ||
| 140 | + { | ||
| 141 | + Some(index) => index, | ||
| 142 | + None => { | ||
| 143 | + let index = grow()? as usize; | ||
| 144 | + if index != self.tokens.len() { | ||
| 145 | + return Err(Error::config("output ring out of step with the decoder")); | ||
| 146 | + } | ||
| 147 | + self.tokens.push(Rc::new(())); | ||
| 148 | + index | ||
| 149 | + } | ||
| 150 | + }; | ||
| 151 | + Ok(Slot { | ||
| 152 | + index: index as u32, | ||
| 153 | + _held: self.tokens[index].clone(), | ||
| 154 | + }) | ||
| 155 | + } | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +/// A ring slot in use. The slot is free again once every copy of this handle is dropped. | ||
| 159 | +#[derive(Clone)] | ||
| 160 | +pub(crate) struct Slot { | ||
| 161 | + index: u32, | ||
| 162 | + _held: Rc<()>, | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +impl Slot { | ||
| 166 | + pub(crate) fn index(&self) -> u32 { | ||
| 167 | + self.index | ||
| 168 | + } | ||
| 169 | +} | ||
| 170 | + | ||
| 171 | +/// The decode session's output images, shared with every picture decoded in it. | ||
| 172 | +#[derive(Clone)] | ||
| 173 | +pub(crate) enum Surfaces { | ||
| 174 | + H264(Rc<RefCell<H264DecodeSession>>), | ||
| 175 | + H265(Rc<RefCell<H265DecodeSession>>), | ||
| 176 | + Av1(Rc<RefCell<Av1DecodeSession>>), | ||
| 177 | + Vp9(Rc<RefCell<Vp9DecodeSession>>), | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +impl Surfaces { | ||
| 181 | + fn nv12(&self, slot: u32, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>)> { | ||
| 182 | + Ok(match self { | ||
| 183 | + Surfaces::H264(s) => s.borrow_mut().read_output(slot, width, height)?, | ||
| 184 | + Surfaces::H265(s) => s.borrow_mut().read_output(slot, width, height)?, | ||
| 185 | + Surfaces::Av1(s) => s.borrow_mut().read_output(slot, width, height)?, | ||
| 186 | + Surfaces::Vp9(s) => s.borrow_mut().read_output(slot, width, height)?, | ||
| 187 | + }) | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + fn rgb( | ||
| 191 | + &self, | ||
| 192 | + slot: u32, | ||
| 193 | + crop: (u32, u32, u32, u32), | ||
| 194 | + out: (u32, u32), | ||
| 195 | + bgra: bool, | ||
| 196 | + coeffs: [i32; 5], | ||
| 197 | + ) -> Result<Vec<u8>> { | ||
| 198 | + Ok(match self { | ||
| 199 | + Surfaces::H264(s) => s | ||
| 200 | + .borrow_mut() | ||
| 201 | + .read_output_rgb(slot, crop, out, bgra, coeffs)?, | ||
| 202 | + Surfaces::H265(s) => s | ||
| 203 | + .borrow_mut() | ||
| 204 | + .read_output_rgb(slot, crop, out, bgra, coeffs)?, | ||
| 205 | + Surfaces::Av1(s) => s | ||
| 206 | + .borrow_mut() | ||
| 207 | + .read_output_rgb(slot, crop, out, bgra, coeffs)?, | ||
| 208 | + Surfaces::Vp9(s) => s | ||
| 209 | + .borrow_mut() | ||
| 210 | + .read_output_rgb(slot, crop, out, bgra, coeffs)?, | ||
| 211 | + }) | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + #[cfg(target_os = "linux")] | ||
| 215 | + fn export(&self, slot: u32, width: u32, height: u32) -> Result<ec_vk::DmaBufExport> { | ||
| 216 | + Ok(match self { | ||
| 217 | + Surfaces::H264(s) => s.borrow_mut().read_output_dmabuf(slot, width, height)?, | ||
| 218 | + Surfaces::H265(s) => s.borrow_mut().read_output_dmabuf(slot, width, height)?, | ||
| 219 | + Surfaces::Av1(s) => s.borrow_mut().read_output_dmabuf(slot, width, height)?, | ||
| 220 | + Surfaces::Vp9(s) => s.borrow_mut().read_output_dmabuf(slot, width, height)?, | ||
| 221 | + }) | ||
| 222 | + } | ||
| 223 | +} | ||
| 224 | + | ||
| 225 | +/// A picture in a ring slot: its display size, bit depth, matrix and, for AV1, the film grain to apply when it is shown. | ||
| 226 | +#[derive(Clone)] | ||
| 227 | +pub(crate) struct VkPicture { | ||
| 228 | + surfaces: Surfaces, | ||
| 229 | + slot: Slot, | ||
| 230 | + width: u32, | ||
| 231 | + height: u32, | ||
| 232 | + bit_depth: u8, | ||
| 233 | + matrix: Matrix, | ||
| 234 | + grain: Option<Grain>, | ||
| 235 | +} | ||
| 236 | + | ||
| 237 | +#[derive(Clone)] | ||
| 238 | +struct Grain { | ||
| 239 | + params: FilmGrainParams, | ||
| 240 | + seq: SequenceHeader, | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +impl VkPicture { | ||
| 244 | + pub(crate) fn new( | ||
| 245 | + surfaces: Surfaces, | ||
| 246 | + slot: Slot, | ||
| 247 | + (width, height): (u32, u32), | ||
| 248 | + bit_depth: u8, | ||
| 249 | + matrix: Matrix, | ||
| 250 | + ) -> VkPicture { | ||
| 251 | + VkPicture { | ||
| 252 | + surfaces, | ||
| 253 | + slot, | ||
| 254 | + width, | ||
| 255 | + height, | ||
| 256 | + bit_depth, | ||
| 257 | + matrix, | ||
| 258 | + grain: None, | ||
| 259 | + } | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | + /// This picture as it is shown with `grain`: unchanged when the frame applies none. | ||
| 263 | + pub(crate) fn with_grain(&self, params: &FilmGrainParams, seq: &SequenceHeader) -> VkPicture { | ||
| 264 | + let mut shown = self.clone(); | ||
| 265 | + if params.apply_grain { | ||
| 266 | + shown.grain = Some(Grain { | ||
| 267 | + params: params.clone(), | ||
| 268 | + seq: seq.clone(), | ||
| 269 | + }); | ||
| 270 | + } | ||
| 271 | + shown | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + fn read(&self) -> Result<(Vec<u8>, Vec<u8>)> { | ||
| 275 | + self.surfaces.nv12(self.slot.index, self.width, self.height) | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + fn i420(&self) -> Result<I420> { | ||
| 279 | + let (y, uv) = self.read()?; | ||
| 280 | + let frame = semi_planar_to_i420( | ||
| 281 | + self.bit_depth, | ||
| 282 | + &y, | ||
| 283 | + &uv, | ||
| 284 | + self.width as usize, | ||
| 285 | + (0, 0), | ||
| 286 | + (self.width, self.height), | ||
| 287 | + )?; | ||
| 288 | + Ok(match &self.grain { | ||
| 289 | + Some(g) => synthesize(frame, &g.params, &g.seq), | ||
| 290 | + None => frame, | ||
| 291 | + }) | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + fn i420_16(&self) -> Result<I420_16> { | ||
| 295 | + let (y, uv) = self.read()?; | ||
| 296 | + let frame = semi_planar_to_i420_16( | ||
| 297 | + self.bit_depth, | ||
| 298 | + &y, | ||
| 299 | + &uv, | ||
| 300 | + self.width as usize, | ||
| 301 | + (0, 0), | ||
| 302 | + (self.width, self.height), | ||
| 303 | + )?; | ||
| 304 | + Ok(match &self.grain { | ||
| 305 | + Some(g) => synthesize16(frame, &g.params, &g.seq), | ||
| 306 | + None => frame, | ||
| 307 | + }) | ||
| 308 | + } | ||
| 309 | + | ||
| 310 | + fn rgb(&self, output: &RgbOutput) -> Result<RgbPicture> { | ||
| 311 | + let bgra = output.order == RgbOrder::Bgra; | ||
| 312 | + if self.grain.is_some() { | ||
| 313 | + if self.bit_depth != 8 { | ||
| 314 | + return Err(Error::unsupported( | ||
| 315 | + "Vulkan AV1 film grain", | ||
| 316 | + "RGB of 10-bit output", | ||
| 317 | + )); | ||
| 318 | + } | ||
| 319 | + return Ok(i420_to_rgb( | ||
| 320 | + &self.i420()?, | ||
| 321 | + output.max_width, | ||
| 322 | + bgra, | ||
| 323 | + self.matrix, | ||
| 324 | + )); | ||
| 325 | + } | ||
| 326 | + let (w, h) = rgb_size(self.width, self.height, output.max_width); | ||
| 327 | + let data = self.surfaces.rgb( | ||
| 328 | + self.slot.index, | ||
| 329 | + (0, 0, self.width, self.height), | ||
| 330 | + (w, h), | ||
| 331 | + bgra, | ||
| 332 | + yuv_to_rgb(self.matrix).words(), | ||
| 333 | + )?; | ||
| 334 | + Ok(RgbPicture::new(data, w, h)) | ||
| 335 | + } | ||
| 336 | + | ||
| 337 | + #[cfg(target_os = "linux")] | ||
| 338 | + fn export(&self) -> Result<DmaBufPicture> { | ||
| 339 | + if self.grain.is_some() { | ||
| 340 | + return Err(Error::unsupported( | ||
| 341 | + "Vulkan DMA-BUF export", | ||
| 342 | + "a picture with film grain", | ||
| 343 | + )); | ||
| 344 | + } | ||
| 345 | + Ok(self | ||
| 346 | + .surfaces | ||
| 347 | + .export(self.slot.index, self.width, self.height)? | ||
| 348 | + .into()) | ||
| 349 | + } | ||
| 350 | +} | ||
| 351 | + | ||
| 352 | +#[cfg(target_os = "linux")] | ||
| 353 | +fn default_matrix(height: u32) -> Matrix { | ||
| 354 | + ColorDescription::resolve(Tags::default(), Tags::default(), height).matrix | ||
| 355 | +} | ||
| 356 | + | ||
| 357 | +#[cfg(target_os = "linux")] | ||
| 358 | +fn dmabuf_from_prime(surface: ec_va::PrimeSurface) -> Result<DmaBufPicture> { | ||
| 359 | + let planes: Vec<(u32, u32, u32)> = surface | ||
| 360 | + .layers | ||
| 361 | + .iter() | ||
| 362 | + .flat_map(|layer| layer.planes.iter().copied()) | ||
| 363 | + .collect(); | ||
| 364 | + let [y, uv] = planes.as_slice() else { | ||
| 365 | + return Err(Error::config("exported surface is not a two-plane 4:2:0 picture")); | ||
| 366 | + }; | ||
| 367 | + let plane = |&(object, offset, pitch): &(u32, u32, u32)| ec_vk::DmaBufPlane { | ||
| 368 | + fd: object as usize, | ||
| 369 | + offset, | ||
| 370 | + pitch, | ||
| 371 | + }; | ||
| 372 | + let modifier = surface.objects.first().map_or(0, |o| o.modifier); | ||
| 373 | + Ok(DmaBufPicture { | ||
| 374 | + fds: surface.objects.into_iter().map(|o| o.fd).collect(), | ||
| 375 | + fourcc: surface.fourcc, | ||
| 376 | + modifier, | ||
| 377 | + width: surface.width, | ||
| 378 | + height: surface.height, | ||
| 379 | + planes: [plane(y), plane(uv)], | ||
| 380 | + }) | ||
| 381 | +} | ||
crates/ec-hw/src/picture.rsmodified+0 -24
| @@ -114,30 +114,6 @@ impl DmaBufPicture { | |||
| 114 | } | 114 | } |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | -/// One decoded picture in the decoder's output form. | ||
| 118 | -#[derive(Debug)] | ||
| 119 | -pub(crate) enum Decoded { | ||
| 120 | - I420(I420), | ||
| 121 | - I420_16(I420_16), | ||
| 122 | - Rgb(RgbPicture), | ||
| 123 | - #[cfg(target_os = "linux")] | ||
| 124 | - DmaBuf(DmaBufPicture), | ||
| 125 | -} | ||
| 126 | - | ||
| 127 | -impl Decoded { | ||
| 128 | - pub(crate) fn try_clone(&self) -> Result<Decoded> { | ||
| 129 | - Ok(match self { | ||
| 130 | - Decoded::I420(frame) => Decoded::I420(frame.clone()), | ||
| 131 | - Decoded::I420_16(frame) => Decoded::I420_16(frame.clone()), | ||
| 132 | - Decoded::Rgb(picture) => Decoded::Rgb(picture.clone()), | ||
| 133 | - #[cfg(target_os = "linux")] | ||
| 134 | - Decoded::DmaBuf(picture) => Decoded::DmaBuf(picture.try_clone().map_err(|e| { | ||
| 135 | - Error::config(format!("duplicating a DMA-BUF picture: {e}")) | ||
| 136 | - })?), | ||
| 137 | - }) | ||
| 138 | - } | ||
| 139 | -} | ||
| 140 | - | ||
| 141 | /// The YUV to RGB coefficients for a matrix; BT.2020 is taken as BT.709, the nearest integer set here. | 117 | /// The YUV to RGB coefficients for a matrix; BT.2020 is taken as BT.709, the nearest integer set here. |
| 142 | pub(crate) fn yuv_to_rgb(matrix: Matrix) -> ec_dsp::rgb::YuvToRgb { | 118 | pub(crate) fn yuv_to_rgb(matrix: Matrix) -> ec_dsp::rgb::YuvToRgb { |
| 143 | match matrix { | 119 | match matrix { |
crates/ec-hw/src/vkdec264.rsmodified+33 -76
| @@ -1,6 +1,8 @@ | |||
| 1 | //! H.264 decoding over Vulkan Video: the shared DPB over DPB slot indices. | 1 | //! H.264 decoding over Vulkan Video: the shared DPB over DPB slot indices. |
| 2 | 2 | ||
| 3 | +use std::cell::RefCell; | ||
| 3 | use std::collections::{HashMap, VecDeque}; | 4 | use std::collections::{HashMap, VecDeque}; |
| 5 | +use std::rc::Rc; | ||
| 4 | 6 | ||
| 5 | use ec_h264_syntax::sps::{ZIGZAG_4X4, ZIGZAG_8X8}; | 7 | use ec_h264_syntax::sps::{ZIGZAG_4X4, ZIGZAG_8X8}; |
| 6 | use ec_h264_syntax::{ | 8 | use ec_h264_syntax::{ |
| @@ -15,10 +17,7 @@ use ec_vk::{ | |||
| 15 | use crate::dpb264::{self, Mark, PicInfo, Poc}; | 17 | use crate::dpb264::{self, Mark, PicInfo, Poc}; |
| 16 | use crate::error::{Error, Result}; | 18 | use crate::error::{Error, Result}; |
| 17 | use crate::h264_common::{peek_pps_id, vui_colour_tags}; | 19 | use crate::h264_common::{peek_pps_id, vui_colour_tags}; |
| 18 | -use crate::hw::{Mode, RgbOrder}; | 20 | +use crate::output::{HwPicture, Ring, Surfaces, VkPicture}; |
| 19 | -use crate::picture::{ | ||
| 20 | - Decoded, I420, RgbPicture, nv12_to_i420, rgb_size, semi_planar_to_i420_16, yuv_to_rgb, | ||
| 21 | -}; | ||
| 22 | use ec_core::color::{ColorDescription, Tags}; | 21 | use ec_core::color::{ColorDescription, Tags}; |
| 23 | 22 | ||
| 24 | const MAX_SLOTS: u32 = 17; | 23 | const MAX_SLOTS: u32 = 17; |
| @@ -27,7 +26,8 @@ type Dpb = dpb264::Dpb<u32>; | |||
| 27 | type Picture = dpb264::Picture<u32>; | 26 | type Picture = dpb264::Picture<u32>; |
| 28 | 27 | ||
| 29 | struct Session { | 28 | struct Session { |
| 30 | - h264: H264DecodeSession, | 29 | + h264: Rc<RefCell<H264DecodeSession>>, |
| 30 | + ring: Ring, | ||
| 31 | max_width: u32, | 31 | max_width: u32, |
| 32 | max_height: u32, | 32 | max_height: u32, |
| 33 | slots: u32, | 33 | slots: u32, |
| @@ -48,7 +48,7 @@ struct Current { | |||
| 48 | 48 | ||
| 49 | struct Side { | 49 | struct Side { |
| 50 | fields: [i32; 2], | 50 | fields: [i32; 2], |
| 51 | - output: Option<(i64, Decoded)>, | 51 | + output: Option<HwPicture>, |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | /// H.264 Vulkan Video decoder: DPB bookkeeping in Rust, pictures decoded into slots. | 54 | /// H.264 Vulkan Video decoder: DPB bookkeeping in Rust, pictures decoded into slots. |
| @@ -61,13 +61,12 @@ pub(crate) struct VkH264Decoder { | |||
| 61 | dpb: Dpb, | 61 | dpb: Dpb, |
| 62 | pics: HashMap<i32, Side>, | 62 | pics: HashMap<i32, Side>, |
| 63 | current: Option<Current>, | 63 | current: Option<Current>, |
| 64 | - ready: VecDeque<(i64, Decoded)>, | 64 | + ready: VecDeque<HwPicture>, |
| 65 | rbsp: Vec<u8>, | 65 | rbsp: Vec<u8>, |
| 66 | - mode: Mode, | ||
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | impl VkH264Decoder { | 68 | impl VkH264Decoder { |
| 70 | - pub(crate) fn new(mode: Mode) -> Result<Self> { | 69 | + pub(crate) fn new() -> Result<Self> { |
| 71 | let gpu = crate::hw::vk_gpu() | 70 | let gpu = crate::hw::vk_gpu() |
| 72 | .ok_or_else(|| Error::unsupported("Vulkan Video H.264 decode", "no Vulkan device"))?; | 71 | .ok_or_else(|| Error::unsupported("Vulkan Video H.264 decode", "no Vulkan device"))?; |
| 73 | Ok(VkH264Decoder { | 72 | Ok(VkH264Decoder { |
| @@ -81,7 +80,6 @@ impl VkH264Decoder { | |||
| 81 | current: None, | 80 | current: None, |
| 82 | ready: VecDeque::new(), | 81 | ready: VecDeque::new(), |
| 83 | rbsp: Vec::new(), | 82 | rbsp: Vec::new(), |
| 84 | - mode, | ||
| 85 | }) | 83 | }) |
| 86 | } | 84 | } |
| 87 | 85 | ||
| @@ -124,7 +122,7 @@ impl VkH264Decoder { | |||
| 124 | self.finish_picture() | 122 | self.finish_picture() |
| 125 | } | 123 | } |
| 126 | 124 | ||
| 127 | - pub(crate) fn next_frame(&mut self) -> Option<(i64, Decoded)> { | 125 | + pub(crate) fn next_frame(&mut self) -> Option<HwPicture> { |
| 128 | self.ready.pop_front() | 126 | self.ready.pop_front() |
| 129 | } | 127 | } |
| 130 | 128 | ||
| @@ -253,8 +251,10 @@ impl VkH264Decoder { | |||
| 253 | profile_idc: sps.profile_idc, | 251 | profile_idc: sps.profile_idc, |
| 254 | }, | 252 | }, |
| 255 | )?; | 253 | )?; |
| 254 | + let ring = Ring::new(h264.ring_len()); | ||
| 256 | self.session = Some(Session { | 255 | self.session = Some(Session { |
| 257 | - h264, | 256 | + h264: Rc::new(RefCell::new(h264)), |
| 257 | + ring, | ||
| 258 | max_width, | 258 | max_width, |
| 259 | max_height, | 259 | max_height, |
| 260 | slots, | 260 | slots, |
| @@ -270,7 +270,7 @@ impl VkH264Decoder { | |||
| 270 | .session | 270 | .session |
| 271 | .as_mut() | 271 | .as_mut() |
| 272 | .ok_or_else(|| Error::config("H.264 parameters before any SPS"))?; | 272 | .ok_or_else(|| Error::config("H.264 parameters before any SPS"))?; |
| 273 | - session.h264.set_parameters(&sps, &pps)?; | 273 | + session.h264.borrow_mut().set_parameters(&sps, &pps)?; |
| 274 | self.params_dirty = false; | 274 | self.params_dirty = false; |
| 275 | Ok(()) | 275 | Ok(()) |
| 276 | } | 276 | } |
| @@ -323,7 +323,8 @@ impl VkH264Decoder { | |||
| 323 | .session | 323 | .session |
| 324 | .as_mut() | 324 | .as_mut() |
| 325 | .ok_or_else(|| Error::config("H.264 picture before any SPS"))?; | 325 | .ok_or_else(|| Error::config("H.264 picture before any SPS"))?; |
| 326 | - session.h264.decode(&H264DecodePicture { | 326 | + let slot = session.ring.take(|| Ok(session.h264.borrow_mut().add_ring_slot()?))?; |
| 327 | + session.h264.borrow_mut().decode(&H264DecodePicture { | ||
| 327 | sps_id: current.sps_id, | 328 | sps_id: current.sps_id, |
| 328 | pps_id: current.pps_id, | 329 | pps_id: current.pps_id, |
| 329 | idr: current.info.is_idr, | 330 | idr: current.info.is_idr, |
| @@ -335,53 +336,23 @@ impl VkH264Decoder { | |||
| 335 | refs: &refs, | 336 | refs: &refs, |
| 336 | slices: &slices, | 337 | slices: &slices, |
| 337 | })?; | 338 | })?; |
| 338 | - let out = match self.mode { | ||
| 339 | - Mode::I420 => { | ||
| 340 | - let (y, uv) = session | ||
| 341 | - .h264 | ||
| 342 | - .read_output(sps.coded_width, sps.coded_height)?; | ||
| 343 | - Decoded::I420(crop(&sps, &y, &uv)) | ||
| 344 | - } | ||
| 345 | - Mode::I420_16 => { | ||
| 346 | - let (y, uv) = session | ||
| 347 | - .h264 | ||
| 348 | - .read_output(sps.coded_width, sps.coded_height)?; | ||
| 349 | - let (left, _, top, _) = sps.crop; | ||
| 350 | - Decoded::I420_16(semi_planar_to_i420_16( | ||
| 351 | - 8, | ||
| 352 | - &y, | ||
| 353 | - &uv, | ||
| 354 | - sps.coded_width as usize, | ||
| 355 | - (left as usize, top as usize), | ||
| 356 | - (sps.width, sps.height), | ||
| 357 | - )?) | ||
| 358 | - } | ||
| 359 | - #[cfg(target_os = "linux")] | ||
| 360 | - Mode::DmaBuf => { | ||
| 361 | - let (left, _, top, _) = sps.crop; | ||
| 362 | - let export = session.h264.read_output_dmabuf(( | ||
| 363 | - left, | ||
| 364 | - top, | ||
| 365 | - sps.width, | ||
| 366 | - sps.height, | ||
| 367 | - ))?; | ||
| 368 | - Decoded::DmaBuf(export.into()) | ||
| 369 | - } | ||
| 370 | - Mode::Rgb(rgb) => { | ||
| 371 | let (left, _, top, _) = sps.crop; | 339 | let (left, _, top, _) = sps.crop; |
| 340 | + session | ||
| 341 | + .h264 | ||
| 342 | + .borrow_mut() | ||
| 343 | + .store_output(slot.index(), (left, top, sps.width, sps.height))?; | ||
| 372 | let matrix = | 344 | let matrix = |
| 373 | - ColorDescription::resolve(Tags::default(), vui_colour_tags(&sps), sps.height) | 345 | + ColorDescription::resolve(Tags::default(), vui_colour_tags(&sps), sps.height).matrix; |
| 374 | - .matrix; | 346 | + let out = HwPicture::vk( |
| 375 | - let (w, h) = rgb_size(sps.width, sps.height, rgb.max_width); | 347 | + current.timestamp, |
| 376 | - let data = session.h264.read_output_rgb( | 348 | + VkPicture::new( |
| 377 | - (left, top, sps.width, sps.height), | 349 | + Surfaces::H264(session.h264.clone()), |
| 378 | - (w, h), | 350 | + slot, |
| 379 | - rgb.order == RgbOrder::Bgra, | 351 | + (sps.width, sps.height), |
| 380 | - yuv_to_rgb(matrix).words(), | 352 | + 8, |
| 381 | - )?; | 353 | + matrix, |
| 382 | - Decoded::Rgb(RgbPicture::new(data, w, h)) | 354 | + ), |
| 383 | - } | 355 | + ); |
| 384 | - }; | ||
| 385 | 356 | ||
| 386 | let picture = Picture { | 357 | let picture = Picture { |
| 387 | id: current.id, | 358 | id: current.id, |
| @@ -403,7 +374,7 @@ impl VkH264Decoder { | |||
| 403 | current.id, | 374 | current.id, |
| 404 | Side { | 375 | Side { |
| 405 | fields, | 376 | fields, |
| 406 | - output: Some((current.timestamp, out)), | 377 | + output: Some(out), |
| 407 | }, | 378 | }, |
| 408 | ); | 379 | ); |
| 409 | self.dpb | 380 | self.dpb |
| @@ -415,8 +386,8 @@ impl VkH264Decoder { | |||
| 415 | fn bump(&mut self, flush: bool) { | 386 | fn bump(&mut self, flush: bool) { |
| 416 | while let Some(idx) = self.dpb.next_output(flush) { | 387 | while let Some(idx) = self.dpb.next_output(flush) { |
| 417 | let id = self.dpb.frames[idx].id; | 388 | let id = self.dpb.frames[idx].id; |
| 418 | - if let Some((ts, frame)) = self.pics.get_mut(&id).and_then(|s| s.output.take()) { | 389 | + if let Some(frame) = self.pics.get_mut(&id).and_then(|s| s.output.take()) { |
| 419 | - self.ready.push_back((ts, frame)); | 390 | + self.ready.push_back(frame); |
| 420 | } | 391 | } |
| 421 | self.dpb.released(idx); | 392 | self.dpb.released(idx); |
| 422 | } | 393 | } |
| @@ -429,20 +400,6 @@ fn field_pocs(poc: i32, delta_bottom: i32) -> [i32; 2] { | |||
| 429 | [poc - delta_bottom.min(0), poc + delta_bottom.max(0)] | 400 | [poc - delta_bottom.min(0), poc + delta_bottom.max(0)] |
| 430 | } | 401 | } |
| 431 | 402 | ||
| 432 | -fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> I420 { | ||
| 433 | - let (left, _, top, _) = sps.crop; | ||
| 434 | - let (left, top) = (left as usize, top as usize); | ||
| 435 | - let pitch = sps.coded_width as usize; | ||
| 436 | - nv12_to_i420( | ||
| 437 | - &y[top * pitch + left..], | ||
| 438 | - pitch, | ||
| 439 | - &uv[(top / 2) * pitch + left..], | ||
| 440 | - pitch, | ||
| 441 | - sps.width as usize, | ||
| 442 | - sps.height as usize, | ||
| 443 | - ) | ||
| 444 | -} | ||
| 445 | - | ||
| 446 | fn lists(l: &ec_h264_syntax::ScalingLists) -> ScalingLists { | 403 | fn lists(l: &ec_h264_syntax::ScalingLists) -> ScalingLists { |
| 447 | let mut out = ScalingLists { | 404 | let mut out = ScalingLists { |
| 448 | l4x4: [[0; 16]; 6], | 405 | l4x4: [[0; 16]; 6], |
crates/ec-hw/src/vkdec265.rsmodified+29 -81
| @@ -1,6 +1,8 @@ | |||
| 1 | //! H.265 decoding over Vulkan Video: the shared HEVC DPB over DPB slot indices. | 1 | //! H.265 decoding over Vulkan Video: the shared HEVC DPB over DPB slot indices. |
| 2 | 2 | ||
| 3 | +use std::cell::RefCell; | ||
| 3 | use std::collections::{HashMap, VecDeque}; | 4 | use std::collections::{HashMap, VecDeque}; |
| 5 | +use std::rc::Rc; | ||
| 4 | 6 | ||
| 5 | use ec_h265_syntax::{ | 7 | use ec_h265_syntax::{ |
| 6 | AnnexBNal, NalUnitType, Pps, ScalingList, ShortTermRefPicSet, SliceHeader, Sps, Vps, | 8 | AnnexBNal, NalUnitType, Pps, ScalingList, ShortTermRefPicSet, SliceHeader, Sps, Vps, |
| @@ -18,16 +20,13 @@ use crate::hevc_dpb::{ | |||
| 18 | }; | 20 | }; |
| 19 | use ec_core::color::{ColorDescription, Tags}; | 21 | use ec_core::color::{ColorDescription, Tags}; |
| 20 | 22 | ||
| 21 | -use crate::hw::{Mode, RgbOrder}; | 23 | +use crate::output::{HwPicture, Ring, Surfaces, VkPicture}; |
| 22 | -use crate::picture::{ | ||
| 23 | - Decoded, I420, I420_16, RgbPicture, rgb_size, semi_planar_to_i420, semi_planar_to_i420_16, | ||
| 24 | - yuv_to_rgb, | ||
| 25 | -}; | ||
| 26 | 24 | ||
| 27 | const MAX_SLOTS: u32 = 17; | 25 | const MAX_SLOTS: u32 = 17; |
| 28 | 26 | ||
| 29 | struct Session { | 27 | struct Session { |
| 30 | - h265: H265DecodeSession, | 28 | + h265: Rc<RefCell<H265DecodeSession>>, |
| 29 | + ring: Ring, | ||
| 31 | bit_depth: u8, | 30 | bit_depth: u8, |
| 32 | max_width: u32, | 31 | max_width: u32, |
| 33 | max_height: u32, | 32 | max_height: u32, |
| @@ -60,15 +59,14 @@ pub(crate) struct VkH265Decoder { | |||
| 60 | sps_map: Vec<Option<Sps>>, | 59 | sps_map: Vec<Option<Sps>>, |
| 61 | pps_map: Vec<Option<Pps>>, | 60 | pps_map: Vec<Option<Pps>>, |
| 62 | dpb: Dpb<u32>, | 61 | dpb: Dpb<u32>, |
| 63 | - outputs: HashMap<u32, Decoded>, | 62 | + outputs: HashMap<u32, HwPicture>, |
| 64 | current: Option<Current>, | 63 | current: Option<Current>, |
| 65 | - ready: VecDeque<(i64, Decoded)>, | 64 | + ready: VecDeque<HwPicture>, |
| 66 | no_rasl_output: bool, | 65 | no_rasl_output: bool, |
| 67 | - mode: Mode, | ||
| 68 | } | 66 | } |
| 69 | 67 | ||
| 70 | impl VkH265Decoder { | 68 | impl VkH265Decoder { |
| 71 | - pub(crate) fn new(mode: Mode) -> Result<Self> { | 69 | + pub(crate) fn new() -> Result<Self> { |
| 72 | let gpu = crate::hw::vk_gpu() | 70 | let gpu = crate::hw::vk_gpu() |
| 73 | .ok_or_else(|| Error::unsupported("Vulkan Video H.265 decode", "no Vulkan device"))?; | 71 | .ok_or_else(|| Error::unsupported("Vulkan Video H.265 decode", "no Vulkan device"))?; |
| 74 | Ok(VkH265Decoder { | 72 | Ok(VkH265Decoder { |
| @@ -83,7 +81,6 @@ impl VkH265Decoder { | |||
| 83 | current: None, | 81 | current: None, |
| 84 | ready: VecDeque::new(), | 82 | ready: VecDeque::new(), |
| 85 | no_rasl_output: false, | 83 | no_rasl_output: false, |
| 86 | - mode, | ||
| 87 | }) | 84 | }) |
| 88 | } | 85 | } |
| 89 | 86 | ||
| @@ -120,7 +117,7 @@ impl VkH265Decoder { | |||
| 120 | self.finish_picture() | 117 | self.finish_picture() |
| 121 | } | 118 | } |
| 122 | 119 | ||
| 123 | - pub(crate) fn next_frame(&mut self) -> Option<(i64, Decoded)> { | 120 | + pub(crate) fn next_frame(&mut self) -> Option<HwPicture> { |
| 124 | self.ready.pop_front() | 121 | self.ready.pop_front() |
| 125 | } | 122 | } |
| 126 | 123 | ||
| @@ -264,8 +261,10 @@ impl VkH265Decoder { | |||
| 264 | bit_depth, | 261 | bit_depth, |
| 265 | }, | 262 | }, |
| 266 | )?; | 263 | )?; |
| 264 | + let ring = Ring::new(h265.ring_len()); | ||
| 267 | self.session = Some(Session { | 265 | self.session = Some(Session { |
| 268 | - h265, | 266 | + h265: Rc::new(RefCell::new(h265)), |
| 267 | + ring, | ||
| 269 | bit_depth, | 268 | bit_depth, |
| 270 | max_width, | 269 | max_width, |
| 271 | max_height, | 270 | max_height, |
| @@ -293,7 +292,7 @@ impl VkH265Decoder { | |||
| 293 | .session | 292 | .session |
| 294 | .as_mut() | 293 | .as_mut() |
| 295 | .ok_or_else(|| Error::config("H.265 parameters before any SPS"))?; | 294 | .ok_or_else(|| Error::config("H.265 parameters before any SPS"))?; |
| 296 | - session.h265.set_parameters(&vps, &sps, &pps)?; | 295 | + session.h265.borrow_mut().set_parameters(&vps, &sps, &pps)?; |
| 297 | self.params_dirty = false; | 296 | self.params_dirty = false; |
| 298 | Ok(()) | 297 | Ok(()) |
| 299 | } | 298 | } |
| @@ -335,7 +334,7 @@ impl VkH265Decoder { | |||
| 335 | .session | 334 | .session |
| 336 | .as_mut() | 335 | .as_mut() |
| 337 | .ok_or_else(|| Error::config("H.265 picture before any SPS"))?; | 336 | .ok_or_else(|| Error::config("H.265 picture before any SPS"))?; |
| 338 | - session.h265.decode(&H265DecodePicture { | 337 | + session.h265.borrow_mut().decode(&H265DecodePicture { |
| 339 | vps_id: sps.vps_id, | 338 | vps_id: sps.vps_id, |
| 340 | sps_id: current.sps_id, | 339 | sps_id: current.sps_id, |
| 341 | pps_id: current.pps_id, | 340 | pps_id: current.pps_id, |
| @@ -354,47 +353,24 @@ impl VkH265Decoder { | |||
| 354 | slices: &slices, | 353 | slices: &slices, |
| 355 | })?; | 354 | })?; |
| 356 | if current.output { | 355 | if current.output { |
| 357 | - let frame = match self.mode { | 356 | + let slot = session.ring.take(|| Ok(session.h265.borrow_mut().add_ring_slot()?))?; |
| 358 | - Mode::I420 => { | ||
| 359 | - let (y, uv) = session.h265.read_output( | ||
| 360 | - sps.pic_width_in_luma_samples, | ||
| 361 | - sps.pic_height_in_luma_samples, | ||
| 362 | - )?; | ||
| 363 | - Decoded::I420(crop(&sps, &y, &uv)?) | ||
| 364 | - } | ||
| 365 | - Mode::I420_16 => { | ||
| 366 | - let (y, uv) = session.h265.read_output( | ||
| 367 | - sps.pic_width_in_luma_samples, | ||
| 368 | - sps.pic_height_in_luma_samples, | ||
| 369 | - )?; | ||
| 370 | - Decoded::I420_16(crop16(&sps, &y, &uv)?) | ||
| 371 | - } | ||
| 372 | - #[cfg(target_os = "linux")] | ||
| 373 | - Mode::DmaBuf => { | ||
| 374 | - let (width, height) = sps.display_size(); | ||
| 375 | - let origin = (2 * sps.conf_win.left as u32, 2 * sps.conf_win.top as u32); | ||
| 376 | - let export = session | ||
| 377 | - .h265 | ||
| 378 | - .read_output_dmabuf((origin.0, origin.1, width, height))?; | ||
| 379 | - Decoded::DmaBuf(export.into()) | ||
| 380 | - } | ||
| 381 | - Mode::Rgb(rgb) => { | ||
| 382 | let (width, height) = sps.display_size(); | 357 | let (width, height) = sps.display_size(); |
| 383 | let origin = (2 * sps.conf_win.left as u32, 2 * sps.conf_win.top as u32); | 358 | let origin = (2 * sps.conf_win.left as u32, 2 * sps.conf_win.top as u32); |
| 384 | - let matrix = | 359 | + session.h265.borrow_mut().store_output( |
| 385 | - ColorDescription::resolve(Tags::default(), vui_colour_tags(&sps), height) | 360 | + slot.index(), |
| 386 | - .matrix; | ||
| 387 | - let (w, h) = rgb_size(width, height, rgb.max_width); | ||
| 388 | - let data = session.h265.read_output_rgb( | ||
| 389 | (origin.0, origin.1, width, height), | 361 | (origin.0, origin.1, width, height), |
| 390 | - (w, h), | ||
| 391 | - rgb.order == RgbOrder::Bgra, | ||
| 392 | - yuv_to_rgb(matrix).words(), | ||
| 393 | )?; | 362 | )?; |
| 394 | - Decoded::Rgb(RgbPicture::new(data, w, h)) | 363 | + let matrix = |
| 395 | - } | 364 | + ColorDescription::resolve(Tags::default(), vui_colour_tags(&sps), height).matrix; |
| 396 | - }; | 365 | + let picture = VkPicture::new( |
| 397 | - self.outputs.insert(current.slot, frame); | 366 | + Surfaces::H265(session.h265.clone()), |
| 367 | + slot, | ||
| 368 | + (width, height), | ||
| 369 | + (8 + sps.bit_depth_luma_minus8) as u8, | ||
| 370 | + matrix, | ||
| 371 | + ); | ||
| 372 | + self.outputs | ||
| 373 | + .insert(current.slot, HwPicture::vk(current.timestamp, picture)); | ||
| 398 | } | 374 | } |
| 399 | 375 | ||
| 400 | self.dpb.pictures.push(hevc_dpb::StoredPicture { | 376 | self.dpb.pictures.push(hevc_dpb::StoredPicture { |
| @@ -420,7 +396,7 @@ impl VkH265Decoder { | |||
| 420 | while let Some(idx) = self.dpb.next_output(flush) { | 396 | while let Some(idx) = self.dpb.next_output(flush) { |
| 421 | let pic = &self.dpb.pictures[idx]; | 397 | let pic = &self.dpb.pictures[idx]; |
| 422 | if let Some(frame) = self.outputs.remove(&pic.surface) { | 398 | if let Some(frame) = self.outputs.remove(&pic.surface) { |
| 423 | - self.ready.push_back((pic.timestamp, frame)); | 399 | + self.ready.push_back(frame); |
| 424 | } | 400 | } |
| 425 | self.dpb.released(idx); | 401 | self.dpb.released(idx); |
| 426 | } | 402 | } |
| @@ -435,34 +411,6 @@ fn store<T: PartialEq>(slot: &mut Option<T>, value: T) -> bool { | |||
| 435 | true | 411 | true |
| 436 | } | 412 | } |
| 437 | 413 | ||
| 438 | -fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> Result<I420> { | ||
| 439 | - semi_planar_to_i420( | ||
| 440 | - (8 + sps.bit_depth_luma_minus8) as u8, | ||
| 441 | - y, | ||
| 442 | - uv, | ||
| 443 | - sps.pic_width_in_luma_samples as usize, | ||
| 444 | - ( | ||
| 445 | - 2 * sps.conf_win.left as usize, | ||
| 446 | - 2 * sps.conf_win.top as usize, | ||
| 447 | - ), | ||
| 448 | - sps.display_size(), | ||
| 449 | - ) | ||
| 450 | -} | ||
| 451 | - | ||
| 452 | -fn crop16(sps: &Sps, y: &[u8], uv: &[u8]) -> Result<I420_16> { | ||
| 453 | - semi_planar_to_i420_16( | ||
| 454 | - (8 + sps.bit_depth_luma_minus8) as u8, | ||
| 455 | - y, | ||
| 456 | - uv, | ||
| 457 | - sps.pic_width_in_luma_samples as usize, | ||
| 458 | - ( | ||
| 459 | - 2 * sps.conf_win.left as usize, | ||
| 460 | - 2 * sps.conf_win.top as usize, | ||
| 461 | - ), | ||
| 462 | - sps.display_size(), | ||
| 463 | - ) | ||
| 464 | -} | ||
| 465 | - | ||
| 466 | fn ptl(p: &ec_h265_syntax::ProfileTierLevel) -> H265ProfileTierLevel { | 414 | fn ptl(p: &ec_h265_syntax::ProfileTierLevel) -> H265ProfileTierLevel { |
| 467 | H265ProfileTierLevel { | 415 | H265ProfileTierLevel { |
| 468 | tier_flag: p.tier_flag, | 416 | tier_flag: p.tier_flag, |
crates/ec-hw/src/vkdec_av1.rsmodified+34 -129
| @@ -1,5 +1,6 @@ | |||
| 1 | //! AV1 decoding over Vulkan Video: the reference slots over DPB slot indices. | 1 | //! AV1 decoding over Vulkan Video: the reference slots over DPB slot indices. |
| 2 | 2 | ||
| 3 | +use std::cell::RefCell; | ||
| 3 | use std::collections::VecDeque; | 4 | use std::collections::VecDeque; |
| 4 | use std::rc::Rc; | 5 | use std::rc::Rc; |
| 5 | 6 | ||
| @@ -20,16 +21,14 @@ use crate::ref_slots::RefSlots; | |||
| 20 | use crate::error::{Error, Result}; | 21 | use crate::error::{Error, Result}; |
| 21 | use ec_core::color::{ColorDescription, Tags}; | 22 | use ec_core::color::{ColorDescription, Tags}; |
| 22 | 23 | ||
| 23 | -use crate::hw::{Mode, RgbOrder}; | 24 | +use crate::output::{HwPicture, Ring, Surfaces, VkPicture}; |
| 24 | -use crate::picture::{ | 25 | +use crate::picture::{I420, I420_16}; |
| 25 | - Decoded, I420, I420_16, RgbPicture, i420_to_rgb, rgb_size, semi_planar_to_i420, | ||
| 26 | - semi_planar_to_i420_16, yuv_to_rgb, | ||
| 27 | -}; | ||
| 28 | 26 | ||
| 29 | const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; | 27 | const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; |
| 30 | 28 | ||
| 31 | struct Session { | 29 | struct Session { |
| 32 | - av1: Av1DecodeSession, | 30 | + av1: Rc<RefCell<Av1DecodeSession>>, |
| 31 | + ring: Ring, | ||
| 33 | bit_depth: u8, | 32 | bit_depth: u8, |
| 34 | width: u32, | 33 | width: u32, |
| 35 | height: u32, | 34 | height: u32, |
| @@ -38,7 +37,7 @@ struct Session { | |||
| 38 | 37 | ||
| 39 | struct Reference { | 38 | struct Reference { |
| 40 | state: Av1RefSlot, | 39 | state: Av1RefSlot, |
| 41 | - picture: Decoded, | 40 | + picture: VkPicture, |
| 42 | } | 41 | } |
| 43 | 42 | ||
| 44 | /// AV1 Vulkan Video decoder: reference bookkeeping in Rust, frames decoded into DPB slots. | 43 | /// AV1 Vulkan Video decoder: reference bookkeeping in Rust, frames decoded into DPB slots. |
| @@ -47,12 +46,11 @@ pub(crate) struct VkAv1Decoder { | |||
| 47 | parser: Av1Parser, | 46 | parser: Av1Parser, |
| 48 | session: Option<Session>, | 47 | session: Option<Session>, |
| 49 | refs: RefSlots<Rc<Reference>>, | 48 | refs: RefSlots<Rc<Reference>>, |
| 50 | - ready: VecDeque<(i64, Decoded)>, | 49 | + ready: VecDeque<HwPicture>, |
| 51 | - mode: Mode, | ||
| 52 | } | 50 | } |
| 53 | 51 | ||
| 54 | impl VkAv1Decoder { | 52 | impl VkAv1Decoder { |
| 55 | - pub(crate) fn new(mode: Mode) -> Result<Self> { | 53 | + pub(crate) fn new() -> Result<Self> { |
| 56 | let gpu = crate::hw::vk_gpu() | 54 | let gpu = crate::hw::vk_gpu() |
| 57 | .ok_or_else(|| Error::unsupported("Vulkan Video AV1 decode", "no Vulkan device"))?; | 55 | .ok_or_else(|| Error::unsupported("Vulkan Video AV1 decode", "no Vulkan device"))?; |
| 58 | Ok(VkAv1Decoder { | 56 | Ok(VkAv1Decoder { |
| @@ -61,7 +59,6 @@ impl VkAv1Decoder { | |||
| 61 | session: None, | 59 | session: None, |
| 62 | refs: RefSlots::new(), | 60 | refs: RefSlots::new(), |
| 63 | ready: VecDeque::new(), | 61 | ready: VecDeque::new(), |
| 64 | - mode, | ||
| 65 | }) | 62 | }) |
| 66 | } | 63 | } |
| 67 | 64 | ||
| @@ -90,7 +87,7 @@ impl VkAv1Decoder { | |||
| 90 | Ok(()) | 87 | Ok(()) |
| 91 | } | 88 | } |
| 92 | 89 | ||
| 93 | - pub(crate) fn next_frame(&mut self) -> Option<(i64, Decoded)> { | 90 | + pub(crate) fn next_frame(&mut self) -> Option<HwPicture> { |
| 94 | self.ready.pop_front() | 91 | self.ready.pop_front() |
| 95 | } | 92 | } |
| 96 | 93 | ||
| @@ -118,16 +115,8 @@ impl VkAv1Decoder { | |||
| 118 | .parser | 115 | .parser |
| 119 | .sequence_header() | 116 | .sequence_header() |
| 120 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; | 117 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; |
| 121 | - if header.film_grain.apply_grain { | 118 | + let shown = reference.picture.with_grain(&header.film_grain, seq); |
| 122 | - grain_supported(seq, self.mode)?; | 119 | + self.ready.push_back(HwPicture::vk(timestamp, shown)); |
| 123 | - } | ||
| 124 | - let shown = present( | ||
| 125 | - self.mode, | ||
| 126 | - reference.picture.try_clone()?, | ||
| 127 | - &header.film_grain, | ||
| 128 | - seq, | ||
| 129 | - )?; | ||
| 130 | - self.ready.push_back((timestamp, shown)); | ||
| 131 | if header.frame_type == FrameType::Key { | 120 | if header.frame_type == FrameType::Key { |
| 132 | self.refs.refresh(u8::MAX, Some(reference)); | 121 | self.refs.refresh(u8::MAX, Some(reference)); |
| 133 | } | 122 | } |
| @@ -146,7 +135,7 @@ impl VkAv1Decoder { | |||
| 146 | .sequence_header() | 135 | .sequence_header() |
| 147 | .cloned() | 136 | .cloned() |
| 148 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; | 137 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; |
| 149 | - check_supported(&seq, header, self.mode)?; | 138 | + check_supported(&seq, header)?; |
| 150 | let (width, height) = (header.upscaled_width, header.frame_height); | 139 | let (width, height) = (header.upscaled_width, header.frame_height); |
| 151 | self.ensure_session(&seq, width, height)?; | 140 | self.ensure_session(&seq, width, height)?; |
| 152 | 141 | ||
| @@ -184,57 +173,22 @@ impl VkAv1Decoder { | |||
| 184 | .session | 173 | .session |
| 185 | .as_mut() | 174 | .as_mut() |
| 186 | .ok_or_else(|| Error::config("AV1 picture before any session"))?; | 175 | .ok_or_else(|| Error::config("AV1 picture before any session"))?; |
| 187 | - session.av1.decode(&picture)?; | 176 | + session.av1.borrow_mut().decode(&picture)?; |
| 188 | 177 | ||
| 189 | if !(header.show_frame || header.refresh_frame_flags != 0) { | 178 | if !(header.show_frame || header.refresh_frame_flags != 0) { |
| 190 | return Ok(()); | 179 | return Ok(()); |
| 191 | } | 180 | } |
| 192 | - let mode = self.mode; | 181 | + let slot = session.ring.take(|| Ok(session.av1.borrow_mut().add_ring_slot()?))?; |
| 193 | - let readback = match mode { | 182 | + session.av1.borrow_mut().store_output(slot.index(), (0, 0, width, height))?; |
| 194 | - Mode::Rgb(_) if seq.color_config.bit_depth == 8 => Mode::I420, | ||
| 195 | - other => other, | ||
| 196 | - }; | ||
| 197 | - let decoded = match readback { | ||
| 198 | - Mode::I420 => { | ||
| 199 | - let (y, uv) = session.av1.read_output(width, height)?; | ||
| 200 | - Decoded::I420(semi_planar_to_i420( | ||
| 201 | - session.bit_depth, | ||
| 202 | - &y, | ||
| 203 | - &uv, | ||
| 204 | - width as usize, | ||
| 205 | - (0, 0), | ||
| 206 | - (width, height), | ||
| 207 | - )?) | ||
| 208 | - } | ||
| 209 | - Mode::I420_16 => { | ||
| 210 | - let (y, uv) = session.av1.read_output(width, height)?; | ||
| 211 | - Decoded::I420_16(semi_planar_to_i420_16( | ||
| 212 | - session.bit_depth, | ||
| 213 | - &y, | ||
| 214 | - &uv, | ||
| 215 | - width as usize, | ||
| 216 | - (0, 0), | ||
| 217 | - (width, height), | ||
| 218 | - )?) | ||
| 219 | - } | ||
| 220 | - #[cfg(target_os = "linux")] | ||
| 221 | - Mode::DmaBuf => { | ||
| 222 | - let export = session.av1.read_output_dmabuf((0, 0, width, height))?; | ||
| 223 | - Decoded::DmaBuf(export.into()) | ||
| 224 | - } | ||
| 225 | - Mode::Rgb(rgb) => { | ||
| 226 | let codes = Tags::from_codes(u64::from(seq.color_config.matrix_coefficients), 0, 0); | 183 | let codes = Tags::from_codes(u64::from(seq.color_config.matrix_coefficients), 0, 0); |
| 227 | let matrix = ColorDescription::resolve(Tags::default(), codes, height).matrix; | 184 | let matrix = ColorDescription::resolve(Tags::default(), codes, height).matrix; |
| 228 | - let (w, h) = rgb_size(width, height, rgb.max_width); | 185 | + let decoded = VkPicture::new( |
| 229 | - let data = session.av1.read_output_rgb( | 186 | + Surfaces::Av1(session.av1.clone()), |
| 230 | - (0, 0, width, height), | 187 | + slot, |
| 231 | - (w, h), | 188 | + (width, height), |
| 232 | - rgb.order == RgbOrder::Bgra, | 189 | + session.bit_depth, |
| 233 | - yuv_to_rgb(matrix).words(), | 190 | + matrix, |
| 234 | - )?; | 191 | + ); |
| 235 | - Decoded::Rgb(RgbPicture::new(data, w, h)) | ||
| 236 | - } | ||
| 237 | - }; | ||
| 238 | let state = Av1RefSlot { | 192 | let state = Av1RefSlot { |
| 239 | slot: setup_slot, | 193 | slot: setup_slot, |
| 240 | frame_type: av1_frame_type(header.frame_type), | 194 | frame_type: av1_frame_type(header.frame_type), |
| @@ -245,8 +199,8 @@ impl VkAv1Decoder { | |||
| 245 | segmentation_enabled: header.segmentation.enabled, | 199 | segmentation_enabled: header.segmentation.enabled, |
| 246 | }; | 200 | }; |
| 247 | if header.show_frame { | 201 | if header.show_frame { |
| 248 | - let shown = present(mode, decoded.try_clone()?, &header.film_grain, &seq)?; | 202 | + let shown = decoded.with_grain(&header.film_grain, &seq); |
| 249 | - self.ready.push_back((timestamp, shown)); | 203 | + self.ready.push_back(HwPicture::vk(timestamp, shown)); |
| 250 | } | 204 | } |
| 251 | if header.refresh_frame_flags != 0 { | 205 | if header.refresh_frame_flags != 0 { |
| 252 | let reference = Rc::new(Reference { | 206 | let reference = Rc::new(Reference { |
| @@ -280,8 +234,10 @@ impl VkAv1Decoder { | |||
| 280 | bit_depth, | 234 | bit_depth, |
| 281 | }, | 235 | }, |
| 282 | )?; | 236 | )?; |
| 237 | + let ring = Ring::new(av1.ring_len()); | ||
| 283 | Session { | 238 | Session { |
| 284 | - av1, | 239 | + av1: Rc::new(RefCell::new(av1)), |
| 240 | + ring, | ||
| 285 | bit_depth, | 241 | bit_depth, |
| 286 | width, | 242 | width, |
| 287 | height, | 243 | height, |
| @@ -290,7 +246,10 @@ impl VkAv1Decoder { | |||
| 290 | } | 246 | } |
| 291 | }; | 247 | }; |
| 292 | if session.sequence.as_ref() != Some(seq) { | 248 | if session.sequence.as_ref() != Some(seq) { |
| 293 | - session.av1.set_sequence_header(&av1_sequence(seq))?; | 249 | + session |
| 250 | + .av1 | ||
| 251 | + .borrow_mut() | ||
| 252 | + .set_sequence_header(&av1_sequence(seq))?; | ||
| 294 | session.sequence = Some(seq.clone()); | 253 | session.sequence = Some(seq.clone()); |
| 295 | } | 254 | } |
| 296 | self.session = Some(session); | 255 | self.session = Some(session); |
| @@ -304,7 +263,7 @@ impl VkAv1Decoder { | |||
| 304 | } | 263 | } |
| 305 | } | 264 | } |
| 306 | 265 | ||
| 307 | -fn check_supported(seq: &SequenceHeader, header: &FrameHeader, mode: Mode) -> Result<()> { | 266 | +fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { |
| 308 | let c = &seq.color_config; | 267 | let c = &seq.color_config; |
| 309 | if seq.seq_profile != 0 | 268 | if seq.seq_profile != 0 |
| 310 | || !matches!(c.bit_depth, 8 | 10) | 269 | || !matches!(c.bit_depth, 8 | 10) |
| @@ -320,64 +279,10 @@ fn check_supported(seq: &SequenceHeader, header: &FrameHeader, mode: Mode) -> Re | |||
| 320 | if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 { | 279 | if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 { |
| 321 | return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size")); | 280 | return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size")); |
| 322 | } | 281 | } |
| 323 | - if header.film_grain.apply_grain { | ||
| 324 | - grain_supported(seq, mode)?; | ||
| 325 | - } | ||
| 326 | Ok(()) | 282 | Ok(()) |
| 327 | } | 283 | } |
| 328 | 284 | ||
| 329 | -fn grain_supported(seq: &SequenceHeader, mode: Mode) -> Result<()> { | 285 | +pub(crate) fn synthesize16(frame: I420_16, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420_16 { |
| 330 | - let eight = seq.color_config.bit_depth == 8; | ||
| 331 | - let supported = match mode { | ||
| 332 | - Mode::I420 | Mode::Rgb(_) => eight, | ||
| 333 | - Mode::I420_16 => true, | ||
| 334 | - #[cfg(target_os = "linux")] | ||
| 335 | - Mode::DmaBuf => false, | ||
| 336 | - }; | ||
| 337 | - if supported { | ||
| 338 | - Ok(()) | ||
| 339 | - } else { | ||
| 340 | - Err(Error::unsupported( | ||
| 341 | - "Vulkan AV1 film grain", | ||
| 342 | - "not for this output format", | ||
| 343 | - )) | ||
| 344 | - } | ||
| 345 | -} | ||
| 346 | - | ||
| 347 | -fn present( | ||
| 348 | - mode: Mode, | ||
| 349 | - picture: Decoded, | ||
| 350 | - grain: &FilmGrainParams, | ||
| 351 | - seq: &SequenceHeader, | ||
| 352 | -) -> Result<Decoded> { | ||
| 353 | - let picture = grained(picture, grain, seq); | ||
| 354 | - Ok(match (mode, picture) { | ||
| 355 | - (Mode::Rgb(rgb), Decoded::I420(frame)) => { | ||
| 356 | - let codes = Tags::from_codes(u64::from(seq.color_config.matrix_coefficients), 0, 0); | ||
| 357 | - let matrix = ColorDescription::resolve(Tags::default(), codes, frame.height).matrix; | ||
| 358 | - Decoded::Rgb(i420_to_rgb( | ||
| 359 | - &frame, | ||
| 360 | - rgb.max_width, | ||
| 361 | - rgb.order == RgbOrder::Bgra, | ||
| 362 | - matrix, | ||
| 363 | - )) | ||
| 364 | - } | ||
| 365 | - (_, other) => other, | ||
| 366 | - }) | ||
| 367 | -} | ||
| 368 | - | ||
| 369 | -fn grained(picture: Decoded, grain: &FilmGrainParams, seq: &SequenceHeader) -> Decoded { | ||
| 370 | - if !grain.apply_grain { | ||
| 371 | - return picture; | ||
| 372 | - } | ||
| 373 | - match picture { | ||
| 374 | - Decoded::I420(frame) => Decoded::I420(synthesize(frame, grain, seq)), | ||
| 375 | - Decoded::I420_16(frame) => Decoded::I420_16(synthesize16(frame, grain, seq)), | ||
| 376 | - other => other, | ||
| 377 | - } | ||
| 378 | -} | ||
| 379 | - | ||
| 380 | -fn synthesize16(frame: I420_16, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420_16 { | ||
| 381 | let bit_depth = u32::from(seq.color_config.bit_depth); | 286 | let bit_depth = u32::from(seq.color_config.bit_depth); |
| 382 | let shift = if bit_depth == 8 { 2 } else { 0 }; | 287 | let shift = if bit_depth == 8 { 2 } else { 0 }; |
| 383 | let narrow = |p: &[u16]| p.iter().map(|&s| s >> shift).collect::<Vec<u16>>(); | 288 | let narrow = |p: &[u16]| p.iter().map(|&s| s >> shift).collect::<Vec<u16>>(); |
| @@ -405,7 +310,7 @@ fn synthesize16(frame: I420_16, grain: &FilmGrainParams, seq: &SequenceHeader) - | |||
| 405 | } | 310 | } |
| 406 | } | 311 | } |
| 407 | 312 | ||
| 408 | -fn synthesize(frame: I420, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420 { | 313 | +pub(crate) fn synthesize(frame: I420, grain: &FilmGrainParams, seq: &SequenceHeader) -> I420 { |
| 409 | let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::<Vec<u16>>(); | 314 | let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::<Vec<u16>>(); |
| 410 | let clean = Picture { | 315 | let clean = Picture { |
| 411 | width: frame.width as usize, | 316 | width: frame.width as usize, |
crates/ec-hw/src/vkdec_vp9.rsmodified+24 -57
| @@ -1,5 +1,6 @@ | |||
| 1 | //! VP9 decoding over Vulkan Video: the reference slots over DPB slot indices. | 1 | //! VP9 decoding over Vulkan Video: the reference slots over DPB slot indices. |
| 2 | 2 | ||
| 3 | +use std::cell::RefCell; | ||
| 3 | use std::collections::VecDeque; | 4 | use std::collections::VecDeque; |
| 4 | use std::rc::Rc; | 5 | use std::rc::Rc; |
| 5 | 6 | ||
| @@ -14,16 +15,14 @@ use ec_vp9_syntax::{ | |||
| 14 | }; | 15 | }; |
| 15 | 16 | ||
| 16 | use crate::error::{Error, Result}; | 17 | use crate::error::{Error, Result}; |
| 17 | -use crate::hw::{Mode, RgbOrder}; | 18 | +use crate::output::{HwPicture, Ring, Surfaces, VkPicture}; |
| 18 | -use crate::picture::{ | ||
| 19 | - Decoded, RgbPicture, rgb_size, semi_planar_to_i420, semi_planar_to_i420_16, yuv_to_rgb, | ||
| 20 | -}; | ||
| 21 | use crate::ref_slots::RefSlots; | 19 | use crate::ref_slots::RefSlots; |
| 22 | 20 | ||
| 23 | const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; | 21 | const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; |
| 24 | 22 | ||
| 25 | struct Session { | 23 | struct Session { |
| 26 | - vp9: Vp9DecodeSession, | 24 | + vp9: Rc<RefCell<Vp9DecodeSession>>, |
| 25 | + ring: Ring, | ||
| 27 | bit_depth: u8, | 26 | bit_depth: u8, |
| 28 | width: u32, | 27 | width: u32, |
| 29 | height: u32, | 28 | height: u32, |
| @@ -31,7 +30,7 @@ struct Session { | |||
| 31 | 30 | ||
| 32 | struct Reference { | 31 | struct Reference { |
| 33 | slot: u32, | 32 | slot: u32, |
| 34 | - picture: Decoded, | 33 | + picture: VkPicture, |
| 35 | } | 34 | } |
| 36 | 35 | ||
| 37 | /// What the next frame needs from the one before it (spec 7.2, UsePrevFrameMvs). | 36 | /// What the next frame needs from the one before it (spec 7.2, UsePrevFrameMvs). |
| @@ -50,12 +49,11 @@ pub(crate) struct VkVp9Decoder { | |||
| 50 | session: Option<Session>, | 49 | session: Option<Session>, |
| 51 | refs: RefSlots<Rc<Reference>>, | 50 | refs: RefSlots<Rc<Reference>>, |
| 52 | previous: Option<PreviousFrame>, | 51 | previous: Option<PreviousFrame>, |
| 53 | - ready: VecDeque<(i64, Decoded)>, | 52 | + ready: VecDeque<HwPicture>, |
| 54 | - mode: Mode, | ||
| 55 | } | 53 | } |
| 56 | 54 | ||
| 57 | impl VkVp9Decoder { | 55 | impl VkVp9Decoder { |
| 58 | - pub(crate) fn new(mode: Mode) -> Result<Self> { | 56 | + pub(crate) fn new() -> Result<Self> { |
| 59 | let gpu = crate::hw::vk_gpu() | 57 | let gpu = crate::hw::vk_gpu() |
| 60 | .ok_or_else(|| Error::unsupported("Vulkan Video VP9 decode", "no Vulkan device"))?; | 58 | .ok_or_else(|| Error::unsupported("Vulkan Video VP9 decode", "no Vulkan device"))?; |
| 61 | Ok(VkVp9Decoder { | 59 | Ok(VkVp9Decoder { |
| @@ -65,7 +63,6 @@ impl VkVp9Decoder { | |||
| 65 | refs: RefSlots::new(), | 63 | refs: RefSlots::new(), |
| 66 | previous: None, | 64 | previous: None, |
| 67 | ready: VecDeque::new(), | 65 | ready: VecDeque::new(), |
| 68 | - mode, | ||
| 69 | }) | 66 | }) |
| 70 | } | 67 | } |
| 71 | 68 | ||
| @@ -79,7 +76,7 @@ impl VkVp9Decoder { | |||
| 79 | Ok(()) | 76 | Ok(()) |
| 80 | } | 77 | } |
| 81 | 78 | ||
| 82 | - pub(crate) fn next_frame(&mut self) -> Option<(i64, Decoded)> { | 79 | + pub(crate) fn next_frame(&mut self) -> Option<HwPicture> { |
| 83 | self.ready.pop_front() | 80 | self.ready.pop_front() |
| 84 | } | 81 | } |
| 85 | 82 | ||
| @@ -106,7 +103,8 @@ impl VkVp9Decoder { | |||
| 106 | "VP9 show_existing_frame names an empty reference slot", | 103 | "VP9 show_existing_frame names an empty reference slot", |
| 107 | )) | 104 | )) |
| 108 | })?; | 105 | })?; |
| 109 | - self.ready.push_back((timestamp, reference.picture.try_clone()?)); | 106 | + self.ready |
| 107 | + .push_back(HwPicture::vk(timestamp, reference.picture.clone())); | ||
| 110 | if let Some(previous) = self.previous.as_mut() { | 108 | if let Some(previous) = self.previous.as_mut() { |
| 111 | previous.show_frame = true; | 109 | previous.show_frame = true; |
| 112 | } | 110 | } |
| @@ -152,7 +150,8 @@ impl VkVp9Decoder { | |||
| 152 | .session | 150 | .session |
| 153 | .as_mut() | 151 | .as_mut() |
| 154 | .ok_or_else(|| Error::config("VP9 picture before any session"))?; | 152 | .ok_or_else(|| Error::config("VP9 picture before any session"))?; |
| 155 | - session.vp9.decode(&picture)?; | 153 | + let slot = session.ring.take(|| Ok(session.vp9.borrow_mut().add_ring_slot()?))?; |
| 154 | + session.vp9.borrow_mut().decode(&picture)?; | ||
| 156 | self.previous = Some(PreviousFrame { | 155 | self.previous = Some(PreviousFrame { |
| 157 | width: header.width, | 156 | width: header.width, |
| 158 | height: header.height, | 157 | height: header.height, |
| @@ -164,53 +163,19 @@ impl VkVp9Decoder { | |||
| 164 | return Ok(()); | 163 | return Ok(()); |
| 165 | } | 164 | } |
| 166 | let (width, height) = (header.width, header.height); | 165 | let (width, height) = (header.width, header.height); |
| 167 | - let decoded = match self.mode { | 166 | + let codes = Tags::from_codes(u64::from(header.color_space.h273().map_or(2, |c| c.2)), 0, 0); |
| 168 | - Mode::I420 => { | 167 | + let matrix = ColorDescription::resolve(Tags::default(), codes, height).matrix; |
| 169 | - let (y, uv) = session.vp9.read_output(width, height)?; | 168 | + session.vp9.borrow_mut().store_output(slot.index(), (0, 0, width, height))?; |
| 170 | - Decoded::I420(semi_planar_to_i420( | 169 | + let decoded = VkPicture::new( |
| 171 | - session.bit_depth, | 170 | + Surfaces::Vp9(session.vp9.clone()), |
| 172 | - &y, | 171 | + slot, |
| 173 | - &uv, | ||
| 174 | - width as usize, | ||
| 175 | - (0, 0), | ||
| 176 | (width, height), | 172 | (width, height), |
| 177 | - )?) | ||
| 178 | - } | ||
| 179 | - Mode::I420_16 => { | ||
| 180 | - let (y, uv) = session.vp9.read_output(width, height)?; | ||
| 181 | - Decoded::I420_16(semi_planar_to_i420_16( | ||
| 182 | session.bit_depth, | 173 | session.bit_depth, |
| 183 | - &y, | 174 | + matrix, |
| 184 | - &uv, | ||
| 185 | - width as usize, | ||
| 186 | - (0, 0), | ||
| 187 | - (width, height), | ||
| 188 | - )?) | ||
| 189 | - } | ||
| 190 | - #[cfg(target_os = "linux")] | ||
| 191 | - Mode::DmaBuf => { | ||
| 192 | - let export = session.vp9.read_output_dmabuf((0, 0, width, height))?; | ||
| 193 | - Decoded::DmaBuf(export.into()) | ||
| 194 | - } | ||
| 195 | - Mode::Rgb(rgb) => { | ||
| 196 | - let codes = Tags::from_codes( | ||
| 197 | - u64::from(header.color_space.h273().map_or(2, |c| c.2)), | ||
| 198 | - 0, | ||
| 199 | - 0, | ||
| 200 | ); | 175 | ); |
| 201 | - let matrix = ColorDescription::resolve(Tags::default(), codes, height).matrix; | ||
| 202 | - let (w, h) = rgb_size(width, height, rgb.max_width); | ||
| 203 | - let data = session.vp9.read_output_rgb( | ||
| 204 | - (0, 0, width, height), | ||
| 205 | - (w, h), | ||
| 206 | - rgb.order == RgbOrder::Bgra, | ||
| 207 | - yuv_to_rgb(matrix).words(), | ||
| 208 | - )?; | ||
| 209 | - Decoded::Rgb(RgbPicture::new(data, w, h)) | ||
| 210 | - } | ||
| 211 | - }; | ||
| 212 | if header.show_frame { | 176 | if header.show_frame { |
| 213 | - self.ready.push_back((timestamp, decoded.try_clone()?)); | 177 | + self.ready |
| 178 | + .push_back(HwPicture::vk(timestamp, decoded.clone())); | ||
| 214 | } | 179 | } |
| 215 | if header.refresh_frame_flags != 0 { | 180 | if header.refresh_frame_flags != 0 { |
| 216 | let reference = Rc::new(Reference { | 181 | let reference = Rc::new(Reference { |
| @@ -247,8 +212,10 @@ impl VkVp9Decoder { | |||
| 247 | bit_depth: header.bit_depth, | 212 | bit_depth: header.bit_depth, |
| 248 | }, | 213 | }, |
| 249 | )?; | 214 | )?; |
| 215 | + let ring = Ring::new(vp9.ring_len()); | ||
| 250 | self.session = Some(Session { | 216 | self.session = Some(Session { |
| 251 | - vp9, | 217 | + vp9: Rc::new(RefCell::new(vp9)), |
| 218 | + ring, | ||
| 252 | bit_depth: header.bit_depth, | 219 | bit_depth: header.bit_depth, |
| 253 | width, | 220 | width, |
| 254 | height, | 221 | height, |
shims/vk-video-ffi/examples/av1_decode_smoke.rsmodified+2 -1
| @@ -301,7 +301,8 @@ fn main() { | |||
| 301 | tile_sizes: &sizes, | 301 | tile_sizes: &sizes, |
| 302 | }; | 302 | }; |
| 303 | session.decode(&pic).expect("decode"); | 303 | session.decode(&pic).expect("decode"); |
| 304 | - let (y, uv) = session.read_output(w, h).expect("read output"); | 304 | + session.store_output(0, (0, 0, w, h)).expect("store"); |
| 305 | + let (y, uv) = session.read_output(0, w, h).expect("read slot"); | ||
| 305 | 306 | ||
| 306 | let reference = fs::read(&reference).expect("read reference"); | 307 | let reference = fs::read(&reference).expect("read reference"); |
| 307 | let luma = (w * h) as usize; | 308 | let luma = (w * h) as usize; |
shims/vk-video-ffi/examples/h264_decode_smoke.rsmodified+2 -1
| @@ -159,7 +159,8 @@ fn main() { | |||
| 159 | slices: &slices, | 159 | slices: &slices, |
| 160 | }; | 160 | }; |
| 161 | session.decode(&pic).expect("decode"); | 161 | session.decode(&pic).expect("decode"); |
| 162 | - let (y, uv) = session.read_output(w, h).expect("read slot"); | 162 | + session.store_output(0, (0, 0, w, h)).expect("store"); |
| 163 | + let (y, uv) = session.read_output(0, w, h).expect("read slot"); | ||
| 163 | 164 | ||
| 164 | let mut pgm = format!("P5\n{w} {h}\n255\n").into_bytes(); | 165 | let mut pgm = format!("P5\n{w} {h}\n255\n").into_bytes(); |
| 165 | pgm.extend_from_slice(&y); | 166 | pgm.extend_from_slice(&y); |
shims/vk-video-ffi/examples/h265_decode_smoke.rsmodified+2 -1
| @@ -258,7 +258,8 @@ fn main() { | |||
| 258 | slices: &slices, | 258 | slices: &slices, |
| 259 | }; | 259 | }; |
| 260 | session.decode(&pic).expect("decode"); | 260 | session.decode(&pic).expect("decode"); |
| 261 | - let (y, uv) = session.read_output(w, h).expect("read slot"); | 261 | + session.store_output(0, (0, 0, w, h)).expect("store"); |
| 262 | + let (y, uv) = session.read_output(0, w, h).expect("read slot"); | ||
| 262 | 263 | ||
| 263 | let ref_data = fs::read(&reference).expect("read reference"); | 264 | let ref_data = fs::read(&reference).expect("read reference"); |
| 264 | let luma = (w * h) as usize; | 265 | let luma = (w * h) as usize; |
shims/vk-video-ffi/src/av1_decode.rsmodified+23 -5
| @@ -853,28 +853,46 @@ impl Av1DecodeSession { | |||
| 853 | } | 853 | } |
| 854 | 854 | ||
| 855 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | 855 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). |
| 856 | - pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | 856 | + pub fn ring_len(&self) -> u32 { |
| 857 | - self.dec.read_nv12(width, height) | 857 | + self.dec.ring_len() |
| 858 | + } | ||
| 859 | + | ||
| 860 | + /// Adds one slot to the output ring and returns its index; every slot is then in use or held. | ||
| 861 | + pub fn add_ring_slot(&mut self) -> Result<u32, Error> { | ||
| 862 | + self.dec.add_ring_slot() | ||
| 863 | + } | ||
| 864 | + | ||
| 865 | + /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. | ||
| 866 | + pub fn store_output(&mut self, slot: u32, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 867 | + self.dec.store(slot as usize, crop) | ||
| 868 | + } | ||
| 869 | + | ||
| 870 | + /// The top-left width x height of ring slot `slot` as NV12 planes (Y, interleaved UV). | ||
| 871 | + pub fn read_output(&mut self, slot: u32, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | ||
| 872 | + self.dec.read_nv12(slot as usize, width, height) | ||
| 858 | } | 873 | } |
| 859 | 874 | ||
| 860 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. | 875 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. |
| 861 | #[cfg(target_os = "linux")] | 876 | #[cfg(target_os = "linux")] |
| 862 | pub fn read_output_dmabuf( | 877 | pub fn read_output_dmabuf( |
| 863 | &mut self, | 878 | &mut self, |
| 864 | - crop: (u32, u32, u32, u32), | 879 | + slot: u32, |
| 880 | + width: u32, | ||
| 881 | + height: u32, | ||
| 865 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 882 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 866 | - self.dec.export(crop) | 883 | + self.dec.export(slot as usize, (width, height)) |
| 867 | } | 884 | } |
| 868 | 885 | ||
| 869 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. | 886 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. |
| 870 | pub fn read_output_rgb( | 887 | pub fn read_output_rgb( |
| 871 | &mut self, | 888 | &mut self, |
| 889 | + slot: u32, | ||
| 872 | crop: (u32, u32, u32, u32), | 890 | crop: (u32, u32, u32, u32), |
| 873 | out: (u32, u32), | 891 | out: (u32, u32), |
| 874 | bgra: bool, | 892 | bgra: bool, |
| 875 | coeffs: [i32; 5], | 893 | coeffs: [i32; 5], |
| 876 | ) -> Result<Vec<u8>, Error> { | 894 | ) -> Result<Vec<u8>, Error> { |
| 877 | - self.dec.read_rgb(crop, out, bgra, coeffs) | 895 | + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) |
| 878 | } | 896 | } |
| 879 | } | 897 | } |
| 880 | 898 | ||
shims/vk-video-ffi/src/dmabuf.rsmodified+46 -155
| @@ -7,7 +7,7 @@ use vulkanalia::vk::{ | |||
| 7 | ExtImageDrmFormatModifierExtensionDeviceCommands, KhrExternalMemoryFdExtensionDeviceCommands, | 7 | ExtImageDrmFormatModifierExtensionDeviceCommands, KhrExternalMemoryFdExtensionDeviceCommands, |
| 8 | }; | 8 | }; |
| 9 | 9 | ||
| 10 | -use crate::session::{Lane, NV12, P010, PLANE_0, PLANE_1, color_range, memory_type, vk}; | 10 | +use crate::session::{EXTERNAL_FAMILY, Lane, NV12, P010, PLANE_0, PLANE_1, memory_type, transfer, vk}; |
| 11 | use std::os::fd::AsFd; | 11 | use std::os::fd::AsFd; |
| 12 | use crate::{Error, Gpu}; | 12 | use crate::{Error, Gpu}; |
| 13 | 13 | ||
| @@ -477,46 +477,26 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> { | |||
| 477 | list.iter() | 477 | list.iter() |
| 478 | .filter(|m| { | 478 | .filter(|m| { |
| 479 | m.drm_format_modifier_tiling_features | 479 | m.drm_format_modifier_tiling_features |
| 480 | - .contains(vk::FormatFeatureFlags::TRANSFER_DST) | 480 | + .contains(vk::FormatFeatureFlags::TRANSFER_DST | vk::FormatFeatureFlags::TRANSFER_SRC) |
| 481 | }) | 481 | }) |
| 482 | .map(|m| m.drm_format_modifier) | 482 | .map(|m| m.drm_format_modifier) |
| 483 | .collect() | 483 | .collect() |
| 484 | } | 484 | } |
| 485 | 485 | ||
| 486 | -fn transfer( | 486 | +/// A ring slot's DMA-BUF: the one memory object, exported once, and its plane layout. |
| 487 | - d: &Device, | 487 | +pub(crate) struct Export { |
| 488 | - cb: vk::CommandBuffer, | 488 | + pub(crate) fd: OwnedFd, |
| 489 | - image: vk::Image, | 489 | + pub(crate) modifier: u64, |
| 490 | - from: (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2, u32), | 490 | + pub(crate) planes: [DmaBufPlane; 2], |
| 491 | - to: (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2, u32), | ||
| 492 | -) { | ||
| 493 | - let barrier = vk::ImageMemoryBarrier2::builder() | ||
| 494 | - .src_stage_mask(from.1) | ||
| 495 | - .src_access_mask(from.2) | ||
| 496 | - .dst_stage_mask(to.1) | ||
| 497 | - .dst_access_mask(to.2) | ||
| 498 | - .old_layout(from.0) | ||
| 499 | - .new_layout(to.0) | ||
| 500 | - .src_queue_family_index(from.3) | ||
| 501 | - .dst_queue_family_index(to.3) | ||
| 502 | - .image(image) | ||
| 503 | - .subresource_range(color_range()) | ||
| 504 | - .build(); | ||
| 505 | - let barriers = [barrier]; | ||
| 506 | - let info = vk::DependencyInfo::builder() | ||
| 507 | - .image_memory_barriers(&barriers) | ||
| 508 | - .build(); | ||
| 509 | - unsafe { d.cmd_pipeline_barrier2(cb, &info) }; | ||
| 510 | } | 491 | } |
| 511 | 492 | ||
| 512 | -/// Copies the crop window `(x, y, width, height)` of `src` (decoded NV12 or P010) into a new exportable image on the graphics lane, releases it to the foreign queue and exports it as one DMA-BUF. | 493 | +/// Creates an exportable NV12 or P010 image of `width` x `height` on the lane's family, leaves it released to the foreign queue in GENERAL, and exports its memory once. |
| 513 | -pub(crate) fn export( | 494 | +pub(crate) fn ring_image( |
| 514 | gpu: &Gpu, | 495 | gpu: &Gpu, |
| 515 | lane: &Lane, | 496 | lane: &Lane, |
| 516 | - src: vk::Image, | ||
| 517 | format: vk::Format, | 497 | format: vk::Format, |
| 518 | - (x, y, width, height): (u32, u32, u32, u32), | 498 | + (width, height): (u32, u32), |
| 519 | -) -> Result<DmaBufExport, Error> { | 499 | +) -> Result<(vk::Image, vk::DeviceMemory, Export), Error> { |
| 520 | check(gpu)?; | 500 | check(gpu)?; |
| 521 | let d = gpu.device(); | 501 | let d = gpu.device(); |
| 522 | let modifiers = export_modifiers(gpu, format); | 502 | let modifiers = export_modifiers(gpu, format); |
| @@ -543,7 +523,7 @@ pub(crate) fn export( | |||
| 543 | } else { | 523 | } else { |
| 544 | vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT | 524 | vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT |
| 545 | }) | 525 | }) |
| 546 | - .usage(vk::ImageUsageFlags::TRANSFER_DST) | 526 | + .usage(vk::ImageUsageFlags::TRANSFER_SRC | vk::ImageUsageFlags::TRANSFER_DST) |
| 547 | .sharing_mode(vk::SharingMode::EXCLUSIVE) | 527 | .sharing_mode(vk::SharingMode::EXCLUSIVE) |
| 548 | .initial_layout(vk::ImageLayout::UNDEFINED) | 528 | .initial_layout(vk::ImageLayout::UNDEFINED) |
| 549 | .push_next(&mut external); | 529 | .push_next(&mut external); |
| @@ -553,98 +533,16 @@ pub(crate) fn export( | |||
| 553 | info.push_next(&mut list).build() | 533 | info.push_next(&mut list).build() |
| 554 | }; | 534 | }; |
| 555 | let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? }; | 535 | let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? }; |
| 556 | - let exported = export_image(gpu, lane, src, image, (x, y, width, height), linear); | 536 | + let memory = match bind_exported(gpu, image) { |
| 537 | + Ok(memory) => memory, | ||
| 538 | + Err(e) => { | ||
| 557 | unsafe { d.destroy_image(image, None) }; | 539 | unsafe { d.destroy_image(image, None) }; |
| 558 | - exported.map(|(fd, modifier, planes)| DmaBufExport { | ||
| 559 | - fd, | ||
| 560 | - modifier, | ||
| 561 | - planes, | ||
| 562 | - width, | ||
| 563 | - height, | ||
| 564 | - p010: format == crate::session::P010, | ||
| 565 | - }) | ||
| 566 | -} | ||
| 567 | - | ||
| 568 | -fn export_image( | ||
| 569 | - gpu: &Gpu, | ||
| 570 | - lane: &Lane, | ||
| 571 | - src: vk::Image, | ||
| 572 | - image: vk::Image, | ||
| 573 | - (x, y, width, height): (u32, u32, u32, u32), | ||
| 574 | - linear: bool, | ||
| 575 | -) -> Result<(OwnedFd, u64, [DmaBufPlane; 2]), Error> { | ||
| 576 | - let d = gpu.device(); | ||
| 577 | - let req = unsafe { d.get_image_memory_requirements(image) }; | ||
| 578 | - let index = memory_type(gpu, req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)?; | ||
| 579 | - let mut export = vk::ExportMemoryAllocateInfo::builder() | ||
| 580 | - .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT) | ||
| 581 | - .build(); | ||
| 582 | - let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder() | ||
| 583 | - .image(image) | ||
| 584 | - .build(); | ||
| 585 | - let alloc = vk::MemoryAllocateInfo::builder() | ||
| 586 | - .allocation_size(req.size) | ||
| 587 | - .memory_type_index(index) | ||
| 588 | - .push_next(&mut dedicated) | ||
| 589 | - .push_next(&mut export) | ||
| 590 | - .build(); | ||
| 591 | - let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? }; | ||
| 592 | - if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } { | ||
| 593 | - unsafe { d.free_memory(memory, None) }; | ||
| 594 | return Err(e); | 540 | return Err(e); |
| 595 | } | 541 | } |
| 596 | - let result = copy_and_export(gpu, lane, src, image, memory, (x, y, width, height), linear); | 542 | + }; |
| 597 | - unsafe { d.free_memory(memory, None) }; | ||
| 598 | - result | ||
| 599 | -} | ||
| 600 | - | ||
| 601 | -fn copy_and_export( | ||
| 602 | - gpu: &Gpu, | ||
| 603 | - lane: &Lane, | ||
| 604 | - src: vk::Image, | ||
| 605 | - image: vk::Image, | ||
| 606 | - memory: vk::DeviceMemory, | ||
| 607 | - (x, y, width, height): (u32, u32, u32, u32), | ||
| 608 | - linear: bool, | ||
| 609 | -) -> Result<(OwnedFd, u64, [DmaBufPlane; 2]), Error> { | ||
| 610 | - let d = gpu.device(); | ||
| 611 | let cb = lane.cb; | 543 | let cb = lane.cb; |
| 612 | - let decode = ( | ||
| 613 | - vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 614 | - vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 615 | - vk::AccessFlags2::NONE, | ||
| 616 | - vk::QUEUE_FAMILY_IGNORED, | ||
| 617 | - ); | ||
| 618 | - let copy_src = ( | ||
| 619 | - vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 620 | - vk::PipelineStageFlags2::COPY, | ||
| 621 | - vk::AccessFlags2::TRANSFER_READ, | ||
| 622 | - vk::QUEUE_FAMILY_IGNORED, | ||
| 623 | - ); | ||
| 624 | - let copy_dst = ( | ||
| 625 | - vk::ImageLayout::TRANSFER_DST_OPTIMAL, | ||
| 626 | - vk::PipelineStageFlags2::COPY, | ||
| 627 | - vk::AccessFlags2::TRANSFER_WRITE, | ||
| 628 | - vk::QUEUE_FAMILY_IGNORED, | ||
| 629 | - ); | ||
| 630 | - let planes_regions = [ | ||
| 631 | - plane_copy(PLANE_0, (x, y), (width, height)), | ||
| 632 | - plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), | ||
| 633 | - ]; | ||
| 634 | lane.begin()?; | 544 | lane.begin()?; |
| 635 | - unsafe { | 545 | + { |
| 636 | - transfer( | ||
| 637 | - d, | ||
| 638 | - cb, | ||
| 639 | - src, | ||
| 640 | - decode, | ||
| 641 | - ( | ||
| 642 | - vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 643 | - vk::PipelineStageFlags2::COPY, | ||
| 644 | - vk::AccessFlags2::TRANSFER_READ, | ||
| 645 | - vk::QUEUE_FAMILY_IGNORED, | ||
| 646 | - ), | ||
| 647 | - ); | ||
| 648 | transfer( | 546 | transfer( |
| 649 | d, | 547 | d, |
| 650 | cb, | 548 | cb, |
| @@ -655,15 +553,12 @@ fn copy_and_export( | |||
| 655 | vk::AccessFlags2::NONE, | 553 | vk::AccessFlags2::NONE, |
| 656 | vk::QUEUE_FAMILY_IGNORED, | 554 | vk::QUEUE_FAMILY_IGNORED, |
| 657 | ), | 555 | ), |
| 658 | - copy_dst, | 556 | + ( |
| 659 | - ); | ||
| 660 | - d.cmd_copy_image( | ||
| 661 | - cb, | ||
| 662 | - src, | ||
| 663 | - vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 664 | - image, | ||
| 665 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, | 557 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
| 666 | - &planes_regions, | 558 | + vk::PipelineStageFlags2::COPY, |
| 559 | + vk::AccessFlags2::TRANSFER_WRITE, | ||
| 560 | + vk::QUEUE_FAMILY_IGNORED, | ||
| 561 | + ), | ||
| 667 | ); | 562 | ); |
| 668 | transfer( | 563 | transfer( |
| 669 | d, | 564 | d, |
| @@ -679,10 +574,9 @@ fn copy_and_export( | |||
| 679 | vk::ImageLayout::GENERAL, | 574 | vk::ImageLayout::GENERAL, |
| 680 | vk::PipelineStageFlags2::NONE, | 575 | vk::PipelineStageFlags2::NONE, |
| 681 | vk::AccessFlags2::NONE, | 576 | vk::AccessFlags2::NONE, |
| 682 | - QUEUE_FAMILY_FOREIGN, | 577 | + EXTERNAL_FAMILY, |
| 683 | ), | 578 | ), |
| 684 | ); | 579 | ); |
| 685 | - transfer(d, cb, src, copy_src, decode); | ||
| 686 | } | 580 | } |
| 687 | lane.end()?; | 581 | lane.end()?; |
| 688 | lane.submit()?; | 582 | lane.submit()?; |
| @@ -703,37 +597,34 @@ fn copy_and_export( | |||
| 703 | layout(gpu, image, linear, 0)?, | 597 | layout(gpu, image, linear, 0)?, |
| 704 | layout(gpu, image, linear, 1)?, | 598 | layout(gpu, image, linear, 1)?, |
| 705 | ]; | 599 | ]; |
| 706 | - Ok((fd, modifier, planes)) | 600 | + Ok((image, memory, Export { fd, modifier, planes })) |
| 707 | } | 601 | } |
| 708 | 602 | ||
| 709 | -pub(crate) fn plane_copy( | 603 | +fn bind_exported(gpu: &Gpu, image: vk::Image) -> Result<vk::DeviceMemory, Error> { |
| 710 | - aspect: vk::ImageAspectFlags, | 604 | + let d = gpu.device(); |
| 711 | - origin: (u32, u32), | 605 | + let req = unsafe { d.get_image_memory_requirements(image) }; |
| 712 | - (width, height): (u32, u32), | 606 | + let index = memory_type(gpu, req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)?; |
| 713 | -) -> vk::ImageCopy { | 607 | + let mut export = vk::ExportMemoryAllocateInfo::builder() |
| 714 | - let layers = vk::ImageSubresourceLayers { | 608 | + .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT) |
| 715 | - aspect_mask: aspect, | 609 | + .build(); |
| 716 | - mip_level: 0, | 610 | + let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder() |
| 717 | - base_array_layer: 0, | 611 | + .image(image) |
| 718 | - layer_count: 1, | 612 | + .build(); |
| 719 | - }; | 613 | + let alloc = vk::MemoryAllocateInfo::builder() |
| 720 | - vk::ImageCopy { | 614 | + .allocation_size(req.size) |
| 721 | - src_subresource: layers, | 615 | + .memory_type_index(index) |
| 722 | - src_offset: vk::Offset3D { | 616 | + .push_next(&mut dedicated) |
| 723 | - x: origin.0 as i32, | 617 | + .push_next(&mut export) |
| 724 | - y: origin.1 as i32, | 618 | + .build(); |
| 725 | - z: 0, | 619 | + let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? }; |
| 726 | - }, | 620 | + if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } { |
| 727 | - dst_subresource: layers, | 621 | + unsafe { d.free_memory(memory, None) }; |
| 728 | - dst_offset: vk::Offset3D { x: 0, y: 0, z: 0 }, | 622 | + return Err(e); |
| 729 | - extent: vk::Extent3D { | ||
| 730 | - width, | ||
| 731 | - height, | ||
| 732 | - depth: 1, | ||
| 733 | - }, | ||
| 734 | } | 623 | } |
| 624 | + Ok(memory) | ||
| 735 | } | 625 | } |
| 736 | 626 | ||
| 627 | + | ||
| 737 | fn export_fd(gpu: &Gpu, memory: vk::DeviceMemory) -> Result<OwnedFd, Error> { | 628 | fn export_fd(gpu: &Gpu, memory: vk::DeviceMemory) -> Result<OwnedFd, Error> { |
| 738 | let info = vk::MemoryGetFdInfoKHR::builder() | 629 | let info = vk::MemoryGetFdInfoKHR::builder() |
| 739 | .memory(memory) | 630 | .memory(memory) |
shims/vk-video-ffi/src/h264_decode.rsmodified+23 -5
| @@ -457,28 +457,46 @@ impl H264DecodeSession { | |||
| 457 | } | 457 | } |
| 458 | 458 | ||
| 459 | /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | 459 | /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). |
| 460 | - pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | 460 | + pub fn ring_len(&self) -> u32 { |
| 461 | - self.dec.read_nv12(width, height) | 461 | + self.dec.ring_len() |
| 462 | + } | ||
| 463 | + | ||
| 464 | + /// Adds one slot to the output ring and returns its index; every slot is then in use or held. | ||
| 465 | + pub fn add_ring_slot(&mut self) -> Result<u32, Error> { | ||
| 466 | + self.dec.add_ring_slot() | ||
| 467 | + } | ||
| 468 | + | ||
| 469 | + /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. | ||
| 470 | + pub fn store_output(&mut self, slot: u32, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 471 | + self.dec.store(slot as usize, crop) | ||
| 472 | + } | ||
| 473 | + | ||
| 474 | + /// The top-left width x height of ring slot `slot` as NV12 planes (Y, interleaved UV). | ||
| 475 | + pub fn read_output(&mut self, slot: u32, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | ||
| 476 | + self.dec.read_nv12(slot as usize, width, height) | ||
| 462 | } | 477 | } |
| 463 | 478 | ||
| 464 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. | 479 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. |
| 465 | #[cfg(target_os = "linux")] | 480 | #[cfg(target_os = "linux")] |
| 466 | pub fn read_output_dmabuf( | 481 | pub fn read_output_dmabuf( |
| 467 | &mut self, | 482 | &mut self, |
| 468 | - crop: (u32, u32, u32, u32), | 483 | + slot: u32, |
| 484 | + width: u32, | ||
| 485 | + height: u32, | ||
| 469 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 486 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 470 | - self.dec.export(crop) | 487 | + self.dec.export(slot as usize, (width, height)) |
| 471 | } | 488 | } |
| 472 | 489 | ||
| 473 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. | 490 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. |
| 474 | pub fn read_output_rgb( | 491 | pub fn read_output_rgb( |
| 475 | &mut self, | 492 | &mut self, |
| 493 | + slot: u32, | ||
| 476 | crop: (u32, u32, u32, u32), | 494 | crop: (u32, u32, u32, u32), |
| 477 | out: (u32, u32), | 495 | out: (u32, u32), |
| 478 | bgra: bool, | 496 | bgra: bool, |
| 479 | coeffs: [i32; 5], | 497 | coeffs: [i32; 5], |
| 480 | ) -> Result<Vec<u8>, Error> { | 498 | ) -> Result<Vec<u8>, Error> { |
| 481 | - self.dec.read_rgb(crop, out, bgra, coeffs) | 499 | + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) |
| 482 | } | 500 | } |
| 483 | } | 501 | } |
| 484 | 502 | ||
shims/vk-video-ffi/src/h265_decode.rsmodified+23 -5
| @@ -694,28 +694,46 @@ impl H265DecodeSession { | |||
| 694 | } | 694 | } |
| 695 | 695 | ||
| 696 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | 696 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). |
| 697 | - pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | 697 | + pub fn ring_len(&self) -> u32 { |
| 698 | - self.dec.read_nv12(width, height) | 698 | + self.dec.ring_len() |
| 699 | + } | ||
| 700 | + | ||
| 701 | + /// Adds one slot to the output ring and returns its index; every slot is then in use or held. | ||
| 702 | + pub fn add_ring_slot(&mut self) -> Result<u32, Error> { | ||
| 703 | + self.dec.add_ring_slot() | ||
| 704 | + } | ||
| 705 | + | ||
| 706 | + /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. | ||
| 707 | + pub fn store_output(&mut self, slot: u32, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 708 | + self.dec.store(slot as usize, crop) | ||
| 709 | + } | ||
| 710 | + | ||
| 711 | + /// The top-left width x height of ring slot `slot` as NV12 planes (Y, interleaved UV). | ||
| 712 | + pub fn read_output(&mut self, slot: u32, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | ||
| 713 | + self.dec.read_nv12(slot as usize, width, height) | ||
| 699 | } | 714 | } |
| 700 | 715 | ||
| 701 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. | 716 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. |
| 702 | #[cfg(target_os = "linux")] | 717 | #[cfg(target_os = "linux")] |
| 703 | pub fn read_output_dmabuf( | 718 | pub fn read_output_dmabuf( |
| 704 | &mut self, | 719 | &mut self, |
| 705 | - crop: (u32, u32, u32, u32), | 720 | + slot: u32, |
| 721 | + width: u32, | ||
| 722 | + height: u32, | ||
| 706 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 723 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 707 | - self.dec.export(crop) | 724 | + self.dec.export(slot as usize, (width, height)) |
| 708 | } | 725 | } |
| 709 | 726 | ||
| 710 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. | 727 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. |
| 711 | pub fn read_output_rgb( | 728 | pub fn read_output_rgb( |
| 712 | &mut self, | 729 | &mut self, |
| 730 | + slot: u32, | ||
| 713 | crop: (u32, u32, u32, u32), | 731 | crop: (u32, u32, u32, u32), |
| 714 | out: (u32, u32), | 732 | out: (u32, u32), |
| 715 | bgra: bool, | 733 | bgra: bool, |
| 716 | coeffs: [i32; 5], | 734 | coeffs: [i32; 5], |
| 717 | ) -> Result<Vec<u8>, Error> { | 735 | ) -> Result<Vec<u8>, Error> { |
| 718 | - self.dec.read_rgb(crop, out, bgra, coeffs) | 736 | + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) |
| 719 | } | 737 | } |
| 720 | } | 738 | } |
| 721 | 739 | ||
shims/vk-video-ffi/src/lib.rsmodified+23 -6
| @@ -138,6 +138,10 @@ pub enum Direction { | |||
| 138 | /// Profile a capability is queried for, 4:2:0 only. | 138 | /// Profile a capability is queried for, 4:2:0 only. |
| 139 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 139 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 140 | pub enum CodecProfile { | 140 | pub enum CodecProfile { |
| 141 | + /// H.264 Constrained Baseline, 8-bit. | ||
| 142 | + H264ConstrainedBaseline, | ||
| 143 | + /// H.264 Main, 8-bit. | ||
| 144 | + H264Main, | ||
| 141 | /// H.264 High, 8-bit. | 145 | /// H.264 High, 8-bit. |
| 142 | H264High, | 146 | H264High, |
| 143 | /// H.265 Main, 8-bit. | 147 | /// H.265 Main, 8-bit. |
| @@ -150,6 +154,8 @@ pub enum CodecProfile { | |||
| 150 | Av1Main10, | 154 | Av1Main10, |
| 151 | /// VP9 Profile 0, 8-bit. | 155 | /// VP9 Profile 0, 8-bit. |
| 152 | Vp9Profile0, | 156 | Vp9Profile0, |
| 157 | + /// VP9 Profile 2, 10-bit. | ||
| 158 | + Vp9Profile2, | ||
| 153 | } | 159 | } |
| 154 | 160 | ||
| 155 | /// Capabilities of one codec, profile and direction. | 161 | /// Capabilities of one codec, profile and direction. |
| @@ -361,7 +367,10 @@ impl Gpu { | |||
| 361 | /// Capabilities of every codec, profile and direction the device supports. | 367 | /// Capabilities of every codec, profile and direction the device supports. |
| 362 | pub fn caps(&self) -> Vec<CodecCaps> { | 368 | pub fn caps(&self) -> Vec<CodecCaps> { |
| 363 | [ | 369 | [ |
| 370 | + (Codec::H264, Direction::Encode, CodecProfile::H264Main), | ||
| 364 | (Codec::H264, Direction::Encode, CodecProfile::H264High), | 371 | (Codec::H264, Direction::Encode, CodecProfile::H264High), |
| 372 | + (Codec::H264, Direction::Decode, CodecProfile::H264ConstrainedBaseline), | ||
| 373 | + (Codec::H264, Direction::Decode, CodecProfile::H264Main), | ||
| 365 | (Codec::H264, Direction::Decode, CodecProfile::H264High), | 374 | (Codec::H264, Direction::Decode, CodecProfile::H264High), |
| 366 | (Codec::H265, Direction::Encode, CodecProfile::H265Main), | 375 | (Codec::H265, Direction::Encode, CodecProfile::H265Main), |
| 367 | (Codec::H265, Direction::Encode, CodecProfile::H265Main10), | 376 | (Codec::H265, Direction::Encode, CodecProfile::H265Main10), |
| @@ -372,6 +381,7 @@ impl Gpu { | |||
| 372 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main), | 381 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main), |
| 373 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main10), | 382 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main10), |
| 374 | (Codec::Vp9, Direction::Decode, CodecProfile::Vp9Profile0), | 383 | (Codec::Vp9, Direction::Decode, CodecProfile::Vp9Profile0), |
| 384 | + (Codec::Vp9, Direction::Decode, CodecProfile::Vp9Profile2), | ||
| 375 | ] | 385 | ] |
| 376 | .into_iter() | 386 | .into_iter() |
| 377 | .filter_map(|(codec, direction, profile)| { | 387 | .filter_map(|(codec, direction, profile)| { |
| @@ -423,7 +433,7 @@ impl Gpu { | |||
| 423 | return Err(Error::Unsupported(format!("{codec:?} {direction:?}"))); | 433 | return Err(Error::Unsupported(format!("{codec:?} {direction:?}"))); |
| 424 | } | 434 | } |
| 425 | let depth = match profile { | 435 | let depth = match profile { |
| 426 | - CodecProfile::H265Main10 | CodecProfile::Av1Main10 => { | 436 | + CodecProfile::H265Main10 | CodecProfile::Av1Main10 | CodecProfile::Vp9Profile2 => { |
| 427 | vk::VideoComponentBitDepthFlagsKHR::_10 | 437 | vk::VideoComponentBitDepthFlagsKHR::_10 |
| 428 | } | 438 | } |
| 429 | _ => vk::VideoComponentBitDepthFlagsKHR::_8, | 439 | _ => vk::VideoComponentBitDepthFlagsKHR::_8, |
| @@ -435,14 +445,22 @@ impl Gpu { | |||
| 435 | .luma_bit_depth(depth) | 445 | .luma_bit_depth(depth) |
| 436 | .chroma_bit_depth(depth) | 446 | .chroma_bit_depth(depth) |
| 437 | }; | 447 | }; |
| 448 | + let h264_idc = match profile { | ||
| 449 | + CodecProfile::H264ConstrainedBaseline => vk::video::STD_VIDEO_H264_PROFILE_IDC_BASELINE, | ||
| 450 | + CodecProfile::H264Main => vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN, | ||
| 451 | + _ => vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH, | ||
| 452 | + }; | ||
| 453 | + let vp9_profile = match profile { | ||
| 454 | + CodecProfile::Vp9Profile2 => vk::video::STD_VIDEO_VP9_PROFILE_2, | ||
| 455 | + _ => vk::video::STD_VIDEO_VP9_PROFILE_0, | ||
| 456 | + }; | ||
| 438 | let h265_idc = match profile { | 457 | let h265_idc = match profile { |
| 439 | CodecProfile::H265Main10 => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN_10, | 458 | CodecProfile::H265Main10 => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN_10, |
| 440 | _ => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN, | 459 | _ => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN, |
| 441 | }; | 460 | }; |
| 442 | match (codec, direction) { | 461 | match (codec, direction) { |
| 443 | (Codec::H264, Direction::Encode) => { | 462 | (Codec::H264, Direction::Encode) => { |
| 444 | - let mut h = vk::VideoEncodeH264ProfileInfoKHR::builder() | 463 | + let mut h = vk::VideoEncodeH264ProfileInfoKHR::builder().std_profile_idc(h264_idc); |
| 445 | - .std_profile_idc(vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH); | ||
| 446 | let p = base(operation).push_next(&mut h); | 464 | let p = base(operation).push_next(&mut h); |
| 447 | self.query( | 465 | self.query( |
| 448 | codec, | 466 | codec, |
| @@ -454,7 +472,7 @@ impl Gpu { | |||
| 454 | } | 472 | } |
| 455 | (Codec::H264, Direction::Decode) => { | 473 | (Codec::H264, Direction::Decode) => { |
| 456 | let mut h = vk::VideoDecodeH264ProfileInfoKHR::builder() | 474 | let mut h = vk::VideoDecodeH264ProfileInfoKHR::builder() |
| 457 | - .std_profile_idc(vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH) | 475 | + .std_profile_idc(h264_idc) |
| 458 | .picture_layout(vk::VideoDecodeH264PictureLayoutFlagsKHR::PROGRESSIVE); | 476 | .picture_layout(vk::VideoDecodeH264PictureLayoutFlagsKHR::PROGRESSIVE); |
| 459 | let p = base(operation).push_next(&mut h); | 477 | let p = base(operation).push_next(&mut h); |
| 460 | self.query( | 478 | self.query( |
| @@ -512,8 +530,7 @@ impl Gpu { | |||
| 512 | ) | 530 | ) |
| 513 | } | 531 | } |
| 514 | (Codec::Vp9, _) => { | 532 | (Codec::Vp9, _) => { |
| 515 | - let mut h = vk::VideoDecodeVP9ProfileInfoKHR::builder() | 533 | + let mut h = vk::VideoDecodeVP9ProfileInfoKHR::builder().std_profile(vp9_profile); |
| 516 | - .std_profile(vk::video::STD_VIDEO_VP9_PROFILE_0); | ||
| 517 | let p = base(operation).push_next(&mut h); | 534 | let p = base(operation).push_next(&mut h); |
| 518 | self.query( | 535 | self.query( |
| 519 | codec, | 536 | codec, |
shims/vk-video-ffi/src/session.rsmodified+336 -61
| @@ -7,7 +7,7 @@ use vulkanalia::vk::{ | |||
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| 9 | #[cfg(target_os = "linux")] | 9 | #[cfg(target_os = "linux")] |
| 10 | -use crate::dmabuf::{DmaBuf, DmaBufFormat, ImportCache, acquire, plane_copy, release}; | 10 | +use crate::dmabuf::{DmaBuf, DmaBufFormat, ImportCache, acquire, release}; |
| 11 | #[cfg(target_os = "linux")] | 11 | #[cfg(target_os = "linux")] |
| 12 | use crate::rgb_convert::check_size; | 12 | use crate::rgb_convert::check_size; |
| 13 | use crate::rgb_convert::{Input, RgbConverter}; | 13 | use crate::rgb_convert::{Input, RgbConverter}; |
| @@ -163,6 +163,74 @@ pub(crate) fn color_range() -> vk::ImageSubresourceRange { | |||
| 163 | } | 163 | } |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | +pub(crate) const EXTERNAL_FAMILY: u32 = if cfg!(target_os = "linux") { | ||
| 167 | + u32::MAX - 1 | ||
| 168 | +} else { | ||
| 169 | + vk::QUEUE_FAMILY_IGNORED | ||
| 170 | +}; | ||
| 171 | + | ||
| 172 | +pub(crate) fn ring_owner(lane: &Lane) -> u32 { | ||
| 173 | + if cfg!(target_os = "linux") { | ||
| 174 | + lane.family | ||
| 175 | + } else { | ||
| 176 | + vk::QUEUE_FAMILY_IGNORED | ||
| 177 | + } | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +pub(crate) fn transfer( | ||
| 181 | + d: &Device, | ||
| 182 | + cb: vk::CommandBuffer, | ||
| 183 | + image: vk::Image, | ||
| 184 | + from: (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2, u32), | ||
| 185 | + to: (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2, u32), | ||
| 186 | +) { | ||
| 187 | + let barrier = vk::ImageMemoryBarrier2::builder() | ||
| 188 | + .src_stage_mask(from.1) | ||
| 189 | + .src_access_mask(from.2) | ||
| 190 | + .dst_stage_mask(to.1) | ||
| 191 | + .dst_access_mask(to.2) | ||
| 192 | + .old_layout(from.0) | ||
| 193 | + .new_layout(to.0) | ||
| 194 | + .src_queue_family_index(from.3) | ||
| 195 | + .dst_queue_family_index(to.3) | ||
| 196 | + .image(image) | ||
| 197 | + .subresource_range(color_range()) | ||
| 198 | + .build(); | ||
| 199 | + let barriers = [barrier]; | ||
| 200 | + let info = vk::DependencyInfo::builder() | ||
| 201 | + .image_memory_barriers(&barriers) | ||
| 202 | + .build(); | ||
| 203 | + unsafe { d.cmd_pipeline_barrier2(cb, &info) }; | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +pub(crate) fn plane_copy( | ||
| 207 | + aspect: vk::ImageAspectFlags, | ||
| 208 | + origin: (u32, u32), | ||
| 209 | + (width, height): (u32, u32), | ||
| 210 | +) -> vk::ImageCopy { | ||
| 211 | + let layers = vk::ImageSubresourceLayers { | ||
| 212 | + aspect_mask: aspect, | ||
| 213 | + mip_level: 0, | ||
| 214 | + base_array_layer: 0, | ||
| 215 | + layer_count: 1, | ||
| 216 | + }; | ||
| 217 | + vk::ImageCopy { | ||
| 218 | + src_subresource: layers, | ||
| 219 | + src_offset: vk::Offset3D { | ||
| 220 | + x: origin.0 as i32, | ||
| 221 | + y: origin.1 as i32, | ||
| 222 | + z: 0, | ||
| 223 | + }, | ||
| 224 | + dst_subresource: layers, | ||
| 225 | + dst_offset: vk::Offset3D { x: 0, y: 0, z: 0 }, | ||
| 226 | + extent: vk::Extent3D { | ||
| 227 | + width, | ||
| 228 | + height, | ||
| 229 | + depth: 1, | ||
| 230 | + }, | ||
| 231 | + } | ||
| 232 | +} | ||
| 233 | + | ||
| 166 | pub(crate) fn copy_region( | 234 | pub(crate) fn copy_region( |
| 167 | offset: u64, | 235 | offset: u64, |
| 168 | aspect: vk::ImageAspectFlags, | 236 | aspect: vk::ImageAspectFlags, |
| @@ -1304,6 +1372,16 @@ pub(crate) struct Decoder { | |||
| 1304 | rgb_out: Buffer, | 1372 | rgb_out: Buffer, |
| 1305 | rgb: Option<RgbPass>, | 1373 | rgb: Option<RgbPass>, |
| 1306 | pub(crate) coded: bool, | 1374 | pub(crate) coded: bool, |
| 1375 | + ring: Vec<RingSlot>, | ||
| 1376 | +} | ||
| 1377 | + | ||
| 1378 | +const RING_EXTRA: u32 = 8; | ||
| 1379 | + | ||
| 1380 | +struct RingSlot { | ||
| 1381 | + image: vk::Image, | ||
| 1382 | + memory: vk::DeviceMemory, | ||
| 1383 | + #[cfg(target_os = "linux")] | ||
| 1384 | + export: crate::dmabuf::Export, | ||
| 1307 | } | 1385 | } |
| 1308 | 1386 | ||
| 1309 | impl Decoder { | 1387 | impl Decoder { |
| @@ -1336,6 +1414,7 @@ impl Decoder { | |||
| 1336 | rgb_out: Default::default(), | 1414 | rgb_out: Default::default(), |
| 1337 | rgb: None, | 1415 | rgb: None, |
| 1338 | coded: false, | 1416 | coded: false, |
| 1417 | + ring: Vec::new(), | ||
| 1339 | }; | 1418 | }; |
| 1340 | let mut list = vk::VideoProfileListInfoKHR::builder() | 1419 | let mut list = vk::VideoProfileListInfoKHR::builder() |
| 1341 | .profiles(std::slice::from_ref(profile)) | 1420 | .profiles(std::slice::from_ref(profile)) |
| @@ -1395,6 +1474,11 @@ impl Decoder { | |||
| 1395 | (dec.rgb_out.buffer, rgb_size), | 1474 | (dec.rgb_out.buffer, rgb_size), |
| 1396 | )?); | 1475 | )?); |
| 1397 | 1476 | ||
| 1477 | + for _ in 0..max_dpb_slots + RING_EXTRA { | ||
| 1478 | + let slot = dec.ring_slot()?; | ||
| 1479 | + dec.ring.push(slot); | ||
| 1480 | + } | ||
| 1481 | + | ||
| 1398 | let d = gpu.device(); | 1482 | let d = gpu.device(); |
| 1399 | let cb = dec.decode.cb; | 1483 | let cb = dec.decode.cb; |
| 1400 | dec.decode.begin()?; | 1484 | dec.decode.begin()?; |
| @@ -1446,24 +1530,164 @@ impl Decoder { | |||
| 1446 | Ok(()) | 1530 | Ok(()) |
| 1447 | } | 1531 | } |
| 1448 | 1532 | ||
| 1449 | - /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | 1533 | + fn ring_slot(&self) -> Result<RingSlot, Error> { |
| 1534 | + let gpu = self.vs.gpu.clone(); | ||
| 1535 | + #[cfg(target_os = "linux")] | ||
| 1536 | + { | ||
| 1537 | + let (image, memory, export) = crate::dmabuf::ring_image( | ||
| 1538 | + &gpu, | ||
| 1539 | + &self.graphics, | ||
| 1540 | + self.vs.format, | ||
| 1541 | + (self.max_extent.width, self.max_extent.height), | ||
| 1542 | + )?; | ||
| 1543 | + Ok(RingSlot { | ||
| 1544 | + image, | ||
| 1545 | + memory, | ||
| 1546 | + export, | ||
| 1547 | + }) | ||
| 1548 | + } | ||
| 1549 | + #[cfg(not(target_os = "linux"))] | ||
| 1550 | + { | ||
| 1551 | + let d = gpu.device(); | ||
| 1552 | + let info = vk::ImageCreateInfo::builder() | ||
| 1553 | + .image_type(vk::ImageType::_2D) | ||
| 1554 | + .format(self.vs.format) | ||
| 1555 | + .extent(vk::Extent3D { | ||
| 1556 | + width: self.max_extent.width, | ||
| 1557 | + height: self.max_extent.height, | ||
| 1558 | + depth: 1, | ||
| 1559 | + }) | ||
| 1560 | + .mip_levels(1) | ||
| 1561 | + .array_layers(1) | ||
| 1562 | + .samples(vk::SampleCountFlags::_1) | ||
| 1563 | + .tiling(vk::ImageTiling::OPTIMAL) | ||
| 1564 | + .usage(vk::ImageUsageFlags::TRANSFER_SRC | vk::ImageUsageFlags::TRANSFER_DST) | ||
| 1565 | + .sharing_mode(vk::SharingMode::EXCLUSIVE) | ||
| 1566 | + .initial_layout(vk::ImageLayout::UNDEFINED) | ||
| 1567 | + .build(); | ||
| 1568 | + let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? }; | ||
| 1569 | + let req = unsafe { d.get_image_memory_requirements(image) }; | ||
| 1570 | + let index = memory_type(&gpu, req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)?; | ||
| 1571 | + let alloc = vk::MemoryAllocateInfo::builder() | ||
| 1572 | + .allocation_size(req.size) | ||
| 1573 | + .memory_type_index(index) | ||
| 1574 | + .build(); | ||
| 1575 | + let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? }; | ||
| 1576 | + unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory")? }; | ||
| 1577 | + self.graphics.begin()?; | ||
| 1578 | + init_barrier(d, self.graphics.cb, image, vk::ImageLayout::GENERAL); | ||
| 1579 | + self.graphics.end()?; | ||
| 1580 | + self.graphics.submit()?; | ||
| 1581 | + Ok(RingSlot { image, memory }) | ||
| 1582 | + } | ||
| 1583 | + } | ||
| 1584 | + | ||
| 1585 | + /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. | ||
| 1586 | + pub(crate) fn store(&mut self, slot: usize, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 1587 | + self.check_window(crop)?; | ||
| 1588 | + let (x, y, width, height) = crop; | ||
| 1589 | + let target = self.slot_image(slot)?; | ||
| 1590 | + let gpu = self.vs.gpu.clone(); | ||
| 1591 | + let d = gpu.device(); | ||
| 1592 | + let cb = self.graphics.cb; | ||
| 1593 | + let owner = ring_owner(&self.graphics); | ||
| 1594 | + let source = self.output.image; | ||
| 1595 | + let regions = [ | ||
| 1596 | + plane_copy(PLANE_0, (x, y), (width, height)), | ||
| 1597 | + plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), | ||
| 1598 | + ]; | ||
| 1599 | + self.graphics.begin()?; | ||
| 1600 | + unsafe { | ||
| 1601 | + transfer( | ||
| 1602 | + d, | ||
| 1603 | + cb, | ||
| 1604 | + source, | ||
| 1605 | + ( | ||
| 1606 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 1607 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 1608 | + vk::AccessFlags2::NONE, | ||
| 1609 | + vk::QUEUE_FAMILY_IGNORED, | ||
| 1610 | + ), | ||
| 1611 | + ( | ||
| 1612 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 1613 | + vk::PipelineStageFlags2::COPY, | ||
| 1614 | + vk::AccessFlags2::TRANSFER_READ, | ||
| 1615 | + vk::QUEUE_FAMILY_IGNORED, | ||
| 1616 | + ), | ||
| 1617 | + ); | ||
| 1618 | + transfer( | ||
| 1619 | + d, | ||
| 1620 | + cb, | ||
| 1621 | + target, | ||
| 1622 | + ( | ||
| 1623 | + vk::ImageLayout::GENERAL, | ||
| 1624 | + vk::PipelineStageFlags2::NONE, | ||
| 1625 | + vk::AccessFlags2::NONE, | ||
| 1626 | + EXTERNAL_FAMILY, | ||
| 1627 | + ), | ||
| 1628 | + ( | ||
| 1629 | + vk::ImageLayout::TRANSFER_DST_OPTIMAL, | ||
| 1630 | + vk::PipelineStageFlags2::COPY, | ||
| 1631 | + vk::AccessFlags2::TRANSFER_WRITE, | ||
| 1632 | + owner, | ||
| 1633 | + ), | ||
| 1634 | + ); | ||
| 1635 | + d.cmd_copy_image( | ||
| 1636 | + cb, | ||
| 1637 | + source, | ||
| 1638 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 1639 | + target, | ||
| 1640 | + vk::ImageLayout::TRANSFER_DST_OPTIMAL, | ||
| 1641 | + ®ions, | ||
| 1642 | + ); | ||
| 1643 | + transfer( | ||
| 1644 | + d, | ||
| 1645 | + cb, | ||
| 1646 | + target, | ||
| 1647 | + ( | ||
| 1648 | + vk::ImageLayout::TRANSFER_DST_OPTIMAL, | ||
| 1649 | + vk::PipelineStageFlags2::COPY, | ||
| 1650 | + vk::AccessFlags2::TRANSFER_WRITE, | ||
| 1651 | + owner, | ||
| 1652 | + ), | ||
| 1653 | + ( | ||
| 1654 | + vk::ImageLayout::GENERAL, | ||
| 1655 | + vk::PipelineStageFlags2::NONE, | ||
| 1656 | + vk::AccessFlags2::NONE, | ||
| 1657 | + EXTERNAL_FAMILY, | ||
| 1658 | + ), | ||
| 1659 | + ); | ||
| 1660 | + transfer( | ||
| 1661 | + d, | ||
| 1662 | + cb, | ||
| 1663 | + source, | ||
| 1664 | + ( | ||
| 1665 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 1666 | + vk::PipelineStageFlags2::COPY, | ||
| 1667 | + vk::AccessFlags2::TRANSFER_READ, | ||
| 1668 | + vk::QUEUE_FAMILY_IGNORED, | ||
| 1669 | + ), | ||
| 1670 | + ( | ||
| 1671 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 1672 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 1673 | + vk::AccessFlags2::NONE, | ||
| 1674 | + vk::QUEUE_FAMILY_IGNORED, | ||
| 1675 | + ), | ||
| 1676 | + ); | ||
| 1677 | + } | ||
| 1678 | + self.graphics.end()?; | ||
| 1679 | + self.graphics.submit() | ||
| 1680 | + } | ||
| 1681 | + | ||
| 1682 | + /// Copies the top-left width x height of ring slot `slot` out as NV12 or P010 planes (Y, interleaved UV). | ||
| 1450 | pub(crate) fn read_nv12( | 1683 | pub(crate) fn read_nv12( |
| 1451 | &mut self, | 1684 | &mut self, |
| 1685 | + slot: usize, | ||
| 1452 | width: u32, | 1686 | width: u32, |
| 1453 | height: u32, | 1687 | height: u32, |
| 1454 | ) -> Result<(Vec<u8>, Vec<u8>), Error> { | 1688 | ) -> Result<(Vec<u8>, Vec<u8>), Error> { |
| 1455 | - if !self.coded { | 1689 | + self.check_size((width, height))?; |
| 1456 | - return Err(Error::Invalid("nothing decoded".into())); | 1690 | + let target = self.slot_image(slot)?; |
| 1457 | - } | ||
| 1458 | - if width == 0 | ||
| 1459 | - || height == 0 | ||
| 1460 | - || width % 2 != 0 | ||
| 1461 | - || height % 2 != 0 | ||
| 1462 | - || width > self.max_extent.width | ||
| 1463 | - || height > self.max_extent.height | ||
| 1464 | - { | ||
| 1465 | - return Err(Error::Invalid("readback size".into())); | ||
| 1466 | - } | ||
| 1467 | let gpu = self.vs.gpu.clone(); | 1691 | let gpu = self.vs.gpu.clone(); |
| 1468 | let d = gpu.device(); | 1692 | let d = gpu.device(); |
| 1469 | let bytes = bytes_per_sample(self.vs.format); | 1693 | let bytes = bytes_per_sample(self.vs.format); |
| @@ -1473,40 +1697,28 @@ impl Decoder { | |||
| 1473 | copy_region(luma as u64, PLANE_1, width / 2, height / 2), | 1697 | copy_region(luma as u64, PLANE_1, width / 2, height / 2), |
| 1474 | ]; | 1698 | ]; |
| 1475 | self.graphics.begin()?; | 1699 | self.graphics.begin()?; |
| 1476 | - self.copy_to_buffer(d, self.graphics.cb, self.readback.buffer, &copies); | 1700 | + self.slot_to_buffer(d, self.graphics.cb, target, self.readback.buffer, &copies); |
| 1477 | self.graphics.end()?; | 1701 | self.graphics.end()?; |
| 1478 | self.graphics.submit()?; | 1702 | self.graphics.submit()?; |
| 1479 | let bytes = self.readback.bytes(d, luma * 3 / 2)?; | 1703 | let bytes = self.readback.bytes(d, luma * 3 / 2)?; |
| 1480 | Ok((bytes[..luma].to_vec(), bytes[luma..].to_vec())) | 1704 | Ok((bytes[..luma].to_vec(), bytes[luma..].to_vec())) |
| 1481 | } | 1705 | } |
| 1482 | 1706 | ||
| 1483 | - /// Copies the crop window `(x, y, width, height)` of the last decoded picture as packed 4-byte pixels, box-downscaled to `out` and converted to RGB on the GPU with the integer `coeffs` `[ky, rv, gu, gv, bu]`. `bgra` picks the byte order; alpha is 255. | 1707 | + /// Copies the crop window `(x, y, width, height)` of ring slot `slot` as packed 4-byte pixels, box-downscaled to `out` and converted to RGB on the GPU with the integer `coeffs` `[ky, rv, gu, gv, bu]`. `bgra` picks the byte order; alpha is 255. |
| 1484 | pub(crate) fn read_rgb( | 1708 | pub(crate) fn read_rgb( |
| 1485 | &mut self, | 1709 | &mut self, |
| 1710 | + slot: usize, | ||
| 1486 | crop: (u32, u32, u32, u32), | 1711 | crop: (u32, u32, u32, u32), |
| 1487 | out: (u32, u32), | 1712 | out: (u32, u32), |
| 1488 | bgra: bool, | 1713 | bgra: bool, |
| 1489 | coeffs: [i32; 5], | 1714 | coeffs: [i32; 5], |
| 1490 | ) -> Result<Vec<u8>, Error> { | 1715 | ) -> Result<Vec<u8>, Error> { |
| 1716 | + self.check_window(crop)?; | ||
| 1491 | let (x, y, width, height) = crop; | 1717 | let (x, y, width, height) = crop; |
| 1492 | - if !self.coded { | 1718 | + if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { |
| 1493 | - return Err(Error::Invalid("nothing decoded".into())); | ||
| 1494 | - } | ||
| 1495 | - if width == 0 | ||
| 1496 | - || height == 0 | ||
| 1497 | - || width % 2 != 0 | ||
| 1498 | - || height % 2 != 0 | ||
| 1499 | - || x % 2 != 0 | ||
| 1500 | - || y % 2 != 0 | ||
| 1501 | - || x + width > self.max_extent.width | ||
| 1502 | - || y + height > self.max_extent.height | ||
| 1503 | - || out.0 == 0 | ||
| 1504 | - || out.1 == 0 | ||
| 1505 | - || out.0 > width | ||
| 1506 | - || out.1 > height | ||
| 1507 | - { | ||
| 1508 | return Err(Error::Invalid("RGB readback size".into())); | 1719 | return Err(Error::Invalid("RGB readback size".into())); |
| 1509 | } | 1720 | } |
| 1721 | + let target = self.slot_image(slot)?; | ||
| 1510 | let pass = self | 1722 | let pass = self |
| 1511 | .rgb | 1723 | .rgb |
| 1512 | .as_ref() | 1724 | .as_ref() |
| @@ -1521,7 +1733,7 @@ impl Decoder { | |||
| 1521 | copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), | 1733 | copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), |
| 1522 | ]; | 1734 | ]; |
| 1523 | self.graphics.begin()?; | 1735 | self.graphics.begin()?; |
| 1524 | - self.copy_to_buffer(d, cb, self.planes.buffer, &copies); | 1736 | + self.slot_to_buffer(d, cb, target, self.planes.buffer, &copies); |
| 1525 | let barrier = vk::BufferMemoryBarrier2::builder() | 1737 | let barrier = vk::BufferMemoryBarrier2::builder() |
| 1526 | .src_stage_mask(vk::PipelineStageFlags2::COPY) | 1738 | .src_stage_mask(vk::PipelineStageFlags2::COPY) |
| 1527 | .src_access_mask(vk::AccessFlags2::TRANSFER_WRITE) | 1739 | .src_access_mask(vk::AccessFlags2::TRANSFER_WRITE) |
| @@ -1556,13 +1768,45 @@ impl Decoder { | |||
| 1556 | Ok(self.rgb_out.bytes(d, len)?.to_vec()) | 1768 | Ok(self.rgb_out.bytes(d, len)?.to_vec()) |
| 1557 | } | 1769 | } |
| 1558 | 1770 | ||
| 1559 | - /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. | 1771 | + /// The DMA-BUF of ring slot `slot`, `width` x `height` from its top-left: a second handle on the slot's memory, which the same slot returns every time. |
| 1560 | #[cfg(target_os = "linux")] | 1772 | #[cfg(target_os = "linux")] |
| 1561 | pub(crate) fn export( | 1773 | pub(crate) fn export( |
| 1562 | - &mut self, | 1774 | + &self, |
| 1563 | - crop: (u32, u32, u32, u32), | 1775 | + slot: usize, |
| 1776 | + (width, height): (u32, u32), | ||
| 1564 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 1777 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 1565 | - let (x, y, width, height) = crop; | 1778 | + self.check_size((width, height))?; |
| 1779 | + let ring = self | ||
| 1780 | + .ring | ||
| 1781 | + .get(slot) | ||
| 1782 | + .ok_or_else(|| Error::Invalid("ring slot out of range".into()))?; | ||
| 1783 | + let fd = ring | ||
| 1784 | + .export | ||
| 1785 | + .fd | ||
| 1786 | + .try_clone() | ||
| 1787 | + .map_err(|e| Error::Invalid(format!("duplicating DMA-BUF: {e}")))?; | ||
| 1788 | + Ok(crate::dmabuf::DmaBufExport { | ||
| 1789 | + fd, | ||
| 1790 | + modifier: ring.export.modifier, | ||
| 1791 | + planes: ring.export.planes, | ||
| 1792 | + width, | ||
| 1793 | + height, | ||
| 1794 | + p010: self.vs.format == P010, | ||
| 1795 | + }) | ||
| 1796 | + } | ||
| 1797 | + | ||
| 1798 | + pub(crate) fn ring_len(&self) -> u32 { | ||
| 1799 | + self.ring.len() as u32 | ||
| 1800 | + } | ||
| 1801 | + | ||
| 1802 | + /// Adds one slot to the ring when every slot is held, and returns its index. | ||
| 1803 | + pub(crate) fn add_ring_slot(&mut self) -> Result<u32, Error> { | ||
| 1804 | + let slot = self.ring_slot()?; | ||
| 1805 | + self.ring.push(slot); | ||
| 1806 | + Ok(self.ring.len() as u32 - 1) | ||
| 1807 | + } | ||
| 1808 | + | ||
| 1809 | + fn check_size(&self, (width, height): (u32, u32)) -> Result<(), Error> { | ||
| 1566 | if !self.coded { | 1810 | if !self.coded { |
| 1567 | return Err(Error::Invalid("nothing decoded".into())); | 1811 | return Err(Error::Invalid("nothing decoded".into())); |
| 1568 | } | 1812 | } |
| @@ -1570,49 +1814,74 @@ impl Decoder { | |||
| 1570 | || height == 0 | 1814 | || height == 0 |
| 1571 | || width % 2 != 0 | 1815 | || width % 2 != 0 |
| 1572 | || height % 2 != 0 | 1816 | || height % 2 != 0 |
| 1573 | - || x % 2 != 0 | 1817 | + || width > self.max_extent.width |
| 1574 | - || y % 2 != 0 | 1818 | + || height > self.max_extent.height |
| 1575 | - || x + width > self.max_extent.width | ||
| 1576 | - || y + height > self.max_extent.height | ||
| 1577 | { | 1819 | { |
| 1578 | - return Err(Error::Invalid("DMA-BUF export size".into())); | 1820 | + return Err(Error::Invalid("picture size".into())); |
| 1579 | } | 1821 | } |
| 1580 | - let gpu = self.vs.gpu.clone(); | 1822 | + Ok(()) |
| 1581 | - crate::dmabuf::export(&gpu, &self.graphics, self.output.image, self.vs.format, crop) | 1823 | + } |
| 1824 | + | ||
| 1825 | + fn check_window(&self, (x, y, width, height): (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 1826 | + self.check_size((width, height))?; | ||
| 1827 | + if x % 2 != 0 || y % 2 != 0 || x + width > self.max_extent.width || y + height > self.max_extent.height { | ||
| 1828 | + return Err(Error::Invalid("picture window".into())); | ||
| 1829 | + } | ||
| 1830 | + Ok(()) | ||
| 1582 | } | 1831 | } |
| 1583 | 1832 | ||
| 1584 | - fn copy_to_buffer( | 1833 | + fn slot_image(&self, slot: usize) -> Result<vk::Image, Error> { |
| 1834 | + self.ring | ||
| 1835 | + .get(slot) | ||
| 1836 | + .map(|s| s.image) | ||
| 1837 | + .ok_or_else(|| Error::Invalid("ring slot out of range".into())) | ||
| 1838 | + } | ||
| 1839 | + | ||
| 1840 | + fn slot_to_buffer( | ||
| 1585 | &self, | 1841 | &self, |
| 1586 | d: &Device, | 1842 | d: &Device, |
| 1587 | cb: vk::CommandBuffer, | 1843 | cb: vk::CommandBuffer, |
| 1844 | + image: vk::Image, | ||
| 1588 | target: vk::Buffer, | 1845 | target: vk::Buffer, |
| 1589 | copies: &[vk::BufferImageCopy], | 1846 | copies: &[vk::BufferImageCopy], |
| 1590 | ) { | 1847 | ) { |
| 1591 | - let image = self.output.image; | 1848 | + let owner = ring_owner(&self.graphics); |
| 1592 | unsafe { | 1849 | unsafe { |
| 1593 | - barrier( | 1850 | + slot_acquire(d, cb, image, owner); |
| 1851 | + d.cmd_copy_image_to_buffer( | ||
| 1852 | + cb, | ||
| 1853 | + image, | ||
| 1854 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 1855 | + target, | ||
| 1856 | + copies, | ||
| 1857 | + ); | ||
| 1858 | + slot_release(d, cb, image, owner); | ||
| 1859 | + } | ||
| 1860 | + } | ||
| 1861 | +} | ||
| 1862 | + | ||
| 1863 | +fn slot_acquire(d: &Device, cb: vk::CommandBuffer, image: vk::Image, owner: u32) { | ||
| 1864 | + transfer( | ||
| 1594 | d, | 1865 | d, |
| 1595 | cb, | 1866 | cb, |
| 1596 | image, | 1867 | image, |
| 1597 | ( | 1868 | ( |
| 1598 | - vk::ImageLayout::VIDEO_DECODE_DST_KHR, | 1869 | + vk::ImageLayout::GENERAL, |
| 1599 | - vk::PipelineStageFlags2::ALL_COMMANDS, | 1870 | + vk::PipelineStageFlags2::NONE, |
| 1600 | vk::AccessFlags2::NONE, | 1871 | vk::AccessFlags2::NONE, |
| 1872 | + EXTERNAL_FAMILY, | ||
| 1601 | ), | 1873 | ), |
| 1602 | ( | 1874 | ( |
| 1603 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | 1875 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
| 1604 | vk::PipelineStageFlags2::COPY, | 1876 | vk::PipelineStageFlags2::COPY, |
| 1605 | vk::AccessFlags2::TRANSFER_READ, | 1877 | vk::AccessFlags2::TRANSFER_READ, |
| 1878 | + owner, | ||
| 1606 | ), | 1879 | ), |
| 1607 | ); | 1880 | ); |
| 1608 | - d.cmd_copy_image_to_buffer( | 1881 | +} |
| 1609 | - cb, | 1882 | + |
| 1610 | - image, | 1883 | +fn slot_release(d: &Device, cb: vk::CommandBuffer, image: vk::Image, owner: u32) { |
| 1611 | - vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | 1884 | + transfer( |
| 1612 | - target, | ||
| 1613 | - copies, | ||
| 1614 | - ); | ||
| 1615 | - barrier( | ||
| 1616 | d, | 1885 | d, |
| 1617 | cb, | 1886 | cb, |
| 1618 | image, | 1887 | image, |
| @@ -1620,16 +1889,16 @@ impl Decoder { | |||
| 1620 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | 1889 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
| 1621 | vk::PipelineStageFlags2::COPY, | 1890 | vk::PipelineStageFlags2::COPY, |
| 1622 | vk::AccessFlags2::TRANSFER_READ, | 1891 | vk::AccessFlags2::TRANSFER_READ, |
| 1892 | + owner, | ||
| 1623 | ), | 1893 | ), |
| 1624 | ( | 1894 | ( |
| 1625 | - vk::ImageLayout::VIDEO_DECODE_DST_KHR, | 1895 | + vk::ImageLayout::GENERAL, |
| 1626 | - vk::PipelineStageFlags2::ALL_COMMANDS, | 1896 | + vk::PipelineStageFlags2::NONE, |
| 1627 | vk::AccessFlags2::NONE, | 1897 | vk::AccessFlags2::NONE, |
| 1898 | + EXTERNAL_FAMILY, | ||
| 1628 | ), | 1899 | ), |
| 1629 | ); | 1900 | ); |
| 1630 | } | 1901 | } |
| 1631 | - } | ||
| 1632 | -} | ||
| 1633 | 1902 | ||
| 1634 | impl Drop for Decoder { | 1903 | impl Drop for Decoder { |
| 1635 | fn drop(&mut self) { | 1904 | fn drop(&mut self) { |
| @@ -1641,6 +1910,12 @@ impl Drop for Decoder { | |||
| 1641 | for img in self.dpb.iter().chain(std::iter::once(&self.output)) { | 1910 | for img in self.dpb.iter().chain(std::iter::once(&self.output)) { |
| 1642 | img.destroy(d); | 1911 | img.destroy(d); |
| 1643 | } | 1912 | } |
| 1913 | + for slot in &self.ring { | ||
| 1914 | + unsafe { | ||
| 1915 | + d.destroy_image(slot.image, None); | ||
| 1916 | + d.free_memory(slot.memory, None); | ||
| 1917 | + } | ||
| 1918 | + } | ||
| 1644 | self.bitstream.destroy(d); | 1919 | self.bitstream.destroy(d); |
| 1645 | self.readback.destroy(d); | 1920 | self.readback.destroy(d); |
| 1646 | self.planes.destroy(d); | 1921 | self.planes.destroy(d); |
shims/vk-video-ffi/src/vp9_decode.rsmodified+23 -5
| @@ -413,28 +413,46 @@ impl Vp9DecodeSession { | |||
| 413 | } | 413 | } |
| 414 | 414 | ||
| 415 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | 415 | /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). |
| 416 | - pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | 416 | + pub fn ring_len(&self) -> u32 { |
| 417 | - self.dec.read_nv12(width, height) | 417 | + self.dec.ring_len() |
| 418 | + } | ||
| 419 | + | ||
| 420 | + /// Adds one slot to the output ring and returns its index; every slot is then in use or held. | ||
| 421 | + pub fn add_ring_slot(&mut self) -> Result<u32, Error> { | ||
| 422 | + self.dec.add_ring_slot() | ||
| 423 | + } | ||
| 424 | + | ||
| 425 | + /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. | ||
| 426 | + pub fn store_output(&mut self, slot: u32, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | ||
| 427 | + self.dec.store(slot as usize, crop) | ||
| 428 | + } | ||
| 429 | + | ||
| 430 | + /// The top-left width x height of ring slot `slot` as NV12 planes (Y, interleaved UV). | ||
| 431 | + pub fn read_output(&mut self, slot: u32, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | ||
| 432 | + self.dec.read_nv12(slot as usize, width, height) | ||
| 418 | } | 433 | } |
| 419 | 434 | ||
| 420 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. | 435 | /// Exports the crop window `(x, y, width, height)` of the last decoded picture as one DMA-BUF of NV12 or P010 planes. |
| 421 | #[cfg(target_os = "linux")] | 436 | #[cfg(target_os = "linux")] |
| 422 | pub fn read_output_dmabuf( | 437 | pub fn read_output_dmabuf( |
| 423 | &mut self, | 438 | &mut self, |
| 424 | - crop: (u32, u32, u32, u32), | 439 | + slot: u32, |
| 440 | + width: u32, | ||
| 441 | + height: u32, | ||
| 425 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 442 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 426 | - self.dec.export(crop) | 443 | + self.dec.export(slot as usize, (width, height)) |
| 427 | } | 444 | } |
| 428 | 445 | ||
| 429 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. | 446 | /// Packed 4-byte pixels of the crop window `(x, y, width, height)` of the last decoded picture, box-downscaled to `out`, converted to RGB with integer `coeffs` `[ky, rv, gu, gv, bu]`; `bgra` picks the byte order. |
| 430 | pub fn read_output_rgb( | 447 | pub fn read_output_rgb( |
| 431 | &mut self, | 448 | &mut self, |
| 449 | + slot: u32, | ||
| 432 | crop: (u32, u32, u32, u32), | 450 | crop: (u32, u32, u32, u32), |
| 433 | out: (u32, u32), | 451 | out: (u32, u32), |
| 434 | bgra: bool, | 452 | bgra: bool, |
| 435 | coeffs: [i32; 5], | 453 | coeffs: [i32; 5], |
| 436 | ) -> Result<Vec<u8>, Error> { | 454 | ) -> Result<Vec<u8>, Error> { |
| 437 | - self.dec.read_rgb(crop, out, bgra, coeffs) | 455 | + self.dec.read_rgb(slot as usize, crop, out, bgra, coeffs) |
| 438 | } | 456 | } |
| 439 | } | 457 | } |
| 440 | 458 | ||