| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use super::mc::PlaneMut; |
| 15 | use super::tiles::TileScan; |
| 16 | use crate::cabac::Contexts; |
| 17 | use crate::decode::DecodedPicture; |
| 18 | use ec_h265_syntax::{Pps, Sps}; |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub const EDGE_TU: u8 = 1; |
| 23 | |
| 24 | |
| 25 | pub const EDGE_PU: u8 = 2; |
| 26 | |
| 27 | |
| 28 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 29 | pub struct BlockInfo { |
| 30 | |
| 31 | |
| 32 | pub intra: bool, |
| 33 | |
| 34 | pub skip: bool, |
| 35 | |
| 36 | pub pcm: bool, |
| 37 | |
| 38 | pub bypass: bool, |
| 39 | |
| 40 | pub depth: u8, |
| 41 | |
| 42 | pub intra_mode: u8, |
| 43 | |
| 44 | pub qp: i8, |
| 45 | |
| 46 | pub nz_luma: bool, |
| 47 | |
| 48 | |
| 49 | pub edge_v: u8, |
| 50 | |
| 51 | pub edge_h: u8, |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 58 | pub struct PuMotion { |
| 59 | |
| 60 | pub pred: [bool; 2], |
| 61 | |
| 62 | |
| 63 | pub ref_pic: [i32; 2], |
| 64 | |
| 65 | pub mv: [[i32; 2]; 2], |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 71 | pub struct SaoParams { |
| 72 | |
| 73 | pub type_idx: [u8; 3], |
| 74 | |
| 75 | pub band_position: [u8; 3], |
| 76 | |
| 77 | pub eo_class: [u8; 3], |
| 78 | |
| 79 | |
| 80 | pub offset: [[i16; 4]; 3], |
| 81 | } |
| 82 | |
| 83 | |
| 84 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 85 | pub struct CtbInfo { |
| 86 | |
| 87 | pub slice: u16, |
| 88 | |
| 89 | |
| 90 | pub slice_addr: u32, |
| 91 | |
| 92 | pub tile: u16, |
| 93 | |
| 94 | pub sao: SaoParams, |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 100 | pub struct SliceFilter { |
| 101 | |
| 102 | pub deblock_disabled: bool, |
| 103 | |
| 104 | pub beta_offset_div2: i32, |
| 105 | |
| 106 | pub tc_offset_div2: i32, |
| 107 | |
| 108 | pub loop_filter_across_slices: bool, |
| 109 | |
| 110 | pub sao_luma: bool, |
| 111 | |
| 112 | pub sao_chroma: bool, |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | #[derive(Default)] |
| 118 | pub struct SliceCarry { |
| 119 | |
| 120 | pub slice_addr: usize, |
| 121 | |
| 122 | |
| 123 | pub wpp: Option<Contexts>, |
| 124 | |
| 125 | pub ds: Option<Contexts>, |
| 126 | |
| 127 | pub last_qp: i32, |
| 128 | } |
| 129 | |
| 130 | |
| 131 | pub struct PicState { |
| 132 | |
| 133 | pub width: usize, |
| 134 | |
| 135 | pub chroma_format_idc: u32, |
| 136 | |
| 137 | pub height: usize, |
| 138 | |
| 139 | pub min_tb_log2: u32, |
| 140 | |
| 141 | pub ctb_log2: u32, |
| 142 | |
| 143 | pub ctb_cols: usize, |
| 144 | |
| 145 | pub ctb_rows: usize, |
| 146 | |
| 147 | pub w4: usize, |
| 148 | |
| 149 | pub h4: usize, |
| 150 | |
| 151 | pub bit_depth_y: u32, |
| 152 | |
| 153 | pub bit_depth_c: u32, |
| 154 | |
| 155 | pub constrained_intra_pred: bool, |
| 156 | |
| 157 | pub strong_intra_smoothing: bool, |
| 158 | |
| 159 | pub intra_smoothing_disabled: bool, |
| 160 | |
| 161 | pub pcm_loop_filter_disabled: bool, |
| 162 | |
| 163 | pub pps_cb_qp_offset: i32, |
| 164 | |
| 165 | pub pps_cr_qp_offset: i32, |
| 166 | |
| 167 | pub planes: DecodedPicture, |
| 168 | |
| 169 | pub info: Vec<BlockInfo>, |
| 170 | |
| 171 | pub motion: Vec<PuMotion>, |
| 172 | |
| 173 | pub ctb: Vec<CtbInfo>, |
| 174 | |
| 175 | pub slices: Vec<SliceFilter>, |
| 176 | |
| 177 | pub tiles: TileScan, |
| 178 | |
| 179 | pub loop_filter_across_tiles: bool, |
| 180 | |
| 181 | pub carry: SliceCarry, |
| 182 | |
| 183 | |
| 184 | pub origin_y: usize, |
| 185 | |
| 186 | pub origin_c: usize, |
| 187 | |
| 188 | pub origin_unit: usize, |
| 189 | } |
| 190 | |
| 191 | impl PicState { |
| 192 | |
| 193 | |
| 194 | #[allow(clippy::too_many_arguments)] |
| 195 | pub fn new( |
| 196 | width: usize, |
| 197 | height: usize, |
| 198 | ctb_log2: u32, |
| 199 | min_tb_log2: u32, |
| 200 | bit_depth_y: u32, |
| 201 | bit_depth_c: u32, |
| 202 | pixels: bool, |
| 203 | ) -> PicState { |
| 204 | let ctb = 1usize << ctb_log2; |
| 205 | let ctb_cols = width.div_ceil(ctb); |
| 206 | let ctb_rows = height.div_ceil(ctb); |
| 207 | let w4 = width.div_ceil(4); |
| 208 | let h4 = height.div_ceil(4); |
| 209 | let planes = if pixels { |
| 210 | DecodedPicture::grey_planes(width, height, bit_depth_y, bit_depth_c) |
| 211 | } else { |
| 212 | DecodedPicture { |
| 213 | width, |
| 214 | height, |
| 215 | bit_depth: bit_depth_y, |
| 216 | bit_depth_c, |
| 217 | chroma_format_idc: 1, |
| 218 | y: Vec::new(), |
| 219 | cb: Vec::new(), |
| 220 | cr: Vec::new(), |
| 221 | } |
| 222 | }; |
| 223 | PicState { |
| 224 | width, |
| 225 | height, |
| 226 | chroma_format_idc: 1, |
| 227 | min_tb_log2, |
| 228 | ctb_log2, |
| 229 | ctb_cols, |
| 230 | ctb_rows, |
| 231 | w4, |
| 232 | h4, |
| 233 | bit_depth_y, |
| 234 | bit_depth_c, |
| 235 | constrained_intra_pred: false, |
| 236 | strong_intra_smoothing: false, |
| 237 | intra_smoothing_disabled: false, |
| 238 | pcm_loop_filter_disabled: false, |
| 239 | pps_cb_qp_offset: 0, |
| 240 | pps_cr_qp_offset: 0, |
| 241 | planes, |
| 242 | info: vec![BlockInfo::default(); w4 * h4], |
| 243 | motion: vec![PuMotion::default(); w4 * h4], |
| 244 | ctb: vec![CtbInfo::default(); ctb_cols * ctb_rows], |
| 245 | slices: Vec::new(), |
| 246 | tiles: TileScan::single(ctb_cols, ctb_rows), |
| 247 | loop_filter_across_tiles: true, |
| 248 | carry: SliceCarry::default(), |
| 249 | origin_y: 0, |
| 250 | origin_c: 0, |
| 251 | origin_unit: 0, |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | pub fn sub_x(&self) -> u32 { |
| 257 | crate::decode::chroma_shifts(self.chroma_format_idc).0 |
| 258 | } |
| 259 | |
| 260 | |
| 261 | pub fn sub_y(&self) -> u32 { |
| 262 | crate::decode::chroma_shifts(self.chroma_format_idc).1 |
| 263 | } |
| 264 | |
| 265 | |
| 266 | pub fn has_pixels(&self) -> bool { |
| 267 | !self.planes.y.is_empty() |
| 268 | } |
| 269 | |
| 270 | |
| 271 | pub fn planes_mut(&mut self) -> [PlaneMut<'_>; 3] { |
| 272 | let (bd_y, bd_c) = (self.bit_depth_y, self.bit_depth_c); |
| 273 | let w = self.planes.width; |
| 274 | let cw = self.width.div_ceil(1 << self.sub_x()); |
| 275 | let (oy, oc) = (self.origin_y, self.origin_c); |
| 276 | let p = &mut self.planes; |
| 277 | [ |
| 278 | PlaneMut { |
| 279 | samples: &mut p.y, |
| 280 | stride: w, |
| 281 | bit_depth: bd_y, |
| 282 | origin: oy, |
| 283 | }, |
| 284 | PlaneMut { |
| 285 | samples: &mut p.cb, |
| 286 | stride: cw, |
| 287 | bit_depth: bd_c, |
| 288 | origin: oc, |
| 289 | }, |
| 290 | PlaneMut { |
| 291 | samples: &mut p.cr, |
| 292 | stride: cw, |
| 293 | bit_depth: bd_c, |
| 294 | origin: oc, |
| 295 | }, |
| 296 | ] |
| 297 | } |
| 298 | |
| 299 | |
| 300 | pub fn plane_origin(&self, c: usize) -> usize { |
| 301 | if c == 0 { self.origin_y } else { self.origin_c } |
| 302 | } |
| 303 | |
| 304 | |
| 305 | pub fn unit(&self, x: usize, y: usize) -> usize { |
| 306 | ((y >> 2) - self.origin_unit / 4) * self.w4 + (x >> 2) |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | pub fn zscan(&self, x: usize, y: usize) -> usize { |
| 313 | let rs = (y >> self.ctb_log2) * self.ctb_cols + (x >> self.ctb_log2); |
| 314 | let ts = self.tiles.rs_to_ts.get(rs).copied().unwrap_or(rs); |
| 315 | let units_per_ctb_side = 1usize << (self.ctb_log2 - 2); |
| 316 | let mask = units_per_ctb_side - 1; |
| 317 | let (ux, uy) = ((x >> 2) & mask, (y >> 2) & mask); |
| 318 | let mut z = 0usize; |
| 319 | for bit in 0..self.ctb_log2 as usize - 2 { |
| 320 | z |= ((ux >> bit) & 1) << (2 * bit); |
| 321 | z |= ((uy >> bit) & 1) << (2 * bit + 1); |
| 322 | } |
| 323 | (ts << (2 * (self.ctb_log2 as usize - 2))) | z |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | pub fn available(&self, xc: usize, yc: usize, xn: isize, yn: isize) -> bool { |
| 330 | if xn < 0 || yn < 0 || xn as usize >= self.width || yn as usize >= self.height { |
| 331 | return false; |
| 332 | } |
| 333 | let (xn, yn) = (xn as usize, yn as usize); |
| 334 | if self.zscan(xn, yn) > self.zscan(xc, yc) { |
| 335 | return false; |
| 336 | } |
| 337 | let rs_n = (yn >> self.ctb_log2) * self.ctb_cols + (xn >> self.ctb_log2); |
| 338 | let rs_c = (yc >> self.ctb_log2) * self.ctb_cols + (xc >> self.ctb_log2); |
| 339 | if rs_n == rs_c { |
| 340 | return true; |
| 341 | } |
| 342 | match (self.ctb.get(rs_n), self.ctb.get(rs_c)) { |
| 343 | (Some(n), Some(c)) => n.slice_addr == c.slice_addr && n.tile == c.tile, |
| 344 | _ => false, |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | pub fn ctb_rs(&self, x: usize, y: usize) -> usize { |
| 350 | (y >> self.ctb_log2) * self.ctb_cols + (x >> self.ctb_log2) |
| 351 | } |
| 352 | |
| 353 | |
| 354 | pub fn update_info( |
| 355 | &mut self, |
| 356 | x: usize, |
| 357 | y: usize, |
| 358 | w: usize, |
| 359 | h: usize, |
| 360 | f: impl Fn(&mut BlockInfo), |
| 361 | ) { |
| 362 | for by in (y..(y + h).min(self.height)).step_by(4) { |
| 363 | for bx in (x..(x + w).min(self.width)).step_by(4) { |
| 364 | let idx = self.unit(bx, by); |
| 365 | f(&mut self.info[idx]); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
| 371 | |
| 372 | pub fn mark_edges(&mut self, x: usize, y: usize, w: usize, h: usize, kind: u8) { |
| 373 | for by in (y..(y + h).min(self.height)).step_by(4) { |
| 374 | let idx = self.unit(x, by); |
| 375 | if x < self.width { |
| 376 | self.info[idx].edge_v |= kind; |
| 377 | } |
| 378 | } |
| 379 | for bx in (x..(x + w).min(self.width)).step_by(4) { |
| 380 | let idx = self.unit(bx, y); |
| 381 | if y < self.height { |
| 382 | self.info[idx].edge_h |= kind; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
| 389 | pub fn for_sps_pps(sps: &Sps, pps: &Pps, pixels: bool) -> PicState { |
| 390 | let min_cb_log2 = sps.log2_min_cb_size_minus3 + 3; |
| 391 | let ctb_log2 = min_cb_log2 + sps.log2_diff_max_min_cb_size; |
| 392 | let min_tb_log2 = sps.log2_min_tb_size_minus2 + 2; |
| 393 | let mut pic = PicState::new( |
| 394 | sps.pic_width_in_luma_samples as usize, |
| 395 | sps.pic_height_in_luma_samples as usize, |
| 396 | ctb_log2, |
| 397 | min_tb_log2, |
| 398 | 8 + sps.bit_depth_luma_minus8, |
| 399 | 8 + sps.bit_depth_chroma_minus8, |
| 400 | pixels, |
| 401 | ); |
| 402 | if sps.chroma_format_idc != 1 { |
| 403 | pic.chroma_format_idc = sps.chroma_format_idc; |
| 404 | if pixels { |
| 405 | pic.planes = DecodedPicture::grey_planes_fmt( |
| 406 | pic.width, |
| 407 | pic.height, |
| 408 | pic.bit_depth_y, |
| 409 | pic.bit_depth_c, |
| 410 | sps.chroma_format_idc, |
| 411 | ); |
| 412 | } |
| 413 | } |
| 414 | pic.constrained_intra_pred = pps.constrained_intra_pred; |
| 415 | pic.strong_intra_smoothing = sps.strong_intra_smoothing; |
| 416 | pic.intra_smoothing_disabled = sps.range.intra_smoothing_disabled; |
| 417 | pic.pcm_loop_filter_disabled = sps.pcm_enabled && sps.pcm.is_some_and(|p| p.4); |
| 418 | pic.pps_cb_qp_offset = pps.cb_qp_offset; |
| 419 | pic.pps_cr_qp_offset = pps.cr_qp_offset; |
| 420 | pic.loop_filter_across_tiles = pps.loop_filter_across_tiles_enabled; |
| 421 | pic |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | pub fn band(&self, y0: usize, own: usize, pixels: bool) -> PicState { |
| 430 | let bound = usize::from(y0 > 0); |
| 431 | let own_units = own.div_ceil(4); |
| 432 | let (sx, sy) = crate::decode::chroma_shifts(self.chroma_format_idc); |
| 433 | let cw = self.width.div_ceil(1 << sx); |
| 434 | let own_c = own.div_ceil(1 << sy); |
| 435 | let planes = DecodedPicture { |
| 436 | width: self.width, |
| 437 | height: self.height, |
| 438 | bit_depth: self.bit_depth_y, |
| 439 | bit_depth_c: self.bit_depth_c, |
| 440 | chroma_format_idc: self.chroma_format_idc, |
| 441 | y: if pixels { |
| 442 | vec![0; (bound + own) * self.width] |
| 443 | } else { |
| 444 | Vec::new() |
| 445 | }, |
| 446 | cb: if pixels { |
| 447 | vec![0; (bound + own_c) * cw] |
| 448 | } else { |
| 449 | Vec::new() |
| 450 | }, |
| 451 | cr: if pixels { |
| 452 | vec![0; (bound + own_c) * cw] |
| 453 | } else { |
| 454 | Vec::new() |
| 455 | }, |
| 456 | }; |
| 457 | PicState { |
| 458 | width: self.width, |
| 459 | height: self.height, |
| 460 | chroma_format_idc: self.chroma_format_idc, |
| 461 | min_tb_log2: self.min_tb_log2, |
| 462 | ctb_log2: self.ctb_log2, |
| 463 | ctb_cols: self.ctb_cols, |
| 464 | ctb_rows: self.ctb_rows, |
| 465 | w4: self.w4, |
| 466 | h4: self.h4, |
| 467 | bit_depth_y: self.bit_depth_y, |
| 468 | bit_depth_c: self.bit_depth_c, |
| 469 | constrained_intra_pred: self.constrained_intra_pred, |
| 470 | strong_intra_smoothing: self.strong_intra_smoothing, |
| 471 | intra_smoothing_disabled: self.intra_smoothing_disabled, |
| 472 | pcm_loop_filter_disabled: self.pcm_loop_filter_disabled, |
| 473 | pps_cb_qp_offset: self.pps_cb_qp_offset, |
| 474 | pps_cr_qp_offset: self.pps_cr_qp_offset, |
| 475 | planes, |
| 476 | info: vec![BlockInfo::default(); (bound + own_units) * self.w4], |
| 477 | motion: Vec::new(), |
| 478 | ctb: vec![CtbInfo::default(); self.ctb.len()], |
| 479 | slices: Vec::new(), |
| 480 | tiles: self.tiles.clone(), |
| 481 | loop_filter_across_tiles: self.loop_filter_across_tiles, |
| 482 | carry: SliceCarry { |
| 483 | slice_addr: self.carry.slice_addr, |
| 484 | ..SliceCarry::default() |
| 485 | }, |
| 486 | origin_y: y0 - bound, |
| 487 | origin_c: (y0 >> sy) - bound, |
| 488 | origin_unit: y0 - 4 * bound, |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | pub fn bit_depth(&self, c: usize) -> u32 { |
| 494 | if c == 0 { |
| 495 | self.bit_depth_y |
| 496 | } else { |
| 497 | self.bit_depth_c |
| 498 | } |
| 499 | } |
| 500 | } |