forge
Explore
Log in

Don't you dare go Hollow

A group of one picture writes a header a decoder can read

Browse files at this commit

9a188c5355cd6ee349939e20cfe1edab5f23abeaAhmet Kaan GümüşParent: eff035e
  • fix
    • a group of one picture writes a header a decoder can read
Show full message
🐛 fix(engine-hw): a group of one picture writes a header a decoder can read

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.rsmodified+25 -0
@@ -1865,6 +1865,31 @@ mod tests {
1865 assert_eq!(gop_limit(u32::MAX, false), u16::MAX);1865 assert_eq!(gop_limit(u32::MAX, false), u16::MAX);
1866 }1866 }
1867 1867
1868+ /// What that group of one writes into the sequence header, which is the
1869+ /// half that shipped broken: the predictor hands the limit straight to
1870+ /// `SpsBuilder::max_frame_num` (`h264/predictor.rs:89`), and `1.ilog2()`
1871+ /// is 0, so the old `0u8 - 4u8` wrapped to 252 in release. Every intra-only
1872+ /// hardware file then carried `log2_max_frame_num_minus4 = 252` -- outside
1873+ /// the spec's 0..=12 -- and no decoder would parse it. Zero is the right
1874+ /// answer, not merely a legal one: `frame_num` never leaves 0 when every
1875+ /// picture is an IDR. No GPU: this is the bitstream side of the seat.
1876+ #[test]
1877+ fn an_intra_seat_writes_a_sequence_header_a_decoder_can_read() {
1878+ use cros_codecs::codec::h264::parser::SpsBuilder;
1879+
1880+ let limit = u32::from(gop_limit(30, true));
1881+ let sps = SpsBuilder::new().max_frame_num(limit).max_pic_order_cnt_lsb(limit * 2).build();
1882+ assert_eq!(sps.log2_max_frame_num_minus4, 0, "252 is what a wrap looks like");
1883+ assert_eq!(sps.log2_max_pic_order_cnt_lsb_minus4, 0);
1884+ // The ordinary export's limit is untouched by the saturation, and a
1885+ // limit at the top of the `u16` stays inside the spec's 0..=12.
1886+ let long = u32::from(gop_limit(30, false));
1887+ assert_eq!(SpsBuilder::new().max_frame_num(long).build().log2_max_frame_num_minus4, 1);
1888+ assert!(
1889+ SpsBuilder::new().max_frame_num(u32::MAX).build().log2_max_frame_num_minus4 <= 12
1890+ );
1891+ }
1892+
1868 /// The other half of the same fault: a slice is marked IDR only where the1893 /// The other half of the same fault: a slice is marked IDR only where the
1869 /// access unit carries an SPS, so a "forced key frame" that writes no1894 /// access unit carries an SPS, so a "forced key frame" that writes no
1870 /// parameter sets reads back as an ordinary I picture -- no decoder starts1895 /// parameter sets reads back as an ordinary I picture -- no decoder starts
vendor/cros-codecs/src/codec/h264/parser.rsmodified+9 -2
@@ -1170,8 +1170,13 @@ impl SpsBuilder {
1170 self1170 self
1171 }1171 }
1172 1172
1173+ /// Saturating, because an all-IDR stream asks for `max_frame_num` 1: its
1174+ /// `ilog2()` is 0 and `0u8 - 4u8` wraps to 252 in release, an SPS out of
1175+ /// the spec's 0..=12 that every decoder refuses. Zero is not merely legal
1176+ /// there, it is right: `frame_num` never leaves 0 when every picture is an
1177+ /// IDR.
1173 pub fn max_frame_num(self, value: u32) -> Self {1178 pub fn max_frame_num(self, value: u32) -> Self {
1174- self.log2_max_frame_num_minus4(value.ilog2() as u8 - 4u8)1179+ self.log2_max_frame_num_minus4(value.ilog2().saturating_sub(4).min(12) as u8)
1175 }1180 }
1176 1181
1177 pub fn pic_order_cnt_type(mut self, value: u8) -> Self {1182 pub fn pic_order_cnt_type(mut self, value: u8) -> Self {
@@ -1184,8 +1189,10 @@ impl SpsBuilder {
1184 self1189 self
1185 }1190 }
1186 1191
1192+ /// Saturating for the same reason as [`SpsBuilder::max_frame_num`]: the
1193+ /// all-IDR caller asks for 2 here, whose `ilog2()` is 1.
1187 pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self {1194 pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self {
1188- self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2() as u8 - 4u8)1195+ self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2().saturating_sub(4).min(12) as u8)
1189 }1196 }
1190 1197
1191 pub fn delta_pic_order_always_zero_flag(mut self, value: bool) -> Self {1198 pub fn delta_pic_order_always_zero_flag(mut self, value: bool) -> Self {
vendor/cros-codecs/src/codec/h265/parser.rsmodified+4 -1
@@ -1233,8 +1233,11 @@ impl SpsBuilder {
1233 self1233 self
1234 }1234 }
1235 1235
1236+ /// Saturating: a `value` under 16 has no legal `minus4` below zero and
1237+ /// `0u8 - 4u8` wraps to 252 in release -- the H.264 twin of this shipped
1238+ /// exactly that in every all-IDR SPS. Clamped above to the spec's 12.
1236 pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self {1239 pub fn max_pic_order_cnt_lsb(self, value: u32) -> Self {
1237- self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2() as u8 - 4u8)1240+ self.log2_max_pic_order_cnt_lsb_minus4(value.ilog2().saturating_sub(4).min(12) as u8)
1238 }1241 }
1239 1242
1240 pub fn sub_layer_ordering_info_present_flag(mut self, value: bool) -> Self {1243 pub fn sub_layer_ordering_info_present_flag(mut self, value: bool) -> Self {