Merge changes I38e93fe2,I6d6a0fb6,I51155833,If4c3e5d4,Ia2f40ef2
[profile/ivi/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/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   }
28
29   virtual bool Continue() {
30     return !HasFatalFailure() && !abort_;
31   }
32
33   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video) {
34     if (kf_do_force_kf_)
35       flags_ = (video->frame() % 3) ? 0 : VPX_EFLAG_FORCE_KF;
36   }
37
38   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
39     if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) {
40       kf_pts_list_.push_back(pkt->data.frame.pts);
41       kf_count_++;
42       abort_ |= kf_count_ > kf_count_max_;
43     }
44   }
45
46   bool kf_do_force_kf_;
47   int kf_count_;
48   int kf_count_max_;
49   std::vector< vpx_codec_pts_t > kf_pts_list_;
50 };
51
52 TEST_P(KeyframeTest, TestRandomVideoSource) {
53   // Validate that encoding the RandomVideoSource produces multiple keyframes.
54   // This validates the results of the TestDisableKeyframes test.
55   kf_count_max_ = 2;  // early exit successful tests.
56
57   ::libvpx_test::RandomVideoSource video;
58   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
59
60   EXPECT_GT(kf_count_, 1);
61 }
62
63 TEST_P(KeyframeTest, TestDisableKeyframes) {
64   cfg_.kf_mode = VPX_KF_DISABLED;
65   kf_count_max_ = 1;  // early exit failed tests.
66
67   ::libvpx_test::RandomVideoSource video;
68   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
69
70   EXPECT_EQ(1, kf_count_);
71 }
72
73 TEST_P(KeyframeTest, TestForceKeyframe) {
74   cfg_.kf_mode = VPX_KF_DISABLED;
75   kf_do_force_kf_ = true;
76
77   ::libvpx_test::DummyVideoSource video;
78   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
79
80   // verify that every third frame is a keyframe.
81   for (std::vector<vpx_codec_pts_t>::iterator iter = kf_pts_list_.begin();
82        iter != kf_pts_list_.end();
83        ++iter) {
84     ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
85   }
86 }
87
88 TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
89   cfg_.kf_max_dist = 25;
90
91   ::libvpx_test::DummyVideoSource video;
92   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
93
94   // verify that keyframe interval matches kf_max_dist
95   for (std::vector<vpx_codec_pts_t>::iterator iter = kf_pts_list_.begin();
96        iter != kf_pts_list_.end();
97        iter++) {
98     ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
99   }
100 }
101
102 INSTANTIATE_TEST_CASE_P(AllModes, KeyframeTest, ALL_TEST_MODES);
103 }  // namespace