Merge branch 'lane-fitu' (5a90cf79): per-transform-unit filter intra in the tx-depth search, armed on screen frames; film rows byte-identical, screen +49.2/-16.0 -> +48.2/-16.4
Browse files at this commit1 changed file +87 -48
- crates/ec-av1/src/encode.rs +87 -48
crates/ec-av1/src/encode.rsmodified+87 -48
| @@ -1868,12 +1868,19 @@ impl Plane<'_> { | |||
| 1868 | /// reconstructed on its own. Commits the result and hands back the | 1868 | /// reconstructed on its own. Commits the result and hands back the |
| 1869 | /// block-coordinate levels, the block's squared error and what its | 1869 | /// block-coordinate levels, the block's squared error and what its |
| 1870 | /// coefficients cost. | 1870 | /// coefficients cost. |
| 1871 | + #[allow(clippy::too_many_arguments)] | ||
| 1871 | fn code_tx_depth( | 1872 | fn code_tx_depth( |
| 1872 | &mut self, | 1873 | &mut self, |
| 1873 | at: At, | 1874 | at: At, |
| 1874 | mode: u8, | 1875 | mode: u8, |
| 1875 | angle_delta: i32, | 1876 | angle_delta: i32, |
| 1876 | depth: usize, | 1877 | depth: usize, |
| 1878 | + // This block's `filter_intra_mode` when it is coded with one, in | ||
| 1879 | + // which case every transform unit predicts recursively off the | ||
| 1880 | + // reconstruction the units before it left -- what the decoder does | ||
| 1881 | + // per unit (decode.rs `decode_block`, `push_intra_rect`'s | ||
| 1882 | + // `filter_intra`), not once over the whole block. | ||
| 1883 | + filter_intra: Option<u8>, | ||
| 1877 | search: &Search, fctx: &crate::decode::FrameCtx, | 1884 | search: &Search, fctx: &crate::decode::FrameCtx, |
| 1878 | ) -> (Vec<i32>, f64, f64) { | 1885 | ) -> (Vec<i32>, f64, f64) { |
| 1879 | let At { x, y, side, reach, .. } = at; | 1886 | let At { x, y, side, reach, .. } = at; |
| @@ -1901,7 +1908,36 @@ impl Plane<'_> { | |||
| 1901 | reach: Reach::of_tu(side, side, col_off, row_off, tx, tx, reach), | 1908 | reach: Reach::of_tu(side, side, col_off, row_off, tx, tx, reach), |
| 1902 | set, | 1909 | set, |
| 1903 | }; | 1910 | }; |
| 1904 | - let trial = self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx); | 1911 | + let trial = match filter_intra { |
| 1912 | + Some(fi) => { | ||
| 1913 | + let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | ||
| 1914 | + let (above, left, corner) = | ||
| 1915 | + self.edges_into(tu.x, tu.y, tx, tu.reach, &mut above_buf, &mut left_buf); | ||
| 1916 | + let mut prediction = vec![0u8; tx * tx]; | ||
| 1917 | + filter_intra_predict_u8( | ||
| 1918 | + usize::from(fi), | ||
| 1919 | + above, | ||
| 1920 | + left, | ||
| 1921 | + corner, | ||
| 1922 | + tx, | ||
| 1923 | + &mut prediction, | ||
| 1924 | + fctx, | ||
| 1925 | + ); | ||
| 1926 | + self.code_from_prediction( | ||
| 1927 | + tu.x, | ||
| 1928 | + tu.y, | ||
| 1929 | + tx, | ||
| 1930 | + &prediction, | ||
| 1931 | + false, | ||
| 1932 | + search.base_q_idx, | ||
| 1933 | + search.deadzone, | ||
| 1934 | + set, | ||
| 1935 | + ) | ||
| 1936 | + } | ||
| 1937 | + None => { | ||
| 1938 | + self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx) | ||
| 1939 | + } | ||
| 1940 | + }; | ||
| 1905 | sse += trial.sse; | 1941 | sse += trial.sse; |
| 1906 | bits += trial.bits; | 1942 | bits += trial.bits; |
| 1907 | self.commit(tu.x, tu.y, tx, &trial); | 1943 | self.commit(tu.x, tu.y, tx, &trial); |
| @@ -3848,7 +3884,7 @@ fn code_square( | |||
| 3848 | luma_coeffs, | 3884 | luma_coeffs, |
| 3849 | ); | 3885 | ); |
| 3850 | for depth in 1..=max_tx_depth(side) { | 3886 | for depth in 1..=max_tx_depth(side) { |
| 3851 | - let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, search, fctx); | 3887 | + let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx); |
| 3852 | let cost_d = sse | 3888 | let cost_d = sse |
| 3853 | + search.lambda | 3889 | + search.lambda |
| 3854 | * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); | 3890 | * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); |
| @@ -3920,14 +3956,19 @@ fn code_square( | |||
| 3920 | // by construction (`av1_filter_intra_allowed`), and its luma transform | 3956 | // by construction (`av1_filter_intra_allowed`), and its luma transform |
| 3921 | // types are coded from `fimode_to_intradir`'s row (`crate::tile::tx_row`). | 3957 | // types are coded from `fimode_to_intradir`'s row (`crate::tile::tx_row`). |
| 3922 | // | 3958 | // |
| 3923 | - // corner-cut, ceiling named: the candidate is only ever priced at | 3959 | + // lane-fitu: on a SCREEN frame the candidate is priced at every transform |
| 3924 | - // `tx_depth` 0 (one transform over the whole block), so a block whose | 3960 | + // depth the ordinary intra search runs, each unit predicting recursively |
| 3925 | - // ordinary intra winner wanted a SPLIT transform is compared against a | 3961 | + // off the units before it ([`Plane::code_tx_depth`]'s `filter_intra` |
| 3926 | - // handicapped filter-intra arm -- and only 19-44% of this encoder's intra | 3962 | + // arm), so a block whose ordinary winner wanted a split transform is |
| 3927 | - // blocks code depth 0. Upgrade path: predict per transform unit inside | 3963 | + // compared against a filter arm that may split too. |
| 3928 | - // [`Plane::code_tx_depth`] (the decoder already reads `filter_intra` per | 3964 | + // |
| 3929 | - // unit, decode.rs:11835) and run the depth search under the winning | 3965 | + // Measured (native BD table, lane-fitu): searching the depths on EVERY |
| 3930 | - // filter mode. | 3966 | + // frame moves screen -1.0/-0.4 and the bars rows -0.4/-0.7 but leaves |
| 3967 | + // film A flat and film B +0.1/+0.5 WORSE -- the extra filter blocks are a | ||
| 3968 | + // locally cheaper reconstruction that the film rows go on to predict from | ||
| 3969 | + // (class `local-rd-on-references`) -- at +30..35% encoder wall. Screen | ||
| 3970 | + // content is where it pays, and a non-screen frame keeps the depth-0-only | ||
| 3971 | + // arm, byte for byte. | ||
| 3931 | let mut filter_intra: Option<u8> = None; | 3972 | let mut filter_intra: Option<u8> = None; |
| 3932 | if filter_intra_on() | 3973 | if filter_intra_on() |
| 3933 | && palette.is_none() | 3974 | && palette.is_none() |
| @@ -3943,47 +3984,39 @@ fn code_square( | |||
| 3943 | } | 3984 | } |
| 3944 | let one_bits = symbol_bits(row, 1); | 3985 | let one_bits = symbol_bits(row, 1); |
| 3945 | let kept = luma.snapshot(x, y, side); | 3986 | let kept = luma.snapshot(x, y, side); |
| 3946 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | 3987 | + let max_depth = if tx_select && search.screen { max_tx_depth(side) } else { 0 }; |
| 3947 | - let (above, left, corner) = | 3988 | + let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None; |
| 3948 | - luma.edges_into(x, y, side, at.reach, &mut above_buf, &mut left_buf); | ||
| 3949 | - let mut prediction = vec![0u8; side * side]; | ||
| 3950 | for fi in 0..5u8 { | 3989 | for fi in 0..5u8 { |
| 3951 | - filter_intra_predict_u8( | 3990 | + for depth in 0..=max_depth { |
| 3952 | - usize::from(fi), | 3991 | + let (levels, sse, bits) = |
| 3953 | - above, | 3992 | + luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx); |
| 3954 | - left, | 3993 | + let cost_f = sse |
| 3955 | - corner, | 3994 | + + search.lambda |
| 3956 | - side, | 3995 | + * (bits |
| 3957 | - &mut prediction, fctx, | 3996 | + + mode_bits[DC_PRED as usize] |
| 3958 | - ); | 3997 | + + one_bits |
| 3959 | - let trial = luma.code_from_prediction( | 3998 | + + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi)) |
| 3960 | - x, | 3999 | + + if tx_select { tx_depth_bits(side, depth) } else { 0.0 }); |
| 3961 | - y, | 4000 | + if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) { |
| 3962 | - side, | 4001 | + best = Some(( |
| 3963 | - &prediction, | 4002 | + cost_f, |
| 3964 | - false, | 4003 | + fi, |
| 3965 | - search.base_q_idx, | 4004 | + depth, |
| 3966 | - search.deadzone, | 4005 | + luma.snapshot(x, y, side), |
| 3967 | - luma_set, | 4006 | + coeffs(&levels, side), |
| 3968 | - ); | 4007 | + )); |
| 3969 | - let cost_f = trial.sse | 4008 | + } |
| 3970 | - + search.lambda | ||
| 3971 | - * (trial.bits | ||
| 3972 | - + mode_bits[DC_PRED as usize] | ||
| 3973 | - + one_bits | ||
| 3974 | - + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi)) | ||
| 3975 | - + if tx_select { tx_depth_bits(side, 0) } else { 0.0 }); | ||
| 3976 | - if cost_f < cost { | ||
| 3977 | - cost = cost_f; | ||
| 3978 | - mode = DC_PRED; | ||
| 3979 | - angle_delta = 0; | ||
| 3980 | - tx_depth = 0; | ||
| 3981 | - luma_coeffs = coeffs(&trial.levels, side); | ||
| 3982 | - luma.commit(x, y, side, &trial); | ||
| 3983 | - filter_intra = Some(fi); | ||
| 3984 | } | 4009 | } |
| 3985 | } | 4010 | } |
| 3986 | - if filter_intra.is_none() { | 4011 | + if let Some((cost_f, fi, depth, snapshot, levels)) = best { |
| 4012 | + cost = cost_f; | ||
| 4013 | + mode = DC_PRED; | ||
| 4014 | + angle_delta = 0; | ||
| 4015 | + tx_depth = depth as u8; | ||
| 4016 | + luma_coeffs = levels; | ||
| 4017 | + luma.restore(x, y, side, &snapshot); | ||
| 4018 | + filter_intra = Some(fi); | ||
| 4019 | + } else { | ||
| 3987 | luma.restore(x, y, side, &kept); | 4020 | luma.restore(x, y, side, &kept); |
| 3988 | } | 4021 | } |
| 3989 | } | 4022 | } |
| @@ -11078,6 +11111,12 @@ mod tests { | |||
| 11078 | #[test] | 11111 | #[test] |
| 11079 | fn the_search_picks_the_direction_the_picture_runs() { | 11112 | fn the_search_picks_the_direction_the_picture_runs() { |
| 11080 | let fctx = &crate::decode::FrameCtx::new(); | 11113 | let fctx = &crate::decode::FrameCtx::new(); |
| 11114 | + // Same confound as [`every_mode_decodes_to_what_the_encoder_predicted`]: | ||
| 11115 | + // a filter-intra block is coded `DC_PRED` whatever mode the arm | ||
| 11116 | + // offers, and these stripes are SCREEN content, where the filter arm | ||
| 11117 | + // now searches its own transform depth (lane-fitu) and wins outright | ||
| 11118 | + // -- which says nothing about the mode search this gate measures. | ||
| 11119 | + force_filter_intra(Some(false)); | ||
| 11081 | for (vertical, want) in [(true, V_PRED), (false, H_PRED)] { | 11120 | for (vertical, want) in [(true, V_PRED), (false, H_PRED)] { |
| 11082 | let picture = stripes(128, 96, vertical); | 11121 | let picture = stripes(128, 96, vertical); |
| 11083 | // Only the pair, because on stripes a third mode predicts exactly | 11122 | // Only the pair, because on stripes a third mode predicts exactly |