]> granicus.if.org Git - libvpx/blob - test/keyframe_test.cc
Merge "Fix decimation_count in drop_frame"
[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 "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   }
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>::const_iterator iter = kf_pts_list_.begin();
82        iter != kf_pts_list_.end(); ++iter) {
83     ASSERT_EQ(0, *iter % 3) << "Unexpected keyframe at frame " << *iter;
84   }
85 }
86
87 TEST_P(KeyframeTest, TestKeyframeMaxDistance) {
88   cfg_.kf_max_dist = 25;
89
90   ::libvpx_test::DummyVideoSource video;
91   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
92
93   // verify that keyframe interval matches kf_max_dist
94   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
95        iter != kf_pts_list_.end(); ++iter) {
96     ASSERT_EQ(0, *iter % 25) << "Unexpected keyframe at frame " << *iter;
97   }
98 }
99
100 TEST_P(KeyframeTest, TestAutoKeyframe) {
101   cfg_.kf_mode = VPX_KF_AUTO;
102   kf_do_force_kf_ = false;
103
104   // This clip has a cut scene every 30 frames -> Frame 0, 30, 60, 90, 120.
105   // I check only the first 40 frames to make sure there's a keyframe at frame
106   // 0 and 30.
107   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
108                                        30, 1, 0, 40);
109
110   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
111
112   EXPECT_EQ(2u, kf_pts_list_.size()) << " Not the right number of keyframes ";
113
114   // Verify that keyframes match the file keyframes in the file.
115   for (std::vector<vpx_codec_pts_t>::const_iterator iter = kf_pts_list_.begin();
116        iter != kf_pts_list_.end(); ++iter) {
117
118     if (deadline_ == VPX_DL_REALTIME && *iter > 0)
119       EXPECT_EQ(0, (*iter - 1) % 30) << "Unexpected keyframe at frame "
120         << *iter;
121     else
122       EXPECT_EQ(0, *iter % 30) << "Unexpected keyframe at frame " << *iter;
123   }
124 }
125
126 INSTANTIATE_TEST_CASE_P(AllModes, KeyframeTest, ALL_TEST_MODES);
127 }  // namespace