FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/time.c
Date: 2026-08-31 17:12:07
Exec Total Coverage
Lines: 10 12 83.3%
Functions: 3 4 75.0%
Branches: 1 4 25.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2003 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <time.h>
26 #if HAVE_GETTIMEOFDAY
27 #include <sys/time.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #if HAVE_WINDOWS_H
33 #include <windows.h>
34 #endif
35
36 #include "time.h"
37 #include "error.h"
38 #include "mathematics.h"
39
40 29993 int64_t av_gettime(void)
41 {
42 #if HAVE_GETTIMEOFDAY
43 struct timeval tv;
44 29993 gettimeofday(&tv, NULL);
45 29993 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
46 #elif HAVE_GETSYSTEMTIMEASFILETIME
47 FILETIME ft;
48 int64_t t;
49 GetSystemTimeAsFileTime(&ft);
50 t = (int64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
51 return t / 10 - 11644473600000000; /* Jan 1, 1601 */
52 #else
53 return -1;
54 #endif
55 }
56
57 3439104 int64_t av_gettime_relative(void)
58 {
59 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
60 #ifdef __APPLE__
61 if (&clock_gettime)
62 #endif
63 {
64 struct timespec ts;
65 3439104 clock_gettime(CLOCK_MONOTONIC, &ts);
66 3439104 return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
67 }
68 #elif defined(_WIN32)
69 LARGE_INTEGER freq;
70 LARGE_INTEGER counter;
71 QueryPerformanceFrequency(&freq);
72 QueryPerformanceCounter(&counter);
73 return av_rescale(counter.QuadPart, 1000000, freq.QuadPart);
74 #endif
75 return av_gettime() + 42 * 60 * 60 * INT64_C(1000000);
76 }
77
78 int av_gettime_relative_is_monotonic(void)
79 {
80 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
81 #ifdef __APPLE__
82 if (!&clock_gettime)
83 return 0;
84 #endif
85 return 1;
86 #elif defined(_WIN32)
87 return 1;
88 #else
89 return 0;
90 #endif
91 }
92
93 209 int av_usleep(unsigned usec)
94 {
95 #if HAVE_NANOSLEEP
96 209 struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
97
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 209 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
209 while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
98 209 return 0;
99 #elif HAVE_USLEEP
100 return usleep(usec);
101 #elif HAVE_SLEEP
102 Sleep(usec / 1000);
103 return 0;
104 #else
105 return AVERROR(ENOSYS);
106 #endif
107 }
108