use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FourCC([u8; 4]);
impl FourCC {
pub const fn new(bytes: [u8; 4]) -> Self {
Self(bytes)
}
pub fn from_str(s: &str) -> Option<Self> {
let bytes = s.as_bytes();
if bytes.len() == 4 && bytes.iter().all(|b| b.is_ascii()) {
Some(Self([bytes[0], bytes[1], bytes[2], bytes[3]]))
} else {
None
}
}
pub const fn from_u32(value: u32) -> Self {
Self(value.to_be_bytes())
}
pub const fn as_u32(&self) -> u32 {
u32::from_be_bytes(self.0)
}
pub const fn as_bytes(&self) -> &[u8; 4] {
&self.0
}
pub fn as_str(&self) -> &str {
std::str::from_utf8(&self.0).unwrap_or("????")
}
}
impl fmt::Debug for FourCC {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FourCC(\"{}\")", self.as_str())
}
}
impl fmt::Display for FourCC {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VideoFourCc {
Avc,
Hevc,
Av1,
Vp9,
Vp8,
}
impl VideoFourCc {
pub const AVC_FOURCC: FourCC = FourCC::new(*b"avc1");
pub const HEVC_FOURCC: FourCC = FourCC::new(*b"hvc1");
pub const AV1_FOURCC: FourCC = FourCC::new(*b"av01");
pub const VP9_FOURCC: FourCC = FourCC::new(*b"vp09");
pub const VP8_FOURCC: FourCC = FourCC::new(*b"vp08");
pub const fn fourcc(&self) -> FourCC {
match self {
VideoFourCc::Avc => Self::AVC_FOURCC,
VideoFourCc::Hevc => Self::HEVC_FOURCC,
VideoFourCc::Av1 => Self::AV1_FOURCC,
VideoFourCc::Vp9 => Self::VP9_FOURCC,
VideoFourCc::Vp8 => Self::VP8_FOURCC,
}
}
pub fn from_fourcc(fourcc: FourCC) -> Option<Self> {
match fourcc.as_bytes() {
b"avc1" => Some(VideoFourCc::Avc),
b"hvc1" => Some(VideoFourCc::Hevc),
b"av01" => Some(VideoFourCc::Av1),
b"vp09" => Some(VideoFourCc::Vp9),
b"vp08" => Some(VideoFourCc::Vp8),
_ => None,
}
}
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() >= 4 {
let fourcc = FourCC::new([bytes[0], bytes[1], bytes[2], bytes[3]]);
Self::from_fourcc(fourcc)
} else {
None
}
}
pub fn from_u32(value: u32) -> Option<Self> {
Self::from_fourcc(FourCC::from_u32(value))
}
pub fn from_fourcc_str(s: &str) -> Option<Self> {
FourCC::from_str(s).and_then(Self::from_fourcc)
}
pub fn as_fourcc_str(&self) -> &'static str {
match self {
VideoFourCc::Avc => "avc1",
VideoFourCc::Hevc => "hvc1",
VideoFourCc::Av1 => "av01",
VideoFourCc::Vp9 => "vp09",
VideoFourCc::Vp8 => "vp08",
}
}
pub const fn name(&self) -> &'static str {
match self {
VideoFourCc::Avc => "H.264/AVC",
VideoFourCc::Hevc => "H.265/HEVC",
VideoFourCc::Av1 => "AV1",
VideoFourCc::Vp9 => "VP9",
VideoFourCc::Vp8 => "VP8",
}
}
}
impl fmt::Display for VideoFourCc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AudioFourCc {
Aac,
Opus,
Flac,
Ac3,
Eac3,
Mp3,
}
impl AudioFourCc {
pub const AAC_FOURCC: FourCC = FourCC::new(*b"mp4a");
pub const OPUS_FOURCC: FourCC = FourCC::new(*b"Opus");
pub const FLAC_FOURCC: FourCC = FourCC::new(*b"fLaC");
pub const AC3_FOURCC: FourCC = FourCC::new(*b"ac-3");
pub const EAC3_FOURCC: FourCC = FourCC::new(*b"ec-3");
pub const MP3_FOURCC: FourCC = FourCC::new(*b".mp3");
pub const fn fourcc(&self) -> FourCC {
match self {
AudioFourCc::Aac => Self::AAC_FOURCC,
AudioFourCc::Opus => Self::OPUS_FOURCC,
AudioFourCc::Flac => Self::FLAC_FOURCC,
AudioFourCc::Ac3 => Self::AC3_FOURCC,
AudioFourCc::Eac3 => Self::EAC3_FOURCC,
AudioFourCc::Mp3 => Self::MP3_FOURCC,
}
}
pub fn from_fourcc(fourcc: FourCC) -> Option<Self> {
match fourcc.as_bytes() {
b"mp4a" => Some(AudioFourCc::Aac),
b"Opus" => Some(AudioFourCc::Opus),
b"fLaC" => Some(AudioFourCc::Flac),
b"ac-3" => Some(AudioFourCc::Ac3),
b"ec-3" => Some(AudioFourCc::Eac3),
b".mp3" => Some(AudioFourCc::Mp3),
_ => None,
}
}
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() >= 4 {
let fourcc = FourCC::new([bytes[0], bytes[1], bytes[2], bytes[3]]);
Self::from_fourcc(fourcc)
} else {
None
}
}
pub fn from_u32(value: u32) -> Option<Self> {
Self::from_fourcc(FourCC::from_u32(value))
}
pub fn from_fourcc_str(s: &str) -> Option<Self> {
FourCC::from_str(s).and_then(Self::from_fourcc)
}
pub fn as_fourcc_str(&self) -> &'static str {
match self {
AudioFourCc::Aac => "mp4a",
AudioFourCc::Opus => "Opus",
AudioFourCc::Flac => "fLaC",
AudioFourCc::Ac3 => "ac-3",
AudioFourCc::Eac3 => "ec-3",
AudioFourCc::Mp3 => ".mp3",
}
}
pub const fn name(&self) -> &'static str {
match self {
AudioFourCc::Aac => "AAC",
AudioFourCc::Opus => "Opus",
AudioFourCc::Flac => "FLAC",
AudioFourCc::Ac3 => "AC-3",
AudioFourCc::Eac3 => "E-AC-3",
AudioFourCc::Mp3 => "MP3",
}
}
}
impl fmt::Display for AudioFourCc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fourcc_from_str() {
let fourcc = FourCC::from_str("avc1").unwrap();
assert_eq!(fourcc.as_str(), "avc1");
assert_eq!(fourcc.as_bytes(), b"avc1");
assert!(FourCC::from_str("avc").is_none());
assert!(FourCC::from_str("avc12").is_none());
assert!(FourCC::from_str("avc\u{00e9}").is_none());
}
#[test]
fn test_fourcc_u32_conversion() {
let fourcc = FourCC::from_str("av01").unwrap();
assert_eq!(fourcc.as_u32(), 0x61763031);
let fourcc2 = FourCC::from_u32(0x61763031);
assert_eq!(fourcc2.as_str(), "av01");
assert_eq!(fourcc, fourcc2);
}
#[test]
fn test_fourcc_debug_display() {
let fourcc = FourCC::from_str("hvc1").unwrap();
assert_eq!(format!("{:?}", fourcc), "FourCC(\"hvc1\")");
assert_eq!(format!("{}", fourcc), "hvc1");
}
#[test]
fn test_video_fourcc_values() {
assert_eq!(VideoFourCc::Avc.fourcc().as_str(), "avc1");
assert_eq!(VideoFourCc::Hevc.fourcc().as_str(), "hvc1");
assert_eq!(VideoFourCc::Av1.fourcc().as_str(), "av01");
assert_eq!(VideoFourCc::Vp9.fourcc().as_str(), "vp09");
assert_eq!(VideoFourCc::Vp8.fourcc().as_str(), "vp08");
}
#[test]
fn test_video_fourcc_parsing() {
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("avc1").unwrap()),
Some(VideoFourCc::Avc)
);
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("hvc1").unwrap()),
Some(VideoFourCc::Hevc)
);
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("av01").unwrap()),
Some(VideoFourCc::Av1)
);
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("vp09").unwrap()),
Some(VideoFourCc::Vp9)
);
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("vp08").unwrap()),
Some(VideoFourCc::Vp8)
);
assert_eq!(
VideoFourCc::from_fourcc(FourCC::from_str("xxxx").unwrap()),
None
);
}
#[test]
fn test_video_fourcc_from_bytes() {
assert_eq!(VideoFourCc::from_bytes(b"avc1"), Some(VideoFourCc::Avc));
assert_eq!(
VideoFourCc::from_bytes(b"hvc1extra"),
Some(VideoFourCc::Hevc)
);
assert_eq!(VideoFourCc::from_bytes(b"av"), None); }
#[test]
fn test_video_fourcc_from_u32() {
assert_eq!(VideoFourCc::from_u32(0x61766331), Some(VideoFourCc::Avc));
assert_eq!(VideoFourCc::from_u32(0x61763031), Some(VideoFourCc::Av1));
}
#[test]
fn test_video_fourcc_name() {
assert_eq!(VideoFourCc::Avc.name(), "H.264/AVC");
assert_eq!(VideoFourCc::Hevc.name(), "H.265/HEVC");
assert_eq!(VideoFourCc::Av1.name(), "AV1");
assert_eq!(VideoFourCc::Vp9.name(), "VP9");
assert_eq!(VideoFourCc::Vp8.name(), "VP8");
}
#[test]
fn test_audio_fourcc_values() {
assert_eq!(AudioFourCc::Aac.fourcc().as_str(), "mp4a");
assert_eq!(AudioFourCc::Opus.fourcc().as_str(), "Opus");
assert_eq!(AudioFourCc::Flac.fourcc().as_str(), "fLaC");
assert_eq!(AudioFourCc::Ac3.fourcc().as_str(), "ac-3");
assert_eq!(AudioFourCc::Eac3.fourcc().as_str(), "ec-3");
assert_eq!(AudioFourCc::Mp3.fourcc().as_str(), ".mp3");
}
#[test]
fn test_audio_fourcc_parsing() {
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("mp4a").unwrap()),
Some(AudioFourCc::Aac)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("Opus").unwrap()),
Some(AudioFourCc::Opus)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("fLaC").unwrap()),
Some(AudioFourCc::Flac)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("ac-3").unwrap()),
Some(AudioFourCc::Ac3)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("ec-3").unwrap()),
Some(AudioFourCc::Eac3)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str(".mp3").unwrap()),
Some(AudioFourCc::Mp3)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("xxxx").unwrap()),
None
);
}
#[test]
fn test_audio_fourcc_case_sensitivity() {
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("Opus").unwrap()),
Some(AudioFourCc::Opus)
);
assert_eq!(
AudioFourCc::from_fourcc(FourCC::from_str("opus").unwrap()),
None
);
}
#[test]
fn test_audio_fourcc_from_bytes() {
assert_eq!(AudioFourCc::from_bytes(b"mp4a"), Some(AudioFourCc::Aac));
assert_eq!(AudioFourCc::from_bytes(b"Opus"), Some(AudioFourCc::Opus));
assert_eq!(AudioFourCc::from_bytes(b"mp"), None); }
#[test]
fn test_audio_fourcc_from_u32() {
assert_eq!(AudioFourCc::from_u32(0x6d703461), Some(AudioFourCc::Aac));
assert_eq!(AudioFourCc::from_u32(0x4f707573), Some(AudioFourCc::Opus));
}
#[test]
fn test_audio_fourcc_name() {
assert_eq!(AudioFourCc::Aac.name(), "AAC");
assert_eq!(AudioFourCc::Opus.name(), "Opus");
assert_eq!(AudioFourCc::Flac.name(), "FLAC");
assert_eq!(AudioFourCc::Ac3.name(), "AC-3");
assert_eq!(AudioFourCc::Eac3.name(), "E-AC-3");
assert_eq!(AudioFourCc::Mp3.name(), "MP3");
}
#[test]
fn test_fourcc_equality() {
let a = FourCC::from_str("avc1").unwrap();
let b = FourCC::new(*b"avc1");
let c = FourCC::from_u32(0x61766331);
assert_eq!(a, b);
assert_eq!(b, c);
assert_eq!(a, c);
}
#[test]
fn test_fourcc_hash() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(VideoFourCc::Avc);
set.insert(VideoFourCc::Hevc);
set.insert(VideoFourCc::Avc);
assert_eq!(set.len(), 2);
assert!(set.contains(&VideoFourCc::Avc));
assert!(set.contains(&VideoFourCc::Hevc));
}
#[test]
fn test_video_fourcc_display() {
assert_eq!(format!("{}", VideoFourCc::Avc), "H.264/AVC");
assert_eq!(format!("{}", VideoFourCc::Hevc), "H.265/HEVC");
}
#[test]
fn test_audio_fourcc_display() {
assert_eq!(format!("{}", AudioFourCc::Aac), "AAC");
assert_eq!(format!("{}", AudioFourCc::Opus), "Opus");
}
#[test]
fn test_video_fourcc_from_fourcc_str() {
assert_eq!(VideoFourCc::from_fourcc_str("avc1"), Some(VideoFourCc::Avc));
assert_eq!(
VideoFourCc::from_fourcc_str("hvc1"),
Some(VideoFourCc::Hevc)
);
assert_eq!(VideoFourCc::from_fourcc_str("av01"), Some(VideoFourCc::Av1));
assert_eq!(VideoFourCc::from_fourcc_str("vp09"), Some(VideoFourCc::Vp9));
assert_eq!(VideoFourCc::from_fourcc_str("vp08"), Some(VideoFourCc::Vp8));
assert_eq!(VideoFourCc::from_fourcc_str("xxxx"), None);
assert_eq!(VideoFourCc::from_fourcc_str("avc"), None); assert_eq!(VideoFourCc::from_fourcc_str("avc12"), None); }
#[test]
fn test_video_fourcc_as_fourcc_str() {
assert_eq!(VideoFourCc::Avc.as_fourcc_str(), "avc1");
assert_eq!(VideoFourCc::Hevc.as_fourcc_str(), "hvc1");
assert_eq!(VideoFourCc::Av1.as_fourcc_str(), "av01");
assert_eq!(VideoFourCc::Vp9.as_fourcc_str(), "vp09");
assert_eq!(VideoFourCc::Vp8.as_fourcc_str(), "vp08");
}
#[test]
fn test_audio_fourcc_from_fourcc_str() {
assert_eq!(AudioFourCc::from_fourcc_str("mp4a"), Some(AudioFourCc::Aac));
assert_eq!(
AudioFourCc::from_fourcc_str("Opus"),
Some(AudioFourCc::Opus)
);
assert_eq!(
AudioFourCc::from_fourcc_str("fLaC"),
Some(AudioFourCc::Flac)
);
assert_eq!(AudioFourCc::from_fourcc_str("ac-3"), Some(AudioFourCc::Ac3));
assert_eq!(
AudioFourCc::from_fourcc_str("ec-3"),
Some(AudioFourCc::Eac3)
);
assert_eq!(AudioFourCc::from_fourcc_str(".mp3"), Some(AudioFourCc::Mp3));
assert_eq!(AudioFourCc::from_fourcc_str("xxxx"), None);
assert_eq!(AudioFourCc::from_fourcc_str("opus"), None); }
#[test]
fn test_audio_fourcc_as_fourcc_str() {
assert_eq!(AudioFourCc::Aac.as_fourcc_str(), "mp4a");
assert_eq!(AudioFourCc::Opus.as_fourcc_str(), "Opus");
assert_eq!(AudioFourCc::Flac.as_fourcc_str(), "fLaC");
assert_eq!(AudioFourCc::Ac3.as_fourcc_str(), "ac-3");
assert_eq!(AudioFourCc::Eac3.as_fourcc_str(), "ec-3");
assert_eq!(AudioFourCc::Mp3.as_fourcc_str(), ".mp3");
}
#[test]
fn test_fourcc_str_roundtrip() {
for codec in [
VideoFourCc::Avc,
VideoFourCc::Hevc,
VideoFourCc::Av1,
VideoFourCc::Vp9,
VideoFourCc::Vp8,
] {
let s = codec.as_fourcc_str();
let parsed = VideoFourCc::from_fourcc_str(s).unwrap();
assert_eq!(parsed, codec);
}
for codec in [
AudioFourCc::Aac,
AudioFourCc::Opus,
AudioFourCc::Flac,
AudioFourCc::Ac3,
AudioFourCc::Eac3,
AudioFourCc::Mp3,
] {
let s = codec.as_fourcc_str();
let parsed = AudioFourCc::from_fourcc_str(s).unwrap();
assert_eq!(parsed, codec);
}
}
}