From 24cf97b0ae4b649dc219f430e5b6ff6c62e841ee Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 24 Feb 2016 13:55:35 -0500 Subject: [PATCH] Only draw frames that are newer than the last frame we drew --- viddec.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/viddec.cpp b/viddec.cpp index 8b87e14..9e638c5 100644 --- a/viddec.cpp +++ b/viddec.cpp @@ -13,6 +13,8 @@ static unsigned char* s_DecodeBuffer; static unsigned int s_DecodeBufferLength; static int s_LastTextureType; static int s_LastTextureId; +static int s_NextDecodeFrameNumber; +static int s_LastDisplayFrameNumber; static unsigned char s_LastSps[256]; static unsigned char s_LastPps[256]; static unsigned int s_LastSpsLength; @@ -146,6 +148,8 @@ void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* s_LastTextureId = 0; s_LastSpsLength = 0; s_LastPpsLength = 0; + s_NextDecodeFrameNumber = 0; + s_LastDisplayFrameNumber = 0; g_Instance->m_VideoDecoder->Initialize(g_Instance->m_Graphics3D, PP_VIDEOPROFILE_H264HIGH, @@ -284,7 +288,7 @@ int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) { } // Start the decoding - g_Instance->m_VideoDecoder->Decode(0, offset, s_DecodeBuffer, pp::BlockUntilComplete()); + g_Instance->m_VideoDecoder->Decode(s_NextDecodeFrameNumber++, offset, s_DecodeBuffer, pp::BlockUntilComplete()); return DR_OK; } @@ -415,12 +419,21 @@ void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) { return; } - m_PendingPictureQueue.push(picture); + // Ensure we only push newer frames onto the display queue + if (picture.decode_id > s_LastDisplayFrameNumber) { + m_PendingPictureQueue.push(picture); + s_LastDisplayFrameNumber = picture.decode_id; + } + else { + // This picture is older than the last one we displayed. Discard + // it without displaying. + g_Instance->m_VideoDecoder->RecyclePicture(picture); + } g_Instance->m_VideoDecoder->GetPicture( g_Instance->m_CallbackFactory.NewCallbackWithOutput(&MoonlightInstance::PictureReady)); - if (!m_IsPainting) { + if (!m_IsPainting && !m_PendingPictureQueue.empty()) { PaintPicture(); } }