MediaTime.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  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.
  16. *
  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.
  27. */
  28. #include "config.h"
  29. #include "MediaTime.h"
  30. #include <algorithm>
  31. #include <wtf/CheckedArithmetic.h>
  32. #include <wtf/MathExtras.h>
  33. using namespace std;
  34. namespace WTF {
  35. static int32_t greatestCommonDivisor(int32_t a, int32_t b)
  36. {
  37. // Euclid's Algorithm
  38. int32_t temp = 0;
  39. while (b) {
  40. temp = b;
  41. b = a % b;
  42. a = temp;
  43. }
  44. return a;
  45. }
  46. static int32_t leastCommonMultiple(int32_t a, int32_t b, int32_t &result)
  47. {
  48. return safeMultiply(a, b / greatestCommonDivisor(a, b), result);
  49. }
  50. const int32_t MediaTime::MaximumTimeScale = 0x7fffffffL;
  51. MediaTime::MediaTime()
  52. : m_timeValue(0)
  53. , m_timeScale(DefaultTimeScale)
  54. , m_timeFlags(Valid)
  55. {
  56. }
  57. MediaTime::MediaTime(int64_t value, int32_t scale, uint32_t flags)
  58. : m_timeValue(value)
  59. , m_timeScale(scale)
  60. , m_timeFlags(flags)
  61. {
  62. }
  63. MediaTime::~MediaTime()
  64. {
  65. }
  66. MediaTime::MediaTime(const MediaTime& rhs)
  67. {
  68. *this = rhs;
  69. }
  70. MediaTime MediaTime::createWithFloat(float floatTime, int32_t timeScale)
  71. {
  72. if (floatTime != floatTime)
  73. return invalidTime();
  74. if (std::isinf(floatTime))
  75. return std::signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime();
  76. if (floatTime > numeric_limits<int64_t>::max())
  77. return positiveInfiniteTime();
  78. if (floatTime < numeric_limits<int64_t>::min())
  79. return negativeInfiniteTime();
  80. while (floatTime * timeScale > numeric_limits<int64_t>::max())
  81. timeScale /= 2;
  82. return MediaTime(static_cast<int64_t>(floatTime * timeScale), timeScale, Valid);
  83. }
  84. MediaTime MediaTime::createWithDouble(double doubleTime, int32_t timeScale)
  85. {
  86. if (doubleTime != doubleTime)
  87. return invalidTime();
  88. if (std::isinf(doubleTime))
  89. return std::signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime();
  90. if (doubleTime > numeric_limits<int64_t>::max())
  91. return positiveInfiniteTime();
  92. if (doubleTime < numeric_limits<int64_t>::min())
  93. return negativeInfiniteTime();
  94. while (doubleTime * timeScale > numeric_limits<int64_t>::max())
  95. timeScale /= 2;
  96. return MediaTime(static_cast<int64_t>(doubleTime * timeScale), timeScale, Valid);
  97. }
  98. float MediaTime::toFloat() const
  99. {
  100. if (isInvalid() || isIndefinite())
  101. return std::numeric_limits<float>::quiet_NaN();
  102. if (isPositiveInfinite())
  103. return std::numeric_limits<float>::infinity();
  104. if (isNegativeInfinite())
  105. return -std::numeric_limits<float>::infinity();
  106. return static_cast<float>(m_timeValue) / m_timeScale;
  107. }
  108. double MediaTime::toDouble() const
  109. {
  110. if (isInvalid() || isIndefinite())
  111. return std::numeric_limits<double>::quiet_NaN();
  112. if (isPositiveInfinite())
  113. return std::numeric_limits<double>::infinity();
  114. if (isNegativeInfinite())
  115. return -std::numeric_limits<double>::infinity();
  116. return static_cast<double>(m_timeValue) / m_timeScale;
  117. }
  118. MediaTime& MediaTime::operator=(const MediaTime& rhs)
  119. {
  120. m_timeValue = rhs.m_timeValue;
  121. m_timeScale = rhs.m_timeScale;
  122. m_timeFlags = rhs.m_timeFlags;
  123. return *this;
  124. }
  125. MediaTime MediaTime::operator+(const MediaTime& rhs) const
  126. {
  127. if (rhs.isInvalid() || isInvalid())
  128. return invalidTime();
  129. if (rhs.isIndefinite() || isIndefinite())
  130. return indefiniteTime();
  131. if (isPositiveInfinite()) {
  132. if (rhs.isNegativeInfinite())
  133. return invalidTime();
  134. return positiveInfiniteTime();
  135. }
  136. if (isNegativeInfinite()) {
  137. if (rhs.isPositiveInfinite())
  138. return invalidTime();
  139. return negativeInfiniteTime();
  140. }
  141. int32_t commonTimeScale;
  142. if (!leastCommonMultiple(this->m_timeScale, rhs.m_timeScale, commonTimeScale) || commonTimeScale > MaximumTimeScale)
  143. commonTimeScale = MaximumTimeScale;
  144. MediaTime a = *this;
  145. MediaTime b = rhs;
  146. a.setTimeScale(commonTimeScale);
  147. b.setTimeScale(commonTimeScale);
  148. while (!safeAdd(a.m_timeValue, b.m_timeValue, a.m_timeValue)) {
  149. if (commonTimeScale == 1)
  150. return a.m_timeValue > 0 ? positiveInfiniteTime() : negativeInfiniteTime();
  151. commonTimeScale /= 2;
  152. a.setTimeScale(commonTimeScale);
  153. b.setTimeScale(commonTimeScale);
  154. }
  155. return a;
  156. }
  157. MediaTime MediaTime::operator-(const MediaTime& rhs) const
  158. {
  159. if (rhs.isInvalid() || isInvalid())
  160. return invalidTime();
  161. if (rhs.isIndefinite() || isIndefinite())
  162. return indefiniteTime();
  163. if (isPositiveInfinite()) {
  164. if (rhs.isPositiveInfinite())
  165. return invalidTime();
  166. return positiveInfiniteTime();
  167. }
  168. if (isNegativeInfinite()) {
  169. if (rhs.isNegativeInfinite())
  170. return invalidTime();
  171. return negativeInfiniteTime();
  172. }
  173. int32_t commonTimeScale;
  174. if (!leastCommonMultiple(this->m_timeScale, rhs.m_timeScale, commonTimeScale) || commonTimeScale > MaximumTimeScale)
  175. commonTimeScale = MaximumTimeScale;
  176. MediaTime a = *this;
  177. MediaTime b = rhs;
  178. a.setTimeScale(commonTimeScale);
  179. b.setTimeScale(commonTimeScale);
  180. while (!safeSub(a.m_timeValue, b.m_timeValue, a.m_timeValue)) {
  181. if (commonTimeScale == 1)
  182. return a.m_timeValue > 0 ? positiveInfiniteTime() : negativeInfiniteTime();
  183. commonTimeScale /= 2;
  184. a.setTimeScale(commonTimeScale);
  185. b.setTimeScale(commonTimeScale);
  186. }
  187. return a;
  188. }
  189. bool MediaTime::operator<(const MediaTime& rhs) const
  190. {
  191. return compare(rhs) == LessThan;
  192. }
  193. bool MediaTime::operator>(const MediaTime& rhs) const
  194. {
  195. return compare(rhs) == GreaterThan;
  196. }
  197. bool MediaTime::operator==(const MediaTime& rhs) const
  198. {
  199. return compare(rhs) == EqualTo;
  200. }
  201. bool MediaTime::operator>=(const MediaTime& rhs) const
  202. {
  203. return compare(rhs) >= EqualTo;
  204. }
  205. bool MediaTime::operator<=(const MediaTime& rhs) const
  206. {
  207. return compare(rhs) <= EqualTo;
  208. }
  209. MediaTime::ComparisonFlags MediaTime::compare(const MediaTime& rhs) const
  210. {
  211. if ((isPositiveInfinite() && rhs.isPositiveInfinite())
  212. || (isNegativeInfinite() && rhs.isNegativeInfinite())
  213. || (isInvalid() && rhs.isInvalid())
  214. || (isIndefinite() && rhs.isIndefinite()))
  215. return EqualTo;
  216. if (isInvalid())
  217. return GreaterThan;
  218. if (rhs.isInvalid())
  219. return LessThan;
  220. if (rhs.isNegativeInfinite() || isPositiveInfinite())
  221. return GreaterThan;
  222. if (rhs.isPositiveInfinite() || isNegativeInfinite())
  223. return LessThan;
  224. if (isIndefinite())
  225. return GreaterThan;
  226. if (rhs.isIndefinite())
  227. return LessThan;
  228. int64_t rhsWhole = rhs.m_timeValue / rhs.m_timeScale;
  229. int64_t lhsWhole = m_timeValue / m_timeScale;
  230. if (lhsWhole > rhsWhole)
  231. return GreaterThan;
  232. if (lhsWhole < rhsWhole)
  233. return LessThan;
  234. int64_t rhsRemain = rhs.m_timeValue % rhs.m_timeScale;
  235. int64_t lhsRemain = m_timeValue % m_timeScale;
  236. int64_t lhsFactor = lhsRemain * rhs.m_timeScale;
  237. int64_t rhsFactor = rhsRemain * m_timeScale;
  238. if (lhsFactor == rhsFactor)
  239. return EqualTo;
  240. return lhsFactor > rhsFactor ? GreaterThan : LessThan;
  241. }
  242. const MediaTime& MediaTime::zeroTime()
  243. {
  244. static const MediaTime* time = new MediaTime(0, 1, Valid);
  245. return *time;
  246. }
  247. const MediaTime& MediaTime::invalidTime()
  248. {
  249. static const MediaTime* time = new MediaTime(-1, 1, 0);
  250. return *time;
  251. }
  252. const MediaTime& MediaTime::positiveInfiniteTime()
  253. {
  254. static const MediaTime* time = new MediaTime(0, 1, PositiveInfinite | Valid);
  255. return *time;
  256. }
  257. const MediaTime& MediaTime::negativeInfiniteTime()
  258. {
  259. static const MediaTime* time = new MediaTime(-1, 1, NegativeInfinite | Valid);
  260. return *time;
  261. }
  262. const MediaTime& MediaTime::indefiniteTime()
  263. {
  264. static const MediaTime* time = new MediaTime(0, 1, Indefinite | Valid);
  265. return *time;
  266. }
  267. void MediaTime::setTimeScale(int32_t timeScale)
  268. {
  269. if (timeScale == m_timeScale)
  270. return;
  271. timeScale = std::min(MaximumTimeScale, timeScale);
  272. int64_t wholePart = m_timeValue / m_timeScale;
  273. // If setting the time scale will cause an overflow, divide the
  274. // timescale by two until the number will fit, and round the
  275. // result.
  276. int64_t newWholePart;
  277. while (!safeMultiply(wholePart, timeScale, newWholePart))
  278. timeScale /= 2;
  279. int64_t remainder = m_timeValue % m_timeScale;
  280. m_timeValue = newWholePart + (remainder * timeScale) / m_timeScale;
  281. m_timeScale = timeScale;
  282. }
  283. static int32_t signum(int64_t val)
  284. {
  285. return (0 < val) - (val < 0);
  286. }
  287. MediaTime abs(const MediaTime& rhs)
  288. {
  289. if (rhs.isInvalid())
  290. return MediaTime::invalidTime();
  291. if (rhs.isNegativeInfinite() || rhs.isPositiveInfinite())
  292. return MediaTime::positiveInfiniteTime();
  293. MediaTime val = rhs;
  294. val.m_timeValue *= signum(rhs.m_timeScale) * signum(rhs.m_timeValue);
  295. return val;
  296. }
  297. }