From 0a799547aa601b75705900772b343e9c9f1da150 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <kasper93@gmail.com>
Date: Sun, 8 Oct 2023 18:32:42 +0200
Subject: [PATCH] vo: define <= 0 as unsupported for last_queue_display_time

Also sanitize vsync values, just in case.
---
 video/out/vo.c | 14 ++++++++++++--
 video/out/vo.h |  2 +-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/video/out/vo.c b/video/out/vo.c
index 4e90a02eb6..8d907d6bde 100644
--- a/video/out/vo.c
+++ b/video/out/vo.c
@@ -513,6 +513,14 @@ static void update_vsync_timing_after_swap(struct vo *vo,
     if (in->num_successive_vsyncs <= 2)
         return;
 
+    if (vsync_time <= 0 || vsync_time <= prev_vsync) {
+        in->prev_vsync = 0;
+        return;
+    }
+
+    if (prev_vsync <= 0)
+        return;
+
     if (in->num_vsync_samples >= MAX_VSYNC_SAMPLES)
         in->num_vsync_samples -= 1;
     MP_TARRAY_INSERT_AT(in, in->vsync_samples, in->num_vsync_samples, 0,
@@ -526,8 +534,10 @@ static void update_vsync_timing_after_swap(struct vo *vo,
     }
 
     double avg = 0;
-    for (int n = 0; n < in->num_vsync_samples; n++)
+    for (int n = 0; n < in->num_vsync_samples; n++) {
+        assert(in->vsync_samples[n] > 0);
         avg += in->vsync_samples[n];
+    }
     in->estimated_vsync_interval = avg / in->num_vsync_samples;
     in->estimated_vsync_jitter =
         vsync_stddef(vo, in->vsync_interval) / in->vsync_interval;
@@ -948,7 +958,7 @@ static bool render_frame(struct vo *vo)
             vo->driver->get_vsync(vo, &vsync);
 
         // Make up some crap if presentation feedback is missing.
-        if (vsync.last_queue_display_time < 0)
+        if (vsync.last_queue_display_time <= 0)
             vsync.last_queue_display_time = mp_time_ns();
 
         stats_time_end(in->stats, "video-flip");
diff --git a/video/out/vo.h b/video/out/vo.h
index 233f8c1094..6efc6ef63f 100644
--- a/video/out/vo.h
+++ b/video/out/vo.h
@@ -262,7 +262,7 @@ struct vo_frame {
 struct vo_vsync_info {
     // mp_time_ns() timestamp at which the last queued frame will likely be
     // displayed (this is in the future, unless the frame is instantly output).
-    // -1 if unset or unsupported.
+    // 0 or lower if unset or unsupported.
     // This implies the latency of the output.
     int64_t last_queue_display_time;