xref: /aosp_15_r20/external/libaom/test/sharpness_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2021, 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 <unordered_map>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "test/y4m_video_source.h"
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker namespace {
22*77c1e3ccSAndroid Build Coastguard Worker const unsigned int kFrames = 10;
23*77c1e3ccSAndroid Build Coastguard Worker const int kBitrate = 500;
24*77c1e3ccSAndroid Build Coastguard Worker const unsigned int kCqLevel = 18;
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker // List of psnr thresholds for different test combinations
27*77c1e3ccSAndroid Build Coastguard Worker // keys: test-mode, cpu-used, sharpness.
28*77c1e3ccSAndroid Build Coastguard Worker const std::unordered_map<
29*77c1e3ccSAndroid Build Coastguard Worker     int, std::unordered_map<int, std::unordered_map<int, double>>>
30*77c1e3ccSAndroid Build Coastguard Worker     kPsnrThreshold = { { static_cast<int>(::libaom_test::kTwoPassGood),
31*77c1e3ccSAndroid Build Coastguard Worker                          { { 2, { { 2, 37.6 }, { 5, 37.6 } } },
32*77c1e3ccSAndroid Build Coastguard Worker                            { 4, { { 2, 37.5 }, { 5, 37.5 } } },
33*77c1e3ccSAndroid Build Coastguard Worker                            { 6, { { 2, 37.3 }, { 5, 37.3 } } } } },
34*77c1e3ccSAndroid Build Coastguard Worker                        { static_cast<int>(::libaom_test::kAllIntra),
35*77c1e3ccSAndroid Build Coastguard Worker                          { { 3, { { 2, 42.2 }, { 5, 42.2 } } },
36*77c1e3ccSAndroid Build Coastguard Worker                            { 6, { { 2, 41.8 }, { 4, 41.9 }, { 5, 41.9 } } },
37*77c1e3ccSAndroid Build Coastguard Worker                            { 9, { { 2, 41.0 }, { 5, 41.0 } } } } } };
38*77c1e3ccSAndroid Build Coastguard Worker 
39*77c1e3ccSAndroid Build Coastguard Worker // This class is used to test sharpness parameter configured through control
40*77c1e3ccSAndroid Build Coastguard Worker // call using AOME_SET_SHARPNESS for different encoder configurations.
41*77c1e3ccSAndroid Build Coastguard Worker class SharpnessTest
42*77c1e3ccSAndroid Build Coastguard Worker     : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode, int,
43*77c1e3ccSAndroid Build Coastguard Worker                                                  int>,
44*77c1e3ccSAndroid Build Coastguard Worker       public ::libaom_test::EncoderTest {
45*77c1e3ccSAndroid Build Coastguard Worker  protected:
SharpnessTest()46*77c1e3ccSAndroid Build Coastguard Worker   SharpnessTest()
47*77c1e3ccSAndroid Build Coastguard Worker       : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
48*77c1e3ccSAndroid Build Coastguard Worker         cpu_used_(GET_PARAM(2)), sharpness_level_(GET_PARAM(3)), psnr_(0.0),
49*77c1e3ccSAndroid Build Coastguard Worker         nframes_(0) {}
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker   ~SharpnessTest() override = default;
52*77c1e3ccSAndroid Build Coastguard Worker 
SetUp()53*77c1e3ccSAndroid Build Coastguard Worker   void SetUp() override {
54*77c1e3ccSAndroid Build Coastguard Worker     InitializeConfig(encoding_mode_);
55*77c1e3ccSAndroid Build Coastguard Worker     if (encoding_mode_ == ::libaom_test::kTwoPassGood) {
56*77c1e3ccSAndroid Build Coastguard Worker       cfg_.rc_target_bitrate = kBitrate;
57*77c1e3ccSAndroid Build Coastguard Worker       cfg_.g_lag_in_frames = 5;
58*77c1e3ccSAndroid Build Coastguard Worker     }
59*77c1e3ccSAndroid Build Coastguard Worker   }
60*77c1e3ccSAndroid Build Coastguard Worker 
BeginPassHook(unsigned int)61*77c1e3ccSAndroid Build Coastguard Worker   void BeginPassHook(unsigned int) override {
62*77c1e3ccSAndroid Build Coastguard Worker     psnr_ = 0.0;
63*77c1e3ccSAndroid Build Coastguard Worker     nframes_ = 0;
64*77c1e3ccSAndroid Build Coastguard Worker   }
65*77c1e3ccSAndroid Build Coastguard Worker 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)66*77c1e3ccSAndroid Build Coastguard Worker   void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
67*77c1e3ccSAndroid Build Coastguard Worker     psnr_ += pkt->data.psnr.psnr[0];
68*77c1e3ccSAndroid Build Coastguard Worker     nframes_++;
69*77c1e3ccSAndroid Build Coastguard Worker   }
70*77c1e3ccSAndroid Build Coastguard Worker 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)71*77c1e3ccSAndroid Build Coastguard Worker   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
72*77c1e3ccSAndroid Build Coastguard Worker                           ::libaom_test::Encoder *encoder) override {
73*77c1e3ccSAndroid Build Coastguard Worker     if (video->frame() == 0) {
74*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
75*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_SHARPNESS, sharpness_level_);
76*77c1e3ccSAndroid Build Coastguard Worker       if (encoding_mode_ == ::libaom_test::kTwoPassGood) {
77*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
78*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
79*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
80*77c1e3ccSAndroid Build Coastguard Worker       } else if (encoding_mode_ == ::libaom_test::kAllIntra) {
81*77c1e3ccSAndroid Build Coastguard Worker         encoder->Control(AOME_SET_CQ_LEVEL, kCqLevel);
82*77c1e3ccSAndroid Build Coastguard Worker       }
83*77c1e3ccSAndroid Build Coastguard Worker     }
84*77c1e3ccSAndroid Build Coastguard Worker   }
85*77c1e3ccSAndroid Build Coastguard Worker 
GetAveragePsnr() const86*77c1e3ccSAndroid Build Coastguard Worker   double GetAveragePsnr() const {
87*77c1e3ccSAndroid Build Coastguard Worker     if (nframes_) return psnr_ / nframes_;
88*77c1e3ccSAndroid Build Coastguard Worker     return 0.0;
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker 
GetPsnrThreshold()91*77c1e3ccSAndroid Build Coastguard Worker   double GetPsnrThreshold() {
92*77c1e3ccSAndroid Build Coastguard Worker     return kPsnrThreshold.at(encoding_mode_).at(cpu_used_).at(sharpness_level_);
93*77c1e3ccSAndroid Build Coastguard Worker   }
94*77c1e3ccSAndroid Build Coastguard Worker 
DoTest()95*77c1e3ccSAndroid Build Coastguard Worker   void DoTest() {
96*77c1e3ccSAndroid Build Coastguard Worker     init_flags_ = AOM_CODEC_USE_PSNR;
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker     std::unique_ptr<libaom_test::VideoSource> video(
99*77c1e3ccSAndroid Build Coastguard Worker         new libaom_test::Y4mVideoSource("paris_352_288_30.y4m", 0, kFrames));
100*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NE(video, nullptr);
101*77c1e3ccSAndroid Build Coastguard Worker 
102*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
103*77c1e3ccSAndroid Build Coastguard Worker     const double psnr = GetAveragePsnr();
104*77c1e3ccSAndroid Build Coastguard Worker     EXPECT_GT(psnr, GetPsnrThreshold())
105*77c1e3ccSAndroid Build Coastguard Worker         << "encoding mode = " << encoding_mode_ << ", cpu used = " << cpu_used_
106*77c1e3ccSAndroid Build Coastguard Worker         << ", sharpness level = " << sharpness_level_;
107*77c1e3ccSAndroid Build Coastguard Worker   }
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker  private:
110*77c1e3ccSAndroid Build Coastguard Worker   const libaom_test::TestMode encoding_mode_;
111*77c1e3ccSAndroid Build Coastguard Worker   const int cpu_used_;
112*77c1e3ccSAndroid Build Coastguard Worker   const int sharpness_level_;
113*77c1e3ccSAndroid Build Coastguard Worker   double psnr_;
114*77c1e3ccSAndroid Build Coastguard Worker   unsigned int nframes_;
115*77c1e3ccSAndroid Build Coastguard Worker };
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker class SharpnessTestLarge : public SharpnessTest {};
118*77c1e3ccSAndroid Build Coastguard Worker 
119*77c1e3ccSAndroid Build Coastguard Worker class SharpnessAllIntraTest : public SharpnessTest {};
120*77c1e3ccSAndroid Build Coastguard Worker 
121*77c1e3ccSAndroid Build Coastguard Worker class SharpnessAllIntraTestLarge : public SharpnessTest {};
122*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(SharpnessTestLarge,SharpnessPSNRTest)123*77c1e3ccSAndroid Build Coastguard Worker TEST_P(SharpnessTestLarge, SharpnessPSNRTest) { DoTest(); }
124*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(SharpnessAllIntraTest,SharpnessPSNRTest)125*77c1e3ccSAndroid Build Coastguard Worker TEST_P(SharpnessAllIntraTest, SharpnessPSNRTest) { DoTest(); }
126*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(SharpnessAllIntraTestLarge,SharpnessPSNRTest)127*77c1e3ccSAndroid Build Coastguard Worker TEST_P(SharpnessAllIntraTestLarge, SharpnessPSNRTest) { DoTest(); }
128*77c1e3ccSAndroid Build Coastguard Worker 
129*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(SharpnessTestLarge,
130*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(::libaom_test::kTwoPassGood),
131*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(2, 4, 6),  // cpu_used
132*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(2, 5));    // sharpness level
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(SharpnessAllIntraTest,
135*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(::libaom_test::kAllIntra),
136*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(6),   // cpu_used
137*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(4));  // sharpness level
138*77c1e3ccSAndroid Build Coastguard Worker 
139*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(SharpnessAllIntraTestLarge,
140*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(::libaom_test::kAllIntra),
141*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(3, 6, 9),  // cpu_used
142*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Values(2, 5));    // sharpness level
143*77c1e3ccSAndroid Build Coastguard Worker }  // namespace
144