keyframe_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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() == 0) {
  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) {
  63. EXPECT_GT(kf_count_, 1);
  64. }
  65. }
  66. TEST_P(KeyframeTest, TestDisableKeyframes) {
  67. cfg_.kf_mode = VPX_KF_DISABLED;
  68. kf_count_max_ = 1; // early exit failed tests.
  69. ::libvpx_test::RandomVideoSource video;
  70. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  71. EXPECT_EQ(1, kf_count_);
  72. }
  73. TEST_P(KeyframeTest, TestForceKeyframe) {
  74. cfg_.kf_mode = VPX_KF_DISABLED;
  75. kf_do_force_kf_ = true;
  76. ::libvpx_test::DummyVideoSource video;
  77. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  78. // verify that every third frame is a keyframe.
  79. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  80. iter != kf_pts_list_.end(); ++iter) {
  81. ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
  82. }
  83. }
  84. TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
  85. cfg_.kf_max_dist = 25;
  86. ::libvpx_test::DummyVideoSource video;
  87. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  88. // verify that keyframe interval matches kf_max_dist
  89. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  90. iter != kf_pts_list_.end(); ++iter) {
  91. ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
  92. }
  93. }
  94. TEST_P(KeyframeTest, TestAutoKeyframe) {
  95. cfg_.kf_mode = VPX_KF_AUTO;
  96. kf_do_force_kf_ = false;
  97. // Force a deterministic speed step in Real Time mode, as the faster modes
  98. // may not produce a keyframe like we expect. This is necessary when running
  99. // on very slow environments (like Valgrind). The step -11 was determined
  100. // experimentally as the fastest mode that still throws the keyframe.
  101. if (deadline_ == VPX_DL_REALTIME) set_cpu_used_ = -11;
  102. // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
  103. // I check only the first 40 frames to make sure there's a keyframe at frame
  104. // 0 and 30.
  105. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  106. 30, 1, 0, 40);
  107. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  108. // In realtime mode - auto placed keyframes are exceedingly rare, don't
  109. // bother with this check
  110. if (GET_PARAM(1) > 0) {
  111. EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
  112. }
  113. // Verify that keyframes match the file keyframes in the file.
  114. for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
  115. iter != kf_pts_list_.end(); ++iter) {
  116. if (deadline_ == VPX_DL_REALTIME && *iter > 0)
  117. EXPECT_EQ(0, (*iter - 1) % 30)
  118. << "Unexpected keyframe at frame " << *iter;
  119. else
  120. EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
  121. }
  122. }
  123. VP8_INSTANTIATE_TEST_CASE(KeyframeTest, ALL_TEST_MODES);
  124. } // namespace