B2: ec-h264-syntax AnnexBIter::next loops instead of recursing on empty NAL spans
Browse files at this commitc63953f614afcf7da156ae11988e9d62e6a9a07aParent: 22f0f3f2 changed files +24 -20
crates/ec-h264-syntax/src/nal.rsmodified+18 -20
| @@ -148,27 +148,25 @@ impl<'a> Iterator for AnnexBIter<'a> { | ||
| 148 | 148 | type Item = &'a [u8]; |
| 149 | 149 | |
| 150 | 150 | fn next(&mut self) -> Option<&'a [u8]> { |
| 151 | - let start = self.next_start_code(self.pos)? + 3; | |
| 152 | - if start >= self.data.len() { | |
| 153 | - self.pos = self.data.len(); | |
| 154 | - return None; | |
| 155 | - } | |
| 156 | - let (end, next_pos) = match self.next_start_code(start) { | |
| 157 | - Some(sc) => (sc, sc), | |
| 158 | - None => (self.data.len(), self.data.len()), | |
| 159 | - }; | |
| 160 | - self.pos = next_pos; | |
| 161 | - // Trailing zero bytes before the next start code belong to the start | |
| 162 | - // code prefix / trailing_zero_8bits, not to this NAL unit. | |
| 163 | - let mut end = end; | |
| 164 | - while end > start && self.data[end - 1] == 0 { | |
| 165 | - end -= 1; | |
| 166 | - } | |
| 167 | - if end == start { | |
| 168 | - // Empty NAL unit (all-zero span): skip to the next one. | |
| 169 | - return self.next(); | |
| 151 | + loop { | |
| 152 | + let start = self.next_start_code(self.pos)? + 3; | |
| 153 | + if start >= self.data.len() { | |
| 154 | + self.pos = self.data.len(); | |
| 155 | + return None; | |
| 156 | + } | |
| 157 | + let (end, next_pos) = match self.next_start_code(start) { | |
| 158 | + Some(sc) => (sc, sc), | |
| 159 | + None => (self.data.len(), self.data.len()), | |
| 160 | + }; | |
| 161 | + self.pos = next_pos; | |
| 162 | + let mut end = end; | |
| 163 | + while end > start && self.data[end - 1] == 0 { | |
| 164 | + end -= 1; | |
| 165 | + } | |
| 166 | + if end > start { | |
| 167 | + return Some(&self.data[start..end]); | |
| 168 | + } | |
| 170 | 169 | } |
| 171 | - Some(&self.data[start..end]) | |
| 172 | 170 | } |
| 173 | 171 | } |
| 174 | 172 |
crates/ec-h264-syntax/tests/fuzz_regressions.rsmodified+6 -0
| @@ -22,3 +22,9 @@ fn pps_scaling_delta_out_of_range_is_corrupt_not_a_panic() { | ||
| 22 | 22 | let err = Pps::parse(&rbsp, |_| None).expect_err("an out-of-range delta_scale is corrupt"); |
| 23 | 23 | assert!(err.to_string().contains("delta_scale"), "wrong refusal: {err}"); |
| 24 | 24 | } |
| 25 | + | |
| 26 | +#[test] | |
| 27 | +fn annexb_repeated_empty_start_codes_do_not_recurse() { | |
| 28 | + let data = [0u8, 0, 1].repeat(1 << 18); | |
| 29 | + assert_eq!(AnnexBIter::new(&data).count(), 0); | |
| 30 | +} |