''' The copyright in this software is being made available under the BSD License, included below. This software may be subject to other third party and contributor rights, including patent rights, and no such rights are granted under this license. Copyright (c) 2022-2024, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. Copyright (c) 2024 Dolby International AB All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' #!/usr/bin/python3 import math import sys assert sys.version_info >= (3, 6) class BDRate(): def __init__( self ): self._refDist = [] self._refRate = [] self._tgtDist = [] self._tgtRate = [] def getBDRate( self ): assert len( self._refRate ) > 0 assert len( self._tgtRate ) > 0 assert len( self._refDist ) == len( self._refRate ) assert len( self._tgtDist ) == len( self._tgtRate ) _minDist = max( min( self._refDist ), min( self._tgtDist ) ) _maxDist = min( max( self._refDist ), max( self._tgtDist ) ) _vA = self.calcArea( self._refRate, self._refDist, _minDist, _maxDist ) _vB = self.calcArea( self._tgtRate, self._tgtDist, _minDist, _maxDist ) _avg = ( _vB - _vA ) / ( _maxDist - _minDist ) return ( ( 10.0 ** _avg ) - 1.0 ) * 100.0 def calcArea( self, _rates, _dists, _minDist, _maxDist ): _numPts = len( _rates ) _logRate = [ 0.0 ] * _numPts _logDist = [ 0.0 ] * _numPts for i in range( _numPts ): _logRate[ i ] = math.log10( _rates[ _numPts - 1 - i ] ) _logDist[ i ] = _dists[ _numPts - 1 - i ] _H = [ 0.0 ] * ( _numPts - 1 ) _delta = [ 0.0 ] * ( _numPts - 1 ) for i in range( _numPts - 1 ): _H[ i ] = _logDist[ i + 1 ] - _logDist[ i ] _delta[ i ] = ( _logRate[ i + 1 ] - _logRate[ i ] ) / _H[ i ] _d = [ 0.0 ] * _numPts _d[ 0 ] = self.pchipend( _H[ 0 ], _H[ 1 ], _delta[ 0 ], _delta[ 1 ] ) for i in range( 1, _numPts - 1 ): _d[ i ] = ( 3 * _H[ i - 1 ] + 3 * _H[ i ] ) / ( ( 2 * _H[ i ] + _H[ i - 1 ] ) / _delta[ i - 1 ] + ( _H[ i ] + 2 * _H[ i - 1 ] ) / _delta[ i ] ) _d[ _numPts - 1 ] = self.pchipend( _H[ 2 ], _H[ 1 ], _delta[ 2 ], _delta[ 1 ] ) _c = [ 0.0 ] * ( _numPts - 1 ) _b = [ 0.0 ] * ( _numPts - 1 ) for i in range( _numPts - 1 ): _c[ i ] = ( 3 * _delta[ i ] - 2 * _d[ i ] - _d[ i + 1 ] ) / _H[ i ] _b[ i ] = ( _d[ i ] - 2 * _delta[ i ] + _d[ i + 1 ] ) / ( _H[ i ] * _H[ i ] ) _s0 = 0.0 _s1 = 0.0 _result = 0.0 for i in range( _numPts - 1 ): _s0 = _logDist[ i ] _s1 = _logDist[ i + 1 ] # clip s0 to valid range _s0 = max( _s0, _minDist ) _s0 = min( _s0, _maxDist ) # clip s1 to valid range _s1 = max( _s1, _minDist ) _s1 = min( _s1, _maxDist ) _s0 = _s0 - _logDist[ i ] _s1 = _s1 - _logDist[ i ] if _s1 > _s0: _result = _result + ( _s1 - _s0 ) * _logRate[ i ] _result = _result + ( _s1 * _s1 - _s0 * _s0 ) * _d[ i ] / 2 _result = _result + ( _s1 * _s1 * _s1 - _s0 * _s0 * _s0 ) * _c[ i ] / 3 _result = _result + ( _s1 * _s1 * _s1 * _s1 - _s0 * _s0 * _s0 * _s0 ) * _b[ i ] / 4 return _result def pchipend( self, _h1, _h2, _del1, _del2 ): _d = ( ( 2 * _h1 + _h2 ) * _del1 - _h1 * _del2 ) / ( _h1 + _h2 ) if _d * _del1 < 0: _d = 0 elif ( _del1 * _del2 < 0 ) and ( abs( _d ) > abs( 3 * _del1 ) ): _d = 3 * _del1 return _d ''' Below: Dummy example to calculate the BD-Rate ''' if __name__ == "__main__": _bd = BDRate() _bd._refDist = [ 9.936, 9.026, 8.160, 7.255, 6.174, 5.370, 4.540, 3.835, 3.195, 2.680, 1.716, 1.236, 0.519 ] _bd._refRate = [ 0.175, 0.200, 0.237, 0.292, 0.376, 0.468, 0.602, 0.773, 1.014, 1.338, 2.203, 2.755, 3.255 ] _bd._tgtDist = [ 9.884, 9.021, 8.161, 7.259, 6.179, 5.373, 4.539, 3.834, 3.196, 2.680, 1.714, 1.238, 0.518 ] _bd._tgtRate = [ 0.176, 0.201, 0.237, 0.293, 0.375, 0.469, 0.602, 0.773, 1.013, 1.338, 2.205, 2.756, 3.259 ] bdrate = _bd.getBDRate() print( "The BD rate is: %.3f" % bdrate )