forge
Explore
Log in

Don't you dare go Hollow

S2: VA Encoder::stage(EncFrame) validates, acquires and uploads for all four entry points; the bit-depth and size checks are one copy each

Browse files at this commit

db2a8962d9f287cd07a0332d2ac6217c479553cdAhmet Kaan GümüşParent: d52a168
crates/ec-hw/src/enc/mod.rsmodified+26 -43
@@ -108,7 +108,11 @@ struct InFlight<D: SurfaceMemoryDescriptor> {
108108 keyframe: bool,
109109 }
110110
111-/// A hardware encoder.
111+enum EncFrame<'a> {
112+ I420(&'a I420),
113+ I420_16(&'a I420_16),
114+}
115+
112116 /// A hardware encoder.
113117 pub struct Encoder {
114118 /// The GPU reset epoch this encoder was created in.
@@ -275,36 +279,14 @@ impl Encoder {
275279 "encode: pipelined frames pending; call flush first",
276280 ));
277281 }
278- if self.config.bit_depth != 8 {
279- return Err(Error::config(format!(
280- "encode: this encoder is configured for {} bits; call encode_16",
281- self.config.bit_depth
282- )));
283- }
284- self.check_size(frame.width, frame.height)?;
285- let input = self
286- .pool
287- .acquire()
288- .ok_or_else(|| Error::config("encode: every surface is still in use"))?;
289- upload(&self.context, frame, input.surface(), self.coded_size)?;
282+ let input = self.stage(EncFrame::I420(frame))?;
290283 self.encode_settled(Rc::clone(input.surface()), meta)
291284 }
292285
293286 /// Submit one frame without waiting for it; the oldest pending frame comes back
294287 /// once more than [`DEPTH`] are in flight.
295288 pub fn submit(&mut self, frame: &I420, meta: FrameMetadata) -> Result<Option<CodedFrame>> {
296- if self.config.bit_depth != 8 {
297- return Err(Error::config(format!(
298- "submit: this encoder is configured for {} bits; call submit_16",
299- self.config.bit_depth
300- )));
301- }
302- self.check_size(frame.width, frame.height)?;
303- let input = self
304- .pool
305- .acquire()
306- .ok_or_else(|| Error::config("submit: every surface is still in use"))?;
307- upload(&self.context, frame, input.surface(), self.coded_size)?;
289+ let input = self.stage(EncFrame::I420(frame))?;
308290 self.push_pipelined(input, meta)
309291 }
310292
@@ -314,18 +296,7 @@ impl Encoder {
314296 frame: &I420_16,
315297 meta: FrameMetadata,
316298 ) -> Result<Option<CodedFrame>> {
317- if self.config.bit_depth != 10 {
318- return Err(Error::config(format!(
319- "submit_16: this encoder is configured for {} bits; call submit",
320- self.config.bit_depth
321- )));
322- }
323- self.check_size(frame.width, frame.height)?;
324- let input = self
325- .pool
326- .acquire()
327- .ok_or_else(|| Error::config("submit_16: every surface is still in use"))?;
328- upload_16(&self.context, frame, input.surface(), self.coded_size)?;
299+ let input = self.stage(EncFrame::I420_16(frame))?;
329300 self.push_pipelined(input, meta)
330301 }
331302
@@ -370,19 +341,31 @@ impl Encoder {
370341 "encode_16: pipelined frames pending; call flush first",
371342 ));
372343 }
373- if self.config.bit_depth != 10 {
344+ let input = self.stage(EncFrame::I420_16(frame))?;
345+ self.encode_settled(Rc::clone(input.surface()), meta)
346+ }
347+
348+ fn stage(&mut self, frame: EncFrame) -> Result<Rc<PooledSurface>> {
349+ let (bits, width, height) = match frame {
350+ EncFrame::I420(f) => (8, f.width, f.height),
351+ EncFrame::I420_16(f) => (10, f.width, f.height),
352+ };
353+ if self.config.bit_depth != bits {
374354 return Err(Error::config(format!(
375- "encode_16: this encoder is configured for {} bits; call encode",
355+ "this encoder is configured for {} bits",
376356 self.config.bit_depth
377357 )));
378358 }
379- self.check_size(frame.width, frame.height)?;
359+ self.check_size(width, height)?;
380360 let input = self
381361 .pool
382362 .acquire()
383- .ok_or_else(|| Error::config("encode: every surface is still in use"))?;
384- upload_16(&self.context, frame, input.surface(), self.coded_size)?;
385- self.encode_settled(Rc::clone(input.surface()), meta)
363+ .ok_or_else(|| Error::config("every surface is still in use"))?;
364+ match frame {
365+ EncFrame::I420(f) => upload(&self.context, f, input.surface(), self.coded_size)?,
366+ EncFrame::I420_16(f) => upload_16(&self.context, f, input.surface(), self.coded_size)?,
367+ }
368+ Ok(input)
386369 }
387370
388371 fn check_size(&self, width: u32, height: u32) -> Result<()> {