xref: /aosp_15_r20/external/libaom/test/end_to_end_qmpsnr_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2022, 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 
14 #include "aom_ports/mem.h"
15 #include "aom_dsp/ssim.h"
16 #include "av1/common/blockd.h"
17 #include "gtest/gtest.h"
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 
23 namespace {
24 
25 const unsigned int kFrames = 10;
26 const unsigned int kCqLevel = 18;
27 // List of ssim thresholds for speed settings 0-8 with all intra encoding mode.
28 const double kSsimThreshold[] = { 83.4, 83.4, 83.4, 83.3, 83.3,
29                                   83.0, 82.3, 81.1, 81.1 };
30 
31 typedef struct {
32   const char *filename;
33   unsigned int input_bit_depth;
34   aom_img_fmt fmt;
35   aom_bit_depth_t bit_depth;
36   unsigned int profile;
37 } TestVideoParam;
38 
operator <<(std::ostream & os,const TestVideoParam & test_arg)39 std::ostream &operator<<(std::ostream &os, const TestVideoParam &test_arg) {
40   return os << "TestVideoParam { filename:" << test_arg.filename
41             << " input_bit_depth:" << test_arg.input_bit_depth
42             << " fmt:" << test_arg.fmt << " bit_depth:" << test_arg.bit_depth
43             << " profile:" << test_arg.profile << " }";
44 }
45 
46 const TestVideoParam kTestVectors[] = {
47   { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
48   { "park_joy_90p_8_422.y4m", 8, AOM_IMG_FMT_I422, AOM_BITS_8, 2 },
49   { "park_joy_90p_8_444.y4m", 8, AOM_IMG_FMT_I444, AOM_BITS_8, 1 },
50 #if CONFIG_AV1_HIGHBITDEPTH
51   { "park_joy_90p_10_420.y4m", 10, AOM_IMG_FMT_I42016, AOM_BITS_10, 0 },
52   { "park_joy_90p_10_422.y4m", 10, AOM_IMG_FMT_I42216, AOM_BITS_10, 2 },
53   { "park_joy_90p_10_444.y4m", 10, AOM_IMG_FMT_I44416, AOM_BITS_10, 1 },
54   { "park_joy_90p_12_420.y4m", 12, AOM_IMG_FMT_I42016, AOM_BITS_12, 2 },
55   { "park_joy_90p_12_422.y4m", 12, AOM_IMG_FMT_I42216, AOM_BITS_12, 2 },
56   { "park_joy_90p_12_444.y4m", 12, AOM_IMG_FMT_I44416, AOM_BITS_12, 2 },
57 #endif
58 };
59 
60 // This class is used to check adherence to given ssim value, while using the
61 // "dist-metric=qm-psnr" option.
62 class EndToEndQMPSNRTest
63     : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode,
64                                                  TestVideoParam, int>,
65       public ::libaom_test::EncoderTest {
66  protected:
EndToEndQMPSNRTest()67   EndToEndQMPSNRTest()
68       : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
69         test_video_param_(GET_PARAM(2)), cpu_used_(GET_PARAM(3)), nframes_(0),
70         ssim_(0.0) {}
71 
72   ~EndToEndQMPSNRTest() override = default;
73 
SetUp()74   void SetUp() override { InitializeConfig(encoding_mode_); }
75 
BeginPassHook(unsigned int)76   void BeginPassHook(unsigned int) override {
77     nframes_ = 0;
78     ssim_ = 0.0;
79   }
80 
CalculateFrameLevelSSIM(const aom_image_t * img_src,const aom_image_t * img_enc,aom_bit_depth_t bit_depth,unsigned int input_bit_depth)81   void CalculateFrameLevelSSIM(const aom_image_t *img_src,
82                                const aom_image_t *img_enc,
83                                aom_bit_depth_t bit_depth,
84                                unsigned int input_bit_depth) override {
85     double frame_ssim;
86     double plane_ssim[MAX_MB_PLANE] = { 0.0, 0.0, 0.0 };
87     int crop_widths[PLANE_TYPES];
88     int crop_heights[PLANE_TYPES];
89     crop_widths[PLANE_TYPE_Y] = img_src->d_w;
90     crop_heights[PLANE_TYPE_Y] = img_src->d_h;
91     // Width of UV planes calculated based on chroma_shift values.
92     crop_widths[PLANE_TYPE_UV] =
93         img_src->x_chroma_shift == 1 ? (img_src->w + 1) >> 1 : img_src->w;
94     crop_heights[PLANE_TYPE_UV] =
95         img_src->y_chroma_shift == 1 ? (img_src->h + 1) >> 1 : img_src->h;
96     nframes_++;
97 
98 #if CONFIG_AV1_HIGHBITDEPTH
99     uint8_t is_hbd = bit_depth > AOM_BITS_8;
100     if (is_hbd) {
101       // HBD ssim calculation.
102       uint8_t shift = bit_depth - input_bit_depth;
103       for (int i = AOM_PLANE_Y; i < MAX_MB_PLANE; ++i) {
104         const int is_uv = i > AOM_PLANE_Y;
105         plane_ssim[i] = aom_highbd_ssim2(
106             CONVERT_TO_BYTEPTR(img_src->planes[i]),
107             CONVERT_TO_BYTEPTR(img_enc->planes[i]),
108             img_src->stride[is_uv] >> is_hbd, img_enc->stride[is_uv] >> is_hbd,
109             crop_widths[is_uv], crop_heights[is_uv], input_bit_depth, shift);
110       }
111       frame_ssim = plane_ssim[AOM_PLANE_Y] * .8 +
112                    .1 * (plane_ssim[AOM_PLANE_U] + plane_ssim[AOM_PLANE_V]);
113       // Accumulate to find sequence level ssim value.
114       ssim_ += frame_ssim;
115       return;
116     }
117 #else
118     (void)bit_depth;
119     (void)input_bit_depth;
120 #endif  // CONFIG_AV1_HIGHBITDEPTH
121 
122     // LBD ssim calculation.
123     for (int i = AOM_PLANE_Y; i < MAX_MB_PLANE; ++i) {
124       const int is_uv = i > AOM_PLANE_Y;
125       plane_ssim[i] = aom_ssim2(img_src->planes[i], img_enc->planes[i],
126                                 img_src->stride[is_uv], img_enc->stride[is_uv],
127                                 crop_widths[is_uv], crop_heights[is_uv]);
128     }
129     frame_ssim = plane_ssim[AOM_PLANE_Y] * .8 +
130                  .1 * (plane_ssim[AOM_PLANE_U] + plane_ssim[AOM_PLANE_V]);
131     // Accumulate to find sequence level ssim value.
132     ssim_ += frame_ssim;
133   }
134 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)135   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
136                           ::libaom_test::Encoder *encoder) override {
137     if (video->frame() == 0) {
138       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
139       encoder->Control(AV1E_SET_TILE_COLUMNS, 4);
140       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
141       encoder->Control(AOME_SET_TUNING, AOM_TUNE_SSIM);
142       encoder->Control(AOME_SET_CQ_LEVEL, kCqLevel);
143       encoder->SetOption("dist-metric", "qm-psnr");
144     }
145   }
146 
GetAverageSsim() const147   double GetAverageSsim() const {
148     if (nframes_) return 100 * pow(ssim_ / nframes_, 8.0);
149     return 0.0;
150   }
151 
GetSsimThreshold()152   double GetSsimThreshold() { return kSsimThreshold[cpu_used_]; }
153 
DoTest()154   void DoTest() {
155     cfg_.g_profile = test_video_param_.profile;
156     cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
157     cfg_.g_bit_depth = test_video_param_.bit_depth;
158     if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
159 
160     std::unique_ptr<libaom_test::VideoSource> video(
161         new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
162                                         kFrames));
163     ASSERT_NE(video, nullptr);
164     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
165     const double ssim = GetAverageSsim();
166     EXPECT_GT(ssim, GetSsimThreshold())
167         << "encoding mode = " << encoding_mode_ << ", cpu used = " << cpu_used_;
168   }
169 
170  private:
171   const libaom_test::TestMode encoding_mode_;
172   const TestVideoParam test_video_param_;
173   const int cpu_used_;
174   unsigned int nframes_;
175   double ssim_;
176 };
177 
178 class EndToEndQMPSNRTestLarge : public EndToEndQMPSNRTest {};
179 
TEST_P(EndToEndQMPSNRTestLarge,EndtoEndQMPSNRTest)180 TEST_P(EndToEndQMPSNRTestLarge, EndtoEndQMPSNRTest) { DoTest(); }
181 
TEST_P(EndToEndQMPSNRTest,EndtoEndQMPSNRTest)182 TEST_P(EndToEndQMPSNRTest, EndtoEndQMPSNRTest) { DoTest(); }
183 
184 AV1_INSTANTIATE_TEST_SUITE(EndToEndQMPSNRTestLarge,
185                            ::testing::Values(::libaom_test::kAllIntra),
186                            ::testing::ValuesIn(kTestVectors),
187                            ::testing::Values(2, 4, 6, 8));  // cpu_used
188 
189 AV1_INSTANTIATE_TEST_SUITE(EndToEndQMPSNRTest,
190                            ::testing::Values(::libaom_test::kAllIntra),
191                            ::testing::Values(kTestVectors[0]),  // 420
192                            ::testing::Values(6));               // cpu_used
193 }  // namespace
194