xref: /aosp_15_r20/external/libaom/test/rt_end_to_end_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2019, 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 <ostream>
14 #include <string>
15 #include <unordered_map>
16 
17 #include "gtest/gtest.h"
18 
19 #include "test/codec_factory.h"
20 #include "test/encode_test_driver.h"
21 #include "test/util.h"
22 #include "test/y4m_video_source.h"
23 #include "test/yuv_video_source.h"
24 
25 namespace {
26 
27 const unsigned int kFrames = 10;
28 const int kBitrate = 500;
29 
30 // List of psnr thresholds for speed settings 6-8
31 // keys: video, speed, aq mode.
32 std::unordered_map<std::string,
33                    std::unordered_map<int, std::unordered_map<int, double>>>
34     kPsnrThreshold = { { "park_joy_90p_8_420.y4m",
35                          { { 5, { { 0, 34.0 }, { 3, 35.0 } } },
36                            { 6, { { 0, 34.0 }, { 3, 35.0 } } },
37                            { 7, { { 0, 33.0 }, { 3, 34.0 } } },
38                            { 8, { { 0, 33.0 }, { 3, 34.0 } } },
39                            { 9, { { 0, 33.0 }, { 3, 34.0 } } },
40                            { 10, { { 0, 33.0 }, { 3, 34.0 } } } } },
41                        { "paris_352_288_30.y4m",
42                          { { 5, { { 0, 35.0 }, { 3, 35.0 } } },
43                            { 6, { { 0, 35.0 }, { 3, 35.0 } } },
44                            { 7, { { 0, 34.0 }, { 3, 34.0 } } },
45                            { 8, { { 0, 34.0 }, { 3, 35.0 } } },
46                            { 9, { { 0, 34.0 }, { 3, 34.0 } } },
47                            { 10, { { 0, 34.0 }, { 3, 34.0 } } } } },
48                        { "niklas_1280_720_30.y4m",
49                          { { 5, { { 0, 32.0 }, { 3, 32.0 } } },
50                            { 6, { { 0, 32.0 }, { 3, 32.0 } } },
51                            { 7, { { 0, 31.0 }, { 3, 31.0 } } },
52                            { 8, { { 0, 31.0 }, { 3, 31.0 } } },
53                            { 9, { { 0, 31.0 }, { 3, 31.0 } } },
54                            { 10, { { 0, 31.0 }, { 3, 31.0 } } } } },
55                        { "hantro_collage_w352h288_nv12.yuv",
56                          { { 5, { { 0, 32.0 }, { 3, 32.0 } } },
57                            { 6, { { 0, 32.0 }, { 3, 32.0 } } },
58                            { 7, { { 0, 32.0 }, { 3, 32.0 } } },
59                            { 8, { { 0, 32.0 }, { 3, 32.0 } } },
60                            { 9, { { 0, 31.0 }, { 3, 31.0 } } },
61                            { 10, { { 0, 31.0 }, { 3, 31.0 } } } } } };
62 
63 typedef struct {
64   const char *filename;
65   unsigned int input_bit_depth;
66   aom_img_fmt fmt;
67   aom_bit_depth_t bit_depth;
68   unsigned int profile;
69 } TestVideoParam;
70 
operator <<(std::ostream & os,const TestVideoParam & test_arg)71 std::ostream &operator<<(std::ostream &os, const TestVideoParam &test_arg) {
72   return os << "TestVideoParam { filename:" << test_arg.filename
73             << " input_bit_depth:" << test_arg.input_bit_depth
74             << " fmt:" << test_arg.fmt << " bit_depth:" << test_arg.bit_depth
75             << " profile:" << test_arg.profile << " }";
76 }
77 
78 const TestVideoParam kTestVectors[] = {
79   { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
80   { "paris_352_288_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
81   { "niklas_1280_720_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
82   { "hantro_collage_w352h288_nv12.yuv", 8, AOM_IMG_FMT_NV12, AOM_BITS_8, 0 },
83 };
84 
85 // Params: test video, speed, aq mode, threads, tile columns, tile rows.
86 class RTEndToEndTest
87     : public ::libaom_test::CodecTestWith6Params<TestVideoParam, int,
88                                                  unsigned int, int, int, int>,
89       public ::libaom_test::EncoderTest {
90  protected:
RTEndToEndTest()91   RTEndToEndTest()
92       : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
93         cpu_used_(GET_PARAM(2)), psnr_(0.0), nframes_(0),
94         aq_mode_(GET_PARAM(3)), threads_(GET_PARAM(4)),
95         tile_columns_(GET_PARAM(5)), tile_rows_(GET_PARAM(6)) {}
96 
97   ~RTEndToEndTest() override = default;
98 
SetUp()99   void SetUp() override {
100     InitializeConfig(::libaom_test::kRealTime);
101 
102     cfg_.g_threads = threads_;
103     cfg_.rc_buf_sz = 1000;
104     cfg_.rc_buf_initial_sz = 500;
105     cfg_.rc_buf_optimal_sz = 600;
106     cfg_.kf_max_dist = 9999;
107     cfg_.kf_min_dist = 9999;
108   }
109 
BeginPassHook(unsigned int)110   void BeginPassHook(unsigned int) override {
111     psnr_ = 0.0;
112     nframes_ = 0;
113   }
114 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)115   void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
116     psnr_ += pkt->data.psnr.psnr[0];
117     nframes_++;
118   }
119 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)120   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
121                           ::libaom_test::Encoder *encoder) override {
122     if (video->frame() == 0) {
123       encoder->Control(AV1E_SET_ENABLE_RESTORATION, 0);
124       encoder->Control(AV1E_SET_ENABLE_OBMC, 0);
125       encoder->Control(AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
126       encoder->Control(AV1E_SET_ENABLE_WARPED_MOTION, 0);
127       encoder->Control(AV1E_SET_DELTAQ_MODE, 0);
128       encoder->Control(AV1E_SET_ENABLE_TPL_MODEL, 0);
129       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
130       encoder->Control(AV1E_SET_TILE_COLUMNS, tile_columns_);
131       encoder->Control(AV1E_SET_TILE_ROWS, tile_rows_);
132       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
133       encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
134       encoder->Control(AV1E_SET_AQ_MODE, aq_mode_);
135       encoder->Control(AV1E_SET_ROW_MT, 1);
136       encoder->Control(AV1E_SET_ENABLE_CDEF, 1);
137       encoder->Control(AV1E_SET_COEFF_COST_UPD_FREQ, 2);
138       encoder->Control(AV1E_SET_MODE_COST_UPD_FREQ, 2);
139       encoder->Control(AV1E_SET_MV_COST_UPD_FREQ, 2);
140       encoder->Control(AV1E_SET_DV_COST_UPD_FREQ, 2);
141     }
142   }
143 
GetAveragePsnr() const144   double GetAveragePsnr() const {
145     if (nframes_) return psnr_ / nframes_;
146     return 0.0;
147   }
148 
GetPsnrThreshold()149   double GetPsnrThreshold() {
150     return kPsnrThreshold[test_video_param_.filename][cpu_used_][aq_mode_];
151   }
152 
DoTest()153   void DoTest() {
154     cfg_.rc_target_bitrate = kBitrate;
155     cfg_.g_error_resilient = 0;
156     cfg_.g_profile = test_video_param_.profile;
157     cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
158     cfg_.g_bit_depth = test_video_param_.bit_depth;
159     init_flags_ = AOM_CODEC_USE_PSNR;
160     if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
161 
162     std::unique_ptr<libaom_test::VideoSource> video;
163     if (is_extension_y4m(test_video_param_.filename))
164       video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
165                                                   kFrames));
166     else
167       video.reset(new libaom_test::YUVVideoSource(test_video_param_.filename,
168                                                   test_video_param_.fmt, 352,
169                                                   288, 30, 1, 0, kFrames));
170     ASSERT_NE(video, nullptr);
171 
172     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
173     const double psnr = GetAveragePsnr();
174     EXPECT_GT(psnr, GetPsnrThreshold())
175         << "cpu used = " << cpu_used_ << " aq mode = " << aq_mode_;
176   }
177 
178   TestVideoParam test_video_param_;
179   int cpu_used_;
180 
181  private:
182   double psnr_;
183   unsigned int nframes_;
184   unsigned int aq_mode_;
185   int threads_;
186   int tile_columns_;
187   int tile_rows_;
188 };
189 
190 class RTEndToEndTestThreaded : public RTEndToEndTest {};
191 
TEST_P(RTEndToEndTest,EndtoEndPSNRTest)192 TEST_P(RTEndToEndTest, EndtoEndPSNRTest) { DoTest(); }
193 
TEST_P(RTEndToEndTestThreaded,EndtoEndPSNRTest)194 TEST_P(RTEndToEndTestThreaded, EndtoEndPSNRTest) { DoTest(); }
195 
196 AV1_INSTANTIATE_TEST_SUITE(RTEndToEndTest, ::testing::ValuesIn(kTestVectors),
197                            ::testing::Range(5, 12),
198                            ::testing::Values<unsigned int>(0, 3),
199                            ::testing::Values(1), ::testing::Values(1),
200                            ::testing::Values(1));
201 
202 AV1_INSTANTIATE_TEST_SUITE(RTEndToEndTestThreaded,
203                            ::testing::ValuesIn(kTestVectors),
204                            ::testing::Range(5, 12),
205                            ::testing::Values<unsigned int>(0, 3),
206                            ::testing::Range(2, 6), ::testing::Range(1, 5),
207                            ::testing::Range(1, 5));
208 }  // namespace
209