From c4dadc9da1b4f972d145444f503d8b9abb883bc2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 27 Sep 2026 08:44:26 +0000 Subject: [PATCH] libvpx: never budget a frame less than 3/4 of the frame interval Frame durations follow the timestamps (variable frame rate), but a frame whose timestamp had to be bumped to stay after the previous one got a duration of one 90 kHz tick. CBR rate control then gave it almost no bits, and it came out as a smear. This happens right after a keyframe is re-sent for a still screen (stamped "now") when the next capture carries an earlier timestamp; it showed up as a rare wrong-coloured pixel in the stream-task test under load. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G --- crates/voelin-media/src/codec/vpx.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/voelin-media/src/codec/vpx.rs b/crates/voelin-media/src/codec/vpx.rs index 73d30b5..cc83f18 100644 --- a/crates/voelin-media/src/codec/vpx.rs +++ b/crates/voelin-media/src/codec/vpx.rs @@ -199,19 +199,23 @@ impl VideoEncoder for VpxEncoder { let FrameData::I420 { y, u, v } = &i420.data else { unreachable!("to_i420 returns I420"); }; - // libvpx needs strictly increasing timestamps; the duration of a - // frame is the time since the previous one (rate control follows a - // variable frame rate). + // libvpx needs strictly increasing timestamps. The duration of a + // frame, from which CBR rate control budgets its bits, is the time + // since the previous one (a variable frame rate), but at least 3/4 + // of the nominal interval: a frame whose timestamp had to be bumped + // (e.g. after a keyframe re-sent while the screen was still) would + // otherwise get almost no bits and come out as a smear. let interval = VIDEO_CLOCK_RATE / u64::from(self.config.fps.max(1)); let mut pts = i420.pts_90khz() as i64; - let duration = match self.last_pts { + let elapsed = match self.last_pts { Some(last) if pts <= last => { pts = last + 1; - 1 + 0 } - Some(last) => ((pts - last) as u64).min(VIDEO_CLOCK_RATE), + Some(last) => (pts - last) as u64, None => interval, }; + let duration = elapsed.clamp(interval * 3 / 4, VIDEO_CLOCK_RATE); self.last_pts = Some(pts); let image = raw::I420 { width: i420.width,