]> granicus.if.org Git - libvpx/blobdiff - test/keyframe_test.cc
test: apply clang-format
[libvpx] / test / keyframe_test.cc
index db18e5dab5140c2f4764503d98653e2de218e04b..03cdedc1a56e096e0c952f8acf0d7acc7899f9c5 100644 (file)
@@ -9,30 +9,36 @@
  */
 #include <climits>
 #include <vector>
-#include "test/encode_test_driver.h"
-#include "test/video_source.h"
 #include "third_party/googletest/src/include/gtest/gtest.h"
+#include "test/codec_factory.h"
+#include "test/encode_test_driver.h"
+#include "test/i420_video_source.h"
+#include "test/util.h"
 
 namespace {
 
-class KeyframeTest : public ::libvpx_test::EncoderTest,
-  public ::testing::TestWithParam<enum libvpx_test::TestMode> {
+class KeyframeTest
+    : public ::libvpx_test::EncoderTest,
+      public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
  protected:
+  KeyframeTest() : EncoderTest(GET_PARAM(0)) {}
+  virtual ~KeyframeTest() {}
+
   virtual void SetUp() {
     InitializeConfig();
-    SetMode(GetParam());
+    SetMode(GET_PARAM(1));
     kf_count_ = 0;
     kf_count_max_ = INT_MAX;
     kf_do_force_kf_ = false;
+    set_cpu_used_ = 0;
   }
 
-  virtual bool Continue() {
-    return !HasFatalFailure() && !abort_;
-  }
-
-  virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video) {
+  virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
+                                  ::libvpx_test::Encoder *encoder) {
     if (kf_do_force_kf_)
-      flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
+      frame_flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
+    if (set_cpu_used_ && video->frame() == 1)
+      encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
   }
 
   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
@@ -46,7 +52,8 @@ class KeyframeTest : public ::libvpx_test::EncoderTest,
   bool kf_do_force_kf_;
   int kf_count_;
   int kf_count_max_;
-  std::vector< vpx_codec_pts_t > kf_pts_list_;
+  std::vector<vpx_codec_pts_t> kf_pts_list_;
+  int set_cpu_used_;
 };
 
 TEST_P(KeyframeTest, TestRandomVideoSource) {
@@ -57,7 +64,9 @@ TEST_P(KeyframeTest, TestRandomVideoSource) {
   ::libvpx_test::RandomVideoSource video;
   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
 
-  EXPECT_GT(kf_count_, 1);
+  // In realtime mode - auto placed keyframes are exceedingly rare,  don't
+  // bother with this check   if(GetParam() > 0)
+  if (GET_PARAM(1) > 0) EXPECT_GT(kf_count_, 1);
 }
 
 TEST_P(KeyframeTest, TestDisableKeyframes) {
@@ -78,9 +87,8 @@ TEST_P(KeyframeTest, TestForceKeyframe) {
   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
 
   // verify that every third frame is a keyframe.
-  for (std::vector<vpx_codec_pts_t>::iterator iter = kf_pts_list_.begin();
-       iter != kf_pts_list_.end();
-       ++iter) {
+  for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
+       iter != kf_pts_list_.end(); ++iter) {
     ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
   }
 }
@@ -92,12 +100,45 @@ TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
 
   // verify that keyframe interval matches kf_max_dist
-  for (std::vector<vpx_codec_pts_t>::iterator iter = kf_pts_list_.begin();
-       iter != kf_pts_list_.end();
-       iter++) {
+  for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
+       iter != kf_pts_list_.end(); ++iter) {
     ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
   }
 }
 
-INSTANTIATE_TEST_CASE_P(AllModes, KeyframeTest, ALL_TEST_MODES);
+TEST_P(KeyframeTest, TestAutoKeyframe) {
+  cfg_.kf_mode = VPX_KF_AUTO;
+  kf_do_force_kf_ = false;
+
+  // Force a deterministic speed step in Real Time mode, as the faster modes
+  // may not produce a keyframe like we expect. This is necessary when running
+  // on very slow environments (like Valgrind). The step -11 was determined
+  // experimentally as the fastest mode that still throws the keyframe.
+  if (deadline_ == VPX_DL_REALTIME) set_cpu_used_ = -11;
+
+  // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
+  // I check only the first 40 frames to make sure there's a keyframe at frame
+  // 0 and 30.
+  ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
+                                       30, 1, 0, 40);
+
+  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
+
+  // In realtime mode - auto placed keyframes are exceedingly rare,  don't
+  // bother with this check
+  if (GET_PARAM(1) > 0)
+    EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
+
+  // Verify that keyframes match the file keyframes in the file.
+  for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
+       iter != kf_pts_list_.end(); ++iter) {
+    if (deadline_ == VPX_DL_REALTIME && *iter > 0)
+      EXPECT_EQ(0, (*iter - 1) % 30) << "Unexpected keyframe at frame "
+                                     << *iter;
+    else
+      EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
+  }
+}
+
+VP8_INSTANTIATE_TEST_CASE(KeyframeTest, ALL_TEST_MODES);
 }  // namespace