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 "third_party/googletest/src/include/gtest/gtest.h"
11 #include "test/codec_factory.h"
12 #include "test/encode_test_driver.h"
13 #include "test/i420_video_source.h"
14 #include "test/util.h"
15 namespace {
16 
17 #if CONFIG_VP8_ENCODER
18 
19 // lookahead range: [kLookAheadMin, kLookAheadMax).
20 const int kLookAheadMin = 5;
21 const int kLookAheadMax = 26;
22 
23 class AltRefTest : public ::libvpx_test::EncoderTest,
24                    public ::libvpx_test::CodecTestWithParam<int> {
25  protected:
AltRefTest()26   AltRefTest() : EncoderTest(GET_PARAM(0)), altref_count_(0) {}
~AltRefTest()27   virtual ~AltRefTest() {}
28 
SetUp()29   virtual void SetUp() {
30     InitializeConfig();
31     SetMode(libvpx_test::kTwoPassGood);
32   }
33 
BeginPassHook(unsigned int)34   virtual void BeginPassHook(unsigned int /*pass*/) { altref_count_ = 0; }
35 
PreEncodeFrameHook(libvpx_test::VideoSource * video,libvpx_test::Encoder * encoder)36   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
37                                   libvpx_test::Encoder *encoder) {
38     if (video->frame() == 0) {
39       encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
40       encoder->Control(VP8E_SET_CPUUSED, 3);
41     }
42   }
43 
FramePktHook(const vpx_codec_cx_pkt_t * pkt)44   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
45     if (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE) ++altref_count_;
46   }
47 
altref_count() const48   int altref_count() const { return altref_count_; }
49 
50  private:
51   int altref_count_;
52 };
53 
TEST_P(AltRefTest,MonotonicTimestamps)54 TEST_P(AltRefTest, MonotonicTimestamps) {
55   const vpx_rational timebase = { 33333333, 1000000000 };
56   cfg_.g_timebase = timebase;
57   cfg_.rc_target_bitrate = 1000;
58   cfg_.g_lag_in_frames = GET_PARAM(1);
59 
60   libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
61                                      timebase.den, timebase.num, 0, 30);
62   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
63   EXPECT_GE(altref_count(), 1);
64 }
65 
66 VP8_INSTANTIATE_TEST_SUITE(AltRefTest,
67                            ::testing::Range(kLookAheadMin, kLookAheadMax));
68 
69 #endif  // CONFIG_VP8_ENCODER
70 
71 class AltRefForcedKeyTestLarge
72     : public ::libvpx_test::EncoderTest,
73       public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
74  protected:
AltRefForcedKeyTestLarge()75   AltRefForcedKeyTestLarge()
76       : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
77         cpu_used_(GET_PARAM(2)), forced_kf_frame_num_(1), frame_num_(0) {}
~AltRefForcedKeyTestLarge()78   virtual ~AltRefForcedKeyTestLarge() {}
79 
SetUp()80   virtual void SetUp() {
81     InitializeConfig();
82     SetMode(encoding_mode_);
83     cfg_.rc_end_usage = VPX_VBR;
84     cfg_.g_threads = 0;
85   }
86 
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)87   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
88                                   ::libvpx_test::Encoder *encoder) {
89     if (video->frame() == 0) {
90       encoder->Control(VP8E_SET_CPUUSED, cpu_used_);
91       encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
92 #if CONFIG_VP9_ENCODER
93       // override test default for tile columns if necessary.
94       if (GET_PARAM(0) == &libvpx_test::kVP9) {
95         encoder->Control(VP9E_SET_TILE_COLUMNS, 6);
96       }
97 #endif
98     }
99     frame_flags_ =
100         (video->frame() == forced_kf_frame_num_) ? VPX_EFLAG_FORCE_KF : 0;
101   }
102 
FramePktHook(const vpx_codec_cx_pkt_t * pkt)103   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
104     if (frame_num_ == forced_kf_frame_num_) {
105       ASSERT_TRUE(!!(pkt->data.frame.flags & VPX_FRAME_IS_KEY))
106           << "Frame #" << frame_num_ << " isn't a keyframe!";
107     }
108     ++frame_num_;
109   }
110 
111   ::libvpx_test::TestMode encoding_mode_;
112   int cpu_used_;
113   unsigned int forced_kf_frame_num_;
114   unsigned int frame_num_;
115 };
116 
TEST_P(AltRefForcedKeyTestLarge,Frame1IsKey)117 TEST_P(AltRefForcedKeyTestLarge, Frame1IsKey) {
118   const vpx_rational timebase = { 1, 30 };
119   const int lag_values[] = { 3, 15, 25, -1 };
120 
121   forced_kf_frame_num_ = 1;
122   for (int i = 0; lag_values[i] != -1; ++i) {
123     frame_num_ = 0;
124     cfg_.g_lag_in_frames = lag_values[i];
125     libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
126                                        timebase.den, timebase.num, 0, 30);
127     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
128   }
129 }
130 
TEST_P(AltRefForcedKeyTestLarge,ForcedFrameIsKey)131 TEST_P(AltRefForcedKeyTestLarge, ForcedFrameIsKey) {
132   const vpx_rational timebase = { 1, 30 };
133   const int lag_values[] = { 3, 15, 25, -1 };
134 
135   for (int i = 0; lag_values[i] != -1; ++i) {
136     frame_num_ = 0;
137     forced_kf_frame_num_ = lag_values[i] - 1;
138     cfg_.g_lag_in_frames = lag_values[i];
139     libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
140                                        timebase.den, timebase.num, 0, 30);
141     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
142   }
143 }
144 
145 VP8_INSTANTIATE_TEST_SUITE(AltRefForcedKeyTestLarge,
146                            ::testing::Values(::libvpx_test::kOnePassGood),
147                            ::testing::Range(0, 9));
148 
149 VP9_INSTANTIATE_TEST_SUITE(AltRefForcedKeyTestLarge,
150                            ::testing::Values(::libvpx_test::kOnePassGood),
151                            ::testing::Range(0, 9));
152 }  // namespace
153