forge
Explore
Log in

Don't you dare go Hollow

S8: Encoder::encode_with split into begin_frame, code_bands and finish_frame (same statements in the same order; FrameStep carries the frame decisions)

Browse files at this commit

a8d48883636746b0b073fa261d0db72ba0d72c27Ahmet Kaan GümüşParent: ad62b23
crates/ec-h264/src/enc/mod.rsmodified+53 -25
@@ -306,7 +306,6 @@ impl Encoder {
306306 pub fn encode(&mut self, frame: &PictureView<'_>) -> Result<EncodedPicture> {
307307 self.encode_with(frame, false)
308308 }
309-
310309 /// [`Encoder::encode`], coding the picture as an IDR when `force_keyframe`
311310 /// is set; the GOP restarts from it.
312311 pub fn encode_with(
@@ -314,6 +313,12 @@ impl Encoder {
314313 frame: &PictureView<'_>,
315314 force_keyframe: bool,
316315 ) -> Result<EncodedPicture> {
316+ let step = self.begin_frame(frame, force_keyframe)?;
317+ let outputs = self.code_bands(&step);
318+ Ok(self.finish_frame(step, outputs))
319+ }
320+
321+ fn begin_frame(&mut self, frame: &PictureView<'_>, force_keyframe: bool) -> Result<FrameStep> {
317322 if frame.width != self.cfg.width || frame.height != self.cfg.height {
318323 return Err(Error::corrupt("frame size differs from the encoder's"));
319324 }
@@ -335,13 +340,23 @@ impl Encoder {
335340 self.frame_num = 0;
336341 }
337342 let slice_type = if idr { SliceType::I } else { SliceType::P };
338-
339343 let mb_w = self.sps.mb_width as usize;
340344 let mb_h = self.sps.mb_height as usize;
341345 let bands = split_bands(mb_h, self.threads);
342346 let picture_id = self.next_id;
343347 self.next_id = self.next_id.wrapping_add(1);
348+ Ok(FrameStep {
349+ idr,
350+ qp,
351+ slice_type,
352+ mb_w,
353+ mb_h,
354+ bands,
355+ picture_id,
356+ })
357+ }
344358
359+ fn code_bands(&mut self, step: &FrameStep) -> Vec<Vec<u8>> {
345360 // Per-band coding. Every worker owns its picture and its bitstream;
346361 // the frame's shared inputs (source, reference, parameters) travel in
347362 // one `Arc`, and come back to the encoder once every band has answered.
@@ -350,20 +365,20 @@ impl Encoder {
350365 cfg: self.cfg.clone(),
351366 rc: self.rc.clone(),
352367 src: std::mem::take(&mut self.src),
353- reference: (!idr).then(|| std::mem::take(&mut self.reference)),
354- bands: bands.clone(),
355- slice_type,
368+ reference: (!step.idr).then(|| std::mem::take(&mut self.reference)),
369+ bands: step.bands.clone(),
370+ slice_type: step.slice_type,
356371 frame_num: self.frame_num,
357- idr,
372+ idr: step.idr,
358373 idr_pic_id: self.idr_pic_id,
359- qp,
360- target: self.rc.frame_target(idr),
361- total_mbs: (mb_w * mb_h) as f64,
374+ qp: step.qp,
375+ target: self.rc.frame_target(step.idr),
376+ total_mbs: (step.mb_w * step.mb_h) as f64,
362377 cqo: self.cqo,
363- mb_w,
378+ mb_w: step.mb_w,
364379 });
365380
366- let mut answers = Vec::with_capacity(bands.len());
381+ let mut answers = Vec::with_capacity(step.bands.len());
367382 if self.threads == 1 {
368383 // One band: coded on this thread, with no workers.
369384 let pic = std::mem::take(&mut self.workers[0]);
@@ -371,7 +386,7 @@ impl Encoder {
371386 pic,
372387 shared: Arc::clone(&shared),
373388 band: 0,
374- picture_id,
389+ picture_id: step.picture_id,
375390 }));
376391 } else {
377392 let threads = self.threads;
@@ -385,15 +400,15 @@ impl Encoder {
385400 pic: std::mem::take(worker),
386401 shared: Arc::clone(&shared),
387402 band,
388- picture_id,
403+ picture_id: step.picture_id,
389404 },
390405 );
391406 }
392- for band in 0..bands.len() {
407+ for band in 0..step.bands.len() {
393408 answers.push(pool.collect(band));
394409 }
395410 }
396- let mut outputs: Vec<Vec<u8>> = Vec::with_capacity(bands.len());
411+ let mut outputs: Vec<Vec<u8>> = Vec::with_capacity(step.bands.len());
397412 for (band, answer) in answers.into_iter().enumerate() {
398413 outputs.push(answer.rbsp);
399414 self.workers[band] = answer.pic;
@@ -406,13 +421,16 @@ impl Encoder {
406421 if let Some(reference) = shared.reference {
407422 self.reference = reference;
408423 }
424+ outputs
425+ }
409426
427+ fn finish_frame(&mut self, step: FrameStep, outputs: Vec<Vec<u8>>) -> EncodedPicture {
410428 // Stitch the bands into worker 0's picture, then filter the whole
411429 // picture the way a decoder does.
412430 let (first, rest) = self.workers.split_at_mut(1);
413431 let master = &mut first[0];
414432 master.slices.clear();
415- for _ in 0..bands.len() {
433+ for _ in 0..step.bands.len() {
416434 master.slices.push(DeblockParams {
417435 disable_deblock_idc: 0,
418436 alpha_offset: 0,
@@ -421,7 +439,7 @@ impl Encoder {
421439 cr_qp_offset: self.cqo,
422440 });
423441 }
424- for (band, worker) in bands.iter().skip(1).zip(rest.iter()) {
442+ for (band, worker) in step.bands.iter().skip(1).zip(rest.iter()) {
425443 merge_band(master, worker, *band);
426444 }
427445 for &f in &master.mb_flags {
@@ -441,11 +459,11 @@ impl Encoder {
441459
442460 // Assemble the access unit.
443461 let mut au = Vec::with_capacity(outputs.iter().map(Vec::len).sum::<usize>() + 64);
444- if idr {
462+ if step.idr {
445463 au.extend_from_slice(&self.sps_nal);
446464 au.extend_from_slice(&self.pps_nal);
447465 }
448- let unit = if idr {
466+ let unit = if step.idr {
449467 NalUnitType::SliceIdr
450468 } else {
451469 NalUnitType::Slice
@@ -455,17 +473,17 @@ impl Encoder {
455473 }
456474
457475 let bits = au.len() as u64 * 8;
458- self.rc.update(idr, qp, bits);
476+ self.rc.update(step.idr, step.qp, bits);
459477 self.frames += 1;
460478 self.frame_num = (self.frame_num + 1) % (1 << headers::LOG2_MAX_FRAME_NUM);
461- if idr {
479+ if step.idr {
462480 self.idr_pic_id ^= 1;
463481 }
464- Ok(EncodedPicture {
482+ EncodedPicture {
465483 au,
466- key_frame: idr,
467- qp,
468- })
484+ key_frame: step.idr,
485+ qp: step.qp,
486+ }
469487 }
470488
471489 /// Copy the caller's planes into whole-macroblock buffers, replicating the
@@ -514,6 +532,16 @@ impl Encoder {
514532 }
515533 }
516534
535+struct FrameStep {
536+ idr: bool,
537+ qp: i32,
538+ slice_type: SliceType,
539+ mb_w: usize,
540+ mb_h: usize,
541+ bands: Vec<(usize, usize)>,
542+ picture_id: i32,
543+}
544+
517545 /// What every band of one picture reads. The workers share it through an `Arc`.
518546 struct FrameShared {
519547 sps: Sps,