2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "MediaTime.h"
33 #include <wtf/CheckedArithmetic.h>
34 #include <wtf/MathExtras.h>
40 static int32_t greatestCommonDivisor(int32_t a, int32_t b)
52 static int32_t leastCommonMultiple(int32_t a, int32_t b, int32_t &result)
54 return safeMultiply(a, b / greatestCommonDivisor(a, b), result);
57 const int32_t MediaTime::MaximumTimeScale = 0x7fffffffL;
59 MediaTime::MediaTime()
61 , m_timeScale(DefaultTimeScale)
66 MediaTime::MediaTime(int64_t value, int32_t scale, uint32_t flags)
73 MediaTime::~MediaTime()
77 MediaTime::MediaTime(const MediaTime& rhs)
82 MediaTime MediaTime::createWithFloat(float floatTime, int32_t timeScale)
84 if (floatTime != floatTime)
87 return signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime();
88 if (floatTime > numeric_limits<int64_t>::max())
89 return positiveInfiniteTime();
90 if (floatTime < numeric_limits<int64_t>::min())
91 return negativeInfiniteTime();
93 while (floatTime * timeScale > numeric_limits<int64_t>::max())
95 return MediaTime(static_cast<int64_t>(floatTime * timeScale), timeScale, Valid);
98 MediaTime MediaTime::createWithDouble(double doubleTime, int32_t timeScale)
100 if (doubleTime != doubleTime)
101 return invalidTime();
102 if (isinf(doubleTime))
103 return signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime();
104 if (doubleTime > numeric_limits<int64_t>::max())
105 return positiveInfiniteTime();
106 if (doubleTime < numeric_limits<int64_t>::min())
107 return negativeInfiniteTime();
109 while (doubleTime * timeScale > numeric_limits<int64_t>::max())
111 return MediaTime(static_cast<int64_t>(doubleTime * timeScale), timeScale, Valid);
114 float MediaTime::toFloat() const
116 if (isInvalid() || isIndefinite())
117 return std::numeric_limits<float>::quiet_NaN();
118 if (isPositiveInfinite())
119 return std::numeric_limits<float>::infinity();
120 if (isNegativeInfinite())
121 return -std::numeric_limits<float>::infinity();
122 return static_cast<float>(m_timeValue) / m_timeScale;
125 double MediaTime::toDouble() const
127 if (isInvalid() || isIndefinite())
128 return std::numeric_limits<double>::quiet_NaN();
129 if (isPositiveInfinite())
130 return std::numeric_limits<double>::infinity();
131 if (isNegativeInfinite())
132 return -std::numeric_limits<double>::infinity();
133 return static_cast<double>(m_timeValue) / m_timeScale;
136 MediaTime& MediaTime::operator=(const MediaTime& rhs)
138 m_timeValue = rhs.m_timeValue;
139 m_timeScale = rhs.m_timeScale;
140 m_timeFlags = rhs.m_timeFlags;
144 MediaTime MediaTime::operator+(const MediaTime& rhs) const
146 if (rhs.isInvalid() || isInvalid())
147 return invalidTime();
149 if (rhs.isIndefinite() || isIndefinite())
150 return indefiniteTime();
152 if (isPositiveInfinite()) {
153 if (rhs.isNegativeInfinite())
154 return invalidTime();
155 return positiveInfiniteTime();
158 if (isNegativeInfinite()) {
159 if (rhs.isPositiveInfinite())
160 return invalidTime();
161 return negativeInfiniteTime();
164 int32_t commonTimeScale;
165 if (!leastCommonMultiple(this->m_timeScale, rhs.m_timeScale, commonTimeScale) || commonTimeScale > MaximumTimeScale)
166 commonTimeScale = MaximumTimeScale;
169 a.setTimeScale(commonTimeScale);
170 b.setTimeScale(commonTimeScale);
171 while (!safeAdd(a.m_timeValue, b.m_timeValue, a.m_timeValue)) {
172 if (commonTimeScale == 1)
173 return a.m_timeValue > 0 ? positiveInfiniteTime() : negativeInfiniteTime();
174 commonTimeScale /= 2;
175 a.setTimeScale(commonTimeScale);
176 b.setTimeScale(commonTimeScale);
181 MediaTime MediaTime::operator-(const MediaTime& rhs) const
183 if (rhs.isInvalid() || isInvalid())
184 return invalidTime();
186 if (rhs.isIndefinite() || isIndefinite())
187 return indefiniteTime();
189 if (isPositiveInfinite()) {
190 if (rhs.isPositiveInfinite())
191 return invalidTime();
192 return positiveInfiniteTime();
195 if (isNegativeInfinite()) {
196 if (rhs.isNegativeInfinite())
197 return invalidTime();
198 return negativeInfiniteTime();
201 int32_t commonTimeScale;
202 if (!leastCommonMultiple(this->m_timeScale, rhs.m_timeScale, commonTimeScale) || commonTimeScale > MaximumTimeScale)
203 commonTimeScale = MaximumTimeScale;
206 a.setTimeScale(commonTimeScale);
207 b.setTimeScale(commonTimeScale);
208 while (!safeSub(a.m_timeValue, b.m_timeValue, a.m_timeValue)) {
209 if (commonTimeScale == 1)
210 return a.m_timeValue > 0 ? positiveInfiniteTime() : negativeInfiniteTime();
211 commonTimeScale /= 2;
212 a.setTimeScale(commonTimeScale);
213 b.setTimeScale(commonTimeScale);
218 bool MediaTime::operator<(const MediaTime& rhs) const
220 return compare(rhs) == LessThan;
223 bool MediaTime::operator>(const MediaTime& rhs) const
225 return compare(rhs) == GreaterThan;
228 bool MediaTime::operator==(const MediaTime& rhs) const
230 return compare(rhs) == EqualTo;
233 bool MediaTime::operator>=(const MediaTime& rhs) const
235 return compare(rhs) >= EqualTo;
238 bool MediaTime::operator<=(const MediaTime& rhs) const
240 return compare(rhs) <= EqualTo;
243 MediaTime::ComparisonFlags MediaTime::compare(const MediaTime& rhs) const
245 if ((isPositiveInfinite() && rhs.isPositiveInfinite())
246 || (isNegativeInfinite() && rhs.isNegativeInfinite())
247 || (isInvalid() && rhs.isInvalid())
248 || (isIndefinite() && rhs.isIndefinite()))
257 if (rhs.isNegativeInfinite() || isPositiveInfinite())
260 if (rhs.isPositiveInfinite() || isNegativeInfinite())
266 if (rhs.isIndefinite())
269 int64_t rhsWhole = rhs.m_timeValue / rhs.m_timeScale;
270 int64_t lhsWhole = m_timeValue / m_timeScale;
271 if (lhsWhole > rhsWhole)
273 if (lhsWhole < rhsWhole)
276 int64_t rhsRemain = rhs.m_timeValue % rhs.m_timeScale;
277 int64_t lhsRemain = m_timeValue % m_timeScale;
278 int64_t lhsFactor = lhsRemain * rhs.m_timeScale;
279 int64_t rhsFactor = rhsRemain * m_timeScale;
281 if (lhsFactor == rhsFactor)
283 return lhsFactor > rhsFactor ? GreaterThan : LessThan;
286 const MediaTime& MediaTime::zeroTime()
288 static const MediaTime* time = new MediaTime(0, 1, Valid);
292 const MediaTime& MediaTime::invalidTime()
294 static const MediaTime* time = new MediaTime(-1, 1, 0);
298 const MediaTime& MediaTime::positiveInfiniteTime()
300 static const MediaTime* time = new MediaTime(0, 1, PositiveInfinite | Valid);
304 const MediaTime& MediaTime::negativeInfiniteTime()
306 static const MediaTime* time = new MediaTime(-1, 1, NegativeInfinite | Valid);
310 const MediaTime& MediaTime::indefiniteTime()
312 static const MediaTime* time = new MediaTime(0, 1, Indefinite | Valid);
316 void MediaTime::setTimeScale(int32_t timeScale)
318 if (timeScale == m_timeScale)
320 timeScale = std::min(MaximumTimeScale, timeScale);
321 int64_t wholePart = m_timeValue / m_timeScale;
323 // If setting the time scale will cause an overflow, divide the
324 // timescale by two until the number will fit, and round the
326 int64_t newWholePart;
327 while (!safeMultiply(wholePart, timeScale, newWholePart))
330 int64_t remainder = m_timeValue % m_timeScale;
331 m_timeValue = newWholePart + (remainder * timeScale) / m_timeScale;
332 m_timeScale = timeScale;
335 static int32_t signum(int64_t val)
337 return (0 < val) - (val < 0);
340 MediaTime abs(const MediaTime& rhs)
343 return MediaTime::invalidTime();
344 if (rhs.isNegativeInfinite() || rhs.isPositiveInfinite())
345 return MediaTime::positiveInfiniteTime();
347 val.m_timeValue *= signum(rhs.m_timeScale) * signum(rhs.m_timeValue);