From c6e5c906e8cf69b17b4ac95208718e30e36dc6c9 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Tue, 28 Jul 2015 23:50:29 +0100 Subject: [PATCH] Change the functions to handle supported formats --- app/ModulesHandle.cpp | 11 +++-- app/PlaYUVerApp.cpp | 22 +++++---- lib/LibAvContextHandle.cpp | 58 +++++++----------------- lib/LibAvContextHandle.h | 10 ++--- lib/LibOpenCVHandler.cpp | 47 ++++++------------- lib/LibOpenCVHandler.h | 10 ++--- lib/PlaYUVerStream.cpp | 92 ++++++++++++++------------------------ lib/PlaYUVerStream.h | 33 +++++++++++--- 8 files changed, 115 insertions(+), 168 deletions(-) diff --git a/app/ModulesHandle.cpp b/app/ModulesHandle.cpp index 69d7fef..7527f3b 100644 --- a/app/ModulesHandle.cpp +++ b/app/ModulesHandle.cpp @@ -470,17 +470,16 @@ Void ModulesHandle::applyAllModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) QString supported = tr( "Supported Files (" ); QStringList formatsList; - std::vector formatsExt = PlaYUVerStream::supportedWriteFormatsExt(); - std::vector formatsName = PlaYUVerStream::supportedWriteFormatsName(); - for( UInt i = 0; i < formatsName.size(); i++ ) + std::vector supportedFmts = PlaYUVerStream::supportedWriteFormats(); + for( UInt i = 0; i < supportedFmts.size(); i++ ) { QString currFmt; supported.append( " *." ); - supported.append( QString::fromStdString( formatsExt[i] ) ); - currFmt.append( QString::fromStdString( formatsName[i] ) ); + supported.append( QString::fromStdString( supportedFmts[i].formatExt ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatName ) ); currFmt.append( " (*." ); - currFmt.append( QString::fromStdString( formatsExt[i] ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatExt ) ); currFmt.append( ")" ); formatsList << currFmt; } diff --git a/app/PlaYUVerApp.cpp b/app/PlaYUVerApp.cpp index b186726..7ca1b0d 100644 --- a/app/PlaYUVerApp.cpp +++ b/app/PlaYUVerApp.cpp @@ -259,17 +259,16 @@ Void PlaYUVerApp::open() { QString supported = tr( "Supported Files (" ); QStringList formatsList; - std::vector formatsExt = PlaYUVerStream::supportedReadFormatsExt(); - std::vector formatsName = PlaYUVerStream::supportedReadFormatsName(); + std::vector supportedFmts = PlaYUVerStream::supportedReadFormats(); - for( UInt i = 0; i < formatsName.size(); i++ ) + for( UInt i = 0; i < supportedFmts.size(); i++ ) { QString currFmt; supported.append( " *." ); - supported.append( QString::fromStdString( formatsExt[i] ) ); - currFmt.append( QString::fromStdString( formatsName[i] ) ); + supported.append( QString::fromStdString( supportedFmts[i].formatExt ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatName ) ); currFmt.append( " (*." ); - currFmt.append( QString::fromStdString( formatsExt[i] ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatExt ) ); currFmt.append( ")" ); formatsList << currFmt; } @@ -306,17 +305,16 @@ Void PlaYUVerApp::save() VideoSubWindow *saveWindow = m_pcCurrentVideoSubWindow; QString supported = tr( "Supported Files (" ); QStringList formatsList; - std::vector formatsExt = PlaYUVerStream::supportedSaveFormatsExt(); - std::vector formatsName = PlaYUVerStream::supportedSaveFormatsName(); - for( UInt i = 0; i < formatsName.size(); i++ ) + std::vector supportedFmts = PlaYUVerStream::supportedSaveFormats(); + for( UInt i = 0; i < supportedFmts.size(); i++ ) { QString currFmt; supported.append( " *." ); - supported.append( QString::fromStdString( formatsExt[i] ) ); - currFmt.append( QString::fromStdString( formatsName[i] ) ); + supported.append( QString::fromStdString( supportedFmts[i].formatExt ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatName ) ); currFmt.append( " (*." ); - currFmt.append( QString::fromStdString( formatsExt[i] ) ); + currFmt.append( QString::fromStdString( supportedFmts[i].formatExt ) ); currFmt.append( ")" ); formatsList << currFmt; } diff --git a/lib/LibAvContextHandle.cpp b/lib/LibAvContextHandle.cpp index 4c0c3aa..9d44b2b 100644 --- a/lib/LibAvContextHandle.cpp +++ b/lib/LibAvContextHandle.cpp @@ -32,54 +32,30 @@ namespace plaYUVer { -std::vector LibAvContextHandle::supportedReadFormatsExt() +std::vector LibAvContextHandle::supportedReadFormats() { - std::vector formatsExt; - formatsExt.push_back( "avi" ); - formatsExt.push_back( "mp4" ); - formatsExt.push_back( "wmv" ); - formatsExt.push_back( "pgm" ); - formatsExt.push_back( "png" ); - formatsExt.push_back( "bmp" ); - formatsExt.push_back( "jpg" ); - return formatsExt; + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + REGIST_PLAYUVER_SUPPORTED_FMT( "Portable Grayscale Map ", "pgm" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Portable Network Graphics", "png" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Joint Photographic Experts Group", "jpg" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Windows Bitmap", "bmp" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Audio video interleaved", "avi" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Windows media video", "wmv" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "MPEG4", "mp4" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Matroska Multimedia Container", "mkv" ); + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector LibAvContextHandle::supportedReadFormatsName() +std::vector LibAvContextHandle::supportedWriteFormats() { - std::vector formatsName; - formatsName.push_back( "Audio video interleaved" ); - formatsName.push_back( "MPEG4" ); - formatsName.push_back( "Windows media video" ); - formatsName.push_back( "Portable Grayscale Map" ); - formatsName.push_back( "Portable Network Graphics" ); - formatsName.push_back( "Windows Bitmap" ); - formatsName.push_back( "Joint Photographic Experts Group" ); - return formatsName; + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector LibAvContextHandle::supportedWriteFormatsExt() +std::vector LibAvContextHandle::supportedSaveFormats() { - std::vector formatsExt; - return formatsExt; -} - -std::vector LibAvContextHandle::supportedWriteFormatsName() -{ - std::vector formatsName; - return formatsName; -} - -std::vector LibAvContextHandle::supportedSaveFormatsExt() -{ - std::vector formatsExt; - return formatsExt; -} - -std::vector LibAvContextHandle::supportedSaveFormatsName() -{ - std::vector formatsName; - return formatsName; + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } static int open_codec_context( int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type ) diff --git a/lib/LibAvContextHandle.h b/lib/LibAvContextHandle.h index 2f01b00..5a5506e 100644 --- a/lib/LibAvContextHandle.h +++ b/lib/LibAvContextHandle.h @@ -48,6 +48,7 @@ extern "C" } #include "PlaYUVerDefs.h" +#include "PlaYUVerStream.h" namespace plaYUVer { @@ -56,12 +57,9 @@ class LibAvContextHandle { public: - static std::vector supportedReadFormatsExt(); - static std::vector supportedReadFormatsName(); - static std::vector supportedWriteFormatsExt(); - static std::vector supportedWriteFormatsName(); - static std::vector supportedSaveFormatsExt(); - static std::vector supportedSaveFormatsName(); + static std::vector supportedReadFormats(); + static std::vector supportedWriteFormats(); + static std::vector supportedSaveFormats(); LibAvContextHandle() : m_bHasStream( false ) diff --git a/lib/LibOpenCVHandler.cpp b/lib/LibOpenCVHandler.cpp index a28516b..136cf64 100644 --- a/lib/LibOpenCVHandler.cpp +++ b/lib/LibOpenCVHandler.cpp @@ -32,46 +32,27 @@ namespace plaYUVer { -std::vector LibOpenCVHandler::supportedReadFormatsExt() +std::vector LibOpenCVHandler::supportedReadFormats() { - std::vector formatsExt; - formatsExt.push_back( "pgm" ); - formatsExt.push_back( "png" ); - formatsExt.push_back( "bmp" ); - formatsExt.push_back( "jpg" ); - return formatsExt; + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + REGIST_PLAYUVER_SUPPORTED_FMT( "Portable Grayscale Map ", "pgm" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Portable Network Graphics", "png" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Joint Photographic Experts Group", "jpg" ); + REGIST_PLAYUVER_SUPPORTED_FMT( "Windows Bitmap", "bmp" ); + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector LibOpenCVHandler::supportedReadFormatsName() +std::vector LibOpenCVHandler::supportedWriteFormats() { - std::vector formatsName; - formatsName.push_back( "Portable Grayscale Map" ); - formatsName.push_back( "Portable Network Graphics" ); - formatsName.push_back( "Windows Bitmap" ); - formatsName.push_back( "Joint Photographic Experts Group" ); - return formatsName; + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector LibOpenCVHandler::supportedWriteFormatsExt() +std::vector LibOpenCVHandler::supportedSaveFormats() { - std::vector formatsExt; - return formatsExt; -} - -std::vector LibOpenCVHandler::supportedWriteFormatsName() -{ - std::vector formatsName; - return formatsName; -} - -std::vector LibOpenCVHandler::supportedSaveFormatsExt() -{ - return supportedReadFormatsExt(); -} - -std::vector LibOpenCVHandler::supportedSaveFormatsName() -{ - return supportedReadFormatsName(); + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + APPEND_PLAYUVER_SUPPORTED_FMT( LibOpenCVHandler, Read ); + END_REGIST_PLAYUVER_SUPPORTED_FMT; } PlaYUVerFrame* LibOpenCVHandler::loadFrame( std::string filename ) diff --git a/lib/LibOpenCVHandler.h b/lib/LibOpenCVHandler.h index 257ba5e..7a71eb9 100644 --- a/lib/LibOpenCVHandler.h +++ b/lib/LibOpenCVHandler.h @@ -31,6 +31,7 @@ #include #include #include "PlaYUVerDefs.h" +#include "PlaYUVerStream.h" namespace plaYUVer { @@ -41,12 +42,9 @@ class LibOpenCVHandler { public: - static std::vector supportedReadFormatsExt(); - static std::vector supportedReadFormatsName(); - static std::vector supportedWriteFormatsExt(); - static std::vector supportedWriteFormatsName(); - static std::vector supportedSaveFormatsExt(); - static std::vector supportedSaveFormatsName(); + static std::vector supportedReadFormats(); + static std::vector supportedWriteFormats(); + static std::vector supportedSaveFormats(); LibOpenCVHandler() { diff --git a/lib/PlaYUVerStream.cpp b/lib/PlaYUVerStream.cpp index f123d72..297567c 100644 --- a/lib/PlaYUVerStream.cpp +++ b/lib/PlaYUVerStream.cpp @@ -38,71 +38,44 @@ namespace plaYUVer { -std::vector PlaYUVerStream::supportedReadFormatsExt() +std::vector PlaYUVerStream::supportedReadFormats() { - std::vector formatsExt; - formatsExt.push_back( "yuv" ); + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + REGIST_PLAYUVER_SUPPORTED_FMT( "Raw Video", "yuv" ); #ifdef USE_FFMPEG - std::vector libAvFmt = LibAvContextHandle::supportedReadFormatsExt(); - formatsExt.insert( formatsExt.end(), libAvFmt.begin(), libAvFmt.end() ); + APPEND_PLAYUVER_SUPPORTED_FMT( LibAvContextHandle, Read ); #endif //#ifdef USE_OPENCV -// std::vector opencvFmt = LibOpenCVHandler::supportedReadFormatsExt(); -// formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() ); +// APPEND_PLAYUVER_SUPPORTED_FMT( LibOpenCVHandler ); //#endif - return formatsExt; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector PlaYUVerStream::supportedReadFormatsName() +std::vector PlaYUVerStream::supportedWriteFormats() { - std::vector formatsName; - formatsName.push_back( "Raw video" ); + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + REGIST_PLAYUVER_SUPPORTED_FMT( "Raw Video", "yuv" ); #ifdef USE_FFMPEG - std::vector libAvFmt = LibAvContextHandle::supportedReadFormatsName(); - formatsName.insert( formatsName.end(), libAvFmt.begin(), libAvFmt.end() ); + APPEND_PLAYUVER_SUPPORTED_FMT( LibAvContextHandle, Write ); #endif -//#ifdef USE_OPENCV -// std::vector opencvFmt = LibOpenCVHandler::supportedReadFormatsName(); -// formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() ); -//#endif - return formatsName; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector PlaYUVerStream::supportedWriteFormatsExt() +std::vector PlaYUVerStream::supportedSaveFormats() { - std::vector formatsExt; - formatsExt.push_back( "yuv" ); - return formatsExt; -} - -std::vector PlaYUVerStream::supportedWriteFormatsName() -{ - std::vector formatsName; - formatsName.push_back( "Raw video" ); - return formatsName; -} - -std::vector PlaYUVerStream::supportedSaveFormatsExt() -{ - std::vector formatsExt = supportedWriteFormatsExt(); + INI_REGIST_PLAYUVER_SUPPORTED_FMT; + APPEND_PLAYUVER_SUPPORTED_FMT( PlaYUVerStream, Write ); +#ifdef USE_FFMPEG + APPEND_PLAYUVER_SUPPORTED_FMT( LibAvContextHandle, Save ); +#endif #ifdef USE_OPENCV - std::vector opencvFmt = LibOpenCVHandler::supportedSaveFormatsExt(); - formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() ); + APPEND_PLAYUVER_SUPPORTED_FMT( LibOpenCVHandler, Save ); #endif - formatsExt.erase( unique( formatsExt.begin(), formatsExt.end() ), formatsExt.end() ); - return formatsExt; + END_REGIST_PLAYUVER_SUPPORTED_FMT; } -std::vector PlaYUVerStream::supportedSaveFormatsName() +std::vector PlaYUVerStream::stdResolutionSizes() { - std::vector formatsName = supportedWriteFormatsName(); -#ifdef USE_OPENCV - std::vector opencvFmt = LibOpenCVHandler::supportedSaveFormatsName(); - formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() ); -#endif - formatsName.erase( unique( formatsName.begin(), formatsName.end() ), formatsName.end() ); - return formatsName; -} #define REGIST_PLAYUVER_STANDARD_RESOLUTION( name, width, height) \ stdResElement.shortName = name; \ @@ -110,8 +83,6 @@ std::vector PlaYUVerStream::supportedSaveFormatsName() stdResElement.uiHeight = height; \ stdResList.push_back( stdResElement ); -std::vector PlaYUVerStream::stdResolutionSizes() -{ std::vector stdResList; PlaYUVerStdResolution stdResElement; REGIST_PLAYUVER_STANDARD_RESOLUTION( "CIF", 352, 288 ); @@ -174,14 +145,17 @@ Void PlaYUVerStream::findHandler() } std::string currExt = m_cFilename.substr( m_cFilename.find_last_of( "." ) + 1 ); + currExt = lowercase( currExt ); + if( m_bIsInput ) { #ifdef USE_FFMPEG - for( UInt i = 0; i < LibAvContextHandle::supportedReadFormatsExt().size(); i++ ) + std::vector supportedFmts = LibAvContextHandle::supportedReadFormats(); + for( UInt i = 0; i < supportedFmts.size(); i++ ) { - if( LibAvContextHandle::supportedReadFormatsExt()[i] == lowercase( currExt ) ) + if( supportedFmts[i].formatExt == currExt ) { - m_cFormatName = LibAvContextHandle::supportedReadFormatsExt()[i]; + m_cFormatName = supportedFmts[i].formatExt[i]; m_iStreamHandler = FFMPEG; return; } @@ -193,11 +167,12 @@ Void PlaYUVerStream::findHandler() else { #ifdef USE_FFMPEG - for( UInt i = 0; i < LibAvContextHandle::supportedWriteFormatsExt().size(); i++ ) + std::vector supportedFmts = LibAvContextHandle::supportedWriteFormats(); + for( UInt i = 0; i < supportedFmts.size(); i++ ) { - if( LibAvContextHandle::supportedWriteFormatsExt()[i] == lowercase( currExt ) ) + if( supportedFmts[i].formatExt == currExt ) { - m_cFormatName = LibAvContextHandle::supportedWriteFormatsExt()[i]; + m_cFormatName = supportedFmts[i].formatExt; m_iStreamHandler = FFMPEG; return; } @@ -651,12 +626,13 @@ Bool PlaYUVerStream::saveFrame( const std::string& filename, PlaYUVerFrame *save //std::string currExt = QFileInfo( qtString ).suffix().toStdString(); std::string currExt = filename.substr( filename.find_last_of( "." ) + 1 ); - for( UInt i = 0; i < PlaYUVerStream::supportedSaveFormatsExt().size(); i++ ) + std::vector supportedFmts = PlaYUVerStream::supportedSaveFormats(); + for( UInt i = 0; i < supportedFmts.size(); i++ ) { - if( PlaYUVerStream::supportedSaveFormatsExt()[i] == currExt ) + if( supportedFmts[i].formatExt == currExt ) { iFileFormat = i; - fmtExt = PlaYUVerStream::supportedSaveFormatsExt()[i]; + fmtExt = supportedFmts[i].formatExt[i]; break; } } diff --git a/lib/PlaYUVerStream.h b/lib/PlaYUVerStream.h index 29be023..cdc26e8 100644 --- a/lib/PlaYUVerStream.h +++ b/lib/PlaYUVerStream.h @@ -45,6 +45,30 @@ typedef struct UInt uiHeight; } PlaYUVerStdResolution; +typedef struct +{ + std::string formatName; + std::string formatExt; +} PlaYUVerSupportedFormat; + +#define INI_REGIST_PLAYUVER_SUPPORTED_FMT \ + std::vector formatsList; \ + PlaYUVerSupportedFormat formatElem; + +#define END_REGIST_PLAYUVER_SUPPORTED_FMT \ + return formatsList; + +#define REGIST_PLAYUVER_SUPPORTED_FMT( name, ext ) \ + formatElem.formatName = name; \ + formatElem.formatExt = lowercase( ext ); \ + formatsList.push_back( formatElem ); + +#define APPEND_PLAYUVER_SUPPORTED_FMT( class_name, fct ) \ + { \ + std::vector new_fmts = class_name::supported##fct##Formats(); \ + formatsList.insert( formatsList.end(), new_fmts.begin(), new_fmts.end() ); \ + } + /** * \class PlaYUVerStream * \ingroup PlaYUVerLib PlaYUVerLib_Stream @@ -99,12 +123,9 @@ private: public: - static std::vector supportedReadFormatsExt(); - static std::vector supportedReadFormatsName(); - static std::vector supportedWriteFormatsExt(); - static std::vector supportedWriteFormatsName(); - static std::vector supportedSaveFormatsExt(); - static std::vector supportedSaveFormatsName(); + static std::vector supportedReadFormats(); + static std::vector supportedWriteFormats(); + static std::vector supportedSaveFormats(); static std::vector stdResolutionSizes();