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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
//! Frame conversion, colour mapping, and codec options.

use crate::capabilities::{NativeEncoderClass, encoder_class};
use alpha2_ffmpeg_bridge::{DecodedFrame, DisplayRotation};
use std::ffi::{CStr, CString};

use alpha2_media::{
    ChromaLocation, ColorMatrix, ColorMetadata, ColorPrimaries, ColorRange, Container, DecodeError,
    DecodedVideoFrame, DynamicRange, EncodeError, Orientation, PixelFormat, Plane, Rect, Rotation,
    SampleAspectRatio, TransferFunction, YuvFormat,
};
use alpha2_time::{MediaTime, TICKS_PER_SECOND};
use rsmpeg::avcodec::AVCodec;
use rsmpeg::avutil::{AVDictionary, AVFrame, AVRational, av_rescale_q, ra};
use rsmpeg::ffi;
use rsmpeg::swscale::SwsContext;

#[allow(clippy::too_many_lines)]
pub(crate) fn convert_video_frame(
    frame: &DecodedFrame,
    codec: ffi::AVCodecID,
    time_base: AVRational,
    color: ColorMetadata,
    scaler: &mut FrameScaler,
) -> Result<DecodedVideoFrame, DecodeError> {
    let width = u32::try_from(frame.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(frame.height).map_err(|_| DecodeError::OutOfRange)?;
    let pts = if frame.best_effort_timestamp == ffi::AV_NOPTS_VALUE {
        frame.pts
    } else {
        frame.best_effort_timestamp
    };
    let 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),
        ),
    };
    // YCbCr layouts the renderer samples go as planes (shared when every byte is written, else
    // copied); the GPU applies subsampling, depth, crop, rotation, and aspect ratio.
    if let Some(planes) = frame.yuv_planes().map_err(adapter_error)? {
        let layout = planes.layout;
        return Ok(DecodedVideoFrame {
            pts: timestamp(pts, time_base).ok_or(DecodeError::OutOfRange)?,
            dts: None,
            duration: timestamp(frame.duration, time_base),
            visible_rect,
            sample_aspect_ratio: declared_sar(frame.sample_aspect_ratio).unwrap_or_default(),
            orientation: Orientation::UPRIGHT,
            pixel_format: PixelFormat::Yuv(YuvFormat {
                chroma_shift_x: layout.chroma_shift_x,
                chroma_shift_y: layout.chroma_shift_y,
                bit_depth: layout.bit_depth,
                interleaved_chroma: layout.interleaved_chroma,
                msb_aligned: layout.msb_aligned,
                alpha: false,
            }),
            chroma_location: decoded_chroma_location(codec, frame.chroma_location),
            color: Some(color),
            planes: planes
                .planes
                .into_iter()
                .map(|plane| Plane {
                    bytes: plane.bytes,
                    stride: plane.stride,
                    width: plane.width,
                    height: plane.height,
                })
                .collect(),
        });
    }
    // Converting allocates a whole frame; a frame nothing in alpha2 renders is refused first.
    if !alpha2_media::frame_size_supported(width, height) {
        return Err(DecodeError::Unsupported(format!(
            "a {width}x{height} frame is larger than {} pixels or {} on a side",
            alpha2_media::MAX_FRAME_PIXELS,
            alpha2_media::MAX_FRAME_DIMENSION
        )));
    }
    let (pixel_format, planes, color) = if converts_like_rgba8(color) {
        (
            PixelFormat::Rgba8,
            vec![rgba8_plane(frame, color, scaler)?],
            color,
        )
    } else {
        full_resolution_planes(frame, color, scaler)?
    };
    Ok(DecodedVideoFrame {
        pts: timestamp(pts, time_base).ok_or(DecodeError::OutOfRange)?,
        dts: None,
        duration: timestamp(frame.duration, time_base),
        visible_rect,
        sample_aspect_ratio: declared_sar(frame.sample_aspect_ratio).unwrap_or_default(),
        orientation: Orientation::UPRIGHT,
        pixel_format,
        // Neither layout is subsampled.
        chroma_location: ChromaLocation::Unspecified,
        color: Some(color),
        planes,
    })
}

/// Whether a frame of `color` the renderer cannot sample may reach it as
/// 8-bit sRGB-encoded RGBA: an SDR frame in BT.709 primaries, for which the
/// renderer's conversion is the matrix and range alone. Any other frame keeps
/// its own components so the renderer still tone-maps it, converts its
/// gamut, or refuses it, as it does a frame it samples.
fn converts_like_rgba8(color: ColorMetadata) -> bool {
    !color.is_hdr() && color.primaries == ColorPrimaries::Bt709
}

/// Converts `frame` to one plane of straight 8-bit RGBA with `color`'s
/// matrix and range.
fn rgba8_plane(
    frame: &AVFrame,
    color: ColorMetadata,
    scaler: &mut FrameScaler,
) -> Result<Plane, DecodeError> {
    let width = u32::try_from(frame.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(frame.height).map_err(|_| DecodeError::OutOfRange)?;
    let active = scaler.for_frame(frame, ffi::AV_PIX_FMT_RGBA)?;
    configure_yuv_to_rgba(active, color).map_err(adapter_error)?;
    let mut bytes = vec![0; packed_len(width, height, 4)?];
    alpha2_ffmpeg_bridge::scale_frame_packed(active, frame, ffi::AV_PIX_FMT_RGBA, &mut bytes)
        .map_err(adapter_error)?;
    Ok(Plane {
        bytes: bytes.into(),
        stride: width.saturating_mul(4),
        width,
        height,
    })
}

/// Repacks `frame` into 4:4:4 planes of its own components at its own
/// depth (8, 10, 12, or 16 bits), its alpha in a plane after them, without
/// converting its color: luma and chroma stay in `color`'s matrix and range,
/// and red, green, and blue come out in that order, tagged with the identity
/// matrix. At its own depth swscale moves samples without rescaling them,
/// which it does not do exactly between depths.
fn full_resolution_planes(
    frame: &AVFrame,
    color: ColorMetadata,
    scaler: &mut FrameScaler,
) -> Result<(PixelFormat, Vec<Plane>, ColorMetadata), DecodeError> {
    let width = u32::try_from(frame.width).map_err(|_| DecodeError::OutOfRange)?;
    let height = u32::try_from(frame.height).map_err(|_| DecodeError::OutOfRange)?;
    let profile = alpha2_ffmpeg_bridge::sample_profile(frame.format)
        .ok_or_else(|| DecodeError::Unsupported("unsupported FFmpeg pixel format".into()))?;
    let depth = match profile.bit_depth {
        0..=8 => 8,
        9..=10 => 10,
        11..=12 => 12,
        _ => 16,
    };
    let target = planar_444_format(profile.is_rgb, profile.has_alpha, depth);
    let active = scaler.for_frame(frame, target)?;
    if !profile.is_rgb {
        // The same matrix and range on both sides: swscale changes only depth, subsampling, and
        // layout.
        let coefficients = sws_coefficients(color.matrix);
        let full = color.range == ColorRange::Full;
        alpha2_ffmpeg_bridge::configure_colorspace(active, coefficients, full, coefficients, full)
            .map_err(adapter_error)?;
    }
    let plane_count = if profile.has_alpha { 4 } else { 3 };
    let sample_bytes = if depth > 8 { 2 } else { 1 };
    let plane_len = packed_len(width, height, sample_bytes)?;
    let mut bytes = vec![0; plane_len * plane_count];
    alpha2_ffmpeg_bridge::scale_frame_packed(active, frame, target, &mut bytes)
        .map_err(adapter_error)?;
    let bytes = bytes::Bytes::from(bytes);
    let stride = width.saturating_mul(u32::try_from(sample_bytes).unwrap_or(2));
    let plane = |index: usize| Plane {
        bytes: bytes.slice(index * plane_len..(index + 1) * plane_len),
        stride,
        width,
        height,
    };
    // `gbrp` stores green, blue, red; the identity matrix reads red, green, blue.
    let order: &[usize] = if profile.is_rgb {
        &[2, 0, 1]
    } else {
        &[0, 1, 2]
    };
    let mut planes = order.iter().map(|&index| plane(index)).collect::<Vec<_>>();
    if profile.has_alpha {
        planes.push(plane(3));
    }
    let format = YuvFormat::planar(0, 0, depth);
    let format = if profile.has_alpha {
        format.with_alpha()
    } else {
        format
    };
    let color = if profile.is_rgb {
        ColorMetadata {
            matrix: ColorMatrix::Rgb,
            range: ColorRange::Full,
            ..color
        }
    } else {
        color
    };
    Ok((PixelFormat::Yuv(format), planes, color))
}

/// The planar 4:4:4 format of RGB or YCbCr components, with alpha or
/// without, at `depth` bits in this machine's byte order.
const fn planar_444_format(rgb: bool, alpha: bool, depth: u8) -> ffi::AVPixelFormat {
    match (rgb, alpha, depth) {
        (true, false, 8) => ffi::AV_PIX_FMT_GBRP,
        (true, true, 8) => ffi::AV_PIX_FMT_GBRAP,
        (true, false, 10) => ffi::AV_PIX_FMT_GBRP10LE,
        (true, true, 10) => ffi::AV_PIX_FMT_GBRAP10LE,
        (true, false, 12) => ffi::AV_PIX_FMT_GBRP12LE,
        (true, true, 12) => ffi::AV_PIX_FMT_GBRAP12LE,
        (true, false, _) => ffi::AV_PIX_FMT_GBRP16LE,
        (true, true, _) => ffi::AV_PIX_FMT_GBRAP16LE,
        (false, false, 8) => ffi::AV_PIX_FMT_YUV444P,
        (false, true, 8) => ffi::AV_PIX_FMT_YUVA444P,
        (false, false, 10) => ffi::AV_PIX_FMT_YUV444P10LE,
        (false, true, 10) => ffi::AV_PIX_FMT_YUVA444P10LE,
        (false, false, 12) => ffi::AV_PIX_FMT_YUV444P12LE,
        (false, true, 12) => ffi::AV_PIX_FMT_YUVA444P12LE,
        (false, false, _) => ffi::AV_PIX_FMT_YUV444P16LE,
        (false, true, _) => ffi::AV_PIX_FMT_YUVA444P16LE,
    }
}

/// The scaler a stream's frames convert with, kept for as long as they keep
/// the size and format it was made for.
///
/// `sws_getCachedContext` is no cache here: it reuses a context only when
/// the flags and source format it holds equal the ones asked for, and
/// `FFmpeg` rewrites both while it initialises one (full chroma
/// interpolation for RGB output from unsubsampled or odd-width input, the
/// alpha twin of an `RGB0`-style source), so it would remake the context for
/// every such frame.
#[derive(Default)]
pub(crate) struct FrameScaler {
    context: Option<(ScalerShape, SwsContext)>,
    made: u64,
}

/// What a converting scaler is made for.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct ScalerShape {
    width: i32,
    height: i32,
    source: ffi::AVPixelFormat,
    target: ffi::AVPixelFormat,
}

impl FrameScaler {
    /// The scaler from `frame`'s size and format to `target` at that size,
    /// made only when the last one was made for another.
    fn for_frame(
        &mut self,
        frame: &AVFrame,
        target: ffi::AVPixelFormat,
    ) -> Result<&mut SwsContext, DecodeError> {
        let shape = ScalerShape {
            width: frame.width,
            height: frame.height,
            source: frame.format,
            target,
        };
        if self
            .context
            .as_ref()
            .is_none_or(|(made_for, _)| *made_for != shape)
        {
            self.context = None;
            let context = SwsContext::get_context(
                frame.width,
                frame.height,
                frame.format,
                frame.width,
                frame.height,
                target,
                ffi::SWS_BILINEAR,
                None,
                None,
                None,
            )
            .ok_or_else(|| DecodeError::Unsupported("unsupported FFmpeg pixel format".into()))?;
            self.made += 1;
            self.context = Some((shape, context));
        }
        self.context
            .as_mut()
            .map(|(_, context)| context)
            .ok_or_else(|| DecodeError::Unsupported("unsupported FFmpeg pixel format".into()))
    }

    /// How many scalers this has made.
    #[cfg(test)]
    pub(crate) const fn made(&self) -> u64 {
        self.made
    }

    /// Whether a scaler is held.
    #[cfg(test)]
    pub(crate) const fn is_empty(&self) -> bool {
        self.context.is_none()
    }
}

/// Bytes in a tightly packed `width`x`height` plane of `sample_bytes` per pixel.
fn packed_len(width: u32, height: u32, sample_bytes: usize) -> Result<usize, DecodeError> {
    usize::try_from(width)
        .ok()
        .and_then(|width| {
            usize::try_from(height)
                .ok()
                .and_then(|height| width.checked_mul(height))
        })
        .and_then(|pixels| pixels.checked_mul(sample_bytes))
        .ok_or(DecodeError::OutOfRange)
}

/// Where the chroma of a `codec` stream or frame sits, given the location
/// `FFmpeg` reports for it.
///
/// H.264 infers chroma sample location type 0 (left) for a stream whose SPS
/// declares none (E.2.1), but `FFmpeg`'s decoder starts its SPS zeroed and
/// leaves a stream without VUI, or with a reserved type, unspecified, which
/// the renderer would sample centred. HEVC, the other codec with that rule,
/// already reports left.
pub(crate) fn decoded_chroma_location(
    codec: ffi::AVCodecID,
    location: ffi::AVChromaLocation,
) -> ChromaLocation {
    if codec == ffi::AV_CODEC_ID_H264 && location == ffi::AVCHROMA_LOC_UNSPECIFIED {
        ChromaLocation::Left
    } else {
        chroma_location(location)
    }
}

pub(crate) fn chroma_location(location: ffi::AVChromaLocation) -> ChromaLocation {
    match location {
        ffi::AVCHROMA_LOC_LEFT => ChromaLocation::Left,
        ffi::AVCHROMA_LOC_CENTER => ChromaLocation::Center,
        ffi::AVCHROMA_LOC_TOPLEFT => ChromaLocation::TopLeft,
        _ => ChromaLocation::Unspecified,
    }
}

/// Sets `scaler` to convert full-range RGB into `color`'s YCbCr matrix and range.
pub(crate) fn configure_rgba_to_yuv(
    scaler: &mut SwsContext,
    color: ColorMetadata,
) -> Result<(), alpha2_ffmpeg_bridge::BridgeError> {
    alpha2_ffmpeg_bridge::configure_colorspace(
        scaler,
        i32::try_from(ffi::SWS_CS_ITU709).expect("color coefficient fits i32"),
        true,
        sws_coefficients(color.matrix),
        color.range == ColorRange::Full,
    )
}

/// Sets `scaler` to convert `color`'s YCbCr matrix and range into full-range RGB.
pub(crate) fn configure_yuv_to_rgba(
    scaler: &mut SwsContext,
    color: ColorMetadata,
) -> Result<(), alpha2_ffmpeg_bridge::BridgeError> {
    alpha2_ffmpeg_bridge::configure_colorspace(
        scaler,
        sws_coefficients(color.matrix),
        color.range == ColorRange::Full,
        i32::try_from(ffi::SWS_CS_ITU709).expect("color coefficient fits i32"),
        true,
    )
}

pub(crate) fn sws_coefficients(matrix: ColorMatrix) -> i32 {
    let value = match matrix {
        ColorMatrix::Bt601 => ffi::SWS_CS_ITU601,
        ColorMatrix::Bt709 | ColorMatrix::Rgb => ffi::SWS_CS_ITU709,
        ColorMatrix::Bt2020Ncl => ffi::SWS_CS_BT2020,
    };
    i32::try_from(value).expect("color coefficient fits i32")
}

/// The engine's tick clock as an `FFmpeg` time base.
pub(crate) fn tick_base() -> AVRational {
    ra(
        1,
        i32::try_from(TICKS_PER_SECOND).expect("tick rate fits i32"),
    )
}

/// `frame` as an encoder `AVFrame` built from its own planes, or `None` when
/// it is not I420 and has to be converted first.
///
/// # Errors
/// For an I420 frame that is cropped or not `width`x`height`.
pub(crate) fn direct_yuv420p_frame(
    frame: &DecodedVideoFrame,
    width: i32,
    height: i32,
) -> Result<Option<AVFrame>, EncodeError> {
    if frame.pixel_format != PixelFormat::Yuv(YuvFormat::I420) {
        return Ok(None);
    }
    if frame.planes.len() != 3
        || frame.visible_rect.x != 0
        || frame.visible_rect.y != 0
        || frame.planes[0].width != u32::try_from(width).unwrap_or(0)
        || frame.planes[0].height != u32::try_from(height).unwrap_or(0)
    {
        return Err(EncodeError::Adapter(
            "direct YUV encoder input must contain one uncropped coded frame".into(),
        ));
    }
    alpha2_ffmpeg_bridge::yuv420p_frame(
        &frame.planes[0].bytes,
        frame.planes[0].stride,
        &frame.planes[1].bytes,
        frame.planes[1].stride,
        &frame.planes[2].bytes,
        frame.planes[2].stride,
        width,
        height,
    )
    .map(Some)
    .map_err(encode_adapter_error)
}

/// Turns a decoded YUV frame of any layout into the 8-bit 4:2:0 an encoder
/// takes. A `Y'CbCr` frame keeps its codes and colour: only the sample depth
/// and the chroma resolution change. Planes of red, green, and blue (the
/// identity matrix, which a decoder gives an RGB frame outside BT.709's
/// gamut) are converted into the output's matrix and range, as an RGBA frame
/// is, their gamut left as it is. Alpha, which the output has no place for,
/// is left out. One stream's frames share one scaler.
pub(crate) struct Yuv420pConverter {
    output: ColorMetadata,
    scaler: Option<(ffi::AVPixelFormat, u32, u32, SwsContext)>,
}

impl Yuv420pConverter {
    /// A converter into 4:2:0 of `output`'s matrix and range.
    pub(crate) const fn new(output: ColorMetadata) -> Self {
        Self {
            output,
            scaler: None,
        }
    }

    /// `frame` as YUV420P, `None` for a frame that is not YUV.
    ///
    /// # Errors
    /// For a cropped frame, one whose size is not the encoder's, or a layout
    /// `FFmpeg` has no pixel format for.
    pub(crate) fn convert(
        &mut self,
        frame: &DecodedVideoFrame,
        width: i32,
        height: i32,
    ) -> Result<Option<AVFrame>, EncodeError> {
        let PixelFormat::Yuv(format) = frame.pixel_format else {
            return Ok(None);
        };
        let rgb = frame
            .color
            .is_some_and(|color| color.matrix == ColorMatrix::Rgb);
        if format == YuvFormat::I420 && !rgb {
            return direct_yuv420p_frame(frame, width, height);
        }
        let (width, height) = (
            u32::try_from(width).unwrap_or(0),
            u32::try_from(height).unwrap_or(0),
        );
        let stored = if format.interleaved_chroma { 2 } else { 3 };
        if frame.visible_rect.x != 0
            || frame.visible_rect.y != 0
            || frame.planes.len() < stored
            || frame.planes[0].width != width
            || frame.planes[0].height != height
        {
            return Err(EncodeError::Adapter(
                "YUV encoder input must contain one uncropped frame of the encoder's size".into(),
            ));
        }
        let (source_format, planes) = if rgb {
            if format.chroma_shift_x != 0
                || format.chroma_shift_y != 0
                || format.interleaved_chroma
                || format.msb_aligned
                || !matches!(format.bit_depth, 8 | 10 | 12 | 16)
            {
                return Err(EncodeError::Adapter(format!(
                    "no FFmpeg pixel format stores red, green, and blue as {format:?}"
                )));
            }
            // `gbrp` stores green, blue, red; the identity matrix's planes are red, green, blue.
            let planes = [1, 2, 0]
                .iter()
                .map(|&index: &usize| (&frame.planes[index].bytes[..], frame.planes[index].stride))
                .collect::<Vec<_>>();
            (planar_444_format(true, false, format.bit_depth), planes)
        } else {
            let layout = alpha2_ffmpeg_bridge::YuvPlaneLayout {
                chroma_shift_x: format.chroma_shift_x,
                chroma_shift_y: format.chroma_shift_y,
                bit_depth: format.bit_depth,
                interleaved_chroma: format.interleaved_chroma,
                msb_aligned: format.msb_aligned,
            };
            let source_format =
                alpha2_ffmpeg_bridge::yuv_pixel_format(layout).ok_or_else(|| {
                    EncodeError::Adapter(format!("no FFmpeg pixel format stores {format:?}"))
                })?;
            let planes = frame.planes[..stored]
                .iter()
                .map(|plane| (&plane.bytes[..], plane.stride))
                .collect::<Vec<_>>();
            (source_format, planes)
        };
        let source = alpha2_ffmpeg_bridge::planar_frame(source_format, &planes, width, height)
            .map_err(encode_adapter_error)?;
        if self
            .scaler
            .as_ref()
            .is_none_or(|(cached, w, h, _)| (*cached, *w, *h) != (source_format, width, height))
        {
            let (signed_width, signed_height) = (
                i32::try_from(width).map_err(|_| EncodeError::Adapter("width overflow".into()))?,
                i32::try_from(height)
                    .map_err(|_| EncodeError::Adapter("height overflow".into()))?,
            );
            let mut scaler = SwsContext::get_context(
                signed_width,
                signed_height,
                source_format,
                signed_width,
                signed_height,
                ffi::AV_PIX_FMT_YUV420P,
                ffi::SWS_BILINEAR,
                None,
                None,
                None,
            )
            .ok_or_else(|| EncodeError::Adapter("cannot create a YUV to YUV420P scaler".into()))?;
            if rgb {
                configure_rgba_to_yuv(&mut scaler, self.output).map_err(encode_adapter_error)?;
            }
            self.scaler = Some((source_format, width, height, scaler));
        }
        let Some((_, _, _, scaler)) = self.scaler.as_mut() else {
            unreachable!("the scaler was just made");
        };
        let mut output = AVFrame::new();
        output.set_format(ffi::AV_PIX_FMT_YUV420P);
        output.set_width(source.width);
        output.set_height(source.height);
        output.get_buffer(1).map_err(encode_adapter_error)?;
        scaler
            .scale_frame(&source, 0, source.height, &mut output)
            .map_err(encode_adapter_error)?;
        Ok(Some(output))
    }
}

/// `value` in `base` as engine ticks; `None` for `AV_NOPTS_VALUE` or an invalid base.
pub(crate) fn timestamp(value: i64, base: AVRational) -> Option<MediaTime> {
    (value != ffi::AV_NOPTS_VALUE && base.den > 0)
        .then(|| MediaTime::from_ticks(av_rescale_q(value, base, tick_base())))
}

/// A pixel aspect ratio as declared, in lowest terms, or `None` where it is
/// undeclared: `FFmpeg` writes an unknown ratio with a numerator of zero or
/// less, and starts every stream's at 0/1.
pub(crate) fn declared_sar(value: AVRational) -> Option<SampleAspectRatio> {
    SampleAspectRatio::new(
        u64::try_from(value.num).ok()?,
        u64::try_from(value.den).ok()?,
    )
}

/// The pixel aspect ratio a stream's pictures are shown with, by `FFmpeg`'s
/// rule (`av_guess_sample_aspect_ratio`): what the container declares, else
/// what `pictures` declare, else square pixels. For a stream, `pictures` are
/// its codec parameters (the SPS VUI, a sequence header); for a decoded
/// picture, its own ratio.
pub(crate) fn guess_sar(container: AVRational, pictures: AVRational) -> SampleAspectRatio {
    declared_sar(container)
        .or_else(|| declared_sar(pictures))
        .unwrap_or_default()
}

/// The rotation a stream's legacy `rotate` metadata tag declares, `Deg0` without one.
pub(crate) fn stream_rotation(metadata: Option<rsmpeg::avutil::AVDictionaryRef<'_>>) -> Rotation {
    let Some(metadata) = metadata else {
        return Rotation::Deg0;
    };
    let Some(entry) = metadata.get(c"rotate", None, 0) else {
        return Rotation::Deg0;
    };
    match entry
        .value()
        .to_string_lossy()
        .parse::<i32>()
        .unwrap_or(0)
        .rem_euclid(360)
    {
        90 => Rotation::Deg90,
        180 => Rotation::Deg180,
        270 => Rotation::Deg270,
        _ => Rotation::Deg0,
    }
}

/// The clockwise [`Rotation`] for `FFmpeg`'s counter-clockwise display angle.
///
/// `Rotation` is what to apply to show the frame, turning clockwise, as
/// `VideoFrame.rotation` is. `FFmpeg`'s display matrix angle turns the other
/// way, so a stream written with `-display_rotation 90` shows turned 270
/// degrees.
pub(crate) fn rotation_from_display(counter_clockwise: f64) -> Option<Rotation> {
    let normalized = (-counter_clockwise).rem_euclid(360.0);
    if !(0.5..359.5).contains(&normalized) {
        Some(Rotation::Deg0)
    } else if (normalized - 90.0).abs() < 0.5 {
        Some(Rotation::Deg90)
    } else if (normalized - 180.0).abs() < 0.5 {
        Some(Rotation::Deg180)
    } else if (normalized - 270.0).abs() < 0.5 {
        Some(Rotation::Deg270)
    } else {
        None
    }
}

/// The [`Orientation`] a display matrix declares; `None` for an angle that
/// is not a multiple of 90 degrees.
///
/// A mirroring matrix is, in `FFmpeg`'s reading, its rotation followed by a
/// horizontal flip. Flipping after a turn is flipping before the opposite
/// turn, so the mirror comes first and the angle changes sign: Exif
/// `Orientation` 5 (a rotation of 90 degrees clockwise, then a flip) is a
/// mirror turned 270 degrees.
pub(crate) fn orientation_from_display(display: DisplayRotation) -> Option<Orientation> {
    if display.flipped {
        rotation_from_display(-display.counter_clockwise_degrees).map(|rotation| Orientation {
            rotation,
            mirror: true,
        })
    } else {
        rotation_from_display(display.counter_clockwise_degrees).map(Orientation::rotated)
    }
}

/// The display matrix that shows a picture as `orientation` says, the
/// inverse of [`orientation_from_display`]; `None` for a picture shown as
/// stored, which needs none.
pub(crate) fn display_of(orientation: Orientation) -> Option<DisplayRotation> {
    if orientation == Orientation::default() {
        return None;
    }
    let clockwise = match orientation.rotation {
        Rotation::Deg0 => 0.0,
        Rotation::Deg90 => 90.0,
        Rotation::Deg180 => 180.0,
        Rotation::Deg270 => 270.0,
    };
    Some(DisplayRotation {
        counter_clockwise_degrees: if orientation.mirror {
            clockwise
        } else {
            -clockwise
        },
        flipped: orientation.mirror,
    })
}

/// `hvc1`: an HEVC sample entry whose parameter sets are all in its `hvcC`.
const HVC1: u32 = u32::from_le_bytes(*b"hvc1");
/// `hev1`: one whose parameter sets may also be in the stream.
const HEV1: u32 = u32::from_le_bytes(*b"hev1");

/// The `FFmpeg` muxer that writes `container`.
pub(crate) const fn muxer_name(container: Container) -> &'static CStr {
    match container {
        Container::Mp4 => c"mp4",
        Container::Mov => c"mov",
        Container::Mkv => c"matroska",
        Container::Webm => c"webm",
    }
}

/// The options `container`'s muxer writes its header with.
///
/// Matroska and `WebM` get `fflags=+bitexact`: without it, `matroskaenc` gives
/// every track, and a Matroska file its segment, a random UID, so two exports
/// of the same frames would differ. With it the track UIDs are their numbers,
/// the segment has no UID (it links to no other), and the writing
/// application is `Lavf` without a version. MP4 and MOV are deterministic
/// without it and keep their header as it is.
pub(crate) fn muxer_header_options(container: Container) -> Option<AVDictionary> {
    matches!(container, Container::Mkv | Container::Webm)
        .then(|| AVDictionary::new(c"fflags", c"+bitexact", 0))
}

/// The codec tag a stream of `codec_id` carrying `tag` is written to
/// `container` with.
///
/// MP4 and MOV name a stream's codec by its sample entry. HEVC is `hvc1`, as
/// browsers and Apple's players expect and as the browser exporter writes it;
/// an encoder's parameter sets are always in the sample description (global
/// header). A copied HEVC stream keeps the entry it came with, which says
/// where its parameter sets are. Any other copied stream's tag is cleared, so
/// the muxer picks its container's; an encoded one keeps what the encoder set.
///
/// Matroska and `WebM` name it by a `CodecID` string the muxer derives from the
/// codec, and a fourcc left over from another container can make it refuse
/// the stream, so the tag is always cleared there.
pub(crate) fn container_codec_tag(
    container: Container,
    codec_id: ffi::AVCodecID,
    tag: u32,
    copied: bool,
) -> u32 {
    if matches!(container, Container::Mkv | Container::Webm) {
        0
    } else if codec_id == ffi::AV_CODEC_ID_HEVC {
        if tag == HVC1 || tag == HEV1 {
            tag
        } else {
            HVC1
        }
    } else if copied {
        0
    } else {
        tag
    }
}

pub(crate) fn color_metadata(
    parameters: &rsmpeg::avcodec::AVCodecParameters,
) -> Option<ColorMetadata> {
    map_color(
        parameters.color_primaries,
        parameters.color_trc,
        parameters.color_space,
        parameters.color_range,
    )
}

pub(crate) fn frame_color_metadata(frame: &AVFrame) -> Option<ColorMetadata> {
    map_color(
        frame.color_primaries,
        frame.color_trc,
        frame.colorspace,
        frame.color_range,
    )
}

/// The color `FFmpeg`'s tags describe, `None` when all four are unspecified.
///
/// A tag that is set to anything unrecognized, or left unspecified beside
/// others, reads as its BT.709 (limited range) default; PQ and HLG make the
/// color HDR.
pub(crate) fn map_color(
    primaries: ffi::AVColorPrimaries,
    transfer: ffi::AVColorTransferCharacteristic,
    matrix: ffi::AVColorSpace,
    range: ffi::AVColorRange,
) -> Option<ColorMetadata> {
    if primaries == ffi::AVCOL_PRI_UNSPECIFIED
        && transfer == ffi::AVCOL_TRC_UNSPECIFIED
        && matrix == ffi::AVCOL_SPC_UNSPECIFIED
        && range == ffi::AVCOL_RANGE_UNSPECIFIED
    {
        return None;
    }
    let primaries = match primaries {
        ffi::AVCOL_PRI_BT2020 => ColorPrimaries::Bt2020,
        ffi::AVCOL_PRI_SMPTE170M | ffi::AVCOL_PRI_BT470BG => ColorPrimaries::Bt601,
        _ => ColorPrimaries::Bt709,
    };
    let transfer = match transfer {
        ffi::AVCOL_TRC_SMPTE2084 => TransferFunction::Pq,
        ffi::AVCOL_TRC_ARIB_STD_B67 => TransferFunction::Hlg,
        ffi::AVCOL_TRC_IEC61966_2_1 => TransferFunction::Srgb,
        _ => TransferFunction::Bt709,
    };
    let matrix = match matrix {
        ffi::AVCOL_SPC_BT2020_NCL => ColorMatrix::Bt2020Ncl,
        ffi::AVCOL_SPC_SMPTE170M | ffi::AVCOL_SPC_BT470BG => ColorMatrix::Bt601,
        ffi::AVCOL_SPC_RGB => ColorMatrix::Rgb,
        _ => ColorMatrix::Bt709,
    };
    Some(ColorMetadata {
        primaries,
        transfer,
        matrix,
        range: if range == ffi::AVCOL_RANGE_JPEG {
            ColorRange::Full
        } else {
            ColorRange::Limited
        },
        dynamic_range: if matches!(transfer, TransferFunction::Pq | TransferFunction::Hlg) {
            DynamicRange::Hdr
        } else {
            DynamicRange::Sdr
        },
    })
}

/// Options every video decoder opens with.
///
/// `flags=+unaligned` lets `FFmpeg` crop a software frame exactly. Without
/// it, a left crop is rounded down to keep plane pointers aligned, and one
/// below that alignment (64 columns of 8-bit 4:2:0) is dropped whole: the
/// frame then shows columns its stream crops away. The planes are shared
/// with the decoder, wherever they start. A hardware frame keeps its left
/// and top crop either way, as its visible rectangle.
pub(crate) fn video_decoder_options(threads: i64) -> AVDictionary {
    AVDictionary::new_int(c"threads", threads, 0).set(c"flags", c"+unaligned", 0)
}

/// Options every export sends `codec`, run on the GPU when `on_hardware`, for
/// frames `width` wide.
pub(crate) fn video_encoder_options(
    color: ColorMetadata,
    codec: &AVCodec,
    on_hardware: bool,
    threads: i64,
    width: u32,
) -> AVDictionary {
    let mut options = color_options(color);
    // A hybrid encoder is a software transform until it is told otherwise, and `hw_encoding` exists
    // only on the encoders that are hybrid, so setting it anywhere else would trip
    // `reject_unused_encode_options`.
    if encoder_class(codec) == NativeEncoderClass::Hybrid {
        options = options.set_int(c"hw_encoding", i64::from(on_hardware), 0);
    }
    if on_hardware {
        return options;
    }
    let name = codec.name().to_string_lossy();
    for (key, value) in software_encoder_profile(&name, width, threads) {
        options = set_encoder_option(options, key, &value);
    }
    options.set_int(c"threads", threads, 0)
}

/// Speed and structure options a software encoder runs with, fixed per
/// encoder so an export is reproducible: the levels choose only the rate.
///
/// - `libvpx-vp9` runs libvpx's `good` deadline at `cpu-used` 4 with
///   row-based multithreading, one tile column per 256 pixels of width (the
///   most libvpx allows, and what its threads split across), and its
///   alt-ref frames over a 25-frame look-ahead.
/// - `libsvtav1` runs preset 8. SVT-AV1 sizes its threads from a level of
///   parallelism (1 to 6), not from `threads`; the level is the one SVT-AV1
///   would pick for a machine with `threads` cores, so the export's codec
///   thread budget still bounds it. Above level 1 its pictures can be coded
///   in whatever order its threads finish them (two encodes of the same
///   frames at levels 3 and 5 differed); [`deterministic_encode`] runs it at
///   level 1, which codes them in decode order.
///
/// `libvpx-vp9` writes the same bytes at any thread count.
fn software_encoder_profile(name: &str, width: u32, threads: i64) -> Vec<(&'static CStr, String)> {
    match name {
        "libvpx-vp9" => vec![
            (c"deadline", "good".into()),
            (c"cpu-used", "4".into()),
            (c"row-mt", "1".into()),
            (c"tile-columns", vp9_tile_columns_log2(width).to_string()),
            (c"lag-in-frames", "25".into()),
            (c"auto-alt-ref", "1".into()),
        ],
        "libsvtav1" => vec![
            (c"preset", "8".into()),
            (
                c"svtav1-params",
                format!(
                    "lp={}",
                    if deterministic_encode() {
                        1
                    } else {
                        svt_av1_parallelism(threads)
                    }
                ),
            ),
        ],
        _ => Vec::new(),
    }
}

/// Whether `ALPHA2_DETERMINISTIC_ENCODE=1` asks every software encoder for
/// output that is the same bytes on every run, at the cost of speed. Only
/// `libsvtav1` needs it: it then runs on one thread of pictures.
pub(crate) fn deterministic_encode() -> bool {
    std::env::var_os("ALPHA2_DETERMINISTIC_ENCODE").is_some_and(|value| value == "1")
}

/// `tile-columns` for libvpx: log2 of the tile columns a `width`-pixel frame
/// splits into, at least 256 pixels each.
pub(crate) fn vp9_tile_columns_log2(width: u32) -> u32 {
    (width / 256).max(1).ilog2().min(6)
}

/// SVT-AV1's level of parallelism for a budget of `threads` workers: the
/// level its own default gives a machine with that many cores (levels 1 to 6
/// for 1, 2, 3-5, 6-11, 12-23, and 24 or more cores).
pub(crate) fn svt_av1_parallelism(threads: i64) -> u32 {
    match threads {
        ..=1 => 1,
        2 => 2,
        3..=5 => 3,
        6..=11 => 4,
        12..=23 => 5,
        _ => 6,
    }
}

/// Sets `key` to `value` in an encoder's options. A list-valued option
/// (`svtav1-params`, `:`-separated `key=value` pairs) already set gains
/// `value` instead of losing what it held.
pub(crate) fn set_encoder_option(options: AVDictionary, key: &CStr, value: &str) -> AVDictionary {
    let value = CString::new(value).unwrap_or_default();
    if key == c"svtav1-params" && options.get(key, None, 0).is_some() {
        let appended = CString::new(format!(":{}", value.to_string_lossy())).unwrap_or_default();
        options.set(key, &appended, ffi::AV_DICT_APPEND)
    } else {
        options.set(key, &value, 0)
    }
}

/// Returns the worker budget for one standalone software video codec context.
///
/// A standalone context is capped at 16 workers. Without an override, two
/// cores are reserved for non-codec work. The export host uses
/// [`codec_thread_allocation`] to divide a budget of up to 64 workers across
/// all active contexts.
#[must_use]
pub fn codec_thread_budget() -> i64 {
    i64::try_from(configured_codec_thread_budget().min(16)).unwrap_or(1)
}

/// The software-codec worker budget `ALPHA2_FFMPEG_THREADS` sets
/// ([`codec_thread_override`]).
///
/// # Errors
/// Returns the setting when it is not a positive worker count, which the
/// budget ignores for its default: a caller that reports the budget's origin
/// says so rather than calling it explicit.
pub fn configured_codec_thread_override() -> Result<Option<usize>, String> {
    codec_thread_override(std::env::var_os("ALPHA2_FFMPEG_THREADS").as_deref())
}

/// The worker budget `setting` names: `None` unset, a positive count up to
/// 64 (clamped), or the setting itself as the error when it is anything else
/// (`0`, a word), which leaves the default in force.
///
/// # Errors
/// Returns the setting, lossily as text, when it is not a positive count.
pub fn codec_thread_override(setting: Option<&std::ffi::OsStr>) -> Result<Option<usize>, String> {
    let Some(setting) = setting else {
        return Ok(None);
    };
    setting
        .to_str()
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .map(|value| Some(value.min(64)))
        .ok_or_else(|| setting.to_string_lossy().into_owned())
}

fn configured_codec_thread_budget() -> usize {
    configured_codec_thread_override()
        .ok()
        .flatten()
        .unwrap_or_else(|| {
            std::thread::available_parallelism()
                .map_or(1, std::num::NonZeroUsize::get)
                .saturating_sub(2)
                .max(1)
        })
        .clamp(1, 64)
}

/// What one export needs from the shared software-codec worker budget.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CodecThreadRequest {
    /// Decoder contexts the pool may run at once.
    pub decode_concurrency: usize,
    /// Whether decode sessions produce device-resident frames.
    pub hardware_decode: bool,
    /// Luma samples in the largest video source, or zero when unknown.
    pub source_frame_pixels: u64,
    /// Whether the video encoder draws on this budget.
    pub software_video_encoder: bool,
}

/// Luma samples one software decoder worker is sized to carry.
const DECODE_PIXELS_PER_THREAD: u64 = 2_000_000;
/// Smallest worker count a software decode session is given.
const MIN_SOFTWARE_DECODER_THREADS: usize = 2;
/// Largest worker count a software decode session is given. Frame threading
/// stops paying above this on a single stream, and every worker above it is
/// contending the encoder for cores instead.
const MAX_SOFTWARE_DECODER_THREADS: usize = 6;
/// Workers the software video encoder keeps whatever decode asks for.
const ENCODER_THREAD_FLOOR: usize = 4;

/// One export-wide software-codec allocation. Every decoder receives
/// `decoder_threads`; the software video encoder receives `encoder_threads`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CodecThreadAllocation {
    /// Total software codec workers available to this export.
    pub total_threads: usize,
    /// Number of decoder contexts allowed to execute concurrently.
    pub decode_concurrency: usize,
    /// `FFmpeg` worker count assigned to each decoder context.
    pub decoder_threads: i64,
    /// `FFmpeg` worker count assigned to the software video encoder, or zero for hardware.
    pub encoder_threads: i64,
    /// Workers this allocation actually hands out, decoders plus encoder.
    pub allocated_threads: usize,
}

/// Divides the export CPU budget across concurrently active codec contexts.
///
/// The configured value is an export-wide cap and a context always needs one
/// worker, so requested decoder concurrency is reduced before threads are
/// assigned. Hardware video encoding does not consume this software budget.
///
/// The split is weighted toward the output, but only when there is an output
/// to weight it toward. With a software video encoder a software decode
/// session is sized to its own frame area rather than to an even share, and
/// the encoder takes the remainder above a floor, because it is the stage that
/// paces the run: an even split gave two 27 %-utilized decoders 14 of 22
/// workers and the 98 %-utilized encoder 8. With a hardware video encoder
/// there is nobody to hand the difference to, and taking it from decode is
/// pure loss: measured on the 4K fixture with nvenc, cutting each software
/// decoder from 11 workers to 5 moved `decodeWaitMs` from 250 ms to 1,340 ms,
/// so decode stops being ahead of the renderer. That configuration keeps the
/// even split.
///
/// A hardware session decodes nothing on the CPU, so one worker should be
/// enough, but it keeps the even share: that is the split it has been
/// measured with, and no smaller share has been measured for speed.
#[must_use]
pub fn codec_thread_allocation(request: CodecThreadRequest) -> CodecThreadAllocation {
    let configured = configured_codec_thread_budget();
    let encoder_slots = usize::from(request.software_video_encoder);
    let decode_concurrency = request
        .decode_concurrency
        .max(1)
        .min(configured.saturating_sub(encoder_slots).max(1));
    let total_threads = configured.max(decode_concurrency.saturating_add(encoder_slots));
    // The encoder's floor is reserved before decode is sized, so a small budget starves the stage
    // with slack rather than the one on the path.
    let encoder_floor = if request.software_video_encoder {
        ENCODER_THREAD_FLOOR.min(total_threads.saturating_sub(decode_concurrency).max(1))
    } else {
        0
    };
    let decode_budget = total_threads
        .saturating_sub(encoder_floor)
        .max(decode_concurrency);
    let even_share = {
        let contexts = decode_concurrency.saturating_add(encoder_slots).max(1);
        (total_threads / contexts).max(1)
    };
    let decoder_threads = if request.hardware_decode || !request.software_video_encoder {
        // Hardware decode keeps the even share it was measured with (see above), and with a
        // hardware encoder nothing can use what decode would give up.
        even_share
    } else {
        software_decoder_threads(request.source_frame_pixels)
            .clamp(1, (decode_budget / decode_concurrency).max(1))
    };
    let decode_assigned = decoder_threads.saturating_mul(decode_concurrency);
    // The floor is already reserved out of `decode_budget`; taking the remainder rather than the
    // floor keeps the total inside the budget when decode could not yield (the hardware case).
    let encoder_threads = if request.software_video_encoder {
        total_threads.saturating_sub(decode_assigned).max(1)
    } else {
        0
    };
    CodecThreadAllocation {
        total_threads,
        decode_concurrency,
        decoder_threads: i64::try_from(decoder_threads).unwrap_or(1).clamp(1, 64),
        encoder_threads: i64::try_from(encoder_threads).unwrap_or(0).clamp(0, 64),
        allocated_threads: decode_assigned.saturating_add(encoder_threads),
    }
}

/// Workers one software decode session needs for a frame of this area.
///
/// Zero pixels means the source could not be probed, which takes the minimum
/// rather than the maximum: the decoders are not the stage with the deadline.
fn software_decoder_threads(source_frame_pixels: u64) -> usize {
    usize::try_from(source_frame_pixels.div_ceil(DECODE_PIXELS_PER_THREAD))
        .unwrap_or(MAX_SOFTWARE_DECODER_THREADS)
        .clamp(MIN_SOFTWARE_DECODER_THREADS, MAX_SOFTWARE_DECODER_THREADS)
}

/// The options an `FFmpeg` open left in its dictionary, which it did not consume.
pub(crate) fn unused_option_names(options: Option<AVDictionary>) -> Option<String> {
    let options = options?;
    let names = options
        .iter()
        .map(|entry| entry.key().to_string_lossy().into_owned())
        .collect::<Vec<_>>();
    (!names.is_empty()).then(|| names.join(", "))
}

pub(crate) fn reject_unused_decode_options(
    options: Option<AVDictionary>,
) -> Result<(), DecodeError> {
    if let Some(names) = unused_option_names(options) {
        Err(DecodeError::Adapter(format!(
            "FFmpeg decoder did not consume options: {names}"
        )))
    } else {
        Ok(())
    }
}

pub(crate) fn reject_unused_encode_options(
    options: Option<AVDictionary>,
) -> Result<(), EncodeError> {
    if let Some(names) = unused_option_names(options) {
        Err(EncodeError::Adapter(format!(
            "FFmpeg encoder did not consume options: {names}"
        )))
    } else {
        Ok(())
    }
}

/// The tags an encoder writes for `color`, as `FFmpeg` numbers them.
///
/// `FFmpeg`'s numbers are ITU-T H.273's code points, which an H.264 or HEVC
/// VUI and an AV1 sequence header carry as they are, so this is the one
/// table every export's colour signalling reads: [`color_options`], a copy's
/// source check, and the export bitstream filter.
pub(crate) fn color_tags(
    color: ColorMetadata,
) -> (
    ffi::AVColorPrimaries,
    ffi::AVColorTransferCharacteristic,
    ffi::AVColorSpace,
    ffi::AVColorRange,
) {
    (
        match color.primaries {
            ColorPrimaries::Bt601 => ffi::AVCOL_PRI_SMPTE170M,
            ColorPrimaries::Bt709 => ffi::AVCOL_PRI_BT709,
            ColorPrimaries::Bt2020 => ffi::AVCOL_PRI_BT2020,
        },
        match color.transfer {
            TransferFunction::Bt709 => ffi::AVCOL_TRC_BT709,
            TransferFunction::Srgb => ffi::AVCOL_TRC_IEC61966_2_1,
            TransferFunction::Pq => ffi::AVCOL_TRC_SMPTE2084,
            TransferFunction::Hlg => ffi::AVCOL_TRC_ARIB_STD_B67,
        },
        match color.matrix {
            ColorMatrix::Bt601 => ffi::AVCOL_SPC_SMPTE170M,
            ColorMatrix::Bt709 => ffi::AVCOL_SPC_BT709,
            ColorMatrix::Bt2020Ncl => ffi::AVCOL_SPC_BT2020_NCL,
            ColorMatrix::Rgb => ffi::AVCOL_SPC_RGB,
        },
        match color.range {
            ColorRange::Limited => ffi::AVCOL_RANGE_MPEG,
            ColorRange::Full => ffi::AVCOL_RANGE_JPEG,
        },
    )
}

/// The colour options an encoder opens with for `color`: [`color_tags`]'
/// values, so the encoder's fields, the container's `colr` box and the VUI
/// the export bitstream filter writes ([`crate::bitstream`]) all come from
/// one table.
pub(crate) fn color_options(color: ColorMetadata) -> AVDictionary {
    let (primaries, transfer, matrix, range) = color_tags(color);
    AVDictionary::new_int(c"color_primaries", i64::from(primaries), 0)
        .set_int(c"color_trc", i64::from(transfer), 0)
        .set_int(c"colorspace", i64::from(matrix), 0)
        .set_int(c"color_range", i64::from(range), 0)
}

/// What a failed native GPU call's status said about the device, when
/// `error` is one: the evidence the adapter errors below keep typed.
fn gpu_fault(error: &dyn std::any::Any) -> Option<alpha2_media::GpuFault> {
    error
        .downcast_ref::<alpha2_ffmpeg_bridge::BridgeError>()
        .and_then(alpha2_ffmpeg_bridge::BridgeError::gpu_fault)
}

/// `error` as a decoder adapter failure; a native GPU call's fault stays
/// typed ([`DecodeError::Gpu`]) instead of becoming only words.
pub(crate) fn adapter_error(error: impl std::fmt::Display + 'static) -> DecodeError {
    match gpu_fault(&error) {
        Some(fault) => DecodeError::Gpu {
            fault,
            message: error.to_string(),
        },
        None => DecodeError::Adapter(error.to_string()),
    }
}

/// `error` as an encoder adapter failure; a native GPU call's fault stays
/// typed ([`EncodeError::Gpu`]) instead of becoming only words.
pub(crate) fn encode_adapter_error(error: impl std::fmt::Display + 'static) -> EncodeError {
    match gpu_fault(&error) {
        Some(fault) => EncodeError::Gpu {
            fault,
            message: error.to_string(),
        },
        None => EncodeError::Adapter(error.to_string()),
    }
}

pub(crate) fn encode_adapter_error_at(
    context: &str,
    error: impl std::fmt::Display + 'static,
) -> EncodeError {
    match gpu_fault(&error) {
        Some(fault) => EncodeError::Gpu {
            fault,
            message: format!("{context}: {error}"),
        },
        None => EncodeError::Adapter(format!("{context}: {error}")),
    }
}