fix(enc-ffmpeg): use encoder_time_base in H264PacketEncoder to fix 4-8x playback speed#1970
Merged
richiemcilroy merged 1 commit intoJul 2, 2026
Conversation
| let tb = self.encoder_time_base; | ||
| if timestamp != Duration::MAX { | ||
| let tb = self.encoder.time_base(); | ||
| let rate = tb.denominator() as f64 / tb.numerator() as f64; |
There was a problem hiding this comment.
tb is now coming from config (vs the post-open codec), so it might be worth guarding against an invalid/zero Rational here — otherwise rate can become inf/NaN and the float->int cast can silently produce huge PTS.
Suggested change
| let rate = tb.denominator() as f64 / tb.numerator() as f64; | |
| debug_assert!( | |
| tb.numerator() > 0 && tb.denominator() > 0, | |
| "invalid encoder_time_base" | |
| ); | |
| let rate = tb.denominator() as f64 / tb.numerator() as f64; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
H264PacketEncoder.update_pts was computing PTS using the FFmpeg encoder's time_base after avcodec_open2.
h264_videotoolbox can override this value after opening, but the
cap-muxersubprocess is always initialised with the storedencoder_time_base(=input_config.time_base).When these diverge, the muxer rescales PTS against the wrong base and the video plays back at the wrong speed, reported as 4-8x too fast in v0.5.3 on macOS.
Fix: use the stored encoder_time_base and frame_rate (the values already sent to cap-muxer via InitVideo) instead of reading them back from the post-open encoder object, which may have been modified by the codec.
Greptile Summary
This PR fixes a 4-8x playback speed regression on macOS by correcting how PTS values are computed in
H264PacketEncoder::update_pts. The root cause is thath264_videotoolboxcan change the codec'stime_baseafteravcodec_open2, diverging from theencoder_time_basealready sent tocap-muxerviaInitVideo, causing the muxer to rescale PTS with the wrong denominator.self.encoder.time_base()andself.encoder.frame_rate()calls inupdate_ptswith the storedself.encoder_time_baseandself.frame_ratefields, which are the exact values the muxer subprocess was initialized with.let tb = self.encoder_time_basebefore theif/elsebranches so both the normal-timestamp path and the fallbackframe.pts()path use the same consistent base.Confidence Score: 5/5
Minimal, targeted fix that eliminates a time-base mismatch between the encoder and muxer; all three changed call sites are consistently updated.
The change is a one-function, three-site substitution of two struct field reads for two post-open encoder accessor calls. Both fields were already populated at construction with the values sent to cap-muxer, and the public time_base() / frame_rate() accessors already returned them. The fix is internally consistent, well-commented, and accompanied by existing unit tests that cover the encode path.
No files require special attention.
Important Files Changed
Reviews (1): Last reviewed commit: "fix(enc-ffmpeg): use encoder_time_base i..." | Re-trigger Greptile