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 <noreply@anthropic.com>
This commit is contained in:
@@ -311,12 +311,7 @@ impl NvencEncoder {
|
|||||||
// hold frame size roughly constant and absorb motion as a momentary QP (quality) dip instead
|
// 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
|
// — 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).
|
// (larger = better motion quality but bigger per-frame bursts).
|
||||||
let vbv_frames = std::env::var("PUNKTFUNK_VBV_FRAMES")
|
let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env())
|
||||||
.ok()
|
|
||||||
.and_then(|s| s.parse::<f32>().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);
|
.clamp(1.0, i32::MAX as f64);
|
||||||
// SAFETY: `video` is the ffmpeg-next encoder builder wrapping a freshly-allocated
|
// 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
|
// `AVCodecContext` that we hold by value and have not opened yet; `video.as_mut_ptr()` returns
|
||||||
|
|||||||
@@ -195,13 +195,8 @@ unsafe fn open_vaapi_encoder_mode(
|
|||||||
video.set_frame_rate(Some(Rational(fps as i32, 1)));
|
video.set_frame_rate(Some(Rational(fps as i32, 1)));
|
||||||
video.set_bit_rate(bitrate_bps as usize);
|
video.set_bit_rate(bitrate_bps as usize);
|
||||||
video.set_max_bit_rate(bitrate_bps as usize); // == target → vaapi_encode picks CBR when supported
|
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")
|
let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env())
|
||||||
.ok()
|
.clamp(1.0, i32::MAX as f64);
|
||||||
.and_then(|s| s.parse::<f32>().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);
|
|
||||||
video.set_max_b_frames(0);
|
video.set_max_b_frames(0);
|
||||||
let raw = video.as_mut_ptr();
|
let raw = video.as_mut_ptr();
|
||||||
(*raw).rc_buffer_size = vbv_bits as i32;
|
(*raw).rc_buffer_size = vbv_bits as i32;
|
||||||
|
|||||||
@@ -191,13 +191,8 @@ unsafe fn open_win_encoder(
|
|||||||
video.set_frame_rate(Some(Rational(fps as i32, 1)));
|
video.set_frame_rate(Some(Rational(fps as i32, 1)));
|
||||||
video.set_bit_rate(bitrate_bps as usize);
|
video.set_bit_rate(bitrate_bps as usize);
|
||||||
video.set_max_bit_rate(bitrate_bps as usize); // target == max → CBR
|
video.set_max_bit_rate(bitrate_bps as usize); // target == max → CBR
|
||||||
let vbv_frames = std::env::var("PUNKTFUNK_VBV_FRAMES")
|
let vbv_bits = ((bitrate_bps as f64 / fps.max(1) as f64) * crate::encode::vbv_frames_env())
|
||||||
.ok()
|
.clamp(1.0, i32::MAX as f64);
|
||||||
.and_then(|s| s.parse::<f32>().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);
|
|
||||||
video.set_max_b_frames(0);
|
video.set_max_b_frames(0);
|
||||||
let raw = video.as_mut_ptr();
|
let raw = video.as_mut_ptr();
|
||||||
(*raw).rc_buffer_size = vbv_bits as i32;
|
(*raw).rc_buffer_size = vbv_bits as i32;
|
||||||
|
|||||||
Reference in New Issue
Block a user