Every distortion score is over the inside of the frame only
Browse files at this commit62921b6a284b8536f23a1e1a71cb0ae4848e6765Parent: e08e2e6- fix
- every distortion score is over the inside of the frame only
2 changed files +232 -69
- crates/ec-av1/src/encode.rs +182 -69
- crates/ec-av1/src/encoder.rs +50 -0
crates/ec-av1/src/encode.rsmodified+182 -69
| @@ -541,7 +541,8 @@ pub fn take_b64_var_tx_hits() -> usize { | |||
| 541 | /// (lane-b64b). AV1 asks only that the block's own half is inside | 541 | /// (lane-b64b). AV1 asks only that the block's own half is inside |
| 542 | /// (`has_cols`/`has_rows`, spec `decode_partition`), and the writer already | 542 | /// (`has_cols`/`has_rows`, spec `decode_partition`), and the writer already |
| 543 | /// codes that case; what was missing was a trial whose SSE is scored over the | 543 | /// codes that case; what was missing was a trial whose SSE is scored over the |
| 544 | -/// INSIDE part alone ([`inside_sse`]) instead of over the padding the encoder | 544 | +/// INSIDE part alone ([`Plane::block_sse`], which clips since lane-sse) |
| 545 | +/// instead of over the padding the encoder | ||
| 545 | /// invented past the edge. Both gate films are superblock-aligned, so this | 546 | /// invented past the edge. Both gate films are superblock-aligned, so this |
| 546 | /// only reaches the user's own arbitrary export sizes. | 547 | /// only reaches the user's own arbitrary export sizes. |
| 547 | /// | 548 | /// |
| @@ -1694,6 +1695,58 @@ struct CoefCtxMap { | |||
| 1694 | tu_split: bool, | 1695 | tu_split: bool, |
| 1695 | } | 1696 | } |
| 1696 | 1697 | ||
| 1698 | +/// Whether [`Plane::block_sse`] scores a block the true frame edge cuts | ||
| 1699 | +/// through over its INSIDE part alone. On; `EC_AV1_EDGESSE=0` restores the | ||
| 1700 | +/// padding-inclusive score the search used before lane-sse, which is how the | ||
| 1701 | +/// before/after arms of the straddling gate rows are read off ONE binary. | ||
| 1702 | +fn edge_sse() -> bool { | ||
| 1703 | + static ENV: std::sync::LazyLock<Option<bool>> = | ||
| 1704 | + std::sync::LazyLock::new(|| crate::envflags::var("EC_AV1_EDGESSE").ok().map(|v| v != "0")); | ||
| 1705 | + ENV.unwrap_or(true) | ||
| 1706 | +} | ||
| 1707 | + | ||
| 1708 | +/// Whether [`Plane::block_sse`] counts the blocks the true frame edge cuts | ||
| 1709 | +/// through and the share of their raw squared error that sat in the padding | ||
| 1710 | +/// (`EC_AV1_EDGE_CENSUS=1`). Off by default: only edge blocks would pay for | ||
| 1711 | +/// it, but the numbers are an instrument, not a decision (class | ||
| 1712 | +/// `gate-blind-to-feature` -- both gate films are superblock-aligned, so | ||
| 1713 | +/// without this census no gate row can say the clip fires at all). | ||
| 1714 | +fn edge_census() -> bool { | ||
| 1715 | + static ENV: std::sync::LazyLock<bool> = | ||
| 1716 | + std::sync::LazyLock::new(|| crate::envflags::var("EC_AV1_EDGE_CENSUS").ok().as_deref() == Some("1")); | ||
| 1717 | + *ENV | ||
| 1718 | +} | ||
| 1719 | + | ||
| 1720 | +/// Per block side (indexed by `side.trailing_zeros()`, so 2 is 4x4 and 7 is | ||
| 1721 | +/// 128x128): how many scores clipped, the inside squared error they kept and | ||
| 1722 | +/// the padding squared error they dropped. | ||
| 1723 | +static EDGE_SCORES: [[std::sync::atomic::AtomicU64; 3]; 8] = | ||
| 1724 | + [const { [const { std::sync::atomic::AtomicU64::new(0) }; 3] }; 8]; | ||
| 1725 | + | ||
| 1726 | +/// One clipped score, for [`take_edge_census`]. | ||
| 1727 | +fn record_edge_score(side: usize, inside: u64, padding: u64) { | ||
| 1728 | + let row = &EDGE_SCORES[(side.trailing_zeros() as usize).min(7)]; | ||
| 1729 | + row[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 1730 | + row[1].fetch_add(inside, std::sync::atomic::Ordering::Relaxed); | ||
| 1731 | + row[2].fetch_add(padding, std::sync::atomic::Ordering::Relaxed); | ||
| 1732 | +} | ||
| 1733 | + | ||
| 1734 | +/// The straddling-block census since the last call, and zero it: one | ||
| 1735 | +/// `(side, scores, inside_sse, padding_sse)` row per block size that fired. | ||
| 1736 | +#[must_use] | ||
| 1737 | +pub fn take_edge_census() -> Vec<(usize, u64, u64, u64)> { | ||
| 1738 | + EDGE_SCORES | ||
| 1739 | + .iter() | ||
| 1740 | + .enumerate() | ||
| 1741 | + .filter_map(|(log2, row)| { | ||
| 1742 | + let n = row[0].swap(0, std::sync::atomic::Ordering::Relaxed); | ||
| 1743 | + let inside = row[1].swap(0, std::sync::atomic::Ordering::Relaxed); | ||
| 1744 | + let padding = row[2].swap(0, std::sync::atomic::Ordering::Relaxed); | ||
| 1745 | + (n != 0).then_some((1usize << log2, n, inside, padding)) | ||
| 1746 | + }) | ||
| 1747 | + .collect() | ||
| 1748 | +} | ||
| 1749 | + | ||
| 1697 | struct Plane<'a> { | 1750 | struct Plane<'a> { |
| 1698 | source: &'a [u8], | 1751 | source: &'a [u8], |
| 1699 | reconstruction: Vec<u8>, | 1752 | reconstruction: Vec<u8>, |
| @@ -1850,17 +1903,54 @@ impl Plane<'_> { | |||
| 1850 | } | 1903 | } |
| 1851 | 1904 | ||
| 1852 | /// Squared error between the source block at `(x, y)` and a reconstruction | 1905 | /// Squared error between the source block at `(x, y)` and a reconstruction |
| 1853 | - /// of it. Exact for the same reason [`Self::block_sad`] is: the largest | 1906 | + /// of it, over the part of the block that is INSIDE the true frame. |
| 1854 | - /// total is `255^2 * 64 * 64`, under 2^28. | 1907 | + /// Exact for the same reason [`Self::block_sad`] is: the largest total is |
| 1908 | + /// `255^2 * 64 * 64`, under 2^28. | ||
| 1909 | + /// | ||
| 1910 | + /// A block the frame edge cuts through is still coded whole -- the | ||
| 1911 | + /// decoder reconstructs its outside samples too and the display crop | ||
| 1912 | + /// throws them away -- so scoring the padding would rank a mode, a | ||
| 1913 | + /// transform or a partition tree by how well it codes samples the source | ||
| 1914 | + /// does not have (libaom clips the same way: `av1_dist_block` measures | ||
| 1915 | + /// over `max_blocks_wide`/`max_blocks_high`). The padding is replicated | ||
| 1916 | + /// edge pixels, so predicting it is nearly free, but coding coefficients | ||
| 1917 | + /// for it spends real bits. Both gate films are superblock-aligned; this | ||
| 1918 | + /// only reaches arbitrary export sizes (lane-sse, generalising | ||
| 1919 | + /// lane-b64b's 64-root-only rescore of the 64 root). | ||
| 1855 | fn block_sse(&self, x: usize, y: usize, side: usize, reconstruction: &[u8]) -> f64 { | 1920 | fn block_sse(&self, x: usize, y: usize, side: usize, reconstruction: &[u8]) -> f64 { |
| 1921 | + let inside_rows = side.min(self.true_height.saturating_sub(y)); | ||
| 1922 | + let inside_cols = side.min(self.true_width.saturating_sub(x)); | ||
| 1923 | + let straddles = inside_rows < side || inside_cols < side; | ||
| 1924 | + // The knob is read only for a block the edge cuts, so the interior | ||
| 1925 | + // path -- the hot one -- carries no extra load at all. | ||
| 1926 | + let (rows, cols) = match straddles && !edge_sse() { | ||
| 1927 | + true => (side, side), | ||
| 1928 | + false => (inside_rows, inside_cols), | ||
| 1929 | + }; | ||
| 1856 | let mut sse = 0u64; | 1930 | let mut sse = 0u64; |
| 1857 | - for row in 0..side { | 1931 | + for row in 0..rows { |
| 1858 | - let source = &self.source[(y + row) * self.width + x..][..side]; | 1932 | + let source = &self.source[(y + row) * self.width + x..][..cols]; |
| 1859 | - for (&s, &r) in source.iter().zip(&reconstruction[row * side..][..side]) { | 1933 | + for (&s, &r) in source.iter().zip(&reconstruction[row * side..][..cols]) { |
| 1860 | let d = u32::from(s.abs_diff(r)); | 1934 | let d = u32::from(s.abs_diff(r)); |
| 1861 | sse += u64::from(d * d); | 1935 | sse += u64::from(d * d); |
| 1862 | } | 1936 | } |
| 1863 | } | 1937 | } |
| 1938 | + if straddles && edge_census() { | ||
| 1939 | + let (mut inside, mut whole) = (0u64, 0u64); | ||
| 1940 | + for row in 0..side { | ||
| 1941 | + let source = &self.source[(y + row) * self.width + x..][..side]; | ||
| 1942 | + for (col, (&s, &r)) in | ||
| 1943 | + source.iter().zip(&reconstruction[row * side..][..side]).enumerate() | ||
| 1944 | + { | ||
| 1945 | + let d = u64::from(u32::from(s.abs_diff(r)).pow(2)); | ||
| 1946 | + whole += d; | ||
| 1947 | + if row < inside_rows && col < inside_cols { | ||
| 1948 | + inside += d; | ||
| 1949 | + } | ||
| 1950 | + } | ||
| 1951 | + } | ||
| 1952 | + record_edge_score(side, inside, whole - inside); | ||
| 1953 | + } | ||
| 1864 | sse as f64 | 1954 | sse as f64 |
| 1865 | } | 1955 | } |
| 1866 | 1956 | ||
| @@ -3964,10 +4054,19 @@ fn ibc_sse( | |||
| 3964 | (sx, sy): (usize, usize), | 4054 | (sx, sy): (usize, usize), |
| 3965 | side: usize, | 4055 | side: usize, |
| 3966 | ) -> f64 { | 4056 | ) -> f64 { |
| 4057 | + // Inside the true frame only, for the reason [`Plane::block_sse`] is | ||
| 4058 | + // (lane-sse): an intrabc block at the edge would otherwise be ranked by | ||
| 4059 | + // how well it copies the padding. | ||
| 3967 | let mut sse = 0.0f64; | 4060 | let mut sse = 0.0f64; |
| 3968 | - for row in 0..side { | 4061 | + let clip = |whole: usize, inside: usize| match edge_sse() { |
| 3969 | - let src = &luma.source[(y + row) * luma.width + x..][..side]; | 4062 | + true => whole.min(inside), |
| 3970 | - let rec = &luma.reconstruction[(sy + row) * luma.width + sx..][..side]; | 4063 | + false => whole, |
| 4064 | + }; | ||
| 4065 | + let rows = clip(side, luma.true_height.saturating_sub(y)); | ||
| 4066 | + let cols = clip(side, luma.true_width.saturating_sub(x)); | ||
| 4067 | + for row in 0..rows { | ||
| 4068 | + let src = &luma.source[(y + row) * luma.width + x..][..cols]; | ||
| 4069 | + let rec = &luma.reconstruction[(sy + row) * luma.width + sx..][..cols]; | ||
| 3971 | for (&s, &r) in src.iter().zip(rec) { | 4070 | for (&s, &r) in src.iter().zip(rec) { |
| 3972 | let d = f64::from(i32::from(s) - i32::from(r)); | 4071 | let d = f64::from(i32::from(s) - i32::from(r)); |
| 3973 | sse += d * d; | 4072 | sse += d * d; |
| @@ -3975,9 +4074,11 @@ fn ibc_sse( | |||
| 3975 | } | 4074 | } |
| 3976 | let half = side / 2; | 4075 | let half = side / 2; |
| 3977 | for plane in chroma { | 4076 | for plane in chroma { |
| 3978 | - for row in 0..half { | 4077 | + let rows = clip(half, plane.true_height.saturating_sub(y / 2)); |
| 3979 | - let src = &plane.source[(y / 2 + row) * plane.width + x / 2..][..half]; | 4078 | + let cols = clip(half, plane.true_width.saturating_sub(x / 2)); |
| 3980 | - let rec = &plane.reconstruction[(sy / 2 + row) * plane.width + sx / 2..][..half]; | 4079 | + for row in 0..rows { |
| 4080 | + let src = &plane.source[(y / 2 + row) * plane.width + x / 2..][..cols]; | ||
| 4081 | + let rec = &plane.reconstruction[(sy / 2 + row) * plane.width + sx / 2..][..cols]; | ||
| 3981 | for (&s, &r) in src.iter().zip(rec) { | 4082 | for (&s, &r) in src.iter().zip(rec) { |
| 3982 | let d = f64::from(i32::from(s) - i32::from(r)); | 4083 | let d = f64::from(i32::from(s) - i32::from(r)); |
| 3983 | sse += d * d; | 4084 | sse += d * d; |
| @@ -6028,46 +6129,6 @@ where | |||
| 6028 | /// of the frame's reconstruction because a tile's prediction never reads a | 6129 | /// of the frame's reconstruction because a tile's prediction never reads a |
| 6029 | /// sample outside its own rectangle, and every sample inside it is written | 6130 | /// sample outside its own rectangle, and every sample inside it is written |
| 6030 | /// before it is read. | 6131 | /// before it is read. |
| 6031 | -/// [`Plane::block_sse`] over the part of a block that is INSIDE the true | ||
| 6032 | -/// frame. A block the edge cuts through is still coded whole -- the decoder | ||
| 6033 | -/// reconstructs its outside samples too and the display crop throws them away | ||
| 6034 | -/// -- so scoring them would rank a 64x64 root by how well it predicts padding | ||
| 6035 | -/// the source does not have (lane-b64b). | ||
| 6036 | -fn inside_sse(plane: &Plane, x: usize, y: usize, side: usize, reconstruction: &[u8]) -> f64 { | ||
| 6037 | - let rows = side.min(plane.true_height.saturating_sub(y)); | ||
| 6038 | - let cols = side.min(plane.true_width.saturating_sub(x)); | ||
| 6039 | - let mut sse = 0u64; | ||
| 6040 | - for row in 0..rows { | ||
| 6041 | - let source = &plane.source[(y + row) * plane.width + x..][..cols]; | ||
| 6042 | - for (col, &s) in source.iter().enumerate() { | ||
| 6043 | - let d = u32::from(s.abs_diff(reconstruction[row * side + col])); | ||
| 6044 | - sse += u64::from(d * d); | ||
| 6045 | - } | ||
| 6046 | - } | ||
| 6047 | - sse as f64 | ||
| 6048 | -} | ||
| 6049 | - | ||
| 6050 | -/// Rescores one 64x64 root trial set (luma and the two chroma planes) over | ||
| 6051 | -/// the inside part of a superblock the frame edge cuts through. | ||
| 6052 | -fn clip_sb64_sse( | ||
| 6053 | - trials: &mut [Trial; 3], | ||
| 6054 | - luma: &Plane, | ||
| 6055 | - chroma: &[Plane; 2], | ||
| 6056 | - (x, y): (usize, usize), | ||
| 6057 | - side: usize, | ||
| 6058 | -) { | ||
| 6059 | - trials[0].sse = inside_sse(luma, x, y, side, &trials[0].reconstruction); | ||
| 6060 | - for plane in 0..2 { | ||
| 6061 | - trials[plane + 1].sse = inside_sse( | ||
| 6062 | - &chroma[plane], | ||
| 6063 | - x / 2, | ||
| 6064 | - y / 2, | ||
| 6065 | - side / 2, | ||
| 6066 | - &trials[plane + 1].reconstruction, | ||
| 6067 | - ); | ||
| 6068 | - } | ||
| 6069 | -} | ||
| 6070 | - | ||
| 6071 | fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { | 6132 | fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, true_height: usize) -> Plane<'_> { |
| 6072 | Plane { | 6133 | Plane { |
| 6073 | source, | 6134 | source, |
| @@ -7589,12 +7650,9 @@ fn search_skip_64( | |||
| 7589 | } | 7650 | } |
| 7590 | } | 7651 | } |
| 7591 | 7652 | ||
| 7592 | - // A superblock the true frame edge cuts through: every trial below is | ||
| 7593 | - // ranked over its inside part only ([`clip_sb64_sse`]). | ||
| 7594 | - let edge = x + side > luma.true_width || y + side > luma.true_height; | ||
| 7595 | let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; | 7653 | let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; |
| 7596 | for (bits, info) in cands { | 7654 | for (bits, info) in cands { |
| 7597 | - let mut trials = [ | 7655 | + let trials = [ |
| 7598 | (0usize, x, y, side, true), | 7656 | (0usize, x, y, side, true), |
| 7599 | (1, x / 2, y / 2, side / 2, false), | 7657 | (1, x / 2, y / 2, side / 2, false), |
| 7600 | (2, x / 2, y / 2, side / 2, false), | 7658 | (2, x / 2, y / 2, side / 2, false), |
| @@ -7631,9 +7689,6 @@ fn search_skip_64( | |||
| 7631 | TxbSet::Luma32Inter, | 7689 | TxbSet::Luma32Inter, |
| 7632 | ) | 7690 | ) |
| 7633 | }); | 7691 | }); |
| 7634 | - if edge { | ||
| 7635 | - clip_sb64_sse(&mut trials, luma, chroma, (x, y), side); | ||
| 7636 | - } | ||
| 7637 | let cost = trials[0].sse | 7692 | let cost = trials[0].sse |
| 7638 | + trials[1].sse | 7693 | + trials[1].sse |
| 7639 | + trials[2].sse | 7694 | + trials[2].sse |
| @@ -7706,7 +7761,7 @@ fn search_skip_64( | |||
| 7706 | ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | 7761 | ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); |
| 7707 | ccands.dedup_by_key(|c| c.0); | 7762 | ccands.dedup_by_key(|c| c.0); |
| 7708 | for (mvs, bits, info) in ccands { | 7763 | for (mvs, bits, info) in ccands { |
| 7709 | - let mut trials = [ | 7764 | + let trials = [ |
| 7710 | (0usize, x, y, side, true), | 7765 | (0usize, x, y, side, true), |
| 7711 | (1, x / 2, y / 2, side / 2, false), | 7766 | (1, x / 2, y / 2, side / 2, false), |
| 7712 | (2, x / 2, y / 2, side / 2, false), | 7767 | (2, x / 2, y / 2, side / 2, false), |
| @@ -7750,9 +7805,6 @@ fn search_skip_64( | |||
| 7750 | TxbSet::Luma32Inter, | 7805 | TxbSet::Luma32Inter, |
| 7751 | ) | 7806 | ) |
| 7752 | }); | 7807 | }); |
| 7753 | - if edge { | ||
| 7754 | - clip_sb64_sse(&mut trials, luma, chroma, (x, y), side); | ||
| 7755 | - } | ||
| 7756 | let syntax = fixed - ref_bits + bits; | 7808 | let syntax = fixed - ref_bits + bits; |
| 7757 | let cost = | 7809 | let cost = |
| 7758 | trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax; | 7810 | trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax; |
| @@ -7785,7 +7837,7 @@ fn search_skip_64( | |||
| 7785 | let mut luma_levels: Option<(Vec<i32>, u8)> = None; | 7837 | let mut luma_levels: Option<(Vec<i32>, u8)> = None; |
| 7786 | if residual { | 7838 | if residual { |
| 7787 | let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; | 7839 | let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; |
| 7788 | - let mut residual_trials = [0usize, 1, 2].map(|plane| { | 7840 | + let residual_trials = [0usize, 1, 2].map(|plane| { |
| 7789 | let (target, px, py, s) = if plane == 0 { | 7841 | let (target, px, py, s) = if plane == 0 { |
| 7790 | (&*luma, x, y, side) | 7842 | (&*luma, x, y, side) |
| 7791 | } else { | 7843 | } else { |
| @@ -7802,9 +7854,6 @@ fn search_skip_64( | |||
| 7802 | sets[plane], | 7854 | sets[plane], |
| 7803 | ) | 7855 | ) |
| 7804 | }); | 7856 | }); |
| 7805 | - if edge { | ||
| 7806 | - clip_sb64_sse(&mut residual_trials, luma, chroma, (x, y), side); | ||
| 7807 | - } | ||
| 7808 | let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); | 7857 | let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); |
| 7809 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); | 7858 | let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); |
| 7810 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the | 7859 | // lane-b64b: the root's own var-tx decision -- one TX_64X64 or the |
| @@ -9532,7 +9581,7 @@ pub(crate) fn encode_inter_frame( | |||
| 9532 | // frame is offered it, and (lane-b64b, `EC_AV1_B64EDGE`) one the | 9581 | // frame is offered it, and (lane-b64b, `EC_AV1_B64EDGE`) one the |
| 9533 | // edge CUTS THROUGH: the spec's own `has_cols`/`has_rows` ask | 9582 | // edge CUTS THROUGH: the spec's own `has_cols`/`has_rows` ask |
| 9534 | // only that the block's half is inside, and the trial is ranked | 9583 | // only that the block's half is inside, and the trial is ranked |
| 9535 | - // over the inside part alone ([`clip_sb64_sse`]). | 9584 | + // over the inside part alone ([`Plane::block_sse`]). |
| 9536 | let (x64, y64) = (sb_c * SUPERBLOCK, sb_r * SUPERBLOCK); | 9585 | let (x64, y64) = (sb_c * SUPERBLOCK, sb_r * SUPERBLOCK); |
| 9537 | let whole_inside = (sb_c as u32 + 1) * crate::tile::SB_MI <= header.mi_cols | 9586 | let whole_inside = (sb_c as u32 + 1) * crate::tile::SB_MI <= header.mi_cols |
| 9538 | && (sb_r as u32 + 1) * crate::tile::SB_MI <= header.mi_rows | 9587 | && (sb_r as u32 + 1) * crate::tile::SB_MI <= header.mi_rows |
| @@ -10502,6 +10551,31 @@ mod tests { | |||
| 10502 | /// by predicting -- the textured half of a frame whose other half is | 10551 | /// by predicting -- the textured half of a frame whose other half is |
| 10503 | /// flat -- the factor FALLS below 1, i.e. that half is coded more | 10552 | /// flat -- the factor FALLS below 1, i.e. that half is coded more |
| 10504 | /// distortion-averse than the half nothing gains from. | 10553 | /// distortion-averse than the half nothing gains from. |
| 10554 | + /// The distortion metric itself (lane-sse): a block the true frame edge | ||
| 10555 | + /// cuts through is scored over its INSIDE part only, so a difference that | ||
| 10556 | + /// lives entirely in the padding costs nothing and a block wholly past | ||
| 10557 | + /// the edge scores zero. Before this, the search ranked modes, | ||
| 10558 | + /// transforms and partition trees on samples the display crop throws | ||
| 10559 | + /// away -- which lane-b64b had fixed for the 64x64 root alone. | ||
| 10560 | + #[test] | ||
| 10561 | + fn a_block_the_frame_edge_cuts_is_scored_over_its_inside_only() { | ||
| 10562 | + // A 40x40 true frame on a 64x64 padded coding surface. | ||
| 10563 | + let source = vec![10u8; 64 * 64]; | ||
| 10564 | + let plane = fresh_plane(&source, 64, 64, 40, 40); | ||
| 10565 | + let mut reconstruction = vec![10u8; 64 * 64]; | ||
| 10566 | + // Off by 190 at (50, 50) -- past the true edge in both axes. | ||
| 10567 | + reconstruction[50 * 64 + 50] = 200; | ||
| 10568 | + assert_eq!(plane.block_sse(0, 0, 64, &reconstruction), 0.0, "padding was scored"); | ||
| 10569 | + // Off by 10 at (39, 39) -- the last sample the decoder shows. | ||
| 10570 | + reconstruction[39 * 64 + 39] = 20; | ||
| 10571 | + assert_eq!(plane.block_sse(0, 0, 64, &reconstruction), 100.0, "inside was not scored"); | ||
| 10572 | + // A 16x16 block whose every sample is padding. | ||
| 10573 | + let block = vec![200u8; 16 * 16]; | ||
| 10574 | + assert_eq!(plane.block_sse(48, 0, 16, &block), 0.0, "a block past the edge scored"); | ||
| 10575 | + // A 16x16 block the edge cuts: 8 of its columns are inside. | ||
| 10576 | + assert_eq!(plane.block_sse(32, 32, 16, &block), (190.0 * 190.0) * 64.0); | ||
| 10577 | + } | ||
| 10578 | + | ||
| 10505 | #[test] | 10579 | #[test] |
| 10506 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { | 10580 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { |
| 10507 | // Four 64x64 superblocks across: the shipped map forms its ratio per | 10581 | // Four 64x64 superblocks across: the shipped map forms its ratio per |
| @@ -14943,6 +15017,27 @@ mod tests { | |||
| 14943 | crate::probe::dims(clip) | 15017 | crate::probe::dims(clip) |
| 14944 | } | 15018 | } |
| 14945 | 15019 | ||
| 15020 | + /// `EC_AV1_NATIVE_CROP=<width>x<height>`: the native gate's window at a | ||
| 15021 | + /// size of the reader's choosing instead of [`crate::probe::gate_crop`]'s | ||
| 15022 | + /// superblock-aligned one, still centred and still even for 4:2:0. | ||
| 15023 | + /// | ||
| 15024 | + /// WHY IT EXISTS (lane-sse): `gate_crop` rounds DOWN to whole 128-wide | ||
| 15025 | + /// superblocks by design, so no default gate row has a block the frame | ||
| 15026 | + /// edge cuts through -- while every arbitrary export size does (3840x1608 | ||
| 15027 | + /// is 25.1 superblock rows, 1920x1080 is 8.4). `EC_AV1_NATIVE_CROP= | ||
| 15028 | + /// 1920x1080` is the straddling arm the edge-clipped distortion score is | ||
| 15029 | + /// read on. | ||
| 15030 | + fn native_crop_override(clip: &str) -> Option<(String, usize, usize)> { | ||
| 15031 | + let spec = std::env::var("EC_AV1_NATIVE_CROP").ok()?; | ||
| 15032 | + let (w, h) = spec.split_once('x').expect("EC_AV1_NATIVE_CROP is <width>x<height>"); | ||
| 15033 | + let (cw, ch): (usize, usize) = | ||
| 15034 | + (w.parse().expect("crop width"), h.parse().expect("crop height")); | ||
| 15035 | + let (nw, nh) = crate::probe::dims(clip)?; | ||
| 15036 | + assert!(cw <= nw && ch <= nh, "{cw}x{ch} does not fit in {nw}x{nh}"); | ||
| 15037 | + let (x, y) = ((nw - cw) / 2 & !1, (nh - ch) / 2 & !1); | ||
| 15038 | + Some((format!("crop={cw}:{ch}:{x}:{y}"), cw, ch)) | ||
| 15039 | + } | ||
| 15040 | + | ||
| 14946 | /// The rows of [`bd_rate_screen_native`] -- (label, path, seek) -- shared | 15041 | /// The rows of [`bd_rate_screen_native`] -- (label, path, seek) -- shared |
| 14947 | /// with `probe_screen_detect` so the detector sweep measures exactly the | 15042 | /// with `probe_screen_detect` so the detector sweep measures exactly the |
| 14948 | /// clips the keep table is read off. Selector env vars as documented on | 15043 | /// clips the keep table is read off. Selector env vars as documented on |
| @@ -15210,7 +15305,10 @@ mod tests { | |||
| 15210 | // of the middle of the frame; the offsets stay even for 4:2:0. | 15305 | // of the middle of the frame; the offsets stay even for 4:2:0. |
| 15211 | // ONE definition, shared with `examples/enc_probe` so the probe | 15306 | // ONE definition, shared with `examples/enc_probe` so the probe |
| 15212 | // cannot measure a different window than this gate (lane-probe). | 15307 | // cannot measure a different window than this gate (lane-probe). |
| 15213 | - let (vf, cw, ch) = crate::probe::gate_crop(path); | 15308 | + let (vf, cw, ch) = match native_crop_override(path) { |
| 15309 | + Some(over) => over, | ||
| 15310 | + None => crate::probe::gate_crop(path), | ||
| 15311 | + }; | ||
| 15214 | let source = clip_frames_vf(path, seek, &vf, cw, ch, frames); | 15312 | let source = clip_frames_vf(path, seek, &vf, cw, ch, frames); |
| 15215 | SCREEN_FRAMES.iter().for_each(|c| { | 15313 | SCREEN_FRAMES.iter().for_each(|c| { |
| 15216 | c.store(0, std::sync::atomic::Ordering::Relaxed); | 15314 | c.store(0, std::sync::atomic::Ordering::Relaxed); |
| @@ -15220,10 +15318,25 @@ mod tests { | |||
| 15220 | let _ = crate::tile::take_intrabc_hits(); | 15318 | let _ = crate::tile::take_intrabc_hits(); |
| 15221 | let _ = take_intrabc_search(); | 15319 | let _ = take_intrabc_search(); |
| 15222 | let _ = crate::tile::take_filter_intra_hits(); | 15320 | let _ = crate::tile::take_filter_intra_hits(); |
| 15321 | + let _ = take_edge_census(); | ||
| 15223 | // The sequence path itself codes the pyramid now | 15322 | // The sequence path itself codes the pyramid now |
| 15224 | // (lane-av1pyrdef), and prints what it requested and what the | 15323 | // (lane-av1pyrdef), and prints what it requested and what the |
| 15225 | // content gate left effective. | 15324 | // content gate left effective. |
| 15226 | let (ours, ours_wall) = our_ladder(name, &source, cw, ch, fctx); | 15325 | let (ours, ours_wall) = our_ladder(name, &source, cw, ch, fctx); |
| 15326 | + // lane-sse: the blocks the true frame edge cuts through, and how | ||
| 15327 | + // much of their RAW squared error sat in the padding the encoder | ||
| 15328 | + // invented past it -- what the inside-only score stopped ranking | ||
| 15329 | + // on. Empty on a superblock-aligned crop, which is every default | ||
| 15330 | + // gate row (class `gate-blind-to-feature`). | ||
| 15331 | + if edge_census() { | ||
| 15332 | + for (side, n, inside, padding) in take_edge_census() { | ||
| 15333 | + eprintln!( | ||
| 15334 | + "{name}: edge {side}x{side} scores={n} inside_sse={inside} \ | ||
| 15335 | + padding_sse={padding} ({:.1}% of raw)", | ||
| 15336 | + 100.0 * padding as f64 / (inside + padding).max(1) as f64, | ||
| 15337 | + ); | ||
| 15338 | + } | ||
| 15339 | + } | ||
| 15227 | // gate-blind-to-feature: what the screen tools actually did at | 15340 | // gate-blind-to-feature: what the screen tools actually did at |
| 15228 | // native resolution, over this clip's four encodes. | 15341 | // native resolution, over this clip's four encodes. |
| 15229 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); | 15342 | let screen_on = SCREEN_FRAMES[1].load(std::sync::atomic::Ordering::Relaxed); |
crates/ec-av1/src/encoder.rsmodified+50 -0
| @@ -3204,6 +3204,56 @@ mod tests { | |||
| 3204 | } | 3204 | } |
| 3205 | } | 3205 | } |
| 3206 | 3206 | ||
| 3207 | + /// The 1080p SHAPE at a fifth of its size (lane-sse): 384x216 is three | ||
| 3208 | + /// whole superblock columns and 1.6875 superblock rows, so the bottom | ||
| 3209 | + /// 88-pixel band straddles at EVERY level -- the 64x64 root lane-b64b | ||
| 3210 | + /// already clipped, and the 32x32 (216 = 6*32 + 24) and 16x16 (13*16 + 8) | ||
| 3211 | + /// leaves it did not. Every one of them is now scored over its inside | ||
| 3212 | + /// part alone, and a distortion change that moved a partition or a | ||
| 3213 | + /// transform decision the writer did not follow would desync the tile, so | ||
| 3214 | + /// both decoders reconstruct it sample-exact. | ||
| 3215 | + #[test] | ||
| 3216 | + fn a_1080p_shaped_clip_straddles_at_every_block_size_and_decodes_sample_exact() { | ||
| 3217 | + let _knobs = crate::speed::knob_write(); | ||
| 3218 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 3219 | + let (width, height) = (384usize, 216usize); | ||
| 3220 | + let sources: Vec<Picture> = (0..3).map(|i| test_card(width, height, i)).collect(); | ||
| 3221 | + let config = EncoderConfig { | ||
| 3222 | + width, | ||
| 3223 | + height, | ||
| 3224 | + base_q_idx: 120, | ||
| 3225 | + gop: 8, | ||
| 3226 | + colour: Colour::Bt709Limited, | ||
| 3227 | + tile_cols_log2: 0, | ||
| 3228 | + tile_rows_log2: 0, | ||
| 3229 | + }; | ||
| 3230 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 3231 | + let mut stream = Vec::new(); | ||
| 3232 | + for packet in encode_all(&mut enc, &sources) { | ||
| 3233 | + stream.extend_from_slice(&packet.data); | ||
| 3234 | + } | ||
| 3235 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 3236 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | ||
| 3237 | + if !have_ffmpeg() { | ||
| 3238 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 3239 | + return; | ||
| 3240 | + } | ||
| 3241 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 3242 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | ||
| 3243 | + for (i, (ours_i, theirs_i)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3244 | + let got: Vec<u8> = ours_i.y.iter().map(|&v| v as u8).collect(); | ||
| 3245 | + if let Some(at) = got.iter().zip(theirs_i).position(|(x, y)| x != y) { | ||
| 3246 | + panic!( | ||
| 3247 | + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 3248 | + at % width, | ||
| 3249 | + at / width, | ||
| 3250 | + got[at], | ||
| 3251 | + theirs_i[at], | ||
| 3252 | + ); | ||
| 3253 | + } | ||
| 3254 | + } | ||
| 3255 | + } | ||
| 3256 | + | ||
| 3207 | /// A clip whose inter residual is HIGH-FREQUENCY (lane-b64b): every | 3257 | /// A clip whose inter residual is HIGH-FREQUENCY (lane-b64b): every |
| 3208 | /// frame adds a 4-pixel checkerboard, so the 64x64 root's prediction is | 3258 | /// frame adds a 4-pixel checkerboard, so the 64x64 root's prediction is |
| 3209 | /// off by exactly the detail a TX_64X64 throws away (it codes only the | 3259 | /// off by exactly the detail a TX_64X64 throws away (it codes only the |