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