diff --git a/crates/ec-hw/src/dec/hevc.rs b/crates/ec-hw/src/dec/hevc.rs index c23a2183..03c7e9a1 100644 --- a/crates/ec-hw/src/dec/hevc.rs +++ b/crates/ec-hw/src/dec/hevc.rs @@ -93,6 +93,11 @@ pub struct HevcDecoder { prev_tid0_poc: i32, /// Set once a picture has been decoded since the last reset. started: bool, + /// `NoRaslOutputFlag` (8.1.3) of the most recent IRAP: RASL pictures + /// associated with it must not be decoded or output when set, because + /// they predict from pictures before a random-access point this decoder + /// never saw. + no_rasl_output: bool, max_reorder: usize, max_dec_pic_buffering: usize, } @@ -111,6 +116,7 @@ impl HevcDecoder { current: None, prev_tid0_poc: 0, started: false, + no_rasl_output: false, max_reorder: 1, max_dec_pic_buffering: 6, } @@ -171,6 +177,7 @@ impl HevcDecoder { self.ready.clear(); self.prev_tid0_poc = 0; self.started = false; + self.no_rasl_output = false; } /// What the stream turned out to be. @@ -202,7 +209,14 @@ impl HevcDecoder { if sh.first_slice_segment_in_pic { self.finish_picture()?; - self.start_picture(&sps, &sh, nal_type, temporal_id, timestamp)?; + // 8.1.3 / C.5.2.2: a RASL picture associated with an IRAP whose + // NoRaslOutputFlag is 1 predicts from pictures before that IRAP, + // which a mid-stream start never decoded. It is not output and + // is not guaranteed decodable, so it is dropped whole rather than + // handed to the driver. + if !(is_rasl_picture(nal_type) && self.no_rasl_output) { + self.start_picture(&sps, &sh, nal_type, temporal_id, timestamp)?; + } } if self.current.is_none() { return Ok(()); @@ -256,6 +270,14 @@ impl HevcDecoder { ) -> Result<()> { self.ensure_session(sps)?; + if nal_type.is_irap() { + // 8.1.3: 1 for an IDR, a BLA, or the first picture of a coded + // video sequence — which a mid-stream start's first IRAP always + // is, `self.started` still being false at that point. + self.no_rasl_output = + nal_type.is_idr() || is_bla_picture(nal_type) || !self.started; + } + let poc = self.picture_order_count(sps, sh, nal_type); // 8.3.2 runs before the picture is decoded and after its POC is known: // every stored picture the set does not name stops being a reference. @@ -636,6 +658,18 @@ fn is_leading_picture(nal_type: NalUnitType) -> bool { (6..=9).contains(&nal_type.code()) } +/// RASL_N and RASL_R (8, 9): leading pictures dropped whole when their IRAP's +/// `NoRaslOutputFlag` is 1 (8.1.3). +fn is_rasl_picture(nal_type: NalUnitType) -> bool { + matches!(nal_type.code(), 8 | 9) +} + +/// BLA_W_LP, BLA_W_RADL and BLA_N_LP (16..=18): always `NoRaslOutputFlag` 1 +/// (8.1.3), a broken-link splice point that never has a decodable RASL. +fn is_bla_picture(nal_type: NalUnitType) -> bool { + (16..=18).contains(&nal_type.code()) +} + /// `slice_pic_parameter_set_id`, readable without any parameter set. fn peek_pps_id(rbsp: &[u8], nal_type: NalUnitType) -> Result { let mut r = ec_core::BitReader::new(rbsp); diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index 0b7c8fd1..6f0f5ea9 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -1032,3 +1032,93 @@ fn short(path: &Path) -> String { .map(|n| n.to_string_lossy().to_string()) .unwrap_or_default() } + +/// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in +/// decode order, even though they display before it (they predict from +/// pictures the CRA's own random-access point does not have). A decoder +/// started fresh at that CRA — what a seek does — must therefore drop those +/// RASL pictures per H.265 8.1.3's `NoRaslOutputFlag`, or its output shifts by +/// however many RASL pictures the GOP has, handing back the wrong picture for +/// every display index at or after the seek target. +#[test] +fn hevc_seek_matches_linear_mkv() { + let Some(display) = display() else { return }; + let path = bitstreams().join("test_hevc.mkv"); + let Some(data) = elementary(&path, Codec::H265, 45) else { + eprintln!("skipped: no test_hevc.mkv fixture"); + return; + }; + let units = split_access_units(&data, Codec::H265); + + // Linear decode: every picture from the start, frame 30 kept for + // comparison (the file's second CRA, per `test_hevc.mkv`'s 30-picture + // GOPs). + const TARGET: usize = 30; + let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); + let mut linear_frame_30 = None; + let mut index = 0usize; + decode_stream(&mut decoder, Codec::H265, &data, 45, |frame| { + if index == TARGET { + linear_frame_30 = Some(frame.to_i420().expect("readback")); + } + index += 1; + }) + .expect("linear decode"); + let linear_frame_30 = linear_frame_30.expect("linear decode reached frame 30"); + + // Find the access unit whose first VCL NAL is the CRA at this GOP + // boundary, and feed the decoder from there on, exactly as a seek would: + // nothing from before it goes in. + let cra_at = units + .iter() + .position(|au| starts_with_cra(au)) + .expect("a CRA access unit exists"); + + // A seek reuses the parameter sets a decoder already learned; a fresh one + // has none, so it is primed the same way `ec_hw::Decoder::reset` expects a + // caller to: decode the leading VPS/SPS/PPS (their IDR picture is thrown + // away) and reset picture state before feeding the seek target. + let mut seek_decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); + seek_decoder.decode(units[0], 0).expect("prime parameter sets"); + seek_decoder.reset(); + let mut seeked_frame = None; + for (i, unit) in units[cra_at..].iter().enumerate() { + seek_decoder.decode(unit, i as i64).expect("seek decode"); + if let Some(frame) = seek_decoder.next_frame() { + seeked_frame = Some(frame.to_i420().expect("readback")); + break; + } + } + if seeked_frame.is_none() { + seek_decoder.flush().expect("flush"); + seeked_frame = seek_decoder + .next_frame() + .map(|f| f.to_i420().expect("readback")); + } + let seeked_frame = seeked_frame.expect("a frame came out after the seek"); + + assert_eq!( + seeked_frame, linear_frame_30, + "seeking to the CRA before frame 30 handed back a different picture \ + than a linear decode of frame 30 — RASL pictures were not dropped" + ); +} + +/// True when an access unit's first VCL NAL is an HEVC CRA (`nal_type` 21). +/// A CRA's access unit may open with its own VPS/SPS/PPS, so this skips past +/// non-VCL NALs to the first one that carries a slice. +fn starts_with_cra(au: &[u8]) -> bool { + let mut i = 0usize; + while i + 3 < au.len() { + if au[i] == 0 && au[i + 1] == 0 && au[i + 2] == 1 { + let t = (au[i + 3] >> 1) & 0x3f; + if t <= 31 { + return t == 21; + } + i += 3; + continue; + } + i += 1; + } + false +}