xref: /aosp_15_r20/external/libaom/test/gf_pyr_height_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <ostream>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "test/i420_video_source.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker namespace {
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker static const struct GFPyrHeightTestParam {
23*77c1e3ccSAndroid Build Coastguard Worker   int gf_min_pyr_height;
24*77c1e3ccSAndroid Build Coastguard Worker   int gf_max_pyr_height;
25*77c1e3ccSAndroid Build Coastguard Worker   double psnr_thresh;
26*77c1e3ccSAndroid Build Coastguard Worker } kTestParams[] = {
27*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 0
28*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 32.30 },
29*77c1e3ccSAndroid Build Coastguard Worker   { 0, 1, 33.90 },
30*77c1e3ccSAndroid Build Coastguard Worker   { 0, 2, 34.00 },
31*77c1e3ccSAndroid Build Coastguard Worker   { 0, 3, 34.20 },
32*77c1e3ccSAndroid Build Coastguard Worker   { 0, 4, 34.30 },
33*77c1e3ccSAndroid Build Coastguard Worker   { 0, 5, 34.35 },
34*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 1
35*77c1e3ccSAndroid Build Coastguard Worker   { 1, 1, 33.90 },
36*77c1e3ccSAndroid Build Coastguard Worker   { 1, 2, 34.00 },
37*77c1e3ccSAndroid Build Coastguard Worker   { 1, 3, 34.20 },
38*77c1e3ccSAndroid Build Coastguard Worker   { 1, 4, 34.30 },
39*77c1e3ccSAndroid Build Coastguard Worker   { 1, 5, 34.35 },
40*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 2
41*77c1e3ccSAndroid Build Coastguard Worker   { 2, 2, 34.00 },
42*77c1e3ccSAndroid Build Coastguard Worker   { 2, 3, 34.20 },
43*77c1e3ccSAndroid Build Coastguard Worker   { 2, 4, 34.30 },
44*77c1e3ccSAndroid Build Coastguard Worker   { 2, 5, 34.35 },
45*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 3
46*77c1e3ccSAndroid Build Coastguard Worker   { 3, 3, 34.20 },
47*77c1e3ccSAndroid Build Coastguard Worker   { 3, 4, 34.30 },
48*77c1e3ccSAndroid Build Coastguard Worker   { 3, 5, 34.35 },
49*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 4
50*77c1e3ccSAndroid Build Coastguard Worker   { 4, 4, 34.30 },
51*77c1e3ccSAndroid Build Coastguard Worker   { 4, 5, 34.35 },
52*77c1e3ccSAndroid Build Coastguard Worker   // gf_min_pyr_height = 5
53*77c1e3ccSAndroid Build Coastguard Worker   { 5, 5, 34.35 },
54*77c1e3ccSAndroid Build Coastguard Worker };
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker // Compiler may decide to add some padding to the struct above for alignment,
57*77c1e3ccSAndroid Build Coastguard Worker // which the gtest may try to print (on error for example). This would cause
58*77c1e3ccSAndroid Build Coastguard Worker // valgrind to complain that the padding is uninitialized. To avoid that, we
59*77c1e3ccSAndroid Build Coastguard Worker // provide our own function to print the struct.
60*77c1e3ccSAndroid Build Coastguard Worker // This also makes '--gtest_list_tests' output more understandable.
operator <<(std::ostream & os,const GFPyrHeightTestParam & p)61*77c1e3ccSAndroid Build Coastguard Worker std::ostream &operator<<(std::ostream &os, const GFPyrHeightTestParam &p) {
62*77c1e3ccSAndroid Build Coastguard Worker   os << "GFPyrHeightTestParam { "
63*77c1e3ccSAndroid Build Coastguard Worker      << "gf_min_pyr_height = " << p.gf_min_pyr_height << ", "
64*77c1e3ccSAndroid Build Coastguard Worker      << "gf_max_pyr_height = " << p.gf_max_pyr_height << ", "
65*77c1e3ccSAndroid Build Coastguard Worker      << "psnr_thresh = " << p.psnr_thresh << " }";
66*77c1e3ccSAndroid Build Coastguard Worker   return os;
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker 
69*77c1e3ccSAndroid Build Coastguard Worker // Params: encoding mode, rate control mode and GFPyrHeightTestParam object.
70*77c1e3ccSAndroid Build Coastguard Worker class GFPyrHeightTest
71*77c1e3ccSAndroid Build Coastguard Worker     : public ::libaom_test::CodecTestWith3Params<
72*77c1e3ccSAndroid Build Coastguard Worker           libaom_test::TestMode, aom_rc_mode, GFPyrHeightTestParam>,
73*77c1e3ccSAndroid Build Coastguard Worker       public ::libaom_test::EncoderTest {
74*77c1e3ccSAndroid Build Coastguard Worker  protected:
GFPyrHeightTest()75*77c1e3ccSAndroid Build Coastguard Worker   GFPyrHeightTest()
76*77c1e3ccSAndroid Build Coastguard Worker       : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
77*77c1e3ccSAndroid Build Coastguard Worker         rc_mode_(GET_PARAM(2)) {
78*77c1e3ccSAndroid Build Coastguard Worker     gf_min_pyr_height_ = GET_PARAM(3).gf_min_pyr_height;
79*77c1e3ccSAndroid Build Coastguard Worker     gf_max_pyr_height_ = GET_PARAM(3).gf_max_pyr_height;
80*77c1e3ccSAndroid Build Coastguard Worker     psnr_threshold_ = GET_PARAM(3).psnr_thresh;
81*77c1e3ccSAndroid Build Coastguard Worker   }
82*77c1e3ccSAndroid Build Coastguard Worker   ~GFPyrHeightTest() override = default;
83*77c1e3ccSAndroid Build Coastguard Worker 
SetUp()84*77c1e3ccSAndroid Build Coastguard Worker   void SetUp() override {
85*77c1e3ccSAndroid Build Coastguard Worker     InitializeConfig(encoding_mode_);
86*77c1e3ccSAndroid Build Coastguard Worker     const aom_rational timebase = { 1, 30 };
87*77c1e3ccSAndroid Build Coastguard Worker     cfg_.g_timebase = timebase;
88*77c1e3ccSAndroid Build Coastguard Worker     cpu_used_ = 4;
89*77c1e3ccSAndroid Build Coastguard Worker     cfg_.rc_end_usage = rc_mode_;
90*77c1e3ccSAndroid Build Coastguard Worker     if (rc_mode_ == AOM_VBR) {
91*77c1e3ccSAndroid Build Coastguard Worker       cfg_.rc_target_bitrate = 200;
92*77c1e3ccSAndroid Build Coastguard Worker     }
93*77c1e3ccSAndroid Build Coastguard Worker     cfg_.g_lag_in_frames = 19;
94*77c1e3ccSAndroid Build Coastguard Worker     cfg_.g_threads = 0;
95*77c1e3ccSAndroid Build Coastguard Worker     init_flags_ = AOM_CODEC_USE_PSNR;
96*77c1e3ccSAndroid Build Coastguard Worker   }
97*77c1e3ccSAndroid Build Coastguard Worker 
BeginPassHook(unsigned int)98*77c1e3ccSAndroid Build Coastguard Worker   void BeginPassHook(unsigned int) override {
99*77c1e3ccSAndroid Build Coastguard Worker     psnr_ = 0.0;
100*77c1e3ccSAndroid Build Coastguard Worker     nframes_ = 0;
101*77c1e3ccSAndroid Build Coastguard Worker   }
102*77c1e3ccSAndroid Build Coastguard Worker 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)103*77c1e3ccSAndroid Build Coastguard Worker   void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
104*77c1e3ccSAndroid Build Coastguard Worker     psnr_ += pkt->data.psnr.psnr[0];
105*77c1e3ccSAndroid Build Coastguard Worker     nframes_++;
106*77c1e3ccSAndroid Build Coastguard Worker   }
107*77c1e3ccSAndroid Build Coastguard Worker 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)108*77c1e3ccSAndroid Build Coastguard Worker   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
109*77c1e3ccSAndroid Build Coastguard Worker                           ::libaom_test::Encoder *encoder) override {
110*77c1e3ccSAndroid Build Coastguard Worker     if (video->frame() == 0) {
111*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
112*77c1e3ccSAndroid Build Coastguard Worker       if (rc_mode_ == AOM_Q) {
113*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_CQ_LEVEL, 32);
114*77c1e3ccSAndroid Build Coastguard Worker       }
115*77c1e3ccSAndroid Build Coastguard Worker       if (encoding_mode_ != ::libaom_test::kRealTime) {
116*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
117*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
118*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
119*77c1e3ccSAndroid Build Coastguard Worker       }
120*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AV1E_SET_GF_MIN_PYRAMID_HEIGHT, gf_min_pyr_height_);
121*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AV1E_SET_GF_MAX_PYRAMID_HEIGHT, gf_max_pyr_height_);
122*77c1e3ccSAndroid Build Coastguard Worker     }
123*77c1e3ccSAndroid Build Coastguard Worker   }
124*77c1e3ccSAndroid Build Coastguard Worker 
GetAveragePsnr() const125*77c1e3ccSAndroid Build Coastguard Worker   double GetAveragePsnr() const {
126*77c1e3ccSAndroid Build Coastguard Worker     if (nframes_) return psnr_ / nframes_;
127*77c1e3ccSAndroid Build Coastguard Worker     return 0.0;
128*77c1e3ccSAndroid Build Coastguard Worker   }
129*77c1e3ccSAndroid Build Coastguard Worker 
GetPsnrThreshold()130*77c1e3ccSAndroid Build Coastguard Worker   double GetPsnrThreshold() { return psnr_threshold_; }
131*77c1e3ccSAndroid Build Coastguard Worker 
132*77c1e3ccSAndroid Build Coastguard Worker   ::libaom_test::TestMode encoding_mode_;
133*77c1e3ccSAndroid Build Coastguard Worker   aom_rc_mode rc_mode_;
134*77c1e3ccSAndroid Build Coastguard Worker   double psnr_threshold_;
135*77c1e3ccSAndroid Build Coastguard Worker   int gf_min_pyr_height_;
136*77c1e3ccSAndroid Build Coastguard Worker   int gf_max_pyr_height_;
137*77c1e3ccSAndroid Build Coastguard Worker   int cpu_used_;
138*77c1e3ccSAndroid Build Coastguard Worker   int nframes_;
139*77c1e3ccSAndroid Build Coastguard Worker   double psnr_;
140*77c1e3ccSAndroid Build Coastguard Worker };
141*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(GFPyrHeightTest,EncodeAndVerifyPSNR)142*77c1e3ccSAndroid Build Coastguard Worker TEST_P(GFPyrHeightTest, EncodeAndVerifyPSNR) {
143*77c1e3ccSAndroid Build Coastguard Worker   libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
144*77c1e3ccSAndroid Build Coastguard Worker                                      cfg_.g_timebase.den, cfg_.g_timebase.num,
145*77c1e3ccSAndroid Build Coastguard Worker                                      0, 32);
146*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
147*77c1e3ccSAndroid Build Coastguard Worker   EXPECT_GT(GetAveragePsnr(), GetPsnrThreshold())
148*77c1e3ccSAndroid Build Coastguard Worker       << "GF Min Pyramid Height = " << gf_min_pyr_height_ << ", "
149*77c1e3ccSAndroid Build Coastguard Worker       << "GF Max Pyramid Height = " << gf_max_pyr_height_;
150*77c1e3ccSAndroid Build Coastguard Worker }
151*77c1e3ccSAndroid Build Coastguard Worker 
152*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(GFPyrHeightTest, NONREALTIME_TEST_MODES,
153*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(AOM_Q, AOM_VBR),
154*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::ValuesIn(kTestParams));
155*77c1e3ccSAndroid Build Coastguard Worker }  // namespace
156