ironmonitor 7.0.2

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! Hardware codec and media capability detection — video encode/decode, GPU compute.
//!
//! # Platform Support
//!
//! - **Linux**: Reads `/sys/class/drm/card*/device/`, `vainfo`, `vdpauinfo`
//! - **Windows**: Uses WMI (`Win32_VideoController`), DXVA queries
//! - **macOS**: Uses `VTIsHardwareDecodeSupported`, system_profiler
//!
//! ## Inference
//!
//! When direct API queries aren't available, codec support is *inferred* from
//! GPU model name and generation using a built-in capability database.
//!
//! # Examples
//!
//! ```no_run
//! use ironmonitor::codec::CodecMonitor;
//!
//! let monitor = CodecMonitor::new().unwrap();
//! for cap in monitor.capabilities() {
//!     println!("{}: {} {:?} (max {})",
//!         cap.device, cap.codec, cap.direction, cap.max_resolution);
//! }
//! ```

use crate::error::IronError;
use serde::{Deserialize, Serialize};

/// Video codec type
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum VideoCodec {
    H264,
    H265,
    H266,
    VP8,
    VP9,
    AV1,
    MPEG2,
    MPEG4,
    VC1,
    JPEG,
    ProRes,
    Other(String),
}

/// Codec direction — encode, decode, or both
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CodecDirection {
    Decode,
    Encode,
    Both,
}

/// Maximum supported resolution
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum MaxResolution {
    SD,     // 720x480
    HD,     // 1280x720
    FullHD, // 1920x1080
    QHD,    // 2560x1440
    UHD4K,  // 3840x2160
    UHD8K,  // 7680x4320
    Unknown,
}

/// Color depth / bit depth support
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BitDepth {
    Bit8,
    Bit10,
    Bit12,
    Unknown,
}

/// Source of the capability information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CapabilitySource {
    /// Detected via hardware/driver API
    DirectQuery,
    /// Inferred from GPU model/generation
    Inferred,
    /// User-provided override
    Manual,
}

/// A single codec capability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodecCapability {
    /// Device name (e.g., "NVIDIA RTX 4090", "Intel UHD 770")
    pub device: String,
    /// Codec type
    pub codec: VideoCodec,
    /// Direction (encode/decode/both)
    pub direction: CodecDirection,
    /// Maximum supported resolution
    pub max_resolution: MaxResolution,
    /// Maximum bit depth
    pub max_bit_depth: BitDepth,
    /// Maximum FPS at the maximum resolution.
    ///
    /// `None` on every path this crate has, and that is the honest value. The
    /// field held a literal `60` at all twelve construction sites while the
    /// ontology declared it `Derived` *from* `codec` and `max_resolution` --
    /// arithmetic that does not exist. 60 is also not a frame rate an 8K HEVC
    /// engine reaches, so the constant was not even a safe floor.
    pub max_fps: Option<u32>,
    /// Hardware engine name (e.g., "NVENC", "NVDEC", "VCE", "QuickSync")
    pub engine: String,
    /// How this capability was determined
    pub source: CapabilitySource,
    /// Confidence level (0.0-1.0), 1.0 = directly queried
    pub confidence: f32,
}

/// Compute capability for GPGPU workloads
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputeCapability {
    /// Device name
    pub device: String,
    /// CUDA compute capability (e.g., "8.9") — NVIDIA only
    pub cuda_version: String,
    /// OpenCL version supported
    pub opencl_version: String,
    /// Vulkan compute supported
    pub vulkan_compute: bool,
    /// Metal compute supported (Apple)
    pub metal_compute: bool,
    /// DirectCompute version (Windows)
    pub direct_compute: String,
    /// Estimated TFLOPS (FP32)
    pub estimated_tflops_fp32: f32,
    /// Estimated TFLOPS (FP16 / half precision)
    pub estimated_tflops_fp16: f32,
    /// Tensor core / AI accelerator present
    pub has_tensor_cores: bool,
    /// Ray tracing hardware acceleration
    pub has_ray_tracing: bool,
    /// Source of capability info
    pub source: CapabilitySource,
}

/// Monitor for codec and compute capabilities
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();

        // Detect GPU names for inference
        let gpu_names = Self::detect_gpu_names();

        // Try direct query first, then fall back to inference
        #[cfg(target_os = "linux")]
        self.refresh_linux();

        #[cfg(target_os = "windows")]
        self.refresh_windows();

        #[cfg(target_os = "macos")]
        self.refresh_macos();

        // Inference: if no capabilities detected directly, infer from GPU names
        if self.codec_caps.is_empty() {
            for name in &gpu_names {
                self.infer_from_gpu_name(name);
            }
        }

        // Always infer compute capabilities from GPU names
        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
    }

    /// Check if a specific codec is supported for decode.
    pub fn can_decode(&self, codec: &VideoCodec) -> bool {
        self.codec_caps.iter().any(|c| {
            &c.codec == codec
                && matches!(c.direction, CodecDirection::Decode | CodecDirection::Both)
        })
    }

    /// Check if a specific codec is supported for encode.
    pub fn can_encode(&self, codec: &VideoCodec) -> bool {
        self.codec_caps.iter().any(|c| {
            &c.codec == codec
                && matches!(c.direction, CodecDirection::Encode | CodecDirection::Both)
        })
    }

    /// Get max decode resolution for a codec.
    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)
    }

    /// Get estimated TFLOPS (FP32) across all devices.
    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")]
        {
            // Read from /sys/class/drm/card*/device/
            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);
                        }
                    }
                }
            }

            // Also try lspci for GPU names
            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
    }

    /// Infer codec capabilities from GPU model name.
    fn infer_from_gpu_name(&mut self, gpu_name: &str) {
        let lower = gpu_name.to_lowercase();

        // NVIDIA inference
        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);
        }

        // AMD inference
        if lower.contains("amd") || lower.contains("radeon") || lower.contains("rx ") {
            self.infer_amd_codecs(gpu_name, &lower);
        }

        // Intel inference
        if lower.contains("intel")
            || lower.contains("uhd")
            || lower.contains("iris")
            || lower.contains("arc")
        {
            self.infer_intel_codecs(gpu_name, &lower);
        }

        // Apple inference
        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");

        // All modern NVIDIA: H.264, H.265 decode/encode
        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,
            });
        }

        // AV1: RTX 40xx decode+encode, RTX 30xx decode only
        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); // used for generation match above
    }

    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,
            });
        }

        // M3+ and M2 Pro/Max/Ultra have AV1 decode
        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,
            });
        }
    }

    /// Infer compute capabilities from GPU name.
    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, // Apple Neural Engine
                has_ray_tracing: lower.contains("m3") || lower.contains("m4"),
                source: CapabilitySource::Inferred,
            });
        }
    }

    #[cfg(target_os = "linux")]
    fn refresh_linux(&mut self) {
        // Try vainfo for VA-API capabilities
        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;
                }
                // "VAProfileH264Main            : VAEntrypointVLD"
                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) {
        // Query DXVA capabilities is complex; we rely on inference primarily
        // But we can check for basic GPU info
    }

    #[cfg(target_os = "macos")]
    fn refresh_macos(&mut self) {
        // macOS: VideoToolbox capabilities are best inferred from chip name
    }
}

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)); // RTX 40xx
    }

    #[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();
    }
}