xref: /aosp_15_r20/external/libvpx/test/vp8_ratectrl_rtc_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2021 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <fstream>  // NOLINT
12 #include <string>
13 
14 #include "./vpx_config.h"
15 #include "gtest/gtest.h"
16 #include "test/codec_factory.h"
17 #include "test/encode_test_driver.h"
18 #include "test/i420_video_source.h"
19 #include "test/util.h"
20 #include "test/video_source.h"
21 #include "vp8/vp8_ratectrl_rtc.h"
22 #include "vpx/vpx_codec.h"
23 #include "vpx_ports/bitops.h"
24 
25 namespace {
26 
27 struct Vp8RCTestVideo {
28   Vp8RCTestVideo() = default;
Vp8RCTestVideo__anon3be2e9f30111::Vp8RCTestVideo29   Vp8RCTestVideo(const char *name_, int width_, int height_,
30                  unsigned int frames_)
31       : name(name_), width(width_), height(height_), frames(frames_) {}
32 
operator <<(std::ostream & os,const Vp8RCTestVideo & video)33   friend std::ostream &operator<<(std::ostream &os,
34                                   const Vp8RCTestVideo &video) {
35     os << video.name << " " << video.width << " " << video.height << " "
36        << video.frames;
37     return os;
38   }
39   const char *name;
40   int width;
41   int height;
42   unsigned int frames;
43 };
44 
45 const Vp8RCTestVideo kVp8RCTestVectors[] = {
46   Vp8RCTestVideo("niklas_640_480_30.yuv", 640, 480, 470),
47   Vp8RCTestVideo("desktop_office1.1280_720-020.yuv", 1280, 720, 300),
48   Vp8RCTestVideo("hantro_collage_w352h288.yuv", 352, 288, 100),
49 };
50 
51 class Vp8RcInterfaceTest
52     : public ::libvpx_test::EncoderTest,
53       public ::libvpx_test::CodecTestWith2Params<int, Vp8RCTestVideo> {
54  public:
Vp8RcInterfaceTest()55   Vp8RcInterfaceTest()
56       : EncoderTest(GET_PARAM(0)), key_interval_(3000), encoder_exit_(false),
57         frame_drop_thresh_(0) {}
58   ~Vp8RcInterfaceTest() override = default;
59 
60  protected:
SetUp()61   void SetUp() override {
62     InitializeConfig();
63     SetMode(::libvpx_test::kRealTime);
64   }
65 
66   // From error_resilience_test.cc
SetFrameFlags(int frame_num,int num_temp_layers)67   int SetFrameFlags(int frame_num, int num_temp_layers) {
68     int frame_flags = 0;
69     if (num_temp_layers == 2) {
70       if (frame_num % 2 == 0) {
71         // Layer 0: predict from L and ARF, update L.
72         frame_flags =
73             VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
74       } else {
75         // Layer 1: predict from L, G and ARF, and update G.
76         frame_flags = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
77                       VP8_EFLAG_NO_UPD_ENTROPY;
78       }
79     } else if (num_temp_layers == 3) {
80       if (frame_num % 4 == 0) {
81         // Layer 0: predict from L, update L.
82         frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
83                       VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF;
84       } else if ((frame_num - 2) % 4 == 0) {
85         // Layer 1: predict from L, G,  update G.
86         frame_flags =
87             VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_REF_ARF;
88       } else if ((frame_num - 1) % 2 == 0) {
89         // Layer 2: predict from L, G, ARF; update ARG.
90         frame_flags = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
91       }
92     }
93     return frame_flags;
94   }
95 
SetLayerId(int frame_num,int num_temp_layers)96   int SetLayerId(int frame_num, int num_temp_layers) {
97     int layer_id = 0;
98     if (num_temp_layers == 2) {
99       if (frame_num % 2 == 0) {
100         layer_id = 0;
101       } else {
102         layer_id = 1;
103       }
104     } else if (num_temp_layers == 3) {
105       if (frame_num % 4 == 0) {
106         layer_id = 0;
107       } else if ((frame_num - 2) % 4 == 0) {
108         layer_id = 1;
109       } else if ((frame_num - 1) % 2 == 0) {
110         layer_id = 2;
111       }
112     }
113     return layer_id;
114   }
115 
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)116   void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
117                           ::libvpx_test::Encoder *encoder) override {
118     if (rc_cfg_.ts_number_layers > 1) {
119       const int layer_id = SetLayerId(video->frame(), cfg_.ts_number_layers);
120       const int frame_flags =
121           SetFrameFlags(video->frame(), cfg_.ts_number_layers);
122       frame_params_.temporal_layer_id = layer_id;
123       if (video->frame() > 0) {
124         encoder->Control(VP8E_SET_TEMPORAL_LAYER_ID, layer_id);
125         encoder->Control(VP8E_SET_FRAME_FLAGS, frame_flags);
126       }
127     } else {
128       if (video->frame() == 0) {
129         encoder->Control(VP8E_SET_CPUUSED, -6);
130         encoder->Control(VP8E_SET_RTC_EXTERNAL_RATECTRL, 1);
131         encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 1000);
132         if (rc_cfg_.is_screen) {
133           encoder->Control(VP8E_SET_SCREEN_CONTENT_MODE, 1);
134         }
135       } else if (frame_params_.frame_type == libvpx::RcFrameType::kInterFrame) {
136         // Disable golden frame update.
137         frame_flags_ |= VP8_EFLAG_NO_UPD_GF;
138         frame_flags_ |= VP8_EFLAG_NO_UPD_ARF;
139       }
140     }
141     frame_params_.frame_type = video->frame() % key_interval_ == 0
142                                    ? libvpx::RcFrameType::kKeyFrame
143                                    : libvpx::RcFrameType::kInterFrame;
144     encoder_exit_ = video->frame() == test_video_.frames;
145   }
146 
PostEncodeFrameHook(::libvpx_test::Encoder * encoder)147   void PostEncodeFrameHook(::libvpx_test::Encoder *encoder) override {
148     if (encoder_exit_) {
149       return;
150     }
151     int qp;
152     libvpx::UVDeltaQP uv_delta_qp;
153     encoder->Control(VP8E_GET_LAST_QUANTIZER, &qp);
154     if (rc_api_->ComputeQP(frame_params_) == libvpx::FrameDropDecision::kOk) {
155       ASSERT_EQ(rc_api_->GetQP(), qp);
156       uv_delta_qp = rc_api_->GetUVDeltaQP();
157       // delta_qp for UV channel is only set for screen.
158       if (!rc_cfg_.is_screen) {
159         ASSERT_EQ(uv_delta_qp.uvdc_delta_q, 0);
160         ASSERT_EQ(uv_delta_qp.uvac_delta_q, 0);
161       }
162     } else {
163       num_drops_++;
164     }
165   }
166 
FramePktHook(const vpx_codec_cx_pkt_t * pkt)167   void FramePktHook(const vpx_codec_cx_pkt_t *pkt) override {
168     rc_api_->PostEncodeUpdate(pkt->data.frame.sz);
169   }
170 
RunOneLayer()171   void RunOneLayer() {
172     test_video_ = GET_PARAM(2);
173     target_bitrate_ = GET_PARAM(1);
174     SetConfig();
175     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
176     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
177 
178     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
179                                          test_video_.height, 30, 1, 0,
180                                          test_video_.frames);
181 
182     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
183   }
184 
RunOneLayerScreen()185   void RunOneLayerScreen() {
186     test_video_ = GET_PARAM(2);
187     target_bitrate_ = GET_PARAM(1);
188     SetConfig();
189     rc_cfg_.is_screen = true;
190     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
191     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
192 
193     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
194                                          test_video_.height, 30, 1, 0,
195                                          test_video_.frames);
196 
197     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
198   }
199 
RunOneLayerDropFrames()200   void RunOneLayerDropFrames() {
201     test_video_ = GET_PARAM(2);
202     target_bitrate_ = GET_PARAM(1);
203     frame_drop_thresh_ = 30;
204     num_drops_ = 0;
205     // Use lower target_bitrate and max_quantizer to trigger drops.
206     target_bitrate_ = target_bitrate_ >> 2;
207     SetConfig();
208     rc_cfg_.max_quantizer = 56;
209     cfg_.rc_max_quantizer = 56;
210     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
211     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
212 
213     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
214                                          test_video_.height, 30, 1, 0,
215                                          test_video_.frames);
216 
217     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
218     // Check that some frames were dropped, otherwise test has no value.
219     ASSERT_GE(num_drops_, 1);
220   }
221 
RunPeriodicKey()222   void RunPeriodicKey() {
223     test_video_ = GET_PARAM(2);
224     target_bitrate_ = GET_PARAM(1);
225     key_interval_ = 100;
226     frame_drop_thresh_ = 30;
227     SetConfig();
228     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
229     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
230 
231     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
232                                          test_video_.height, 30, 1, 0,
233                                          test_video_.frames);
234 
235     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
236   }
237 
RunTemporalLayers2TL()238   void RunTemporalLayers2TL() {
239     test_video_ = GET_PARAM(2);
240     target_bitrate_ = GET_PARAM(1);
241     SetConfigTemporalLayers(2);
242     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
243     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
244 
245     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
246                                          test_video_.height, 30, 1, 0,
247                                          test_video_.frames);
248 
249     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
250   }
251 
RunTemporalLayers3TL()252   void RunTemporalLayers3TL() {
253     test_video_ = GET_PARAM(2);
254     target_bitrate_ = GET_PARAM(1);
255     SetConfigTemporalLayers(3);
256     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
257     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
258 
259     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
260                                          test_video_.height, 30, 1, 0,
261                                          test_video_.frames);
262 
263     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
264   }
265 
RunTemporalLayers3TLDropFrames()266   void RunTemporalLayers3TLDropFrames() {
267     test_video_ = GET_PARAM(2);
268     target_bitrate_ = GET_PARAM(1);
269     frame_drop_thresh_ = 30;
270     num_drops_ = 0;
271     // Use lower target_bitrate and max_quantizer to trigger drops.
272     target_bitrate_ = target_bitrate_ >> 2;
273     SetConfigTemporalLayers(3);
274     rc_cfg_.max_quantizer = 56;
275     cfg_.rc_max_quantizer = 56;
276     rc_api_ = libvpx::VP8RateControlRTC::Create(rc_cfg_);
277     ASSERT_TRUE(rc_api_->UpdateRateControl(rc_cfg_));
278 
279     ::libvpx_test::I420VideoSource video(test_video_.name, test_video_.width,
280                                          test_video_.height, 30, 1, 0,
281                                          test_video_.frames);
282 
283     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
284     // Check that some frames were dropped, otherwise test has no value.
285     ASSERT_GE(num_drops_, 1);
286   }
287 
288  private:
SetConfig()289   void SetConfig() {
290     rc_cfg_.width = test_video_.width;
291     rc_cfg_.height = test_video_.height;
292     rc_cfg_.max_quantizer = 60;
293     rc_cfg_.min_quantizer = 2;
294     rc_cfg_.target_bandwidth = target_bitrate_;
295     rc_cfg_.buf_initial_sz = 600;
296     rc_cfg_.buf_optimal_sz = 600;
297     rc_cfg_.buf_sz = target_bitrate_;
298     rc_cfg_.undershoot_pct = 50;
299     rc_cfg_.overshoot_pct = 50;
300     rc_cfg_.max_intra_bitrate_pct = 1000;
301     rc_cfg_.framerate = 30.0;
302     rc_cfg_.layer_target_bitrate[0] = target_bitrate_;
303     rc_cfg_.frame_drop_thresh = frame_drop_thresh_;
304 
305     // Encoder settings for ground truth.
306     cfg_.g_w = test_video_.width;
307     cfg_.g_h = test_video_.height;
308     cfg_.rc_undershoot_pct = 50;
309     cfg_.rc_overshoot_pct = 50;
310     cfg_.rc_buf_initial_sz = 600;
311     cfg_.rc_buf_optimal_sz = 600;
312     cfg_.rc_buf_sz = target_bitrate_;
313     cfg_.rc_dropframe_thresh = 0;
314     cfg_.rc_min_quantizer = 2;
315     cfg_.rc_max_quantizer = 60;
316     cfg_.rc_end_usage = VPX_CBR;
317     cfg_.g_lag_in_frames = 0;
318     cfg_.g_error_resilient = 1;
319     cfg_.rc_target_bitrate = target_bitrate_;
320     cfg_.kf_min_dist = key_interval_;
321     cfg_.kf_max_dist = key_interval_;
322     cfg_.rc_dropframe_thresh = frame_drop_thresh_;
323   }
324 
SetConfigTemporalLayers(int temporal_layers)325   void SetConfigTemporalLayers(int temporal_layers) {
326     rc_cfg_.width = test_video_.width;
327     rc_cfg_.height = test_video_.height;
328     rc_cfg_.max_quantizer = 60;
329     rc_cfg_.min_quantizer = 2;
330     rc_cfg_.target_bandwidth = target_bitrate_;
331     rc_cfg_.buf_initial_sz = 600;
332     rc_cfg_.buf_optimal_sz = 600;
333     rc_cfg_.buf_sz = target_bitrate_;
334     rc_cfg_.undershoot_pct = 50;
335     rc_cfg_.overshoot_pct = 50;
336     rc_cfg_.max_intra_bitrate_pct = 1000;
337     rc_cfg_.framerate = 30.0;
338     rc_cfg_.frame_drop_thresh = frame_drop_thresh_;
339     if (temporal_layers == 2) {
340       rc_cfg_.layer_target_bitrate[0] = 60 * target_bitrate_ / 100;
341       rc_cfg_.layer_target_bitrate[1] = target_bitrate_;
342       rc_cfg_.ts_rate_decimator[0] = 2;
343       rc_cfg_.ts_rate_decimator[1] = 1;
344     } else if (temporal_layers == 3) {
345       rc_cfg_.layer_target_bitrate[0] = 40 * target_bitrate_ / 100;
346       rc_cfg_.layer_target_bitrate[1] = 60 * target_bitrate_ / 100;
347       rc_cfg_.layer_target_bitrate[2] = target_bitrate_;
348       rc_cfg_.ts_rate_decimator[0] = 4;
349       rc_cfg_.ts_rate_decimator[1] = 2;
350       rc_cfg_.ts_rate_decimator[2] = 1;
351     }
352 
353     rc_cfg_.ts_number_layers = temporal_layers;
354 
355     // Encoder settings for ground truth.
356     cfg_.g_w = test_video_.width;
357     cfg_.g_h = test_video_.height;
358     cfg_.rc_undershoot_pct = 50;
359     cfg_.rc_overshoot_pct = 50;
360     cfg_.rc_buf_initial_sz = 600;
361     cfg_.rc_buf_optimal_sz = 600;
362     cfg_.rc_buf_sz = target_bitrate_;
363     cfg_.rc_dropframe_thresh = 0;
364     cfg_.rc_min_quantizer = 2;
365     cfg_.rc_max_quantizer = 60;
366     cfg_.rc_end_usage = VPX_CBR;
367     cfg_.g_lag_in_frames = 0;
368     cfg_.g_error_resilient = 1;
369     cfg_.rc_target_bitrate = target_bitrate_;
370     cfg_.kf_min_dist = key_interval_;
371     cfg_.kf_max_dist = key_interval_;
372     cfg_.rc_dropframe_thresh = frame_drop_thresh_;
373     // 2 Temporal layers, no spatial layers, CBR mode.
374     cfg_.ss_number_layers = 1;
375     cfg_.ts_number_layers = temporal_layers;
376     if (temporal_layers == 2) {
377       cfg_.ts_rate_decimator[0] = 2;
378       cfg_.ts_rate_decimator[1] = 1;
379       cfg_.ts_periodicity = 2;
380       cfg_.ts_target_bitrate[0] = 60 * cfg_.rc_target_bitrate / 100;
381       cfg_.ts_target_bitrate[1] = cfg_.rc_target_bitrate;
382     } else if (temporal_layers == 3) {
383       cfg_.ts_rate_decimator[0] = 4;
384       cfg_.ts_rate_decimator[1] = 2;
385       cfg_.ts_rate_decimator[2] = 1;
386       cfg_.ts_periodicity = 4;
387       cfg_.ts_target_bitrate[0] = 40 * cfg_.rc_target_bitrate / 100;
388       cfg_.ts_target_bitrate[1] = 60 * cfg_.rc_target_bitrate / 100;
389       cfg_.ts_target_bitrate[2] = cfg_.rc_target_bitrate;
390     }
391   }
392 
393   std::unique_ptr<libvpx::VP8RateControlRTC> rc_api_;
394   libvpx::VP8RateControlRtcConfig rc_cfg_;
395   int key_interval_;
396   int target_bitrate_;
397   Vp8RCTestVideo test_video_;
398   libvpx::VP8FrameParamsQpRTC frame_params_;
399   bool encoder_exit_;
400   int frame_drop_thresh_;
401   int num_drops_;
402 };
403 
TEST_P(Vp8RcInterfaceTest,OneLayer)404 TEST_P(Vp8RcInterfaceTest, OneLayer) { RunOneLayer(); }
405 
TEST_P(Vp8RcInterfaceTest,OneLayerScreen)406 TEST_P(Vp8RcInterfaceTest, OneLayerScreen) { RunOneLayerScreen(); }
407 
TEST_P(Vp8RcInterfaceTest,OneLayerDropFrames)408 TEST_P(Vp8RcInterfaceTest, OneLayerDropFrames) { RunOneLayerDropFrames(); }
409 
TEST_P(Vp8RcInterfaceTest,OneLayerPeriodicKey)410 TEST_P(Vp8RcInterfaceTest, OneLayerPeriodicKey) { RunPeriodicKey(); }
411 
TEST_P(Vp8RcInterfaceTest,TemporalLayers2TL)412 TEST_P(Vp8RcInterfaceTest, TemporalLayers2TL) { RunTemporalLayers2TL(); }
413 
TEST_P(Vp8RcInterfaceTest,TemporalLayers3TL)414 TEST_P(Vp8RcInterfaceTest, TemporalLayers3TL) { RunTemporalLayers3TL(); }
415 
TEST_P(Vp8RcInterfaceTest,TemporalLayers3TLDropFrames)416 TEST_P(Vp8RcInterfaceTest, TemporalLayers3TLDropFrames) {
417   RunTemporalLayers3TLDropFrames();
418 }
419 
420 VP8_INSTANTIATE_TEST_SUITE(Vp8RcInterfaceTest,
421                            ::testing::Values(200, 400, 1000),
422                            ::testing::ValuesIn(kVp8RCTestVectors));
423 
424 }  // namespace
425