]> git.sesse.net Git - ffmpeg/commitdiff
avdevice: use av_gettime_relative() for elapsed time calculations
authorMarton Balint <cus@passwd.hu>
Sat, 6 Feb 2021 23:55:25 +0000 (00:55 +0100)
committerMarton Balint <cus@passwd.hu>
Sat, 27 Feb 2021 20:41:50 +0000 (21:41 +0100)
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.

Related to ticket #9089.

Signed-off-by: Marton Balint <cus@passwd.hu>
libavdevice/bktr.c
libavdevice/fbdev_dec.c
libavdevice/gdigrab.c
libavdevice/kmsgrab.c
libavdevice/xcbgrab.c

index 2601adbba8b4b9ab9de653eaea1f5f470d2c7eb2..0688028f90856975e0976b39f60012f7769767bd 100644 (file)
@@ -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;
index 6a5181686812c7e8eac06b8ccb02adee3a8ca891..586caeef8890e00d0ed49a325857730c8e6a9dd9 100644 (file)
@@ -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 +
index f4444406fa65159208631c5102ab5b15f5731f2a..9b2c55fe902a28a7090e902a236d75c86e1a95ac 100644 (file)
@@ -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,
index 94e32b9cae464b9ac302c9751ddb3e1aaae30d3d..6cc305b16f3c54312a82f166bb2422081529b0ff 100644 (file)
@@ -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) {
index be5d5ea2cfe1bc6fd96ebdc156a5b3a65b7e5ae2..9604a5aaf24db6b13a2f27033f84cc9a639122f8 100644 (file)
@@ -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);