| 1 | |
| 2 | |
| 3 | use super::*; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | #[cold] |
| 12 | #[inline(never)] |
| 13 | fn trace_base_eob(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 14 | eprintln!( |
| 15 | "TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | #[cold] |
| 20 | #[inline(never)] |
| 21 | fn trace_base(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 22 | eprintln!("TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"); |
| 23 | } |
| 24 | |
| 25 | #[cold] |
| 26 | #[inline(never)] |
| 27 | fn trace_br(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, k: i32) { |
| 28 | eprintln!("TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}"); |
| 29 | } |
| 30 | |
| 31 | #[cold] |
| 32 | #[inline(never)] |
| 33 | fn coeff_step_base(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, v: i32) { |
| 34 | let (rng, _) = dec.debug_state(); |
| 35 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}"); |
| 36 | } |
| 37 | |
| 38 | #[cold] |
| 39 | #[inline(never)] |
| 40 | fn coeff_step_br(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, k: i32) { |
| 41 | let (rng, _) = dec.debug_state(); |
| 42 | eprintln!("EC_COEFF_STEP tag=br c={scan_idx} pos={apos} ctx={ctx} k={k} rng={rng}"); |
| 43 | } |
| 44 | |
| 45 | #[cold] |
| 46 | #[inline(never)] |
| 47 | fn coeff_step_sign(dec: &SymbolDecoder, c: usize, negative: bool) { |
| 48 | let (rng, _) = dec.debug_state(); |
| 49 | eprintln!( |
| 50 | "EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", |
| 51 | negative as i32 |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | #[cold] |
| 56 | #[inline(never)] |
| 57 | fn coeff_step_post_golomb(dec: &SymbolDecoder, c: usize, level: i32) { |
| 58 | let (rng, _) = dec.debug_state(); |
| 59 | eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}"); |
| 60 | } |
| 61 | |
| 62 | #[cold] |
| 63 | #[inline(never)] |
| 64 | fn coeff_step_base_eob_level(dec: &SymbolDecoder, level: i32) { |
| 65 | let (rng, _) = dec.debug_state(); |
| 66 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | #[track_caller] |
| 71 | pub(super) fn read_coeffs( |
| 72 | dec: &mut SymbolDecoder, |
| 73 | coding: &mut TxbTables, |
| 74 | scan: &[u16], |
| 75 | skip_ctx: usize, |
| 76 | sign_ctx: usize, |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | default_tx_type: TxType, |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | rect_shape: Option<(usize, usize)>, |
| 90 | ) -> Result<(Grid, TxType)> { |
| 91 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 92 | let side = coding.side; |
| 93 | |
| 94 | |
| 95 | |
| 96 | let census_entry_rng = dec.debug_state().0; |
| 97 | let (census_w, census_h) = rect_shape.unwrap_or((side, side)); |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | let iv_plane = CENSUS_PLANE.with(|c| c.get()).min(2); |
| 103 | let iv_bit = unit_iv_in(iv_plane, census_w, census_h, skip_ctx, dec); |
| 104 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 105 | let (range, value) = dec.debug_state(); |
| 106 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 107 | } |
| 108 | if crate::envflags::env_flag!("EC_ECDUMP_IN") { |
| 109 | let (range, value) = dec.debug_state(); |
| 110 | let bit = dec.debug_bitpos(); |
| 111 | eprintln!( |
| 112 | "EC_ECDUMP_IN side={side} ctx={skip_ctx} dcctx={sign_ctx} ECIN=({value},{range},{bit})" |
| 113 | ); |
| 114 | } |
| 115 | let dbg_txbskip = if coeff_trace_on() { |
| 116 | coding.txb_skip[skip_ctx] |
| 117 | } else { |
| 118 | [0; 3] |
| 119 | }; |
| 120 | let sq_bit = if coeff_trace_on() { |
| 121 | dec.debug_bitpos() |
| 122 | } else { |
| 123 | 0 |
| 124 | }; |
| 125 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 126 | unit_iv_skip( |
| 127 | dec, iv_bit, iv_plane, census_w, census_h, skip_ctx, all_zero, |
| 128 | ); |
| 129 | |
| 130 | |
| 131 | |
| 132 | if crate::envflags::env_flag!("EC_IBCTX") { |
| 133 | eprintln!( |
| 134 | "EC_IBCTX2 side={} skip_ctx={skip_ctx} sign_ctx={sign_ctx} bit={sq_bit} at {}", |
| 135 | coding.side, |
| 136 | std::panic::Location::caller(), |
| 137 | ); |
| 138 | } |
| 139 | if coeff_trace_on() { |
| 140 | let (rng, _) = dec.debug_state(); |
| 141 | eprintln!( |
| 142 | "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} dcctx={sign_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng} bit={sq_bit} post_bit={}", |
| 143 | coding.side, |
| 144 | all_zero as i32, |
| 145 | dec.debug_bitpos() |
| 146 | ); |
| 147 | } |
| 148 | if crate::envflags::env_flag!("EC_AV1_TELL") { |
| 149 | eprintln!( |
| 150 | "TELL label=post_txb_skip ctx={skip_ctx} all_zero={} tell={} range={}", |
| 151 | all_zero as u8, |
| 152 | dec.debug_bitpos(), |
| 153 | dec.debug_state().0 |
| 154 | ); |
| 155 | } |
| 156 | if trace { |
| 157 | eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); |
| 158 | } |
| 159 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 160 | let (range, value) = dec.debug_state(); |
| 161 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 162 | } |
| 163 | if all_zero { |
| 164 | |
| 165 | |
| 166 | census_unit( |
| 167 | census_w, |
| 168 | census_h, |
| 169 | TxType::DctDct, |
| 170 | 0, |
| 171 | true, |
| 172 | census_entry_rng, |
| 173 | dec, |
| 174 | ); |
| 175 | unit_iv_end(dec, iv_plane, census_w, census_h, skip_ctx); |
| 176 | return Ok((Grid::Zero(side * side), TxType::DctDct)); |
| 177 | } |
| 178 | let mut grid = vec![0i32; side * side]; |
| 179 | let mut tx_type = default_tx_type; |
| 180 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | let len = tx_type_cdf.len(); |
| 190 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") && len == 3 { |
| 191 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 192 | } |
| 193 | crate::msac::SymbolDecoder::set_symr_cdf("tx_type"); |
| 194 | let t = dec.symbol(tx_type_cdf); |
| 195 | crate::census::tx_type(len, t); |
| 196 | if coeff_trace_on() { |
| 197 | let (rng, _) = dec.debug_state(); |
| 198 | eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); |
| 199 | } |
| 200 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 201 | let (range, value) = dec.debug_state(); |
| 202 | eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); |
| 203 | } |
| 204 | if trace { |
| 205 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 206 | } |
| 207 | tx_type = tx_type_from_symbol(len, t).ok_or_else(|| { |
| 208 | unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")) |
| 209 | })?; |
| 210 | } |
| 211 | |
| 212 | let class = TxClass::of(tx_type); |
| 213 | if class != TxClass::TwoD { |
| 214 | hit!(TX_CLASS1_HITS); |
| 215 | hit!(RECT_CLASS1_HITS); |
| 216 | } |
| 217 | let eob = read_eob(dec, coding, class); |
| 218 | if trace { |
| 219 | eprintln!("TRACE eob value={eob}"); |
| 220 | } |
| 221 | let ec_trace_coeff = coeff_trace_on(); |
| 222 | if ec_trace_coeff { |
| 223 | let (rng, _) = dec.debug_state(); |
| 224 | let cls = match class { |
| 225 | TxClass::TwoD => "2d", |
| 226 | TxClass::Horiz => "horiz", |
| 227 | TxClass::Vert => "vert", |
| 228 | }; |
| 229 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} tx={tx_type:?} class={cls} rng={rng}"); |
| 230 | } |
| 231 | let class_scan; |
| 232 | let scan: &[u16] = if class == TxClass::TwoD { |
| 233 | scan |
| 234 | } else { |
| 235 | class_scan = class_scan_table(side, class); |
| 236 | &class_scan |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | debug_assert!(side.is_power_of_two() && side <= 32); |
| 243 | let sh = side.trailing_zeros(); |
| 244 | let col_mask = side - 1; |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | let stride = side + 4; |
| 251 | let mut levels_buf = [0u8; 36 * 36]; |
| 252 | let levels = &mut levels_buf[..stride * stride]; |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | let mut nz_buf = [0u16; 1024]; |
| 262 | let mut nz_n = 0usize; |
| 263 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 264 | for scan_idx in (0..eob).rev() { |
| 265 | let pos = scan[scan_idx] as usize; |
| 266 | let (row, col) = (pos >> sh, pos & col_mask); |
| 267 | let idx = row * stride + col; |
| 268 | let level = if scan_idx == eob - 1 { |
| 269 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 270 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 271 | if trace { |
| 272 | trace_base_eob(scan_idx, pos, row, col, ctx, v); |
| 273 | } |
| 274 | v |
| 275 | } else { |
| 276 | let ctx = if class == TxClass::TwoD && rect_shape.is_none() { |
| 277 | if row == 0 && col == 0 { |
| 278 | 0 |
| 279 | } else { |
| 280 | base_mag(levels, idx, &base_taps) |
| 281 | + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 282 | } |
| 283 | } else { |
| 284 | base_ctx(levels, stride, row, col, class, rect_shape) |
| 285 | }; |
| 286 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 287 | if ec_trace_coeff { |
| 288 | |
| 289 | |
| 290 | coeff_step_base(dec, scan_idx, col * side + row, ctx, v); |
| 291 | } |
| 292 | if trace { |
| 293 | trace_base(scan_idx, pos, row, col, ctx, v); |
| 294 | } |
| 295 | v |
| 296 | }; |
| 297 | let level = if level > NUM_BASE_LEVELS { |
| 298 | let ctx = { |
| 299 | let mag = br_mag(levels, idx, &br_taps); |
| 300 | if row == 0 && col == 0 { |
| 301 | mag |
| 302 | } else { |
| 303 | let near_origin = match class { |
| 304 | TxClass::TwoD => row < 2 && col < 2, |
| 305 | TxClass::Horiz => col == 0, |
| 306 | TxClass::Vert => row == 0, |
| 307 | }; |
| 308 | if near_origin { mag + 7 } else { mag + 14 } |
| 309 | } |
| 310 | }; |
| 311 | let mut level = level; |
| 312 | let mut sent = 0; |
| 313 | loop { |
| 314 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 315 | if ec_trace_coeff { |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | coeff_step_br(dec, scan_idx, col * side + row, ctx, k); |
| 321 | } |
| 322 | if trace { |
| 323 | trace_br(scan_idx, pos, row, col, ctx, k); |
| 324 | } |
| 325 | level += k; |
| 326 | sent += BR_STEP; |
| 327 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | level |
| 332 | } else { |
| 333 | level |
| 334 | }; |
| 335 | if ec_trace_coeff && scan_idx == eob - 1 { |
| 336 | coeff_step_base_eob_level(dec, level); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | debug_assert!((0..=15).contains(&level), "level {level} past the u8 frame"); |
| 344 | levels[idx] = level as u8; |
| 345 | if level != 0 { |
| 346 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 347 | nz_n += 1; |
| 348 | } |
| 349 | } |
| 350 | if ec_trace_coeff { |
| 351 | let (rng, _) = dec.debug_state(); |
| 352 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 353 | } |
| 354 | |
| 355 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 356 | let c = (packed >> 4) as usize; |
| 357 | let pos = scan[c] as usize; |
| 358 | let level = (packed & 15) as i32; |
| 359 | let negative = if pos == 0 { |
| 360 | let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; |
| 361 | if trace { |
| 362 | eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); |
| 363 | } |
| 364 | v |
| 365 | } else { |
| 366 | dec.literal(1) == 1 |
| 367 | }; |
| 368 | if ec_trace_coeff { |
| 369 | coeff_step_sign(dec, c, negative); |
| 370 | } |
| 371 | let level = if level > MAX_BR_LEVEL { |
| 372 | let g = read_golomb(dec)?; |
| 373 | if trace { |
| 374 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 375 | } |
| 376 | level + g as i32 |
| 377 | } else { |
| 378 | level |
| 379 | }; |
| 380 | if ec_trace_coeff { |
| 381 | coeff_step_post_golomb(dec, c, level); |
| 382 | } |
| 383 | grid[pos] = if negative { -level } else { level }; |
| 384 | } |
| 385 | census_unit( |
| 386 | census_w, |
| 387 | census_h, |
| 388 | tx_type, |
| 389 | eob, |
| 390 | false, |
| 391 | census_entry_rng, |
| 392 | dec, |
| 393 | ); |
| 394 | unit_iv_end(dec, iv_plane, census_w, census_h, skip_ctx); |
| 395 | Ok((Grid::Own(grid), tx_type)) |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | #[track_caller] |
| 411 | |
| 412 | #[allow(clippy::too_many_arguments)] |
| 413 | pub(super) fn read_coeffs_rect( |
| 414 | dec: &mut SymbolDecoder, |
| 415 | coding: &mut TxbTables, |
| 416 | scan: &[u16], |
| 417 | w: usize, |
| 418 | h: usize, |
| 419 | skip_ctx: usize, |
| 420 | sign_ctx: usize, |
| 421 | default_tx_type: TxType, |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | plane: usize, |
| 427 | ) -> Result<(Grid, TxType)> { |
| 428 | let rect_trace = coeff_trace_on(); |
| 429 | let entry_rng = dec.debug_state().0; |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | set_census_plane(plane); |
| 437 | let ibctx_on = crate::envflags::env_flag!("EC_IBCTX"); |
| 438 | let entry_bit = if ibctx_on { dec.debug_bitpos() } else { 0 }; |
| 439 | let iv_bit = unit_iv_in(plane, w, h, skip_ctx, dec); |
| 440 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 441 | unit_iv_skip(dec, iv_bit, plane, w, h, skip_ctx, all_zero); |
| 442 | if rect_trace { |
| 443 | eprintln!( |
| 444 | "EC_COEFF_STEP tag=all_zero plane={plane} ctx={skip_ctx} entry={entry_rng} all_zero={} rng={} bit={entry_bit} post_bit={}", |
| 445 | all_zero as i32, |
| 446 | dec.debug_state().0, |
| 447 | dec.debug_bitpos() |
| 448 | ); |
| 449 | } |
| 450 | if ibctx_on { |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | eprintln!( |
| 458 | "EC_IBCTX w={w} h={h} skip_ctx={skip_ctx} sign_ctx={sign_ctx} bit={entry_bit} post_bit={} at {}", |
| 459 | dec.debug_bitpos(), |
| 460 | std::panic::Location::caller(), |
| 461 | ); |
| 462 | } |
| 463 | if all_zero { |
| 464 | census_unit(w, h, TxType::DctDct, 0, true, entry_rng, dec); |
| 465 | unit_iv_end(dec, plane, w, h, skip_ctx); |
| 466 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 467 | } |
| 468 | let mut grid = vec![0i32; w * h]; |
| 469 | hit!(RECT_COEFF_TU_HITS); |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | let mut tx_type = default_tx_type; |
| 477 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 478 | let len = tx_type_cdf.len(); |
| 479 | let t = dec.symbol(tx_type_cdf); |
| 480 | crate::census::tx_type(len, t); |
| 481 | tx_type = tx_type_from_symbol(len, t).ok_or_else(|| { |
| 482 | unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")) |
| 483 | })?; |
| 484 | if rect_trace { |
| 485 | eprintln!( |
| 486 | "EC_COEFF_STEP tag=tx_type plane={plane} rng={}", |
| 487 | dec.debug_state().0 |
| 488 | ); |
| 489 | } |
| 490 | } |
| 491 | let class = TxClass::of(tx_type); |
| 492 | if class != TxClass::TwoD { |
| 493 | hit!(TX_CLASS1_HITS); |
| 494 | } |
| 495 | let eob = read_eob(dec, coding, class); |
| 496 | if rect_trace { |
| 497 | eprintln!( |
| 498 | "EC_COEFF_STEP tag=eob eob={eob} rng={}", |
| 499 | dec.debug_state().0 |
| 500 | ); |
| 501 | } |
| 502 | let class_scan; |
| 503 | let scan: &[u16] = if class == TxClass::TwoD { |
| 504 | scan |
| 505 | } else { |
| 506 | class_scan = class_scan_table_wh(w, h, class); |
| 507 | &class_scan |
| 508 | }; |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | let stride = w + 4; |
| 516 | let n = stride * (h + 4); |
| 517 | if !(w.is_power_of_two() && n <= 36 * 36) { |
| 518 | panic!("rect coeff scratch w={w} h={h} n={n}"); |
| 519 | } |
| 520 | let (mut b96, mut b256, mut b768); |
| 521 | |
| 522 | let levels: &mut [u8] = if n <= 96 { |
| 523 | b96 = [0u8; 96]; |
| 524 | &mut b96[..n] |
| 525 | } else if n <= 256 { |
| 526 | b256 = [0u8; 256]; |
| 527 | &mut b256[..n] |
| 528 | } else { |
| 529 | b768 = [0u8; 768]; |
| 530 | &mut b768[..n] |
| 531 | }; |
| 532 | let wsh = w.trailing_zeros(); |
| 533 | let wmask = w - 1; |
| 534 | |
| 535 | let mut nz_buf = [0u16; 1024]; |
| 536 | let mut nz_n = 0usize; |
| 537 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 538 | let (wlt, wgt) = (w < h, w > h); |
| 539 | for scan_idx in (0..eob).rev() { |
| 540 | let pos = scan[scan_idx] as usize; |
| 541 | let (row, col) = (pos >> wsh, pos & wmask); |
| 542 | let idx = row * stride + col; |
| 543 | |
| 544 | let _apos = col * h + row; |
| 545 | let level = if scan_idx == eob - 1 { |
| 546 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 547 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | v |
| 555 | } else { |
| 556 | let ctx = if class == TxClass::TwoD { |
| 557 | if row == 0 && col == 0 { |
| 558 | 0 |
| 559 | } else { |
| 560 | let m = base_mag(levels, idx, &base_taps); |
| 561 | if wlt && row < 2 { |
| 562 | m + 11 |
| 563 | } else if wgt && col < 2 { |
| 564 | m + 16 |
| 565 | } else { |
| 566 | m + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 567 | } |
| 568 | } |
| 569 | } else { |
| 570 | base_ctx_rect(levels, stride, w, h, row, col, class) |
| 571 | }; |
| 572 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 573 | if rect_trace { |
| 574 | eprintln!( |
| 575 | "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 576 | dec.debug_state().0 |
| 577 | ); |
| 578 | } |
| 579 | v |
| 580 | }; |
| 581 | let level = if level > NUM_BASE_LEVELS { |
| 582 | let ctx = { |
| 583 | let mag = br_mag(levels, idx, &br_taps); |
| 584 | if row == 0 && col == 0 { |
| 585 | mag |
| 586 | } else { |
| 587 | let near_origin = match class { |
| 588 | TxClass::TwoD => row < 2 && col < 2, |
| 589 | TxClass::Horiz => col == 0, |
| 590 | TxClass::Vert => row == 0, |
| 591 | }; |
| 592 | if near_origin { mag + 7 } else { mag + 14 } |
| 593 | } |
| 594 | }; |
| 595 | let mut level = level; |
| 596 | let mut sent = 0; |
| 597 | loop { |
| 598 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 599 | if rect_trace { |
| 600 | eprintln!( |
| 601 | "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}", |
| 602 | dec.debug_state().0 |
| 603 | ); |
| 604 | } |
| 605 | level += k; |
| 606 | sent += BR_STEP; |
| 607 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | level |
| 612 | } else { |
| 613 | level |
| 614 | }; |
| 615 | debug_assert!((0..=15).contains(&level), "level {level} past the u8 frame"); |
| 616 | levels[idx] = level as u8; |
| 617 | if level != 0 { |
| 618 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 619 | nz_n += 1; |
| 620 | } |
| 621 | if rect_trace && scan_idx == eob - 1 { |
| 622 | |
| 623 | |
| 624 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 625 | let (rng, _) = dec.debug_state(); |
| 626 | eprintln!( |
| 627 | "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={level} rng={rng}" |
| 628 | ); |
| 629 | } |
| 630 | } |
| 631 | if rect_trace { |
| 632 | let (rng, _) = dec.debug_state(); |
| 633 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 634 | } |
| 635 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 636 | let pos = scan[(packed >> 4) as usize] as usize; |
| 637 | let level = (packed & 15) as i32; |
| 638 | let negative = if pos == 0 { |
| 639 | dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 |
| 640 | } else { |
| 641 | dec.literal(1) == 1 |
| 642 | }; |
| 643 | if rect_trace { |
| 644 | let (rng, _) = dec.debug_state(); |
| 645 | eprintln!( |
| 646 | "EC_COEFF_STEP tag=sign_rect pos={pos} sign={} dcctx={sign_ctx} rng={rng}", |
| 647 | u8::from(negative) |
| 648 | ); |
| 649 | } |
| 650 | let level = if level > MAX_BR_LEVEL { |
| 651 | let g = read_golomb(dec)?; |
| 652 | level + g as i32 |
| 653 | } else { |
| 654 | level |
| 655 | }; |
| 656 | if rect_trace { |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | let (rng, _) = dec.debug_state(); |
| 665 | eprintln!( |
| 666 | "EC_COEFF_STEP tag=post_golomb c={} pos={pos} level={level} rng={rng}", |
| 667 | (packed >> 4) |
| 668 | ); |
| 669 | } |
| 670 | grid[pos] = if negative { -level } else { level }; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 677 | let nz: Vec<String> = grid |
| 678 | .iter() |
| 679 | .enumerate() |
| 680 | .filter(|&(_, &v)| v != 0) |
| 681 | .map(|(i, &v)| format!("{}/{}:{v}", i / w, i % w)) |
| 682 | .collect(); |
| 683 | eprintln!( |
| 684 | "OUR_LEVELS w={w} h={h} eob={eob} tx={tx_type:?} nz={}", |
| 685 | nz.join(",") |
| 686 | ); |
| 687 | } |
| 688 | census_unit(w, h, tx_type, eob, false, entry_rng, dec); |
| 689 | unit_iv_end(dec, plane, w, h, skip_ctx); |
| 690 | Ok((Grid::Own(grid), tx_type)) |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | #[allow(clippy::too_many_arguments)] |
| 711 | pub(super) fn read_chroma_coeffs_rect( |
| 712 | dec: &mut SymbolDecoder, |
| 713 | coding: &mut TxbTables, |
| 714 | scan: &[u16], |
| 715 | w: usize, |
| 716 | h: usize, |
| 717 | skip_ctx: usize, |
| 718 | sign_ctx: usize, |
| 719 | default_tx_type: TxType, |
| 720 | fctx: &crate::decode::FrameCtx, |
| 721 | |
| 722 | |
| 723 | |
| 724 | plane: usize, |
| 725 | ) -> Result<(Grid, TxType)> { |
| 726 | if mono(fctx) { |
| 727 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 728 | } |
| 729 | read_coeffs_rect( |
| 730 | dec, |
| 731 | coding, |
| 732 | scan, |
| 733 | w, |
| 734 | h, |
| 735 | skip_ctx, |
| 736 | sign_ctx, |
| 737 | default_tx_type, |
| 738 | plane, |
| 739 | ) |
| 740 | } |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | #[derive(Clone, Copy, Default)] |
| 746 | pub(super) struct Neighbour { |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | pub(super) level: u8, |
| 756 | pub(super) dc: Option<bool>, |
| 757 | } |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | #[inline] |
| 767 | pub(super) fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) { |
| 768 | let end = (start + n).min(dst.len()); |
| 769 | if start < end { |
| 770 | dst[start..end].fill(v); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | pub(super) fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 775 | |
| 776 | |
| 777 | |
| 778 | if is_zero_grid(grid) { |
| 779 | return Neighbour::default(); |
| 780 | } |
| 781 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 782 | Neighbour { |
| 783 | level: cul.min(7) as u8, |
| 784 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | pub(crate) const SKIP_CONTEXTS: [[usize; 5]; 5] = [ |
| 792 | [1, 2, 2, 2, 3], |
| 793 | [2, 4, 4, 4, 5], |
| 794 | [2, 4, 4, 4, 5], |
| 795 | [2, 4, 4, 4, 5], |
| 796 | [3, 5, 5, 5, 6], |
| 797 | ]; |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | #[derive(Clone, Copy)] |
| 805 | pub(super) struct BlkCell { |
| 806 | pub(super) skip: bool, |
| 807 | pub(super) written: bool, |
| 808 | pub(super) org: [u16; 2], |
| 809 | pub(super) dim: [u8; 2], |
| 810 | } |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | #[derive(Clone)] |
| 822 | pub(super) struct Neighbours { |
| 823 | pub(super) above: Vec<[Neighbour; 3]>, |
| 824 | pub(super) left: Vec<[Neighbour; 3]>, |
| 825 | pub(super) above_mode: Vec<usize>, |
| 826 | pub(super) left_mode: Vec<usize>, |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | pub(super) above_uv_mode: Vec<usize>, |
| 837 | pub(super) left_uv_mode: Vec<usize>, |
| 838 | pub(super) above_side: Vec<usize>, |
| 839 | pub(super) left_side: Vec<usize>, |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | pub(super) above_side_mi: Vec<u8>, |
| 849 | pub(super) left_side_mi: Vec<u8>, |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | pub(super) sub8_mode_col: Vec<(u16, u8)>, |
| 867 | pub(super) sub8_mode_row: Vec<(u16, u8)>, |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | pub(super) uv_mode_col: Vec<(u16, u8)>, |
| 874 | pub(super) uv_mode_row: Vec<(u16, u8)>, |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | pub(super) uv_mode_grid: Vec<u8>, |
| 884 | |
| 885 | |
| 886 | pub(super) uv_grid_cols: usize, |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | pub(super) uv_owner: Vec<u32>, |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | pub(super) above_skip: Vec<bool>, |
| 903 | pub(super) left_skip: Vec<bool>, |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | pub(super) above_txfm: Vec<u8>, |
| 913 | pub(super) left_txfm: Vec<u8>, |
| 914 | |
| 915 | |
| 916 | |
| 917 | pub(super) above_skip_mode: Vec<bool>, |
| 918 | pub(super) left_skip_mode: Vec<bool>, |
| 919 | |
| 920 | |
| 921 | |
| 922 | pub(super) above_inter: Vec<bool>, |
| 923 | pub(super) left_inter: Vec<bool>, |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | pub(super) above_ref: Vec<i8>, |
| 932 | pub(super) left_ref: Vec<i8>, |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | pub(super) above_ref1: Vec<Option<i8>>, |
| 946 | pub(super) left_ref1: Vec<Option<i8>>, |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | pub(super) above_comp_group_idx: Vec<u8>, |
| 953 | pub(super) left_comp_group_idx: Vec<u8>, |
| 954 | pub(super) above_compound_idx: Vec<u8>, |
| 955 | pub(super) left_compound_idx: Vec<u8>, |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | pub(super) above_filter: Vec<[u8; 2]>, |
| 961 | pub(super) left_filter: Vec<[u8; 2]>, |
| 962 | pub(super) mi_cols: usize, |
| 963 | pub(super) mi_rows: usize, |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | pub(super) blk_grid: Vec<BlkCell>, |
| 990 | pub(super) skip_grid_cols_mi: usize, |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | pub(super) inter_grid: Vec<bool>, |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | pub(super) tx_grid: Vec<u8>, |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | pub(super) tx_h_grid: Vec<u8>, |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | pub(super) uv_tx_grid: Vec<u8>, |
| 1030 | pub(super) uv_tx_h_grid: Vec<u8>, |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | pub(super) ref_grid: Vec<i8>, |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | pub(super) delta_lf_grid: Vec<[i8; 4]>, |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | pub(super) above_palette_size: Vec<u8>, |
| 1052 | pub(super) left_palette_size: Vec<u8>, |
| 1053 | pub(super) above_palette_colors: Vec<[u16; 8]>, |
| 1054 | pub(super) left_palette_colors: Vec<[u16; 8]>, |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | pub(super) above_palette_uv_size: Vec<u8>, |
| 1059 | pub(super) left_palette_uv_size: Vec<u8>, |
| 1060 | pub(super) above_palette_uv_colors: Vec<[u16; 8]>, |
| 1061 | pub(super) left_palette_uv_colors: Vec<[u16; 8]>, |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | pub(super) tile_row0_mi: usize, |
| 1070 | pub(super) tile_col0_mi: usize, |
| 1071 | } |
| 1072 | |
| 1073 | #[allow(dead_code)] |
| 1074 | impl Neighbours { |
| 1075 | |
| 1076 | |
| 1077 | pub(super) fn new( |
| 1078 | cols: usize, |
| 1079 | rows: usize, |
| 1080 | mi_cols: usize, |
| 1081 | mi_rows: usize, |
| 1082 | fctx: &crate::decode::FrameCtx, |
| 1083 | ) -> Self { |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | let sb_sub = (sb_mi_cur(fctx) as usize) / (SUB / MI); |
| 1092 | let (cols, rows) = (cols.next_multiple_of(sb_sub), rows.next_multiple_of(sb_sub)); |
| 1093 | Self { |
| 1094 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 1095 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 1096 | above_mode: vec![DC_PRED; cols], |
| 1097 | left_mode: vec![DC_PRED; rows], |
| 1098 | above_uv_mode: vec![DC_PRED; cols], |
| 1099 | left_uv_mode: vec![DC_PRED; rows], |
| 1100 | above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 1101 | left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 1102 | above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)], |
| 1103 | left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)], |
| 1104 | sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 1105 | sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 1106 | uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 1107 | uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 1108 | uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1109 | uv_grid_cols: cols * (SUB / MI), |
| 1110 | uv_owner: vec![0; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1111 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 1112 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | above_skip: vec![false; cols * (SUB / MI)], |
| 1118 | left_skip: vec![false; rows * (SUB / MI)], |
| 1119 | above_skip_mode: vec![false; cols * (SUB / MI)], |
| 1120 | left_skip_mode: vec![false; rows * (SUB / MI)], |
| 1121 | above_inter: vec![false; cols * (SUB / MI)], |
| 1122 | left_inter: vec![false; rows * (SUB / MI)], |
| 1123 | above_ref: vec![-1; cols * (SUB / MI)], |
| 1124 | left_ref: vec![-1; rows * (SUB / MI)], |
| 1125 | above_ref1: vec![None; cols * (SUB / MI)], |
| 1126 | left_ref1: vec![None; rows * (SUB / MI)], |
| 1127 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 1128 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 1129 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 1130 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 1131 | above_filter: vec![[3u8; 2]; cols * (SUB / MI)], |
| 1132 | left_filter: vec![[3u8; 2]; rows * (SUB / MI)], |
| 1133 | mi_cols, |
| 1134 | mi_rows, |
| 1135 | blk_grid: vec![ |
| 1136 | BlkCell { |
| 1137 | skip: false, |
| 1138 | written: false, |
| 1139 | org: [0; 2], |
| 1140 | dim: [1; 2] |
| 1141 | }; |
| 1142 | cols * (SUB / MI) * rows * (SUB / MI) |
| 1143 | ], |
| 1144 | skip_grid_cols_mi: cols * (SUB / MI), |
| 1145 | inter_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1146 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1147 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1148 | uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1149 | uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1150 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1151 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1152 | above_palette_size: vec![0; cols * (SUB / MI)], |
| 1153 | left_palette_size: vec![0; rows * (SUB / MI)], |
| 1154 | above_palette_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 1155 | left_palette_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 1156 | above_palette_uv_size: vec![0; cols * (SUB / MI)], |
| 1157 | left_palette_uv_size: vec![0; rows * (SUB / MI)], |
| 1158 | above_palette_uv_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 1159 | left_palette_uv_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 1160 | tile_row0_mi: 0, |
| 1161 | tile_col0_mi: 0, |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | pub(super) fn start_tile( |
| 1172 | &mut self, |
| 1173 | row0_mi: usize, |
| 1174 | col0_mi: usize, |
| 1175 | col1_mi: usize, |
| 1176 | fctx: &crate::decode::FrameCtx, |
| 1177 | ) { |
| 1178 | |
| 1179 | |
| 1180 | fctx.seg_tile_origin.with(|c| c.set((row0_mi, col0_mi))); |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | UV_OWNER_EPOCH.with(|c| c.set(c.get().wrapping_add(1) | (1 << 24))); |
| 1185 | self.tile_row0_mi = row0_mi; |
| 1186 | self.tile_col0_mi = col0_mi; |
| 1187 | let end = col1_mi.min(self.above.len()); |
| 1188 | for i in col0_mi.min(end)..end { |
| 1189 | self.above[i] = Default::default(); |
| 1190 | self.above_side_mi[i] = NO_NEIGHBOUR_SIDE as u8; |
| 1191 | } |
| 1192 | let (sc0, sc1) = ( |
| 1193 | col0_mi / (SUB / MI), |
| 1194 | (col1_mi / (SUB / MI)).min(self.above_mode.len()), |
| 1195 | ); |
| 1196 | for i in sc0.min(sc1)..sc1 { |
| 1197 | self.above_mode[i] = DC_PRED; |
| 1198 | self.above_side[i] = NO_NEIGHBOUR_SIDE; |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | let mend = col1_mi.min(self.above_skip.len()); |
| 1203 | for i in col0_mi.min(mend)..mend { |
| 1204 | self.above_skip[i] = false; |
| 1205 | self.above_skip_mode[i] = false; |
| 1206 | self.above_inter[i] = false; |
| 1207 | self.above_ref[i] = -1; |
| 1208 | self.above_ref1[i] = None; |
| 1209 | self.above_comp_group_idx[i] = 0; |
| 1210 | self.above_compound_idx[i] = 0; |
| 1211 | self.above_filter[i] = [3u8; 2]; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | let mcol_end = col1_mi.min(self.sub8_mode_col.len()); |
| 1217 | for i in col0_mi.min(mcol_end)..mcol_end { |
| 1218 | self.sub8_mode_col[i] = (u16::MAX, 0); |
| 1219 | self.uv_mode_col[i] = (u16::MAX, 0); |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | let tend = col1_mi.min(self.above_txfm.len()); |
| 1226 | for i in col0_mi.min(tend)..tend { |
| 1227 | self.above_txfm[i] = TXFM_CTX_INIT; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | pub(super) fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 1237 | self.record_palette_y_rect( |
| 1238 | (at.0 * (SUB / MI), at.1 * (SUB / MI)), |
| 1239 | side, |
| 1240 | side, |
| 1241 | size, |
| 1242 | colors, |
| 1243 | ); |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | pub(super) fn record_palette_y_rect( |
| 1251 | &mut self, |
| 1252 | mi: (usize, usize), |
| 1253 | w: usize, |
| 1254 | h: usize, |
| 1255 | size: usize, |
| 1256 | colors: [u16; 8], |
| 1257 | ) { |
| 1258 | let (r, c) = mi; |
| 1259 | if crate::envflags::env_flag!("EC_PALPUB") { |
| 1260 | eprintln!("EC_PALPUB yrect mi={r},{c} bw={w} bh={h} y={size}"); |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | for cell in 0..(w / MI).max(1) { |
| 1269 | if let Some(e) = self.above_palette_size.get_mut(c + cell) { |
| 1270 | *e = size as u8; |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | if size > 0 { |
| 1275 | self.above_palette_colors[c + cell] = colors; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | for cell in 0..(h / MI).max(1) { |
| 1280 | if let Some(e) = self.left_palette_size.get_mut(r + cell) { |
| 1281 | *e = size as u8; |
| 1282 | if size > 0 { |
| 1283 | self.left_palette_colors[r + cell] = colors; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | pub(super) fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 1303 | self.palette_ctx_and_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | pub(super) fn palette_ctx_and_cache_mi(&self, mi: (usize, usize)) -> (usize, Vec<u16>) { |
| 1314 | let (r, c) = mi; |
| 1315 | let above_ok = r % 16 != 0; |
| 1316 | |
| 1317 | let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0; |
| 1318 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 1319 | ( |
| 1320 | usize::from(self.above_palette_size[c]), |
| 1321 | self.above_palette_colors[c], |
| 1322 | ) |
| 1323 | } else { |
| 1324 | (0, [0u16; 8]) |
| 1325 | }; |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | let left_ok = c > self.tile_col0_mi; |
| 1332 | let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 { |
| 1333 | ( |
| 1334 | usize::from(self.left_palette_size[r]), |
| 1335 | self.left_palette_colors[r], |
| 1336 | ) |
| 1337 | } else { |
| 1338 | (0, [0u16; 8]) |
| 1339 | }; |
| 1340 | let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); |
| 1341 | let mut cache = Vec::with_capacity(16); |
| 1342 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 1343 | if cache.last() != Some(&v) { |
| 1344 | cache.push(v); |
| 1345 | } |
| 1346 | }; |
| 1347 | let (mut ai, mut li) = (0usize, 0usize); |
| 1348 | while ai < above_n && li < left_n { |
| 1349 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 1350 | if vl < va { |
| 1351 | push_dedup(&mut cache, vl); |
| 1352 | li += 1; |
| 1353 | } else { |
| 1354 | push_dedup(&mut cache, va); |
| 1355 | ai += 1; |
| 1356 | if vl == va { |
| 1357 | li += 1; |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | while ai < above_n { |
| 1362 | push_dedup(&mut cache, above_colors[ai]); |
| 1363 | ai += 1; |
| 1364 | } |
| 1365 | while li < left_n { |
| 1366 | push_dedup(&mut cache, left_colors[li]); |
| 1367 | li += 1; |
| 1368 | } |
| 1369 | (ctx, cache) |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | pub(super) fn record_palette_uv( |
| 1376 | &mut self, |
| 1377 | at: (usize, usize), |
| 1378 | side: usize, |
| 1379 | size: usize, |
| 1380 | colors: [u16; 8], |
| 1381 | ) { |
| 1382 | self.record_palette_uv_rect( |
| 1383 | (at.0 * (SUB / MI), at.1 * (SUB / MI)), |
| 1384 | side, |
| 1385 | side, |
| 1386 | size, |
| 1387 | colors, |
| 1388 | ); |
| 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | pub(super) fn record_palette_uv_rect( |
| 1396 | &mut self, |
| 1397 | mi: (usize, usize), |
| 1398 | w: usize, |
| 1399 | h: usize, |
| 1400 | size: usize, |
| 1401 | colors: [u16; 8], |
| 1402 | ) { |
| 1403 | let (r, c) = mi; |
| 1404 | |
| 1405 | for cell in 0..(w / MI).max(1) { |
| 1406 | if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) { |
| 1407 | *e = size as u8; |
| 1408 | if size > 0 { |
| 1409 | self.above_palette_uv_colors[c + cell] = colors; |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | for cell in 0..(h / MI).max(1) { |
| 1414 | if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) { |
| 1415 | *e = size as u8; |
| 1416 | if size > 0 { |
| 1417 | self.left_palette_uv_colors[r + cell] = colors; |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | pub(super) fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> { |
| 1429 | self.palette_uv_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | pub(super) fn palette_uv_cache_mi(&self, mi: (usize, usize)) -> Vec<u16> { |
| 1435 | let (r, c) = mi; |
| 1436 | let above_ok = r % 16 != 0; |
| 1437 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 1438 | ( |
| 1439 | usize::from(self.above_palette_uv_size[c]), |
| 1440 | self.above_palette_uv_colors[c], |
| 1441 | ) |
| 1442 | } else { |
| 1443 | (0, [0u16; 8]) |
| 1444 | }; |
| 1445 | |
| 1446 | let left_ok = c > self.tile_col0_mi; |
| 1447 | let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 { |
| 1448 | ( |
| 1449 | usize::from(self.left_palette_uv_size[r]), |
| 1450 | self.left_palette_uv_colors[r], |
| 1451 | ) |
| 1452 | } else { |
| 1453 | (0, [0u16; 8]) |
| 1454 | }; |
| 1455 | let mut cache = Vec::with_capacity(16); |
| 1456 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 1457 | if cache.last() != Some(&v) { |
| 1458 | cache.push(v); |
| 1459 | } |
| 1460 | }; |
| 1461 | let (mut ai, mut li) = (0usize, 0usize); |
| 1462 | while ai < above_n && li < left_n { |
| 1463 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 1464 | if vl < va { |
| 1465 | push_dedup(&mut cache, vl); |
| 1466 | li += 1; |
| 1467 | } else { |
| 1468 | push_dedup(&mut cache, va); |
| 1469 | ai += 1; |
| 1470 | if vl == va { |
| 1471 | li += 1; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | while ai < above_n { |
| 1476 | push_dedup(&mut cache, above_colors[ai]); |
| 1477 | ai += 1; |
| 1478 | } |
| 1479 | while li < left_n { |
| 1480 | push_dedup(&mut cache, left_colors[li]); |
| 1481 | li += 1; |
| 1482 | } |
| 1483 | cache |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | pub(super) fn fill_lf_grid( |
| 1491 | &mut self, |
| 1492 | at_mi: (usize, usize), |
| 1493 | side_mi: usize, |
| 1494 | tx_px: u8, |
| 1495 | ref_frame: i8, |
| 1496 | fctx: &crate::decode::FrameCtx, |
| 1497 | ) { |
| 1498 | self.fill_lf_grid_rect(at_mi, side_mi, side_mi, tx_px, tx_px, ref_frame, fctx); |
| 1499 | } |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | |
| 1518 | |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | pub(super) fn suppress_internal_lf_edges( |
| 1543 | &mut self, |
| 1544 | at_mi: (usize, usize), |
| 1545 | w_mi: usize, |
| 1546 | h_mi: usize, |
| 1547 | fctx: &FrameCtx, |
| 1548 | ) { |
| 1549 | let (mi_r, mi_c) = at_mi; |
| 1550 | let (uv_w, uv_h) = ( |
| 1551 | ((w_mi * MI) >> ss_x(fctx)).max(4) as u8, |
| 1552 | ((h_mi * MI) >> ss_y(fctx)).max(4) as u8, |
| 1553 | ); |
| 1554 | for rr in 0..h_mi { |
| 1555 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 1556 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_w); |
| 1557 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_h); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | #[allow(clippy::too_many_arguments)] |
| 1563 | pub(super) fn fill_lf_grid_rect( |
| 1564 | &mut self, |
| 1565 | at_mi: (usize, usize), |
| 1566 | w_mi: usize, |
| 1567 | h_mi: usize, |
| 1568 | tx_px: u8, |
| 1569 | tx_h_px: u8, |
| 1570 | ref_frame: i8, |
| 1571 | fctx: &crate::decode::FrameCtx, |
| 1572 | ) { |
| 1573 | self.fill_lf_grid_rect_inner(at_mi, w_mi, h_mi, tx_px, tx_h_px, ref_frame, true, fctx) |
| 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | |
| 1604 | #[allow(clippy::too_many_arguments)] |
| 1605 | pub(super) fn fill_lf_grid_rect_after_vartx( |
| 1606 | &mut self, |
| 1607 | at_mi: (usize, usize), |
| 1608 | w_mi: usize, |
| 1609 | h_mi: usize, |
| 1610 | tx_px: u8, |
| 1611 | tx_h_px: u8, |
| 1612 | ref_frame: i8, |
| 1613 | fctx: &crate::decode::FrameCtx, |
| 1614 | ) { |
| 1615 | self.fill_lf_grid_rect_inner(at_mi, w_mi, h_mi, tx_px, tx_h_px, ref_frame, false, fctx) |
| 1616 | } |
| 1617 | |
| 1618 | #[allow(clippy::too_many_arguments)] |
| 1619 | pub(super) fn fill_lf_grid_rect_inner( |
| 1620 | &mut self, |
| 1621 | at_mi: (usize, usize), |
| 1622 | w_mi: usize, |
| 1623 | h_mi: usize, |
| 1624 | tx_px: u8, |
| 1625 | tx_h_px: u8, |
| 1626 | ref_frame: i8, |
| 1627 | publish_txfm_bands: bool, |
| 1628 | fctx: &crate::decode::FrameCtx, |
| 1629 | ) { |
| 1630 | let (mi_r, mi_c) = at_mi; |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | let uv_tx_w = ((w_mi * MI) >> ss_x(fctx)).clamp(4, 32) as u8; |
| 1635 | let uv_tx_h = ((h_mi * MI) >> ss_y(fctx)).clamp(4, 32) as u8; |
| 1636 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 1637 | eprintln!( |
| 1638 | "EC_LFGRID mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}" |
| 1639 | ); |
| 1640 | } |
| 1641 | crate::census::tx(tx_px, tx_h_px); |
| 1642 | let cur = fctx.current_delta_lf.with(|c| c.get()); |
| 1643 | let snapshot: [i8; 4] = if fctx.delta_lf_multi.with(|c| c.get()) { |
| 1644 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| 1645 | } else { |
| 1646 | [cur[0].clamp(-63, 63) as i8; 4] |
| 1647 | }; |
| 1648 | for rr in 0..h_mi { |
| 1649 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 1650 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 1651 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 1652 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_tx_w); |
| 1653 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_tx_h); |
| 1654 | fill_span(&mut self.ref_grid, start, w_mi, ref_frame); |
| 1655 | fill_span(&mut self.delta_lf_grid, start, w_mi, snapshot); |
| 1656 | } |
| 1657 | if publish_txfm_bands && fctx.intra_only.with(std::cell::Cell::get) { |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | txfm_partition_update_rect( |
| 1671 | self, |
| 1672 | at_mi, |
| 1673 | (tx_px as usize, tx_h_px as usize), |
| 1674 | (w_mi * MI, h_mi * MI), |
| 1675 | ); |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | for i in 0..w_mi { |
| 1682 | if let Some(cell) = self.above_inter.get_mut(mi_c + i) { |
| 1683 | *cell = false; |
| 1684 | } |
| 1685 | } |
| 1686 | for i in 0..h_mi { |
| 1687 | if let Some(cell) = self.left_inter.get_mut(mi_r + i) { |
| 1688 | *cell = false; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | self.mark_inter_grid_rect(at_mi, w_mi, h_mi, false); |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | fctx.intrabc_mi_grid.with(|g| { |
| 1707 | if let Some((grid, _, _)) = g.borrow_mut().as_mut() { |
| 1708 | grid.cover_rect(mi_r, mi_c, w_mi, h_mi); |
| 1709 | } |
| 1710 | }); |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | |
| 1720 | |
| 1721 | pub(super) fn fill_lf_grid_leaf_luma( |
| 1722 | &mut self, |
| 1723 | at_mi: (usize, usize), |
| 1724 | w_mi: usize, |
| 1725 | h_mi: usize, |
| 1726 | tx_px: u8, |
| 1727 | tx_h_px: u8, |
| 1728 | ) { |
| 1729 | let (mi_r, mi_c) = at_mi; |
| 1730 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 1731 | eprintln!( |
| 1732 | "EC_LFLEAF mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}" |
| 1733 | ); |
| 1734 | } |
| 1735 | for rr in 0..h_mi { |
| 1736 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 1737 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 1738 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | pub(super) fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { |
| 1747 | self.fill_skip_grid_rect(at_mi, side_mi, side_mi, skip); |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | pub(super) fn fill_skip_grid_rect(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize, skip: bool) { |
| 1752 | let (mi_r, mi_c) = at_mi; |
| 1753 | if skip && w_mi != h_mi { |
| 1754 | hit!(RECT_SKIP_BAND_HITS); |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | crate::census::block_intra_frame(w_mi, h_mi, skip); |
| 1759 | for rr in 0..h_mi { |
| 1760 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 1761 | fill_span( |
| 1762 | &mut self.blk_grid, |
| 1763 | start, |
| 1764 | w_mi, |
| 1765 | BlkCell { |
| 1766 | skip, |
| 1767 | written: true, |
| 1768 | org: [mi_r as u16, mi_c as u16], |
| 1769 | dim: [w_mi as u8, h_mi as u8], |
| 1770 | }, |
| 1771 | ); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | pub(super) fn mark_inter_grid_rect( |
| 1787 | &mut self, |
| 1788 | at_mi: (usize, usize), |
| 1789 | w_mi: usize, |
| 1790 | h_mi: usize, |
| 1791 | is_inter: bool, |
| 1792 | ) { |
| 1793 | for rr in 0..h_mi { |
| 1794 | let start = (at_mi.0 + rr) * self.skip_grid_cols_mi + at_mi.1; |
| 1795 | fill_span(&mut self.inter_grid, start, w_mi, is_inter); |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | |
| 1800 | |
| 1801 | |
| 1802 | |
| 1803 | pub(super) fn inter_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 1804 | self.inter_grid |
| 1805 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 1806 | .copied() |
| 1807 | .unwrap_or(false) |
| 1808 | } |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | pub(super) fn blk_side_at(&self, mi_r: usize, mi_c: usize) -> (usize, usize) { |
| 1818 | self.blk_grid |
| 1819 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 1820 | .filter(|c| c.written) |
| 1821 | .map_or((0, 0), |c| (c.dim[0] as usize * MI, c.dim[1] as usize * MI)) |
| 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | pub(super) fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 1834 | self.blk_grid |
| 1835 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 1836 | .is_some_and(|c| c.skip) |
| 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | pub(super) fn skip_txfm_ctx(&self, mi_r: usize, mi_c: usize) -> usize { |
| 1845 | usize::from(mi_r > self.tile_row0_mi && self.skip_at(mi_r - 1, mi_c)) |
| 1846 | + usize::from(mi_c > self.tile_col0_mi && self.skip_at(mi_r, mi_c - 1)) |
| 1847 | } |
| 1848 | |
| 1849 | pub(super) fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { |
| 1850 | (0..2).all(|rr| { |
| 1851 | (0..2).all(|cc| { |
| 1852 | self.blk_grid |
| 1853 | .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 1854 | .is_some_and(|c| c.skip) |
| 1855 | }) |
| 1856 | }) |
| 1857 | } |
| 1858 | |
| 1859 | pub(super) fn start_row(&mut self) { |
| 1860 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 1861 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 1862 | self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 1863 | self.left_side |
| 1864 | .iter_mut() |
| 1865 | .for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 1866 | self.left_side_mi |
| 1867 | .iter_mut() |
| 1868 | .for_each(|s| *s = NO_NEIGHBOUR_SIDE as u8); |
| 1869 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 1870 | self.left_skip_mode.iter_mut().for_each(|s| *s = false); |
| 1871 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 1872 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 1873 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 1874 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 1875 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 1876 | self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); |
| 1877 | |
| 1878 | |
| 1879 | self.sub8_mode_row |
| 1880 | .iter_mut() |
| 1881 | .for_each(|s| *s = (u16::MAX, 0)); |
| 1882 | self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0)); |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| 1888 | self.left_palette_colors |
| 1889 | .iter_mut() |
| 1890 | .for_each(|c| *c = [0u16; 8]); |
| 1891 | self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0); |
| 1892 | self.left_palette_uv_colors |
| 1893 | .iter_mut() |
| 1894 | .for_each(|c| *c = [0u16; 8]); |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 1901 | } |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 | |
| 1910 | #[allow(dead_code)] |
| 1911 | |
| 1912 | #[allow(clippy::too_many_arguments)] |
| 1913 | pub(super) fn record_inter( |
| 1914 | &mut self, |
| 1915 | at: (usize, usize), |
| 1916 | side: usize, |
| 1917 | skip: bool, |
| 1918 | is_inter: bool, |
| 1919 | ref_frame: i8, |
| 1920 | filter: [u8; 2], |
| 1921 | skip_mode: bool, |
| 1922 | ) { |
| 1923 | self.record_inter_rect(at, side, side, skip, is_inter, ref_frame, filter, skip_mode); |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | #[allow(clippy::too_many_arguments)] |
| 1932 | #[allow(dead_code)] |
| 1933 | pub(super) fn record_inter_rect( |
| 1934 | &mut self, |
| 1935 | at: (usize, usize), |
| 1936 | w_sub: usize, |
| 1937 | h_sub: usize, |
| 1938 | skip: bool, |
| 1939 | is_inter: bool, |
| 1940 | ref_frame: i8, |
| 1941 | filter: [u8; 2], |
| 1942 | skip_mode: bool, |
| 1943 | ) { |
| 1944 | let (r, c) = at; |
| 1945 | self.record_inter_rect_mi( |
| 1946 | (r * (SUB / MI), c * (SUB / MI)), |
| 1947 | w_sub / MI, |
| 1948 | h_sub / MI, |
| 1949 | skip, |
| 1950 | is_inter, |
| 1951 | ref_frame, |
| 1952 | filter, |
| 1953 | skip_mode, |
| 1954 | ); |
| 1955 | } |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | #[allow(clippy::too_many_arguments)] |
| 1962 | pub(super) fn record_inter_rect_mi( |
| 1963 | &mut self, |
| 1964 | at_mi: (usize, usize), |
| 1965 | w_mi: usize, |
| 1966 | h_mi: usize, |
| 1967 | skip: bool, |
| 1968 | is_inter: bool, |
| 1969 | ref_frame: i8, |
| 1970 | filter: [u8; 2], |
| 1971 | skip_mode: bool, |
| 1972 | ) { |
| 1973 | let (r, c) = at_mi; |
| 1974 | crate::census::block(w_mi, h_mi, is_inter, ref_frame, skip); |
| 1975 | |
| 1976 | |
| 1977 | |
| 1978 | |
| 1979 | |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | |
| 1986 | |
| 1987 | |
| 1988 | if is_inter { |
| 1989 | self.record_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 1990 | self.record_uv_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 1991 | } |
| 1992 | |
| 1993 | |
| 1994 | self.mark_inter_grid_rect(at_mi, w_mi, h_mi, is_inter); |
| 1995 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 1996 | self.above_skip[c..c + w_mi].fill(skip); |
| 1997 | self.above_skip_mode[c..c + w_mi].fill(skip_mode); |
| 1998 | self.above_inter[c..c + w_mi].fill(is_inter); |
| 1999 | self.above_ref[c..c + w_mi].fill(ref_frame); |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | self.above_ref1[c..c + w_mi].fill(None); |
| 2004 | |
| 2005 | |
| 2006 | |
| 2007 | |
| 2008 | |
| 2009 | self.above_comp_group_idx[c..c + w_mi].fill(if altref { 3 } else { 0 }); |
| 2010 | self.above_compound_idx[c..c + w_mi].fill(u8::from(altref)); |
| 2011 | self.above_filter[c..c + w_mi].fill(filter); |
| 2012 | self.left_skip[r..r + h_mi].fill(skip); |
| 2013 | self.left_skip_mode[r..r + h_mi].fill(skip_mode); |
| 2014 | self.left_inter[r..r + h_mi].fill(is_inter); |
| 2015 | self.left_ref[r..r + h_mi].fill(ref_frame); |
| 2016 | self.left_ref1[r..r + h_mi].fill(None); |
| 2017 | self.left_comp_group_idx[r..r + h_mi].fill(if altref { 3 } else { 0 }); |
| 2018 | self.left_compound_idx[r..r + h_mi].fill(u8::from(altref)); |
| 2019 | self.left_filter[r..r + h_mi].fill(filter); |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | |
| 2025 | |
| 2026 | |
| 2027 | |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | |
| 2032 | #[allow(dead_code)] |
| 2033 | pub(super) fn record_compound_ctx( |
| 2034 | &mut self, |
| 2035 | at: (usize, usize), |
| 2036 | side: usize, |
| 2037 | ref1: i8, |
| 2038 | comp_group_idx: u8, |
| 2039 | compound_idx: u8, |
| 2040 | ) { |
| 2041 | self.record_compound_ctx_rect(at, side, side, ref1, comp_group_idx, compound_idx); |
| 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | |
| 2046 | #[allow(clippy::too_many_arguments)] |
| 2047 | #[allow(dead_code)] |
| 2048 | pub(super) fn record_compound_ctx_rect( |
| 2049 | &mut self, |
| 2050 | at: (usize, usize), |
| 2051 | w_sub: usize, |
| 2052 | h_sub: usize, |
| 2053 | ref1: i8, |
| 2054 | comp_group_idx: u8, |
| 2055 | compound_idx: u8, |
| 2056 | ) { |
| 2057 | let (r, c) = at; |
| 2058 | self.record_compound_ctx_rect_mi( |
| 2059 | (r * (SUB / MI), c * (SUB / MI)), |
| 2060 | w_sub / MI, |
| 2061 | h_sub / MI, |
| 2062 | ref1, |
| 2063 | comp_group_idx, |
| 2064 | compound_idx, |
| 2065 | ); |
| 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | |
| 2070 | pub(super) fn record_compound_ctx_rect_mi( |
| 2071 | &mut self, |
| 2072 | at_mi: (usize, usize), |
| 2073 | w_mi: usize, |
| 2074 | h_mi: usize, |
| 2075 | ref1: i8, |
| 2076 | comp_group_idx: u8, |
| 2077 | compound_idx: u8, |
| 2078 | ) { |
| 2079 | let (r, c) = at_mi; |
| 2080 | crate::census::compound(); |
| 2081 | for cell in 0..w_mi { |
| 2082 | self.above_ref1[c + cell] = Some(ref1); |
| 2083 | self.above_comp_group_idx[c + cell] = comp_group_idx; |
| 2084 | self.above_compound_idx[c + cell] = compound_idx; |
| 2085 | } |
| 2086 | for cell in 0..h_mi { |
| 2087 | self.left_ref1[r + cell] = Some(ref1); |
| 2088 | self.left_comp_group_idx[r + cell] = comp_group_idx; |
| 2089 | self.left_compound_idx[r + cell] = compound_idx; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | |
| 2099 | pub(super) fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 2100 | let (r, c) = at; |
| 2101 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | pub(super) fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 2109 | 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side) |
| 2110 | + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side) |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | |
| 2115 | pub(super) fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 2116 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | pub(super) fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 2122 | let side_mi = side / MI; |
| 2123 | |
| 2124 | |
| 2125 | |
| 2126 | |
| 2127 | |
| 2128 | |
| 2129 | let above_mi = side_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 2130 | let left_mi = side_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 2131 | let mut out = [(false, false, 0i32); 3]; |
| 2132 | for cell in 0..above_mi { |
| 2133 | let above = &self.above[mi_c + cell]; |
| 2134 | for (plane, o) in out.iter_mut().enumerate() { |
| 2135 | o.0 |= above[plane].level != 0; |
| 2136 | o.2 += dc_vote(above[plane].dc); |
| 2137 | } |
| 2138 | } |
| 2139 | for cell in 0..left_mi { |
| 2140 | let left = &self.left[mi_r + cell]; |
| 2141 | for (plane, o) in out.iter_mut().enumerate() { |
| 2142 | o.1 |= left[plane].level != 0; |
| 2143 | o.2 += dc_vote(left[plane].dc); |
| 2144 | } |
| 2145 | } |
| 2146 | out |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | |
| 2151 | |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | |
| 2158 | |
| 2159 | |
| 2160 | |
| 2161 | |
| 2162 | pub(super) fn around_mi_422_chroma( |
| 2163 | &self, |
| 2164 | (mi_r, mi_c): (usize, usize), |
| 2165 | w: usize, |
| 2166 | h: usize, |
| 2167 | ) -> [(bool, bool, i32); 3] { |
| 2168 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 2169 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 2170 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 2171 | let mut out = [(false, false, 0i32); 3]; |
| 2172 | for cell in (0..above_mi).step_by(2) { |
| 2173 | let above = &self.above[mi_c + cell]; |
| 2174 | for (plane, o) in out.iter_mut().enumerate() { |
| 2175 | o.0 |= above[plane].level != 0; |
| 2176 | o.2 += dc_vote(above[plane].dc); |
| 2177 | } |
| 2178 | } |
| 2179 | for cell in 0..left_mi { |
| 2180 | let left = &self.left[mi_r + cell]; |
| 2181 | for (plane, o) in out.iter_mut().enumerate() { |
| 2182 | o.1 |= left[plane].level != 0; |
| 2183 | o.2 += dc_vote(left[plane].dc); |
| 2184 | } |
| 2185 | } |
| 2186 | if crate::envflags::env_flag!("EC_DCDUMP") { |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | for (plane, o) in out.iter().enumerate() { |
| 2191 | let vote = o.2; |
| 2192 | let ab: Vec<String> = (0..above_mi) |
| 2193 | .step_by(2) |
| 2194 | .map(|k| { |
| 2195 | format!( |
| 2196 | "{:?}/{}", |
| 2197 | self.above[mi_c + k][plane].dc, |
| 2198 | self.above[mi_c + k][plane].level |
| 2199 | ) |
| 2200 | }) |
| 2201 | .collect(); |
| 2202 | let lf: Vec<String> = (0..left_mi) |
| 2203 | .map(|k| { |
| 2204 | format!( |
| 2205 | "{:?}/{}", |
| 2206 | self.left[mi_r + k][plane].dc, |
| 2207 | self.left[mi_r + k][plane].level |
| 2208 | ) |
| 2209 | }) |
| 2210 | .collect(); |
| 2211 | eprintln!( |
| 2212 | "EC_DCDUMP422 mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", |
| 2213 | ab.join(","), |
| 2214 | lf.join(",") |
| 2215 | ); |
| 2216 | } |
| 2217 | } |
| 2218 | out |
| 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | |
| 2224 | pub(super) fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 2225 | self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) |
| 2226 | } |
| 2227 | |
| 2228 | |
| 2229 | |
| 2230 | pub(super) fn around_mi_rect( |
| 2231 | &self, |
| 2232 | (mi_r, mi_c): (usize, usize), |
| 2233 | w: usize, |
| 2234 | h: usize, |
| 2235 | ) -> [(bool, bool, i32); 3] { |
| 2236 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 2237 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 2238 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 2239 | |
| 2240 | let mut out = [(false, false, 0i32); 3]; |
| 2241 | for cell in 0..above_mi { |
| 2242 | let above = &self.above[mi_c + cell]; |
| 2243 | for (plane, o) in out.iter_mut().enumerate() { |
| 2244 | o.0 |= above[plane].level != 0; |
| 2245 | o.2 += dc_vote(above[plane].dc); |
| 2246 | } |
| 2247 | } |
| 2248 | for cell in 0..left_mi { |
| 2249 | let left = &self.left[mi_r + cell]; |
| 2250 | for (plane, o) in out.iter_mut().enumerate() { |
| 2251 | o.1 |= left[plane].level != 0; |
| 2252 | o.2 += dc_vote(left[plane].dc); |
| 2253 | } |
| 2254 | } |
| 2255 | if crate::envflags::env_flag!("EC_DCDUMP") { |
| 2256 | for (plane, o) in out.iter().enumerate() { |
| 2257 | let vote = o.2; |
| 2258 | let ab: Vec<String> = (0..above_mi) |
| 2259 | .map(|k| { |
| 2260 | format!( |
| 2261 | "{:?}/{}", |
| 2262 | self.above[mi_c + k][plane].dc, |
| 2263 | self.above[mi_c + k][plane].level |
| 2264 | ) |
| 2265 | }) |
| 2266 | .collect(); |
| 2267 | let lf: Vec<String> = (0..left_mi) |
| 2268 | .map(|k| { |
| 2269 | format!( |
| 2270 | "{:?}/{}", |
| 2271 | self.left[mi_r + k][plane].dc, |
| 2272 | self.left[mi_r + k][plane].level |
| 2273 | ) |
| 2274 | }) |
| 2275 | .collect(); |
| 2276 | eprintln!( |
| 2277 | "EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", |
| 2278 | ab.join(","), |
| 2279 | lf.join(",") |
| 2280 | ); |
| 2281 | } |
| 2282 | } |
| 2283 | out |
| 2284 | } |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | |
| 2293 | pub(super) fn luma_skip_ctx(&self, at_mi: (usize, usize), side_mi: usize) -> usize { |
| 2294 | self.luma_skip_ctx_rect(at_mi, side_mi, side_mi) |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | pub(super) fn luma_skip_ctx_rect(&self, (mi_r, mi_c): (usize, usize), w_mi: usize, h_mi: usize) -> usize { |
| 2301 | let side_mi = w_mi; |
| 2302 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 2303 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 2304 | let mut top = 0u8; |
| 2305 | let mut left = 0u8; |
| 2306 | for cell in 0..above_mi { |
| 2307 | top |= self.above[mi_c + cell][0].level; |
| 2308 | } |
| 2309 | for cell in 0..left_mi { |
| 2310 | left |= self.left[mi_r + cell][0].level; |
| 2311 | } |
| 2312 | let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; |
| 2313 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 2314 | let ab: Vec<String> = (0..above_mi) |
| 2315 | .map(|k| self.above[mi_c + k][0].level.to_string()) |
| 2316 | .collect(); |
| 2317 | let lf: Vec<String> = (0..left_mi) |
| 2318 | .map(|k| self.left[mi_r + k][0].level.to_string()) |
| 2319 | .collect(); |
| 2320 | |
| 2321 | |
| 2322 | |
| 2323 | |
| 2324 | let recent = crate::msac::symtrace::ecdump_recent(4) |
| 2325 | .iter() |
| 2326 | .map(|(_low, value, rng, bytepos)| format!("({value},{rng},{bytepos})")) |
| 2327 | .collect::<Vec<_>>() |
| 2328 | .join(" "); |
| 2329 | eprintln!( |
| 2330 | "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]", |
| 2331 | ab.join(","), |
| 2332 | lf.join(","), |
| 2333 | recent |
| 2334 | ); |
| 2335 | } |
| 2336 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 2337 | eprintln!( |
| 2338 | "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx} band={:?}", |
| 2339 | self.left.iter().map(|e| e[0].level).collect::<Vec<_>>() |
| 2340 | ); |
| 2341 | } |
| 2342 | ctx |
| 2343 | } |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | pub(super) fn record( |
| 2353 | &mut self, |
| 2354 | at: (usize, usize), |
| 2355 | side: usize, |
| 2356 | mode: usize, |
| 2357 | uv_mode: usize, |
| 2358 | grids: &[&[i32]; 3], |
| 2359 | ) { |
| 2360 | self.record_rect(at, side, side, mode, uv_mode, grids); |
| 2361 | } |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | |
| 2370 | |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | pub(super) fn record_mode_mi(&mut self, mi_r: usize, mi_c: usize, mi_w: usize, mi_h: usize, mode: usize) { |
| 2377 | let (last_r, last_c) = ((mi_r + mi_h - 1) as u16, (mi_c + mi_w - 1) as u16); |
| 2378 | let mode = mode as u8; |
| 2379 | for cell in 0..mi_w { |
| 2380 | if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { |
| 2381 | *slot = (last_r, mode); |
| 2382 | } |
| 2383 | } |
| 2384 | for cell in 0..mi_h { |
| 2385 | if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { |
| 2386 | *slot = (last_c, mode); |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | pub(super) fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 2396 | match self.sub8_mode_col.get(mi_c) { |
| 2397 | Some(&(row, m)) if mi_r > self.tile_row0_mi && usize::from(row) == mi_r - 1 => { |
| 2398 | Some(usize::from(m)) |
| 2399 | } |
| 2400 | _ => None, |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | pub(super) fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 2405 | match self.sub8_mode_row.get(mi_r) { |
| 2406 | Some(&(col, m)) if mi_c > self.tile_col0_mi && usize::from(col) == mi_c - 1 => { |
| 2407 | Some(usize::from(m)) |
| 2408 | } |
| 2409 | _ => None, |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | pub(super) fn record_uv_mode_mi( |
| 2416 | &mut self, |
| 2417 | mi_r: usize, |
| 2418 | mi_c: usize, |
| 2419 | mi_w: usize, |
| 2420 | mi_h: usize, |
| 2421 | uv_mode: usize, |
| 2422 | ) { |
| 2423 | let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); |
| 2424 | fill_span( |
| 2425 | &mut self.uv_mode_col, |
| 2426 | mi_c, |
| 2427 | mi_w, |
| 2428 | (last_r as u16, uv_mode as u8), |
| 2429 | ); |
| 2430 | fill_span( |
| 2431 | &mut self.uv_mode_row, |
| 2432 | mi_r, |
| 2433 | mi_h, |
| 2434 | (last_c as u16, uv_mode as u8), |
| 2435 | ); |
| 2436 | |
| 2437 | |
| 2438 | let stride = self.uv_grid_cols; |
| 2439 | let n = stride.saturating_sub(mi_c).min(last_c + 1 - mi_c); |
| 2440 | for row in mi_r..=last_r { |
| 2441 | fill_span( |
| 2442 | &mut self.uv_mode_grid, |
| 2443 | row * stride + mi_c, |
| 2444 | n, |
| 2445 | uv_mode as u8, |
| 2446 | ); |
| 2447 | } |
| 2448 | |
| 2449 | |
| 2450 | let owner = UV_OWNER_EPOCH.with(|c| c.get()) | ((mi_r as u32) << 12) | mi_c as u32; |
| 2451 | for row in mi_r..=last_r { |
| 2452 | fill_span(&mut self.uv_owner, row * stride + mi_c, n, owner); |
| 2453 | } |
| 2454 | if crate::envflags::env_flag!("EC_LEFTSSY") { |
| 2455 | eprintln!("EC_LEAF mi=({mi_r},{mi_c}) wh=({mi_w},{mi_h}) uv_mode={uv_mode}"); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | |
| 2460 | |
| 2461 | |
| 2462 | |
| 2463 | pub(super) fn smooth_uv_neighbour( |
| 2464 | &self, |
| 2465 | mi_r: usize, |
| 2466 | mi_c: usize, |
| 2467 | r: usize, |
| 2468 | c: usize, |
| 2469 | fctx: &crate::decode::FrameCtx, |
| 2470 | ) -> bool { |
| 2471 | |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | |
| 2483 | |
| 2484 | |
| 2485 | |
| 2486 | |
| 2487 | |
| 2488 | |
| 2489 | |
| 2490 | |
| 2491 | |
| 2492 | let snap_y = (1usize << ss_y(fctx)) - 1; |
| 2493 | let snap_x = (1usize << ss_x(fctx)) - 1; |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | |
| 2498 | |
| 2499 | self.smooth_uv_neighbour_unsnapped( |
| 2500 | mi_r & !snap_y, |
| 2501 | mi_c & !snap_x, |
| 2502 | r, |
| 2503 | c, |
| 2504 | ss_x(fctx), |
| 2505 | ss_y(fctx), |
| 2506 | true, |
| 2507 | ) |
| 2508 | } |
| 2509 | |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | |
| 2523 | |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | |
| 2528 | #[allow(clippy::too_many_arguments)] |
| 2529 | pub(super) fn smooth_uv_neighbour_unsnapped( |
| 2530 | &self, |
| 2531 | mi_r: usize, |
| 2532 | mi_c: usize, |
| 2533 | r: usize, |
| 2534 | c: usize, |
| 2535 | ss_x: usize, |
| 2536 | ss_y: usize, |
| 2537 | decode_path: bool, |
| 2538 | ) -> bool { |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | |
| 2543 | |
| 2544 | |
| 2545 | |
| 2546 | let have_above = mi_r > self.tile_row0_mi; |
| 2547 | let have_left = mi_c > self.tile_col0_mi; |
| 2548 | if self.tile_row0_mi > 0 && !have_above && self.above_uv_mode[c] != DC_PRED |
| 2549 | || self.tile_col0_mi > 0 && !have_left && self.left_uv_mode[r] != DC_PRED |
| 2550 | { |
| 2551 | hit!(UV_TILE_EDGE_SUPPRESSED_HITS); |
| 2552 | } |
| 2553 | |
| 2554 | |
| 2555 | let cell = |grid: &Vec<u8>, row: usize, col: usize| -> Option<usize> { |
| 2556 | (col < self.uv_grid_cols) |
| 2557 | .then(|| grid.get(row * self.uv_grid_cols + col).copied()) |
| 2558 | .flatten() |
| 2559 | .filter(|&m| m != u8::MAX) |
| 2560 | .map(usize::from) |
| 2561 | }; |
| 2562 | |
| 2563 | |
| 2564 | |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | |
| 2569 | |
| 2570 | |
| 2571 | |
| 2572 | |
| 2573 | |
| 2574 | |
| 2575 | |
| 2576 | |
| 2577 | |
| 2578 | |
| 2579 | |
| 2580 | |
| 2581 | |
| 2582 | |
| 2583 | |
| 2584 | |
| 2585 | |
| 2586 | |
| 2587 | |
| 2588 | |
| 2589 | |
| 2590 | |
| 2591 | |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | |
| 2596 | |
| 2597 | let above_mi = (mi_r.saturating_sub(1), mi_c + ss_x); |
| 2598 | |
| 2599 | |
| 2600 | |
| 2601 | |
| 2602 | |
| 2603 | |
| 2604 | |
| 2605 | |
| 2606 | |
| 2607 | |
| 2608 | |
| 2609 | |
| 2610 | |
| 2611 | |
| 2612 | |
| 2613 | |
| 2614 | |
| 2615 | |
| 2616 | let left_mi = (mi_r + ss_y, mi_c.saturating_sub(1)); |
| 2617 | let read_left = |row: usize| -> usize { |
| 2618 | match self.uv_mode_row.get(row) { |
| 2619 | _ if have_left && let Some(m) = cell(&self.uv_mode_grid, row, left_mi.1) => m, |
| 2620 | Some(&(col, m)) if have_left && usize::from(col) == left_mi.1 => usize::from(m), |
| 2621 | _ if have_left => self.left_uv_mode[r], |
| 2622 | _ => DC_PRED, |
| 2623 | } |
| 2624 | }; |
| 2625 | let above = match self.uv_mode_col.get(above_mi.1) { |
| 2626 | _ if have_above && let Some(m) = cell(&self.uv_mode_grid, above_mi.0, above_mi.1) => m, |
| 2627 | Some(&(row, m)) if have_above && usize::from(row) == above_mi.0 => usize::from(m), |
| 2628 | _ if have_above => self.above_uv_mode[c], |
| 2629 | _ => DC_PRED, |
| 2630 | }; |
| 2631 | let left = read_left(left_mi.0); |
| 2632 | |
| 2633 | |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | |
| 2638 | |
| 2639 | |
| 2640 | if decode_path && have_left && ss_y > 0 { |
| 2641 | hit!(UV_LEFT_SS_Y_READS); |
| 2642 | |
| 2643 | |
| 2644 | |
| 2645 | let own_row = read_left(mi_r); |
| 2646 | if own_row != left { |
| 2647 | hit!(UV_LEFT_SS_Y_MODE_DIFF_HITS); |
| 2648 | } |
| 2649 | if is_smooth_mode(own_row) != is_smooth_mode(left) { |
| 2650 | hit!(UV_LEFT_SS_Y_SMOOTH_DIFF_HITS); |
| 2651 | } |
| 2652 | |
| 2653 | |
| 2654 | |
| 2655 | let col = left_mi.1; |
| 2656 | let epoch = UV_OWNER_EPOCH.with(|c| c.get()) & 0xff00_0000; |
| 2657 | let own = (col < self.uv_grid_cols) |
| 2658 | .then(|| self.uv_owner.get(mi_r * self.uv_grid_cols + col).copied()) |
| 2659 | .flatten() |
| 2660 | .filter(|o| o & 0xff00_0000 == epoch) |
| 2661 | .unwrap_or(0); |
| 2662 | let down = self |
| 2663 | .uv_owner |
| 2664 | .get((mi_r + ss_y) * self.uv_grid_cols + col) |
| 2665 | .copied() |
| 2666 | .filter(|o| o & 0xff00_0000 == epoch) |
| 2667 | .unwrap_or(0); |
| 2668 | if own != 0 && down != 0 && own != down { |
| 2669 | hit!(UV_LEFT_1MI_SPLIT_HITS); |
| 2670 | if crate::envflags::env_flag!("EC_LEFTSSY") { |
| 2671 | eprintln!( |
| 2672 | "EC_LEFTSSY epoch={} mi=({mi_r},{mi_c}) left_col={col} \ |
| 2673 | owners=({},{})->({},{}) modes={own_row}/{left}", |
| 2674 | epoch >> 24, |
| 2675 | own >> 12 & 0xfff, |
| 2676 | own & 0xfff, |
| 2677 | down >> 12 & 0xfff, |
| 2678 | down & 0xfff, |
| 2679 | ); |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| { |
| 2684 | !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if usize::from(row) == mi_r - 1 && usize::from(cm) == m) |
| 2685 | }) || have_left && cell(&self.uv_mode_grid, left_mi.0, left_mi.1).is_some_and(|m| { |
| 2686 | !matches!(self.uv_mode_row.get(left_mi.0), Some(&(col, cm)) if usize::from(col) == left_mi.1 && usize::from(cm) == m) |
| 2687 | }) { |
| 2688 | hit!(UV_MODE_GRID_OVERRIDE_HITS); |
| 2689 | } |
| 2690 | |
| 2691 | |
| 2692 | |
| 2693 | if above != self.above_uv_mode[c] || left != self.left_uv_mode[r] { |
| 2694 | hit!(UV_MODE_MI_OVERRIDE_HITS); |
| 2695 | } |
| 2696 | if (is_smooth_mode(self.above_uv_mode[c]) && !is_smooth_mode(above)) |
| 2697 | || (is_smooth_mode(self.left_uv_mode[r]) && !is_smooth_mode(left)) |
| 2698 | { |
| 2699 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 2700 | } |
| 2701 | is_smooth_mode(above) || is_smooth_mode(left) |
| 2702 | } |
| 2703 | |
| 2704 | |
| 2705 | |
| 2706 | |
| 2707 | |
| 2708 | |
| 2709 | |
| 2710 | |
| 2711 | |
| 2712 | |
| 2713 | |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | |
| 2719 | |
| 2720 | |
| 2721 | pub(super) fn modes_above_left_mi( |
| 2722 | &self, |
| 2723 | mi_r: usize, |
| 2724 | mi_c: usize, |
| 2725 | fallback: (usize, usize), |
| 2726 | ) -> (usize, usize) { |
| 2727 | let above = if mi_r > self.tile_row0_mi { |
| 2728 | self.mode_above_mi(mi_r, mi_c).unwrap_or(fallback.0) |
| 2729 | } else { |
| 2730 | DC_PRED |
| 2731 | }; |
| 2732 | let left = if mi_c > self.tile_col0_mi { |
| 2733 | self.mode_left_mi(mi_r, mi_c).unwrap_or(fallback.1) |
| 2734 | } else { |
| 2735 | DC_PRED |
| 2736 | }; |
| 2737 | (above, left) |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | |
| 2742 | |
| 2743 | |
| 2744 | |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | |
| 2749 | pub(super) fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { |
| 2750 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 2751 | let (above, left) = |
| 2752 | self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r])); |
| 2753 | if above != self.above_mode[c] || left != self.left_mode[r] { |
| 2754 | hit!(MODE_MI_OVERRIDE_HITS); |
| 2755 | } |
| 2756 | if (is_smooth_mode(self.above_mode[c]) && !is_smooth_mode(above)) |
| 2757 | || (is_smooth_mode(self.left_mode[r]) && !is_smooth_mode(left)) |
| 2758 | { |
| 2759 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) |
| 2764 | || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) |
| 2765 | { |
| 2766 | hit!(MODE_TILE_EDGE_COARSE_LEAKS); |
| 2767 | } |
| 2768 | (above, left) |
| 2769 | } |
| 2770 | |
| 2771 | pub(super) fn record_rect( |
| 2772 | &mut self, |
| 2773 | at: (usize, usize), |
| 2774 | w: usize, |
| 2775 | h: usize, |
| 2776 | mode: usize, |
| 2777 | uv_mode: usize, |
| 2778 | grids: &[&[i32]; 3], |
| 2779 | ) { |
| 2780 | self.record_rect_mi(sub16_to_mi(at), w, h, mode, uv_mode, grids); |
| 2781 | } |
| 2782 | |
| 2783 | |
| 2784 | |
| 2785 | |
| 2786 | |
| 2787 | |
| 2788 | pub(super) fn record_rect_mi( |
| 2789 | &mut self, |
| 2790 | at_mi: (usize, usize), |
| 2791 | w: usize, |
| 2792 | h: usize, |
| 2793 | mode: usize, |
| 2794 | uv_mode: usize, |
| 2795 | grids: &[&[i32]; 3], |
| 2796 | ) { |
| 2797 | self.record_rect_mi_modes(at_mi, w, h, mode, uv_mode); |
| 2798 | self.record_mi_rect(at_mi, w, h, grids); |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | |
| 2803 | pub(super) fn record_rect_mi_modes( |
| 2804 | &mut self, |
| 2805 | at_mi: (usize, usize), |
| 2806 | w: usize, |
| 2807 | h: usize, |
| 2808 | mode: usize, |
| 2809 | uv_mode: usize, |
| 2810 | ) { |
| 2811 | let (r, c) = (at_mi.0 / (SUB / MI), at_mi.1 / (SUB / MI)); |
| 2812 | |
| 2813 | |
| 2814 | |
| 2815 | for cell in 0..w / SUB { |
| 2816 | if c + cell >= self.above_mode.len() { |
| 2817 | break; |
| 2818 | } |
| 2819 | self.above_mode[c + cell] = mode; |
| 2820 | self.above_uv_mode[c + cell] = uv_mode; |
| 2821 | self.above_side[c + cell] = w; |
| 2822 | } |
| 2823 | for cell in 0..h / SUB { |
| 2824 | if r + cell >= self.left_mode.len() { |
| 2825 | break; |
| 2826 | } |
| 2827 | self.left_mode[r + cell] = mode; |
| 2828 | self.left_uv_mode[r + cell] = uv_mode; |
| 2829 | self.left_side[r + cell] = h; |
| 2830 | } |
| 2831 | self.record_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, mode); |
| 2832 | self.record_uv_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, uv_mode); |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | |
| 2837 | |
| 2838 | |
| 2839 | |
| 2840 | |
| 2841 | |
| 2842 | pub(super) fn record_rect_mi_luma_only( |
| 2843 | &mut self, |
| 2844 | at_mi: (usize, usize), |
| 2845 | w: usize, |
| 2846 | h: usize, |
| 2847 | mode: usize, |
| 2848 | uv_mode: usize, |
| 2849 | luma_grid: &[i32], |
| 2850 | ) { |
| 2851 | self.record_rect_mi_modes(at_mi, w, h, mode, uv_mode); |
| 2852 | fill_span(&mut self.left_side_mi, at_mi.0, h / MI, h as u8); |
| 2853 | fill_span(&mut self.above_side_mi, at_mi.1, w / MI, w as u8); |
| 2854 | self.record_mi_luma_rect(at_mi, w, h, luma_grid); |
| 2855 | } |
| 2856 | |
| 2857 | |
| 2858 | |
| 2859 | |
| 2860 | |
| 2861 | pub(super) fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[&[i32]; 3]) { |
| 2862 | self.record_mi_rect(at_mi, side, side, grids); |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | pub(super) fn record_mi_rect(&mut self, at_mi: (usize, usize), w: usize, h: usize, grids: &[&[i32]; 3]) { |
| 2868 | let (mi_r, mi_c) = at_mi; |
| 2869 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(grids[plane])); |
| 2870 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 2871 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 2872 | eprintln!( |
| 2873 | "EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", |
| 2874 | states[0].level |
| 2875 | ); |
| 2876 | } |
| 2877 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 2878 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 2879 | |
| 2880 | |
| 2881 | |
| 2882 | |
| 2883 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 2884 | let bound_h = [ |
| 2885 | self.mi_rows, |
| 2886 | round_up_even(self.mi_rows), |
| 2887 | round_up_even(self.mi_rows), |
| 2888 | ]; |
| 2889 | let bound_w = [ |
| 2890 | self.mi_cols, |
| 2891 | round_up_even(self.mi_cols), |
| 2892 | round_up_even(self.mi_cols), |
| 2893 | ]; |
| 2894 | |
| 2895 | |
| 2896 | |
| 2897 | |
| 2898 | |
| 2899 | |
| 2900 | |
| 2901 | let luma_h = h_mi.min(bound_h[0].saturating_sub(mi_r)); |
| 2902 | let chroma_h = h_mi.min(bound_h[1].saturating_sub(mi_r)); |
| 2903 | let uv_only = [Neighbour::default(), states[1], states[2]]; |
| 2904 | fill_span(&mut self.left, mi_r, luma_h, states); |
| 2905 | fill_span(&mut self.left, mi_r + luma_h, chroma_h - luma_h, uv_only); |
| 2906 | fill_span( |
| 2907 | &mut self.left, |
| 2908 | mi_r + chroma_h, |
| 2909 | h_mi - chroma_h, |
| 2910 | [Neighbour::default(); 3], |
| 2911 | ); |
| 2912 | let luma_w = w_mi.min(bound_w[0].saturating_sub(mi_c)); |
| 2913 | let chroma_w = w_mi.min(bound_w[1].saturating_sub(mi_c)); |
| 2914 | let uv_only_w = [Neighbour::default(), states[1], states[2]]; |
| 2915 | fill_span(&mut self.above, mi_c, luma_w, states); |
| 2916 | fill_span(&mut self.above, mi_c + luma_w, chroma_w - luma_w, uv_only_w); |
| 2917 | fill_span( |
| 2918 | &mut self.above, |
| 2919 | mi_c + chroma_w, |
| 2920 | w_mi - chroma_w, |
| 2921 | [Neighbour::default(); 3], |
| 2922 | ); |
| 2923 | } |
| 2924 | |
| 2925 | |
| 2926 | |
| 2927 | |
| 2928 | |
| 2929 | |
| 2930 | |
| 2931 | |
| 2932 | |
| 2933 | |
| 2934 | pub(super) fn record_mi_luma(&mut self, at_mi: (usize, usize), tx_px: usize, grid: &[i32]) { |
| 2935 | self.record_mi_luma_rect(at_mi, tx_px, tx_px, grid); |
| 2936 | } |
| 2937 | |
| 2938 | |
| 2939 | |
| 2940 | |
| 2941 | |
| 2942 | |
| 2943 | |
| 2944 | |
| 2945 | pub(super) fn record_mi_chroma( |
| 2946 | &mut self, |
| 2947 | (mi_r, mi_c): (usize, usize), |
| 2948 | w_px: usize, |
| 2949 | h_px: usize, |
| 2950 | plane: usize, |
| 2951 | grid: &[i32], |
| 2952 | ) { |
| 2953 | |
| 2954 | |
| 2955 | |
| 2956 | |
| 2957 | |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | |
| 2964 | |
| 2965 | let (step_x, step_y) = (w_px / MI, h_px / MI); |
| 2966 | |
| 2967 | let (row_mid, col_mid) = (mi_r & (step_y - 1) != 0, mi_c & (step_x - 1) != 0); |
| 2968 | if row_mid || col_mid { |
| 2969 | CHROMA_MIDCELL_PUB_HITS.with(|c| { |
| 2970 | let mut v = c.get(); |
| 2971 | v[0] += usize::from(row_mid); |
| 2972 | v[1] += usize::from(col_mid); |
| 2973 | v[2] += usize::from(row_mid && col_mid); |
| 2974 | c.set(v); |
| 2975 | }); |
| 2976 | } |
| 2977 | let (mi_r, mi_c) = (mi_r & !(step_y - 1), mi_c & !(step_x - 1)); |
| 2978 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 2979 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 2980 | let state = neighbour_state(grid); |
| 2981 | if crate::envflags::env_flag!("EC_CHROMA_PUB") { |
| 2982 | eprintln!( |
| 2983 | "EC_CHROMA_PUB plane={plane} mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={} dc={}", |
| 2984 | state.level, |
| 2985 | state.dc.map_or(-1, i32::from) |
| 2986 | ); |
| 2987 | } |
| 2988 | for cell in 0..(h_px / MI).min(bound_h.saturating_sub(mi_r)) { |
| 2989 | self.left[mi_r + cell][plane] = state; |
| 2990 | } |
| 2991 | for cell in 0..(w_px / MI).min(bound_w.saturating_sub(mi_c)) { |
| 2992 | self.above[mi_c + cell][plane] = state; |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | |
| 2997 | |
| 2998 | |
| 2999 | pub(super) fn record_mi_luma_rect( |
| 3000 | &mut self, |
| 3001 | (mi_r, mi_c): (usize, usize), |
| 3002 | w_px: usize, |
| 3003 | h_px: usize, |
| 3004 | grid: &[i32], |
| 3005 | ) { |
| 3006 | let state = neighbour_state(grid); |
| 3007 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 3008 | eprintln!( |
| 3009 | "EC_ECPUB tu mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={}", |
| 3010 | state.level |
| 3011 | ); |
| 3012 | } |
| 3013 | for cell in 0..h_px / MI { |
| 3014 | if mi_r + cell < self.mi_rows { |
| 3015 | self.left[mi_r + cell][0] = state; |
| 3016 | } |
| 3017 | } |
| 3018 | for cell in 0..w_px / MI { |
| 3019 | if mi_c + cell < self.mi_cols { |
| 3020 | self.above[mi_c + cell][0] = state; |
| 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | |
| 3026 | |
| 3027 | |
| 3028 | |
| 3029 | |
| 3030 | pub(super) fn record_split_luma( |
| 3031 | &mut self, |
| 3032 | at: (usize, usize), |
| 3033 | side: usize, |
| 3034 | mode: usize, |
| 3035 | uv_mode: usize, |
| 3036 | chroma_grids: [&[i32]; 2], |
| 3037 | ) { |
| 3038 | self.record_split_luma_rect(at, side, side, mode, uv_mode, chroma_grids); |
| 3039 | } |
| 3040 | |
| 3041 | |
| 3042 | |
| 3043 | |
| 3044 | |
| 3045 | |
| 3046 | pub(super) fn record_split_luma_rect( |
| 3047 | &mut self, |
| 3048 | at: (usize, usize), |
| 3049 | w: usize, |
| 3050 | h: usize, |
| 3051 | mode: usize, |
| 3052 | uv_mode: usize, |
| 3053 | chroma_grids: [&[i32]; 2], |
| 3054 | ) { |
| 3055 | let (r, c) = at; |
| 3056 | self.record_split_luma_rect_mi( |
| 3057 | (r * (SUB / MI), c * (SUB / MI)), |
| 3058 | w, |
| 3059 | h, |
| 3060 | mode, |
| 3061 | uv_mode, |
| 3062 | chroma_grids, |
| 3063 | ); |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | |
| 3068 | |
| 3069 | |
| 3070 | |
| 3071 | pub(super) fn record_split_luma_rect_mi( |
| 3072 | &mut self, |
| 3073 | at_mi: (usize, usize), |
| 3074 | w: usize, |
| 3075 | h: usize, |
| 3076 | mode: usize, |
| 3077 | uv_mode: usize, |
| 3078 | chroma_grids: [&[i32]; 2], |
| 3079 | ) { |
| 3080 | self.record_split_luma_rect_mi_modes(at_mi, w, h, mode, uv_mode); |
| 3081 | let (mi_r, _) = at_mi; |
| 3082 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 3083 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 3084 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 3085 | for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { |
| 3086 | let plane = plane_idx + 1; |
| 3087 | let state = neighbour_state(grid); |
| 3088 | for cell in 0..h_mi { |
| 3089 | if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { |
| 3090 | self.left[mi_r + cell][plane] = state; |
| 3091 | } |
| 3092 | } |
| 3093 | for cell in 0..w_mi { |
| 3094 | if cell < w_mi.min(bound_w.saturating_sub(at_mi.1)) { |
| 3095 | self.above[at_mi.1 + cell][plane] = state; |
| 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | |
| 3102 | |
| 3103 | |
| 3104 | |
| 3105 | pub(super) fn record_split_luma_rect_mi_modes( |
| 3106 | &mut self, |
| 3107 | at_mi: (usize, usize), |
| 3108 | w: usize, |
| 3109 | h: usize, |
| 3110 | mode: usize, |
| 3111 | uv_mode: usize, |
| 3112 | ) { |
| 3113 | let (mi_r, mi_c) = at_mi; |
| 3114 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 3115 | for cell in 0..w.div_ceil(SUB) { |
| 3116 | if c + cell >= self.above_mode.len() { |
| 3117 | break; |
| 3118 | } |
| 3119 | self.above_mode[c + cell] = mode; |
| 3120 | self.above_uv_mode[c + cell] = uv_mode; |
| 3121 | self.above_side[c + cell] = w; |
| 3122 | } |
| 3123 | for cell in 0..h.div_ceil(SUB) { |
| 3124 | if r + cell >= self.left_mode.len() { |
| 3125 | break; |
| 3126 | } |
| 3127 | self.left_mode[r + cell] = mode; |
| 3128 | self.left_uv_mode[r + cell] = uv_mode; |
| 3129 | self.left_side[r + cell] = h; |
| 3130 | } |
| 3131 | |
| 3132 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 3133 | self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode); |
| 3134 | self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode); |
| 3135 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 3136 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | |
| 3141 | |
| 3142 | |
| 3143 | |
| 3144 | |
| 3145 | |
| 3146 | |
| 3147 | |
| 3148 | |
| 3149 | |
| 3150 | |
| 3151 | |
| 3152 | |
| 3153 | |
| 3154 | pub(crate) fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize { |
| 3155 | const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED]; |
| 3156 | filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi]) |
| 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | |
| 3161 | |
| 3162 | pub(crate) fn filter_intra_size_class(side: usize) -> Option<usize> { |
| 3163 | match side { |
| 3164 | 4 => Some(0), |
| 3165 | 8 => Some(1), |
| 3166 | 16 => Some(2), |
| 3167 | 32 => Some(3), |
| 3168 | _ => None, |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | |
| 3173 | |
| 3174 | |
| 3175 | |
| 3176 | pub(super) fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> { |
| 3177 | match (bw, bh) { |
| 3178 | (32, 16) => Some(4), |
| 3179 | (16, 32) => Some(5), |
| 3180 | |
| 3181 | |
| 3182 | |
| 3183 | |
| 3184 | |
| 3185 | (16, 8) => Some(6), |
| 3186 | (8, 16) => Some(7), |
| 3187 | |
| 3188 | |
| 3189 | |
| 3190 | (4, 8) => Some(8), |
| 3191 | (8, 4) => Some(9), |
| 3192 | |
| 3193 | |
| 3194 | |
| 3195 | |
| 3196 | |
| 3197 | |
| 3198 | (32, 8) => Some(10), |
| 3199 | (8, 32) => Some(11), |
| 3200 | |
| 3201 | |
| 3202 | (16, 4) => Some(12), |
| 3203 | (4, 16) => Some(13), |
| 3204 | _ if bw == bh => filter_intra_size_class(bw), |
| 3205 | _ => None, |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | |
| 3211 | |
| 3212 | |
| 3213 | |
| 3214 | pub(crate) fn palette_bsize_ctx(side: usize) -> Option<usize> { |
| 3215 | palette_bsize_ctx_wh(side, side) |
| 3216 | } |
| 3217 | |
| 3218 | |
| 3219 | |
| 3220 | |
| 3221 | |
| 3222 | |
| 3223 | |
| 3224 | |
| 3225 | |
| 3226 | |
| 3227 | |
| 3228 | pub(super) fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> { |
| 3229 | |
| 3230 | |
| 3231 | |
| 3232 | |
| 3233 | |
| 3234 | |
| 3235 | if bw > 64 || bh > 64 || bw * bh < 64 { |
| 3236 | return None; |
| 3237 | } |
| 3238 | Some(((bw * bh) as u32).trailing_zeros() as usize - 6) |
| 3239 | } |
| 3240 | |
| 3241 | |
| 3242 | |
| 3243 | |
| 3244 | pub(super) struct PaletteY { |
| 3245 | pub(super) size: usize, |
| 3246 | pub(super) colors: [u16; 8], |
| 3247 | pub(super) map: Vec<u8>, |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | |
| 3252 | |
| 3253 | |
| 3254 | pub(super) struct PaletteUv { |
| 3255 | pub(super) size: usize, |
| 3256 | pub(super) u_colors: [u16; 8], |
| 3257 | pub(super) v_colors: [u16; 8], |
| 3258 | pub(super) map: Vec<u8>, |
| 3259 | } |
| 3260 | |
| 3261 | |
| 3262 | |
| 3263 | |
| 3264 | |
| 3265 | pub(crate) fn ceil_log2(x: u32) -> u32 { |
| 3266 | if x < 2 { |
| 3267 | 0 |
| 3268 | } else { |
| 3269 | 32 - (x - 1).leading_zeros() |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | |
| 3274 | |
| 3275 | fn read_uniform(dec: &mut SymbolDecoder, n: usize) -> usize { |
| 3276 | let l = ceil_log2(n as u32); |
| 3277 | let m = (1usize << l) - n; |
| 3278 | let v = dec.literal(l - 1) as usize; |
| 3279 | if v < m { |
| 3280 | v |
| 3281 | } else { |
| 3282 | (v << 1) - m + dec.literal(1) as usize |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | |
| 3287 | |
| 3288 | |
| 3289 | |
| 3290 | |
| 3291 | fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> { |
| 3292 | let mut out = Vec::with_capacity(transmitted.len() + cached.len()); |
| 3293 | let (mut ci, mut ti) = (0, 0); |
| 3294 | while ci < cached.len() && ti < transmitted.len() { |
| 3295 | if cached[ci] <= transmitted[ti] { |
| 3296 | out.push(cached[ci]); |
| 3297 | ci += 1; |
| 3298 | } else { |
| 3299 | out.push(transmitted[ti]); |
| 3300 | ti += 1; |
| 3301 | } |
| 3302 | } |
| 3303 | out.extend_from_slice(&cached[ci..]); |
| 3304 | out.extend_from_slice(&transmitted[ti..]); |
| 3305 | out |
| 3306 | } |
| 3307 | |
| 3308 | |
| 3309 | |
| 3310 | |
| 3311 | |
| 3312 | |
| 3313 | |
| 3314 | |
| 3315 | |
| 3316 | |
| 3317 | |
| 3318 | |
| 3319 | |
| 3320 | |
| 3321 | pub(super) fn read_palette_colors_y( |
| 3322 | dec: &mut SymbolDecoder, |
| 3323 | n: usize, |
| 3324 | cache: &[u16], |
| 3325 | fctx: &crate::decode::FrameCtx, |
| 3326 | ) -> [u16; 8] { |
| 3327 | let mut cached = Vec::with_capacity(n); |
| 3328 | for &c in cache { |
| 3329 | if cached.len() >= n { |
| 3330 | break; |
| 3331 | } |
| 3332 | if dec.literal(1) == 1 { |
| 3333 | cached.push(c); |
| 3334 | } |
| 3335 | } |
| 3336 | let bd = u32::from(bit_depth(fctx)); |
| 3337 | let max_val = (1i32 << bd) - 1; |
| 3338 | let mut colors = [0u16; 8]; |
| 3339 | let n_cached = cached.len(); |
| 3340 | if n_cached < n { |
| 3341 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 3342 | let first = dec.literal(bd) as u16; |
| 3343 | transmitted.push(first); |
| 3344 | if n_cached + 1 < n { |
| 3345 | let min_bits = bd - 3; |
| 3346 | let mut bits = min_bits + dec.literal(2); |
| 3347 | let mut range = (1i32 << bd) - i32::from(first) - 1; |
| 3348 | for _ in (n_cached + 1)..n { |
| 3349 | let delta = dec.literal(bits) as i32 + 1; |
| 3350 | let prev = *transmitted.last().unwrap(); |
| 3351 | let val = (i32::from(prev) + delta).clamp(0, max_val) as u16; |
| 3352 | range -= i32::from(val) - i32::from(prev); |
| 3353 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 3354 | transmitted.push(val); |
| 3355 | } |
| 3356 | } |
| 3357 | let merged = merge_colors(&transmitted, &cached); |
| 3358 | colors[..n].copy_from_slice(&merged); |
| 3359 | } else { |
| 3360 | colors[..n].copy_from_slice(&cached[..n]); |
| 3361 | } |
| 3362 | colors |
| 3363 | } |
| 3364 | |
| 3365 | |
| 3366 | |
| 3367 | |
| 3368 | |
| 3369 | |
| 3370 | |
| 3371 | |
| 3372 | |
| 3373 | pub(super) fn read_palette_colors_uv( |
| 3374 | dec: &mut SymbolDecoder, |
| 3375 | n: usize, |
| 3376 | cache: &[u16], |
| 3377 | fctx: &crate::decode::FrameCtx, |
| 3378 | ) -> ([u16; 8], [u16; 8]) { |
| 3379 | let mut cached = Vec::with_capacity(n); |
| 3380 | for &c in cache { |
| 3381 | if cached.len() >= n { |
| 3382 | break; |
| 3383 | } |
| 3384 | if dec.literal(1) == 1 { |
| 3385 | cached.push(c); |
| 3386 | } |
| 3387 | } |
| 3388 | let bd = u32::from(bit_depth(fctx)); |
| 3389 | let max_val = 1i32 << bd; |
| 3390 | let mut u_colors = [0u16; 8]; |
| 3391 | let n_cached = cached.len(); |
| 3392 | if n_cached < n { |
| 3393 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 3394 | let first = dec.literal(bd) as u16; |
| 3395 | transmitted.push(first); |
| 3396 | if n_cached + 1 < n { |
| 3397 | let min_bits = bd - 3; |
| 3398 | let mut bits = min_bits + dec.literal(2); |
| 3399 | let mut range = max_val - i32::from(first); |
| 3400 | for _ in (n_cached + 1)..n { |
| 3401 | let delta = dec.literal(bits) as i32; |
| 3402 | let prev = *transmitted.last().unwrap(); |
| 3403 | let val = (i32::from(prev) + delta).clamp(0, max_val - 1) as u16; |
| 3404 | range -= i32::from(val) - i32::from(prev); |
| 3405 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 3406 | transmitted.push(val); |
| 3407 | } |
| 3408 | } |
| 3409 | let merged = merge_colors(&transmitted, &cached); |
| 3410 | u_colors[..n].copy_from_slice(&merged); |
| 3411 | } else { |
| 3412 | u_colors[..n].copy_from_slice(&cached[..n]); |
| 3413 | } |
| 3414 | let mut v_colors = [0u16; 8]; |
| 3415 | if dec.literal(1) == 1 { |
| 3416 | let min_bits_v = bd - 4; |
| 3417 | let bits = min_bits_v + dec.literal(2); |
| 3418 | v_colors[0] = dec.literal(bd) as u16; |
| 3419 | for i in 1..n { |
| 3420 | let mut delta = dec.literal(bits) as i32; |
| 3421 | if delta != 0 && dec.literal(1) == 1 { |
| 3422 | delta = -delta; |
| 3423 | } |
| 3424 | let mut val = i32::from(v_colors[i - 1]) + delta; |
| 3425 | if val < 0 { |
| 3426 | val += max_val; |
| 3427 | } |
| 3428 | if val >= max_val { |
| 3429 | val -= max_val; |
| 3430 | } |
| 3431 | v_colors[i] = val as u16; |
| 3432 | } |
| 3433 | } else { |
| 3434 | |
| 3435 | #[allow(clippy::needless_range_loop)] |
| 3436 | for i in 0..n { |
| 3437 | v_colors[i] = dec.literal(bd) as u16; |
| 3438 | } |
| 3439 | } |
| 3440 | (u_colors, v_colors) |
| 3441 | } |
| 3442 | |
| 3443 | |
| 3444 | |
| 3445 | |
| 3446 | pub(super) const PALETTE_COLOR_INDEX_CONTEXT_LOOKUP: [i32; 9] = [-1, -1, 0, -1, -1, 4, 3, 2, 1]; |
| 3447 | |
| 3448 | |
| 3449 | |
| 3450 | |
| 3451 | |
| 3452 | |
| 3453 | |
| 3454 | |
| 3455 | pub(crate) fn palette_color_index_context( |
| 3456 | map: &[u8], |
| 3457 | side: usize, |
| 3458 | row: usize, |
| 3459 | col: usize, |
| 3460 | n: usize, |
| 3461 | ) -> (usize, [u8; 8]) { |
| 3462 | let left = if col >= 1 { |
| 3463 | Some(map[row * side + col - 1]) |
| 3464 | } else { |
| 3465 | None |
| 3466 | }; |
| 3467 | let up_left = if col >= 1 && row >= 1 { |
| 3468 | Some(map[(row - 1) * side + col - 1]) |
| 3469 | } else { |
| 3470 | None |
| 3471 | }; |
| 3472 | let up = if row >= 1 { |
| 3473 | Some(map[(row - 1) * side + col]) |
| 3474 | } else { |
| 3475 | None |
| 3476 | }; |
| 3477 | let mut scores = [0i32; 8]; |
| 3478 | for (neighbour, weight) in [(left, 2), (up_left, 1), (up, 2)] { |
| 3479 | if let Some(v) = neighbour { |
| 3480 | scores[v as usize] += weight; |
| 3481 | } |
| 3482 | } |
| 3483 | let mut color_order = [0u8; 8]; |
| 3484 | |
| 3485 | #[allow(clippy::needless_range_loop)] |
| 3486 | for i in 0..8 { |
| 3487 | color_order[i] = i as u8; |
| 3488 | } |
| 3489 | for i in 0..3 { |
| 3490 | let mut max = scores[i]; |
| 3491 | let mut max_idx = i; |
| 3492 | |
| 3493 | #[allow(clippy::needless_range_loop)] |
| 3494 | for j in (i + 1)..n { |
| 3495 | if scores[j] > max { |
| 3496 | max = scores[j]; |
| 3497 | max_idx = j; |
| 3498 | } |
| 3499 | } |
| 3500 | if max_idx != i { |
| 3501 | let max_score = scores[max_idx]; |
| 3502 | let max_color = color_order[max_idx]; |
| 3503 | for k in (i + 1..=max_idx).rev() { |
| 3504 | scores[k] = scores[k - 1]; |
| 3505 | color_order[k] = color_order[k - 1]; |
| 3506 | } |
| 3507 | scores[i] = max_score; |
| 3508 | color_order[i] = max_color; |
| 3509 | } |
| 3510 | } |
| 3511 | let hash = scores[0] + scores[1] * 2 + scores[2] * 2; |
| 3512 | let ctx = PALETTE_COLOR_INDEX_CONTEXT_LOOKUP[hash as usize]; |
| 3513 | (ctx as usize, color_order) |
| 3514 | } |
| 3515 | |
| 3516 | |
| 3517 | |
| 3518 | |
| 3519 | |
| 3520 | |
| 3521 | |
| 3522 | pub(super) fn palette_onscreen( |
| 3523 | mi_r: usize, |
| 3524 | mi_c: usize, |
| 3525 | bw: usize, |
| 3526 | bh: usize, |
| 3527 | fctx: &crate::decode::FrameCtx, |
| 3528 | ) -> (usize, usize) { |
| 3529 | |
| 3530 | |
| 3531 | |
| 3532 | |
| 3533 | |
| 3534 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 3535 | palette_onscreen_dims(mi_r, mi_c, bw, bh, (mi_rows, mi_cols)) |
| 3536 | } |
| 3537 | |
| 3538 | |
| 3539 | |
| 3540 | |
| 3541 | |
| 3542 | pub(crate) fn palette_onscreen_dims( |
| 3543 | mi_r: usize, |
| 3544 | mi_c: usize, |
| 3545 | bw: usize, |
| 3546 | bh: usize, |
| 3547 | (mi_rows, mi_cols): (usize, usize), |
| 3548 | ) -> (usize, usize) { |
| 3549 | ( |
| 3550 | bw.min(mi_cols.saturating_sub(mi_c) * MI), |
| 3551 | bh.min(mi_rows.saturating_sub(mi_r) * MI), |
| 3552 | ) |
| 3553 | } |
| 3554 | |
| 3555 | |
| 3556 | |
| 3557 | |
| 3558 | |
| 3559 | |
| 3560 | |
| 3561 | |
| 3562 | pub(crate) fn palette_onscreen_uv( |
| 3563 | luma: (usize, usize), |
| 3564 | on: (usize, usize), |
| 3565 | chroma: (usize, usize), |
| 3566 | ss: (usize, usize), |
| 3567 | ) -> (usize, usize) { |
| 3568 | ( |
| 3569 | (on.0 >> ss.0) + (chroma.0 - (luma.0 >> ss.0)), |
| 3570 | (on.1 >> ss.1) + (chroma.1 - (luma.1 >> ss.1)), |
| 3571 | ) |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | |
| 3576 | |
| 3577 | pub(super) fn decode_color_index_map( |
| 3578 | dec: &mut SymbolDecoder, |
| 3579 | cdfs: &mut Cdfs, |
| 3580 | n: usize, |
| 3581 | side: usize, |
| 3582 | on: (usize, usize), |
| 3583 | uv: bool, |
| 3584 | ) -> Vec<u8> { |
| 3585 | decode_color_index_map_wh(dec, cdfs, n, side, side, on, uv) |
| 3586 | } |
| 3587 | |
| 3588 | |
| 3589 | |
| 3590 | |
| 3591 | |
| 3592 | |
| 3593 | pub(super) fn decode_color_index_map_wh( |
| 3594 | dec: &mut SymbolDecoder, |
| 3595 | cdfs: &mut Cdfs, |
| 3596 | n: usize, |
| 3597 | bw: usize, |
| 3598 | bh: usize, |
| 3599 | |
| 3600 | on: (usize, usize), |
| 3601 | uv: bool, |
| 3602 | ) -> Vec<u8> { |
| 3603 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 3604 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 3605 | if on_bw < bw || on_bh < bh { |
| 3606 | hit!(PALETTE_ONSCREEN_CUT_HITS); |
| 3607 | } |
| 3608 | let mut map = vec![0u8; bw * bh]; |
| 3609 | if trace { |
| 3610 | let (rng, _) = dec.debug_state(); |
| 3611 | eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); |
| 3612 | } |
| 3613 | map[0] = read_uniform(dec, n) as u8; |
| 3614 | if trace { |
| 3615 | let (rng, _) = dec.debug_state(); |
| 3616 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 3617 | } |
| 3618 | for i in 1..(on_bw + on_bh - 1) { |
| 3619 | let j_hi = i.min(on_bw - 1); |
| 3620 | let j_lo = i.saturating_sub(on_bh - 1); |
| 3621 | for j in (j_lo..=j_hi).rev() { |
| 3622 | let row = i - j; |
| 3623 | let col = j; |
| 3624 | let (ctx, color_order) = palette_color_index_context(&map, bw, row, col, n); |
| 3625 | if trace { |
| 3626 | let (rng, _) = dec.debug_state(); |
| 3627 | let row_cdf = if uv { |
| 3628 | &cdfs.palette_uv_color_index[n - 2][ctx] |
| 3629 | } else { |
| 3630 | &cdfs.palette_y_color_index[n - 2][ctx] |
| 3631 | }; |
| 3632 | eprintln!( |
| 3633 | "EC_PAL row={row} col={col} ctx={ctx} n={n} cdf0={} cnt={} rng={rng}", |
| 3634 | row_cdf[0], row_cdf[n] |
| 3635 | ); |
| 3636 | } |
| 3637 | let symbol = if uv { |
| 3638 | dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n]) |
| 3639 | } else { |
| 3640 | dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]) |
| 3641 | }; |
| 3642 | map[row * bw + col] = color_order[symbol]; |
| 3643 | if trace { |
| 3644 | let (rng, _) = dec.debug_state(); |
| 3645 | eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); |
| 3646 | } |
| 3647 | } |
| 3648 | } |
| 3649 | palette_replicate(&mut map, bw, bh, on); |
| 3650 | map |
| 3651 | } |
| 3652 | |
| 3653 | |
| 3654 | |
| 3655 | |
| 3656 | |
| 3657 | |
| 3658 | |
| 3659 | pub(crate) fn palette_replicate(map: &mut [u8], bw: usize, bh: usize, on: (usize, usize)) { |
| 3660 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 3661 | for row in 0..on_bh { |
| 3662 | let last = map[row * bw + on_bw - 1]; |
| 3663 | map[row * bw + on_bw..row * bw + bw].fill(last); |
| 3664 | } |
| 3665 | for row in on_bh..bh { |
| 3666 | map.copy_within((on_bh - 1) * bw..on_bh * bw, row * bw); |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | |
| 3671 | |
| 3672 | |
| 3673 | |
| 3674 | pub(super) fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 { |
| 3675 | let symbol = dec.symbol(cdf); |
| 3676 | if symbol != ANGLE_DELTA_ZERO { |
| 3677 | hit!(ANGLE_DELTA_HITS); |
| 3678 | } |
| 3679 | symbol as i32 - ANGLE_DELTA_ZERO as i32 |
| 3680 | } |
| 3681 | |
| 3682 | |
| 3683 | |
| 3684 | |
| 3685 | |
| 3686 | |
| 3687 | |
| 3688 | |
| 3689 | |
| 3690 | |
| 3691 | |
| 3692 | |
| 3693 | |
| 3694 | |
| 3695 | |
| 3696 | |
| 3697 | |
| 3698 | |
| 3699 | |
| 3700 | |
| 3701 | |
| 3702 | |
| 3703 | |
| 3704 | #[allow(clippy::too_many_arguments)] |
| 3705 | pub(super) fn apply_deblock( |
| 3706 | y: &mut PlaneBuf<'static>, |
| 3707 | u: &mut PlaneBuf<'static>, |
| 3708 | v: &mut PlaneBuf<'static>, |
| 3709 | lf: &LoopFilterParams, |
| 3710 | n: &Neighbours, |
| 3711 | frame_width: usize, |
| 3712 | frame_height: usize, |
| 3713 | fctx: &crate::decode::FrameCtx, |
| 3714 | ) { |
| 3715 | |
| 3716 | |
| 3717 | if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_DEBLOCK") { |
| 3718 | return; |
| 3719 | } |
| 3720 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 3721 | deblock_plane(y, 0, lf, n, frame_width, frame_height, fctx); |
| 3722 | } |
| 3723 | if lf.level[2] != 0 { |
| 3724 | deblock_plane(u, 1, lf, n, frame_width, frame_height, fctx); |
| 3725 | } |
| 3726 | if lf.level[3] != 0 { |
| 3727 | deblock_plane(v, 2, lf, n, frame_width, frame_height, fctx); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | |
| 3732 | |
| 3733 | |
| 3734 | |
| 3735 | |
| 3736 | |
| 3737 | |
| 3738 | |
| 3739 | |
| 3740 | |
| 3741 | |
| 3742 | |
| 3743 | |
| 3744 | |
| 3745 | |
| 3746 | pub(super) fn plane_to_mi(chroma: bool, coord: usize, ss: usize) -> usize { |
| 3747 | |
| 3748 | if chroma { |
| 3749 | ss | ((coord << ss) >> 2) |
| 3750 | } else { |
| 3751 | coord / 4 |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | |
| 3756 | |
| 3757 | |
| 3758 | |
| 3759 | |
| 3760 | pub(super) fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 3761 | let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 3762 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c) |
| 3763 | .copied() |
| 3764 | .unwrap_or(0) as u32 |
| 3765 | } |
| 3766 | |
| 3767 | |
| 3768 | |
| 3769 | |
| 3770 | pub(super) fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 3771 | let grid = if chroma { |
| 3772 | &n.uv_tx_h_grid |
| 3773 | } else { |
| 3774 | &n.tx_h_grid |
| 3775 | }; |
| 3776 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c) |
| 3777 | .copied() |
| 3778 | .unwrap_or(0) as u32 |
| 3779 | } |
| 3780 | |
| 3781 | |
| 3782 | |
| 3783 | |
| 3784 | |
| 3785 | |
| 3786 | |
| 3787 | |
| 3788 | |
| 3789 | |
| 3790 | |
| 3791 | |
| 3792 | |
| 3793 | |
| 3794 | |
| 3795 | |
| 3796 | |
| 3797 | |
| 3798 | |
| 3799 | |
| 3800 | |
| 3801 | |
| 3802 | |
| 3803 | |
| 3804 | pub(super) fn tx_size_context_txfm_rect( |
| 3805 | n: &Neighbours, |
| 3806 | (mi_r, mi_c): (usize, usize), |
| 3807 | own_w: usize, |
| 3808 | own_h: usize, |
| 3809 | fctx: &crate::decode::FrameCtx, |
| 3810 | ) -> usize { |
| 3811 | hit!(INTRA_IN_INTER_TXCTX_HITS); |
| 3812 | |
| 3813 | |
| 3814 | |
| 3815 | let (own_w, own_h) = (own_w.min(64), own_h.min(64)); |
| 3816 | let has_above = mi_r > n.tile_row0_mi; |
| 3817 | let has_left = mi_c > n.tile_col0_mi; |
| 3818 | let mut above = usize::from(n.above_txfm[mi_c]) >= own_w; |
| 3819 | let mut left = usize::from(n.left_txfm[mi_r]) >= own_h; |
| 3820 | |
| 3821 | |
| 3822 | |
| 3823 | |
| 3824 | |
| 3825 | |
| 3826 | |
| 3827 | |
| 3828 | |
| 3829 | |
| 3830 | |
| 3831 | |
| 3832 | if has_above && n.inter_at(mi_r - 1, mi_c) { |
| 3833 | above = n.blk_side_at(mi_r - 1, mi_c).0 >= own_w; |
| 3834 | } |
| 3835 | if has_left && n.inter_at(mi_r, mi_c - 1) { |
| 3836 | left = n.blk_side_at(mi_r, mi_c - 1).1 >= own_h; |
| 3837 | } |
| 3838 | |
| 3839 | |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | |
| 3844 | |
| 3845 | { |
| 3846 | let band_above = has_above |
| 3847 | && if n.above_inter[mi_c] { |
| 3848 | usize::from(n.above_side_mi[mi_c]) >= own_w |
| 3849 | } else { |
| 3850 | usize::from(n.above_txfm[mi_c]) >= own_w |
| 3851 | }; |
| 3852 | let band_left = has_left |
| 3853 | && if n.left_inter[mi_r] { |
| 3854 | usize::from(n.left_side_mi[mi_r]) >= own_h |
| 3855 | } else { |
| 3856 | usize::from(n.left_txfm[mi_r]) >= own_h |
| 3857 | }; |
| 3858 | if band_above != (has_above && usize::from(above) != 0) |
| 3859 | || band_left != (has_left && usize::from(left) != 0) |
| 3860 | { |
| 3861 | hit!(TXCTX_INTER_CELL_BAND_DIVERGENCE_HITS); |
| 3862 | } |
| 3863 | } |
| 3864 | if crate::envflags::env_flag!("EC_TXCTX") { |
| 3865 | eprintln!( |
| 3866 | "EC_TXCTX mi={mi_r},{mi_c} own={own_w}x{own_h} ha={has_above} hl={has_left} \ |
| 3867 | intra_only={} above_txfm={} left_txfm={} above_inter={} left_inter={} \ |
| 3868 | above_side={} left_side={} above={above} left={left} \ |
| 3869 | | cell_inter ab={} lb={} ab_side={} lb_side={}", |
| 3870 | fctx.intra_only.with(std::cell::Cell::get), |
| 3871 | n.above_txfm[mi_c], |
| 3872 | n.left_txfm[mi_r], |
| 3873 | n.above_inter[mi_c], |
| 3874 | n.left_inter[mi_r], |
| 3875 | n.above_side_mi[mi_c], |
| 3876 | n.left_side_mi[mi_r], |
| 3877 | |
| 3878 | |
| 3879 | |
| 3880 | if has_above { n.inter_at(mi_r - 1, mi_c) } else { false }, |
| 3881 | if has_left { n.inter_at(mi_r, mi_c - 1) } else { false }, |
| 3882 | if has_above { n.blk_side_at(mi_r - 1, mi_c).0 } else { 0 }, |
| 3883 | if has_left { n.blk_side_at(mi_r, mi_c - 1).1 } else { 0 }, |
| 3884 | ); |
| 3885 | } |
| 3886 | let ctx = match (has_above, has_left) { |
| 3887 | (true, true) => usize::from(above) + usize::from(left), |
| 3888 | (true, false) => usize::from(above), |
| 3889 | (false, true) => usize::from(left), |
| 3890 | (false, false) => 0, |
| 3891 | }; |
| 3892 | { |
| 3893 | let stale = usize::from(has_above && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w) |
| 3894 | + usize::from(has_left && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h); |
| 3895 | if ctx != stale { |
| 3896 | hit!(TXCTX_DEBLOCK_DIVERGENCE_HITS); |
| 3897 | } |
| 3898 | } |
| 3899 | ctx |
| 3900 | } |
| 3901 | |
| 3902 | |
| 3903 | |
| 3904 | |
| 3905 | |
| 3906 | |
| 3907 | |
| 3908 | pub(super) fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 3909 | let has_above = mi_r > n.tile_row0_mi; |
| 3910 | let has_left = mi_c > n.tile_col0_mi; |
| 3911 | let mut above = usize::from(n.above_txfm[mi_c]) >= side; |
| 3912 | let mut left = usize::from(n.left_txfm[mi_r]) >= side; |
| 3913 | |
| 3914 | |
| 3915 | |
| 3916 | |
| 3917 | |
| 3918 | |
| 3919 | |
| 3920 | |
| 3921 | |
| 3922 | |
| 3923 | |
| 3924 | |
| 3925 | |
| 3926 | |
| 3927 | |
| 3928 | |
| 3929 | if has_above && n.inter_at(mi_r - 1, mi_c) { |
| 3930 | above = n.blk_side_at(mi_r - 1, mi_c).0 >= side; |
| 3931 | } |
| 3932 | if has_left && n.inter_at(mi_r, mi_c - 1) { |
| 3933 | left = n.blk_side_at(mi_r, mi_c - 1).1 >= side; |
| 3934 | } |
| 3935 | |
| 3936 | { |
| 3937 | let band_above = has_above |
| 3938 | && if n.above_inter[mi_c] { |
| 3939 | usize::from(n.above_side_mi[mi_c]) >= side |
| 3940 | } else { |
| 3941 | usize::from(n.above_txfm[mi_c]) >= side |
| 3942 | }; |
| 3943 | let band_left = has_left |
| 3944 | && if n.left_inter[mi_r] { |
| 3945 | usize::from(n.left_side_mi[mi_r]) >= side |
| 3946 | } else { |
| 3947 | usize::from(n.left_txfm[mi_r]) >= side |
| 3948 | }; |
| 3949 | if band_above != (has_above && usize::from(above) != 0) |
| 3950 | || band_left != (has_left && usize::from(left) != 0) |
| 3951 | { |
| 3952 | hit!(TXCTX_INTER_CELL_BAND_DIVERGENCE_HITS); |
| 3953 | } |
| 3954 | } |
| 3955 | if crate::envflags::env_flag!("EC_TXCTX") { |
| 3956 | eprintln!( |
| 3957 | "EC_TXCTX mi={mi_r},{mi_c} own={side} ha={has_above} hl={has_left} \ |
| 3958 | above_txfm={} left_txfm={} above_inter={} left_inter={} above_side={} left_side={} \ |
| 3959 | above={above} left={left} | cell_inter ab={} lb={} ab_side={} lb_side={}", |
| 3960 | n.above_txfm[mi_c], |
| 3961 | n.left_txfm[mi_r], |
| 3962 | n.above_inter[mi_c], |
| 3963 | n.left_inter[mi_r], |
| 3964 | n.above_side_mi[mi_c], |
| 3965 | n.left_side_mi[mi_r], |
| 3966 | if has_above { n.inter_at(mi_r - 1, mi_c) } else { false }, |
| 3967 | if has_left { n.inter_at(mi_r, mi_c - 1) } else { false }, |
| 3968 | if has_above { n.blk_side_at(mi_r - 1, mi_c).0 } else { 0 }, |
| 3969 | if has_left { n.blk_side_at(mi_r, mi_c - 1).1 } else { 0 }, |
| 3970 | ); |
| 3971 | } |
| 3972 | match (has_above, has_left) { |
| 3973 | (true, true) => usize::from(above) + usize::from(left), |
| 3974 | (true, false) => usize::from(above), |
| 3975 | (false, true) => usize::from(left), |
| 3976 | (false, false) => 0, |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | |
| 3981 | |
| 3982 | |
| 3983 | |
| 3984 | |
| 3985 | pub(super) fn read_tx_size( |
| 3986 | dec: &mut SymbolDecoder, |
| 3987 | cdfs: &mut Cdfs, |
| 3988 | n: &Neighbours, |
| 3989 | at_mi: (usize, usize), |
| 3990 | side: usize, |
| 3991 | fctx: &crate::decode::FrameCtx, |
| 3992 | ) -> usize { |
| 3993 | |
| 3994 | |
| 3995 | |
| 3996 | |
| 3997 | |
| 3998 | |
| 3999 | |
| 4000 | if lossless(fctx) { |
| 4001 | return 4; |
| 4002 | } |
| 4003 | |
| 4004 | |
| 4005 | |
| 4006 | |
| 4007 | |
| 4008 | let max_tx = side.min(64); |
| 4009 | let ctx = tx_size_context_txfm(n, at_mi, max_tx); |
| 4010 | let depth = match max_tx { |
| 4011 | 8 => dec.symbol(&mut cdfs.tx_size_cat0[ctx]), |
| 4012 | 16 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), |
| 4013 | 32 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), |
| 4014 | 64 => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), |
| 4015 | _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64/128"), |
| 4016 | }; |
| 4017 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 4018 | let (rng, _) = dec.debug_state(); |
| 4019 | let (mi_r, mi_c) = at_mi; |
| 4020 | eprintln!( |
| 4021 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={depth} ctx={ctx} rng={rng}" |
| 4022 | ); |
| 4023 | } |
| 4024 | if depth != 0 { |
| 4025 | hit!(TX_DEPTH_HITS); |
| 4026 | } |
| 4027 | max_tx >> depth |
| 4028 | } |
| 4029 | |
| 4030 | |
| 4031 | |
| 4032 | |
| 4033 | |
| 4034 | |
| 4035 | #[allow(dead_code)] |
| 4036 | pub(super) fn txfm_partition_ctx(above_px: u8, left_px: u8, blk_max_px: usize, tx_px: usize) -> usize { |
| 4037 | txfm_partition_ctx_rect(above_px, left_px, blk_max_px, tx_px, tx_px) |
| 4038 | } |
| 4039 | |
| 4040 | |
| 4041 | |
| 4042 | |
| 4043 | |
| 4044 | |
| 4045 | |
| 4046 | pub(crate) fn txfm_partition_ctx_rect( |
| 4047 | above_px: u8, |
| 4048 | left_px: u8, |
| 4049 | blk_max_px: usize, |
| 4050 | tx_w: usize, |
| 4051 | tx_h: usize, |
| 4052 | ) -> usize { |
| 4053 | let above = usize::from(usize::from(above_px) < tx_w); |
| 4054 | let left = usize::from(usize::from(left_px) < tx_h); |
| 4055 | let tx_px = tx_w.max(tx_h); |
| 4056 | |
| 4057 | |
| 4058 | |
| 4059 | let max_tx = blk_max_px.min(64); |
| 4060 | let max_idx = max_tx.trailing_zeros() as usize - 2; |
| 4061 | let category = usize::from(tx_px != max_tx && max_tx > 8) + (4 - max_idx) * 2; |
| 4062 | category * 3 + above + left |
| 4063 | } |
| 4064 | |
| 4065 | |
| 4066 | |
| 4067 | |
| 4068 | #[allow(dead_code)] |
| 4069 | pub(super) fn txfm_partition_update(n: &mut Neighbours, at_mi: (usize, usize), tx_px: usize, txb_px: usize) { |
| 4070 | txfm_partition_update_rect(n, at_mi, (tx_px, tx_px), (txb_px, txb_px)); |
| 4071 | } |
| 4072 | |
| 4073 | |
| 4074 | |
| 4075 | |
| 4076 | |
| 4077 | |
| 4078 | |
| 4079 | |
| 4080 | |
| 4081 | |
| 4082 | |
| 4083 | |
| 4084 | |
| 4085 | |
| 4086 | |
| 4087 | |
| 4088 | |
| 4089 | |
| 4090 | |
| 4091 | |
| 4092 | |
| 4093 | |
| 4094 | |
| 4095 | |
| 4096 | |
| 4097 | |
| 4098 | |
| 4099 | |
| 4100 | |
| 4101 | |
| 4102 | |
| 4103 | |
| 4104 | pub(super) fn publish_txfm_bands_if_in_inter( |
| 4105 | n: &mut Neighbours, |
| 4106 | at_mi: (usize, usize), |
| 4107 | (tx_w, tx_h): (usize, usize), |
| 4108 | (bw, bh): (usize, usize), |
| 4109 | fctx: &crate::decode::FrameCtx, |
| 4110 | ) { |
| 4111 | if fctx |
| 4112 | .intra_in_inter_mode |
| 4113 | .with(std::cell::Cell::get) |
| 4114 | .is_none() |
| 4115 | && !allow_intrabc_frame(fctx) |
| 4116 | { |
| 4117 | return; |
| 4118 | } |
| 4119 | let (mi_r, mi_c) = at_mi; |
| 4120 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 4121 | for i in 0..bw / MI { |
| 4122 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 4123 | *cell = tx_w as u8; |
| 4124 | } |
| 4125 | } |
| 4126 | for i in 0..bh / MI { |
| 4127 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 4128 | *cell = tx_h as u8; |
| 4129 | } |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | pub(super) fn txfm_partition_update_rect( |
| 4134 | n: &mut Neighbours, |
| 4135 | (mi_r, mi_c): (usize, usize), |
| 4136 | (tx_w, tx_h): (usize, usize), |
| 4137 | (txb_w, txb_h): (usize, usize), |
| 4138 | ) { |
| 4139 | if crate::envflags::env_flag!("EC_TXUPD") { |
| 4140 | eprintln!( |
| 4141 | "EC_TXUPD rect mi=({},{}) tx=({tx_w},{tx_h}) txb=({txb_w},{txb_h})", |
| 4142 | mi_r, mi_c |
| 4143 | ); |
| 4144 | } |
| 4145 | for i in 0..txb_h / MI { |
| 4146 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 4147 | *cell = tx_h as u8; |
| 4148 | } |
| 4149 | } |
| 4150 | for i in 0..txb_w / MI { |
| 4151 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 4152 | *cell = tx_w as u8; |
| 4153 | } |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | |
| 4158 | |
| 4159 | |
| 4160 | pub(super) fn set_txfm_ctxs( |
| 4161 | n: &mut Neighbours, |
| 4162 | (mi_r, mi_c): (usize, usize), |
| 4163 | tx_px: usize, |
| 4164 | w_mi: usize, |
| 4165 | h_mi: usize, |
| 4166 | skip_inter: bool, |
| 4167 | ) { |
| 4168 | if crate::envflags::env_flag!("EC_TXUPD") { |
| 4169 | eprintln!( |
| 4170 | "EC_TXUPD ctxs mi=({mi_r},{mi_c}) tx={tx_px} wh=({w_mi},{h_mi}) skip_inter={skip_inter}" |
| 4171 | ); |
| 4172 | } |
| 4173 | let bw = if skip_inter { w_mi * MI } else { tx_px }; |
| 4174 | let bh = if skip_inter { h_mi * MI } else { tx_px }; |
| 4175 | for i in 0..w_mi { |
| 4176 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 4177 | *cell = bw as u8; |
| 4178 | } |
| 4179 | } |
| 4180 | for i in 0..h_mi { |
| 4181 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 4182 | *cell = bh as u8; |
| 4183 | } |
| 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | |
| 4188 | |
| 4189 | |
| 4190 | |
| 4191 | |
| 4192 | |
| 4193 | pub(crate) const TXFM_CTX_INIT: u8 = 64; |
| 4194 | |
| 4195 | |
| 4196 | |
| 4197 | pub(super) const MAX_VARTX_DEPTH: usize = 2; |
| 4198 | |
| 4199 | |
| 4200 | |
| 4201 | |
| 4202 | |
| 4203 | pub(super) fn sub_tx_size_map(tx_w: usize, tx_h: usize) -> (usize, usize) { |
| 4204 | match (tx_w, tx_h) { |
| 4205 | (4, 4) => (4, 4), |
| 4206 | (8, 8) => (4, 4), |
| 4207 | (16, 16) => (8, 8), |
| 4208 | (32, 32) => (16, 16), |
| 4209 | (64, 64) => (32, 32), |
| 4210 | (4, 8) | (8, 4) => (4, 4), |
| 4211 | (8, 16) | (16, 8) => (8, 8), |
| 4212 | (16, 32) | (32, 16) => (16, 16), |
| 4213 | (32, 64) | (64, 32) => (32, 32), |
| 4214 | (4, 16) => (4, 8), |
| 4215 | (16, 4) => (8, 4), |
| 4216 | (8, 32) => (8, 16), |
| 4217 | (32, 8) => (16, 8), |
| 4218 | (16, 64) => (16, 32), |
| 4219 | (64, 16) => (32, 16), |
| 4220 | _ => unreachable!("not a TX_SIZES_ALL shape: {tx_w}x{tx_h}"), |
| 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | #[test] |
| 4225 | fn sub_tx_size_map_matches_libaom() { |
| 4226 | |
| 4227 | const TABLE: [((usize, usize), (usize, usize)); 19] = [ |
| 4228 | ((4, 4), (4, 4)), |
| 4229 | ((8, 8), (4, 4)), |
| 4230 | ((16, 16), (8, 8)), |
| 4231 | ((32, 32), (16, 16)), |
| 4232 | ((64, 64), (32, 32)), |
| 4233 | ((4, 8), (4, 4)), |
| 4234 | ((8, 4), (4, 4)), |
| 4235 | ((8, 16), (8, 8)), |
| 4236 | ((16, 8), (8, 8)), |
| 4237 | ((16, 32), (16, 16)), |
| 4238 | ((32, 16), (16, 16)), |
| 4239 | ((32, 64), (32, 32)), |
| 4240 | ((64, 32), (32, 32)), |
| 4241 | ((4, 16), (4, 8)), |
| 4242 | ((16, 4), (8, 4)), |
| 4243 | ((8, 32), (8, 16)), |
| 4244 | ((32, 8), (16, 8)), |
| 4245 | ((16, 64), (16, 32)), |
| 4246 | ((64, 16), (32, 16)), |
| 4247 | ]; |
| 4248 | for (tx, want) in TABLE { |
| 4249 | assert_eq!(sub_tx_size_map(tx.0, tx.1), want, "sub_tx_size_map({tx:?})"); |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | |
| 4254 | |
| 4255 | |
| 4256 | |
| 4257 | |
| 4258 | |
| 4259 | |
| 4260 | |
| 4261 | |
| 4262 | |
| 4263 | |
| 4264 | |
| 4265 | |
| 4266 | |
| 4267 | |
| 4268 | #[test] |
| 4269 | fn txfm_partition_update_rect_extent_is_the_node_not_the_leaf() { |
| 4270 | |
| 4271 | |
| 4272 | const BSIZES: [(&str, usize, usize); 12] = [ |
| 4273 | ("BLOCK_4X4", 1, 1), |
| 4274 | ("BLOCK_4X8", 1, 2), |
| 4275 | ("BLOCK_8X4", 2, 1), |
| 4276 | ("BLOCK_8X8", 2, 2), |
| 4277 | ("BLOCK_8X16", 2, 4), |
| 4278 | ("BLOCK_16X8", 4, 2), |
| 4279 | ("BLOCK_16X16", 4, 4), |
| 4280 | ("BLOCK_16X32", 4, 8), |
| 4281 | ("BLOCK_32X16", 8, 4), |
| 4282 | ("BLOCK_32X32", 8, 8), |
| 4283 | ("BLOCK_4X16", 1, 4), |
| 4284 | ("BLOCK_16X4", 4, 1), |
| 4285 | ]; |
| 4286 | for (name, mi_w, mi_h) in BSIZES { |
| 4287 | |
| 4288 | |
| 4289 | let (tx_w, tx_h) = (mi_w * MI, mi_h * MI); |
| 4290 | assert_eq!(tx_w / MI, mi_w, "{name}: width identity"); |
| 4291 | assert_eq!(tx_h / MI, mi_h, "{name}: height identity"); |
| 4292 | } |
| 4293 | |
| 4294 | |
| 4295 | |
| 4296 | |
| 4297 | |
| 4298 | let fctx = &crate::decode::FrameCtx::new(); |
| 4299 | let mut n = Neighbours::new(4, 4, 32, 32, fctx); |
| 4300 | for cell in n.above_txfm.iter_mut() { |
| 4301 | *cell = 0; |
| 4302 | } |
| 4303 | for cell in n.left_txfm.iter_mut() { |
| 4304 | *cell = 0; |
| 4305 | } |
| 4306 | txfm_partition_update_rect(&mut n, (4, 4), (4, 4), (8, 16)); |
| 4307 | assert_eq!( |
| 4308 | &n.above_txfm[4..6], |
| 4309 | &[4, 4], |
| 4310 | "the node's OWN width (2 mi) carries the leaf's width" |
| 4311 | ); |
| 4312 | assert_eq!( |
| 4313 | &n.left_txfm[4..8], |
| 4314 | &[4, 4, 4, 4], |
| 4315 | "the node's OWN height (4 mi) carries the leaf's height" |
| 4316 | ); |
| 4317 | assert_eq!(n.above_txfm[6], 0, "and nothing past the node's extent"); |
| 4318 | assert_eq!(n.left_txfm[8], 0, "and nothing past the node's extent"); |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | |
| 4323 | |
| 4324 | |
| 4325 | |
| 4326 | |
| 4327 | |
| 4328 | |
| 4329 | |
| 4330 | |
| 4331 | |
| 4332 | |
| 4333 | |
| 4334 | |
| 4335 | |
| 4336 | |
| 4337 | |
| 4338 | |
| 4339 | |
| 4340 | |
| 4341 | |
| 4342 | #[test] |
| 4343 | fn a_var_tx_tree_never_presents_a_leaf_larger_than_the_unit_it_entered() { |
| 4344 | |
| 4345 | |
| 4346 | |
| 4347 | for refusal in [ |
| 4348 | "an inter var-tx tree with a leaf transform larger than 32x32", |
| 4349 | "an inter var-tx tree with a leaf transform larger than 64x64", |
| 4350 | ] { |
| 4351 | assert!( |
| 4352 | crate::refusal_inventory::pins_refusal(crate::test_source::module_source("decode"), refusal), |
| 4353 | "this gate proves {refusal:?}, but that string is no longer in decode.rs" |
| 4354 | ); |
| 4355 | } |
| 4356 | |
| 4357 | fn reachable(entry: (usize, usize), depth: usize, out: &mut Vec<(usize, usize, usize)>) { |
| 4358 | out.push((depth, entry.0, entry.1)); |
| 4359 | if depth == MAX_VARTX_DEPTH { |
| 4360 | return; |
| 4361 | } |
| 4362 | let sub = sub_tx_size_map(entry.0, entry.1); |
| 4363 | if sub == (4, 4) { |
| 4364 | out.push((depth + 1, 4, 4)); |
| 4365 | return; |
| 4366 | } |
| 4367 | reachable(sub, depth + 1, out); |
| 4368 | } |
| 4369 | |
| 4370 | for entry in [(4, 4), (8, 8), (16, 16), (32, 32), (64, 64)] { |
| 4371 | let mut leaves = Vec::new(); |
| 4372 | reachable(entry, 0, &mut leaves); |
| 4373 | for (depth, w, h) in &leaves { |
| 4374 | assert!( |
| 4375 | w.max(h) <= &entry.0.max(entry.1), |
| 4376 | "a {}x{} unit reaches a {w}x{h} leaf at depth {depth}", |
| 4377 | entry.0, |
| 4378 | entry.1 |
| 4379 | ); |
| 4380 | } |
| 4381 | |
| 4382 | if entry == (64, 64) { |
| 4383 | let above: Vec<&(usize, usize, usize)> = |
| 4384 | leaves.iter().filter(|(_, w, h)| w.max(h) > &32).collect(); |
| 4385 | assert_eq!( |
| 4386 | above, |
| 4387 | [&(0usize, 64usize, 64usize)], |
| 4388 | "64x64 entry, leaves above 32" |
| 4389 | ); |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | |
| 4394 | |
| 4395 | |
| 4396 | for side in [8usize, 16, 32, 64, 128] { |
| 4397 | let max_tx = side.min(64); |
| 4398 | let ceiling = if side > 64 { 64 } else { 32 }; |
| 4399 | let units = (side / max_tx) * (side / max_tx); |
| 4400 | if max_tx <= ceiling { |
| 4401 | continue; |
| 4402 | } |
| 4403 | assert_eq!( |
| 4404 | (side, units), |
| 4405 | (64, 1), |
| 4406 | "a {side}px block enters var-tx at {max_tx} over a ceiling of {ceiling} in \ |
| 4407 | {units} units -- an above-ceiling leaf there would NOT be the `single` case" |
| 4408 | ); |
| 4409 | } |
| 4410 | } |
| 4411 | |
| 4412 | |
| 4413 | |
| 4414 | |
| 4415 | |
| 4416 | |
| 4417 | |
| 4418 | |
| 4419 | |
| 4420 | #[allow(clippy::too_many_arguments)] |
| 4421 | pub(super) fn read_var_tx_size( |
| 4422 | dec: &mut SymbolDecoder, |
| 4423 | cdfs: &mut Cdfs, |
| 4424 | n: &mut Neighbours, |
| 4425 | at_mi: (usize, usize), |
| 4426 | blk_max_px: usize, |
| 4427 | (max_w_mi, max_h_mi): (usize, usize), |
| 4428 | (tx_w, tx_h): (usize, usize), |
| 4429 | depth: usize, |
| 4430 | blk_row: usize, |
| 4431 | blk_col: usize, |
| 4432 | out: &mut Vec<(usize, usize, usize, usize)>, |
| 4433 | ) { |
| 4434 | if blk_row >= max_h_mi || blk_col >= max_w_mi { |
| 4435 | return; |
| 4436 | } |
| 4437 | let unit = (at_mi.0 + blk_row, at_mi.1 + blk_col); |
| 4438 | if depth == MAX_VARTX_DEPTH { |
| 4439 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 4440 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 4441 | return; |
| 4442 | } |
| 4443 | let ctx = txfm_partition_ctx_rect( |
| 4444 | n.above_txfm[unit.1], |
| 4445 | n.left_txfm[unit.0], |
| 4446 | blk_max_px, |
| 4447 | tx_w, |
| 4448 | tx_h, |
| 4449 | ); |
| 4450 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 4451 | hit!(TXFM_SPLIT_READS); |
| 4452 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 4453 | let (rng, _) = dec.debug_state(); |
| 4454 | eprintln!( |
| 4455 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split val={} ctx={ctx} rng={rng}", |
| 4456 | unit.0, |
| 4457 | unit.1, |
| 4458 | u8::from(split) |
| 4459 | ); |
| 4460 | } |
| 4461 | if !split { |
| 4462 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 4463 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 4464 | return; |
| 4465 | } |
| 4466 | hit!(TXFM_SPLIT_HITS); |
| 4467 | let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); |
| 4468 | if (sub_w, sub_h) == (4, 4) { |
| 4469 | |
| 4470 | |
| 4471 | for row in 0..tx_h / MI { |
| 4472 | for col in 0..tx_w / MI { |
| 4473 | if blk_row + row < max_h_mi && blk_col + col < max_w_mi { |
| 4474 | out.push((blk_row + row, blk_col + col, 4, 4)); |
| 4475 | } |
| 4476 | } |
| 4477 | } |
| 4478 | txfm_partition_update_rect(n, unit, (4, 4), (tx_w, tx_h)); |
| 4479 | return; |
| 4480 | } |
| 4481 | for row in (0..tx_h / MI).step_by(sub_h / MI) { |
| 4482 | for col in (0..tx_w / MI).step_by(sub_w / MI) { |
| 4483 | read_var_tx_size( |
| 4484 | dec, |
| 4485 | cdfs, |
| 4486 | n, |
| 4487 | at_mi, |
| 4488 | blk_max_px, |
| 4489 | (max_w_mi, max_h_mi), |
| 4490 | (sub_w, sub_h), |
| 4491 | depth + 1, |
| 4492 | blk_row + row, |
| 4493 | blk_col + col, |
| 4494 | out, |
| 4495 | ); |
| 4496 | } |
| 4497 | } |
| 4498 | } |