The RD pricer reads the real neighbour txb_skip/dc_sign contexts
Browse files at this commit9afbc09b9d3759ba5b96fe39f65c3273a0db5237Parent: fbeac19- feat
- the RD pricer reads the real neighbour txb_skip/dc_sign contexts
3 changed files +275 -23
- crates/ec-av1/src/cdf_state.rs +20 -0
- crates/ec-av1/src/encode.rs +205 -10
- crates/ec-av1/src/tile.rs +50 -13
crates/ec-av1/src/cdf_state.rsmodified+20 -0
| @@ -300,6 +300,26 @@ pub(crate) struct TxbTables<'a> { | |||
| 300 | pub tx_type: Option<&'a mut [u16]>, | 300 | pub tx_type: Option<&'a mut [u16]>, |
| 301 | } | 301 | } |
| 302 | 302 | ||
| 303 | +impl TxbSet { | ||
| 304 | + /// Whether this set codes a CHROMA plane's transform, which is what | ||
| 305 | + /// picks the `txb_skip` context rule the writer uses: chroma reads | ||
| 306 | + /// "did the above/left neighbour code anything" (`write_block_planes`), | ||
| 307 | + /// luma reads context 0 for a transform covering its whole block and | ||
| 308 | + /// `SKIP_CONTEXTS` for a smaller unit (`write_luma_tus`). | ||
| 309 | + pub(crate) fn is_chroma(self) -> bool { | ||
| 310 | + matches!( | ||
| 311 | + self, | ||
| 312 | + Self::Chroma4 | ||
| 313 | + | Self::Chroma8 | ||
| 314 | + | Self::Chroma16 | ||
| 315 | + | Self::Chroma32 | ||
| 316 | + | Self::ChromaRect8x4 | ||
| 317 | + | Self::ChromaRect16x8 | ||
| 318 | + | Self::ChromaRect32x16 | ||
| 319 | + ) | ||
| 320 | + } | ||
| 321 | +} | ||
| 322 | + | ||
| 303 | /// Every table a key frame's tile writer adapts. | 323 | /// Every table a key frame's tile writer adapts. |
| 304 | #[derive(Clone)] | 324 | #[derive(Clone)] |
| 305 | pub(crate) struct Cdfs { | 325 | pub(crate) struct Cdfs { |
crates/ec-av1/src/encode.rsmodified+205 -10
| @@ -1180,6 +1180,45 @@ fn too_large() -> Error { | |||
| 1180 | 1180 | ||
| 1181 | /// One plane of the picture being coded, and the reconstruction being built | 1181 | /// One plane of the picture being coded, and the reconstruction being built |
| 1182 | /// beside it. | 1182 | /// beside it. |
| 1183 | +/// What one committed 4x4 cell left behind for the coefficient contexts of | ||
| 1184 | +/// the blocks that read it as a neighbour -- tile.rs `neighbour_state`, which | ||
| 1185 | +/// is the same state decode.rs records. | ||
| 1186 | +#[derive(Clone, Copy, Default, PartialEq, Eq)] | ||
| 1187 | +struct CoefCtx { | ||
| 1188 | + /// spec `cul_level`: the sum of the unit's coded magnitudes, clamped to 7. | ||
| 1189 | + level: u8, | ||
| 1190 | + /// The DC sign this cell votes with: `-1` negative, `1` positive, `0` when | ||
| 1191 | + /// the DC itself was zero (tile.rs `dc_vote`). | ||
| 1192 | + dc: i8, | ||
| 1193 | +} | ||
| 1194 | + | ||
| 1195 | +/// One plane's coefficient contexts, committed in CODING ORDER: the search | ||
| 1196 | +/// prices partition TREES, so the neighbour state a candidate reads has to be | ||
| 1197 | +/// whatever the blocks coded before it left -- the parent's committed | ||
| 1198 | +/// neighbours for a tree's first child, the just-committed sibling for the | ||
| 1199 | +/// later ones. Keeping the map as a full per-4x4-cell grid rather than the | ||
| 1200 | +/// writer's two running bands is what makes that fall out: a trial commits | ||
| 1201 | +/// over its own rectangle ([`Plane::commit`]) and a losing trial is undone | ||
| 1202 | +/// with the pixels ([`Plane::restore`]), so what a candidate reads above and | ||
| 1203 | +/// to its left is exactly what the tile writer's `Neighbours` bands will hold | ||
| 1204 | +/// when it gets there. | ||
| 1205 | +/// | ||
| 1206 | +/// Before this map the pricer took both contexts as zero, which is exact for | ||
| 1207 | +/// a luma transform covering its whole block and the largest remaining row of | ||
| 1208 | +/// the pricer census everywhere else (all-zero chroma under-priced 41-81%). | ||
| 1209 | +#[derive(Default)] | ||
| 1210 | +struct CoefCtxMap { | ||
| 1211 | + cells: Vec<CoefCtx>, | ||
| 1212 | + stride: usize, | ||
| 1213 | + /// Whether the transforms being priced right now are SMALLER than the | ||
| 1214 | + /// block they sit in -- a `TxMode::Select` depth split | ||
| 1215 | + /// ([`Plane::code_tx_depth`]) or an inter var-tx split | ||
| 1216 | + /// ([`commit_inter_luma`]). That is the one thing the (x, y, side) of a | ||
| 1217 | + /// trial cannot tell, and it is what decides whether luma reads context 0 | ||
| 1218 | + /// or `SKIP_CONTEXTS` (tile.rs `write_luma_tus`). | ||
| 1219 | + tu_split: bool, | ||
| 1220 | +} | ||
| 1221 | + | ||
| 1183 | struct Plane<'a> { | 1222 | struct Plane<'a> { |
| 1184 | source: &'a [u8], | 1223 | source: &'a [u8], |
| 1185 | reconstruction: Vec<u8>, | 1224 | reconstruction: Vec<u8>, |
| @@ -1207,6 +1246,11 @@ struct Plane<'a> { | |||
| 1207 | tile_y0: usize, | 1246 | tile_y0: usize, |
| 1208 | tile_x1: usize, | 1247 | tile_x1: usize, |
| 1209 | tile_y1: usize, | 1248 | tile_y1: usize, |
| 1249 | + /// [`CoefCtxMap`]: what the blocks already committed left for the | ||
| 1250 | + /// coefficient contexts of the ones still being priced. Sized on the | ||
| 1251 | + /// first commit, so a plane literal does not have to know its own extent | ||
| 1252 | + /// twice. | ||
| 1253 | + ctx: CoefCtxMap, | ||
| 1210 | } | 1254 | } |
| 1211 | 1255 | ||
| 1212 | impl Plane<'_> { | 1256 | impl Plane<'_> { |
| @@ -1402,7 +1446,16 @@ impl Plane<'_> { | |||
| 1402 | .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2()) | 1446 | .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2()) |
| 1403 | .sum() | 1447 | .sum() |
| 1404 | } else { | 1448 | } else { |
| 1405 | - crate::tile::coeff_bits(&levels, at.set, crate::decode::q_ctx_of(base_q_idx)) | 1449 | + { |
| 1450 | + let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); | ||
| 1451 | + crate::tile::coeff_bits( | ||
| 1452 | + &levels, | ||
| 1453 | + at.set, | ||
| 1454 | + crate::decode::q_ctx_of(base_q_idx), | ||
| 1455 | + skip_ctx, | ||
| 1456 | + sign_ctx, | ||
| 1457 | + ) | ||
| 1458 | + } | ||
| 1406 | }; | 1459 | }; |
| 1407 | #[cfg(test)] | 1460 | #[cfg(test)] |
| 1408 | stage_add(3, t.elapsed()); | 1461 | stage_add(3, t.elapsed()); |
| @@ -1471,7 +1524,16 @@ impl Plane<'_> { | |||
| 1471 | .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2()) | 1524 | .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2()) |
| 1472 | .sum() | 1525 | .sum() |
| 1473 | } else { | 1526 | } else { |
| 1474 | - crate::tile::coeff_bits(&levels, set, crate::decode::q_ctx_of(base_q_idx)) | 1527 | + { |
| 1528 | + let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set); | ||
| 1529 | + crate::tile::coeff_bits( | ||
| 1530 | + &levels, | ||
| 1531 | + set, | ||
| 1532 | + crate::decode::q_ctx_of(base_q_idx), | ||
| 1533 | + skip_ctx, | ||
| 1534 | + sign_ctx, | ||
| 1535 | + ) | ||
| 1536 | + } | ||
| 1475 | }; | 1537 | }; |
| 1476 | #[cfg(test)] | 1538 | #[cfg(test)] |
| 1477 | stage_add(3, t.elapsed()); | 1539 | stage_add(3, t.elapsed()); |
| @@ -1509,6 +1571,9 @@ impl Plane<'_> { | |||
| 1509 | }; | 1571 | }; |
| 1510 | let mut levels = vec![0i32; side * side]; | 1572 | let mut levels = vec![0i32; side * side]; |
| 1511 | census_add(4, n * n); | 1573 | census_add(4, n * n); |
| 1574 | + // A unit smaller than its block reads the neighbour magnitude table | ||
| 1575 | + // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]). | ||
| 1576 | + self.ctx.tu_split = n > 1; | ||
| 1512 | let (mut sse, mut bits) = (0.0, 0.0); | 1577 | let (mut sse, mut bits) = (0.0, 0.0); |
| 1513 | for tu_row in 0..n { | 1578 | for tu_row in 0..n { |
| 1514 | for tu_col in 0..n { | 1579 | for tu_col in 0..n { |
| @@ -1530,15 +1595,100 @@ impl Plane<'_> { | |||
| 1530 | } | 1595 | } |
| 1531 | } | 1596 | } |
| 1532 | } | 1597 | } |
| 1598 | + self.ctx.tu_split = false; | ||
| 1533 | (levels, sse, bits) | 1599 | (levels, sse, bits) |
| 1534 | } | 1600 | } |
| 1535 | 1601 | ||
| 1536 | - /// Writes a trial's reconstruction back into the plane. | 1602 | + /// Writes a trial's reconstruction back into the plane, and the |
| 1603 | + /// coefficient context it leaves for its neighbours ([`CoefCtxMap`]). | ||
| 1537 | fn commit(&mut self, x: usize, y: usize, side: usize, trial: &Trial) { | 1604 | fn commit(&mut self, x: usize, y: usize, side: usize, trial: &Trial) { |
| 1538 | for row in 0..side { | 1605 | for row in 0..side { |
| 1539 | self.reconstruction[(y + row) * self.width + x..][..side] | 1606 | self.reconstruction[(y + row) * self.width + x..][..side] |
| 1540 | .copy_from_slice(&trial.reconstruction[row * side..][..side]); | 1607 | .copy_from_slice(&trial.reconstruction[row * side..][..side]); |
| 1541 | } | 1608 | } |
| 1609 | + self.commit_ctx(x, y, side, &trial.levels); | ||
| 1610 | + } | ||
| 1611 | + | ||
| 1612 | + /// Publishes one committed transform's own context over every 4x4 cell it | ||
| 1613 | + /// covers -- tile.rs `neighbour_state` + `Neighbours::record_planes`, | ||
| 1614 | + /// which is what the writer will read when it reaches the next block. | ||
| 1615 | + fn commit_ctx(&mut self, x: usize, y: usize, side: usize, levels: &[i32]) { | ||
| 1616 | + if self.ctx.cells.is_empty() { | ||
| 1617 | + self.ctx.stride = self.width.div_ceil(4); | ||
| 1618 | + self.ctx.cells = vec![CoefCtx::default(); self.ctx.stride * self.height.div_ceil(4)]; | ||
| 1619 | + } | ||
| 1620 | + let cul = levels.iter().map(|l| l.unsigned_abs()).sum::<u32>().min(7) as u8; | ||
| 1621 | + let state = CoefCtx { | ||
| 1622 | + level: cul, | ||
| 1623 | + dc: match levels[0].signum() { | ||
| 1624 | + 0 => 0, | ||
| 1625 | + -1 => -1, | ||
| 1626 | + _ => 1, | ||
| 1627 | + }, | ||
| 1628 | + }; | ||
| 1629 | + let n = (side / 4).max(1); | ||
| 1630 | + for row in 0..n { | ||
| 1631 | + let start = (y / 4 + row) * self.ctx.stride + x / 4; | ||
| 1632 | + let end = (start + n).min(self.ctx.cells.len()); | ||
| 1633 | + if start < end { | ||
| 1634 | + self.ctx.cells[start..end].fill(state); | ||
| 1635 | + } | ||
| 1636 | + } | ||
| 1637 | + } | ||
| 1638 | + | ||
| 1639 | + /// A block committed without any residual at all ([`ibc_commit`]): the | ||
| 1640 | + /// cells it covers coded nothing, and leaving an earlier trial's state | ||
| 1641 | + /// there would price its neighbours off a block that was never coded. | ||
| 1642 | + fn commit_ctx_zero(&mut self, x: usize, y: usize, side: usize) { | ||
| 1643 | + self.commit_ctx(x, y, side, &[0i32]); | ||
| 1644 | + } | ||
| 1645 | + | ||
| 1646 | + /// The `txb_skip` and `dc_sign` contexts a transform at `(x, y)` of this | ||
| 1647 | + /// plane reads, off what the blocks before it in coding order committed | ||
| 1648 | + /// ([`CoefCtxMap`]) -- the writer's own derivation: chroma reads whether | ||
| 1649 | + /// the above/left neighbours coded anything (tile.rs | ||
| 1650 | + /// `write_block_planes`), luma reads context 0 when its transform covers | ||
| 1651 | + /// its whole block and decode.rs `SKIP_CONTEXTS` off the neighbour | ||
| 1652 | + /// magnitude tiers when it does not (tile.rs `write_luma_tus` / | ||
| 1653 | + /// `Neighbours::luma_skip_ctx`), and both planes read the DC sign vote of | ||
| 1654 | + /// the same neighbours (tile.rs `dc_sign_ctx`). | ||
| 1655 | + /// | ||
| 1656 | + /// A neighbour outside this plane's own TILE does not exist, exactly as | ||
| 1657 | + /// the writer's per-tile `Neighbours` bands start blank. | ||
| 1658 | + fn coef_ctx(&self, x: usize, y: usize, side: usize, set: TxbSet) -> (usize, usize) { | ||
| 1659 | + if self.ctx.cells.is_empty() { | ||
| 1660 | + return (0, 0); | ||
| 1661 | + } | ||
| 1662 | + let n = (side / 4).max(1); | ||
| 1663 | + let (cx, cy) = (x / 4, y / 4); | ||
| 1664 | + let (mut top, mut left) = (0u8, 0u8); | ||
| 1665 | + let mut vote = 0i32; | ||
| 1666 | + if y > self.tile_y0 && cy > 0 { | ||
| 1667 | + for cell in &self.ctx.cells[(cy - 1) * self.ctx.stride + cx..][..n] { | ||
| 1668 | + top |= cell.level; | ||
| 1669 | + vote += i32::from(cell.dc); | ||
| 1670 | + } | ||
| 1671 | + } | ||
| 1672 | + if x > self.tile_x0 && cx > 0 { | ||
| 1673 | + for row in 0..n { | ||
| 1674 | + let cell = self.ctx.cells[(cy + row) * self.ctx.stride + cx - 1]; | ||
| 1675 | + left |= cell.level; | ||
| 1676 | + vote += i32::from(cell.dc); | ||
| 1677 | + } | ||
| 1678 | + } | ||
| 1679 | + let skip_ctx = if set.is_chroma() { | ||
| 1680 | + usize::from(top != 0) + usize::from(left != 0) | ||
| 1681 | + } else if self.ctx.tu_split { | ||
| 1682 | + crate::decode::SKIP_CONTEXTS[usize::from(top).min(4)][usize::from(left).min(4)] | ||
| 1683 | + } else { | ||
| 1684 | + 0 | ||
| 1685 | + }; | ||
| 1686 | + let sign_ctx = match vote.signum() { | ||
| 1687 | + 0 => 0, | ||
| 1688 | + -1 => 1, | ||
| 1689 | + _ => 2, | ||
| 1690 | + }; | ||
| 1691 | + (skip_ctx, sign_ctx) | ||
| 1542 | } | 1692 | } |
| 1543 | 1693 | ||
| 1544 | /// The source samples of one square, contiguous — what a motion search | 1694 | /// The source samples of one square, contiguous — what a motion search |
| @@ -1551,15 +1701,46 @@ impl Plane<'_> { | |||
| 1551 | 1701 | ||
| 1552 | /// The reconstructed samples of one square, so that a partition trial can | 1702 | /// The reconstructed samples of one square, so that a partition trial can |
| 1553 | /// be undone. | 1703 | /// be undone. |
| 1554 | - fn snapshot(&self, x: usize, y: usize, side: usize) -> Vec<u8> { | 1704 | + /// The reconstructed samples of one square AND the coefficient contexts |
| 1555 | - rows_of(&self.reconstruction, self.width, x, y, side) | 1705 | + /// its cells hold, so that a partition trial can be undone whole: a |
| 1706 | + /// losing trial that left its own nz/DC state behind would price the | ||
| 1707 | + /// blocks after it off a block the tile writer never codes. | ||
| 1708 | + fn snapshot(&self, x: usize, y: usize, side: usize) -> (Vec<u8>, Vec<CoefCtx>) { | ||
| 1709 | + ( | ||
| 1710 | + rows_of(&self.reconstruction, self.width, x, y, side), | ||
| 1711 | + self.ctx_rows(x, y, side), | ||
| 1712 | + ) | ||
| 1713 | + } | ||
| 1714 | + | ||
| 1715 | + fn ctx_rows(&self, x: usize, y: usize, side: usize) -> Vec<CoefCtx> { | ||
| 1716 | + if self.ctx.cells.is_empty() { | ||
| 1717 | + return Vec::new(); | ||
| 1718 | + } | ||
| 1719 | + let n = (side / 4).max(1); | ||
| 1720 | + (0..n) | ||
| 1721 | + .flat_map(|row| { | ||
| 1722 | + let start = (y / 4 + row) * self.ctx.stride + x / 4; | ||
| 1723 | + self.ctx.cells[start..(start + n).min(self.ctx.cells.len())].to_vec() | ||
| 1724 | + }) | ||
| 1725 | + .collect() | ||
| 1556 | } | 1726 | } |
| 1557 | 1727 | ||
| 1558 | /// Puts a snapshot back. | 1728 | /// Puts a snapshot back. |
| 1559 | - fn restore(&mut self, x: usize, y: usize, side: usize, samples: &[u8]) { | 1729 | + fn restore(&mut self, x: usize, y: usize, side: usize, saved: &(Vec<u8>, Vec<CoefCtx>)) { |
| 1560 | for row in 0..side { | 1730 | for row in 0..side { |
| 1561 | self.reconstruction[(y + row) * self.width + x..][..side] | 1731 | self.reconstruction[(y + row) * self.width + x..][..side] |
| 1562 | - .copy_from_slice(&samples[row * side..][..side]); | 1732 | + .copy_from_slice(&saved.0[row * side..][..side]); |
| 1733 | + } | ||
| 1734 | + if saved.1.is_empty() { | ||
| 1735 | + return; | ||
| 1736 | + } | ||
| 1737 | + let n = (side / 4).max(1); | ||
| 1738 | + for row in 0..n { | ||
| 1739 | + let start = (y / 4 + row) * self.ctx.stride + x / 4; | ||
| 1740 | + let end = (start + n).min(self.ctx.cells.len()); | ||
| 1741 | + if start < end { | ||
| 1742 | + self.ctx.cells[start..end].copy_from_slice(&saved.1[row * n..][..end - start]); | ||
| 1743 | + } | ||
| 1563 | } | 1744 | } |
| 1564 | } | 1745 | } |
| 1565 | 1746 | ||
| @@ -2381,6 +2562,10 @@ fn commit_inter_luma( | |||
| 2381 | let mut levels = vec![0i32; side * side]; | 2562 | let mut levels = vec![0i32; side * side]; |
| 2382 | let (mut sse, mut bits) = (0.0, 0.0); | 2563 | let (mut sse, mut bits) = (0.0, 0.0); |
| 2383 | let mut units = Vec::with_capacity(4); | 2564 | let mut units = Vec::with_capacity(4); |
| 2565 | + // The split units are smaller than their block, so their `txb_skip` | ||
| 2566 | + // context is the neighbour magnitude table ([`Plane::coef_ctx`]) -- and | ||
| 2567 | + // each unit's own commit publishes what the next one reads, coding order. | ||
| 2568 | + luma.ctx.tu_split = true; | ||
| 2384 | for tu_row in 0..2 { | 2569 | for tu_row in 0..2 { |
| 2385 | for tu_col in 0..2 { | 2570 | for tu_col in 0..2 { |
| 2386 | let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx); | 2571 | let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx); |
| @@ -2397,6 +2582,7 @@ fn commit_inter_luma( | |||
| 2397 | units.push((tu_x, tu_y, trial)); | 2582 | units.push((tu_x, tu_y, trial)); |
| 2398 | } | 2583 | } |
| 2399 | } | 2584 | } |
| 2585 | + luma.ctx.tu_split = false; | ||
| 2400 | let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1)); | 2586 | let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1)); |
| 2401 | let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0)); | 2587 | let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0)); |
| 2402 | if split_cost < flat_cost { | 2588 | if split_cost < flat_cost { |
| @@ -2874,7 +3060,9 @@ fn ibc_commit( | |||
| 2874 | let copied: Vec<u8> = plane.reconstruction[from..from + half].to_vec(); | 3060 | let copied: Vec<u8> = plane.reconstruction[from..from + half].to_vec(); |
| 2875 | plane.reconstruction[to..to + half].copy_from_slice(&copied); | 3061 | plane.reconstruction[to..to + half].copy_from_slice(&copied); |
| 2876 | } | 3062 | } |
| 3063 | + plane.commit_ctx_zero(x / 2, y / 2, half); | ||
| 2877 | } | 3064 | } |
| 3065 | + luma.commit_ctx_zero(x, y, side); | ||
| 2878 | } | 3066 | } |
| 2879 | 3067 | ||
| 2880 | /// Searches this block's own DV: every hash-table position whose 16x16 luma | 3068 | /// Searches this block's own DV: every hash-table position whose 16x16 luma |
| @@ -3972,7 +4160,7 @@ fn snapshot( | |||
| 3972 | chroma: &[Plane; 2], | 4160 | chroma: &[Plane; 2], |
| 3973 | (x, y): (usize, usize), | 4161 | (x, y): (usize, usize), |
| 3974 | side: usize, | 4162 | side: usize, |
| 3975 | -) -> [Vec<u8>; 3] { | 4163 | +) -> [(Vec<u8>, Vec<CoefCtx>); 3] { |
| 3976 | [ | 4164 | [ |
| 3977 | luma.snapshot(x, y, side), | 4165 | luma.snapshot(x, y, side), |
| 3978 | chroma[0].snapshot(x / 2, y / 2, side / 2), | 4166 | chroma[0].snapshot(x / 2, y / 2, side / 2), |
| @@ -3986,7 +4174,7 @@ fn restore( | |||
| 3986 | chroma: &mut [Plane; 2], | 4174 | chroma: &mut [Plane; 2], |
| 3987 | (x, y): (usize, usize), | 4175 | (x, y): (usize, usize), |
| 3988 | side: usize, | 4176 | side: usize, |
| 3989 | - saved: &[Vec<u8>; 3], | 4177 | + saved: &[(Vec<u8>, Vec<CoefCtx>); 3], |
| 3990 | ) { | 4178 | ) { |
| 3991 | luma.restore(x, y, side, &saved[0]); | 4179 | luma.restore(x, y, side, &saved[0]); |
| 3992 | chroma[0].restore(x / 2, y / 2, side / 2, &saved[1]); | 4180 | chroma[0].restore(x / 2, y / 2, side / 2, &saved[1]); |
| @@ -4333,6 +4521,7 @@ fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, tr | |||
| 4333 | tile_y0: 0, | 4521 | tile_y0: 0, |
| 4334 | tile_x1: width, | 4522 | tile_x1: width, |
| 4335 | tile_y1: height, | 4523 | tile_y1: height, |
| 4524 | + ctx: CoefCtxMap::default(), | ||
| 4336 | } | 4525 | } |
| 4337 | } | 4526 | } |
| 4338 | 4527 | ||
| @@ -4604,6 +4793,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4604 | tile_y0: 0, | 4793 | tile_y0: 0, |
| 4605 | tile_x1: picture.width, | 4794 | tile_x1: picture.width, |
| 4606 | tile_y1: picture.height, | 4795 | tile_y1: picture.height, |
| 4796 | + ctx: CoefCtxMap::default(), | ||
| 4607 | }; | 4797 | }; |
| 4608 | let mut chroma = [ | 4798 | let mut chroma = [ |
| 4609 | Plane { | 4799 | Plane { |
| @@ -4617,6 +4807,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4617 | tile_y0: 0, | 4807 | tile_y0: 0, |
| 4618 | tile_x1: picture.width / 2, | 4808 | tile_x1: picture.width / 2, |
| 4619 | tile_y1: picture.height / 2, | 4809 | tile_y1: picture.height / 2, |
| 4810 | + ctx: CoefCtxMap::default(), | ||
| 4620 | }, | 4811 | }, |
| 4621 | Plane { | 4812 | Plane { |
| 4622 | source: &picture_v8, | 4813 | source: &picture_v8, |
| @@ -4629,6 +4820,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 4629 | tile_y0: 0, | 4820 | tile_y0: 0, |
| 4630 | tile_x1: picture.width / 2, | 4821 | tile_x1: picture.width / 2, |
| 4631 | tile_y1: picture.height / 2, | 4822 | tile_y1: picture.height / 2, |
| 4823 | + ctx: CoefCtxMap::default(), | ||
| 4632 | }, | 4824 | }, |
| 4633 | ]; | 4825 | ]; |
| 4634 | 4826 | ||
| @@ -6447,6 +6639,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6447 | tile_y0: 0, | 6639 | tile_y0: 0, |
| 6448 | tile_x1: picture.width, | 6640 | tile_x1: picture.width, |
| 6449 | tile_y1: picture.height, | 6641 | tile_y1: picture.height, |
| 6642 | + ctx: CoefCtxMap::default(), | ||
| 6450 | }; | 6643 | }; |
| 6451 | let mut chroma = [ | 6644 | let mut chroma = [ |
| 6452 | Plane { | 6645 | Plane { |
| @@ -6460,6 +6653,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6460 | tile_y0: 0, | 6653 | tile_y0: 0, |
| 6461 | tile_x1: picture.width / 2, | 6654 | tile_x1: picture.width / 2, |
| 6462 | tile_y1: picture.height / 2, | 6655 | tile_y1: picture.height / 2, |
| 6656 | + ctx: CoefCtxMap::default(), | ||
| 6463 | }, | 6657 | }, |
| 6464 | Plane { | 6658 | Plane { |
| 6465 | source: &picture_v8, | 6659 | source: &picture_v8, |
| @@ -6472,6 +6666,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6472 | tile_y0: 0, | 6666 | tile_y0: 0, |
| 6473 | tile_x1: picture.width / 2, | 6667 | tile_x1: picture.width / 2, |
| 6474 | tile_y1: picture.height / 2, | 6668 | tile_y1: picture.height / 2, |
| 6669 | + ctx: CoefCtxMap::default(), | ||
| 6475 | }, | 6670 | }, |
| 6476 | ]; | 6671 | ]; |
| 6477 | 6672 | ||
| @@ -9739,7 +9934,7 @@ mod tests { | |||
| 9739 | }; | 9934 | }; |
| 9740 | let t = Instant::now(); | 9935 | let t = Instant::now(); |
| 9741 | for _ in 0..N { | 9936 | for _ in 0..N { |
| 9742 | - std::hint::black_box(crate::tile::coeff_bits(&levels, set, 2)); | 9937 | + std::hint::black_box(crate::tile::coeff_bits(&levels, set, 2, 0, 0)); |
| 9743 | } | 9938 | } |
| 9744 | let entropy_t = t.elapsed() / N; | 9939 | let entropy_t = t.elapsed() / N; |
| 9745 | 9940 | ||
crates/ec-av1/src/tile.rsmodified+50 -13
| @@ -3530,7 +3530,14 @@ fn write_block_planes( | |||
| 3530 | Some(plane), | 3530 | Some(plane), |
| 3531 | ); | 3531 | ); |
| 3532 | #[cfg(test)] | 3532 | #[cfg(test)] |
| 3533 | - census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before)); | 3533 | + census_record( |
| 3534 | + planes[plane], | ||
| 3535 | + q_ctx, | ||
| 3536 | + grid, | ||
| 3537 | + f64::from(enc.tell() - before), | ||
| 3538 | + skip_ctx, | ||
| 3539 | + dc_sign_ctx(around[plane].dc_vote), | ||
| 3540 | + ); | ||
| 3534 | ec_rng_trace(|| { | 3541 | ec_rng_trace(|| { |
| 3535 | format!( | 3542 | format!( |
| 3536 | "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}", | 3543 | "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}", |
| @@ -3625,7 +3632,14 @@ fn write_luma_tus( | |||
| 3625 | Some(0), | 3632 | Some(0), |
| 3626 | ); | 3633 | ); |
| 3627 | #[cfg(test)] | 3634 | #[cfg(test)] |
| 3628 | - census_record(set, q_ctx, &unit, f64::from(enc.tell() - before)); | 3635 | + census_record( |
| 3636 | + set, | ||
| 3637 | + q_ctx, | ||
| 3638 | + &unit, | ||
| 3639 | + f64::from(enc.tell() - before), | ||
| 3640 | + skip_ctx, | ||
| 3641 | + dc_sign_ctx(around.dc_vote), | ||
| 3642 | + ); | ||
| 3629 | if n > 1 { | 3643 | if n > 1 { |
| 3630 | neighbours.record_mi_luma(tu_mi, tx, &unit); | 3644 | neighbours.record_mi_luma(tu_mi, tx, &unit); |
| 3631 | } | 3645 | } |
| @@ -3906,9 +3920,9 @@ pub(crate) fn predicted_coeff_bits_sb(superblocks: &[Superblock], base_q_idx: u8 | |||
| 3906 | // top-left 32x32 carries coefficients) and each chroma plane at | 3920 | // top-left 32x32 carries coefficients) and each chroma plane at |
| 3907 | // 32x32; `predicted_coeff_bits` has no 64 arm, so price it here. | 3921 | // 32x32; `predicted_coeff_bits` has no 64 arm, so price it here. |
| 3908 | Superblock::Whole(block) if !block.skip => { | 3922 | Superblock::Whole(block) if !block.skip => { |
| 3909 | - coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx) | 3923 | + coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx, 0, 0) |
| 3910 | - + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx) | 3924 | + + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx, 0, 0) |
| 3911 | - + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx) | 3925 | + + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx, 0, 0) |
| 3912 | } | 3926 | } |
| 3913 | Superblock::Whole(_) => 0.0, | 3927 | Superblock::Whole(_) => 0.0, |
| 3914 | Superblock::Split(quadrants) => predicted_coeff_bits(quadrants, base_q_idx), | 3928 | Superblock::Split(quadrants) => predicted_coeff_bits(quadrants, base_q_idx), |
| @@ -3935,9 +3949,9 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { | |||
| 3935 | (_, false) => (TxbSet::Luma8, TxbSet::Chroma4), | 3949 | (_, false) => (TxbSet::Luma8, TxbSet::Chroma4), |
| 3936 | (_, true) => (TxbSet::Luma8Inter, TxbSet::Chroma4), | 3950 | (_, true) => (TxbSet::Luma8Inter, TxbSet::Chroma4), |
| 3937 | }; | 3951 | }; |
| 3938 | - coeff_bits(&dense(&block.luma, side), luma, q_ctx) | 3952 | + coeff_bits(&dense(&block.luma, side), luma, q_ctx, 0, 0) |
| 3939 | - + coeff_bits(&dense(&block.u, side / 2), chroma, q_ctx) | 3953 | + + coeff_bits(&dense(&block.u, side / 2), chroma, q_ctx, 0, 0) |
| 3940 | - + coeff_bits(&dense(&block.v, side / 2), chroma, q_ctx) | 3954 | + + coeff_bits(&dense(&block.v, side / 2), chroma, q_ctx, 0, 0) |
| 3941 | } | 3955 | } |
| 3942 | blocks | 3956 | blocks |
| 3943 | .iter() | 3957 | .iter() |
| @@ -3953,7 +3967,7 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { | |||
| 3953 | 3967 | ||
| 3954 | #[cfg(test)] | 3968 | #[cfg(test)] |
| 3955 | pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 { | 3969 | pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 { |
| 3956 | - coeff_bits(grid, TxbSet::Luma32, 2) | 3970 | + coeff_bits(grid, TxbSet::Luma32, 2, 0, 0) |
| 3957 | } | 3971 | } |
| 3958 | 3972 | ||
| 3959 | /// What one transform block's levels cost through the CDFs of `set`, in bits. | 3973 | /// What one transform block's levels cost through the CDFs of `set`, in bits. |
| @@ -3981,7 +3995,19 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 { | |||
| 3981 | /// are not yet decided; the map would have to be built per trial, not per | 3995 | /// are not yet decided; the map would have to be built per trial, not per |
| 3982 | /// block. Deferred with the numbers above rather than approximated by a | 3996 | /// block. Deferred with the numbers above rather than approximated by a |
| 3983 | /// fitted constant. | 3997 | /// fitted constant. |
| 3984 | -pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { | 3998 | +pub(crate) fn coeff_bits( |
| 3999 | + grid: &[i32], | ||
| 4000 | + set: TxbSet, | ||
| 4001 | + q_ctx: usize, | ||
| 4002 | + // lane-av1skipctx: the two neighbour-derived contexts, mirrored from the | ||
| 4003 | + // writer's own (`write_block_planes`/`write_luma_tus`) off the search's | ||
| 4004 | + // coding-order-committed map ([`crate::encode::CoefCtxMap`]). `(0, 0)` is | ||
| 4005 | + // what every caller passed before the map existed -- exact for a luma | ||
| 4006 | + // transform covering its whole block, and the census row that was the | ||
| 4007 | + // largest remaining pricer error for every other block. | ||
| 4008 | + skip_ctx: usize, | ||
| 4009 | + sign_ctx: usize, | ||
| 4010 | +) -> f64 { | ||
| 3985 | /// Built once per size: the search prices thirteen modes for every block, | 4011 | /// Built once per size: the search prices thirteen modes for every block, |
| 3986 | /// and the scan is the same table every time. | 4012 | /// and the scan is the same table every time. |
| 3987 | static SCANS: LazyLock<[Vec<u16>; 4]> = LazyLock::new(|| { | 4013 | static SCANS: LazyLock<[Vec<u16>; 4]> = LazyLock::new(|| { |
| @@ -4031,7 +4057,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { | |||
| 4031 | _ => &SCANS[3], | 4057 | _ => &SCANS[3], |
| 4032 | }; | 4058 | }; |
| 4033 | let mut enc = SymbolEncoder::pricer(); | 4059 | let mut enc = SymbolEncoder::pricer(); |
| 4034 | - write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); | 4060 | + write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None); |
| 4035 | enc.bits() | 4061 | enc.bits() |
| 4036 | }; | 4062 | }; |
| 4037 | PRICING_BASE.with_borrow_mut(|base| { | 4063 | PRICING_BASE.with_borrow_mut(|base| { |
| @@ -4182,7 +4208,18 @@ pub(crate) fn census_bucket_label(bucket: usize) -> &'static str { | |||
| 4182 | } | 4208 | } |
| 4183 | 4209 | ||
| 4184 | #[cfg(test)] | 4210 | #[cfg(test)] |
| 4185 | -fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) { | 4211 | +fn census_record( |
| 4212 | + set: TxbSet, | ||
| 4213 | + q_ctx: usize, | ||
| 4214 | + grid: &[i32], | ||
| 4215 | + written: f64, | ||
| 4216 | + // The writer's own neighbour contexts for this very transform block: the | ||
| 4217 | + // census prices the search's estimate the way the search now takes it | ||
| 4218 | + // (lane-av1skipctx), so a row's error is the table/adaptation drift that | ||
| 4219 | + // is left once the context is right. | ||
| 4220 | + skip_ctx: usize, | ||
| 4221 | + sign_ctx: usize, | ||
| 4222 | +) { | ||
| 4186 | if CENSUS.with_borrow(|c| c.is_none()) { | 4223 | if CENSUS.with_borrow(|c| c.is_none()) { |
| 4187 | return; | 4224 | return; |
| 4188 | } | 4225 | } |
| @@ -4196,7 +4233,7 @@ fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) { | |||
| 4196 | 9..=16 => 5, | 4233 | 9..=16 => 5, |
| 4197 | _ => 6, | 4234 | _ => 6, |
| 4198 | }; | 4235 | }; |
| 4199 | - let priced = coeff_bits(grid, set, q_ctx); | 4236 | + let priced = coeff_bits(grid, set, q_ctx, skip_ctx, sign_ctx); |
| 4200 | CENSUS.with_borrow_mut(|rows| { | 4237 | CENSUS.with_borrow_mut(|rows| { |
| 4201 | let rows = rows.as_mut().expect("the census is on"); | 4238 | let rows = rows.as_mut().expect("the census is on"); |
| 4202 | let row = rows.entry((format!("{set:?}"), bucket)).or_default(); | 4239 | let row = rows.entry((format!("{set:?}"), bucket)).or_default(); |