From b7ad00a4c5099535abd37a34412ca2ed0face393 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sun, 10 Oct 2021 19:48:14 +0100 Subject: [PATCH] Minor refactor CalypStream header and format handler --- .clang-tidy | 2 +- lib/CalypFrame.h | 1 + lib/CalypStream.cpp | 94 ++++++++++++++++++++------------------ lib/CalypStream.h | 35 ++++++++------ lib/CalypStreamHandlerIf.h | 70 +++++++++++++--------------- lib/StreamHandlerRaw.cpp | 6 +++ lib/StreamHandlerRaw.h | 2 +- 7 files changed, 113 insertions(+), 97 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index dc6a817..2a4dfb5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-pointer-arithmetic' +Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-non-private-member-variables-in-classes' WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false diff --git a/lib/CalypFrame.h b/lib/CalypFrame.h index ec3d871..931164e 100644 --- a/lib/CalypFrame.h +++ b/lib/CalypFrame.h @@ -93,6 +93,7 @@ enum CLP_RGB_Components enum CLP_Endianness { + CLP_INVALID_ENDIANESS = -1, CLP_BIG_ENDIAN = 0, CLP_LITTLE_ENDIAN = 1, }; diff --git a/lib/CalypStream.cpp b/lib/CalypStream.cpp index 28893f6..7e6f54a 100644 --- a/lib/CalypStream.cpp +++ b/lib/CalypStream.cpp @@ -48,6 +48,8 @@ constexpr auto kDefaultBitsPerPixel = 8; constexpr auto kDefaultFrameRate = 30; +auto find_stream_handler( std::string strFilename, bool bRead ) -> CalypStreamFormat::CreateStreamHandlerFn; + std::vector CalypStream::supportedReadFormats() { INI_REGIST_CALYP_SUPPORTED_FMT; @@ -166,8 +168,7 @@ public: bool isInit; bool isInput; - CalypStreamHandlerIf* handler; - CreateStreamHandlerFn pfctCreateHandler; + std::unique_ptr handler; std::shared_ptr frameBuffer; std::deque> frameFifo; @@ -187,7 +188,6 @@ public: CalypStreamPrivate() { handler = nullptr; - pfctCreateHandler = nullptr; isInput = true; isInit = false; bLoadAll = false; @@ -205,7 +205,6 @@ public: if( handler ) { handler->closeHandler(); - handler->Delete(); } bLoadAll = false; @@ -253,43 +252,6 @@ std::vector CalypStreamFormat::getExts() return arrayExt; } -CreateStreamHandlerFn CalypStream::findStreamHandler( std::string strFilename, bool bRead ) -{ - std::string currExt = strFilename.substr( strFilename.find_last_of( "." ) + 1 ); - currExt = clpLowercase( currExt ); - - std::vector supportedFmts; - if( bRead ) - { - supportedFmts = CalypStream::supportedReadFormats(); - } - else - { - supportedFmts = CalypStream::supportedWriteFormats(); - } - for( unsigned int i = 0; i < supportedFmts.size(); i++ ) - { - std::vector arrayExt = supportedFmts[i].getExts(); - for( std::vector::iterator e = arrayExt.begin(); e != arrayExt.end(); ++e ) - { - if( currExt != "" && currExt == *e ) - { - return supportedFmts[i].formatFct; - } - else if( strFilename.find( *e ) != std::string::npos ) - { - return supportedFmts[i].formatFct; - } - } - } - -#ifdef USE_FFMPEG - return &StreamHandlerLibav::Create; -#else - return &StreamHandlerRaw::Create; -#endif -} - CalypStream::CalypStream() : d{ std::make_unique() } { @@ -348,13 +310,13 @@ bool CalypStream::open( std::string filename, unsigned int width, unsigned int h d->isInit = false; d->isInput = bInput; - d->pfctCreateHandler = CalypStream::findStreamHandler( filename, d->isInput ); - if( !d->pfctCreateHandler ) + auto createHandlerFct = find_stream_handler( filename, d->isInput ); + if( !createHandlerFct ) { throw CalypFailure( "CalypStream", "Invalid handler" ); } - d->handler = d->pfctCreateHandler(); + d->handler = createHandlerFct(); if( !d->handler ) { @@ -434,6 +396,13 @@ bool CalypStream::open( std::string filename, unsigned int width, unsigned int h return d->isInit; } +bool CalypStream::supportsFormating() +{ + if( d->handler != nullptr ) + return false; + return d->handler->m_bSupportsFormat; +} + bool CalypStream::reload() { d->frameFifo.clear(); @@ -695,3 +664,40 @@ bool CalypStream::seekInput( unsigned long new_frame_num ) d->readNextFrame(); return true; } + +auto find_stream_handler( std::string strFilename, bool bRead ) -> CalypStreamFormat::CreateStreamHandlerFn +{ + std::string currExt = strFilename.substr( strFilename.find_last_of( "." ) + 1 ); + currExt = clpLowercase( currExt ); + + std::vector supportedFmts; + if( bRead ) + { + supportedFmts = CalypStream::supportedReadFormats(); + } + else + { + supportedFmts = CalypStream::supportedWriteFormats(); + } + for( unsigned int i = 0; i < supportedFmts.size(); i++ ) + { + std::vector arrayExt = supportedFmts[i].getExts(); + for( std::vector::iterator e = arrayExt.begin(); e != arrayExt.end(); ++e ) + { + if( currExt != "" && currExt == *e ) + { + return supportedFmts[i].formatFct; + } + else if( strFilename.find( *e ) != std::string::npos ) + { + return supportedFmts[i].formatFct; + } + } + } + +#ifdef USE_FFMPEG + return &StreamHandlerLibav::Create; +#else + return &StreamHandlerRaw::Create; +#endif +} \ No newline at end of file diff --git a/lib/CalypStream.h b/lib/CalypStream.h index 4718351..e82bec1 100644 --- a/lib/CalypStream.h +++ b/lib/CalypStream.h @@ -34,17 +34,6 @@ class CalypStreamHandlerIf; -typedef CalypStreamHandlerIf* ( *CreateStreamHandlerFn )( void ); - -struct CalypStreamFormat -{ - std::string formatName; - std::string formatExt; - std::vector getExts(); - std::string formatPattern; - CreateStreamHandlerFn formatFct; -}; - struct CalypStandardResolution { std::string shortName; @@ -52,6 +41,26 @@ struct CalypStandardResolution unsigned int uiHeight; }; +/** + * \class CalypStreamFormat + * \ingroup CalypLibGrp CalypStreamGrp + * \brief Stream format handling class + */ + +class CalypStreamFormat +{ +public: + using CreateStreamHandlerFn = auto ( * )( void ) -> std::unique_ptr; + + CalypStreamFormat() = default; + std::vector getExts(); + + std::string formatName; + std::string formatExt; + std::string formatPattern; + CreateStreamHandlerFn formatFct; +}; + /** * \class CalypStream * \ingroup CalypLibGrp CalypStreamGrp @@ -62,9 +71,6 @@ class CalypStream public: static std::vector supportedReadFormats(); static std::vector supportedWriteFormats(); - - static CreateStreamHandlerFn findStreamHandler( std::string strFilename, bool bRead ); - static std::vector stdResolutionSizes(); CalypStream(); @@ -84,6 +90,7 @@ public: bool open( std::string filename, unsigned int width, unsigned int height, int input_format, unsigned int bitsPel, int endianness, bool hasNegative, unsigned int frame_rate, bool bInput ); + bool supportsFormating(); bool reload(); bool isNative() const; diff --git a/lib/CalypStreamHandlerIf.h b/lib/CalypStreamHandlerIf.h index f8467f6..60cb809 100644 --- a/lib/CalypStreamHandlerIf.h +++ b/lib/CalypStreamHandlerIf.h @@ -51,12 +51,11 @@ formatsList.insert( formatsList.end(), new_fmts.begin(), new_fmts.end() ); \ } -#define REGISTER_CALYP_STREAM_HANDLER( X ) \ -public: \ - static CalypStreamHandlerIf* Create() { return new X(); } \ - void Delete() { delete this; } \ - static std::vector supportedReadFormats(); \ - static std::vector supportedWriteFormats(); \ +#define REGISTER_CALYP_STREAM_HANDLER( X ) \ +public: \ + static std::unique_ptr Create() { return std::make_unique(); } \ + static std::vector supportedReadFormats(); \ + static std::vector supportedWriteFormats(); \ // static int checkforSupportedFile( String, bool ); /** @@ -69,23 +68,7 @@ class CalypStreamHandlerIf friend class CalypStream; public: - CalypStreamHandlerIf() - : m_bIsInput( true ) - , m_bNative( true ) - , m_uiWidth( 0 ) - , m_uiHeight( 0 ) - , m_iPixelFormat( -1 ) - , m_uiBitsPerPixel( 8 ) - , m_iEndianness( -1 ) - , m_dFrameRate( 30 ) - , m_uiCurrFrameFileIdx( 0 ) - , m_uiTotalNumberFrames( 0 ) - , m_uiNBytesPerFrame( 0 ) - , m_isEOF( false ) - { - } virtual ~CalypStreamHandlerIf() {} - virtual void Delete() = 0; virtual bool openHandler( std::string strFilename, bool bInput ) = 0; virtual void closeHandler() = 0; @@ -96,28 +79,41 @@ public: virtual void calculateFrameNumber(){}; - std::string getFormatName() { return m_strFormatName; } - std::string getCodecName() { return m_strCodecName; } + std::string getFormatName() + { + return m_strFormatName; + } + std::string getCodecName() + { + return m_strCodecName; + } protected: - const char* m_pchHandlerName; + CalypStreamHandlerIf() = default; + CalypStreamHandlerIf( bool supportsFormats ) + : m_bSupportsFormat{ supportsFormats } + { + } + + const char* m_pchHandlerName{ "" }; + const bool m_bSupportsFormat{ false }; std::string m_strFormatName; std::string m_strCodecName; - bool m_bIsInput; - bool m_bNative; + bool m_bIsInput{ true }; + bool m_bNative{ true }; std::string m_cFilename; - unsigned int m_uiWidth; - unsigned int m_uiHeight; - int m_iPixelFormat; - unsigned int m_uiBitsPerPixel; - int m_iEndianness; - double m_dFrameRate; - std::uint64_t m_uiCurrFrameFileIdx; - std::uint64_t m_uiTotalNumberFrames; + unsigned int m_uiWidth{ 0 }; + unsigned int m_uiHeight{ 0 }; + int m_iPixelFormat{ -1 }; + unsigned int m_uiBitsPerPixel{ 8 }; + int m_iEndianness{ CLP_INVALID_ENDIANESS }; + double m_dFrameRate{ 30 }; + std::uint64_t m_uiCurrFrameFileIdx{ 0 }; + std::uint64_t m_uiTotalNumberFrames{ 0 }; std::vector m_pStreamBuffer; - std::uint64_t m_uiNBytesPerFrame; - bool m_isEOF; + std::uint64_t m_uiNBytesPerFrame{ 0 }; + bool m_isEOF{ false }; }; #endif // __CALYPSTREAMHANDLERIF_H__ diff --git a/lib/StreamHandlerRaw.cpp b/lib/StreamHandlerRaw.cpp index ff468ac..a08ad9a 100644 --- a/lib/StreamHandlerRaw.cpp +++ b/lib/StreamHandlerRaw.cpp @@ -44,6 +44,12 @@ std::vector StreamHandlerRaw::supportedWriteFormats() END_REGIST_CALYP_SUPPORTED_FMT; } +StreamHandlerRaw::StreamHandlerRaw() + : CalypStreamHandlerIf{ true } +{ + m_pchHandlerName = "RawVideo"; +} + bool StreamHandlerRaw::openHandler( std::string strFilename, bool bInput ) { m_bIsInput = bInput; diff --git a/lib/StreamHandlerRaw.h b/lib/StreamHandlerRaw.h index 44c3b6d..10cc8aa 100644 --- a/lib/StreamHandlerRaw.h +++ b/lib/StreamHandlerRaw.h @@ -40,7 +40,7 @@ private: FILE* m_pFile; /**< The input file pointer >*/ public: - StreamHandlerRaw() { m_pchHandlerName = "RawVideo"; } + StreamHandlerRaw(); ~StreamHandlerRaw() {} bool openHandler( std::string strFilename, bool bInput ); void closeHandler();