OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/strings/stringprintf.h" |
| 7 #include "media/filters/video_cadence_estimator.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace media { |
| 11 |
| 12 // See VideoCadenceEstimator header for more details. |
| 13 static const int kMinimumAcceptableTimeBetweenGlitchesSecs = 8; |
| 14 |
| 15 // Slows down the given |fps| according to NTSC field reduction standards; see |
| 16 // http://en.wikipedia.org/wiki/Frame_rate#Digital_video_and_television |
| 17 static double NTSC(double fps) { |
| 18 return fps / 1.001; |
| 19 } |
| 20 |
| 21 static base::TimeDelta Interval(double hertz) { |
| 22 return base::TimeDelta::FromSecondsD(1.0 / hertz); |
| 23 } |
| 24 |
| 25 static void VerifyCadence(VideoCadenceEstimator* estimator, |
| 26 double frame_hertz, |
| 27 double render_hertz, |
| 28 int expected_cadence) { |
| 29 SCOPED_TRACE(base::StringPrintf("Checking %.03f fps into %0.03f", frame_hertz, |
| 30 render_hertz)); |
| 31 estimator->Reset(); |
| 32 const base::TimeDelta acceptable_drift = Interval(frame_hertz) / 2; |
| 33 const bool found_cadence = estimator->UpdateCadenceEstimate( |
| 34 Interval(render_hertz), Interval(frame_hertz), acceptable_drift); |
| 35 EXPECT_EQ(found_cadence, estimator->has_cadence()); |
| 36 EXPECT_EQ(!!expected_cadence, estimator->has_cadence()); |
| 37 |
| 38 // Nothing further to test. |
| 39 if (!expected_cadence) |
| 40 return; |
| 41 |
| 42 // Spot check a few frame indices. |
| 43 if (frame_hertz <= render_hertz) { |
| 44 EXPECT_EQ(expected_cadence, estimator->GetCadenceForFrame(0)); |
| 45 EXPECT_EQ(expected_cadence, estimator->GetCadenceForFrame(1)); |
| 46 EXPECT_EQ(expected_cadence, estimator->GetCadenceForFrame(2)); |
| 47 } else { |
| 48 EXPECT_EQ(1, estimator->GetCadenceForFrame(0)); |
| 49 EXPECT_EQ(0, estimator->GetCadenceForFrame(1)); |
| 50 EXPECT_EQ(1, estimator->GetCadenceForFrame(expected_cadence)); |
| 51 EXPECT_EQ(0, estimator->GetCadenceForFrame(expected_cadence + 1)); |
| 52 } |
| 53 } |
| 54 |
| 55 // Spot check common display and frame rate pairs for correctness. |
| 56 TEST(VideoCadenceEstimatorTest, CadenceCalculations) { |
| 57 VideoCadenceEstimator estimator( |
| 58 base::TimeDelta::FromSeconds(kMinimumAcceptableTimeBetweenGlitchesSecs)); |
| 59 estimator.disable_cadence_hysteresis_for_testing(); |
| 60 |
| 61 VerifyCadence(&estimator, 24, 60, 0); |
| 62 VerifyCadence(&estimator, NTSC(24), 60, 0); |
| 63 VerifyCadence(&estimator, 25, 60, 0); |
| 64 VerifyCadence(&estimator, NTSC(30), 60, 2); |
| 65 VerifyCadence(&estimator, 30, 60, 2); |
| 66 VerifyCadence(&estimator, 50, 60, 0); |
| 67 VerifyCadence(&estimator, NTSC(60), 60, 1); |
| 68 VerifyCadence(&estimator, 120, 60, 2); |
| 69 |
| 70 // 50Hz is common in the EU. |
| 71 VerifyCadence(&estimator, NTSC(24), 50, 0); |
| 72 VerifyCadence(&estimator, 24, 50, 0); |
| 73 VerifyCadence(&estimator, NTSC(25), 50, 2); |
| 74 VerifyCadence(&estimator, 25, 50, 2); |
| 75 VerifyCadence(&estimator, NTSC(30), 50, 0); |
| 76 VerifyCadence(&estimator, 30, 50, 0); |
| 77 VerifyCadence(&estimator, NTSC(60), 50, 0); |
| 78 VerifyCadence(&estimator, 60, 50, 0); |
| 79 |
| 80 VerifyCadence(&estimator, 25, NTSC(60), 0); |
| 81 VerifyCadence(&estimator, 120, NTSC(60), 0); |
| 82 VerifyCadence(&estimator, 1, NTSC(60), 60); |
| 83 } |
| 84 |
| 85 TEST(VideoCadenceEstimatorTest, CadenceVariesWithAcceptableDrift) { |
| 86 VideoCadenceEstimator estimator( |
| 87 base::TimeDelta::FromSeconds(kMinimumAcceptableTimeBetweenGlitchesSecs)); |
| 88 estimator.disable_cadence_hysteresis_for_testing(); |
| 89 |
| 90 const base::TimeDelta render_interval = Interval(NTSC(60)); |
| 91 const base::TimeDelta frame_interval = Interval(120); |
| 92 |
| 93 base::TimeDelta acceptable_drift = frame_interval / 2; |
| 94 bool found_cadence = estimator.UpdateCadenceEstimate( |
| 95 render_interval, frame_interval, acceptable_drift); |
| 96 EXPECT_FALSE(found_cadence); |
| 97 EXPECT_FALSE(estimator.has_cadence()); |
| 98 |
| 99 // Increasing the acceptable drift should be result in more permissive |
| 100 // detection of cadence. |
| 101 acceptable_drift = render_interval; |
| 102 found_cadence = estimator.UpdateCadenceEstimate( |
| 103 render_interval, frame_interval, acceptable_drift); |
| 104 EXPECT_TRUE(found_cadence); |
| 105 EXPECT_TRUE(estimator.has_cadence()); |
| 106 EXPECT_EQ(2, estimator.get_cadence_for_testing()); |
| 107 } |
| 108 |
| 109 TEST(VideoCadenceEstimatorTest, CadenceVariesWithAcceptableGlitchTime) { |
| 110 scoped_ptr<VideoCadenceEstimator> estimator(new VideoCadenceEstimator( |
| 111 base::TimeDelta::FromSeconds(kMinimumAcceptableTimeBetweenGlitchesSecs))); |
| 112 estimator->disable_cadence_hysteresis_for_testing(); |
| 113 |
| 114 const base::TimeDelta render_interval = Interval(NTSC(60)); |
| 115 const base::TimeDelta frame_interval = Interval(120); |
| 116 const base::TimeDelta acceptable_drift = frame_interval / 2; |
| 117 |
| 118 bool found_cadence = estimator->UpdateCadenceEstimate( |
| 119 render_interval, frame_interval, acceptable_drift); |
| 120 EXPECT_FALSE(found_cadence); |
| 121 EXPECT_FALSE(estimator->has_cadence()); |
| 122 |
| 123 // Decreasing the acceptable glitch time should be result in more permissive |
| 124 // detection of cadence. |
| 125 estimator.reset(new VideoCadenceEstimator(base::TimeDelta::FromSeconds( |
| 126 kMinimumAcceptableTimeBetweenGlitchesSecs / 2))); |
| 127 estimator->disable_cadence_hysteresis_for_testing(); |
| 128 found_cadence = estimator->UpdateCadenceEstimate( |
| 129 render_interval, frame_interval, acceptable_drift); |
| 130 EXPECT_TRUE(found_cadence); |
| 131 EXPECT_TRUE(estimator->has_cadence()); |
| 132 EXPECT_EQ(2, estimator->get_cadence_for_testing()); |
| 133 } |
| 134 |
| 135 } // namespace media |
OLD | NEW |