X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=futatabi%2Fplayer.cpp;h=a82b16e72e6a527618b7e46f34e518ff9427d962;hb=2b37c51ea17b77b88e2f7fda443109acc260aa8f;hp=da63f10647ab58f67f5c49ec194860a5d35b6580;hpb=5ec5103f6c772770d0d2ebf1b7be758fe538a65b;p=nageru diff --git a/futatabi/player.cpp b/futatabi/player.cpp index da63f10..a82b16e 100644 --- a/futatabi/player.cpp +++ b/futatabi/player.cpp @@ -1,13 +1,13 @@ #include "player.h" #include "clip_list.h" -#include "shared/context.h" #include "defs.h" -#include "shared/ffmpeg_raii.h" #include "flags.h" #include "frame_on_disk.h" -#include "shared/httpd.h" #include "jpeg_frame_view.h" +#include "shared/context.h" +#include "shared/ffmpeg_raii.h" +#include "shared/httpd.h" #include "shared/metrics.h" #include "shared/mux.h" #include "shared/timebase.h" @@ -53,9 +53,18 @@ 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_list; + vector clip_list; bool clip_ready; steady_clock::time_point before_sleep = steady_clock::now(); @@ -90,10 +99,10 @@ void Player::play_playlist_once() } steady_clock::time_point origin = steady_clock::now(); // TODO: Add a 100 ms buffer for ramp-up? - int64_t in_pts_origin = clip_list[0].pts_in; + int64_t in_pts_origin = clip_list[0].clip.pts_in; for (size_t clip_idx = 0; clip_idx < clip_list.size(); ++clip_idx) { - const Clip &clip = clip_list[clip_idx]; - const Clip *next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1] : nullptr; + const Clip &clip = clip_list[clip_idx].clip; + const Clip *next_clip = (clip_idx + 1 < clip_list.size()) ? &clip_list[clip_idx + 1].clip : nullptr; int64_t out_pts_origin = pts; double next_clip_fade_time = -1.0; @@ -133,12 +142,11 @@ void Player::play_playlist_once() steady_clock::duration time_behind = steady_clock::now() - next_frame_start; if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(200)) { fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n", - lrint(1e3 * duration(time_behind).count())); + lrint(1e3 * duration(time_behind).count())); ++metric_dropped_unconditional_frame; continue; } - // pts not affected by the swapping below. int64_t in_pts_for_progress = in_pts, in_pts_secondary_for_progress = -1; @@ -171,14 +179,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 progress{{ clip_idx, played_this_clip / total_length }}; - + map progress{ { clip_list[clip_idx].row, 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_list[clip_idx + 1].row] = calc_progress(*next_clip, in_pts_secondary_for_progress); } progress_callback(progress); } @@ -194,7 +197,7 @@ void Player::play_playlist_once() unique_lock lock(queue_state_mu); if (video_stream == nullptr) { // No queue, just wait until the right time and then show the frame. - new_clip_changed.wait_until(lock, next_frame_start, [this]{ + new_clip_changed.wait_until(lock, next_frame_start, [this] { return should_quit || new_clip_ready || override_stream_idx != -1; }); if (should_quit) { @@ -207,7 +210,7 @@ void Player::play_playlist_once() // // In this case, we don't sleep until next_frame_start; the displaying is // done by the queue. - new_clip_changed.wait(lock, [this]{ + new_clip_changed.wait(lock, [this] { if (num_queued_frames < max_queued_frames) { return true; } @@ -233,29 +236,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 +251,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; } } @@ -313,7 +278,7 @@ void Player::play_playlist_once() // decorrelated with no common factor, of course (e.g. 12.345 → 34.567, which we should // really never see in practice). for (double fraction : { 1.0 / 2.0, 1.0 / 3.0, 2.0 / 3.0, 1.0 / 4.0, 3.0 / 4.0, - 1.0 / 5.0, 2.0 / 5.0, 3.0 / 5.0, 4.0 / 5.0 }) { + 1.0 / 5.0, 2.0 / 5.0, 3.0 / 5.0, 4.0 / 5.0 }) { double subsnap_pts = frame_lower.pts + fraction * (frame_upper.pts - frame_lower.pts); if (fabs(subsnap_pts - in_pts) < pts_snap_tolerance) { in_pts_origin += lrint(subsnap_pts) - in_pts; @@ -324,37 +289,27 @@ void Player::play_playlist_once() if (stream_output != FILE_STREAM_OUTPUT && time_behind >= milliseconds(100)) { fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n", - lrint(1e3 * duration(time_behind).count())); + lrint(1e3 * duration(time_behind).count())); ++metric_dropped_interpolated_frame; continue; } 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) { 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) { - 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 +332,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) { @@ -406,15 +397,15 @@ Player::Player(JPEGFrameView *destination, Player::StreamOutput stream_output, A player_thread = thread(&Player::thread_func, this, file_avctx); if (stream_output == HTTPD_STREAM_OUTPUT) { - global_metrics.add("http_output_frames", {{ "type", "original" }, { "reason", "edge_frame_or_no_interpolation" }}, &metric_original_frame); - global_metrics.add("http_output_frames", {{ "type", "faded" }, { "reason", "edge_frame_or_no_interpolation" }}, &metric_faded_frame); - global_metrics.add("http_output_frames", {{ "type", "original" }, { "reason", "snapped" }}, &metric_original_snapped_frame); - global_metrics.add("http_output_frames", {{ "type", "faded" }, { "reason", "snapped" }}, &metric_faded_snapped_frame); - global_metrics.add("http_output_frames", {{ "type", "interpolated" }}, &metric_interpolated_frame); - global_metrics.add("http_output_frames", {{ "type", "interpolated_faded" }}, &metric_interpolated_faded_frame); - global_metrics.add("http_output_frames", {{ "type", "refresh" }}, &metric_refresh_frame); - global_metrics.add("http_dropped_frames", {{ "type", "interpolated" }}, &metric_dropped_interpolated_frame); - global_metrics.add("http_dropped_frames", {{ "type", "unconditional" }}, &metric_dropped_unconditional_frame); + global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_original_frame); + global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "edge_frame_or_no_interpolation" } }, &metric_faded_frame); + global_metrics.add("http_output_frames", { { "type", "original" }, { "reason", "snapped" } }, &metric_original_snapped_frame); + global_metrics.add("http_output_frames", { { "type", "faded" }, { "reason", "snapped" } }, &metric_faded_snapped_frame); + global_metrics.add("http_output_frames", { { "type", "interpolated" } }, &metric_interpolated_frame); + global_metrics.add("http_output_frames", { { "type", "interpolated_faded" } }, &metric_interpolated_faded_frame); + global_metrics.add("http_output_frames", { { "type", "refresh" } }, &metric_refresh_frame); + global_metrics.add("http_dropped_frames", { { "type", "interpolated" } }, &metric_dropped_interpolated_frame); + global_metrics.add("http_dropped_frames", { { "type", "unconditional" } }, &metric_dropped_unconditional_frame); } } @@ -428,7 +419,7 @@ Player::~Player() player_thread.join(); } -void Player::play(const vector &clips) +void Player::play(const vector &clips) { lock_guard lock(queue_state_mu); new_clip_ready = true; @@ -446,7 +437,7 @@ void Player::override_angle(unsigned stream_idx) lock_guard lock(queue_state_mu); if (new_clip_ready) { assert(queued_clip_list.size() == 1); - queued_clip_list[0].stream_idx = stream_idx; + queued_clip_list[0].clip.stream_idx = stream_idx; return; }