[FFmpeg-devel] [PATCH] Make av_gettime use the monotonic clock when available

Michael Niedermayer michaelni at gmx.at
Wed Sep 3 14:49:44 CEST 2014


This also remove all use of av_gettime_relative()
and reduces the difference to ebef9f5a56d7df91e010a177a80cfc8dbe394305

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 cmdutils_opencl.c         |    4 ++--
 configure                 |    4 +++-
 ffmpeg.c                  |   10 +++++-----
 ffplay.c                  |   26 +++++++++++++-------------
 libavcodec/dct-test.c     |    8 ++++----
 libavcodec/fft-test.c     |    4 ++--
 libavcodec/motion-test.c  |    4 ++--
 libavdevice/v4l2.c        |    2 +-
 libavfilter/avf_showcqt.c |    4 ++--
 libavformat/avio.c        |    4 ++--
 libavformat/network.c     |    4 ++--
 libavformat/sapenc.c      |    2 +-
 libavutil/time.c          |   12 +++++-------
 tools/aviocat.c           |    4 ++--
 14 files changed, 46 insertions(+), 46 deletions(-)

diff --git a/cmdutils_opencl.c b/cmdutils_opencl.c
index 3dfd156..d7e3287 100644
--- a/cmdutils_opencl.c
+++ b/cmdutils_opencl.c
@@ -181,12 +181,12 @@ static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
     OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
     OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
 
-    start = av_gettime_relative();
+    start = av_gettime();
     for (i = 0; i < OPENCL_NB_ITER; i++)
         OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
                  global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
     clFinish(ext_opencl_env->command_queue);
-    ret = (av_gettime_relative() - start)/OPENCL_NB_ITER;
+    ret = (av_gettime() - start)/OPENCL_NB_ITER;
 end:
     if (kernel)
         clReleaseKernel(kernel);
diff --git a/configure b/configure
index b1814db..efd7b50 100755
--- a/configure
+++ b/configure
@@ -4655,7 +4655,9 @@ check_func  ${malloc_prefix}memalign            && enable memalign
 check_func  ${malloc_prefix}posix_memalign      && enable posix_memalign
 
 check_func  access
-check_func_headers time.h clock_gettime || { check_func_headers time.h clock_gettime -lrt && add_extralibs -lrt; }
+check_cpp_condition unistd.h "defined(_POSIX_MONOTONIC_CLOCK)" &&
+    check_func_headers time.h clock_gettime || { check_func_headers time.h clock_gettime -lrt && add_extralibs -lrt; }
+
 check_func  fcntl
 check_func  fork
 check_func  gethrtime
diff --git a/ffmpeg.c b/ffmpeg.c
index 220816b..9ded289 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2486,7 +2486,7 @@ static int transcode_init(void)
         InputFile *ifile = input_files[i];
         if (ifile->rate_emu)
             for (j = 0; j < ifile->nb_streams; j++)
-                input_streams[j + ifile->ist_index]->start = av_gettime_relative();
+                input_streams[j + ifile->ist_index]->start = av_gettime();
     }
 
     /* output stream init */
@@ -3286,7 +3286,7 @@ static int get_input_packet(InputFile *f, AVPacket *pkt)
         for (i = 0; i < f->nb_streams; i++) {
             InputStream *ist = input_streams[f->ist_index + i];
             int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
-            int64_t now = av_gettime_relative() - ist->start;
+            int64_t now = av_gettime() - ist->start;
             if (pts > now)
                 return AVERROR(EAGAIN);
         }
@@ -3656,7 +3656,7 @@ static int transcode(void)
         av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
     }
 
-    timer_start = av_gettime_relative();
+    timer_start = av_gettime();
 
 #if HAVE_PTHREADS
     if ((ret = init_input_threads()) < 0)
@@ -3664,7 +3664,7 @@ static int transcode(void)
 #endif
 
     while (!received_sigterm) {
-        int64_t cur_time= av_gettime_relative();
+        int64_t cur_time= av_gettime();
 
         /* if 'q' pressed, exits */
         if (stdin_interaction)
@@ -3711,7 +3711,7 @@ static int transcode(void)
     }
 
     /* dump report by using the first video and audio streams */
-    print_report(1, timer_start, av_gettime_relative());
+    print_report(1, timer_start, av_gettime());
 
     /* close each encoder */
     for (i = 0; i < nb_output_streams; i++) {
diff --git a/ffplay.c b/ffplay.c
index 833b5b1..b9f6426 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -913,7 +913,7 @@ static void video_audio_display(VideoState *s)
         /* to be more precise, we take into account the time spent since
            the last buffer computation */
         if (audio_callback_time) {
-            time_diff = av_gettime_relative() - audio_callback_time;
+            time_diff = av_gettime() - audio_callback_time;
             delay -= (time_diff * s->audio_tgt.freq) / 1000000;
         }
 
@@ -1146,7 +1146,7 @@ static double get_clock(Clock *c)
     if (c->paused) {
         return c->pts;
     } else {
-        double time = av_gettime_relative() / 1000000.0;
+        double time = av_gettime() / 1000000.0;
         return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
     }
 }
@@ -1161,7 +1161,7 @@ static void set_clock_at(Clock *c, double pts, int serial, double time)
 
 static void set_clock(Clock *c, double pts, int serial)
 {
-    double time = av_gettime_relative() / 1000000.0;
+    double time = av_gettime() / 1000000.0;
     set_clock_at(c, pts, serial, time);
 }
 
@@ -1254,7 +1254,7 @@ static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_by
 static void stream_toggle_pause(VideoState *is)
 {
     if (is->paused) {
-        is->frame_timer += av_gettime_relative() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts;
+        is->frame_timer += av_gettime() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts;
         if (is->read_pause_return != AVERROR(ENOSYS)) {
             is->vidclk.paused = 0;
         }
@@ -1366,7 +1366,7 @@ static void video_refresh(void *opaque, double *remaining_time)
         check_external_clock_speed(is);
 
     if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
-        time = av_gettime_relative() / 1000000.0;
+        time = av_gettime() / 1000000.0;
         if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
             video_display(is);
             is->last_vis_time = time;
@@ -1397,7 +1397,7 @@ retry:
             }
 
             if (lastvp->serial != vp->serial && !redisplay)
-                is->frame_timer = av_gettime_relative() / 1000000.0;
+                is->frame_timer = av_gettime() / 1000000.0;
 
             if (is->paused)
                 goto display;
@@ -1409,7 +1409,7 @@ retry:
             else
                 delay = compute_target_delay(last_duration, is);
 
-            time= av_gettime_relative()/1000000.0;
+            time= av_gettime()/1000000.0;
             if (time < is->frame_timer + delay && !redisplay) {
                 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
                 return;
@@ -1483,7 +1483,7 @@ display:
         int aqsize, vqsize, sqsize;
         double av_diff;
 
-        cur_time = av_gettime_relative();
+        cur_time = av_gettime();
         if (!last_time || (cur_time - last_time) >= 30000) {
             aqsize = 0;
             vqsize = 0;
@@ -2013,7 +2013,7 @@ static int video_thread(void *arg)
             goto the_end;
 
         while (ret >= 0) {
-            is->frame_last_returned_time = av_gettime_relative() / 1000000.0;
+            is->frame_last_returned_time = av_gettime() / 1000000.0;
 
             ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
             if (ret < 0) {
@@ -2023,7 +2023,7 @@ static int video_thread(void *arg)
                 break;
             }
 
-            is->frame_last_filter_delay = av_gettime_relative() / 1000000.0 - is->frame_last_returned_time;
+            is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
             if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
                 is->frame_last_filter_delay = 0;
             tb = filt_out->inputs[0]->time_base;
@@ -2424,7 +2424,7 @@ static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
     VideoState *is = opaque;
     int audio_size, len1;
 
-    audio_callback_time = av_gettime_relative();
+    audio_callback_time = av_gettime();
 
     while (len > 0) {
         if (is->audio_buf_index >= is->audio_buf_size) {
@@ -3219,7 +3219,7 @@ static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
     double remaining_time = 0.0;
     SDL_PumpEvents();
     while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
-        if (!cursor_hidden && av_gettime_relative() - cursor_last_shown > CURSOR_HIDE_DELAY) {
+        if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
             SDL_ShowCursor(0);
             cursor_hidden = 1;
         }
@@ -3383,7 +3383,7 @@ static void event_loop(VideoState *cur_stream)
                 SDL_ShowCursor(1);
                 cursor_hidden = 0;
             }
-            cursor_last_shown = av_gettime_relative();
+            cursor_last_shown = av_gettime();
             if (event.type == SDL_MOUSEBUTTONDOWN) {
                 x = event.button.x;
             } else {
diff --git a/libavcodec/dct-test.c b/libavcodec/dct-test.c
index f4f8abd..daf0751 100644
--- a/libavcodec/dct-test.c
+++ b/libavcodec/dct-test.c
@@ -252,7 +252,7 @@ static int dct_error(const struct algo *dct, int test, int is_idct, int speed, c
     init_block(block, test, is_idct, &prng, vals);
     permute(block1, block, dct->perm_type);
 
-    ti = av_gettime_relative();
+    ti = av_gettime();
     it1 = 0;
     do {
         for (it = 0; it < NB_ITS_SPEED; it++) {
@@ -261,7 +261,7 @@ static int dct_error(const struct algo *dct, int test, int is_idct, int speed, c
         }
         emms_c();
         it1 += NB_ITS_SPEED;
-        ti1 = av_gettime_relative() - ti;
+        ti1 = av_gettime() - ti;
     } while (ti1 < 1000000);
 
     printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name,
@@ -412,7 +412,7 @@ static void idct248_error(const char *name,
     if (!speed)
         return;
 
-    ti = av_gettime_relative();
+    ti = av_gettime();
     it1 = 0;
     do {
         for (it = 0; it < NB_ITS_SPEED; it++) {
@@ -422,7 +422,7 @@ static void idct248_error(const char *name,
         }
         emms_c();
         it1 += NB_ITS_SPEED;
-        ti1 = av_gettime_relative() - ti;
+        ti1 = av_gettime() - ti;
     } while (ti1 < 1000000);
 
     printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name,
diff --git a/libavcodec/fft-test.c b/libavcodec/fft-test.c
index ba26f81..70d4e44 100644
--- a/libavcodec/fft-test.c
+++ b/libavcodec/fft-test.c
@@ -445,7 +445,7 @@ int main(int argc, char **argv)
         /* we measure during about 1 seconds */
         nb_its = 1;
         for (;;) {
-            time_start = av_gettime_relative();
+            time_start = av_gettime();
             for (it = 0; it < nb_its; it++) {
                 switch (transform) {
                 case TRANSFORM_MDCT:
@@ -470,7 +470,7 @@ int main(int argc, char **argv)
 #endif /* FFT_FLOAT */
                 }
             }
-            duration = av_gettime_relative() - time_start;
+            duration = av_gettime() - time_start;
             if (duration >= 1000000)
                 break;
             nb_its *= 2;
diff --git a/libavcodec/motion-test.c b/libavcodec/motion-test.c
index 7cfe41c..01ed8ee 100644
--- a/libavcodec/motion-test.c
+++ b/libavcodec/motion-test.c
@@ -91,7 +91,7 @@ static void test_motion(const char *name,
     emms_c();
 
     /* speed test */
-    ti = av_gettime_relative();
+    ti = av_gettime();
     d1 = 0;
     for(it=0;it<NB_ITS;it++) {
         for(y=0;y<HEIGHT-17;y++) {
@@ -103,7 +103,7 @@ static void test_motion(const char *name,
     }
     emms_c();
     dummy = d1; /* avoid optimization */
-    ti = av_gettime_relative() - ti;
+    ti = av_gettime() - ti;
 
     printf("  %0.0f kop/s\n",
            (double)NB_ITS * (WIDTH - 16) * (HEIGHT - 16) /
diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 9f9f944..747e7f4 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -427,7 +427,7 @@ static void mmap_release_buffer(void *opaque, uint8_t *data)
 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
 static int64_t av_gettime_monotonic(void)
 {
-    return av_gettime_relative();
+    return av_gettime();
 }
 #endif
 
diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index e650f74..084608b 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -359,7 +359,7 @@ static int config_output(AVFilterLink *outlink)
 #endif
 
     av_log(ctx, AV_LOG_INFO, "Calculating spectral kernel, please wait\n");
-    start_time = av_gettime_relative();
+    start_time = av_gettime();
     ret = av_expr_parse(&tlength_expr, s->tlength, expr_vars, NULL, NULL, NULL, NULL, 0, ctx);
     if (ret < 0)
         goto eval_error;
@@ -490,7 +490,7 @@ static int config_output(AVFilterLink *outlink)
     av_expr_free(fontcolor_expr);
     av_expr_free(volume_expr);
     av_expr_free(tlength_expr);
-    end_time = av_gettime_relative();
+    end_time = av_gettime();
     av_log(ctx, AV_LOG_INFO, "Elapsed time %.6f s (fft_len=%u, num_coeffs=%u)\n", 1e-6 * (end_time-start_time), fft_len, num_coeffs);
 
     outlink->w = video_width;
diff --git a/libavformat/avio.c b/libavformat/avio.c
index 326bb0a..b48e4f9 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -312,8 +312,8 @@ static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
             } else {
                 if (h->rw_timeout) {
                     if (!wait_since)
-                        wait_since = av_gettime_relative();
-                    else if (av_gettime_relative() > wait_since + h->rw_timeout)
+                        wait_since = av_gettime();
+                    else if (av_gettime() > wait_since + h->rw_timeout)
                         return AVERROR(EIO);
                 }
                 av_usleep(1000);
diff --git a/libavformat/network.c b/libavformat/network.c
index 9f02ec6..5e574e3 100644
--- a/libavformat/network.c
+++ b/libavformat/network.c
@@ -163,8 +163,8 @@ int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterrupt
             return ret;
         if (timeout > 0) {
             if (!wait_start)
-                wait_start = av_gettime_relative();
-            else if (av_gettime_relative() - wait_start > timeout)
+                wait_start = av_gettime();
+            else if (av_gettime() - wait_start > timeout)
                 return AVERROR(ETIMEDOUT);
         }
     }
diff --git a/libavformat/sapenc.c b/libavformat/sapenc.c
index f909557..79a1ca8 100644
--- a/libavformat/sapenc.c
+++ b/libavformat/sapenc.c
@@ -246,7 +246,7 @@ static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     AVFormatContext *rtpctx;
     struct SAPState *sap = s->priv_data;
-    int64_t now = av_gettime_relative();
+    int64_t now = av_gettime();
 
     if (!sap->last_time || now - sap->last_time > 5000000) {
         int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
diff --git a/libavutil/time.c b/libavutil/time.c
index ce4552e..59306f9 100644
--- a/libavutil/time.c
+++ b/libavutil/time.c
@@ -38,7 +38,11 @@
 
 int64_t av_gettime(void)
 {
-#if HAVE_GETTIMEOFDAY
+#if HAVE_CLOCK_GETTIME
+    struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+#elif HAVE_GETTIMEOFDAY
     struct timeval tv;
     gettimeofday(&tv, NULL);
     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
@@ -55,13 +59,7 @@ int64_t av_gettime(void)
 
 int64_t av_gettime_relative(void)
 {
-#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
-    struct timespec ts;
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-#else
     return av_gettime();
-#endif
 }
 
 int av_gettime_relative_is_monotonic(void)
diff --git a/tools/aviocat.c b/tools/aviocat.c
index 3bd62b7..56b918e 100644
--- a/tools/aviocat.c
+++ b/tools/aviocat.c
@@ -82,7 +82,7 @@ int main(int argc, char **argv)
         goto fail;
     }
 
-    start_time = av_gettime_relative();
+    start_time = av_gettime();
     while (1) {
         uint8_t buf[1024];
         int n;
@@ -93,7 +93,7 @@ int main(int argc, char **argv)
         stream_pos += n;
         if (bps) {
             avio_flush(output);
-            while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
+            while ((av_gettime() - start_time) * bps / AV_TIME_BASE < stream_pos)
                 av_usleep(50 * 1000);
         }
     }
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list