SAD-prescreen the 13-mode intra search, gated off by default
Browse files at this commit7f9b971515db1142296bf2d960f65d99cf9a0c6fParent: 2e9837a- perf
- SAD-prescreen the 13-mode intra search, gated off by default
1 changed file +98 -6
- crates/ec-av1/src/encode.rs +98 -6
crates/ec-av1/src/encode.rsmodified+98 -6
| @@ -742,17 +742,83 @@ impl Plane<'_> { | ||
| 742 | 742 | } |
| 743 | 743 | } |
| 744 | 744 | |
| 745 | - /// Codes one block under every mode the search offers and commits the one | |
| 746 | - /// whose squared error and estimated rate come out cheapest. | |
| 745 | + /// Codes one block under every mode the search offers (or, when | |
| 746 | + /// [`Search::top_k`] prunes it, the cheapest `top_k` of them by a SAD | |
| 747 | + /// pre-pass plus DC) and commits the one whose squared error and | |
| 748 | + /// estimated rate come out cheapest. | |
| 749 | + /// | |
| 750 | + /// The prediction is built once per mode regardless -- the full RD trial | |
| 751 | + /// needs it too -- so ranking modes by SAD on that same prediction before | |
| 752 | + /// running the expensive part (forward transform, quantize, reconstruct, | |
| 753 | + /// `coeff_bits`) on only the survivors costs nothing extra to predict, | |
| 754 | + /// only to sum. | |
| 747 | 755 | fn search_block( |
| 748 | 756 | &mut self, |
| 749 | 757 | at: At, |
| 750 | 758 | search: &Search, |
| 751 | 759 | mode_bits: &[f64; 13], |
| 752 | 760 | ) -> (Vec<Coeff>, u8, f64) { |
| 761 | + let At { | |
| 762 | + x, y, side, reach, .. | |
| 763 | + } = at; | |
| 764 | + let (above, left, corner) = self.edges(x, y, side, reach); | |
| 765 | + let mut scored: Vec<(f64, u8, Vec<u8>)> = search | |
| 766 | + .modes | |
| 767 | + .iter() | |
| 768 | + .map(|&mode| { | |
| 769 | + let mut prediction = vec![0u8; side * side]; | |
| 770 | + crate::intra::predict( | |
| 771 | + mode, | |
| 772 | + above.as_deref(), | |
| 773 | + left.as_deref(), | |
| 774 | + corner, | |
| 775 | + side, | |
| 776 | + &mut prediction, | |
| 777 | + ); | |
| 778 | + let sad: f64 = (0..side * side) | |
| 779 | + .map(|i| { | |
| 780 | + let (row, col) = (i / side, i % side); | |
| 781 | + f64::from( | |
| 782 | + (i32::from(self.source[(y + row) * self.width + x + col]) | |
| 783 | + - i32::from(prediction[i])) | |
| 784 | + .abs(), | |
| 785 | + ) | |
| 786 | + }) | |
| 787 | + .sum(); | |
| 788 | + ( | |
| 789 | + sad + search.lambda * mode_bits[usize::from(mode)], | |
| 790 | + mode, | |
| 791 | + prediction, | |
| 792 | + ) | |
| 793 | + }) | |
| 794 | + .collect(); | |
| 795 | + if let Some(k) = search.top_k { | |
| 796 | + if k < scored.len() { | |
| 797 | + let dc_pos = scored.iter().position(|&(_, mode, _)| mode == DC_PRED); | |
| 798 | + let dc_entry = dc_pos.map(|i| scored.remove(i)); | |
| 799 | + scored.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("scores are finite")); | |
| 800 | + scored.truncate(if dc_entry.is_some() { | |
| 801 | + k.saturating_sub(1) | |
| 802 | + } else { | |
| 803 | + k | |
| 804 | + }); | |
| 805 | + if let Some(dc_entry) = dc_entry { | |
| 806 | + scored.push(dc_entry); | |
| 807 | + } | |
| 808 | + } | |
| 809 | + } | |
| 753 | 810 | let mut best: Option<(f64, u8, Trial)> = None; |
| 754 | - for &mode in search.modes { | |
| 755 | - let trial = self.trial(at, mode, search.base_q_idx, search.deadzone); | |
| 811 | + for (_, mode, prediction) in scored { | |
| 812 | + let trial = self.code_from_prediction( | |
| 813 | + x, | |
| 814 | + y, | |
| 815 | + side, | |
| 816 | + &prediction, | |
| 817 | + false, | |
| 818 | + search.base_q_idx, | |
| 819 | + search.deadzone, | |
| 820 | + at.set, | |
| 821 | + ); | |
| 756 | 822 | let cost = trial.sse + search.lambda * (trial.bits + mode_bits[usize::from(mode)]); |
| 757 | 823 | if best |
| 758 | 824 | .as_ref() |
| @@ -762,8 +828,8 @@ impl Plane<'_> { | ||
| 762 | 828 | } |
| 763 | 829 | } |
| 764 | 830 | let (cost, mode, trial) = best.expect("the search offers at least one mode"); |
| 765 | - self.commit(at.x, at.y, at.side, &trial); | |
| 766 | - (coeffs(&trial.levels, at.side), mode, cost) | |
| 831 | + self.commit(x, y, side, &trial); | |
| 832 | + (coeffs(&trial.levels, side), mode, cost) | |
| 767 | 833 | } |
| 768 | 834 | } |
| 769 | 835 | |
| @@ -1011,8 +1077,32 @@ struct Search<'a> { | ||
| 1011 | 1077 | deadzone: f64, |
| 1012 | 1078 | lambda: f64, |
| 1013 | 1079 | modes: &'a [u8], |
| 1080 | + /// How many of `modes` the full RD trial (forward transform, quantize, | |
| 1081 | + /// reconstruct, `coeff_bits`) actually runs on, ranked by a cheap SAD | |
| 1082 | + /// pre-pass on the prediction residual alone plus DC (always kept, since | |
| 1083 | + /// libaom's own intra pruning -- `av1/encoder/speed_features.c`'s | |
| 1084 | + /// `intra_pruning_with_hog` -- never prunes it away). `None` runs every | |
| 1085 | + /// mode through full RD, unchanged from before this lever. | |
| 1086 | + top_k: Option<usize>, | |
| 1014 | 1087 | } |
| 1015 | 1088 | |
| 1089 | +/// [`Search::top_k`]'s value: `None` keeps every mode's search unchanged | |
| 1090 | +/// (the default, until a lever's quality gate justifies pruning by default); | |
| 1091 | +/// `EC_AV1_PRUNE_K` sweeps it in a test build without touching callers. | |
| 1092 | +fn prune_top_k() -> Option<usize> { | |
| 1093 | + let swept = std::env::var("EC_AV1_PRUNE_K") | |
| 1094 | + .ok() | |
| 1095 | + .and_then(|v| v.parse::<usize>().ok()); | |
| 1096 | + match swept { | |
| 1097 | + Some(k) if cfg!(test) => Some(k), | |
| 1098 | + _ => PRUNE_TOP_K, | |
| 1099 | + } | |
| 1100 | +} | |
| 1101 | + | |
| 1102 | +/// The default: unpruned, thirteen full trials per block, same as before this | |
| 1103 | +/// lever. Set from the sweep in the lane report if the quality gate clears it. | |
| 1104 | +const PRUNE_TOP_K: Option<usize> = None; | |
| 1105 | + | |
| 1016 | 1106 | /// One mode's coding of one block, before it is committed. |
| 1017 | 1107 | struct Trial { |
| 1018 | 1108 | levels: Vec<i32>, |
| @@ -1161,6 +1251,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 1161 | 1251 | deadzone, |
| 1162 | 1252 | lambda: lambda_scale() * step * step, |
| 1163 | 1253 | modes, |
| 1254 | + top_k: prune_top_k(), | |
| 1164 | 1255 | }; |
| 1165 | 1256 | |
| 1166 | 1257 | // The block grid this frame is coded over: [`crate::tile::block_grid`]'s |
| @@ -1923,6 +2014,7 @@ pub(crate) fn encode_inter_frame( | ||
| 1923 | 2014 | deadzone, |
| 1924 | 2015 | lambda: lambda_scale() * step * step, |
| 1925 | 2016 | modes: &KEY_FRAME_MODES, |
| 2017 | + top_k: prune_top_k(), | |
| 1926 | 2018 | }; |
| 1927 | 2019 | let mode_bits_table = inter_mode_bits(); |
| 1928 | 2020 |