From de18797fad0dc449b49afccc2c57e460491d9697 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 10:46:40 +0200 Subject: [PATCH 1/7] StreamTranscoder: add offset at the beginning of the stream * Transcoder: can specified an offset when add a StreamTranscoder. * StreamTranscoder: can set the offset. * Add documenation about the offset. --- .../Transcoder/StreamTranscoder.cpp | 61 ++++++++++++++----- .../Transcoder/StreamTranscoder.hpp | 19 +++++- src/AvTranscoder/Transcoder/Transcoder.cpp | 44 ++++++------- src/AvTranscoder/Transcoder/Transcoder.hpp | 20 +++--- 4 files changed, 96 insertions(+), 48 deletions(-) diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp index 12e8a04b..4c2f2bca 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp @@ -32,9 +32,13 @@ StreamTranscoder::StreamTranscoder( , _outputEssence( NULL ) , _transform( NULL ) , _subStreamIndex( -1 ) + , _frameProcessed( 0 ) + , _offset( 0 ) , _transcodeStream( false ) + , _takeFromDummy( false ) , _infiniteProcess( false ) , _verbose( false ) + , _offsetPassed( false ) { // create a re-wrapping case switch( _inputStream->getStreamType() ) @@ -58,7 +62,8 @@ StreamTranscoder::StreamTranscoder( InputStream& inputStream, OutputFile& outputFile, const Profile::ProfileDesc& profile, - const int subStreamIndex + const int subStreamIndex, + const size_t offset ) : _inputStream( &inputStream ) , _outputStream( NULL ) @@ -69,9 +74,13 @@ StreamTranscoder::StreamTranscoder( , _outputEssence( NULL ) , _transform( NULL ) , _subStreamIndex( subStreamIndex ) + , _frameProcessed( 0 ) + , _offset( offset ) , _transcodeStream( true ) + , _takeFromDummy( false ) , _infiniteProcess( false ) , _verbose( false ) + , _offsetPassed( false ) { // create a transcode case switch( _inputStream->getStreamType() ) @@ -99,8 +108,6 @@ StreamTranscoder::StreamTranscoder( DummyVideo* dummyVideo = new DummyVideo(); dummyVideo->setVideoDesc( outputVideo->getVideoDesc() ); _dummyEssence = dummyVideo; - - _currentEssence = _inputEssence; break; } @@ -137,8 +144,6 @@ StreamTranscoder::StreamTranscoder( dummyAudio->setAudioDesc( outputAudio->getAudioDesc() ); _dummyEssence = dummyAudio; - _currentEssence = _inputEssence; - break; } default: @@ -147,6 +152,8 @@ StreamTranscoder::StreamTranscoder( break; } } + + switchEssence( offset != 0 ); } StreamTranscoder::StreamTranscoder( @@ -163,9 +170,13 @@ StreamTranscoder::StreamTranscoder( , _outputEssence( NULL ) , _transform( NULL ) , _subStreamIndex( -1 ) + , _frameProcessed( 0 ) + , _offset( 0 ) , _transcodeStream( true ) + , _takeFromDummy( false ) , _infiniteProcess( false ) , _verbose( false ) + , _offsetPassed( false ) { // create a coding case based on a InputEssence (aka dummy reader) if( ! profile.count( Profile::avProfileType ) ) @@ -237,6 +248,7 @@ StreamTranscoder::~StreamTranscoder() bool StreamTranscoder::processFrame() { + ++_frameProcessed; if( _transcodeStream ) { if( _subStreamIndex < 0 ) @@ -297,6 +309,16 @@ bool StreamTranscoder::processTranscode() DataStream dataStream; if( _verbose ) std::cout << "transcode a frame " << std::endl; + + if( _offset && + _frameProcessed > _offset && + ! _offsetPassed && + _takeFromDummy ) + { + switchToInputEssence(); + _offsetPassed = true; + } + if( _currentEssence->readNextFrame( *_sourceBuffer ) ) { if( _verbose ) @@ -340,6 +362,16 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex ) DataStream dataStream; if( _verbose ) std::cout << "transcode a frame " << std::endl; + + if( _offset && + _frameProcessed > _offset && + ! _offsetPassed && + _takeFromDummy ) + { + switchToInputEssence(); + _offsetPassed = true; + } + if( _currentEssence->readNextFrame( *_sourceBuffer, subStreamIndex ) ) { if( _verbose ) @@ -368,22 +400,21 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex ) return true; } -void StreamTranscoder::switchToDummyEssence() +void StreamTranscoder::switchEssence( bool swithToDummy ) { - if( _dummyEssence == NULL ) - return; - _takeFromDummy = true; - _currentEssence = _dummyEssence; + _takeFromDummy = swithToDummy; + _currentEssence = swithToDummy ? _dummyEssence : _inputEssence; assert( _currentEssence != NULL ); } +void StreamTranscoder::switchToDummyEssence() +{ + switchEssence( true ); +} + void StreamTranscoder::switchToInputEssence() { - if( _inputEssence == NULL ) - return; - _takeFromDummy = false; - _currentEssence = _inputEssence; - assert( _currentEssence != NULL ); + switchEssence( false ); } } diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp index 290a68f4..4768a010 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp @@ -22,16 +22,18 @@ class StreamTranscoder public: /** * @brief rewrap stream + * @note offset feature when rewrap a stream is not supported **/ StreamTranscoder( InputStream& inputStream, OutputFile& outputFile ); /** * @brief transcode stream **/ - StreamTranscoder( InputStream& inputStream, OutputFile& outputFile, const Profile::ProfileDesc& profile, const int subStreamIndex = -1 ); + StreamTranscoder( InputStream& inputStream, OutputFile& outputFile, const Profile::ProfileDesc& profile, const int subStreamIndex = -1, const size_t offset = 0 ); /** * @brief encode from dummy stream + * @note offset feature has no sense here **/ StreamTranscoder( InputEssence& inputEssence, OutputFile& outputFile, const Profile::ProfileDesc& profile ); @@ -47,10 +49,13 @@ class StreamTranscoder void setVerbose( bool verbose = true ){ _verbose = verbose; } + void switchEssence( bool swithToDummy = true ); void switchToDummyEssence(); void switchToInputEssence(); void setInfinityProcess( bool infinity = true ){ _infiniteProcess = infinity; } + void setOffset( bool offset = true ){ _offset = offset; } + private: bool processRewrap(); bool processRewrap( const int subStreamIndex ); @@ -72,11 +77,23 @@ class StreamTranscoder EssenceTransform* _transform; int _subStreamIndex; + + /** + * @brief How many frame processed for this StreamTranscoder. + */ + size_t _frameProcessed; + /** + * @brief Offset, in frame, at the beginning of the StreamTranscoder. + */ + size_t _offset; + bool _transcodeStream; bool _takeFromDummy; bool _infiniteProcess; bool _verbose; + + bool _offsetPassed; }; } diff --git a/src/AvTranscoder/Transcoder/Transcoder.cpp b/src/AvTranscoder/Transcoder/Transcoder.cpp index 26bbf862..c7cb2a17 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.cpp +++ b/src/AvTranscoder/Transcoder/Transcoder.cpp @@ -40,7 +40,7 @@ Transcoder::~Transcoder() } } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, const size_t offset ) { if( profileName.length() == 0 ) // no profile, only re-wrap stream { @@ -51,10 +51,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con } Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName ); - add( filename, streamIndex, transcodeProfile ); + add( filename, streamIndex, transcodeProfile, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset ) { if( profileName.length() == 0 ) // no profile, only re-wrap stream { @@ -72,10 +72,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con } Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName ); - add( filename, streamIndex, transcodeProfile, essenceDesc ); + add( filename, streamIndex, transcodeProfile, essenceDesc, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, const size_t offset ) { _profile.update( profileDesc ); if( ! filename.length() ) @@ -87,10 +87,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro if( _verbose ) std::cout << "add transcoding stream" << std::endl; - addTranscodeStream( filename, streamIndex, profileDesc ); + addTranscodeStream( filename, streamIndex, profileDesc, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset ) { _profile.update( profileDesc ); if( ! filename.length() ) @@ -103,14 +103,14 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro if( _verbose ) std::cout << "add transcoding stream" << std::endl; - addTranscodeStream( filename, streamIndex, profileDesc ); + addTranscodeStream( filename, streamIndex, profileDesc, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, const size_t offset ) { if( subStreamIndex < 0 ) { - add( filename, streamIndex, profileName ); + add( filename, streamIndex, profileName, offset ); return; } @@ -124,10 +124,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con } Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName ); - add( filename, streamIndex, subStreamIndex, transcodeProfile ); + add( filename, streamIndex, subStreamIndex, transcodeProfile, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset ) { if( subStreamIndex < 0 ) { @@ -151,16 +151,16 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con } Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName ); - add( filename, streamIndex, subStreamIndex, transcodeProfile, essenceDesc ); + add( filename, streamIndex, subStreamIndex, transcodeProfile, essenceDesc, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, const size_t offset ) { _profile.update( profileDesc ); if( subStreamIndex < 0 ) { - add( filename, streamIndex, profileDesc ); + add( filename, streamIndex, profileDesc, offset ); return; } @@ -173,10 +173,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con if( _verbose ) std::cout << "add transcoding stream for substream " << subStreamIndex << std::endl; - addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc ); + addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc, offset ); } -void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc ) +void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset ) { _profile.update( profileDesc ); @@ -196,7 +196,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con if( _verbose ) std::cout << "add transcoding stream for substream " << subStreamIndex << std::endl; - addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc ); + addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc, offset ); } void Transcoder::add( StreamTranscoder& stream ) @@ -344,7 +344,7 @@ void Transcoder::addRewrapStream( const std::string& filename, const size_t stre _inputStreams.push_back( &referenceFile->getStream( streamIndex ) ); } -void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile ) +void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile, const size_t offset ) { InputFile* referenceFile = addInputFile( filename, streamIndex ); @@ -353,7 +353,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s case AVMEDIA_TYPE_VIDEO: case AVMEDIA_TYPE_AUDIO: { - _streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile ) ); + _streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, -1 , offset ) ); _inputStreams.push_back( &referenceFile->getStream( streamIndex ) ); break; } @@ -367,7 +367,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s } } -void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile ) +void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile, const size_t offset ) { InputFile* referenceFile = addInputFile( filename, streamIndex ); @@ -376,7 +376,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s case AVMEDIA_TYPE_VIDEO: case AVMEDIA_TYPE_AUDIO: { - _streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, subStreamIndex ) ); + _streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, subStreamIndex, offset ) ); _inputStreams.push_back( &referenceFile->getStream( streamIndex ) ); break; } diff --git a/src/AvTranscoder/Transcoder/Transcoder.hpp b/src/AvTranscoder/Transcoder/Transcoder.hpp index d7f8fa39..21495a00 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.hpp +++ b/src/AvTranscoder/Transcoder/Transcoder.hpp @@ -48,45 +48,45 @@ class Transcoder * @brief Add a stream and set a profile * @note If profileName is empty, rewrap. */ - void add( const std::string& filename, const size_t streamIndex, const std::string& profileName = "" ); + void add( const std::string& filename, const size_t streamIndex, const std::string& profileName = "", const size_t offset = 0 ); /* * @note If filename is empty, add a dummy stream. * @note If filename is empty, profileName can't be empty (no sens to rewrap a dummy stream). */ - void add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc ); + void add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset = 0 ); /** * @brief Add a stream and set a custom profile * @note Profile will be updated, be sure to pass unique profile name. */ - void add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc ); + void add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, const size_t offset = 0 ); /* * @note If filename is empty, add a dummy stream. */ - void add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc ); + void add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset = 0 ); /** * @brief Add a stream and set a profile * @note If profileName is empty, rewrap. * @note If subStreamIndex is negative, no substream is selected it's the stream. */ - void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName = "" ); + void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName = "", const size_t offset = 0 ); /** * @note If filename is empty, add a dummy stream. * @note If filename is empty, profileName can't be empty (no sens to rewrap a dummy stream). */ - void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc ); + void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset = 0 ); /** * @brief Add a stream and set a custom profile * @note Profile will be updated, be sure to pass unique profile name. * @note If subStreamIndex is negative, no substream is selected it's the stream. */ - void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc ); + void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, const size_t offset = 0 ); /** * @note If filename is empty, add a dummy stream. */ - void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc ); + void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset = 0 ); /** * @brief Add the stream @@ -106,9 +106,9 @@ class Transcoder void addRewrapStream( const std::string& filename, const size_t streamIndex ); - void addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile ); + void addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile, const size_t offset = 0 ); - void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile ); + void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile, const size_t offset = 0 ); void addDummyStream( const Profile::ProfileDesc& profile, const CodedDesc& essenceDesc ); From 19c9f5d392150bcf8a5c05125a966d2d9505dbcb Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 11:11:54 +0200 Subject: [PATCH 2/7] Transcoder: add setOutputFps --- src/AvTranscoder/Transcoder/Transcoder.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AvTranscoder/Transcoder/Transcoder.hpp b/src/AvTranscoder/Transcoder/Transcoder.hpp index 21495a00..065e5f9b 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.hpp +++ b/src/AvTranscoder/Transcoder/Transcoder.hpp @@ -102,6 +102,8 @@ class Transcoder void setVerbose( bool verbose = true ); + void setOutputFps( double fps ) { _outputFps = fps; } + private: void addRewrapStream( const std::string& filename, const size_t streamIndex ); From 98dc0b0c65d6dcb5df2148dbb889d6dbe5c475f6 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 11:28:14 +0200 Subject: [PATCH 3/7] StreamTranscoder: clean - remove _infiniteProcess All the politic of transcode is in the Transcoder. --- src/AvTranscoder/Transcoder/StreamTranscoder.cpp | 15 --------------- src/AvTranscoder/Transcoder/StreamTranscoder.hpp | 2 -- src/AvTranscoder/Transcoder/Transcoder.cpp | 4 ---- 3 files changed, 21 deletions(-) diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp index 4c2f2bca..11fedf2d 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp @@ -36,7 +36,6 @@ StreamTranscoder::StreamTranscoder( , _offset( 0 ) , _transcodeStream( false ) , _takeFromDummy( false ) - , _infiniteProcess( false ) , _verbose( false ) , _offsetPassed( false ) { @@ -78,7 +77,6 @@ StreamTranscoder::StreamTranscoder( , _offset( offset ) , _transcodeStream( true ) , _takeFromDummy( false ) - , _infiniteProcess( false ) , _verbose( false ) , _offsetPassed( false ) { @@ -174,7 +172,6 @@ StreamTranscoder::StreamTranscoder( , _offset( 0 ) , _transcodeStream( true ) , _takeFromDummy( false ) - , _infiniteProcess( false ) , _verbose( false ) , _offsetPassed( false ) { @@ -330,12 +327,6 @@ bool StreamTranscoder::processTranscode() } else { - if( _infiniteProcess ) - { - switchToDummyEssence(); - return processTranscode( ); - } - if( _verbose ) std::cout << "encode last frame(s)" << std::endl; if( ! _outputEssence->encodeFrame( dataStream ) ) @@ -383,12 +374,6 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex ) } else { - if( _infiniteProcess ) - { - switchToDummyEssence(); - return processTranscode( ); - } - if( ! _outputEssence->encodeFrame( dataStream ) ) { return false; diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp index 4768a010..7c61356e 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp @@ -52,7 +52,6 @@ class StreamTranscoder void switchEssence( bool swithToDummy = true ); void switchToDummyEssence(); void switchToInputEssence(); - void setInfinityProcess( bool infinity = true ){ _infiniteProcess = infinity; } void setOffset( bool offset = true ){ _offset = offset; } @@ -89,7 +88,6 @@ class StreamTranscoder bool _transcodeStream; bool _takeFromDummy; - bool _infiniteProcess; bool _verbose; diff --git a/src/AvTranscoder/Transcoder/Transcoder.cpp b/src/AvTranscoder/Transcoder/Transcoder.cpp index c7cb2a17..3659820e 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.cpp +++ b/src/AvTranscoder/Transcoder/Transcoder.cpp @@ -321,10 +321,6 @@ void Transcoder::process( ProgressListener& progress ) void Transcoder::setProcessMethod( const EProcessMethod eProcessMethod ) { _eProcessMethod = eProcessMethod; - for( std::vector< StreamTranscoder* >::iterator it = _streamTranscoders.begin(); it != _streamTranscoders.end(); ++it ) - { - (*it)->setInfinityProcess( eProcessMethod == eProcessMethodInfinity ); - } } void Transcoder::setVerbose( bool verbose ) From 4cffcb4ce453dd14a8434ae9bae6a6e6e1c1e765 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 13:56:39 +0200 Subject: [PATCH 4/7] Transcoder: refactoring - switching to dummy into StreamTranscoder * Switch to a dummy or not, is the responsability of the StreamTranscoder. * When set the transcode politic of the Transcoder, prepare each stream to switch to dummy or note if necessary. * fix #46 --- app/genericProcessor/genericProcessor.cpp | 7 +- .../Transcoder/StreamTranscoder.cpp | 28 +++++++ .../Transcoder/StreamTranscoder.hpp | 13 ++++ src/AvTranscoder/Transcoder/Transcoder.cpp | 76 ++++++++++++------- src/AvTranscoder/Transcoder/Transcoder.hpp | 11 +++ 5 files changed, 104 insertions(+), 31 deletions(-) diff --git a/app/genericProcessor/genericProcessor.cpp b/app/genericProcessor/genericProcessor.cpp index da67d7a8..c9858b76 100644 --- a/app/genericProcessor/genericProcessor.cpp +++ b/app/genericProcessor/genericProcessor.cpp @@ -108,13 +108,16 @@ int main( int argc, char** argv ) avtranscoder::OutputFile outputFile( argv[2] ); avtranscoder::Transcoder transcoder( outputFile ); - transcoder.setVerbose( verbose ); - transcoder.setProcessMethod( avtranscoder::eProcessMethodShortest ); if( verbose ) std::cout << "parse config file" << std::endl; parseConfigFile( inputConfigFile, transcoder, profiles ); + // set verbose of all stream + transcoder.setVerbose( verbose ); + transcoder.setProcessMethod( avtranscoder::eProcessMethodInfinity ); + //transcoder.setOutputFps( 12 ); + if( verbose ) std::cout << "start Transcode" << std::endl; diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp index 11fedf2d..78b56dcd 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp @@ -15,6 +15,7 @@ #include #include +#include namespace avtranscoder { @@ -38,6 +39,7 @@ StreamTranscoder::StreamTranscoder( , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) + , _infinityStream( false ) { // create a re-wrapping case switch( _inputStream->getStreamType() ) @@ -79,6 +81,7 @@ StreamTranscoder::StreamTranscoder( , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) + , _infinityStream( false ) { // create a transcode case switch( _inputStream->getStreamType() ) @@ -174,6 +177,7 @@ StreamTranscoder::StreamTranscoder( , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) + , _infinityStream( false ) { // create a coding case based on a InputEssence (aka dummy reader) if( ! profile.count( Profile::avProfileType ) ) @@ -331,6 +335,11 @@ bool StreamTranscoder::processTranscode() std::cout << "encode last frame(s)" << std::endl; if( ! _outputEssence->encodeFrame( dataStream ) ) { + if( _infinityStream ) + { + switchToDummyEssence(); + return processTranscode(); + } return false; } } @@ -376,6 +385,11 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex ) { if( ! _outputEssence->encodeFrame( dataStream ) ) { + if( _infinityStream ) + { + switchToDummyEssence(); + return processTranscode(); + } return false; } } @@ -402,4 +416,18 @@ void StreamTranscoder::switchToInputEssence() switchEssence( false ); } +double StreamTranscoder::getDuration() const +{ + if( _inputStream ) + { + double totalDuration = 0; + totalDuration += _inputStream->getDuration(); + // @todo add offset + return totalDuration; + } + // dummy + else + return std::numeric_limits::max(); +} + } diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp index 7c61356e..adc65cab 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp @@ -53,8 +53,15 @@ class StreamTranscoder void switchToDummyEssence(); void switchToInputEssence(); + void setInfinityStream( bool isInfinity ) { _infinityStream = isInfinity; } void setOffset( bool offset = true ){ _offset = offset; } + /** + * @brief Get the duration of the stream. + * @note if it's a dummy stream, return limit of double. + */ + double getDuration() const; + private: bool processRewrap(); bool processRewrap( const int subStreamIndex ); @@ -92,6 +99,12 @@ class StreamTranscoder bool _verbose; bool _offsetPassed; + + /** + * @brief Automatic switch to dummy + * @note not applicable when rewrap + */ + bool _infinityStream; }; } diff --git a/src/AvTranscoder/Transcoder/Transcoder.cpp b/src/AvTranscoder/Transcoder/Transcoder.cpp index 3659820e..d259fe4d 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.cpp +++ b/src/AvTranscoder/Transcoder/Transcoder.cpp @@ -223,25 +223,7 @@ bool Transcoder::processFrame() if( streamProcessStatus ) continue; - switch( _eProcessMethod ) - { - case eProcessMethodShortest : - _streamTranscoders.clear(); - break; - case eProcessMethodLongest : - ++_finalisedStreams; - _streamTranscoders.at( streamIndex )->switchToDummyEssence(); - if( _verbose ) - std::cout << "-> switch to dummy for stream " << streamIndex << std::endl; - if( _finalisedStreams == _streamTranscoders.size() ) - _streamTranscoders.clear(); - break; - case eProcessMethodInfinity : - if( _verbose ) - std::cout << "-> infinity processing: switch to dummy for stream " << streamIndex << std::endl; - _streamTranscoders.at( streamIndex )->switchToDummyEssence(); - break; - } + _streamTranscoders.clear(); } return true; } @@ -273,21 +255,13 @@ void Transcoder::process( ProgressListener& progress ) _outputFile.beginWrap(); double totalDuration = std::numeric_limits::max(); - double minTotalDuration = std::numeric_limits::max(); - double maxTotalDuration = 0; - - for( size_t i = 0; i < _inputStreams.size(); ++i ) - { - minTotalDuration = std::min( _inputStreams.at( i )->getDuration(), minTotalDuration ); - maxTotalDuration = std::max( _inputStreams.at( i )->getDuration(), maxTotalDuration ); - } switch( _eProcessMethod ) { case eProcessMethodShortest : - totalDuration = minTotalDuration; + totalDuration = getMinTotalDuration(); break; case eProcessMethodLongest : - totalDuration = maxTotalDuration; + totalDuration = getMaxTotalDuration(); break; case eProcessMethodInfinity : totalDuration = std::numeric_limits::max(); @@ -321,6 +295,28 @@ void Transcoder::process( ProgressListener& progress ) void Transcoder::setProcessMethod( const EProcessMethod eProcessMethod ) { _eProcessMethod = eProcessMethod; + + for( size_t i = 0; i < _streamTranscoders.size(); ++i ) + { + switch( _eProcessMethod ) + { + case eProcessMethodShortest : + if( _streamTranscoders.at( i )->getDuration() == getMinTotalDuration() ) + _streamTranscoders.at( i )->setInfinityStream( false ); + else + _streamTranscoders.at( i )->setInfinityStream( true ); + break; + case eProcessMethodLongest : + if( _streamTranscoders.at( i )->getDuration() == getMaxTotalDuration() ) + _streamTranscoders.at( i )->setInfinityStream( false ); + else + _streamTranscoders.at( i )->setInfinityStream( true ); + break; + case eProcessMethodInfinity : + _streamTranscoders.at( i )->setInfinityStream( true ); + break; + } + } } void Transcoder::setVerbose( bool verbose ) @@ -439,4 +435,26 @@ InputFile* Transcoder::addInputFile( const std::string& filename, const size_t s return referenceFile; } +double Transcoder::getMinTotalDuration() const +{ + double minTotalDuration = std::numeric_limits::max(); + + for( size_t i = 0; i < _streamTranscoders.size(); ++i ) + { + minTotalDuration = std::min( _streamTranscoders.at( i )->getDuration(), minTotalDuration ); + } + return minTotalDuration; +} + +double Transcoder::getMaxTotalDuration() const +{ + double maxTotalDuration = 0; + + for( size_t i = 0; i < _streamTranscoders.size(); ++i ) + { + maxTotalDuration = std::max( _streamTranscoders.at( i )->getDuration(), maxTotalDuration ); + } + return maxTotalDuration; +} + } diff --git a/src/AvTranscoder/Transcoder/Transcoder.hpp b/src/AvTranscoder/Transcoder/Transcoder.hpp index 065e5f9b..4ce48c83 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.hpp +++ b/src/AvTranscoder/Transcoder/Transcoder.hpp @@ -116,6 +116,17 @@ class Transcoder InputFile* addInputFile( const std::string& filename, const size_t streamIndex ); + /** + * @brief Get the duration of the shortest stream. + * @note if there is only dummy, return limit of double. + */ + double getMinTotalDuration() const; + /** + * @brief Get the duration of the longest stream. + * @note if there is only dummy, return limit of double. + */ + double getMaxTotalDuration() const; + private: OutputFile& _outputFile; std::vector< InputFile* > _inputFiles; From 639768d9cf7fa96cafc9e691ea4fb9dcdeefb667 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 14:06:32 +0200 Subject: [PATCH 5/7] Transcoder & StreamTranscoder: clean * Remove unused bool _transcodeStream and function isTranscodeStream. * Suppress spaces. * Suppress comments. * Add verbose (symmetry between processTranscode functions). --- src/AvTranscoder/Transcoder/StreamTranscoder.cpp | 10 +++------- src/AvTranscoder/Transcoder/StreamTranscoder.hpp | 8 +++----- src/AvTranscoder/Transcoder/Transcoder.cpp | 1 - 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp index 78b56dcd..9f9d415e 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.cpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.cpp @@ -35,7 +35,6 @@ StreamTranscoder::StreamTranscoder( , _subStreamIndex( -1 ) , _frameProcessed( 0 ) , _offset( 0 ) - , _transcodeStream( false ) , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) @@ -77,7 +76,6 @@ StreamTranscoder::StreamTranscoder( , _subStreamIndex( subStreamIndex ) , _frameProcessed( 0 ) , _offset( offset ) - , _transcodeStream( true ) , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) @@ -153,7 +151,6 @@ StreamTranscoder::StreamTranscoder( break; } } - switchEssence( offset != 0 ); } @@ -173,7 +170,6 @@ StreamTranscoder::StreamTranscoder( , _subStreamIndex( -1 ) , _frameProcessed( 0 ) , _offset( 0 ) - , _transcodeStream( true ) , _takeFromDummy( false ) , _verbose( false ) , _offsetPassed( false ) @@ -250,7 +246,7 @@ StreamTranscoder::~StreamTranscoder() bool StreamTranscoder::processFrame() { ++_frameProcessed; - if( _transcodeStream ) + if( _transform ) { if( _subStreamIndex < 0 ) { @@ -288,12 +284,10 @@ bool StreamTranscoder::processRewrap( const int subStreamIndex ) assert( _outputStream != NULL ); DataStream dataStream; - // std::vector dataStream; if( ! _inputStream->readNextPacket( dataStream ) ) return false; _outputStream->wrap( dataStream ); - // outputStream.wrap( dataStream.at( subStreamIndex ) ); return true; } @@ -383,6 +377,8 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex ) } else { + if( _verbose ) + std::cout << "encode last frame(s)" << std::endl; if( ! _outputEssence->encodeFrame( dataStream ) ) { if( _infinityStream ) diff --git a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp index adc65cab..79dde63c 100644 --- a/src/AvTranscoder/Transcoder/StreamTranscoder.hpp +++ b/src/AvTranscoder/Transcoder/StreamTranscoder.hpp @@ -45,15 +45,14 @@ class StreamTranscoder */ bool processFrame(); - bool isTranscodeStream() const { return _transcodeStream; } - - void setVerbose( bool verbose = true ){ _verbose = verbose; } - void switchEssence( bool swithToDummy = true ); void switchToDummyEssence(); void switchToInputEssence(); + void setVerbose( bool verbose = true ){ _verbose = verbose; } + void setInfinityStream( bool isInfinity ) { _infinityStream = isInfinity; } + void setOffset( bool offset = true ){ _offset = offset; } /** @@ -93,7 +92,6 @@ class StreamTranscoder */ size_t _offset; - bool _transcodeStream; bool _takeFromDummy; bool _verbose; diff --git a/src/AvTranscoder/Transcoder/Transcoder.cpp b/src/AvTranscoder/Transcoder/Transcoder.cpp index d259fe4d..7826eb48 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.cpp +++ b/src/AvTranscoder/Transcoder/Transcoder.cpp @@ -80,7 +80,6 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro _profile.update( profileDesc ); if( ! filename.length() ) { - // if( _verbose ) std::cerr << "can't add a stream with no filename indicated" << std::endl; return; } From 99faa7a49df818d708bb9ed8d61db5fe28849116 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 14:14:05 +0200 Subject: [PATCH 6/7] Transcoder: add doc --- src/AvTranscoder/Transcoder/Transcoder.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/AvTranscoder/Transcoder/Transcoder.hpp b/src/AvTranscoder/Transcoder/Transcoder.hpp index 4ce48c83..ccc76f7f 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.hpp +++ b/src/AvTranscoder/Transcoder/Transcoder.hpp @@ -93,13 +93,29 @@ class Transcoder * @note The stream will be deleted in Transcoder's destructor. */ void add( StreamTranscoder& stream ); - + + /** + * @brief Process the next frame of all streams. + * @return if a frame was processed or not. + */ bool processFrame(); + /** + * @brief Process all the streams, and ended the process depending on the transcode politic. + * @param progress + */ void process( ProgressListener& progress ); + /** + * @brief Set the transcodage politic. + * @note If you call it before adding the streams, the process will stop at the end of the shortest stream. + */ void setProcessMethod( const EProcessMethod eProcessMethod ); + /** + * @brief Set verbose mode for the Transcoder and his streams. + * @note If you call it before adding the streams, no verbose mode will be set for the new streams. + */ void setVerbose( bool verbose = true ); void setOutputFps( double fps ) { _outputFps = fps; } @@ -121,6 +137,7 @@ class Transcoder * @note if there is only dummy, return limit of double. */ double getMinTotalDuration() const; + /** * @brief Get the duration of the longest stream. * @note if there is only dummy, return limit of double. From 50b61a138f566f02b095315d3d6a0ffe59ebfb0b Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Thu, 7 Aug 2014 14:15:08 +0200 Subject: [PATCH 7/7] Trancoder: refactoring - processFrame --- src/AvTranscoder/Transcoder/Transcoder.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/AvTranscoder/Transcoder/Transcoder.cpp b/src/AvTranscoder/Transcoder/Transcoder.cpp index 7826eb48..1510c1c4 100644 --- a/src/AvTranscoder/Transcoder/Transcoder.cpp +++ b/src/AvTranscoder/Transcoder/Transcoder.cpp @@ -219,10 +219,11 @@ bool Transcoder::processFrame() bool streamProcessStatus = _streamTranscoders.at( streamIndex )->processFrame(); - if( streamProcessStatus ) - continue; - - _streamTranscoders.clear(); + if( ! streamProcessStatus ) + { + _streamTranscoders.clear(); + return false; + } } return true; }