From 94c0dad9d6de01f58cad5c7497806fad24a64445 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 12 Feb 2019 10:01:13 +0100 Subject: player: Use floating point arithmetic instead of AVRational av_mul_q calls av_reduce, whose runtime depends on its inputs. This is a latency-sensitive calculation though. --- src/player.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/player.c b/src/player.c index d8ff969..17fe7ed 100644 --- a/src/player.c +++ b/src/player.c @@ -382,6 +382,7 @@ static int play (player_t * const player) { pthread_create (&aoplaythread, NULL, BarAoPlayThread, player); enum { FILL, DRAIN, DONE } drainMode = FILL; int ret = 0; + const double timeBase = av_q2d (player->st->time_base); while (!shouldQuit (player) && drainMode != DONE) { if (drainMode == FILL) { ret = av_read_frame (player->fctx, &pkt); @@ -437,8 +438,7 @@ static int play (player_t * const player) { int64_t bufferHealth = 0; do { pthread_mutex_lock (&player->aoplayLock); - bufferHealth = av_q2d (player->st->time_base) * - (double) (frame->pts - player->lastTimestamp); + bufferHealth = timeBase * (double) (frame->pts - player->lastTimestamp); if (bufferHealth > minBufferHealth) { /* Buffer get healthy, resume */ pthread_cond_broadcast (&player->aoplayCond); @@ -519,7 +519,8 @@ void *BarAoPlayThread (void *data) { assert (filteredFrame != NULL); int ret; - const AVRational timeBase = av_buffersink_get_time_base (player->fbufsink); + const double timeBase = av_q2d (av_buffersink_get_time_base (player->fbufsink)), + timeBaseSt = av_q2d (player->st->time_base); while (!shouldQuit(player)) { pthread_mutex_lock (&player->aoplayLock); ret = av_buffersink_get_frame (player->fbufsink, filteredFrame); @@ -542,10 +543,8 @@ void *BarAoPlayThread (void *data) { ao_play (player->aoDev, (char *) filteredFrame->data[0], filteredFrame->nb_samples * numChannels * bps); - /* we could also use av_q2d here and use double arithmetic */ - const AVRational ptsQ = av_make_q (filteredFrame->pts, 1); - const AVRational timestampQ = av_mul_q (timeBase, ptsQ); - const unsigned int songPlayed = timestampQ.num/timestampQ.den; + const double timestamp = (double) filteredFrame->pts * timeBase; + const unsigned int songPlayed = timestamp; pthread_mutex_lock (&player->lock); player->songPlayed = songPlayed; @@ -559,8 +558,7 @@ void *BarAoPlayThread (void *data) { /* lastTimestamp must be the last pts, but expressed in terms of * st->time_base, not the sink’s time_base. */ - AVRational lastTimestampQ = av_mul_q (timestampQ, av_inv_q (player->st->time_base)); - const int64_t lastTimestamp = lastTimestampQ.num/lastTimestampQ.den; + const int64_t lastTimestamp = timestamp/timeBaseSt; /* notify download thread, we might need more data */ pthread_mutex_lock (&player->aoplayLock); player->lastTimestamp = lastTimestamp; -- cgit v1.2.3