| 1 | use super::*; |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | pub struct Coeff { |
| 12 | |
| 13 | pub row: u8, |
| 14 | |
| 15 | pub col: u8, |
| 16 | |
| 17 | |
| 18 | pub level: i32, |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 24 | pub struct BlockCoeffs { |
| 25 | |
| 26 | pub luma: Vec<Coeff>, |
| 27 | |
| 28 | |
| 29 | |
| 30 | pub luma_tx_types: Vec<TxType>, |
| 31 | |
| 32 | pub u: Vec<Coeff>, |
| 33 | |
| 34 | pub v: Vec<Coeff>, |
| 35 | |
| 36 | |
| 37 | |
| 38 | pub mode: u8, |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | pub uv_mode: u8, |
| 45 | |
| 46 | |
| 47 | |
| 48 | pub skip: bool, |
| 49 | |
| 50 | |
| 51 | |
| 52 | pub inter: Option<InterInfo>, |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | pub motion_mode: u8, |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | pub eight: Option<Vec<BlockCoeffs>>, |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | pub palette: Option<PaletteY>, |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | pub palette_uv: Option<PaletteUv>, |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | pub tx_depth: u8, |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | pub cfl_alphas: Option<(i32, i32)>, |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | pub filter_intra: Option<u8>, |
| 98 | |
| 99 | |
| 100 | |
| 101 | pub angle_delta_y: i8, |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | pub dv: Option<(i32, i32)>, |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 122 | pub struct PaletteY { |
| 123 | |
| 124 | pub size: u8, |
| 125 | |
| 126 | pub colors: [u16; 8], |
| 127 | |
| 128 | pub map: Vec<u8>, |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 137 | pub struct PaletteUv { |
| 138 | |
| 139 | pub size: u8, |
| 140 | |
| 141 | |
| 142 | |
| 143 | pub u_colors: [u16; 8], |
| 144 | |
| 145 | |
| 146 | pub v_colors: [u16; 8], |
| 147 | |
| 148 | |
| 149 | pub map: Vec<u8>, |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 156 | pub enum InterMode { |
| 157 | |
| 158 | NearestMv, |
| 159 | |
| 160 | |
| 161 | NearMv, |
| 162 | |
| 163 | |
| 164 | GlobalMv, |
| 165 | |
| 166 | |
| 167 | NewMv, |
| 168 | |
| 169 | |
| 170 | NearestNearestMv, |
| 171 | |
| 172 | |
| 173 | |
| 174 | NearNearMv, |
| 175 | |
| 176 | |
| 177 | NearestNewMv, |
| 178 | |
| 179 | |
| 180 | NewNearestMv, |
| 181 | |
| 182 | |
| 183 | GlobalGlobalMv, |
| 184 | |
| 185 | |
| 186 | NewNewMv, |
| 187 | } |
| 188 | |
| 189 | impl InterMode { |
| 190 | |
| 191 | |
| 192 | |
| 193 | pub(super) fn compound_index(self) -> Option<usize> { |
| 194 | match self { |
| 195 | InterMode::NearestNearestMv => Some(0), |
| 196 | InterMode::NearNearMv => Some(1), |
| 197 | InterMode::NearestNewMv => Some(2), |
| 198 | InterMode::NewNearestMv => Some(3), |
| 199 | InterMode::GlobalGlobalMv => Some(6), |
| 200 | InterMode::NewNewMv => Some(7), |
| 201 | _ => None, |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 211 | pub struct InterInfo { |
| 212 | |
| 213 | pub mode: InterMode, |
| 214 | |
| 215 | pub mv: (i32, i32), |
| 216 | |
| 217 | |
| 218 | pub ref_frame: i8, |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | pub ref_mv_idx: u8, |
| 224 | |
| 225 | |
| 226 | |
| 227 | pub ref1: Option<i8>, |
| 228 | |
| 229 | |
| 230 | pub mv1: (i32, i32), |
| 231 | } |
| 232 | |
| 233 | impl From<Vec<Coeff>> for BlockCoeffs { |
| 234 | |
| 235 | fn from(luma: Vec<Coeff>) -> Self { |
| 236 | Self { |
| 237 | luma, |
| 238 | ..Self::default() |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | pub(super) const TX32: usize = 32; |
| 245 | |
| 246 | pub(super) const BLOCK: usize = 32; |
| 247 | |
| 248 | |
| 249 | pub(super) const TX16: usize = 16; |
| 250 | |
| 251 | |
| 252 | pub(super) const TX8: usize = 8; |
| 253 | |
| 254 | pub(super) const TX4: usize = 4; |
| 255 | |
| 256 | |
| 257 | pub(super) const SB: usize = 64; |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | pub(super) const NO_NEIGHBOUR_SIDE: usize = 128; |
| 266 | |
| 267 | |
| 268 | pub(super) const SUB: usize = 16; |
| 269 | |
| 270 | |
| 271 | |
| 272 | pub(super) const MI: usize = 4; |
| 273 | |
| 274 | |
| 275 | |
| 276 | #[derive(Clone, Copy, Default)] |
| 277 | pub(super) struct Neighbour { |
| 278 | |
| 279 | pub(super) coded: bool, |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | pub(super) level: u8, |
| 285 | |
| 286 | pub(super) dc: Option<bool>, |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | pub(super) fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { |
| 297 | if mi_cols == 0 || mi_rows == 0 { |
| 298 | return Err(Error::unsupported( |
| 299 | "AV1 tile", |
| 300 | "a coefficient key frame needs a nonzero mode-info grid", |
| 301 | )); |
| 302 | } |
| 303 | Ok(()) |
| 304 | } |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | #[allow(clippy::large_enum_variant)] |
| 316 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 317 | pub enum Superblock { |
| 318 | |
| 319 | |
| 320 | |
| 321 | Whole(BlockCoeffs), |
| 322 | |
| 323 | |
| 324 | Split(Vec<Quadrant>), |
| 325 | } |
| 326 | |
| 327 | |
| 328 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 329 | pub enum Quadrant { |
| 330 | |
| 331 | |
| 332 | Whole(BlockCoeffs), |
| 333 | |
| 334 | |
| 335 | Split(Vec<BlockCoeffs>), |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | Whole64(BlockCoeffs), |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | Whole128(BlockCoeffs), |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | Rect128(bool, Vec<BlockCoeffs>), |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | Ab128(u8, Vec<BlockCoeffs>), |
| 377 | |
| 378 | |
| 379 | Covered, |
| 380 | } |
| 381 | |
| 382 | impl Quadrant { |
| 383 | |
| 384 | pub(crate) fn blocks(&self) -> &[BlockCoeffs] { |
| 385 | match self { |
| 386 | Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => { |
| 387 | std::slice::from_ref(block) |
| 388 | } |
| 389 | Quadrant::Split(blocks) | Quadrant::Rect128(_, blocks) | Quadrant::Ab128(_, blocks) => { |
| 390 | blocks.as_slice() |
| 391 | } |
| 392 | Quadrant::Covered => &[], |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | pub(super) struct Neighbours { |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | pub(super) tile_row0_mi: usize, |
| 407 | pub(super) tile_col0_mi: usize, |
| 408 | |
| 409 | pub(super) above: Vec<[Neighbour; 3]>, |
| 410 | |
| 411 | pub(super) left: Vec<[Neighbour; 3]>, |
| 412 | |
| 413 | |
| 414 | pub(super) above_mode: Vec<usize>, |
| 415 | |
| 416 | pub(super) left_mode: Vec<usize>, |
| 417 | |
| 418 | |
| 419 | pub(super) above_side: Vec<usize>, |
| 420 | |
| 421 | pub(super) left_side: Vec<usize>, |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | pub(super) above_side_mi: Vec<usize>, |
| 431 | |
| 432 | pub(super) left_side_mi: Vec<usize>, |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | pub(super) above_skip: Vec<bool>, |
| 439 | |
| 440 | pub(super) left_skip: Vec<bool>, |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | pub(super) above_tx: Vec<u8>, |
| 447 | |
| 448 | |
| 449 | pub(super) left_tx: Vec<u8>, |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | pub(super) above_txfm: Vec<u8>, |
| 456 | |
| 457 | pub(super) left_txfm: Vec<u8>, |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | pub(super) above_palette_size: Vec<u8>, |
| 466 | |
| 467 | pub(super) left_palette_size: Vec<u8>, |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | pub(super) pending_palette: Option<(u8, [u16; 8])>, |
| 475 | |
| 476 | pub(super) above_palette_colors: Vec<[u16; 8]>, |
| 477 | |
| 478 | |
| 479 | |
| 480 | pub(super) above_palette_uv_size: Vec<u8>, |
| 481 | pub(super) left_palette_uv_size: Vec<u8>, |
| 482 | pub(super) above_palette_uv_colors: Vec<[u16; 8]>, |
| 483 | pub(super) left_palette_uv_colors: Vec<[u16; 8]>, |
| 484 | |
| 485 | pub(super) pending_palette_uv: Option<(u8, [u16; 8])>, |
| 486 | |
| 487 | pub(super) left_palette_colors: Vec<[u16; 8]>, |
| 488 | |
| 489 | |
| 490 | pub(super) above_inter: Vec<bool>, |
| 491 | |
| 492 | pub(super) left_inter: Vec<bool>, |
| 493 | |
| 494 | |
| 495 | |
| 496 | pub(super) above_ref: Vec<i8>, |
| 497 | |
| 498 | pub(super) left_ref: Vec<i8>, |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | pub(super) above_ref1: Vec<Option<i8>>, |
| 504 | |
| 505 | pub(super) left_ref1: Vec<Option<i8>>, |
| 506 | |
| 507 | |
| 508 | |
| 509 | pub(super) above_comp_group_idx: Vec<u8>, |
| 510 | |
| 511 | pub(super) left_comp_group_idx: Vec<u8>, |
| 512 | |
| 513 | pub(super) above_compound_idx: Vec<u8>, |
| 514 | |
| 515 | pub(super) left_compound_idx: Vec<u8>, |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | pub(super) mi_cols: usize, |
| 522 | pub(super) mi_rows: usize, |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | #[derive(Clone, Copy, Default)] |
| 530 | pub(super) struct Around { |
| 531 | |
| 532 | pub(super) above_coded: bool, |
| 533 | |
| 534 | pub(super) left_coded: bool, |
| 535 | |
| 536 | |
| 537 | pub(super) dc_vote: i32, |
| 538 | } |
| 539 | |
| 540 | impl Neighbours { |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | pub(super) fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self { |
| 546 | Self { |
| 547 | tile_row0_mi: 0, |
| 548 | tile_col0_mi: 0, |
| 549 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 550 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 551 | above_mode: vec![DC_PRED; cols], |
| 552 | left_mode: vec![DC_PRED; rows], |
| 553 | above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 554 | left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 555 | above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)], |
| 556 | left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)], |
| 557 | above_skip: vec![false; cols * (SUB / MI)], |
| 558 | left_skip: vec![false; rows * (SUB / MI)], |
| 559 | above_inter: vec![false; cols * (SUB / MI)], |
| 560 | left_inter: vec![false; rows * (SUB / MI)], |
| 561 | above_ref: vec![-1; cols * (SUB / MI)], |
| 562 | left_ref: vec![-1; rows * (SUB / MI)], |
| 563 | above_ref1: vec![None; cols * (SUB / MI)], |
| 564 | left_ref1: vec![None; rows * (SUB / MI)], |
| 565 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 566 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 567 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 568 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 569 | above_tx: vec![0; cols * (SUB / MI)], |
| 570 | left_tx: vec![0; rows * (SUB / MI)], |
| 571 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 572 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 573 | above_palette_size: vec![0; cols * (SUB / MI)], |
| 574 | left_palette_size: vec![0; rows * (SUB / MI)], |
| 575 | pending_palette: None, |
| 576 | above_palette_colors: vec![[0; 8]; cols * (SUB / MI)], |
| 577 | above_palette_uv_size: vec![0; cols * (SUB / MI)], |
| 578 | left_palette_uv_size: vec![0; rows * (SUB / MI)], |
| 579 | above_palette_uv_colors: vec![[0; 8]; cols * (SUB / MI)], |
| 580 | left_palette_uv_colors: vec![[0; 8]; rows * (SUB / MI)], |
| 581 | pending_palette_uv: None, |
| 582 | left_palette_colors: vec![[0; 8]; rows * (SUB / MI)], |
| 583 | mi_cols, |
| 584 | mi_rows, |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | pub(super) fn set_tile_origin(&mut self, row0_mi: usize, col0_mi: usize) { |
| 592 | self.tile_row0_mi = row0_mi; |
| 593 | self.tile_col0_mi = col0_mi; |
| 594 | } |
| 595 | |
| 596 | |
| 597 | |
| 598 | pub(super) fn has_above(&self, mi_r: usize) -> bool { |
| 599 | mi_r > self.tile_row0_mi |
| 600 | } |
| 601 | |
| 602 | pub(super) fn has_left(&self, mi_c: usize) -> bool { |
| 603 | mi_c > self.tile_col0_mi |
| 604 | } |
| 605 | |
| 606 | pub(super) fn start_row(&mut self) { |
| 607 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 608 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 609 | self.left_side |
| 610 | .iter_mut() |
| 611 | .for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 612 | self.left_side_mi |
| 613 | .iter_mut() |
| 614 | .for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 615 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 616 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 617 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 618 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 619 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 620 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 621 | self.left_tx.iter_mut().for_each(|t| *t = 0); |
| 622 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 623 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| 624 | self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0); |
| 625 | self.left_palette_uv_colors |
| 626 | .iter_mut() |
| 627 | .for_each(|c| *c = [0u16; 8]); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | pub(super) fn palette_ctx_and_cache(&self, (r, c): (usize, usize)) -> (usize, Vec<u16>) { |
| 638 | |
| 639 | |
| 640 | |
| 641 | let above_ok = r % 16 != 0; |
| 642 | let above_is_palette = r > 0 && self.above_palette_size[c] > 0; |
| 643 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 644 | ( |
| 645 | usize::from(self.above_palette_size[c]), |
| 646 | self.above_palette_colors[c], |
| 647 | ) |
| 648 | } else { |
| 649 | (0, [0u16; 8]) |
| 650 | }; |
| 651 | let (left_n, left_colors) = if c > 0 && self.left_palette_size[r] > 0 { |
| 652 | ( |
| 653 | usize::from(self.left_palette_size[r]), |
| 654 | self.left_palette_colors[r], |
| 655 | ) |
| 656 | } else { |
| 657 | (0, [0u16; 8]) |
| 658 | }; |
| 659 | let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); |
| 660 | let mut cache = Vec::with_capacity(16); |
| 661 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 662 | if cache.last() != Some(&v) { |
| 663 | cache.push(v); |
| 664 | } |
| 665 | }; |
| 666 | let (mut ai, mut li) = (0usize, 0usize); |
| 667 | while ai < above_n && li < left_n { |
| 668 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 669 | if vl < va { |
| 670 | push_dedup(&mut cache, vl); |
| 671 | li += 1; |
| 672 | } else { |
| 673 | push_dedup(&mut cache, va); |
| 674 | ai += 1; |
| 675 | if vl == va { |
| 676 | li += 1; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | while ai < above_n { |
| 681 | push_dedup(&mut cache, above_colors[ai]); |
| 682 | ai += 1; |
| 683 | } |
| 684 | while li < left_n { |
| 685 | push_dedup(&mut cache, left_colors[li]); |
| 686 | li += 1; |
| 687 | } |
| 688 | (ctx, cache) |
| 689 | } |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | pub(super) fn palette_uv_cache(&self, (r, c): (usize, usize)) -> Vec<u16> { |
| 697 | let above_ok = r % 16 != 0; |
| 698 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 699 | ( |
| 700 | usize::from(self.above_palette_uv_size[c]), |
| 701 | self.above_palette_uv_colors[c], |
| 702 | ) |
| 703 | } else { |
| 704 | (0, [0u16; 8]) |
| 705 | }; |
| 706 | let (left_n, left_colors) = if c > 0 && self.left_palette_uv_size[r] > 0 { |
| 707 | ( |
| 708 | usize::from(self.left_palette_uv_size[r]), |
| 709 | self.left_palette_uv_colors[r], |
| 710 | ) |
| 711 | } else { |
| 712 | (0, [0u16; 8]) |
| 713 | }; |
| 714 | let mut cache = Vec::with_capacity(16); |
| 715 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 716 | if cache.last() != Some(&v) { |
| 717 | cache.push(v); |
| 718 | } |
| 719 | }; |
| 720 | let (mut ai, mut li) = (0usize, 0usize); |
| 721 | while ai < above_n && li < left_n { |
| 722 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 723 | if vl < va { |
| 724 | push_dedup(&mut cache, vl); |
| 725 | li += 1; |
| 726 | } else { |
| 727 | push_dedup(&mut cache, va); |
| 728 | ai += 1; |
| 729 | if vl == va { |
| 730 | li += 1; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | while ai < above_n { |
| 735 | push_dedup(&mut cache, above_colors[ai]); |
| 736 | ai += 1; |
| 737 | } |
| 738 | while li < left_n { |
| 739 | push_dedup(&mut cache, left_colors[li]); |
| 740 | li += 1; |
| 741 | } |
| 742 | cache |
| 743 | } |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | pub(super) fn record_palette_uv( |
| 749 | &mut self, |
| 750 | (r, c): (usize, usize), |
| 751 | (w, h): (usize, usize), |
| 752 | size: u8, |
| 753 | colors: [u16; 8], |
| 754 | ) { |
| 755 | |
| 756 | |
| 757 | for cell in 0..(w / MI).max(1) { |
| 758 | if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) { |
| 759 | *e = size; |
| 760 | if size > 0 { |
| 761 | self.above_palette_uv_colors[c + cell] = colors; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | for cell in 0..(h / MI).max(1) { |
| 766 | if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) { |
| 767 | *e = size; |
| 768 | if size > 0 { |
| 769 | self.left_palette_uv_colors[r + cell] = colors; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | pub(super) fn record_palette_y( |
| 779 | &mut self, |
| 780 | (r, c): (usize, usize), |
| 781 | (w, h): (usize, usize), |
| 782 | size: u8, |
| 783 | colors: [u16; 8], |
| 784 | ) { |
| 785 | |
| 786 | |
| 787 | for cell in 0..(w / MI).max(1) { |
| 788 | if let Some(e) = self.above_palette_size.get_mut(c + cell) { |
| 789 | *e = size; |
| 790 | if size > 0 { |
| 791 | self.above_palette_colors[c + cell] = colors; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | for cell in 0..(h / MI).max(1) { |
| 796 | if let Some(e) = self.left_palette_size.get_mut(r + cell) { |
| 797 | *e = size; |
| 798 | if size > 0 { |
| 799 | self.left_palette_colors[r + cell] = colors; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | pub(super) fn record_txfm( |
| 810 | &mut self, |
| 811 | (mi_r, mi_c): (usize, usize), |
| 812 | tx_px: usize, |
| 813 | w_px: usize, |
| 814 | h_px: usize, |
| 815 | ) { |
| 816 | for i in 0..h_px / MI { |
| 817 | if let Some(cell) = self.left_txfm.get_mut(mi_r + i) { |
| 818 | *cell = tx_px as u8; |
| 819 | } |
| 820 | } |
| 821 | for i in 0..w_px / MI { |
| 822 | if let Some(cell) = self.above_txfm.get_mut(mi_c + i) { |
| 823 | *cell = tx_px as u8; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | pub(super) fn record_txfm_rect( |
| 832 | &mut self, |
| 833 | (mi_r, mi_c): (usize, usize), |
| 834 | (tx_w, tx_h): (usize, usize), |
| 835 | (txb_w, txb_h): (usize, usize), |
| 836 | ) { |
| 837 | for i in 0..txb_h / MI { |
| 838 | if let Some(cell) = self.left_txfm.get_mut(mi_r + i) { |
| 839 | *cell = tx_h as u8; |
| 840 | } |
| 841 | } |
| 842 | for i in 0..txb_w / MI { |
| 843 | if let Some(cell) = self.above_txfm.get_mut(mi_c + i) { |
| 844 | *cell = tx_w as u8; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | pub(super) fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { |
| 861 | let (has_above, has_left) = (self.has_above(mi_r), self.has_left(mi_c)); |
| 862 | let mut above = usize::from(self.above_txfm[mi_c]) >= own_side; |
| 863 | let mut left = usize::from(self.left_txfm[mi_r]) >= own_side; |
| 864 | if has_above && self.above_inter[mi_c] { |
| 865 | above = self.above_side_mi[mi_c] >= own_side; |
| 866 | } |
| 867 | if has_left && self.left_inter[mi_r] { |
| 868 | left = self.left_side_mi[mi_r] >= own_side; |
| 869 | } |
| 870 | usize::from(has_above && above) + usize::from(has_left && left) |
| 871 | } |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | pub(super) fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize { |
| 878 | usize::from(self.has_above(mi_r) && usize::from(self.above_tx[mi_c]) >= max_tx) |
| 879 | + usize::from(self.has_left(mi_c) && usize::from(self.left_tx[mi_r]) >= max_tx) |
| 880 | } |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | pub(super) fn record_tx(&mut self, (mi_r, mi_c): (usize, usize), side: usize, tx_px: usize) { |
| 886 | for cell in 0..side / MI { |
| 887 | if mi_r + cell < self.left_tx.len() { |
| 888 | self.left_tx[mi_r + cell] = tx_px as u8; |
| 889 | } |
| 890 | if mi_c + cell < self.above_tx.len() { |
| 891 | self.above_tx[mi_c + cell] = tx_px as u8; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | pub(super) fn luma_skip_ctx(&self, (mi_r, mi_c): (usize, usize), side_mi: usize) -> usize { |
| 900 | let mut top = 0u8; |
| 901 | let mut left = 0u8; |
| 902 | for cell in 0..side_mi { |
| 903 | top |= self.above[mi_c + cell][0].level; |
| 904 | left |= self.left[mi_r + cell][0].level; |
| 905 | } |
| 906 | crate::decode::SKIP_CONTEXTS[usize::from(top).min(4)][usize::from(left).min(4)] |
| 907 | } |
| 908 | |
| 909 | |
| 910 | |
| 911 | pub(super) fn record_mi_luma(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, grid: &[i32]) { |
| 912 | let state = neighbour_state(grid); |
| 913 | for cell in 0..tx_px / MI { |
| 914 | if mi_r + cell < self.mi_rows { |
| 915 | self.left[mi_r + cell][0] = state; |
| 916 | } |
| 917 | if mi_c + cell < self.mi_cols { |
| 918 | self.above[mi_c + cell][0] = state; |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | pub(super) fn record_mi_chroma( |
| 930 | &mut self, |
| 931 | (mi_r, mi_c): (usize, usize), |
| 932 | span_px: usize, |
| 933 | plane: usize, |
| 934 | grid: &[i32], |
| 935 | ) { |
| 936 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 937 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 938 | let state = neighbour_state(grid); |
| 939 | for cell in 0..(span_px / MI).min(bound_h.saturating_sub(mi_r)) { |
| 940 | self.left[mi_r + cell][plane] = state; |
| 941 | } |
| 942 | for cell in 0..(span_px / MI).min(bound_w.saturating_sub(mi_c)) { |
| 943 | self.above[mi_c + cell][plane] = state; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | pub(super) fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) { |
| 955 | self.record_planes(at, side, mode, grids, true); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | pub(super) fn record_planes( |
| 964 | &mut self, |
| 965 | at: (usize, usize), |
| 966 | side: usize, |
| 967 | mode: usize, |
| 968 | grids: &[Vec<i32>; 3], |
| 969 | luma: bool, |
| 970 | ) { |
| 971 | self.record_planes_rect(at, (side, side), mode, grids, luma); |
| 972 | } |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | pub(super) fn record_planes_rect( |
| 978 | &mut self, |
| 979 | at: (usize, usize), |
| 980 | (w, h): (usize, usize), |
| 981 | mode: usize, |
| 982 | grids: &[Vec<i32>; 3], |
| 983 | luma: bool, |
| 984 | ) { |
| 985 | let (r, c) = at; |
| 986 | for cell in 0..w / SUB { |
| 987 | self.above_mode[c + cell] = mode; |
| 988 | self.above_side[c + cell] = w; |
| 989 | } |
| 990 | for cell in 0..h / SUB { |
| 991 | self.left_mode[r + cell] = mode; |
| 992 | self.left_side[r + cell] = h; |
| 993 | } |
| 994 | self.record_mi_planes_rect((r * (SUB / MI), c * (SUB / MI)), (w, h), grids, luma); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | pub(super) fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) { |
| 1005 | self.record_mi_planes(at_mi, side, grids, true); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | pub(super) fn record_mi_planes( |
| 1010 | &mut self, |
| 1011 | at_mi: (usize, usize), |
| 1012 | side: usize, |
| 1013 | grids: &[Vec<i32>; 3], |
| 1014 | luma: bool, |
| 1015 | ) { |
| 1016 | self.record_mi_planes_rect(at_mi, (side, side), grids, luma); |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | pub(super) fn record_mi_planes_rect( |
| 1022 | &mut self, |
| 1023 | at_mi: (usize, usize), |
| 1024 | (w, h): (usize, usize), |
| 1025 | grids: &[Vec<i32>; 3], |
| 1026 | luma: bool, |
| 1027 | ) { |
| 1028 | let (mi_r, mi_c) = at_mi; |
| 1029 | if screen_armed() { |
| 1030 | let (size, colors) = self.pending_palette.take().unwrap_or((0, [0u16; 8])); |
| 1031 | self.record_palette_y(at_mi, (w, h), size, colors); |
| 1032 | let (uv_size, uv_colors) = self.pending_palette_uv.take().unwrap_or((0, [0u16; 8])); |
| 1033 | self.record_palette_uv(at_mi, (w, h), uv_size, uv_colors); |
| 1034 | } |
| 1035 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 1036 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 1037 | for cell in 0..h_mi { |
| 1038 | self.left_side_mi[mi_r + cell] = h; |
| 1039 | } |
| 1040 | for cell in 0..w_mi { |
| 1041 | self.above_side_mi[mi_c + cell] = w; |
| 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 1050 | let bound_h = [ |
| 1051 | self.mi_rows, |
| 1052 | round_up_even(self.mi_rows), |
| 1053 | round_up_even(self.mi_rows), |
| 1054 | ]; |
| 1055 | let bound_w = [ |
| 1056 | self.mi_cols, |
| 1057 | round_up_even(self.mi_cols), |
| 1058 | round_up_even(self.mi_cols), |
| 1059 | ]; |
| 1060 | for cell in 0..h_mi { |
| 1061 | let keep_left = self.left[mi_r + cell][0]; |
| 1062 | self.left[mi_r + cell] = std::array::from_fn(|plane| { |
| 1063 | if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) { |
| 1064 | states[plane] |
| 1065 | } else { |
| 1066 | Default::default() |
| 1067 | } |
| 1068 | }); |
| 1069 | if !luma { |
| 1070 | self.left[mi_r + cell][0] = keep_left; |
| 1071 | } |
| 1072 | } |
| 1073 | for cell in 0..w_mi { |
| 1074 | let keep_above = self.above[mi_c + cell][0]; |
| 1075 | self.above[mi_c + cell] = std::array::from_fn(|plane| { |
| 1076 | if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) { |
| 1077 | states[plane] |
| 1078 | } else { |
| 1079 | Default::default() |
| 1080 | } |
| 1081 | }); |
| 1082 | if !luma { |
| 1083 | self.above[mi_c + cell][0] = keep_above; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | pub(super) fn record_inter( |
| 1092 | &mut self, |
| 1093 | at: (usize, usize), |
| 1094 | side: usize, |
| 1095 | skip: bool, |
| 1096 | is_inter: bool, |
| 1097 | ref_frame: i8, |
| 1098 | ) { |
| 1099 | let (r, c) = at; |
| 1100 | self.record_inter_mi( |
| 1101 | (r * (SUB / MI), c * (SUB / MI)), |
| 1102 | side, |
| 1103 | skip, |
| 1104 | is_inter, |
| 1105 | ref_frame, |
| 1106 | ); |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | pub(super) fn record_inter_rect_mi( |
| 1112 | &mut self, |
| 1113 | (mi_r, mi_c): (usize, usize), |
| 1114 | (w, h): (usize, usize), |
| 1115 | skip: bool, |
| 1116 | is_inter: bool, |
| 1117 | ref_frame: i8, |
| 1118 | ) { |
| 1119 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 1120 | for cell in 0..h / MI { |
| 1121 | self.left_skip[mi_r + cell] = skip; |
| 1122 | self.left_inter[mi_r + cell] = is_inter; |
| 1123 | self.left_ref[mi_r + cell] = ref_frame; |
| 1124 | self.left_ref1[mi_r + cell] = None; |
| 1125 | self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; |
| 1126 | self.left_compound_idx[mi_r + cell] = u8::from(altref); |
| 1127 | } |
| 1128 | for cell in 0..w / MI { |
| 1129 | self.above_skip[mi_c + cell] = skip; |
| 1130 | self.above_inter[mi_c + cell] = is_inter; |
| 1131 | self.above_ref[mi_c + cell] = ref_frame; |
| 1132 | self.above_ref1[mi_c + cell] = None; |
| 1133 | self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; |
| 1134 | self.above_compound_idx[mi_c + cell] = u8::from(altref); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | pub(super) fn record_compound_rect_mi( |
| 1140 | &mut self, |
| 1141 | (mi_r, mi_c): (usize, usize), |
| 1142 | (w, h): (usize, usize), |
| 1143 | ref1: i8, |
| 1144 | comp_group_idx: u8, |
| 1145 | compound_idx: u8, |
| 1146 | ) { |
| 1147 | for cell in 0..h / MI { |
| 1148 | self.left_ref1[mi_r + cell] = Some(ref1); |
| 1149 | self.left_comp_group_idx[mi_r + cell] = comp_group_idx; |
| 1150 | self.left_compound_idx[mi_r + cell] = compound_idx; |
| 1151 | } |
| 1152 | for cell in 0..w / MI { |
| 1153 | self.above_ref1[mi_c + cell] = Some(ref1); |
| 1154 | self.above_comp_group_idx[mi_c + cell] = comp_group_idx; |
| 1155 | self.above_compound_idx[mi_c + cell] = compound_idx; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | pub(super) fn record_compound_mi( |
| 1165 | &mut self, |
| 1166 | (mi_r, mi_c): (usize, usize), |
| 1167 | side: usize, |
| 1168 | ref1: i8, |
| 1169 | comp_group_idx: u8, |
| 1170 | compound_idx: u8, |
| 1171 | ) { |
| 1172 | for cell in 0..side / MI { |
| 1173 | self.above_ref1[mi_c + cell] = Some(ref1); |
| 1174 | self.left_ref1[mi_r + cell] = Some(ref1); |
| 1175 | self.above_comp_group_idx[mi_c + cell] = comp_group_idx; |
| 1176 | self.left_comp_group_idx[mi_r + cell] = comp_group_idx; |
| 1177 | self.above_compound_idx[mi_c + cell] = compound_idx; |
| 1178 | self.left_compound_idx[mi_r + cell] = compound_idx; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | pub(super) fn record_inter_mi( |
| 1185 | &mut self, |
| 1186 | (mi_r, mi_c): (usize, usize), |
| 1187 | side: usize, |
| 1188 | skip: bool, |
| 1189 | is_inter: bool, |
| 1190 | ref_frame: i8, |
| 1191 | ) { |
| 1192 | for cell in 0..side / MI { |
| 1193 | self.above_skip[mi_c + cell] = skip; |
| 1194 | self.left_skip[mi_r + cell] = skip; |
| 1195 | self.above_inter[mi_c + cell] = is_inter; |
| 1196 | self.left_inter[mi_r + cell] = is_inter; |
| 1197 | self.above_ref[mi_c + cell] = ref_frame; |
| 1198 | self.left_ref[mi_r + cell] = ref_frame; |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 1204 | self.above_ref1[mi_c + cell] = None; |
| 1205 | self.left_ref1[mi_r + cell] = None; |
| 1206 | self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; |
| 1207 | self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; |
| 1208 | self.above_compound_idx[mi_c + cell] = u8::from(altref); |
| 1209 | self.left_compound_idx[mi_r + cell] = u8::from(altref); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | pub(super) fn above_nbr(&self, mi_c: usize) -> crate::mvstack::NeighbourRef { |
| 1217 | let is_inter = self.above_inter[mi_c]; |
| 1218 | let ref0 = if is_inter { self.above_ref[mi_c] } else { 0 }; |
| 1219 | let ref1 = self.above_ref1[mi_c]; |
| 1220 | crate::mvstack::NeighbourRef { |
| 1221 | is_inter, |
| 1222 | ref0, |
| 1223 | ref1, |
| 1224 | uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | pub(super) fn left_nbr(&self, mi_r: usize) -> crate::mvstack::NeighbourRef { |
| 1230 | let is_inter = self.left_inter[mi_r]; |
| 1231 | let ref0 = if is_inter { self.left_ref[mi_r] } else { 0 }; |
| 1232 | let ref1 = self.left_ref1[mi_r]; |
| 1233 | crate::mvstack::NeighbourRef { |
| 1234 | is_inter, |
| 1235 | ref0, |
| 1236 | ref1, |
| 1237 | uni: ref1.is_some_and(|r1| is_uni_comp_ref(ref0, r1)), |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | pub(super) fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] { |
| 1246 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | pub(super) fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [Around; 3] { |
| 1252 | let side_mi = side / MI; |
| 1253 | std::array::from_fn(|plane| { |
| 1254 | let mut around = Around::default(); |
| 1255 | for cell in 0..side_mi { |
| 1256 | let (above, left) = ( |
| 1257 | &self.above[mi_c + cell][plane], |
| 1258 | &self.left[mi_r + cell][plane], |
| 1259 | ); |
| 1260 | around.above_coded |= above.coded; |
| 1261 | around.left_coded |= left.coded; |
| 1262 | around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc); |
| 1263 | } |
| 1264 | around |
| 1265 | }) |
| 1266 | } |
| 1267 | |
| 1268 | pub(super) fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | let (r, c) = at; |
| 1276 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | pub(super) fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 1284 | 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side) |
| 1285 | + usize::from(self.above_side_mi[mi_c] * 2 <= side) |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | pub fn sb_coeff_key_frame_tile( |
| 1307 | mi_cols: u32, |
| 1308 | mi_rows: u32, |
| 1309 | base_q_idx: u8, |
| 1310 | superblocks: &[Superblock], |
| 1311 | ) -> Result<Vec<u8>> { |
| 1312 | sb_coeff_key_frame_tile_tx(mi_cols, mi_rows, base_q_idx, superblocks, false) |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | pub fn sb_coeff_key_frame_tile_tx( |
| 1325 | mi_cols: u32, |
| 1326 | mi_rows: u32, |
| 1327 | base_q_idx: u8, |
| 1328 | superblocks: &[Superblock], |
| 1329 | tx_select: bool, |
| 1330 | ) -> Result<Vec<u8>> { |
| 1331 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 1332 | sb_coeff_key_frame_tile_cdfs( |
| 1333 | mi_cols, |
| 1334 | mi_rows, |
| 1335 | base_q_idx, |
| 1336 | superblocks, |
| 1337 | tx_select, |
| 1338 | &mut cdfs, |
| 1339 | TileRect::whole(mi_cols, mi_rows), |
| 1340 | ) |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | pub(crate) fn sb_coeff_key_frame_tile_cdfs( |
| 1352 | mi_cols: u32, |
| 1353 | mi_rows: u32, |
| 1354 | _base_q_idx: u8, |
| 1355 | superblocks: &[Superblock], |
| 1356 | tx_select: bool, |
| 1357 | cdfs: &mut Cdfs, |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | tile: TileRect, |
| 1363 | ) -> Result<Vec<u8>> { |
| 1364 | |
| 1365 | let _cdef_idx = CdefIdxGuard; |
| 1366 | check_blocks(mi_cols, mi_rows)?; |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | if intrabc_armed() { |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | let grid = crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 1377 | INTRABC_GRID.with(|g| { |
| 1378 | *g.borrow_mut() = Some((grid, mi_cols as usize, mi_rows as usize)); |
| 1379 | }); |
| 1380 | } |
| 1381 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 1382 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 1383 | if superblocks.len() != (sb_cols * sb_rows) as usize { |
| 1384 | return Err(Error::unsupported( |
| 1385 | "AV1 tile", |
| 1386 | "a coefficient key frame needs one entry per superblock", |
| 1387 | )); |
| 1388 | } |
| 1389 | let mut coded: Vec<&BlockCoeffs> = Vec::new(); |
| 1390 | for superblock in superblocks { |
| 1391 | match superblock { |
| 1392 | Superblock::Whole(block) => coded.push(block), |
| 1393 | Superblock::Split(quadrants) => { |
| 1394 | for block in quadrants.iter().flat_map(Quadrant::blocks) { |
| 1395 | match &block.eight { |
| 1396 | Some(leaves) => coded.extend(leaves.iter()), |
| 1397 | None => coded.push(block), |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | if let Some(bad) = coded |
| 1404 | .into_iter() |
| 1405 | .find(|b| usize::from(b.mode) >= INTRA_MODES) |
| 1406 | { |
| 1407 | return Err(Error::unsupported( |
| 1408 | "AV1 tile", |
| 1409 | format!( |
| 1410 | "intra mode {} is not one of the thirteen a key frame codes", |
| 1411 | bad.mode |
| 1412 | ), |
| 1413 | )); |
| 1414 | } |
| 1415 | |
| 1416 | let sub_planes = [TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8]; |
| 1417 | let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; |
| 1418 | let whole_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; |
| 1419 | let scans = [ |
| 1420 | default_scan(TX32), |
| 1421 | default_scan(TX16), |
| 1422 | default_scan(TX8), |
| 1423 | default_scan(TX4), |
| 1424 | ]; |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | let mut neighbours = Neighbours::new( |
| 1429 | cols as usize * 2, |
| 1430 | rows as usize * 2, |
| 1431 | mi_cols as usize, |
| 1432 | mi_rows as usize, |
| 1433 | ); |
| 1434 | neighbours.set_tile_origin(tile.mi_row0 as usize, tile.mi_col0 as usize); |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | let mut enc = SymbolEncoder::new(); |
| 1440 | let sb128 = sb128_armed(); |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | for (sb_r, sb_c, row_start, sb_start) in sb_write_order( |
| 1446 | tile.sb_row0, |
| 1447 | tile.sb_row1.min(sb_rows), |
| 1448 | tile.sb_col0, |
| 1449 | tile.sb_col1.min(sb_cols), |
| 1450 | sb128, |
| 1451 | ) { |
| 1452 | if row_start { |
| 1453 | neighbours.start_row(); |
| 1454 | } |
| 1455 | if sb128 && sb_start { |
| 1456 | write_sb128_root( |
| 1457 | &mut enc, |
| 1458 | cdfs, |
| 1459 | &neighbours, |
| 1460 | sb_r, |
| 1461 | sb_c, |
| 1462 | mi_cols, |
| 1463 | mi_rows, |
| 1464 | PARTITION_SPLIT, |
| 1465 | ); |
| 1466 | } |
| 1467 | { |
| 1468 | if !sb128 { |
| 1469 | write_lr(&mut enc, cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W, SB_MI_W); |
| 1470 | } |
| 1471 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 1472 | let ctx = neighbours.partition_ctx(at, SB); |
| 1473 | ec_rng_trace(|| { |
| 1474 | format!( |
| 1475 | "EC_PART mi_row={} mi_col={} bsize=12 ctx={} tell={}", |
| 1476 | at.0 * 4, |
| 1477 | at.1 * 4, |
| 1478 | ctx, |
| 1479 | enc.tell() |
| 1480 | ) |
| 1481 | }); |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | let (has_cols, has_rows) = ( |
| 1487 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 1488 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 1489 | ); |
| 1490 | |
| 1491 | |
| 1492 | let quadrant_positions: Vec<(usize, usize)> = (0..4) |
| 1493 | .map(|q| (sb_r * 2 + q / 2, sb_c * 2 + q % 2)) |
| 1494 | .filter(|&(r, c)| r < rows && c < cols) |
| 1495 | .map(|(r, c)| (r as usize, c as usize)) |
| 1496 | .collect(); |
| 1497 | |
| 1498 | match &superblocks[(sb_r * sb_cols + sb_c) as usize] { |
| 1499 | Superblock::Whole(block) => { |
| 1500 | if !has_cols || !has_rows { |
| 1501 | return Err(Error::unsupported( |
| 1502 | "AV1 tile", |
| 1503 | "a superblock that is half outside the frame cannot be left whole", |
| 1504 | )); |
| 1505 | } |
| 1506 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[ctx]); |
| 1507 | let grids = [ |
| 1508 | level_grid(&block.luma, TX32)?, |
| 1509 | level_grid(&block.u, TX32)?, |
| 1510 | level_grid(&block.v, TX32)?, |
| 1511 | ]; |
| 1512 | write_block( |
| 1513 | &mut enc, |
| 1514 | cdfs, |
| 1515 | &mut neighbours, |
| 1516 | block, |
| 1517 | at, |
| 1518 | SB, |
| 1519 | &whole_planes, |
| 1520 | &grids, |
| 1521 | [&scans[0], &scans[0], &scans[0]], |
| 1522 | |
| 1523 | |
| 1524 | false, |
| 1525 | tx_select, |
| 1526 | &scans, |
| 1527 | )?; |
| 1528 | ec_rng_trace(|| { |
| 1529 | format!( |
| 1530 | "EC_TOK mi_row={} mi_col={} tell={}", |
| 1531 | at.0 * 4, |
| 1532 | at.1 * 4, |
| 1533 | enc.tell() |
| 1534 | ) |
| 1535 | }); |
| 1536 | } |
| 1537 | Superblock::Split(quadrants) => { |
| 1538 | match (has_cols, has_rows) { |
| 1539 | (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[ctx]), |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | (true, false) => { |
| 1544 | enc.symbol_fixed(1, &gather(&cdfs.partition_w64[ctx], VERT_ALIKE)); |
| 1545 | } |
| 1546 | (false, true) => { |
| 1547 | enc.symbol_fixed(1, &gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)); |
| 1548 | } |
| 1549 | (false, false) => {} |
| 1550 | } |
| 1551 | if quadrants.len() != quadrant_positions.len() { |
| 1552 | return Err(Error::unsupported( |
| 1553 | "AV1 tile", |
| 1554 | "a split superblock needs one block per quadrant inside the frame", |
| 1555 | )); |
| 1556 | } |
| 1557 | for (quadrant, (r, c)) in quadrants.iter().zip(quadrant_positions) { |
| 1558 | let at = (r * 2, c * 2); |
| 1559 | let ctx = neighbours.partition_ctx(at, BLOCK); |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | let (has_cols32, has_rows32) = ( |
| 1567 | has_half(c as u32 * BLOCK_MI, BLOCK_MI, mi_cols), |
| 1568 | has_half(r as u32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 1569 | ); |
| 1570 | ec_rng_trace(|| { |
| 1571 | format!( |
| 1572 | "EC_PART mi_row={} mi_col={} bsize=9 ctx={} tell={}", |
| 1573 | at.0 * 4, |
| 1574 | at.1 * 4, |
| 1575 | ctx, |
| 1576 | enc.tell() |
| 1577 | ) |
| 1578 | }); |
| 1579 | match quadrant { |
| 1580 | |
| 1581 | |
| 1582 | Quadrant::Whole64(_) |
| 1583 | | Quadrant::Whole128(_) |
| 1584 | | Quadrant::Rect128(..) |
| 1585 | | Quadrant::Ab128(..) |
| 1586 | | Quadrant::Covered => { |
| 1587 | return Err(Error::unsupported( |
| 1588 | "AV1 tile", |
| 1589 | "a key frame codes a 64x64 block as `Superblock::Whole`", |
| 1590 | )); |
| 1591 | } |
| 1592 | Quadrant::Whole(block) => { |
| 1593 | if !has_cols32 || !has_rows32 { |
| 1594 | return Err(Error::unsupported( |
| 1595 | "AV1 tile", |
| 1596 | "a 32x32 block that is half outside the true frame \ |
| 1597 | cannot be left whole", |
| 1598 | )); |
| 1599 | } |
| 1600 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx]); |
| 1601 | let grids = [ |
| 1602 | level_grid(&block.luma, TX32)?, |
| 1603 | level_grid(&block.u, TX16)?, |
| 1604 | level_grid(&block.v, TX16)?, |
| 1605 | ]; |
| 1606 | write_block( |
| 1607 | &mut enc, |
| 1608 | cdfs, |
| 1609 | &mut neighbours, |
| 1610 | block, |
| 1611 | at, |
| 1612 | BLOCK, |
| 1613 | &split_planes, |
| 1614 | &grids, |
| 1615 | [&scans[0], &scans[1], &scans[1]], |
| 1616 | true, |
| 1617 | tx_select, |
| 1618 | &scans, |
| 1619 | )?; |
| 1620 | ec_rng_trace(|| { |
| 1621 | format!( |
| 1622 | "EC_TOK mi_row={} mi_col={} tell={}", |
| 1623 | at.0 * 4, |
| 1624 | at.1 * 4, |
| 1625 | enc.tell() |
| 1626 | ) |
| 1627 | }); |
| 1628 | } |
| 1629 | Quadrant::Split(blocks) => { |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | let sub_positions: Vec<(usize, usize)> = (0..4) |
| 1637 | .map(|i| (r * 2 + i / 2, c * 2 + i % 2)) |
| 1638 | .filter(|&(sr, sc)| { |
| 1639 | (sr as u32) * SUB_MI < mi_rows |
| 1640 | && (sc as u32) * SUB_MI < mi_cols |
| 1641 | }) |
| 1642 | .collect(); |
| 1643 | if blocks.len() != sub_positions.len() { |
| 1644 | return Err(Error::unsupported( |
| 1645 | "AV1 tile", |
| 1646 | "a split 32x32 block needs one 16x16 entry per \ |
| 1647 | sub-block inside the true frame", |
| 1648 | )); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | match (has_cols32, has_rows32) { |
| 1657 | (true, true) => { |
| 1658 | enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w32[ctx]); |
| 1659 | } |
| 1660 | (true, false) => { |
| 1661 | enc.symbol_fixed( |
| 1662 | 1, |
| 1663 | &gather(&cdfs.partition_w32[ctx], VERT_ALIKE), |
| 1664 | ); |
| 1665 | } |
| 1666 | (false, true) => { |
| 1667 | enc.symbol_fixed( |
| 1668 | 1, |
| 1669 | &gather(&cdfs.partition_w32[ctx], HORZ_ALIKE), |
| 1670 | ); |
| 1671 | } |
| 1672 | (false, false) => {} |
| 1673 | } |
| 1674 | for (block, (sr, sc)) in blocks.iter().zip(sub_positions) { |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | let (has_cols16, has_rows16) = ( |
| 1680 | has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), |
| 1681 | has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), |
| 1682 | ); |
| 1683 | let at = (sr, sc); |
| 1684 | let ctx = neighbours.partition_ctx(at, SUB); |
| 1685 | ec_rng_trace(|| { |
| 1686 | format!( |
| 1687 | "EC_PART mi_row={} mi_col={} bsize=6 ctx={} tell={}", |
| 1688 | at.0 * 4, |
| 1689 | at.1 * 4, |
| 1690 | ctx, |
| 1691 | enc.tell() |
| 1692 | ) |
| 1693 | }); |
| 1694 | if has_cols16 && has_rows16 { |
| 1695 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); |
| 1696 | let grids = [ |
| 1697 | level_grid(&block.luma, TX16)?, |
| 1698 | level_grid(&block.u, TX8)?, |
| 1699 | level_grid(&block.v, TX8)?, |
| 1700 | ]; |
| 1701 | write_block( |
| 1702 | &mut enc, |
| 1703 | cdfs, |
| 1704 | &mut neighbours, |
| 1705 | block, |
| 1706 | at, |
| 1707 | SUB, |
| 1708 | &sub_planes, |
| 1709 | &grids, |
| 1710 | [&scans[1], &scans[2], &scans[2]], |
| 1711 | true, |
| 1712 | tx_select, |
| 1713 | &scans, |
| 1714 | )?; |
| 1715 | ec_rng_trace(|| { |
| 1716 | format!( |
| 1717 | "EC_TOK mi_row={} mi_col={} tell={}", |
| 1718 | at.0 * 4, |
| 1719 | at.1 * 4, |
| 1720 | enc.tell() |
| 1721 | ) |
| 1722 | }); |
| 1723 | continue; |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | match (has_cols16, has_rows16) { |
| 1737 | (true, false) => enc.symbol_fixed( |
| 1738 | 1, |
| 1739 | &gather(&cdfs.partition_w16[ctx], VERT_ALIKE), |
| 1740 | ), |
| 1741 | (false, true) => enc.symbol_fixed( |
| 1742 | 1, |
| 1743 | &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE), |
| 1744 | ), |
| 1745 | _ => {} |
| 1746 | } |
| 1747 | let leaves = block.eight.as_ref().ok_or_else(|| { |
| 1748 | Error::unsupported( |
| 1749 | "AV1 tile", |
| 1750 | "a 16x16 block the true frame edge cuts through \ |
| 1751 | needs its `eight` leaves populated", |
| 1752 | ) |
| 1753 | })?; |
| 1754 | let (mi_row0, mi_col0) = |
| 1755 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 1756 | let leaf_positions: Vec<(u32, u32)> = (0..4) |
| 1757 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 1758 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 1759 | .collect(); |
| 1760 | if leaves.len() != leaf_positions.len() { |
| 1761 | return Err(Error::unsupported( |
| 1762 | "AV1 tile", |
| 1763 | "a straddling 16x16 block needs one `eight` entry \ |
| 1764 | per 8x8 leaf inside the true frame", |
| 1765 | )); |
| 1766 | } |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | |
| 1772 | |
| 1773 | let mut prev_leaf: Option<((usize, usize), usize)> = None; |
| 1774 | for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) { |
| 1775 | let leaf_mi = (mr as usize, mc as usize); |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 1784 | ec_rng_trace(|| { |
| 1785 | format!( |
| 1786 | "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={}", |
| 1787 | leaf_mi.0, |
| 1788 | leaf_mi.1, |
| 1789 | leaf_ctx, |
| 1790 | enc.tell() |
| 1791 | ) |
| 1792 | }); |
| 1793 | enc.symbol( |
| 1794 | PARTITION_NONE, |
| 1795 | &mut cdfs.partition_w8[leaf_ctx], |
| 1796 | ); |
| 1797 | let grids = [ |
| 1798 | level_grid(&leaf.luma, TX8)?, |
| 1799 | level_grid(&leaf.u, TX4)?, |
| 1800 | level_grid(&leaf.v, TX4)?, |
| 1801 | ]; |
| 1802 | let leaf_mode = write_leaf8( |
| 1803 | &mut enc, |
| 1804 | cdfs, |
| 1805 | &mut neighbours, |
| 1806 | leaf, |
| 1807 | at, |
| 1808 | leaf_mi, |
| 1809 | &grids, |
| 1810 | [&scans[2], &scans[3], &scans[3]], |
| 1811 | prev_leaf, |
| 1812 | tx_select, |
| 1813 | &scans, |
| 1814 | )?; |
| 1815 | prev_leaf = Some((leaf_mi, leaf_mode)); |
| 1816 | ec_rng_trace(|| { |
| 1817 | format!( |
| 1818 | "EC_TOK mi_row={} mi_col={} tell={}", |
| 1819 | leaf_mi.0, |
| 1820 | leaf_mi.1, |
| 1821 | enc.tell() |
| 1822 | ) |
| 1823 | }); |
| 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | if let Some((_, mode)) = prev_leaf { |
| 1841 | neighbours.above_mode[at.1] = mode; |
| 1842 | neighbours.left_mode[at.0] = mode; |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | } |
| 1852 | Ok(enc.finish()) |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | |
| 1858 | pub(super) fn write_uniform(enc: &mut SymbolEncoder, value: usize, n: usize) { |
| 1859 | let l = crate::decode::ceil_log2(n as u32); |
| 1860 | let m = (1usize << l) - n; |
| 1861 | if value < m { |
| 1862 | enc.literal(value as u32, l - 1); |
| 1863 | } else { |
| 1864 | enc.literal(((value + m) >> 1) as u32, l - 1); |
| 1865 | enc.literal(((value + m) & 1) as u32, 1); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | |
| 1874 | |
| 1875 | |
| 1876 | pub(super) fn write_palette_colors_y(enc: &mut SymbolEncoder, colors: &[u16], cache: &[u16]) { |
| 1877 | let n = colors.len(); |
| 1878 | let mut cached: Vec<u16> = Vec::with_capacity(n); |
| 1879 | for &c in cache { |
| 1880 | if cached.len() >= n { |
| 1881 | break; |
| 1882 | } |
| 1883 | let take = colors.contains(&c); |
| 1884 | enc.literal(u32::from(take), 1); |
| 1885 | if take { |
| 1886 | cached.push(c); |
| 1887 | } |
| 1888 | } |
| 1889 | let transmitted: Vec<u16> = colors |
| 1890 | .iter() |
| 1891 | .copied() |
| 1892 | .filter(|c| !cached.contains(c)) |
| 1893 | .collect(); |
| 1894 | if transmitted.is_empty() { |
| 1895 | return; |
| 1896 | } |
| 1897 | enc.literal(u32::from(transmitted[0]), 8); |
| 1898 | if transmitted.len() == 1 { |
| 1899 | return; |
| 1900 | } |
| 1901 | |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | let fits = |extra: u32| -> bool { |
| 1906 | let mut bits = 5 + extra; |
| 1907 | let mut range = 255i32 - i32::from(transmitted[0]); |
| 1908 | for w in transmitted.windows(2) { |
| 1909 | let delta = i32::from(w[1]) - i32::from(w[0]); |
| 1910 | if delta > (1i32 << bits) { |
| 1911 | return false; |
| 1912 | } |
| 1913 | range -= delta; |
| 1914 | bits = bits.min(crate::decode::ceil_log2(range.max(0) as u32)); |
| 1915 | } |
| 1916 | true |
| 1917 | }; |
| 1918 | let extra = (0..=3).find(|&e| fits(e)).unwrap_or(3); |
| 1919 | enc.literal(extra, 2); |
| 1920 | let mut bits = 5 + extra; |
| 1921 | let mut range = 255i32 - i32::from(transmitted[0]); |
| 1922 | for w in transmitted.windows(2) { |
| 1923 | let delta = i32::from(w[1]) - i32::from(w[0]); |
| 1924 | enc.literal((delta - 1) as u32, bits); |
| 1925 | range -= delta; |
| 1926 | bits = bits.min(crate::decode::ceil_log2(range.max(0) as u32)); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | |
| 1937 | |
| 1938 | |
| 1939 | |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | pub(super) fn write_palette_colors_uv( |
| 1944 | enc: &mut SymbolEncoder, |
| 1945 | u_colors: &[u16], |
| 1946 | v_colors: &[u16], |
| 1947 | cache: &[u16], |
| 1948 | ) { |
| 1949 | let n = u_colors.len(); |
| 1950 | let mut cached: Vec<u16> = Vec::with_capacity(n); |
| 1951 | for &c in cache { |
| 1952 | if cached.len() >= n { |
| 1953 | break; |
| 1954 | } |
| 1955 | let take = u_colors.contains(&c); |
| 1956 | enc.literal(u32::from(take), 1); |
| 1957 | if take { |
| 1958 | PALETTE_UV_CACHE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 1959 | cached.push(c); |
| 1960 | } |
| 1961 | } |
| 1962 | let transmitted: Vec<u16> = u_colors |
| 1963 | .iter() |
| 1964 | .copied() |
| 1965 | .filter(|c| !cached.contains(c)) |
| 1966 | .collect(); |
| 1967 | if !transmitted.is_empty() { |
| 1968 | enc.literal(u32::from(transmitted[0]), 8); |
| 1969 | if transmitted.len() > 1 { |
| 1970 | let fits = |extra: u32| -> bool { |
| 1971 | let mut bits = 5 + extra; |
| 1972 | let mut range = 256i32 - i32::from(transmitted[0]); |
| 1973 | for w in transmitted.windows(2) { |
| 1974 | let delta = i32::from(w[1]) - i32::from(w[0]); |
| 1975 | if delta >= (1i32 << bits) { |
| 1976 | return false; |
| 1977 | } |
| 1978 | range -= delta; |
| 1979 | bits = bits.min(crate::decode::ceil_log2(range.max(0) as u32)); |
| 1980 | } |
| 1981 | true |
| 1982 | }; |
| 1983 | let extra = (0..=3).find(|&e| fits(e)).unwrap_or(3); |
| 1984 | enc.literal(extra, 2); |
| 1985 | let mut bits = 5 + extra; |
| 1986 | let mut range = 256i32 - i32::from(transmitted[0]); |
| 1987 | for w in transmitted.windows(2) { |
| 1988 | let delta = i32::from(w[1]) - i32::from(w[0]); |
| 1989 | enc.literal(delta as u32, bits); |
| 1990 | range -= delta; |
| 1991 | bits = bits.min(crate::decode::ceil_log2(range.max(0) as u32)); |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | enc.literal(0, 1); |
| 1996 | for &v in v_colors { |
| 1997 | enc.literal(u32::from(v), 8); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | |
| 2004 | |
| 2005 | pub(super) fn write_color_index_map( |
| 2006 | enc: &mut SymbolEncoder, |
| 2007 | idx_cdfs: &mut [[u16; 9]; 5], |
| 2008 | map: &[u8], |
| 2009 | side: usize, |
| 2010 | n: usize, |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | on: (usize, usize), |
| 2018 | ) { |
| 2019 | let (on_w, on_h) = (on.0.clamp(1, side), on.1.clamp(1, side)); |
| 2020 | write_uniform(enc, usize::from(map[0]), n); |
| 2021 | for i in 1..(on_w + on_h - 1) { |
| 2022 | for j in (i.saturating_sub(on_h - 1)..=i.min(on_w - 1)).rev() { |
| 2023 | let (row, col) = (i - j, j); |
| 2024 | let (ctx, color_order) = |
| 2025 | crate::decode::palette_color_index_context(map, side, row, col, n); |
| 2026 | let want = map[row * side + col]; |
| 2027 | let symbol = color_order[..n] |
| 2028 | .iter() |
| 2029 | .position(|&c| c == want) |
| 2030 | .expect("every colour index is in the block\'s own palette"); |
| 2031 | enc.symbol(symbol, &mut idx_cdfs[ctx][..=n]); |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | pub(crate) fn palette_bits(pal: &PaletteY, side: usize, on: (usize, usize)) -> f64 { |
| 2043 | let Some(bsize_ctx) = crate::decode::palette_bsize_ctx(side) else { |
| 2044 | return f64::INFINITY; |
| 2045 | }; |
| 2046 | let n = usize::from(pal.size); |
| 2047 | let mut enc = SymbolEncoder::pricer(); |
| 2048 | let mut mode_cdf = cdf::PALETTE_Y_MODE[bsize_ctx][0]; |
| 2049 | enc.symbol(1, &mut mode_cdf); |
| 2050 | let mut size_cdf = cdf::PALETTE_Y_SIZE[bsize_ctx]; |
| 2051 | enc.symbol(n - 2, &mut size_cdf); |
| 2052 | write_palette_colors_y(&mut enc, &pal.colors[..n], &[]); |
| 2053 | let mut idx_cdfs = cdf::PALETTE_Y_COLOR_INDEX[n - 2]; |
| 2054 | write_color_index_map(&mut enc, &mut idx_cdfs, &pal.map, side, n, on); |
| 2055 | enc.bits() |
| 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | pub(crate) fn palette_uv_bits(pal: &PaletteUv, side: usize, on: (usize, usize)) -> f64 { |
| 2062 | let Some(bsize_ctx) = crate::decode::palette_bsize_ctx(side) else { |
| 2063 | return f64::INFINITY; |
| 2064 | }; |
| 2065 | let n = usize::from(pal.size); |
| 2066 | let mut enc = SymbolEncoder::pricer(); |
| 2067 | let mut mode_cdf = cdf::PALETTE_UV_MODE[0]; |
| 2068 | enc.symbol(1, &mut mode_cdf); |
| 2069 | let mut size_cdf = cdf::PALETTE_UV_SIZE[bsize_ctx]; |
| 2070 | enc.symbol(n - 2, &mut size_cdf); |
| 2071 | write_palette_colors_uv(&mut enc, &pal.u_colors[..n], &pal.v_colors[..n], &[]); |
| 2072 | let mut idx_cdfs = cdf::PALETTE_UV_COLOR_INDEX[n - 2]; |
| 2073 | write_color_index_map( |
| 2074 | &mut enc, |
| 2075 | &mut idx_cdfs, |
| 2076 | &pal.map, |
| 2077 | palette_uv_side(side), |
| 2078 | n, |
| 2079 | on, |
| 2080 | ); |
| 2081 | enc.bits() |
| 2082 | } |
| 2083 | |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | |
| 2095 | pub(super) fn intrabc_dv_pred(mi_r: usize, mi_c: usize, side: usize) -> (i32, i32) { |
| 2096 | const INTRABC_DELAY_PIXELS: i32 = 256; |
| 2097 | let n4 = side / MI; |
| 2098 | let stack_pred = INTRABC_GRID.with(|g| { |
| 2099 | let g = g.borrow(); |
| 2100 | g.as_ref().map(|(grid, mi_cols, mi_rows)| { |
| 2101 | let stack = crate::mvstack::find_mv_stack( |
| 2102 | grid, mi_r, mi_c, n4, n4, 0, |
| 2103 | *mi_cols, *mi_rows, |
| 2104 | ); |
| 2105 | if stack.nearest_mv == (0, 0) { |
| 2106 | stack.near_mv |
| 2107 | } else { |
| 2108 | stack.nearest_mv |
| 2109 | } |
| 2110 | }) |
| 2111 | }); |
| 2112 | let mut pred = stack_pred.unwrap_or((0, 0)); |
| 2113 | if pred == (0, 0) { |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | let sb_mi = if sb128_armed() { 32i32 } else { 16i32 }; |
| 2121 | let sb_px = sb_mi * MI as i32; |
| 2122 | pred = if (mi_r as i32) < sb_mi { |
| 2123 | (0, -(sb_px + INTRABC_DELAY_PIXELS) * 8) |
| 2124 | } else { |
| 2125 | (-sb_px * 8, 0) |
| 2126 | }; |
| 2127 | } |
| 2128 | ((pred.0 >> 3) * 8, (pred.1 >> 3) * 8) |
| 2129 | } |
| 2130 | |
| 2131 | |
| 2132 | |
| 2133 | |
| 2134 | pub(super) fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>) { |
| 2135 | INTRABC_GRID.with(|g| { |
| 2136 | let mut g = g.borrow_mut(); |
| 2137 | let Some((grid, _, _)) = g.as_mut() else { |
| 2138 | return; |
| 2139 | }; |
| 2140 | let info = crate::mvstack::MiInfo { |
| 2141 | is_inter: dv.is_some(), |
| 2142 | ref_frame: 0, |
| 2143 | ref_frame1: crate::mvstack::NO_REF1, |
| 2144 | mv: crate::mvstack::mv16(dv.unwrap_or((0, 0))), |
| 2145 | mv1: (0, 0), |
| 2146 | is_new_mv: dv.is_some(), |
| 2147 | is_global_mv0: false, |
| 2148 | is_global_mv1: false, |
| 2149 | size: n4 as u8, |
| 2150 | size_h: n4 as u8, |
| 2151 | }; |
| 2152 | for r in mi_r..mi_r + n4 { |
| 2153 | for c in mi_c..mi_c + n4 { |
| 2154 | grid.set(r, c, info); |
| 2155 | } |
| 2156 | } |
| 2157 | }); |
| 2158 | } |
| 2159 | |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | pub(super) fn write_dv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i32) -> Result<()> { |
| 2166 | let mag = diff.unsigned_abs() as i32; |
| 2167 | if mag % 8 != 0 { |
| 2168 | return Err(Error::unsupported( |
| 2169 | "AV1 tile", |
| 2170 | "a block vector is coded at full-pel precision only", |
| 2171 | )); |
| 2172 | } |
| 2173 | enc.symbol(usize::from(diff < 0), &mut c.sign); |
| 2174 | let z = mag - 1; |
| 2175 | let class = mv_class_of(z); |
| 2176 | enc.symbol(class, &mut c.class); |
| 2177 | let local = z - mv_class_base(class); |
| 2178 | if class == 0 { |
| 2179 | enc.symbol(((local >> 3) & 1) as usize, &mut c.class0_bit); |
| 2180 | } else { |
| 2181 | let d = local >> 3; |
| 2182 | for i in 0..class { |
| 2183 | enc.symbol(((d >> i) & 1) as usize, &mut c.bit[i]); |
| 2184 | } |
| 2185 | } |
| 2186 | Ok(()) |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | pub(super) fn write_dv( |
| 2192 | enc: &mut SymbolEncoder, |
| 2193 | cdfs: &mut Cdfs, |
| 2194 | dv: (i32, i32), |
| 2195 | pred: (i32, i32), |
| 2196 | ) -> Result<()> { |
| 2197 | let diff = (dv.0 - pred.0, dv.1 - pred.1); |
| 2198 | let joint = match (diff.0 != 0, diff.1 != 0) { |
| 2199 | (false, false) => 0, |
| 2200 | (false, true) => 1, |
| 2201 | (true, false) => 2, |
| 2202 | (true, true) => 3, |
| 2203 | }; |
| 2204 | enc.symbol(joint, &mut cdfs.dv_joint); |
| 2205 | if diff.0 != 0 { |
| 2206 | write_dv_component(enc, &mut cdfs.dv_comp[0], diff.0)?; |
| 2207 | } |
| 2208 | if diff.1 != 0 { |
| 2209 | write_dv_component(enc, &mut cdfs.dv_comp[1], diff.1)?; |
| 2210 | } |
| 2211 | Ok(()) |
| 2212 | } |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | pub(super) fn write_intrabc_block( |
| 2221 | enc: &mut SymbolEncoder, |
| 2222 | cdfs: &mut Cdfs, |
| 2223 | neighbours: &mut Neighbours, |
| 2224 | dv: (i32, i32), |
| 2225 | mi: (usize, usize), |
| 2226 | side: usize, |
| 2227 | ) -> Result<()> { |
| 2228 | let skip_ctx = |
| 2229 | usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]); |
| 2230 | enc.symbol(1, &mut cdfs.skip[skip_ctx]); |
| 2231 | write_cdef_idx(enc, mi, true); |
| 2232 | enc.symbol(1, &mut cdfs.intrabc); |
| 2233 | write_dv(enc, cdfs, dv, intrabc_dv_pred(mi.0, mi.1, side))?; |
| 2234 | note_intrabc(dv); |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | let empty: [Vec<i32>; 3] = [vec![0], vec![0], vec![0]]; |
| 2243 | for cell in 0..(side / SUB).max(1) { |
| 2244 | let (r, c) = (mi.0 / (SUB / MI), mi.1 / (SUB / MI)); |
| 2245 | neighbours.above_mode[c + cell] = DC_PRED; |
| 2246 | neighbours.left_mode[r + cell] = DC_PRED; |
| 2247 | neighbours.above_side[c + cell] = side; |
| 2248 | neighbours.left_side[r + cell] = side; |
| 2249 | } |
| 2250 | neighbours.record_mi_planes(mi, side, &empty, true); |
| 2251 | neighbours.record_tx(mi, side, side.min(64)); |
| 2252 | for cell in 0..side / MI { |
| 2253 | neighbours.above_skip[mi.1 + cell] = true; |
| 2254 | neighbours.left_skip[mi.0 + cell] = true; |
| 2255 | } |
| 2256 | Ok(()) |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | pub(crate) const UV_CFL_PRED: usize = 13; |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | |
| 2268 | |
| 2269 | pub(crate) fn cfl_joint_sign(alpha_u: i32, alpha_v: i32) -> usize { |
| 2270 | let sign = |a: i32| match a.signum() { |
| 2271 | 0 => 0, |
| 2272 | -1 => 1, |
| 2273 | _ => 2, |
| 2274 | }; |
| 2275 | let (want_u, want_v) = (sign(alpha_u), sign(alpha_v)); |
| 2276 | (0..8) |
| 2277 | .find(|&j| { |
| 2278 | let su = ((j + 1) * 11) >> 5; |
| 2279 | (su, (j + 1) - 3 * su) == (want_u, want_v) |
| 2280 | }) |
| 2281 | .expect("cfl_alpha_signs covers every pair but (ZERO, ZERO)") as usize |
| 2282 | } |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | pub(super) fn cfl_alpha_ctx(joint: usize, plane: usize) -> usize { |
| 2288 | let joint = joint as i32; |
| 2289 | let su = ((joint + 1) * 11) >> 5; |
| 2290 | let sv = (joint + 1) - 3 * su; |
| 2291 | if plane == 0 { |
| 2292 | (joint + 1 - 3) as usize |
| 2293 | } else { |
| 2294 | (sv * 3 + su - 3) as usize |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | pub(crate) fn cfl_alpha_bits(alpha_u: i32, alpha_v: i32) -> f64 { |
| 2301 | use crate::encode::symbol_bits; |
| 2302 | let joint = cfl_joint_sign(alpha_u, alpha_v); |
| 2303 | let mut bits = symbol_bits(&crate::cdf::CFL_SIGN, joint); |
| 2304 | for (plane, alpha) in [alpha_u, alpha_v].into_iter().enumerate() { |
| 2305 | if alpha != 0 { |
| 2306 | bits += symbol_bits( |
| 2307 | &crate::cdf::CFL_ALPHA[cfl_alpha_ctx(joint, plane)], |
| 2308 | (alpha.unsigned_abs() - 1) as usize, |
| 2309 | ); |
| 2310 | } |
| 2311 | } |
| 2312 | bits |
| 2313 | } |
| 2314 | |
| 2315 | |
| 2316 | |
| 2317 | pub(super) fn write_cfl_alphas(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, (alpha_u, alpha_v): (i32, i32)) { |
| 2318 | let joint = cfl_joint_sign(alpha_u, alpha_v); |
| 2319 | enc.symbol(joint, &mut cdfs.cfl_sign); |
| 2320 | for (plane, alpha) in [alpha_u, alpha_v].into_iter().enumerate() { |
| 2321 | if alpha != 0 { |
| 2322 | enc.symbol( |
| 2323 | (alpha.unsigned_abs() - 1) as usize, |
| 2324 | &mut cdfs.cfl_alpha[cfl_alpha_ctx(joint, plane)], |
| 2325 | ); |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | |
| 2331 | #[allow(clippy::too_many_arguments)] |
| 2332 | pub(super) fn write_intra_mode( |
| 2333 | enc: &mut SymbolEncoder, |
| 2334 | cdfs: &mut Cdfs, |
| 2335 | neighbours: &mut Neighbours, |
| 2336 | block: &BlockCoeffs, |
| 2337 | above_mode: usize, |
| 2338 | left_mode: usize, |
| 2339 | cfl: bool, |
| 2340 | |
| 2341 | |
| 2342 | mi: (usize, usize), |
| 2343 | |
| 2344 | |
| 2345 | side: usize, |
| 2346 | ) -> usize { |
| 2347 | let mode = usize::from(block.mode); |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | let skip_ctx = |
| 2353 | usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]); |
| 2354 | enc.symbol(0, &mut cdfs.skip[skip_ctx]); |
| 2355 | write_cdef_idx(enc, mi, false); |
| 2356 | |
| 2357 | |
| 2358 | if intrabc_armed() { |
| 2359 | enc.symbol(0, &mut cdfs.intrabc); |
| 2360 | } |
| 2361 | enc.symbol( |
| 2362 | mode, |
| 2363 | &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]], |
| 2364 | ); |
| 2365 | ec_rng_trace(|| format!("EC_YMODE mode={mode} tell={} rng={}", enc.tell(), enc.rng())); |
| 2366 | if (V_PRED..=D67_PRED).contains(&mode) { |
| 2367 | enc.symbol( |
| 2368 | (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize, |
| 2369 | &mut cdfs.angle_delta[mode - V_PRED], |
| 2370 | ); |
| 2371 | } |
| 2372 | |
| 2373 | |
| 2374 | let uv_mode = usize::from(block.uv_mode); |
| 2375 | if cfl { |
| 2376 | enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]); |
| 2377 | } else { |
| 2378 | debug_assert_ne!(uv_mode, UV_CFL_PRED, "is_cfl_allowed excludes this block"); |
| 2379 | enc.symbol(uv_mode, &mut cdfs.uv_mode_no_cfl[mode]); |
| 2380 | } |
| 2381 | if uv_mode == UV_CFL_PRED { |
| 2382 | write_cfl_alphas( |
| 2383 | enc, |
| 2384 | cdfs, |
| 2385 | block |
| 2386 | .cfl_alphas |
| 2387 | .expect("a UV_CFL_PRED block carries its alphas"), |
| 2388 | ); |
| 2389 | } |
| 2390 | |
| 2391 | |
| 2392 | if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 2393 | enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]); |
| 2394 | } |
| 2395 | |
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | |
| 2400 | |
| 2401 | |
| 2402 | write_palette_syntax( |
| 2403 | enc, |
| 2404 | cdfs, |
| 2405 | neighbours, |
| 2406 | block.palette.as_ref(), |
| 2407 | block.palette_uv.as_ref(), |
| 2408 | block.filter_intra, |
| 2409 | mode, |
| 2410 | uv_mode, |
| 2411 | mi, |
| 2412 | side, |
| 2413 | ); |
| 2414 | mode |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | |
| 2422 | |
| 2423 | |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | #[allow(clippy::too_many_arguments)] |
| 2431 | pub(super) fn write_palette_syntax( |
| 2432 | enc: &mut SymbolEncoder, |
| 2433 | cdfs: &mut Cdfs, |
| 2434 | neighbours: &mut Neighbours, |
| 2435 | palette: Option<&PaletteY>, |
| 2436 | palette_uv: Option<&PaletteUv>, |
| 2437 | |
| 2438 | |
| 2439 | |
| 2440 | |
| 2441 | filter_intra: Option<u8>, |
| 2442 | mode: usize, |
| 2443 | uv_mode: usize, |
| 2444 | mi: (usize, usize), |
| 2445 | side: usize, |
| 2446 | ) { |
| 2447 | if !screen_armed() { |
| 2448 | write_filter_intra(enc, cdfs, mode, side, false, filter_intra); |
| 2449 | return; |
| 2450 | } |
| 2451 | let palette = palette.filter(|_| mode == DC_PRED); |
| 2452 | let palette_uv = palette_uv.filter(|_| uv_mode == DC_PRED); |
| 2453 | if let Some(bsize_ctx) = crate::decode::palette_bsize_ctx(side) { |
| 2454 | let (mode_ctx, cache) = neighbours.palette_ctx_and_cache(mi); |
| 2455 | let uv_cache = neighbours.palette_uv_cache(mi); |
| 2456 | if mode == DC_PRED { |
| 2457 | enc.symbol( |
| 2458 | usize::from(palette.is_some()), |
| 2459 | &mut cdfs.palette_y_mode[bsize_ctx][mode_ctx], |
| 2460 | ); |
| 2461 | } |
| 2462 | if let Some(pal) = palette { |
| 2463 | let n = usize::from(pal.size); |
| 2464 | enc.symbol(n - 2, &mut cdfs.palette_y_size[bsize_ctx]); |
| 2465 | write_palette_colors_y(enc, &pal.colors[..n], &cache); |
| 2466 | } |
| 2467 | if uv_mode == DC_PRED { |
| 2468 | enc.symbol( |
| 2469 | usize::from(palette_uv.is_some()), |
| 2470 | &mut cdfs.palette_uv_mode[usize::from(palette.is_some())], |
| 2471 | ); |
| 2472 | } |
| 2473 | if let Some(pal) = palette_uv { |
| 2474 | let n = usize::from(pal.size); |
| 2475 | enc.symbol(n - 2, &mut cdfs.palette_uv_size[bsize_ctx]); |
| 2476 | write_palette_colors_uv(enc, &pal.u_colors[..n], &pal.v_colors[..n], &uv_cache); |
| 2477 | } |
| 2478 | write_filter_intra(enc, cdfs, mode, side, palette.is_some(), filter_intra); |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | |
| 2483 | let on = crate::decode::palette_onscreen_dims(mi.0, mi.1, side, side, frame_mi_dims()); |
| 2484 | if (on.0 < side || on.1 < side) && (palette.is_some() || palette_uv.is_some()) { |
| 2485 | PALETTE_CUT_MAPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2486 | } |
| 2487 | if let Some(pal) = palette { |
| 2488 | let n = usize::from(pal.size); |
| 2489 | write_color_index_map( |
| 2490 | enc, |
| 2491 | &mut cdfs.palette_y_color_index[n - 2], |
| 2492 | &pal.map, |
| 2493 | side, |
| 2494 | n, |
| 2495 | on, |
| 2496 | ); |
| 2497 | note_palette(n); |
| 2498 | } |
| 2499 | if let Some(pal) = palette_uv { |
| 2500 | let n = usize::from(pal.size); |
| 2501 | let uv_side = palette_uv_side(side); |
| 2502 | write_color_index_map( |
| 2503 | enc, |
| 2504 | &mut cdfs.palette_uv_color_index[n - 2], |
| 2505 | &pal.map, |
| 2506 | uv_side, |
| 2507 | n, |
| 2508 | |
| 2509 | crate::decode::palette_onscreen_uv((side, side), on, (uv_side, uv_side), (1, 1)), |
| 2510 | ); |
| 2511 | note_palette_uv(n); |
| 2512 | } |
| 2513 | } else { |
| 2514 | |
| 2515 | |
| 2516 | write_filter_intra(enc, cdfs, mode, side, false, filter_intra); |
| 2517 | } |
| 2518 | neighbours.pending_palette = palette.map(|p| (p.size, p.colors)); |
| 2519 | neighbours.pending_palette_uv = palette_uv.map(|p| (p.size, p.u_colors)); |
| 2520 | } |
| 2521 | |