Skip to content

Commit

Permalink
Fix the PTS offset logic error when first reading a file on FFmpegRea…
Browse files Browse the repository at this point in the history
…der. Use the calculated 0 - PTS, unless it is too large (more than 1 second off from zero)
  • Loading branch information
jonoomph committed Sep 11, 2020
1 parent 6cc00d6 commit 650adf6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/FFmpegReader.cpp
Expand Up @@ -1868,8 +1868,18 @@ void FFmpegReader::UpdatePTSOffset(bool is_video) {
// VIDEO PACKET
if (video_pts_offset == 99999) // Has the offset been set yet?
{
// Find the difference between PTS and frame number (no more than 10 timebase units allowed)
video_pts_offset = 0 - std::max(GetVideoPTS(), (int64_t) info.video_timebase.ToInt() * 10);
// Find the difference between PTS and frame number
video_pts_offset = 0 - GetVideoPTS();

// Find the difference between PTS and frame number
// Also, determine if PTS is invalid (too far away from zero)
// We compare the PTS to the timebase value equal to 1 second (which means the PTS
// must be within the -1 second to +1 second of zero, otherwise we ignore it)
int64_t max_offset = info.video_timebase.Reciprocal().ToFloat();
if (video_pts_offset < -max_offset || video_pts_offset > max_offset) {
// Ignore PTS, it seems invalid
video_pts_offset = 0;
}

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

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

0 comments on commit 650adf6

Please sign in to comment.