From 1a8e28bf721b84312a0a0f91a3431194dfd40c39 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Mon, 8 Aug 2011 12:39:51 +0430 Subject: [PATCH] improve the audio/video sync It's still ad hoc; the 12 is there for the oss playback delay (almost half a second). It seems to work a lot better for most files, anyway. --- ffs.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/ffs.c b/ffs.c index aee0fba..6c50785 100644 --- a/ffs.c +++ b/ffs.c @@ -84,22 +84,26 @@ static long ts_ms(void) return tv.tv_sec * 1000 + tv.tv_usec / 1000; } -static void wait(long ts, int vdelay) +static int wait(long ts, int vdelay) { int nts = ts_ms(); - if (ts + vdelay > nts) + if (nts > ts && ts + vdelay > nts) { usleep((ts + vdelay - nts) * 1000); + return 0; + } + return 1; } +#define MAX(a, b) ((a) < (b) ? (b) : (a)) + void ffs_wait(struct ffs *ffs) { - long ts = ts_ms(); - if (ts && ffs->ts) { - AVRational *r = &ffs->fc->streams[ffs->si]->time_base; - int vdelay = 1000 * r->num / r->den; - wait(ffs->ts, vdelay < 20 ? 20 : vdelay); - } - ffs->ts = ts_ms(); + AVRational *r = &ffs->fc->streams[ffs->si]->time_base; + int vdelay = 1000 * r->num / r->den; + if (!wait(ffs->ts, MAX(vdelay, 20))) + ffs->ts += MAX(vdelay, 20); + else + ffs->ts = ts_ms(); /* out of sync */ } long ffs_pos(struct ffs *ffs, int diff) @@ -126,7 +130,7 @@ int ffs_vsync(struct ffs *ffs, struct ffs *affs, int abufs) int all = affs->seq_all; int video = cur ? (all - cur) * abufs / cur : abufs; /* video ffs should wait for audio ffs (ignoring buffered packets) */ - return ffs->seq_all + abufs + video < affs->seq_all; + return ffs->seq_all - abufs - video - 12 < affs->seq_all; } void ffs_vinfo(struct ffs *ffs, int *w, int *h) -- 2.11.4.GIT