Merge branch 'lane-h265perf2': closed-form bit proxy for h265 mode ranking
Browse files at this commit2 changed files +72 -13
- crates/ec-h265/src/ctu.rs +8 -13
- crates/ec-h265/src/residual.rs +64 -0
crates/ec-h265/src/ctu.rsmodified+8 -13
| @@ -1370,23 +1370,18 @@ impl<'a> CtuEncoder<'a> { | |||
| 1370 | // The winner gets the full RDOQ refinement below, once. | 1370 | // The winner gets the full RDOQ refinement below, once. |
| 1371 | let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs, false); | 1371 | let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs, false); |
| 1372 | enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf)); | 1372 | enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf)); |
| 1373 | + let mut bits = (enc.bit_count() - bits_before) as f64; | ||
| 1373 | if cbf { | 1374 | if cbf { |
| 1375 | + // Ranking candidates against each other only needs a proxy | ||
| 1376 | + // for encode_residual's exact bit count, not the count | ||
| 1377 | + // itself: the real per-bin CABAC search (context updates, | ||
| 1378 | + // greater1/greater2/remaining binarization) is the hot loop | ||
| 1379 | + // this replaces, and only the winner ever pays for it below. | ||
| 1374 | let scan = intra::scan_index(mode, log2, true); | 1380 | let scan = intra::scan_index(mode, log2, true); |
| 1375 | - let levels = std::mem::take(&mut self.scratch.levels); | 1381 | + bits += residual::estimate_residual_bits(&self.scratch.levels[..n * n], log2, scan); |
| 1376 | - encode_residual( | ||
| 1377 | - enc, | ||
| 1378 | - &levels[..n * n], | ||
| 1379 | - log2, | ||
| 1380 | - 0, | ||
| 1381 | - scan, | ||
| 1382 | - self.sign_hiding, | ||
| 1383 | - self.transform_skip, | ||
| 1384 | - ); | ||
| 1385 | - self.scratch.levels = levels; | ||
| 1386 | } | 1382 | } |
| 1387 | - let bits = enc.bit_count() - bits_before; | ||
| 1388 | enc.restore(&start); | 1383 | enc.restore(&start); |
| 1389 | - let cost = ssd + self.lambda * bits as f64; | 1384 | + let cost = ssd + self.lambda * bits; |
| 1390 | if cost < best.0 { | 1385 | if cost < best.0 { |
| 1391 | best = (cost, mode); | 1386 | best = (cost, mode); |
| 1392 | best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]); | 1387 | best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]); |
crates/ec-h265/src/residual.rsmodified+64 -0
| @@ -213,6 +213,70 @@ const RDOQ_MAX_LEVEL: i32 = 2; | |||
| 213 | /// content still behind x265, so it takes the tie. | 213 | /// content still behind x265, so it takes the tie. |
| 214 | const RDOQ_LAMBDA: f64 = 1.0; | 214 | const RDOQ_LAMBDA: f64 = 1.0; |
| 215 | 215 | ||
| 216 | +/// How hard the significance map's per-position flag is priced in the | ||
| 217 | +/// coefficient-statistics proxy below, in bits. Swept against | ||
| 218 | +/// `bd_psnr_vs_x265` (synthetic clip + a real screen-capture clip) alongside | ||
| 219 | +/// [`ESTIMATE_MAG_SCALE`]; see that constant's comment for the numbers. | ||
| 220 | +const ESTIMATE_SIG_BIT: f64 = 0.15; | ||
| 221 | + | ||
| 222 | +/// Scale on the per-level magnitude term of [`estimate_residual_bits`]. | ||
| 223 | +/// | ||
| 224 | +/// Swept BD-PSNR-YUV against x265 on the synthetic clip (real-bits baseline | ||
| 225 | +/// +7.862 dB): 1.0 reads +7.790, 1.25 reads +7.817, 1.5 reads +7.868 — closest | ||
| 226 | +/// to the baseline, and confirmed on a real 2560x1440 screen capture too | ||
| 227 | +/// (real-bits baseline -0.317 dB, this scale -0.331, both other scales | ||
| 228 | +/// untried there once the synthetic sweep picked a direction). | ||
| 229 | +const ESTIMATE_MAG_SCALE: f64 = 1.5; | ||
| 230 | + | ||
| 231 | +/// A cheap stand-in for [`encode_residual`]'s exact bit count, for ranking | ||
| 232 | +/// intra-mode candidates before the real search runs once on the winner | ||
| 233 | +/// (`choose_luma_mode`). No CABAC context is touched — the last position is | ||
| 234 | +/// priced by the same closed-form binarization the real coder uses (a few | ||
| 235 | +/// arithmetic ops, not a per-bin call), and every level's cost comes from a | ||
| 236 | +/// log2 magnitude estimate instead of walking greater1/greater2/remaining | ||
| 237 | +/// bins one context update at a time. This is a ranking proxy only: the | ||
| 238 | +/// committed bitstream always goes through `encode_residual`. | ||
| 239 | +pub fn estimate_residual_bits(levels: &[i32], log2_size: u32, scan_idx: usize) -> f64 { | ||
| 240 | + let n = 1usize << log2_size; | ||
| 241 | + let positions = &POSITIONS[(log2_size - 2) as usize][scan_idx]; | ||
| 242 | + let mut last_full = 0i32; | ||
| 243 | + let mut mag_bits = 0.0f64; | ||
| 244 | + let mut any = false; | ||
| 245 | + for (i, &level) in levels[..n * n].iter().enumerate() { | ||
| 246 | + if level != 0 { | ||
| 247 | + any = true; | ||
| 248 | + last_full = last_full.max(i32::from(positions[i])); | ||
| 249 | + let v = level.unsigned_abs(); | ||
| 250 | + // 1 (sign) + 2 for the first two magnitudes (mirroring the real | ||
| 251 | + // coder's greater1/greater2 flags), Exp-Golomb-ish beyond that. | ||
| 252 | + mag_bits += 1.0 | ||
| 253 | + + if v <= 2 { | ||
| 254 | + v as f64 | ||
| 255 | + } else { | ||
| 256 | + 2.0 + 2.0 * ((v - 1) as f64).log2() | ||
| 257 | + }; | ||
| 258 | + } | ||
| 259 | + } | ||
| 260 | + if !any { | ||
| 261 | + return 0.0; | ||
| 262 | + } | ||
| 263 | + let last_sub = (last_full / 16) as usize; | ||
| 264 | + let last_pos = (last_full % 16) as usize; | ||
| 265 | + let sub_scan = scan(log2_size - 2, scan_idx); | ||
| 266 | + let pos_scan = scan(2, scan_idx); | ||
| 267 | + let (last_xs, last_ys) = sub_scan[last_sub]; | ||
| 268 | + let (last_xp, last_yp) = pos_scan[last_pos]; | ||
| 269 | + let mut last_x = (last_xs as u32) * 4 + last_xp as u32; | ||
| 270 | + let mut last_y = (last_ys as u32) * 4 + last_yp as u32; | ||
| 271 | + if scan_idx == 2 { | ||
| 272 | + std::mem::swap(&mut last_x, &mut last_y); | ||
| 273 | + } | ||
| 274 | + let (x_prefix, x_suffix) = last_prefix(last_x); | ||
| 275 | + let (y_prefix, y_suffix) = last_prefix(last_y); | ||
| 276 | + let last_bits = (x_prefix + y_prefix + x_suffix + y_suffix) as f64; | ||
| 277 | + last_bits + ESTIMATE_SIG_BIT * (last_full + 1) as f64 + ESTIMATE_MAG_SCALE * mag_bits | ||
| 278 | +} | ||
| 279 | + | ||
| 216 | /// What one candidate set of levels costs in bits, priced by the CABAC coder | 280 | /// What one candidate set of levels costs in bits, priced by the CABAC coder |
| 217 | /// itself against `base`'s context state. | 281 | /// itself against `base`'s context state. |
| 218 | #[allow(clippy::too_many_arguments)] | 282 | #[allow(clippy::too_many_arguments)] |