datarate_test.h (10419B)
1 /* 2 * Copyright (c) 2019, 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 "config/aom_config.h" 13 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 #include "aom/aom_codec.h" 21 22 namespace datarate_test { 23 namespace { 24 class DatarateTest : public ::libaom_test::EncoderTest { 25 public: 26 explicit DatarateTest(const ::libaom_test::CodecFactory *codec) 27 : EncoderTest(codec), set_cpu_used_(0), aq_mode_(0), 28 speed_change_test_(false) {} 29 30 protected: 31 ~DatarateTest() override = default; 32 33 virtual void SetUpCBR() { 34 cfg_.rc_buf_initial_sz = 500; 35 cfg_.rc_buf_optimal_sz = 500; 36 cfg_.rc_buf_sz = 1000; 37 cfg_.rc_dropframe_thresh = 1; 38 cfg_.rc_min_quantizer = 0; 39 cfg_.rc_max_quantizer = 63; 40 cfg_.rc_end_usage = AOM_CBR; 41 cfg_.g_lag_in_frames = 0; 42 } 43 44 virtual void ResetModel() { 45 last_pts_ = 0; 46 bits_in_buffer_model_ = cfg_.rc_target_bitrate * cfg_.rc_buf_initial_sz; 47 frame_number_ = 0; 48 tot_frame_number_ = 0; 49 first_drop_ = 0; 50 num_drops_ = 0; 51 // Denoiser is off by default. 52 denoiser_on_ = 0; 53 bits_total_ = 0; 54 denoiser_offon_test_ = 0; 55 denoiser_offon_period_ = -1; 56 tile_columns_ = 0; 57 tile_rows_ = 0; 58 auto_tiles_ = false; 59 screen_mode_ = false; 60 max_perc_spike_ = 1.0; 61 max_perc_spike_high_ = 1.0; 62 num_spikes_ = 0; 63 num_spikes_high_ = 0; 64 frame_update_bitrate_ = 0; 65 for (int i = 0; i < 3; i++) { 66 target_bitrate_update_[i] = 0; 67 frame_number_dynamic_[i] = 0; 68 bits_total_dynamic_[i] = 0; 69 effective_datarate_dynamic_[i] = 0.0; 70 } 71 avif_mode_ = 0; 72 } 73 74 void PreEncodeFrameHook(::libaom_test::VideoSource *video, 75 ::libaom_test::Encoder *encoder) override { 76 if (video->frame() == 0) { 77 encoder->Control(AOME_SET_CPUUSED, set_cpu_used_); 78 encoder->Control(AV1E_SET_AQ_MODE, aq_mode_); 79 if (auto_tiles_) { 80 encoder->Control(AV1E_SET_AUTO_TILES, 1); 81 } else { 82 encoder->Control(AV1E_SET_TILE_COLUMNS, tile_columns_); 83 encoder->Control(AV1E_SET_TILE_ROWS, tile_rows_); 84 } 85 encoder->Control(AV1E_SET_ROW_MT, 1); 86 if (cfg_.g_usage == AOM_USAGE_REALTIME) { 87 encoder->Control(AV1E_SET_ENABLE_GLOBAL_MOTION, 0); 88 encoder->Control(AV1E_SET_ENABLE_WARPED_MOTION, 0); 89 encoder->Control(AV1E_SET_ENABLE_RESTORATION, 0); 90 encoder->Control(AV1E_SET_ENABLE_OBMC, 0); 91 encoder->Control(AV1E_SET_DELTAQ_MODE, 0); 92 encoder->Control(AV1E_SET_ENABLE_TPL_MODEL, 0); 93 encoder->Control(AV1E_SET_ENABLE_CDEF, 1); 94 encoder->Control(AV1E_SET_COEFF_COST_UPD_FREQ, 2); 95 encoder->Control(AV1E_SET_MODE_COST_UPD_FREQ, 2); 96 encoder->Control(AV1E_SET_MV_COST_UPD_FREQ, 2); 97 encoder->Control(AV1E_SET_DV_COST_UPD_FREQ, 2); 98 encoder->Control(AV1E_SET_POSTENCODE_DROP_RTC, 1); 99 } 100 if (screen_mode_) { 101 encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN); 102 encoder->Control(AV1E_SET_ENABLE_PALETTE, 1); 103 encoder->Control(AV1E_SET_ENABLE_INTRABC, 0); 104 } 105 if (avif_mode_) { 106 encoder->Control(AV1E_SET_COEFF_COST_UPD_FREQ, 0); 107 encoder->Control(AV1E_SET_MODE_COST_UPD_FREQ, 0); 108 encoder->Control(AV1E_SET_MV_COST_UPD_FREQ, 0); 109 #if !CONFIG_REALTIME_ONLY 110 encoder->Control(AV1E_SET_DELTAQ_MODE, 3); 111 #endif 112 #if CONFIG_QUANT_MATRIX 113 encoder->Control(AV1E_SET_ENABLE_QM, 1); 114 #endif 115 encoder->Control(AOME_SET_SHARPNESS, 1); 116 encoder->Control(AV1E_SET_ENABLE_CHROMA_DELTAQ, 1); 117 encoder->Control(AOME_SET_CQ_LEVEL, 0); 118 encoder->Control(AV1E_SET_AQ_MODE, (aq_mode_ > 0) ? 1 : 0); 119 } 120 } 121 122 if (speed_change_test_) { 123 if (video->frame() == 0) { 124 encoder->Control(AOME_SET_CPUUSED, 8); 125 } else if (video->frame() == 30) { 126 encoder->Control(AOME_SET_CPUUSED, 7); 127 } else if (video->frame() == 60) { 128 encoder->Control(AOME_SET_CPUUSED, 6); 129 } else if (video->frame() == 90) { 130 encoder->Control(AOME_SET_CPUUSED, 7); 131 } 132 } 133 134 if (frame_update_bitrate_ > 0) { 135 if (frame_number_ == frame_update_bitrate_) { 136 cfg_.rc_target_bitrate = target_bitrate_update_[1]; 137 encoder->Config(&cfg_); 138 } else if (frame_number_ == 2 * frame_update_bitrate_) { 139 cfg_.rc_target_bitrate = target_bitrate_update_[2]; 140 encoder->Config(&cfg_); 141 } 142 } 143 144 if (denoiser_offon_test_) { 145 ASSERT_GT(denoiser_offon_period_, 0) 146 << "denoiser_offon_period_ is not positive."; 147 if ((video->frame() + 1) % denoiser_offon_period_ == 0) { 148 // Flip denoiser_on_ periodically 149 denoiser_on_ ^= 1; 150 } 151 } 152 153 encoder->Control(AV1E_SET_NOISE_SENSITIVITY, denoiser_on_); 154 155 const aom_rational_t tb = video->timebase(); 156 timebase_ = static_cast<double>(tb.num) / tb.den; 157 duration_ = 0; 158 } 159 160 void FramePktHook(const aom_codec_cx_pkt_t *pkt) override { 161 // Time since last timestamp = duration. 162 aom_codec_pts_t duration = pkt->data.frame.pts - last_pts_; 163 164 if (duration > 1) { 165 // If first drop not set and we have a drop set it to this time. 166 if (!first_drop_) first_drop_ = last_pts_ + 1; 167 // Update the number of frame drops. 168 num_drops_ += static_cast<int>(duration - 1); 169 // Update counter for total number of frames (#frames input to encoder). 170 // Needed for setting the proper layer_id below. 171 tot_frame_number_ += static_cast<int>(duration - 1); 172 } 173 174 // Add to the buffer the bits we'd expect from a constant bitrate server. 175 bits_in_buffer_model_ += static_cast<int64_t>( 176 duration * timebase_ * cfg_.rc_target_bitrate * 1000); 177 178 // Buffer should not go negative. 179 ASSERT_GE(bits_in_buffer_model_, 0) 180 << "Buffer Underrun at frame " << pkt->data.frame.pts; 181 182 const size_t frame_size_in_bits = pkt->data.frame.sz * 8; 183 184 // Update the total encoded bits. 185 bits_total_ += frame_size_in_bits; 186 187 // Update the most recent pts. 188 last_pts_ = pkt->data.frame.pts; 189 ++frame_number_; 190 ++tot_frame_number_; 191 const int per_frame_bandwidth = (cfg_.rc_target_bitrate * 1000) / 30; 192 if (frame_size_in_bits > max_perc_spike_ * per_frame_bandwidth && 193 frame_number_ > 1) 194 num_spikes_++; 195 if (frame_size_in_bits > max_perc_spike_high_ * per_frame_bandwidth && 196 frame_number_ > 1) 197 num_spikes_high_++; 198 199 if (frame_update_bitrate_ > 0) { 200 if (frame_number_ < frame_update_bitrate_) { 201 bits_total_dynamic_[0] += frame_size_in_bits; 202 frame_number_dynamic_[0]++; 203 } else if (frame_number_ >= frame_update_bitrate_ && 204 frame_number_ < 2 * frame_update_bitrate_) { 205 bits_total_dynamic_[1] += frame_size_in_bits; 206 frame_number_dynamic_[1]++; 207 } else { 208 bits_total_dynamic_[2] += frame_size_in_bits; 209 frame_number_dynamic_[2]++; 210 } 211 } 212 } 213 214 void EndPassHook() override { 215 duration_ = (last_pts_ + 1) * timebase_; 216 // Effective file datarate: 217 effective_datarate_ = (bits_total_ / 1000.0) / duration_; 218 if (frame_update_bitrate_ > 0) { 219 for (int i = 0; i < 3; i++) 220 effective_datarate_dynamic_[i] = 221 30 * (bits_total_dynamic_[i] / 1000.0) / frame_number_dynamic_[i]; 222 } 223 } 224 225 void RunBasicRateTargetingTest(::libaom_test::VideoSource *video, 226 const int bitrate, double low_rate_err_limit, 227 double high_rate_err_limit) { 228 cfg_.rc_target_bitrate = bitrate; 229 ResetModel(); 230 ASSERT_NO_FATAL_FAILURE(RunLoop(video)); 231 ASSERT_GE(effective_datarate_, cfg_.rc_target_bitrate * low_rate_err_limit) 232 << " The datarate for the file is lower than target by too much!"; 233 ASSERT_LE(effective_datarate_, cfg_.rc_target_bitrate * high_rate_err_limit) 234 << " The datarate for the file is greater than target by too much!"; 235 } 236 237 void RunBasicRateTargetingTestReversed(::libaom_test::VideoSource *video, 238 const int bitrate, 239 double low_rate_err_limit, 240 double high_rate_err_limit) { 241 cfg_.rc_target_bitrate = bitrate; 242 ResetModel(); 243 ASSERT_NO_FATAL_FAILURE(RunLoop(video)); 244 ASSERT_GE(static_cast<double>(cfg_.rc_target_bitrate), 245 effective_datarate_ * low_rate_err_limit) 246 << " The datarate for the file exceeds the target by too much!"; 247 ASSERT_LE(static_cast<double>(cfg_.rc_target_bitrate), 248 effective_datarate_ * high_rate_err_limit) 249 << " The datarate for the file missed the target!" 250 << cfg_.rc_target_bitrate << " " << effective_datarate_; 251 } 252 253 aom_codec_pts_t last_pts_; 254 double timebase_; 255 int frame_number_; // Counter for number of non-dropped/encoded frames. 256 int tot_frame_number_; // Counter for total number of input frames. 257 int64_t bits_total_; 258 double duration_; 259 double effective_datarate_; 260 int set_cpu_used_; 261 int64_t bits_in_buffer_model_; 262 aom_codec_pts_t first_drop_; 263 int num_drops_; 264 int denoiser_on_; 265 int denoiser_offon_test_; 266 int denoiser_offon_period_; 267 unsigned int aq_mode_; 268 bool speed_change_test_; 269 int tile_columns_; 270 int tile_rows_; 271 bool auto_tiles_; 272 bool screen_mode_; 273 double max_perc_spike_; 274 double max_perc_spike_high_; 275 int num_spikes_; 276 int num_spikes_high_; 277 // These are use for test with dynamic bitrate change. 278 // Used to verify that the encoder can respond and hit bitrate that is updated 279 // during the sequence. 280 int frame_update_bitrate_; 281 int target_bitrate_update_[3]; 282 double effective_datarate_dynamic_[3]; 283 int64_t bits_total_dynamic_[3]; 284 int frame_number_dynamic_[3]; 285 int avif_mode_; 286 }; 287 288 } // namespace 289 } // namespace datarate_test