xref: /aosp_15_r20/external/libaom/test/quant_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, 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 #include "config/aom_config.h"
12 
13 #include "av1/encoder/av1_quantize.h"
14 #include "gtest/gtest.h"
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/i420_video_source.h"
18 #include "test/util.h"
19 #include "test/y4m_video_source.h"
20 
21 namespace {
22 
23 const ::libaom_test::TestMode kTestMode[] =
24 #if CONFIG_REALTIME_ONLY
25     { ::libaom_test::kRealTime };
26 #else
27     { ::libaom_test::kRealTime, ::libaom_test::kOnePassGood };
28 #endif
29 
30 class QMTest
31     : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>,
32       public ::libaom_test::EncoderTest {
33  protected:
QMTest()34   QMTest() : EncoderTest(GET_PARAM(0)) {}
35   ~QMTest() override = default;
36 
SetUp()37   void SetUp() override {
38     InitializeConfig(GET_PARAM(1));
39     set_cpu_used_ = GET_PARAM(2);
40   }
41 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)42   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
43                           ::libaom_test::Encoder *encoder) override {
44     if (video->frame() == 0) {
45       encoder->Control(AOME_SET_CPUUSED, set_cpu_used_);
46       encoder->Control(AV1E_SET_ENABLE_QM, 1);
47       encoder->Control(AV1E_SET_QM_MIN, qm_min_);
48       encoder->Control(AV1E_SET_QM_MAX, qm_max_);
49 
50       encoder->Control(AOME_SET_MAX_INTRA_BITRATE_PCT, 100);
51       if (mode_ == ::libaom_test::kRealTime) {
52         encoder->Control(AV1E_SET_ALLOW_WARPED_MOTION, 0);
53         encoder->Control(AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
54         encoder->Control(AV1E_SET_ENABLE_OBMC, 0);
55       }
56     }
57   }
58 
DoTest(int qm_min,int qm_max)59   void DoTest(int qm_min, int qm_max) {
60     qm_min_ = qm_min;
61     qm_max_ = qm_max;
62     cfg_.kf_max_dist = 12;
63     cfg_.rc_min_quantizer = 8;
64     cfg_.rc_max_quantizer = 56;
65     cfg_.rc_end_usage = AOM_CBR;
66     cfg_.g_lag_in_frames = 6;
67     cfg_.rc_buf_initial_sz = 500;
68     cfg_.rc_buf_optimal_sz = 500;
69     cfg_.rc_buf_sz = 1000;
70     cfg_.rc_target_bitrate = 300;
71     ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
72                                          288, 30, 1, 0, 15);
73     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
74   }
75 
76   int set_cpu_used_;
77   int qm_min_;
78   int qm_max_;
79 };
80 
81 // encodes and decodes without a mismatch.
TEST_P(QMTest,TestNoMisMatchQM1)82 TEST_P(QMTest, TestNoMisMatchQM1) { DoTest(5, 9); }
83 
84 // encodes and decodes without a mismatch.
TEST_P(QMTest,TestNoMisMatchQM2)85 TEST_P(QMTest, TestNoMisMatchQM2) { DoTest(0, 8); }
86 
87 // encodes and decodes without a mismatch.
TEST_P(QMTest,TestNoMisMatchQM3)88 TEST_P(QMTest, TestNoMisMatchQM3) { DoTest(9, 15); }
89 
90 AV1_INSTANTIATE_TEST_SUITE(QMTest, ::testing::ValuesIn(kTestMode),
91                            ::testing::Range(5, 9));
92 
93 #if !CONFIG_REALTIME_ONLY
94 typedef struct {
95   const unsigned int min_q;
96   const unsigned int max_q;
97 } QuantParam;
98 
99 const QuantParam QuantTestParams[] = {
100   { 0, 10 }, { 0, 60 }, { 20, 35 }, { 35, 50 }, { 50, 63 }
101 };
102 
operator <<(std::ostream & os,const QuantParam & test_arg)103 std::ostream &operator<<(std::ostream &os, const QuantParam &test_arg) {
104   return os << "QuantParam { min_q:" << test_arg.min_q
105             << " max_q:" << test_arg.max_q << " }";
106 }
107 
108 /*
109  * This class is used to test whether base_qindex is within min
110  * and max quantizer range configured by user.
111  */
112 class QuantizerBoundsCheckTestLarge
113     : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode,
114                                                  QuantParam, aom_rc_mode>,
115       public ::libaom_test::EncoderTest {
116  protected:
QuantizerBoundsCheckTestLarge()117   QuantizerBoundsCheckTestLarge()
118       : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
119         quant_param_(GET_PARAM(2)), rc_end_usage_(GET_PARAM(3)) {
120     quant_bound_violated_ = false;
121   }
122   ~QuantizerBoundsCheckTestLarge() override = default;
123 
SetUp()124   void SetUp() override {
125     InitializeConfig(encoding_mode_);
126     const aom_rational timebase = { 1, 30 };
127     cfg_.g_timebase = timebase;
128     cfg_.rc_end_usage = rc_end_usage_;
129     cfg_.g_threads = 1;
130     cfg_.rc_min_quantizer = quant_param_.min_q;
131     cfg_.rc_max_quantizer = quant_param_.max_q;
132     cfg_.g_lag_in_frames = 35;
133     if (rc_end_usage_ != AOM_Q) {
134       cfg_.rc_target_bitrate = 400;
135     }
136   }
137 
DoDecode() const138   bool DoDecode() const override { return true; }
139 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)140   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
141                           ::libaom_test::Encoder *encoder) override {
142     if (video->frame() == 0) {
143       encoder->Control(AOME_SET_CPUUSED, 5);
144     }
145   }
146 
HandleDecodeResult(const aom_codec_err_t res_dec,libaom_test::Decoder * decoder)147   bool HandleDecodeResult(const aom_codec_err_t res_dec,
148                           libaom_test::Decoder *decoder) override {
149     EXPECT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
150     if (AOM_CODEC_OK == res_dec) {
151       aom_codec_ctx_t *ctx_dec = decoder->GetDecoder();
152       AOM_CODEC_CONTROL_TYPECHECKED(ctx_dec, AOMD_GET_LAST_QUANTIZER,
153                                     &base_qindex_);
154       min_bound_qindex_ = av1_quantizer_to_qindex(cfg_.rc_min_quantizer);
155       max_bound_qindex_ = av1_quantizer_to_qindex(cfg_.rc_max_quantizer);
156       if ((base_qindex_ < min_bound_qindex_ ||
157            base_qindex_ > max_bound_qindex_) &&
158           quant_bound_violated_ == false) {
159         quant_bound_violated_ = true;
160       }
161     }
162     return AOM_CODEC_OK == res_dec;
163   }
164 
165   ::libaom_test::TestMode encoding_mode_;
166   const QuantParam quant_param_;
167   int base_qindex_;
168   int min_bound_qindex_;
169   int max_bound_qindex_;
170   bool quant_bound_violated_;
171   aom_rc_mode rc_end_usage_;
172 };
173 
TEST_P(QuantizerBoundsCheckTestLarge,QuantizerBoundsCheckEncodeTest)174 TEST_P(QuantizerBoundsCheckTestLarge, QuantizerBoundsCheckEncodeTest) {
175   libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
176                                      cfg_.g_timebase.den, cfg_.g_timebase.num,
177                                      0, 50);
178   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
179   ASSERT_EQ(quant_bound_violated_, false);
180 }
181 
182 AV1_INSTANTIATE_TEST_SUITE(QuantizerBoundsCheckTestLarge,
183                            ::testing::Values(::libaom_test::kOnePassGood,
184                                              ::libaom_test::kTwoPassGood),
185                            ::testing::ValuesIn(QuantTestParams),
186                            ::testing::Values(AOM_Q, AOM_VBR, AOM_CBR, AOM_CQ));
187 #endif  // !CONFIG_REALTIME_ONLY
188 }  // namespace
189