ec-hw, ec-h264: H.264 SPS writes VUI bitstream_restriction (max_num_reorder_frames 0, max_dec_frame_buffering 1), so decoders output each picture immediately; ec-hw and ec-h264 decoders treat pic_order_cnt_type 2 without bitstream_restriction as zero reorder
Browse files at this commit1190657740132e7d92a887ede9a0377034b18823Parent: e8446765 changed files +45 -6
- crates/ec-h264/src/dpb.rs +1 -0
- crates/ec-h264/src/enc/headers.rs +8 -1
- crates/ec-hw/src/dec/dpb264.rs +1 -0
- crates/ec-hw/src/enc/headers.rs +13 -5
- crates/ec-hw/tests/gpu.rs +22 -0
crates/ec-h264/src/dpb.rsmodified+1 -0
| @@ -580,6 +580,7 @@ impl<S: Sample> Dpb<S> { | |||
| 580 | // Without the VUI restriction the level limit is all we know. It | 580 | // Without the VUI restriction the level limit is all we know. It |
| 581 | // over-buffers a low-delay stream, which costs latency but never | 581 | // over-buffers a low-delay stream, which costs latency but never |
| 582 | // order: pictures still leave smallest-POC-first. | 582 | // order: pictures still leave smallest-POC-first. |
| 583 | + None if sps.pic_order_cnt_type == 2 => (level_frames, 0), | ||
| 583 | None => (level_frames, level_frames), | 584 | None => (level_frames, level_frames), |
| 584 | }; | 585 | }; |
| 585 | self.interlaced = !sps.frame_mbs_only; | 586 | self.interlaced = !sps.frame_mbs_only; |
crates/ec-h264/src/enc/headers.rsmodified+8 -1
| @@ -125,7 +125,14 @@ pub(crate) fn write_sps(p: &SeqParams) -> Vec<u8> { | |||
| 125 | w.write_bit(false); // nal_hrd_parameters_present_flag | 125 | w.write_bit(false); // nal_hrd_parameters_present_flag |
| 126 | w.write_bit(false); // vcl_hrd_parameters_present_flag | 126 | w.write_bit(false); // vcl_hrd_parameters_present_flag |
| 127 | w.write_bit(false); // pic_struct_present_flag | 127 | w.write_bit(false); // pic_struct_present_flag |
| 128 | - w.write_bit(false); // bitstream_restriction_flag | 128 | + w.write_bit(true); // bitstream_restriction_flag |
| 129 | + w.write_bit(true); // motion_vectors_over_pic_boundaries_flag | ||
| 130 | + w.write_ue(2); // max_bytes_per_pic_denom | ||
| 131 | + w.write_ue(1); // max_bits_per_mb_denom | ||
| 132 | + w.write_ue(15); // log2_max_mv_length_horizontal | ||
| 133 | + w.write_ue(15); // log2_max_mv_length_vertical | ||
| 134 | + w.write_ue(0); // max_num_reorder_frames | ||
| 135 | + w.write_ue(1); // max_dec_frame_buffering = max_num_ref_frames | ||
| 129 | trailing(&mut w); | 136 | trailing(&mut w); |
| 130 | w.into_bytes() | 137 | w.into_bytes() |
| 131 | } | 138 | } |
crates/ec-hw/src/dec/dpb264.rsmodified+1 -0
| @@ -171,6 +171,7 @@ impl Dpb { | |||
| 171 | (r.max_dec_frame_buffering as usize).min(MAX_STORED - 1), | 171 | (r.max_dec_frame_buffering as usize).min(MAX_STORED - 1), |
| 172 | (r.max_num_reorder_frames as usize).min(MAX_STORED - 1), | 172 | (r.max_num_reorder_frames as usize).min(MAX_STORED - 1), |
| 173 | ), | 173 | ), |
| 174 | + None if sps.pic_order_cnt_type == 2 => (level_frames, 0), | ||
| 174 | None => (level_frames, level_frames), | 175 | None => (level_frames, level_frames), |
| 175 | }; | 176 | }; |
| 176 | self.max_frames = buffering.max(sps.max_num_ref_frames as usize).max(1); | 177 | self.max_frames = buffering.max(sps.max_num_ref_frames as usize).max(1); |
crates/ec-hw/src/enc/headers.rsmodified+13 -5
| @@ -125,7 +125,14 @@ pub(super) fn h264_sps(p: &H264Params) -> Packed { | |||
| 125 | w.write_bit(false); // nal_hrd_parameters_present_flag | 125 | w.write_bit(false); // nal_hrd_parameters_present_flag |
| 126 | w.write_bit(false); // vcl_hrd_parameters_present_flag | 126 | w.write_bit(false); // vcl_hrd_parameters_present_flag |
| 127 | w.write_bit(false); // pic_struct_present_flag | 127 | w.write_bit(false); // pic_struct_present_flag |
| 128 | - w.write_bit(false); // bitstream_restriction_flag | 128 | + w.write_bit(true); // bitstream_restriction_flag |
| 129 | + w.write_bit(true); // motion_vectors_over_pic_boundaries_flag | ||
| 130 | + w.write_ue(2); // max_bytes_per_pic_denom | ||
| 131 | + w.write_ue(1); // max_bits_per_mb_denom | ||
| 132 | + w.write_ue(15); // log2_max_mv_length_horizontal | ||
| 133 | + w.write_ue(15); // log2_max_mv_length_vertical | ||
| 134 | + w.write_ue(0); // max_num_reorder_frames | ||
| 135 | + w.write_ue(1); // max_dec_frame_buffering = max_num_ref_frames | ||
| 129 | trailing(&mut w); | 136 | trailing(&mut w); |
| 130 | nal(PACKED_HEADER_SEQUENCE, &[0x67], &w.into_bytes(), 0) | 137 | nal(PACKED_HEADER_SEQUENCE, &[0x67], &w.into_bytes(), 0) |
| 131 | } | 138 | } |
| @@ -699,12 +706,13 @@ mod h264_profile_tests { | |||
| 699 | // and the scaling-list flag. Main and Baseline must not carry them, so | 706 | // and the scaling-list flag. Main and Baseline must not carry them, so |
| 700 | // their SPS is shorter than High's for the same picture. | 707 | // their SPS is shorter than High's for the same picture. |
| 701 | let high = sps_profile(High).2; | 708 | let high = sps_profile(High).2; |
| 702 | - assert!(sps_profile(Main).2 < high); | ||
| 703 | assert_eq!(sps_profile(Main).2, sps_profile(ConstrainedBaseline).2); | 709 | assert_eq!(sps_profile(Main).2, sps_profile(ConstrainedBaseline).2); |
| 704 | // ue(1) chroma_format_idc is 3 bits, two ue(0) depths are 1 bit each, | 710 | // ue(1) chroma_format_idc is 3 bits, two ue(0) depths are 1 bit each, |
| 705 | - // then the two flags: 3 + 1 + 1 + 1 + 1 = 7 bits. The rbsp_trailing_bits | 711 | + // then the two flags: 7 bits, rounded by rbsp_trailing_bits to 0 or 8. |
| 706 | - // alignment can move the count by one more. | 712 | + assert!([0, 8].contains(&(high - sps_profile(Main).2))); |
| 707 | - assert!((7..=8).contains(&(high - sps_profile(Main).2))); | 713 | + let sps = h264_sps(¶ms(High)).bytes; |
| 714 | + let main = h264_sps(¶ms(Main)).bytes; | ||
| 715 | + assert_ne!(sps[8], main[8]); | ||
| 708 | } | 716 | } |
| 709 | 717 | ||
| 710 | #[test] | 718 | #[test] |
crates/ec-hw/tests/gpu.rsmodified+22 -0
| @@ -3697,3 +3697,25 @@ fn ten_bit_prime_submit_matches_the_cpu_path() { | |||
| 3697 | b.len() | 3697 | b.len() |
| 3698 | ); | 3698 | ); |
| 3699 | } | 3699 | } |
| 3700 | + | ||
| 3701 | +#[test] | ||
| 3702 | +fn h264_encode_decodes_with_no_reorder_delay() { | ||
| 3703 | + let Some(display) = display() else { return }; | ||
| 3704 | + let mut config = EncoderConfig::new(EncCodec::H264, 1280, 720); | ||
| 3705 | + config.framerate = (30, 1); | ||
| 3706 | + config.gop_size = 30; | ||
| 3707 | + config.rate_control = RateControlMode::ConstantQp { qp: 30 }; | ||
| 3708 | + let mut encoder = Encoder::new(&display, config).expect("encoder"); | ||
| 3709 | + let mut decoder = Decoder::new(&display, Codec::H264).expect("decoder"); | ||
| 3710 | + for t in 0..10 { | ||
| 3711 | + let meta = FrameMetadata { | ||
| 3712 | + timestamp: i64::from(t), | ||
| 3713 | + force_keyframe: false, | ||
| 3714 | + }; | ||
| 3715 | + let unit = encoder | ||
| 3716 | + .encode(&source_frame(1280, 720, t), meta) | ||
| 3717 | + .expect("encode"); | ||
| 3718 | + decoder.decode(&unit.data, i64::from(t)).expect("decode"); | ||
| 3719 | + assert!(decoder.next_frame().is_some(), "frame {t} held back"); | ||
| 3720 | + } | ||
| 3721 | +} | ||