#[cfg(test)]
mod h265_test;
use super::*;
pub(crate) const DEFAULT_LEVEL_ID: u8 = 93;
#[derive(Debug, PartialEq)]
pub(crate) struct H265Fmtp {
pub(crate) parameters: HashMap<String, String>,
}
impl H265Fmtp {
fn number(&self, key: &str, absent: u32) -> Option<u32> {
match self.parameters.get(key) {
None => Some(absent),
Some(value) => value.trim().parse().ok(),
}
}
fn profile(&self) -> Option<(u32, u32)> {
Some((
self.number("profile-space", 0)?,
self.number("profile-id", 1)?,
))
}
fn tier_flag(&self) -> Option<u32> {
self.number("tier-flag", 0)
}
pub(crate) fn level_id(&self) -> Option<u8> {
match self.parameters.get("level-id") {
None => Some(DEFAULT_LEVEL_ID),
Some(value) => value.trim().parse().ok(),
}
}
fn interop_constraints(&self) -> Option<u64> {
match self.parameters.get("interop-constraints") {
None => Some(0xB000_0000_0000),
Some(value) => {
let value = value.trim();
(value.len() == 12)
.then(|| u64::from_str_radix(value, 16).ok())
.flatten()
}
}
}
fn profile_compatibility_indicator(&self) -> Option<u32> {
match self.parameters.get("profile-compatibility-indicator") {
None => {
let (_, profile_id) = self.profile()?;
(profile_id < 32).then(|| 1u32 << (31 - profile_id))
}
Some(value) => {
let value = value.trim();
(value.len() == 8)
.then(|| u32::from_str_radix(value, 16).ok())
.flatten()
}
}
}
pub(crate) fn tx_mode(&self) -> Option<&'static str> {
match self.parameters.get("tx-mode") {
None => Some("SRST"),
Some(value) => ["SRST", "MRST", "MRMT"]
.into_iter()
.find(|mode| value.trim().eq_ignore_ascii_case(mode)),
}
}
#[allow(clippy::type_complexity)]
fn configuration(&self) -> Option<((u32, u32), u32, u64, u32, &'static str)> {
Some((
self.profile()?,
self.tier_flag()?,
self.interop_constraints()?,
self.profile_compatibility_indicator()?,
self.tx_mode()?,
))
}
}
impl Fmtp for H265Fmtp {
fn mime_type(&self) -> &str {
"video/h265"
}
fn match_fmtp(&self, f: &dyn Fmtp) -> bool {
let Some(c) = f.as_any().downcast_ref::<H265Fmtp>() else {
return false;
};
matches!((self.configuration(), c.configuration()), (Some(a), Some(b)) if a == b)
}
fn parameter(&self, key: &str) -> Option<&String> {
self.parameters.get(key)
}
fn equal(&self, other: &dyn Fmtp) -> bool {
other.as_any().downcast_ref::<H265Fmtp>() == Some(self)
}
fn as_any(&self) -> &dyn Any {
self
}
}