- // Find the first frame such that in_pts >= frame.pts.
- auto it = lower_bound(frames[stream_idx].begin(),
- frames[stream_idx].end(),
- in_pts);
- if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
- break;
+ double time_left_this_clip = double(clip.pts_out - in_pts) / TIMEBASE / speed;
+ if (!got_next_clip && next_clip_callback != nullptr && time_left_this_clip <= clip.fade_time_seconds) {
+ // Find the next clip so that we can begin a fade.
+ tie(next_clip, next_clip_idx) = next_clip_callback();
+ if (next_clip.pts_in != -1) {
+ got_next_clip = true;
+
+ double duration_next_clip = (next_clip.pts_out - next_clip.pts_in) / TIMEBASE / speed;
+ next_clip_fade_time = std::min(time_left_this_clip, duration_next_clip);
+ in_pts_start_next_clip = next_clip.pts_in + lrint(next_clip_fade_time * TIMEBASE * speed);
+ }
+ }
+
+ // pts not affected by the swapping below.
+ int64_t in_pts_for_progress = in_pts, in_pts_secondary_for_progress = -1;
+
+ int primary_stream_idx = stream_idx;
+ FrameOnDisk secondary_frame;
+ int secondary_stream_idx = -1;
+ float fade_alpha = 0.0f;
+ if (got_next_clip && time_left_this_clip <= next_clip_fade_time) {
+ secondary_stream_idx = next_clip.stream_idx;
+ int64_t in_pts_secondary = lrint(next_clip.pts_in + (next_clip_fade_time - time_left_this_clip) * TIMEBASE * speed);
+ in_pts_secondary_for_progress = in_pts_secondary;
+ fade_alpha = 1.0f - time_left_this_clip / next_clip_fade_time;
+
+ // If more than half-way through the fade, interpolate the next clip
+ // instead of the current one, since it's more visible.
+ if (fade_alpha >= 0.5f) {
+ swap(primary_stream_idx, secondary_stream_idx);
+ swap(in_pts, in_pts_secondary);
+ fade_alpha = 1.0f - fade_alpha;