int main( int argc, char** ppArgv ) { try { if( argc != 3 ) { throw WrongNumberOfArgumentsException( ); } std::string initialAdjustmentParameterString( ppArgv[ 1 ] ); std::istringstream initialAdjustmentParameterIstream( initialAdjustmentParameterString ); std::string targetBitratesString( ppArgv[ 2 ] ); std::istringstream targetBitratesIstream( targetBitratesString ); guessLambdaModifiers( std::cout, initialAdjustmentParameterIstream, targetBitratesIstream, std::cin ); return 0; } catch( std::exception& e ) { std::cerr << e.what( ) << std::endl; } catch( ... ) { std::cerr << "Unknown exception" << std::endl; } return 1; }
void guessLambdaModifiers( std::ostream& o, std::istream& initialAdjustmentParameterIstream, std::istream& targetsIstream, std::istream& metaLogIstream ) { // Parse the initialAdjustmentParameter double initialAdjustmentParameter; initialAdjustmentParameterIstream >> initialAdjustmentParameter; if( initialAdjustmentParameterIstream.fail( ) || initialAdjustmentParameterIstream.good( ) ) { throw InitialAdjustmentParameterParseException( ); } // Parse the targets std::vector< double > targetVector; parseBitrateVector( targetsIstream, targetVector ); if( targetVector.empty( ) || targetsIstream.fail( ) || targetsIstream.good( ) ) throw TargetsParseException( ); // Parse the metalog std::list< MetaLogEntry< std::map< unsigned char, double > > > metaLogEntryList; do { // Parse the Lambda-modifiers MetaLogEntry< std::map< unsigned char, double > > entry; parseLambdaModifierMap( metaLogIstream, entry.lambdaModifiers ); if( !metaLogIstream.good( ) ) throw MetaLogParseException( ); // Skip the ';' if( ';' != metaLogIstream.get( ) ) throw MetaLogParseException( ); if( !metaLogIstream.good( ) ) throw MetaLogParseException( ); // Parse the bitrates parseBitrateVector( metaLogIstream, entry.bitrateVector ); if( metaLogIstream.fail( ) ) throw MetaLogParseException( ); metaLogEntryList.push_back( entry ); if( !metaLogIstream.good( ) ) break; if( metaLogIstream.get( ) != '\n' ) throw MetaLogParseException( ); metaLogIstream.peek( ); } while( metaLogIstream.good( ) ); if( metaLogEntryList.empty( ) ) throw MetaLogParseException( ); // The meta-log should not be empty // Initialize firstIndexVector and check that the sizes and indexes match std::set< unsigned char > firstIndexSet( indexSetFromMap( metaLogEntryList.front( ).lambdaModifiers ) ); if( firstIndexSet.size( ) != targetVector.size( ) ) throw MismatchedIndexesException( ); for( std::list< MetaLogEntry< std::map< unsigned char, double > > >::const_iterator i( metaLogEntryList.begin( ) ); i != metaLogEntryList.end( ); ++i ) { if( indexSetFromMap( i->lambdaModifiers ) != firstIndexSet ) throw MismatchedIndexesException( ); if( i->bitrateVector.size( ) != targetVector.size( ) ) throw MismatchedIndexesException( ); } // Initialize simplifiedMetaLogEntryList std::list< MetaLogEntry< std::vector< double > > > simplifiedMetaLogEntryList; for( std::list< MetaLogEntry< std::map< unsigned char, double > > >::const_iterator i( metaLogEntryList.begin( ) ); i != metaLogEntryList.end( ); ++i ) { simplifiedMetaLogEntryList.push_back( MetaLogEntry< std::vector< double > >( ) ); for( std::map< unsigned char, double >::const_iterator j( i->lambdaModifiers.begin( ) ); j != i->lambdaModifiers.end( ); ++j ) { simplifiedMetaLogEntryList.back( ).lambdaModifiers.push_back( j->second ); } simplifiedMetaLogEntryList.back( ).bitrateVector = i->bitrateVector; } // Run the calculations std::vector< double > resultVector( guessLambdaModifiers( initialAdjustmentParameter, targetVector, simplifiedMetaLogEntryList ) ); // Output the results std::set< unsigned char >::const_iterator indexIter( firstIndexSet.begin( ) ); std::vector< double >::const_iterator resultIter( resultVector.begin( ) ); do { if( indexIter != firstIndexSet.begin( ) ) o << " "; o << "-LM" << ( long )( *indexIter ) << " "; o.setf( std::ostream::fixed, std::ostream::floatfield ); o.precision( 7 ); o << ( *resultIter ); ++indexIter; ++resultIter; } while( indexIter != firstIndexSet.end( ) ); assert( resultIter == resultVector.end( ) ); // The index set and the result vector should be the same size }