[Spice-devel] server: Provide a framerate estimate based on the initial frames

Submitted by Francois Gouget on Nov. 3, 2015, 4:23 p.m.

Details

Message ID alpine.DEB.2.20.1511031720280.30635@amboise
State New
Headers show
Series "server: Provide a framerate estimate based on the initial frames" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Francois Gouget Nov. 3, 2015, 4:23 p.m.
This way the video encoder can actually count on a real estimate when
it is initializing.

Signed-off-by: Francois Gouget <fgouget@codeweavers.com>
---
 server/red_worker.c | 19 +++++++++++++++----
 server/tree.h       |  1 +
 2 files changed, 16 insertions(+), 4 deletions(-)

I sent this patch back in June but I did not get any feedback on it...
Here is an updated version. Good to go?

Patch hide | download patch | download mbox

diff --git a/server/red_worker.c b/server/red_worker.c
index 9111e13..56a1fd9 100644
--- a/server/red_worker.c
+++ b/server/red_worker.c
@@ -455,6 +455,7 @@  typedef struct RedSurface {
 
 typedef struct ItemTrace {
     red_time_t time;
+    red_time_t first_frame_time;
     int frames_count;
     int gradual_frames_count;
     int last_gradual_frame;
@@ -1312,6 +1313,7 @@  static inline void red_add_item_trace(RedWorker *worker, Drawable *item)
 
     trace = &worker->items_trace[worker->next_item_trace++ & ITEMS_TRACE_MASK];
     trace->time = item->creation_time;
+    trace->first_frame_time = item->first_frame_time;
     trace->frames_count = item->frames_count;
     trace->gradual_frames_count = item->gradual_frames_count;
     trace->last_gradual_frame = item->last_gradual_frame;
@@ -2405,7 +2407,12 @@  static void red_create_stream(RedWorker *worker, Drawable *drawable)
     SpiceBitmap *bitmap = &drawable->red_drawable->u.copy.src_bitmap->u.bitmap;
     stream->top_down = !!(bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN);
     drawable->stream = stream;
-    stream->input_fps = MAX_FPS;
+    /* Provide an fps estimate the video encoder can use when initializing
+     * based on the frames that lead to the creation of the stream. Round to
+     * the nearest integer, for instance 24 for 23.976.
+     */
+    uint64_t duration = drawable->creation_time - drawable->first_frame_time;
+    stream->input_fps = ((uint64_t)drawable->frames_count * 1000 * 1000 * 1000 + duration / 2) / duration;
     stream->num_input_frames = 0;
     stream->input_fps_start_time = drawable->creation_time;
     worker->streams_size_total += stream->width * stream->height;
@@ -2413,9 +2420,9 @@  static void red_create_stream(RedWorker *worker, Drawable *drawable)
     WORKER_FOREACH_DCC_SAFE(worker, dcc_ring_item, next, dcc) {
         red_display_create_stream(dcc, stream);
     }
-    spice_debug("stream %d %dx%d (%d, %d) (%d, %d)", (int)(stream - worker->streams_buf), stream->width,
+    spice_debug("stream %d %dx%d (%d, %d) (%d, %d) %u fps", (int)(stream - worker->streams_buf), stream->width,
                 stream->height, stream->dest_area.left, stream->dest_area.top,
-                stream->dest_area.right, stream->dest_area.bottom);
+                stream->dest_area.right, stream->dest_area.bottom, stream->input_fps);
     return;
 }
 
@@ -2669,11 +2676,13 @@  static inline int red_is_stream_start(Drawable *drawable)
 // returns whether a stream was created
 static int red_stream_add_frame(RedWorker *worker,
                                 Drawable *frame_drawable,
+                                red_time_t first_frame_time,
                                 int frames_count,
                                 int gradual_frames_count,
                                 int last_gradual_frame)
 {
     red_update_copy_graduality(worker, frame_drawable);
+    frame_drawable->first_frame_time = first_frame_time;
     frame_drawable->frames_count = frames_count + 1;
     frame_drawable->gradual_frames_count  = gradual_frames_count;
 
@@ -2727,6 +2736,7 @@  static inline void red_stream_maintenance(RedWorker *worker, Drawable *candidate
     } else {
         if (red_is_next_stream_frame(worker, candidate, prev) != STREAM_FRAME_NONE) {
             red_stream_add_frame(worker, candidate,
+                                 prev->first_frame_time,
                                  prev->frames_count,
                                  prev->gradual_frames_count,
                                  prev->last_gradual_frame);
@@ -2888,6 +2898,7 @@  static inline void red_use_stream_trace(RedWorker *worker, Drawable *drawable)
                                        &trace->dest_area, trace->time, NULL, FALSE) !=
                                        STREAM_FRAME_NONE) {
             if (red_stream_add_frame(worker, drawable,
+                                     trace->first_frame_time,
                                      trace->frames_count,
                                      trace->gradual_frames_count,
                                      trace->last_gradual_frame)) {
@@ -3339,7 +3350,7 @@  static Drawable *get_drawable(RedWorker *worker, uint8_t effect, RedDrawable *re
     worker->drawable_count++;
     memset(drawable, 0, sizeof(Drawable));
     drawable->refs = 1;
-    drawable->creation_time = red_get_monotonic_time();
+    drawable->creation_time = drawable->first_frame_time = red_get_monotonic_time();
     ring_item_init(&drawable->list_link);
     ring_item_init(&drawable->surface_list_link);
     ring_item_init(&drawable->tree_item.base.siblings_link);
diff --git a/server/tree.h b/server/tree.h
index 8cd7b05..9deb842 100644
--- a/server/tree.h
+++ b/server/tree.h
@@ -90,6 +90,7 @@  struct Drawable {
     Ring glz_ring;
 
     red_time_t creation_time;
+    red_time_t first_frame_time;
     int frames_count;
     int gradual_frames_count;
     int last_gradual_frame;