2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
12 #include "test/encode_test_driver.h"
13 #include "test/i420_video_source.h"
14 #include "third_party/googletest/src/include/gtest/gtest.h"
18 class KeyframeTest : public ::libvpx_test::EncoderTest,
19 public ::testing::TestWithParam<enum libvpx_test::TestMode> {
21 virtual void SetUp() {
25 kf_count_max_ = INT_MAX;
26 kf_do_force_kf_ = false;
30 virtual bool Continue() const {
31 return !HasFatalFailure() && !abort_;
34 virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
35 ::libvpx_test::Encoder *encoder) {
37 frame_flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
38 if (set_cpu_used_ && video->frame() == 1)
39 encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
42 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
43 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) {
44 kf_pts_list_.push_back(pkt->data.frame.pts);
46 abort_ |= kf_count_ > kf_count_max_;
53 std::vector<vpx_codec_pts_t> kf_pts_list_;
57 TEST_P(KeyframeTest, TestRandomVideoSource) {
58 // Validate that encoding the RandomVideoSource produces multiple keyframes.
59 // This validates the results of the TestDisableKeyframes test.
60 kf_count_max_ = 2; // early exit successful tests.
62 ::libvpx_test::RandomVideoSource video;
63 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
65 // In realtime mode - auto placed keyframes are exceedingly rare, don't
66 // bother with this check if(GetParam() > 0)
68 EXPECT_GT(kf_count_, 1);
71 TEST_P(KeyframeTest, TestDisableKeyframes) {
72 cfg_.kf_mode = VPX_KF_DISABLED;
73 kf_count_max_ = 1; // early exit failed tests.
75 ::libvpx_test::RandomVideoSource video;
76 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
78 EXPECT_EQ(1, kf_count_);
81 TEST_P(KeyframeTest, TestForceKeyframe) {
82 cfg_.kf_mode = VPX_KF_DISABLED;
83 kf_do_force_kf_ = true;
85 ::libvpx_test::DummyVideoSource video;
86 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
88 // verify that every third frame is a keyframe.
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 % 3) << "Unexpected keyframe at frame " << *iter;
95 TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
96 cfg_.kf_max_dist = 25;
98 ::libvpx_test::DummyVideoSource video;
99 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
101 // verify that keyframe interval matches kf_max_dist
102 for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
103 iter != kf_pts_list_.end(); ++iter) {
104 ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
108 TEST_P(KeyframeTest, TestAutoKeyframe) {
109 cfg_.kf_mode = VPX_KF_AUTO;
110 kf_do_force_kf_ = false;
112 // Force a deterministic speed step in Real Time mode, as the faster modes
113 // may not produce a keyframe like we expect. This is necessary when running
114 // on very slow environments (like Valgrind). The step -11 was determined
115 // experimentally as the fastest mode that still throws the keyframe.
116 if (deadline_ == VPX_DL_REALTIME)
119 // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
120 // I check only the first 40 frames to make sure there's a keyframe at frame
122 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
125 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
127 // In realtime mode - auto placed keyframes are exceedingly rare, don't
128 // bother with this check
130 EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
132 // Verify that keyframes match the file keyframes in the file.
133 for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
134 iter != kf_pts_list_.end(); ++iter) {
136 if (deadline_ == VPX_DL_REALTIME && *iter > 0)
137 EXPECT_EQ(0, (*iter - 1) % 30) << "Unexpected keyframe at frame "
140 EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
144 INSTANTIATE_TEST_CASE_P(AllModes, KeyframeTest, ALL_TEST_MODES);