From 816dcc345f81346432825aaa22ca733fc5b212a5 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sun, 1 May 2016 20:02:29 +0100 Subject: [PATCH 1/2] start port the opt parser --- app/PlaYUVerApp.cpp | 2 +- lib/CMakeLists.txt | 2 +- lib/PlaYUVerCmdParser.cpp | 140 ---------- lib/PlaYUVerCmdParser.h | 56 ---- lib/PlaYUVerModuleIf.h | 2 +- lib/PlaYUVerOptions.cpp | 460 ++++++++++++++++++++++++++++--- lib/PlaYUVerOptions.h | 353 +++++------------------- tools/PlaYUVerToolsCmdParser.cpp | 80 +----- tools/PlaYUVerToolsCmdParser.h | 11 +- 9 files changed, 510 insertions(+), 596 deletions(-) delete mode 100644 lib/PlaYUVerCmdParser.cpp delete mode 100644 lib/PlaYUVerCmdParser.h diff --git a/app/PlaYUVerApp.cpp b/app/PlaYUVerApp.cpp index e980754..11ee89a 100644 --- a/app/PlaYUVerApp.cpp +++ b/app/PlaYUVerApp.cpp @@ -34,7 +34,7 @@ #ifdef USE_FERVOR #include "fvupdater.h" #endif -#include "lib/PlaYUVerCmdParser.h" +#include "lib/PlaYUVerOptions.h" #include "PlaYUVerSubWindowHandle.h" #include "AboutDialog.h" #include "SubWindowAbstract.h" diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 18a34e4..32da862 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -15,7 +15,7 @@ set(PlaYUVer_Lib_SRCS StreamHandlerPortableMap.cpp # Command Parser PlaYUVerOptions.cpp - PlaYUVerCmdParser.cpp +# PlaYUVerCmdParser.cpp ) IF( USE_FFMPEG ) diff --git a/lib/PlaYUVerCmdParser.cpp b/lib/PlaYUVerCmdParser.cpp deleted file mode 100644 index 07e5172..0000000 --- a/lib/PlaYUVerCmdParser.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* This file is a part of plaYUVer project - * Copyright (C) 2014-2016 by Luis Lucas (luisfrlucas@gmail.com) - * Joao Carreira (jfmcarreira@gmail.com) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** - * \file PlaYUVerCmdParser.cpp - * \brief Command parser - */ - -#include "config.h" -#include -#include -#include -#include -#include "PlaYUVerCmdParser.h" -#include "PlaYUVerFrame.h" - - -PlaYUVerCmdParser::PlaYUVerCmdParser() -{ - Opts().addOptions()/**/ - ( "help", "produce help message" )/**/ - ( "version", "show version and exit" )/**/ - ( "pel_fmts", "list pixel formats" ) /**/ - ( "quality_metrics", "list supported quality metrics" )/**/ - ( "module_list", "list supported modules" ) /**/ - ( "module_list_full", "detailed list supported modules" ); -} - -PlaYUVerCmdParser::~PlaYUVerCmdParser() -{ - -} - -Options& PlaYUVerCmdParser::Opts() -{ - return m_cParserOptions; -} - -std::list& PlaYUVerCmdParser::getNoArgs() -{ - return m_aUnhandledArgs; -} - -Bool PlaYUVerCmdParser::parse() -{ - try - { - // m_cParserOptions.setDefaults(); - m_aUnhandledArgs = m_cParserOptions.scanArgv( m_iArgc, ( const Char** )m_ppArgv ); - - if( checkListingOpts() ) - { - return false; - } - } - catch( std::exception& e ) - { - std::cerr << "error: " - << e.what() - << "\n"; - return false; - } - catch( ... ) - { - std::cerr << "Exception of unknown type!\n"; - } - return true; -} - -Bool PlaYUVerCmdParser::parse( Options& opts, Int argc, Char *argv[] ) -{ - try - { - // m_cParserOptions.setDefaults(); - m_aUnhandledArgs = opts.scanArgv( argc, ( const Char** )argv ); - - } - catch( std::exception& e ) - { - std::cerr << "error: " - << e.what() - << "\n"; - return false; - } - catch( ... ) - { - std::cerr << "Exception of unknown type!\n"; - } - return true; -} - -Bool PlaYUVerCmdParser::checkListingOpts() -{ - Bool bRet = false; - - if( m_cParserOptions["version"]->count() ) - { - std::cout << "PlaYUVer version " - << PLAYUVER_VERSION_STRING - << "\n"; - bRet |= true; - } - if( m_cParserOptions["pel_fmts"]->count() ) - { - printf( "PlaYUVer supported pixel formats: \n" ); - for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) - { - printf( " %s\n", PlaYUVerFrame::supportedPixelFormatListNames()[i].c_str() ); - } - bRet |= true; - } - if( Opts()["quality_metrics"]->count() ) - { - printf( "PlaYUVer supported quality metrics: \n" ); - for( UInt i = 0; i < PlaYUVerFrame::supportedQualityMetricsList().size(); i++ ) - { - printf( " %s\n", PlaYUVerFrame::supportedQualityMetricsList()[i].c_str() ); - } - bRet |= true; - } - return bRet; -} - - diff --git a/lib/PlaYUVerCmdParser.h b/lib/PlaYUVerCmdParser.h deleted file mode 100644 index 3b12747..0000000 --- a/lib/PlaYUVerCmdParser.h +++ /dev/null @@ -1,56 +0,0 @@ -/* This file is a part of plaYUVer project - * Copyright (C) 2014-2016 by Luis Lucas (luisfrlucas@gmail.com) - * Joao Carreira (jfmcarreira@gmail.com) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** - * \file PlaYUVerCmdParser.h - * \brief Command parser - */ - -#include "PlaYUVerDefs.h" -#include "PlaYUVerOptions.h" - -#ifndef __PLAYUVERCMDPARSER_H__ -#define __PLAYUVERCMDPARSER_H__ - -class PlaYUVerCmdParser -{ -public: - PlaYUVerCmdParser(); - ~PlaYUVerCmdParser(); - - Void config( Int argc, Char *argv[] ) - { - m_iArgc = argc; - m_ppArgv = argv; - } - Bool parse(); - Bool parse( Options& opts, Int argc, Char *argv[] ); - std::list& getNoArgs(); - Options& Opts(); -private: - Int m_iArgc; - Char** m_ppArgv; - Options m_cParserOptions; - std::list m_aUnhandledArgs; - - Bool checkListingOpts(); - Void listModules(); -}; - -#endif // __PLAYUVERCMDPARSER_H__ diff --git a/lib/PlaYUVerModuleIf.h b/lib/PlaYUVerModuleIf.h index cad1f7f..84bd99e 100644 --- a/lib/PlaYUVerModuleIf.h +++ b/lib/PlaYUVerModuleIf.h @@ -101,7 +101,7 @@ public: enum Module_NumberOfFrames m_uiNumberOfFrames; UInt m_uiModuleRequirements; - Options m_cModuleOptions; + PlaYUVerOptions m_cModuleOptions; PlaYUVerModuleIf() { diff --git a/lib/PlaYUVerOptions.cpp b/lib/PlaYUVerOptions.cpp index d134be3..58451c8 100644 --- a/lib/PlaYUVerOptions.cpp +++ b/lib/PlaYUVerOptions.cpp @@ -36,46 +36,273 @@ * \brief Input args handler */ +#include "config.h" +#include "PlaYUVerOptions.h" + #include #include #include #include #include +#include +#include #include #include #include -#include "PlaYUVerProgramOptions.h" + +#include "lib/PlaYUVerModuleIf.h" +#include "modules/PlaYUVerModuleFactory.h" + using namespace std; -Options::Options( const String& name ) +struct ParseFailure: public std::exception +{ + String arg; + String val; + ParseFailure( String arg0, String val0 ) throw() : + arg( arg0 ), + val( val0 ) { } + ~ParseFailure() throw() { } + const char* what() const throw() + { + return "Option Parse Failure"; + } +}; + + +/** OptionBase: Virtual base class for storing information relating to a + * specific option This base class describes common elements. Type specific + * information should be stored in a derived class. */ +class OptionBase +{ +public: + OptionBase( const String& name, const String& desc ) : + arg_count( 0 ), + opt_string( name ), + opt_desc( desc ) + { + } + + virtual ~OptionBase() + { + } + + /* parse argument arg, to obtain a value for the option */ + virtual void parse( const String& arg ) = 0; + /* set the argument to the default value */ + //virtual void setDefault() = 0; + int count() + { + return arg_count; + } + Bool isBinary() + { + return is_binary; + } + + int arg_count; + String opt_string; + String opt_desc; + Bool is_binary; +}; + +/** Type specific option storage */ +class BoolOption: public OptionBase +{ +public: + BoolOption( const String& name, Bool default_val, const String& desc ) : + OptionBase( name, desc ), + opt_storage( false ), + opt_default_val( default_val ) + { + is_binary = true; + } + + void parse( const String& arg ) + { + opt_storage = true; + arg_count++; + } + + void setDefault() + { + opt_storage = opt_default_val; + } + Bool opt_storage; + Bool opt_default_val; +}; + +/** Type specific option storage */ +template +class StandardOption: public OptionBase +{ +public: + StandardOption( const String& name, T& storage, const String& desc ) : + OptionBase( name, desc ), + opt_storage( storage ) + { + is_binary = false; + } + + void parse( const String& arg ); + T& opt_storage; +}; + +/* Generic parsing */ +template +inline void StandardOption::parse( const String& arg ) +{ + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> opt_storage; + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +template<> +inline void StandardOption< std::vector >::parse( const String& arg ) +{ + UInt aux_opt_storage; + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> aux_opt_storage; + opt_storage.push_back( aux_opt_storage ); + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +template<> +inline void StandardOption< std::vector >::parse( const String& arg ) +{ + Int aux_opt_storage; + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> aux_opt_storage; + opt_storage.push_back( aux_opt_storage ); + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +/* string parsing is specialized */ +template<> +inline void StandardOption::parse( const String& arg ) +{ + opt_storage = arg; + arg_count++; +} + +/* string vector parsing is specialized */ +template<> +inline void StandardOption >::parse( const String& arg ) +{ + opt_storage.push_back( arg ); + arg_count++; +} + +/** Option class for argument handling using a user provided function */ +struct FunctionOption: public OptionBase +{ +public: + typedef Void( Func )( PlaYUVerOptions&, const String& ); + + FunctionOption( const String& name, PlaYUVerOptions& parent_, Func *func_, const String& desc ) : + OptionBase( name, desc ), + parent( parent_ ), + func( func_ ) + { + is_binary = false; + } + + void parse( const String& arg ) + { + func( parent, arg ); + arg_count++; + } + + void setDefault() + { + return; + } + +private: + PlaYUVerOptions& parent; + void ( *func )( PlaYUVerOptions&, const String& ); +}; + +struct PlaYUVerOptions::Option +{ + Option() : + opt( 0 ) + { + } + ~Option() + { + if( opt ) + delete opt; + } + std::list opt_long; + std::list opt_short; + OptionBase* opt; +}; + +PlaYUVerOptions::PlaYUVerOptions( const String& name ) { m_cOptionGroupName = name; m_bAllowUnkonw = true; + + /** + * Add default options + */ + addOptions()/**/ + ( "help", "produce help message" )/**/ + ( "version", "show version and exit" )/**/ + ( "pel_fmts", "list pixel formats" ) /**/ + ( "quality_metrics", "list supported quality metrics" )/**/ + ( "module_list", "list supported modules" ) /**/ + ( "module_list_full", "detailed list supported modules" ); } -Options::~Options() +PlaYUVerOptions::~PlaYUVerOptions() { - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { delete *it; } } -OptionBase* Options::operator[]( const String& optName ) +OptionBase* PlaYUVerOptions::operator[]( const String& optName ) { return getOption( optName ); } -OptionBase* Options::getOption( const String& optName ) +OptionBase* PlaYUVerOptions::getOption( const String& optName ) { - Options::OptionMap::iterator opt_it; + PlaYUVerOptions::OptionMap::iterator opt_it; opt_it = opt_short_map.find( optName ); if( opt_it != opt_short_map.end() ) { OptionsList opt_list = ( *opt_it ).second; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { return ( *it )->opt; } @@ -84,7 +311,7 @@ OptionBase* Options::getOption( const String& optName ) if( opt_it != opt_long_map.end() ) { OptionsList opt_list = ( *opt_it ).second; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { return ( *it )->opt; } @@ -92,12 +319,17 @@ OptionBase* Options::getOption( const String& optName ) return NULL; } -Bool Options::hasOpt( const String& optName ) +Bool PlaYUVerOptions::hasOpt( const String& optName ) { return getOption( optName )->count() > 0 ? true : false; } -void Options::addOption( OptionBase *opt ) +OptionSpecific PlaYUVerOptions::addOptions() +{ + return OptionSpecific( *this ); +} + +void PlaYUVerOptions::addOption( OptionBase *opt ) { Option* names = new Option(); names->opt = opt; @@ -130,35 +362,81 @@ void Options::addOption( OptionBase *opt ) } /* Helper method to initiate adding options to Options */ -OptionSpecific Options::addOptions() + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +template +OptionSpecific& OptionSpecific::operator()( const String& name, T& storage, const String& desc ) { - return OptionSpecific( *this ); + parent.addOption( new StandardOption( name, storage, desc ) ); + return *this; } +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +OptionSpecific& OptionSpecific::operator()( const String& name, const String& desc ) +{ + parent.addOption( new BoolOption( name, false, desc ) ); + return *this; +} + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +OptionSpecific& OptionSpecific::operator()( const String& name, Bool default_val, const String& desc ) +{ + parent.addOption( new BoolOption( name, default_val, desc ) ); + return *this; +} + +/** + * Add option described by name to the parent Options list, + * with desc as an optional help description + * instead of storing the value somewhere, a function of type + * FunctionOption::Func is called. It is upto this function to correctly + * handle evaluating the option's value. + */ +// OptionSpecific& OptionSpecific::operator()( const String& name, FunctionOption::Func *func, const String& desc ) +// { +// parent.addOption( new FunctionOption( name, parent, func, desc ) ); +// return *this; +// } + /* for all options in opts, set their storage to their specified * default value */ -Void Options::setDefaults() +Void PlaYUVerOptions::setDefaults() { -// for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) +// for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) // { // ( *it )->opt->setDefault(); // } } -static void setOptions( Options::OptionsList& opt_list, const string& value ) +static void setOptions( PlaYUVerOptions::OptionsList& opt_list, const string& value ) { /* multiple options may be registered for the same name: * allow each to parse value */ - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { ( *it )->opt->parse( value ); } } -Bool Options::storePair( Bool allow_long, Bool allow_short, const string& name, const string& value ) +Bool PlaYUVerOptions::storePair( Bool allow_long, Bool allow_short, const string& name, const string& value ) { bool found = false; - Options::OptionMap::iterator opt_it; + PlaYUVerOptions::OptionMap::iterator opt_it; if( allow_long ) { opt_it = opt_long_map.find( name ); @@ -197,7 +475,7 @@ Bool Options::storePair( Bool allow_long, Bool allow_short, const string& name, return true; } -Bool Options::storePair( const string& name, const string& value ) +Bool PlaYUVerOptions::storePair( const string& name, const string& value ) { return storePair( true, true, name, value ); } @@ -205,7 +483,7 @@ Bool Options::storePair( const string& name, const string& value ) /** * returns number of extra arguments consumed */ -UInt Options::parseLONG( unsigned argc, const char* argv[] ) +UInt PlaYUVerOptions::parseLONG( unsigned argc, const char* argv[] ) { /* gnu style long options can take the forms: * --option=arg @@ -225,8 +503,8 @@ UInt Options::parseLONG( unsigned argc, const char* argv[] ) /* commented out, to return to true GNU style processing * where longopts have to include an =, otherwise they are * booleans */ - if (argc == 1) - return 0; /* run out of argv for argument */ + if( argc == 1 ) + return 0; /* run out of argv for argument */ extra_argc_consumed = 1; #endif if( !storePair( true, false, option, "1" ) ) @@ -246,7 +524,7 @@ UInt Options::parseLONG( unsigned argc, const char* argv[] ) /** * returns number of extra arguments consumed */ -UInt Options::parseLONG( string arg ) +UInt PlaYUVerOptions::parseLONG( string arg ) { /* gnu style long options can take the forms: * --option=arg @@ -275,7 +553,7 @@ UInt Options::parseLONG( string arg ) return extra_argc_consumed; } -unsigned Options::parseSHORT( unsigned argc, const char* argv[] ) +unsigned PlaYUVerOptions::parseSHORT( unsigned argc, const char* argv[] ) { /* short options can take the forms: * --option arg @@ -301,7 +579,33 @@ unsigned Options::parseSHORT( unsigned argc, const char* argv[] ) return 1; } -list Options::scanArgv( unsigned argc, const char* argv[] ) +Bool PlaYUVerOptions::parse( Int argc, Char *argv[] ) +{ + try + { + m_aUnhandledArgs = scanArgv( argc, ( const Char** )argv ); + + if( checkListingOpts() ) + { + return false; + } + } + catch( std::exception& e ) + { + std::cerr << "error: " + << e.what() + << "\n"; + return false; + } + catch( ... ) + { + std::cerr << "Exception of unknown type!\n"; + } + return true; +} + + +list PlaYUVerOptions::scanArgv( unsigned argc, const char* argv[] ) { /* a list for anything that didn't get handled as an option */ list non_option_arguments; @@ -325,7 +629,7 @@ list Options::scanArgv( unsigned argc, const char* argv[] ) { /* handle short (single dash) options */ #if 0 - i += parsePOSIX(opts, argc - i, &argv[i]); + i += parsePOSIX( opts, argc - i, &argv[i] ); #else i += parseSHORT( argc - i, &argv[i] ); #endif @@ -347,7 +651,7 @@ list Options::scanArgv( unsigned argc, const char* argv[] ) return non_option_arguments; } -Void Options::scanArgs( std::vector args_array) +Void PlaYUVerOptions::scanArgs( std::vector args_array ) { for( unsigned i = 0; i < args_array.size(); i++ ) { @@ -362,7 +666,7 @@ static const char spaces[41] = " "; * using the formatting: "-x, --long", * if a short/long option isn't specified, it is not printed */ -static void doHelpOpt( ostream& out, const Options::Option& entry, unsigned pad_short = 0 ) +static void doHelpOpt( ostream& out, const PlaYUVerOptions::Option& entry, unsigned pad_short = 0 ) { pad_short = min( pad_short, 8u ); @@ -391,12 +695,12 @@ static void doHelpOpt( ostream& out, const Options::Option& entry, unsigned pad_ } /* format the help text */ -Void Options::doHelp( ostream& out, unsigned columns ) +Void PlaYUVerOptions::doHelp( ostream& out, unsigned columns ) { const unsigned pad_short = 3; /* first pass: work out the longest option name */ unsigned max_width = 0; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { ostringstream line( ios_base::out ); doHelpOpt( line, **it, pad_short ); @@ -411,7 +715,7 @@ Void Options::doHelp( ostream& out, unsigned columns ) * - if the option text is longer than opt_width, place the help * text at opt_width on the next line. */ - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { ostringstream line( ios_base::out ); line << " "; @@ -489,5 +793,97 @@ Void Options::doHelp( ostream& out, unsigned columns ) } } +Bool PlaYUVerOptions::checkListingOpts() +{ + Bool bRet = false; + + if( hasOpt( "version" ) ) + { + std::cout << "PlaYUVer version " + << PLAYUVER_VERSION_STRING + << "\n"; + bRet |= true; + } + if( hasOpt( "pel_fmts" ) ) + { + printf( "PlaYUVer supported pixel formats: \n" ); + for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) + { + printf( " %s\n", PlaYUVerFrame::supportedPixelFormatListNames()[i].c_str() ); + } + bRet |= true; + } + if( hasOpt( "quality_metrics" ) ) + { + printf( "PlaYUVer supported quality metrics: \n" ); + for( UInt i = 0; i < PlaYUVerFrame::supportedQualityMetricsList().size(); i++ ) + { + printf( " %s\n", PlaYUVerFrame::supportedQualityMetricsList()[i].c_str() ); + } + bRet |= true; + } + if( hasOpt( "module_list" ) || hasOpt( "module_list_full" ) ) + { + listModules(); + bRet |= true; + } + return bRet; +} + +Void PlaYUVerOptions::listModules() +{ + Bool bDetailed = false; + + if( hasOpt( "module_list_full" ) ) + bDetailed = true; + + PlaYUVerModuleIf* pcCurrModuleIf; + PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); + PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); + + printf( "PlaYUVer available modules: \n" ); + printf( " [Internal Name] " ); + if( bDetailed ) + { + printf( " [Full Name] " ); + printf( " [Type] " ); + printf( " [Description]" ); + } + printf( " \n" ); + + Char ModuleNameString[40]; + + for( UInt i = 0; it != PlaYUVerModuleFactoryMap.end(); ++it, i++ ) + { + printf( " " ); + printf( "%-30s", it->first ); + if( bDetailed ) + { + ModuleNameString[0] = '\0'; + pcCurrModuleIf = it->second(); + + if( pcCurrModuleIf->m_pchModuleCategory ) + { + strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleCategory ); + strcat( ModuleNameString, "/" ); + } + strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleName ); + printf( " %-40s", ModuleNameString ); + switch( pcCurrModuleIf->m_iModuleType ) + { + case FRAME_PROCESSING_MODULE: + printf( " Processing " ); + break; + case FRAME_MEASUREMENT_MODULE: + printf( " Measurement " ); + break; + } + printf( " %s", pcCurrModuleIf->m_pchModuleTooltip ); + pcCurrModuleIf->Delete(); + } + printf( "\n" ); + } +} + diff --git a/lib/PlaYUVerOptions.h b/lib/PlaYUVerOptions.h index b686a96..75f19e7 100644 --- a/lib/PlaYUVerOptions.h +++ b/lib/PlaYUVerOptions.h @@ -45,282 +45,19 @@ #include #include #include + #include "PlaYUVerDefs.h" - -struct Options; - -struct ParseFailure: public std::exception -{ - ParseFailure( String arg0, String val0 ) throw() : - arg( arg0 ), - val( val0 ) - { - } - - ~ParseFailure() throw() - { - } - - String arg; - String val; - - const char* what() const throw() - { - return "Option Parse Failure"; - } -}; - -/** OptionBase: Virtual base class for storing information relating to a - * specific option This base class describes common elements. Type specific - * information should be stored in a derived class. */ -struct OptionBase -{ - OptionBase( const String& name, const String& desc ) : - arg_count( 0 ), - opt_string( name ), - opt_desc( desc ) - { - } - - virtual ~OptionBase() - { - } - - /* parse argument arg, to obtain a value for the option */ - virtual void parse( const String& arg ) = 0; - /* set the argument to the default value */ - //virtual void setDefault() = 0; - int count() - { - return arg_count; - } - Bool isBinary() - { - return is_binary; - } - - int arg_count; - String opt_string; - String opt_desc; - Bool is_binary; -}; - -/** Type specific option storage */ -struct FlagOpt: public OptionBase -{ - FlagOpt( const String& name, Bool default_val, const String& desc ) : - OptionBase( name, desc ), - opt_storage( false ), - opt_default_val( default_val ) - { - is_binary = true; - } - - void parse( const String& arg ) - { - opt_storage = true; - arg_count++; - } - - void setDefault() - { - opt_storage = opt_default_val; - } - Bool opt_storage; - Bool opt_default_val; -}; - -/** Type specific option storage */ -template -struct StandardOpt: public OptionBase -{ - StandardOpt( const String& name, T& storage, const String& desc ) : - OptionBase( name, desc ), - opt_storage( storage ) - { - is_binary = false; - } - - void parse( const String& arg ); - -// void setDefault() -// { -// opt_storage = opt_default_val; -// } - - T& opt_storage; -}; - -/* Generic parsing */ -template -inline void StandardOpt::parse( const String& arg ) -{ - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> opt_storage; - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -template<> -inline void StandardOpt< std::vector >::parse( const String& arg ) -{ - UInt aux_opt_storage; - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> aux_opt_storage; - opt_storage.push_back( aux_opt_storage ); - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -template<> -inline void StandardOpt< std::vector >::parse( const String& arg ) -{ - Int aux_opt_storage; - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> aux_opt_storage; - opt_storage.push_back( aux_opt_storage ); - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -/* string parsing is specialized */ -template<> -inline void StandardOpt::parse( const String& arg ) -{ - opt_storage = arg; - arg_count++; -} - -/* string vector parsing is specialized */ -template<> -inline void StandardOpt >::parse( const String& arg ) -{ - opt_storage.push_back( arg ); - arg_count++; -} - -/** Option class for argument handling using a user provided function */ -struct OptionFunc: public OptionBase -{ - typedef Void (Func)( Options&, const String& ); - - OptionFunc( const String& name, Options& parent_, Func *func_, const String& desc ) : - OptionBase( name, desc ), - parent( parent_ ), - func( func_ ) - { - is_binary = false; - } - - void parse( const String& arg ) - { - func( parent, arg ); - arg_count++; - } - - void setDefault() - { - return; - } - -private: - Options& parent; - void (*func)( Options&, const String& ); -}; - -class OptionSpecific; -class Options -{ -public: - - struct Option - { - Option() : - opt( 0 ) - { - } - ; - ~Option() - { - if( opt ) - delete opt; - } - std::list opt_long; - std::list opt_short; - OptionBase* opt; - }; - typedef std::list OptionsList; - - Options( const String& name = "" ); - ~Options(); - - std::list scanArgv( unsigned argc, const char* argv[] ); - Void scanArgs( std::vector args_array); - Void doHelp( std::ostream& out, unsigned columns = 80 ); - - OptionSpecific addOptions(); - Void setDefaults(); - - OptionBase* operator[]( const String& optName ); - OptionBase* getOption( const String& optName ); - - OptionsList& getOptionList() - { - return opt_list; - } - - Bool hasOpt( const String& optName ); - - void addOption( OptionBase *opt ); - -private: - - typedef std::map OptionMap; - OptionMap opt_long_map; - OptionMap opt_short_map; - - OptionsList opt_list; - - String m_cOptionGroupName; - Bool m_bAllowUnkonw; - - Bool storePair( Bool allow_long, Bool allow_short, const String& name, const String& value ); - Bool storePair( const String& name, const String& value ); - UInt parseLONG( unsigned argc, const char* argv[] ); - UInt parseLONG( String arg ); - unsigned parseSHORT( unsigned argc, const char* argv[] ); -}; +struct OptionBase; +class PlaYUVerOptions; /* Class with templated overloaded operator(), for use by Options::addOptions() */ class OptionSpecific { public: - OptionSpecific( Options& parent_ ) : - parent( parent_ ) - { - } + OptionSpecific( PlaYUVerOptions& parent_ ) : + parent( parent_ ) + { } /** * Add option described by name to the parent Options list, @@ -330,11 +67,7 @@ public: */ template OptionSpecific& - operator()( const String& name, T& storage, const String& desc ) - { - parent.addOption( new StandardOpt( name, storage, desc ) ); - return *this; - } + operator()( const String& name, T& storage, const String& desc ); /** * Add option described by name to the parent Options list, @@ -343,11 +76,7 @@ public: * with desc as an optional help description */ OptionSpecific& - operator()( const String& name, const String& desc ) - { - parent.addOption( new FlagOpt( name, false, desc ) ); - return *this; - } + operator()( const String& name, const String& desc ); /** * Add option described by name to the parent Options list, @@ -356,11 +85,7 @@ public: * with desc as an optional help description */ OptionSpecific& - operator()( const String& name, Bool default_val, const String& desc ) - { - parent.addOption( new FlagOpt( name, default_val, desc ) ); - return *this; - } + operator()( const String& name, Bool default_val, const String& desc ); /** * Add option described by name to the parent Options list, @@ -369,14 +94,60 @@ public: * OptionFunc::Func is called. It is upto this function to correctly * handle evaluating the option's value. */ - OptionSpecific& - operator()( const String& name, OptionFunc::Func *func, const String& desc ) - { - parent.addOption( new OptionFunc( name, parent, func, desc ) ); - return *this; - } +// OptionSpecific& +// operator()( const String& name, OptionFunc::Func *func, const String& desc ) private: - Options& parent; + PlaYUVerOptions& parent; }; + +class PlaYUVerOptions +{ +public: + + struct Option; + + typedef std::list OptionsList; + + PlaYUVerOptions( const String& name = "" ); + ~PlaYUVerOptions(); + + Bool parse( Int argc, Char *argv[] ); + std::list scanArgv( unsigned argc, const char* argv[] ); + Void scanArgs( std::vector args_array ); + Void doHelp( std::ostream& out, unsigned columns = 80 ); + + Bool checkListingOpts(); + Void listModules(); + Void setDefaults(); + + OptionBase* operator[]( const String& optName ); + OptionBase* getOption( const String& optName ); + + Bool hasOpt( const String& optName ); + + OptionSpecific addOptions(); + void addOption( OptionBase *opt ); + +private: + typedef std::map OptionMap; + OptionMap opt_long_map; + OptionMap opt_short_map; + + OptionsList opt_list; + + String m_cOptionGroupName; + Bool m_bAllowUnkonw; + std::list m_aUnhandledArgs; + + Bool storePair( Bool allow_long, Bool allow_short, const String& name, const String& value ); + Bool storePair( const String& name, const String& value ); + UInt parseLONG( unsigned argc, const char* argv[] ); + UInt parseLONG( String arg ); + UInt parseSHORT( unsigned argc, const char* argv[] ); + +}; + + + #endif // __PROGRAMOPTIONS_H__ diff --git a/tools/PlaYUVerToolsCmdParser.cpp b/tools/PlaYUVerToolsCmdParser.cpp index 2af415a..1d5c1c1 100644 --- a/tools/PlaYUVerToolsCmdParser.cpp +++ b/tools/PlaYUVerToolsCmdParser.cpp @@ -58,26 +58,20 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char *argv[] ) { Int iRet = 0; - Opts().addOptions()/**/ - ( "quiet,q", m_bQuiet, "disable verbose" ); - - Opts().addOptions()/**/ + m_cOptions.addOptions()/**/ + ( "quiet,q", m_bQuiet, "disable verbose" ) ( "input,i", m_apcInputs, "input file" ) /**/ - //( "input,i", m_strInput, "input file" ) /**/ ( "output,o", m_strOutput, "output file" ) /**/ ( "size,s", m_strResolution, "size (WxH)" ) /**/ ( "pel_fmt,p", m_strPelFmt, "pixel format" ) /**/ ( "bits_pel", m_uiBitsPerPixel, "bits per pixel" ) /**/ ( "endianness", m_strEndianness, "File endianness (big, little)" ) /**/ - ( "frames,f", m_iFrames, "number of frames to parse" ); - - Opts().addOptions()/**/ + ( "frames,f", m_iFrames, "number of frames to parse" )/**/ ( "quality", m_strQualityMetric, "select a quality metric" ) /**/ ( "module", m_strModule, "select a module (use internal name)" ) /**/ ( "save", "save a specific frame" ); - config( argc, argv ); - if( !parse() ) + if( !m_cOptions.parse( argc, argv ) ) { iRet = 1; } @@ -87,82 +81,26 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char *argv[] ) m_uiLogLevel = LOG_RESULT; } - if( Opts().hasOpt("module") && Opts().hasOpt("help") ) + if( m_cOptions.hasOpt("module") && m_cOptions.hasOpt("help") ) { listModuleHelp(); iRet = 1; } - else if( Opts().hasOpt("help") ) + else if( m_cOptions.hasOpt("help") ) { printf( "Usage: %s modules/quality/save [options] -input=input_file [--output=output_file]\n", argv[0] ); - Opts().doHelp( std::cout ); + m_cOptions.doHelp( std::cout ); iRet = 1; } - if( Opts().hasOpt("module_list") || Opts().hasOpt("module_list_full") ) + if( m_cOptions.hasOpt("module_list") || m_cOptions.hasOpt("module_list_full") ) { - listModules(); + m_cOptions.listModules(); iRet = 1; } return iRet; } -Void PlaYUVerToolsCmdParser::listModules() -{ - Bool bDetailed = false; - - if( Opts().hasOpt("module_list_full") ) - bDetailed = true; - - PlaYUVerModuleIf* pcCurrModuleIf; - PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); - PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); - - printf( "PlaYUVer available modules: \n" ); - //printf( " " ); - printf( " [Internal Name] " ); - if( bDetailed ) - { - printf( " [Full Name] " ); - printf( " [Type] " ); - printf( " [Description]" ); - } - printf( " \n" ); - - Char ModuleNameString[40]; - - for( UInt i = 0; it != PlaYUVerModuleFactoryMap.end(); ++it, i++ ) - { - printf( " " ); - printf( "%-30s", it->first ); - if( bDetailed ) - { - ModuleNameString[0] = '\0'; - pcCurrModuleIf = it->second(); - - if( pcCurrModuleIf->m_pchModuleCategory ) - { - strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleCategory ); - strcat( ModuleNameString, "/" ); - } - strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleName ); - printf( " %-40s", ModuleNameString ); - switch( pcCurrModuleIf->m_iModuleType ) - { - case FRAME_PROCESSING_MODULE: - printf( " Processing " ); - break; - case FRAME_MEASUREMENT_MODULE: - printf( " Measurement " ); - break; - } - printf( " %s", pcCurrModuleIf->m_pchModuleTooltip ); - pcCurrModuleIf->Delete(); - } - printf( "\n" ); - } -} - Void PlaYUVerToolsCmdParser::listModuleHelp() { String moduleName = m_strModule; diff --git a/tools/PlaYUVerToolsCmdParser.h b/tools/PlaYUVerToolsCmdParser.h index adfde00..0796fd2 100644 --- a/tools/PlaYUVerToolsCmdParser.h +++ b/tools/PlaYUVerToolsCmdParser.h @@ -27,12 +27,12 @@ #include "config.h" #include "lib/PlaYUVerDefs.h" -#include "lib/PlaYUVerCmdParser.h" +#include "lib/PlaYUVerOptions.h" class PlaYUVerStream; -class PlaYUVerToolsCmdParser: public PlaYUVerCmdParser +class PlaYUVerToolsCmdParser { friend class PlaYUVerTools; public: @@ -60,8 +60,13 @@ public: Int parseToolsArgs( Int argc, Char *argv[] ); + PlaYUVerOptions& Opts() + { + return m_cOptions; + } + private: - Options m_cParserOptions; + PlaYUVerOptions m_cOptions; UInt m_uiLogLevel; /** From 67e6d49be14ed3720bdbbe7e50fa98057aac6959 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 2 May 2016 23:31:41 +0100 Subject: [PATCH 2/2] Change Option parsing implmentation Clean structs and classes. Hide implementation details Remove redundant classes --- app/ModulesHandle.cpp | 5 +- app/ModulesHandleOptDialog.cpp | 6 +- app/PlaYUVerApp.cpp | 15 +- app/PlaYUVerApp.h | 4 +- app/VideoSubWindow.cpp | 6 +- lib/PlaYUVerDefs.h | 16 ++ lib/PlaYUVerFrame.cpp | 20 +-- lib/PlaYUVerOptions.cpp | 265 +++++++++++-------------------- lib/PlaYUVerOptions.h | 139 ++++++++++------ lib/PlaYUVerStream.cpp | 22 +-- lib/PlaYUVerStream.h | 4 +- lib/StreamHandlerLibav.cpp | 2 +- tools/PlaYUVerTools.cpp | 120 +++++++------- tools/PlaYUVerTools.h | 3 +- tools/PlaYUVerToolsCmdParser.cpp | 8 +- tools/PlaYUVerToolsCmdParser.h | 3 +- tools/main.cpp | 2 +- 17 files changed, 307 insertions(+), 333 deletions(-) diff --git a/app/ModulesHandle.cpp b/app/ModulesHandle.cpp index f9fb0b9..2feee91 100644 --- a/app/ModulesHandle.cpp +++ b/app/ModulesHandle.cpp @@ -495,7 +495,7 @@ Void ModulesHandle::applyAllModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) if( !pcCurrModuleIf->m_pcModuleStream ) { UInt Width = 0, Height = 0, FrameRate = 30; - Int BitsPixel, InputFormat = -1; + Int BitsPixel, Endianness, InputFormat = -1; QString supported = tr( "Supported Files (" ); QStringList formatsList; @@ -525,10 +525,11 @@ Void ModulesHandle::applyAllModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) Height = pcCurrModuleIf->m_pcProcessedFrame->getHeight(); InputFormat = pcCurrModuleIf->m_pcProcessedFrame->getPelFormat(); BitsPixel = pcCurrModuleIf->m_pcProcessedFrame->getBitsPel(); + Endianness = pcCurrModuleIf->m_pcProcessedFrame->getEndianness(); FrameRate = pcCurrModuleIf->m_pcSubWindow[0]->getInputStream()->getFrameRate(); pcCurrModuleIf->m_pcModuleStream = new PlaYUVerStream; - if( !pcCurrModuleIf->m_pcModuleStream->open( fileName.toStdString(), Width, Height, InputFormat, BitsPixel, FrameRate, false ) ) + if( !pcCurrModuleIf->m_pcModuleStream->open( fileName.toStdString(), Width, Height, InputFormat, BitsPixel, Endianness, FrameRate, false ) ) { delete pcCurrModuleIf->m_pcModuleStream; pcCurrModuleIf->m_pcModuleStream = NULL; diff --git a/app/ModulesHandleOptDialog.cpp b/app/ModulesHandleOptDialog.cpp index ac8bdd4..6e1a61b 100644 --- a/app/ModulesHandleOptDialog.cpp +++ b/app/ModulesHandleOptDialog.cpp @@ -86,11 +86,11 @@ ModulesHandleOptDialog::ModulesHandleOptDialog( QWidget *parent, PlaYUVerAppModu resize( 400, 10 ); setWindowTitle( "Select module parameters" ); - const Options::OptionsList& moduleOptions = m_pcCurrModuleIf->m_pcModule->m_cModuleOptions.getOptionList(); + const PlaYUVerOptions::OptionsList& moduleOptions = m_pcCurrModuleIf->m_pcModule->m_cModuleOptions.getOptionList(); QVBoxLayout* optionsLayout = new QVBoxLayout; OpionConfiguration* pcOption; - for( Options::OptionsList::const_iterator it = moduleOptions.begin(); it != moduleOptions.end(); ++it ) + for( PlaYUVerOptions::OptionsList::const_iterator it = moduleOptions.begin(); it != moduleOptions.end(); ++it ) { pcOption = new OpionConfiguration( ( *it )->opt ); m_apcOptionList.append( pcOption ); @@ -142,7 +142,7 @@ Int ModulesHandleOptDialog::runConfiguration() } if( argsArray.size() > 0 ) { - m_pcCurrModuleIf->m_pcModule->m_cModuleOptions.scanArgs( argsArray ); + m_pcCurrModuleIf->m_pcModule->m_cModuleOptions.parse( argsArray ); } return QDialog::Accepted; } diff --git a/app/PlaYUVerApp.cpp b/app/PlaYUVerApp.cpp index 11ee89a..798fa98 100644 --- a/app/PlaYUVerApp.cpp +++ b/app/PlaYUVerApp.cpp @@ -94,37 +94,36 @@ PlaYUVerApp::PlaYUVerApp() : Bool PlaYUVerApp::parseArgs( Int argc, Char *argv[] ) { Bool bRet = false; - m_pcCmdParser = new PlaYUVerCmdParser(); - m_pcCmdParser->config( argc, argv ); + PlaYUVerOptions pcCmdParser; std::vector m_apcInputs; String strResolution( "" ); String strPelFmt( "" ); - m_pcCmdParser->Opts().addOptions()/**/ + pcCmdParser.addOptions()/**/ ( "input,i", m_apcInputs, "input file" ) /**/ ( "size,s", strResolution, "size (WxH)" ) /**/ ( "pel_fmt", strPelFmt, "pixel format" ); - if( !m_pcCmdParser->parse() ) + if( !pcCmdParser.parse( argc, argv ) ) { bRet |= true; } - if( m_pcCmdParser->Opts()["help"]->count() ) + if( pcCmdParser.hasOpt("help") ) { printf( "Usage: %s [options] input_file[s]\n", argv[0] ); - m_pcCmdParser->Opts().doHelp( std::cout ); + pcCmdParser.doHelp( std::cout ); bRet |= true; } - if( m_pcCmdParser->Opts()["input"]->count() ) + if( pcCmdParser.hasOpt("input") ) { for( UInt i = 1; i < m_apcInputs.size(); i++ ) { loadFile( QString::fromStdString( m_apcInputs[i] ) ); } } - std::list& argv_unhandled = m_pcCmdParser->getNoArgs(); + std::list& argv_unhandled = pcCmdParser.getUnhandledArgs(); for( std::list::const_iterator it = argv_unhandled.begin(); it != argv_unhandled.end(); it++ ) { loadFile( QString::fromStdString( *it ) ); diff --git a/app/PlaYUVerApp.h b/app/PlaYUVerApp.h index a83d450..02826b7 100644 --- a/app/PlaYUVerApp.h +++ b/app/PlaYUVerApp.h @@ -55,7 +55,7 @@ class QAction; class QSignalMapper; -class PlaYUVerCmdParser; +class PlaYUVerOptions; class PlaYUVerStream; class PlaYUVerSubWindowHandle; class VideoHandle; @@ -118,7 +118,7 @@ private Q_SLOTS: private: PlaYUVerAppAdaptor* m_pDBusAdaptor; - PlaYUVerCmdParser* m_pcCmdParser; + PlaYUVerOptions* m_pcCmdParser; /** * Save the current subwindow for every category */ diff --git a/app/VideoSubWindow.cpp b/app/VideoSubWindow.cpp index ef158e4..7413d1e 100644 --- a/app/VideoSubWindow.cpp +++ b/app/VideoSubWindow.cpp @@ -328,7 +328,7 @@ Bool VideoSubWindow::loadFile( QString cFilename, Bool bForceDialog ) try { - m_pCurrStream->open( cFilename.toStdString(), Width, Height, InputFormat, BitsPel, Endianness, FrameRate ); + m_pCurrStream->open( cFilename.toStdString(), Width, Height, InputFormat, BitsPel, Endianness, FrameRate, true ); } catch( const char *msg ) { @@ -336,7 +336,7 @@ Bool VideoSubWindow::loadFile( QString cFilename, Bool bForceDialog ) { return false; } - m_pCurrStream->open( cFilename.toStdString(), Width, Height, InputFormat, BitsPel, Endianness, FrameRate ); + m_pCurrStream->open( cFilename.toStdString(), Width, Height, InputFormat, BitsPel, Endianness, FrameRate, true ); } m_sStreamInfo.m_cFilename = cFilename; @@ -367,7 +367,7 @@ Bool VideoSubWindow::loadFile( PlaYUVerStreamInfo* streamInfo ) } if( !m_pCurrStream->open( streamInfo->m_cFilename.toStdString(), streamInfo->m_uiWidth, streamInfo->m_uiHeight, streamInfo->m_iPelFormat, - streamInfo->m_uiBitsPelPixel, streamInfo->m_iEndianness, streamInfo->m_uiFrameRate ) ) + streamInfo->m_uiBitsPelPixel, streamInfo->m_iEndianness, streamInfo->m_uiFrameRate, true) ) { return false; } diff --git a/lib/PlaYUVerDefs.h b/lib/PlaYUVerDefs.h index 6262817..b89e5f2 100644 --- a/lib/PlaYUVerDefs.h +++ b/lib/PlaYUVerDefs.h @@ -134,6 +134,22 @@ inline String uppercase( const String& in ) return out; } +struct PlaYUVerFailure: public std::exception +{ + String m_class_name; + String m_error_msg; + PlaYUVerFailure( String error_msg ) throw() : + m_error_msg( error_msg ) { } + PlaYUVerFailure( String class_name, String error_msg ) throw() : + m_class_name( class_name ), + m_error_msg( error_msg ) { } + ~PlaYUVerFailure() throw() { } + const Char* what() const throw() + { + String* msg = new String( "[" + m_class_name + "] " + m_error_msg ); + return msg->c_str(); + } +}; #endif // __PLAYUVERDEFS_H__ diff --git a/lib/PlaYUVerFrame.cpp b/lib/PlaYUVerFrame.cpp index 8f440fe..0f5b2c4 100644 --- a/lib/PlaYUVerFrame.cpp +++ b/lib/PlaYUVerFrame.cpp @@ -216,13 +216,13 @@ Void PlaYUVerFramePrivate::init( UInt width, UInt height, Int pel_format, Int bi m_iEndianness = endianness; if( m_uiBitsPel > 8 && m_iEndianness == -1 ) { - throw "Invalid endianness type for Bits per Pixel greater than 8"; + throw PlaYUVerFailure( "PlaYUVerFrame", "Invalid endianness type for Bits per Pixel greater than 8" ); } m_uiHalfPelValue = 1 << ( m_uiBitsPel - 1 ); if( m_uiWidth == 0 || m_uiHeight == 0 || m_iPixelFormat == -1 || bitsPixel > 16 ) { - throw "Cannot create a PlYUVerFrame of this type"; + throw PlaYUVerFailure( "PlaYUVerFrame", "Cannot create a PlYUVerFrame of this type" ); } m_pcPelFormat = &( g_PlaYUVerPixFmtDescriptorsList[pel_format] ); @@ -1090,14 +1090,14 @@ Void PlaYUVerFrame::fromCvMat( cv::Mat* pcCvFrame ) { switch( pcCvFrame->channels() ) { - case 1: - d->m_iPixelFormat = findPixelFormat( "GRAY" ); - break; - case 3: - d->m_iPixelFormat = findPixelFormat( "BGR24" ); - break; - default: - return; + case 1: + d->m_iPixelFormat = findPixelFormat( "GRAY" ); + break; + case 3: + d->m_iPixelFormat = findPixelFormat( "BGR24" ); + break; + default: + return; } } d->init( pcCvFrame->cols, pcCvFrame->rows, d->m_iPixelFormat, 8, -1 ); diff --git a/lib/PlaYUVerOptions.cpp b/lib/PlaYUVerOptions.cpp index 58451c8..816dba2 100644 --- a/lib/PlaYUVerOptions.cpp +++ b/lib/PlaYUVerOptions.cpp @@ -65,73 +65,27 @@ struct ParseFailure: public std::exception arg( arg0 ), val( val0 ) { } ~ParseFailure() throw() { } - const char* what() const throw() + const Char* what() const throw() { return "Option Parse Failure"; } }; -/** OptionBase: Virtual base class for storing information relating to a - * specific option This base class describes common elements. Type specific - * information should be stored in a derived class. */ -class OptionBase -{ -public: - OptionBase( const String& name, const String& desc ) : - arg_count( 0 ), - opt_string( name ), - opt_desc( desc ) - { - } - - virtual ~OptionBase() - { - } - - /* parse argument arg, to obtain a value for the option */ - virtual void parse( const String& arg ) = 0; - /* set the argument to the default value */ - //virtual void setDefault() = 0; - int count() - { - return arg_count; - } - Bool isBinary() - { - return is_binary; - } - - int arg_count; - String opt_string; - String opt_desc; - Bool is_binary; -}; - /** Type specific option storage */ class BoolOption: public OptionBase { public: - BoolOption( const String& name, Bool default_val, const String& desc ) : - OptionBase( name, desc ), - opt_storage( false ), - opt_default_val( default_val ) + BoolOption( const String& name, const String& desc ) : + OptionBase( name, desc ) { is_binary = true; } void parse( const String& arg ) { - opt_storage = true; arg_count++; } - - void setDefault() - { - opt_storage = opt_default_val; - } - Bool opt_storage; - Bool opt_default_val; }; /** Type specific option storage */ @@ -148,21 +102,25 @@ public: void parse( const String& arg ); T& opt_storage; + std::vector opt_storage_array; }; /* Generic parsing */ template inline void StandardOption::parse( const String& arg ) { - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try + if( !is_binary ) { - arg_ss >> opt_storage; - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> opt_storage; + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } } arg_count++; } @@ -239,47 +197,63 @@ public: arg_count++; } - void setDefault() - { - return; - } - private: PlaYUVerOptions& parent; void ( *func )( PlaYUVerOptions&, const String& ); }; -struct PlaYUVerOptions::Option +/* Helper method to initiate adding options to Options */ + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, const String& desc ) { - Option() : - opt( 0 ) - { - } - ~Option() - { - if( opt ) - delete opt; - } - std::list opt_long; - std::list opt_short; - OptionBase* opt; -}; + addOption( new BoolOption( name, desc ) ); + return *this; +} + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with desc as an optional help description + */ +template +PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, T& storage, const String& desc ) +{ + addOption( new StandardOption( name, storage, desc ) ); + return *this; +} + +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, Bool& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, UInt& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, Int& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, Int64& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, String& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, std::vector& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, std::vector& storage, const String& desc ); +template PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, std::vector& storage, const String& desc ); + +/** + * Add option described by name to the parent Options list, + * with desc as an optional help description + * instead of storing the value somewhere, a function of type + * FunctionOption::Func is called. It is upto this function to correctly + * handle evaluating the option's value. + */ +// PlaYUVerOptions& PlaYUVerOptions::operator()( const String& name, FunctionOption::Func *func, const String& desc ) +// { +// addOption( new FunctionOption( name, parent, func, desc ) ); +// return *this; +// } PlaYUVerOptions::PlaYUVerOptions( const String& name ) { m_cOptionGroupName = name; m_bAllowUnkonw = true; - - /** - * Add default options - */ - addOptions()/**/ - ( "help", "produce help message" )/**/ - ( "version", "show version and exit" )/**/ - ( "pel_fmts", "list pixel formats" ) /**/ - ( "quality_metrics", "list supported quality metrics" )/**/ - ( "module_list", "list supported modules" ) /**/ - ( "module_list_full", "detailed list supported modules" ); } PlaYUVerOptions::~PlaYUVerOptions() @@ -321,12 +295,25 @@ OptionBase* PlaYUVerOptions::getOption( const String& optName ) Bool PlaYUVerOptions::hasOpt( const String& optName ) { - return getOption( optName )->count() > 0 ? true : false; + OptionBase* opt = getOption( optName ); + return opt ? opt->count() > 0 ? true : false : false; } -OptionSpecific PlaYUVerOptions::addOptions() +Void PlaYUVerOptions::addDefaultOptions() { - return OptionSpecific( *this ); + addOptions()/**/ + ( "help,h", "produce help message" )/**/ + ( "version", "show version and exit" )/**/ + ( "pel_fmts", "list pixel formats" ) /**/ + ( "quality_metrics", "list supported quality metrics" )/**/ + ( "module_list", "list supported modules" ) /**/ + ( "module_list_full", "detailed list supported modules" ); +} + + +PlaYUVerOptions& PlaYUVerOptions::addOptions() +{ + return *this; } void PlaYUVerOptions::addOption( OptionBase *opt ) @@ -361,67 +348,7 @@ void PlaYUVerOptions::addOption( OptionBase *opt ) opt_list.push_back( names ); } -/* Helper method to initiate adding options to Options */ -/** - * Add option described by name to the parent Options list, - * with storage for the option's value - * with default_val as the default value - * with desc as an optional help description - */ -template -OptionSpecific& OptionSpecific::operator()( const String& name, T& storage, const String& desc ) -{ - parent.addOption( new StandardOption( name, storage, desc ) ); - return *this; -} - -/** - * Add option described by name to the parent Options list, - * with storage for the option's value - * with default_val as the default value - * with desc as an optional help description - */ -OptionSpecific& OptionSpecific::operator()( const String& name, const String& desc ) -{ - parent.addOption( new BoolOption( name, false, desc ) ); - return *this; -} - -/** - * Add option described by name to the parent Options list, - * with storage for the option's value - * with default_val as the default value - * with desc as an optional help description - */ -OptionSpecific& OptionSpecific::operator()( const String& name, Bool default_val, const String& desc ) -{ - parent.addOption( new BoolOption( name, default_val, desc ) ); - return *this; -} - -/** - * Add option described by name to the parent Options list, - * with desc as an optional help description - * instead of storing the value somewhere, a function of type - * FunctionOption::Func is called. It is upto this function to correctly - * handle evaluating the option's value. - */ -// OptionSpecific& OptionSpecific::operator()( const String& name, FunctionOption::Func *func, const String& desc ) -// { -// parent.addOption( new FunctionOption( name, parent, func, desc ) ); -// return *this; -// } - -/* for all options in opts, set their storage to their specified - * default value */ -Void PlaYUVerOptions::setDefaults() -{ -// for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) -// { -// ( *it )->opt->setDefault(); -// } -} static void setOptions( PlaYUVerOptions::OptionsList& opt_list, const string& value ) { @@ -483,7 +410,7 @@ Bool PlaYUVerOptions::storePair( const string& name, const string& value ) /** * returns number of extra arguments consumed */ -UInt PlaYUVerOptions::parseLONG( unsigned argc, const char* argv[] ) +UInt PlaYUVerOptions::parseLONG( UInt argc, Char* argv[] ) { /* gnu style long options can take the forms: * --option=arg @@ -494,7 +421,7 @@ UInt PlaYUVerOptions::parseLONG( unsigned argc, const char* argv[] ) size_t arg_opt_sep = arg.find_first_of( '=' ); string option = arg.substr( arg_opt_start, arg_opt_sep - arg_opt_start ); - unsigned extra_argc_consumed = 0; + UInt extra_argc_consumed = 0; if( arg_opt_sep == string::npos ) { /* no argument found => argument in argv[1] (maybe) */ @@ -534,7 +461,7 @@ UInt PlaYUVerOptions::parseLONG( string arg ) size_t arg_opt_sep = arg.find_first_of( '=' ); string option = arg.substr( arg_opt_start, arg_opt_sep - arg_opt_start ); - unsigned extra_argc_consumed = 0; + UInt extra_argc_consumed = 0; if( arg_opt_sep == string::npos ) { /* no argument found => argument in argv[1] (maybe) */ @@ -553,7 +480,7 @@ UInt PlaYUVerOptions::parseLONG( string arg ) return extra_argc_consumed; } -unsigned PlaYUVerOptions::parseSHORT( unsigned argc, const char* argv[] ) +UInt PlaYUVerOptions::parseSHORT( UInt argc, Char* argv[] ) { /* short options can take the forms: * --option arg @@ -579,11 +506,11 @@ unsigned PlaYUVerOptions::parseSHORT( unsigned argc, const char* argv[] ) return 1; } -Bool PlaYUVerOptions::parse( Int argc, Char *argv[] ) +Bool PlaYUVerOptions::parse( UInt argc, Char *argv[] ) { try { - m_aUnhandledArgs = scanArgv( argc, ( const Char** )argv ); + m_aUnhandledArgs = scanArgv( argc, argv ); if( checkListingOpts() ) { @@ -605,12 +532,12 @@ Bool PlaYUVerOptions::parse( Int argc, Char *argv[] ) } -list PlaYUVerOptions::scanArgv( unsigned argc, const char* argv[] ) +list PlaYUVerOptions::scanArgv( UInt argc, Char *argv[] ) { /* a list for anything that didn't get handled as an option */ - list non_option_arguments; + list non_option_arguments; - for( unsigned i = 1; i < argc; i++ ) + for( UInt i = 1; i < argc; i++ ) { if( argv[i][0] != '-' ) { @@ -651,9 +578,9 @@ list PlaYUVerOptions::scanArgv( unsigned argc, const char* argv[] ) return non_option_arguments; } -Void PlaYUVerOptions::scanArgs( std::vector args_array ) +Void PlaYUVerOptions::parse( std::vector args_array ) { - for( unsigned i = 0; i < args_array.size(); i++ ) + for( UInt i = 0; i < args_array.size(); i++ ) { i += parseLONG( args_array[i] ); } @@ -666,13 +593,13 @@ static const char spaces[41] = " "; * using the formatting: "-x, --long", * if a short/long option isn't specified, it is not printed */ -static void doHelpOpt( ostream& out, const PlaYUVerOptions::Option& entry, unsigned pad_short = 0 ) +static void doHelpOpt( ostream& out, const PlaYUVerOptions::Option& entry, UInt pad_short = 0 ) { pad_short = min( pad_short, 8u ); if( !entry.opt_short.empty() ) { - unsigned pad = max( ( int )pad_short - ( int )entry.opt_short.front().size(), 0 ); + UInt pad = max( ( int )pad_short - ( int )entry.opt_short.front().size(), 0 ); out << "-" << entry.opt_short.front(); if( !entry.opt_long.empty() ) @@ -695,20 +622,20 @@ static void doHelpOpt( ostream& out, const PlaYUVerOptions::Option& entry, unsig } /* format the help text */ -Void PlaYUVerOptions::doHelp( ostream& out, unsigned columns ) +Void PlaYUVerOptions::doHelp( ostream& out, UInt columns ) { - const unsigned pad_short = 3; + const UInt pad_short = 3; /* first pass: work out the longest option name */ - unsigned max_width = 0; + UInt max_width = 0; for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { ostringstream line( ios_base::out ); doHelpOpt( line, **it, pad_short ); - max_width = max( max_width, ( unsigned )line.tellp() ); + max_width = max( max_width, ( UInt )line.tellp() ); } - unsigned opt_width = min( max_width + 2, 28u + pad_short ) + 2; - unsigned desc_width = columns - opt_width; + UInt opt_width = min( max_width + 2, 28u + pad_short ) + 2; + UInt desc_width = columns - opt_width; /* second pass: write out formatted option and help text. * - align start of help text to start at opt_width diff --git a/lib/PlaYUVerOptions.h b/lib/PlaYUVerOptions.h index 75f19e7..599e935 100644 --- a/lib/PlaYUVerOptions.h +++ b/lib/PlaYUVerOptions.h @@ -48,26 +48,89 @@ #include "PlaYUVerDefs.h" -struct OptionBase; -class PlaYUVerOptions; -/* Class with templated overloaded operator(), for use by Options::addOptions() */ -class OptionSpecific +/** OptionBase: Virtual base class for storing information relating to a + * specific option This base class describes common elements. + * Type specific information should be stored in a derived class. + */ +class OptionBase { public: - OptionSpecific( PlaYUVerOptions& parent_ ) : - parent( parent_ ) + OptionBase( const String& name, const String& desc ) : + arg_count( 0 ), + opt_string( name ), + opt_desc( desc ) { } - /** - * Add option described by name to the parent Options list, - * with storage for the option's value - * with default_val as the default value - * with desc as an optional help description - */ - template - OptionSpecific& - operator()( const String& name, T& storage, const String& desc ); + virtual ~OptionBase() {} + + /* parse argument arg, to obtain a value for the option */ + virtual void parse( const String& arg ) = 0; + Int count() + { + return arg_count; + } + Bool isBinary() + { + return is_binary; + } + + int arg_count; + String opt_string; + String opt_desc; + Bool is_binary; +}; + + +class PlaYUVerOptions +{ +public: + + struct Option + { + Option() : + opt( 0 ) + { + } + ~Option() + { + if( opt ) + delete opt; + } + std::list opt_long; + std::list opt_short; + OptionBase* opt; + }; + + + typedef std::list OptionsList; + + PlaYUVerOptions( const String& name = "" ); + ~PlaYUVerOptions(); + + Bool parse( UInt argc, Char *argv[] ); + Void parse( std::vector args_array ); + + std::list& getUnhandledArgs() + { + return m_aUnhandledArgs; + } + + Void doHelp( std::ostream& out, unsigned columns = 80 ); + + OptionBase* operator[]( const String& optName ); + OptionBase* getOption( const String& optName ); + + Bool hasOpt( const String& optName ); + + OptionsList getOptionList() + { + return opt_list; + } + + Void addDefaultOptions(); + PlaYUVerOptions& addOptions(); + void addOption( OptionBase *opt ); /** * Add option described by name to the parent Options list, @@ -75,7 +138,7 @@ public: * with default_val as the default value * with desc as an optional help description */ - OptionSpecific& + PlaYUVerOptions& operator()( const String& name, const String& desc ); /** @@ -84,8 +147,10 @@ public: * with default_val as the default value * with desc as an optional help description */ - OptionSpecific& - operator()( const String& name, Bool default_val, const String& desc ); + template + PlaYUVerOptions& + operator()( const String& name, T& storage, const String& desc ); + /** * Add option described by name to the parent Options list, @@ -94,40 +159,12 @@ public: * OptionFunc::Func is called. It is upto this function to correctly * handle evaluating the option's value. */ -// OptionSpecific& -// operator()( const String& name, OptionFunc::Func *func, const String& desc ) -private: - PlaYUVerOptions& parent; -}; +// PlaYUVerOptions& +// operator()( const String& name, OptionFunc::Func *func, const String& desc ); -class PlaYUVerOptions -{ -public: - - struct Option; - - typedef std::list OptionsList; - - PlaYUVerOptions( const String& name = "" ); - ~PlaYUVerOptions(); - - Bool parse( Int argc, Char *argv[] ); - std::list scanArgv( unsigned argc, const char* argv[] ); - Void scanArgs( std::vector args_array ); - Void doHelp( std::ostream& out, unsigned columns = 80 ); - Bool checkListingOpts(); Void listModules(); - Void setDefaults(); - - OptionBase* operator[]( const String& optName ); - OptionBase* getOption( const String& optName ); - - Bool hasOpt( const String& optName ); - - OptionSpecific addOptions(); - void addOption( OptionBase *opt ); private: typedef std::map OptionMap; @@ -142,12 +179,12 @@ private: Bool storePair( Bool allow_long, Bool allow_short, const String& name, const String& value ); Bool storePair( const String& name, const String& value ); - UInt parseLONG( unsigned argc, const char* argv[] ); + UInt parseLONG( UInt argc, Char* argv[] ); UInt parseLONG( String arg ); - UInt parseSHORT( unsigned argc, const char* argv[] ); + UInt parseSHORT( UInt argc, Char* argv[] ); + std::list scanArgv( UInt argc, Char* argv[] ); }; - #endif // __PROGRAMOPTIONS_H__ diff --git a/lib/PlaYUVerStream.cpp b/lib/PlaYUVerStream.cpp index 586cc96..c7b1a3e 100644 --- a/lib/PlaYUVerStream.cpp +++ b/lib/PlaYUVerStream.cpp @@ -196,14 +196,14 @@ Bool PlaYUVerStream::open( String filename, UInt width, UInt height, Int input_f m_pfctCreateHandler = PlaYUVerStream::findStreamHandler( filename, m_bIsInput ); if( !m_pfctCreateHandler ) { - throw "[PlaYUVerStream] Invalid handler"; + throw PlaYUVerFailure("PlaYUVerStream", "Invalid handler"); } m_pcHandler = m_pfctCreateHandler(); if( !m_pcHandler ) { - throw "[PlaYUVerStream] Cannot create handler"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot create handler"); } m_cFilename = filename; @@ -220,7 +220,7 @@ Bool PlaYUVerStream::open( String filename, UInt width, UInt height, Int input_f if( !m_pcHandler->openHandler( m_cFilename, m_bIsInput ) ) { close(); - throw "[PlaYUVerStream] Cannot open file"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot open file " + m_cFilename ); return m_bInit; } @@ -228,7 +228,7 @@ Bool PlaYUVerStream::open( String filename, UInt width, UInt height, Int input_f if( m_pcHandler->m_uiWidth <= 0 || m_pcHandler->m_uiHeight <= 0 || m_pcHandler->m_iPixelFormat < 0 ) { close(); - throw "[PlaYUVerStream] Incorrect configuration"; + throw PlaYUVerFailure("PlaYUVerStream", "Incorrect configuration"); return m_bInit; } @@ -244,7 +244,7 @@ Bool PlaYUVerStream::open( String filename, UInt width, UInt height, Int input_f catch( const char *msg ) { close(); - throw "[PlaYUVerStream] Cannot allocated frame buffer"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot allocated frame buffer"); return m_bInit; } } @@ -261,14 +261,14 @@ Bool PlaYUVerStream::open( String filename, UInt width, UInt height, Int input_f if( m_bIsInput && m_uiTotalFrameNum <= 0 ) { close(); - throw "[PlaYUVerStream] Incorrect configuration: less than one frame"; + throw PlaYUVerFailure("PlaYUVerStream", "Incorrect configuration: less than one frame"); return m_bInit; } if( !m_pcHandler->configureBuffer( m_pcCurrFrame ) ) { close(); - throw "[PlaYUVerStream] Cannot allocated memory"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot allocated memory"); return m_bInit; } @@ -286,7 +286,7 @@ Bool PlaYUVerStream::reload() m_pcHandler->closeHandler(); if( !m_pcHandler->openHandler( m_cFilename, m_bIsInput ) ) { - throw "[PlaYUVerStream] Cannot open file using FFmpeg libs"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot open file"); } m_pcHandler->setBytesPerFrame( m_pcCurrFrame->getBytesPerFrame() ); m_uiTotalFrameNum = m_pcHandler->calculateFrameNumber(); @@ -466,7 +466,7 @@ Void PlaYUVerStream::readFrame() if( !m_pcHandler->read( m_pcNextFrame ) ) { - throw "[PlaYUVerStream] Cannot read file"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot read file"); return; } @@ -483,7 +483,7 @@ Void PlaYUVerStream::writeFrame( PlaYUVerFrame *pcFrame ) { if( !m_pcHandler->write( pcFrame ) ) { - throw "[PlaYUVerStream] Cannot write into the file"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot write into the file"); } return; } @@ -588,7 +588,7 @@ Bool PlaYUVerStream::seekInput( UInt64 new_frame_num ) if( !m_pcHandler->seek( new_frame_num ) ) { - throw "[PlaYUVerStream] Cannot write into the file"; + throw PlaYUVerFailure("PlaYUVerStream", "Cannot write into the file"); } m_uiCurrFrameFileIdx = new_frame_num; diff --git a/lib/PlaYUVerStream.h b/lib/PlaYUVerStream.h index 3449985..690c2dc 100644 --- a/lib/PlaYUVerStream.h +++ b/lib/PlaYUVerStream.h @@ -103,8 +103,8 @@ public: String getFormatName(); String getCodecName(); - Bool open( String filename, String resolution, String input_format, UInt bitsPel, Int endianness, UInt frame_rate, Bool bInput = true ); - Bool open( String filename, UInt width, UInt height, Int input_format, UInt bitsPel, Int endianness, UInt frame_rate, Bool bInput = true ); + Bool open( String filename, String resolution, String input_format, UInt bitsPel, Int endianness, UInt frame_rate, Bool bInput ); + Bool open( String filename, UInt width, UInt height, Int input_format, UInt bitsPel, Int endianness, UInt frame_rate, Bool bInput ); Bool reload(); Void close(); diff --git a/lib/StreamHandlerLibav.cpp b/lib/StreamHandlerLibav.cpp index 8b9e9aa..d62216e 100644 --- a/lib/StreamHandlerLibav.cpp +++ b/lib/StreamHandlerLibav.cpp @@ -216,7 +216,7 @@ Bool StreamHandlerLibav::openHandler( String strFilename, Bool bInput ) } if( m_iPixelFormat == PlaYUVerFrame::NO_FMT ) { - throw "Cannot open file using FFmpeg libs - unsupported pixel format"; + throw PlaYUVerFailure("Cannot open file using FFmpeg libs - unsupported pixel format"); return false; } diff --git a/tools/PlaYUVerTools.cpp b/tools/PlaYUVerTools.cpp index a714ad6..5c2c1a1 100644 --- a/tools/PlaYUVerTools.cpp +++ b/tools/PlaYUVerTools.cpp @@ -60,9 +60,9 @@ Int PlaYUVerTools::openInputs() /** * Create input streams */ - if( m_cCmdLineHandler.Opts().hasOpt( "input" ) ) + if( Opts().hasOpt( "input" ) ) { - std::vector inputFileNames = m_cCmdLineHandler.m_apcInputs; + std::vector inputFileNames = m_apcInputs; String resolutionString( "" ); String fmtString( "yuv420p" ); @@ -72,25 +72,25 @@ Int PlaYUVerTools::openInputs() PlaYUVerStream* pcStream; for( UInt i = 0; i < inputFileNames.size() && i < MAX_NUMBER_INPUTS; i++ ) { - if( m_cCmdLineHandler.Opts().hasOpt( "size" ) ) + if( Opts().hasOpt( "size" ) ) { - resolutionString = GET_PARAM( m_cCmdLineHandler.m_strResolution, 0 ); + resolutionString = GET_PARAM( m_strResolution, 0 ); } - if( m_cCmdLineHandler.Opts().hasOpt( "pel_fmt" ) ) + if( Opts().hasOpt( "pel_fmt" ) ) { - fmtString = GET_PARAM( m_cCmdLineHandler.m_strPelFmt, i ); + fmtString = GET_PARAM( m_strPelFmt, i ); } - if( m_cCmdLineHandler.Opts().hasOpt( "bits_pel" ) ) + if( Opts().hasOpt( "bits_pel" ) ) { - uiBitPerPixel = GET_PARAM( m_cCmdLineHandler.m_uiBitsPerPixel, i ); + uiBitPerPixel = GET_PARAM( m_uiBitsPerPixel, i ); } - if( m_cCmdLineHandler.Opts().hasOpt( "endianness" ) ) + if( Opts().hasOpt( "endianness" ) ) { - if( GET_PARAM( m_cCmdLineHandler.m_strEndianness, i ) == "big" ) + if( GET_PARAM( m_strEndianness, i ) == "big" ) { uiEndianness = 0; } - if( GET_PARAM( m_cCmdLineHandler.m_strEndianness, i ) == "little" ) + if( GET_PARAM( m_strEndianness, i ) == "little" ) { uiEndianness = 1; } @@ -98,25 +98,25 @@ Int PlaYUVerTools::openInputs() pcStream = new PlaYUVerStream; try { - if( !pcStream->open( inputFileNames[i], resolutionString, fmtString, uiBitPerPixel, uiEndianness, 1 ) ) + if( !pcStream->open( inputFileNames[i], resolutionString, fmtString, uiBitPerPixel, uiEndianness, 1, true ) ) { - m_cCmdLineHandler.log( LOG_ERROR, "Cannot open input stream %s! ", inputFileNames[i].c_str() ); + log( LOG_ERROR, "Cannot open input stream %s! ", inputFileNames[i].c_str() ); return 2; } m_apcInputStreams.push_back( pcStream ); } catch( const char *msg ) { - m_cCmdLineHandler.log( LOG_ERROR, "Cannot open input stream %s with the following error: \n%s\n", inputFileNames[i].c_str(), msg ); + log( LOG_ERROR, "Cannot open input stream %s with the following error: \n%s\n", inputFileNames[i].c_str(), msg ); return 2; } } } m_uiNumberOfFrames = MAX_UINT; - if( m_cCmdLineHandler.Opts().hasOpt( "frames" ) ) + if( Opts().hasOpt( "frames" ) ) { - m_uiNumberOfFrames = m_cCmdLineHandler.m_iFrames; + m_uiNumberOfFrames = m_iFrames; } m_uiNumberOfComponents = MAX_UINT; @@ -135,7 +135,7 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) { Int iRet = 0; - if( ( iRet = m_cCmdLineHandler.parseToolsArgs( argc, argv ) ) > 0 ) + if( ( iRet = parseToolsArgs( argc, argv ) ) > 0 ) { return iRet; } @@ -145,11 +145,11 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) return 2; } - if( m_cCmdLineHandler.Opts().hasOpt( "save" ) ) + if( Opts().hasOpt( "save" ) ) { if( m_apcInputStreams.size() == 0 ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid number of input streams! " ); + log( LOG_ERROR, "Invalid number of input streams! " ); return 2; } Int64 currFrames = 0; @@ -160,34 +160,34 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) if( currFrames < numberOfFrames ) numberOfFrames = currFrames; } - m_iFrameNum = m_cCmdLineHandler.m_iFrames; + m_iFrameNum = m_iFrames; if( !( m_iFrameNum >= 0 && m_iFrameNum < numberOfFrames ) ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid frame number! Use --frame option " ); + log( LOG_ERROR, "Invalid frame number! Use --frame option " ); return 2; } - if( m_cCmdLineHandler.Opts().hasOpt( "output" ) ) - m_pcOutputFileNames.push_back( m_cCmdLineHandler.m_strOutput ); + if( Opts().hasOpt( "output" ) ) + m_pcOutputFileNames.push_back( m_strOutput ); if( m_pcOutputFileNames.size() != m_apcInputStreams.size() ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid number of outputs! Each input must have an output filename. " ); + log( LOG_ERROR, "Invalid number of outputs! Each input must have an output filename. " ); return 2; } m_uiOperation = SAVE_OPERATION; m_fpProcess = &PlaYUVerTools::SaveOperation; - m_cCmdLineHandler.log( LOG_INFO, "PlaYUVer Save Frame\n" ); + log( LOG_INFO, "PlaYUVer Save Frame\n" ); } /** * Check Quality operation */ - if( m_cCmdLineHandler.Opts().hasOpt( "quality" ) ) + if( Opts().hasOpt( "quality" ) ) { - String qualityMetric = m_cCmdLineHandler.m_strQualityMetric; + String qualityMetric = m_strQualityMetric; if( m_apcInputStreams.size() < 2 ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid number of inputs! " ); + log( LOG_ERROR, "Invalid number of inputs! " ); return 2; } for( UInt i = 0; i < PlaYUVerFrame::supportedQualityMetricsList().size(); i++ ) @@ -199,20 +199,20 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) } if( m_uiQualityMetric == -1 ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid quality metric! " ); + log( LOG_ERROR, "Invalid quality metric! " ); return 2; } m_uiOperation = QUALITY_OPERATION; m_fpProcess = &PlaYUVerTools::QualityOperation; - m_cCmdLineHandler.log( LOG_INFO, "PlaYUVer Quality\n" ); + log( LOG_INFO, "PlaYUVer Quality\n" ); } /** * Check Module operation */ - if( m_cCmdLineHandler.Opts().hasOpt( "module" ) ) + if( Opts().hasOpt( "module" ) ) { - String moduleName = m_cCmdLineHandler.m_strModule; + String moduleName = m_strModule; PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); @@ -226,17 +226,17 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) } if( !m_pcCurrModuleIf ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid module! " ); + log( LOG_ERROR, "Invalid module! " ); return 2; } if( m_apcInputStreams.size() != m_pcCurrModuleIf->m_uiNumberOfFrames ) { - m_cCmdLineHandler.log( LOG_ERROR, "Invalid number of inputs! " ); + log( LOG_ERROR, "Invalid number of inputs! " ); return 2; } - m_pcCurrModuleIf->m_cModuleOptions.scanArgv( argc, ( const Char** )argv ); + m_pcCurrModuleIf->m_cModuleOptions.parse( argc, argv ); // Create Module Bool moduleCreated = false; @@ -256,7 +256,7 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) } if( !moduleCreated ) { - m_cCmdLineHandler.log( LOG_ERROR, "Module is not supported with the selected inputs! " ); + log( LOG_ERROR, "Module is not supported with the selected inputs! " ); return 2; } @@ -264,8 +264,8 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) { // Check outputs std::vector outputFileNames; - if( m_cCmdLineHandler.Opts().hasOpt( "output" ) ) - outputFileNames.push_back( m_cCmdLineHandler.m_strOutput ); + if( Opts().hasOpt( "output" ) ) + outputFileNames.push_back( m_strOutput ); if( outputFileNames.size() == 1 ) { @@ -279,7 +279,7 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) pcModFrame = applyFrameModule(); } PlaYUVerStream* pcModStream = new PlaYUVerStream; - if( !pcModStream->open( outputFileNames[0], pcModFrame->getWidth(), pcModFrame->getHeight(), pcModFrame->getPelFormat(), pcModFrame->getBitsPel(), 1, false ) ) + if( !pcModStream->open( outputFileNames[0], pcModFrame->getWidth(), pcModFrame->getHeight(), pcModFrame->getPelFormat(), pcModFrame->getBitsPel(), pcModFrame->getEndianness(), 1, false ) ) { delete pcModStream; pcModStream = NULL; @@ -289,19 +289,19 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] ) } else { - m_cCmdLineHandler.log( LOG_ERROR, "One output is required! " ); + log( LOG_ERROR, "One output is required! " ); return 2; } } m_uiOperation = MODULE_OPERATION; m_fpProcess = &PlaYUVerTools::ModuleOperation; - m_cCmdLineHandler.log( LOG_INFO, "PlaYUVer Module\n" ); + log( LOG_INFO, "PlaYUVer Module\n" ); } if( m_uiOperation == INVALID_OPERATION ) { - m_cCmdLineHandler.log( LOG_ERROR, "No operation was selected! " ); + log( LOG_ERROR, "No operation was selected! " ); return 2; } return iRet; @@ -361,19 +361,19 @@ Int PlaYUVerTools::QualityOperation() } metric_fmt += " "; - m_cCmdLineHandler.log( LOG_INFO, " Measuring Quality using %s ... \n", pchQualityMetricName ); - m_cCmdLineHandler.log( LOG_INFO, "# Frame ", pchQualityMetricName ); + log( LOG_INFO, " Measuring Quality using %s ... \n", pchQualityMetricName ); + log( LOG_INFO, "# Frame ", pchQualityMetricName ); for( UInt s = 1; s < m_apcInputStreams.size(); s++ ) { for( UInt c = 0; c < m_uiNumberOfComponents; c++ ) { - m_cCmdLineHandler.log( LOG_INFO, "%s_%d_%d ", pchQualityMetricName, s, c ); + log( LOG_INFO, "%s_%d_%d ", pchQualityMetricName, s, c ); } - m_cCmdLineHandler.log( LOG_INFO, " " ); + log( LOG_INFO, " " ); } - m_cCmdLineHandler.log( LOG_INFO, "\n" ); + log( LOG_INFO, "\n" ); for( UInt s = 0; s < m_apcInputStreams.size(); s++ ) { @@ -385,22 +385,22 @@ Int PlaYUVerTools::QualityOperation() } for( UInt frame = 0; frame < m_uiNumberOfFrames; frame++ ) { - m_cCmdLineHandler.log( LOG_INFO, " %3d ", frame ); + log( LOG_INFO, " %3d ", frame ); for( UInt s = 0; s < m_apcInputStreams.size(); s++ ) apcCurrFrame[s] = m_apcInputStreams[s]->getCurrFrame(); for( UInt s = 1; s < m_apcInputStreams.size(); s++ ) { - m_cCmdLineHandler.log( LOG_RESULT, " " ); + log( LOG_RESULT, " " ); for( UInt c = 0; c < m_uiNumberOfComponents; c++ ) { dQuality = apcCurrFrame[s]->getQuality( m_uiQualityMetric, apcCurrFrame[0], c ); adAverageQuality[s - 1][c] = ( adAverageQuality[s - 1][c] * Double( frame ) + dQuality ) / Double( frame + 1 ); - m_cCmdLineHandler.log( LOG_RESULT, metric_fmt.c_str(), dQuality ); + log( LOG_RESULT, metric_fmt.c_str(), dQuality ); } - m_cCmdLineHandler.log( LOG_RESULT, " " ); + log( LOG_RESULT, " " ); } - m_cCmdLineHandler.log( LOG_RESULT, "\n" ); + log( LOG_RESULT, "\n" ); for( UInt s = 0; s < m_apcInputStreams.size(); s++ ) { abEOF[s] = m_apcInputStreams[s]->setNextFrame(); @@ -410,16 +410,16 @@ Int PlaYUVerTools::QualityOperation() } } } - m_cCmdLineHandler.log( LOG_INFO, "\n Mean Values: \n " ); + log( LOG_INFO, "\n Mean Values: \n " ); for( UInt s = 0; s < m_apcInputStreams.size() - 1; s++ ) { for( UInt c = 0; c < m_uiNumberOfComponents; c++ ) { - m_cCmdLineHandler.log( LOG_INFO, metric_fmt.c_str(), adAverageQuality[s][c] ); + log( LOG_INFO, metric_fmt.c_str(), adAverageQuality[s][c] ); } - m_cCmdLineHandler.log( LOG_RESULT, " " ); + log( LOG_RESULT, " " ); } - m_cCmdLineHandler.log( LOG_INFO, "\n" ); + log( LOG_INFO, "\n" ); return 0; } @@ -436,7 +436,7 @@ PlaYUVerFrame* PlaYUVerTools::applyFrameModule() Int PlaYUVerTools::ModuleOperation() { std::vector apcFrameList; - m_cCmdLineHandler.log( LOG_INFO, " Applying Module %s/%s ...\n", m_pcCurrModuleIf->m_pchModuleCategory, m_pcCurrModuleIf->m_pchModuleName ); + log( LOG_INFO, " Applying Module %s/%s ...\n", m_pcCurrModuleIf->m_pchModuleCategory, m_pcCurrModuleIf->m_pchModuleName ); PlaYUVerFrame* pcProcessedFrame = NULL; Double dMeasurementResult = 0.0; @@ -477,8 +477,8 @@ Int PlaYUVerTools::ModuleOperation() { dMeasurementResult = m_pcCurrModuleIf->measure( m_apcInputStreams[0]->getCurrFrame() ); } - m_cCmdLineHandler.log( LOG_INFO, " %3d", frame ); - m_cCmdLineHandler.log( LOG_RESULT, " %8.3f \n", dMeasurementResult ); + log( LOG_INFO, " %3d", frame ); + log( LOG_RESULT, " %8.3f \n", dMeasurementResult ); dAveragedMeasurementResult = ( dAveragedMeasurementResult * Double( frame ) + dMeasurementResult ) / Double( frame + 1 ); } @@ -494,7 +494,7 @@ Int PlaYUVerTools::ModuleOperation() if( m_pcCurrModuleIf->m_iModuleType == FRAME_MEASUREMENT_MODULE ) { - m_cCmdLineHandler.log( LOG_INFO, "\n Mean Value: \n %8.3f\n", dAveragedMeasurementResult ); + log( LOG_INFO, "\n Mean Value: \n %8.3f\n", dAveragedMeasurementResult ); } return 0; diff --git a/tools/PlaYUVerTools.h b/tools/PlaYUVerTools.h index baf0fcd..1b8634f 100644 --- a/tools/PlaYUVerTools.h +++ b/tools/PlaYUVerTools.h @@ -36,7 +36,7 @@ class PlaYUVerModuleIf; #define MAX_NUMBER_INPUTS 10 -class PlaYUVerTools +class PlaYUVerTools: public PlaYUVerToolsCmdParser { public: PlaYUVerTools(); @@ -47,7 +47,6 @@ public: Int Close(); private: - PlaYUVerToolsCmdParser m_cCmdLineHandler; Bool m_bVerbose; UInt m_uiOperation; diff --git a/tools/PlaYUVerToolsCmdParser.cpp b/tools/PlaYUVerToolsCmdParser.cpp index 1d5c1c1..012f492 100644 --- a/tools/PlaYUVerToolsCmdParser.cpp +++ b/tools/PlaYUVerToolsCmdParser.cpp @@ -36,6 +36,8 @@ PlaYUVerToolsCmdParser::PlaYUVerToolsCmdParser() m_uiLogLevel = 0; m_bQuiet = false; m_iFrames = -1; + + m_cOptions.addDefaultOptions(); } PlaYUVerToolsCmdParser::~PlaYUVerToolsCmdParser() @@ -92,12 +94,6 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char *argv[] ) m_cOptions.doHelp( std::cout ); iRet = 1; } - - if( m_cOptions.hasOpt("module_list") || m_cOptions.hasOpt("module_list_full") ) - { - m_cOptions.listModules(); - iRet = 1; - } return iRet; } diff --git a/tools/PlaYUVerToolsCmdParser.h b/tools/PlaYUVerToolsCmdParser.h index 0796fd2..6aa4e12 100644 --- a/tools/PlaYUVerToolsCmdParser.h +++ b/tools/PlaYUVerToolsCmdParser.h @@ -34,7 +34,6 @@ class PlaYUVerStream; class PlaYUVerToolsCmdParser { - friend class PlaYUVerTools; public: PlaYUVerToolsCmdParser(); ~PlaYUVerToolsCmdParser(); @@ -65,7 +64,7 @@ public: return m_cOptions; } -private: +protected: PlaYUVerOptions m_cOptions; UInt m_uiLogLevel; diff --git a/tools/main.cpp b/tools/main.cpp index 6a4088e..97fbaf7 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -25,7 +25,7 @@ #include "PlaYUVerTools.h" -int main( int argc, char *argv[] ) +int main( int argc, Char *argv[] ) { Int iRet = 0; PlaYUVerTools PlaYUVerToolsApp;