diff mbox series

[FFmpeg-devel] avdevice: use av_gettime_relative() for elapsed time calculations

Message ID 20210219203545.32764-1-cus@passwd.hu
State Accepted
Commit 2d32aed85c4874a2596202b6f0c08e6ce49e3bca
Headers show
Series [FFmpeg-devel] avdevice: use av_gettime_relative() for elapsed time calculations
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Marton Balint Feb. 19, 2021, 8:35 p.m. UTC
av_gettime_relative() is using the monotonic clock therefore more suitable for
elapsed time calculations. Packet timestamps are still kept absolute, although
that should be configurable in the future.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavdevice/bktr.c      | 4 ++--
 libavdevice/fbdev_dec.c | 6 +++---
 libavdevice/gdigrab.c   | 6 +++---
 libavdevice/kmsgrab.c   | 5 +++--
 libavdevice/xcbgrab.c   | 7 ++++---
 5 files changed, 15 insertions(+), 13 deletions(-)

Comments

Marton Balint Feb. 26, 2021, 10:27 p.m. UTC | #1
On Fri, 19 Feb 2021, Marton Balint wrote:

> av_gettime_relative() is using the monotonic clock therefore more suitable for
> elapsed time calculations. Packet timestamps are still kept absolute, although
> that should be configurable in the future.
>
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
> libavdevice/bktr.c      | 4 ++--
> libavdevice/fbdev_dec.c | 6 +++---
> libavdevice/gdigrab.c   | 6 +++---
> libavdevice/kmsgrab.c   | 5 +++--
> libavdevice/xcbgrab.c   | 7 ++++---
> 5 files changed, 15 insertions(+), 13 deletions(-)

Will apply soon. BKTR is untested.

Regards,
Marton

>
> diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c
> index 2601adbba8..0688028f90 100644
> --- a/libavdevice/bktr.c
> +++ b/libavdevice/bktr.c
> @@ -225,14 +225,14 @@ static void bktr_getframe(uint64_t per_frame)
> {
>     uint64_t curtime;
> 
> -    curtime = av_gettime();
> +    curtime = av_gettime_relative();
>     if (!last_frame_time
>         || ((last_frame_time + per_frame) > curtime)) {
>         if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
>             if (!nsignals)
>                 av_log(NULL, AV_LOG_INFO,
>                        "SLEPT NO signals - %d microseconds late\n",
> -                       (int)(av_gettime() - last_frame_time - per_frame));
> +                       (int)(av_gettime_relative() - last_frame_time - per_frame));
>         }
>     }
>     nsignals = 0;
> diff --git a/libavdevice/fbdev_dec.c b/libavdevice/fbdev_dec.c
> index 6a51816868..586caeef88 100644
> --- a/libavdevice/fbdev_dec.c
> +++ b/libavdevice/fbdev_dec.c
> @@ -157,11 +157,11 @@ static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
>     uint8_t *pin, *pout;
>
>     if (fbdev->time_frame == AV_NOPTS_VALUE)
> -        fbdev->time_frame = av_gettime();
> +        fbdev->time_frame = av_gettime_relative();
>
>     /* wait based on the frame rate */
>     while (1) {
> -        curtime = av_gettime();
> +        curtime = av_gettime_relative();
>         delay = fbdev->time_frame - curtime;
>         av_log(avctx, AV_LOG_TRACE,
>                 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
> @@ -186,7 +186,7 @@ static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
>                "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
>     }
> 
> -    pkt->pts = curtime;
> +    pkt->pts = av_gettime();
>
>     /* compute visible data offset */
>     pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
> diff --git a/libavdevice/gdigrab.c b/libavdevice/gdigrab.c
> index f4444406fa..9b2c55fe90 100644
> --- a/libavdevice/gdigrab.c
> +++ b/libavdevice/gdigrab.c
> @@ -394,7 +394,7 @@ gdigrab_read_header(AVFormatContext *s1)
>     gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
>                            (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
>     gdigrab->time_base   = av_inv_q(gdigrab->framerate);
> -    gdigrab->time_frame  = av_gettime() / av_q2d(gdigrab->time_base);
> +    gdigrab->time_frame  = av_gettime_relative() / av_q2d(gdigrab->time_base);
>
>     gdigrab->hwnd       = hwnd;
>     gdigrab->source_hdc = source_hdc;
> @@ -551,7 +551,7 @@ static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
>
>     /* wait based on the frame rate */
>     for (;;) {
> -        curtime = av_gettime();
> +        curtime = av_gettime_relative();
>         delay = time_frame * av_q2d(time_base) - curtime;
>         if (delay <= 0) {
>             if (delay < INT64_C(-1000000) * av_q2d(time_base)) {
> @@ -568,7 +568,7 @@ static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
>
>     if (av_new_packet(pkt, file_size) < 0)
>         return AVERROR(ENOMEM);
> -    pkt->pts = curtime;
> +    pkt->pts = av_gettime();
>
>     /* Blit screen grab */
>     if (!BitBlt(dest_hdc, 0, 0,
> diff --git a/libavdevice/kmsgrab.c b/libavdevice/kmsgrab.c
> index 94e32b9cae..6cc305b16f 100644
> --- a/libavdevice/kmsgrab.c
> +++ b/libavdevice/kmsgrab.c
> @@ -268,7 +268,7 @@ static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
>     int64_t now;
>     int err;
> 
> -    now = av_gettime();
> +    now = av_gettime_relative();
>     if (ctx->frame_last) {
>         int64_t delay;
>         while (1) {
> @@ -276,10 +276,11 @@ static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
>             if (delay <= 0)
>                 break;
>             av_usleep(delay);
> -            now = av_gettime();
> +            now = av_gettime_relative();
>         }
>     }
>     ctx->frame_last = now;
> +    now = av_gettime();
>
>     plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
>     if (!plane) {
> diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c
> index be5d5ea2cf..9604a5aaf2 100644
> --- a/libavdevice/xcbgrab.c
> +++ b/libavdevice/xcbgrab.c
> @@ -206,7 +206,7 @@ static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
>     c->time_frame += c->frame_duration;
>
>     for (;;) {
> -        curtime = av_gettime();
> +        curtime = av_gettime_relative();
>         delay   = c->time_frame - curtime;
>         if (delay <= 0)
>             break;
> @@ -422,7 +422,8 @@ static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
>     int ret = 0;
>     int64_t pts;
> 
> -    pts = wait_frame(s, pkt);
> +    wait_frame(s, pkt);
> +    pts = av_gettime();
>
>     if (c->follow_mouse || c->draw_mouse) {
>         pc  = xcb_query_pointer(c->conn, c->screen->root);
> @@ -596,7 +597,7 @@ static int create_stream(AVFormatContext *s)
>     c->time_base  = (AVRational){ st->avg_frame_rate.den,
>                                   st->avg_frame_rate.num };
>     c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
> -    c->time_frame = av_gettime();
> +    c->time_frame = av_gettime_relative();
>
>     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
>     free(geo);
> -- 
> 2.26.2
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c
index 2601adbba8..0688028f90 100644
--- a/libavdevice/bktr.c
+++ b/libavdevice/bktr.c
@@ -225,14 +225,14 @@  static void bktr_getframe(uint64_t per_frame)
 {
     uint64_t curtime;
 
-    curtime = av_gettime();
+    curtime = av_gettime_relative();
     if (!last_frame_time
         || ((last_frame_time + per_frame) > curtime)) {
         if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
             if (!nsignals)
                 av_log(NULL, AV_LOG_INFO,
                        "SLEPT NO signals - %d microseconds late\n",
-                       (int)(av_gettime() - last_frame_time - per_frame));
+                       (int)(av_gettime_relative() - last_frame_time - per_frame));
         }
     }
     nsignals = 0;
diff --git a/libavdevice/fbdev_dec.c b/libavdevice/fbdev_dec.c
index 6a51816868..586caeef88 100644
--- a/libavdevice/fbdev_dec.c
+++ b/libavdevice/fbdev_dec.c
@@ -157,11 +157,11 @@  static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     uint8_t *pin, *pout;
 
     if (fbdev->time_frame == AV_NOPTS_VALUE)
-        fbdev->time_frame = av_gettime();
+        fbdev->time_frame = av_gettime_relative();
 
     /* wait based on the frame rate */
     while (1) {
-        curtime = av_gettime();
+        curtime = av_gettime_relative();
         delay = fbdev->time_frame - curtime;
         av_log(avctx, AV_LOG_TRACE,
                 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
@@ -186,7 +186,7 @@  static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
                "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
     }
 
-    pkt->pts = curtime;
+    pkt->pts = av_gettime();
 
     /* compute visible data offset */
     pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
diff --git a/libavdevice/gdigrab.c b/libavdevice/gdigrab.c
index f4444406fa..9b2c55fe90 100644
--- a/libavdevice/gdigrab.c
+++ b/libavdevice/gdigrab.c
@@ -394,7 +394,7 @@  gdigrab_read_header(AVFormatContext *s1)
     gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
                            (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
     gdigrab->time_base   = av_inv_q(gdigrab->framerate);
-    gdigrab->time_frame  = av_gettime() / av_q2d(gdigrab->time_base);
+    gdigrab->time_frame  = av_gettime_relative() / av_q2d(gdigrab->time_base);
 
     gdigrab->hwnd       = hwnd;
     gdigrab->source_hdc = source_hdc;
@@ -551,7 +551,7 @@  static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
 
     /* wait based on the frame rate */
     for (;;) {
-        curtime = av_gettime();
+        curtime = av_gettime_relative();
         delay = time_frame * av_q2d(time_base) - curtime;
         if (delay <= 0) {
             if (delay < INT64_C(-1000000) * av_q2d(time_base)) {
@@ -568,7 +568,7 @@  static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
 
     if (av_new_packet(pkt, file_size) < 0)
         return AVERROR(ENOMEM);
-    pkt->pts = curtime;
+    pkt->pts = av_gettime();
 
     /* Blit screen grab */
     if (!BitBlt(dest_hdc, 0, 0,
diff --git a/libavdevice/kmsgrab.c b/libavdevice/kmsgrab.c
index 94e32b9cae..6cc305b16f 100644
--- a/libavdevice/kmsgrab.c
+++ b/libavdevice/kmsgrab.c
@@ -268,7 +268,7 @@  static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     int64_t now;
     int err;
 
-    now = av_gettime();
+    now = av_gettime_relative();
     if (ctx->frame_last) {
         int64_t delay;
         while (1) {
@@ -276,10 +276,11 @@  static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
             if (delay <= 0)
                 break;
             av_usleep(delay);
-            now = av_gettime();
+            now = av_gettime_relative();
         }
     }
     ctx->frame_last = now;
+    now = av_gettime();
 
     plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
     if (!plane) {
diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c
index be5d5ea2cf..9604a5aaf2 100644
--- a/libavdevice/xcbgrab.c
+++ b/libavdevice/xcbgrab.c
@@ -206,7 +206,7 @@  static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
     c->time_frame += c->frame_duration;
 
     for (;;) {
-        curtime = av_gettime();
+        curtime = av_gettime_relative();
         delay   = c->time_frame - curtime;
         if (delay <= 0)
             break;
@@ -422,7 +422,8 @@  static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
     int ret = 0;
     int64_t pts;
 
-    pts = wait_frame(s, pkt);
+    wait_frame(s, pkt);
+    pts = av_gettime();
 
     if (c->follow_mouse || c->draw_mouse) {
         pc  = xcb_query_pointer(c->conn, c->screen->root);
@@ -596,7 +597,7 @@  static int create_stream(AVFormatContext *s)
     c->time_base  = (AVRational){ st->avg_frame_rate.den,
                                   st->avg_frame_rate.num };
     c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
-    c->time_frame = av_gettime();
+    c->time_frame = av_gettime_relative();
 
     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
     free(geo);