From 4d595798788cf4d682b4520c044937fcf18626d7 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 13:12:00 +0300 Subject: [PATCH] feat(h265-syntax): the fields a VA-API decoder asks for and this encoder does not write An audit of every field in VAPictureParameterBufferHEVC and VASliceParameterBufferHEVC against what this crate hands back turned up four the parser walked past: PCM depths and block sizes, explicit tile column and row geometry, colour_plane_id, and the emulation prevention byte count of the slice data. All four are carried now, with round-trip tests, because the consumer is a hardware decode path that cannot re-parse a header the parser already read. What is still deliberately absent, and why: reference lists and the picture buffer (decoder state, not syntax), scaling lists (refused as Unsupported), and pred_weight_table (walked for length, not modelled - no HEVC stream this family produces uses weighted prediction). --- crates/ec-h265-syntax/src/lib.rs | 2 +- crates/ec-h265-syntax/src/ps.rs | 82 +++++++++++++++++++++++++---- crates/ec-h265-syntax/src/slice.rs | 39 +++++++++++++- crates/ec-h265-syntax/tests/fuzz.rs | 1 + crates/ec-h265/src/encoder.rs | 1 + 5 files changed, 114 insertions(+), 11 deletions(-) diff --git a/crates/ec-h265-syntax/src/lib.rs b/crates/ec-h265-syntax/src/lib.rs index b31b7aa3..2262c216 100644 --- a/crates/ec-h265-syntax/src/lib.rs +++ b/crates/ec-h265-syntax/src/lib.rs @@ -37,5 +37,5 @@ pub mod vui; pub use nal::{AnnexBNal, NalHeader, NalUnitType, escape_rbsp, split_annex_b, unescape_rbsp}; pub use ps::{ConformanceWindow, Pps, ProfileTierLevel, ShortTermRefPicSet, Sps, Vps}; -pub use slice::{ParsePositions, SliceHeader, SliceType}; +pub use slice::{ParsePositions, SliceHeader, SliceType, count_emulation_prevention_bytes}; pub use vui::{ColourDescription, VideoSignalType, VuiParameters}; diff --git a/crates/ec-h265-syntax/src/ps.rs b/crates/ec-h265-syntax/src/ps.rs index c94997ac..98904959 100644 --- a/crates/ec-h265-syntax/src/ps.rs +++ b/crates/ec-h265-syntax/src/ps.rs @@ -305,6 +305,11 @@ pub struct Sps { pub sao_enabled: bool, /// `pcm_enabled_flag`. pub pcm_enabled: bool, + /// PCM sample depths and block sizes, present when `pcm_enabled`: + /// `(pcm_sample_bit_depth_luma_minus1, pcm_sample_bit_depth_chroma_minus1, + /// log2_min_pcm_luma_coding_block_size_minus3, + /// log2_diff_max_min_pcm_luma_coding_block_size, pcm_loop_filter_disabled_flag)`. + pub pcm: Option<(u32, u32, u32, u32, bool)>, /// `num_short_term_ref_pic_sets`. pub num_short_term_ref_pic_sets: u32, /// `long_term_ref_pics_present_flag`. @@ -404,6 +409,13 @@ impl Sps { w.write_bit(self.amp_enabled); w.write_bit(self.sao_enabled); w.write_bit(self.pcm_enabled); + if let Some((luma, chroma, log2_min, log2_diff, filter_off)) = self.pcm { + w.write_bits(luma, 4); + w.write_bits(chroma, 4); + w.write_ue(log2_min); + w.write_ue(log2_diff); + w.write_bit(filter_off); + } w.write_ue(self.num_short_term_ref_pic_sets); w.write_bit(self.long_term_ref_pics_present); if self.long_term_ref_pics_present { @@ -512,13 +524,17 @@ impl Sps { let amp_enabled = r.read_bit()?; let sao_enabled = r.read_bit()?; let pcm_enabled = r.read_bit()?; - if pcm_enabled { - r.read_bits(4)?; // pcm_sample_bit_depth_luma_minus1 - r.read_bits(4)?; // pcm_sample_bit_depth_chroma_minus1 - r.read_ue()?; // log2_min_pcm_luma_coding_block_size_minus3 - r.read_ue()?; // log2_diff_max_min_pcm_luma_coding_block_size - r.read_bit()?; // pcm_loop_filter_disabled_flag - } + let pcm = if pcm_enabled { + Some(( + r.read_bits(4)?, + r.read_bits(4)?, + r.read_ue()?, + r.read_ue()?, + r.read_bit()?, + )) + } else { + None + }; let num_short_term_ref_pic_sets = r.read_ue()?; if num_short_term_ref_pic_sets > 64 { return Err(Error::corrupt("HEVC SPS: num_short_term_ref_pic_sets > 64")); @@ -572,6 +588,7 @@ impl Sps { amp_enabled, sao_enabled, pcm_enabled, + pcm, num_short_term_ref_pic_sets, long_term_ref_pics_present, num_long_term_ref_pics_sps, @@ -716,6 +733,11 @@ pub struct Pps { pub num_tile_rows_minus1: u32, /// `uniform_spacing_flag`. pub uniform_spacing: bool, + /// `column_width_minus1[]`, empty when the spacing is uniform (a decoder + /// derives the widths then, and so should a caller). + pub column_width_minus1: Vec, + /// `row_height_minus1[]`, empty when the spacing is uniform. + pub row_height_minus1: Vec, /// `loop_filter_across_tiles_enabled_flag`. pub loop_filter_across_tiles_enabled: bool, /// `pps_loop_filter_across_slices_enabled_flag`. @@ -766,6 +788,8 @@ impl Default for Pps { num_tile_columns_minus1: 0, num_tile_rows_minus1: 0, uniform_spacing: true, + column_width_minus1: Vec::new(), + row_height_minus1: Vec::new(), loop_filter_across_tiles_enabled: true, loop_filter_across_slices_enabled: true, deblocking_filter_control_present: false, @@ -812,6 +836,14 @@ impl Pps { w.write_ue(self.num_tile_columns_minus1); w.write_ue(self.num_tile_rows_minus1); w.write_bit(self.uniform_spacing); + if !self.uniform_spacing { + for &width in &self.column_width_minus1 { + w.write_ue(width); + } + for &height in &self.row_height_minus1 { + w.write_ue(height); + } + } w.write_bit(self.loop_filter_across_tiles_enabled); } w.write_bit(self.loop_filter_across_slices_enabled); @@ -868,11 +900,16 @@ impl Pps { pps.num_tile_rows_minus1 = r.read_ue()?; pps.uniform_spacing = r.read_bit()?; if !pps.uniform_spacing { + if pps.num_tile_columns_minus1 > 20 || pps.num_tile_rows_minus1 > 22 { + return Err(Error::corrupt("HEVC PPS: tile count beyond any level")); + } for _ in 0..pps.num_tile_columns_minus1 { - r.read_ue()?; + let value = r.read_ue()?; + pps.column_width_minus1.push(value); } for _ in 0..pps.num_tile_rows_minus1 { - r.read_ue()?; + let value = r.read_ue()?; + pps.row_height_minus1.push(value); } } pps.loop_filter_across_tiles_enabled = r.read_bit()?; @@ -949,6 +986,7 @@ mod tests { amp_enabled: false, sao_enabled: false, pcm_enabled: false, + pcm: None, num_short_term_ref_pic_sets: 0, long_term_ref_pics_present: false, num_long_term_ref_pics_sps: 0, @@ -998,6 +1036,32 @@ mod tests { ..Pps::default() }; assert_eq!(Pps::parse(&pps.to_rbsp()).unwrap(), pps); + + // The fields a hardware decoder needs but this family's own encoder + // never writes: explicit tile geometry and PCM. + let tiled = Pps { + tiles_enabled: true, + num_tile_columns_minus1: 2, + num_tile_rows_minus1: 1, + uniform_spacing: false, + column_width_minus1: vec![9, 19], + row_height_minus1: vec![7], + loop_filter_across_tiles_enabled: false, + cu_qp_delta_enabled: true, + diff_cu_qp_delta_depth: 2, + cb_qp_offset: -3, + cr_qp_offset: 4, + ..Pps::default() + }; + assert_eq!(Pps::parse(&tiled.to_rbsp()).unwrap(), tiled); + + let pcm = Sps { + pcm_enabled: true, + pcm: Some((7, 7, 0, 2, true)), + sao_enabled: true, + ..sample_sps() + }; + assert_eq!(Sps::parse(&pcm.to_rbsp()).unwrap(), pcm); } #[test] diff --git a/crates/ec-h265-syntax/src/slice.rs b/crates/ec-h265-syntax/src/slice.rs index 51245870..b9a3a269 100644 --- a/crates/ec-h265-syntax/src/slice.rs +++ b/crates/ec-h265-syntax/src/slice.rs @@ -57,6 +57,9 @@ pub struct SliceHeader { pub pic_output_flag: bool, /// `slice_pic_order_cnt_lsb`; absent (0) on IDR pictures. pub poc_lsb: u32, + /// `colour_plane_id`, present only when the sequence codes its three + /// planes as separate pictures. + pub colour_plane_id: u8, /// `slice_sao_luma_flag`. pub sao_luma: bool, /// `slice_sao_chroma_flag`. @@ -107,6 +110,7 @@ impl SliceHeader { slice_type: SliceType::I, pic_output_flag: true, poc_lsb: 0, + colour_plane_id: 0, sao_luma: false, sao_chroma: false, num_ref_idx_l0_active_minus1: 0, @@ -244,7 +248,7 @@ impl SliceHeader { h.pic_output_flag = r.read_bit()?; } if sps.separate_colour_plane { - r.read_bits(2)?; // colour_plane_id + h.colour_plane_id = r.read_bits(2)? as u8; } let mut st_rps = ShortTermRefPicSet::default(); let mut num_long_term = 0; @@ -437,6 +441,29 @@ impl ParsePositions { } } +/// How many emulation prevention bytes a NAL unit payload carries. +/// +/// `VASliceParameterBufferHEVC::slice_data_num_emu_prevn_bytes` wants this for +/// the slice data a driver is handed, and a caller that has the escaped bytes +/// should not have to unescape them twice to find out. +pub fn count_emulation_prevention_bytes(escaped_payload: &[u8]) -> usize { + let mut zeros = 0usize; + let mut count = 0usize; + for &b in escaped_payload { + if zeros >= 2 && b == 3 { + count += 1; + zeros = 0; + continue; + } + if b == 0 { + zeros += 1; + } else { + zeros = 0; + } + } + count +} + /// Walk past a `pred_weight_table()` (7.3.6.3). fn skip_pred_weight_table(r: &mut BitReader, h: &SliceHeader, sps: &Sps) -> Result<()> { r.read_ue()?; // luma_log2_weight_denom @@ -509,6 +536,7 @@ mod tests { amp_enabled: false, sao_enabled: false, pcm_enabled: false, + pcm: None, num_short_term_ref_pic_sets: 0, long_term_ref_pics_present: false, num_long_term_ref_pics_sps: 0, @@ -542,6 +570,15 @@ mod tests { assert_eq!(pos.slice_data_byte_offset(&rbsp), 2 + rbsp.len()); } + #[test] + fn emulation_prevention_bytes_are_counted() { + assert_eq!(count_emulation_prevention_bytes(&[0, 0, 3, 1, 0xff]), 1); + assert_eq!(count_emulation_prevention_bytes(&[0, 0, 3, 0, 0, 3, 2]), 2); + // A 3 that is not preceded by two zeros is payload, not an escape. + assert_eq!(count_emulation_prevention_bytes(&[0, 3, 3, 3]), 0); + assert_eq!(count_emulation_prevention_bytes(&[]), 0); + } + #[test] fn byte_offset_counts_escape_bytes() { // RBSP 00 00 01 ... escapes to 00 00 03 01 ...; a header of four RBSP diff --git a/crates/ec-h265-syntax/tests/fuzz.rs b/crates/ec-h265-syntax/tests/fuzz.rs index e7a26644..67542af4 100644 --- a/crates/ec-h265-syntax/tests/fuzz.rs +++ b/crates/ec-h265-syntax/tests/fuzz.rs @@ -63,6 +63,7 @@ fn reference_sps() -> Sps { amp_enabled: false, sao_enabled: true, pcm_enabled: false, + pcm: None, num_short_term_ref_pic_sets: 0, long_term_ref_pics_present: false, num_long_term_ref_pics_sps: 0, diff --git a/crates/ec-h265/src/encoder.rs b/crates/ec-h265/src/encoder.rs index 18d42058..6e5a938d 100644 --- a/crates/ec-h265/src/encoder.rs +++ b/crates/ec-h265/src/encoder.rs @@ -197,6 +197,7 @@ impl Encoder { // No in-loop filters: see `Encoder` docs and the PPS below. sao_enabled: false, pcm_enabled: false, + pcm: None, num_short_term_ref_pic_sets: 0, long_term_ref_pics_present: false, num_long_term_ref_pics_sps: 0,