edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·6828b15·Tahinli·3 months ago·393 commits
| 1 | //! Picture parameter set (spec 7.3.2.2, 7.4.2.2). |
| 2 | |
| 3 | use ec_core::BitReader; |
| 4 | use ec_core::error::{Error, Result}; |
| 5 | |
| 6 | use crate::more_rbsp_data; |
| 7 | use crate::sps::{ScalingLists, Sps, parse_scaling_matrix}; |
| 8 | |
| 9 | /// Slice-group (FMO) map data, parsed but not interpreted here. |
| 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | pub enum SliceGroupMap { |
| 12 | /// Type 0: `run_length_minus1` per group. |
| 13 | Interleaved(Vec<u32>), |
| 14 | /// Type 1: dispersed, no extra data. |
| 15 | Dispersed, |
| 16 | /// Type 2: `(top_left, bottom_right)` per group but the last. |
| 17 | Foreground(Vec<(u32, u32)>), |
| 18 | /// Types 3..=5: `(change_direction_flag, change_rate_minus1)`. |
| 19 | Changing { |
| 20 | /// `slice_group_map_type` (3, 4 or 5). |
| 21 | map_type: u8, |
| 22 | /// `slice_group_change_direction_flag`. |
| 23 | change_direction: bool, |
| 24 | /// `slice_group_change_rate_minus1`. |
| 25 | change_rate_minus1: u32, |
| 26 | }, |
| 27 | /// Type 6: explicit per-map-unit assignment. |
| 28 | Explicit(Vec<u32>), |
| 29 | } |
| 30 | |
| 31 | /// Picture parameter set with decode-ready fields. |
| 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 33 | pub struct Pps { |
| 34 | /// `pic_parameter_set_id` (0..=255). |
| 35 | pub id: u32, |
| 36 | /// `seq_parameter_set_id` of the SPS this PPS refers to. |
| 37 | pub sps_id: u8, |
| 38 | /// `entropy_coding_mode_flag`: false = CAVLC, true = CABAC. |
| 39 | pub entropy_coding_mode: bool, |
| 40 | /// `bottom_field_pic_order_in_frame_present_flag`. |
| 41 | pub bottom_field_pic_order_in_frame_present: bool, |
| 42 | /// `num_slice_groups_minus1` + 1. |
| 43 | pub num_slice_groups: u32, |
| 44 | /// FMO map when `num_slice_groups > 1`. |
| 45 | pub slice_group_map: Option<SliceGroupMap>, |
| 46 | /// `num_ref_idx_l0_default_active_minus1` + 1. |
| 47 | pub num_ref_idx_l0_default_active: u32, |
| 48 | /// `num_ref_idx_l1_default_active_minus1` + 1. |
| 49 | pub num_ref_idx_l1_default_active: u32, |
| 50 | /// `weighted_pred_flag`. |
| 51 | pub weighted_pred: bool, |
| 52 | /// `weighted_bipred_idc` (0..=2). |
| 53 | pub weighted_bipred_idc: u8, |
| 54 | /// `pic_init_qp_minus26` + 26. |
| 55 | pub pic_init_qp: i32, |
| 56 | /// `pic_init_qs_minus26` + 26. |
| 57 | pub pic_init_qs: i32, |
| 58 | /// `chroma_qp_index_offset` (-12..=12), for Cb. |
| 59 | pub chroma_qp_index_offset: i32, |
| 60 | /// `deblocking_filter_control_present_flag`. |
| 61 | pub deblocking_filter_control_present: bool, |
| 62 | /// `constrained_intra_pred_flag`. |
| 63 | pub constrained_intra_pred: bool, |
| 64 | /// `redundant_pic_cnt_present_flag`. |
| 65 | pub redundant_pic_cnt_present: bool, |
| 66 | /// `transform_8x8_mode_flag` (High-profile tail; false when absent). |
| 67 | pub transform_8x8_mode: bool, |
| 68 | /// PPS scaling lists when `pic_scaling_matrix_present_flag`, resolved |
| 69 | /// against the SPS lists per fall-back rule B. |
| 70 | pub scaling_lists: Option<ScalingLists>, |
| 71 | /// `second_chroma_qp_index_offset` for Cr (defaults to the Cb offset). |
| 72 | pub second_chroma_qp_index_offset: i32, |
| 73 | } |
| 74 | |
| 75 | impl Pps { |
| 76 | /// Parse a PPS RBSP (header byte consumed, emulation stripped). |
| 77 | /// |
| 78 | /// `sps_lookup` resolves the referenced SPS — needed for the High-profile |
| 79 | /// tail (scaling-list fall-back and 4:4:4 list count). |
| 80 | pub fn parse<'s>(rbsp: &[u8], sps_lookup: impl Fn(u8) -> Option<&'s Sps>) -> Result<Pps> { |
| 81 | let mut reader = BitReader::new(rbsp); |
| 82 | let r = &mut reader; |
| 83 | let id = r.read_ue()?; |
| 84 | if id > 255 { |
| 85 | return Err(Error::corrupt("pic_parameter_set_id > 255")); |
| 86 | } |
| 87 | let sps_id = r.read_ue()?; |
| 88 | if sps_id > 31 { |
| 89 | return Err(Error::corrupt("seq_parameter_set_id > 31")); |
| 90 | } |
| 91 | let sps_id = sps_id as u8; |
| 92 | let entropy_coding_mode = r.read_bit()?; |
| 93 | let bottom_field_pic_order_in_frame_present = r.read_bit()?; |
| 94 | let num_slice_groups = r.read_ue()?.checked_add(1).ok_or(Error::NeedMore)?; |
| 95 | if num_slice_groups > 8 { |
| 96 | return Err(Error::corrupt("num_slice_groups > 8")); |
| 97 | } |
| 98 | let slice_group_map = if num_slice_groups > 1 { |
| 99 | Some(Self::parse_slice_groups(r, num_slice_groups)?) |
| 100 | } else { |
| 101 | None |
| 102 | }; |
| 103 | let num_ref_idx_l0_default_active = r.read_ue()? + 1; |
| 104 | let num_ref_idx_l1_default_active = r.read_ue()? + 1; |
| 105 | if num_ref_idx_l0_default_active > 32 || num_ref_idx_l1_default_active > 32 { |
| 106 | return Err(Error::corrupt("num_ref_idx_default_active > 32")); |
| 107 | } |
| 108 | let weighted_pred = r.read_bit()?; |
| 109 | let weighted_bipred_idc = r.read_bits(2)? as u8; |
| 110 | let pic_init_qp = 26 + r.read_se()?; |
| 111 | let pic_init_qs = 26 + r.read_se()?; |
| 112 | let chroma_qp_index_offset = r.read_se()?; |
| 113 | if !(-12..=12).contains(&chroma_qp_index_offset) { |
| 114 | return Err(Error::corrupt("chroma_qp_index_offset outside [-12, 12]")); |
| 115 | } |
| 116 | let deblocking_filter_control_present = r.read_bit()?; |
| 117 | let constrained_intra_pred = r.read_bit()?; |
| 118 | let redundant_pic_cnt_present = r.read_bit()?; |
| 119 | |
| 120 | let mut transform_8x8_mode = false; |
| 121 | let mut scaling_lists = None; |
| 122 | let mut second_chroma_qp_index_offset = chroma_qp_index_offset; |
| 123 | if more_rbsp_data(r, rbsp) { |
| 124 | transform_8x8_mode = r.read_bit()?; |
| 125 | if r.read_bit()? { |
| 126 | // pic_scaling_matrix_present_flag |
| 127 | let sps = sps_lookup(sps_id); |
| 128 | let chroma_444 = sps.map(|s| s.chroma_format_idc == 3).unwrap_or(false); |
| 129 | let count = 6 + if chroma_444 { 6 } else { 2 } * usize::from(transform_8x8_mode); |
| 130 | scaling_lists = Some(parse_scaling_matrix( |
| 131 | r, |
| 132 | count, |
| 133 | sps.and_then(|s| s.scaling_lists.as_ref()), |
| 134 | )?); |
| 135 | } |
| 136 | second_chroma_qp_index_offset = r.read_se()?; |
| 137 | if !(-12..=12).contains(&second_chroma_qp_index_offset) { |
| 138 | return Err(Error::corrupt( |
| 139 | "second_chroma_qp_index_offset outside [-12, 12]", |
| 140 | )); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | Ok(Pps { |
| 145 | id, |
| 146 | sps_id, |
| 147 | entropy_coding_mode, |
| 148 | bottom_field_pic_order_in_frame_present, |
| 149 | num_slice_groups, |
| 150 | slice_group_map, |
| 151 | num_ref_idx_l0_default_active, |
| 152 | num_ref_idx_l1_default_active, |
| 153 | weighted_pred, |
| 154 | weighted_bipred_idc, |
| 155 | pic_init_qp, |
| 156 | pic_init_qs, |
| 157 | chroma_qp_index_offset, |
| 158 | deblocking_filter_control_present, |
| 159 | constrained_intra_pred, |
| 160 | redundant_pic_cnt_present, |
| 161 | transform_8x8_mode, |
| 162 | scaling_lists, |
| 163 | second_chroma_qp_index_offset, |
| 164 | }) |
| 165 | } |
| 166 | |
| 167 | fn parse_slice_groups(r: &mut BitReader<'_>, groups: u32) -> Result<SliceGroupMap> { |
| 168 | let map_type = r.read_ue()?; |
| 169 | Ok(match map_type { |
| 170 | 0 => { |
| 171 | let mut runs = Vec::with_capacity(groups as usize); |
| 172 | for _ in 0..groups { |
| 173 | runs.push(r.read_ue()?); |
| 174 | } |
| 175 | SliceGroupMap::Interleaved(runs) |
| 176 | } |
| 177 | 1 => SliceGroupMap::Dispersed, |
| 178 | 2 => { |
| 179 | let mut rects = Vec::with_capacity(groups as usize - 1); |
| 180 | for _ in 0..groups - 1 { |
| 181 | rects.push((r.read_ue()?, r.read_ue()?)); |
| 182 | } |
| 183 | SliceGroupMap::Foreground(rects) |
| 184 | } |
| 185 | 3..=5 => SliceGroupMap::Changing { |
| 186 | map_type: map_type as u8, |
| 187 | change_direction: r.read_bit()?, |
| 188 | change_rate_minus1: r.read_ue()?, |
| 189 | }, |
| 190 | 6 => { |
| 191 | let count = r.read_ue()?.checked_add(1).ok_or(Error::NeedMore)?; |
| 192 | if count > 1 << 20 { |
| 193 | return Err(Error::corrupt("slice group map unit count implausible")); |
| 194 | } |
| 195 | let bits = 32 - (groups - 1).leading_zeros().min(31); |
| 196 | let bits = if groups > 1 { bits.max(1) } else { 1 }; |
| 197 | let mut ids = Vec::with_capacity(count as usize); |
| 198 | for _ in 0..count { |
| 199 | ids.push(r.read_bits(bits)?); |
| 200 | } |
| 201 | SliceGroupMap::Explicit(ids) |
| 202 | } |
| 203 | _ => return Err(Error::corrupt("slice_group_map_type > 6")), |
| 204 | }) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #[cfg(test)] |
| 209 | mod tests { |
| 210 | use super::*; |
| 211 | use ec_core::BitWriter; |
| 212 | |
| 213 | fn write_baseline_pps(w: &mut BitWriter) { |
| 214 | w.write_ue(0); // pps id |
| 215 | w.write_ue(0); // sps id |
| 216 | w.write_bit(false); // CAVLC |
| 217 | w.write_bit(false); // bottom_field_pic_order |
| 218 | w.write_ue(0); // one slice group |
| 219 | w.write_ue(0); // l0 default |
| 220 | w.write_ue(0); // l1 default |
| 221 | w.write_bit(false); // weighted_pred |
| 222 | w.write_bits(0, 2); // weighted_bipred_idc |
| 223 | w.write_se(0); // pic_init_qp = 26 |
| 224 | w.write_se(0); // pic_init_qs |
| 225 | w.write_se(2); // chroma_qp_index_offset |
| 226 | w.write_bit(true); // deblocking control present |
| 227 | w.write_bit(false); // constrained intra |
| 228 | w.write_bit(false); // redundant pic cnt |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn baseline_pps_without_tail() { |
| 233 | let mut w = BitWriter::new(); |
| 234 | write_baseline_pps(&mut w); |
| 235 | w.write_bit(true); // rbsp_stop_one_bit |
| 236 | w.align_to_byte(); |
| 237 | let pps = Pps::parse(w.as_bytes(), |_| None).unwrap(); |
| 238 | assert_eq!(pps.id, 0); |
| 239 | assert!(!pps.entropy_coding_mode); |
| 240 | assert_eq!(pps.pic_init_qp, 26); |
| 241 | assert_eq!(pps.chroma_qp_index_offset, 2); |
| 242 | assert_eq!(pps.second_chroma_qp_index_offset, 2); // inherits |
| 243 | assert!(!pps.transform_8x8_mode); |
| 244 | assert!(pps.deblocking_filter_control_present); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn high_profile_tail_parsed() { |
| 249 | let mut w = BitWriter::new(); |
| 250 | write_baseline_pps(&mut w); |
| 251 | w.write_bit(true); // transform_8x8_mode |
| 252 | w.write_bit(false); // no pic scaling matrix |
| 253 | w.write_se(-3); // second_chroma_qp_index_offset |
| 254 | w.write_bit(true); // stop bit |
| 255 | w.align_to_byte(); |
| 256 | let pps = Pps::parse(w.as_bytes(), |_| None).unwrap(); |
| 257 | assert!(pps.transform_8x8_mode); |
| 258 | assert_eq!(pps.second_chroma_qp_index_offset, -3); |
| 259 | assert_eq!(pps.chroma_qp_index_offset, 2); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn truncated_pps_is_need_more() { |
| 264 | assert!(Pps::parse(&[], |_| None).unwrap_err().is_need_more()); |
| 265 | } |
| 266 | } |