[Spice-devel] server: Simplify the MJPEG encoder's maximum framerate estimation

Submitted by Francois Gouget on Dec. 3, 2015, 3:28 p.m.

Details

Message ID alpine.DEB.2.20.1512031626330.14129@amboise
State New
Headers show
Series "server: Simplify the MJPEG encoder's maximum framerate estimation" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Francois Gouget Dec. 3, 2015, 3:28 p.m.
Also round it to the nearest integer instead of rounding down.

Signed-off-by: Francois Gouget <fgouget@codeweavers.com>
---
 server/mjpeg_encoder.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

Patch hide | download patch | download mbox

diff --git a/server/mjpeg_encoder.c b/server/mjpeg_encoder.c
index 9b331c1..752dae8 100644
--- a/server/mjpeg_encoder.c
+++ b/server/mjpeg_encoder.c
@@ -341,15 +341,7 @@  static inline uint32_t mjpeg_encoder_get_latency(MJpegEncoder *encoder)
 
 static uint32_t get_max_fps(uint64_t frame_size, uint64_t bytes_per_sec)
 {
-    double fps;
-    double send_time_ms;
-
-    if (!bytes_per_sec) {
-        return 0;
-    }
-    send_time_ms = frame_size * 1000.0 / bytes_per_sec;
-    fps = send_time_ms ? 1000 / send_time_ms : MJPEG_MAX_FPS;
-    return fps;
+    return frame_size ? (bytes_per_sec + frame_size / 2) / frame_size : MJPEG_MAX_FPS;
 }
 
 static inline void mjpeg_encoder_reset_quality(MJpegEncoder *encoder,

Comments

> 
> Also round it to the nearest integer instead of rounding down.
> 
> Signed-off-by: Francois Gouget <fgouget@codeweavers.com>
> ---
>  server/mjpeg_encoder.c | 10 +---------
>  1 file changed, 1 insertion(+), 9 deletions(-)
> 
> diff --git a/server/mjpeg_encoder.c b/server/mjpeg_encoder.c
> index 9b331c1..752dae8 100644
> --- a/server/mjpeg_encoder.c
> +++ b/server/mjpeg_encoder.c
> @@ -341,15 +341,7 @@ static inline uint32_t
> mjpeg_encoder_get_latency(MJpegEncoder *encoder)
>  
>  static uint32_t get_max_fps(uint64_t frame_size, uint64_t bytes_per_sec)
>  {
> -    double fps;
> -    double send_time_ms;
> -
> -    if (!bytes_per_sec) {
> -        return 0;
> -    }
> -    send_time_ms = frame_size * 1000.0 / bytes_per_sec;
> -    fps = send_time_ms ? 1000 / send_time_ms : MJPEG_MAX_FPS;
> -    return fps;
> +    return frame_size ? (bytes_per_sec + frame_size / 2) / frame_size :
> MJPEG_MAX_FPS;
>  }
>  
>  static inline void mjpeg_encoder_reset_quality(MJpegEncoder *encoder,

I think the exact simplification is

if (!bytes_per_sec) {
  return 0;
}
return frame_size ? bytes_per_sec / frame_size : MJPEG_MAX_FPS;

You already noted that you used rounding instead or truncation;
depending on the usage can be correct or not.
This change that if bytes_per_sec == 0 and frame_size == 0 previous
code returned 0 while now returns MJPEG_MAX_FPS. I think this
is not possible!

Frediano
On Thu, 3 Dec 2015, Frediano Ziglio wrote:
[...]
> I think the exact simplification is
> 
> if (!bytes_per_sec) {
>   return 0;
> }
> return frame_size ? bytes_per_sec / frame_size : MJPEG_MAX_FPS;
> 
> You already noted that you used rounding instead or truncation;
> depending on the usage can be correct or not.

The function seems to be meant to return the maximum safe framerate so 
always rounding down may indeed be better. I resent the patch and added 
a note in the commit log for the bytes_per_sec == frame_size == 0 case.