diff --git a/crates/ec-h264/src/dpb.rs b/crates/ec-h264/src/dpb.rs index ab6e7347..75a080ab 100644 --- a/crates/ec-h264/src/dpb.rs +++ b/crates/ec-h264/src/dpb.rs @@ -580,6 +580,7 @@ impl Dpb { // Without the VUI restriction the level limit is all we know. It // over-buffers a low-delay stream, which costs latency but never // order: pictures still leave smallest-POC-first. + None if sps.pic_order_cnt_type == 2 => (level_frames, 0), None => (level_frames, level_frames), }; self.interlaced = !sps.frame_mbs_only; diff --git a/crates/ec-h264/src/enc/headers.rs b/crates/ec-h264/src/enc/headers.rs index 5f0a2afb..dcafaf28 100644 --- a/crates/ec-h264/src/enc/headers.rs +++ b/crates/ec-h264/src/enc/headers.rs @@ -125,7 +125,14 @@ pub(crate) fn write_sps(p: &SeqParams) -> Vec { w.write_bit(false); // nal_hrd_parameters_present_flag w.write_bit(false); // vcl_hrd_parameters_present_flag w.write_bit(false); // pic_struct_present_flag - w.write_bit(false); // bitstream_restriction_flag + w.write_bit(true); // bitstream_restriction_flag + w.write_bit(true); // motion_vectors_over_pic_boundaries_flag + w.write_ue(2); // max_bytes_per_pic_denom + w.write_ue(1); // max_bits_per_mb_denom + w.write_ue(15); // log2_max_mv_length_horizontal + w.write_ue(15); // log2_max_mv_length_vertical + w.write_ue(0); // max_num_reorder_frames + w.write_ue(1); // max_dec_frame_buffering = max_num_ref_frames trailing(&mut w); w.into_bytes() } diff --git a/crates/ec-hw/src/dec/dpb264.rs b/crates/ec-hw/src/dec/dpb264.rs index 59088e70..7de0ccfe 100644 --- a/crates/ec-hw/src/dec/dpb264.rs +++ b/crates/ec-hw/src/dec/dpb264.rs @@ -171,6 +171,7 @@ impl Dpb { (r.max_dec_frame_buffering as usize).min(MAX_STORED - 1), (r.max_num_reorder_frames as usize).min(MAX_STORED - 1), ), + None if sps.pic_order_cnt_type == 2 => (level_frames, 0), None => (level_frames, level_frames), }; self.max_frames = buffering.max(sps.max_num_ref_frames as usize).max(1); diff --git a/crates/ec-hw/src/enc/headers.rs b/crates/ec-hw/src/enc/headers.rs index aa92f9b0..4796a53c 100644 --- a/crates/ec-hw/src/enc/headers.rs +++ b/crates/ec-hw/src/enc/headers.rs @@ -125,7 +125,14 @@ pub(super) fn h264_sps(p: &H264Params) -> Packed { w.write_bit(false); // nal_hrd_parameters_present_flag w.write_bit(false); // vcl_hrd_parameters_present_flag w.write_bit(false); // pic_struct_present_flag - w.write_bit(false); // bitstream_restriction_flag + w.write_bit(true); // bitstream_restriction_flag + w.write_bit(true); // motion_vectors_over_pic_boundaries_flag + w.write_ue(2); // max_bytes_per_pic_denom + w.write_ue(1); // max_bits_per_mb_denom + w.write_ue(15); // log2_max_mv_length_horizontal + w.write_ue(15); // log2_max_mv_length_vertical + w.write_ue(0); // max_num_reorder_frames + w.write_ue(1); // max_dec_frame_buffering = max_num_ref_frames trailing(&mut w); nal(PACKED_HEADER_SEQUENCE, &[0x67], &w.into_bytes(), 0) } @@ -699,12 +706,13 @@ mod h264_profile_tests { // and the scaling-list flag. Main and Baseline must not carry them, so // their SPS is shorter than High's for the same picture. let high = sps_profile(High).2; - assert!(sps_profile(Main).2 < high); assert_eq!(sps_profile(Main).2, sps_profile(ConstrainedBaseline).2); // ue(1) chroma_format_idc is 3 bits, two ue(0) depths are 1 bit each, - // then the two flags: 3 + 1 + 1 + 1 + 1 = 7 bits. The rbsp_trailing_bits - // alignment can move the count by one more. - assert!((7..=8).contains(&(high - sps_profile(Main).2))); + // then the two flags: 7 bits, rounded by rbsp_trailing_bits to 0 or 8. + assert!([0, 8].contains(&(high - sps_profile(Main).2))); + let sps = h264_sps(¶ms(High)).bytes; + let main = h264_sps(¶ms(Main)).bytes; + assert_ne!(sps[8], main[8]); } #[test] diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index ec513c46..62da7cc8 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -3697,3 +3697,25 @@ fn ten_bit_prime_submit_matches_the_cpu_path() { b.len() ); } + +#[test] +fn h264_encode_decodes_with_no_reorder_delay() { + let Some(display) = display() else { return }; + let mut config = EncoderConfig::new(EncCodec::H264, 1280, 720); + config.framerate = (30, 1); + config.gop_size = 30; + config.rate_control = RateControlMode::ConstantQp { qp: 30 }; + let mut encoder = Encoder::new(&display, config).expect("encoder"); + let mut decoder = Decoder::new(&display, Codec::H264).expect("decoder"); + for t in 0..10 { + let meta = FrameMetadata { + timestamp: i64::from(t), + force_keyframe: false, + }; + let unit = encoder + .encode(&source_frame(1280, 720, t), meta) + .expect("encode"); + decoder.decode(&unit.data, i64::from(t)).expect("decode"); + assert!(decoder.next_frame().is_some(), "frame {t} held back"); + } +}