Skip to main content

oa/video/
capability.rs

1//! Backend-neutral reporting for hardware video queue and codec support.
2
3pub use crate::video::types::{
4	VideoDecodeCapabilities, VideoDeviceCapabilities, VideoEncodeCapabilities,
5};
6use crate::{Result, Runtime};
7
8/// Chroma sampling carried by a video codec profile.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum VideoChromaSubsampling {
11	Monochrome,
12	Yuv420,
13	Yuv422,
14	Yuv444,
15}
16
17/// Component precision carried by a video codec profile.
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum VideoComponentBitDepth {
20	Eight,
21	Ten,
22	Twelve,
23}
24
25/// H.264 profile identity used for exact decoder capability queries.
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum H264Profile {
28	Baseline,
29	Main,
30	High,
31	High10,
32	High444Predictive,
33}
34
35/// H.264 decoded-picture layout.
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum H264PictureLayout {
38	Progressive,
39	InterlacedInterleavedLines,
40	InterlacedSeparatePlanes,
41}
42
43/// H.265 profile identity used for exact decoder capability queries.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub enum H265Profile {
46	Main,
47	Main10,
48	MainStillPicture,
49	FormatRangeExtensions,
50	ScreenContentCodingExtensions,
51}
52
53/// AV1 profile identity used for exact decoder capability queries.
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55pub enum Av1Profile {
56	Main,
57	High,
58	Professional,
59}
60
61/// VP9 profile identity used for exact decoder capability queries.
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum Vp9Profile {
64	Profile0,
65	Profile1,
66	Profile2,
67	Profile3,
68}
69
70/// One complete decode profile presented to the selected device.
71#[derive(Clone, Copy, Debug, PartialEq, Eq)]
72pub enum VideoDecodeProfile {
73	H264 {
74		profile: H264Profile,
75		picture_layout: H264PictureLayout,
76		chroma_subsampling: VideoChromaSubsampling,
77		luma_bit_depth: VideoComponentBitDepth,
78		chroma_bit_depth: VideoComponentBitDepth,
79	},
80	H265 {
81		profile: H265Profile,
82		chroma_subsampling: VideoChromaSubsampling,
83		luma_bit_depth: VideoComponentBitDepth,
84		chroma_bit_depth: VideoComponentBitDepth,
85	},
86	Av1 {
87		profile: Av1Profile,
88		film_grain_support: bool,
89		chroma_subsampling: VideoChromaSubsampling,
90		luma_bit_depth: VideoComponentBitDepth,
91		chroma_bit_depth: VideoComponentBitDepth,
92	},
93	Vp9 {
94		profile: Vp9Profile,
95		chroma_subsampling: VideoChromaSubsampling,
96		luma_bit_depth: VideoComponentBitDepth,
97		chroma_bit_depth: VideoComponentBitDepth,
98	},
99}
100
101impl VideoDecodeProfile {
102	/// Construct the common progressive H.264 4:2:0 8-bit profile.
103	pub const fn h264_420_8bit(profile: H264Profile) -> Self {
104		Self::H264 {
105			profile,
106			picture_layout: H264PictureLayout::Progressive,
107			chroma_subsampling: VideoChromaSubsampling::Yuv420,
108			luma_bit_depth: VideoComponentBitDepth::Eight,
109			chroma_bit_depth: VideoComponentBitDepth::Eight,
110		}
111	}
112
113	/// Construct the common H.265 4:2:0 profile at one component precision.
114	pub const fn h265_420(profile: H265Profile, bit_depth: VideoComponentBitDepth) -> Self {
115		Self::H265 {
116			profile,
117			chroma_subsampling: VideoChromaSubsampling::Yuv420,
118			luma_bit_depth: bit_depth,
119			chroma_bit_depth: bit_depth,
120		}
121	}
122
123	/// Construct the common AV1 4:2:0 profile at one component precision.
124	pub const fn av1_420(
125		profile: Av1Profile,
126		bit_depth: VideoComponentBitDepth,
127		film_grain_support: bool,
128	) -> Self {
129		Self::Av1 {
130			profile,
131			film_grain_support,
132			chroma_subsampling: VideoChromaSubsampling::Yuv420,
133			luma_bit_depth: bit_depth,
134			chroma_bit_depth: bit_depth,
135		}
136	}
137
138	/// Construct the common VP9 4:2:0 profile at one component precision.
139	pub const fn vp9_420(profile: Vp9Profile, bit_depth: VideoComponentBitDepth) -> Self {
140		Self::Vp9 {
141			profile,
142			chroma_subsampling: VideoChromaSubsampling::Yuv420,
143			luma_bit_depth: bit_depth,
144			chroma_bit_depth: bit_depth,
145		}
146	}
147}
148
149/// One exact encode profile presented to the selected device.
150#[derive(Clone, Copy, Debug, PartialEq, Eq)]
151pub enum VideoEncodeProfile {
152	H264 {
153		profile: H264Profile,
154		chroma_subsampling: VideoChromaSubsampling,
155		luma_bit_depth: VideoComponentBitDepth,
156		chroma_bit_depth: VideoComponentBitDepth,
157	},
158	H265 {
159		profile: H265Profile,
160		chroma_subsampling: VideoChromaSubsampling,
161		luma_bit_depth: VideoComponentBitDepth,
162		chroma_bit_depth: VideoComponentBitDepth,
163	},
164}
165
166impl VideoEncodeProfile {
167	/// Construct the donor H.264 High 4:2:0 8-bit encode profile.
168	pub const fn h264_high_420_8bit() -> Self {
169		Self::H264 {
170			profile: H264Profile::High,
171			chroma_subsampling: VideoChromaSubsampling::Yuv420,
172			luma_bit_depth: VideoComponentBitDepth::Eight,
173			chroma_bit_depth: VideoComponentBitDepth::Eight,
174		}
175	}
176
177	/// Construct the donor H.265 Main 4:2:0 8-bit encode profile.
178	pub const fn h265_main_420_8bit() -> Self {
179		Self::H265 {
180			profile: H265Profile::Main,
181			chroma_subsampling: VideoChromaSubsampling::Yuv420,
182			luma_bit_depth: VideoComponentBitDepth::Eight,
183			chroma_bit_depth: VideoComponentBitDepth::Eight,
184		}
185	}
186}
187
188/// Two-dimensional device limit expressed without Vulkan types.
189#[derive(Clone, Copy, Debug, PartialEq, Eq)]
190pub struct VideoExtent {
191	pub width: u32,
192	pub height: u32,
193}
194
195/// Multi-plane YUV image layout admitted by the Video runtime.
196#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
197pub enum VideoPixelFormat {
198	/// Three-plane 8-bit 4:2:0 (I420-style plane layout).
199	Yuv420Planar8,
200	/// Two-plane 8-bit 4:2:0 (NV12-style plane layout).
201	Nv12,
202	/// Three-plane 10-bit 4:2:0 stored in 16-bit components.
203	Yuv420Planar10,
204	/// Two-plane 10-bit 4:2:0 stored in 16-bit components (P010).
205	P010,
206	/// Two-plane 12-bit 4:2:0 stored in 16-bit components (P012).
207	P012,
208}
209
210/// One Vulkan-video-compatible image representation without Vulkan types.
211#[derive(Clone, Copy, Debug, PartialEq, Eq)]
212pub struct VideoImageFormat {
213	pub(crate) pixel_format: VideoPixelFormat,
214	pub(crate) optimal_tiling: bool,
215	pub(crate) sampled: bool,
216	pub(crate) storage: bool,
217	pub(crate) transfer_source: bool,
218	pub(crate) transfer_destination: bool,
219}
220
221impl VideoImageFormat {
222	/// Return the semantic plane and component layout.
223	pub const fn pixel_format(self) -> VideoPixelFormat {
224		self.pixel_format
225	}
226
227	/// Return whether the device requires or supports optimal image tiling here.
228	pub const fn optimal_tiling(self) -> bool {
229		self.optimal_tiling
230	}
231
232	/// Return whether the reported image usage includes sampling.
233	pub const fn sampled(self) -> bool {
234		self.sampled
235	}
236
237	/// Return whether the reported image usage includes storage access.
238	pub const fn storage(self) -> bool {
239		self.storage
240	}
241
242	/// Return whether the reported image usage includes transfer-source access.
243	pub const fn transfer_source(self) -> bool {
244		self.transfer_source
245	}
246
247	/// Return whether the reported image usage includes transfer-destination access.
248	pub const fn transfer_destination(self) -> bool {
249		self.transfer_destination
250	}
251}
252
253/// Decode-output and decoded-picture-buffer formats for one exact profile.
254#[derive(Clone, Debug, PartialEq, Eq)]
255pub struct VideoDecodeFormats {
256	pub(crate) profile: VideoDecodeProfile,
257	pub(crate) output: Vec<VideoImageFormat>,
258	pub(crate) dpb: Vec<VideoImageFormat>,
259	pub(crate) unrecognized_output_formats: usize,
260	pub(crate) unrecognized_dpb_formats: usize,
261}
262
263/// Encode-input and reconstructed-picture formats for one exact profile.
264#[derive(Clone, Debug, PartialEq, Eq)]
265pub struct VideoEncodeFormats {
266	pub(crate) profile: VideoEncodeProfile,
267	pub(crate) input: Vec<VideoImageFormat>,
268	pub(crate) dpb: Vec<VideoImageFormat>,
269	pub(crate) unrecognized_input_formats: usize,
270	pub(crate) unrecognized_dpb_formats: usize,
271}
272
273impl VideoEncodeFormats {
274	pub const fn profile(&self) -> VideoEncodeProfile {
275		self.profile
276	}
277	pub fn input(&self) -> &[VideoImageFormat] {
278		&self.input
279	}
280	pub fn dpb(&self) -> &[VideoImageFormat] {
281		&self.dpb
282	}
283	pub const fn unrecognized_input_formats(&self) -> usize {
284		self.unrecognized_input_formats
285	}
286	pub const fn unrecognized_dpb_formats(&self) -> usize {
287		self.unrecognized_dpb_formats
288	}
289}
290
291impl VideoDecodeFormats {
292	/// Return the exact profile used for this query.
293	pub const fn profile(&self) -> VideoDecodeProfile {
294		self.profile
295	}
296
297	/// Borrow OARS-recognized decoded-output image formats.
298	pub fn output(&self) -> &[VideoImageFormat] {
299		&self.output
300	}
301
302	/// Borrow OARS-recognized decoded-picture-buffer image formats.
303	pub fn dpb(&self) -> &[VideoImageFormat] {
304		&self.dpb
305	}
306
307	/// Return how many backend output formats OARS cannot yet represent.
308	pub const fn unrecognized_output_formats(&self) -> usize {
309		self.unrecognized_output_formats
310	}
311
312	/// Return how many backend DPB formats OARS cannot yet represent.
313	pub const fn unrecognized_dpb_formats(&self) -> usize {
314		self.unrecognized_dpb_formats
315	}
316}
317
318/// Codec-specific maximum level reported for a decode profile.
319#[derive(Clone, Copy, Debug, PartialEq, Eq)]
320pub enum VideoDecodeLevel {
321	H264(u32),
322	H265(u32),
323	Av1(u32),
324	Vp9(u32),
325}
326
327/// Codec-specific limits returned with an exact encode capability query.
328#[derive(Clone, Copy, Debug, PartialEq, Eq)]
329pub enum VideoEncodeCodecCapabilities {
330	H264 {
331		/// H.264 bitstream level_idc (for example, 42 for level 4.2).
332		max_level: u32,
333		max_slice_count: u32,
334		max_p_l0_references: u32,
335		max_b_l0_references: u32,
336		max_l1_references: u32,
337		max_temporal_layers: u32,
338		min_qp: i32,
339		max_qp: i32,
340	},
341	H265 {
342		max_level: u32,
343		max_slice_segment_count: u32,
344		max_tiles: VideoExtent,
345		ctb_size_16: bool,
346		ctb_size_32: bool,
347		ctb_size_64: bool,
348		transform_size_4: bool,
349		transform_size_8: bool,
350		transform_size_16: bool,
351		transform_size_32: bool,
352		max_p_l0_references: u32,
353		max_b_l0_references: u32,
354		max_l1_references: u32,
355		max_sub_layers: u32,
356		min_qp: i32,
357		max_qp: i32,
358	},
359}
360
361impl VideoEncodeCapabilities {
362	pub const fn profile(self) -> VideoEncodeProfile {
363		self.profile
364	}
365	pub const fn min_coded_extent(self) -> VideoExtent {
366		self.min_coded_extent
367	}
368	pub const fn max_coded_extent(self) -> VideoExtent {
369		self.max_coded_extent
370	}
371	pub const fn picture_access_granularity(self) -> VideoExtent {
372		self.picture_access_granularity
373	}
374	pub const fn input_picture_granularity(self) -> VideoExtent {
375		self.input_picture_granularity
376	}
377	pub const fn min_bitstream_offset_alignment(self) -> u64 {
378		self.min_bitstream_offset_alignment
379	}
380	pub const fn min_bitstream_size_alignment(self) -> u64 {
381		self.min_bitstream_size_alignment
382	}
383	pub const fn max_dpb_slots(self) -> u32 {
384		self.max_dpb_slots
385	}
386	pub const fn max_active_reference_pictures(self) -> u32 {
387		self.max_active_reference_pictures
388	}
389	pub const fn max_rate_control_layers(self) -> u32 {
390		self.max_rate_control_layers
391	}
392	pub const fn max_bitrate(self) -> u64 {
393		self.max_bitrate
394	}
395	pub const fn max_quality_levels(self) -> u32 {
396		self.max_quality_levels
397	}
398	pub const fn supports_constant_qp(self) -> bool {
399		self.constant_qp
400	}
401	pub const fn supports_cbr(self) -> bool {
402		self.cbr
403	}
404	pub const fn supports_vbr(self) -> bool {
405		self.vbr
406	}
407	pub const fn supports_feedback_offset(self) -> bool {
408		self.feedback_offset
409	}
410	pub const fn supports_feedback_bytes_written(self) -> bool {
411		self.feedback_bytes_written
412	}
413	pub const fn supports_feedback_overrides(self) -> bool {
414		self.feedback_overrides
415	}
416	pub const fn protected_content(self) -> bool {
417		self.protected_content
418	}
419	pub const fn separate_reference_images(self) -> bool {
420		self.separate_reference_images
421	}
422	pub const fn codec(self) -> VideoEncodeCodecCapabilities {
423		self.codec
424	}
425}
426
427impl VideoDecodeCapabilities {
428	/// Return the exact profile used for this query.
429	pub const fn profile(self) -> VideoDecodeProfile {
430		self.profile
431	}
432
433	/// Return the smallest supported coded extent.
434	pub const fn min_coded_extent(self) -> VideoExtent {
435		self.min_coded_extent
436	}
437
438	/// Return the largest supported coded extent.
439	pub const fn max_coded_extent(self) -> VideoExtent {
440		self.max_coded_extent
441	}
442
443	/// Return the device's coded-picture access granularity.
444	pub const fn picture_access_granularity(self) -> VideoExtent {
445		self.picture_access_granularity
446	}
447
448	/// Return the minimum encoded-bitstream buffer offset alignment in bytes.
449	pub const fn min_bitstream_offset_alignment(self) -> u64 {
450		self.min_bitstream_offset_alignment
451	}
452
453	/// Return the minimum encoded-bitstream buffer size alignment in bytes.
454	pub const fn min_bitstream_size_alignment(self) -> u64 {
455		self.min_bitstream_size_alignment
456	}
457
458	/// Return the maximum number of decoded-picture-buffer slots.
459	pub const fn max_dpb_slots(self) -> u32 {
460		self.max_dpb_slots
461	}
462
463	/// Return the maximum number of simultaneously active reference pictures.
464	pub const fn max_active_reference_pictures(self) -> u32 {
465		self.max_active_reference_pictures
466	}
467
468	/// Return the codec-specific maximum supported level.
469	pub const fn level(self) -> VideoDecodeLevel {
470		self.level
471	}
472
473	/// Return H.264 field-offset granularity, or `None` for another codec.
474	pub const fn field_offset_granularity(self) -> Option<(i32, i32)> {
475		self.field_offset_granularity
476	}
477
478	/// Return whether decoded output may alias its DPB picture.
479	pub const fn dpb_and_output_coincide(self) -> bool {
480		self.dpb_and_output_coincide
481	}
482
483	/// Return whether decoded output may be distinct from its DPB picture.
484	pub const fn dpb_and_output_distinct(self) -> bool {
485		self.dpb_and_output_distinct
486	}
487
488	/// Return whether this profile supports protected video sessions.
489	pub const fn protected_content(self) -> bool {
490		self.protected_content
491	}
492
493	/// Return whether each DPB slot may use a separate reference image.
494	pub const fn separate_reference_images(self) -> bool {
495		self.separate_reference_images
496	}
497}
498
499impl VideoDeviceCapabilities {
500	/// Return the first physical-device queue family advertising video decode.
501	pub const fn decode_queue_family(self) -> Option<u32> {
502		self.decode_queue_family
503	}
504
505	/// Return the first physical-device queue family advertising video encode.
506	pub const fn encode_queue_family(self) -> Option<u32> {
507		self.encode_queue_family
508	}
509
510	/// Return whether the selected video-decode queue supports operation-status queries.
511	pub const fn supports_decode_result_status_queries(self) -> bool {
512		self.decode_result_status_queries
513	}
514
515	/// Return whether the selected video-encode queue supports operation-status queries.
516	pub const fn supports_encode_result_status_queries(self) -> bool {
517		self.encode_result_status_queries
518	}
519
520	/// Return whether the physical device advertises H.264 decode.
521	pub const fn supports_h264_decode(self) -> bool {
522		self.h264_decode
523	}
524
525	/// Return whether the physical device advertises H.265 decode.
526	pub const fn supports_h265_decode(self) -> bool {
527		self.h265_decode
528	}
529
530	/// Return whether the physical device advertises AV1 decode.
531	pub const fn supports_av1_decode(self) -> bool {
532		self.av1_decode
533	}
534
535	/// Return whether the physical device advertises VP9 decode.
536	pub const fn supports_vp9_decode(self) -> bool {
537		self.vp9_decode
538	}
539
540	/// Return whether the physical device advertises H.264 encode.
541	pub const fn supports_h264_encode(self) -> bool {
542		self.h264_encode
543	}
544
545	/// Return whether the physical device advertises H.265 encode.
546	pub const fn supports_h265_encode(self) -> bool {
547		self.h265_encode
548	}
549
550	/// Return whether the physical device advertises AV1 encode.
551	pub const fn supports_av1_encode(self) -> bool {
552		self.av1_encode
553	}
554
555	/// Return whether the Runtime enabled the advertised Vulkan Video queue extensions.
556	pub const fn video_queues_enabled(self) -> bool {
557		self.video_queues_enabled
558	}
559
560	/// Return whether this Runtime currently has an enabled decoder-session path.
561	pub const fn decoder_sessions_available(self) -> bool {
562		self.decoder_sessions_available
563	}
564
565	/// Return whether this Runtime currently has an enabled encoder-session path.
566	pub const fn encoder_sessions_available(self) -> bool {
567		self.encoder_sessions_available
568	}
569}
570
571/// Query Vulkan Video queues and codec extensions on the Runtime's selected device.
572///
573/// This query reports both physical-device advertisement and whether Runtime
574/// creation enabled video queues, and whether at least one qualified codec
575/// session path exists.
576///
577/// # Errors
578///
579/// Returns an error when Vulkan device-extension or queue-family enumeration
580/// fails.
581pub fn query_device_capabilities(runtime: &Runtime) -> Result<VideoDeviceCapabilities> {
582	runtime.query_video_device_capabilities()
583}
584
585/// Query the selected device's exact limits for one decoder profile.
586///
587/// Extension advertisement alone does not prove that a particular profile,
588/// chroma layout, bit depth, or codec option is usable. This query presents the
589/// complete profile to Vulkan and returns `MissingCapability` when rejected.
590///
591/// # Errors
592///
593/// Returns an error when the codec extension is unavailable, the exact profile
594/// is unsupported, or the Vulkan capability query fails.
595pub fn query_decode_capabilities(
596	runtime: &Runtime,
597	profile: VideoDecodeProfile,
598) -> Result<VideoDecodeCapabilities> {
599	runtime.query_video_decode_capabilities(profile)
600}
601
602/// Enumerate decoded-output and DPB image formats for one exact profile.
603///
604/// # Errors
605///
606/// Returns an error when the profile or its codec extension is unsupported, a
607/// Vulkan enumeration fails, or the driver reports an unreasonable result size.
608pub fn query_decode_formats(
609	runtime: &Runtime,
610	profile: VideoDecodeProfile,
611) -> Result<VideoDecodeFormats> {
612	runtime.query_video_decode_formats(profile)
613}
614
615/// Query exact H.264 or H.265 encoder limits without creating a session.
616pub fn query_encode_capabilities(
617	runtime: &Runtime,
618	profile: VideoEncodeProfile,
619) -> Result<VideoEncodeCapabilities> {
620	runtime.query_video_encode_capabilities(profile)
621}
622
623/// Enumerate encode-input and DPB formats for one exact profile.
624pub fn query_encode_formats(
625	runtime: &Runtime,
626	profile: VideoEncodeProfile,
627) -> Result<VideoEncodeFormats> {
628	runtime.query_video_encode_formats(profile)
629}