I’ve got a pretty happy relationship with AVFoundation, but the CoreMedia API’s can sometimes be inscrutable. Today, it’s time division. Given the existence of
CMTimeMultiply, one might expect ‘CMTimeDivide’. Instead, we find
Notice that CoreMedia works smartly underneath the hood. Rather than divide the value, it simply scales the timescale, keeping the value evenly divisible by the timescale. What happens if we use
We can see that instead of scaling the timescale, the value and timescale have been expanded, as well as a new flag added.
This tells us that CoreMedia has done some rounding in the
CMTimeMultiplyByFloat64 calculation, while
CMTimeMultiplyByRatio remained exact. The documentation sheds some more light on the exact behavior:
The exact rational value will be preserved, if possible without overflow. If an overflow would occur, a new timescale will be chosen so as to minimize the rounding error. Default rounding will be applied when converting the result to this timescale. If the result value still overflows when timescale == 1, then the result will be either positive or negative infinity, depending on the direction of the overflow.