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 commita8d48883636746b0b073fa261d0db72ba0d72c27Parent: ad62b231 changed file +53 -25
- crates/ec-h264/src/enc/mod.rs +53 -25
crates/ec-h264/src/enc/mod.rsmodified+53 -25
| @@ -306,7 +306,6 @@ impl Encoder { | ||
| 306 | 306 | pub fn encode(&mut self, frame: &PictureView<'_>) -> Result<EncodedPicture> { |
| 307 | 307 | self.encode_with(frame, false) |
| 308 | 308 | } |
| 309 | - | |
| 310 | 309 | /// [`Encoder::encode`], coding the picture as an IDR when `force_keyframe` |
| 311 | 310 | /// is set; the GOP restarts from it. |
| 312 | 311 | pub fn encode_with( |
| @@ -314,6 +313,12 @@ impl Encoder { | ||
| 314 | 313 | frame: &PictureView<'_>, |
| 315 | 314 | force_keyframe: bool, |
| 316 | 315 | ) -> 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> { | |
| 317 | 322 | if frame.width != self.cfg.width || frame.height != self.cfg.height { |
| 318 | 323 | return Err(Error::corrupt("frame size differs from the encoder's")); |
| 319 | 324 | } |
| @@ -335,13 +340,23 @@ impl Encoder { | ||
| 335 | 340 | self.frame_num = 0; |
| 336 | 341 | } |
| 337 | 342 | let slice_type = if idr { SliceType::I } else { SliceType::P }; |
| 338 | - | |
| 339 | 343 | let mb_w = self.sps.mb_width as usize; |
| 340 | 344 | let mb_h = self.sps.mb_height as usize; |
| 341 | 345 | let bands = split_bands(mb_h, self.threads); |
| 342 | 346 | let picture_id = self.next_id; |
| 343 | 347 | 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 | + } | |
| 344 | 358 | |
| 359 | + fn code_bands(&mut self, step: &FrameStep) -> Vec<Vec<u8>> { | |
| 345 | 360 | // Per-band coding. Every worker owns its picture and its bitstream; |
| 346 | 361 | // the frame's shared inputs (source, reference, parameters) travel in |
| 347 | 362 | // one `Arc`, and come back to the encoder once every band has answered. |
| @@ -350,20 +365,20 @@ impl Encoder { | ||
| 350 | 365 | cfg: self.cfg.clone(), |
| 351 | 366 | rc: self.rc.clone(), |
| 352 | 367 | 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, | |
| 356 | 371 | frame_num: self.frame_num, |
| 357 | - idr, | |
| 372 | + idr: step.idr, | |
| 358 | 373 | 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, | |
| 362 | 377 | cqo: self.cqo, |
| 363 | - mb_w, | |
| 378 | + mb_w: step.mb_w, | |
| 364 | 379 | }); |
| 365 | 380 | |
| 366 | - let mut answers = Vec::with_capacity(bands.len()); | |
| 381 | + let mut answers = Vec::with_capacity(step.bands.len()); | |
| 367 | 382 | if self.threads == 1 { |
| 368 | 383 | // One band: coded on this thread, with no workers. |
| 369 | 384 | let pic = std::mem::take(&mut self.workers[0]); |
| @@ -371,7 +386,7 @@ impl Encoder { | ||
| 371 | 386 | pic, |
| 372 | 387 | shared: Arc::clone(&shared), |
| 373 | 388 | band: 0, |
| 374 | - picture_id, | |
| 389 | + picture_id: step.picture_id, | |
| 375 | 390 | })); |
| 376 | 391 | } else { |
| 377 | 392 | let threads = self.threads; |
| @@ -385,15 +400,15 @@ impl Encoder { | ||
| 385 | 400 | pic: std::mem::take(worker), |
| 386 | 401 | shared: Arc::clone(&shared), |
| 387 | 402 | band, |
| 388 | - picture_id, | |
| 403 | + picture_id: step.picture_id, | |
| 389 | 404 | }, |
| 390 | 405 | ); |
| 391 | 406 | } |
| 392 | - for band in 0..bands.len() { | |
| 407 | + for band in 0..step.bands.len() { | |
| 393 | 408 | answers.push(pool.collect(band)); |
| 394 | 409 | } |
| 395 | 410 | } |
| 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()); | |
| 397 | 412 | for (band, answer) in answers.into_iter().enumerate() { |
| 398 | 413 | outputs.push(answer.rbsp); |
| 399 | 414 | self.workers[band] = answer.pic; |
| @@ -406,13 +421,16 @@ impl Encoder { | ||
| 406 | 421 | if let Some(reference) = shared.reference { |
| 407 | 422 | self.reference = reference; |
| 408 | 423 | } |
| 424 | + outputs | |
| 425 | + } | |
| 409 | 426 | |
| 427 | + fn finish_frame(&mut self, step: FrameStep, outputs: Vec<Vec<u8>>) -> EncodedPicture { | |
| 410 | 428 | // Stitch the bands into worker 0's picture, then filter the whole |
| 411 | 429 | // picture the way a decoder does. |
| 412 | 430 | let (first, rest) = self.workers.split_at_mut(1); |
| 413 | 431 | let master = &mut first[0]; |
| 414 | 432 | master.slices.clear(); |
| 415 | - for _ in 0..bands.len() { | |
| 433 | + for _ in 0..step.bands.len() { | |
| 416 | 434 | master.slices.push(DeblockParams { |
| 417 | 435 | disable_deblock_idc: 0, |
| 418 | 436 | alpha_offset: 0, |
| @@ -421,7 +439,7 @@ impl Encoder { | ||
| 421 | 439 | cr_qp_offset: self.cqo, |
| 422 | 440 | }); |
| 423 | 441 | } |
| 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()) { | |
| 425 | 443 | merge_band(master, worker, *band); |
| 426 | 444 | } |
| 427 | 445 | for &f in &master.mb_flags { |
| @@ -441,11 +459,11 @@ impl Encoder { | ||
| 441 | 459 | |
| 442 | 460 | // Assemble the access unit. |
| 443 | 461 | let mut au = Vec::with_capacity(outputs.iter().map(Vec::len).sum::<usize>() + 64); |
| 444 | - if idr { | |
| 462 | + if step.idr { | |
| 445 | 463 | au.extend_from_slice(&self.sps_nal); |
| 446 | 464 | au.extend_from_slice(&self.pps_nal); |
| 447 | 465 | } |
| 448 | - let unit = if idr { | |
| 466 | + let unit = if step.idr { | |
| 449 | 467 | NalUnitType::SliceIdr |
| 450 | 468 | } else { |
| 451 | 469 | NalUnitType::Slice |
| @@ -455,17 +473,17 @@ impl Encoder { | ||
| 455 | 473 | } |
| 456 | 474 | |
| 457 | 475 | let bits = au.len() as u64 * 8; |
| 458 | - self.rc.update(idr, qp, bits); | |
| 476 | + self.rc.update(step.idr, step.qp, bits); | |
| 459 | 477 | self.frames += 1; |
| 460 | 478 | self.frame_num = (self.frame_num + 1) % (1 << headers::LOG2_MAX_FRAME_NUM); |
| 461 | - if idr { | |
| 479 | + if step.idr { | |
| 462 | 480 | self.idr_pic_id ^= 1; |
| 463 | 481 | } |
| 464 | - Ok(EncodedPicture { | |
| 482 | + EncodedPicture { | |
| 465 | 483 | au, |
| 466 | - key_frame: idr, | |
| 467 | - qp, | |
| 468 | - }) | |
| 484 | + key_frame: step.idr, | |
| 485 | + qp: step.qp, | |
| 486 | + } | |
| 469 | 487 | } |
| 470 | 488 | |
| 471 | 489 | /// Copy the caller's planes into whole-macroblock buffers, replicating the |
| @@ -514,6 +532,16 @@ impl Encoder { | ||
| 514 | 532 | } |
| 515 | 533 | } |
| 516 | 534 | |
| 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 | + | |
| 517 | 545 | /// What every band of one picture reads. The workers share it through an `Arc`. |
| 518 | 546 | struct FrameShared { |
| 519 | 547 | sps: Sps, |