Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(enc-ffmpeg): advance non-monotonic video pts by a single tick
Richie McIlroy971d1d5ba7fd
diff --git a/crates/enc-ffmpeg/src/base.rs b/crates/enc-ffmpeg/src/base.rsindex ab0f5dc1a..2edb1226f 100644--- a/crates/enc-ffmpeg/src/base.rs+++ b/crates/enc-ffmpeg/src/base.rs@@ -48,12 +48,7 @@ impl EncoderBase {let pts = (timestamp.as_secs_f64() * rate).round() as i64;let first_pts = self.first_pts.get_or_insert(pts);- let pts = normalize_input_pts(- pts - *first_pts,- self.last_frame_pts,- encoder.time_base(),- encoder.frame_rate(),- );+ let pts = normalize_input_pts(pts - *first_pts, self.last_frame_pts);self.last_frame_pts = Some(pts);frame.set_pts(Some(pts));} else {@@ -64,12 +59,7 @@ impl EncoderBase {let first_pts = self.first_pts.get_or_insert(pts);- let pts = normalize_input_pts(- pts - *first_pts,- self.last_frame_pts,- encoder.time_base(),- encoder.frame_rate(),- );+ let pts = normalize_input_pts(pts - *first_pts, self.last_frame_pts);self.last_frame_pts = Some(pts);frame.set_pts(Some(pts));}@@ -223,12 +213,7 @@ fn nominal_packet_duration(time_base: Rational, frame_rate: Rational) -> Option<.filter(|ticks| *ticks > 0)}-fn normalize_input_pts(- pts: i64,- last_pts: Option<i64>,- time_base: Rational,- frame_rate: Rational,-) -> i64 {+fn normalize_input_pts(pts: i64, last_pts: Option<i64>) -> i64 {let Some(last_pts) = last_pts else {return pts;};@@ -237,5 +222,31 @@ fn normalize_input_pts(return pts;}- last_pts + nominal_packet_duration(time_base, frame_rate).unwrap_or(1)+ // A tie or backwards timestamp carries no time — advance a single tick+ // so pts stay strictly monotonic and the real timestamps immediately+ // take over again. Any larger bump outruns a source delivering faster+ // than the nominal rate (each corrected frame pushes the next one into+ // correction too), re-timing the recording to the bump cadence.+ last_pts + 1+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn normalize_input_pts_passes_monotonic_input_through() {+ // Any strictly-forward delta passes untouched, even sub-frame ones+ // from sources far faster than the nominal rate (1000fps = 1ms).+ assert_eq!(normalize_input_pts(16_667, Some(0)), 16_667);+ assert_eq!(normalize_input_pts(1_000, Some(0)), 1_000);+ }++ #[test]+ fn normalize_input_pts_bumps_non_monotonic_input_by_one_tick() {+ // Any larger bump outruns a fast source: each corrected frame pushes+ // the next into correction too, re-timing the whole recording.+ assert_eq!(normalize_input_pts(90_000, Some(100_000)), 100_001);+ assert_eq!(normalize_input_pts(100_000, Some(100_000)), 100_001);+ }}diff --git a/crates/enc-ffmpeg/src/video/h264_packet.rs b/crates/enc-ffmpeg/src/video/h264_packet.rsindex 51cded8c6..decf625ac 100644--- a/crates/enc-ffmpeg/src/video/h264_packet.rs+++ b/crates/enc-ffmpeg/src/video/h264_packet.rs@@ -207,14 +207,12 @@ impl H264PacketEncoder {let rate = tb.denominator() as f64 / tb.numerator() as f64;let pts = (timestamp.as_secs_f64() * rate).round() as i64;let first_pts = *self.first_pts.get_or_insert(pts);- let pts =- normalize_input_pts(pts - first_pts, self.last_frame_pts, tb, self.frame_rate);+ let pts = normalize_input_pts(pts - first_pts, self.last_frame_pts);self.last_frame_pts = Some(pts);frame.set_pts(Some(pts));} else if let Some(pts) = frame.pts() {let first_pts = *self.first_pts.get_or_insert(pts);- let pts =- normalize_input_pts(pts - first_pts, self.last_frame_pts, tb, self.frame_rate);+ let pts = normalize_input_pts(pts - first_pts, self.last_frame_pts);self.last_frame_pts = Some(pts);frame.set_pts(Some(pts));} else {@@ -272,12 +270,7 @@ impl H264PacketEncoder {}}-fn normalize_input_pts(- pts: i64,- last_pts: Option<i64>,- time_base: Rational,- frame_rate: Rational,-) -> i64 {+fn normalize_input_pts(pts: i64, last_pts: Option<i64>) -> i64 {let Some(last_pts) = last_pts else {return pts;};@@ -286,26 +279,14 @@ fn normalize_input_pts(return pts;}- last_pts + nominal_packet_duration(time_base, frame_rate).unwrap_or(1)+ // A tie or backwards timestamp carries no time — advance a single tick+ // so pts stay strictly monotonic and the real timestamps immediately+ // take over again. Any larger bump outruns a source delivering faster+ // than the nominal rate (each corrected frame pushes the next one into+ // correction too), re-timing the recording to the bump cadence.+ last_pts + 1}-fn nominal_packet_duration(time_base: Rational, frame_rate: Rational) -> Option<i64> {- let time_base_num = time_base.numerator();- let time_base_den = time_base.denominator();- let frame_rate_num = frame_rate.numerator();- let frame_rate_den = frame_rate.denominator();-- if time_base_num <= 0 || time_base_den <= 0 || frame_rate_num <= 0 || frame_rate_den <= 0 {- return None;- }-- let ticks = (frame_rate_den as f64 * time_base_den as f64)- / (frame_rate_num as f64 * time_base_num as f64);- ticks- .is_finite()- .then(|| ticks.round() as i64)- .filter(|ticks| *ticks > 0)-}#[cfg(test)]mod tests {@@ -313,6 +294,22 @@ mod tests {use crate::video::h264::{H264EncoderBuilder, H264Preset};use cap_media_info::{Pixel, VideoInfo};+ #[test]+ fn normalize_input_pts_passes_monotonic_input_through() {+ // Any strictly-forward delta passes untouched, even sub-frame ones+ // from sources far faster than the nominal rate (1000fps = 1ms).+ assert_eq!(normalize_input_pts(16_667, Some(0)), 16_667);+ assert_eq!(normalize_input_pts(1_000, Some(0)), 1_000);+ }++ #[test]+ fn normalize_input_pts_bumps_non_monotonic_input_by_one_tick() {+ // Any larger bump outruns a fast source: each corrected frame pushes+ // the next into correction too, re-timing the whole recording.+ assert_eq!(normalize_input_pts(90_000, Some(100_000)), 100_001);+ assert_eq!(normalize_input_pts(100_000, Some(100_000)), 100_001);+ }+fn test_video_info() -> VideoInfo {VideoInfo {pixel_format: Pixel::NV12,