Skip to content

Commit

Permalink
[WTF] MediaTime: compute flags on multiplication with doubles
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=246746

Reviewed by Yusuke Suzuki.

The current multiplication algorithm in WTF::MediaTime updates the time
value without updating the flags. This becomes a problem if the
multiplication promotes the number to infinity.

This patch fixes this problem and adds a test for it.

* Source/WTF/wtf/MediaTime.cpp:
(WTF::MediaTime::operator* const):
* Tools/TestWebKitAPI/Tests/WTF/MediaTime.cpp:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/255767@main
  • Loading branch information
ntrrgc authored and calvaris committed Jan 28, 2023
1 parent 2336d9d commit ec4de9d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Source/WTF/wtf/MediaTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,10 @@ MediaTime MediaTime::operator*(int32_t rhs) const
return positiveInfiniteTime();
}

MediaTime a = *this;

if (a.hasDoubleValue()) {
a.m_timeValueAsDouble *= rhs;
return a;
}
if (hasDoubleValue())
return MediaTime::createWithDouble(m_timeValueAsDouble * rhs);

MediaTime a = *this;
while (!safeMultiply(a.m_timeValue, rhs, a.m_timeValue)) {
if (a.m_timeScale == 1)
return signum(a.m_timeValue) == signum(rhs) ? positiveInfiniteTime() : negativeInfiniteTime();
Expand Down
2 changes: 2 additions & 0 deletions Tools/TestWebKitAPI/Tests/WTF/MediaTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ TEST(WTF, MediaTime)
EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::lowest()) + MediaTime::createWithDouble(numeric_limits<double>::lowest()), MediaTime::negativeInfiniteTime());
EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::lowest()) - MediaTime::createWithDouble(numeric_limits<double>::max()), MediaTime::negativeInfiniteTime());
EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::max()) - MediaTime::createWithDouble(numeric_limits<double>::lowest()), MediaTime::positiveInfiniteTime());
EXPECT_NE(MediaTime::createWithDouble(numeric_limits<double>::max()), MediaTime::positiveInfiniteTime());
EXPECT_EQ(MediaTime::createWithDouble(numeric_limits<double>::max()) * 2, MediaTime::positiveInfiniteTime());

// Rounding
EXPECT_EQ(MediaTime(1, 1).toTimeScale(2).timeValue(), 2);
Expand Down

0 comments on commit ec4de9d

Please sign in to comment.