From 2c3880579176cd210553aacf27a6ae62d5edae75 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 1 Jan 2014 20:47:10 -0800 Subject: [PATCH] Retry opening the context if it fails at first. This fixes #4550, the hang on the crash video in FF4. --- Core/HW/MediaEngine.cpp | 55 ++++++++++++++++++++++------------------- Core/HW/MediaEngine.h | 1 + 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Core/HW/MediaEngine.cpp b/Core/HW/MediaEngine.cpp index 79f4fcfd7e..4f079536c2 100644 --- a/Core/HW/MediaEngine.cpp +++ b/Core/HW/MediaEngine.cpp @@ -152,34 +152,11 @@ MediaEngine::~MediaEngine() { } void MediaEngine::closeMedia() { -#ifdef USE_FFMPEG - if (m_buffer) - av_free(m_buffer); - if (m_pFrameRGB) - av_free(m_pFrameRGB); - if (m_pFrame) - av_free(m_pFrame); - if (m_pIOContext && m_pIOContext->buffer) - av_free(m_pIOContext->buffer); - if (m_pIOContext) - av_free(m_pIOContext); - if (m_pCodecCtx) - avcodec_close(m_pCodecCtx); - if (m_pFormatCtx) - avformat_close_input(&m_pFormatCtx); -#endif // USE_FFMPEG + closeContext(); if (m_pdata) delete m_pdata; if (m_demux) delete m_demux; - m_buffer = 0; -#ifdef USE_FFMPEG - m_pFrame = 0; - m_pFrameRGB = 0; - m_pIOContext = 0; - m_pCodecCtx = 0; - m_pFormatCtx = 0; -#endif m_pdata = 0; m_demux = 0; AT3Close(&m_audioContext); @@ -267,8 +244,10 @@ bool MediaEngine::openContext() { if (avformat_open_input((AVFormatContext**)&m_pFormatCtx, NULL, NULL, NULL) != 0) return false; - if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0) + if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0) { + closeContext(); return false; + } if (m_videoStream >= (int)m_pFormatCtx->nb_streams) { WARN_LOG_REPORT(ME, "Bad video stream %d", m_videoStream); @@ -310,6 +289,32 @@ bool MediaEngine::openContext() { return true; } +void MediaEngine::closeContext() +{ +#ifdef USE_FFMPEG + if (m_buffer) + av_free(m_buffer); + if (m_pFrameRGB) + av_free(m_pFrameRGB); + if (m_pFrame) + av_free(m_pFrame); + if (m_pIOContext && m_pIOContext->buffer) + av_free(m_pIOContext->buffer); + if (m_pIOContext) + av_free(m_pIOContext); + if (m_pCodecCtx) + avcodec_close(m_pCodecCtx); + if (m_pFormatCtx) + avformat_close_input(&m_pFormatCtx); + m_pFrame = 0; + m_pFrameRGB = 0; + m_pIOContext = 0; + m_pCodecCtx = 0; + m_pFormatCtx = 0; +#endif + m_buffer = 0; +} + bool MediaEngine::loadStream(u8* buffer, int readSize, int RingbufferSize) { closeMedia(); diff --git a/Core/HW/MediaEngine.h b/Core/HW/MediaEngine.h index 2d1fd55d23..3969f5901a 100644 --- a/Core/HW/MediaEngine.h +++ b/Core/HW/MediaEngine.h @@ -60,6 +60,7 @@ public: bool loadStream(u8* buffer, int readSize, int RingbufferSize); // open the mpeg context bool openContext(); + void closeContext(); // Returns number of packets actually added. I guess the buffer might be full. int addStreamData(u8* buffer, int addSize);