Improve TIMESPEC_TO_TIME

Add explicit TO_NSEC and TO_USEC versions to make it clearer and to
allow for some optimizations.
This commit is contained in:
Wim Taymans 2018-11-07 09:56:24 +01:00
parent bf6843743a
commit 088ee9f09e
8 changed files with 20 additions and 18 deletions

View file

@ -129,8 +129,10 @@ struct spa_fraction {
#define SPA_USEC_PER_MSEC (1000ll)
#define SPA_MSEC_PER_SEC (1000ll)
#define SPA_TIMESPEC_TO_TIME(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
#define SPA_TIMEVAL_TO_TIME(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * 1000ll)
#define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
#define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
#define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
#define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
#ifdef __GNUC__
#define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))

View file

@ -643,7 +643,7 @@ push_frames(struct state *state,
if (b->h) {
b->h->seq = state->sample_count;
b->h->pts = SPA_TIMESPEC_TO_TIME(&state->now);
b->h->pts = SPA_TIMESPEC_TO_NSEC(&state->now);
b->h->dts_offset = 0;
}
@ -716,7 +716,7 @@ static void alsa_on_playback_timeout_event(struct spa_source *source)
state->filled = state->buffer_frames - avail;
if (state->clock) {
state->clock->nsec = SPA_TIMESPEC_TO_TIME(&state->now);
state->clock->nsec = SPA_TIMESPEC_TO_NSEC(&state->now);
state->clock->rate = SPA_FRACTION(1, state->rate);
state->clock->position = state->sample_count;
state->clock->delay = state->filled;
@ -789,10 +789,10 @@ static void alsa_on_capture_timeout_event(struct spa_source *source)
avail = snd_pcm_status_get_avail(status);
snd_pcm_status_get_htstamp(status, &state->now);
clock_gettime(CLOCK_MONOTONIC, &now);
clock_gettime(CLOCK_MONOTONIC, &state->now);
if (state->clock) {
state->clock->nsec = SPA_TIMESPEC_TO_TIME(&state->now);
state->clock->nsec = SPA_TIMESPEC_TO_NSEC(&state->now);
state->clock->rate = SPA_FRACTION(1, state->rate);
state->clock->position = state->sample_count;
state->clock->delay = avail;

View file

@ -1157,7 +1157,7 @@ static int mmap_read(struct impl *this)
if (xioctl(port->fd, VIDIOC_DQBUF, &buf) < 0)
return -errno;
pts = SPA_TIMEVAL_TO_TIME(&buf.timestamp);
pts = SPA_TIMEVAL_TO_NSEC(&buf.timestamp);
if (this->clock) {
this->clock->nsec = pts;

View file

@ -465,7 +465,7 @@ static void run_graph(struct data *data)
}
clock_gettime(CLOCK_MONOTONIC, &now);
start = SPA_TIMESPEC_TO_TIME(&now);
start = SPA_TIMESPEC_TO_NSEC(&now);
printf("running\n");
@ -487,7 +487,7 @@ static void run_graph(struct data *data)
}
clock_gettime(CLOCK_MONOTONIC, &now);
stop = SPA_TIMESPEC_TO_TIME(&now);
stop = SPA_TIMESPEC_TO_NSEC(&now);
printf("stopping, elapsed %" PRIi64 "\n", stop - start);

View file

@ -333,7 +333,7 @@ int main(int argc, char *argv[])
fmt = spa_pod_builder_pop(&b);
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
fprintf(stderr, "build 2: ");
@ -356,7 +356,7 @@ int main(int argc, char *argv[])
&SPA_FRACTION(INT32_MAX,1));
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
spa_debug_format(0, NULL, &fmt->pod);
@ -385,7 +385,7 @@ int main(int argc, char *argv[])
"}", NULL);
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
spa_debug_format(0, NULL, &fmt->pod);
@ -417,7 +417,7 @@ int main(int argc, char *argv[])
fmt = spa_pod_builder_pop(&b);
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
spa_debug_format(0, NULL, &fmt->pod);
@ -445,7 +445,7 @@ int main(int argc, char *argv[])
0);
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
spa_debug_format(0, NULL, &fmt->pod);
@ -472,7 +472,7 @@ int main(int argc, char *argv[])
fmt = spa_pod_builder_pop(&b);
}
clock_gettime(CLOCK_MONOTONIC, &ts2);
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_TIME(&ts2) - SPA_TIMESPEC_TO_TIME(&ts1));
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
spa_debug_pod(0, NULL, &fmt->pod);
spa_debug_format(0, NULL, &fmt->pod);