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 | fn unescape(p: &[u8]) -> Vec<u8> { | 162 | fn unescape(p: &[u8]) -> Vec<u8> { |
| 154 | let mut out = Vec::with_capacity(p.len()); | 163 | let mut out = Vec::with_capacity(p.len()); |
| 155 | let mut zeros = 0; | 164 | let mut zeros = 0; |
| @@ -242,13 +251,11 @@ impl H264Extractor { | |||
| 242 | }; | 251 | }; |
| 243 | i += 1; | 252 | i += 1; |
| 244 | for _ in 0..count { | 253 | for _ in 0..count { |
| 245 | - let Some(l) = avcc.get(i..i + 2) else { return }; | 254 | + let Some((n, next)) = u16_prefixed(avcc, i) else { |
| 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 { | ||
| 248 | return; | 255 | return; |
| 249 | }; | 256 | }; |
| 250 | sets.push(n); | 257 | sets.push(n); |
| 251 | - i += 2 + l; | 258 | + i = next; |
| 252 | } | 259 | } |
| 253 | } | 260 | } |
| 254 | for n in sets { | 261 | for n in sets { |
| @@ -408,13 +415,11 @@ impl HevcExtractor { | |||
| 408 | let count = u16::from_be_bytes([h[1], h[2]]) as usize; | 415 | let count = u16::from_be_bytes([h[1], h[2]]) as usize; |
| 409 | i += 3; | 416 | i += 3; |
| 410 | for _ in 0..count { | 417 | for _ in 0..count { |
| 411 | - let Some(l) = hvcc.get(i..i + 2) else { return }; | 418 | + let Some((n, next)) = u16_prefixed(hvcc, i) else { |
| 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 { | ||
| 414 | return; | 419 | return; |
| 415 | }; | 420 | }; |
| 416 | self.nal(n, None); | 421 | self.nal(n, None); |
| 417 | - i += 2 + l; | 422 | + i = next; |
| 418 | } | 423 | } |
| 419 | } | 424 | } |
| 420 | } | 425 | } |