[FFmpeg-devel] [PATCH 3/3] avdevice: use av_gettime_relative() for timestamps

Marton Balint cus at passwd.hu
Sun Feb 7 02:04:23 EET 2021


av_gettime_relative() is using the monotonic clock therefore more suitable as a
timestamp source and for relative time calculations.

Probably fixes ticket #9089.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavdevice/alsa_dec.c        | 2 +-
 libavdevice/bktr.c            | 6 +++---
 libavdevice/fbdev_dec.c       | 4 ++--
 libavdevice/gdigrab.c         | 4 ++--
 libavdevice/jack.c            | 2 +-
 libavdevice/kmsgrab.c         | 4 ++--
 libavdevice/openal-dec.c      | 2 +-
 libavdevice/oss_dec.c         | 2 +-
 libavdevice/pulse_audio_dec.c | 2 +-
 libavdevice/sndio_dec.c       | 2 +-
 libavdevice/xcbgrab.c         | 4 ++--
 11 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/libavdevice/alsa_dec.c b/libavdevice/alsa_dec.c
index 36494e921c..04875af590 100644
--- a/libavdevice/alsa_dec.c
+++ b/libavdevice/alsa_dec.c
@@ -125,7 +125,7 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
         ff_timefilter_reset(s->timefilter);
     }
 
-    dts = av_gettime();
+    dts = av_gettime_relative();
     snd_pcm_delay(s->h, &delay);
     dts -= av_rescale(delay + res, 1000000, s->sample_rate);
     pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period);
diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c
index 2601adbba8..8e66fd64d6 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;
@@ -250,7 +250,7 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
 
     bktr_getframe(s->per_frame);
 
-    pkt->pts = av_gettime();
+    pkt->pts = av_gettime_relative();
     memcpy(pkt->data, video_buf, video_buf_size);
 
     return video_buf_size;
diff --git a/libavdevice/fbdev_dec.c b/libavdevice/fbdev_dec.c
index 6a51816868..a25f0ff7c1 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",
diff --git a/libavdevice/gdigrab.c b/libavdevice/gdigrab.c
index f4444406fa..814a1914a6 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)) {
diff --git a/libavdevice/jack.c b/libavdevice/jack.c
index 34f1c6de97..7b973b8478 100644
--- a/libavdevice/jack.c
+++ b/libavdevice/jack.c
@@ -77,7 +77,7 @@ static int process_callback(jack_nframes_t nframes, void *arg)
 
     /* Retrieve filtered cycle time */
     cycle_time = ff_timefilter_update(self->timefilter,
-                                      av_gettime() / 1000000.0 - (double) cycle_delay / self->sample_rate,
+                                      av_gettime_relative() / 1000000.0 - (double) cycle_delay / self->sample_rate,
                                       self->buffer_size);
 
     /* Check if an empty packet is available, and if there's enough space to send it back once filled */
diff --git a/libavdevice/kmsgrab.c b/libavdevice/kmsgrab.c
index 94e32b9cae..58ad9b6de3 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,7 +276,7 @@ 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;
diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c
index 57de665eb6..72320b2c6f 100644
--- a/libavdevice/openal-dec.c
+++ b/libavdevice/openal-dec.c
@@ -201,7 +201,7 @@ static int read_packet(AVFormatContext* ctx, AVPacket *pkt)
     /* Create a packet of appropriate size */
     if ((error = av_new_packet(pkt, nb_samples*ad->sample_step)) < 0)
         goto fail;
-    pkt->pts = av_gettime();
+    pkt->pts = av_gettime_relative();
 
     /* Fill the packet with the available samples */
     alcCaptureSamples(ad->device, pkt->data, nb_samples);
diff --git a/libavdevice/oss_dec.c b/libavdevice/oss_dec.c
index 13ace7000d..bd8b34d7cd 100644
--- a/libavdevice/oss_dec.c
+++ b/libavdevice/oss_dec.c
@@ -87,7 +87,7 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
     pkt->size = ret;
 
     /* compute pts of the start of the packet */
-    cur_time = av_gettime();
+    cur_time = av_gettime_relative();
     bdelay = ret;
     if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
         bdelay += abufi.bytes;
diff --git a/libavdevice/pulse_audio_dec.c b/libavdevice/pulse_audio_dec.c
index 50a3c971ae..932ca4252f 100644
--- a/libavdevice/pulse_audio_dec.c
+++ b/libavdevice/pulse_audio_dec.c
@@ -304,7 +304,7 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
         goto unlock_and_fail;
     }
 
-    dts = av_gettime();
+    dts = av_gettime_relative();
     pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
 
     if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
diff --git a/libavdevice/sndio_dec.c b/libavdevice/sndio_dec.c
index ebb485a2c7..d321fa3031 100644
--- a/libavdevice/sndio_dec.c
+++ b/libavdevice/sndio_dec.c
@@ -75,7 +75,7 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
     s->softpos += ret;
 
     /* compute pts of the start of the packet */
-    cur_time = av_gettime();
+    cur_time = av_gettime_relative();
 
     bdelay = ret + s->hwpos - s->softpos;
 
diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c
index 95bdc8ab9d..a8a2a1fd76 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;
@@ -591,7 +591,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



More information about the ffmpeg-devel mailing list