edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Safe Vulkan Video over vulkanalia: capability probe and H.264 encode. |
| 2 | #![deny(missing_docs)] |
| 3 | |
| 4 | use std::collections::BTreeSet; |
| 5 | use std::ffi::c_char; |
| 6 | use std::fmt; |
| 7 | use std::sync::Arc; |
| 8 | |
| 9 | mod av1_decode; |
| 10 | mod av1_encode; |
| 11 | #[cfg(windows)] |
| 12 | mod d3d11; |
| 13 | #[cfg(target_os = "linux")] |
| 14 | mod dmabuf; |
| 15 | mod h264_decode; |
| 16 | mod h264_encode; |
| 17 | mod h265_decode; |
| 18 | mod h265_encode; |
| 19 | mod rgb_convert; |
| 20 | mod rgb_decode; |
| 21 | mod session; |
| 22 | mod vp9_decode; |
| 23 | |
| 24 | pub use av1_decode::{ |
| 25 | Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams, |
| 26 | Av1FrameHeader, Av1FrameType, Av1GlobalMotion, Av1InterpolationFilter, Av1LoopFilter, |
| 27 | Av1LoopRestoration, Av1Quantization, Av1RefSlot, Av1Restoration, Av1Segmentation, |
| 28 | Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, |
| 29 | }; |
| 30 | pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 31 | pub use session::DPB_SLOTS; |
| 32 | #[cfg(windows)] |
| 33 | pub use d3d11::{D3d11Pool, D3d11Texture, d3d11_importable}; |
| 34 | #[cfg(windows)] |
| 35 | pub use windows; |
| 36 | #[cfg(target_os = "linux")] |
| 37 | pub use dmabuf::{DmaBuf, DmaBufExport, DmaBufFormat, DmaBufPlane, dup_raw_fd, read_dmabuf, rgb_import_modifiers}; |
| 38 | pub use vp9_decode::{ |
| 39 | Vp9ColorConfig, Vp9DecodeConfig, Vp9DecodePicture, Vp9DecodeSession, Vp9FrameHeader, |
| 40 | Vp9InterpolationFilter, Vp9LoopFilter, Vp9Segmentation, |
| 41 | }; |
| 42 | pub use h264_decode::{ |
| 43 | H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, H264RefSlot, H264Sps, |
| 44 | ScalingLists, |
| 45 | }; |
| 46 | pub use h264_encode::{ |
| 47 | H264EncodeConfig, H264EncodeSession, H264Picture, H264Profile, H264Ref, LOG2_MAX_FRAME_NUM, |
| 48 | RateControl, |
| 49 | }; |
| 50 | pub use h265_decode::{ |
| 51 | H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, H265LongTermRefPicSps, |
| 52 | H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, H265ScalingLists, H265ShortTermRefPicSet, |
| 53 | H265Sps, H265Vps, |
| 54 | }; |
| 55 | pub use h265_encode::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; |
| 56 | |
| 57 | /// Colour description written into the VUI (H.264, H.265) or the colour config (AV1). |
| 58 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 59 | pub struct Colour { |
| 60 | /// `colour_primaries` (H.273 code points). |
| 61 | pub primaries: u8, |
| 62 | /// `transfer_characteristics`. |
| 63 | pub transfer: u8, |
| 64 | /// `matrix_coefficients`. |
| 65 | pub matrix: u8, |
| 66 | /// `video_full_range_flag` / `color_range`. |
| 67 | pub full_range: bool, |
| 68 | } |
| 69 | |
| 70 | use vk::KhrVideoQueueExtensionInstanceCommands; |
| 71 | use vulkanalia::loader::LibloadingLoader; |
| 72 | use vulkanalia::prelude::v1_3::*; |
| 73 | |
| 74 | const WANTED: [vk::Extension; 16] = [ |
| 75 | vk::KHR_VIDEO_QUEUE_EXTENSION, |
| 76 | vk::KHR_VIDEO_ENCODE_QUEUE_EXTENSION, |
| 77 | vk::KHR_VIDEO_DECODE_QUEUE_EXTENSION, |
| 78 | vk::KHR_VIDEO_ENCODE_H264_EXTENSION, |
| 79 | vk::KHR_VIDEO_ENCODE_H265_EXTENSION, |
| 80 | vk::KHR_VIDEO_ENCODE_AV1_EXTENSION, |
| 81 | vk::KHR_VIDEO_DECODE_H264_EXTENSION, |
| 82 | vk::KHR_VIDEO_DECODE_H265_EXTENSION, |
| 83 | vk::KHR_VIDEO_DECODE_AV1_EXTENSION, |
| 84 | vk::KHR_VIDEO_DECODE_VP9_EXTENSION, |
| 85 | vk::KHR_VIDEO_MAINTENANCE1_EXTENSION, |
| 86 | vk::EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION, |
| 87 | vk::KHR_EXTERNAL_MEMORY_FD_EXTENSION, |
| 88 | vk::EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION, |
| 89 | vk::EXT_QUEUE_FAMILY_FOREIGN_EXTENSION, |
| 90 | vk::KHR_EXTERNAL_MEMORY_WIN32_EXTENSION, |
| 91 | ]; |
| 92 | |
| 93 | /// Errors from opening a GPU or probing it. |
| 94 | #[derive(Debug)] |
| 95 | pub enum Error { |
| 96 | /// The Vulkan loader library could not be loaded. |
| 97 | NoLoader(String), |
| 98 | /// No physical device has a usable video queue family. |
| 99 | NoDevice, |
| 100 | /// A Vulkan call failed: VkResult code and call name. |
| 101 | Vk(i32, &'static str), |
| 102 | /// The device was lost (VK_ERROR_DEVICE_LOST): a GPU reset or a hung queue. Every object on it is dead. |
| 103 | DeviceLost(&'static str), |
| 104 | /// The requested codec or direction is not supported. |
| 105 | Unsupported(String), |
| 106 | /// The caller passed an invalid picture, buffer or sequence state. |
| 107 | Invalid(String), |
| 108 | } |
| 109 | |
| 110 | impl fmt::Display for Error { |
| 111 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 | match self { |
| 113 | Error::NoLoader(e) => write!(f, "no Vulkan loader: {e}"), |
| 114 | Error::NoDevice => write!(f, "no Vulkan Video device"), |
| 115 | Error::Vk(code, call) => write!(f, "{call} failed: VkResult {code}"), |
| 116 | Error::DeviceLost(call) => write!(f, "{call} failed: device lost"), |
| 117 | Error::Unsupported(what) => write!(f, "unsupported: {what}"), |
| 118 | Error::Invalid(what) => write!(f, "invalid: {what}"), |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl std::error::Error for Error {} |
| 124 | |
| 125 | /// Video codec. |
| 126 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 127 | pub enum Codec { |
| 128 | /// H.264 / AVC. |
| 129 | H264, |
| 130 | /// H.265 / HEVC. |
| 131 | H265, |
| 132 | /// AV1. |
| 133 | Av1, |
| 134 | /// VP9. |
| 135 | Vp9, |
| 136 | } |
| 137 | |
| 138 | /// Video direction. |
| 139 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 140 | pub enum Direction { |
| 141 | /// Encode. |
| 142 | Encode, |
| 143 | /// Decode. |
| 144 | Decode, |
| 145 | } |
| 146 | |
| 147 | /// Profile a capability is queried for, 4:2:0 only. |
| 148 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 149 | pub enum CodecProfile { |
| 150 | /// H.264 Constrained Baseline, 8-bit. |
| 151 | H264ConstrainedBaseline, |
| 152 | /// H.264 Main, 8-bit. |
| 153 | H264Main, |
| 154 | /// H.264 High, 8-bit. |
| 155 | H264High, |
| 156 | /// H.265 Main, 8-bit. |
| 157 | H265Main, |
| 158 | /// H.265 Main 10, 10-bit. |
| 159 | H265Main10, |
| 160 | /// AV1 Main, 8-bit. |
| 161 | Av1Main, |
| 162 | /// AV1 Main, 10-bit. |
| 163 | Av1Main10, |
| 164 | /// VP9 Profile 0, 8-bit. |
| 165 | Vp9Profile0, |
| 166 | /// VP9 Profile 2, 10-bit. |
| 167 | Vp9Profile2, |
| 168 | } |
| 169 | |
| 170 | /// Capabilities of one codec, profile and direction. |
| 171 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 172 | pub struct CodecCaps { |
| 173 | /// Codec. |
| 174 | pub codec: Codec, |
| 175 | /// Direction. |
| 176 | pub direction: Direction, |
| 177 | /// Profile. |
| 178 | pub profile: CodecProfile, |
| 179 | /// Maximum coded width. |
| 180 | pub max_width: u32, |
| 181 | /// Maximum coded height. |
| 182 | pub max_height: u32, |
| 183 | /// Maximum DPB slots. |
| 184 | pub max_dpb_slots: u32, |
| 185 | /// Maximum active reference pictures. |
| 186 | pub max_active_refs: u32, |
| 187 | } |
| 188 | |
| 189 | /// An opened Vulkan device with video queues. Cheap to clone; the device lives until the last clone drops. |
| 190 | #[derive(Clone)] |
| 191 | pub struct Gpu { |
| 192 | inner: Arc<Inner>, |
| 193 | } |
| 194 | |
| 195 | struct Inner { |
| 196 | device: Device, |
| 197 | instance: Instance, |
| 198 | physical: vk::PhysicalDevice, |
| 199 | name: String, |
| 200 | vendor_id: u32, |
| 201 | encode: Option<u32>, |
| 202 | decode: Option<u32>, |
| 203 | graphics: Option<u32>, |
| 204 | _entry: Entry, |
| 205 | } |
| 206 | |
| 207 | struct Families { |
| 208 | encode: Option<u32>, |
| 209 | decode: Option<u32>, |
| 210 | graphics: Option<u32>, |
| 211 | } |
| 212 | |
| 213 | fn queue_families(instance: &Instance, physical: vk::PhysicalDevice) -> Families { |
| 214 | let flags: Vec<vk::QueueFlags> = |
| 215 | unsafe { instance.get_physical_device_queue_family_properties(physical) } |
| 216 | .iter() |
| 217 | .map(|p| p.queue_flags) |
| 218 | .collect(); |
| 219 | let find = |pred: &dyn Fn(vk::QueueFlags) -> bool| { |
| 220 | flags.iter().position(|&f| pred(f)).map(|i| i as u32) |
| 221 | }; |
| 222 | Families { |
| 223 | encode: find(&|f| f.contains(vk::QueueFlags::VIDEO_ENCODE_KHR)), |
| 224 | decode: find(&|f| f.contains(vk::QueueFlags::VIDEO_DECODE_KHR)), |
| 225 | graphics: find(&|f| { |
| 226 | f.contains(vk::QueueFlags::GRAPHICS) || f.contains(vk::QueueFlags::COMPUTE) |
| 227 | }), |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | fn setup(instance: &Instance) -> Result<(vk::PhysicalDevice, Device, String, u32, Families), Error> { |
| 232 | let devices = unsafe { instance.enumerate_physical_devices() } |
| 233 | .map_err(|c| crate::session::vk_error(c, "vkEnumeratePhysicalDevices"))?; |
| 234 | let (physical, families) = devices |
| 235 | .into_iter() |
| 236 | .find_map(|pd| { |
| 237 | let f = queue_families(instance, pd); |
| 238 | (f.encode.is_some() || f.decode.is_some()).then_some((pd, f)) |
| 239 | }) |
| 240 | .ok_or(Error::NoDevice)?; |
| 241 | let graphics = families.graphics.ok_or(Error::NoDevice)?; |
| 242 | |
| 243 | let mut indices = BTreeSet::from([graphics]); |
| 244 | indices.extend(families.encode); |
| 245 | indices.extend(families.decode); |
| 246 | let priority = [1.0f32]; |
| 247 | let queues: Vec<vk::DeviceQueueCreateInfo> = indices |
| 248 | .iter() |
| 249 | .map(|&index| { |
| 250 | vk::DeviceQueueCreateInfo::builder() |
| 251 | .queue_family_index(index) |
| 252 | .queue_priorities(&priority) |
| 253 | .build() |
| 254 | }) |
| 255 | .collect(); |
| 256 | |
| 257 | let available: Vec<vk::ExtensionName> = |
| 258 | unsafe { instance.enumerate_device_extension_properties(physical, None) } |
| 259 | .map_err(|c| crate::session::vk_error(c, "vkEnumerateDeviceExtensionProperties"))? |
| 260 | .iter() |
| 261 | .map(|e| e.extension_name) |
| 262 | .collect(); |
| 263 | let enabled: Vec<vk::ExtensionName> = WANTED |
| 264 | .iter() |
| 265 | .map(|e| e.name) |
| 266 | .filter(|n| available.contains(n)) |
| 267 | .collect(); |
| 268 | let names: Vec<*const c_char> = enabled.iter().map(|n| n.as_cstr().as_ptr()).collect(); |
| 269 | |
| 270 | let mut sync2 = vk::PhysicalDeviceSynchronization2Features::builder(); |
| 271 | let mut ycbcr = vk::PhysicalDeviceSamplerYcbcrConversionFeatures::builder(); |
| 272 | let mut maint = vk::PhysicalDeviceVideoMaintenance1FeaturesKHR::builder(); |
| 273 | let mut av1_enc = vk::PhysicalDeviceVideoEncodeAV1FeaturesKHR::builder(); |
| 274 | let mut vp9_dec = vk::PhysicalDeviceVideoDecodeVP9FeaturesKHR::builder(); |
| 275 | let mut features = vk::PhysicalDeviceFeatures2::builder() |
| 276 | .push_next(&mut sync2) |
| 277 | .push_next(&mut ycbcr); |
| 278 | if enabled.contains(&vk::KHR_VIDEO_MAINTENANCE1_EXTENSION.name) { |
| 279 | features = features.push_next(&mut maint); |
| 280 | } |
| 281 | if enabled.contains(&vk::KHR_VIDEO_ENCODE_AV1_EXTENSION.name) { |
| 282 | features = features.push_next(&mut av1_enc); |
| 283 | } |
| 284 | if enabled.contains(&vk::KHR_VIDEO_DECODE_VP9_EXTENSION.name) { |
| 285 | features = features.push_next(&mut vp9_dec); |
| 286 | } |
| 287 | unsafe { instance.get_physical_device_features2(physical, &mut features) }; |
| 288 | |
| 289 | let info = vk::DeviceCreateInfo::builder() |
| 290 | .queue_create_infos(&queues) |
| 291 | .enabled_extension_names(&names) |
| 292 | .push_next(&mut features); |
| 293 | let device = unsafe { instance.create_device(physical, &info, None) } |
| 294 | .map_err(|c| crate::session::vk_error(c, "vkCreateDevice"))?; |
| 295 | |
| 296 | let properties = unsafe { instance.get_physical_device_properties(physical) }; |
| 297 | let name = properties.device_name.to_string_lossy().into_owned(); |
| 298 | Ok((physical, device, name, properties.vendor_id, families)) |
| 299 | } |
| 300 | |
| 301 | impl Gpu { |
| 302 | /// Loads Vulkan and opens the first device with a video queue family. |
| 303 | pub fn open() -> Result<Gpu, Error> { |
| 304 | let library = if cfg!(windows) { |
| 305 | "vulkan-1.dll" |
| 306 | } else { |
| 307 | "libvulkan.so.1" |
| 308 | }; |
| 309 | let loader = unsafe { LibloadingLoader::new(library) } |
| 310 | .map_err(|e| Error::NoLoader(e.to_string()))?; |
| 311 | let entry = unsafe { Entry::new(loader) }.map_err(|e| Error::NoLoader(e.to_string()))?; |
| 312 | let app = vk::ApplicationInfo::builder().api_version(vk::make_version(1, 3, 0)); |
| 313 | let info = vk::InstanceCreateInfo::builder().application_info(&app); |
| 314 | let instance = unsafe { entry.create_instance(&info, None) } |
| 315 | .map_err(|c| crate::session::vk_error(c, "vkCreateInstance"))?; |
| 316 | match setup(&instance) { |
| 317 | Ok((physical, device, name, vendor_id, families)) => Ok(Gpu { |
| 318 | inner: Arc::new(Inner { |
| 319 | device, |
| 320 | instance, |
| 321 | physical, |
| 322 | name, |
| 323 | vendor_id, |
| 324 | encode: families.encode, |
| 325 | decode: families.decode, |
| 326 | graphics: families.graphics, |
| 327 | _entry: entry, |
| 328 | }), |
| 329 | }), |
| 330 | Err(e) => { |
| 331 | unsafe { instance.destroy_instance(None) }; |
| 332 | Err(e) |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /// Device name as reported by the driver. |
| 338 | pub fn device_name(&self) -> String { |
| 339 | self.inner.name.clone() |
| 340 | } |
| 341 | |
| 342 | /// PCI vendor ID of the device, 0x1002 for AMD. |
| 343 | pub fn vendor_id(&self) -> u32 { |
| 344 | self.inner.vendor_id |
| 345 | } |
| 346 | |
| 347 | /// Vulkan instance, physical device and device, for examples that build their own images. |
| 348 | #[doc(hidden)] |
| 349 | pub fn vulkan(&self) -> (&Instance, vk::PhysicalDevice, &Device) { |
| 350 | ( |
| 351 | &self.inner.instance, |
| 352 | self.inner.physical, |
| 353 | &self.inner.device, |
| 354 | ) |
| 355 | } |
| 356 | |
| 357 | pub(crate) fn device(&self) -> &Device { |
| 358 | &self.inner.device |
| 359 | } |
| 360 | |
| 361 | pub(crate) fn instance(&self) -> &Instance { |
| 362 | &self.inner.instance |
| 363 | } |
| 364 | |
| 365 | pub(crate) fn physical(&self) -> vk::PhysicalDevice { |
| 366 | self.inner.physical |
| 367 | } |
| 368 | |
| 369 | pub(crate) fn encode_family(&self) -> Option<u32> { |
| 370 | self.inner.encode |
| 371 | } |
| 372 | |
| 373 | pub(crate) fn decode_family(&self) -> Option<u32> { |
| 374 | self.inner.decode |
| 375 | } |
| 376 | |
| 377 | pub(crate) fn graphics_family(&self) -> Option<u32> { |
| 378 | self.inner.graphics |
| 379 | } |
| 380 | |
| 381 | /// Capabilities of every codec, profile and direction the device supports. |
| 382 | pub fn caps(&self) -> Vec<CodecCaps> { |
| 383 | [ |
| 384 | (Codec::H264, Direction::Encode, CodecProfile::H264ConstrainedBaseline), |
| 385 | (Codec::H264, Direction::Encode, CodecProfile::H264Main), |
| 386 | (Codec::H264, Direction::Encode, CodecProfile::H264High), |
| 387 | (Codec::H264, Direction::Decode, CodecProfile::H264ConstrainedBaseline), |
| 388 | (Codec::H264, Direction::Decode, CodecProfile::H264Main), |
| 389 | (Codec::H264, Direction::Decode, CodecProfile::H264High), |
| 390 | (Codec::H265, Direction::Encode, CodecProfile::H265Main), |
| 391 | (Codec::H265, Direction::Encode, CodecProfile::H265Main10), |
| 392 | (Codec::H265, Direction::Decode, CodecProfile::H265Main), |
| 393 | (Codec::H265, Direction::Decode, CodecProfile::H265Main10), |
| 394 | (Codec::Av1, Direction::Encode, CodecProfile::Av1Main), |
| 395 | (Codec::Av1, Direction::Encode, CodecProfile::Av1Main10), |
| 396 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main), |
| 397 | (Codec::Av1, Direction::Decode, CodecProfile::Av1Main10), |
| 398 | (Codec::Vp9, Direction::Decode, CodecProfile::Vp9Profile0), |
| 399 | (Codec::Vp9, Direction::Decode, CodecProfile::Vp9Profile2), |
| 400 | ] |
| 401 | .into_iter() |
| 402 | .filter_map(|(codec, direction, profile)| { |
| 403 | self.codec_caps(codec, direction, profile).ok() |
| 404 | }) |
| 405 | .collect() |
| 406 | } |
| 407 | |
| 408 | fn codec_caps( |
| 409 | &self, |
| 410 | codec: Codec, |
| 411 | direction: Direction, |
| 412 | profile: CodecProfile, |
| 413 | ) -> Result<CodecCaps, Error> { |
| 414 | let (operation, extension) = match (codec, direction) { |
| 415 | (Codec::H264, Direction::Encode) => ( |
| 416 | vk::VideoCodecOperationFlagsKHR::ENCODE_H264, |
| 417 | vk::KHR_VIDEO_ENCODE_H264_EXTENSION, |
| 418 | ), |
| 419 | (Codec::H264, Direction::Decode) => ( |
| 420 | vk::VideoCodecOperationFlagsKHR::DECODE_H264, |
| 421 | vk::KHR_VIDEO_DECODE_H264_EXTENSION, |
| 422 | ), |
| 423 | (Codec::H265, Direction::Encode) => ( |
| 424 | vk::VideoCodecOperationFlagsKHR::ENCODE_H265, |
| 425 | vk::KHR_VIDEO_ENCODE_H265_EXTENSION, |
| 426 | ), |
| 427 | (Codec::H265, Direction::Decode) => ( |
| 428 | vk::VideoCodecOperationFlagsKHR::DECODE_H265, |
| 429 | vk::KHR_VIDEO_DECODE_H265_EXTENSION, |
| 430 | ), |
| 431 | (Codec::Av1, Direction::Encode) => ( |
| 432 | vk::VideoCodecOperationFlagsKHR::ENCODE_AV1, |
| 433 | vk::KHR_VIDEO_ENCODE_AV1_EXTENSION, |
| 434 | ), |
| 435 | (Codec::Av1, Direction::Decode) => ( |
| 436 | vk::VideoCodecOperationFlagsKHR::DECODE_AV1, |
| 437 | vk::KHR_VIDEO_DECODE_AV1_EXTENSION, |
| 438 | ), |
| 439 | (Codec::Vp9, Direction::Decode) => ( |
| 440 | vk::VideoCodecOperationFlagsKHR::DECODE_VP9, |
| 441 | vk::KHR_VIDEO_DECODE_VP9_EXTENSION, |
| 442 | ), |
| 443 | (Codec::Vp9, Direction::Encode) => { |
| 444 | return Err(Error::Unsupported("VP9 encode".into())); |
| 445 | } |
| 446 | }; |
| 447 | if !self.inner.device.extensions().contains(&extension.name) { |
| 448 | return Err(Error::Unsupported(format!("{codec:?} {direction:?}"))); |
| 449 | } |
| 450 | let depth = match profile { |
| 451 | CodecProfile::H265Main10 | CodecProfile::Av1Main10 | CodecProfile::Vp9Profile2 => { |
| 452 | vk::VideoComponentBitDepthFlagsKHR::_10 |
| 453 | } |
| 454 | _ => vk::VideoComponentBitDepthFlagsKHR::_8, |
| 455 | }; |
| 456 | let base = |operation| { |
| 457 | vk::VideoProfileInfoKHR::builder() |
| 458 | .video_codec_operation(operation) |
| 459 | .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) |
| 460 | .luma_bit_depth(depth) |
| 461 | .chroma_bit_depth(depth) |
| 462 | }; |
| 463 | let h264_idc = match profile { |
| 464 | CodecProfile::H264ConstrainedBaseline => vk::video::STD_VIDEO_H264_PROFILE_IDC_BASELINE, |
| 465 | CodecProfile::H264Main => vk::video::STD_VIDEO_H264_PROFILE_IDC_MAIN, |
| 466 | _ => vk::video::STD_VIDEO_H264_PROFILE_IDC_HIGH, |
| 467 | }; |
| 468 | let vp9_profile = match profile { |
| 469 | CodecProfile::Vp9Profile2 => vk::video::STD_VIDEO_VP9_PROFILE_2, |
| 470 | _ => vk::video::STD_VIDEO_VP9_PROFILE_0, |
| 471 | }; |
| 472 | let h265_idc = match profile { |
| 473 | CodecProfile::H265Main10 => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN_10, |
| 474 | _ => vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN, |
| 475 | }; |
| 476 | match (codec, direction) { |
| 477 | (Codec::H264, Direction::Encode) => { |
| 478 | let mut h = vk::VideoEncodeH264ProfileInfoKHR::builder().std_profile_idc(h264_idc); |
| 479 | let p = base(operation).push_next(&mut h); |
| 480 | self.query( |
| 481 | codec, |
| 482 | direction, |
| 483 | profile, |
| 484 | &p, |
| 485 | &mut vk::VideoEncodeH264CapabilitiesKHR::builder(), |
| 486 | ) |
| 487 | } |
| 488 | (Codec::H264, Direction::Decode) => { |
| 489 | let mut h = vk::VideoDecodeH264ProfileInfoKHR::builder() |
| 490 | .std_profile_idc(h264_idc) |
| 491 | .picture_layout(vk::VideoDecodeH264PictureLayoutFlagsKHR::PROGRESSIVE); |
| 492 | let p = base(operation).push_next(&mut h); |
| 493 | self.query( |
| 494 | codec, |
| 495 | direction, |
| 496 | profile, |
| 497 | &p, |
| 498 | &mut vk::VideoDecodeH264CapabilitiesKHR::builder(), |
| 499 | ) |
| 500 | } |
| 501 | (Codec::H265, Direction::Encode) => { |
| 502 | let mut h = vk::VideoEncodeH265ProfileInfoKHR::builder().std_profile_idc(h265_idc); |
| 503 | let p = base(operation).push_next(&mut h); |
| 504 | self.query( |
| 505 | codec, |
| 506 | direction, |
| 507 | profile, |
| 508 | &p, |
| 509 | &mut vk::VideoEncodeH265CapabilitiesKHR::builder(), |
| 510 | ) |
| 511 | } |
| 512 | (Codec::H265, Direction::Decode) => { |
| 513 | let mut h = vk::VideoDecodeH265ProfileInfoKHR::builder().std_profile_idc(h265_idc); |
| 514 | let p = base(operation).push_next(&mut h); |
| 515 | self.query( |
| 516 | codec, |
| 517 | direction, |
| 518 | profile, |
| 519 | &p, |
| 520 | &mut vk::VideoDecodeH265CapabilitiesKHR::builder(), |
| 521 | ) |
| 522 | } |
| 523 | (Codec::Av1, Direction::Encode) => { |
| 524 | let mut h = vk::VideoEncodeAV1ProfileInfoKHR::builder() |
| 525 | .std_profile(vk::video::STD_VIDEO_AV1_PROFILE_MAIN); |
| 526 | let p = base(operation).push_next(&mut h); |
| 527 | self.query( |
| 528 | codec, |
| 529 | direction, |
| 530 | profile, |
| 531 | &p, |
| 532 | &mut vk::VideoEncodeAV1CapabilitiesKHR::builder(), |
| 533 | ) |
| 534 | } |
| 535 | (Codec::Av1, Direction::Decode) => { |
| 536 | let mut h = vk::VideoDecodeAV1ProfileInfoKHR::builder() |
| 537 | .std_profile(vk::video::STD_VIDEO_AV1_PROFILE_MAIN); |
| 538 | let p = base(operation).push_next(&mut h); |
| 539 | self.query( |
| 540 | codec, |
| 541 | direction, |
| 542 | profile, |
| 543 | &p, |
| 544 | &mut vk::VideoDecodeAV1CapabilitiesKHR::builder(), |
| 545 | ) |
| 546 | } |
| 547 | (Codec::Vp9, _) => { |
| 548 | let mut h = vk::VideoDecodeVP9ProfileInfoKHR::builder().std_profile(vp9_profile); |
| 549 | let p = base(operation).push_next(&mut h); |
| 550 | self.query( |
| 551 | codec, |
| 552 | direction, |
| 553 | profile, |
| 554 | &p, |
| 555 | &mut vk::VideoDecodeVP9CapabilitiesKHR::builder(), |
| 556 | ) |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | fn query<C>( |
| 562 | &self, |
| 563 | codec: Codec, |
| 564 | direction: Direction, |
| 565 | codec_profile: CodecProfile, |
| 566 | profile: &vk::VideoProfileInfoKHR, |
| 567 | codec_caps: &mut C, |
| 568 | ) -> Result<CodecCaps, Error> |
| 569 | where |
| 570 | C: vk::Cast, |
| 571 | C::Target: vk::ExtendsVideoCapabilitiesKHR, |
| 572 | { |
| 573 | let mut decode = vk::VideoDecodeCapabilitiesKHR::builder(); |
| 574 | let mut encode = vk::VideoEncodeCapabilitiesKHR::builder(); |
| 575 | let mut caps = vk::VideoCapabilitiesKHR::builder().push_next(codec_caps); |
| 576 | match direction { |
| 577 | Direction::Decode => caps = caps.push_next(&mut decode), |
| 578 | Direction::Encode => caps = caps.push_next(&mut encode), |
| 579 | } |
| 580 | unsafe { |
| 581 | self.inner.instance.get_physical_device_video_capabilities_khr( |
| 582 | self.inner.physical, |
| 583 | profile, |
| 584 | &mut caps, |
| 585 | ) |
| 586 | } |
| 587 | .map_err(|c| crate::session::vk_error(c, "vkGetPhysicalDeviceVideoCapabilitiesKHR"))?; |
| 588 | Ok(CodecCaps { |
| 589 | codec, |
| 590 | direction, |
| 591 | profile: codec_profile, |
| 592 | max_width: caps.max_coded_extent.width, |
| 593 | max_height: caps.max_coded_extent.height, |
| 594 | max_dpb_slots: caps.max_dpb_slots, |
| 595 | max_active_refs: caps.max_active_reference_pictures, |
| 596 | }) |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | impl Drop for Inner { |
| 601 | fn drop(&mut self) { |
| 602 | unsafe { |
| 603 | let _ = self.device.device_wait_idle(); |
| 604 | self.device.destroy_device(None); |
| 605 | self.instance.destroy_instance(None); |
| 606 | } |
| 607 | } |
| 608 | } |