Mercurial > mplayer.hg
annotate osdep/timer-darwin.c @ 25823:60fda6c0c7dc
set -vc=ffmpeg12 as dvdnav prefered decoder
author | ben |
---|---|
date | Fri, 25 Jan 2008 19:27:42 +0000 |
parents | 531116b7693d |
children | 8a26190978a6 |
rev | line source |
---|---|
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
1 /* |
12954 | 2 * Precise timer routines using Mach timing |
3 * | |
4 * Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
5 * |
12954 | 6 * Permission is hereby granted, free of charge, to any person |
7 * obtaining a copy of this software and associated documentation | |
8 * files (the "Software"), to deal in the Software without | |
9 * restriction, including without limitation the rights to use, copy, | |
10 * modify, merge, publish, distribute, sublicense, and/or sell copies | |
11 * of the Software, and to permit persons to whom the Software is | |
12 * furnished to do so, subject to the following conditions: | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
13 * |
12954 | 14 * The above copyright notice and this permission notice shall be |
15 * included in all copies or substantial portions of the Software. | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
16 */ |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
17 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
18 #include <unistd.h> |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
19 #include <stdlib.h> |
12954 | 20 #include <time.h> |
21 #include <math.h> | |
22 #include <sys/time.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
23 #include <mach/mach_time.h> |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
24 |
16985 | 25 #include "config.h" |
26 #include "mp_msg.h" | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
27 #include "timer.h" |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
28 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
29 /* global variables */ |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
30 static double relative_time, startup_time; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
31 static double timebase_ratio; |
12954 | 32 |
33 const char *timer_name = "Darwin accurate"; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
34 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
35 |
12954 | 36 |
15275 | 37 /* the core sleep function, uses floats and is used in MPlayer G2 */ |
38 float sleep_accurate(float time_frame) | |
39 { | |
40 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time(); | |
41 | |
42 mach_wait_until(deadline); | |
43 | |
44 return (mach_absolute_time() - deadline) * timebase_ratio; | |
45 } | |
46 | |
12954 | 47 /* wrapper for MPlayer G1 */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
48 int usec_sleep(int usec_delay) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
49 { |
15275 | 50 return sleep_accurate(usec_delay / 1e6) * 1e6; |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
51 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
52 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
53 |
12954 | 54 /* current time in microseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
55 unsigned int GetTimer() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
56 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
57 return (unsigned int)((mach_absolute_time() * timebase_ratio - startup_time) |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
58 * 1e6); |
12954 | 59 } |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
60 |
12954 | 61 /* current time in milliseconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
62 unsigned int GetTimerMS() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
63 { |
24815
c10db72e1afc
Fix GetTimerMS() discontinuous return value bug caused by GetTimer() overflow
ulion
parents:
16985
diff
changeset
|
64 return (unsigned int)((mach_absolute_time() * timebase_ratio - startup_time) |
c10db72e1afc
Fix GetTimerMS() discontinuous return value bug caused by GetTimer() overflow
ulion
parents:
16985
diff
changeset
|
65 * 1e3); |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
66 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
67 |
12954 | 68 /* time spent between now and last call in seconds */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
69 float GetRelativeTime() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
70 { |
12954 | 71 double last_time = relative_time; |
72 | |
73 if (!relative_time) | |
74 InitTimer(); | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
75 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
76 relative_time = mach_absolute_time() * timebase_ratio; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
77 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
78 return (float)(relative_time-last_time); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
79 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
80 |
12954 | 81 /* initialize timer, must be called at least once at start */ |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
82 void InitTimer() |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
83 { |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
84 struct mach_timebase_info timebase; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
85 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
86 mach_timebase_info(&timebase); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
87 timebase_ratio = (double)timebase.numer / (double)timebase.denom |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
88 * (double)1e-9; |
12954 | 89 |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
90 relative_time = startup_time = |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
91 (double)(mach_absolute_time() * timebase_ratio); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
92 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
93 |
12954 | 94 #if 0 |
95 #include <stdio.h> | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
96 |
25100 | 97 int main(void) { |
12954 | 98 int i,j, r, c = 200; |
99 long long t = 0; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
100 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
101 InitTimer(); |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
102 |
12954 | 103 for (i = 0; i < c; i++) { |
104 const int delay = rand() / (RAND_MAX / 1e5); | |
105 j = GetTimer(); | |
106 #if 1 | |
107 r = usec_sleep(delay); | |
108 #else | |
109 r = sleep_accurate(delay / 1e6) * 1e6; | |
110 #endif | |
111 j = (GetTimer() - j) - delay; | |
112 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r); | |
113 t += j - r; | |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
114 } |
12954 | 115 fprintf(stderr, "average error:\t%lli\n", t / c); |
10148
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
116 |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
117 return 0; |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
118 } |
139b44930abc
Precise timer for Darwin (it's more accurate than timer-macosx.c)
alex
parents:
diff
changeset
|
119 #endif |