Bug 1207478: P3. Reduce chance of overflow. r=jwwang
authorJean-Yves Avenard <jyavenard@mozilla.com>
Thu, 24 Sep 2015 10:42:54 +1000
changeset 264149 2ef8751efbba524ff8dab70f81b1462eded482f5
parent 264148 016d6f4c79bdcba1303f88d4bce3a48214f6c637
child 264150 8e634cc7b44a4ea7dadc58de3236facabbfcb0a9
push id65541
push userjyavenard@mozilla.com
push dateThu, 24 Sep 2015 06:43:20 +0000
treeherdermozilla-inbound@8e634cc7b44a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwwang
bugs1207478
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1207478: P3. Reduce chance of overflow. r=jwwang
dom/media/VideoUtils.cpp
--- a/dom/media/VideoUtils.cpp
+++ b/dom/media/VideoUtils.cpp
@@ -20,30 +20,39 @@
 #include "nsIServiceManager.h"
 
 #include <stdint.h>
 
 namespace mozilla {
 
 using layers::PlanarYCbCrImage;
 
+static inline CheckedInt64 SaferMultDiv(int64_t aValue, uint32_t aMul, uint32_t aDiv) {
+  int64_t major = aValue / aDiv;
+  int64_t remainder = aValue % aDiv;
+  return CheckedInt64(remainder) * aMul / aDiv + major * aMul;
+}
+
 // Converts from number of audio frames to microseconds, given the specified
 // audio rate.
 CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate) {
-  return (CheckedInt64(aFrames) * USECS_PER_S) / aRate;
+  return SaferMultDiv(aFrames, USECS_PER_S, aRate);
 }
 
 media::TimeUnit FramesToTimeUnit(int64_t aFrames, uint32_t aRate) {
-  return (media::TimeUnit::FromMicroseconds(aFrames) * USECS_PER_S) / aRate;
+  int64_t major = aFrames / aRate;
+  int64_t remainder = aFrames % aRate;
+  return media::TimeUnit::FromMicroseconds(major) * USECS_PER_S +
+    (media::TimeUnit::FromMicroseconds(remainder) * USECS_PER_S) / aRate;
 }
 
 // Converts from microseconds to number of audio frames, given the specified
 // audio rate.
 CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate) {
-  return (CheckedInt64(aUsecs) * aRate) / USECS_PER_S;
+  return SaferMultDiv(aUsecs, aRate, USECS_PER_S);
 }
 
 // Format TimeUnit as number of frames at given rate.
 CheckedInt64 TimeUnitToFrames(const media::TimeUnit& aTime, uint32_t aRate) {
   return UsecsToFrames(aTime.ToMicroseconds(), aRate);
 }
 
 nsresult SecondsToUsecs(double aSeconds, int64_t& aOutUsecs) {