mediaway-encoder 0.1.5

Hardware-accelerated video/audio encoding (OS-native backends)
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//! Hardware-gated tests for [`super::D3d12VideoEncoder`] — real `ID3D12Device`, real
//! `ID3D12VideoDevice3` H.264 support check, real `EncodeFrame` submission. Skips
//! gracefully (never fails the default suite) when this machine/driver lacks D3D12
//! native video-encode support.

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::print_stderr,
    reason = "unit tests"
)]

use crate::{RateControlConfig, VideoEncoder, VideoEncoderConfig, VideoInputPreference};
use mediaway_common::{
    Bytes, CodecKind, GpuDeviceHandle, NativeHandle, PixelFormat, Rational, VideoFrame,
    VideoFrameStorage,
};
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
use windows::Win32::Graphics::Direct3D12::{
    D3D12_MESSAGE, D3D12CreateDevice, D3D12GetDebugInterface, ID3D12Debug, ID3D12Device,
    ID3D12InfoQueue,
};
use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIAdapter1, IDXGIFactory1};
use windows::core::Interface;

use super::D3d12VideoEncoder;

#[path = "d3d12_video_encode_tests_av1.rs"]
mod av1_tests;

// D3D12_FEATURE_VIDEO_ENCODER_OUTPUT_RESOLUTION reports a real minimum encode
// resolution on real hardware (observed: 160x64 on an NVIDIA RTX 4090) — below it,
// CreateVideoEncoderHeap fails with E_INVALIDARG. 176x144 (QCIF, 16-pixel MB-aligned)
// comfortably clears that floor on every driver this backend has been tested against.
const WIDTH: u32 = 176;
const HEIGHT: u32 = 144;

// HEVC's SPS requires pic_width/height_in_luma_samples to be an exact multiple of the
// driver-reported minimum coding-unit size (8/16/32/64 depending on hardware) — this
// backend has no conformance-window/cropping support (see bitstream_hevc.rs), so the test
// picks a size that is a multiple of every legal CU size up to 64.
const WIDTH_HEVC: u32 = 256;
const HEIGHT_HEVC: u32 = 192;

// AV1's D3D12_FEATURE_VIDEO_ENCODER_OUTPUT_RESOLUTION reports a real minimum encode
// resolution on real hardware (observed: 192x128 on an NVIDIA RTX 4090, higher than
// H.264/HEVC's) — reuse the HEVC test's 256x192 (already known to clear every codec's
// observed minimum on this hardware, and a multiple of AV1's `ResolutionWidthMultipleRequirement`).
const WIDTH_AV1: u32 = WIDTH_HEVC;
const HEIGHT_AV1: u32 = HEIGHT_HEVC;

/// Parse Annex-B `nal_unit_type` values (start-code-prefixed NALs) out of `payload`.
fn nal_unit_types(payload: &[u8]) -> Vec<u8> {
    let mut types = Vec::new();
    let mut i = 0usize;
    while i + 3 < payload.len() {
        let is_start_code_3 = payload[i] == 0 && payload[i + 1] == 0 && payload[i + 2] == 1;
        let is_start_code_4 = i + 4 < payload.len()
            && payload[i] == 0
            && payload[i + 1] == 0
            && payload[i + 2] == 0
            && payload[i + 3] == 1;
        if is_start_code_4 {
            types.push(payload[i + 4] & 0x1F);
            i += 5;
        } else if is_start_code_3 {
            types.push(payload[i + 3] & 0x1F);
            i += 4;
        } else {
            i += 1;
        }
    }
    types
}

/// Parse Annex-B HEVC `nal_unit_type` values (2-byte NAL header: `forbidden_zero_bit`(1) +
/// `nal_unit_type`(6) + `nuh_layer_id`(6) + `nuh_temporal_id_plus1`(3), Rec. ITU-T H.265
/// §7.3.1.2 — `nal_unit_type` is the top 6 bits of the first header byte, after the
/// 1-bit-zero `forbidden_zero_bit`).
fn nal_unit_types_hevc(payload: &[u8]) -> Vec<u8> {
    let mut types = Vec::new();
    let mut i = 0usize;
    while i + 3 < payload.len() {
        let is_start_code_3 = payload[i] == 0 && payload[i + 1] == 0 && payload[i + 2] == 1;
        let is_start_code_4 = i + 4 < payload.len()
            && payload[i] == 0
            && payload[i + 1] == 0
            && payload[i + 2] == 0
            && payload[i + 3] == 1;
        if is_start_code_4 && i + 5 < payload.len() {
            types.push((payload[i + 4] >> 1) & 0x3F);
            i += 6;
        } else if is_start_code_3 && i + 4 < payload.len() {
            types.push((payload[i + 3] >> 1) & 0x3F);
            i += 5;
        } else {
            i += 1;
        }
    }
    types
}

fn nv12_frame(pts: i64) -> VideoFrame {
    nv12_frame_sized(pts, WIDTH, HEIGHT)
}

fn nv12_frame_sized(pts: i64, width: u32, height: u32) -> VideoFrame {
    let len = (width as usize) * (height as usize) + (width as usize) * (height as usize) / 2;
    // Mid-gray luma+chroma — content doesn't matter, only that a real encode runs.
    let data = vec![128u8; len];
    VideoFrame {
        pts,
        duration: 1,
        width,
        height,
        format: PixelFormat::Nv12,
        storage: VideoFrameStorage::Cpu {
            data: Bytes::from(data),
        },
    }
}

/// Open a real hardware D3D12 device on adapter 0, enabling the debug layer first so
/// [`dump_d3d12_info_queue`] can report the driver's exact validation messages on
/// failure. Returns `None` (after printing why) if this machine has no usable D3D12
/// adapter — never panics.
fn open_real_d3d12_device() -> Option<ID3D12Device> {
    let mut debug: Option<ID3D12Debug> = None;
    if unsafe { D3D12GetDebugInterface(&raw mut debug) }.is_ok() {
        if let Some(debug) = debug {
            unsafe { debug.EnableDebugLayer() };
        }
    }

    let factory: IDXGIFactory1 = match unsafe { CreateDXGIFactory1() } {
        Ok(f) => f,
        Err(e) => {
            eprintln!("skip: CreateDXGIFactory1 ({e:?})");
            return None;
        }
    };
    let adapter: IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(0) } {
        Ok(a) => a,
        Err(e) => {
            eprintln!("skip: EnumAdapters1 ({e:?})");
            return None;
        }
    };
    let mut device: Option<ID3D12Device> = None;
    if unsafe { D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &raw mut device) }.is_err() {
        eprintln!("skip: D3D12CreateDevice failed");
        return None;
    }
    if device.is_none() {
        eprintln!("skip: null D3D12 device");
    }
    device
}

/// Print every message the D3D12 debug layer has queued (real validation errors —
/// e.g. resource-state/heap-type mismatches — with the exact resource/call named),
/// then clear the queue. No-op when `iq` is `None` (debug layer unavailable).
fn dump_d3d12_info_queue(iq: Option<&ID3D12InfoQueue>) {
    let Some(iq) = iq else { return };
    let n = unsafe { iq.GetNumStoredMessages() };
    for i in 0..n {
        let mut len = 0usize;
        if unsafe { iq.GetMessage(i, None, &raw mut len) }.is_err() || len == 0 {
            continue;
        }
        // 8-byte-aligned buffer: `D3D12_MESSAGE` needs pointer alignment on 64-bit targets.
        let mut buf: Vec<u64> = vec![0; len.div_ceil(8)];
        let msg_ptr = buf.as_mut_ptr().cast::<D3D12_MESSAGE>();
        if unsafe { iq.GetMessage(i, Some(msg_ptr), &raw mut len) }.is_ok() {
            // SAFETY: `GetMessage` just wrote a valid `D3D12_MESSAGE` into `buf`.
            let msg = unsafe { &*msg_ptr };
            // SAFETY: `pDescription` is a NUL-terminated ASCII string `DescriptionByteLength`
            // bytes long (including the NUL), per the D3D12 debug-layer contract.
            let desc = unsafe {
                std::slice::from_raw_parts(
                    msg.pDescription,
                    msg.DescriptionByteLength.saturating_sub(1),
                )
            };
            eprintln!("D3D12 InfoQueue[{i}]: {}", String::from_utf8_lossy(desc));
        }
    }
    unsafe { iq.ClearStoredMessages() };
}

/// Real D3D12 device → real H.264 `ID3D12VideoDevice3` support check → real
/// `EncodeFrame` submissions → real Annex-B output (SPS `nal_unit_type == 7`, IDR
/// `nal_unit_type == 5` present in every packet). Skips (does not fail) if this
/// machine's adapter/driver lacks D3D12 native video-encode support.
#[test]
fn d3d12_native_h264_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::H264,
        width: WIDTH,
        height: HEIGHT,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open failed ({e:?}) — no D3D12 H.264 video-encode \
                 support on this device/driver?"
            );
            return;
        }
    };

    let mut packets = 0usize;
    for i in 0..3i64 {
        let frame = nv12_frame(i);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i}");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        assert!(packet.is_keyframe, "packet {i} should be an IDR keyframe");

        let types = nal_unit_types(&packet.payload);
        assert!(
            types.contains(&7),
            "packet {i} missing SPS NAL (type 7); found types {types:?}"
        );
        assert!(
            types.contains(&5),
            "packet {i} missing IDR slice NAL (type 5); found types {types:?}"
        );
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!("d3d12 native h264 encode ok: {packets} packets, all with real SPS+IDR Annex-B NALs");
}

/// Real D3D12 device → real H.264 GOP-mode (`gop_size: 3`) → real `EncodeFrame`
/// submissions with single-forward-reference P frames → real Annex-B I/P NAL cadence
/// (`GOPLength=3`, `PPicturePeriod=1`: IDR, P, P, IDR, P, P, IDR — type 5 vs type 1) and
/// `Packet::is_keyframe` matching. Skips (does not fail) if this machine's adapter/driver
/// lacks D3D12 native H.264 video-encode support, or falls back to IDR-only if the driver
/// can't honor `MaxReferenceFramesInDPB >= 1` for this config (ADR-0007's 2026-08-06
/// addendum) — in that case this test still passes (every packet is then an IDR, a valid,
/// documented fallback).
#[test]
fn d3d12_native_h264_gop_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::H264,
        width: WIDTH,
        height: HEIGHT,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 3,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open (GOP) failed ({e:?}) — no D3D12 H.264 video-encode \
                 support on this device/driver?"
            );
            return;
        }
    };

    let mut keyframe_flags = Vec::new();
    let mut nal_type_cadence = Vec::new();
    for i in 0..7i64 {
        let frame = nv12_frame(i);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (GOP) failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (GOP)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (GOP) failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        let types = nal_unit_types(&packet.payload);
        let is_idr_nal = types.contains(&5);
        let is_p_nal = types.contains(&1);
        assert!(
            is_idr_nal || is_p_nal,
            "packet {i} has neither an IDR (type 5) nor a P (type 1) slice NAL; found {types:?}"
        );
        assert_eq!(
            packet.is_keyframe, is_idr_nal,
            "packet {i}: Packet::is_keyframe ({}) disagrees with its own NAL type {types:?}",
            packet.is_keyframe
        );
        keyframe_flags.push(packet.is_keyframe);
        nal_type_cadence.push(if is_idr_nal { 'I' } else { 'P' });
    }

    enc.flush().expect("flush");
    let cadence: String = nal_type_cadence.iter().collect();
    // Either GOP mode landed (real `IPPIPPI` cadence) or the driver couldn't honor
    // `MaxReferenceFramesInDPB >= 1` and this backend silently fell back to IDR-only
    // (`IIIIIII`) — both are valid, documented outcomes (ADR-0007's 2026-08-06
    // addendum); anything else (a cadence that isn't periodic-by-3 and isn't
    // all-IDR) is a real bug.
    let is_gop_cadence = cadence == "IPPIPPI";
    let is_idr_only_fallback = keyframe_flags.iter().all(|&k| k);
    assert!(
        is_gop_cadence || is_idr_only_fallback,
        "unexpected I/P cadence {cadence:?} — neither GOP mode's IPPIPPI nor an all-IDR fallback"
    );
    eprintln!("d3d12 native h264 GOP encode ok: cadence {cadence:?}");
}

/// Real D3D12 device → real H.264 row-based intra-refresh (`intra_refresh_period: 4`) →
/// real `EncodeFrame` submissions → only the very first packet is an IDR (type 5); every
/// packet after that is a P slice (type 1) forever — an unbounded GOP, unlike
/// [`d3d12_native_h264_gop_encode_or_skip`]'s periodic re-IDR. `Packet::is_keyframe` must
/// agree. Falls back to the documented all-IDR outcome if the driver can't honor
/// `D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_ROW_BASED` for this config (ADR-0007's
/// 2026-08-06 addendum) — that's still a pass, not a failure.
#[test]
fn d3d12_native_h264_intra_refresh_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::H264,
        width: WIDTH,
        height: HEIGHT,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: Some(4),
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open (intra-refresh) failed ({e:?}) — no D3D12 \
                 H.264 video-encode support on this device/driver?"
            );
            return;
        }
    };

    let mut nal_type_cadence = Vec::new();
    for i in 0..9i64 {
        let frame = nv12_frame(i);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (intra-refresh) failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (intra-refresh)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (intra-refresh) failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        let types = nal_unit_types(&packet.payload);
        let is_idr_nal = types.contains(&5);
        let is_p_nal = types.contains(&1);
        assert!(
            is_idr_nal || is_p_nal,
            "packet {i} has neither an IDR (type 5) nor a P (type 1) slice NAL; found {types:?}"
        );
        assert_eq!(
            packet.is_keyframe, is_idr_nal,
            "packet {i}: Packet::is_keyframe ({}) disagrees with its own NAL type {types:?}",
            packet.is_keyframe
        );
        nal_type_cadence.push(if is_idr_nal { 'I' } else { 'P' });
    }

    enc.flush().expect("flush");
    let cadence: String = nal_type_cadence.iter().collect();
    // Either intra-refresh landed (real "IPPPPPPPP" — exactly one IDR, ever) or the
    // driver couldn't honor row-based intra refresh and this backend fell back to
    // IDR-only ("IIIIIIIII"); anything else is a real bug.
    let is_intra_refresh_cadence = cadence == "IPPPPPPPP";
    let is_idr_only_fallback = cadence == "IIIIIIIII";
    assert!(
        is_intra_refresh_cadence || is_idr_only_fallback,
        "unexpected I/P cadence {cadence:?} — neither intra-refresh's single-IDR-forever \
         nor an all-IDR fallback"
    );
    eprintln!("d3d12 native h264 intra-refresh encode ok: cadence {cadence:?}");
}

/// Real D3D12 device → `VideoEncoderConfig::rate_control` requested → real `EncodeFrame`
/// submissions, then a live [`VideoEncoder::set_bitrate`] retarget mid-session with more
/// frames pushed after it. Real CBR when this driver accepts `RateControlState::Cbr` for
/// the chosen (IDR-only) GOP tier, the documented fixed-QP fallback otherwise
/// (`open`'s one-extra-probe design, see `d3d12_video_encode.rs`'s doc) — this test cannot
/// tell which outcome landed without reaching into a private field, so it accepts either:
/// `set_bitrate` returning `Ok(())` (real CBR) or `Err(EncodeError::Unsupported)` (fixed-QP
/// fallback) are both legitimate, and either way encoding must keep working right after the
/// call — that's the actual thing under test. Skips (does not fail) if this machine's
/// adapter/driver lacks D3D12 native H.264 video-encode support at all.
#[test]
fn d3d12_native_h264_cbr_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::H264,
        width: WIDTH,
        height: HEIGHT,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: Some(RateControlConfig {
            target_bitrate_bps: 500_000,
            vbv_buffer_size_bytes: None,
        }),
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: D3d12VideoEncoder::open (H.264 CBR) failed ({e:?})");
            return;
        }
    };

    for i in 0..3i64 {
        let frame = nv12_frame(i);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (H.264 CBR) failed ({e:?})");
            return;
        }
        match enc.poll_packet() {
            Ok(Some(p)) => assert!(!p.payload.is_empty(), "packet {i} payload is empty"),
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (H.264 CBR)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (H.264 CBR) failed ({e:?})");
                return;
            }
        }
    }

    let cbr_selected = match enc.set_bitrate(250_000) {
        Ok(()) => true,
        Err(e) => {
            eprintln!("d3d12 h264 set_bitrate: {e:?} (fixed-QP fallback, expected)");
            false
        }
    };

    for i in 3..6i64 {
        let frame = nv12_frame(i);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame after set_bitrate failed ({e:?})");
            return;
        }
        match enc.poll_packet() {
            Ok(Some(p)) => assert!(
                !p.payload.is_empty(),
                "post-set_bitrate packet {i} payload is empty"
            ),
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (post-set_bitrate)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (post-set_bitrate) failed ({e:?})");
                return;
            }
        }
    }

    enc.flush().expect("flush");
    eprintln!(
        "d3d12 native h264 CBR encode ok: cbr_selected={cbr_selected}, encoding kept working \
         across set_bitrate"
    );
}

/// Real D3D12 device → real HEVC `ID3D12VideoDevice3` support check → real `EncodeFrame`
/// submissions → real Annex-B output (VPS `nal_unit_type == 32`, SPS `== 33`,
/// PPS `== 34`, IDR `== 19` (`IDR_W_RADL`) or `== 20` (`IDR_N_LP`) present in every
/// packet). Skips (does not fail) if this machine's adapter/driver lacks D3D12 native HEVC
/// video-encode support.
#[test]
fn d3d12_native_hevc_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Hevc,
        width: WIDTH_HEVC,
        height: HEIGHT_HEVC,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open (HEVC) failed ({e:?}) — no D3D12 HEVC video-encode \
                 support on this device/driver?"
            );
            return;
        }
    };

    let mut packets = 0usize;
    for i in 0..3i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (HEVC) failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (HEVC) failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        assert!(packet.is_keyframe, "packet {i} should be an IDR keyframe");

        let types = nal_unit_types_hevc(&packet.payload);
        assert!(
            types.contains(&32),
            "packet {i} missing VPS NAL (type 32); found types {types:?}"
        );
        assert!(
            types.contains(&33),
            "packet {i} missing SPS NAL (type 33); found types {types:?}"
        );
        assert!(
            types.contains(&34),
            "packet {i} missing PPS NAL (type 34); found types {types:?}"
        );
        assert!(
            types.contains(&19) || types.contains(&20),
            "packet {i} missing IDR slice NAL (type 19/20); found types {types:?}"
        );
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!(
        "d3d12 native hevc encode ok: {packets} packets, all with real VPS+SPS+PPS+IDR Annex-B NALs"
    );
}

/// Real D3D12 device → real HEVC GOP-mode (`gop_size: 3`) → real `EncodeFrame`
/// submissions with single-forward-reference P frames → real Annex-B I/P NAL cadence
/// (IDR `nal_unit_type` 19/20 vs P `nal_unit_type` 1, `TRAIL_R`) and `Packet::is_keyframe`
/// matching. Skips (does not fail) if this machine's adapter/driver lacks D3D12 native HEVC
/// video-encode support, or falls back to IDR-only if the driver can't honor
/// `MaxReferenceFramesInDPB >= 1` for this config (ADR-0007's 2026-08-06 addendum) — in
/// that case this test still passes (every packet is then an IDR, a valid fallback).
#[test]
fn d3d12_native_hevc_gop_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Hevc,
        width: WIDTH_HEVC,
        height: HEIGHT_HEVC,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 3,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open (HEVC GOP) failed ({e:?}) — no D3D12 HEVC \
                 video-encode support on this device/driver?"
            );
            return;
        }
    };

    let mut keyframe_flags = Vec::new();
    let mut nal_type_cadence = Vec::new();
    for i in 0..7i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (HEVC GOP) failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC GOP)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (HEVC GOP) failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        let types = nal_unit_types_hevc(&packet.payload);
        let is_idr_nal = types.contains(&19) || types.contains(&20);
        let is_p_nal = types.contains(&1);
        assert!(
            is_idr_nal || is_p_nal,
            "packet {i} has neither an IDR (type 19/20) nor a P (type 1, TRAIL_R) slice NAL; \
             found {types:?}"
        );
        assert_eq!(
            packet.is_keyframe, is_idr_nal,
            "packet {i}: Packet::is_keyframe ({}) disagrees with its own NAL type {types:?}",
            packet.is_keyframe
        );
        keyframe_flags.push(packet.is_keyframe);
        nal_type_cadence.push(if is_idr_nal { 'I' } else { 'P' });
    }

    enc.flush().expect("flush");
    let cadence: String = nal_type_cadence.iter().collect();
    let is_gop_cadence = cadence == "IPPIPPI";
    let is_idr_only_fallback = keyframe_flags.iter().all(|&k| k);
    assert!(
        is_gop_cadence || is_idr_only_fallback,
        "unexpected I/P cadence {cadence:?} — neither GOP mode's IPPIPPI nor an all-IDR fallback"
    );
    eprintln!("d3d12 native hevc GOP encode ok: cadence {cadence:?}");
}

/// HEVC sibling of [`d3d12_native_h264_intra_refresh_encode_or_skip`] — see that test's
/// doc for the shared design (unbounded GOP, single startup IDR, cadence assertion).
#[test]
fn d3d12_native_hevc_intra_refresh_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Hevc,
        width: WIDTH_HEVC,
        height: HEIGHT_HEVC,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: Some(4),
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!(
                "skip: D3d12VideoEncoder::open (HEVC intra-refresh) failed ({e:?}) — no D3D12 \
                 HEVC video-encode support on this device/driver?"
            );
            return;
        }
    };

    let mut nal_type_cadence = Vec::new();
    for i in 0..9i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (HEVC intra-refresh) failed ({e:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(p)) => p,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC intra-refresh)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (HEVC intra-refresh) failed ({e:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        let types = nal_unit_types_hevc(&packet.payload);
        let is_idr_nal = types.contains(&19) || types.contains(&20);
        let is_p_nal = types.contains(&1);
        assert!(
            is_idr_nal || is_p_nal,
            "packet {i} has neither an IDR (type 19/20) nor a P (type 1, TRAIL_R) slice NAL; \
             found {types:?}"
        );
        assert_eq!(
            packet.is_keyframe, is_idr_nal,
            "packet {i}: Packet::is_keyframe ({}) disagrees with its own NAL type {types:?}",
            packet.is_keyframe
        );
        nal_type_cadence.push(if is_idr_nal { 'I' } else { 'P' });
    }

    enc.flush().expect("flush");
    let cadence: String = nal_type_cadence.iter().collect();
    let is_intra_refresh_cadence = cadence == "IPPPPPPPP";
    let is_idr_only_fallback = cadence == "IIIIIIIII";
    assert!(
        is_intra_refresh_cadence || is_idr_only_fallback,
        "unexpected I/P cadence {cadence:?} — neither intra-refresh's single-IDR-forever \
         nor an all-IDR fallback"
    );
    eprintln!("d3d12 native hevc intra-refresh encode ok: cadence {cadence:?}");
}

/// HEVC sibling of [`d3d12_native_h264_cbr_encode_or_skip`] — same real CBR-or-documented
/// fixed-QP-fallback design, `set_bitrate` retarget mid-session, same either-outcome
/// acceptance criterion (see that test's doc for why).
#[test]
fn d3d12_native_hevc_cbr_encode_or_skip() {
    let Some(device) = open_real_d3d12_device() else {
        return;
    };
    let Some(handle) = NativeHandle::new(Interface::as_raw(&device) as usize) else {
        eprintln!("skip: null D3D12 device pointer");
        return;
    };
    let info_queue: Option<ID3D12InfoQueue> = device.cast().ok();

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Hevc,
        width: WIDTH_HEVC,
        height: HEIGHT_HEVC,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: Some(GpuDeviceHandle::DirectX12(handle)),
        gop_size: 1,
        rate_control: Some(RateControlConfig {
            target_bitrate_bps: 500_000,
            vbv_buffer_size_bytes: None,
        }),
        intra_refresh_period: None,
    };

    let mut enc = match D3d12VideoEncoder::open(&cfg) {
        Ok(e) => e,
        Err(e) => {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: D3d12VideoEncoder::open (HEVC CBR) failed ({e:?})");
            return;
        }
    };

    for i in 0..3i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame (HEVC CBR) failed ({e:?})");
            return;
        }
        match enc.poll_packet() {
            Ok(Some(p)) => assert!(!p.payload.is_empty(), "packet {i} payload is empty"),
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC CBR)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (HEVC CBR) failed ({e:?})");
                return;
            }
        }
    }

    let cbr_selected = match enc.set_bitrate(250_000) {
        Ok(()) => true,
        Err(e) => {
            eprintln!("d3d12 hevc set_bitrate: {e:?} (fixed-QP fallback, expected)");
            false
        }
    };

    for i in 3..6i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(e) = enc.push_frame(&frame) {
            dump_d3d12_info_queue(info_queue.as_ref());
            eprintln!("skip: push_frame after set_bitrate failed ({e:?})");
            return;
        }
        match enc.poll_packet() {
            Ok(Some(p)) => assert!(
                !p.payload.is_empty(),
                "post-set_bitrate packet {i} payload is empty"
            ),
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (post-set_bitrate)");
                return;
            }
            Err(e) => {
                eprintln!("skip: poll_packet (post-set_bitrate) failed ({e:?})");
                return;
            }
        }
    }

    enc.flush().expect("flush");
    eprintln!(
        "d3d12 native hevc CBR encode ok: cbr_selected={cbr_selected}, encoding kept working \
         across set_bitrate"
    );
}