void MediaTime::setTimeScale(int32_t timeScale) { if (timeScale == m_timeScale) return; timeScale = std::min(MaximumTimeScale, timeScale); int64_t wholePart = m_timeValue / m_timeScale; // If setting the time scale will cause an overflow, divide the // timescale by two until the number will fit, and round the // result. int64_t newWholePart; while (!safeMultiply(wholePart, timeScale, newWholePart)) timeScale /= 2; int64_t remainder = m_timeValue % m_timeScale; m_timeValue = newWholePart + (remainder * timeScale) / m_timeScale; m_timeScale = timeScale; }
MediaTime MediaTime::operator*(int32_t rhs) const { if (isInvalid()) return invalidTime(); if (isIndefinite()) return indefiniteTime(); if (!rhs) return zeroTime(); if (isPositiveInfinite()) { if (rhs > 0) return positiveInfiniteTime(); return negativeInfiniteTime(); } if (isNegativeInfinite()) { if (rhs > 0) return negativeInfiniteTime(); return positiveInfiniteTime(); } MediaTime a = *this; if (a.hasDoubleValue()) { a.m_timeValueAsDouble *= rhs; return a; } while (!safeMultiply(a.m_timeValue, rhs, a.m_timeValue)) { if (a.m_timeScale == 1) return signum(a.m_timeValue) == signum(rhs) ? positiveInfiniteTime() : negativeInfiniteTime(); a.setTimeScale(a.m_timeScale / 2); } return a; }
static int32_t leastCommonMultiple(int32_t a, int32_t b, int32_t &result) { return safeMultiply(a, b / greatestCommonDivisor(a, b), result); }