S13: avcC and hvcC parameter sets read through one u16-prefixed helper (same reads, same early returns)
Browse files at this commit73bc1eff94d3ac487cbaec5da1ce0c02e4b55832Parent: ac6f8031 changed file +13 -8
- crates/ec-cc/src/extract.rs +13 -8
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])) { | ||
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | |
| 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 | + | |
| 153 | 162 | fn unescape(p: &[u8]) -> Vec<u8> { |
| 154 | 163 | let mut out = Vec::with_capacity(p.len()); |
| 155 | 164 | let mut zeros = 0; |
| @@ -242,13 +251,11 @@ impl H264Extractor { | ||
| 242 | 251 | }; |
| 243 | 252 | i += 1; |
| 244 | 253 | 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 { | |
| 248 | 255 | return; |
| 249 | 256 | }; |
| 250 | 257 | sets.push(n); |
| 251 | - i += 2 + l; | |
| 258 | + i = next; | |
| 252 | 259 | } |
| 253 | 260 | } |
| 254 | 261 | for n in sets { |
| @@ -408,13 +415,11 @@ impl HevcExtractor { | ||
| 408 | 415 | let count = u16::from_be_bytes([h[1], h[2]]) as usize; |
| 409 | 416 | i += 3; |
| 410 | 417 | 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 { | |
| 414 | 419 | return; |
| 415 | 420 | }; |
| 416 | 421 | self.nal(n, None); |
| 417 | - i += 2 + l; | |
| 422 | + i = next; | |
| 418 | 423 | } |
| 419 | 424 | } |
| 420 | 425 | } |