Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(enc-ffmpeg): normalize encoder pts and repair output packet timestamps

Richie McIlroya8ee5a83a8e8
diff --git a/crates/enc-ffmpeg/src/base.rs b/crates/enc-ffmpeg/src/base.rs
index 3a5a78e8b..88ffbef5b 100644
--- a/crates/enc-ffmpeg/src/base.rs
+++ b/crates/enc-ffmpeg/src/base.rs
@@ -1,7 +1,7 @@
use std::time::Duration;
use ffmpeg::{
- Packet,
+ Packet, Rational,
codec::encoder,
format::{self},
frame,
@@ -11,6 +11,7 @@ pub struct EncoderBase {
packet: ffmpeg::Packet,
stream_index: usize,
first_pts: Option<i64>,
+ last_frame_pts: Option<i64>,
last_written_dts: Option<i64>,
}
@@ -19,6 +20,7 @@ impl EncoderBase {
Self {
packet: Packet::empty(),
first_pts: None,
+ last_frame_pts: None,
stream_index,
last_written_dts: None,
}
@@ -37,7 +39,14 @@ impl EncoderBase {
let pts = (timestamp.as_secs_f64() * rate).round() as i64;
let first_pts = self.first_pts.get_or_insert(pts);
- frame.set_pts(Some(pts - *first_pts));
+ let pts = normalize_input_pts(
+ pts - *first_pts,
+ self.last_frame_pts,
+ encoder.time_base(),
+ encoder.frame_rate(),
+ );
+ self.last_frame_pts = Some(pts);
+ frame.set_pts(Some(pts));
} else {
let Some(pts) = frame.pts() else {
tracing::error!("Frame has no pts");
@@ -46,7 +55,14 @@ impl EncoderBase {
let first_pts = self.first_pts.get_or_insert(pts);
- frame.set_pts(Some(pts - *first_pts));
+ let pts = normalize_input_pts(
+ pts - *first_pts,
+ self.last_frame_pts,
+ encoder.time_base(),
+ encoder.frame_rate(),
+ );
+ self.last_frame_pts = Some(pts);
+ frame.set_pts(Some(pts));
}
}
@@ -73,6 +89,21 @@ impl EncoderBase {
output.stream(self.stream_index).unwrap().time_base(),
);
+ match (self.packet.pts(), self.packet.dts()) {
+ (Some(pts), None) => self.packet.set_dts(Some(pts)),
+ (None, Some(dts)) => self.packet.set_pts(Some(dts)),
+ _ => {}
+ }
+
+ if self.packet.duration() <= 0
+ && let Some(duration) = nominal_packet_duration(
+ output.stream(self.stream_index).unwrap().time_base(),
+ encoder.frame_rate(),
+ )
+ {
+ self.packet.set_duration(duration);
+ }
+
if let (Some(dts), Some(last_dts)) = (self.packet.dts(), self.last_written_dts)
&& dts <= last_dts
{
@@ -108,3 +139,38 @@ impl EncoderBase {
self.process_packets(output, encoder)
}
}
+
+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)
+}
+
+fn normalize_input_pts(
+ pts: i64,
+ last_pts: Option<i64>,
+ time_base: Rational,
+ frame_rate: Rational,
+) -> i64 {
+ let Some(last_pts) = last_pts else {
+ return pts;
+ };
+
+ if pts > last_pts {
+ return pts;
+ }
+
+ last_pts + nominal_packet_duration(time_base, frame_rate).unwrap_or(1)
+}
diff --git a/crates/enc-ffmpeg/src/video/h264_packet.rs b/crates/enc-ffmpeg/src/video/h264_packet.rs
index 3a802feae..d207fe5df 100644
--- a/crates/enc-ffmpeg/src/video/h264_packet.rs
+++ b/crates/enc-ffmpeg/src/video/h264_packet.rs
@@ -21,6 +21,7 @@ pub struct H264PacketEncoder {
input_height: u32,
packet: Packet,
first_pts: Option<i64>,
+ last_frame_pts: Option<i64>,
last_written_dts: Option<i64>,
encoder_time_base: Rational,
frame_rate: Rational,
@@ -63,6 +64,7 @@ impl H264PacketEncoder {
input_height: opened.input_height,
packet: Packet::empty(),
first_pts: None,
+ last_frame_pts: None,
last_written_dts: None,
encoder_time_base,
frame_rate,
@@ -200,11 +202,25 @@ impl H264PacketEncoder {
let tb = self.encoder.time_base();
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);
- frame.set_pts(Some(pts - *first_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.encoder.frame_rate(),
+ );
+ 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);
- frame.set_pts(Some(pts - *first_pts));
+ let first_pts = *self.first_pts.get_or_insert(pts);
+ let pts = normalize_input_pts(
+ pts - first_pts,
+ self.last_frame_pts,
+ self.encoder.time_base(),
+ self.encoder.frame_rate(),
+ );
+ self.last_frame_pts = Some(pts);
+ frame.set_pts(Some(pts));
} else {
tracing::error!("Frame has no pts");
}
@@ -215,6 +231,12 @@ impl H264PacketEncoder {
F: FnMut(EncodedPacket) -> Result<(), EncodePacketError>,
{
while self.encoder.receive_packet(&mut self.packet).is_ok() {
+ match (self.packet.pts(), self.packet.dts()) {
+ (Some(pts), None) => self.packet.set_dts(Some(pts)),
+ (None, Some(dts)) => self.packet.set_pts(Some(dts)),
+ _ => {}
+ }
+
if let (Some(dts), Some(last_dts)) = (self.packet.dts(), self.last_written_dts)
&& dts <= last_dts
{
@@ -254,6 +276,41 @@ impl H264PacketEncoder {
}
}
+fn normalize_input_pts(
+ pts: i64,
+ last_pts: Option<i64>,
+ time_base: Rational,
+ frame_rate: Rational,
+) -> i64 {
+ let Some(last_pts) = last_pts else {
+ return pts;
+ };
+
+ if pts > last_pts {
+ return pts;
+ }
+
+ last_pts + nominal_packet_duration(time_base, frame_rate).unwrap_or(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 {
use super::*;