keyframe_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <climits>
  11. #include <vector>
  12. #include "third_party/googletest/src/include/gtest/gtest.h"
  13. #include "test/codec_factory.h"
  14. #include "test/encode_test_driver.h"
  15. #include "test/i420_video_source.h"
  16. #include "test/util.h"
  17. namespace {
  18. class KeyframeTest
  19. : public ::libvpx_test::EncoderTest,
  20. public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
  21. protected:
  22. KeyframeTest() : EncoderTest(GET_PARAM(0)) {}
  23. virtual ~KeyframeTest() {}
  24. virtual void SetUp() {
  25. InitializeConfig();
  26. SetMode(GET_PARAM(1));
  27. kf_count_ = 0;
  28. kf_count_max_ = INT_MAX;
  29. kf_do_force_kf_ = false;
  30. set_cpu_used_ = 0;
  31. }
  32. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
  33. ::libvpx_test::Encoder *encoder) {
  34. if (kf_do_force_kf_) {
  35. frame_flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
  36. }
  37. if (set_cpu_used_ && video->frame() == 1) {
  38. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  39. }
  40. }
  41. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  42. if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) {
  43. kf_pts_list_.push_back(pkt->data.frame.pts);
  44. kf_count_++;
  45. abort_ |= kf_count_ > kf_count_max_;
  46. }
  47. }
  48. bool kf_do_force_kf_;
  49. int kf_count_;
  50. int kf_count_max_;
  51. std::vector<vpx_codec_pts_t> kf_pts_list_;
  52. int set_cpu_used_;
  53. };
  54. TEST_P(KeyframeTest, TestRandomVideoSource) {
  55. // Validate that encoding the RandomVideoSource produces multiple keyframes.
  56. // This validates the results of the TestDisableKeyframes test.
  57. kf_count_max_ = 2; // early exit successful tests.
  58. ::libvpx_test::RandomVideoSource video;
  59. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  60. // In realtime mode - auto placed keyframes are exceedingly rare, don't
  61. // bother with this check if(GetParam() > 0)
  62. if (GET_PARAM(1) > 0) EXPECT_GT(kf_count_, 1);
  63. }
  64. TEST_P(KeyframeTest, TestDisableKeyframes) {
  65. cfg_.kf_mode = VPX_KF_DISABLED;
  66. kf_count_max_ = 1; // early exit failed tests.
  67. ::libvpx_test::RandomVideoSource video;
  68. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  69. EXPECT_EQ(1, kf_count_);
  70. }
  71. TEST_P(KeyframeTest, TestForceKeyframe) {
  72. cfg_.kf_mode = VPX_KF_DISABLED;
  73. kf_do_force_kf_ = true;
  74. ::libvpx_test::DummyVideoSource video;
  75. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  76. // verify that every third frame is a keyframe.
  77. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  78. iter != kf_pts_list_.end(); ++iter) {
  79. ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
  80. }
  81. }
  82. TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
  83. cfg_.kf_max_dist = 25;
  84. ::libvpx_test::DummyVideoSource video;
  85. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  86. // verify that keyframe interval matches kf_max_dist
  87. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  88. iter != kf_pts_list_.end(); ++iter) {
  89. ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
  90. }
  91. }
  92. TEST_P(KeyframeTest, TestAutoKeyframe) {
  93. cfg_.kf_mode = VPX_KF_AUTO;
  94. kf_do_force_kf_ = false;
  95. // Force a deterministic speed step in Real Time mode, as the faster modes
  96. // may not produce a keyframe like we expect. This is necessary when running
  97. // on very slow environments (like Valgrind). The step -11 was determined
  98. // experimentally as the fastest mode that still throws the keyframe.
  99. if (deadline_ == VPX_DL_REALTIME) set_cpu_used_ = -11;
  100. // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
  101. // I check only the first 40 frames to make sure there's a keyframe at frame
  102. // 0 and 30.
  103. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  104. 30, 1, 0, 40);
  105. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  106. // In realtime mode - auto placed keyframes are exceedingly rare, don't
  107. // bother with this check
  108. if (GET_PARAM(1) > 0)
  109. EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
  110. // Verify that keyframes match the file keyframes in the file.
  111. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  112. iter != kf_pts_list_.end(); ++iter) {
  113. if (deadline_ == VPX_DL_REALTIME && *iter > 0)
  114. EXPECT_EQ(0, (*iter - 1) % 30)
  115. << "Unexpected keyframe at frame " << *iter;
  116. else
  117. EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
  118. }
  119. }
  120. VP8_INSTANTIATE_TEST_CASE(KeyframeTest, ALL_TEST_MODES);
  121. } // namespace