From 1edb0fe1af61b9e5c872ad04cab86d4a6dbb4f73 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 29 Sep 2014 22:37:41 +0100 Subject: [PATCH] Separate the open and close code from the main functions --- source/lib/PlaYUVerStream.cpp | 44 ++++++++++++++++++++++++++++------- source/lib/PlaYUVerStream.h | 5 ++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/source/lib/PlaYUVerStream.cpp b/source/lib/PlaYUVerStream.cpp index 5349bab..3f283c5 100644 --- a/source/lib/PlaYUVerStream.cpp +++ b/source/lib/PlaYUVerStream.cpp @@ -38,6 +38,7 @@ PlaYUVerStream::PlaYUVerStream() { m_bInit = false; m_bIsInput = true; + m_bIsOpened = false; m_bLoadAll = false; m_iErrorStatus = 0; @@ -151,14 +152,20 @@ Bool PlaYUVerStream::open( QString filename, UInt width, UInt height, Int input_ UInt64 frame_bytes_input = m_pcCurrFrame->getBytesPerFrame(); + if( !avStatus ) + { + if( !openFile() ) + { + close(); + return m_bInit; + } + m_cFormatName = QString::fromUtf8( "YUV" ); + m_cCodedName = QString::fromUtf8( "Raw Video" ); + } + if( !avStatus && m_iFileFormat == YUVINPUT ) { - m_pFile = NULL; - Char *filename = new char[m_cFilename.length() + 1]; - memcpy( filename, m_cFilename.toUtf8().constData(), m_cFilename.length() + 1 * sizeof(char) ); - m_pFile = fopen( filename, m_bIsInput ? "rb" : "wb" ); - delete[] filename; - if( m_pFile == NULL ) + if( !openFile() ) { close(); return m_bInit; @@ -218,13 +225,34 @@ Bool PlaYUVerStream::open( QString filename, UInt width, UInt height, Int input_ return m_bInit; } +Bool PlaYUVerStream::openFile() +{ + m_pFile = NULL; + Char *filename = new char[m_cFilename.length() + 1]; + memcpy( filename, m_cFilename.toUtf8().constData(), m_cFilename.length() + 1 * sizeof(char) ); + m_pFile = fopen( filename, m_bIsInput ? "rb" : "wb" ); + delete[] filename; + if( m_pFile == NULL ) + { + return false; + } + fseek( m_pFile, m_iCurrFrameNum >= 0 ? m_iCurrFrameNum : 0, SEEK_SET ); + return true; +} + +Void PlaYUVerStream::closeFile() +{ + if( !m_bInit && m_pFile ) + return; + fclose( m_pFile ); +} + Void PlaYUVerStream::close() { if( !m_bInit ) return; - if( m_pFile ) - fclose( m_pFile ); + closeFile(); #ifdef USE_FFMPEG m_cLibAvContext->closeAvFormat(); diff --git a/source/lib/PlaYUVerStream.h b/source/lib/PlaYUVerStream.h index e8cd5d8..c420779 100644 --- a/source/lib/PlaYUVerStream.h +++ b/source/lib/PlaYUVerStream.h @@ -42,6 +42,8 @@ private: Bool m_bInit; Bool m_bIsInput; + Bool m_bIsOpened; + Bool m_bLoadAll; Int m_iErrorStatus; @@ -216,6 +218,9 @@ public: Bool open( QString filename, UInt width, UInt height, Int input_format, UInt frame_rate, Bool bInput = true ); Void close(); + Bool openFile(); + Void closeFile(); + static Bool guessFormat( QString filename, UInt& rWidth, UInt& rHeight, Int& rInputFormat, UInt& rFrameRate ); Void getFormat( UInt& rWidth, UInt& rHeight, Int& rInputFormat, UInt& rFrameRate );