voelin-media: bitrate increases reach encoders that cannot change it running

VA-API, AMF, SVT-AV1 and the other wrappers without a live bitrate change
applied a new target only at the next keyframe, so a still screen kept
the bandwidth estimate's low start value for good. An increase of half or
more now reopens the session (a keyframe) once it is five seconds old;
smaller ones still wait for a keyframe, drops below half apply at once.

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-29 01:13:44 +00:00
1 parent 0844d35411
commit 0924cfcc82
3 files changed
+50 -4

No files matched your search

+20 -2
View File
@@ -13,7 +13,7 @@
use std::collections::VecDeque;
use std::ffi::c_int;
use std::sync::OnceLock;
use std::time::Duration;
use std::time::{Duration, Instant};
use super::layout::{self, HwFramesFields};
use super::sys::{self, FrameHead, PacketHead, Ptr, Rational, cstr};
@@ -451,6 +451,9 @@ struct Session {
/// No packet came out yet: the first one starts the stream, so it is a
/// keyframe even where the wrapper does not flag it (rav1e).
first_packet: bool,
/// When it was opened (bitrate increases reopen at most every
/// [`REOPEN_FOR_BITRATE`]).
opened: Instant,
timestamps: Timestamps,
nv12: bool,
}
@@ -475,6 +478,11 @@ impl Drop for Session {
}
}
/// How long a session runs before a bitrate increase of half or more
/// reopens it (a keyframe) on backends that cannot change the bitrate
/// running; smaller increases wait for the next keyframe.
const REOPEN_FOR_BITRATE: Duration = Duration::from_secs(5);
/// When to recreate the session.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Reinit {
@@ -630,6 +638,7 @@ impl FfmpegEncoder {
packet: std::ptr::null_mut(),
last_pts: None,
first_packet: true,
opened: Instant::now(),
timestamps: Timestamps::new(),
nv12: self.spec.input != Input::Yuv420p,
};
@@ -1177,7 +1186,9 @@ impl VideoEncoder for FfmpegEncoder {
fn set_bitrate(&mut self, bps: u32) -> Result<()> {
let bps = bps.max(1);
self.config.bitrate_bps = bps;
let Some((ctx, running)) = self.session.as_ref().map(|s| (s.ctx, s.bitrate)) else {
let Some((ctx, running, opened)) =
self.session.as_ref().map(|s| (s.ctx, s.bitrate, s.opened))
else {
return Ok(());
};
if running == bps {
@@ -1198,6 +1209,13 @@ impl VideoEncoder for FfmpegEncoder {
} else if u64::from(bps) * 2 < u64::from(running) {
// Congestion: waiting for a keyframe would keep overshooting.
self.reinit = Reinit::Now;
} else if u64::from(bps) * 2 > u64::from(running) * 3
&& opened.elapsed() >= REOPEN_FOR_BITRATE
{
// Much more room (the estimate ramping up after the start): a
// screen that never asks for a keyframe would otherwise keep
// the start bitrate.
self.reinit = Reinit::Now;
} else {
self.reinit = self.reinit.max(Reinit::AtKeyframe);
}
+26
View File
@@ -229,6 +229,32 @@ fn unusable_backends_fail_cleanly() {
}
}
/// A backend whose wrapper cannot change the bitrate running (libaom here,
/// VA-API alike) waits for the next keyframe, but reopens at once when the
/// target falls below half.
#[test]
fn bitrate_changes_without_live_reconfiguration() {
if ffmpeg().is_none() || !available("libaom-av1") {
return;
}
let screen = SyntheticScreen::new(W, H);
let config = EncoderConfig { fps: 30, bitrate_bps: 1_000_000, ..EncoderConfig::default() };
let mut encoder = FfmpegEncoder::new("libaom-av1", config).unwrap();
let mut key = |encoder: &mut FfmpegEncoder, n: u64, force: bool| {
encoder.encode(&screen.frame(n, 30), force).unwrap().iter().any(|f| f.keyframe)
};
assert!(key(&mut encoder, 0, false));
assert!(!key(&mut encoder, 1, false));
encoder.set_bitrate(800_000).unwrap();
assert!(!key(&mut encoder, 2, false), "a small change waits for a keyframe");
encoder.set_bitrate(300_000).unwrap();
assert!(key(&mut encoder, 3, false), "congestion: at once");
encoder.set_bitrate(400_000).unwrap();
assert!(!key(&mut encoder, 4, false));
assert!(key(&mut encoder, 5, true));
assert!(!key(&mut encoder, 6, false));
}
/// DMA-BUF import is for VA-API encoders and NV12 buffers; anything else is
/// refused as unavailable, so the caller maps the buffer instead.
#[cfg(target_os = "linux")]
+4 -2
View File
@@ -324,8 +324,10 @@ the software encoders above are used as before.
are skipped; alternatives are tried in order.
- Runtime changes: x264, NVENC and Quick Sync take bitrate changes on the
running encoder (FFmpeg's wrappers compare `b` / `maxrate` / `bufsize`
before each frame); the others reopen at the next keyframe, or at once
when the target falls below half (congestion). A new frame rate or size
before each frame); the others reopen at the next keyframe, at once
when the target falls below half (congestion), and when it rose by half
or more and the session is 5 s old (the estimate ramping up after the
start would otherwise never reach the encoder). A new frame rate or size
opens a new session (keyframe); the old one is flushed first, so frames an
encoder with a delay still held come out.
- Buffers: per session one software `AVFrame` (`av_frame_get_buffer`,