Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Unified Diff: media/base/moving_average.cc

Issue 1459923003: Fix Bug: Video with Variable Frame Rate plays at incorrect speed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/base/moving_average.cc
diff --git a/media/base/moving_average.cc b/media/base/moving_average.cc
index b78f4c0e3dc90a13b00068e31c031a4e39739813..512d2a19c1c17ad9a37ae2a03080fce3a78a8dff 100644
--- a/media/base/moving_average.cc
+++ b/media/base/moving_average.cc
@@ -9,8 +9,7 @@
namespace media {
MovingAverage::MovingAverage(size_t depth)
- : depth_(depth), count_(0), samples_(depth_) {
-}
+ : depth_(depth), count_(0), samples_(depth_), square_sum_ms_(0) {}
MovingAverage::~MovingAverage() {
}
@@ -20,6 +19,8 @@ void MovingAverage::AddSample(base::TimeDelta sample) {
// exceeds |depth_|.
base::TimeDelta& oldest = samples_[count_++ % depth_];
total_ += sample - oldest;
+ square_sum_ms_ += sample.InMillisecondsF() * sample.InMillisecondsF() -
DaleCurtis 2015/11/20 23:50:46 Why work in float here? Keep these as timedelta*ti
qiangchen 2015/11/25 18:06:33 I do not think there is a multiplication defined i
DaleCurtis 2015/11/25 21:29:24 They are defined, which is why I suggested it :) I
qiangchen 2015/12/01 19:37:53 There is multiplication defined between numeric (i
+ oldest.InMillisecondsF() * oldest.InMillisecondsF();
oldest = sample;
}
@@ -32,9 +33,22 @@ base::TimeDelta MovingAverage::Average() const {
return total_ / std::min(static_cast<uint64_t>(depth_), count_);
}
+base::TimeDelta MovingAverage::Deviation() const {
+ DCHECK_GT(count_, 0u);
+
+ const int size = std::min(static_cast<uint64_t>(depth_), count_);
+ const double average_ms = Average().InMillisecondsF();
+ double sqr_deviation_ms = square_sum_ms_ / size - average_ms * average_ms;
DaleCurtis 2015/11/20 23:50:46 cast size to double and keep the rest as TimeDelta
qiangchen 2015/11/25 18:06:33 Perhaps, we can use int64 to store the square_sum_
DaleCurtis 2015/11/25 21:29:23 sgtm
qiangchen 2015/12/01 19:37:53 Done.
+ if (sqr_deviation_ms < 0)
+ sqr_deviation_ms = 0;
+
+ return base::TimeDelta::FromMillisecondsD(sqrt(sqr_deviation_ms));
+}
+
void MovingAverage::Reset() {
count_ = 0;
total_ = base::TimeDelta();
+ square_sum_ms_ = 0;
std::fill(samples_.begin(), samples_.end(), base::TimeDelta());
}

Powered by Google App Engine
This is Rietveld 408576698