use crate::error::IronError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum VideoCodec {
H264,
H265,
H266,
VP8,
VP9,
AV1,
MPEG2,
MPEG4,
VC1,
JPEG,
ProRes,
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CodecDirection {
Decode,
Encode,
Both,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum MaxResolution {
SD, HD, FullHD, QHD, UHD4K, UHD8K, Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BitDepth {
Bit8,
Bit10,
Bit12,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CapabilitySource {
DirectQuery,
Inferred,
Manual,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodecCapability {
pub device: String,
pub codec: VideoCodec,
pub direction: CodecDirection,
pub max_resolution: MaxResolution,
pub max_bit_depth: BitDepth,
pub max_fps: Option<u32>,
pub engine: String,
pub source: CapabilitySource,
pub confidence: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputeCapability {
pub device: String,
pub cuda_version: String,
pub opencl_version: String,
pub vulkan_compute: bool,
pub metal_compute: bool,
pub direct_compute: String,
pub estimated_tflops_fp32: f32,
pub estimated_tflops_fp16: f32,
pub has_tensor_cores: bool,
pub has_ray_tracing: bool,
pub source: CapabilitySource,
}
pub struct CodecMonitor {
codec_caps: Vec<CodecCapability>,
compute_caps: Vec<ComputeCapability>,
}
impl CodecMonitor {
pub fn new() -> Result<Self, IronError> {
let mut monitor = Self {
codec_caps: Vec::new(),
compute_caps: Vec::new(),
};
monitor.refresh()?;
Ok(monitor)
}
pub fn refresh(&mut self) -> Result<(), IronError> {
self.codec_caps.clear();
self.compute_caps.clear();
let gpu_names = Self::detect_gpu_names();
#[cfg(target_os = "linux")]
self.refresh_linux();
#[cfg(target_os = "windows")]
self.refresh_windows();
#[cfg(target_os = "macos")]
self.refresh_macos();
if self.codec_caps.is_empty() {
for name in &gpu_names {
self.infer_from_gpu_name(name);
}
}
for name in &gpu_names {
self.infer_compute_from_gpu(name);
}
Ok(())
}
pub fn capabilities(&self) -> &[CodecCapability] {
&self.codec_caps
}
pub fn compute_capabilities(&self) -> &[ComputeCapability] {
&self.compute_caps
}
pub fn can_decode(&self, codec: &VideoCodec) -> bool {
self.codec_caps.iter().any(|c| {
&c.codec == codec
&& matches!(c.direction, CodecDirection::Decode | CodecDirection::Both)
})
}
pub fn can_encode(&self, codec: &VideoCodec) -> bool {
self.codec_caps.iter().any(|c| {
&c.codec == codec
&& matches!(c.direction, CodecDirection::Encode | CodecDirection::Both)
})
}
pub fn max_decode_resolution(&self, codec: &VideoCodec) -> MaxResolution {
self.codec_caps
.iter()
.filter(|c| {
&c.codec == codec
&& matches!(c.direction, CodecDirection::Decode | CodecDirection::Both)
})
.map(|c| c.max_resolution.clone())
.max()
.unwrap_or(MaxResolution::Unknown)
}
pub fn total_tflops_fp32(&self) -> f32 {
self.compute_caps
.iter()
.map(|c| c.estimated_tflops_fp32)
.sum()
}
fn detect_gpu_names() -> Vec<String> {
let mut names = Vec::new();
#[cfg(target_os = "linux")]
{
if let Ok(entries) = std::fs::read_dir("/sys/class/drm") {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with("card") && !name.contains('-') {
let label = std::fs::read_to_string(entry.path().join("device/label"))
.or_else(|_| {
std::fs::read_to_string(entry.path().join("device/product_name"))
})
.unwrap_or_default()
.trim()
.to_string();
if !label.is_empty() {
names.push(label);
}
}
}
}
if names.is_empty() {
if let Ok(output) = std::process::Command::new("lspci")
.args(["-d", "::0300", "-nn"])
.output()
{
let text = String::from_utf8(output.stdout).unwrap_or_default();
for line in text.lines() {
if let Some(pos) = line.find(": ") {
names.push(line[pos + 2..].to_string());
}
}
}
}
}
#[cfg(target_os = "windows")]
{
if let Ok(output) = std::process::Command::new("powershell")
.args([
"-NoProfile",
"-Command",
"Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name",
])
.output()
{
let text = String::from_utf8(output.stdout).unwrap_or_default();
for line in text.lines() {
let n = line.trim();
if !n.is_empty() {
names.push(n.to_string());
}
}
}
}
#[cfg(target_os = "macos")]
{
if let Ok(output) = std::process::Command::new("system_profiler")
.args(["SPDisplaysDataType"])
.output()
{
let text = String::from_utf8(output.stdout).unwrap_or_default();
for line in text.lines() {
let line = line.trim();
if line.contains("Chipset Model:") {
if let Some(model) = line.split(':').nth(1) {
names.push(model.trim().to_string());
}
}
}
}
}
names
}
fn infer_from_gpu_name(&mut self, gpu_name: &str) {
let lower = gpu_name.to_lowercase();
if lower.contains("nvidia")
|| lower.contains("geforce")
|| lower.contains("quadro")
|| lower.contains("tesla")
|| lower.contains("rtx")
|| lower.contains("gtx")
{
self.infer_nvidia_codecs(gpu_name, &lower);
}
if lower.contains("amd") || lower.contains("radeon") || lower.contains("rx ") {
self.infer_amd_codecs(gpu_name, &lower);
}
if lower.contains("intel")
|| lower.contains("uhd")
|| lower.contains("iris")
|| lower.contains("arc")
{
self.infer_intel_codecs(gpu_name, &lower);
}
if lower.contains("apple")
|| lower.contains("m1")
|| lower.contains("m2")
|| lower.contains("m3")
|| lower.contains("m4")
{
self.infer_apple_codecs(gpu_name);
}
}
fn infer_nvidia_codecs(&mut self, gpu_name: &str, lower: &str) {
let is_rtx40 = lower.contains("40") && lower.contains("rtx");
let is_rtx30 = lower.contains("30") && lower.contains("rtx");
let is_rtx20 = lower.contains("20") && lower.contains("rtx");
let is_gtx16 = lower.contains("16") && lower.contains("gtx");
let is_gtx10 = lower.contains("10") && lower.contains("gtx");
let codecs = vec![
(
VideoCodec::H264,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit8,
),
(
VideoCodec::H265,
CodecDirection::Both,
MaxResolution::UHD8K,
BitDepth::Bit10,
),
(
VideoCodec::VP9,
CodecDirection::Decode,
MaxResolution::UHD8K,
BitDepth::Bit10,
),
(
VideoCodec::MPEG2,
CodecDirection::Decode,
MaxResolution::FullHD,
BitDepth::Bit8,
),
(
VideoCodec::VC1,
CodecDirection::Decode,
MaxResolution::FullHD,
BitDepth::Bit8,
),
];
for (codec, dir, res, depth) in codecs {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec,
direction: dir,
max_resolution: res,
max_bit_depth: depth,
max_fps: None,
engine: "NVENC/NVDEC".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
});
}
if is_rtx40 {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Both,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "NVENC/NVDEC".into(),
source: CapabilitySource::Inferred,
confidence: 0.9,
});
} else if is_rtx30 {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Decode,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "NVDEC".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
});
}
let _ = (is_rtx20, is_gtx16, is_gtx10); }
fn infer_amd_codecs(&mut self, gpu_name: &str, lower: &str) {
let is_rdna3 = lower.contains("7") && (lower.contains("rx ") || lower.contains("radeon"));
let is_rdna2 = lower.contains("6") && (lower.contains("rx ") || lower.contains("radeon"));
let codecs = vec![
(
VideoCodec::H264,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit8,
),
(
VideoCodec::H265,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit10,
),
(
VideoCodec::VP9,
CodecDirection::Decode,
MaxResolution::UHD4K,
BitDepth::Bit10,
),
];
for (codec, dir, res, depth) in codecs {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec,
direction: dir,
max_resolution: res,
max_bit_depth: depth,
max_fps: None,
engine: "VCN".into(),
source: CapabilitySource::Inferred,
confidence: 0.8,
});
}
if is_rdna3 {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Both,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "VCN 4.0".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
});
} else if is_rdna2 {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Decode,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "VCN 3.0".into(),
source: CapabilitySource::Inferred,
confidence: 0.8,
});
}
}
fn infer_intel_codecs(&mut self, gpu_name: &str, lower: &str) {
let is_arc = lower.contains("arc");
let codecs = vec![
(
VideoCodec::H264,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit8,
),
(
VideoCodec::H265,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit10,
),
(
VideoCodec::VP9,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit10,
),
];
for (codec, dir, res, depth) in codecs {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec,
direction: dir,
max_resolution: res,
max_bit_depth: depth,
max_fps: None,
engine: "Quick Sync".into(),
source: CapabilitySource::Inferred,
confidence: 0.8,
});
}
if is_arc {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Both,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "Xe Media Engine".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
});
}
}
fn infer_apple_codecs(&mut self, gpu_name: &str) {
let codecs = vec![
(
VideoCodec::H264,
CodecDirection::Both,
MaxResolution::UHD4K,
BitDepth::Bit8,
),
(
VideoCodec::H265,
CodecDirection::Both,
MaxResolution::UHD8K,
BitDepth::Bit10,
),
(
VideoCodec::VP9,
CodecDirection::Decode,
MaxResolution::UHD4K,
BitDepth::Bit10,
),
(
VideoCodec::ProRes,
CodecDirection::Both,
MaxResolution::UHD8K,
BitDepth::Bit10,
),
];
for (codec, dir, res, depth) in codecs {
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec,
direction: dir,
max_resolution: res,
max_bit_depth: depth,
max_fps: None,
engine: "Apple Media Engine".into(),
source: CapabilitySource::Inferred,
confidence: 0.9,
});
}
let lower = gpu_name.to_lowercase();
if lower.contains("m3")
|| lower.contains("m4")
|| (lower.contains("m2")
&& (lower.contains("pro") || lower.contains("max") || lower.contains("ultra")))
{
self.codec_caps.push(CodecCapability {
device: gpu_name.to_string(),
codec: VideoCodec::AV1,
direction: CodecDirection::Decode,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: None,
engine: "Apple Media Engine".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
});
}
}
fn infer_compute_from_gpu(&mut self, gpu_name: &str) {
let lower = gpu_name.to_lowercase();
if lower.contains("nvidia")
|| lower.contains("geforce")
|| lower.contains("rtx")
|| lower.contains("gtx")
|| lower.contains("quadro")
|| lower.contains("tesla")
{
let (cuda, tflops_32, tflops_16, tensor, rt) =
if lower.contains("rtx 40") || lower.contains("rtx 4090") {
("8.9", 82.6_f32, 165.2_f32, true, true)
} else if lower.contains("rtx 4080") {
("8.9", 48.7, 97.5, true, true)
} else if lower.contains("rtx 4070") {
("8.9", 29.1, 58.3, true, true)
} else if lower.contains("rtx 30") || lower.contains("rtx 3090") {
("8.6", 35.6, 71.2, true, true)
} else if lower.contains("rtx 3080") {
("8.6", 29.8, 59.6, true, true)
} else if lower.contains("rtx 20") || lower.contains("rtx 2080") {
("7.5", 14.2, 28.4, true, true)
} else if lower.contains("gtx 1080") {
("6.1", 8.9, 0.0, false, false)
} else if lower.contains("gtx 10") {
("6.1", 6.5, 0.0, false, false)
} else {
("6.0", 5.0, 0.0, false, false)
};
self.compute_caps.push(ComputeCapability {
device: gpu_name.to_string(),
cuda_version: cuda.to_string(),
opencl_version: "3.0".into(),
vulkan_compute: true,
metal_compute: false,
direct_compute: "5.0".into(),
estimated_tflops_fp32: tflops_32,
estimated_tflops_fp16: tflops_16,
has_tensor_cores: tensor,
has_ray_tracing: rt,
source: CapabilitySource::Inferred,
});
}
if lower.contains("apple")
|| lower.contains("m1")
|| lower.contains("m2")
|| lower.contains("m3")
|| lower.contains("m4")
{
let tflops = if lower.contains("m4") || lower.contains("ultra") {
27.0_f32
} else if lower.contains("m3") || lower.contains("max") {
14.2
} else if lower.contains("m2") || lower.contains("pro") {
6.8
} else {
2.6
};
self.compute_caps.push(ComputeCapability {
device: gpu_name.to_string(),
cuda_version: String::new(),
opencl_version: String::new(),
vulkan_compute: false,
metal_compute: true,
direct_compute: String::new(),
estimated_tflops_fp32: tflops,
estimated_tflops_fp16: tflops * 2.0,
has_tensor_cores: true, has_ray_tracing: lower.contains("m3") || lower.contains("m4"),
source: CapabilitySource::Inferred,
});
}
}
#[cfg(target_os = "linux")]
fn refresh_linux(&mut self) {
if let Ok(output) = std::process::Command::new("vainfo")
.args(["--display", "drm"])
.output()
{
let text = String::from_utf8(output.stdout).unwrap_or_default();
let device = "VA-API";
for line in text.lines() {
let line = line.trim();
if !line.starts_with("VAProfile") {
continue;
}
let parts: Vec<&str> = line.split(':').collect();
if parts.len() < 2 {
continue;
}
let profile = parts[0].trim();
let entrypoint = parts[1].trim();
let codec = if profile.contains("H264") {
VideoCodec::H264
} else if profile.contains("HEVC") || profile.contains("H265") {
VideoCodec::H265
} else if profile.contains("VP9") {
VideoCodec::VP9
} else if profile.contains("VP8") {
VideoCodec::VP8
} else if profile.contains("AV1") {
VideoCodec::AV1
} else if profile.contains("MPEG2") {
VideoCodec::MPEG2
} else if profile.contains("JPEG") {
VideoCodec::JPEG
} else {
continue;
};
let direction = if entrypoint.contains("VLD") {
CodecDirection::Decode
} else if entrypoint.contains("Enc") {
CodecDirection::Encode
} else {
continue;
};
self.codec_caps.push(CodecCapability {
device: device.to_string(),
codec,
direction,
max_resolution: MaxResolution::UHD4K,
max_bit_depth: if profile.contains("10") {
BitDepth::Bit10
} else {
BitDepth::Bit8
},
max_fps: None,
engine: "VA-API".into(),
source: CapabilitySource::DirectQuery,
confidence: 1.0,
});
}
}
}
#[cfg(target_os = "windows")]
fn refresh_windows(&mut self) {
}
#[cfg(target_os = "macos")]
fn refresh_macos(&mut self) {
}
}
impl Default for CodecMonitor {
fn default() -> Self {
Self::new().unwrap_or(Self {
codec_caps: Vec::new(),
compute_caps: Vec::new(),
})
}
}
impl std::fmt::Display for VideoCodec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::H264 => write!(f, "H.264/AVC"),
Self::H265 => write!(f, "H.265/HEVC"),
Self::H266 => write!(f, "H.266/VVC"),
Self::VP8 => write!(f, "VP8"),
Self::VP9 => write!(f, "VP9"),
Self::AV1 => write!(f, "AV1"),
Self::MPEG2 => write!(f, "MPEG-2"),
Self::MPEG4 => write!(f, "MPEG-4"),
Self::VC1 => write!(f, "VC-1"),
Self::JPEG => write!(f, "JPEG"),
Self::ProRes => write!(f, "ProRes"),
Self::Other(s) => write!(f, "{}", s),
}
}
}
impl std::fmt::Display for MaxResolution {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SD => write!(f, "SD (480p)"),
Self::HD => write!(f, "HD (720p)"),
Self::FullHD => write!(f, "Full HD (1080p)"),
Self::QHD => write!(f, "QHD (1440p)"),
Self::UHD4K => write!(f, "4K UHD"),
Self::UHD8K => write!(f, "8K UHD"),
Self::Unknown => write!(f, "Unknown"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_codec_monitor_creation() {
let monitor = CodecMonitor::new();
assert!(monitor.is_ok());
}
#[test]
fn test_codec_monitor_default() {
let monitor = CodecMonitor::default();
let _ = monitor.capabilities();
let _ = monitor.compute_capabilities();
let _ = monitor.total_tflops_fp32();
}
#[test]
fn test_nvidia_inference() {
let mut monitor = CodecMonitor {
codec_caps: Vec::new(),
compute_caps: Vec::new(),
};
monitor.infer_from_gpu_name("NVIDIA GeForce RTX 4090");
assert!(!monitor.codec_caps.is_empty());
assert!(monitor.can_decode(&VideoCodec::H265));
assert!(monitor.can_encode(&VideoCodec::H264));
assert!(monitor.can_decode(&VideoCodec::AV1));
assert!(monitor.can_encode(&VideoCodec::AV1)); }
#[test]
fn test_compute_inference() {
let mut monitor = CodecMonitor {
codec_caps: Vec::new(),
compute_caps: Vec::new(),
};
monitor.infer_compute_from_gpu("NVIDIA GeForce RTX 4090");
assert!(!monitor.compute_caps.is_empty());
assert!(monitor.compute_caps[0].has_tensor_cores);
assert!(monitor.compute_caps[0].has_ray_tracing);
assert!(monitor.compute_caps[0].estimated_tflops_fp32 > 50.0);
}
#[test]
fn test_serialization() {
let cap = CodecCapability {
device: "Test GPU".into(),
codec: VideoCodec::AV1,
direction: CodecDirection::Decode,
max_resolution: MaxResolution::UHD8K,
max_bit_depth: BitDepth::Bit10,
max_fps: Some(60),
engine: "NVDEC".into(),
source: CapabilitySource::Inferred,
confidence: 0.85,
};
let json = serde_json::to_string(&cap).unwrap();
assert!(json.contains("AV1"));
let _: CodecCapability = serde_json::from_str(&json).unwrap();
}
}