#[cfg(test)]
mod h265_test;
use super::*;
pub(crate) const DEFAULT_LEVEL_ID: u8 = 93;
pub(crate) const PARAMETERS: &[ParameterUse] = &[
row("profile-space", ParameterRole::Configuration, true, true),
row("profile-id", ParameterRole::Configuration, true, true),
row("tier-flag", ParameterRole::Configuration, true, true),
row("level-id", ParameterRole::Configuration, true, true),
row(
"interop-constraints",
ParameterRole::Configuration,
true,
true,
),
row(
"profile-compatibility-indicator",
ParameterRole::Configuration,
true,
true,
),
row("tx-mode", ParameterRole::Configuration, true, true),
row(
"max-recv-level-id",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"sprop-max-don-diff",
ParameterRole::StreamProperty,
false,
true,
),
row(
"sprop-depack-buf-nalus",
ParameterRole::StreamProperty,
false,
true,
),
row(
"sprop-depack-buf-bytes",
ParameterRole::StreamProperty,
false,
true,
),
row(
"depack-buf-cap",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"sprop-segmentation-id",
ParameterRole::StreamProperty,
true,
true,
),
row(
"sprop-spatial-segmentation-idc",
ParameterRole::StreamProperty,
true,
true,
),
row("max-br", ParameterRole::ReceiverCapability, true, false),
row("max-cpb", ParameterRole::ReceiverCapability, true, false),
row("max-dpb", ParameterRole::ReceiverCapability, true, false),
row("max-lsr", ParameterRole::ReceiverCapability, true, false),
row("max-lps", ParameterRole::ReceiverCapability, true, false),
row("max-tr", ParameterRole::ReceiverCapability, true, false),
row("max-tc", ParameterRole::ReceiverCapability, true, false),
row("max-fps", ParameterRole::ReceiverCapability, true, false),
row("sprop-vps", ParameterRole::ParameterSets, false, true),
row("sprop-sps", ParameterRole::ParameterSets, false, true),
row("sprop-pps", ParameterRole::ParameterSets, false, true),
row("sprop-sei", ParameterRole::ParameterSets, false, true),
row(
"sprop-sub-layer-id",
ParameterRole::StreamProperty,
false,
true,
),
row(
"recv-sub-layer-id",
ParameterRole::OperationPoint,
true,
false,
),
row(
"dec-parallel-cap",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"include-dph",
ParameterRole::ReceiverCapability,
true,
false,
),
];
const fn row(
name: &'static str,
role: ParameterRole,
recvonly: bool,
sendonly: bool,
) -> ParameterUse {
ParameterUse {
name,
role,
recvonly,
sendonly,
}
}
#[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
}
}