From 9a188c5355cd6ee349939e20cfe1edab5f23abea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 04:32:07 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine-hw):=20a=20group=20of?= =?UTF-8?q?=20one=20picture=20writes=20a=20header=20a=20decoder=20can=20re?= =?UTF-8?q?ad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every intra-only hardware file was undecodable. The intra seat asks for a group of one picture, the predictor hands that limit to `SpsBuilder::max_frame_num`, and `1.ilog2()` is 0 -- so `0u8 - 4u8` wrapped to 252 in release and every SPS shipped `log2_max_frame_num_minus4 = 252`, outside the spec's 0..=12. Decoders answered "out of range (0-12): got 252". Saturate instead, in both builders that do the arithmetic (the `max_pic_order_cnt_lsb` twin asks for 2 and wrapped to 253) and in the H.265 parser's copy of the same line. Zero is not merely legal for an all-IDR stream, it is right: `frame_num` never leaves 0 there. The limit itself stays 1 -- that is the IDR-per-access-unit mechanism, and only the header field was garbage. --- crates/engine-hw/src/lib.rs | 25 +++++++++++++++++++++ vendor/cros-codecs/src/codec/h264/parser.rs | 11 +++++++-- vendor/cros-codecs/src/codec/h265/parser.rs | 5 ++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/engine-hw/src/lib.rs b/crates/engine-hw/src/lib.rs index 120c007..5f4d2cf 100644 --- a/crates/engine-hw/src/lib.rs +++ b/crates/engine-hw/src/lib.rs @@ -1865,6 +1865,31 @@ mod tests { assert_eq!(gop_limit(u32::MAX, false), u16::MAX); } + /// What that group of one writes into the sequence header, which is the + /// half that shipped broken: the predictor hands the limit straight to + /// `SpsBuilder::max_frame_num` (`h264/predictor.rs:89`), and `1.ilog2()` + /// is 0, so the old `0u8 - 4u8` wrapped to 252 in release. Every intra-only + /// hardware file then carried `log2_max_frame_num_minus4 = 252` -- outside + /// the spec's 0..=12 -- and no decoder would parse it. Zero is the right + /// answer, not merely a legal one: `frame_num` never leaves 0 when every + /// picture is an IDR. No GPU: this is the bitstream side of the seat. + #[test] + fn an_intra_seat_writes_a_sequence_header_a_decoder_can_read() { + use cros_codecs::codec::h264::parser::SpsBuilder; + + let limit = u32::from(gop_limit(30, true)); + let sps = SpsBuilder::new().max_frame_num(limit).max_pic_order_cnt_lsb(limit * 2).build(); + assert_eq!(sps.log2_max_frame_num_minus4, 0, "252 is what a wrap looks like"); + assert_eq!(sps.log2_max_pic_order_cnt_lsb_minus4, 0); + // The ordinary export's limit is untouched by the saturation, and a + // limit at the top of the `u16` stays inside the spec's 0..=12. + let long = u32::from(gop_limit(30, false)); + assert_eq!(SpsBuilder::new().max_frame_num(long).build().log2_max_frame_num_minus4, 1); + assert!( + SpsBuilder::new().max_frame_num(u32::MAX).build().log2_max_frame_num_minus4 <= 12 + ); + } + /// The other half of the same fault: a slice is marked IDR only where the /// access unit carries an SPS, so a "forced key frame" that writes no /// parameter sets reads back as an ordinary I picture -- no decoder starts diff --git a/vendor/cros-codecs/src/codec/h264/parser.rs b/vendor/cros-codecs/src/codec/h264/parser.rs index 285242a..a8ce698 100644 --- a/vendor/cros-codecs/src/codec/h264/parser.rs +++ b/vendor/cros-codecs/src/codec/h264/parser.rs @@ -1170,8 +1170,13 @@ impl SpsBuilder { self } + /// Saturating, because an all-IDR stream asks for `max_frame_num` 1: its + /// `ilog2()` is 0 and `0u8 - 4u8` wraps to 252 in release, an SPS out of + /// the spec's 0..=12 that every decoder refuses. Zero is not merely legal + /// there, it is right: `frame_num` never leaves 0 when every picture is an + /// IDR. pub fn max_frame_num(self, value: u32) -> Self { - self.log2_max_frame_num_minus4(value.ilog2() as u8 - 4u8) + self.log2_max_frame_num_minus4(value.ilog2().saturating_sub(4).min(12) as u8) } pub fn pic_order_cnt_type(mut self, value: u8) -> Self { @@ -1184,8 +1189,10 @@ impl SpsBuilder { self } + /// Saturating for the same reason as [`SpsBuilder::max_frame_num`]: the + /// all-IDR caller asks for 2 here, whose `ilog2()` is 1. pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self { - self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2() as u8 - 4u8) + self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2().saturating_sub(4).min(12) as u8) } pub fn delta_pic_order_always_zero_flag(mut self, value: bool) -> Self { diff --git a/vendor/cros-codecs/src/codec/h265/parser.rs b/vendor/cros-codecs/src/codec/h265/parser.rs index 7bac147..e112678 100644 --- a/vendor/cros-codecs/src/codec/h265/parser.rs +++ b/vendor/cros-codecs/src/codec/h265/parser.rs @@ -1233,8 +1233,11 @@ impl SpsBuilder { self } + /// Saturating: a `value` under 16 has no legal `minus4` below zero and + /// `0u8 - 4u8` wraps to 252 in release -- the H.264 twin of this shipped + /// exactly that in every all-IDR SPS. Clamped above to the spec's 12. pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self { - self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2() as u8 - 4u8) + self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2().saturating_sub(4).min(12) as u8) } pub fn sub_layer_ordering_info_present_flag(mut self, value: bool) -> Self {