The fields a VA-API decoder asks for and this encoder does not write
Browse files at this commit4d595798788cf4d682b4520c044937fcf18626d7Parent: 646127f- feat
- the fields a VA-API decoder asks for and this encoder does not write
5 changed files +114 -11
- crates/ec-h265-syntax/src/lib.rs +1 -1
- crates/ec-h265-syntax/src/ps.rs +73 -9
- crates/ec-h265-syntax/src/slice.rs +38 -1
- crates/ec-h265-syntax/tests/fuzz.rs +1 -0
- crates/ec-h265/src/encoder.rs +1 -0
crates/ec-h265-syntax/src/lib.rsmodified+1 -1
| @@ -37,5 +37,5 @@ pub mod vui; | |||
| 37 | 37 | ||
| 38 | pub use nal::{AnnexBNal, NalHeader, NalUnitType, escape_rbsp, split_annex_b, unescape_rbsp}; | 38 | pub use nal::{AnnexBNal, NalHeader, NalUnitType, escape_rbsp, split_annex_b, unescape_rbsp}; |
| 39 | pub use ps::{ConformanceWindow, Pps, ProfileTierLevel, ShortTermRefPicSet, Sps, Vps}; | 39 | pub use ps::{ConformanceWindow, Pps, ProfileTierLevel, ShortTermRefPicSet, Sps, Vps}; |
| 40 | -pub use slice::{ParsePositions, SliceHeader, SliceType}; | 40 | +pub use slice::{ParsePositions, SliceHeader, SliceType, count_emulation_prevention_bytes}; |
| 41 | pub use vui::{ColourDescription, VideoSignalType, VuiParameters}; | 41 | pub use vui::{ColourDescription, VideoSignalType, VuiParameters}; |
crates/ec-h265-syntax/src/ps.rsmodified+73 -9
| @@ -305,6 +305,11 @@ pub struct Sps { | |||
| 305 | pub sao_enabled: bool, | 305 | pub sao_enabled: bool, |
| 306 | /// `pcm_enabled_flag`. | 306 | /// `pcm_enabled_flag`. |
| 307 | pub pcm_enabled: bool, | 307 | pub pcm_enabled: bool, |
| 308 | + /// PCM sample depths and block sizes, present when `pcm_enabled`: | ||
| 309 | + /// `(pcm_sample_bit_depth_luma_minus1, pcm_sample_bit_depth_chroma_minus1, | ||
| 310 | + /// log2_min_pcm_luma_coding_block_size_minus3, | ||
| 311 | + /// log2_diff_max_min_pcm_luma_coding_block_size, pcm_loop_filter_disabled_flag)`. | ||
| 312 | + pub pcm: Option<(u32, u32, u32, u32, bool)>, | ||
| 308 | /// `num_short_term_ref_pic_sets`. | 313 | /// `num_short_term_ref_pic_sets`. |
| 309 | pub num_short_term_ref_pic_sets: u32, | 314 | pub num_short_term_ref_pic_sets: u32, |
| 310 | /// `long_term_ref_pics_present_flag`. | 315 | /// `long_term_ref_pics_present_flag`. |
| @@ -404,6 +409,13 @@ impl Sps { | |||
| 404 | w.write_bit(self.amp_enabled); | 409 | w.write_bit(self.amp_enabled); |
| 405 | w.write_bit(self.sao_enabled); | 410 | w.write_bit(self.sao_enabled); |
| 406 | w.write_bit(self.pcm_enabled); | 411 | w.write_bit(self.pcm_enabled); |
| 412 | + if let Some((luma, chroma, log2_min, log2_diff, filter_off)) = self.pcm { | ||
| 413 | + w.write_bits(luma, 4); | ||
| 414 | + w.write_bits(chroma, 4); | ||
| 415 | + w.write_ue(log2_min); | ||
| 416 | + w.write_ue(log2_diff); | ||
| 417 | + w.write_bit(filter_off); | ||
| 418 | + } | ||
| 407 | w.write_ue(self.num_short_term_ref_pic_sets); | 419 | w.write_ue(self.num_short_term_ref_pic_sets); |
| 408 | w.write_bit(self.long_term_ref_pics_present); | 420 | w.write_bit(self.long_term_ref_pics_present); |
| 409 | if self.long_term_ref_pics_present { | 421 | if self.long_term_ref_pics_present { |
| @@ -512,13 +524,17 @@ impl Sps { | |||
| 512 | let amp_enabled = r.read_bit()?; | 524 | let amp_enabled = r.read_bit()?; |
| 513 | let sao_enabled = r.read_bit()?; | 525 | let sao_enabled = r.read_bit()?; |
| 514 | let pcm_enabled = r.read_bit()?; | 526 | let pcm_enabled = r.read_bit()?; |
| 515 | - if pcm_enabled { | 527 | + let pcm = if pcm_enabled { |
| 516 | - r.read_bits(4)?; // pcm_sample_bit_depth_luma_minus1 | 528 | + Some(( |
| 517 | - r.read_bits(4)?; // pcm_sample_bit_depth_chroma_minus1 | 529 | + r.read_bits(4)?, |
| 518 | - r.read_ue()?; // log2_min_pcm_luma_coding_block_size_minus3 | 530 | + r.read_bits(4)?, |
| 519 | - r.read_ue()?; // log2_diff_max_min_pcm_luma_coding_block_size | 531 | + r.read_ue()?, |
| 520 | - r.read_bit()?; // pcm_loop_filter_disabled_flag | 532 | + r.read_ue()?, |
| 521 | - } | 533 | + r.read_bit()?, |
| 534 | + )) | ||
| 535 | + } else { | ||
| 536 | + None | ||
| 537 | + }; | ||
| 522 | let num_short_term_ref_pic_sets = r.read_ue()?; | 538 | let num_short_term_ref_pic_sets = r.read_ue()?; |
| 523 | if num_short_term_ref_pic_sets > 64 { | 539 | if num_short_term_ref_pic_sets > 64 { |
| 524 | return Err(Error::corrupt("HEVC SPS: num_short_term_ref_pic_sets > 64")); | 540 | return Err(Error::corrupt("HEVC SPS: num_short_term_ref_pic_sets > 64")); |
| @@ -572,6 +588,7 @@ impl Sps { | |||
| 572 | amp_enabled, | 588 | amp_enabled, |
| 573 | sao_enabled, | 589 | sao_enabled, |
| 574 | pcm_enabled, | 590 | pcm_enabled, |
| 591 | + pcm, | ||
| 575 | num_short_term_ref_pic_sets, | 592 | num_short_term_ref_pic_sets, |
| 576 | long_term_ref_pics_present, | 593 | long_term_ref_pics_present, |
| 577 | num_long_term_ref_pics_sps, | 594 | num_long_term_ref_pics_sps, |
| @@ -716,6 +733,11 @@ pub struct Pps { | |||
| 716 | pub num_tile_rows_minus1: u32, | 733 | pub num_tile_rows_minus1: u32, |
| 717 | /// `uniform_spacing_flag`. | 734 | /// `uniform_spacing_flag`. |
| 718 | pub uniform_spacing: bool, | 735 | pub uniform_spacing: bool, |
| 736 | + /// `column_width_minus1[]`, empty when the spacing is uniform (a decoder | ||
| 737 | + /// derives the widths then, and so should a caller). | ||
| 738 | + pub column_width_minus1: Vec<u32>, | ||
| 739 | + /// `row_height_minus1[]`, empty when the spacing is uniform. | ||
| 740 | + pub row_height_minus1: Vec<u32>, | ||
| 719 | /// `loop_filter_across_tiles_enabled_flag`. | 741 | /// `loop_filter_across_tiles_enabled_flag`. |
| 720 | pub loop_filter_across_tiles_enabled: bool, | 742 | pub loop_filter_across_tiles_enabled: bool, |
| 721 | /// `pps_loop_filter_across_slices_enabled_flag`. | 743 | /// `pps_loop_filter_across_slices_enabled_flag`. |
| @@ -766,6 +788,8 @@ impl Default for Pps { | |||
| 766 | num_tile_columns_minus1: 0, | 788 | num_tile_columns_minus1: 0, |
| 767 | num_tile_rows_minus1: 0, | 789 | num_tile_rows_minus1: 0, |
| 768 | uniform_spacing: true, | 790 | uniform_spacing: true, |
| 791 | + column_width_minus1: Vec::new(), | ||
| 792 | + row_height_minus1: Vec::new(), | ||
| 769 | loop_filter_across_tiles_enabled: true, | 793 | loop_filter_across_tiles_enabled: true, |
| 770 | loop_filter_across_slices_enabled: true, | 794 | loop_filter_across_slices_enabled: true, |
| 771 | deblocking_filter_control_present: false, | 795 | deblocking_filter_control_present: false, |
| @@ -812,6 +836,14 @@ impl Pps { | |||
| 812 | w.write_ue(self.num_tile_columns_minus1); | 836 | w.write_ue(self.num_tile_columns_minus1); |
| 813 | w.write_ue(self.num_tile_rows_minus1); | 837 | w.write_ue(self.num_tile_rows_minus1); |
| 814 | w.write_bit(self.uniform_spacing); | 838 | w.write_bit(self.uniform_spacing); |
| 839 | + if !self.uniform_spacing { | ||
| 840 | + for &width in &self.column_width_minus1 { | ||
| 841 | + w.write_ue(width); | ||
| 842 | + } | ||
| 843 | + for &height in &self.row_height_minus1 { | ||
| 844 | + w.write_ue(height); | ||
| 845 | + } | ||
| 846 | + } | ||
| 815 | w.write_bit(self.loop_filter_across_tiles_enabled); | 847 | w.write_bit(self.loop_filter_across_tiles_enabled); |
| 816 | } | 848 | } |
| 817 | w.write_bit(self.loop_filter_across_slices_enabled); | 849 | w.write_bit(self.loop_filter_across_slices_enabled); |
| @@ -868,11 +900,16 @@ impl Pps { | |||
| 868 | pps.num_tile_rows_minus1 = r.read_ue()?; | 900 | pps.num_tile_rows_minus1 = r.read_ue()?; |
| 869 | pps.uniform_spacing = r.read_bit()?; | 901 | pps.uniform_spacing = r.read_bit()?; |
| 870 | if !pps.uniform_spacing { | 902 | if !pps.uniform_spacing { |
| 903 | + if pps.num_tile_columns_minus1 > 20 || pps.num_tile_rows_minus1 > 22 { | ||
| 904 | + return Err(Error::corrupt("HEVC PPS: tile count beyond any level")); | ||
| 905 | + } | ||
| 871 | for _ in 0..pps.num_tile_columns_minus1 { | 906 | for _ in 0..pps.num_tile_columns_minus1 { |
| 872 | - r.read_ue()?; | 907 | + let value = r.read_ue()?; |
| 908 | + pps.column_width_minus1.push(value); | ||
| 873 | } | 909 | } |
| 874 | for _ in 0..pps.num_tile_rows_minus1 { | 910 | for _ in 0..pps.num_tile_rows_minus1 { |
| 875 | - r.read_ue()?; | 911 | + let value = r.read_ue()?; |
| 912 | + pps.row_height_minus1.push(value); | ||
| 876 | } | 913 | } |
| 877 | } | 914 | } |
| 878 | pps.loop_filter_across_tiles_enabled = r.read_bit()?; | 915 | pps.loop_filter_across_tiles_enabled = r.read_bit()?; |
| @@ -949,6 +986,7 @@ mod tests { | |||
| 949 | amp_enabled: false, | 986 | amp_enabled: false, |
| 950 | sao_enabled: false, | 987 | sao_enabled: false, |
| 951 | pcm_enabled: false, | 988 | pcm_enabled: false, |
| 989 | + pcm: None, | ||
| 952 | num_short_term_ref_pic_sets: 0, | 990 | num_short_term_ref_pic_sets: 0, |
| 953 | long_term_ref_pics_present: false, | 991 | long_term_ref_pics_present: false, |
| 954 | num_long_term_ref_pics_sps: 0, | 992 | num_long_term_ref_pics_sps: 0, |
| @@ -998,6 +1036,32 @@ mod tests { | |||
| 998 | ..Pps::default() | 1036 | ..Pps::default() |
| 999 | }; | 1037 | }; |
| 1000 | assert_eq!(Pps::parse(&pps.to_rbsp()).unwrap(), pps); | 1038 | assert_eq!(Pps::parse(&pps.to_rbsp()).unwrap(), pps); |
| 1039 | + | ||
| 1040 | + // The fields a hardware decoder needs but this family's own encoder | ||
| 1041 | + // never writes: explicit tile geometry and PCM. | ||
| 1042 | + let tiled = Pps { | ||
| 1043 | + tiles_enabled: true, | ||
| 1044 | + num_tile_columns_minus1: 2, | ||
| 1045 | + num_tile_rows_minus1: 1, | ||
| 1046 | + uniform_spacing: false, | ||
| 1047 | + column_width_minus1: vec![9, 19], | ||
| 1048 | + row_height_minus1: vec![7], | ||
| 1049 | + loop_filter_across_tiles_enabled: false, | ||
| 1050 | + cu_qp_delta_enabled: true, | ||
| 1051 | + diff_cu_qp_delta_depth: 2, | ||
| 1052 | + cb_qp_offset: -3, | ||
| 1053 | + cr_qp_offset: 4, | ||
| 1054 | + ..Pps::default() | ||
| 1055 | + }; | ||
| 1056 | + assert_eq!(Pps::parse(&tiled.to_rbsp()).unwrap(), tiled); | ||
| 1057 | + | ||
| 1058 | + let pcm = Sps { | ||
| 1059 | + pcm_enabled: true, | ||
| 1060 | + pcm: Some((7, 7, 0, 2, true)), | ||
| 1061 | + sao_enabled: true, | ||
| 1062 | + ..sample_sps() | ||
| 1063 | + }; | ||
| 1064 | + assert_eq!(Sps::parse(&pcm.to_rbsp()).unwrap(), pcm); | ||
| 1001 | } | 1065 | } |
| 1002 | 1066 | ||
| 1003 | #[test] | 1067 | #[test] |
crates/ec-h265-syntax/src/slice.rsmodified+38 -1
| @@ -57,6 +57,9 @@ pub struct SliceHeader { | |||
| 57 | pub pic_output_flag: bool, | 57 | pub pic_output_flag: bool, |
| 58 | /// `slice_pic_order_cnt_lsb`; absent (0) on IDR pictures. | 58 | /// `slice_pic_order_cnt_lsb`; absent (0) on IDR pictures. |
| 59 | pub poc_lsb: u32, | 59 | pub poc_lsb: u32, |
| 60 | + /// `colour_plane_id`, present only when the sequence codes its three | ||
| 61 | + /// planes as separate pictures. | ||
| 62 | + pub colour_plane_id: u8, | ||
| 60 | /// `slice_sao_luma_flag`. | 63 | /// `slice_sao_luma_flag`. |
| 61 | pub sao_luma: bool, | 64 | pub sao_luma: bool, |
| 62 | /// `slice_sao_chroma_flag`. | 65 | /// `slice_sao_chroma_flag`. |
| @@ -107,6 +110,7 @@ impl SliceHeader { | |||
| 107 | slice_type: SliceType::I, | 110 | slice_type: SliceType::I, |
| 108 | pic_output_flag: true, | 111 | pic_output_flag: true, |
| 109 | poc_lsb: 0, | 112 | poc_lsb: 0, |
| 113 | + colour_plane_id: 0, | ||
| 110 | sao_luma: false, | 114 | sao_luma: false, |
| 111 | sao_chroma: false, | 115 | sao_chroma: false, |
| 112 | num_ref_idx_l0_active_minus1: 0, | 116 | num_ref_idx_l0_active_minus1: 0, |
| @@ -244,7 +248,7 @@ impl SliceHeader { | |||
| 244 | h.pic_output_flag = r.read_bit()?; | 248 | h.pic_output_flag = r.read_bit()?; |
| 245 | } | 249 | } |
| 246 | if sps.separate_colour_plane { | 250 | if sps.separate_colour_plane { |
| 247 | - r.read_bits(2)?; // colour_plane_id | 251 | + h.colour_plane_id = r.read_bits(2)? as u8; |
| 248 | } | 252 | } |
| 249 | let mut st_rps = ShortTermRefPicSet::default(); | 253 | let mut st_rps = ShortTermRefPicSet::default(); |
| 250 | let mut num_long_term = 0; | 254 | let mut num_long_term = 0; |
| @@ -437,6 +441,29 @@ impl ParsePositions { | |||
| 437 | } | 441 | } |
| 438 | } | 442 | } |
| 439 | 443 | ||
| 444 | +/// How many emulation prevention bytes a NAL unit payload carries. | ||
| 445 | +/// | ||
| 446 | +/// `VASliceParameterBufferHEVC::slice_data_num_emu_prevn_bytes` wants this for | ||
| 447 | +/// the slice data a driver is handed, and a caller that has the escaped bytes | ||
| 448 | +/// should not have to unescape them twice to find out. | ||
| 449 | +pub fn count_emulation_prevention_bytes(escaped_payload: &[u8]) -> usize { | ||
| 450 | + let mut zeros = 0usize; | ||
| 451 | + let mut count = 0usize; | ||
| 452 | + for &b in escaped_payload { | ||
| 453 | + if zeros >= 2 && b == 3 { | ||
| 454 | + count += 1; | ||
| 455 | + zeros = 0; | ||
| 456 | + continue; | ||
| 457 | + } | ||
| 458 | + if b == 0 { | ||
| 459 | + zeros += 1; | ||
| 460 | + } else { | ||
| 461 | + zeros = 0; | ||
| 462 | + } | ||
| 463 | + } | ||
| 464 | + count | ||
| 465 | +} | ||
| 466 | + | ||
| 440 | /// Walk past a `pred_weight_table()` (7.3.6.3). | 467 | /// Walk past a `pred_weight_table()` (7.3.6.3). |
| 441 | fn skip_pred_weight_table(r: &mut BitReader, h: &SliceHeader, sps: &Sps) -> Result<()> { | 468 | fn skip_pred_weight_table(r: &mut BitReader, h: &SliceHeader, sps: &Sps) -> Result<()> { |
| 442 | r.read_ue()?; // luma_log2_weight_denom | 469 | r.read_ue()?; // luma_log2_weight_denom |
| @@ -509,6 +536,7 @@ mod tests { | |||
| 509 | amp_enabled: false, | 536 | amp_enabled: false, |
| 510 | sao_enabled: false, | 537 | sao_enabled: false, |
| 511 | pcm_enabled: false, | 538 | pcm_enabled: false, |
| 539 | + pcm: None, | ||
| 512 | num_short_term_ref_pic_sets: 0, | 540 | num_short_term_ref_pic_sets: 0, |
| 513 | long_term_ref_pics_present: false, | 541 | long_term_ref_pics_present: false, |
| 514 | num_long_term_ref_pics_sps: 0, | 542 | num_long_term_ref_pics_sps: 0, |
| @@ -542,6 +570,15 @@ mod tests { | |||
| 542 | assert_eq!(pos.slice_data_byte_offset(&rbsp), 2 + rbsp.len()); | 570 | assert_eq!(pos.slice_data_byte_offset(&rbsp), 2 + rbsp.len()); |
| 543 | } | 571 | } |
| 544 | 572 | ||
| 573 | + #[test] | ||
| 574 | + fn emulation_prevention_bytes_are_counted() { | ||
| 575 | + assert_eq!(count_emulation_prevention_bytes(&[0, 0, 3, 1, 0xff]), 1); | ||
| 576 | + assert_eq!(count_emulation_prevention_bytes(&[0, 0, 3, 0, 0, 3, 2]), 2); | ||
| 577 | + // A 3 that is not preceded by two zeros is payload, not an escape. | ||
| 578 | + assert_eq!(count_emulation_prevention_bytes(&[0, 3, 3, 3]), 0); | ||
| 579 | + assert_eq!(count_emulation_prevention_bytes(&[]), 0); | ||
| 580 | + } | ||
| 581 | + | ||
| 545 | #[test] | 582 | #[test] |
| 546 | fn byte_offset_counts_escape_bytes() { | 583 | fn byte_offset_counts_escape_bytes() { |
| 547 | // RBSP 00 00 01 ... escapes to 00 00 03 01 ...; a header of four RBSP | 584 | // RBSP 00 00 01 ... escapes to 00 00 03 01 ...; a header of four RBSP |
crates/ec-h265-syntax/tests/fuzz.rsmodified+1 -0
| @@ -63,6 +63,7 @@ fn reference_sps() -> Sps { | |||
| 63 | amp_enabled: false, | 63 | amp_enabled: false, |
| 64 | sao_enabled: true, | 64 | sao_enabled: true, |
| 65 | pcm_enabled: false, | 65 | pcm_enabled: false, |
| 66 | + pcm: None, | ||
| 66 | num_short_term_ref_pic_sets: 0, | 67 | num_short_term_ref_pic_sets: 0, |
| 67 | long_term_ref_pics_present: false, | 68 | long_term_ref_pics_present: false, |
| 68 | num_long_term_ref_pics_sps: 0, | 69 | num_long_term_ref_pics_sps: 0, |
crates/ec-h265/src/encoder.rsmodified+1 -0
| @@ -197,6 +197,7 @@ impl Encoder { | |||
| 197 | // No in-loop filters: see `Encoder` docs and the PPS below. | 197 | // No in-loop filters: see `Encoder` docs and the PPS below. |
| 198 | sao_enabled: false, | 198 | sao_enabled: false, |
| 199 | pcm_enabled: false, | 199 | pcm_enabled: false, |
| 200 | + pcm: None, | ||
| 200 | num_short_term_ref_pic_sets: 0, | 201 | num_short_term_ref_pic_sets: 0, |
| 201 | long_term_ref_pics_present: false, | 202 | long_term_ref_pics_present: false, |
| 202 | num_long_term_ref_pics_sps: 0, | 203 | num_long_term_ref_pics_sps: 0, |