Filter intra predicts per transform unit, depths searched on screen frames
Browse files at this commit235add97870eb53db5ef903c9cad3fc816a23ee7Parent: db67894- feat
- filter intra predicts per transform unit, depths searched on screen frames
1 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 | 1868 | /// reconstructed on its own. Commits the result and hands back the |
| 1869 | 1869 | /// block-coordinate levels, the block's squared error and what its |
| 1870 | 1870 | /// coefficients cost. |
| 1871 | + #[allow(clippy::too_many_arguments)] | |
| 1871 | 1872 | fn code_tx_depth( |
| 1872 | 1873 | &mut self, |
| 1873 | 1874 | at: At, |
| 1874 | 1875 | mode: u8, |
| 1875 | 1876 | angle_delta: i32, |
| 1876 | 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 | 1884 | search: &Search, fctx: &crate::decode::FrameCtx, |
| 1878 | 1885 | ) -> (Vec<i32>, f64, f64) { |
| 1879 | 1886 | let At { x, y, side, reach, .. } = at; |
| @@ -1901,7 +1908,36 @@ impl Plane<'_> { | ||
| 1901 | 1908 | reach: Reach::of_tu(side, side, col_off, row_off, tx, tx, reach), |
| 1902 | 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 | 1941 | sse += trial.sse; |
| 1906 | 1942 | bits += trial.bits; |
| 1907 | 1943 | self.commit(tu.x, tu.y, tx, &trial); |
| @@ -3848,7 +3884,7 @@ fn code_square( | ||
| 3848 | 3884 | luma_coeffs, |
| 3849 | 3885 | ); |
| 3850 | 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 | 3888 | let cost_d = sse |
| 3853 | 3889 | + search.lambda |
| 3854 | 3890 | * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); |
| @@ -3920,14 +3956,19 @@ fn code_square( | ||
| 3920 | 3956 | // by construction (`av1_filter_intra_allowed`), and its luma transform |
| 3921 | 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 | |
| 3924 | - // `tx_depth` 0 (one transform over the whole block), so a block whose | |
| 3925 | - // ordinary intra winner wanted a SPLIT transform is compared against a | |
| 3926 | - // handicapped filter-intra arm -- and only 19-44% of this encoder's intra | |
| 3927 | - // blocks code depth 0. Upgrade path: predict per transform unit inside | |
| 3928 | - // [`Plane::code_tx_depth`] (the decoder already reads `filter_intra` per | |
| 3929 | - // unit, decode.rs:11835) and run the depth search under the winning | |
| 3930 | - // filter mode. | |
| 3959 | + // lane-fitu: on a SCREEN frame the candidate is priced at every transform | |
| 3960 | + // depth the ordinary intra search runs, each unit predicting recursively | |
| 3961 | + // off the units before it ([`Plane::code_tx_depth`]'s `filter_intra` | |
| 3962 | + // arm), so a block whose ordinary winner wanted a split transform is | |
| 3963 | + // compared against a filter arm that may split too. | |
| 3964 | + // | |
| 3965 | + // Measured (native BD table, lane-fitu): searching the depths on EVERY | |
| 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 | 3972 | let mut filter_intra: Option<u8> = None; |
| 3932 | 3973 | if filter_intra_on() |
| 3933 | 3974 | && palette.is_none() |
| @@ -3943,47 +3984,39 @@ fn code_square( | ||
| 3943 | 3984 | } |
| 3944 | 3985 | let one_bits = symbol_bits(row, 1); |
| 3945 | 3986 | let kept = luma.snapshot(x, y, side); |
| 3946 | - let (mut above_buf, mut left_buf) = ([0u8; 2 * BLOCK], [0u8; 2 * BLOCK]); | |
| 3947 | - let (above, left, corner) = | |
| 3948 | - luma.edges_into(x, y, side, at.reach, &mut above_buf, &mut left_buf); | |
| 3949 | - let mut prediction = vec![0u8; side * side]; | |
| 3987 | + let max_depth = if tx_select && search.screen { max_tx_depth(side) } else { 0 }; | |
| 3988 | + let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None; | |
| 3950 | 3989 | for fi in 0..5u8 { |
| 3951 | - filter_intra_predict_u8( | |
| 3952 | - usize::from(fi), | |
| 3953 | - above, | |
| 3954 | - left, | |
| 3955 | - corner, | |
| 3956 | - side, | |
| 3957 | - &mut prediction, fctx, | |
| 3958 | - ); | |
| 3959 | - let trial = luma.code_from_prediction( | |
| 3960 | - x, | |
| 3961 | - y, | |
| 3962 | - side, | |
| 3963 | - &prediction, | |
| 3964 | - false, | |
| 3965 | - search.base_q_idx, | |
| 3966 | - search.deadzone, | |
| 3967 | - luma_set, | |
| 3968 | - ); | |
| 3969 | - let cost_f = trial.sse | |
| 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); | |
| 3990 | + for depth in 0..=max_depth { | |
| 3991 | + let (levels, sse, bits) = | |
| 3992 | + luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx); | |
| 3993 | + let cost_f = sse | |
| 3994 | + + search.lambda | |
| 3995 | + * (bits | |
| 3996 | + + mode_bits[DC_PRED as usize] | |
| 3997 | + + one_bits | |
| 3998 | + + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi)) | |
| 3999 | + + if tx_select { tx_depth_bits(side, depth) } else { 0.0 }); | |
| 4000 | + if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) { | |
| 4001 | + best = Some(( | |
| 4002 | + cost_f, | |
| 4003 | + fi, | |
| 4004 | + depth, | |
| 4005 | + luma.snapshot(x, y, side), | |
| 4006 | + coeffs(&levels, side), | |
| 4007 | + )); | |
| 4008 | + } | |
| 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 | 4020 | luma.restore(x, y, side, &kept); |
| 3988 | 4021 | } |
| 3989 | 4022 | } |
| @@ -11078,6 +11111,12 @@ mod tests { | ||
| 11078 | 11111 | #[test] |
| 11079 | 11112 | fn the_search_picks_the_direction_the_picture_runs() { |
| 11080 | 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 | 11120 | for (vertical, want) in [(true, V_PRED), (false, H_PRED)] { |
| 11082 | 11121 | let picture = stripes(128, 96, vertical); |
| 11083 | 11122 | // Only the pair, because on stripes a third mode predicts exactly |