From abecb5226c39761e0523de79761a908a66cab971 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 16 Jul 2026 12:48:26 +0200 Subject: [PATCH] refactor(host): route the three libav backends' VBV re-parse through vbv_frames_env The libavcodec paths (Linux NVENC, VAAPI, Windows QSV) each re-parsed PUNKTFUNK_VBV_FRAMES locally in f32, duplicating and diverging in precision from the f64 vbv_frames_env() helper the direct-NVENC/AMF paths already use. Now that the helper lives in encode/codec.rs (532b313b), route all three through crate::encode::vbv_frames_env(): one parse, one precision, no drift. Behaviour-identical (same filter finite && > 0, same 1.0 default), f64 not f32. Verified: Linux cargo check green (linux/mod.rs + vaapi.rs compile); ffmpeg_win.rs is Windows-cfg and mirrors the amf.rs/nvenc.rs sites already using the helper. Co-Authored-By: Claude Fable 5 --- crates/punktfunk-host/src/encode/linux/mod.rs | 7 +------ crates/punktfunk-host/src/encode/linux/vaapi.rs | 9 ++------- crates/punktfunk-host/src/encode/windows/ffmpeg_win.rs | 9 ++------- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/crates/punktfunk-host/src/encode/linux/mod.rs b/crates/punktfunk-host/src/encode/linux/mod.rs index 4a592d66..d495c0be 100644 --- a/crates/punktfunk-host/src/encode/linux/mod.rs +++ b/crates/punktfunk-host/src/encode/linux/mod.rs @@ -311,12 +311,7 @@ impl NvencEncoder { // hold frame size roughly constant and absorb motion as a momentary QP (quality) dip instead // — the trade we want. Default = 1 frame of bits (bitrate/fps); PUNKTFUNK_VBV_FRAMES tunes it // (larger = better motion quality but bigger per-frame bursts). - let vbv_frames = std::env::var("PUNKTFUNK_VBV_FRAMES") - .ok() - .and_then(|s| s.parse::().ok()) - .filter(|v| v.is_finite() && *v > 0.0) - .unwrap_or(1.0); - let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * vbv_frames as f64) + let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env()) .clamp(1.0, i32::MAX as f64); // SAFETY: `video` is the ffmpeg-next encoder builder wrapping a freshly-allocated // `AVCodecContext` that we hold by value and have not opened yet; `video.as_mut_ptr()` returns diff --git a/crates/punktfunk-host/src/encode/linux/vaapi.rs b/crates/punktfunk-host/src/encode/linux/vaapi.rs index 5385b4f6..7a0903b1 100644 --- a/crates/punktfunk-host/src/encode/linux/vaapi.rs +++ b/crates/punktfunk-host/src/encode/linux/vaapi.rs @@ -195,13 +195,8 @@ unsafe fn open_vaapi_encoder_mode( video.set_frame_rate(Some(Rational(fps as i32, 1))); video.set_bit_rate(bitrate_bps as usize); video.set_max_bit_rate(bitrate_bps as usize); // == target → vaapi_encode picks CBR when supported - let vbv_frames = std::env::var("PUNKTFUNK_VBV_FRAMES") - .ok() - .and_then(|s| s.parse::().ok()) - .filter(|v| v.is_finite() && *v > 0.0) - .unwrap_or(1.0); - let vbv_bits = - ((bitrate_bps as f64 / fps.max(1) as f64) * vbv_frames as f64).clamp(1.0, i32::MAX as f64); + let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env()) + .clamp(1.0, i32::MAX as f64); video.set_max_b_frames(0); let raw = video.as_mut_ptr(); (*raw).rc_buffer_size = vbv_bits as i32; diff --git a/crates/punktfunk-host/src/encode/windows/ffmpeg_win.rs b/crates/punktfunk-host/src/encode/windows/ffmpeg_win.rs index ebbefd4a..39fd3da7 100644 --- a/crates/punktfunk-host/src/encode/windows/ffmpeg_win.rs +++ b/crates/punktfunk-host/src/encode/windows/ffmpeg_win.rs @@ -191,13 +191,8 @@ unsafe fn open_win_encoder( video.set_frame_rate(Some(Rational(fps as i32, 1))); video.set_bit_rate(bitrate_bps as usize); video.set_max_bit_rate(bitrate_bps as usize); // target == max → CBR - let vbv_frames = std::env::var("PUNKTFUNK_VBV_FRAMES") - .ok() - .and_then(|s| s.parse::().ok()) - .filter(|v| v.is_finite() && *v > 0.0) - .unwrap_or(1.0); - let vbv_bits = - ((bitrate_bps as f64 / fps.max(1) as f64) * vbv_frames as f64).clamp(1.0, i32::MAX as f64); + let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env()) + .clamp(1.0, i32::MAX as f64); video.set_max_b_frames(0); let raw = video.as_mut_ptr(); (*raw).rc_buffer_size = vbv_bits as i32;