wip(av1): tile writer codes tx_depth and per-transform-unit intra luma
Browse files at this commitcf8b659a0b066c16cc2bba4701e80050cf3b4562Parent: b0a80f33 changed files +304 -11
- crates/ec-av1/src/decode.rs +1 -1
- crates/ec-av1/src/encode.rs +3 -0
- crates/ec-av1/src/tile.rs +300 -10
crates/ec-av1/src/decode.rsmodified+1 -1
| @@ -6360,7 +6360,7 @@ fn neighbour_state(grid: &[i32]) -> Neighbour { | ||
| 6360 | 6360 | /// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, |
| 6361 | 6361 | /// transform unit smaller than the block it sits in): `top`/`left` are the |
| 6362 | 6362 | /// above/left neighbours' magnitude tiers, each already clamped to 4. |
| 6363 | -const SKIP_CONTEXTS: [[usize; 5]; 5] = [ | |
| 6363 | +pub(crate) const SKIP_CONTEXTS: [[usize; 5]; 5] = [ | |
| 6364 | 6364 | [1, 2, 2, 2, 3], |
| 6365 | 6365 | [2, 4, 4, 4, 5], |
| 6366 | 6366 | [2, 4, 4, 4, 5], |
crates/ec-av1/src/encode.rsmodified+3 -0
| @@ -2001,6 +2001,7 @@ fn code_square_inter( | ||
| 2001 | 2001 | uv_mode: DC_PRED, |
| 2002 | 2002 | skip: skip_new, |
| 2003 | 2003 | eight: None, |
| 2004 | + tx_depth: 0, | |
| 2004 | 2005 | inter: Some(InterInfo { |
| 2005 | 2006 | mode: InterMode::NewMv, |
| 2006 | 2007 | mv: new_mv, |
| @@ -2023,6 +2024,7 @@ fn code_square_inter( | ||
| 2023 | 2024 | uv_mode: DC_PRED, |
| 2024 | 2025 | skip, |
| 2025 | 2026 | eight: None, |
| 2027 | + tx_depth: 0, | |
| 2026 | 2028 | inter: Some(InterInfo { |
| 2027 | 2029 | mode: InterMode::NearestMv, |
| 2028 | 2030 | mv, |
| @@ -3256,6 +3258,7 @@ fn search_inter_block( | ||
| 3256 | 3258 | skip: best.skip, |
| 3257 | 3259 | inter: best.inter, |
| 3258 | 3260 | eight: None, |
| 3261 | + tx_depth: 0, | |
| 3259 | 3262 | }, |
| 3260 | 3263 | best.cost, |
| 3261 | 3264 | ) |
crates/ec-av1/src/tile.rsmodified+300 -10
| @@ -477,6 +477,13 @@ pub struct BlockCoeffs { | ||
| 477 | 477 | /// at their defaults and unused, the same way a `Whole` [`Superblock`]'s |
| 478 | 478 | /// per-quadrant fields are unused. |
| 479 | 479 | pub eight: Option<Vec<BlockCoeffs>>, |
| 480 | + /// How many times this block's luma transform is halved under | |
| 481 | + /// `TxMode::Select` (spec `tx_depth`, 0 = one transform over the whole | |
| 482 | + /// block). Read only by the writers a `tx_select` frame calls; a | |
| 483 | + /// `TxMode::Largest` frame codes no symbol and leaves this at zero. | |
| 484 | + /// `luma` then carries the whole block's levels in BLOCK coordinates, | |
| 485 | + /// which the writer slices per transform unit. | |
| 486 | + pub tx_depth: u8, | |
| 480 | 487 | } |
| 481 | 488 | |
| 482 | 489 | /// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec |
| @@ -543,6 +550,11 @@ const MI: usize = 4; | ||
| 543 | 550 | struct Neighbour { |
| 544 | 551 | /// Whether the plane's transform block carried a coefficient. |
| 545 | 552 | coded: bool, |
| 553 | + /// Its cumulative coefficient level (spec `cul_level`, clamped to 7) -- | |
| 554 | + /// what the *luma* `txb_skip_ctx` of a transform unit smaller than its | |
| 555 | + /// own block reads (`get_txb_ctx_general`, decode.rs `luma_skip_ctx`), | |
| 556 | + /// where "coded or not" is not enough. | |
| 557 | + level: u8, | |
| 546 | 558 | /// The sign of its DC, absent when the DC itself is zero. |
| 547 | 559 | dc: Option<bool>, |
| 548 | 560 | } |
| @@ -640,6 +652,15 @@ struct Neighbours { | ||
| 640 | 652 | above_skip: Vec<bool>, |
| 641 | 653 | /// The same, down the left edge. |
| 642 | 654 | left_skip: Vec<bool>, |
| 655 | + /// The side, in pixels, of the transform last written in this 4x4 | |
| 656 | + /// mode-info column -- the deblock `tx_grid` a key frame's | |
| 657 | + /// `get_tx_size_context` reads (decode.rs `tx_size_context`/`tx_px_at`), | |
| 658 | + /// which is what picks the CDF row of a `TxMode::Select` block's | |
| 659 | + /// `tx_depth` symbol. | |
| 660 | + above_tx: Vec<u8>, | |
| 661 | + /// The same, down the left edge (the transform's HEIGHT, `tx_h_px_at`; | |
| 662 | + /// every transform this writer codes is square, so one value serves). | |
| 663 | + left_tx: Vec<u8>, | |
| 643 | 664 | /// Whether the neighbour was coded inter, for the `is_inter` context. |
| 644 | 665 | /// Unused by the key frame writers. |
| 645 | 666 | above_inter: Vec<bool>, |
| @@ -688,6 +709,8 @@ impl Neighbours { | ||
| 688 | 709 | left_skip: vec![false; rows * (SUB / MI)], |
| 689 | 710 | above_inter: vec![false; cols * (SUB / MI)], |
| 690 | 711 | left_inter: vec![false; rows * (SUB / MI)], |
| 712 | + above_tx: vec![0; cols * (SUB / MI)], | |
| 713 | + left_tx: vec![0; rows * (SUB / MI)], | |
| 691 | 714 | mi_cols, |
| 692 | 715 | mi_rows, |
| 693 | 716 | } |
| @@ -701,6 +724,57 @@ impl Neighbours { | ||
| 701 | 724 | self.left_side_mi.iter_mut().for_each(|s| *s = SB); |
| 702 | 725 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 703 | 726 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 727 | + self.left_tx.iter_mut().for_each(|t| *t = 0); | |
| 728 | + } | |
| 729 | + | |
| 730 | + /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]): | |
| 731 | + /// whether the transform above is at least as wide as this block's own | |
| 732 | + /// largest transform, plus whether the one to the left is at least as | |
| 733 | + /// tall. A neighbour outside the tile contributes nothing. | |
| 734 | + fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize { | |
| 735 | + usize::from(mi_r > 0 && usize::from(self.above_tx[mi_c]) >= max_tx) | |
| 736 | + + usize::from(mi_c > 0 && usize::from(self.left_tx[mi_r]) >= max_tx) | |
| 737 | + } | |
| 738 | + | |
| 739 | + /// Publishes one block's resolved transform side over every 4x4 unit it | |
| 740 | + /// covers, the way the decoder's `fill_lf_grid` does at the end of every | |
| 741 | + /// block. | |
| 742 | + fn record_tx(&mut self, (mi_r, mi_c): (usize, usize), side: usize, tx_px: usize) { | |
| 743 | + for cell in 0..side / MI { | |
| 744 | + if mi_r + cell < self.left_tx.len() { | |
| 745 | + self.left_tx[mi_r + cell] = tx_px as u8; | |
| 746 | + } | |
| 747 | + if mi_c + cell < self.above_tx.len() { | |
| 748 | + self.above_tx[mi_c + cell] = tx_px as u8; | |
| 749 | + } | |
| 750 | + } | |
| 751 | + } | |
| 752 | + | |
| 753 | + /// The luma `txb_skip_ctx` of a transform unit smaller than its own block | |
| 754 | + /// (decode.rs `Neighbours::luma_skip_ctx`): the above and left magnitude | |
| 755 | + /// tiers, each ORed over the unit's own span and clamped to 4. | |
| 756 | + fn luma_skip_ctx(&self, (mi_r, mi_c): (usize, usize), side_mi: usize) -> usize { | |
| 757 | + let mut top = 0u8; | |
| 758 | + let mut left = 0u8; | |
| 759 | + for cell in 0..side_mi { | |
| 760 | + top |= self.above[mi_c + cell][0].level; | |
| 761 | + left |= self.left[mi_r + cell][0].level; | |
| 762 | + } | |
| 763 | + crate::decode::SKIP_CONTEXTS[usize::from(top).min(4)][usize::from(left).min(4)] | |
| 764 | + } | |
| 765 | + | |
| 766 | + /// Publishes ONE luma transform unit's coefficient context, the way the | |
| 767 | + /// decoder's `record_mi_luma` does between the units of a split block. | |
| 768 | + fn record_mi_luma(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, grid: &[i32]) { | |
| 769 | + let state = neighbour_state(grid); | |
| 770 | + for cell in 0..tx_px / MI { | |
| 771 | + if mi_r + cell < self.mi_rows { | |
| 772 | + self.left[mi_r + cell][0] = state; | |
| 773 | + } | |
| 774 | + if mi_c + cell < self.mi_cols { | |
| 775 | + self.above[mi_c + cell][0] = state; | |
| 776 | + } | |
| 777 | + } | |
| 704 | 778 | } |
| 705 | 779 | |
| 706 | 780 | /// Writes one coded block into every 16x16 column and row it covers, and, |
| @@ -711,6 +785,22 @@ impl Neighbours { | ||
| 711 | 785 | /// derived from the true `mi_cols`/`mi_rows`, not from this block's own |
| 712 | 786 | /// side). |
| 713 | 787 | fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) { |
| 788 | + self.record_planes(at, side, mode, grids, true); | |
| 789 | + } | |
| 790 | + | |
| 791 | + /// [`Self::record`] with a switch for plane 0: a block whose luma was | |
| 792 | + /// written as several transform units published each unit's own context | |
| 793 | + /// as it went ([`Self::record_mi_luma`]), so the whole-block write here | |
| 794 | + /// would clobber what the next block reads (decode.rs | |
| 795 | + /// `record_split_luma`). | |
| 796 | + fn record_planes( | |
| 797 | + &mut self, | |
| 798 | + at: (usize, usize), | |
| 799 | + side: usize, | |
| 800 | + mode: usize, | |
| 801 | + grids: &[Vec<i32>; 3], | |
| 802 | + luma: bool, | |
| 803 | + ) { | |
| 714 | 804 | let (r, c) = at; |
| 715 | 805 | for cell in 0..side / SUB { |
| 716 | 806 | self.above_mode[c + cell] = mode; |
| @@ -718,7 +808,7 @@ impl Neighbours { | ||
| 718 | 808 | self.above_side[c + cell] = side; |
| 719 | 809 | self.left_side[r + cell] = side; |
| 720 | 810 | } |
| 721 | - self.record_mi((r * (SUB / MI), c * (SUB / MI)), side, grids); | |
| 811 | + self.record_mi_planes((r * (SUB / MI), c * (SUB / MI)), side, grids, luma); | |
| 722 | 812 | } |
| 723 | 813 | |
| 724 | 814 | /// The coefficient-context half of [`Self::record`], taking the block's |
| @@ -729,6 +819,17 @@ impl Neighbours { | ||
| 729 | 819 | /// grid, so no resizing is needed to write into them at this |
| 730 | 820 | /// finer-than-SUB granularity. |
| 731 | 821 | fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) { |
| 822 | + self.record_mi_planes(at_mi, side, grids, true); | |
| 823 | + } | |
| 824 | + | |
| 825 | + /// [`Self::record_mi`] with [`Self::record_planes`]'s plane-0 switch. | |
| 826 | + fn record_mi_planes( | |
| 827 | + &mut self, | |
| 828 | + at_mi: (usize, usize), | |
| 829 | + side: usize, | |
| 830 | + grids: &[Vec<i32>; 3], | |
| 831 | + luma: bool, | |
| 832 | + ) { | |
| 732 | 833 | let (mi_r, mi_c) = at_mi; |
| 733 | 834 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 734 | 835 | let side_mi = side / MI; |
| @@ -754,6 +855,8 @@ impl Neighbours { | ||
| 754 | 855 | round_up_even(self.mi_cols), |
| 755 | 856 | ]; |
| 756 | 857 | for cell in 0..side_mi { |
| 858 | + let keep_left = self.left[mi_r + cell][0]; | |
| 859 | + let keep_above = self.above[mi_c + cell][0]; | |
| 757 | 860 | self.left[mi_r + cell] = std::array::from_fn(|plane| { |
| 758 | 861 | if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) { |
| 759 | 862 | states[plane] |
| @@ -768,6 +871,10 @@ impl Neighbours { | ||
| 768 | 871 | Default::default() |
| 769 | 872 | } |
| 770 | 873 | }); |
| 874 | + if !luma { | |
| 875 | + self.left[mi_r + cell][0] = keep_left; | |
| 876 | + self.above[mi_c + cell][0] = keep_above; | |
| 877 | + } | |
| 771 | 878 | } |
| 772 | 879 | } |
| 773 | 880 | |
| @@ -860,6 +967,25 @@ pub fn sb_coeff_key_frame_tile( | ||
| 860 | 967 | mi_rows: u32, |
| 861 | 968 | base_q_idx: u8, |
| 862 | 969 | superblocks: &[Superblock], |
| 970 | +) -> Result<Vec<u8>> { | |
| 971 | + sb_coeff_key_frame_tile_tx(mi_cols, mi_rows, base_q_idx, superblocks, false) | |
| 972 | +} | |
| 973 | + | |
| 974 | +/// [`sb_coeff_key_frame_tile`] for a frame header carrying | |
| 975 | +/// `tx_mode == TxMode::Select` (`tx_select`): every block then codes a | |
| 976 | +/// `tx_depth` symbol and, where that depth is nonzero, its luma residual as | |
| 977 | +/// several transform units ([`write_luma_select`]). With `tx_select` false | |
| 978 | +/// this writes exactly the stream it always did. | |
| 979 | +/// | |
| 980 | +/// # Errors | |
| 981 | +/// As [`sb_coeff_key_frame_tile`], plus a 64x64 block whose `tx_depth` is | |
| 982 | +/// nonzero (not written yet). | |
| 983 | +pub fn sb_coeff_key_frame_tile_tx( | |
| 984 | + mi_cols: u32, | |
| 985 | + mi_rows: u32, | |
| 986 | + base_q_idx: u8, | |
| 987 | + superblocks: &[Superblock], | |
| 988 | + tx_select: bool, | |
| 863 | 989 | ) -> Result<Vec<u8>> { |
| 864 | 990 | check_blocks(mi_cols, mi_rows)?; |
| 865 | 991 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| @@ -978,7 +1104,9 @@ pub fn sb_coeff_key_frame_tile( | ||
| 978 | 1104 | // A 64x64 block is too big to be offered chroma from |
| 979 | 1105 | // luma, so its chroma mode reads the table without it. |
| 980 | 1106 | false, |
| 981 | - ); | |
| 1107 | + tx_select, | |
| 1108 | + &scans, | |
| 1109 | + )?; | |
| 982 | 1110 | ec_rng_trace(|| { |
| 983 | 1111 | format!( |
| 984 | 1112 | "EC_TOK mi_row={} mi_col={} tell={}", |
| @@ -1056,7 +1184,9 @@ pub fn sb_coeff_key_frame_tile( | ||
| 1056 | 1184 | &grids, |
| 1057 | 1185 | [&scans[0], &scans[1], &scans[1]], |
| 1058 | 1186 | true, |
| 1059 | - ); | |
| 1187 | + tx_select, | |
| 1188 | + &scans, | |
| 1189 | + )?; | |
| 1060 | 1190 | ec_rng_trace(|| { |
| 1061 | 1191 | format!( |
| 1062 | 1192 | "EC_TOK mi_row={} mi_col={} tell={}", |
| @@ -1149,7 +1279,9 @@ pub fn sb_coeff_key_frame_tile( | ||
| 1149 | 1279 | &grids, |
| 1150 | 1280 | [&scans[1], &scans[2], &scans[2]], |
| 1151 | 1281 | true, |
| 1152 | - ); | |
| 1282 | + tx_select, | |
| 1283 | + &scans, | |
| 1284 | + )?; | |
| 1153 | 1285 | ec_rng_trace(|| { |
| 1154 | 1286 | format!( |
| 1155 | 1287 | "EC_TOK mi_row={} mi_col={} tell={}", |
| @@ -1251,7 +1383,9 @@ pub fn sb_coeff_key_frame_tile( | ||
| 1251 | 1383 | &grids, |
| 1252 | 1384 | [&scans[2], &scans[3], &scans[3]], |
| 1253 | 1385 | prev_leaf, |
| 1254 | - ); | |
| 1386 | + tx_select, | |
| 1387 | + &scans, | |
| 1388 | + )?; | |
| 1255 | 1389 | prev_leaf = Some((leaf_mi, leaf_mode)); |
| 1256 | 1390 | ec_rng_trace(|| { |
| 1257 | 1391 | format!( |
| @@ -1345,7 +1479,13 @@ fn write_block( | ||
| 1345 | 1479 | grids: &[Vec<i32>; 3], |
| 1346 | 1480 | scans: [&Vec<u16>; 3], |
| 1347 | 1481 | cfl: bool, |
| 1348 | -) { | |
| 1482 | + // The frame's `tx_mode == TxMode::Select`, and the block's own chosen | |
| 1483 | + // depth: with it the luma residual goes through [`write_luma_select`] | |
| 1484 | + // (one `tx_depth` symbol, then one transform unit per tile of the block) | |
| 1485 | + // instead of the single whole-block transform below. | |
| 1486 | + tx_select: bool, | |
| 1487 | + all_scans: &[Vec<u16>; 4], | |
| 1488 | +) -> Result<()> { | |
| 1349 | 1489 | let (r, c) = at; |
| 1350 | 1490 | let mode = write_intra_mode( |
| 1351 | 1491 | enc, |
| @@ -1355,6 +1495,23 @@ fn write_block( | ||
| 1355 | 1495 | neighbours.left_mode[r], |
| 1356 | 1496 | cfl, |
| 1357 | 1497 | ); |
| 1498 | + let at_mi = (r * (SUB / MI), c * (SUB / MI)); | |
| 1499 | + let split = if tx_select { | |
| 1500 | + let tx = write_luma_select( | |
| 1501 | + enc, | |
| 1502 | + cdfs, | |
| 1503 | + neighbours, | |
| 1504 | + at_mi, | |
| 1505 | + side, | |
| 1506 | + usize::from(block.tx_depth), | |
| 1507 | + &grids[0], | |
| 1508 | + mode, | |
| 1509 | + all_scans, | |
| 1510 | + )?; | |
| 1511 | + tx < side | |
| 1512 | + } else { | |
| 1513 | + false | |
| 1514 | + }; | |
| 1358 | 1515 | write_block_planes( |
| 1359 | 1516 | enc, |
| 1360 | 1517 | cdfs, |
| @@ -1363,8 +1520,10 @@ fn write_block( | ||
| 1363 | 1520 | &scans, |
| 1364 | 1521 | &neighbours.around(at, side), |
| 1365 | 1522 | mode, |
| 1523 | + tx_select, | |
| 1366 | 1524 | ); |
| 1367 | - neighbours.record(at, side, mode, grids); | |
| 1525 | + neighbours.record_planes(at, side, mode, grids, !split); | |
| 1526 | + Ok(()) | |
| 1368 | 1527 | } |
| 1369 | 1528 | |
| 1370 | 1529 | /// Writes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect): its own |
| @@ -1386,7 +1545,9 @@ fn write_leaf8( | ||
| 1386 | 1545 | grids: &[Vec<i32>; 3], |
| 1387 | 1546 | scans: [&Vec<u16>; 3], |
| 1388 | 1547 | prev_leaf: Option<((usize, usize), usize)>, |
| 1389 | -) -> usize { | |
| 1548 | + tx_select: bool, | |
| 1549 | + all_scans: &[Vec<u16>; 4], | |
| 1550 | +) -> Result<usize> { | |
| 1390 | 1551 | let (r, c) = outer_at; |
| 1391 | 1552 | let mut above_mode = neighbours.above_mode[c]; |
| 1392 | 1553 | let mut left_mode = neighbours.left_mode[r]; |
| @@ -1410,6 +1571,22 @@ fn write_leaf8( | ||
| 1410 | 1571 | // unchanged). |
| 1411 | 1572 | let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, true); |
| 1412 | 1573 | let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]; |
| 1574 | + let split = if tx_select { | |
| 1575 | + let tx = write_luma_select( | |
| 1576 | + enc, | |
| 1577 | + cdfs, | |
| 1578 | + neighbours, | |
| 1579 | + leaf_mi, | |
| 1580 | + 8, | |
| 1581 | + usize::from(block.tx_depth), | |
| 1582 | + &grids[0], | |
| 1583 | + mode, | |
| 1584 | + all_scans, | |
| 1585 | + )?; | |
| 1586 | + tx < 8 | |
| 1587 | + } else { | |
| 1588 | + false | |
| 1589 | + }; | |
| 1413 | 1590 | write_block_planes( |
| 1414 | 1591 | enc, |
| 1415 | 1592 | cdfs, |
| @@ -1418,9 +1595,10 @@ fn write_leaf8( | ||
| 1418 | 1595 | &scans, |
| 1419 | 1596 | &neighbours.around_mi(leaf_mi, 8), |
| 1420 | 1597 | mode, |
| 1598 | + tx_select, | |
| 1421 | 1599 | ); |
| 1422 | - neighbours.record_mi(leaf_mi, 8, grids); | |
| 1423 | - mode | |
| 1600 | + neighbours.record_mi_planes(leaf_mi, 8, grids, !split); | |
| 1601 | + Ok(mode) | |
| 1424 | 1602 | } |
| 1425 | 1603 | |
| 1426 | 1604 | /// Writes the three transform blocks of one coded block, in the order a |
| @@ -1434,8 +1612,14 @@ fn write_block_planes( | ||
| 1434 | 1612 | scans: &[&Vec<u16>; 3], |
| 1435 | 1613 | around: &[Around; 3], |
| 1436 | 1614 | mode: usize, |
| 1615 | + // A block whose luma was already written as several transform units | |
| 1616 | + // ([`write_luma_select`]) has only its chroma planes left here. | |
| 1617 | + skip_luma: bool, | |
| 1437 | 1618 | ) { |
| 1438 | 1619 | for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() { |
| 1620 | + if plane == 0 && skip_luma { | |
| 1621 | + continue; | |
| 1622 | + } | |
| 1439 | 1623 | // Luma's transform covers its whole block, which fixes the all-zero |
| 1440 | 1624 | // flag's context at zero; a chroma transform reads whether its |
| 1441 | 1625 | // neighbours coded anything, on top of the offset the chroma tables |
| @@ -1464,11 +1648,114 @@ fn write_block_planes( | ||
| 1464 | 1648 | } |
| 1465 | 1649 | } |
| 1466 | 1650 | |
| 1651 | +/// Writes one intra block's luma residual under `TxMode::Select`: the | |
| 1652 | +/// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size | |
| 1653 | +/// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the | |
| 1654 | +/// `(side / tx)^2` transform units in raster order -- each predicted and | |
| 1655 | +/// coded against what the units before it already left behind, which is why | |
| 1656 | +/// every unit publishes its own coefficient context before the next one reads | |
| 1657 | +/// it (decode.rs `decode_block`'s multi-transform-unit branch). | |
| 1658 | +/// | |
| 1659 | +/// `grid` is the block's levels in BLOCK coordinates (`side` by `side`) when | |
| 1660 | +/// the transform splits, and the single transform's own coefficient grid when | |
| 1661 | +/// it does not. Hands back the resolved transform side in pixels. | |
| 1662 | +#[allow(clippy::too_many_arguments)] | |
| 1663 | +fn write_luma_select( | |
| 1664 | + enc: &mut SymbolEncoder, | |
| 1665 | + cdfs: &mut Cdfs, | |
| 1666 | + neighbours: &mut Neighbours, | |
| 1667 | + at_mi: (usize, usize), | |
| 1668 | + side: usize, | |
| 1669 | + depth: usize, | |
| 1670 | + grid: &[i32], | |
| 1671 | + mode: usize, | |
| 1672 | + scans: &[Vec<u16>; 4], | |
| 1673 | +) -> Result<usize> { | |
| 1674 | + let max_tx = side.min(64); | |
| 1675 | + let ctx = neighbours.tx_size_ctx(at_mi, max_tx); | |
| 1676 | + match max_tx { | |
| 1677 | + 8 => enc.symbol(depth, &mut cdfs.tx_size_cat0[ctx]), | |
| 1678 | + 16 => enc.symbol(depth, &mut cdfs.tx_size_cat1[ctx]), | |
| 1679 | + 32 => enc.symbol(depth, &mut cdfs.tx_size_cat2[ctx]), | |
| 1680 | + 64 => enc.symbol(depth, &mut cdfs.tx_size_cat3[ctx]), | |
| 1681 | + _ => { | |
| 1682 | + return Err(Error::unsupported( | |
| 1683 | + "AV1 tile", | |
| 1684 | + "a tx_depth symbol is only coded at 8/16/32/64", | |
| 1685 | + )); | |
| 1686 | + } | |
| 1687 | + } | |
| 1688 | + let tx = max_tx >> depth; | |
| 1689 | + if depth > 0 && side > 32 { | |
| 1690 | + // A 64x64 block's own levels reach the writer as the 32x32 corner a | |
| 1691 | + // 64-point transform codes, not as a 64x64 block grid, so there is | |
| 1692 | + // nothing to slice per unit here. | |
| 1693 | + return Err(Error::unsupported( | |
| 1694 | + "AV1 tile", | |
| 1695 | + "a 64x64 block's split luma transform is not written yet", | |
| 1696 | + )); | |
| 1697 | + } | |
| 1698 | + let coeff_side = tx.min(32); | |
| 1699 | + let n = side / tx; | |
| 1700 | + let set = match tx { | |
| 1701 | + 32 => TxbSet::Luma32, | |
| 1702 | + 16 => TxbSet::Luma16, | |
| 1703 | + 8 => TxbSet::Luma8, | |
| 1704 | + 4 => TxbSet::Luma4, | |
| 1705 | + _ => TxbSet::Luma64, | |
| 1706 | + }; | |
| 1707 | + let scan = match coeff_side { | |
| 1708 | + 32 => &scans[0], | |
| 1709 | + 16 => &scans[1], | |
| 1710 | + 8 => &scans[2], | |
| 1711 | + _ => &scans[3], | |
| 1712 | + }; | |
| 1713 | + let (mi_r, mi_c) = at_mi; | |
| 1714 | + for tu_row in 0..n { | |
| 1715 | + for tu_col in 0..n { | |
| 1716 | + let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI)); | |
| 1717 | + let unit = if n == 1 { | |
| 1718 | + grid.to_vec() | |
| 1719 | + } else { | |
| 1720 | + (0..tx) | |
| 1721 | + .flat_map(|row| { | |
| 1722 | + grid[(tu_row * tx + row) * side + tu_col * tx..][..tx].to_vec() | |
| 1723 | + }) | |
| 1724 | + .collect() | |
| 1725 | + }; | |
| 1726 | + // spec `get_txb_ctx_general`: a lone transform unit covering its | |
| 1727 | + // whole block reads context 0, a smaller one the neighbour | |
| 1728 | + // magnitude table. | |
| 1729 | + let skip_ctx = if n == 1 { | |
| 1730 | + 0 | |
| 1731 | + } else { | |
| 1732 | + neighbours.luma_skip_ctx(tu_mi, tx / MI) | |
| 1733 | + }; | |
| 1734 | + let around = neighbours.around_mi(tu_mi, tx)[0]; | |
| 1735 | + write_coeffs( | |
| 1736 | + enc, | |
| 1737 | + &mut cdfs.txb(set, mode), | |
| 1738 | + &unit, | |
| 1739 | + scan, | |
| 1740 | + skip_ctx, | |
| 1741 | + dc_sign_ctx(around.dc_vote), | |
| 1742 | + Some(0), | |
| 1743 | + ); | |
| 1744 | + if n > 1 { | |
| 1745 | + neighbours.record_mi_luma(tu_mi, tx, &unit); | |
| 1746 | + } | |
| 1747 | + } | |
| 1748 | + } | |
| 1749 | + neighbours.record_tx(at_mi, side, tx); | |
| 1750 | + Ok(tx) | |
| 1751 | +} | |
| 1752 | + | |
| 1467 | 1753 | /// What a coded transform block leaves behind for the blocks that read it as a |
| 1468 | 1754 | /// neighbour. |
| 1469 | 1755 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 1470 | 1756 | Neighbour { |
| 1471 | 1757 | coded: grid.iter().any(|&l| l != 0), |
| 1758 | + level: grid.iter().map(|l| l.unsigned_abs()).sum::<u32>().min(7) as u8, | |
| 1472 | 1759 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 1473 | 1760 | } |
| 1474 | 1761 | } |
| @@ -2664,6 +2951,7 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2664 | 2951 | &[&scan32, &scan16, &scan16], |
| 2665 | 2952 | &neighbours.around(at, BLOCK), |
| 2666 | 2953 | mode_for_tx, |
| 2954 | + false, | |
| 2667 | 2955 | ); |
| 2668 | 2956 | neighbours.record(at, BLOCK, mode_for_tx, &grids); |
| 2669 | 2957 | } |
| @@ -2849,6 +3137,7 @@ fn write_inter_frame_leaf( | ||
| 2849 | 3137 | &[scan16, scan8, scan8], |
| 2850 | 3138 | &neighbours.around(at, SUB), |
| 2851 | 3139 | mode_for_tx, |
| 3140 | + false, | |
| 2852 | 3141 | ); |
| 2853 | 3142 | neighbours.record(at, SUB, mode_for_tx, &grids); |
| 2854 | 3143 | } |
| @@ -3047,6 +3336,7 @@ fn write_inter_frame_leaf8( | ||
| 3047 | 3336 | &[scan8, scan4, scan4], |
| 3048 | 3337 | &neighbours.around_mi(leaf_mi, 8), |
| 3049 | 3338 | mode_for_tx, |
| 3339 | + false, | |
| 3050 | 3340 | ); |
| 3051 | 3341 | neighbours.record_mi(leaf_mi, 8, &grids); |
| 3052 | 3342 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter); |