diff --git a/crates/reims-vgpu/src/graphics.rs b/crates/reims-vgpu/src/graphics.rs index 7c8f29f1e..9ce42b2b6 100644 --- a/crates/reims-vgpu/src/graphics.rs +++ b/crates/reims-vgpu/src/graphics.rs @@ -176,6 +176,8 @@ mod compile; mod completion; mod create_waits; mod decode; +#[cfg(any(target_os = "linux", windows))] +mod decode_sizes; mod device_info; #[cfg(windows)] mod drawn; diff --git a/crates/reims-vgpu/src/graphics/decode_sizes.rs b/crates/reims-vgpu/src/graphics/decode_sizes.rs new file mode 100644 index 000000000..04855db69 --- /dev/null +++ b/crates/reims-vgpu/src/graphics/decode_sizes.rs @@ -0,0 +1,96 @@ +//! The sizes a host decoder takes, as its arm's own query of the decoder lists them +//! ([`DecodeSizes`]), and why it takes no picture of a size outside them. A VideoToolbox session +//! of a stream outside them is refused at its start (`GraphicsVideo::guest_declines`), as the +//! decoder would refuse its first picture. + +/// The sizes of the pictures a decoder takes, as its arm's query lists them: each side from +/// `min` up to `max`, and at most `max_macroblocks` 16x16 blocks a picture. A bound the query +/// lists none of is 0, `u32::MAX` or `None`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct DecodeSizes { + /// The narrowest and the shortest picture. + pub min: (u32, u32), + /// The widest and the tallest. + pub max: (u32, u32), + /// The most 16x16 blocks a picture holds. + pub max_macroblocks: Option, +} + +impl DecodeSizes { + /// Why a decoder of these sizes takes no picture of `width` by `height`, its line led by + /// `what` ("NVDEC decodes H.264"); `None` where it takes it. + pub(super) fn declines(&self, what: &str, width: u32, height: u32) -> Option { + let ((min_width, min_height), (max_width, max_height)) = (self.min, self.max); + let blocks = u64::from(width.div_ceil(16)) * u64::from(height.div_ceil(16)); + let too_many = (self.max_macroblocks).is_some_and(|most| blocks > u64::from(most)); + if width < min_width || height < min_height { + Some(format!( + "{what} from {min_width}x{min_height}, not {width}x{height}" + )) + } else if width > max_width || height > max_height || too_many { + Some(match self.max_macroblocks { + Some(most) => format!( + "{what} up to {max_width}x{max_height} ({most} macroblocks), not \ + {width}x{height}" + ), + None => format!("{what} up to {max_width}x{max_height}, not {width}x{height}"), + }) + } else { + None + } + } +} + +#[cfg(test)] +mod tests { + use super::DecodeSizes; + + /// A picture is taken from the least size up to the largest and the most blocks, each side + /// and bound alone declining it + #[test] + fn a_picture_outside_any_bound_is_declined_naming_the_bounds() { + let sizes = DecodeSizes { + min: (48, 64), + max: (4096, 4096), + max_macroblocks: Some(65_536), + }; + for (width, height) in [(48, 64), (4096, 4096), (1920, 1080)] { + assert_eq!(sizes.declines("D", width, height), None, "{width}x{height}"); + } + let from = "D from 48x64, not"; + let up_to = "D up to 4096x4096 (65536 macroblocks), not"; + for (width, height, why) in [ + (32, 64, format!("{from} 32x64")), + (48, 48, format!("{from} 48x48")), + (4112, 64, format!("{up_to} 4112x64")), + (48, 4112, format!("{up_to} 48x4112")), + ] { + assert_eq!(sizes.declines("D", width, height), Some(why)); + } + let fewer = DecodeSizes { + max_macroblocks: Some(8160), + ..sizes + }; + assert_eq!(fewer.declines("D", 1920, 1088), None); + assert_eq!( + fewer.declines("D", 1920, 1104).as_deref(), + Some("D up to 4096x4096 (8160 macroblocks), not 1920x1104") + ); + let listed_none = DecodeSizes { + min: (0, 0), + max: (u32::MAX, u32::MAX), + max_macroblocks: None, + }; + for (width, height) in [(1, 1), (16, 16), (u32::MAX, u32::MAX)] { + assert_eq!(listed_none.declines("D", width, height), None); + } + let unblocked = DecodeSizes { + max_macroblocks: None, + ..sizes + }; + assert_eq!( + unblocked.declines("D", 4097, 64).as_deref(), + Some("D up to 4096x4096, not 4097x64") + ); + } +} diff --git a/crates/reims-vgpu/src/graphics/nvidia.rs b/crates/reims-vgpu/src/graphics/nvidia.rs index 691a67333..f62759442 100644 --- a/crates/reims-vgpu/src/graphics/nvidia.rs +++ b/crates/reims-vgpu/src/graphics/nvidia.rs @@ -120,6 +120,7 @@ use super::av1::{GuestAv1, HOST_VIDEO_CODEC_AV1}; use super::decode::{ GraphicsVideo, GraphicsVideoSession, GuestDecoder, GuestEncoder, GuestOutput, PlanePages, }; +use super::decode_sizes::DecodeSizes; use super::gather::{GatherPipeline, Gatherer, Planar, gathers}; use super::video_host::{ BackendError, CONVERTED_CODE, DecodeOutput, Extent, Failed, HostDecoder, HostEncodeSession, @@ -485,17 +486,34 @@ fn supported(gpu: &Gpu, codec: NvdecCodec, bit_depth: u32) -> Option = &'a dyn Fn(NvdecCodec, ChromaFormat, u32) -> Result; + +/// Why NVDEC's decoders take none of `stream`'s pictures, a stream of `codec` the codec module +/// takes, by what `caps` answers: an HEVC stream of the range extensions as +/// [`declines_range_extensions`] says; any other but 4:2:0's; `None` where they decode it. +fn declines(caps: CapsOf<'_>, codec: u32, stream: &Stream) -> Option { + if codec == RGFX_VIDEO_CODEC_HEVC && stream.range_extensions { + return declines_range_extensions(caps, stream); + } + (stream.chroma_format_idc != 1 || stream.range_extensions).then(|| { + format!( + "{NAME}'s decoders land 4:2:0 pictures, and HEVC's range extensions where NVDEC \ + decodes them, alone" + ) + }) +} + /// Why NVDEC's decoders take none of `stream`'s pictures, an HEVC stream of the range -/// extensions: NVDEC's caps of HEVC at the stream's chroma format and depth, which are to say it -/// decodes it, into the output the decoder makes of them (4:2:0 into `SurfaceFormat::Nv12` at 8 -/// bits and `P016` past; 4:4:4 into its three planes, `Yuv444` at 8 bits and `Yuv444_16Bit` -/// past), at the stream's size; `None` where they do. A 4:2:2 stream is declined whatever they +/// extensions: `caps` of HEVC at the stream's chroma format and depth, which are to say it +/// decodes it ([`declines_by`]); `None` where they do. A 4:2:2 stream is declined whatever they /// say: no decode of it into NVDEC's 4:2:2 outputs is built here. -fn declines_range_extensions(gpu: &Gpu, stream: &Stream) -> Option { +fn declines_range_extensions(caps: CapsOf<'_>, stream: &Stream) -> Option { let bits = u32::from(stream.luma_bits); - let (chroma, name) = match stream.chroma_format_idc { - 1 => (ChromaFormat::Yuv420, "4:2:0"), - 3 => (ChromaFormat::Yuv444, "4:4:4"), + let (chroma, what) = match stream.chroma_format_idc { + 1 => (ChromaFormat::Yuv420, "HEVC 4:2:0"), + 3 => (ChromaFormat::Yuv444, "HEVC 4:4:4"), 2 => { return Some(format!( "{NAME}'s decoders here land no HEVC 4:2:2 pictures" @@ -507,13 +525,31 @@ fn declines_range_extensions(gpu: &Gpu, stream: &Stream) -> Option { )); } }; - let caps = match decode_capabilities(gpu, NvdecCodec::Hevc, chroma, bits) { + declines_by( + caps(NvdecCodec::Hevc, chroma, bits), + what, + chroma, + bits, + stream, + ) +} + +/// Why NVDEC's decoder of `answer`, the caps of `what` at `chroma` and `bits`, decodes none of +/// `stream`'s pictures: where they do not say it decodes it, into the output the decoder makes +/// of it (4:2:0 into `SurfaceFormat::Nv12` at 8 bits and `P016` past; 4:4:4 into its three +/// planes, `Yuv444` at 8 bits and `Yuv444_16Bit` past), at the stream's size (`nMinWidth` and +/// `nMinHeight` up to `nMaxWidth`, `nMaxHeight` and `nMaxMBCount`), as the decoder's make checks +/// them; `None` where they do. +fn declines_by( + answer: Result, + what: &str, + chroma: ChromaFormat, + bits: u32, + stream: &Stream, +) -> Option { + let caps = match answer { Ok(caps) => caps, - Err(error) => { - return Some(format!( - "{NAME}'s caps of HEVC {name} at {bits} bits: {error}" - )); - } + Err(error) => return Some(format!("{NAME}'s caps of {what} at {bits} bits: {error}")), }; let output = match (chroma, bits > 8) { (ChromaFormat::Yuv444, false) => SurfaceFormat::Yuv444, @@ -521,30 +557,24 @@ fn declines_range_extensions(gpu: &Gpu, stream: &Stream) -> Option { (_, true) => SurfaceFormat::P016, (_, false) => SurfaceFormat::Nv12, }; - let (width, height) = (stream.width, stream.height); + let sizes = DecodeSizes { + min: (caps.min_width, caps.min_height), + max: (caps.max_width, caps.max_height), + max_macroblocks: Some(caps.max_macroblocks), + }; if !caps.supported { - Some(format!("{NAME} does not decode HEVC {name} at {bits} bits")) + Some(format!("{NAME} does not decode {what} at {bits} bits")) } else if !caps.output_formats.contains(&output) { Some(format!( - "{NAME} writes HEVC {name} at {bits} bits as {:?}, not {output:?}", + "{NAME} writes {what} at {bits} bits as {:?}, not {output:?}", caps.output_formats )) - } else if width < caps.min_width || height < caps.min_height { - Some(format!( - "{NAME} decodes HEVC {name} from {}x{}, not {width}x{height}", - caps.min_width, caps.min_height - )) - } else if width > caps.max_width - || height > caps.max_height - || u64::from(width.div_ceil(16)) * u64::from(height.div_ceil(16)) - > u64::from(caps.max_macroblocks) - { - Some(format!( - "{NAME} decodes HEVC {name} up to {}x{} ({} macroblocks), not {width}x{height}", - caps.max_width, caps.max_height, caps.max_macroblocks - )) } else { - None + sizes.declines( + &format!("{NAME} decodes {what}"), + stream.width, + stream.height, + ) } } @@ -737,26 +767,22 @@ impl GraphicsVideo

for Shared

{ })) } - /// 4:2:0 streams of Main and Main 10, HEVC's range extensions where NVDEC decodes them - /// ([`declines_range_extensions`]), AV1's at the depths and sizes its caps list - /// (`super::av1::declines`), and VP9's where NVDEC decodes the stream's profile at its depth + /// 4:2:0 streams of Main and Main 10, and HEVC's range extensions where NVDEC decodes them, + /// by NVDEC's own caps ([`declines`]); AV1's at the depths and sizes its caps list + /// (`super::av1::declines`); and VP9's where NVDEC decodes the stream's profile at its depth /// and size ([`declines_vp9`]). fn guest_declines(&self, codec: u32, stream: &Stream) -> Option { if codec == HOST_VIDEO_CODEC_AV1 { return super::av1::declines(NAME, self.0.av1, stream); } - if codec == RGFX_VIDEO_CODEC_HEVC && stream.range_extensions { - return declines_range_extensions(&self.0.gpu, stream); - } if codec == RGFX_VIDEO_CODEC_VP9 && stream.chroma_format_idc == 1 { return declines_vp9(self.0.vp9.as_ref(), stream); } - (stream.chroma_format_idc != 1 || stream.range_extensions).then(|| { - format!( - "{NAME}'s decoders land 4:2:0 pictures, and HEVC's range extensions where NVDEC \ - decodes them, alone" - ) - }) + let gpu = &self.0.gpu; + let caps = |codec, chroma, bits| { + decode_capabilities(gpu, codec, chroma, bits).map_err(|error| error.to_string()) + }; + declines(&caps, codec, stream) } } diff --git a/crates/reims-vgpu/src/graphics/vaapi.rs b/crates/reims-vgpu/src/graphics/vaapi.rs index 1a3521ae5..8c15a23e7 100644 --- a/crates/reims-vgpu/src/graphics/vaapi.rs +++ b/crates/reims-vgpu/src/graphics/vaapi.rs @@ -137,9 +137,9 @@ use reims_vgpu_video::vaapi::{ H264_MAX_MACROBLOCKS, H264Decoder, HevcDecoder, ImportWrites, PlaneRows, Profile, Target, TargetCodec, TargetFor, VideoEncoder, Vp9Decoder, encoder_support, }; +use reims_vgpu_video::vaapi::{SurfaceCapabilities, range_extensions_decoder, rt_format_names}; #[cfg(unix)] use reims_vgpu_video::vaapi::{TARGET_ALIGNMENT, TargetChecks, TargetPlane, TargetShape}; -use reims_vgpu_video::vaapi::{range_extensions_decoder, rt_format_names}; #[cfg(unix)] use reims_vgpu_vulkan::core::num::ToU64 as _; use reims_vgpu_vulkan::core::num::ToUsize as _; @@ -157,6 +157,7 @@ use super::av1::{GuestAv1, HOST_VIDEO_CODEC_AV1}; use super::decode::{ GraphicsVideo, GraphicsVideoSession, GuestDecoder, GuestEncoder, GuestOutput, PlanePages, }; +use super::decode_sizes::DecodeSizes; use reims_vgpu_video::paravirt::{Encoder, EncoderConfig, Stream}; #[cfg(unix)] @@ -384,9 +385,9 @@ struct VaapiVideo { /// What writes pictures into device caches through their memory, where the platform makes /// caches of dma-bufs and the driver's checks passed anything (`cache_writes`, on Unix). targets: Option>>, - /// The driver's HEVC range extensions decoders it lists: what VideoToolbox's sessions of - /// those streams decode on ([`declines_range_extensions`]). - hevc_range_extensions: Vec, + /// The driver's H.264 and HEVC decoders it lists: what VideoToolbox's sessions decode on, + /// checked at each one's start ([`declines`]). + decoders: Vec, /// What the driver decodes of AV1 for VideoToolbox's decoders (`super::av1`): its /// `AV1Profile0` decoder's largest and smallest frames and depths; `None` where it lists /// none. @@ -420,7 +421,12 @@ impl VaapiVideo

{ }; let mut caps = caps_of(&capabilities); caps.extend(encode::encode_caps_of(&encoders, &capabilities)); - let hevc_range_extensions = [ + let decoders = [ + Profile::H264_CONSTRAINED_BASELINE, + Profile::H264_MAIN, + Profile::H264_HIGH, + Profile::HEVC_MAIN, + Profile::HEVC_MAIN10, Profile::HEVC_MAIN12, Profile::HEVC_MAIN422_10, Profile::HEVC_MAIN422_12, @@ -487,7 +493,7 @@ impl VaapiVideo

{ imports, copies, targets, - hevc_range_extensions, + decoders, av1, #[cfg(unix)] gathers: Arc::new(GatherPipeline::new(device)), @@ -581,17 +587,14 @@ impl GraphicsVideo

for VaapiVideo

{ true } - /// 4:2:0 streams of Main and Main 10, HEVC's range extensions where the driver's decoder of - /// their chroma format and depth decodes them ([`declines_range_extensions`]), AV1's at the - /// depths and sizes the driver lists (`super::av1::declines`), and VP9's where the driver's - /// config of the stream's profile takes its depth and size. + /// 4:2:0 streams of Main and Main 10, and HEVC's range extensions where the driver's + /// decoder of their chroma format and depth decodes them, by the driver's own answers + /// ([`declines`]); AV1's at the depths and sizes the driver lists (`super::av1::declines`); + /// and VP9's where the driver's config of the stream's profile takes its depth and size. fn guest_declines(&self, codec: u32, stream: &Stream) -> Option { if codec == HOST_VIDEO_CODEC_AV1 { return super::av1::declines(NAME, self.av1, stream); } - if codec == RGFX_VIDEO_CODEC_HEVC && stream.range_extensions { - return declines_range_extensions(&self.hevc_range_extensions, stream); - } if codec == RGFX_VIDEO_CODEC_VP9 && stream.chroma_format_idc == 1 { return Vp9Decoder::supports( &self.display, @@ -602,12 +605,7 @@ impl GraphicsVideo

for VaapiVideo

{ .err() .map(|error| format!("{NAME}: {error}")); } - (stream.chroma_format_idc != 1 || stream.range_extensions).then(|| { - format!( - "{NAME}'s decoders land 4:2:0 pictures, and HEVC's range extensions where the \ - driver decodes them, alone" - ) - }) + declines(&self.decoders, codec, stream) } /// One for each codec the driver encodes a profile of (`encode::guest_encoders`). @@ -670,6 +668,42 @@ impl GraphicsVideo

for VaapiVideo

{ } } +/// Why the driver's decoders among `configs` take none of `stream`'s pictures, a stream of +/// `codec` the codec module takes: an HEVC stream of the range extensions as +/// [`declines_range_extensions`] says; any other but 4:2:0's; `None` where they decode it. +fn declines(configs: &[ConfigCapabilities], codec: u32, stream: &Stream) -> Option { + if codec == RGFX_VIDEO_CODEC_HEVC && stream.range_extensions { + return declines_range_extensions(configs, stream); + } + (stream.chroma_format_idc != 1 || stream.range_extensions).then(|| { + format!( + "{NAME}'s decoders land 4:2:0 pictures, and HEVC's range extensions where the \ + driver decodes them, alone" + ) + }) +} + +/// The sizes `config`'s decoder takes, of `surfaces`: from the least its surfaces list (0 where +/// they list none) up to the largest the config lists, else its surfaces. +fn sizes_of(config: &ConfigCapabilities, surfaces: Option<&SurfaceCapabilities>) -> DecodeSizes { + let listed = |of: fn(&SurfaceCapabilities) -> Option| surfaces.and_then(of); + DecodeSizes { + min: ( + listed(|s| s.min_width).unwrap_or(0), + listed(|s| s.min_height).unwrap_or(0), + ), + max: ( + (config.max_width) + .or(listed(|s| s.max_width)) + .unwrap_or(u32::MAX), + (config.max_height) + .or(listed(|s| s.max_height)) + .unwrap_or(u32::MAX), + ), + max_macroblocks: None, + } +} + /// Why the driver decodes none of an HEVC range extensions `stream`'s pictures into the surfaces /// the VA crate's decoder lands them from, `None` where it decodes them: on the decoder among /// `configs` of the profile of the stream's chroma format and depth (`range_extensions_decoder`), @@ -710,29 +744,11 @@ fn declines_range_extensions(configs: &[ConfigCapabilities], stream: &Stream) -> formats.join(", ") )); } - let (width, height) = (stream.width, stream.height); - let (min_width, min_height) = ( - surfaces.min_width.unwrap_or(0), - surfaces.min_height.unwrap_or(0), - ); - let (max_width, max_height) = ( - config.max_width.or(surfaces.max_width).unwrap_or(u32::MAX), - config - .max_height - .or(surfaces.max_height) - .unwrap_or(u32::MAX), - ); - if width < min_width || height < min_height { - Some(format!( - "{NAME} decodes {name} from {min_width}x{min_height}, not {width}x{height}" - )) - } else if width > max_width || height > max_height { - Some(format!( - "{NAME} decodes {name} up to {max_width}x{max_height}, not {width}x{height}" - )) - } else { - None - } + sizes_of(config, Some(surfaces)).declines( + &format!("{NAME} decodes {name}"), + stream.width, + stream.height, + ) } /// The caps of H.264, HEVC and VP9 decode the driver's answers give (module doc); an entry for