xref: /aosp_15_r20/external/libaom/test/loopfilter_control_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 
16 #include "gtest/gtest.h"
17 
18 #include "test/codec_factory.h"
19 #include "test/encode_test_driver.h"
20 #include "test/util.h"
21 #include "test/y4m_video_source.h"
22 #include "test/yuv_video_source.h"
23 
24 namespace {
25 
26 const unsigned int kFrames = 10;
27 const int kBitrate = 500;
28 
29 // List of psnr thresholds for LF settings 0-3
30 // keys: video, LF control, aq mode.
31 std::unordered_map<std::string,
32                    std::unordered_map<int, std::unordered_map<int, double>>>
33     kPsnrThreshold = { { "park_joy_90p_8_420.y4m",
34                          { { 0, { { 0, 33.0 }, { 3, 33.0 } } },
35                            { 1, { { 0, 33.0 }, { 3, 33.0 } } },
36                            { 2, { { 0, 33.0 }, { 3, 33.0 } } },
37                            { 3, { { 0, 33.0 }, { 3, 33.0 } } } } },
38                        { "paris_352_288_30.y4m",
39                          { { 0, { { 0, 33.0 }, { 3, 34.0 } } },
40                            { 1, { { 0, 33.0 }, { 3, 34.0 } } },
41                            { 2, { { 0, 33.0 }, { 3, 34.0 } } },
42                            { 3, { { 0, 33.0 }, { 3, 34.0 } } } } },
43                        { "niklas_1280_720_30.y4m",
44                          { { 0, { { 0, 31.0 }, { 3, 30.0 } } },
45                            { 1, { { 0, 31.0 }, { 3, 31.0 } } },
46                            { 2, { { 0, 31.0 }, { 3, 31.0 } } },
47                            { 3, { { 0, 31.0 }, { 3, 31.0 } } } } } };
48 
49 typedef struct {
50   const char *filename;
51   unsigned int input_bit_depth;
52   aom_img_fmt fmt;
53   aom_bit_depth_t bit_depth;
54   unsigned int profile;
55 } TestVideoParam;
56 
operator <<(std::ostream & os,const TestVideoParam & test_arg)57 std::ostream &operator<<(std::ostream &os, const TestVideoParam &test_arg) {
58   return os << "TestVideoParam { filename:" << test_arg.filename
59             << " input_bit_depth:" << test_arg.input_bit_depth
60             << " fmt:" << test_arg.fmt << " bit_depth:" << test_arg.bit_depth
61             << " profile:" << test_arg.profile << " }";
62 }
63 
64 const TestVideoParam kTestVectors[] = {
65   { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
66   { "paris_352_288_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
67   { "niklas_1280_720_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
68 };
69 
70 // Params: test video, lf_control, aq mode, threads, tile columns.
71 class LFControlEndToEndTest
72     : public ::libaom_test::CodecTestWith5Params<TestVideoParam, int,
73                                                  unsigned int, int, int>,
74       public ::libaom_test::EncoderTest {
75  protected:
LFControlEndToEndTest()76   LFControlEndToEndTest()
77       : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
78         lf_control_(GET_PARAM(2)), psnr_(0.0), nframes_(0),
79         aq_mode_(GET_PARAM(3)), threads_(GET_PARAM(4)),
80         tile_columns_(GET_PARAM(5)) {}
81 
82   ~LFControlEndToEndTest() override = default;
83 
SetUp()84   void SetUp() override {
85     InitializeConfig(::libaom_test::kRealTime);
86 
87     cfg_.g_threads = threads_;
88     cfg_.rc_buf_sz = 1000;
89     cfg_.rc_buf_initial_sz = 500;
90     cfg_.rc_buf_optimal_sz = 600;
91     cfg_.kf_max_dist = 9999;
92     cfg_.kf_min_dist = 9999;
93   }
94 
BeginPassHook(unsigned int)95   void BeginPassHook(unsigned int) override {
96     psnr_ = 0.0;
97     nframes_ = 0;
98   }
99 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)100   void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
101     psnr_ += pkt->data.psnr.psnr[0];
102     nframes_++;
103   }
104 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)105   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
106                           ::libaom_test::Encoder *encoder) override {
107     if (video->frame() == 0) {
108       encoder->Control(AV1E_SET_ENABLE_RESTORATION, 0);
109       encoder->Control(AV1E_SET_ENABLE_OBMC, 0);
110       encoder->Control(AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
111       encoder->Control(AV1E_SET_ENABLE_WARPED_MOTION, 0);
112       encoder->Control(AV1E_SET_DELTAQ_MODE, 0);
113       encoder->Control(AV1E_SET_ENABLE_TPL_MODEL, 0);
114       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
115       encoder->Control(AV1E_SET_TILE_COLUMNS, tile_columns_);
116       encoder->Control(AOME_SET_CPUUSED, 10);
117       encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
118       encoder->Control(AV1E_SET_AQ_MODE, aq_mode_);
119       encoder->Control(AV1E_SET_ROW_MT, 1);
120       encoder->Control(AV1E_SET_ENABLE_CDEF, 1);
121       encoder->Control(AV1E_SET_COEFF_COST_UPD_FREQ, 2);
122       encoder->Control(AV1E_SET_MODE_COST_UPD_FREQ, 2);
123       encoder->Control(AV1E_SET_MV_COST_UPD_FREQ, 2);
124       encoder->Control(AV1E_SET_DV_COST_UPD_FREQ, 2);
125       encoder->Control(AV1E_SET_LOOPFILTER_CONTROL, lf_control_);
126     }
127   }
128 
GetAveragePsnr() const129   double GetAveragePsnr() const {
130     if (nframes_) return psnr_ / nframes_;
131     return 0.0;
132   }
133 
GetPsnrThreshold()134   double GetPsnrThreshold() {
135     return kPsnrThreshold[test_video_param_.filename][lf_control_][aq_mode_];
136   }
137 
DoTest()138   void DoTest() {
139     cfg_.rc_target_bitrate = kBitrate;
140     cfg_.g_error_resilient = 0;
141     cfg_.g_profile = test_video_param_.profile;
142     cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
143     cfg_.g_bit_depth = test_video_param_.bit_depth;
144     init_flags_ = AOM_CODEC_USE_PSNR;
145     if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
146 
147     std::unique_ptr<libaom_test::VideoSource> video;
148     video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
149                                                 kFrames));
150     ASSERT_NE(video, nullptr);
151 
152     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
153     const double psnr = GetAveragePsnr();
154     EXPECT_GT(psnr, GetPsnrThreshold())
155         << "loopfilter control = " << lf_control_ << " aq mode = " << aq_mode_;
156   }
157 
158   TestVideoParam test_video_param_;
159   int lf_control_;
160 
161  private:
162   double psnr_;
163   unsigned int nframes_;
164   unsigned int aq_mode_;
165   int threads_;
166   int tile_columns_;
167 };
168 
169 class LFControlEndToEndTestThreaded : public LFControlEndToEndTest {};
170 
TEST_P(LFControlEndToEndTest,EndtoEndPSNRTest)171 TEST_P(LFControlEndToEndTest, EndtoEndPSNRTest) { DoTest(); }
172 
TEST_P(LFControlEndToEndTestThreaded,EndtoEndPSNRTest)173 TEST_P(LFControlEndToEndTestThreaded, EndtoEndPSNRTest) { DoTest(); }
174 
TEST(LFControlGetterTest,NullptrInput)175 TEST(LFControlGetterTest, NullptrInput) {
176   int *lf_level = nullptr;
177   aom_codec_ctx_t encoder;
178   aom_codec_enc_cfg_t cfg;
179   aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg, 1);
180   EXPECT_EQ(aom_codec_enc_init(&encoder, aom_codec_av1_cx(), &cfg, 0),
181             AOM_CODEC_OK);
182   EXPECT_EQ(aom_codec_control(&encoder, AOME_GET_LOOPFILTER_LEVEL, lf_level),
183             AOM_CODEC_INVALID_PARAM);
184   EXPECT_EQ(aom_codec_destroy(&encoder), AOM_CODEC_OK);
185 }
186 
187 AV1_INSTANTIATE_TEST_SUITE(LFControlEndToEndTest,
188                            ::testing::ValuesIn(kTestVectors),
189                            ::testing::Range(0, 4),
190                            ::testing::Values<unsigned int>(0, 3),
191                            ::testing::Values(1), ::testing::Values(1));
192 
193 AV1_INSTANTIATE_TEST_SUITE(LFControlEndToEndTestThreaded,
194                            ::testing::ValuesIn(kTestVectors),
195                            ::testing::Range(0, 4),
196                            ::testing::Values<unsigned int>(0, 3),
197                            ::testing::Range(2, 5), ::testing::Range(2, 5));
198 }  // namespace
199