]> git.sesse.net Git - nageru/blobdiff - futatabi/player.cpp
Simplify progress calculation.
[nageru] / futatabi / player.cpp
index da63f10647ab58f67f5c49ec194860a5d35b6580..6320d37b54883a96aae3a207a628c330bb66664c 100644 (file)
@@ -53,6 +53,15 @@ void Player::thread_func(AVFormatContext *file_avctx)
        }
 }
 
+namespace {
+
+double calc_progress(const Clip &clip, int64_t pts)
+{
+       return double(pts - clip.pts_in) / (clip.pts_out - clip.pts_in);
+}
+
+}  // namespace
+
 void Player::play_playlist_once()
 {
        vector<Clip> clip_list;
@@ -171,14 +180,9 @@ void Player::play_playlist_once()
 
                        if (progress_callback != nullptr) {
                                // NOTE: None of this will take into account any snapping done below.
-                               double played_this_clip = double(in_pts_for_progress - clip.pts_in) / TIMEBASE / clip.speed;
-                               double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / clip.speed;
-                               map<size_t, double> progress{{ clip_idx, played_this_clip / total_length }};
-
+                               map<size_t, double> progress{{ clip_idx, calc_progress(clip, in_pts_for_progress) }};
                                if (next_clip != nullptr && time_left_this_clip <= next_clip_fade_time) {
-                                       double played_next_clip = double(in_pts_secondary_for_progress - next_clip->pts_in) / TIMEBASE / next_clip->speed;
-                                       double total_next_length = double(next_clip->pts_out - next_clip->pts_in) / TIMEBASE / next_clip->speed;
-                                       progress[clip_idx + 1] = played_next_clip / total_next_length;
+                                       progress[clip_idx + 1] = calc_progress(*next_clip, in_pts_secondary_for_progress);
                                }
                                progress_callback(progress);
                        }
@@ -233,29 +237,11 @@ void Player::play_playlist_once()
                                }
                        }
 
-                       if (frame_lower.pts == frame_upper.pts || global_flags.interpolation_quality == 0) {
-                               auto display_func = [this, primary_stream_idx, frame_lower, secondary_frame, fade_alpha]{
-                                       if (destination != nullptr) {
-                                               destination->setFrame(primary_stream_idx, frame_lower, secondary_frame, fade_alpha);
-                                       }
-                               };
-                               if (video_stream == nullptr) {
-                                       display_func();
-                               } else {
-                                       if (secondary_stream_idx == -1) {
-                                               ++metric_original_frame;
-                                               video_stream->schedule_original_frame(
-                                                       next_frame_start, pts, display_func, QueueSpotHolder(this),
-                                                       frame_lower);
-                                       } else {
-                                               assert(secondary_frame.pts != -1);
-                                               ++metric_faded_frame;
-                                               video_stream->schedule_faded_frame(next_frame_start, pts, display_func,
-                                                       QueueSpotHolder(this), frame_lower,
-                                                       secondary_frame, fade_alpha);
-                                       }
-                               }
-                               last_pts_played = frame_lower.pts;
+                       // If there's nothing to interpolate between, or if interpolation is turned off,
+                       // or we're a preview, then just display the frame.
+                       if (frame_lower.pts == frame_upper.pts || global_flags.interpolation_quality == 0 || video_stream == nullptr) {
+                               display_single_frame(primary_stream_idx, frame_lower, secondary_stream_idx,
+                                                    secondary_frame, fade_alpha, next_frame_start, /*snapped=*/false);
                                continue;
                        }
 
@@ -266,30 +252,10 @@ void Player::play_playlist_once()
                        bool snapped = false;
                        for (FrameOnDisk snap_frame : { frame_lower, frame_upper }) {
                                if (fabs(snap_frame.pts - in_pts) < pts_snap_tolerance) {
-                                       auto display_func = [this, primary_stream_idx, snap_frame, secondary_frame, fade_alpha]{
-                                               if (destination != nullptr) {
-                                                       destination->setFrame(primary_stream_idx, snap_frame, secondary_frame, fade_alpha);
-                                               }
-                                       };
-                                       if (video_stream == nullptr) {
-                                               display_func();
-                                       } else {
-                                               if (secondary_stream_idx == -1) {
-                                                       ++metric_original_snapped_frame;
-                                                       video_stream->schedule_original_frame(
-                                                               next_frame_start, pts, display_func,
-                                                               QueueSpotHolder(this), snap_frame);
-                                               } else {
-                                                       assert(secondary_frame.pts != -1);
-                                                       ++metric_faded_snapped_frame;
-                                                       video_stream->schedule_faded_frame(
-                                                               next_frame_start, pts, display_func, QueueSpotHolder(this),
-                                                               snap_frame, secondary_frame, fade_alpha);
-                                               }
-                                       }
+                                       display_single_frame(primary_stream_idx, snap_frame, secondary_stream_idx,
+                                                            secondary_frame, fade_alpha, next_frame_start, /*snapped=*/true);
                                        in_pts_origin += snap_frame.pts - in_pts;
                                        snapped = true;
-                                       last_pts_played = snap_frame.pts;
                                        break;
                                }
                        }
@@ -330,31 +296,21 @@ void Player::play_playlist_once()
                        }
 
                        double alpha = double(in_pts - frame_lower.pts) / (frame_upper.pts - frame_lower.pts);
-
-                       if (video_stream == nullptr) {
-                               // Previews don't do any interpolation.
-                               assert(secondary_stream_idx == -1);
+                       auto display_func = [this](shared_ptr<Frame> frame) {
                                if (destination != nullptr) {
-                                       destination->setFrame(primary_stream_idx, frame_lower);
+                                       destination->setFrame(frame);
                                }
-                               last_pts_played = frame_lower.pts;
+                       };
+                       if (secondary_stream_idx == -1) {
+                               ++metric_interpolated_frame;
                        } else {
-                               auto display_func = [this](shared_ptr<Frame> frame) {
-                                       if (destination != nullptr) {
-                                               destination->setFrame(frame);
-                                       }
-                               };
-                               if (secondary_stream_idx == -1) {
-                                       ++metric_interpolated_frame;
-                               } else {
-                                       ++metric_interpolated_faded_frame;
-                               }
-                               video_stream->schedule_interpolated_frame(
-                                       next_frame_start, pts, display_func, QueueSpotHolder(this),
-                                       frame_lower, frame_upper, alpha,
-                                       secondary_frame, fade_alpha);
-                               last_pts_played = in_pts;  // Not really needed; only previews use last_pts_played.
+                               ++metric_interpolated_faded_frame;
                        }
+                       video_stream->schedule_interpolated_frame(
+                               next_frame_start, pts, display_func, QueueSpotHolder(this),
+                               frame_lower, frame_upper, alpha,
+                               secondary_frame, fade_alpha);
+                       last_pts_played = in_pts;  // Not really needed; only previews use last_pts_played.
                }
 
                // The clip ended.
@@ -377,6 +333,42 @@ void Player::play_playlist_once()
        }
 }
 
+void Player::display_single_frame(int primary_stream_idx, const FrameOnDisk &primary_frame, int secondary_stream_idx, const FrameOnDisk &secondary_frame, double fade_alpha, steady_clock::time_point frame_start, bool snapped)
+{
+               auto display_func = [this, primary_stream_idx, primary_frame, secondary_frame, fade_alpha]{
+               if (destination != nullptr) {
+                       destination->setFrame(primary_stream_idx, primary_frame, secondary_frame, fade_alpha);
+               }
+       };
+       if (video_stream == nullptr) {
+               display_func();
+       } else {
+               if (secondary_stream_idx == -1) {
+                       // NOTE: We could be increasing unused metrics for previews, but that's harmless.
+                       if (snapped) {
+                               ++metric_original_snapped_frame;
+                       } else {
+                               ++metric_original_frame;
+                       }
+                       video_stream->schedule_original_frame(
+                               frame_start, pts, display_func, QueueSpotHolder(this),
+                               primary_frame);
+               } else {
+                       assert(secondary_frame.pts != -1);
+                       // NOTE: We could be increasing unused metrics for previews, but that's harmless.
+                       if (snapped) {
+                               ++metric_faded_snapped_frame;
+                       } else {
+                               ++metric_faded_frame;
+                       }
+                       video_stream->schedule_faded_frame(frame_start, pts, display_func,
+                               QueueSpotHolder(this), primary_frame,
+                               secondary_frame, fade_alpha);
+               }
+       }
+       last_pts_played = primary_frame.pts;
+}
+
 // Find the frame immediately before and after this point.
 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper)
 {