Merge "Removed unused dr from VP8D_COMP"
[platform/upstream/libvpx.git] / 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 "test/encode_test_driver.h"
13 #include "test/i420_video_source.h"
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15
16 namespace {
17
18 class KeyframeTest : public ::libvpx_test::EncoderTest,
19     public ::testing::TestWithParam<enum libvpx_test::TestMode> {
20  protected:
21   virtual void SetUp() {
22     InitializeConfig();
23     SetMode(GetParam());
24     kf_count_ = 0;
25     kf_count_max_ = INT_MAX;
26     kf_do_force_kf_ = false;
27     set_cpu_used_ = 0;
28   }
29
30   virtual bool Continue() const {
31     return !HasFatalFailure() && !abort_;
32   }
33
34   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
35                                   ::libvpx_test::Encoder *encoder) {
36     if (kf_do_force_kf_)
37       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_);
40   }
41
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);
45       kf_count_++;
46       abort_ |= kf_count_ > kf_count_max_;
47     }
48   }
49
50   bool kf_do_force_kf_;
51   int kf_count_;
52   int kf_count_max_;
53   std::vector<vpx_codec_pts_t> kf_pts_list_;
54   int set_cpu_used_;
55 };
56
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.
61
62   ::libvpx_test::RandomVideoSource video;
63   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
64
65   EXPECT_GT(kf_count_, 1);
66 }
67
68 TEST_P(KeyframeTest, TestDisableKeyframes) {
69   cfg_.kf_mode = VPX_KF_DISABLED;
70   kf_count_max_ = 1;  // early exit failed tests.
71
72   ::libvpx_test::RandomVideoSource video;
73   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
74
75   EXPECT_EQ(1, kf_count_);
76 }
77
78 TEST_P(KeyframeTest, TestForceKeyframe) {
79   cfg_.kf_mode = VPX_KF_DISABLED;
80   kf_do_force_kf_ = true;
81
82   ::libvpx_test::DummyVideoSource video;
83   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
84
85   // verify that every third frame is a keyframe.
86   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
87        iter != kf_pts_list_.end(); ++iter) {
88     ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
89   }
90 }
91
92 TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
93   cfg_.kf_max_dist = 25;
94
95   ::libvpx_test::DummyVideoSource video;
96   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
97
98   // verify that keyframe interval matches kf_max_dist
99   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
100        iter != kf_pts_list_.end(); ++iter) {
101     ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
102   }
103 }
104
105 TEST_P(KeyframeTest, TestAutoKeyframe) {
106   cfg_.kf_mode = VPX_KF_AUTO;
107   kf_do_force_kf_ = false;
108
109   // Force a deterministic speed step in Real Time mode, as the faster modes
110   // may not produce a keyframe like we expect. This is necessary when running
111   // on very slow environments (like Valgrind). The step -11 was determined
112   // experimentally as the fastest mode that still throws the keyframe.
113   if (deadline_ == VPX_DL_REALTIME)
114     set_cpu_used_ = -11;
115
116   // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
117   // I check only the first 40 frames to make sure there's a keyframe at frame
118   // 0 and 30.
119   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
120                                        30, 1, 0, 40);
121
122   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
123
124   EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
125
126   // Verify that keyframes match the file keyframes in the file.
127   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
128        iter != kf_pts_list_.end(); ++iter) {
129
130     if (deadline_ == VPX_DL_REALTIME && *iter > 0)
131       EXPECT_EQ(0, (*iter - 1) % 30) << "Unexpected keyframe at frame "
132         << *iter;
133     else
134       EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
135   }
136 }
137
138 INSTANTIATE_TEST_CASE_P(AllModes, KeyframeTest, ALL_TEST_MODES);
139 }  // namespace