alpha2-native 0.4.3

Native headless and real-time host boundary for alpha2.
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
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
//! Native export frame contract shared by software and hardware decoders.

use std::any::Any;
use std::fmt::Debug;
use std::sync::Arc;

use alpha2_media::{
    CancelToken, ChromaLocation, ColorMetadata, DecodeError, DecodeEvent, DecodeRequest,
    DecodedVideoFrame, Decoder, Orientation, PixelFormat, Rect, SampleAspectRatio, StreamInfo,
};
use alpha2_time::MediaTime;

use crate::NativeDecoder;
#[cfg(all(
    any(target_os = "linux", target_os = "macos", target_os = "windows"),
    feature = "hwdecode"
))]
use crate::convert::timestamp;
use crate::decode::DecodeStageTimings;
#[cfg(all(target_os = "windows", feature = "hwdecode"))]
use alpha2_native_gpu::SharedD3D12Context;
#[cfg(all(target_os = "macos", feature = "hwdecode"))]
use alpha2_native_gpu::SharedMetalContext;
#[cfg(all(target_os = "linux", feature = "hwdecode"))]
use alpha2_native_gpu::SharedVulkanContext;
#[cfg(all(
    any(target_os = "linux", target_os = "macos", target_os = "windows"),
    feature = "hwdecode"
))]
use rsmpeg::{avutil::AVFrame, ffi};

/// Why a video source is decoded in software instead of on the GPU.
///
/// Recorded per source so a run can report what prevented the hardware path
/// rather than leaving an unexplained software fallback.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum HardwareDecodeFallback {
    /// No shared GPU decode context was created for this run.
    ContextUnavailable,
    /// The codec exposes no hardware configuration for this backend.
    NoHardwareConfig,
    /// The stream declares a color space the hardware path does not present.
    UnsupportedColor,
    /// The stream's chroma subsampling or bit depth has no resident path on
    /// this device, such as 4:2:2, 4:4:4, or 10-bit without P010 support.
    UnsupportedProfile,
    /// The decoder produced a surface format that cannot be imported.
    UnsupportedSurfaceFormat,
    /// The container or stream could not be opened at all.
    SourceUnavailable,
    /// The hardware decoder rejected this stream while opening.
    DecoderOpenFailed,
    /// Reverse playback keeps this cursor on the software decoder.
    ReverseWindow,
    /// The stream's SPS crops its left or top edge, which this backend's
    /// surfaces do not show: `VideoToolbox` hands back the coded picture's
    /// top-left corner with the crop cleared. A D3D12VA picture that reaches
    /// past its surface is refused the same way.
    UnsupportedCrop,
    /// The renderer could not import a decoded surface, so its pixels were
    /// downloaded and uploaded from the CPU instead.
    ImportFailed,
}

impl HardwareDecodeFallback {
    /// Stable key used when reporting fallback counts.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ContextUnavailable => "contextUnavailable",
            Self::NoHardwareConfig => "noHardwareConfig",
            Self::UnsupportedColor => "unsupportedColor",
            Self::UnsupportedProfile => "unsupportedProfile",
            Self::UnsupportedSurfaceFormat => "unsupportedSurfaceFormat",
            Self::SourceUnavailable => "sourceUnavailable",
            Self::DecoderOpenFailed => "decoderOpenFailed",
            Self::ReverseWindow => "reverseWindow",
            Self::UnsupportedCrop => "unsupportedCrop",
            Self::ImportFailed => "importFailed",
        }
    }
}

/// A hardware decode session that could not be opened, with its typed reason.
#[derive(Debug)]
pub struct HardwareOpenError {
    /// Why the hardware path is unavailable for this source.
    pub reason: HardwareDecodeFallback,
    /// The underlying decode failure.
    pub source: DecodeError,
}

impl HardwareOpenError {
    /// Pairs a decode failure with the reason a caller should record.
    #[must_use]
    pub const fn new(reason: HardwareDecodeFallback, source: DecodeError) -> Self {
        Self { reason, source }
    }
}

impl std::fmt::Display for HardwareOpenError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}: {:?}", self.reason.as_str(), self.source)
    }
}

impl std::error::Error for HardwareOpenError {}

/// One backend's hardware-decoder negotiation parameters.
#[cfg(all(
    any(target_os = "linux", target_os = "macos", target_os = "windows"),
    feature = "hwdecode"
))]
pub(crate) struct HardwareDecoderSpec {
    /// `FFmpeg` hardware device type to match in the codec's configurations.
    pub device_type: ffi::AVHWDeviceType,
    /// Opaque hardware pixel format the decoder must produce.
    pub pixel_format: ffi::AVPixelFormat,
    /// Backend name used in diagnostics.
    pub backend: &'static str,
    /// Whether the configuration must accept a shared device context.
    pub require_device_context: bool,
    /// Whether the renderer can present 10-bit planes from this backend.
    pub ten_bit: bool,
    /// Whether this backend's frames keep an SPS's left and top crop. `FFmpeg`'s
    /// `VideoToolbox` hwaccel clears every frame's crop, trusting the session
    /// to apply it, but the session only shortens the picture to the visible
    /// size from the right and bottom (H.264) or rescales it (HEVC), so a
    /// left or top crop would show the wrong pixels with nothing to say so.
    /// `FFmpeg`'s D3D12VA hwaccel keeps the crop; 8.1.3 makes its surfaces
    /// and the decoder's output the visible size (`avctx->width`), which would
    /// lose the visible columns and rows past a leading crop, but the session
    /// gives it surfaces of the coded size (`alpha2_d3d12_get_format`).
    pub keeps_leading_crop: bool,
    /// Callback that selects `pixel_format` during negotiation.
    pub get_format: unsafe extern "C" fn(
        *mut ffi::AVCodecContext,
        *const ffi::AVPixelFormat,
    ) -> ffi::AVPixelFormat,
}

/// Plane organization of a native export frame.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum NativePlaneLayout {
    /// Owned CPU YCbCr planes.
    CpuYuv(alpha2_media::YuvFormat),
    /// Owned CPU RGBA pixels, 8 bits each, straight alpha
    /// ([`PixelFormat::Rgba8`]).
    CpuRgba8,
    /// Owned CPU premultiplied linear-light RGBA, a half float each
    /// ([`PixelFormat::LinearRgbaF16`]).
    CpuLinearRgbaF16,
    /// Three imported R8 GPU planes.
    GpuI420,
    /// Imported R8 luma and RG8 chroma planes.
    GpuNv12,
    /// Imported R16 luma and RG16 chroma planes holding 10 significant bits.
    GpuP010,
}

impl NativePlaneLayout {
    /// Significant bits per component for this layout.
    #[must_use]
    pub const fn bit_depth(self) -> u8 {
        match self {
            Self::CpuRgba8 | Self::GpuI420 | Self::GpuNv12 => 8,
            Self::CpuLinearRgbaF16 => 16,
            Self::CpuYuv(format) => format.bit_depth,
            Self::GpuP010 => 10,
        }
    }

    /// Bytes one coded pixel occupies across every plane of this layout.
    #[cfg_attr(
        not(all(
            any(target_os = "linux", target_os = "macos", target_os = "windows"),
            feature = "hwdecode"
        )),
        allow(dead_code)
    )]
    const fn bytes_per_pixel_numerator(self) -> u64 {
        match self {
            Self::CpuRgba8 => 8,
            Self::CpuLinearRgbaF16 => 16,
            Self::GpuI420 | Self::GpuNv12 => 3,
            Self::GpuP010 => 6,
            // Luma, then two chroma planes sharing 4 >> (shift x + shift y) of its area, in half
            // bytes per sample.
            Self::CpuYuv(format) => {
                format.bytes_per_sample() as u64
                    * (2 + (4 >> (format.chroma_shift_x + format.chroma_shift_y)))
            }
        }
    }
}

/// Maps a hardware surface's software pixel format to its plane layout.
///
/// Returning `None` keeps an unsupported surface from being presented as if it
/// were 4:2:0, so the caller reports a typed fallback instead.
#[cfg(all(
    any(target_os = "linux", target_os = "macos", target_os = "windows"),
    feature = "hwdecode"
))]
pub(crate) const fn surface_plane_layout(format: ffi::AVPixelFormat) -> Option<NativePlaneLayout> {
    match format {
        ffi::AV_PIX_FMT_NV12 => Some(NativePlaneLayout::GpuNv12),
        ffi::AV_PIX_FMT_P010LE => Some(NativePlaneLayout::GpuP010),
        _ => None,
    }
}

/// The visible rectangle of a hardware frame of `width` x `height` with
/// `crop` (left, top, right, bottom) still on it, or `None` when that
/// rectangle reaches past the `surface_width` x `surface_height` texture the
/// frame was decoded into.
///
/// `FFmpeg` applies only the right and bottom crop to a hardware frame's size
/// and leaves the left and top crop on the frame, so the rectangle ends where
/// the frame does. A surface smaller than that never held the picture's last
/// columns or rows: `FFmpeg` 8.1.3 alone makes D3D12VA surfaces the visible
/// size, which is too small for a left or top crop. The session gives the
/// decoder surfaces of the coded size (`alpha2_d3d12_get_format`), so this
/// guards against a decoder that makes them smaller all the same.
#[cfg_attr(
    not(all(target_os = "windows", feature = "hwdecode")),
    allow(dead_code)
)]
pub(crate) fn visible_rect_within_surface(
    width: u32,
    height: u32,
    [left, top, right, bottom]: [usize; 4],
    surface_width: u32,
    surface_height: u32,
) -> Option<Rect> {
    let x = u32::try_from(left).ok()?;
    let y = u32::try_from(top).ok()?;
    let visible = Rect {
        x,
        y,
        width: width
            .checked_sub(x)?
            .checked_sub(u32::try_from(right).ok()?)?,
        height: height
            .checked_sub(y)?
            .checked_sub(u32::try_from(bottom).ok()?)?,
    };
    (u64::from(x) + u64::from(visible.width) <= u64::from(surface_width)
        && u64::from(y) + u64::from(visible.height) <= u64::from(surface_height))
    .then_some(visible)
}

/// Platform-neutral metadata carried identically by CPU and hardware frames.
///
/// It says nothing of the memory behind the picture: a hardware surface may
/// be larger than its frame (a 1080p H.264 surface is 1088 rows), by an
/// amount each platform decides, and an importer reports the extent it
/// samples itself.
#[derive(Clone, Debug, PartialEq)]
pub struct NativeVideoFrameMetadata {
    /// Presentation timestamp.
    pub pts: MediaTime,
    /// Decode timestamp when available.
    pub dts: Option<MediaTime>,
    /// Nominal duration when available.
    pub duration: Option<MediaTime>,
    /// Visible crop.
    pub visible_rect: Rect,
    /// How the picture is shown upright.
    pub orientation: Orientation,
    /// Pixel aspect ratio.
    pub sample_aspect_ratio: SampleAspectRatio,
    /// Color declaration.
    pub color: Option<ColorMetadata>,
    /// Chroma sample position.
    pub chroma_location: ChromaLocation,
    /// Significant component bits.
    pub bit_depth: u8,
    /// Memory plane organization.
    pub plane_layout: NativePlaneLayout,
    /// GPU device identity, zero for CPU frames.
    pub device_identity: u64,
    /// Decoder or surface-pool generation.
    pub decoder_generation: u64,
    /// Physical picture identity within the generation.
    pub picture_identity: u64,
}

/// Opaque decoder surface retained until GPU completion.
pub trait HardwareFrameLease: Debug + Send + Sync + 'static {
    /// Approximate allocation size charged to the surface budget.
    fn retained_bytes(&self) -> u64;

    /// Allows the platform interop crate to recover its private lease type.
    fn as_any(&self) -> &dyn Any;

    /// Converts a retained lease for platform-specific import without exposing
    /// the backend type in the common frame enum.
    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
}

#[cfg(all(target_os = "linux", feature = "hwdecode"))]
impl HardwareFrameLease for alpha2_ffmpeg_bridge::VulkanFrameLease {
    fn retained_bytes(&self) -> u64 {
        self.info().map_or(0, |info| {
            let pixels = u64::try_from(info.width)
                .unwrap_or(0)
                .saturating_mul(u64::try_from(info.height).unwrap_or(0));
            // An unrecognized layout is charged as 8-bit 4:2:0; the frame is rejected before
            // import, so this only bounds the queue briefly.
            let numerator = surface_plane_layout(info.software_format)
                .unwrap_or(NativePlaneLayout::GpuNv12)
                .bytes_per_pixel_numerator();
            pixels.saturating_mul(numerator).div_ceil(2)
        })
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }
}

#[cfg(all(target_os = "macos", feature = "hwdecode"))]
impl HardwareFrameLease for alpha2_ffmpeg_bridge::VideoToolboxFrameLease {
    fn retained_bytes(&self) -> u64 {
        self.info().map_or(0, |info| {
            let pixels = u64::try_from(info.width)
                .unwrap_or(0)
                .saturating_mul(u64::try_from(info.height).unwrap_or(0));
            // An unrecognized layout is charged as 8-bit 4:2:0; the frame is rejected before
            // import, so this only bounds the queue briefly.
            let numerator = surface_plane_layout(info.software_format)
                .unwrap_or(NativePlaneLayout::GpuNv12)
                .bytes_per_pixel_numerator();
            pixels.saturating_mul(numerator).div_ceil(2)
        })
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }
}

#[cfg(all(target_os = "windows", feature = "hwdecode"))]
impl HardwareFrameLease for alpha2_ffmpeg_bridge::D3D12FrameLease {
    fn retained_bytes(&self) -> u64 {
        self.info().map_or(0, |info| {
            let pixels = u64::try_from(info.width)
                .unwrap_or(0)
                .saturating_mul(u64::try_from(info.height).unwrap_or(0));
            // An unrecognized layout is charged as 8-bit 4:2:0; the frame is rejected before
            // import, so this only bounds the queue briefly.
            let numerator = surface_plane_layout(info.software_format)
                .unwrap_or(NativePlaneLayout::GpuNv12)
                .bytes_per_pixel_numerator();
            pixels.saturating_mul(numerator).div_ceil(2)
        })
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }
}

/// One decoded export frame with either owned CPU pixels or an opaque surface.
#[derive(Debug)]
pub enum NativeVideoFrame {
    /// Software-decoded frame.
    Cpu(DecodedVideoFrame),
    /// Hardware-decoded frame whose platform details remain behind its lease.
    Hardware {
        /// Presentation and cache identity.
        metadata: NativeVideoFrameMetadata,
        /// Ref-counted decoder-surface ownership.
        surface: Arc<dyn HardwareFrameLease>,
    },
}

impl NativeVideoFrame {
    /// Presentation timestamp.
    #[must_use]
    pub fn pts(&self) -> MediaTime {
        match self {
            Self::Cpu(frame) => frame.pts,
            Self::Hardware { metadata, .. } => metadata.pts,
        }
    }

    /// Nominal frame duration when known.
    #[must_use]
    pub fn duration(&self) -> Option<MediaTime> {
        match self {
            Self::Cpu(frame) => frame.duration,
            Self::Hardware { metadata, .. } => metadata.duration,
        }
    }

    /// Returns metadata without exposing the backing representation.
    #[must_use]
    pub fn metadata(&self) -> NativeVideoFrameMetadata {
        match self {
            Self::Cpu(frame) => metadata_from_cpu(frame),
            Self::Hardware { metadata, .. } => metadata.clone(),
        }
    }

    /// Whether this frame is backed by a decoder surface rather than CPU pixels.
    #[must_use]
    pub const fn is_hardware(&self) -> bool {
        matches!(self, Self::Hardware { .. })
    }

    /// Returns the software frame when this is the CPU variant.
    #[must_use]
    pub fn as_cpu(&self) -> Option<&DecodedVideoFrame> {
        match self {
            Self::Cpu(frame) => Some(frame),
            Self::Hardware { .. } => None,
        }
    }

    /// A CPU copy of this frame: the frame itself when it already is one, or
    /// the decoder surface's pixels downloaded through `FFmpeg`.
    ///
    /// This is the fallback for a surface the renderer could not import, so
    /// the frame still reaches the export instead of failing it. Timing and
    /// presentation come from this frame's metadata; the pixels go through
    /// the same conversion a software decoder's frames do.
    ///
    /// # Errors
    /// Returns a native decode error when the surface cannot be downloaded or
    /// converted.
    pub fn to_cpu(&self) -> Result<DecodedVideoFrame, DecodeError> {
        let (metadata, surface) = match self {
            Self::Cpu(frame) => return Ok(frame.clone()),
            Self::Hardware { metadata, surface } => (metadata, surface),
        };
        let mut software = download_surface(surface.as_ref())?;
        // In tick units so the timestamp cannot fall out of range; the metadata replaces it below.
        software.set_pts(metadata.pts.ticks());
        let mut scaler = crate::convert::FrameScaler::default();
        // The surface's own siting replaces the conversion's below.
        let mut frame = crate::convert::convert_video_frame(
            &software,
            rsmpeg::ffi::AV_CODEC_ID_NONE,
            crate::convert::tick_base(),
            metadata.color.unwrap_or_default(),
            &mut scaler,
        )?;
        frame.pts = metadata.pts;
        frame.duration = metadata.duration;
        frame.orientation = metadata.orientation;
        frame.sample_aspect_ratio = metadata.sample_aspect_ratio;
        frame.chroma_location = metadata.chroma_location;
        // The conversion keeps the given color unless it repacked RGB under the identity matrix; a
        // surface without one stays without one.
        if metadata.color.is_none() {
            frame.color = None;
        }
        Ok(frame)
    }

    /// Approximate retained bytes for separate CPU and surface budgets.
    #[must_use]
    pub fn retained_bytes(&self) -> u64 {
        match self {
            Self::Cpu(frame) => frame.planes.iter().fold(0_u64, |total, plane| {
                total.saturating_add(u64::try_from(plane.bytes.len()).unwrap_or(u64::MAX))
            }),
            Self::Hardware { surface, .. } => surface.retained_bytes(),
        }
    }
}

/// Downloads a decoder surface of whichever platform lease this build knows.
#[cfg_attr(
    not(all(
        any(target_os = "linux", target_os = "macos", target_os = "windows"),
        feature = "hwdecode"
    )),
    allow(clippy::unnecessary_wraps)
)]
fn download_surface(
    surface: &dyn HardwareFrameLease,
) -> Result<alpha2_ffmpeg_bridge::DecodedFrame, DecodeError> {
    let any = surface.as_any();
    let downloaded: Option<
        Result<alpha2_ffmpeg_bridge::DecodedFrame, alpha2_ffmpeg_bridge::BridgeError>,
    > = None;
    #[cfg(all(target_os = "linux", feature = "hwdecode"))]
    let downloaded = downloaded.or_else(|| {
        any.downcast_ref::<alpha2_ffmpeg_bridge::VulkanFrameLease>()
            .map(alpha2_ffmpeg_bridge::VulkanFrameLease::download)
    });
    #[cfg(all(target_os = "macos", feature = "hwdecode"))]
    let downloaded = downloaded.or_else(|| {
        any.downcast_ref::<alpha2_ffmpeg_bridge::VideoToolboxFrameLease>()
            .map(alpha2_ffmpeg_bridge::VideoToolboxFrameLease::download)
    });
    #[cfg(all(target_os = "windows", feature = "hwdecode"))]
    let downloaded = downloaded.or_else(|| {
        any.downcast_ref::<alpha2_ffmpeg_bridge::D3D12FrameLease>()
            .map(alpha2_ffmpeg_bridge::D3D12FrameLease::download)
    });
    let _ = any;
    downloaded
        .ok_or_else(|| {
            DecodeError::Unsupported("this hardware surface cannot be downloaded".into())
        })?
        .map_err(|error| DecodeError::Adapter(error.to_string()))
}

/// Export decoder producing [`NativeVideoFrame`]s from one `FFmpeg` session.
///
/// Software and hardware sessions share the same demux, seek, and timing
/// logic ([`NativeDecoder`]); a hardware backend changes only where the
/// frame's pixels live.
pub struct NativeExportDecoder {
    backend: ExportDecoderBackend,
    /// Hardware frames this session has produced.
    hardware_frames: u64,
    /// Why the hardware path gave up, once it has.
    hardware_failure: Option<HardwareDecodeFallback>,
}

enum ExportDecoderBackend {
    Software(NativeDecoder),
    #[cfg(all(target_os = "linux", feature = "hwdecode"))]
    Vulkan {
        decoder: NativeDecoder,
        generation: u64,
    },
    #[cfg(all(target_os = "macos", feature = "hwdecode"))]
    VideoToolbox {
        decoder: NativeDecoder,
        generation: u64,
        device_identity: u64,
    },
    #[cfg(all(target_os = "windows", feature = "hwdecode"))]
    D3D12 {
        decoder: NativeDecoder,
        generation: u64,
        device_identity: u64,
    },
}

impl NativeExportDecoder {
    /// Takes the decode-stage spans accumulated since the last call.
    ///
    /// A hardware session reports no conversion time because it produces a
    /// surface rather than pixels; its cost is in `receive_frame`.
    pub fn take_stage_timings(&mut self) -> DecodeStageTimings {
        match &mut self.backend {
            ExportDecoderBackend::Software(decoder) => decoder.take_stage_timings(),
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder.take_stage_timings(),
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder.take_stage_timings(),
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder.take_stage_timings(),
        }
    }

    /// Whether this session currently produces device-resident frames.
    #[must_use]
    pub const fn is_hardware(&self) -> bool {
        match &self.backend {
            ExportDecoderBackend::Software(_) => false,
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { .. } => true,
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { .. } => true,
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { .. } => true,
        }
    }

    /// Opens the selected video stream using the software implementation.
    ///
    /// # Errors
    /// Returns a native decode error if the input or selected stream cannot be opened.
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self, DecodeError> {
        NativeDecoder::open(path).map(|decoder| Self {
            backend: ExportDecoderBackend::Software(decoder),
            hardware_frames: 0,
            hardware_failure: None,
        })
    }

    /// Opens with an explicit codec worker allocation.
    ///
    /// # Errors
    /// Returns a native decode error for invalid worker counts or stream setup failures.
    pub fn open_with_threads(
        path: impl AsRef<std::path::Path>,
        threads: i64,
    ) -> Result<Self, DecodeError> {
        NativeDecoder::open_with_threads(path, threads).map(|decoder| Self {
            backend: ExportDecoderBackend::Software(decoder),
            hardware_frames: 0,
            hardware_failure: None,
        })
    }

    /// Opens a Vulkan hardware decoder on the renderer's logical device.
    ///
    /// # Errors
    /// Returns a native decode error if the stream lacks a compatible Vulkan
    /// hardware configuration or the decoder cannot use the shared device.
    #[cfg(all(target_os = "linux", feature = "hwdecode"))]
    pub fn open_vulkan(
        path: impl AsRef<std::path::Path>,
        threads: i64,
        context: &SharedVulkanContext,
        generation: u64,
    ) -> Result<Self, HardwareOpenError> {
        let spec = HardwareDecoderSpec {
            device_type: ffi::AV_HWDEVICE_TYPE_VULKAN,
            pixel_format: ffi::AV_PIX_FMT_VULKAN,
            backend: "Vulkan",
            require_device_context: true,
            ten_bit: context.supports_ten_bit(),
            keeps_leading_crop: true,
            get_format: alpha2_ffmpeg_bridge::vulkan_get_format_callback(),
        };
        NativeDecoder::open_hardware(path, threads, context.ffmpeg_device(), &spec).map(|decoder| {
            Self {
                backend: ExportDecoderBackend::Vulkan {
                    decoder,
                    generation,
                },
                hardware_frames: 0,
                hardware_failure: None,
            }
        })
    }

    /// Opens a `VideoToolbox` hardware decoder producing `CoreVideo` surfaces.
    ///
    /// # Errors
    /// Returns a native decode error if the selected stream cannot negotiate
    /// `AV_PIX_FMT_VIDEOTOOLBOX` or create a hardware decoder session.
    #[cfg(all(target_os = "macos", feature = "hwdecode"))]
    pub fn open_videotoolbox(
        path: impl AsRef<std::path::Path>,
        threads: i64,
        context: &SharedMetalContext,
        generation: u64,
    ) -> Result<Self, HardwareOpenError> {
        // VideoToolbox allocates its own decode session rather than adopting the renderer's device;
        // Metal interop happens at import time.
        let device = rsmpeg::avutil::AVHWDeviceContext::create(
            ffi::AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
            None,
            None,
            0,
        )
        .map_err(crate::convert::adapter_error)
        .map_err(|error| {
            HardwareOpenError::new(HardwareDecodeFallback::DecoderOpenFailed, error)
        })?;
        let spec = HardwareDecoderSpec {
            device_type: ffi::AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
            pixel_format: ffi::AV_PIX_FMT_VIDEOTOOLBOX,
            backend: "VideoToolbox",
            require_device_context: false,
            ten_bit: context.supports_ten_bit(),
            keeps_leading_crop: false,
            get_format: alpha2_ffmpeg_bridge::videotoolbox_get_format_callback(),
        };
        NativeDecoder::open_hardware(path, threads, device, &spec).map(|decoder| Self {
            backend: ExportDecoderBackend::VideoToolbox {
                decoder,
                generation,
                device_identity: context.identity().device,
            },
            hardware_frames: 0,
            hardware_failure: None,
        })
    }

    /// Opens a D3D12VA hardware decoder on the renderer's D3D12 device.
    ///
    /// # Errors
    /// Returns a native decode error if the stream lacks a compatible D3D12VA
    /// configuration or the shared device cannot create a decode session.
    #[cfg(all(target_os = "windows", feature = "hwdecode"))]
    pub fn open_d3d12(
        path: impl AsRef<std::path::Path>,
        threads: i64,
        context: &SharedD3D12Context,
        generation: u64,
    ) -> Result<Self, HardwareOpenError> {
        let spec = HardwareDecoderSpec {
            device_type: ffi::AV_HWDEVICE_TYPE_D3D12VA,
            pixel_format: ffi::AV_PIX_FMT_D3D12,
            backend: "D3D12VA",
            require_device_context: true,
            ten_bit: context.supports_ten_bit(),
            keeps_leading_crop: true,
            get_format: alpha2_ffmpeg_bridge::d3d12_get_format_callback(),
        };
        NativeDecoder::open_hardware(path, threads, context.ffmpeg_device(), &spec).map(|decoder| {
            Self {
                backend: ExportDecoderBackend::D3D12 {
                    decoder,
                    generation,
                    device_identity: context.identity().device,
                },
                hardware_frames: 0,
                hardware_failure: None,
            }
        })
    }

    /// Stream descriptions from the common decode session.
    #[must_use]
    pub fn streams(&self) -> &[StreamInfo] {
        match &self.backend {
            ExportDecoderBackend::Software(decoder) => decoder.streams(),
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder.streams(),
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder.streams(),
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder.streams(),
        }
    }

    /// Effective end of the selected video source.
    #[must_use]
    pub fn video_duration(&self) -> Option<MediaTime> {
        match &self.backend {
            ExportDecoderBackend::Software(decoder) => decoder.video_duration(),
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder.video_duration(),
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder.video_duration(),
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder.video_duration(),
        }
    }

    /// Resets codec and demux state.
    ///
    /// # Errors
    /// Returns a native decode error if the codec state cannot be reset.
    pub fn flush(&mut self) -> Result<(), DecodeError> {
        match &mut self.backend {
            ExportDecoderBackend::Software(decoder) => decoder.flush(),
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder.flush(),
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder.flush(),
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder.flush(),
        }
    }

    /// Seeks and returns the selected native frame.
    ///
    /// # Errors
    /// Returns a native decode error for cancellation, seek, decode, or timestamp failures.
    pub fn seek_video(
        &mut self,
        request: DecodeRequest,
        cancel: &CancelToken,
    ) -> Result<NativeVideoFrame, DecodeError> {
        let result = self.seek_video_on_backend(request, cancel);
        self.observe(result, |_| true)
    }

    /// Decodes the next native frame, or `None` at end of stream.
    ///
    /// # Errors
    /// Returns a native decode error for cancellation or codec failures.
    pub fn decode_video_next(
        &mut self,
        cancel: &CancelToken,
    ) -> Result<Option<NativeVideoFrame>, DecodeError> {
        let result = self.decode_video_next_on_backend(cancel);
        self.observe(result, Option::is_some)
    }

    /// When the frame [`Self::decode_video_next`] returns next is
    /// presented, or `None` at the end of the stream.
    ///
    /// The frame is decoded now and kept for that call, so a caller can find
    /// the frame presented at a time (the last one at or before its
    /// [`Self::presentation_deadline`]) without consuming the one after.
    ///
    /// # Errors
    /// Returns a native decode error for cancellation or codec failures.
    pub fn peek_video_pts(
        &mut self,
        cancel: &CancelToken,
    ) -> Result<Option<MediaTime>, DecodeError> {
        let result = self.native_decoder().peek_raw_pts(cancel);
        self.observe(result, |_| false)
    }

    /// Configures corruption handling on the selected native backend.
    pub fn set_decode_policy(&mut self, policy: crate::DecodePolicy) {
        self.native_decoder().set_decode_policy(policy);
    }

    /// Takes the color a project declares for this source, which every frame
    /// then carries ([`NativeDecoder::declare_color`]).
    ///
    /// # Errors
    /// Returns [`HardwareDecodeFallback::UnsupportedColor`] for a hardware
    /// session declared HDR: the hardware path presents SDR only, as it
    /// refuses a stream tagged HDR when it opens.
    pub fn with_declared_color(
        mut self,
        color: Option<ColorMetadata>,
    ) -> Result<Self, HardwareOpenError> {
        if self.is_hardware() && color.is_some_and(ColorMetadata::is_hdr) {
            return Err(HardwareOpenError::new(
                HardwareDecodeFallback::UnsupportedColor,
                DecodeError::Unsupported("hardware export currently supports SDR only".into()),
            ));
        }
        self.native_decoder().declare_color(color);
        Ok(self)
    }

    /// Reports damage before any recovery or terminal error.
    pub fn set_decode_reporter(
        &mut self,
        reporter: impl Fn(crate::DecodeDiagnostic) + Send + Sync + 'static,
    ) {
        self.native_decoder().set_decode_reporter(reporter);
    }

    /// Cumulative corruption diagnostics for this session.
    pub fn decode_diagnostics(&mut self) -> &crate::DecodeDiagnostics {
        self.native_decoder().decode_diagnostics()
    }

    /// The latest timestamp a frame of this stream may carry and still be
    /// the one shown at `time` ([`NativeDecoder::presentation_deadline`]).
    #[must_use]
    pub fn presentation_deadline(&self, time: MediaTime) -> MediaTime {
        match &self.backend {
            ExportDecoderBackend::Software(decoder) => decoder,
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder,
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder,
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder,
        }
        .presentation_deadline(time)
    }

    /// The `FFmpeg` session behind whichever backend decodes.
    fn native_decoder(&mut self) -> &mut NativeDecoder {
        match &mut self.backend {
            ExportDecoderBackend::Software(decoder) => decoder,
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan { decoder, .. } => decoder,
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox { decoder, .. } => decoder,
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 { decoder, .. } => decoder,
        }
    }

    /// Why this session's hardware path gave up, once a call has returned
    /// [`DecodeError::Unsupported`] for it.
    #[must_use]
    pub const fn hardware_failure(&self) -> Option<HardwareDecodeFallback> {
        self.hardware_failure
    }

    /// Classifies a hardware session's failures so a caller can fall back.
    ///
    /// `FFmpeg` initializes a hardware decoder lazily, in `get_format` for the
    /// first packet, so a device that cannot decode this profile or codec
    /// (VP9 on a device without VP9 decode, 10-bit H.264 on NVDEC) fails
    /// there with a generic codec error rather than in `avcodec_open2`. Before
    /// the session has produced a frame, such an error is reported as
    /// [`DecodeError::Unsupported`] with [`HardwareDecodeFallback::DecoderOpenFailed`],
    /// which a caller can answer by reopening the source in software. After
    /// the first frame, a codec error is a real failure and stays one.
    fn observe<T>(
        &mut self,
        result: Result<T, DecodeError>,
        produced: impl Fn(&T) -> bool,
    ) -> Result<T, DecodeError> {
        if !self.is_hardware() {
            return result;
        }
        match result {
            Ok(value) => {
                if produced(&value) {
                    self.hardware_frames = self.hardware_frames.saturating_add(1);
                }
                Ok(value)
            }
            Err(DecodeError::Adapter(message)) if self.hardware_frames == 0 => {
                self.hardware_failure = Some(HardwareDecodeFallback::DecoderOpenFailed);
                Err(DecodeError::Unsupported(format!(
                    "the hardware decoder failed before its first frame: {message}"
                )))
            }
            Err(DecodeError::Unsupported(message)) => {
                self.hardware_failure
                    .get_or_insert(HardwareDecodeFallback::UnsupportedSurfaceFormat);
                Err(DecodeError::Unsupported(message))
            }
            Err(error) => Err(error),
        }
    }

    fn seek_video_on_backend(
        &mut self,
        request: DecodeRequest,
        cancel: &CancelToken,
    ) -> Result<NativeVideoFrame, DecodeError> {
        match &mut self.backend {
            ExportDecoderBackend::Software(decoder) => match decoder.seek(request, cancel)?.event {
                DecodeEvent::Video(frame) => Ok(NativeVideoFrame::Cpu(frame)),
                _ => Err(DecodeError::Adapter(
                    "video decoder returned a non-video event".into(),
                )),
            },
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan {
                decoder,
                generation,
            } => {
                let frame = decoder.seek_raw(request.time, cancel)?;
                hardware_frame(decoder, frame.into_frame(), *generation)
            }
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox {
                decoder,
                generation,
                device_identity,
            } => {
                let frame = decoder.seek_raw(request.time, cancel)?;
                videotoolbox_frame(decoder, frame.into_frame(), *generation, *device_identity)
            }
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 {
                decoder,
                generation,
                device_identity,
            } => {
                let frame = decoder.seek_raw(request.time, cancel)?;
                d3d12_frame(
                    decoder,
                    frame.into_frame(),
                    *generation,
                    *device_identity,
                    &mut self.hardware_failure,
                )
            }
        }
    }

    fn decode_video_next_on_backend(
        &mut self,
        cancel: &CancelToken,
    ) -> Result<Option<NativeVideoFrame>, DecodeError> {
        match &mut self.backend {
            ExportDecoderBackend::Software(decoder) => match decoder.decode_next(cancel)? {
                DecodeEvent::Video(frame) => Ok(Some(NativeVideoFrame::Cpu(frame))),
                DecodeEvent::EndOfStream => Ok(None),
                _ => Err(DecodeError::Adapter(
                    "video decoder returned a non-video event".into(),
                )),
            },
            #[cfg(all(target_os = "linux", feature = "hwdecode"))]
            ExportDecoderBackend::Vulkan {
                decoder,
                generation,
            } => decoder
                .decode_raw_next(cancel)?
                .map(|frame| hardware_frame(decoder, frame.into_frame(), *generation))
                .transpose(),
            #[cfg(all(target_os = "macos", feature = "hwdecode"))]
            ExportDecoderBackend::VideoToolbox {
                decoder,
                generation,
                device_identity,
            } => decoder
                .decode_raw_next(cancel)?
                .map(|frame| {
                    videotoolbox_frame(decoder, frame.into_frame(), *generation, *device_identity)
                })
                .transpose(),
            #[cfg(all(target_os = "windows", feature = "hwdecode"))]
            ExportDecoderBackend::D3D12 {
                decoder,
                generation,
                device_identity,
            } => decoder
                .decode_raw_next(cancel)?
                .map(|frame| {
                    d3d12_frame(
                        decoder,
                        frame.into_frame(),
                        *generation,
                        *device_identity,
                        &mut self.hardware_failure,
                    )
                })
                .transpose(),
        }
    }
}

/// A decoded D3D12VA frame as a [`NativeVideoFrame`]. A picture that reaches
/// past its surface ([`visible_rect_within_surface`]) is refused as
/// [`DecodeError::Unsupported`] with `failure` set to
/// [`HardwareDecodeFallback::UnsupportedCrop`], so the source goes on in
/// software: neither an import nor a download of that surface can show the
/// columns or rows it never held.
#[cfg(all(target_os = "windows", feature = "hwdecode"))]
fn d3d12_frame(
    decoder: &NativeDecoder,
    frame: AVFrame,
    generation: u64,
    device_identity: u64,
    failure: &mut Option<HardwareDecodeFallback>,
) -> Result<NativeVideoFrame, DecodeError> {
    let info = alpha2_ffmpeg_bridge::d3d12_frame_info(&frame)
        .map_err(|error| DecodeError::Adapter(error.to_string()))?;
    let plane_layout = surface_plane_layout(info.software_format).ok_or_else(|| {
        DecodeError::Unsupported(format!(
            "D3D12VA output {} is not an importable 4:2:0 surface",
            info.software_format
        ))
    })?;
    let width = u32::try_from(frame.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(frame.height).map_err(|_| DecodeError::OutOfRange)?;
    let crop = [
        frame.crop_left,
        frame.crop_top,
        frame.crop_right,
        frame.crop_bottom,
    ];
    let Some(visible_rect) =
        visible_rect_within_surface(width, height, crop, info.surface_width, info.surface_height)
    else {
        *failure = Some(HardwareDecodeFallback::UnsupportedCrop);
        return Err(DecodeError::Unsupported(format!(
            "D3D12VA decoded a {width}x{height} picture cropped {} columns from the left and {} rows from the top into a {}x{} surface, which does not hold it",
            frame.crop_left, frame.crop_top, info.surface_width, info.surface_height
        )));
    };
    let raw_pts = if frame.best_effort_timestamp == ffi::AV_NOPTS_VALUE {
        frame.pts
    } else {
        frame.best_effort_timestamp
    };
    let pts = timestamp(raw_pts, decoder.time_base).ok_or(DecodeError::OutOfRange)?;
    let metadata = NativeVideoFrameMetadata {
        pts,
        dts: None,
        duration: timestamp(frame.duration, decoder.time_base),
        visible_rect,
        orientation: decoder.orientation,
        sample_aspect_ratio: decoder.picture_sample_aspect_ratio(&frame),
        color: decoder.picture_color(&frame),
        chroma_location: decoder.picture_chroma_location(&frame),
        bit_depth: plane_layout.bit_depth(),
        plane_layout,
        device_identity,
        decoder_generation: generation,
        picture_identity: info.resource
            ^ info.fence_value
            ^ u64::from_ne_bytes(pts.ticks().to_ne_bytes()),
    };
    Ok(NativeVideoFrame::Hardware {
        metadata,
        surface: Arc::new(alpha2_ffmpeg_bridge::D3D12FrameLease::new(frame)),
    })
}

#[cfg(all(target_os = "macos", feature = "hwdecode"))]
fn videotoolbox_frame(
    decoder: &NativeDecoder,
    frame: AVFrame,
    generation: u64,
    device_identity: u64,
) -> Result<NativeVideoFrame, DecodeError> {
    let info = alpha2_ffmpeg_bridge::videotoolbox_frame_info(&frame)
        .map_err(|error| DecodeError::Adapter(error.to_string()))?;
    let plane_layout = surface_plane_layout(info.software_format).ok_or_else(|| {
        DecodeError::Unsupported(format!(
            "VideoToolbox output {} is not an importable 4:2:0 surface",
            info.software_format
        ))
    })?;
    let width = u32::try_from(info.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(info.height).map_err(|_| DecodeError::OutOfRange)?;
    let raw_pts = if frame.best_effort_timestamp == ffi::AV_NOPTS_VALUE {
        frame.pts
    } else {
        frame.best_effort_timestamp
    };
    let pts = timestamp(raw_pts, decoder.time_base).ok_or(DecodeError::OutOfRange)?;
    let metadata = NativeVideoFrameMetadata {
        pts,
        dts: None,
        duration: timestamp(frame.duration, decoder.time_base),
        visible_rect: Rect {
            x: u32::try_from(frame.crop_left).unwrap_or(0),
            y: u32::try_from(frame.crop_top).unwrap_or(0),
            width: width.saturating_sub(
                u32::try_from(frame.crop_left.saturating_add(frame.crop_right)).unwrap_or(0),
            ),
            height: height.saturating_sub(
                u32::try_from(frame.crop_top.saturating_add(frame.crop_bottom)).unwrap_or(0),
            ),
        },
        orientation: decoder.orientation,
        sample_aspect_ratio: decoder.picture_sample_aspect_ratio(&frame),
        color: decoder.picture_color(&frame),
        chroma_location: decoder.picture_chroma_location(&frame),
        bit_depth: plane_layout.bit_depth(),
        plane_layout,
        device_identity,
        decoder_generation: generation,
        picture_identity: info.pixel_buffer ^ u64::from_ne_bytes(pts.ticks().to_ne_bytes()),
    };
    Ok(NativeVideoFrame::Hardware {
        metadata,
        surface: Arc::new(alpha2_ffmpeg_bridge::VideoToolboxFrameLease::new(frame)),
    })
}

#[cfg(all(target_os = "linux", feature = "hwdecode"))]
fn hardware_frame(
    decoder: &NativeDecoder,
    frame: AVFrame,
    generation: u64,
) -> Result<NativeVideoFrame, DecodeError> {
    let info = alpha2_ffmpeg_bridge::vulkan_frame_info(&frame)
        .map_err(|error| DecodeError::Adapter(error.to_string()))?;
    let plane_layout = surface_plane_layout(info.software_format).ok_or_else(|| {
        DecodeError::Unsupported(format!(
            "Vulkan output {} is not an importable 4:2:0 surface",
            info.software_format
        ))
    })?;
    // For hardware formats `av_frame_apply_cropping` only subtracts the right and bottom crop; the
    // surface keeps the padded coded extent, so the visible rectangle sits inside it.
    let width = u32::try_from(frame.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(frame.height).map_err(|_| DecodeError::OutOfRange)?;
    let coded_width = u32::try_from(info.coded_width).map_err(|_| DecodeError::OutOfRange)?;
    let coded_height = u32::try_from(info.coded_height).map_err(|_| DecodeError::OutOfRange)?;
    if coded_width < width || coded_height < height {
        return Err(DecodeError::Adapter(
            "Vulkan frame is larger than its allocated surface".into(),
        ));
    }
    let raw_pts = if frame.best_effort_timestamp == ffi::AV_NOPTS_VALUE {
        frame.pts
    } else {
        frame.best_effort_timestamp
    };
    let pts = timestamp(raw_pts, decoder.time_base).ok_or(DecodeError::OutOfRange)?;
    let metadata = NativeVideoFrameMetadata {
        pts,
        dts: None,
        duration: timestamp(frame.duration, decoder.time_base),
        visible_rect: Rect {
            x: u32::try_from(frame.crop_left).unwrap_or(0),
            y: u32::try_from(frame.crop_top).unwrap_or(0),
            width: width.saturating_sub(
                u32::try_from(frame.crop_left.saturating_add(frame.crop_right)).unwrap_or(0),
            ),
            height: height.saturating_sub(
                u32::try_from(frame.crop_top.saturating_add(frame.crop_bottom)).unwrap_or(0),
            ),
        },
        orientation: decoder.orientation,
        sample_aspect_ratio: decoder.picture_sample_aspect_ratio(&frame),
        color: decoder.picture_color(&frame),
        chroma_location: decoder.picture_chroma_location(&frame),
        bit_depth: plane_layout.bit_depth(),
        plane_layout,
        device_identity: info.device,
        decoder_generation: generation,
        picture_identity: info.images[0] ^ u64::from_ne_bytes(pts.ticks().to_ne_bytes()),
    };
    Ok(NativeVideoFrame::Hardware {
        metadata,
        surface: Arc::new(alpha2_ffmpeg_bridge::VulkanFrameLease::new(frame)),
    })
}

fn metadata_from_cpu(frame: &DecodedVideoFrame) -> NativeVideoFrameMetadata {
    let plane_layout = match frame.pixel_format {
        PixelFormat::Yuv(format) => NativePlaneLayout::CpuYuv(format),
        PixelFormat::LinearRgbaF16 => NativePlaneLayout::CpuLinearRgbaF16,
        _ => NativePlaneLayout::CpuRgba8,
    };
    NativeVideoFrameMetadata {
        pts: frame.pts,
        dts: frame.dts,
        duration: frame.duration,
        visible_rect: frame.visible_rect,
        orientation: frame.orientation,
        sample_aspect_ratio: frame.sample_aspect_ratio,
        color: frame.color,
        chroma_location: frame.chroma_location,
        bit_depth: plane_layout.bit_depth(),
        plane_layout,
        device_identity: 0,
        decoder_generation: 0,
        picture_identity: u64::from_ne_bytes(frame.pts.ticks().to_ne_bytes()),
    }
}