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