use ffmpeg_next::ffi::{AVHWDeviceType, AVPixelFormat};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Backend {
VideoToolbox,
Vaapi,
Cuda,
D3d11va,
}
impl Backend {
pub(crate) fn av_hwdevice_type(self) -> AVHWDeviceType {
match self {
Self::VideoToolbox => AVHWDeviceType::AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
Self::Vaapi => AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI,
Self::Cuda => AVHWDeviceType::AV_HWDEVICE_TYPE_CUDA,
Self::D3d11va => AVHWDeviceType::AV_HWDEVICE_TYPE_D3D11VA,
}
}
pub(crate) fn hw_pixel_format(self) -> AVPixelFormat {
match self {
Self::VideoToolbox => AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX,
Self::Vaapi => AVPixelFormat::AV_PIX_FMT_VAAPI,
Self::Cuda => AVPixelFormat::AV_PIX_FMT_CUDA,
Self::D3d11va => AVPixelFormat::AV_PIX_FMT_D3D11,
}
}
}
pub(crate) fn probe_order() -> &'static [Backend] {
#[cfg(target_vendor = "apple")]
{
&[Backend::VideoToolbox]
}
#[cfg(target_os = "linux")]
{
&[Backend::Vaapi, Backend::Cuda]
}
#[cfg(target_os = "windows")]
{
&[Backend::D3d11va, Backend::Cuda]
}
#[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "windows",)))]
{
&[]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_backends_have_hwdevice_type_and_pix_fmt() {
for b in [
Backend::VideoToolbox,
Backend::Vaapi,
Backend::Cuda,
Backend::D3d11va,
] {
let _ = b.av_hwdevice_type();
let _ = b.hw_pixel_format();
}
}
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos",
))]
#[test]
fn apple_probe_order() {
assert_eq!(probe_order(), &[Backend::VideoToolbox]);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_probe_order() {
assert_eq!(probe_order(), &[Backend::Vaapi, Backend::Cuda]);
}
#[cfg(target_os = "windows")]
#[test]
fn windows_probe_order() {
assert_eq!(probe_order(), &[Backend::D3d11va, Backend::Cuda]);
}
}