use bytes::{BufMut, Bytes, BytesMut};
use std::sync::Arc;
pub const START_CODES: [&[u8]; 2] = [&[0, 0, 0, 1], &[0, 0, 1]];
pub fn annex_b_nals(data: &[u8]) -> Vec<&[u8]> {
let mut nals = Vec::new();
let mut i = 0;
while i + 3 <= data.len() {
let code = if data[i..].starts_with(&[0, 0, 0, 1]) {
4
} else if data[i..].starts_with(&[0, 0, 1]) {
3
} else {
i += 1;
continue;
};
let start = i + code;
let mut j = start;
while j < data.len() && !data[j..].starts_with(&[0, 0, 1]) {
j += 1;
}
if j >= 1 && data[j - 1..].starts_with(&[0, 0, 0, 1]) {
j -= 1;
}
nals.push(&data[start..j]);
i = j;
}
nals
}
pub fn nal_type_h264(nal: &[u8]) -> Option<u8> {
nal.first().map(|byte| byte & 0x1f)
}
pub fn nal_type_h265(nal: &[u8]) -> Option<u8> {
if nal.len() < 2 {
return None;
}
Some((nal[0] >> 1) & 0x3f)
}
pub fn annexb_to_avcc(data: &[u8]) -> Bytes {
let mut out = BytesMut::with_capacity(data.len());
for nalu in annex_b_nals(data) {
let end = nalu.iter().rposition(|&b| b != 0).map_or(0, |i| i + 1);
if end == 0 {
continue;
}
out.put_u32(end as u32);
out.put_slice(&nalu[..end]);
}
out.freeze()
}
pub fn is_idr(codec: super::Codec, data: &[u8]) -> bool {
for nal in annex_b_nals(data) {
let idr = match codec {
super::Codec::H264 => nal_type_h264(nal) == Some(5),
super::Codec::H265 => matches!(nal_type_h265(nal), Some(19 | 20)),
_ => false,
};
if idr {
return true;
}
}
false
}
pub fn is_h264_idr(nal: &[u8]) -> bool {
nal_type_h264(nal) == Some(5)
}
pub fn is_h265_random_access(nal: &[u8]) -> bool {
matches!(nal_type_h265(nal), Some(16..=21))
}
pub fn is_random_access_point(codec: super::Codec, data: &[u8]) -> bool {
match codec {
super::Codec::H264 => annex_b_nals(data).iter().any(|nal| is_h264_idr(nal)),
super::Codec::H265 => annex_b_nals(data)
.iter()
.any(|nal| is_h265_random_access(nal)),
_ => true,
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ParameterSets {
pub vps: Option<Bytes>,
pub sps: Option<Bytes>,
pub pps: Option<Bytes>,
}
impl ParameterSets {
pub fn is_empty(&self) -> bool {
self.vps.is_none() && self.sps.is_none() && self.pps.is_none()
}
}
pub fn h264_parameter_sets(data: &[u8]) -> ParameterSets {
let mut sets = ParameterSets::default();
for nal in annex_b_nals(data) {
match nal_type_h264(nal) {
Some(7) if sets.sps.is_none() => sets.sps = Some(Bytes::copy_from_slice(nal)),
Some(8) if sets.pps.is_none() => sets.pps = Some(Bytes::copy_from_slice(nal)),
_ => {}
}
if sets.sps.is_some() && sets.pps.is_some() {
break;
}
}
sets
}
pub fn h265_parameter_sets(data: &[u8]) -> ParameterSets {
let mut sets = ParameterSets::default();
for nal in annex_b_nals(data) {
match nal_type_h265(nal) {
Some(32) if sets.vps.is_none() => sets.vps = Some(Bytes::copy_from_slice(nal)),
Some(33) if sets.sps.is_none() => sets.sps = Some(Bytes::copy_from_slice(nal)),
Some(34) if sets.pps.is_none() => sets.pps = Some(Bytes::copy_from_slice(nal)),
_ => {}
}
if sets.vps.is_some() && sets.sps.is_some() && sets.pps.is_some() {
break;
}
}
sets
}
pub fn base64_nal(nal: &[u8]) -> String {
use base64::Engine as _;
base64::engine::general_purpose::STANDARD.encode(nal)
}
pub fn h264_profile_level_id(sps: &[u8]) -> Option<String> {
let offset = if sps.first().is_some_and(|byte| byte & 0x1f == 7) {
1 } else {
0 };
let profile = sps.get(offset)?;
let constraint = sps.get(offset + 1)?;
let level = sps.get(offset + 2)?;
Some(format!("{profile:02x}{constraint:02x}{level:02x}"))
}
pub fn h264_fmtp(sps: &[u8], pps: &[u8]) -> String {
let profile = h264_profile_level_id(sps).unwrap_or_else(|| "42001f".into());
format!(
"profile-level-id={profile};sprop-parameter-sets={},{};packetization-mode=1",
base64_nal(sps),
base64_nal(pps)
)
}
pub fn h265_fmtp(vps: &[u8], sps: &[u8], pps: &[u8]) -> String {
format!(
"sprop-vps={};sprop-sps={};sprop-pps={}",
base64_nal(vps),
base64_nal(sps),
base64_nal(pps)
)
}
pub fn build_avc_config_record(sps: &[u8], pps: &[u8]) -> Option<Bytes> {
if sps.len() < 4 || pps.is_empty() {
return None;
}
let mut buf = BytesMut::with_capacity(sps.len() + pps.len() + 16);
buf.put_u8(1); buf.put_u8(sps[1]); buf.put_u8(sps[2]); buf.put_u8(sps[3]); buf.put_u8(0xff); buf.put_u8(0xe1); buf.put_u16(sps.len() as u16);
buf.put_slice(sps);
buf.put_u8(1); buf.put_u16(pps.len() as u16);
buf.put_slice(pps);
Some(buf.freeze())
}
pub fn build_hevc_config_record(vps: &[u8], sps: &[u8], pps: &[u8]) -> Option<Bytes> {
if vps.len() < 2 || sps.len() <= 14 || pps.len() < 2 {
return None;
}
let profile = &sps[2..];
let mut buf = BytesMut::with_capacity(128);
buf.put_u8(1);
buf.put_u8(profile[1]);
buf.put_slice(&profile[2..6]);
buf.put_slice(&profile[6..12]);
buf.put_u8(profile[12]);
buf.put_u16(0xf000);
buf.put_u8(0xfc);
buf.put_u8(0xfd);
buf.put_u8(0xf8);
buf.put_u8(0xf8);
buf.put_u16(0);
buf.put_u8(0x0f);
buf.put_u8(3);
for (kind, nal) in [(32u8, vps), (33, sps), (34, pps)] {
buf.put_u8(0x80 | kind);
buf.put_u16(1);
buf.put_u16(nal.len() as u16);
buf.put_slice(nal);
}
Some(buf.freeze())
}
const AAC_SAMPLE_RATES: [u32; 13] = [
96_000, 88_200, 64_000, 48_000, 44_100, 32_000, 24_000, 22_050, 16_000, 12_000, 11_025, 8_000,
7_350,
];
pub fn aac_sample_rate_for_index(index: u8) -> Option<u32> {
AAC_SAMPLE_RATES.get(index as usize).copied()
}
pub fn aac_sample_rate_index(sample_rate: u32) -> u8 {
AAC_SAMPLE_RATES
.iter()
.position(|rate| *rate == sample_rate)
.unwrap_or(4) as u8
}
pub fn aac_audio_specific_config_bytes(sample_rate: u32, channels: u8) -> Bytes {
let freq_index = aac_sample_rate_index(sample_rate);
let channel_config = channels.min(7);
let byte0 = (2u8 << 3) | ((freq_index >> 1) & 0x07);
let byte1 = ((freq_index & 0x01) << 7) | ((channel_config & 0x0F) << 3);
Bytes::from(vec![byte0, byte1])
}
pub fn aac_audio_specific_config(sample_rate: u32, channels: u8) -> Option<String> {
let sampling_frequency_index = match sample_rate {
96_000 => 0,
88_200 => 1,
64_000 => 2,
48_000 => 3,
44_100 => 4,
32_000 => 5,
24_000 => 6,
22_050 => 7,
16_000 => 8,
12_000 => 9,
11_025 => 10,
8_000 => 11,
_ => return None,
};
let channel_configuration = channels.clamp(1, 7);
let mut asc = 0u16;
asc |= 2 << 11; asc |= sampling_frequency_index << 7;
asc |= u16::from(channel_configuration) << 3;
Some(format!("{asc:04x}"))
}
pub fn aac_fmtp(config: &str) -> String {
format!(
"profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config={config}"
)
}
pub type FmtpValue = Arc<str>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn annex_b_splitting_handles_3_and_4_byte_codes() {
let data: &[u8] = &[
0, 0, 0, 1, 0x65, 1, 2, 0, 0, 1, 0x41, 3, 0, 0, 0, 1, 0x67, 4,
];
let nals = annex_b_nals(data);
assert_eq!(nals.len(), 3);
assert_eq!(nals[0], &[0x65, 1, 2]);
assert_eq!(nals[1], &[0x41, 3]);
assert_eq!(nals[2], &[0x67, 4]);
}
#[test]
fn empty_or_raw_data_yields_no_nals() {
assert!(annex_b_nals(&[]).is_empty());
assert!(annex_b_nals(&[0x65, 0x88, 0x84]).is_empty());
}
#[test]
fn h264_sets_extraction_and_fmtp() {
let idr = [
0, 0, 0, 1, 0x67, 0x42, 0x00, 0x1f, 0x8c, 0x8d, 0x40, 0, 0, 0, 1, 0x68, 0xce, 0x3c, 0x80, 0, 0, 0, 1, 0x65, 0x88, 0x84, ];
let sets = h264_parameter_sets(&idr);
assert_eq!(
sets.sps.as_deref(),
Some(&[0x67, 0x42, 0x00, 0x1f, 0x8c, 0x8d, 0x40][..])
);
assert_eq!(sets.pps.as_deref(), Some(&[0x68, 0xce, 0x3c, 0x80][..]));
let fmtp = h264_fmtp(sets.sps.as_deref().unwrap(), sets.pps.as_deref().unwrap());
assert!(
fmtp.starts_with("profile-level-id=42001f;sprop-parameter-sets="),
"{fmtp}"
);
assert!(fmtp.ends_with(";packetization-mode=1"));
assert!(is_idr(crate::Codec::H264, &idr));
}
#[test]
fn h265_sets_extraction() {
let idr = [
0, 0, 0, 1, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00,
0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x3c, 0x95, 0xc0, 0x90, 0xa1, 0, 0, 0, 1, 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00,
0x03, 0x00, 0x00, 0x03, 0x00, 0x3c, 0xa0, 0, 0, 0, 1, 0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40, 0, 0, 0, 1, 0x26, 0x01, 0xaf, 0x0a, ];
let sets = h265_parameter_sets(&idr);
assert!(sets.vps.is_some() && sets.sps.is_some() && sets.pps.is_some());
assert!(is_idr(crate::Codec::H265, &idr));
let fmtp = h265_fmtp(
sets.vps.as_deref().unwrap(),
sets.sps.as_deref().unwrap(),
sets.pps.as_deref().unwrap(),
);
assert!(fmtp.starts_with("sprop-vps="), "{fmtp}");
}
#[test]
fn aac_config_generation() {
assert_eq!(
aac_audio_specific_config(48_000, 2).as_deref(),
Some("1190")
);
assert_eq!(
aac_audio_specific_config(44_100, 2).as_deref(),
Some("1210")
);
assert_eq!(aac_audio_specific_config(8_000, 1).as_deref(), Some("1588"));
assert_eq!(
aac_audio_specific_config(11_025, 2).as_deref(),
Some("1510")
);
assert!(aac_audio_specific_config(12_345, 2).is_none());
}
}