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> {
108 keyframe: bool,108 keyframe: bool,
109 }109 }
110 110
111-/// A hardware encoder.111+enum EncFrame<'a> {
112+ I420(&'a I420),
113+ I420_16(&'a I420_16),
114+}
115+
112 /// A hardware encoder.116 /// A hardware encoder.
113 pub struct Encoder {117 pub struct Encoder {
114 /// The GPU reset epoch this encoder was created in.118 /// The GPU reset epoch this encoder was created in.
@@ -275,36 +279,14 @@ impl Encoder {
275 "encode: pipelined frames pending; call flush first",279 "encode: pipelined frames pending; call flush first",
276 ));280 ));
277 }281 }
278- if self.config.bit_depth != 8 {282+ let input = self.stage(EncFrame::I420(frame))?;
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)?;
290 self.encode_settled(Rc::clone(input.surface()), meta)283 self.encode_settled(Rc::clone(input.surface()), meta)
291 }284 }
292 285
293 /// Submit one frame without waiting for it; the oldest pending frame comes back286 /// Submit one frame without waiting for it; the oldest pending frame comes back
294 /// once more than [`DEPTH`] are in flight.287 /// once more than [`DEPTH`] are in flight.
295 pub fn submit(&mut self, frame: &I420, meta: FrameMetadata) -> Result<Option<CodedFrame>> {288 pub fn submit(&mut self, frame: &I420, meta: FrameMetadata) -> Result<Option<CodedFrame>> {
296- if self.config.bit_depth != 8 {289+ let input = self.stage(EncFrame::I420(frame))?;
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)?;
308 self.push_pipelined(input, meta)290 self.push_pipelined(input, meta)
309 }291 }
310 292
@@ -314,18 +296,7 @@ impl Encoder {
314 frame: &I420_16,296 frame: &I420_16,
315 meta: FrameMetadata,297 meta: FrameMetadata,
316 ) -> Result<Option<CodedFrame>> {298 ) -> Result<Option<CodedFrame>> {
317- if self.config.bit_depth != 10 {299+ let input = self.stage(EncFrame::I420_16(frame))?;
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)?;
329 self.push_pipelined(input, meta)300 self.push_pipelined(input, meta)
330 }301 }
331 302
@@ -370,19 +341,31 @@ impl Encoder {
370 "encode_16: pipelined frames pending; call flush first",341 "encode_16: pipelined frames pending; call flush first",
371 ));342 ));
372 }343 }
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 {
374 return Err(Error::config(format!(354 return Err(Error::config(format!(
375- "encode_16: this encoder is configured for {} bits; call encode",355+ "this encoder is configured for {} bits",
376 self.config.bit_depth356 self.config.bit_depth
377 )));357 )));
378 }358 }
379- self.check_size(frame.width, frame.height)?;359+ self.check_size(width, height)?;
380 let input = self360 let input = self
381 .pool361 .pool
382 .acquire()362 .acquire()
383- .ok_or_else(|| Error::config("encode: every surface is still in use"))?;363+ .ok_or_else(|| Error::config("every surface is still in use"))?;
384- upload_16(&self.context, frame, input.surface(), self.coded_size)?;364+ match frame {
385- self.encode_settled(Rc::clone(input.surface()), meta)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)
386 }369 }
387 370
388 fn check_size(&self, width: u32, height: u32) -> Result<()> {371 fn check_size(&self, width: u32, height: u32) -> Result<()> {