forge
Explore
Log in

Don't you dare go Hollow

Drop RASL pictures after a seek's random-access CRA

Browse files at this commit

e1b58c9cd0283309b7accafdc4c6d66ba347ceceAhmet Kaan GümüşParent: 6f27127
  • fix
    • drop RASL pictures after a seek's random-access CRA
Show full message
fix(ec-hw-hevc): drop RASL pictures after a seek's random-access CRA

Root cause (crates/ec-hw/src/dec/hevc.rs): a CRA that opens a mid-stream
random-access point stores its RASL leading pictures *after* itself in
decode order even though they display *before* it (H.265 open-GOP coding).
Those RASL pictures predict from pictures before the CRA, which a decoder
started at that CRA — exactly what a seek does — never decoded. The old
code decoded and output them anyway: bump() orders output purely by POC,
so the (garbage) RASL pictures came out ahead of the CRA itself, shifting
every subsequent output index by however many RASL pictures the GOP has.

Per 8.1.3, `NoRaslOutputFlag` is 1 for the first picture of a coded video
sequence — which the first IRAP after a `reset()` (mid-stream start)
always is. RASL pictures (nal_type 8/9) associated with such an IRAP must
not be decoded or output (8.1.3 / C.5.2.2). `HevcDecoder` now tracks that
flag (`no_rasl_output`, set in `start_picture` on every IRAP) and skips
`start_picture` entirely for a RASL access unit while it holds, so the
picture is dropped whole rather than handed to the driver or bumped out.

New test `hevc_seek_matches_linear_mkv` (crates/ec-hw/tests/gpu.rs) drives
this through the real entry surface: it primes a fresh `Decoder` with the
stream's parameter sets, `reset()`s it (a seek's exact contract per the
existing reset() doc comment), feeds it from the CRA access unit
preceding display frame 30 of test_hevc.mkv, and asserts the first
decoded picture matches a linear decode of frame 30. Confirmed failing
before this fix (mismatched I420 planes) and passing after.

cargo test -p ec-hw --test gpu -- --test-threads=1: 18 passed (17
pre-existing + the new one), 0 failed.
crates/ec-hw/src/dec/hevc.rsmodified+35 -1
@@ -93,6 +93,11 @@ pub struct HevcDecoder {
93 prev_tid0_poc: i32,93 prev_tid0_poc: i32,
94 /// Set once a picture has been decoded since the last reset.94 /// Set once a picture has been decoded since the last reset.
95 started: bool,95 started: bool,
96+ /// `NoRaslOutputFlag` (8.1.3) of the most recent IRAP: RASL pictures
97+ /// associated with it must not be decoded or output when set, because
98+ /// they predict from pictures before a random-access point this decoder
99+ /// never saw.
100+ no_rasl_output: bool,
96 max_reorder: usize,101 max_reorder: usize,
97 max_dec_pic_buffering: usize,102 max_dec_pic_buffering: usize,
98 }103 }
@@ -111,6 +116,7 @@ impl HevcDecoder {
111 current: None,116 current: None,
112 prev_tid0_poc: 0,117 prev_tid0_poc: 0,
113 started: false,118 started: false,
119+ no_rasl_output: false,
114 max_reorder: 1,120 max_reorder: 1,
115 max_dec_pic_buffering: 6,121 max_dec_pic_buffering: 6,
116 }122 }
@@ -171,6 +177,7 @@ impl HevcDecoder {
171 self.ready.clear();177 self.ready.clear();
172 self.prev_tid0_poc = 0;178 self.prev_tid0_poc = 0;
173 self.started = false;179 self.started = false;
180+ self.no_rasl_output = false;
174 }181 }
175 182
176 /// What the stream turned out to be.183 /// What the stream turned out to be.
@@ -202,7 +209,14 @@ impl HevcDecoder {
202 209
203 if sh.first_slice_segment_in_pic {210 if sh.first_slice_segment_in_pic {
204 self.finish_picture()?;211 self.finish_picture()?;
205- self.start_picture(&sps, &sh, nal_type, temporal_id, timestamp)?;212+ // 8.1.3 / C.5.2.2: a RASL picture associated with an IRAP whose
213+ // NoRaslOutputFlag is 1 predicts from pictures before that IRAP,
214+ // which a mid-stream start never decoded. It is not output and
215+ // is not guaranteed decodable, so it is dropped whole rather than
216+ // handed to the driver.
217+ if !(is_rasl_picture(nal_type) && self.no_rasl_output) {
218+ self.start_picture(&sps, &sh, nal_type, temporal_id, timestamp)?;
219+ }
206 }220 }
207 if self.current.is_none() {221 if self.current.is_none() {
208 return Ok(());222 return Ok(());
@@ -256,6 +270,14 @@ impl HevcDecoder {
256 ) -> Result<()> {270 ) -> Result<()> {
257 self.ensure_session(sps)?;271 self.ensure_session(sps)?;
258 272
273+ if nal_type.is_irap() {
274+ // 8.1.3: 1 for an IDR, a BLA, or the first picture of a coded
275+ // video sequence — which a mid-stream start's first IRAP always
276+ // is, `self.started` still being false at that point.
277+ self.no_rasl_output =
278+ nal_type.is_idr() || is_bla_picture(nal_type) || !self.started;
279+ }
280+
259 let poc = self.picture_order_count(sps, sh, nal_type);281 let poc = self.picture_order_count(sps, sh, nal_type);
260 // 8.3.2 runs before the picture is decoded and after its POC is known:282 // 8.3.2 runs before the picture is decoded and after its POC is known:
261 // every stored picture the set does not name stops being a reference.283 // every stored picture the set does not name stops being a reference.
@@ -636,6 +658,18 @@ fn is_leading_picture(nal_type: NalUnitType) -> bool {
636 (6..=9).contains(&nal_type.code())658 (6..=9).contains(&nal_type.code())
637 }659 }
638 660
661+/// RASL_N and RASL_R (8, 9): leading pictures dropped whole when their IRAP's
662+/// `NoRaslOutputFlag` is 1 (8.1.3).
663+fn is_rasl_picture(nal_type: NalUnitType) -> bool {
664+ matches!(nal_type.code(), 8 | 9)
665+}
666+
667+/// BLA_W_LP, BLA_W_RADL and BLA_N_LP (16..=18): always `NoRaslOutputFlag` 1
668+/// (8.1.3), a broken-link splice point that never has a decodable RASL.
669+fn is_bla_picture(nal_type: NalUnitType) -> bool {
670+ (16..=18).contains(&nal_type.code())
671+}
672+
639 /// `slice_pic_parameter_set_id`, readable without any parameter set.673 /// `slice_pic_parameter_set_id`, readable without any parameter set.
640 fn peek_pps_id(rbsp: &[u8], nal_type: NalUnitType) -> Result<u32> {674 fn peek_pps_id(rbsp: &[u8], nal_type: NalUnitType) -> Result<u32> {
641 let mut r = ec_core::BitReader::new(rbsp);675 let mut r = ec_core::BitReader::new(rbsp);
crates/ec-hw/tests/gpu.rsmodified+90 -0
@@ -1032,3 +1032,93 @@ fn short(path: &Path) -> String {
1032 .map(|n| n.to_string_lossy().to_string())1032 .map(|n| n.to_string_lossy().to_string())
1033 .unwrap_or_default()1033 .unwrap_or_default()
1034 }1034 }
1035+
1036+/// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in
1037+/// decode order, even though they display before it (they predict from
1038+/// pictures the CRA's own random-access point does not have). A decoder
1039+/// started fresh at that CRA — what a seek does — must therefore drop those
1040+/// RASL pictures per H.265 8.1.3's `NoRaslOutputFlag`, or its output shifts by
1041+/// however many RASL pictures the GOP has, handing back the wrong picture for
1042+/// every display index at or after the seek target.
1043+#[test]
1044+fn hevc_seek_matches_linear_mkv() {
1045+ let Some(display) = display() else { return };
1046+ let path = bitstreams().join("test_hevc.mkv");
1047+ let Some(data) = elementary(&path, Codec::H265, 45) else {
1048+ eprintln!("skipped: no test_hevc.mkv fixture");
1049+ return;
1050+ };
1051+ let units = split_access_units(&data, Codec::H265);
1052+
1053+ // Linear decode: every picture from the start, frame 30 kept for
1054+ // comparison (the file's second CRA, per `test_hevc.mkv`'s 30-picture
1055+ // GOPs).
1056+ const TARGET: usize = 30;
1057+ let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1058+ let mut linear_frame_30 = None;
1059+ let mut index = 0usize;
1060+ decode_stream(&mut decoder, Codec::H265, &data, 45, |frame| {
1061+ if index == TARGET {
1062+ linear_frame_30 = Some(frame.to_i420().expect("readback"));
1063+ }
1064+ index += 1;
1065+ })
1066+ .expect("linear decode");
1067+ let linear_frame_30 = linear_frame_30.expect("linear decode reached frame 30");
1068+
1069+ // Find the access unit whose first VCL NAL is the CRA at this GOP
1070+ // boundary, and feed the decoder from there on, exactly as a seek would:
1071+ // nothing from before it goes in.
1072+ let cra_at = units
1073+ .iter()
1074+ .position(|au| starts_with_cra(au))
1075+ .expect("a CRA access unit exists");
1076+
1077+ // A seek reuses the parameter sets a decoder already learned; a fresh one
1078+ // has none, so it is primed the same way `ec_hw::Decoder::reset` expects a
1079+ // caller to: decode the leading VPS/SPS/PPS (their IDR picture is thrown
1080+ // away) and reset picture state before feeding the seek target.
1081+ let mut seek_decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1082+ seek_decoder.decode(units[0], 0).expect("prime parameter sets");
1083+ seek_decoder.reset();
1084+ let mut seeked_frame = None;
1085+ for (i, unit) in units[cra_at..].iter().enumerate() {
1086+ seek_decoder.decode(unit, i as i64).expect("seek decode");
1087+ if let Some(frame) = seek_decoder.next_frame() {
1088+ seeked_frame = Some(frame.to_i420().expect("readback"));
1089+ break;
1090+ }
1091+ }
1092+ if seeked_frame.is_none() {
1093+ seek_decoder.flush().expect("flush");
1094+ seeked_frame = seek_decoder
1095+ .next_frame()
1096+ .map(|f| f.to_i420().expect("readback"));
1097+ }
1098+ let seeked_frame = seeked_frame.expect("a frame came out after the seek");
1099+
1100+ assert_eq!(
1101+ seeked_frame, linear_frame_30,
1102+ "seeking to the CRA before frame 30 handed back a different picture \
1103+ than a linear decode of frame 30 — RASL pictures were not dropped"
1104+ );
1105+}
1106+
1107+/// True when an access unit's first VCL NAL is an HEVC CRA (`nal_type` 21).
1108+/// A CRA's access unit may open with its own VPS/SPS/PPS, so this skips past
1109+/// non-VCL NALs to the first one that carries a slice.
1110+fn starts_with_cra(au: &[u8]) -> bool {
1111+ let mut i = 0usize;
1112+ while i + 3 < au.len() {
1113+ if au[i] == 0 && au[i + 1] == 0 && au[i + 2] == 1 {
1114+ let t = (au[i + 3] >> 1) & 0x3f;
1115+ if t <= 31 {
1116+ return t == 21;
1117+ }
1118+ i += 3;
1119+ continue;
1120+ }
1121+ i += 1;
1122+ }
1123+ false
1124+}