From db2a8962d9f287cd07a0332d2ac6217c479553cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:17:58 +0300 Subject: [PATCH] S2: VA Encoder::stage(EncFrame) validates, acquires and uploads for all four entry points; the bit-depth and size checks are one copy each --- crates/ec-hw/src/enc/mod.rs | 69 ++++++++++++++----------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index 9f2ed5b9..7f52416c 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -108,7 +108,11 @@ struct InFlight { keyframe: bool, } -/// A hardware encoder. +enum EncFrame<'a> { + I420(&'a I420), + I420_16(&'a I420_16), +} + /// A hardware encoder. pub struct Encoder { /// The GPU reset epoch this encoder was created in. @@ -275,36 +279,14 @@ impl Encoder { "encode: pipelined frames pending; call flush first", )); } - if self.config.bit_depth != 8 { - return Err(Error::config(format!( - "encode: this encoder is configured for {} bits; call encode_16", - self.config.bit_depth - ))); - } - self.check_size(frame.width, frame.height)?; - let input = self - .pool - .acquire() - .ok_or_else(|| Error::config("encode: every surface is still in use"))?; - upload(&self.context, frame, input.surface(), self.coded_size)?; + let input = self.stage(EncFrame::I420(frame))?; self.encode_settled(Rc::clone(input.surface()), meta) } /// Submit one frame without waiting for it; the oldest pending frame comes back /// once more than [`DEPTH`] are in flight. pub fn submit(&mut self, frame: &I420, meta: FrameMetadata) -> Result> { - if self.config.bit_depth != 8 { - return Err(Error::config(format!( - "submit: this encoder is configured for {} bits; call submit_16", - self.config.bit_depth - ))); - } - self.check_size(frame.width, frame.height)?; - let input = self - .pool - .acquire() - .ok_or_else(|| Error::config("submit: every surface is still in use"))?; - upload(&self.context, frame, input.surface(), self.coded_size)?; + let input = self.stage(EncFrame::I420(frame))?; self.push_pipelined(input, meta) } @@ -314,18 +296,7 @@ impl Encoder { frame: &I420_16, meta: FrameMetadata, ) -> Result> { - if self.config.bit_depth != 10 { - return Err(Error::config(format!( - "submit_16: this encoder is configured for {} bits; call submit", - self.config.bit_depth - ))); - } - self.check_size(frame.width, frame.height)?; - let input = self - .pool - .acquire() - .ok_or_else(|| Error::config("submit_16: every surface is still in use"))?; - upload_16(&self.context, frame, input.surface(), self.coded_size)?; + let input = self.stage(EncFrame::I420_16(frame))?; self.push_pipelined(input, meta) } @@ -370,19 +341,31 @@ impl Encoder { "encode_16: pipelined frames pending; call flush first", )); } - if self.config.bit_depth != 10 { + let input = self.stage(EncFrame::I420_16(frame))?; + self.encode_settled(Rc::clone(input.surface()), meta) + } + + fn stage(&mut self, frame: EncFrame) -> Result> { + let (bits, width, height) = match frame { + EncFrame::I420(f) => (8, f.width, f.height), + EncFrame::I420_16(f) => (10, f.width, f.height), + }; + if self.config.bit_depth != bits { return Err(Error::config(format!( - "encode_16: this encoder is configured for {} bits; call encode", + "this encoder is configured for {} bits", self.config.bit_depth ))); } - self.check_size(frame.width, frame.height)?; + self.check_size(width, height)?; let input = self .pool .acquire() - .ok_or_else(|| Error::config("encode: every surface is still in use"))?; - upload_16(&self.context, frame, input.surface(), self.coded_size)?; - self.encode_settled(Rc::clone(input.surface()), meta) + .ok_or_else(|| Error::config("every surface is still in use"))?; + match frame { + EncFrame::I420(f) => upload(&self.context, f, input.surface(), self.coded_size)?, + EncFrame::I420_16(f) => upload_16(&self.context, f, input.surface(), self.coded_size)?, + } + Ok(input) } fn check_size(&self, width: u32, height: u32) -> Result<()> {