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