Merge branch 'h264-seek-leading'
Browse files at this commit7 changed files +320 -16
- crates/ec-h264/src/decoder.rs +33 -3
- crates/ec-h264/src/dpb.rs +18 -7
- crates/ec-h264/tests/conformance.rs +131 -0
- crates/ec-hw/src/dec/dpb264.rs +13 -3
- crates/ec-hw/src/dec/h264.rs +22 -3
- crates/ec-hw/tests/gpu.rs +94 -0
- scripts/gen-fixtures.sh +9 -0
crates/ec-h264/src/decoder.rsmodified+33 -3
| @@ -134,6 +134,19 @@ pub struct Decoder { | |||
| 134 | out: VecDeque<VideoFrame>, | 134 | out: VecDeque<VideoFrame>, |
| 135 | /// Presentation timestamp to attach to the next picture started. | 135 | /// Presentation timestamp to attach to the next picture started. |
| 136 | pending_pts: Option<Timestamp>, | 136 | pending_pts: Option<Timestamp>, |
| 137 | + /// `PicOrderCnt` of the random-access point most recently established by | ||
| 138 | + /// [`Decoder::reset_pictures`] — the first picture decoded after it, | ||
| 139 | + /// i.e. the I picture a seek landed on. `None` before that picture | ||
| 140 | + /// completes. Absent a recovery_point SEI (not parsed here), D.2.8's | ||
| 141 | + /// recovery point defaults to this picture itself: anything with a | ||
| 142 | + /// smaller POC that had to guess a reference (see `cur_ref_padded`) | ||
| 143 | + /// predicts from something never decoded since the seek and must not | ||
| 144 | + /// reach the caller. | ||
| 145 | + recovery_poc: Option<i32>, | ||
| 146 | + /// Set while decoding the open picture if `build_ref_lists` had to pad a | ||
| 147 | + /// reference list, i.e. this picture asked for a reference the DPB does | ||
| 148 | + /// not have. Reset per picture in `start_picture`. | ||
| 149 | + cur_ref_padded: bool, | ||
| 137 | } | 150 | } |
| 138 | 151 | ||
| 139 | /// What [`Decoder::push_nal`] did with a NAL unit. | 152 | /// What [`Decoder::push_nal`] did with a NAL unit. |
| @@ -199,6 +212,8 @@ impl Decoder { | |||
| 199 | output_order: OutputOrder::default(), | 212 | output_order: OutputOrder::default(), |
| 200 | out: VecDeque::new(), | 213 | out: VecDeque::new(), |
| 201 | pending_pts: None, | 214 | pending_pts: None, |
| 215 | + recovery_poc: None, | ||
| 216 | + cur_ref_padded: false, | ||
| 202 | } | 217 | } |
| 203 | } | 218 | } |
| 204 | 219 | ||
| @@ -301,13 +316,18 @@ impl Decoder { | |||
| 301 | self.cur.extend_borders(); | 316 | self.cur.extend_borders(); |
| 302 | self.cur.complete = true; | 317 | self.cur.complete = true; |
| 303 | self.last_decoded = self.cur.id; | 318 | self.last_decoded = self.cur.id; |
| 319 | + // The first picture completed after a reset is the random-access | ||
| 320 | + // point the seek landed on; its own POC becomes the recovery | ||
| 321 | + // threshold (see `recovery_poc`'s doc comment). | ||
| 322 | + let recovery_poc = *self.recovery_poc.get_or_insert(self.cur.poc); | ||
| 323 | + let output_ok = !(self.cur_ref_padded && self.cur.poc < recovery_poc); | ||
| 304 | let sps_id = self.cur.sps_id as usize; | 324 | let sps_id = self.cur.sps_id as usize; |
| 305 | let sps = self.sps_map[sps_id] | 325 | let sps = self.sps_map[sps_id] |
| 306 | .take() | 326 | .take() |
| 307 | .ok_or_else(|| Error::corrupt("active SPS vanished"))?; | 327 | .ok_or_else(|| Error::corrupt("active SPS vanished"))?; |
| 308 | let pic = core::mem::take(&mut self.cur); | 328 | let pic = core::mem::take(&mut self.cur); |
| 309 | let marking = self.has_marking.then_some(&self.cur_marking); | 329 | let marking = self.has_marking.then_some(&self.cur_marking); |
| 310 | - let stored = self.dpb.store(pic, &sps, &self.cur_info, marking); | 330 | + let stored = self.dpb.store(pic, &sps, &self.cur_info, marking, output_ok); |
| 311 | self.sps_map[sps_id] = Some(sps); | 331 | self.sps_map[sps_id] = Some(sps); |
| 312 | stored?; | 332 | stored?; |
| 313 | self.has_picture = false; | 333 | self.has_picture = false; |
| @@ -357,6 +377,7 @@ impl Decoder { | |||
| 357 | self.out.clear(); | 377 | self.out.clear(); |
| 358 | self.last_decoded = -1; | 378 | self.last_decoded = -1; |
| 359 | self.pending_pts = None; | 379 | self.pending_pts = None; |
| 380 | + self.recovery_poc = None; | ||
| 360 | } | 381 | } |
| 361 | 382 | ||
| 362 | /// Move pictures out of the decoded picture buffer into the output queue. | 383 | /// Move pictures out of the decoded picture buffer into the output queue. |
| @@ -472,7 +493,7 @@ impl Decoder { | |||
| 472 | [RefList::default(), RefList::default()] | 493 | [RefList::default(), RefList::default()] |
| 473 | } else { | 494 | } else { |
| 474 | self.dpb.number_short_term(sh.frame_num, sps); | 495 | self.dpb.number_short_term(sh.frame_num, sps); |
| 475 | - self.dpb.build_ref_lists( | 496 | + let (lists, padded) = self.dpb.build_ref_lists( |
| 476 | sh.slice_type, | 497 | sh.slice_type, |
| 477 | self.cur.poc, | 498 | self.cur.poc, |
| 478 | sh.frame_num as i32, | 499 | sh.frame_num as i32, |
| @@ -480,7 +501,15 @@ impl Decoder { | |||
| 480 | sh.num_ref_idx_l0_active as usize, | 501 | sh.num_ref_idx_l0_active as usize, |
| 481 | sh.num_ref_idx_l1_active as usize, | 502 | sh.num_ref_idx_l1_active as usize, |
| 482 | (&sh.ref_pic_list_mod_l0, &sh.ref_pic_list_mod_l1), | 503 | (&sh.ref_pic_list_mod_l0, &sh.ref_pic_list_mod_l1), |
| 483 | - )? | 504 | + )?; |
| 505 | + // A ref list shorter than requested was padded by repeating an | ||
| 506 | + // available entry (8.2.4.2's unspecified tail) rather than the | ||
| 507 | + // reference this slice actually asked for. Right after a seek's | ||
| 508 | + // reset, that means the picture predicts from something never | ||
| 509 | + // decoded since the random-access start; recorded here so | ||
| 510 | + // `end_picture` can hold its output back (see `recovery_poc`). | ||
| 511 | + self.cur_ref_padded |= padded; | ||
| 512 | + lists | ||
| 484 | }; | 513 | }; |
| 485 | 514 | ||
| 486 | let mut r = if pps.entropy_coding_mode { | 515 | let mut r = if pps.entropy_coding_mode { |
| @@ -632,6 +661,7 @@ impl Decoder { | |||
| 632 | sh: &SliceHeader, | 661 | sh: &SliceHeader, |
| 633 | header: NalHeader, | 662 | header: NalHeader, |
| 634 | ) -> Result<()> { | 663 | ) -> Result<()> { |
| 664 | + self.cur_ref_padded = false; | ||
| 635 | let info = PicInfo { | 665 | let info = PicInfo { |
| 636 | is_idr: header.is_idr(), | 666 | is_idr: header.is_idr(), |
| 637 | is_reference: header.ref_idc != 0, | 667 | is_reference: header.ref_idc != 0, |
crates/ec-h264/src/dpb.rsmodified+18 -7
| @@ -644,7 +644,8 @@ impl Dpb { | |||
| 644 | num_l0: usize, | 644 | num_l0: usize, |
| 645 | num_l1: usize, | 645 | num_l1: usize, |
| 646 | mods: (&[RefPicListMod], &[RefPicListMod]), | 646 | mods: (&[RefPicListMod], &[RefPicListMod]), |
| 647 | - ) -> Result<[RefList; 2]> { | 647 | + ) -> Result<([RefList; 2], bool)> { |
| 648 | + let mut padded = false; | ||
| 648 | let mut lists = [RefList::default(), RefList::default()]; | 649 | let mut lists = [RefList::default(), RefList::default()]; |
| 649 | // Short-term candidates, then long-term, both by index into `frames`. | 650 | // Short-term candidates, then long-term, both by index into `frames`. |
| 650 | let mut short: [usize; MAX_STORED] = [0; MAX_STORED]; | 651 | let mut short: [usize; MAX_STORED] = [0; MAX_STORED]; |
| @@ -737,13 +738,16 @@ impl Dpb { | |||
| 737 | // conformant stream fills it by modification; repeating entry 0 | 738 | // conformant stream fills it by modification; repeating entry 0 |
| 738 | // keeps every index in range meanwhile. | 739 | // keeps every index in range meanwhile. |
| 739 | let first = lists[x].entries[0]; | 740 | let first = lists[x].entries[0]; |
| 741 | + if lists[x].len < wanted[x] { | ||
| 742 | + padded = true; | ||
| 743 | + } | ||
| 740 | while lists[x].len < wanted[x] { | 744 | while lists[x].len < wanted[x] { |
| 741 | lists[x].push(usize::from(first)); | 745 | lists[x].push(usize::from(first)); |
| 742 | } | 746 | } |
| 743 | lists[x].len = wanted[x]; | 747 | lists[x].len = wanted[x]; |
| 744 | self.modify_list(&mut lists[x], mods[x], curr_pic_num, max_pic_num); | 748 | self.modify_list(&mut lists[x], mods[x], curr_pic_num, max_pic_num); |
| 745 | } | 749 | } |
| 746 | - Ok(lists) | 750 | + Ok((lists, padded)) |
| 747 | } | 751 | } |
| 748 | 752 | ||
| 749 | /// Reference picture list modification (clause 8.2.4.3), following the | 753 | /// Reference picture list modification (clause 8.2.4.3), following the |
| @@ -826,15 +830,22 @@ impl Dpb { | |||
| 826 | } | 830 | } |
| 827 | 831 | ||
| 828 | /// Store a decoded picture and apply the reference marking of 8.2.5. | 832 | /// Store a decoded picture and apply the reference marking of 8.2.5. |
| 833 | + /// | ||
| 834 | + /// `output_ok` is false for a picture that predicted from a reference | ||
| 835 | + /// never decoded since the last `reset()` (a random-access leading | ||
| 836 | + /// picture whose ref list build had to pad) — it still joins the DPB, as | ||
| 837 | + /// a reference for later pictures and for `PicNum`/`FrameNumWrap` | ||
| 838 | + /// bookkeeping, but the bumping process must never hand it to a caller. | ||
| 829 | pub(crate) fn store( | 839 | pub(crate) fn store( |
| 830 | &mut self, | 840 | &mut self, |
| 831 | mut pic: Picture, | 841 | mut pic: Picture, |
| 832 | sps: &Sps, | 842 | sps: &Sps, |
| 833 | info: &PicInfo, | 843 | info: &PicInfo, |
| 834 | marking: Option<&DecRefPicMarking>, | 844 | marking: Option<&DecRefPicMarking>, |
| 845 | + output_ok: bool, | ||
| 835 | ) -> Result<()> { | 846 | ) -> Result<()> { |
| 836 | let has_mmco5 = marking.is_some_and(|m| m.mmcos.iter().any(|c| c.op == 5)); | 847 | let has_mmco5 = marking.is_some_and(|m| m.mmcos.iter().any(|c| c.op == 5)); |
| 837 | - pic.output = true; | 848 | + pic.output = output_ok; |
| 838 | pic.complete = true; | 849 | pic.complete = true; |
| 839 | // Clause 8.2.4.1 against this picture's frame_num, which is what both | 850 | // Clause 8.2.4.1 against this picture's frame_num, which is what both |
| 840 | // the sliding window (8.2.5.3) and the MMCO picture numbers (8.2.5.4) | 851 | // the sliding window (8.2.5.3) and the MMCO picture numbers (8.2.5.4) |
| @@ -1275,7 +1286,7 @@ mod tests { | |||
| 1275 | lt.long_term_frame_idx = 1; | 1286 | lt.long_term_frame_idx = 1; |
| 1276 | lt.pic_num = 1; | 1287 | lt.pic_num = 1; |
| 1277 | dpb.frames.push(lt); | 1288 | dpb.frames.push(lt); |
| 1278 | - let lists = dpb | 1289 | + let (lists, _) = dpb |
| 1279 | .build_ref_lists(SliceType::P, 12, 6, 256, 4, 0, (&[], &[])) | 1290 | .build_ref_lists(SliceType::P, 12, 6, 256, 4, 0, (&[], &[])) |
| 1280 | .unwrap(); | 1291 | .unwrap(); |
| 1281 | let nums: Vec<i32> = (0..lists[0].len()) | 1292 | let nums: Vec<i32> = (0..lists[0].len()) |
| @@ -1292,7 +1303,7 @@ mod tests { | |||
| 1292 | push_ref(&mut dpb, 1, 8, Mark::Short); | 1303 | push_ref(&mut dpb, 1, 8, Mark::Short); |
| 1293 | push_ref(&mut dpb, 2, 4, Mark::Short); | 1304 | push_ref(&mut dpb, 2, 4, Mark::Short); |
| 1294 | push_ref(&mut dpb, 3, 12, Mark::Short); | 1305 | push_ref(&mut dpb, 3, 12, Mark::Short); |
| 1295 | - let lists = dpb | 1306 | + let (lists, _) = dpb |
| 1296 | .build_ref_lists(SliceType::B, 6, 4, 256, 4, 4, (&[], &[])) | 1307 | .build_ref_lists(SliceType::B, 6, 4, 256, 4, 4, (&[], &[])) |
| 1297 | .unwrap(); | 1308 | .unwrap(); |
| 1298 | let poc = |l: &RefList| -> Vec<i32> { | 1309 | let poc = |l: &RefList| -> Vec<i32> { |
| @@ -1313,7 +1324,7 @@ mod tests { | |||
| 1313 | push_ref(&mut dpb, 1, 2, Mark::Short); | 1324 | push_ref(&mut dpb, 1, 2, Mark::Short); |
| 1314 | // Both references precede the current picture, so initialisation gives | 1325 | // Both references precede the current picture, so initialisation gives |
| 1315 | // list 0 and list 1 the same descending-POC order. | 1326 | // list 0 and list 1 the same descending-POC order. |
| 1316 | - let lists = dpb | 1327 | + let (lists, _) = dpb |
| 1317 | .build_ref_lists(SliceType::B, 10, 2, 256, 2, 2, (&[], &[])) | 1328 | .build_ref_lists(SliceType::B, 10, 2, 256, 2, 2, (&[], &[])) |
| 1318 | .unwrap(); | 1329 | .unwrap(); |
| 1319 | let l0: Vec<i32> = (0..2) | 1330 | let l0: Vec<i32> = (0..2) |
| @@ -1338,7 +1349,7 @@ mod tests { | |||
| 1338 | abs_diff_pic_num_minus1: 2, | 1349 | abs_diff_pic_num_minus1: 2, |
| 1339 | add: false, | 1350 | add: false, |
| 1340 | }]; | 1351 | }]; |
| 1341 | - let lists = dpb | 1352 | + let (lists, _) = dpb |
| 1342 | .build_ref_lists(SliceType::P, 12, 6, 256, 3, 0, (&mods, &[])) | 1353 | .build_ref_lists(SliceType::P, 12, 6, 256, 3, 0, (&mods, &[])) |
| 1343 | .unwrap(); | 1354 | .unwrap(); |
| 1344 | let nums: Vec<i32> = (0..3) | 1355 | let nums: Vec<i32> = (0..3) |
crates/ec-h264/tests/conformance.rsmodified+131 -0
| @@ -2245,3 +2245,134 @@ fn high_profile_8x8_inter_sequences_match_ffmpeg() { | |||
| 2245 | let _ = std::fs::remove_dir_all(&dir); | 2245 | let _ = std::fs::remove_dir_all(&dir); |
| 2246 | assert!(failures.is_empty(), "{failures:#?}"); | 2246 | assert!(failures.is_empty(), "{failures:#?}"); |
| 2247 | } | 2247 | } |
| 2248 | + | ||
| 2249 | +/// An open-GOP boundary's non-IDR I picture (`nal_unit_type` 1, not 5) is | ||
| 2250 | +/// followed in decode order by leading B pictures that display *before* it | ||
| 2251 | +/// but predict from the previous GOP. A decoder started fresh at that I | ||
| 2252 | +/// picture — what a seek does — never decoded those previous-GOP references, | ||
| 2253 | +/// so it must not hand back the leading B pictures at all (H.264's analogue | ||
| 2254 | +/// of HEVC's RASL/`NoRaslOutputFlag` case): output must resume at the I | ||
| 2255 | +/// picture itself, per D.2.8 absent a recovery_point SEI. | ||
| 2256 | +#[test] | ||
| 2257 | +fn h264_seek_matches_linear_open_gop() { | ||
| 2258 | + if !have_ffmpeg() { | ||
| 2259 | + eprintln!("SKIP: ffmpeg not on PATH"); | ||
| 2260 | + return; | ||
| 2261 | + } | ||
| 2262 | + let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/video/h264-open-gop.mp4"); | ||
| 2263 | + if !src.is_file() { | ||
| 2264 | + eprintln!("SKIP: no fixtures/video/h264-open-gop.mp4 (run scripts/gen-fixtures.sh)"); | ||
| 2265 | + return; | ||
| 2266 | + } | ||
| 2267 | + let dir = scratch("open-gop"); | ||
| 2268 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 2269 | + let annexb = dir.join("open-gop.264"); | ||
| 2270 | + assert!(extract_annexb(&src, &annexb, 120), "ffmpeg extraction"); | ||
| 2271 | + let data = std::fs::read(&annexb).unwrap(); | ||
| 2272 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 2273 | + | ||
| 2274 | + // Split into access units the way a demuxer would: a new one starts at | ||
| 2275 | + // each slice NAL (type 1 or 5). | ||
| 2276 | + let units: Vec<Vec<u8>> = ec_h264_syntax::AnnexBIter::new(&data) | ||
| 2277 | + .map(<[u8]>::to_vec) | ||
| 2278 | + .collect(); | ||
| 2279 | + let mut access_units: Vec<Vec<u8>> = Vec::new(); | ||
| 2280 | + let mut current: Vec<u8> = Vec::new(); | ||
| 2281 | + let mut current_has_slice = false; | ||
| 2282 | + for unit in &units { | ||
| 2283 | + let is_slice = matches!(unit[0] & 0x1F, 1 | 5); | ||
| 2284 | + if is_slice && current_has_slice { | ||
| 2285 | + access_units.push(std::mem::take(&mut current)); | ||
| 2286 | + current_has_slice = false; | ||
| 2287 | + } | ||
| 2288 | + current_has_slice |= is_slice; | ||
| 2289 | + current.extend_from_slice(&[0, 0, 0, 1]); | ||
| 2290 | + current.extend_from_slice(unit); | ||
| 2291 | + } | ||
| 2292 | + if current_has_slice { | ||
| 2293 | + access_units.push(current); | ||
| 2294 | + } | ||
| 2295 | + | ||
| 2296 | + // Linear decode: every picture from the start, display frame 30 kept for | ||
| 2297 | + // comparison (the fixture's second I picture, `keyint=30`). | ||
| 2298 | + const TARGET: usize = 30; | ||
| 2299 | + let mut dec = Decoder::new(); | ||
| 2300 | + let mut linear_frame_30 = None; | ||
| 2301 | + let mut index = 0usize; | ||
| 2302 | + for nal in ec_h264_syntax::AnnexBIter::new(&data) { | ||
| 2303 | + if dec.push_nal(nal).expect("push_nal") == NalOutcome::PictureBoundary { | ||
| 2304 | + dec.end_picture().expect("end_picture"); | ||
| 2305 | + dec.push_nal(nal).expect("re-push"); | ||
| 2306 | + } | ||
| 2307 | + while let Some(f) = dec.next_frame() { | ||
| 2308 | + if index == TARGET { | ||
| 2309 | + linear_frame_30 = Some(frame_bytes(&f)); | ||
| 2310 | + } | ||
| 2311 | + index += 1; | ||
| 2312 | + } | ||
| 2313 | + } | ||
| 2314 | + dec.flush().expect("flush"); | ||
| 2315 | + while let Some(f) = dec.next_frame() { | ||
| 2316 | + if index == TARGET { | ||
| 2317 | + linear_frame_30 = Some(frame_bytes(&f)); | ||
| 2318 | + } | ||
| 2319 | + index += 1; | ||
| 2320 | + } | ||
| 2321 | + let linear_frame_30 = linear_frame_30.expect("linear decode reached frame 30"); | ||
| 2322 | + | ||
| 2323 | + // Find the access unit whose first VCL NAL is a non-IDR I slice | ||
| 2324 | + // (`nal_unit_type` 1, `slice_type % 5 == 2`) — the open-GOP boundary. | ||
| 2325 | + let is_open_gop_i = |au: &[u8]| -> bool { | ||
| 2326 | + let Some((&header, payload)) = au.get(4).zip(au.get(5..)) else { | ||
| 2327 | + return false; | ||
| 2328 | + }; | ||
| 2329 | + if header & 0x1F != 1 { | ||
| 2330 | + return false; | ||
| 2331 | + } | ||
| 2332 | + let mut r = ec_core::BitReader::new(payload); | ||
| 2333 | + if r.read_ue().is_err() { | ||
| 2334 | + return false; | ||
| 2335 | + } | ||
| 2336 | + matches!(r.read_ue(), Ok(code) if code % 5 == 2) | ||
| 2337 | + }; | ||
| 2338 | + let cra_at = access_units | ||
| 2339 | + .iter() | ||
| 2340 | + .position(|au| is_open_gop_i(au)) | ||
| 2341 | + .expect("an open-GOP I access unit exists"); | ||
| 2342 | + | ||
| 2343 | + // A seek reuses the parameter sets a decoder already learned; a fresh one | ||
| 2344 | + // is primed the way `Decoder::reset_pictures`'s doc comment expects: the | ||
| 2345 | + // leading IDR's parameter sets are decoded and its picture discarded. | ||
| 2346 | + let mut seek_dec = Decoder::new(); | ||
| 2347 | + for nal in ec_h264_syntax::AnnexBIter::new(&access_units[0]) { | ||
| 2348 | + seek_dec.push_nal(nal).expect("prime parameter sets"); | ||
| 2349 | + } | ||
| 2350 | + seek_dec.end_picture().expect("end priming IDR"); | ||
| 2351 | + seek_dec.reset_pictures(); | ||
| 2352 | + | ||
| 2353 | + let mut seeked_frame = None; | ||
| 2354 | + 'outer: for au in &access_units[cra_at..] { | ||
| 2355 | + for nal in ec_h264_syntax::AnnexBIter::new(au) { | ||
| 2356 | + if seek_dec.push_nal(nal).expect("seek decode") == NalOutcome::PictureBoundary { | ||
| 2357 | + seek_dec.end_picture().expect("end_picture"); | ||
| 2358 | + seek_dec.push_nal(nal).expect("re-push"); | ||
| 2359 | + } | ||
| 2360 | + } | ||
| 2361 | + if let Some(f) = seek_dec.next_frame() { | ||
| 2362 | + seeked_frame = Some(frame_bytes(&f)); | ||
| 2363 | + break 'outer; | ||
| 2364 | + } | ||
| 2365 | + } | ||
| 2366 | + if seeked_frame.is_none() { | ||
| 2367 | + seek_dec.flush().expect("flush"); | ||
| 2368 | + seeked_frame = seek_dec.next_frame().map(|f| frame_bytes(&f)); | ||
| 2369 | + } | ||
| 2370 | + let seeked_frame = seeked_frame.expect("a frame came out after the seek"); | ||
| 2371 | + | ||
| 2372 | + assert_eq!( | ||
| 2373 | + seeked_frame, linear_frame_30, | ||
| 2374 | + "seeking to the open-GOP I before frame 30 handed back a different \ | ||
| 2375 | + picture than a linear decode of frame 30 — leading pictures with \ | ||
| 2376 | + missing references were not dropped" | ||
| 2377 | + ); | ||
| 2378 | +} | ||
crates/ec-hw/src/dec/dpb264.rsmodified+13 -3
| @@ -420,7 +420,8 @@ impl Dpb { | |||
| 420 | num_l0: usize, | 420 | num_l0: usize, |
| 421 | num_l1: usize, | 421 | num_l1: usize, |
| 422 | mods: (&[RefPicListMod], &[RefPicListMod]), | 422 | mods: (&[RefPicListMod], &[RefPicListMod]), |
| 423 | - ) -> Result<[RefList; 2]> { | 423 | + ) -> Result<([RefList; 2], bool)> { |
| 424 | + let mut padded = false; | ||
| 424 | let mut lists = [RefList::default(), RefList::default()]; | 425 | let mut lists = [RefList::default(), RefList::default()]; |
| 425 | let mut short: [usize; MAX_STORED] = [0; MAX_STORED]; | 426 | let mut short: [usize; MAX_STORED] = [0; MAX_STORED]; |
| 426 | let mut n_short = 0usize; | 427 | let mut n_short = 0usize; |
| @@ -505,13 +506,16 @@ impl Dpb { | |||
| 505 | ))); | 506 | ))); |
| 506 | } | 507 | } |
| 507 | let first = lists[x].entries[0]; | 508 | let first = lists[x].entries[0]; |
| 509 | + if lists[x].len < wanted[x] { | ||
| 510 | + padded = true; | ||
| 511 | + } | ||
| 508 | while lists[x].len < wanted[x] { | 512 | while lists[x].len < wanted[x] { |
| 509 | lists[x].push(usize::from(first)); | 513 | lists[x].push(usize::from(first)); |
| 510 | } | 514 | } |
| 511 | lists[x].len = wanted[x]; | 515 | lists[x].len = wanted[x]; |
| 512 | self.modify_list(&mut lists[x], mods[x], curr_pic_num, max_pic_num); | 516 | self.modify_list(&mut lists[x], mods[x], curr_pic_num, max_pic_num); |
| 513 | } | 517 | } |
| 514 | - Ok(lists) | 518 | + Ok((lists, padded)) |
| 515 | } | 519 | } |
| 516 | 520 | ||
| 517 | /// Reference picture list modification (8.2.4.3), by the insert-and-compact | 521 | /// Reference picture list modification (8.2.4.3), by the insert-and-compact |
| @@ -589,15 +593,21 @@ impl Dpb { | |||
| 589 | } | 593 | } |
| 590 | 594 | ||
| 591 | /// Store a decoded picture and apply the reference marking of 8.2.5. | 595 | /// Store a decoded picture and apply the reference marking of 8.2.5. |
| 596 | + /// | ||
| 597 | + /// `output_ok` is false for a picture that predicted from a reference | ||
| 598 | + /// never decoded since the last `reset()` (a random-access leading | ||
| 599 | + /// picture whose ref list build had to pad) — it still joins the DPB as | ||
| 600 | + /// a reference and for bookkeeping, but must never be handed to a caller. | ||
| 592 | pub(crate) fn store( | 601 | pub(crate) fn store( |
| 593 | &mut self, | 602 | &mut self, |
| 594 | mut pic: Picture, | 603 | mut pic: Picture, |
| 595 | sps: &Sps, | 604 | sps: &Sps, |
| 596 | info: &PicInfo, | 605 | info: &PicInfo, |
| 597 | marking: Option<&DecRefPicMarking>, | 606 | marking: Option<&DecRefPicMarking>, |
| 607 | + output_ok: bool, | ||
| 598 | ) -> Result<()> { | 608 | ) -> Result<()> { |
| 599 | let has_mmco5 = marking.is_some_and(|m| m.mmcos.iter().any(|c| c.op == 5)); | 609 | let has_mmco5 = marking.is_some_and(|m| m.mmcos.iter().any(|c| c.op == 5)); |
| 600 | - pic.output = true; | 610 | + pic.output = output_ok; |
| 601 | self.number_short_term(info.frame_num, sps); | 611 | self.number_short_term(info.frame_num, sps); |
| 602 | pic.frame_num_wrap = pic.frame_num as i32; | 612 | pic.frame_num_wrap = pic.frame_num as i32; |
| 603 | pic.pic_num = pic.frame_num as i32; | 613 | pic.pic_num = pic.frame_num as i32; |
crates/ec-hw/src/dec/h264.rsmodified+22 -3
| @@ -38,6 +38,8 @@ struct Current { | |||
| 38 | /// Parameter and data buffers submitted so far. | 38 | /// Parameter and data buffers submitted so far. |
| 39 | buffers: Vec<Buffer>, | 39 | buffers: Vec<Buffer>, |
| 40 | slices: usize, | 40 | slices: usize, |
| 41 | + /// Set if any slice's ref list build had to pad (see `H264Decoder::recovery_poc`). | ||
| 42 | + ref_padded: bool, | ||
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | /// A stateless H.264 decoder. | 45 | /// A stateless H.264 decoder. |
| @@ -52,6 +54,14 @@ pub struct H264Decoder { | |||
| 52 | /// Reusable unescape buffer; NAL payloads are rewritten into it in turn. | 54 | /// Reusable unescape buffer; NAL payloads are rewritten into it in turn. |
| 53 | rbsp: Vec<u8>, | 55 | rbsp: Vec<u8>, |
| 54 | gap_frames: u64, | 56 | gap_frames: u64, |
| 57 | + /// `PicOrderCnt` of the random-access point most recently established by | ||
| 58 | + /// `reset()` — the first picture decoded after it. `None` before that | ||
| 59 | + /// picture completes. Absent a recovery_point SEI (not parsed here), | ||
| 60 | + /// D.2.8's recovery point defaults to this picture itself: anything with | ||
| 61 | + /// a smaller POC that had to guess a reference (`Current::ref_padded`) | ||
| 62 | + /// predicts from something never decoded since the seek and must not | ||
| 63 | + /// reach the caller. | ||
| 64 | + recovery_poc: Option<i32>, | ||
| 55 | } | 65 | } |
| 56 | 66 | ||
| 57 | impl H264Decoder { | 67 | impl H264Decoder { |
| @@ -68,6 +78,7 @@ impl H264Decoder { | |||
| 68 | current: None, | 78 | current: None, |
| 69 | rbsp: Vec::new(), | 79 | rbsp: Vec::new(), |
| 70 | gap_frames: 0, | 80 | gap_frames: 0, |
| 81 | + recovery_poc: None, | ||
| 71 | } | 82 | } |
| 72 | } | 83 | } |
| 73 | 84 | ||
| @@ -132,6 +143,7 @@ impl H264Decoder { | |||
| 132 | self.dpb.clear(); | 143 | self.dpb.clear(); |
| 133 | self.ready.clear(); | 144 | self.ready.clear(); |
| 134 | self.gap_frames = 0; | 145 | self.gap_frames = 0; |
| 146 | + self.recovery_poc = None; | ||
| 135 | } | 147 | } |
| 136 | 148 | ||
| 137 | /// What the stream turned out to be, once its first SPS was seen. | 149 | /// What the stream turned out to be, once its first SPS was seen. |
| @@ -268,6 +280,7 @@ impl H264Decoder { | |||
| 268 | marking: sh.dec_ref_pic_marking.clone(), | 280 | marking: sh.dec_ref_pic_marking.clone(), |
| 269 | buffers: Vec::new(), | 281 | buffers: Vec::new(), |
| 270 | slices: 0, | 282 | slices: 0, |
| 283 | + ref_padded: false, | ||
| 271 | }); | 284 | }); |
| 272 | Ok(()) | 285 | Ok(()) |
| 273 | } | 286 | } |
| @@ -317,7 +330,7 @@ impl H264Decoder { | |||
| 317 | [RefList::default(), RefList::default()] | 330 | [RefList::default(), RefList::default()] |
| 318 | } else { | 331 | } else { |
| 319 | self.dpb.number_short_term(sh.frame_num, sps); | 332 | self.dpb.number_short_term(sh.frame_num, sps); |
| 320 | - self.dpb.build_ref_lists( | 333 | + let (lists, padded) = self.dpb.build_ref_lists( |
| 321 | sh.slice_type, | 334 | sh.slice_type, |
| 322 | current.poc.value, | 335 | current.poc.value, |
| 323 | sh.frame_num as i32, | 336 | sh.frame_num as i32, |
| @@ -325,7 +338,9 @@ impl H264Decoder { | |||
| 325 | sh.num_ref_idx_l0_active as usize, | 338 | sh.num_ref_idx_l0_active as usize, |
| 326 | sh.num_ref_idx_l1_active as usize, | 339 | sh.num_ref_idx_l1_active as usize, |
| 327 | (&sh.ref_pic_list_mod_l0, &sh.ref_pic_list_mod_l1), | 340 | (&sh.ref_pic_list_mod_l0, &sh.ref_pic_list_mod_l1), |
| 328 | - )? | 341 | + )?; |
| 342 | + current.ref_padded |= padded; | ||
| 343 | + lists | ||
| 329 | }; | 344 | }; |
| 330 | 345 | ||
| 331 | let slice_param = slice_parameters(sh, pps, nal, &lists, &self.dpb); | 346 | let slice_param = slice_parameters(sh, pps, nal, &lists, &self.dpb); |
| @@ -359,6 +374,10 @@ impl H264Decoder { | |||
| 359 | 374 | ||
| 360 | session.submit(¤t.surface, current.buffers)?; | 375 | session.submit(¤t.surface, current.buffers)?; |
| 361 | 376 | ||
| 377 | + // See `recovery_poc`'s doc comment. | ||
| 378 | + let recovery_poc = *self.recovery_poc.get_or_insert(current.poc.value); | ||
| 379 | + let output_ok = !(current.ref_padded && current.poc.value < recovery_poc); | ||
| 380 | + | ||
| 362 | let picture = Picture { | 381 | let picture = Picture { |
| 363 | id: current.id, | 382 | id: current.id, |
| 364 | surface: Arc::clone(¤t.surface), | 383 | surface: Arc::clone(¤t.surface), |
| @@ -376,7 +395,7 @@ impl H264Decoder { | |||
| 376 | output: true, | 395 | output: true, |
| 377 | }; | 396 | }; |
| 378 | self.dpb | 397 | self.dpb |
| 379 | - .store(picture, &sps, ¤t.info, current.marking.as_ref())?; | 398 | + .store(picture, &sps, ¤t.info, current.marking.as_ref(), output_ok)?; |
| 380 | self.bump(false); | 399 | self.bump(false); |
| 381 | Ok(()) | 400 | Ok(()) |
| 382 | } | 401 | } |
crates/ec-hw/tests/gpu.rsmodified+94 -0
| @@ -1469,6 +1469,100 @@ fn hevc_seek_matches_linear_mkv() { | |||
| 1469 | ); | 1469 | ); |
| 1470 | } | 1470 | } |
| 1471 | 1471 | ||
| 1472 | +/// An H.264 open-GOP I picture is not an IDR: it is a plain I slice | ||
| 1473 | +/// (`nal_unit_type` 1) whose predecessors in decode order can still hold | ||
| 1474 | +/// references from before it, so leading pictures between it and the next | ||
| 1475 | +/// IDR may predict from a picture never decoded since a fresh start there — | ||
| 1476 | +/// exactly what a seek does. A decoder started at that I must drop those | ||
| 1477 | +/// leading pictures per D.2.8's recovery point default, or its output shifts | ||
| 1478 | +/// by however many leading pictures the GOP has. | ||
| 1479 | +#[test] | ||
| 1480 | +fn h264_seek_matches_linear_open_gop() { | ||
| 1481 | + let Some(display) = display() else { return }; | ||
| 1482 | + let path = bitstreams().join("h264-open-gop.mp4"); | ||
| 1483 | + let Some(data) = elementary(&path, Codec::H264, 60) else { | ||
| 1484 | + eprintln!("skipped: no h264-open-gop.mp4 fixture (run scripts/gen-fixtures.sh)"); | ||
| 1485 | + return; | ||
| 1486 | + }; | ||
| 1487 | + let units = split_access_units(&data, Codec::H264); | ||
| 1488 | + | ||
| 1489 | + // Linear decode: every picture from the start, frame 30 kept for | ||
| 1490 | + // comparison (the fixture's second I picture, `keyint=30`). | ||
| 1491 | + const TARGET: usize = 30; | ||
| 1492 | + let mut decoder = Decoder::new(&display, Codec::H264).expect("decoder opens"); | ||
| 1493 | + let mut linear_frame_30 = None; | ||
| 1494 | + let mut index = 0usize; | ||
| 1495 | + decode_stream(&mut decoder, Codec::H264, &data, 60, |frame| { | ||
| 1496 | + if index == TARGET { | ||
| 1497 | + linear_frame_30 = Some(frame.to_i420().expect("readback")); | ||
| 1498 | + } | ||
| 1499 | + index += 1; | ||
| 1500 | + }) | ||
| 1501 | + .expect("linear decode"); | ||
| 1502 | + let linear_frame_30 = linear_frame_30.expect("linear decode reached frame 30"); | ||
| 1503 | + | ||
| 1504 | + // Find the access unit whose first VCL NAL is the open-GOP I picture at | ||
| 1505 | + // this GOP boundary, and feed the decoder from there on, exactly as a | ||
| 1506 | + // seek would: nothing from before it goes in. | ||
| 1507 | + let open_gop_at = units | ||
| 1508 | + .iter() | ||
| 1509 | + .position(|au| is_open_gop_i(au)) | ||
| 1510 | + .expect("an open-GOP I access unit exists"); | ||
| 1511 | + | ||
| 1512 | + // A seek reuses the parameter sets a decoder already learned; a fresh one | ||
| 1513 | + // has none, so it is primed the same way `ec_hw::Decoder::reset` expects a | ||
| 1514 | + // caller to: decode the leading IDR (its picture is thrown away) and | ||
| 1515 | + // reset picture state before feeding the seek target. | ||
| 1516 | + let mut seek_decoder = Decoder::new(&display, Codec::H264).expect("decoder opens"); | ||
| 1517 | + seek_decoder.decode(units[0], 0).expect("prime parameter sets"); | ||
| 1518 | + seek_decoder.reset(); | ||
| 1519 | + let mut seeked_frame = None; | ||
| 1520 | + for (i, unit) in units[open_gop_at..].iter().enumerate() { | ||
| 1521 | + seek_decoder.decode(unit, i as i64).expect("seek decode"); | ||
| 1522 | + if let Some(frame) = seek_decoder.next_frame() { | ||
| 1523 | + seeked_frame = Some(frame.to_i420().expect("readback")); | ||
| 1524 | + break; | ||
| 1525 | + } | ||
| 1526 | + } | ||
| 1527 | + if seeked_frame.is_none() { | ||
| 1528 | + seek_decoder.flush().expect("flush"); | ||
| 1529 | + seeked_frame = seek_decoder | ||
| 1530 | + .next_frame() | ||
| 1531 | + .map(|f| f.to_i420().expect("readback")); | ||
| 1532 | + } | ||
| 1533 | + let seeked_frame = seeked_frame.expect("a frame came out after the seek"); | ||
| 1534 | + | ||
| 1535 | + assert_eq!( | ||
| 1536 | + seeked_frame, linear_frame_30, | ||
| 1537 | + "seeking to the open-GOP I before frame 30 handed back a different \ | ||
| 1538 | + picture than a linear decode of frame 30 — leading pictures with \ | ||
| 1539 | + missing references were not dropped" | ||
| 1540 | + ); | ||
| 1541 | +} | ||
| 1542 | + | ||
| 1543 | +/// True when an access unit's first VCL NAL is an H.264 open-GOP I slice | ||
| 1544 | +/// (`nal_unit_type` 1, `slice_type % 5 == 2`), found by scanning for the | ||
| 1545 | +/// access unit's first start code rather than assuming a fixed prefix length. | ||
| 1546 | +fn is_open_gop_i(au: &[u8]) -> bool { | ||
| 1547 | + let mut i = 0usize; | ||
| 1548 | + while i + 3 < au.len() { | ||
| 1549 | + if au[i] != 0 || au[i + 1] != 0 || au[i + 2] != 1 { | ||
| 1550 | + i += 1; | ||
| 1551 | + continue; | ||
| 1552 | + } | ||
| 1553 | + let header = au[i + 3]; | ||
| 1554 | + if header & 0x1f != 1 { | ||
| 1555 | + return false; | ||
| 1556 | + } | ||
| 1557 | + let mut r = ec_core::BitReader::new(&au[i + 4..]); | ||
| 1558 | + if r.read_ue().is_err() { | ||
| 1559 | + return false; | ||
| 1560 | + } | ||
| 1561 | + return matches!(r.read_ue(), Ok(code) if code % 5 == 2); | ||
| 1562 | + } | ||
| 1563 | + false | ||
| 1564 | +} | ||
| 1565 | + | ||
| 1472 | /// True when an access unit's first VCL NAL is an HEVC CRA (`nal_type` 21). | 1566 | /// True when an access unit's first VCL NAL is an HEVC CRA (`nal_type` 21). |
| 1473 | /// A CRA's access unit may open with its own VPS/SPS/PPS, so this skips past | 1567 | /// A CRA's access unit may open with its own VPS/SPS/PPS, so this skips past |
| 1474 | /// non-VCL NALs to the first one that carries a slice. | 1568 | /// non-VCL NALs to the first one that carries a slice. |
scripts/gen-fixtures.shmodified+9 -0
| @@ -198,6 +198,15 @@ for spec in "${VIDEO_CODECS[@]}"; do | |||
| 198 | esac | 198 | esac |
| 199 | done | 199 | done |
| 200 | 200 | ||
| 201 | +# Open-GOP H.264: a non-IDR I picture (nal_unit_type 1, not 5) at a GOP | ||
| 202 | +# boundary, with B-pyramid pictures around it referencing across the | ||
| 203 | +# boundary. A decoder started fresh at that I (what a seek does) must not | ||
| 204 | +# hand back a picture whose references it never decoded — the H.264 sibling | ||
| 205 | +# of the HEVC RASL/NoRaslOutputFlag case. | ||
| 206 | +gen "$FIXTURES/video/h264-open-gop.mp4" \ | ||
| 207 | + -f lavfi -i "testsrc2=size=320x240:rate=30:duration=4" -pix_fmt yuv420p \ | ||
| 208 | + -c:v libx264 -x264-params "keyint=30:min-keyint=30:open-gop=1:bframes=3:b-pyramid=normal:scenecut=0" | ||
| 209 | + | ||
| 201 | # -------------------------------------------------------------- summary ----- | 210 | # -------------------------------------------------------------- summary ----- |
| 202 | printf '\n%s\n' "== fixtures: $FIXTURES ==" | 211 | printf '\n%s\n' "== fixtures: $FIXTURES ==" |
| 203 | printf '%-42s %10s %s\n' FILE SIZE PROBE | 212 | printf '%-42s %10s %s\n' FILE SIZE PROBE |