From da780309d707ac995941c14d1f5bbc8f1a7239b8 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 30 Jul 2014 00:40:45 +0200 Subject: audio: better initial sync for files where audio starts later Some files have the first audio much later into the video (for whatever reasons). Instead of appending large amounts of silence to the audio buffer (and refusing to sync if the audio to append is "too large"), just wait until enough video has played. --- player/audio.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/player/audio.c b/player/audio.c index b5eb8884f9..f371b69bb4 100644 --- a/player/audio.c +++ b/player/audio.c @@ -405,6 +405,15 @@ void fill_audio_out_buffers(struct MPContext *mpctx, double endpts) } } + // If EOF was reached before, but now something can be decoded, try to + // restart audio properly. This helps with video files where audio starts + // later. Retrying is needed to get the correct sync PTS. + if (mpctx->audio_status == STATUS_EOF && status == AD_OK) { + mpctx->audio_status = STATUS_SYNCING; + mpctx->sleeptime = 0; + return; // retry on next iteration + } + bool end_sync = status != AD_OK; // (on error/EOF, start playback immediately) if (skip >= 0) { int max = mp_audio_buffer_samples(mpctx->ao_buffer); @@ -412,12 +421,13 @@ void fill_audio_out_buffers(struct MPContext *mpctx, double endpts) // If something is left, we definitely reached the target time. end_sync |= sync_known && skip < max; } else if (skip < 0) { - if (-skip < 1000000) { // heuristic against making the buffer too large - mp_audio_buffer_prepend_silence(mpctx->ao_buffer, -skip); - } else { - MP_ERR(mpctx, "Audio starts too late: sync. failed.\n"); - ao_reset(mpctx->ao); + if (-skip > playsize) { // heuristic against making the buffer too large + ao_reset(mpctx->ao); // some AOs repeat data on underflow + mpctx->audio_status = STATUS_EOF; + mpctx->delay = 0; + return; } + mp_audio_buffer_prepend_silence(mpctx->ao_buffer, -skip); end_sync = true; } -- cgit v1.2.3