forge
Explore
Log in

Don't you dare go Hollow

S13: avcC and hvcC parameter sets read through one u16-prefixed helper (same reads, same early returns)

Browse files at this commit

73bc1eff94d3ac487cbaec5da1ce0c02e4b55832Ahmet Kaan GümüşParent: ac6f803
crates/ec-cc/src/extract.rsmodified+13 -8
@@ -150,6 +150,15 @@ fn for_each_nal(data: &[u8], fmt: NalFormat, mut f: impl FnMut(&[u8])) {
150150 }
151151 }
152152
153+/// A parameter set in `avcC` or `hvcC`: a big-endian u16 length, then the NAL.
154+/// Returns the NAL and the offset just past it.
155+fn u16_prefixed(data: &[u8], at: usize) -> Option<(&[u8], usize)> {
156+ let len = data.get(at..at + 2)?;
157+ let len = u16::from_be_bytes([len[0], len[1]]) as usize;
158+ let nal = data.get(at + 2..at + 2 + len)?;
159+ Some((nal, at + 2 + len))
160+}
161+
153162 fn unescape(p: &[u8]) -> Vec<u8> {
154163 let mut out = Vec::with_capacity(p.len());
155164 let mut zeros = 0;
@@ -242,13 +251,11 @@ impl H264Extractor {
242251 };
243252 i += 1;
244253 for _ in 0..count {
245- let Some(l) = avcc.get(i..i + 2) else { return };
246- let l = u16::from_be_bytes([l[0], l[1]]) as usize;
247- let Some(n) = avcc.get(i + 2..i + 2 + l) else {
254+ let Some((n, next)) = u16_prefixed(avcc, i) else {
248255 return;
249256 };
250257 sets.push(n);
251- i += 2 + l;
258+ i = next;
252259 }
253260 }
254261 for n in sets {
@@ -408,13 +415,11 @@ impl HevcExtractor {
408415 let count = u16::from_be_bytes([h[1], h[2]]) as usize;
409416 i += 3;
410417 for _ in 0..count {
411- let Some(l) = hvcc.get(i..i + 2) else { return };
412- let l = u16::from_be_bytes([l[0], l[1]]) as usize;
413- let Some(n) = hvcc.get(i + 2..i + 2 + l) else {
418+ let Some((n, next)) = u16_prefixed(hvcc, i) else {
414419 return;
415420 };
416421 self.nal(n, None);
417- i += 2 + l;
422+ i = next;
418423 }
419424 }
420425 }