From c3b924a2393c46513e5b02d5b821084d668e0bb9 Mon Sep 17 00:00:00 2001 From: Johns Date: Fri, 27 Jan 2012 23:33:10 +0100 Subject: [PATCH] Reduces audio latency, increases audio buffer time. --- ChangeLog | 1 + Makefile | 2 +- audio.c | 30 ++++++++---------------------- audio.h | 2 -- softhddev.c | 9 ++++++--- video.c | 4 +++- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index 391cbc5..140e85a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ User johns Date: + Reduces audio latency, increases audio buffer time. Made video_test working again. Disabled VA-API Intel vaAssociateSubpicture workaround. Fix bug: Must release lock for VideoPollEvent. diff --git a/Makefile b/Makefile index deed91f..257dae0 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ GIT_REV = $(shell git describe --always 2>/dev/null) ### Configuration (edit this for your needs) -CONFIG := -DDEBUG +CONFIG := #-DDEBUG #CONFIG += -DHAVE_PTHREAD_NAME CONFIG += $(shell pkg-config --exists vdpau && echo "-DUSE_VDPAU") CONFIG += $(shell pkg-config --exists libva && echo "-DUSE_VAAPI") diff --git a/audio.c b/audio.c index 2142b5a..6c41503 100644 --- a/audio.c +++ b/audio.c @@ -137,8 +137,7 @@ static unsigned AudioSampleRate; ///< audio sample rate in hz static unsigned AudioChannels; ///< number of audio channels static const int AudioBytesProSample = 2; ///< number of bytes per sample static int64_t AudioPTS; ///< audio pts clock -static const int AudioBufferTime = 300; ///< audio buffer time in ms -static int AudioMoreBufferTime = 1; ///< increase buffer time +static const int AudioBufferTime = 450; ///< audio buffer time in ms #ifdef USE_AUDIO_THREAD static pthread_t AudioThread; ///< audio play thread @@ -1087,13 +1086,11 @@ static int AlsaSetup(int *freq, int *channels, int use_ac3) AlsaStartThreshold = snd_pcm_frames_to_bytes(AlsaPCMHandle, period_size); // buffer time/delay in ms if (AlsaStartThreshold < - (*freq * *channels * AudioBytesProSample * AudioMoreBufferTime * - AudioBufferTime) / 1000U) { + (*freq * *channels * AudioBytesProSample * AudioBufferTime) / 1000U) { AlsaStartThreshold = - (*freq * *channels * AudioBytesProSample * AudioMoreBufferTime * - AudioBufferTime) / 1000U; + (*freq * *channels * AudioBytesProSample * AudioBufferTime) / + 1000U; } - AudioMoreBufferTime = 1; // no bigger, than the buffer if (AlsaStartThreshold > RingBufferFreeBytes(AlsaRingBuffer)) { AlsaStartThreshold = RingBufferFreeBytes(AlsaRingBuffer); @@ -1712,13 +1709,12 @@ static int OssSetup(int *freq, int *channels, int use_ac3) OssStartThreshold = bi.bytes + tmp; // buffer time/delay in ms if (OssStartThreshold < - (*freq * *channels * AudioBytesProSample * AudioMoreBufferTime * - AudioBufferTime) / 1000U) { + (*freq * *channels * AudioBytesProSample * AudioBufferTime) / + 1000U) { OssStartThreshold = - (*freq * *channels * AudioBytesProSample * - AudioMoreBufferTime * AudioBufferTime) / 1000U; + (*freq * *channels * AudioBytesProSample * AudioBufferTime) / + 1000U; } - AudioMoreBufferTime = 1; // no bigger, than the buffer if (OssStartThreshold > RingBufferFreeBytes(OssRingBuffer)) { OssStartThreshold = RingBufferFreeBytes(OssRingBuffer); @@ -2101,16 +2097,6 @@ int AudioSetup(int *freq, int *channels, int use_ac3) return AudioUsedModule->Setup(freq, channels, use_ac3); } -/** -** Increase audio buffer time. -** -** Some channels need a bigger audio buffer to buffer video. -*/ -void AudioIncreaseBufferTime(void) -{ - AudioMoreBufferTime = 4; -} - /** ** Set pcm audio device. ** diff --git a/audio.h b/audio.h index 8924159..2a4c45f 100644 --- a/audio.h +++ b/audio.h @@ -42,8 +42,6 @@ extern int AudioSetup(int *, int *, int); ///< setup audio output //extern void AudioPlay(void); ///< play audio //extern void AudioPause(void); ///< pause audio -extern void AudioIncreaseBufferTime(void); ///< use bigger buffer - extern void AudioSetDevice(const char *); ///< set PCM audio device extern void AudioSetDeviceAC3(const char *); ///< set Passthrough device extern void AudioInit(void); ///< setup audio module diff --git a/softhddev.c b/softhddev.c index 5abdd11..60d70c9 100644 --- a/softhddev.c +++ b/softhddev.c @@ -292,7 +292,6 @@ int PlayAudio(const uint8_t * data, int size, } avpkt->pts = AV_NOPTS_VALUE; - AudioIncreaseBufferTime(); CodecAudioOpen(MyAudioDecoder, NULL, CODEC_ID_MP2); AudioCodecID = CODEC_ID_MP2; data += n; @@ -794,11 +793,15 @@ int PlayVideo(const uint8_t * data, int size) Debug(3, "video: not detected\n"); return size; } - // FIXME: incomplete packets produce artefacts after channel switch - if (0 && VideoCodecID == CODEC_ID_MPEG2VIDEO) { + // incomplete packets produce artefacts after channel switch + // packet < 65526 is the last split packet, detect it here for + // better latency + if (size < 65526 && VideoCodecID == CODEC_ID_MPEG2VIDEO) { // mpeg codec supports incomplete packets // waiting for a full complete packages, increases needed delays + VideoEnqueue(pts, check, size - 9 - n); VideoNextPacket(CODEC_ID_MPEG2VIDEO); + return size; } } diff --git a/video.c b/video.c index 6e62d5c..bf946e0 100644 --- a/video.c +++ b/video.c @@ -3471,7 +3471,8 @@ static void VaapiAdvanceFrame(void) } // debug duplicate frames } else if (filled == 1) { - decoder->FramesDuped++; + ++decoder->FramesDuped; + decoder->DropNextFrame = 0; Warning(_ ("video: display buffer empty, duping frame (%d/%d) %d\n"), decoder->FramesDuped, decoder->FrameCounter, @@ -6423,6 +6424,7 @@ static void VdpauAdvanceFrame(void) if (filled <= 1 + 2 * decoder->Interlaced) { // keep use of last surface ++decoder->FramesDuped; + decoder->DropNextFrame = 0; Warning(_ ("video: display buffer empty, duping frame (%d/%d) %d\n"), decoder->FramesDuped, decoder->FrameCounter,