A NEWMV that lands on the stack's NEARESTMV reuses its trials
Browse files at this commit12435b7e0731031c1945d7fa3d194626ae31f95cParent: 56cc232- perf
- a NEWMV that lands on the stack's NEARESTMV reuses its trials
1 changed file +43 -18
- crates/ec-av1/src/encode.rs +43 -18
crates/ec-av1/src/encode.rsmodified+43 -18
| @@ -1856,18 +1856,28 @@ fn code_square_inter( | |||
| 1856 | ); | 1856 | ); |
| 1857 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); | 1857 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 1858 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { | 1858 | if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { |
| 1859 | - let luma_new = mc_trial( | 1859 | + // Same reuse as `search_inter_block`: a NEWMV equal to the |
| 1860 | - luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, | 1860 | + // stack's NEARESTMV is the same three trials, already run above. |
| 1861 | - false, search.base_q_idx, search.deadzone, luma_set, fctx, | 1861 | + let (luma_new, u_new, v_new) = if new_mv == mv { |
| 1862 | - ); | 1862 | + (luma_trial.clone(), u.clone(), v.clone()) |
| 1863 | - let u_new = mc_trial( | 1863 | + } else { |
| 1864 | - &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, ref_u.2, | 1864 | + ( |
| 1865 | - ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | 1865 | + mc_trial( |
| 1866 | - ); | 1866 | + luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, |
| 1867 | - let v_new = mc_trial( | 1867 | + ref_luma.3, false, search.base_q_idx, search.deadzone, luma_set, fctx, |
| 1868 | - &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, ref_v.2, | 1868 | + ), |
| 1869 | - ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | 1869 | + mc_trial( |
| 1870 | - ); | 1870 | + &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, |
| 1871 | + ref_u.2, ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, | ||
| 1872 | + fctx, | ||
| 1873 | + ), | ||
| 1874 | + mc_trial( | ||
| 1875 | + &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, | ||
| 1876 | + ref_v.2, ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, | ||
| 1877 | + fctx, | ||
| 1878 | + ), | ||
| 1879 | + ) | ||
| 1880 | + }; | ||
| 1871 | let skip_new = luma_new.levels.iter().all(|&l| l == 0) | 1881 | let skip_new = luma_new.levels.iter().all(|&l| l == 0) |
| 1872 | && u_new.levels.iter().all(|&l| l == 0) | 1882 | && u_new.levels.iter().all(|&l| l == 0) |
| 1873 | && v_new.levels.iter().all(|&l| l == 0); | 1883 | && v_new.levels.iter().all(|&l| l == 0); |
| @@ -2972,6 +2982,14 @@ fn search_inter_block( | |||
| 2972 | // The NEARESTMV candidate: `skip` is decided below from whether its | 2982 | // The NEARESTMV candidate: `skip` is decided below from whether its |
| 2973 | // trial actually found a nonzero level, once its residual is priced | 2983 | // trial actually found a nonzero level, once its residual is priced |
| 2974 | // against the same CDFs the tile writer codes it with. | 2984 | // against the same CDFs the tile writer codes it with. |
| 2985 | + // | ||
| 2986 | + // lane-av1speed: its three trials are kept (rather than moved straight | ||
| 2987 | + // into the candidate) so that a NEWMV landing on the very same vector -- | ||
| 2988 | + // common on static content, where the stack's nearest vector is also | ||
| 2989 | + // what the search finds -- reuses them instead of running the same | ||
| 2990 | + // motion compensation, transform, quantize and reconstruct again. The | ||
| 2991 | + // reused trials are the same values, so the choice is bit-identical. | ||
| 2992 | + let nearest_trials; | ||
| 2975 | { | 2993 | { |
| 2976 | let mv = stack.nearest_mv; | 2994 | let mv = stack.nearest_mv; |
| 2977 | let luma_trial = mc_trial( | 2995 | let luma_trial = mc_trial( |
| @@ -3038,6 +3056,7 @@ fn search_inter_block( | |||
| 3038 | } else { | 3056 | } else { |
| 3039 | luma_trial.bits + u.bits + v.bits | 3057 | luma_trial.bits + u.bits + v.bits |
| 3040 | }); | 3058 | }); |
| 3059 | + nearest_trials = (luma_trial.clone(), u.clone(), v.clone()); | ||
| 3041 | consider(Candidate { | 3060 | consider(Candidate { |
| 3042 | cost, | 3061 | cost, |
| 3043 | luma: luma_trial, | 3062 | luma: luma_trial, |
| @@ -3072,7 +3091,11 @@ fn search_inter_block( | |||
| 3072 | stage_add(0, t.elapsed()); | 3091 | stage_add(0, t.elapsed()); |
| 3073 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); | 3092 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 3074 | if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { | 3093 | if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { |
| 3075 | - let luma_trial = mc_trial( | 3094 | + let (luma_trial, u, v) = if mv == stack.nearest_mv { |
| 3095 | + nearest_trials | ||
| 3096 | + } else { | ||
| 3097 | + ( | ||
| 3098 | + mc_trial( | ||
| 3076 | luma, | 3099 | luma, |
| 3077 | x, | 3100 | x, |
| 3078 | y, | 3101 | y, |
| @@ -3087,8 +3110,8 @@ fn search_inter_block( | |||
| 3087 | search.base_q_idx, | 3110 | search.base_q_idx, |
| 3088 | search.deadzone, | 3111 | search.deadzone, |
| 3089 | TxbSet::Luma32Inter, fctx, | 3112 | TxbSet::Luma32Inter, fctx, |
| 3090 | - ); | 3113 | + ), |
| 3091 | - let u = mc_trial( | 3114 | + mc_trial( |
| 3092 | &chroma[0], | 3115 | &chroma[0], |
| 3093 | x / 2, | 3116 | x / 2, |
| 3094 | y / 2, | 3117 | y / 2, |
| @@ -3103,8 +3126,8 @@ fn search_inter_block( | |||
| 3103 | search.base_q_idx, | 3126 | search.base_q_idx, |
| 3104 | search.deadzone, | 3127 | search.deadzone, |
| 3105 | chroma_set, fctx, | 3128 | chroma_set, fctx, |
| 3106 | - ); | 3129 | + ), |
| 3107 | - let v = mc_trial( | 3130 | + mc_trial( |
| 3108 | &chroma[1], | 3131 | &chroma[1], |
| 3109 | x / 2, | 3132 | x / 2, |
| 3110 | y / 2, | 3133 | y / 2, |
| @@ -3119,7 +3142,9 @@ fn search_inter_block( | |||
| 3119 | search.base_q_idx, | 3142 | search.base_q_idx, |
| 3120 | search.deadzone, | 3143 | search.deadzone, |
| 3121 | chroma_set, fctx, | 3144 | chroma_set, fctx, |
| 3122 | - ); | 3145 | + ), |
| 3146 | + ) | ||
| 3147 | + }; | ||
| 3123 | let drl_bits = if stack.entries.len() > 1 { | 3148 | let drl_bits = if stack.entries.len() > 1 { |
| 3124 | symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) | 3149 | symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) |
| 3125 | } else { | 3150 | } else { |