Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions fframes/src/renderer/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ pub struct EncoderOptions<'a> {
/// Size of group of picture
/// @default 12
pub gop_size: i32,
/// The shortest part of the video encoded as a separate encoder run (see
/// [`EncoderOptions::min_segment_frames`]). `None` keeps parts at most a second long so
/// they encode in parallel.
///
/// Every part is a fresh encoder: rate control that spans frames, such as an x264
/// bitrate cap (`maxrate` + `bufsize`), starts over in each one, so a cap holds only
/// within a part. Set this to the video's length in frames (or more) to encode it in
/// one run when a cap must hold across the whole file.
/// @default `None`
pub segment_frames: Option<usize>,
/// Specify the resulting sample rate of the audio stream (ignored for video streams)
/// @default 44100
pub sample_rate: usize,
Expand Down Expand Up @@ -193,9 +203,12 @@ impl EncoderOptions<'_> {
/// to make sure that individual chunks are never less than 2xGOP size.
/// The shortest part of a video the renderers encode separately. Every part starts
/// with a keyframe, so it is never shorter than a GOP or than a second of video,
/// whichever is smaller.
/// whichever is smaller, unless [`EncoderOptions::segment_frames`] sets it.
pub fn min_segment_frames(&self, fps: usize) -> usize {
(self.gop_size.max(1) as usize).min(fps.max(1))
match self.segment_frames {
Some(frames) => frames.max(1),
None => (self.gop_size.max(1) as usize).min(fps.max(1)),
}
}

pub fn split_gop_chunks(
Expand Down Expand Up @@ -241,6 +254,7 @@ impl Default for EncoderOptions<'_> {
color_matrix: YuvMatrix::Bt601,
codec_params: None,
gop_size: 24,
segment_frames: None,
max_qdiff: 4,
pixel_format: AVPixelFormat::AV_PIX_FMT_YUV420P,
preferred_encoder: None,
Expand Down
14 changes: 14 additions & 0 deletions fframes/src/renderer/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ mod tests {
assert_eq!(tiny.claim(0), None);
}

#[test]
fn a_minimum_of_the_whole_video_makes_one_segment() {
// EncoderOptions::segment_frames set to the length: one encoder run for the file
let scheduler = FrameScheduler::new(1200, 8, 1200);
let frames: Vec<_> = std::iter::from_fn(|| scheduler.claim(0)).collect();
assert_eq!(frames.len(), 1200);
assert!(frames.iter().all(|claim| claim.segment == 0));
assert!(frames[1199].last_in_segment);
assert_eq!(
frames.iter().filter(|claim| claim.last_in_segment).count(),
1
);
}

#[test]
fn every_frame_is_claimed_once_and_segments_are_contiguous() {
for (total, workers, min) in [(1000, 4, 10), (97, 16, 24), (48, 3, 24), (5000, 16, 24)] {
Expand Down