Skip to main content

alpha2_host_core/
codecs.rs

1//! The codecs native hosts encode.
2
3/// Video codecs the native encoders implement.
4#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
5pub enum NativeVideoCodec {
6    /// H.264/AVC, preferred for broad browser, Windows, and Apple playback.
7    H264,
8    /// H.265/HEVC.
9    Hevc,
10    /// VP9, where the host has a VP9 encoder.
11    Vp9,
12    /// AV1, where the host has an AV1 encoder.
13    Av1,
14    /// MPEG-4 Part 2, retained for compatibility with existing alpha2 hosts.
15    Mpeg4Part2,
16    /// Apple `ProRes`: 422 HQ, or 4444 with alpha.
17    ProRes,
18}
19
20impl NativeVideoCodec {
21    /// Stable identifier suitable for an application protocol or settings file.
22    #[must_use]
23    pub const fn as_str(self) -> &'static str {
24        match self {
25            Self::H264 => "h264",
26            Self::Hevc => "hevc",
27            Self::Vp9 => "vp9",
28            Self::Av1 => "av1",
29            Self::Mpeg4Part2 => "mpeg4-part2",
30            Self::ProRes => "prores",
31        }
32    }
33
34    /// The pictures an encoder must take to be offered: 10-bit 4:2:2 for
35    /// `ProRes`, 8-bit 4:2:0 otherwise.
36    #[must_use]
37    pub const fn base_layout(self) -> alpha2_media::PixelLayout {
38        match self {
39            Self::ProRes => alpha2_media::PixelLayout {
40                chroma: alpha2_media::Chroma::Yuv422,
41                bit_depth: alpha2_media::BitDepth::Ten,
42                alpha: false,
43                planes: alpha2_media::PlaneLayout::Planar,
44            },
45            _ => alpha2_media::PixelLayout::YUV420P,
46        }
47    }
48
49    /// The engine's codec this encoder family implements.
50    #[must_use]
51    pub const fn model(self) -> alpha2_media::VideoCodec {
52        match self {
53            Self::H264 => alpha2_media::VideoCodec::H264,
54            Self::Hevc => alpha2_media::VideoCodec::Hevc,
55            Self::Vp9 => alpha2_media::VideoCodec::Vp9,
56            Self::Av1 => alpha2_media::VideoCodec::Av1,
57            Self::Mpeg4Part2 => alpha2_media::VideoCodec::Mpeg4Part2,
58            Self::ProRes => alpha2_media::VideoCodec::ProRes,
59        }
60    }
61
62    /// The native codec for the engine's `codec`.
63    #[must_use]
64    pub const fn from_model(codec: alpha2_media::VideoCodec) -> Self {
65        match codec {
66            alpha2_media::VideoCodec::H264 => Self::H264,
67            alpha2_media::VideoCodec::Hevc => Self::Hevc,
68            alpha2_media::VideoCodec::Vp9 => Self::Vp9,
69            alpha2_media::VideoCodec::Av1 => Self::Av1,
70            alpha2_media::VideoCodec::Mpeg4Part2 => Self::Mpeg4Part2,
71            alpha2_media::VideoCodec::ProRes => Self::ProRes,
72        }
73    }
74}
75
76/// Audio codecs the native encoders implement.
77#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
78pub enum NativeAudioCodec {
79    /// AAC-LC compatible audio.
80    Aac,
81    /// Opus.
82    Opus,
83}
84
85impl NativeAudioCodec {
86    /// Every codec, in the order the engine lists them.
87    pub const ALL: [Self; 2] = [Self::Aac, Self::Opus];
88
89    /// Stable identifier suitable for an application protocol or settings file.
90    #[must_use]
91    pub const fn as_str(self) -> &'static str {
92        match self {
93            Self::Aac => "aac",
94            Self::Opus => "opus",
95        }
96    }
97
98    /// The engine's codec this encoder family implements.
99    #[must_use]
100    pub const fn model(self) -> alpha2_media::AudioCodec {
101        match self {
102            Self::Aac => alpha2_media::AudioCodec::Aac,
103            Self::Opus => alpha2_media::AudioCodec::Opus,
104        }
105    }
106
107    /// The native codec for the engine's `codec`.
108    #[must_use]
109    pub const fn from_model(codec: alpha2_media::AudioCodec) -> Self {
110        match codec {
111            alpha2_media::AudioCodec::Aac => Self::Aac,
112            alpha2_media::AudioCodec::Opus => Self::Opus,
113        }
114    }
115}