Bug 1264199: P0.1. Export SaferMultDiv method. r?gerald
MozReview-Commit-ID: CzewhPB1Nod
--- a/dom/media/VideoUtils.cpp
+++ b/dom/media/VideoUtils.cpp
@@ -26,20 +26,20 @@
#include "nsContentTypeParser.h"
#include <stdint.h>
namespace mozilla {
using layers::PlanarYCbCrImage;
-static inline CheckedInt64 SaferMultDiv(int64_t aValue, uint32_t aMul, uint32_t aDiv) {
+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;
+ return CheckedInt64(remainder) * aMul / aDiv + CheckedInt64(major) * aMul;
}
// Converts from number of audio frames to microseconds, given the specified
// audio rate.
CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate) {
return SaferMultDiv(aFrames, USECS_PER_S, aRate);
}
--- a/dom/media/VideoUtils.h
+++ b/dom/media/VideoUtils.h
@@ -126,16 +126,19 @@ media::TimeIntervals GetEstimatedBuffere
int64_t aDurationUsecs);
// Converts from number of audio frames (aFrames) to microseconds, given
// the specified audio rate (aRate).
CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate);
// Converts from number of audio frames (aFrames) TimeUnit, given
// the specified audio rate (aRate).
media::TimeUnit FramesToTimeUnit(int64_t aFrames, uint32_t aRate);
+// Perform aValue * aMul / aDiv, reducing the possibility of overflow due to
+// aValue * aMul overflowing.
+CheckedInt64 SaferMultDiv(int64_t aValue, uint32_t aMul, uint32_t aDiv);
// Converts from microseconds (aUsecs) to number of audio frames, given the
// specified audio rate (aRate). Stores the result in aOutFrames. Returns
// true if the operation succeeded, or false if there was an integer
// overflow while calulating the conversion.
CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate);
// Format TimeUnit as number of frames at given rate.