+
+ /* Audio and video frame PTS values may not start with 0. We want
+ to fiddle them so that:
+
+ 1. One of them starts at time 0.
+ 2. The first video PTS value ends up on a frame boundary.
+
+ Then we remove big initial gaps in PTS and we allow our
+ insertion of black frames to work.
+
+ We will do:
+ audio_pts_to_use = audio_pts_from_ffmpeg + pts_offset;
+ video_pts_to_use = video_pts_from_ffmpeg + pts_offset;
+ */
+
+ bool const have_video = video && c->first_video();
+ bool const have_audio = audio && c->audio_stream() && c->audio_stream()->first_audio;
+
+ /* First, make one of them start at 0 */
+
+ if (have_audio && have_video) {
+ _pts_offset = - min (c->first_video().get(), c->audio_stream()->first_audio.get());
+ } else if (have_video) {
+ _pts_offset = - c->first_video().get();
+ } else if (have_audio) {
+ _pts_offset = - c->audio_stream()->first_audio.get();
+ }
+
+ /* Now adjust both so that the video pts starts on a frame */
+ if (have_video && have_audio) {
+ double first_video = c->first_video().get() + _pts_offset;
+ double const old_first_video = first_video;
+
+ /* Round the first video up to a frame boundary */
+ if (fabs (rint (first_video * c->video_frame_rate()) - first_video * c->video_frame_rate()) > 1e-6) {
+ first_video = ceil (first_video * c->video_frame_rate()) / c->video_frame_rate ();
+ }
+
+ _pts_offset += first_video - old_first_video;
+ }