ingraph/primitives/mediaframe/codec/video.rs
1//! The VIDEO track's codec seat.
2
3use mediaframe::codec::VideoCodec as Foreign;
4
5/// **What a video track's bytes are encoded with** — an FFmpeg video codec name in the lowercase
6/// canon, or the open escape for one this build's roster does not carry.
7///
8/// The citizen wrapping [`mediaframe::codec::VideoCodec`], which is where the roster, the escape
9/// and the ASCII fold all live. What this type adds is the right to give them a wire face, a filter
10/// and a storage shape — the rights the orphan rule reserves to the crate owning the type, which is
11/// why the newtype exists at all.
12///
13/// # Every string is a value, and that is the vocabulary rather than this type
14///
15/// [`new`](Self::new) cannot fail. `h264` names a codec, `Some Codec 9000` names one this build has
16/// never heard of, and both are values — the second in the escape arm, losslessly. What separates
17/// them is [`is_registered`](Self::is_registered), which is the question the absent refusal would
18/// have been asking.
19///
20/// Refusing the unknown one would be the worse bargain by some distance, and it is the same bargain
21/// [`Language`](crate::primitives::lang::Language) states for an unregistered subtag: the FFmpeg
22/// table gains codecs, a generated roster is a snapshot, and a container in the wild carries
23/// whatever its muxer wrote. Refusing would make a file unindexable because of a table
24/// `mediaframe` last regenerated at n9.0.
25///
26/// # Equality is the CANONICAL slug, and the door is what makes that true
27///
28/// `VideoCodec::new("H264") == VideoCodec::new("h264")`, because the door folds and both hold the
29/// named `h264`. The fold happens ONCE, at construction, so a stored column is canonical and
30/// storage equality IS value equality: `WHERE "codec" = 'h264'` finds the row an `H264` tag
31/// produced.
32///
33/// It holds against the upstream value too, which is the case that needs saying:
34/// [`From<mediaframe::codec::VideoCodec>`](From) re-runs the slug table, so a
35/// `mediaframe::codec::VideoCodec::other("h264")` — an escape arm holding a name the roster
36/// already spells, which upstream admits and compares unequal to the named variant — becomes the
37/// named variant on the way in. No second spelling of one codec exists inside this type.
38///
39/// # Ordering is ALPHABETICAL over that slug
40///
41/// `av1 < h264 < prores`. It orders the SPELLINGS and says nothing about the codecs — see the
42/// module note, where the bargain is argued and the agreement with the `TEXT` column's own order is
43/// what earns it a cursor seat.
44#[derive(Clone, PartialEq, Eq, Hash)]
45pub struct VideoCodec(Foreign);
46
47impl VideoCodec {
48 /// Read a video codec name, WIDE — any case, known or not.
49 ///
50 /// The ONE door in. Every other route to a value of this type — [`FromStr`](core::str::FromStr),
51 /// the GraphQL scalar, the column decode, the cursor read and the conversion from the upstream
52 /// value — goes through here, so there is one fold and one table lookup in the house.
53 ///
54 /// It is total: a name the roster does not carry lands in the escape arm, ASCII-folded to the
55 /// lowercase canon.
56 #[inline]
57 #[must_use]
58 pub fn new(text: &str) -> Self {
59 // `from_str` is `Infallible` upstream, and the `unwrap_or_else` is how that is spent without a
60 // panic path: the closure takes an uninhabited value, so there is nothing here to fire.
61 Self(text.parse().unwrap_or_else(|never| match never {}))
62 }
63
64 /// The wrapped upstream value, borrowed — the road to the roster's own `match`, its
65 /// `IsVariant` predicates and everything else `mediaframe` publishes on it.
66 #[inline]
67 #[must_use]
68 pub const fn get(&self) -> &Foreign {
69 &self.0
70 }
71
72 /// The wrapped upstream value, owned. Canonical, the door having made it so.
73 #[inline]
74 #[must_use]
75 pub fn into_inner(self) -> Foreign {
76 self.0
77 }
78
79 /// The canonical slug — the text this value stores as, pages by, and crosses the wire as.
80 #[inline]
81 #[must_use]
82 pub fn as_str(&self) -> &str {
83 self.0.as_str()
84 }
85
86 /// Whether this build's roster CARRIES this codec, as against holding it in the open escape.
87 ///
88 /// `true` for `h264`, `false` for a name FFmpeg n9.0 did not have. It is the one question that is
89 /// actually about the table rather than about the value, and the honest form of the check a
90 /// fallible door would have been pretending to make — see the module note.
91 ///
92 /// The roster ITSELF is not restated here: it is
93 /// [`mediaframe::codec::VideoCodec::ROSTER`](mediaframe::codec::VideoCodec#associatedconstant.ROSTER),
94 /// and a citizen republishing its upstream's list would be a second copy to keep in step.
95 #[inline]
96 #[must_use]
97 pub fn is_registered(&self) -> bool {
98 !self.0.is_other()
99 }
100}
101
102super::codec_citizen!(
103 VideoCodec,
104 mediaframe::codec::VideoCodec,
105 VideoCodecWire,
106 "VideoCodecFilter"
107);
108
109#[cfg(test)]
110mod tests;