Skip to main content

hang/catalog/video/
codec.rs

1use super::*;
2
3use std::str::FromStr;
4
5use derive_more::{Display, From};
6
7use crate::Error;
8
9/// Supported video codec mimetypes.
10// TODO implement serde for convience
11#[derive(Debug, Clone, PartialEq, Eq, Display, From)]
12#[non_exhaustive]
13pub enum VideoCodec {
14	/// H.264/AVC codec with profile and level information
15	H264(H264),
16	/// H.265/HEVC codec with profile and level information
17	H265(H265),
18	/// VP9 codec with profile and color information
19	VP9(VP9),
20	/// AV1 codec with profile and level information
21	AV1(AV1),
22
23	/// VP8 codec (no additional parameters)
24	#[display("vp8")]
25	VP8,
26
27	/// Unknown or unsupported codec with original string
28	#[display("{_0}")]
29	Unknown(String),
30}
31
32/// Coarse video codec family, used for tag-only matching.
33#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum VideoCodecKind {
36	/// H.264 / AVC.
37	H264,
38	/// H.265 / HEVC.
39	H265,
40	/// VP8.
41	VP8,
42	/// VP9.
43	VP9,
44	/// AV1.
45	AV1,
46	/// Any codec not listed above.
47	Unknown,
48}
49
50impl VideoCodec {
51	/// Return the coarse codec family for tag-only matching.
52	pub fn kind(&self) -> VideoCodecKind {
53		match self {
54			Self::H264(_) => VideoCodecKind::H264,
55			Self::H265(_) => VideoCodecKind::H265,
56			Self::VP9(_) => VideoCodecKind::VP9,
57			Self::AV1(_) => VideoCodecKind::AV1,
58			Self::VP8 => VideoCodecKind::VP8,
59			Self::Unknown(_) => VideoCodecKind::Unknown,
60		}
61	}
62}
63
64impl FromStr for VideoCodec {
65	type Err = Error;
66
67	fn from_str(s: &str) -> Result<Self, Self::Err> {
68		if s.starts_with("avc1.") || s.starts_with("avc3.") {
69			return H264::from_str(s).map(Into::into);
70		} else if s.starts_with("hvc1.") || s.starts_with("hev1.") {
71			return H265::from_str(s).map(Into::into);
72		} else if s == "vp8" {
73			return Ok(Self::VP8);
74		} else if s.starts_with("vp09.") {
75			return VP9::from_str(s).map(Into::into);
76		} else if s.starts_with("av01.") {
77			return AV1::from_str(s).map(Into::into);
78		}
79
80		Ok(Self::Unknown(s.to_string()))
81	}
82}