]> granicus.if.org Git - libvpx/blob - test/keyframe_test.cc
Merge "Support multiple codecs in test infrastructure" into experimental
[libvpx] / test / keyframe_test.cc
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
18 namespace {
19
20 class KeyframeTest : public ::libvpx_test::EncoderTest,
21     public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
22  protected:
23   KeyframeTest() : EncoderTest(GET_PARAM(0)) {}
24
25   virtual void SetUp() {
26     InitializeConfig();
27     SetMode(GET_PARAM(1));
28     kf_count_ = 0;
29     kf_count_max_ = INT_MAX;
30     kf_do_force_kf_ = false;
31     set_cpu_used_ = 0;
32   }
33
34   virtual bool Continue() const {
35     return !HasFatalFailure() && !abort_;
36   }
37
38   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
39                                   ::libvpx_test::Encoder *encoder) {
40     if (kf_do_force_kf_)
41       frame_flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
42     if (set_cpu_used_ && video->frame() == 1)
43       encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
44   }
45
46   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
47     if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) {
48       kf_pts_list_.push_back(pkt->data.frame.pts);
49       kf_count_++;
50       abort_ |= kf_count_ > kf_count_max_;
51     }
52   }
53
54   bool kf_do_force_kf_;
55   int kf_count_;
56   int kf_count_max_;
57   std::vector<vpx_codec_pts_t> kf_pts_list_;
58   int set_cpu_used_;
59 };
60
61 TEST_P(KeyframeTest, TestRandomVideoSource) {
62   // Validate that encoding the RandomVideoSource produces multiple keyframes.
63   // This validates the results of the TestDisableKeyframes test.
64   kf_count_max_ = 2;  // early exit successful tests.
65
66   ::libvpx_test::RandomVideoSource video;
67   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
68
69   // In realtime mode - auto placed keyframes are exceedingly rare,  don't
70   // bother with this check   if(GetParam() > 0)
71   if (GET_PARAM(1) > 0)
72     EXPECT_GT(kf_count_, 1);
73 }
74
75 TEST_P(KeyframeTest, TestDisableKeyframes) {
76   cfg_.kf_mode = VPX_KF_DISABLED;
77   kf_count_max_ = 1;  // early exit failed tests.
78
79   ::libvpx_test::RandomVideoSource video;
80   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
81
82   EXPECT_EQ(1, kf_count_);
83 }
84
85 TEST_P(KeyframeTest, TestForceKeyframe) {
86   cfg_.kf_mode = VPX_KF_DISABLED;
87   kf_do_force_kf_ = true;
88
89   ::libvpx_test::DummyVideoSource video;
90   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
91
92   // verify that every third frame is a keyframe.
93   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
94        iter != kf_pts_list_.end(); ++iter) {
95     ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
96   }
97 }
98
99 TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
100   cfg_.kf_max_dist = 25;
101
102   ::libvpx_test::DummyVideoSource video;
103   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
104
105   // verify that keyframe interval matches kf_max_dist
106   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
107        iter != kf_pts_list_.end(); ++iter) {
108     ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
109   }
110 }
111
112 TEST_P(KeyframeTest, TestAutoKeyframe) {
113   cfg_.kf_mode = VPX_KF_AUTO;
114   kf_do_force_kf_ = false;
115
116   // Force a deterministic speed step in Real Time mode, as the faster modes
117   // may not produce a keyframe like we expect. This is necessary when running
118   // on very slow environments (like Valgrind). The step -11 was determined
119   // experimentally as the fastest mode that still throws the keyframe.
120   if (deadline_ == VPX_DL_REALTIME)
121     set_cpu_used_ = -11;
122
123   // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
124   // I check only the first 40 frames to make sure there's a keyframe at frame
125   // 0 and 30.
126   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
127                                        30, 1, 0, 40);
128
129   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
130
131   // In realtime mode - auto placed keyframes are exceedingly rare,  don't
132   // bother with this check
133   if (GET_PARAM(1) > 0)
134     EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
135
136   // Verify that keyframes match the file keyframes in the file.
137   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
138        iter != kf_pts_list_.end(); ++iter) {
139
140     if (deadline_ == VPX_DL_REALTIME && *iter > 0)
141       EXPECT_EQ(0, (*iter - 1) % 30) << "Unexpected keyframe at frame "
142         << *iter;
143     else
144       EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
145   }
146 }
147
148 VP8_INSTANTIATE_TEST_CASE(KeyframeTest, ALL_TEST_MODES);
149 }  // namespace