• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "av1/ratectrl_rtc.h"
13 
14 #include <memory>
15 
16 #include "test/codec_factory.h"
17 #include "test/encode_test_driver.h"
18 #include "test/util.h"
19 #include "test/y4m_video_source.h"
20 #include "test/yuv_video_source.h"
21 #include "test/i420_video_source.h"
22 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
23 
24 namespace {
25 
26 constexpr size_t kNumFrames = 450;
27 
28 constexpr int kTemporalId[4] = { 0, 2, 1, 2 };
29 
30 // Parameter: aq mode: 0 and 3
31 class RcInterfaceTest : public ::libaom_test::EncoderTest,
32                         public ::libaom_test::CodecTestWithParam<int> {
33  public:
RcInterfaceTest()34   RcInterfaceTest()
35       : EncoderTest(GET_PARAM(0)), aq_mode_(GET_PARAM(1)), key_interval_(3000),
36         encoder_exit_(false), layer_frame_cnt_(0) {
37     memset(&svc_params_, 0, sizeof(svc_params_));
38     memset(&layer_id_, 0, sizeof(layer_id_));
39   }
40 
~RcInterfaceTest()41   ~RcInterfaceTest() override {}
42 
43  protected:
SetUp()44   void SetUp() override { InitializeConfig(::libaom_test::kRealTime); }
45 
GetNumSpatialLayers()46   int GetNumSpatialLayers() override { return rc_cfg_.ss_number_layers; }
47 
PreEncodeFrameHook(libaom_test::VideoSource * video,libaom_test::Encoder * encoder)48   void PreEncodeFrameHook(libaom_test::VideoSource *video,
49                           libaom_test::Encoder *encoder) override {
50     int key_int = key_interval_;
51     const int use_svc =
52         rc_cfg_.ss_number_layers > 1 || rc_cfg_.ts_number_layers > 1;
53     encoder->Control(AV1E_SET_RTC_EXTERNAL_RC, 1);
54     if (video->frame() == 0 && layer_frame_cnt_ == 0) {
55       encoder->Control(AOME_SET_CPUUSED, 7);
56       encoder->Control(AV1E_SET_AQ_MODE, aq_mode_);
57       encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
58       encoder->Control(AOME_SET_MAX_INTRA_BITRATE_PCT,
59                        rc_cfg_.max_intra_bitrate_pct);
60       if (use_svc) encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
61     }
62     // SVC specific settings
63     if (use_svc) {
64       frame_params_.spatial_layer_id =
65           layer_frame_cnt_ % rc_cfg_.ss_number_layers;
66       frame_params_.temporal_layer_id = kTemporalId[video->frame() % 4];
67       layer_id_.spatial_layer_id = frame_params_.spatial_layer_id;
68       layer_id_.temporal_layer_id = frame_params_.temporal_layer_id;
69       encoder->Control(AV1E_SET_SVC_LAYER_ID, &layer_id_);
70       key_int = key_interval_ * rc_cfg_.ss_number_layers;
71     }
72     frame_params_.frame_type =
73         layer_frame_cnt_ % key_int == 0 ? aom::kKeyFrame : aom::kInterFrame;
74     encoder_exit_ = video->frame() == kNumFrames;
75   }
76 
PostEncodeFrameHook(::libaom_test::Encoder * encoder)77   void PostEncodeFrameHook(::libaom_test::Encoder *encoder) override {
78     if (encoder_exit_) {
79       return;
80     }
81     layer_frame_cnt_++;
82     int qp;
83     encoder->Control(AOME_GET_LAST_QUANTIZER, &qp);
84     rc_api_->ComputeQP(frame_params_);
85     ASSERT_EQ(rc_api_->GetQP(), qp);
86   }
87 
FramePktHook(const aom_codec_cx_pkt_t * pkt)88   void FramePktHook(const aom_codec_cx_pkt_t *pkt) override {
89     if (layer_id_.spatial_layer_id == 0)
90       rc_api_->PostEncodeUpdate(pkt->data.frame.sz - 2);
91     else
92       rc_api_->PostEncodeUpdate(pkt->data.frame.sz);
93   }
94 
MismatchHook(const aom_image_t * img1,const aom_image_t * img2)95   void MismatchHook(const aom_image_t *img1, const aom_image_t *img2) override {
96     (void)img1;
97     (void)img2;
98   }
99 
RunOneLayer()100   void RunOneLayer() {
101     key_interval_ = 10000;
102     SetConfig();
103     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
104     frame_params_.spatial_layer_id = 0;
105     frame_params_.temporal_layer_id = 0;
106 
107     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
108                                          1, 0, kNumFrames);
109 
110     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
111   }
112 
RunOneLayerPeriodicKey()113   void RunOneLayerPeriodicKey() {
114     key_interval_ = 100;
115     SetConfig();
116     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
117     frame_params_.spatial_layer_id = 0;
118     frame_params_.temporal_layer_id = 0;
119 
120     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
121                                          1, 0, kNumFrames);
122 
123     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
124   }
125 
RunSvc()126   void RunSvc() {
127     key_interval_ = 10000;
128     SetConfigSvc();
129     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
130     frame_params_.spatial_layer_id = 0;
131     frame_params_.temporal_layer_id = 0;
132 
133     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
134                                          1, 0, kNumFrames);
135 
136     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
137   }
138 
RunSvcPeriodicKey()139   void RunSvcPeriodicKey() {
140     key_interval_ = 100;
141     SetConfigSvc();
142     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
143     frame_params_.spatial_layer_id = 0;
144     frame_params_.temporal_layer_id = 0;
145 
146     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
147                                          1, 0, kNumFrames);
148 
149     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
150   }
151 
152  private:
SetConfig()153   void SetConfig() {
154     rc_cfg_.width = 640;
155     rc_cfg_.height = 480;
156     rc_cfg_.max_quantizer = 52;
157     rc_cfg_.min_quantizer = 2;
158     rc_cfg_.target_bandwidth = 1000;
159     rc_cfg_.buf_initial_sz = 600;
160     rc_cfg_.buf_optimal_sz = 600;
161     rc_cfg_.buf_sz = 1000;
162     rc_cfg_.undershoot_pct = 50;
163     rc_cfg_.overshoot_pct = 50;
164     rc_cfg_.max_intra_bitrate_pct = 1000;
165     rc_cfg_.framerate = 30.0;
166     rc_cfg_.ss_number_layers = 1;
167     rc_cfg_.ts_number_layers = 1;
168     rc_cfg_.scaling_factor_num[0] = 1;
169     rc_cfg_.scaling_factor_den[0] = 1;
170     rc_cfg_.layer_target_bitrate[0] = 1000;
171     rc_cfg_.max_quantizers[0] = 52;
172     rc_cfg_.min_quantizers[0] = 2;
173     rc_cfg_.aq_mode = aq_mode_;
174 
175     // Encoder settings for ground truth.
176     cfg_.g_w = 640;
177     cfg_.g_h = 480;
178     cfg_.rc_undershoot_pct = 50;
179     cfg_.rc_overshoot_pct = 50;
180     cfg_.rc_buf_initial_sz = 600;
181     cfg_.rc_buf_optimal_sz = 600;
182     cfg_.rc_buf_sz = 1000;
183     cfg_.rc_dropframe_thresh = 0;
184     cfg_.rc_min_quantizer = 2;
185     cfg_.rc_max_quantizer = 52;
186     cfg_.rc_end_usage = AOM_CBR;
187     cfg_.g_lag_in_frames = 0;
188     cfg_.g_error_resilient = 0;
189     cfg_.rc_target_bitrate = 1000;
190     cfg_.kf_min_dist = key_interval_;
191     cfg_.kf_max_dist = key_interval_;
192   }
193 
SetConfigSvc()194   void SetConfigSvc() {
195     rc_cfg_.width = 640;
196     rc_cfg_.height = 480;
197     rc_cfg_.max_quantizer = 52;
198     rc_cfg_.min_quantizer = 2;
199     rc_cfg_.target_bandwidth = 1000;
200     rc_cfg_.buf_initial_sz = 600;
201     rc_cfg_.buf_optimal_sz = 600;
202     rc_cfg_.buf_sz = 1000;
203     rc_cfg_.undershoot_pct = 50;
204     rc_cfg_.overshoot_pct = 50;
205     rc_cfg_.max_intra_bitrate_pct = 1000;
206     rc_cfg_.framerate = 30.0;
207     rc_cfg_.ss_number_layers = 3;
208     rc_cfg_.ts_number_layers = 3;
209     rc_cfg_.aq_mode = aq_mode_;
210 
211     rc_cfg_.scaling_factor_num[0] = 1;
212     rc_cfg_.scaling_factor_den[0] = 4;
213     rc_cfg_.scaling_factor_num[1] = 2;
214     rc_cfg_.scaling_factor_den[1] = 4;
215     rc_cfg_.scaling_factor_num[2] = 4;
216     rc_cfg_.scaling_factor_den[2] = 4;
217 
218     rc_cfg_.ts_rate_decimator[0] = 4;
219     rc_cfg_.ts_rate_decimator[1] = 2;
220     rc_cfg_.ts_rate_decimator[2] = 1;
221 
222     rc_cfg_.layer_target_bitrate[0] = 100;
223     rc_cfg_.layer_target_bitrate[1] = 140;
224     rc_cfg_.layer_target_bitrate[2] = 200;
225     rc_cfg_.layer_target_bitrate[3] = 250;
226     rc_cfg_.layer_target_bitrate[4] = 350;
227     rc_cfg_.layer_target_bitrate[5] = 500;
228     rc_cfg_.layer_target_bitrate[6] = 450;
229     rc_cfg_.layer_target_bitrate[7] = 630;
230     rc_cfg_.layer_target_bitrate[8] = 900;
231 
232     for (int sl = 0; sl < rc_cfg_.ss_number_layers; ++sl) {
233       for (int tl = 0; tl < rc_cfg_.ts_number_layers; ++tl) {
234         const int i = sl * rc_cfg_.ts_number_layers + tl;
235         rc_cfg_.max_quantizers[i] = 56;
236         rc_cfg_.min_quantizers[i] = 2;
237       }
238     }
239 
240     // Encoder settings for ground truth.
241     cfg_.g_w = 640;
242     cfg_.g_h = 480;
243     svc_params_.number_spatial_layers = 3;
244     svc_params_.number_temporal_layers = 3;
245     cfg_.g_timebase.num = 1;
246     cfg_.g_timebase.den = 30;
247     svc_params_.scaling_factor_num[0] = 72;
248     svc_params_.scaling_factor_den[0] = 288;
249     svc_params_.scaling_factor_num[1] = 144;
250     svc_params_.scaling_factor_den[1] = 288;
251     svc_params_.scaling_factor_num[2] = 288;
252     svc_params_.scaling_factor_den[2] = 288;
253     for (int i = 0; i < AOM_MAX_LAYERS; ++i) {
254       svc_params_.max_quantizers[i] = 56;
255       svc_params_.min_quantizers[i] = 2;
256     }
257     cfg_.rc_end_usage = AOM_CBR;
258     cfg_.g_lag_in_frames = 0;
259     cfg_.g_error_resilient = 0;
260     // 3 temporal layers
261     svc_params_.framerate_factor[0] = 4;
262     svc_params_.framerate_factor[1] = 2;
263     svc_params_.framerate_factor[2] = 1;
264 
265     cfg_.rc_buf_initial_sz = 600;
266     cfg_.rc_buf_optimal_sz = 600;
267     cfg_.rc_buf_sz = 1000;
268     cfg_.rc_min_quantizer = 2;
269     cfg_.rc_max_quantizer = 56;
270     cfg_.g_threads = 1;
271     cfg_.kf_min_dist = key_interval_;
272     cfg_.kf_max_dist = key_interval_;
273     cfg_.rc_target_bitrate = 1000;
274     cfg_.rc_overshoot_pct = 50;
275     cfg_.rc_undershoot_pct = 50;
276 
277     svc_params_.layer_target_bitrate[0] = 100;
278     svc_params_.layer_target_bitrate[1] = 140;
279     svc_params_.layer_target_bitrate[2] = 200;
280     svc_params_.layer_target_bitrate[3] = 250;
281     svc_params_.layer_target_bitrate[4] = 350;
282     svc_params_.layer_target_bitrate[5] = 500;
283     svc_params_.layer_target_bitrate[6] = 450;
284     svc_params_.layer_target_bitrate[7] = 630;
285     svc_params_.layer_target_bitrate[8] = 900;
286   }
287 
288   std::unique_ptr<aom::AV1RateControlRTC> rc_api_;
289   aom::AV1RateControlRtcConfig rc_cfg_;
290   int aq_mode_;
291   int key_interval_;
292   aom::AV1FrameParamsRTC frame_params_;
293   bool encoder_exit_;
294   aom_svc_params_t svc_params_;
295   aom_svc_layer_id_t layer_id_;
296   int layer_frame_cnt_;
297 };
298 
TEST_P(RcInterfaceTest,OneLayer)299 TEST_P(RcInterfaceTest, OneLayer) { RunOneLayer(); }
300 
TEST_P(RcInterfaceTest,OneLayerPeriodicKey)301 TEST_P(RcInterfaceTest, OneLayerPeriodicKey) { RunOneLayerPeriodicKey(); }
302 
TEST_P(RcInterfaceTest,Svc)303 TEST_P(RcInterfaceTest, Svc) { RunSvc(); }
304 
TEST_P(RcInterfaceTest,SvcPeriodicKey)305 TEST_P(RcInterfaceTest, SvcPeriodicKey) { RunSvcPeriodicKey(); }
306 
307 AV1_INSTANTIATE_TEST_SUITE(RcInterfaceTest, ::testing::Values(0, 3));
308 
309 }  // namespace
310