From eb7b07c2f0e604acddb511ca57eef6c5b01f4332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 19:03:00 +0300 Subject: [PATCH] perf(av1): bound the RDOQ pass to small levels and a per-block price budget Unbounded it re-coded the block once per candidate level and a fine-quantizer 32x32 carries hundreds, which took the 12-frame native gate from minutes to not finishing a row in 35. Levels above 2 are never candidates and a block spends at most 24 prices, taken from the tail backwards; the q=150 pin moves 9853 -> 9059 B against the unbounded pass's 8959, at +26% wall instead of +256%. --- crates/ec-av1/src/tile.rs | 47 +++++++++++++++++++++++++++++++++++++-- lanes/rdoq-ctl.log | 26 ++++++++++++++++++++++ lanes/rdoq-l07.log | 6 +++++ lanes/rdoq-r2.log | 6 +++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 lanes/rdoq-l07.log create mode 100644 lanes/rdoq-r2.log diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 3b502dd1..786d9d6c 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -4525,6 +4525,32 @@ pub(crate) fn coeff_bits( }) } +/// The largest level [`rdoq`] offers a candidate for, and the most prices it +/// may spend on one block. `EC_AV1_RDOQ_MAXLEVEL` / `EC_AV1_RDOQ_BUDGET` +/// sweep them; see [`rdoq`] for why both exist. +const RDOQ_MAX_LEVEL: u32 = 2; +const RDOQ_BUDGET: u32 = 24; + +fn rdoq_max_level() -> u32 { + static ENV: LazyLock = LazyLock::new(|| { + crate::envflags::var("EC_AV1_RDOQ_MAXLEVEL") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(RDOQ_MAX_LEVEL) + }); + *ENV +} + +fn rdoq_budget() -> u32 { + static ENV: LazyLock = LazyLock::new(|| { + crate::envflags::var("EC_AV1_RDOQ_BUDGET") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(RDOQ_BUDGET) + }); + *ENV +} + /// Rate-distortion optimised quantisation (lane-rdoq): the pass every /// reference encoder runs after the quantiser and this one did not have. /// @@ -4602,18 +4628,35 @@ pub(crate) fn rdoq( if census { census_block(levels, scaled, side, coded, bits); } + // Where the pass may spend its prices. Every candidate here costs a whole + // `write_coeffs`, where rav1e's incremental model costs a table lookup, + // so both bounds exist to keep that affordable -- and both cut where the + // decisions do not pay anyway: + // * a level above [`RDOQ_MAX_LEVEL`] is never a candidate: one step off + // a big level gives back a fraction of a bit for a whole quantiser + // step of squared error. What pays is `1 -> 0` and `2 -> 1`. + // * at most [`RDOQ_BUDGET`] prices per block, spent from the TAIL + // backwards -- the eob end, where dropping a level also shortens the + // eob -- so a fine-quantizer block carrying two hundred coefficients + // cannot cost two hundred re-codings. + let mut budget = rdoq_budget(); + let max_level = rdoq_max_level(); for &position in scan_of(coded).iter().rev() { + if budget == 0 { + break; + } let (row, col) = (usize::from(position) / coded, usize::from(position) % coded); let i = row * side + col; - if levels[i] == 0 { + if levels[i] == 0 || levels[i].unsigned_abs() > max_level { continue; } let s = f64::from(scaled[i]); let weight = if i == 0 { dc_weight } else { 1.0 }; - loop { + while budget > 0 { let level = levels[i]; let candidate = level - level.signum(); levels[i] = candidate; + budget -= 1; let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx); let distortion = weight * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2)); diff --git a/lanes/rdoq-ctl.log b/lanes/rdoq-ctl.log index f10b6a11..5b01bd9f 100644 --- a/lanes/rdoq-ctl.log +++ b/lanes/rdoq-ctl.log @@ -18,3 +18,29 @@ bars 2160p: inter var-tx depths of 9166 blocks -- leaf single 0=1267 1=519 (29.1 bars 2160p: screen frames on=0 off=4 (0.0% on); palette blocks luma 0 chroma 0; intrabc blocks 0 (searched 0 / valid 0 / won 0) bars 2160p: filter intra on=true blocks 13065 (modes DC=3422 V=776 H=2756 D157=4292 PAETH=1819) | bars 2160p 1920x1024 | 43.68 dB/232087 B, 47.61 dB/341622 B, 51.20 dB/455971 B, 54.53 dB/583936 B | +12.7% | -9.8% | 43.1s:6.3s:20.9s | +film A (1080p source): sequence, pyramid requested Some(Pyramid { mini_gop: 8, arf_q_offset: -32, leaf_q_offset: 12, mid_q_offset: Some(-8), quarter_q_offset: None, key_q_offset: -48 }) effective Some(Pyramid { mini_gop: 8, arf_q_offset: -32, leaf_q_offset: 12, mid_q_offset: Some(-8), quarter_q_offset: None, key_q_offset: -48 }) +film A (1080p source): pricer armings default=20 real=213 +film A (1080p source): motion_mode 32x32 SIMPLE=41201 OBMC=0 WARP=1328 | 16x16 SIMPLE=20494 OBMC=0 WARP=1604 | 8x8 SIMPLE=4922 OBMC=0 WARP=846 (0.0% OBMC / 5.4% WARP of 70395 eligible) +film A (1080p source): inter var-tx depths of 86180 blocks -- leaf single 0=13762 1=2584 (15.8% split) | leaf compound 0=33584 1=6988 (17.2% split) | 32x32 single 0=7232 1=5556 (43.4% split) | 32x32 compound 0=10091 1=6383 (38.7% split) +film A (1080p source): screen frames on=0 off=4 (0.0% on); palette blocks luma 0 chroma 0; intrabc blocks 0 (searched 0 / valid 0 / won 0) +film A (1080p source): filter intra on=true blocks 24046 (modes DC=6056 V=3557 H=4424 D157=6925 PAETH=3084) +| film A (1080p source) 1920x768 | 44.24 dB/75308 B, 45.83 dB/125849 B, 47.06 dB/217800 B, 48.32 dB/519421 B | +37.1% | +7.3% | 77.1s:11.0s:16.9s | +film B (2160p HDR source): sequence, pyramid requested Some(Pyramid { mini_gop: 8, arf_q_offset: -32, leaf_q_offset: 12, mid_q_offset: Some(-8), quarter_q_offset: None, key_q_offset: -48 }) effective Some(Pyramid { mini_gop: 8, arf_q_offset: -32, leaf_q_offset: 12, mid_q_offset: Some(-8), quarter_q_offset: None, key_q_offset: -48 }) +film B (2160p HDR source): pricer armings default=20 real=190 +film B (2160p HDR source): motion_mode 32x32 SIMPLE=51492 OBMC=0 WARP=1137 | 16x16 SIMPLE=13701 OBMC=0 WARP=1016 | 8x8 SIMPLE=2084 OBMC=0 WARP=450 (0.0% OBMC / 3.7% WARP of 69880 eligible) +film B (2160p HDR source): inter var-tx depths of 75943 blocks -- leaf single 0=12882 1=2214 (14.7% split) | leaf compound 0=28716 1=5173 (15.3% split) | 32x32 single 0=7785 1=5849 (42.9% split) | 32x32 compound 0=8393 1=4931 (37.0% split) +film B (2160p HDR source): screen frames on=0 off=4 (0.0% on); palette blocks luma 0 chroma 0; intrabc blocks 0 (searched 0 / valid 0 / won 0) +film B (2160p HDR source): filter intra on=true blocks 18034 (modes DC=4989 V=2447 H=2570 D157=6251 PAETH=1777) +| film B (2160p HDR source) 1920x1024 | 46.74 dB/34523 B, 48.06 dB/70377 B, 49.16 dB/149444 B, 50.29 dB/398789 B | +52.5% | +22.3% | 78.1s:22.4s:16.4s | +screen capture (2026-07-25 02-09-04.mkv): sequence, pyramid requested Some(Pyramid { mini_gop: 8, arf_q_offset: -32, leaf_q_offset: 12, mid_q_offset: Some(-8), quarter_q_offset: None, key_q_offset: -48 }) effective None +screen capture (2026-07-25 02-09-04.mkv): pricer armings default=155 real=0 +screen capture (2026-07-25 02-09-04.mkv): motion_mode 32x32 SIMPLE=23654 OBMC=0 WARP=160 | 16x16 SIMPLE=1322 OBMC=0 WARP=318 | 8x8 SIMPLE=562 OBMC=0 WARP=576 (0.0% OBMC / 4.0% WARP of 26592 eligible) +screen capture (2026-07-25 02-09-04.mkv): inter var-tx depths of 34092 blocks -- leaf single 0=5772 1=7043 (55.0% split) | leaf compound 0=2826 1=2851 (50.2% split) | 32x32 single 0=6059 1=8017 (57.0% split) | 32x32 compound 0=860 1=664 (43.6% split) +screen capture (2026-07-25 02-09-04.mkv): screen frames on=48 off=0 (100.0% on); palette blocks luma 9907 chroma 1109; intrabc blocks 0 (searched 0 / valid 0 / won 0) +screen capture (2026-07-25 02-09-04.mkv): filter intra on=true blocks 8198 (modes DC=98 V=2410 H=5376 D157=223 PAETH=91) +| screen capture (2026-07-25 02-09-04.mkv) 1920x1024 | 45.87 dB/40155 B, 48.55 dB/51325 B, 50.86 dB/63998 B, 53.07 dB/80203 B | +33.4% | -23.8% | 45.1s:10.3s:14.4s | +ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 602 filtered out; finished in 457.99s + +RC=0 diff --git a/lanes/rdoq-l07.log b/lanes/rdoq-l07.log new file mode 100644 index 00000000..5d647e9f --- /dev/null +++ b/lanes/rdoq-l07.log @@ -0,0 +1,6 @@ + +running 1 test +test encode::tests::bd_rate_screen_native ... +native crop, 12 frames, gop=12; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128 +| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) | +|---|---|---|---|---| diff --git a/lanes/rdoq-r2.log b/lanes/rdoq-r2.log new file mode 100644 index 00000000..5d647e9f --- /dev/null +++ b/lanes/rdoq-r2.log @@ -0,0 +1,6 @@ + +running 1 test +test encode::tests::bd_rate_screen_native ... +native crop, 12 frames, gop=12; tiles (0, 0), tile threads 1, intrabc false, palette maxcolors 128 +| clip | ours PSNR/bytes per point | BD-rate vs libaom | BD-rate vs rav1e | wall ours:libaom:rav1e (noisy) | +|---|---|---|---|---|