forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

config.rs171 lines, 5.3 KiBHistoryRawDownload
1//! Turns encoder output (Annex-B) into mp4 samples and sample-entry config records.
2
3const NAL_IDR: u8 = 5;
4const NAL_SEI: u8 = 6;
5const NAL_SPS: u8 = 7;
6const NAL_PPS: u8 = 8;
7const NAL_AUD: u8 = 9;
8
9const HEVC_VPS: u8 = 32;
10const HEVC_SPS: u8 = 33;
11const HEVC_PPS: u8 = 34;
12const HEVC_AUD: u8 = 35;
13
14const AV1C_HEAD: [u8; 4] = [0x81, 0x1F, 0x0C, 0x00];
15
16/// An H.264 sample, whether it is an IDR, and its first SPS and PPS when present.
17pub type H264Split = (Vec<u8>, bool, Option<(Vec<u8>, Vec<u8>)>);
18
19/// Splits an H.264 Annex-B access unit into a length-prefixed sample.
20///
21/// SPS, PPS, AUD and SEI are stripped from the sample. `None` when the unit holds no coded slice.
22pub fn h264_split_annex_b(annex_b: &[u8]) -> Option<H264Split> {
23 let nals = split_annex_b(annex_b);
24 let first = |kind: u8| nals.iter().copied().find(|n| nal_type(n) == kind);
25 let params = match (first(NAL_SPS), first(NAL_PPS)) {
26 (Some(sps), Some(pps)) => Some((sps.to_vec(), pps.to_vec())),
27 _ => None,
28 };
29 let mut out = Vec::with_capacity(annex_b.len());
30 let mut is_idr = false;
31 for nal in nals {
32 match nal_type(nal) {
33 NAL_SPS | NAL_PPS | NAL_AUD | NAL_SEI => continue,
34 NAL_IDR => is_idr = true,
35 _ => {}
36 }
37 out.extend_from_slice(&(nal.len() as u32).to_be_bytes());
38 out.extend_from_slice(nal);
39 }
40 (!out.is_empty()).then_some((out, is_idr, params))
41}
42
43/// An H.264 `AVCDecoderConfigurationRecord` for one SPS and one PPS, with four-byte NAL length prefixes.
44pub fn avcc_record(sps: &[u8], pps: &[u8]) -> Vec<u8> {
45 let profile = sps.get(1..4).unwrap_or(&[0, 0, 0]);
46 let mut out = vec![1, profile[0], profile[1], profile[2], 0xFF, 0xE1];
47 out.extend_from_slice(&(sps.len() as u16).to_be_bytes());
48 out.extend_from_slice(sps);
49 out.push(1);
50 out.extend_from_slice(&(pps.len() as u16).to_be_bytes());
51 out.extend_from_slice(pps);
52 out
53}
54
55/// An HEVC `HEVCDecoderConfigurationRecord` built from the VPS, SPS and PPS of an Annex-B access unit.
56///
57/// `None` when the unit lacks a VPS, SPS or PPS.
58pub fn hvcc_record(annex_b: &[u8], ten_bit: bool) -> Option<Vec<u8>> {
59 let nals = split_annex_b(annex_b);
60 let of = |kind: u8| nals.iter().copied().find(|n| hevc_nal_type(n) == kind);
61 let (vps, sps) = (of(HEVC_VPS)?, of(HEVC_SPS)?);
62 let ppss: Vec<&[u8]> = nals
63 .iter()
64 .copied()
65 .filter(|n| hevc_nal_type(n) == HEVC_PPS)
66 .collect();
67 if ppss.is_empty() {
68 return None;
69 }
70 let rbsp = unescape(sps.get(2..)?);
71 let mut rec = vec![1u8];
72 rec.extend_from_slice(rbsp.get(1..13)?);
73 rec.extend_from_slice(&[0xF0, 0x00]);
74 rec.push(0xFC);
75 rec.push(0xFC | 1);
76 let depth = if ten_bit { 0xFA } else { 0xF8 };
77 rec.push(depth);
78 rec.push(depth);
79 rec.extend_from_slice(&0u16.to_be_bytes());
80 rec.push(0b0000_1111);
81 rec.push(3);
82 for (kind, units) in [
83 (HEVC_VPS, vec![vps]),
84 (HEVC_SPS, vec![sps]),
85 (HEVC_PPS, ppss),
86 ] {
87 rec.push(0x80 | kind);
88 rec.extend_from_slice(&(units.len() as u16).to_be_bytes());
89 for nal in units {
90 rec.extend_from_slice(&(nal.len() as u16).to_be_bytes());
91 rec.extend_from_slice(nal);
92 }
93 }
94 Some(rec)
95}
96
97/// Splits an HEVC Annex-B access unit into a length-prefixed sample, dropping VPS, SPS, PPS and AUD.
98///
99/// `None` when the unit holds no NAL left to write.
100pub fn annex_b_to_hvcc(annex_b: &[u8]) -> Option<Vec<u8>> {
101 let mut out = Vec::with_capacity(annex_b.len());
102 for nal in split_annex_b(annex_b) {
103 if matches!(
104 hevc_nal_type(nal),
105 HEVC_VPS | HEVC_SPS | HEVC_PPS | HEVC_AUD
106 ) {
107 continue;
108 }
109 out.extend_from_slice(&(nal.len() as u32).to_be_bytes());
110 out.extend_from_slice(nal);
111 }
112 (!out.is_empty()).then_some(out)
113}
114
115/// An `av1C` record: the four fixed bytes, then the sequence header OBU as the config OBUs.
116pub fn av1c_record(ten_bit: bool, config_obus: &[u8]) -> Vec<u8> {
117 let mut head = AV1C_HEAD;
118 if ten_bit {
119 head[2] |= 0x40;
120 }
121 let mut out = head.to_vec();
122 out.extend_from_slice(config_obus);
123 out
124}
125
126fn hevc_nal_type(nal: &[u8]) -> u8 {
127 nal.first().map_or(0xFF, |b| (b >> 1) & 0x3F)
128}
129
130fn unescape(nal: &[u8]) -> Vec<u8> {
131 let mut out = Vec::with_capacity(nal.len());
132 for (i, &byte) in nal.iter().enumerate() {
133 if byte == 3 && i >= 2 && nal[i - 1] == 0 && nal[i - 2] == 0 {
134 continue;
135 }
136 out.push(byte);
137 }
138 out
139}
140
141fn split_annex_b(src: &[u8]) -> Vec<&[u8]> {
142 let mut nals = Vec::new();
143 let mut start = None;
144 let mut i = 0;
145 while i + 2 < src.len() {
146 if src[i] == 0 && src[i + 1] == 0 && src[i + 2] == 1 {
147 if let Some(s) = start {
148 push_nal(&mut nals, &src[s..i]);
149 }
150 i += 3;
151 start = Some(i);
152 } else {
153 i += 1;
154 }
155 }
156 if let Some(s) = start {
157 push_nal(&mut nals, &src[s..]);
158 }
159 nals
160}
161
162fn push_nal<'a>(nals: &mut Vec<&'a [u8]>, nal: &'a [u8]) {
163 let end = nal.iter().rposition(|&b| b != 0).map_or(0, |p| p + 1);
164 if end > 0 {
165 nals.push(&nal[..end]);
166 }
167}
168
169fn nal_type(nal: &[u8]) -> u8 {
170 nal.first().map_or(0, |b| b & 0x1F)
171}