45 void parseBitrateVector( std::istream& left, std::vector< double >& right )
47 assert( right.empty( ) );
51 assert( left.good( ) );
59 if( bitrate <= (
double )0.0 )
61 left.setstate( std::istream::failbit );
65 right.push_back( bitrate );
72 if( left.peek( ) ==
' ' )
89 double incrementLambdaModifier(
90 double initialAdjustmentParameter,
92 const Point& previousPoint )
95 assert( (
double )0.0 < previousPoint.
bitrate );
106 assert( 0.0 < point1.
bitrate );
107 assert( 0.0 < point2.
bitrate );
119 double initialAdjustmentParameter,
120 double targetBitrate,
121 const std::list< Point >& pointList,
122 double interDampeningFactor )
124 assert( (
double )0.0 < interDampeningFactor );
125 assert( interDampeningFactor <= (
double )1.0 );
126 assert( !pointList.empty( ) );
128 double preliminaryResult;
130 if( 1 == pointList.size( ) )
132 preliminaryResult = incrementLambdaModifier( initialAdjustmentParameter, targetBitrate, pointList.back( ) );
136 std::list< Point >::const_reverse_iterator i( pointList.rbegin( ) );
144 preliminaryResult = incrementLambdaModifier( initialAdjustmentParameter, targetBitrate, pointList.back( ) );
152 double previousResult( pointList.back( ).lambdaModifier );
156 double intermediate( std::log( (
double )1.0 + std::abs( preliminaryResult - previousResult ) / previousResult ) );
157 assert( (
double )0.0 <= intermediate );
158 if( ( preliminaryResult - previousResult ) < 0.0 )
160 preliminaryResult = previousResult * ( ( double )1.0 - intermediate );
164 preliminaryResult = previousResult * ( ( double )1.0 + intermediate );
172 result = previousResult + interDampeningFactor * ( preliminaryResult - previousResult );
173 interDampeningFactor /= ( double )2.0;
174 }
while( result <= (
double )0.0 );
181 Point pointFromFullMetaLogEntry(
unsigned char index,
const MetaLogEntry< std::vector< double > >& fullEntry )
185 result.
bitrate = fullEntry.bitrateVector[ index ];
195 double interDampeningFactor(
double parameter,
double cumulativeDelta )
197 assert( 0.0 <= cumulativeDelta );
198 assert( 0.0 <= parameter );
199 return (
double )1.0 / ( parameter * cumulativeDelta + ( double )1.0 );
204 double initialAdjustmentParameter,
205 const std::vector< double > &targetBitrateVector,
206 const std::list<
MetaLogEntry< std::vector< double > > >& metaLogEntryList )
208 assert( !targetBitrateVector.empty( ) );
209 assert( !metaLogEntryList.empty( ) );
211 double cumulativeDelta( 0.0 );
212 std::vector< double > resultVector;
213 for(
unsigned char i( 0 ); i < targetBitrateVector.size( ); ++i )
216 std::list< Point > pointList;
217 std::list< MetaLogEntry< std::vector< double > > >::const_reverse_iterator j( metaLogEntryList.rbegin( ) );
218 pointList.push_front( pointFromFullMetaLogEntry( i, *j ) );
220 if( j != metaLogEntryList.rend( ) )
222 pointList.push_front( pointFromFullMetaLogEntry( i, *j ) );
227 initialAdjustmentParameter,
228 targetBitrateVector[ i ],
230 interDampeningFactor( 50.0, cumulativeDelta ) ) );
231 resultVector.push_back( newLambdaModifier );
234 const double oldLambdaModifier( pointList.back( ).lambdaModifier );
235 cumulativeDelta += std::abs( newLambdaModifier - oldLambdaModifier ) / oldLambdaModifier;
247 void ignoreUpTo( std::istream& i,
char character )
249 while( i.good( ) && character != i.get( ) )
259 void parseLambdaModifierMap( std::istream& left, std::map< unsigned char, double >& right )
263 assert( left.good( ) );
266 if(
'-' != left.get( ) )
268 left.setstate( std::istream::failbit );
274 if(
'L' != left.get( ) )
276 left.setstate( std::istream::failbit );
282 if(
'M' != left.get( ) )
284 left.setstate( std::istream::failbit );
298 if( indexLong < std::numeric_limits< unsigned char >::min( ) )
300 left.setstate( std::istream::failbit );
302 if( std::numeric_limits< unsigned char >::max( ) < indexLong )
304 left.setstate( std::istream::failbit );
310 unsigned char index( (
unsigned char )indexLong );
312 if(
' ' != left.get( ) )
314 left.setstate( std::istream::failbit );
322 double lambdaModifier;
323 left >> lambdaModifier;
324 if( lambdaModifier <= (
double )0.0 || ( !right.empty( ) && ( right.count( index ) != 0 || index <= right.rbegin( )->first ) ) )
326 left.setstate( std::istream::failbit );
330 right[ index ] = lambdaModifier;
338 if( left.peek( ) ==
' ' )
351 std::set< unsigned char > indexSetFromMap(
const std::map< unsigned char, double >& in )
353 std::set< unsigned char > result;
354 for(
typename std::map< unsigned char, double >::const_iterator i( in.begin( ) ); i != in.end( ); ++i )
356 result.insert( i->first );
364 std::istream& initialAdjustmentParameterIstream,
365 std::istream& targetsIstream,
366 std::istream& metaLogIstream )
369 double initialAdjustmentParameter;
370 initialAdjustmentParameterIstream >> initialAdjustmentParameter;
371 if( initialAdjustmentParameterIstream.fail( ) || initialAdjustmentParameterIstream.good( ) )
377 std::vector< double > targetVector;
378 parseBitrateVector( targetsIstream, targetVector );
379 if( targetVector.empty( ) || targetsIstream.fail( ) || targetsIstream.good( ) )
385 std::list< MetaLogEntry< std::map< unsigned char, double > > > metaLogEntryList;
391 if( !metaLogIstream.good( ) )
397 if(
';' != metaLogIstream.get( ) )
401 if( !metaLogIstream.good( ) )
408 if( metaLogIstream.fail( ) )
412 metaLogEntryList.push_back( entry );
414 if( !metaLogIstream.good( ) )
418 if( metaLogIstream.get( ) !=
'\n' )
422 metaLogIstream.peek( );
423 }
while( metaLogIstream.good( ) );
424 if( metaLogEntryList.empty( ) )
430 std::set< unsigned char > firstIndexSet( indexSetFromMap( metaLogEntryList.front( ).lambdaModifiers ) );
431 if( firstIndexSet.size( ) != targetVector.size( ) )
435 for( std::list<
MetaLogEntry< std::map< unsigned char, double > > >::const_iterator i( metaLogEntryList.begin( ) );
436 i != metaLogEntryList.end( );
439 if( indexSetFromMap( i->lambdaModifiers ) != firstIndexSet )
443 if( i->bitrateVector.size( ) != targetVector.size( ) )
450 std::list< MetaLogEntry< std::vector< double > > > simplifiedMetaLogEntryList;
451 for( std::list<
MetaLogEntry< std::map< unsigned char, double > > >::const_iterator i( metaLogEntryList.begin( ) );
452 i != metaLogEntryList.end( );
455 simplifiedMetaLogEntryList.push_back(
MetaLogEntry< std::vector< double > >( ) );
456 for( std::map< unsigned char, double >::const_iterator j( i->lambdaModifiers.begin( ) ); j != i->lambdaModifiers.end( ); ++j )
458 simplifiedMetaLogEntryList.back( ).lambdaModifiers.push_back( j->second );
460 simplifiedMetaLogEntryList.back( ).bitrateVector = i->bitrateVector;
464 std::vector< double > resultVector(
guessLambdaModifiers( initialAdjustmentParameter, targetVector, simplifiedMetaLogEntryList ) );
467 std::set< unsigned char >::const_iterator indexIter( firstIndexSet.begin( ) );
468 std::vector< double >::const_iterator resultIter( resultVector.begin( ) );
471 if( indexIter != firstIndexSet.begin( ) )
475 o <<
"-LM" << ( long )( *indexIter ) <<
" ";
476 o.setf( std::ostream::fixed, std::ostream::floatfield );
478 o << ( *resultIter );
482 }
while( indexIter != firstIndexSet.end( ) );
483 assert( resultIter == resultVector.end( ) );
Contains a Lambda-modifier and bitrate for only a single index.
Thrown if there is an error parsing the initial adjustment parameter.
Thrown if there is an error parsing the targets.
std::vector< double > guessLambdaModifiers(double initialAdjustmentParameter, const std::vector< double > &targetBitrateVector, const std::list< MetaLogEntry< std::vector< double > > > &metaLogEntryList)
TLambdaModifier lambdaModifiers
std::vector< double > bitrateVector
double polateLambdaModifier(double targetBitrate, const Point &point1, const Point &point2)
double guessLambdaModifier(double initialAdjustmentParameter, double targetBitrate, const std::list< Point > &pointList, double interDampeningFactor)
Thrown if there is a mismatch in the vector sizes or the Lambda-modifier indexes. ...