From e05fd44f2264936b46a4d4609ef254610e488f4b Mon Sep 17 00:00:00 2001 From: ferstar Date: Thu, 15 Oct 2026 03:31:56 +0800 Subject: [PATCH] Feed NVENC BGR0 capture frames directly The GPU path skipped the CPU colour conversion to YUV420P: frames already in BGR0 at the encoder size go straight to h264_nvenc, other sizes are scaled to BGR0. The libx264 path keeps YUV420P. Probe: 300 frames cut CPU from ~2.4 s to ~0.8 s with the same bitrate. --- src/inproc_encoder.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/inproc_encoder.rs b/src/inproc_encoder.rs index 23427a6..6c17052 100644 --- a/src/inproc_encoder.rs +++ b/src/inproc_encoder.rs @@ -87,7 +87,9 @@ pub fn run(config: Config, units: mpsc::Sender>, stop: Arc, let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?; context.set_width(width); context.set_height(height); - context.set_format(Pixel::YUV420P); + // NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P. + let pixel = if config.gpu { Pixel::BGRZ } else { Pixel::YUV420P }; + context.set_format(pixel); context.set_time_base(Rational::new(1, config.fps as i32)); context.set_frame_rate(Some(Rational::new(config.fps as i32, 1))); context.set_gop(config.gop); @@ -121,7 +123,7 @@ pub fn run(config: Config, units: mpsc::Sender>, stop: Arc, let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None; let mut decoded = frame::Video::empty(); - let mut yuv = frame::Video::new(Pixel::YUV420P, width, height); + let mut converted = frame::Video::new(pixel, width, height); let mut packet = Packet::empty(); let started = Instant::now(); let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps)); @@ -146,16 +148,22 @@ pub fn run(config: Config, units: mpsc::Sender>, stop: Arc, let slot = slot.max(last_slot + 1); last_slot = slot; - let key = (decoded.format(), decoded.width(), decoded.height()); - if scaler.as_ref().map(|(_, k)| *k) != Some(key) { - let context = scaling::Context::get(key.0, key.1, key.2, Pixel::YUV420P, width, height, scaling::Flags::BILINEAR).map_err(text)?; - scaler = Some((context, key)); - } - if let Some((context, _)) = scaler.as_mut() { context.run(&decoded, &mut yuv).map_err(text)?; } + // Pass the capture buffer straight through when it already matches the encoder input. + let frame = if decoded.format() == pixel && decoded.width() == width && decoded.height() == height { + &mut decoded + } else { + let key = (decoded.format(), decoded.width(), decoded.height()); + if scaler.as_ref().map(|(_, k)| *k) != Some(key) { + let context = scaling::Context::get(key.0, key.1, key.2, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?; + scaler = Some((context, key)); + } + if let Some((context, _)) = scaler.as_mut() { context.run(&decoded, &mut converted).map_err(text)?; } + &mut converted + }; - yuv.set_pts(Some(slot)); - yuv.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None }); - encoder.send_frame(&yuv).map_err(text)?; + frame.set_pts(Some(slot)); + frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None }); + encoder.send_frame(frame).map_err(text)?; if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); } } Ok(())