Skip to content

Commit 650adf6

Browse files
committed
Fix the PTS offset logic error when first reading a file on FFmpegReader. Use the calculated 0 - PTS, unless it is too large (more than 1 second off from zero)
1 parent 6cc00d6 commit 650adf6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/FFmpegReader.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,8 +1868,18 @@ void FFmpegReader::UpdatePTSOffset(bool is_video) {
18681868
// VIDEO PACKET
18691869
if (video_pts_offset == 99999) // Has the offset been set yet?
18701870
{
1871-
// Find the difference between PTS and frame number (no more than 10 timebase units allowed)
1872-
video_pts_offset = 0 - std::max(GetVideoPTS(), (int64_t) info.video_timebase.ToInt() * 10);
1871+
// Find the difference between PTS and frame number
1872+
video_pts_offset = 0 - GetVideoPTS();
1873+
1874+
// Find the difference between PTS and frame number
1875+
// Also, determine if PTS is invalid (too far away from zero)
1876+
// We compare the PTS to the timebase value equal to 1 second (which means the PTS
1877+
// must be within the -1 second to +1 second of zero, otherwise we ignore it)
1878+
int64_t max_offset = info.video_timebase.Reciprocal().ToFloat();
1879+
if (video_pts_offset < -max_offset || video_pts_offset > max_offset) {
1880+
// Ignore PTS, it seems invalid
1881+
video_pts_offset = 0;
1882+
}
18731883

18741884
// debug output
18751885
ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::UpdatePTSOffset (Video)", "video_pts_offset", video_pts_offset, "is_video", is_video);
@@ -1878,8 +1888,16 @@ void FFmpegReader::UpdatePTSOffset(bool is_video) {
18781888
// AUDIO PACKET
18791889
if (audio_pts_offset == 99999) // Has the offset been set yet?
18801890
{
1881-
// Find the difference between PTS and frame number (no more than 10 timebase units allowed)
1882-
audio_pts_offset = 0 - std::max(packet->pts, (int64_t) info.audio_timebase.ToInt() * 10);
1891+
// Find the difference between PTS and frame number
1892+
// Also, determine if PTS is invalid (too far away from zero)
1893+
// We compare the PTS to the timebase value equal to 1 second (which means the PTS
1894+
// must be within the -1 second to +1 second of zero, otherwise we ignore it)
1895+
audio_pts_offset = 0 - packet->pts;
1896+
int64_t max_offset = info.audio_timebase.Reciprocal().ToFloat();
1897+
if (audio_pts_offset < -max_offset || audio_pts_offset > max_offset) {
1898+
// Ignore PTS, it seems invalid
1899+
audio_pts_offset = 0;
1900+
}
18831901

18841902
// debug output
18851903
ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::UpdatePTSOffset (Audio)", "audio_pts_offset", audio_pts_offset, "is_video", is_video);

0 commit comments

Comments
 (0)