Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/test_aac_preroll.mp4
Binary file not shown.
27 changes: 12 additions & 15 deletions src/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ void FFmpegReader::UpdatePTSOffset() {
// Video packet
if (!has_video_pts && packet->stream_index == videoStream) {
// Get the video packet start time (in seconds)
video_pts_offset_seconds = 0.0 - (video_pts * info.video_timebase.ToDouble());
video_pts_offset_seconds = 0.0 - (pts * info.video_timebase.ToDouble());

// Is timestamp close to zero (within X seconds)
// Ignore wildly invalid timestamps (i.e. -234923423423)
Expand All @@ -2283,20 +2283,17 @@ void FFmpegReader::UpdatePTSOffset() {
}
}

// Do we have all valid timestamps to determine PTS offset?
if (has_video_pts && has_audio_pts) {
// Set PTS Offset to the smallest offset
// [ video timestamp ]
// [ audio timestamp ]
//
// ** SHIFT TIMESTAMPS TO ZERO **
//
//[ video timestamp ]
// [ audio timestamp ]
//
// Since all offsets are negative at this point, we want the max value, which
// represents the closest to zero
pts_offset_seconds = std::max(video_pts_offset_seconds, audio_pts_offset_seconds);
// Choose timestamp origin:
// - If video exists, anchor timeline frame mapping to video start.
// This avoids AAC priming / audio preroll shifting video frame 1 to frame 2.
// - If no video exists (audio-only readers), use audio start.
if (info.has_video && has_video_pts) {
pts_offset_seconds = video_pts_offset_seconds;
} else if (!info.has_video && has_audio_pts) {
pts_offset_seconds = audio_pts_offset_seconds;
} else if (has_video_pts && has_audio_pts) {
// Fallback when stream flags are unusual but both timestamps exist.
pts_offset_seconds = video_pts_offset_seconds;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,4 @@ TEST_CASE( "Decoding AV1 Video", "[libopenshot][ffmpegreader]" )
} catch (const InvalidFile & e) {
// Ignore older FFmpeg versions which don't support AV1
}
}
}