libvpx speed control: no VP8 levels below 6, react to overload at once

The bench showed the adaptive `cpu-used` oscillating: with headroom at
-6 it stepped VP8 down to -5 and -4, which on screen content cost about
ten times the encode time for under 1 dB, the encoder fell behind, frames
were dropped, and it stepped back up. VP8 now stays within -10..-6. A
single frame that takes longer than the whole frame interval makes the
encoder faster at once, and when a slower level has to be undone, the
next attempt to go slower waits twice as long (up to 16x 15 frames).

The simulcast test also checks that `EncodedSource::set_layer_bitrate`
reaches exactly that layer's encoder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
Claude committed 2026-09-27 13:45:13 +00:00
1 parent 64a3b95742
commit 6187cc24de
3 files changed
+65 -15

No files matched your search

+13
View File
@@ -2004,6 +2004,19 @@ mod tests {
let frames = collect(&mut source, |f| f.iter().any(|f| f.layer == 5 && f.keyframe)).await;
assert!(!frames.iter().any(|f| f.layer == 0 && f.keyframe), "layer 0 got a keyframe");
// A viewer's estimate lowers layer 5's encoder bitrate only.
source.set_layer_bitrate(5, 150_000);
let target = |id| {
let stats = source.streamer().stats();
stats.layers.iter().find(|l| l.id == id).map(|l| l.bitrate)
};
let deadline = Instant::now() + Duration::from_secs(5);
while target(5) != Some(150_000) {
assert!(Instant::now() < deadline, "layer 5 at {:?}", target(5));
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert_eq!(target(0), Some(800_000));
// VP9, layer 5 gone, a new layer 7 at a fixed size.
let fixed = LayerSpec { size: Some((96, 64)), ..layer(7, 1.0, None, 200_000) };
let update = StreamerConfigUpdate {
+45 -11
View File
@@ -54,14 +54,19 @@ struct Levels {
start: i32,
}
// VP8 levels above 10 are no faster on screen content, only worse.
const VP8_LEVELS: Levels = Levels { slowest: 4, fastest: 10, start: 10 };
// On screen content (a probe with the desktop test pattern), VP8 levels
// above 10 were no faster, only about 3 dB worse, and levels below 6 about
// ten times slower for under 1 dB.
const VP8_LEVELS: Levels = Levels { slowest: 6, fastest: 10, start: 10 };
const VP9_LEVELS: Levels = Levels { slowest: 5, fastest: 9, start: 8 };
/// Adapts the speed level to how long frames take to encode compared to
/// the frame interval: faster above 75 % of it, slower (better quality)
/// below 35 %, deciding every 15 frames on a running mean of the encode
/// time (keyframes excluded: they are always slow).
/// the frame interval (keyframes excluded: they are always slow):
/// - faster at once when a single frame took more than the whole interval,
/// and when the running mean is above 75 % of it;
/// - slower (better quality) only when the mean stayed below 35 % for a
/// while: 15 frames, doubling (up to 16x) each time a slower level had to
/// be undone, so it does not oscillate around a level that is too slow.
pub struct SpeedControl {
level: i32,
slowest: i32,
@@ -69,6 +74,10 @@ pub struct SpeedControl {
/// Mean encode time in seconds.
mean: f64,
frames: u32,
/// Frames the mean must stay low before going slower.
patience: u32,
/// The last change went slower.
slowed: bool,
}
impl SpeedControl {
@@ -83,6 +92,8 @@ impl SpeedControl {
fastest: levels.fastest,
mean: 0.0,
frames: 0,
patience: Self::WINDOW,
slowed: false,
}
}
@@ -92,16 +103,22 @@ impl SpeedControl {
/// Account one encode; returns the new level if it changed.
pub fn update(&mut self, took: Duration, interval: Duration) -> Option<i32> {
if interval.is_zero() {
return None;
}
let took = took.as_secs_f64();
self.mean = if self.frames == 0 { took } else { self.mean * 0.8 + took * 0.2 };
self.frames += 1;
if self.frames < Self::WINDOW || interval.is_zero() {
return None;
}
let load = self.mean / interval.as_secs_f64();
let level = if load > Self::TOO_SLOW {
let budget = interval.as_secs_f64();
let load = self.mean / budget;
let faster = took > budget || (self.frames >= Self::WINDOW && load > Self::TOO_SLOW);
let level = if faster {
if self.slowed {
// Going slower was a mistake: wait longer next time.
self.patience = (self.patience * 2).min(Self::WINDOW * 16);
}
(self.level + 1).min(self.fastest)
} else if load < Self::TOO_FAST {
} else if self.frames >= self.patience && load < Self::TOO_FAST {
(self.level - 1).max(self.slowest)
} else {
self.level
@@ -109,6 +126,7 @@ impl SpeedControl {
if level == self.level {
return None;
}
self.slowed = level < self.level;
self.level = level;
self.frames = 0;
Some(level)
@@ -401,6 +419,22 @@ mod tests {
control.update(Duration::from_millis(30), interval);
}
assert_eq!(control.level(), VP8_LEVELS.fastest);
// One frame over the budget: faster at once.
let mut control = SpeedControl::new(&VP9_LEVELS);
assert_eq!(control.update(Duration::from_millis(40), interval), Some(9));
// A slower level that turns out too slow makes the next try wait.
let mut control = SpeedControl::new(&VP8_LEVELS);
let mut n = 0;
while control.update(Duration::from_millis(2), interval).is_none() {
n += 1;
}
assert_eq!((n, control.level()), (14, 9));
assert_eq!(control.update(Duration::from_millis(40), interval), Some(10));
let mut n = 0;
while control.update(Duration::from_millis(2), interval).is_none() {
n += 1;
}
assert_eq!(n, 29, "patience doubled");
// In between: stays.
let level = control.level();
for _ in 0..100 {
+7 -4
View File
@@ -154,10 +154,13 @@ VP9 row-mt, tile columns from the thread count and width (tiles are at least
256 pixels wide) and cyclic-refresh AQ, VP8 token partitions from the thread
count. Threads: all CPUs (or the streamer's share per layer), at most one per
320x240 pixels. `cpu-used` adapts to the measured encode time against the
frame interval (faster above 75 %, slower below 35 %, decided every 15
frames): VP8 starts at −10 and moves within −10..−4 (on screen content −12
and faster were no quicker and about 3 dB worse), VP9 starts at 8 within
5..9; `EncoderConfig::speed` fixes it instead. Frame durations for rate
frame interval (faster at once when one frame overruns the interval or the
mean is above 75 %; slower when the mean stayed below 35 % for 15 frames,
waiting twice as long after each slower step that had to be undone): VP8
starts at −10 and moves within −10..−6 (on screen content −12 and faster
were no quicker and about 3 dB worse, −5 and slower about ten times slower
for under 1 dB), VP9 starts at 8 within 5..9; `EncoderConfig::speed` fixes
it instead. Frame durations for rate
control follow the timestamps (variable frame rate), at least 3/4 of the
nominal interval. `set_bitrate` reconfigures the running encoder
(`vpx_codec_enc_config_set`). The C API is wrapped in