From 9c2e6fdbb85b8d7d144055dd02d5a1056dc568e8 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Fri, 17 Mar 2017 15:27:47 +0000 Subject: [PATCH] Add rate reduction operation to PlaYUVerTools --- tools/PlaYUVerTools.cpp | 74 ++++++++++++++++++++++++++++++++ tools/PlaYUVerTools.h | 3 ++ tools/PlaYUVerToolsCmdParser.cpp | 3 +- tools/PlaYUVerToolsCmdParser.h | 1 + 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/tools/PlaYUVerTools.cpp b/tools/PlaYUVerTools.cpp index 844c827..d122cce 100644 --- a/tools/PlaYUVerTools.cpp +++ b/tools/PlaYUVerTools.cpp @@ -183,6 +183,59 @@ Int PlaYUVerTools::Open( Int argc, Char* argv[] ) log( LOG_INFO, "PlaYUVer Save Frame\n" ); } + if( Opts().hasOpt( "rate-reduction" ) ) + { + if( m_apcInputStreams.size() == 0 ) + { + log( LOG_ERROR, "Invalid number of input streams! " ); + return 2; + } + Int64 currFrames = 0; + Int64 numberOfFrames = LONG_MAX; + for( UInt i = 0; i < m_apcInputStreams.size(); i++ ) + { + currFrames = m_apcInputStreams[i]->getFrameNum(); + if( currFrames < numberOfFrames ) + numberOfFrames = currFrames; + } + if( m_iRateReductionFactor <= 0 ) + { + log( LOG_ERROR, "Invalid frame rate reduction value!" ); + return 2; + } + + if( Opts().hasOpt( "output" ) ) + m_pcOutputFileNames.push_back( m_strOutput ); + + PlaYUVerFrame* pcInputFrame = m_apcInputStreams[0]->getCurrFrame(); + PlaYUVerStream* pcOutputStream = new PlaYUVerStream; + try + { + pcOutputStream->open( m_pcOutputFileNames[0], pcInputFrame->getWidth(), pcInputFrame->getHeight(), + pcInputFrame->getPelFormat(), pcInputFrame->getBitsPel(), PLAYUVER_LITTLE_ENDIAN, 1, + false ); + } + catch( const char* msg ) + { + log( LOG_ERROR, "Cannot open input stream %s with the following error %s!\n", m_pcOutputFileNames[0].c_str(), + msg ); + delete pcOutputStream; + pcOutputStream = NULL; + return 2; + } + m_apcOutputStreams.push_back( pcOutputStream ); + + if( m_apcOutputStreams.size() != m_apcInputStreams.size() ) + { + log( LOG_ERROR, "Invalid number of outputs! Each input must have an output filename. " ); + return 2; + } + + m_uiOperation = RATE_REDUCTION_OPERATION; + m_fpProcess = &PlaYUVerTools::RateReductionOperation; + log( LOG_INFO, "PlaYUVer Frame Rate Reduction\n" ); + } + /** * Check Quality operation */ @@ -344,6 +397,27 @@ Int PlaYUVerTools::SaveOperation() return 0; } +Int PlaYUVerTools::RateReductionOperation() +{ + Bool abEOF; + log( LOG_INFO, "\n Reducing frame rate by a factor of %d ... ", m_iRateReductionFactor ); + for( UInt frame = 0; frame < m_uiNumberOfFrames; frame++ ) + { + log( LOG_INFO, "\n Reading frame %d ... ", frame ); + if( ( frame % m_iRateReductionFactor ) == 0 ) + { + log( LOG_INFO, "Writing", frame ); + m_apcOutputStreams[0]->writeFrame( m_apcInputStreams[0]->getCurrFrame() ); + } + abEOF = m_apcInputStreams[0]->setNextFrame(); + if( !abEOF ) + { + m_apcInputStreams[0]->readFrame(); + } + } + return 0; +} + Int PlaYUVerTools::QualityOperation() { const char* pchQualityMetricName = PlaYUVerFrame::supportedQualityMetricsList()[m_uiQualityMetric].c_str(); diff --git a/tools/PlaYUVerTools.h b/tools/PlaYUVerTools.h index 0e39134..8bdb859 100644 --- a/tools/PlaYUVerTools.h +++ b/tools/PlaYUVerTools.h @@ -54,6 +54,7 @@ class PlaYUVerTools : public PlaYUVerToolsCmdParser { INVALID_OPERATION, SAVE_OPERATION, + RATE_REDUCTION_OPERATION, QUALITY_OPERATION, MODULE_OPERATION, }; @@ -72,6 +73,8 @@ class PlaYUVerTools : public PlaYUVerToolsCmdParser std::vector m_pcOutputFileNames; Int SaveOperation(); + Int RateReductionOperation(); + Int m_uiQualityMetric; Int QualityOperation(); diff --git a/tools/PlaYUVerToolsCmdParser.cpp b/tools/PlaYUVerToolsCmdParser.cpp index 5e3d59f..a893787 100644 --- a/tools/PlaYUVerToolsCmdParser.cpp +++ b/tools/PlaYUVerToolsCmdParser.cpp @@ -70,7 +70,8 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char* argv[] ) ( "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" ); + ( "save", "save a specific frame" ) /**/ + ( "rate-reduction", m_iRateReductionFactor, "reduce the frame rate" ); /**/ if( !m_cOptions.parse( argc, argv ) ) { diff --git a/tools/PlaYUVerToolsCmdParser.h b/tools/PlaYUVerToolsCmdParser.h index 9b48180..1b60e16 100644 --- a/tools/PlaYUVerToolsCmdParser.h +++ b/tools/PlaYUVerToolsCmdParser.h @@ -72,6 +72,7 @@ class PlaYUVerToolsCmdParser String m_strOutput; Int64 m_iFrames; + Int m_iRateReductionFactor; String m_strQualityMetric; String m_strModule;