• 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 "gtest/gtest.h"
17 #include "test/codec_factory.h"
18 #include "test/encode_test_driver.h"
19 #include "test/i420_video_source.h"
20 #include "test/util.h"
21 
22 namespace {
23 
24 constexpr size_t kNumFrames = 450;
25 
26 const int kTemporalId3Layer[4] = { 0, 2, 1, 2 };
27 const int kTemporalId2Layer[2] = { 0, 1 };
28 const int kTemporalRateAllocation3Layer[3] = { 50, 70, 100 };
29 const int kTemporalRateAllocation2Layer[2] = { 60, 100 };
30 const int kSpatialLayerBitrate[3] = { 200, 500, 900 };
31 
32 // Parameter: aq mode: 0 and 3
33 class RcInterfaceTest : public ::libaom_test::EncoderTest,
34                         public ::libaom_test::CodecTestWithParam<int> {
35  public:
RcInterfaceTest()36   RcInterfaceTest()
37       : EncoderTest(GET_PARAM(0)), aq_mode_(GET_PARAM(1)), key_interval_(3000),
38         encoder_exit_(false), layer_frame_cnt_(0), superframe_cnt_(0),
39         frame_cnt_(0), dynamic_temporal_layers_(false),
40         dynamic_spatial_layers_(false), num_drops_(0), max_consec_drop_ms_(0),
41         frame_drop_thresh_(0) {
42     memset(&svc_params_, 0, sizeof(svc_params_));
43     memset(&layer_id_, 0, sizeof(layer_id_));
44   }
45 
46   ~RcInterfaceTest() override = default;
47 
48  protected:
SetUp()49   void SetUp() override { InitializeConfig(::libaom_test::kRealTime); }
50 
GetNumSpatialLayers()51   int GetNumSpatialLayers() override { return rc_cfg_.ss_number_layers; }
52 
PreEncodeFrameHook(libaom_test::VideoSource * video,libaom_test::Encoder * encoder)53   void PreEncodeFrameHook(libaom_test::VideoSource *video,
54                           libaom_test::Encoder *encoder) override {
55     int key_int = key_interval_;
56     const int use_svc =
57         rc_cfg_.ss_number_layers > 1 || rc_cfg_.ts_number_layers > 1;
58     encoder->Control(AV1E_SET_RTC_EXTERNAL_RC, 1);
59     if (video->frame() == 0 && layer_frame_cnt_ == 0) {
60       encoder->Control(AOME_SET_CPUUSED, 7);
61       encoder->Control(AV1E_SET_AQ_MODE, aq_mode_);
62       encoder->Control(AV1E_SET_ENABLE_ORDER_HINT, 0);
63       if (rc_cfg_.is_screen) {
64         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN);
65       } else {
66         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
67       }
68       encoder->Control(AOME_SET_MAX_INTRA_BITRATE_PCT,
69                        rc_cfg_.max_intra_bitrate_pct);
70       if (use_svc) encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
71       encoder->Control(AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR,
72                        max_consec_drop_ms_);
73     }
74     // SVC specific settings
75     if (use_svc) {
76       frame_params_.spatial_layer_id =
77           layer_frame_cnt_ % rc_cfg_.ss_number_layers;
78       if (rc_cfg_.ts_number_layers == 3)
79         frame_params_.temporal_layer_id =
80             kTemporalId3Layer[superframe_cnt_ % 4];
81       else if (rc_cfg_.ts_number_layers == 2)
82         frame_params_.temporal_layer_id =
83             kTemporalId2Layer[superframe_cnt_ % 2];
84       else
85         frame_params_.temporal_layer_id = 0;
86       layer_id_.spatial_layer_id = frame_params_.spatial_layer_id;
87       layer_id_.temporal_layer_id = frame_params_.temporal_layer_id;
88       encoder->Control(AV1E_SET_SVC_LAYER_ID, &layer_id_);
89       key_int = key_interval_ * rc_cfg_.ss_number_layers;
90     }
91     frame_params_.frame_type =
92         layer_frame_cnt_ % key_int == 0 ? aom::kKeyFrame : aom::kInterFrame;
93     encoder_exit_ = video->frame() == kNumFrames;
94     frame_flags_ = 0;
95 
96     if (dynamic_temporal_layers_) {
97       if (superframe_cnt_ == 100 && layer_id_.spatial_layer_id == 0) {
98         // Go down to 2 temporal layers.
99         SetConfigSvc(3, 2);
100         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
101         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
102       } else if (superframe_cnt_ == 200 && layer_id_.spatial_layer_id == 0) {
103         // Go down to 1 temporal layer.
104         SetConfigSvc(3, 1);
105         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
106         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
107       } else if (superframe_cnt_ == 300 && layer_id_.spatial_layer_id == 0) {
108         // Go back up to 3 temporal layers.
109         SetConfigSvc(3, 3);
110         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
111         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
112       }
113     } else if (dynamic_spatial_layers_) {
114       // In this example the #spatial layers is modified on the fly,
115       // so we go from (120p,240p,480p) to (240p,480p), etc.
116       if (superframe_cnt_ == 100 && layer_id_.spatial_layer_id == 0) {
117         // Change to 2 spatial layers (240p, 480p).
118         SetConfigSvc(2, 3);
119         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
120         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
121       } else if (superframe_cnt_ == 200 && layer_id_.spatial_layer_id == 0) {
122         // Change to 1 spatial layer (480p).
123         SetConfigSvc(1, 3);
124         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
125         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
126       } else if (superframe_cnt_ == 300 && layer_id_.spatial_layer_id == 0) {
127         // Go back to 3 spatial layers (120p, 240p, 480p).
128         SetConfigSvc(3, 3);
129         encoder->Control(AV1E_SET_SVC_PARAMS, &svc_params_);
130         // In the fixed SVC mode (which is what is used in this test):
131         // Key frame is required here on SL0 since 120p will try to predict
132         // from LAST which was the 480p, so decoder will throw an error
133         // (reference must be smaller than 4x4). In the flexible mode
134         // (not used here) we can set the frame flags to predict off the 2x2
135         // reference instead,
136         frame_flags_ = AOM_EFLAG_FORCE_KF;
137         frame_params_.frame_type = aom::kKeyFrame;
138         ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
139       }
140     }
141     // TODO(marpan): Add dynamic spatial layers based on 0 layer bitrate.
142     // That is actual usage in SW where configuration (#spatial, #temporal)
143     // layers is fixed, but top layer is dropped or re-enabled based on
144     // bitrate. This requires external RC to handle dropped (zero-size) frames.
145   }
146 
PostEncodeFrameHook(::libaom_test::Encoder * encoder)147   void PostEncodeFrameHook(::libaom_test::Encoder *encoder) override {
148     if (encoder_exit_) {
149       return;
150     }
151     layer_frame_cnt_++;
152     frame_cnt_++;
153     if (layer_id_.spatial_layer_id == rc_cfg_.ss_number_layers - 1)
154       superframe_cnt_++;
155     int qp;
156     encoder->Control(AOME_GET_LAST_QUANTIZER, &qp);
157     if (rc_api_->ComputeQP(frame_params_) == aom::kFrameDropDecisionOk) {
158       ASSERT_EQ(rc_api_->GetQP(), qp) << "at frame " << frame_cnt_ - 1;
159       int encoder_lpf_level;
160       encoder->Control(AOME_GET_LOOPFILTER_LEVEL, &encoder_lpf_level);
161       aom::AV1LoopfilterLevel loopfilter_level = rc_api_->GetLoopfilterLevel();
162       ASSERT_EQ(loopfilter_level.filter_level[0], encoder_lpf_level);
163       aom::AV1CdefInfo cdef_level = rc_api_->GetCdefInfo();
164       int cdef_y_strengths[16];
165       encoder->Control(AV1E_GET_LUMA_CDEF_STRENGTH, cdef_y_strengths);
166       ASSERT_EQ(cdef_level.cdef_strength_y, cdef_y_strengths[0]);
167     } else {
168       num_drops_++;
169     }
170   }
171 
FramePktHook(const aom_codec_cx_pkt_t * pkt)172   void FramePktHook(const aom_codec_cx_pkt_t *pkt) override {
173     if (layer_id_.spatial_layer_id == 0)
174       rc_api_->PostEncodeUpdate(pkt->data.frame.sz - 2);
175     else
176       rc_api_->PostEncodeUpdate(pkt->data.frame.sz);
177   }
178 
MismatchHook(const aom_image_t * img1,const aom_image_t * img2)179   void MismatchHook(const aom_image_t *img1, const aom_image_t *img2) override {
180     (void)img1;
181     (void)img2;
182   }
183 
RunOneLayer()184   void RunOneLayer() {
185     key_interval_ = 10000;
186     SetConfig();
187     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
188     frame_params_.spatial_layer_id = 0;
189     frame_params_.temporal_layer_id = 0;
190 
191     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
192                                          1, 0, kNumFrames);
193 
194     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
195   }
196 
RunOneLayerScreen()197   void RunOneLayerScreen() {
198     key_interval_ = 10000;
199     SetConfig();
200     rc_cfg_.is_screen = true;
201     rc_cfg_.width = 352;
202     rc_cfg_.height = 288;
203     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
204     frame_params_.spatial_layer_id = 0;
205     frame_params_.temporal_layer_id = 0;
206 
207     ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
208                                          288, 30, 1, 0, 140);
209 
210     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
211   }
212 
RunOneLayerDropFramesCBR()213   void RunOneLayerDropFramesCBR() {
214     key_interval_ = 10000;
215     max_consec_drop_ms_ = 250;
216     frame_drop_thresh_ = 30;
217     SetConfig();
218     rc_cfg_.target_bandwidth = 100;
219     cfg_.rc_target_bitrate = 100;
220     rc_cfg_.max_quantizer = 50;
221     cfg_.rc_max_quantizer = 50;
222     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
223     frame_params_.spatial_layer_id = 0;
224     frame_params_.temporal_layer_id = 0;
225 
226     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
227                                          1, 0, kNumFrames);
228 
229     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
230     // Check that some frames were dropped, otherwise test has no value.
231     ASSERT_GE(num_drops_, 1);
232   }
233 
RunOneLayerPeriodicKey()234   void RunOneLayerPeriodicKey() {
235     key_interval_ = 100;
236     SetConfig();
237     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
238     frame_params_.spatial_layer_id = 0;
239     frame_params_.temporal_layer_id = 0;
240 
241     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
242                                          1, 0, kNumFrames);
243 
244     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
245   }
246 
RunSvc()247   void RunSvc() {
248     key_interval_ = 10000;
249     SetConfigSvc(3, 3);
250     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
251     frame_params_.spatial_layer_id = 0;
252     frame_params_.temporal_layer_id = 0;
253 
254     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
255                                          1, 0, kNumFrames);
256 
257     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
258   }
259 
RunSvcPeriodicKey()260   void RunSvcPeriodicKey() {
261     key_interval_ = 100;
262     SetConfigSvc(3, 3);
263     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
264     frame_params_.spatial_layer_id = 0;
265     frame_params_.temporal_layer_id = 0;
266 
267     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
268                                          1, 0, kNumFrames);
269 
270     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
271   }
272 
RunSvcDynamicTemporal()273   void RunSvcDynamicTemporal() {
274     dynamic_temporal_layers_ = true;
275     key_interval_ = 10000;
276     SetConfigSvc(3, 3);
277     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
278     frame_params_.spatial_layer_id = 0;
279     frame_params_.temporal_layer_id = 0;
280 
281     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
282                                          1, 0, kNumFrames);
283 
284     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
285   }
286 
RunSvcDynamicSpatial()287   void RunSvcDynamicSpatial() {
288     dynamic_spatial_layers_ = true;
289     key_interval_ = 10000;
290     SetConfigSvc(3, 3);
291     rc_api_ = aom::AV1RateControlRTC::Create(rc_cfg_);
292     frame_params_.spatial_layer_id = 0;
293     frame_params_.temporal_layer_id = 0;
294 
295     ::libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480, 30,
296                                          1, 0, kNumFrames);
297 
298     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
299   }
300 
301  private:
SetConfig()302   void SetConfig() {
303     rc_cfg_.width = 640;
304     rc_cfg_.height = 480;
305     rc_cfg_.max_quantizer = 52;
306     rc_cfg_.min_quantizer = 2;
307     rc_cfg_.target_bandwidth = 1000;
308     rc_cfg_.buf_initial_sz = 600;
309     rc_cfg_.buf_optimal_sz = 600;
310     rc_cfg_.buf_sz = 1000;
311     rc_cfg_.undershoot_pct = 50;
312     rc_cfg_.overshoot_pct = 50;
313     rc_cfg_.max_intra_bitrate_pct = 1000;
314     rc_cfg_.framerate = 30.0;
315     rc_cfg_.ss_number_layers = 1;
316     rc_cfg_.ts_number_layers = 1;
317     rc_cfg_.scaling_factor_num[0] = 1;
318     rc_cfg_.scaling_factor_den[0] = 1;
319     rc_cfg_.layer_target_bitrate[0] = 1000;
320     rc_cfg_.max_quantizers[0] = 52;
321     rc_cfg_.min_quantizers[0] = 2;
322     rc_cfg_.aq_mode = aq_mode_;
323     rc_cfg_.frame_drop_thresh = frame_drop_thresh_;
324     rc_cfg_.max_consec_drop_ms = max_consec_drop_ms_;
325 
326     // Encoder settings for ground truth.
327     cfg_.g_w = 640;
328     cfg_.g_h = 480;
329     cfg_.rc_undershoot_pct = 50;
330     cfg_.rc_overshoot_pct = 50;
331     cfg_.rc_buf_initial_sz = 600;
332     cfg_.rc_buf_optimal_sz = 600;
333     cfg_.rc_buf_sz = 1000;
334     cfg_.rc_dropframe_thresh = 0;
335     cfg_.rc_min_quantizer = 2;
336     cfg_.rc_max_quantizer = 52;
337     cfg_.rc_end_usage = AOM_CBR;
338     cfg_.g_lag_in_frames = 0;
339     cfg_.g_error_resilient = 0;
340     cfg_.rc_target_bitrate = 1000;
341     cfg_.kf_min_dist = key_interval_;
342     cfg_.kf_max_dist = key_interval_;
343     cfg_.rc_dropframe_thresh = frame_drop_thresh_;
344   }
345 
SetConfigSvc(int number_spatial_layers,int number_temporal_layers)346   void SetConfigSvc(int number_spatial_layers, int number_temporal_layers) {
347     rc_cfg_.width = 640;
348     rc_cfg_.height = 480;
349     rc_cfg_.max_quantizer = 56;
350     rc_cfg_.min_quantizer = 2;
351     rc_cfg_.buf_initial_sz = 600;
352     rc_cfg_.buf_optimal_sz = 600;
353     rc_cfg_.buf_sz = 1000;
354     rc_cfg_.undershoot_pct = 50;
355     rc_cfg_.overshoot_pct = 50;
356     rc_cfg_.max_intra_bitrate_pct = 1000;
357     rc_cfg_.framerate = 30.0;
358     rc_cfg_.aq_mode = aq_mode_;
359     rc_cfg_.ss_number_layers = number_spatial_layers;
360     rc_cfg_.ts_number_layers = number_temporal_layers;
361 
362     // Encoder settings for ground truth.
363     cfg_.g_w = 640;
364     cfg_.g_h = 480;
365     cfg_.rc_max_quantizer = 56;
366     cfg_.rc_min_quantizer = 2;
367     cfg_.rc_buf_initial_sz = 600;
368     cfg_.rc_buf_optimal_sz = 600;
369     cfg_.rc_buf_sz = 1000;
370     cfg_.rc_overshoot_pct = 50;
371     cfg_.rc_undershoot_pct = 50;
372     cfg_.g_threads = 1;
373     cfg_.kf_min_dist = key_interval_;
374     cfg_.kf_max_dist = key_interval_;
375     cfg_.g_timebase.num = 1;
376     cfg_.g_timebase.den = 30;
377     cfg_.rc_end_usage = AOM_CBR;
378     cfg_.g_lag_in_frames = 0;
379     cfg_.g_error_resilient = 0;
380     svc_params_.number_spatial_layers = number_spatial_layers;
381     svc_params_.number_temporal_layers = number_temporal_layers;
382 
383     // Scale factors.
384     if (number_spatial_layers == 3) {
385       rc_cfg_.scaling_factor_num[0] = 1;
386       rc_cfg_.scaling_factor_den[0] = 4;
387       rc_cfg_.scaling_factor_num[1] = 2;
388       rc_cfg_.scaling_factor_den[1] = 4;
389       rc_cfg_.scaling_factor_num[2] = 4;
390       rc_cfg_.scaling_factor_den[2] = 4;
391       svc_params_.scaling_factor_num[0] = 1;
392       svc_params_.scaling_factor_den[0] = 4;
393       svc_params_.scaling_factor_num[1] = 2;
394       svc_params_.scaling_factor_den[1] = 4;
395       svc_params_.scaling_factor_num[2] = 4;
396       svc_params_.scaling_factor_den[2] = 4;
397     } else if (number_spatial_layers == 2) {
398       rc_cfg_.scaling_factor_num[0] = 1;
399       rc_cfg_.scaling_factor_den[0] = 2;
400       rc_cfg_.scaling_factor_num[1] = 2;
401       rc_cfg_.scaling_factor_den[1] = 2;
402       svc_params_.scaling_factor_num[0] = 1;
403       svc_params_.scaling_factor_den[0] = 2;
404       svc_params_.scaling_factor_num[1] = 2;
405       svc_params_.scaling_factor_den[1] = 2;
406     } else if (number_spatial_layers == 1) {
407       rc_cfg_.scaling_factor_num[0] = 1;
408       rc_cfg_.scaling_factor_den[0] = 1;
409       svc_params_.scaling_factor_num[0] = 1;
410       svc_params_.scaling_factor_den[0] = 1;
411     }
412 
413     // TS rate decimator.
414     if (number_temporal_layers == 3) {
415       rc_cfg_.ts_rate_decimator[0] = 4;
416       rc_cfg_.ts_rate_decimator[1] = 2;
417       rc_cfg_.ts_rate_decimator[2] = 1;
418       svc_params_.framerate_factor[0] = 4;
419       svc_params_.framerate_factor[1] = 2;
420       svc_params_.framerate_factor[2] = 1;
421     } else if (number_temporal_layers == 2) {
422       rc_cfg_.ts_rate_decimator[0] = 2;
423       rc_cfg_.ts_rate_decimator[1] = 1;
424       svc_params_.framerate_factor[0] = 2;
425       svc_params_.framerate_factor[1] = 1;
426     } else if (number_temporal_layers == 1) {
427       rc_cfg_.ts_rate_decimator[0] = 1;
428       svc_params_.framerate_factor[0] = 1;
429     }
430 
431     // Bitate.
432     rc_cfg_.target_bandwidth = 0;
433     cfg_.rc_target_bitrate = 0;
434     for (int sl = 0; sl < number_spatial_layers; sl++) {
435       int spatial_bitrate = 0;
436       if (number_spatial_layers <= 3)
437         spatial_bitrate = kSpatialLayerBitrate[sl];
438       for (int tl = 0; tl < number_temporal_layers; tl++) {
439         int layer = sl * number_temporal_layers + tl;
440         if (number_temporal_layers == 3) {
441           rc_cfg_.layer_target_bitrate[layer] =
442               kTemporalRateAllocation3Layer[tl] * spatial_bitrate / 100;
443           svc_params_.layer_target_bitrate[layer] =
444               kTemporalRateAllocation3Layer[tl] * spatial_bitrate / 100;
445         } else if (number_temporal_layers == 2) {
446           rc_cfg_.layer_target_bitrate[layer] =
447               kTemporalRateAllocation2Layer[tl] * spatial_bitrate / 100;
448           svc_params_.layer_target_bitrate[layer] =
449               kTemporalRateAllocation2Layer[tl] * spatial_bitrate / 100;
450         } else if (number_temporal_layers == 1) {
451           rc_cfg_.layer_target_bitrate[layer] = spatial_bitrate;
452           svc_params_.layer_target_bitrate[layer] = spatial_bitrate;
453         }
454       }
455       rc_cfg_.target_bandwidth += spatial_bitrate;
456       cfg_.rc_target_bitrate += spatial_bitrate;
457     }
458 
459     // Layer min/max quantizer.
460     for (int sl = 0; sl < number_spatial_layers; ++sl) {
461       for (int tl = 0; tl < number_temporal_layers; ++tl) {
462         const int i = sl * number_temporal_layers + tl;
463         rc_cfg_.max_quantizers[i] = rc_cfg_.max_quantizer;
464         rc_cfg_.min_quantizers[i] = rc_cfg_.min_quantizer;
465         svc_params_.max_quantizers[i] = cfg_.rc_max_quantizer;
466         svc_params_.min_quantizers[i] = cfg_.rc_min_quantizer;
467       }
468     }
469   }
470 
471   std::unique_ptr<aom::AV1RateControlRTC> rc_api_;
472   aom::AV1RateControlRtcConfig rc_cfg_;
473   int aq_mode_;
474   int key_interval_;
475   aom::AV1FrameParamsRTC frame_params_;
476   bool encoder_exit_;
477   aom_svc_params_t svc_params_;
478   aom_svc_layer_id_t layer_id_;
479   int layer_frame_cnt_;
480   int superframe_cnt_;
481   int frame_cnt_;
482   bool dynamic_temporal_layers_;
483   bool dynamic_spatial_layers_;
484   int num_drops_;
485   int max_consec_drop_ms_;
486   int frame_drop_thresh_;
487 };
488 
489 class RcExternMethodsInterfaceTest
490     : public ::libaom_test::EncoderTest,
491       public ::libaom_test::CodecTestWithParam<int> {
492  public:
RcExternMethodsInterfaceTest()493   RcExternMethodsInterfaceTest()
494       : EncoderTest(GET_PARAM(0)), aq_mode_(GET_PARAM(1)) {
495     SetConfig();
496   }
497   ~RcExternMethodsInterfaceTest() = default;
498 
499   // Test APIS
500   void TestCreateRateControl();
501   void TestUpdateRateControl();
502   void TestGetLoopFilterLevelRateControl();
503   void TestPostEncodeUpdateRateControl();
504   void TestGetQPRateControl();
505   void TestComputeQPRateControl();
506   void TestGetSegmentationDataRateControl();
507   void TestGetCdefInfoRateControl();
508   void TestCreateRateControlConfig();
509   void TestDestroyRateControlRTC();
510   void SetConfig();
511 
512  private:
513   aom::AV1RateControlRtcConfig rc_cfg_;
514   int aq_mode_;
515   aom::AV1FrameParamsRTC frame_params_;
516 };
517 
SetConfig()518 void RcExternMethodsInterfaceTest::SetConfig() {
519   rc_cfg_.width = 640;
520   rc_cfg_.height = 480;
521   rc_cfg_.max_quantizer = 52;
522   rc_cfg_.min_quantizer = 2;
523   rc_cfg_.target_bandwidth = 1000;
524   rc_cfg_.buf_initial_sz = 600;
525   rc_cfg_.buf_optimal_sz = 600;
526   rc_cfg_.buf_sz = 1000;
527   rc_cfg_.undershoot_pct = 50;
528   rc_cfg_.overshoot_pct = 50;
529   rc_cfg_.max_intra_bitrate_pct = 1000;
530   rc_cfg_.framerate = 30.0;
531   rc_cfg_.ss_number_layers = 1;
532   rc_cfg_.ts_number_layers = 1;
533   rc_cfg_.scaling_factor_num[0] = 1;
534   rc_cfg_.scaling_factor_den[0] = 1;
535   rc_cfg_.layer_target_bitrate[0] = 1000;
536   rc_cfg_.max_quantizers[0] = 52;
537   rc_cfg_.min_quantizers[0] = 2;
538   rc_cfg_.aq_mode = aq_mode_;
539 }
540 
TestCreateRateControl()541 void RcExternMethodsInterfaceTest::TestCreateRateControl() {
542   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
543 
544   ASSERT_NE(controller, nullptr);
545 
546   av1_ratecontrol_rtc_destroy(controller);
547 }
548 
TestUpdateRateControl()549 void RcExternMethodsInterfaceTest::TestUpdateRateControl() {
550   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
551 
552   ASSERT_NE(controller, nullptr);
553 
554   ASSERT_TRUE(av1_ratecontrol_rtc_update(controller, &rc_cfg_));
555 
556   av1_ratecontrol_rtc_destroy(controller);
557 }
558 
TestGetQPRateControl()559 void RcExternMethodsInterfaceTest::TestGetQPRateControl() {
560   frame_params_.spatial_layer_id = 0;
561   frame_params_.temporal_layer_id = 0;
562   frame_params_.frame_type = kAomKeyFrame;
563 
564   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
565   ASSERT_NE(controller, nullptr);
566 
567   const AomFrameDropDecision decision =
568       av1_ratecontrol_rtc_compute_qp(controller, &frame_params_);
569   if (decision == kAomFrameDropDecisionOk) {
570     int qp = av1_ratecontrol_rtc_get_qp(controller);
571     ASSERT_NE(qp, 0);  // 0 is invalid for qp
572   }
573   av1_ratecontrol_rtc_destroy(controller);
574 }
575 
TestComputeQPRateControl()576 void RcExternMethodsInterfaceTest::TestComputeQPRateControl() {
577   frame_params_.spatial_layer_id = 0;
578   frame_params_.temporal_layer_id = 0;
579   frame_params_.frame_type = kAomKeyFrame;
580 
581   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
582   ASSERT_NE(controller, nullptr);
583 
584   const AomFrameDropDecision decision =
585       av1_ratecontrol_rtc_compute_qp(controller, &frame_params_);
586   ASSERT_EQ(decision, kAomFrameDropDecisionOk);
587   av1_ratecontrol_rtc_destroy(controller);
588 }
589 
TestGetLoopFilterLevelRateControl()590 void RcExternMethodsInterfaceTest::TestGetLoopFilterLevelRateControl() {
591   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
592   ASSERT_NE(controller, nullptr);
593 
594   AomAV1LoopfilterLevel lpf_level;
595   lpf_level.filter_level[0] = 0xfdbd;
596   lpf_level.filter_level[1] = 0xfdbd;
597   lpf_level.filter_level_u = 0xfdbd;
598   lpf_level.filter_level_v = 0xfdbd;
599 
600   lpf_level = av1_ratecontrol_rtc_get_loop_filter_level(controller);
601 
602   ASSERT_NE(lpf_level.filter_level[0], 0xfdbd);
603   ASSERT_NE(lpf_level.filter_level[1], 0xfdbd);
604   ASSERT_NE(lpf_level.filter_level_u, 0xfdbd);
605   ASSERT_NE(lpf_level.filter_level_v, 0xfdbd);
606 
607   av1_ratecontrol_rtc_destroy(controller);
608 }
609 
TestPostEncodeUpdateRateControl()610 void RcExternMethodsInterfaceTest::TestPostEncodeUpdateRateControl() {
611   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
612   ASSERT_NE(controller, nullptr);
613   av1_ratecontrol_rtc_post_encode_update(controller, 380);
614   av1_ratecontrol_rtc_destroy(controller);
615 }
616 
TestGetSegmentationDataRateControl()617 void RcExternMethodsInterfaceTest::TestGetSegmentationDataRateControl() {
618   rc_cfg_.aq_mode = 1;  // VARIANCE_AQ = 1
619   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
620   ASSERT_NE(controller, nullptr);
621   AomAV1SegmentationData segmentation_data;
622   // This should return false as this test case doesn't run any part of the
623   // underlying rate control, and cyclic refresh will not be turned on.
624   ASSERT_FALSE(
625       av1_ratecontrol_rtc_get_segmentation(controller, &segmentation_data));
626   av1_ratecontrol_rtc_destroy(controller);
627 }
628 
TestGetCdefInfoRateControl()629 void RcExternMethodsInterfaceTest::TestGetCdefInfoRateControl() {
630   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
631   ASSERT_NE(controller, nullptr);
632   AomAV1CdefInfo cdef_level;
633   cdef_level.cdef_strength_y = 0xabcd;
634   cdef_level.cdef_strength_uv = 0xabcd;
635   cdef_level.damping = 0xabcd;
636   cdef_level = av1_ratecontrol_rtc_get_cdef_info(controller);
637 
638   ASSERT_NE(cdef_level.cdef_strength_y, 0xabcd);
639   ASSERT_NE(cdef_level.cdef_strength_uv, 0xabcd);
640   ASSERT_NE(cdef_level.damping, 0xabcd);
641 
642   av1_ratecontrol_rtc_destroy(controller);
643 }
644 
TestCreateRateControlConfig()645 void RcExternMethodsInterfaceTest::TestCreateRateControlConfig() {
646   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
647   ASSERT_NE(controller, nullptr);
648 
649   AomAV1RateControlRtcConfig config;
650   av1_ratecontrol_rtc_init_ratecontrol_config(&config);
651   ASSERT_EQ(config.width, 1280);
652   ASSERT_EQ(config.height, 720);
653   // only width and height is checked. can be extended
654 
655   av1_ratecontrol_rtc_destroy(controller);
656 }
657 
TestDestroyRateControlRTC()658 void RcExternMethodsInterfaceTest::TestDestroyRateControlRTC() {
659   AomAV1RateControlRTC *controller = av1_ratecontrol_rtc_create(&rc_cfg_);
660   ASSERT_NE(controller, nullptr);
661 
662   av1_ratecontrol_rtc_destroy(controller);
663 }
664 
TEST_P(RcInterfaceTest,OneLayer)665 TEST_P(RcInterfaceTest, OneLayer) { RunOneLayer(); }
666 
TEST_P(RcInterfaceTest,OneLayerDropFramesCBR)667 TEST_P(RcInterfaceTest, OneLayerDropFramesCBR) { RunOneLayerDropFramesCBR(); }
668 
TEST_P(RcInterfaceTest,OneLayerPeriodicKey)669 TEST_P(RcInterfaceTest, OneLayerPeriodicKey) { RunOneLayerPeriodicKey(); }
670 
TEST_P(RcInterfaceTest,OneLayerScreen)671 TEST_P(RcInterfaceTest, OneLayerScreen) { RunOneLayerScreen(); }
672 
TEST_P(RcInterfaceTest,Svc)673 TEST_P(RcInterfaceTest, Svc) { RunSvc(); }
674 
TEST_P(RcInterfaceTest,SvcPeriodicKey)675 TEST_P(RcInterfaceTest, SvcPeriodicKey) { RunSvcPeriodicKey(); }
676 
TEST_P(RcInterfaceTest,SvcDynamicTemporal)677 TEST_P(RcInterfaceTest, SvcDynamicTemporal) { RunSvcDynamicTemporal(); }
678 
TEST_P(RcInterfaceTest,SvcDynamicSpatial)679 TEST_P(RcInterfaceTest, SvcDynamicSpatial) { RunSvcDynamicSpatial(); }
680 
TEST_P(RcExternMethodsInterfaceTest,CreateRateControlTest)681 TEST_P(RcExternMethodsInterfaceTest, CreateRateControlTest) {
682   TestCreateRateControl();
683 }
684 
TEST_P(RcExternMethodsInterfaceTest,UpdateRateControllerTest)685 TEST_P(RcExternMethodsInterfaceTest, UpdateRateControllerTest) {
686   TestUpdateRateControl();
687 }
688 
TEST_P(RcExternMethodsInterfaceTest,GetQpRateControllerTest)689 TEST_P(RcExternMethodsInterfaceTest, GetQpRateControllerTest) {
690   TestGetQPRateControl();
691 }
692 
TEST_P(RcExternMethodsInterfaceTest,ComputeQPRateControllerTest)693 TEST_P(RcExternMethodsInterfaceTest, ComputeQPRateControllerTest) {
694   TestComputeQPRateControl();
695 }
696 
TEST_P(RcExternMethodsInterfaceTest,GetLoopFilterLevelRateControllerTest)697 TEST_P(RcExternMethodsInterfaceTest, GetLoopFilterLevelRateControllerTest) {
698   TestGetLoopFilterLevelRateControl();
699 }
700 
TEST_P(RcExternMethodsInterfaceTest,PostEncodeUpdateRateControllerTest)701 TEST_P(RcExternMethodsInterfaceTest, PostEncodeUpdateRateControllerTest) {
702   TestPostEncodeUpdateRateControl();
703 }
704 
TEST_P(RcExternMethodsInterfaceTest,GetSegmenationDataRateControllerTest)705 TEST_P(RcExternMethodsInterfaceTest, GetSegmenationDataRateControllerTest) {
706   TestGetSegmentationDataRateControl();
707 }
708 
TEST_P(RcExternMethodsInterfaceTest,GetCdedInfoRateControllerTest)709 TEST_P(RcExternMethodsInterfaceTest, GetCdedInfoRateControllerTest) {
710   TestGetCdefInfoRateControl();
711 }
712 
TEST_P(RcExternMethodsInterfaceTest,CreateRateControlConfigTest)713 TEST_P(RcExternMethodsInterfaceTest, CreateRateControlConfigTest) {
714   TestCreateRateControlConfig();
715 }
716 
TEST_P(RcExternMethodsInterfaceTest,DestroyRateControlRTCTest)717 TEST_P(RcExternMethodsInterfaceTest, DestroyRateControlRTCTest) {
718   TestDestroyRateControlRTC();
719 }
720 
721 AV1_INSTANTIATE_TEST_SUITE(RcInterfaceTest, ::testing::Values(0, 3));
722 AV1_INSTANTIATE_TEST_SUITE(RcExternMethodsInterfaceTest,
723                            ::testing::Values(0, 3));
724 
725 }  // namespace
726