Skip to main content

alpha2_host_core/
rate_control.rs

1//! Rate-control vocabulary shared by every encoder backend.
2
3/// A way of controlling an encoder's rate, independent of family spelling.
4#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
5pub enum RateControlMechanism {
6    /// Constant bitrate.
7    ConstantBitrate,
8    /// Variable (average) bitrate.
9    VariableBitrate,
10    /// Constant quantizer: `FFmpeg`'s `cqp`/`constqp`, the family of `crf`.
11    ConstantQuantizer,
12}
13
14impl RateControlMechanism {
15    /// Every mechanism, in the order the probe reports them.
16    pub const ALL: [Self; 3] = [
17        Self::ConstantBitrate,
18        Self::VariableBitrate,
19        Self::ConstantQuantizer,
20    ];
21
22    /// Stable identifier for diagnostics.
23    #[must_use]
24    pub const fn as_str(self) -> &'static str {
25        match self {
26            Self::ConstantBitrate => "cbr",
27            Self::VariableBitrate => "vbr",
28            Self::ConstantQuantizer => "cqp",
29        }
30    }
31}
32
33/// Which form of a resolved rate control an encoder actually ran.
34#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
35pub enum AppliedRateControl {
36    /// The constant quantizer.
37    Quantizer,
38    /// The quantizer mapped onto the encoder's own quality scale (`VideoToolbox`'s 0-1 `Quality`).
39    Quality,
40    /// The bitrate: none was asked for, or the encoder refused the quantizer.
41    Bitrate,
42}
43
44impl AppliedRateControl {
45    /// Stable identifier for the export result.
46    #[must_use]
47    pub const fn as_str(self) -> &'static str {
48        match self {
49            Self::Quantizer => "quantizer",
50            Self::Quality => "quality",
51            Self::Bitrate => "bitrate",
52        }
53    }
54}
55
56/// `VideoToolbox` quality (`kVTCompressionPropertyKey_Quality`, 0-1, higher better) per H.264
57/// quantizer at the levels' quantizers (`alpha2_media::level_quantizer`).
58///
59/// Each point holds `h264_videotoolbox` on Apple silicon within 0.5 dB PSNR-Y of `OpenH264` at the
60/// same level on a complex 1080p30 project (40.6 to 52.0 dB), at 0.66x-0.85x its bitrate. Grainy
61/// content needs about 0.05-0.10 more for the same match.
62const VIDEOTOOLBOX_QUALITY: [(u32, f64); 5] =
63    [(23, 0.65), (27, 0.57), (31, 0.50), (35, 0.40), (41, 0.25)];
64
65/// The `VideoToolbox` quality for an H.264 `quantizer`: a measured table interpolated,
66/// extrapolated along its end segments, rounded to hundredths and clamped to 0.01-1.
67#[must_use]
68pub fn videotoolbox_quality(quantizer: u32) -> f64 {
69    let table = &VIDEOTOOLBOX_QUALITY;
70    let segment = table
71        .windows(2)
72        .find(|pair| quantizer <= pair[1].0)
73        .unwrap_or(&table[table.len() - 2..]);
74    let ((q0, v0), (q1, v1)) = (segment[0], segment[1]);
75    let position = (f64::from(quantizer) - f64::from(q0)) / (f64::from(q1) - f64::from(q0));
76    let quality = v0 + (v1 - v0) * position;
77    (quality * 100.0).round().clamp(1.0, 100.0) / 100.0
78}