mediaway-encoder 0.1.4

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
//! Hardware-gated test for [`super::VulkanVideoEncoder`] — the real,
//! reusable, multi-frame `crate::VideoEncoder` impl (as opposed
//! to `session_tests.rs`'s one-shot `encode_synthetic_intra_frame`
//! diagnostic). Skips (never fails the default suite) when this machine's
//! Vulkan loader/driver lacks an H.264 encode queue family — same convention
//! as `session_tests.rs`/`probe_tests.rs`.

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::print_stderr,
    reason = "test modules may unwrap / print"
)]

use crate::{RateControlConfig, VideoEncoder, VideoEncoderConfig, VideoInputPreference};
use mediaway_common::{Bytes, CodecKind, PixelFormat, Rational, VideoFrame, VideoFrameStorage};

use crate::vulkan::VulkanVideoEncoder;
use crate::vulkan::nal::{NalHeader, scan_nal_headers, scan_nal_headers_hevc, scan_obu_headers};

// Comfortably clears this crate's reference RTX 4090's observed 160x64
// minimum coded extent (see `adr/0001`'s Stage 1 addendum) while staying
// 16-aligned for H.264 macroblocks.
const WIDTH: u32 = 176;
const HEIGHT: u32 = 144;

// HEVC's `picture_access_granularity` is 32x32 on this driver (H.264's is
// 16x16 — the two are not the same, see `session.rs::Capabilities`'s doc
// comment) — reuse `mediaway-encoder-windows`'s D3D12 HEVC test's 256x192,
// already known to clear every codec's observed minimum on this hardware.
const WIDTH_HEVC: u32 = 256;
const HEIGHT_HEVC: u32 = 192;

// AV1's own `picture_access_granularity` on this driver was not queried ahead
// of writing this test — 256x192 (a multiple of AV1's 64x64 superblock size)
// is reused as a resolution comfortably likely to clear whatever the driver
// reports; `VulkanVideoEncoder::open` failing here is treated as a skip, not
// a hard failure, same as every other hardware-gated test in this crate.
const WIDTH_AV1: u32 = 256;
const HEIGHT_AV1: u32 = 192;

fn nv12_frame_sized(pts: i64, width: u32, height: u32) -> VideoFrame {
    let len = (width as usize) * (height as usize) * 3 / 2;
    VideoFrame {
        pts,
        duration: 1,
        width,
        height,
        format: PixelFormat::Nv12,
        storage: VideoFrameStorage::Cpu {
            data: Bytes::from(vec![128u8; len]),
        },
    }
}

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

/// Opens a real `VulkanVideoEncoder`, pushes 3 synthetic gray NV12 frames
/// through the full pipeline (upload → `EncodeFrame` → real driver-reported
/// byte count → readback), and asserts each returned packet is a real,
/// byte-exact (not zero-padded) Annex-B bitstream containing SPS/PPS/IDR NALs
/// — the same real hardware path `session_tests.rs` exercises once, now
/// reused across multiple frames on one persistent session.
#[test]
#[allow(
    clippy::similar_names,
    reason = "has_sps/has_pps/has_idr_slice read clearer than de-aliased names"
)]
fn push_three_frames_or_skip() {
    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: None,
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open failed ({error:?}) — no encode-capable Vulkan device?"
            );
            return;
        }
    };

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

        let headers = scan_nal_headers(&packet.payload);
        let has_sps = headers.iter().any(|h| h.nal_unit_type == 7);
        let has_pps = headers.iter().any(|h| h.nal_unit_type == 8);
        let has_idr_slice = headers.iter().any(|h| h.nal_unit_type == 5);
        assert!(
            has_sps,
            "packet {i} missing SPS (type 7); found {headers:?}"
        );
        assert!(
            has_pps,
            "packet {i} missing PPS (type 8); found {headers:?}"
        );
        assert!(
            has_idr_slice,
            "packet {i} missing IDR slice (type 5); found {headers:?}"
        );
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!("vulkan H.264 VideoEncoder ok: {packets} packets, all real SPS+PPS+IDR Annex-B NALs");
}

/// HEVC sibling of [`push_three_frames_or_skip`] — same real hardware
/// pipeline, HEVC's 2-byte NAL header and third parameter set (VPS).
#[test]
#[allow(
    clippy::similar_names,
    reason = "has_vps/has_sps/has_pps/has_idr_slice read clearer than de-aliased names"
)]
fn push_three_hevc_frames_or_skip() {
    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: None,
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (HEVC) failed ({error:?}) — no encode-capable Vulkan device?"
            );
            return;
        }
    };

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

        let headers = scan_nal_headers_hevc(&packet.payload);
        let has_vps = headers.iter().any(|h| h.nal_unit_type == 32);
        let has_sps = headers.iter().any(|h| h.nal_unit_type == 33);
        let has_pps = headers.iter().any(|h| h.nal_unit_type == 34);
        let has_idr_slice = headers
            .iter()
            .any(|h| h.nal_unit_type == 19 || h.nal_unit_type == 20);
        assert!(
            has_vps,
            "packet {i} missing VPS (type 32); found {headers:?}"
        );
        assert!(
            has_sps,
            "packet {i} missing SPS (type 33); found {headers:?}"
        );
        assert!(
            has_pps,
            "packet {i} missing PPS (type 34); found {headers:?}"
        );
        assert!(
            has_idr_slice,
            "packet {i} missing IDR slice (type 19/20); found {headers:?}"
        );
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan HEVC VideoEncoder ok: {packets} packets, all real VPS+SPS+PPS+IDR Annex-B NALs"
    );
}

/// AV1 sibling of [`push_three_frames_or_skip`] — same real hardware
/// pipeline, low-overhead-format OBU stream instead of Annex-B NAL units.
///
/// **Known-broken on this crate's reference RTX 4090 (driver 32.0.15.9579),
/// hardware-verified 2026-07-29 — see `adr/0001`'s AV1 addendum.** The
/// session-parameters-fetched `OBU_SEQUENCE_HEADER` is real and independently
/// parseable (asserted below), but every `vkCmdEncodeVideoKHR` frame's own
/// output is not a valid OBU stream. Independently confirmed **not** to be a
/// bug in this crate: `ffmpeg -c:v av1_vulkan` on this exact machine produces
/// AV1 output `dav1d` itself rejects with real decode errors (up to 73% of
/// packets), so the per-frame check below is downgraded to a documented skip
/// rather than a hard failure — this is a driver-maturity limitation, not
/// this crate's own bitstream construction.
#[test]
fn push_three_av1_frames_or_skip() {
    // AV1 OBU types (AV1 spec §6.2.2): 1 = OBU_SEQUENCE_HEADER,
    // 3 = OBU_FRAME_HEADER, 6 = OBU_FRAME (frame header + tile group combined
    // — this crate's single-tile output may use either shape).
    const OBU_SEQUENCE_HEADER: u8 = 1;
    const OBU_FRAME_HEADER: u8 = 3;
    const OBU_FRAME: u8 = 6;

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Av1,
        width: WIDTH_AV1,
        height: HEIGHT_AV1,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: None,
        gop_size: 1,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (AV1) failed ({error:?}) — no encode-capable Vulkan device, or this driver lacks AV1 encode?"
            );
            return;
        }
    };

    let mut packets = 0usize;
    for i in 0..3i64 {
        let frame = nv12_frame_sized(i, WIDTH_AV1, HEIGHT_AV1);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame (AV1) failed ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (AV1)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (AV1) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        assert!(packet.is_keyframe, "packet {i} should be a key frame");

        let headers = scan_obu_headers(&packet.payload);
        let has_sequence_header = headers.iter().any(|h| h.obu_type == OBU_SEQUENCE_HEADER);
        let has_frame = headers
            .iter()
            .any(|h| h.obu_type == OBU_FRAME || h.obu_type == OBU_FRAME_HEADER);
        assert!(
            has_sequence_header,
            "packet {i} missing OBU_SEQUENCE_HEADER (type 1); found {headers:?}"
        );
        if !has_frame {
            eprintln!(
                "skip: packet {i}'s own frame data is not a valid OBU (found {headers:?}) — \
                 known driver-maturity limitation on this hardware, see `adr/0001`'s AV1 addendum"
            );
            return;
        }
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan AV1 VideoEncoder ok: {packets} packets, all real OBU sequence header + frame OBUs"
    );
}

/// ADR-0002: pushes 7 frames with `gop_size = 3` through a real
/// `VulkanVideoEncoder` and asserts the expected IDR (NAL type 5) / P-slice
/// (NAL type 1) cadence: frame 0 is IDR (`GopState`'s first call is always
/// IDR), frames 1-2 are P (`frames_since_idr < gop_size`), frame 3 cycles
/// back to IDR (`frames_since_idr >= gop_size`), and the pattern repeats —
/// `I P P I P P I`. Falls back to a documented skip (not a hard failure) if
/// this driver reports `Capabilities::supports_p_frames == false`
/// (`VulkanVideoEncoder::open` degrades to IDR-only with no error in that
/// case, per ADR-0002's capability-gating contract) — every packet would
/// then be IDR and the cadence assertions below would legitimately fail, so
/// this test cannot tell "P-frames unsupported" apart from "P-frames
/// broken" without inspecting `Capabilities` directly; it reports the
/// mismatch and skips rather than hard-failing either way.
#[test]
fn push_seven_frames_gop_or_skip() {
    const GOP_SIZE: u32 = 3;
    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: None,
        gop_size: GOP_SIZE,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (GOP) failed ({error:?}) — no encode-capable Vulkan device?"
            );
            return;
        }
    };

    let expected_idr = [true, false, false, true, false, false, true];
    let mut idr_count = 0usize;
    let mut p_count = 0usize;
    for (i, &want_idr) in expected_idr.iter().enumerate() {
        let frame = nv12_frame(i64::try_from(i).unwrap_or(0));
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame (GOP) failed at {i} ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (GOP)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (GOP) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");

        let headers = scan_nal_headers(&packet.payload);
        let has_idr_slice = headers.iter().any(|h| h.nal_unit_type == 5);
        let has_p_slice = headers.iter().any(|h| h.nal_unit_type == 1);
        if want_idr != packet.is_keyframe {
            eprintln!(
                "skip: packet {i} keyframe flag ({}) doesn't match expected GOP cadence ({want_idr}) — \
                 this driver may report Capabilities::supports_p_frames == false, degrading to \
                 IDR-only per ADR-0002's capability-gating fallback",
                packet.is_keyframe
            );
            return;
        }
        if want_idr {
            assert!(
                has_idr_slice,
                "packet {i} expected IDR (NAL type 5); found {headers:?}"
            );
            idr_count += 1;
        } else {
            assert!(
                has_p_slice,
                "packet {i} expected a P-slice (NAL type 1); found {headers:?}"
            );
            assert!(
                !has_idr_slice,
                "packet {i} expected no IDR NAL; found {headers:?}"
            );
            p_count += 1;
        }
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan H.264 GOP VideoEncoder ok: {idr_count} IDR + {p_count} P packets, \
         cadence matched gop_size={GOP_SIZE}"
    );
}

/// ADR-0002: sibling of [`push_seven_frames_gop_or_skip`] with
/// `rate_control` also set — real CBR when `Capabilities::supports_cbr`,
/// today's fixed-QP `DISABLED` fallback otherwise (both are legitimate,
/// capability-gated outcomes this test cannot distinguish without
/// inspecting `Capabilities` directly, same caveat as the GOP-only test
/// above). Does not assert exact byte counts (driver/rate-control-behavior
/// dependent, per this crate's own honesty rule) — only that real,
/// non-empty packets keep coming back and that no single packet balloons to
/// an unreasonable multiple of one uncompressed frame's size.
#[test]
fn push_frames_gop_with_rate_control_or_skip() {
    const GOP_SIZE: u32 = 4;
    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: None,
        gop_size: GOP_SIZE,
        rate_control: Some(RateControlConfig {
            target_bitrate_bps: 500_000,
            vbv_buffer_size_bytes: Some(125_000),
        }),
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (GOP+CBR) failed ({error:?}) — no encode-capable Vulkan device?"
            );
            return;
        }
    };

    let uncompressed_frame_bytes = (WIDTH as usize) * (HEIGHT as usize) * 3 / 2;
    let mut total_bytes = 0usize;
    let mut packets = 0usize;
    for i in 0..6i64 {
        let frame = nv12_frame(i);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame (GOP+CBR) failed at {i} ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (GOP+CBR)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (GOP+CBR) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        assert!(
            packet.payload.len() < uncompressed_frame_bytes * 4,
            "packet {i} suspiciously large: {} bytes (uncompressed frame is {uncompressed_frame_bytes})",
            packet.payload.len()
        );
        total_bytes += packet.payload.len();
        packets += 1;
    }

    // Live retarget mid-session (`VideoEncoder::set_bitrate`) — real when CBR was actually
    // selected (`Capabilities::supports_cbr`), `Unsupported` on the same fixed-QP fallback
    // this test's doc already treats as legitimate; either way `push_frame` must keep
    // producing real packets right after the call, since `push_frame` rebuilds
    // `VkVideoEncodeRateControlLayerInfoKHR` from `rate_control_params` fresh every call
    // (see `VulkanVideoEncoder::set_bitrate`'s doc) rather than caching anything from the
    // retargeted call itself.
    match enc.set_bitrate(250_000) {
        Ok(()) => eprintln!("vulkan H.264 CBR set_bitrate: accepted (real CBR session)"),
        Err(error) => {
            eprintln!("vulkan H.264 CBR set_bitrate: {error:?} (fixed-QP fallback, expected)");
        }
    }
    for i in 6..9i64 {
        let frame = nv12_frame(i);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame after set_bitrate failed at {i} ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (post-set_bitrate)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (post-set_bitrate) failed ({error:?})");
                return;
            }
        };
        assert!(
            !packet.payload.is_empty(),
            "post-set_bitrate packet {i} payload is empty"
        );
        total_bytes += packet.payload.len();
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan H.264 GOP+CBR VideoEncoder ok: {packets} packets, {total_bytes} total bytes \
         (target_bitrate_bps=500000, driver-dependent actual rate not independently verified)"
    );
}

/// `true` iff `headers` contains a NAL unit that is not VPS (32) / SPS (33) /
/// PPS (34) — i.e. a real slice NAL, IDR or otherwise. Every packet this
/// encoder produces carries the full VPS+SPS+PPS header bytes ahead of its
/// slice payload (`VulkanVideoEncoder::push_frame` prepends
/// `self.header_bytes` unconditionally, GOP or not), so a slice-type check
/// like this — rather than just "the header list is non-empty" — is needed
/// to confirm a P-frame packet's own coded picture data is really present,
/// not just its (always-repeated) parameter sets.
fn has_slice_nal(headers: &[NalHeader]) -> bool {
    headers.iter().any(|h| !matches!(h.nal_unit_type, 32..=34))
}

/// HEVC sibling of [`push_seven_frames_gop_or_skip`] — same real hardware
/// pipeline and `I P P I P P I` cadence, HEVC's 2-byte NAL header and IDR
/// NAL types (19/20) in place of H.264's single type-5 IDR. Unlike H.264's
/// P-slice check (a fixed NAL type 1), this test does not hardcode which
/// exact NAL unit type the driver picks for a P-frame (H.265 Table 7-1 has
/// several candidates depending on `TemporalId`/`discardable_flag`, e.g.
/// `TRAIL_R`) — it only asserts a P-frame packet carries *some* non-IDR
/// slice NAL ([`has_slice_nal`], with the IDR NAL types explicitly excluded)
/// and reports the real NAL types seen for a human to sanity-check.
#[test]
fn push_seven_hevc_frames_gop_or_skip() {
    const GOP_SIZE: u32 = 3;
    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: None,
        gop_size: GOP_SIZE,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (HEVC GOP) failed ({error:?}) — no encode-capable Vulkan device?"
            );
            return;
        }
    };

    let expected_idr = [true, false, false, true, false, false, true];
    let mut idr_count = 0usize;
    let mut p_count = 0usize;
    for (i, &want_idr) in expected_idr.iter().enumerate() {
        let frame = nv12_frame_sized(i64::try_from(i).unwrap_or(0), WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame (HEVC GOP) failed at {i} ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC GOP)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (HEVC GOP) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");

        let headers = scan_nal_headers_hevc(&packet.payload);
        let has_idr_slice = headers
            .iter()
            .any(|h| h.nal_unit_type == 19 || h.nal_unit_type == 20);
        if want_idr != packet.is_keyframe {
            eprintln!(
                "skip: packet {i} keyframe flag ({}) doesn't match expected GOP cadence ({want_idr}) — \
                 this driver may report Capabilities::supports_p_frames == false, degrading to \
                 IDR-only per ADR-0002's capability-gating fallback",
                packet.is_keyframe
            );
            return;
        }
        if want_idr {
            assert!(
                has_idr_slice,
                "packet {i} expected IDR (NAL type 19/20); found {headers:?}"
            );
            idr_count += 1;
        } else {
            assert!(
                has_slice_nal(&headers),
                "packet {i} expected a real slice NAL; found {headers:?}"
            );
            assert!(
                !has_idr_slice,
                "packet {i} expected no IDR NAL; found {headers:?}"
            );
            p_count += 1;
        }
        eprintln!("HEVC GOP packet {i}: is_keyframe={want_idr} NALs={headers:?}");
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan HEVC GOP VideoEncoder ok: {idr_count} IDR + {p_count} P packets, \
         cadence matched gop_size={GOP_SIZE}"
    );
}

/// ADR-0002 scopes CBR rate control to H.264 only this pass —
/// `VulkanVideoEncoder::open`'s `rate_control_params` stays `None`
/// unconditionally for an HEVC session (`session_command_hevc.rs`'s
/// `record_video_coding_hevc` never reads it, always `DISABLED` fixed-QP).
/// Unlike [`push_frames_gop_with_rate_control_or_skip`] (H.264's real
/// CBR-or-documented-fallback sibling), this test is not a CBR sanity check
/// — there is no CBR path here to sanity-check — it only confirms that
/// requesting `rate_control` on an HEVC config is safely and silently
/// ignored: the session still opens, the GOP cadence still holds, and
/// packets stay reasonably sized under today's fixed-QP encode.
#[test]
fn push_hevc_frames_gop_with_rate_control_requested_or_skip() {
    const GOP_SIZE: u32 = 4;
    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: None,
        gop_size: GOP_SIZE,
        rate_control: Some(RateControlConfig {
            target_bitrate_bps: 500_000,
            vbv_buffer_size_bytes: Some(125_000),
        }),
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (HEVC GOP+rate_control requested) failed ({error:?}) \
                 — no encode-capable Vulkan device?"
            );
            return;
        }
    };

    let uncompressed_frame_bytes = (WIDTH_HEVC as usize) * (HEIGHT_HEVC as usize) * 3 / 2;
    let mut total_bytes = 0usize;
    let mut packets = 0usize;
    for i in 0..6i64 {
        let frame = nv12_frame_sized(i, WIDTH_HEVC, HEIGHT_HEVC);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!(
                "skip: push_frame (HEVC GOP+rate_control requested) failed at {i} ({error:?})"
            );
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (HEVC GOP+rate_control requested)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (HEVC GOP+rate_control requested) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        assert!(
            packet.payload.len() < uncompressed_frame_bytes * 4,
            "packet {i} suspiciously large: {} bytes (uncompressed frame is {uncompressed_frame_bytes})",
            packet.payload.len()
        );
        total_bytes += packet.payload.len();
        packets += 1;
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan HEVC GOP+rate_control-requested VideoEncoder ok: {packets} packets, \
         {total_bytes} total bytes (rate_control silently ignored per ADR-0002, fixed-QP path used)"
    );
}

/// ADR-0002's AV1 follow-up: attempts the same `I P P I P P I` GOP cadence
/// check [`push_seven_frames_gop_or_skip`]/[`push_seven_hevc_frames_gop_or_skip`]
/// run, for AV1. **Written to honestly skip, not fail, on the known-broken
/// per-frame bitstream** — this crate's AV1 base (IDR-only) encode is already
/// hardware-verified to produce invalid per-frame OBU output on this crate's
/// reference RTX 4090 (a driver-maturity limitation, independently confirmed
/// via system `FFmpeg`'s own `av1_vulkan` on the same machine — see
/// `adr/0001`'s AV1 addendum and [`push_three_av1_frames_or_skip`]), so GOP
/// mode built on top of that base is expected to hit the same issue. The
/// `is_keyframe` cadence itself (`Av1GopState::decide`'s own state machine,
/// pure Rust, no driver involvement) is still hard-asserted — same "P-frames
/// unsupported vs. broken, can't tell apart without inspecting `Capabilities`
/// directly" caveat as the H.264/HEVC GOP tests applies to that fallback
/// path. If every packet's own frame data unexpectedly *is* a valid OBU, this
/// test reports that surprising result and stops — it does not treat that as
/// proof the known driver bug is fixed in general (only this narrow
/// resolution/GOP shape).
#[test]
fn push_seven_av1_frames_gop_or_skip() {
    const OBU_SEQUENCE_HEADER: u8 = 1;
    const OBU_FRAME_HEADER: u8 = 3;
    const OBU_FRAME: u8 = 6;
    const GOP_SIZE: u32 = 3;

    let cfg = VideoEncoderConfig {
        codec: CodecKind::Av1,
        width: WIDTH_AV1,
        height: HEIGHT_AV1,
        time_base: Rational::new(1, 30),
        bitrate_bps: 500_000,
        pixel_format: PixelFormat::Nv12,
        input: VideoInputPreference::CpuUploadOk,
        gpu_device: None,
        gop_size: GOP_SIZE,
        rate_control: None,
        intra_refresh_period: None,
    };

    let mut enc = match VulkanVideoEncoder::open(&cfg) {
        Ok(enc) => enc,
        Err(error) => {
            eprintln!(
                "skip: VulkanVideoEncoder::open (AV1 GOP) failed ({error:?}) — no encode-capable Vulkan device, or this driver lacks AV1 encode?"
            );
            return;
        }
    };

    let expected_key = [true, false, false, true, false, false, true];
    let mut key_count = 0usize;
    let mut inter_count = 0usize;
    for (i, &want_key) in expected_key.iter().enumerate() {
        let frame = nv12_frame_sized(i64::try_from(i).unwrap_or(0), WIDTH_AV1, HEIGHT_AV1);
        if let Err(error) = enc.push_frame(&frame) {
            eprintln!("skip: push_frame (AV1 GOP) failed at {i} ({error:?})");
            return;
        }
        let packet = match enc.poll_packet() {
            Ok(Some(packet)) => packet,
            Ok(None) => {
                eprintln!("skip: no packet after push_frame {i} (AV1 GOP)");
                return;
            }
            Err(error) => {
                eprintln!("skip: poll_packet (AV1 GOP) failed ({error:?})");
                return;
            }
        };
        assert!(!packet.payload.is_empty(), "packet {i} payload is empty");
        if want_key != packet.is_keyframe {
            eprintln!(
                "skip: packet {i} keyframe flag ({}) doesn't match expected GOP cadence ({want_key}) — \
                 this driver may report Capabilities::supports_p_frames == false, degrading to \
                 key-frame-only per ADR-0002's capability-gating fallback",
                packet.is_keyframe
            );
            return;
        }
        if want_key {
            key_count += 1;
        } else {
            inter_count += 1;
        }

        let headers = scan_obu_headers(&packet.payload);
        let has_sequence_header = headers.iter().any(|h| h.obu_type == OBU_SEQUENCE_HEADER);
        assert!(
            has_sequence_header,
            "packet {i} missing OBU_SEQUENCE_HEADER (type 1); found {headers:?}"
        );
        let has_frame = headers
            .iter()
            .any(|h| h.obu_type == OBU_FRAME || h.obu_type == OBU_FRAME_HEADER);
        if !has_frame {
            eprintln!(
                "skip: packet {i}'s own frame data is not a valid OBU (found {headers:?}) — \
                 known driver-maturity limitation on this hardware, same root cause as \
                 push_three_av1_frames_or_skip, see `adr/0001`'s AV1 addendum and \
                 `adr/vulkan/0002`'s AV1 follow-up section"
            );
            return;
        }
    }

    enc.flush().expect("flush");
    eprintln!(
        "vulkan AV1 GOP VideoEncoder ok: {key_count} key + {inter_count} inter packets, all real \
         OBU sequence header + frame OBUs, cadence matched gop_size={GOP_SIZE} — surprising result, \
         does not by itself mean the known driver bug is fixed in general"
    );
}