Unified · Split
| @@ -397,6 +397,10 @@ TEST_INTRA_PRED_SPEED_BIN=./test_intra_pred_speed$(EXE_SFX) · 396 unmodified lines | ||
| 397 | 397 | TEST_INTRA_PRED_SPEED_SRCS=$(addprefix test/,$(call enabled,TEST_INTRA_PRED_SPEED_SRCS)) |
| 398 | 398 | TEST_INTRA_PRED_SPEED_OBJS := $(sort $(call objs,$(TEST_INTRA_PRED_SPEED_SRCS))) |
| 399 | 399 | |
| 400 | +RC_INTERFACE_TEST_BIN=./test_rc_interface$(EXE_SFX) | |
| 401 | +RC_INTERFACE_TEST_SRCS=$(addprefix test/,$(call enabled,RC_INTERFACE_TEST_SRCS)) | |
| 402 | +RC_INTERFACE_TEST_OBJS := $(sort $(call objs,$(RC_INTERFACE_TEST_SRCS))) | |
| 403 | + | |
| 400 | 404 | libvpx_test_srcs.txt: |
| 401 | 405 | @echo " [CREATE] $@" |
| 402 | 406 | @echo $(LIBVPX_TEST_SRCS) | xargs -n1 echo | LC_ALL=C sort -u > $@ |
| @@ -486,6 +490,25 @@ test_intra_pred_speed.$(VCPROJ_SFX): $(TEST_INTRA_PRED_SPEED_SRCS) vpx.$(VCPROJ_ · 83 unmodified lines | ||
| 486 | 490 | -I. -I"$(SRC_PATH_BARE)/third_party/googletest/src/include" \ |
| 487 | 491 | -L. -l$(CODEC_LIB) -l$(GTEST_LIB) $^ |
| 488 | 492 | endif # TEST_INTRA_PRED_SPEED |
| 493 | + | |
| 494 | +ifneq ($(strip $(RC_INTERFACE_TEST_OBJS)),) | |
| 495 | +PROJECTS-$(CONFIG_MSVS) += test_rc_interface.$(VCPROJ_SFX) | |
| 496 | +test_rc_interface.$(VCPROJ_SFX): \ | |
| 497 | + $(RC_INTERFACE_TEST_SRCS) vpx.$(VCPROJ_SFX) gtest.$(VCPROJ_SFX) | |
| 498 | + @echo " [CREATE] $@" | |
| 499 | + $(qexec)$(GEN_VCPROJ) \ | |
| 500 | + --exe \ | |
| 501 | + --target=$(TOOLCHAIN) \ | |
| 502 | + --name=test_rc_interface \ | |
| 503 | + -D_VARIADIC_MAX=10 \ | |
| 504 | + --proj-guid=CD837F5F-52D8-4314-A370-895D614166A7 \ | |
| 505 | + --ver=$(CONFIG_VS_VERSION) \ | |
| 506 | + --src-path-bare="$(SRC_PATH_BARE)" \ | |
| 507 | + $(if $(CONFIG_STATIC_MSVCRT),--static-crt) \ | |
| 508 | + --out=$@ $(INTERNAL_CFLAGS) $(CFLAGS) \ | |
| 509 | + -I.-I"$(SRC_PATH_BARE)/third_party/googletest/src/include" \ | |
| 510 | + -L. -l$(CODEC_LIB) -l$(GTEST_LIB) -lvp9rc$^ | |
| 511 | +endif # RC_INTERFACE_TEST | |
| 489 | 512 | endif |
| 490 | 513 | else |
| 491 | 514 | |
| @@ -503,6 +526,21 @@ OBJS-yes += $(GTEST_OBJS) · 11 unmodified lines | ||
| 503 | 526 | LIBS-yes += $(BUILD_PFX)libgtest.a $(BUILD_PFX)libgtest_g.a |
| 504 | 527 | $(BUILD_PFX)libgtest_g.a: $(GTEST_OBJS) |
| 505 | 528 | |
| 529 | +ifeq ($(CONFIG_VP9_ENCODER),yes) | |
| 530 | + VP9_PREFIX=vp9/ | |
| 531 | + include $(SRC_PATH_BARE)/$(VP9_PREFIX)vp9cx.mk | |
| 532 | + RC_RTC_SRCS := $(addprefix $(VP9_PREFIX),$(call enabled,VP9_CX_SRCS)) | |
| 533 | + RC_RTC_SRCS += $(VP9_PREFIX)vp9cx.mk vpx/vp8.h vpx/vp8cx.h | |
| 534 | + RC_RTC_SRCS += $(VP9_PREFIX)ratectrl_rtc.cc | |
| 535 | + RC_RTC_SRCS += $(VP9_PREFIX)ratectrl_rtc.h | |
| 536 | + VP9_CX_SRCS-$(CONFIG_VP9_ENCODER) += ratectrl_rtc.cc | |
| 537 | + VP9_CX_SRCS-$(CONFIG_VP9_ENCODER) += ratectrl_rtc.h | |
| 538 | + RC_RTC_OBJS=$(call objs,$(RC_RTC_SRCS)) | |
| 539 | + OBJS-yes += $(RC_RTC_OBJS) | |
| 540 | + LIBS-yes += $(BUILD_PFX)libvp9rc.a $(BUILD_PFX)libvp9rc_g.a | |
| 541 | + $(BUILD_PFX)libvp9rc_g.a: $(RC_RTC_OBJS) | |
| 542 | +endif | |
| 543 | + | |
| 506 | 544 | LIBVPX_TEST_OBJS=$(sort $(call objs,$(LIBVPX_TEST_SRCS))) |
| 507 | 545 | $(LIBVPX_TEST_OBJS) $(LIBVPX_TEST_OBJS:.o=.d): CXXFLAGS += $(GTEST_INCLUDES) |
| 508 | 546 | OBJS-yes += $(LIBVPX_TEST_OBJS) |
| @@ -527,6 +565,18 @@ $(eval $(call linkerxx_template,$(TEST_INTRA_PRED_SPEED_BIN), \ · 18 unmodified lines | ||
| 527 | 565 | -L. -lvpx -lgtest $(extralibs) -lm)) |
| 528 | 566 | endif # TEST_INTRA_PRED_SPEED |
| 529 | 567 | |
| 568 | +ifneq ($(strip $(RC_INTERFACE_TEST_OBJS)),) | |
| 569 | +$(RC_INTERFACE_TEST_OBJS) $(RC_INTERFACE_TEST_OBJS:.o=.d): \ | |
| 570 | + CXXFLAGS += $(GTEST_INCLUDES) | |
| 571 | +OBJS-yes += $(RC_INTERFACE_TEST_OBJS) | |
| 572 | +BINS-yes += $(RC_INTERFACE_TEST_BIN) | |
| 573 | + | |
| 574 | +$(RC_INTERFACE_TEST_BIN): $(TEST_LIBS) libvp9rc.a | |
| 575 | +$(eval $(call linkerxx_template,$(RC_INTERFACE_TEST_BIN), \ | |
| 576 | + $(RC_INTERFACE_TEST_OBJS) \ | |
| 577 | + -L. -lvpx -lgtest -lvp9rc $(extralibs) -lm)) | |
| 578 | +endif # RC_INTERFACE_TEST | |
| 579 | + | |
| 530 | 580 | endif # CONFIG_UNIT_TESTS |
| 531 | 581 | |
| 532 | 582 | # Install test sources only if codec source is included |
| @@ -534,6 +584,7 @@ INSTALL-SRCS-$(CONFIG_CODEC_SRCS) += $(patsubst $(SRC_PATH_BARE)/%,%,\ · 1 unmodified line | ||
| 534 | 584 | $(shell find $(SRC_PATH_BARE)/third_party/googletest -type f)) |
| 535 | 585 | INSTALL-SRCS-$(CONFIG_CODEC_SRCS) += $(LIBVPX_TEST_SRCS) |
| 536 | 586 | INSTALL-SRCS-$(CONFIG_CODEC_SRCS) += $(TEST_INTRA_PRED_SPEED_SRCS) |
| 587 | +INSTALL-SRCS-$(CONFIG_CODEC_SRCS) += $(RC_INTERFACE_TEST_SRCS) | |
| 537 | 588 | |
| 538 | 589 | define test_shard_template |
| 539 | 590 | test:: test_shard.$(1) |
| @@ -574,6 +625,7 @@ endif · 34 unmodified lines | ||
| 574 | 625 | |
| 575 | 626 | ## Update the global src list |
| 576 | 627 | SRCS += $(CODEC_SRCS) $(LIBVPX_TEST_SRCS) $(GTEST_SRCS) |
| 628 | +SRCS += $(RC_INTERFACE_TEST_SRCS) | |
| 577 | 629 | |
| 578 | 630 | ## |
| 579 | 631 | ## vpxdec/vpxenc tests. |
| @@ -0,0 +1,229 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2020 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 | +#include "vp9/ratectrl_rtc.h" | |
| 11 | + | |
| 12 | +#include <fstream> // NOLINT | |
| 13 | +#include <string> | |
| 14 | + | |
| 15 | +#include "./vpx_config.h" | |
| 16 | +#include "third_party/googletest/src/include/gtest/gtest.h" | |
| 17 | +#include "test/codec_factory.h" | |
| 18 | +#include "test/encode_test_driver.h" | |
| 19 | +#include "test/util.h" | |
| 20 | +#include "test/video_source.h" | |
| 21 | +#include "vpx/vpx_codec.h" | |
| 22 | +#include "vpx_ports/bitops.h" | |
| 23 | + | |
| 24 | +namespace { | |
| 25 | + | |
| 26 | +const size_t kNumFrame = 850; | |
| 27 | + | |
| 28 | +struct FrameInfo { | |
| 29 | + friend std::istream &operator>>(std::istream &is, FrameInfo &info) { | |
| 30 | + is >> info.frame_id >> info.spatial_id >> info.temporal_id >> info.base_q >> | |
| 31 | + info.target_bandwidth >> info.buffer_level >> info.filter_level_ >> | |
| 32 | + info.bytes_used; | |
| 33 | + return is; | |
| 34 | + } | |
| 35 | + int frame_id; | |
| 36 | + int spatial_id; | |
| 37 | + int temporal_id; | |
| 38 | + // Base QP | |
| 39 | + int base_q; | |
| 40 | + size_t target_bandwidth; | |
| 41 | + size_t buffer_level; | |
| 42 | + // Loopfilter level | |
| 43 | + int filter_level_; | |
| 44 | + // Frame size for current frame, used for pose encode update | |
| 45 | + size_t bytes_used; | |
| 46 | +}; | |
| 47 | + | |
| 48 | +// This test runs the rate control interface and compare against ground truth | |
| 49 | +// generated by encoders. | |
| 50 | +// Settings for the encoder: | |
| 51 | +// For 1 layer: | |
| 52 | +// | |
| 53 | +// examples/vpx_temporal_svc_encoder gipsrec_motion1.1280_720.yuv out vp9 | |
| 54 | +// 1280 720 1 30 7 0 0 1 0 1000 | |
| 55 | +// | |
| 56 | +// For SVC (3 temporal layers, 3 spatial layers): | |
| 57 | +// | |
| 58 | +// examples/vp9_spatial_svc_encoder -f 10000 -w 1280 -h 720 -t 1/30 -sl 3 | |
| 59 | +// -k 10000 -bl 100,140,200,250,350,500,450,630,900 -b 1600 --rc-end-usage=1 | |
| 60 | +// --lag-in-frames=0 --passes=1 --speed=7 --threads=1 | |
| 61 | +// --temporal-layering-mode=3 -aq 1 -rcstat 1 | |
| 62 | +// gipsrec_motion1.1280_720.yuv -o out.webm | |
| 63 | +// | |
| 64 | +// - AQ_Mode 0 | |
| 65 | +// - Disable golden refresh | |
| 66 | +// - Bitrate x 2 at frame/superframe 200 | |
| 67 | +// - Bitrate / 4 at frame/superframe 400 | |
| 68 | +// | |
| 69 | +// The generated file includes: | |
| 70 | +// frame number, spatial layer ID, temporal layer ID, base QP, target | |
| 71 | +// bandwidth, buffer level, loopfilter level, encoded frame size | |
| 72 | +// TODO(jianj): Remove golden files, and run actual encoding in this test. | |
| 73 | +class RcInterfaceTest : public ::testing::Test { | |
| 74 | + public: | |
| 75 | + explicit RcInterfaceTest() {} | |
| 76 | + | |
| 77 | + virtual ~RcInterfaceTest() {} | |
| 78 | + | |
| 79 | + protected: | |
| 80 | + void RunOneLayer() { | |
| 81 | + SetConfigOneLayer(); | |
| 82 | + rc_api_->Create(rc_cfg_); | |
| 83 | + FrameInfo frame_info; | |
| 84 | + libvpx::VP9FrameParamsQpRTC frame_params; | |
| 85 | + frame_params.frame_type = KEY_FRAME; | |
| 86 | + frame_params.spatial_layer_id = 0; | |
| 87 | + frame_params.temporal_layer_id = 0; | |
| 88 | + std::ifstream one_layer_file; | |
| 89 | + one_layer_file.open(libvpx_test::GetDataPath() + | |
| 90 | + "/rc_interface_test_one_layer"); | |
| 91 | + ASSERT_EQ(one_layer_file.rdstate() & std::ifstream::failbit, 0); | |
| 92 | + for (size_t i = 0; i < kNumFrame; i++) { | |
| 93 | + one_layer_file >> frame_info; | |
| 94 | + if (frame_info.frame_id > 0) frame_params.frame_type = INTER_FRAME; | |
| 95 | + if (frame_info.frame_id == 200) { | |
| 96 | + rc_cfg_.target_bandwidth = rc_cfg_.target_bandwidth * 2; | |
| 97 | + rc_api_->UpdateRateControl(rc_cfg_); | |
| 98 | + } else if (frame_info.frame_id == 400) { | |
| 99 | + rc_cfg_.target_bandwidth = rc_cfg_.target_bandwidth / 4; | |
| 100 | + rc_api_->UpdateRateControl(rc_cfg_); | |
| 101 | + } | |
| 102 | + ASSERT_EQ(frame_info.spatial_id, 0); | |
| 103 | + ASSERT_EQ(frame_info.temporal_id, 0); | |
| 104 | + rc_api_->ComputeQP(frame_params); | |
| 105 | + ASSERT_EQ(rc_api_->GetQP(), frame_info.base_q); | |
| 106 | + ASSERT_EQ(rc_api_->GetLoopfilterLevel(), frame_info.filter_level_); | |
| 107 | + rc_api_->PostEncodeUpdate(frame_info.bytes_used); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + void RunSVC() { | |
| 112 | + SetConfigSVC(); | |
| 113 | + rc_api_->Create(rc_cfg_); | |
| 114 | + FrameInfo frame_info; | |
| 115 | + libvpx::VP9FrameParamsQpRTC frame_params; | |
| 116 | + frame_params.frame_type = KEY_FRAME; | |
| 117 | + std::ifstream svc_file; | |
| 118 | + svc_file.open(std::string(std::getenv("LIBVPX_TEST_DATA_PATH")) + | |
| 119 | + "/rc_interface_test_svc"); | |
| 120 | + ASSERT_EQ(svc_file.rdstate() & std::ifstream::failbit, 0); | |
| 121 | + for (size_t i = 0; i < kNumFrame * rc_cfg_.ss_number_layers; i++) { | |
| 122 | + svc_file >> frame_info; | |
| 123 | + if (frame_info.frame_id > 0) frame_params.frame_type = INTER_FRAME; | |
| 124 | + if (frame_info.frame_id == 200 * rc_cfg_.ss_number_layers) { | |
| 125 | + for (int layer = 0; | |
| 126 | + layer < rc_cfg_.ss_number_layers * rc_cfg_.ts_number_layers; | |
| 127 | + layer++) | |
| 128 | + rc_cfg_.layer_target_bitrate[layer] *= 2; | |
| 129 | + rc_cfg_.target_bandwidth *= 2; | |
| 130 | + rc_api_->UpdateRateControl(rc_cfg_); | |
| 131 | + } else if (frame_info.frame_id == 400 * rc_cfg_.ss_number_layers) { | |
| 132 | + for (int layer = 0; | |
| 133 | + layer < rc_cfg_.ss_number_layers * rc_cfg_.ts_number_layers; | |
| 134 | + layer++) | |
| 135 | + rc_cfg_.layer_target_bitrate[layer] /= 4; | |
| 136 | + rc_cfg_.target_bandwidth /= 4; | |
| 137 | + rc_api_->UpdateRateControl(rc_cfg_); | |
| 138 | + } | |
| 139 | + frame_params.spatial_layer_id = frame_info.spatial_id; | |
| 140 | + frame_params.temporal_layer_id = frame_info.temporal_id; | |
| 141 | + rc_api_->ComputeQP(frame_params); | |
| 142 | + ASSERT_EQ(rc_api_->GetQP(), frame_info.base_q); | |
| 143 | + ASSERT_EQ(rc_api_->GetLoopfilterLevel(), frame_info.filter_level_); | |
| 144 | + rc_api_->PostEncodeUpdate(frame_info.bytes_used); | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + private: | |
| 149 | + void SetConfigOneLayer() { | |
| 150 | + rc_cfg_.width = 1280; | |
| 151 | + rc_cfg_.height = 720; | |
| 152 | + rc_cfg_.max_quantizer = 52; | |
| 153 | + rc_cfg_.min_quantizer = 2; | |
| 154 | + rc_cfg_.target_bandwidth = 1000; | |
| 155 | + rc_cfg_.buf_initial_sz = 600; | |
| 156 | + rc_cfg_.buf_optimal_sz = 600; | |
| 157 | + rc_cfg_.buf_sz = 1000; | |
| 158 | + rc_cfg_.undershoot_pct = 50; | |
| 159 | + rc_cfg_.overshoot_pct = 50; | |
| 160 | + rc_cfg_.max_intra_bitrate_pct = 1000; | |
| 161 | + rc_cfg_.framerate = 30.0; | |
| 162 | + rc_cfg_.ss_number_layers = 1; | |
| 163 | + rc_cfg_.ts_number_layers = 1; | |
| 164 | + rc_cfg_.scaling_factor_num[0] = 1; | |
| 165 | + rc_cfg_.scaling_factor_den[0] = 1; | |
| 166 | + rc_cfg_.layer_target_bitrate[0] = 1000; | |
| 167 | + rc_cfg_.max_quantizers[0] = 52; | |
| 168 | + rc_cfg_.min_quantizers[0] = 2; | |
| 169 | + } | |
| 170 | + | |
| 171 | + void SetConfigSVC() { | |
| 172 | + rc_cfg_.width = 1280; | |
| 173 | + rc_cfg_.height = 720; | |
| 174 | + rc_cfg_.max_quantizer = 56; | |
| 175 | + rc_cfg_.min_quantizer = 2; | |
| 176 | + rc_cfg_.target_bandwidth = 1600; | |
| 177 | + rc_cfg_.buf_initial_sz = 500; | |
| 178 | + rc_cfg_.buf_optimal_sz = 600; | |
| 179 | + rc_cfg_.buf_sz = 1000; | |
| 180 | + rc_cfg_.undershoot_pct = 50; | |
| 181 | + rc_cfg_.overshoot_pct = 50; | |
| 182 | + rc_cfg_.max_intra_bitrate_pct = 900; | |
| 183 | + rc_cfg_.framerate = 30.0; | |
| 184 | + rc_cfg_.ss_number_layers = 3; | |
| 185 | + rc_cfg_.ts_number_layers = 3; | |
| 186 | + | |
| 187 | + rc_cfg_.scaling_factor_num[0] = 1; | |
| 188 | + rc_cfg_.scaling_factor_den[0] = 4; | |
| 189 | + rc_cfg_.scaling_factor_num[1] = 2; | |
| 190 | + rc_cfg_.scaling_factor_den[1] = 4; | |
| 191 | + rc_cfg_.scaling_factor_num[2] = 4; | |
| 192 | + rc_cfg_.scaling_factor_den[2] = 4; | |
| 193 | + | |
| 194 | + rc_cfg_.ts_rate_decimator[0] = 4; | |
| 195 | + rc_cfg_.ts_rate_decimator[1] = 2; | |
| 196 | + rc_cfg_.ts_rate_decimator[2] = 1; | |
| 197 | + | |
| 198 | + rc_cfg_.layer_target_bitrate[0] = 100; | |
| 199 | + rc_cfg_.layer_target_bitrate[1] = 140; | |
| 200 | + rc_cfg_.layer_target_bitrate[2] = 200; | |
| 201 | + rc_cfg_.layer_target_bitrate[3] = 250; | |
| 202 | + rc_cfg_.layer_target_bitrate[4] = 350; | |
| 203 | + rc_cfg_.layer_target_bitrate[5] = 500; | |
| 204 | + rc_cfg_.layer_target_bitrate[6] = 450; | |
| 205 | + rc_cfg_.layer_target_bitrate[7] = 630; | |
| 206 | + rc_cfg_.layer_target_bitrate[8] = 900; | |
| 207 | + | |
| 208 | + for (int sl = 0; sl < rc_cfg_.ss_number_layers; ++sl) { | |
| 209 | + for (int tl = 0; tl < rc_cfg_.ts_number_layers; ++tl) { | |
| 210 | + const int i = sl * rc_cfg_.ts_number_layers + tl; | |
| 211 | + rc_cfg_.max_quantizers[i] = 56; | |
| 212 | + rc_cfg_.min_quantizers[i] = 2; | |
| 213 | + } | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + std::unique_ptr<libvpx::VP9RateControlRTC> rc_api_; | |
| 218 | + libvpx::VP9RateControlRtcConfig rc_cfg_; | |
| 219 | +}; | |
| 220 | + | |
| 221 | +TEST_F(RcInterfaceTest, OneLayer) { RunOneLayer(); } | |
| 222 | + | |
| 223 | +TEST_F(RcInterfaceTest, SVC) { RunSVC(); } | |
| 224 | +} // namespace | |
| 225 | + | |
| 226 | +int main(int argc, char **argv) { | |
| 227 | + ::testing::InitGoogleTest(&argc, argv); | |
| 228 | + return RUN_ALL_TESTS(); | |
| 229 | +} | |
| @@ -203,6 +203,8 @@ LIBVPX_TEST_SRCS-$(CONFIG_ENCODERS) += sum_squares_test.cc · 202 unmodified lines | ||
| 203 | 203 | TEST_INTRA_PRED_SPEED_SRCS-yes := test_intra_pred_speed.cc |
| 204 | 204 | TEST_INTRA_PRED_SPEED_SRCS-yes += ../md5_utils.h ../md5_utils.c |
| 205 | 205 | |
| 206 | +RC_INTERFACE_TEST_SRCS-$(CONFIG_VP9_ENCODER) := ratectrl_rtc_test.cc | |
| 207 | + | |
| 206 | 208 | endif # CONFIG_SHARED |
| 207 | 209 | |
| 208 | 210 | include $(SRC_PATH_BARE)/test/test-data.mk |
| @@ -1523,8 +1523,29 @@ static void init_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) { · 1522 unmodified lines | ||
| 1523 | 1523 | vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height); |
| 1524 | 1524 | } |
| 1525 | 1525 | |
| 1526 | −static void set_rc_buffer_sizes(RATE_CONTROL *rc, | |
| 1527 | − const VP9EncoderConfig *oxcf) { | |
| 1526 | +void vp9_check_reset_rc_flag(VP9_COMP *cpi) { | |
| 1527 | + RATE_CONTROL *rc = &cpi->rc; | |
| 1528 | + | |
| 1529 | + if (cpi->common.current_video_frame > | |
| 1530 | + (unsigned int)cpi->svc.number_spatial_layers) { | |
| 1531 | + if (cpi->use_svc) { | |
| 1532 | + vp9_svc_check_reset_layer_rc_flag(cpi); | |
| 1533 | + } else { | |
| 1534 | + if (rc->avg_frame_bandwidth > (3 * rc->last_avg_frame_bandwidth >> 1) || | |
| 1535 | + rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) { | |
| 1536 | + rc->rc_1_frame = 0; | |
| 1537 | + rc->rc_2_frame = 0; | |
| 1538 | + rc->bits_off_target = rc->optimal_buffer_level; | |
| 1539 | + rc->buffer_level = rc->optimal_buffer_level; | |
| 1540 | + } | |
| 1541 | + } | |
| 1542 | + } | |
| 1543 | +} | |
| 1544 | + | |
| 1545 | +void vp9_set_rc_buffer_sizes(VP9_COMP *cpi) { | |
| 1546 | + RATE_CONTROL *rc = &cpi->rc; | |
| 1547 | + const VP9EncoderConfig *oxcf = &cpi->oxcf; | |
| 1548 | + | |
| 1528 | 1549 | const int64_t bandwidth = oxcf->target_bandwidth; |
| 1529 | 1550 | const int64_t starting = oxcf->starting_buffer_level_ms; |
| 1530 | 1551 | const int64_t optimal = oxcf->optimal_buffer_level_ms; |
| @@ -1535,6 +1556,11 @@ static void set_rc_buffer_sizes(RATE_CONTROL *rc, · 4 unmodified lines | ||
| 1535 | 1556 | (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000; |
| 1536 | 1557 | rc->maximum_buffer_size = |
| 1537 | 1558 | (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000; |
| 1559 | + | |
| 1560 | + // Under a configuration change, where maximum_buffer_size may change, | |
| 1561 | + // keep buffer level clipped to the maximum allowed buffer size. | |
| 1562 | + rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); | |
| 1563 | + rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size); | |
| 1538 | 1564 | } |
| 1539 | 1565 | |
| 1540 | 1566 | #if CONFIG_VP9_HIGHBITDEPTH |
| @@ -1991,12 +2017,7 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) { · 450 unmodified lines | ||
| 1991 | 2017 | } |
| 1992 | 2018 | cpi->encode_breakout = cpi->oxcf.encode_breakout; |
| 1993 | 2019 | |
| 1994 | − set_rc_buffer_sizes(rc, &cpi->oxcf); | |
| 1995 | − | |
| 1996 | − // Under a configuration change, where maximum_buffer_size may change, | |
| 1997 | − // keep buffer level clipped to the maximum allowed buffer size. | |
| 1998 | − rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); | |
| 1999 | − rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size); | |
| 2020 | + vp9_set_rc_buffer_sizes(cpi); | |
| 2000 | 2021 | |
| 2001 | 2022 | // Set up frame rate and related parameters rate control values. |
| 2002 | 2023 | vp9_new_framerate(cpi, cpi->framerate); |
| @@ -2057,23 +2078,7 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) { · 54 unmodified lines | ||
| 2057 | 2078 | (int)cpi->oxcf.target_bandwidth); |
| 2058 | 2079 | } |
| 2059 | 2080 | |
| 2060 | − // Check for resetting the rc flags (rc_1_frame, rc_2_frame) if the | |
| 2061 | − // configuration change has a large change in avg_frame_bandwidth. | |
| 2062 | − // For SVC check for resetting based on spatial layer average bandwidth. | |
| 2063 | − // Also reset buffer level to optimal level. | |
| 2064 | − if (cm->current_video_frame > (unsigned int)cpi->svc.number_spatial_layers) { | |
| 2065 | − if (cpi->use_svc) { | |
| 2066 | − vp9_svc_check_reset_layer_rc_flag(cpi); | |
| 2067 | − } else { | |
| 2068 | − if (rc->avg_frame_bandwidth > (3 * rc->last_avg_frame_bandwidth >> 1) || | |
| 2069 | − rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) { | |
| 2070 | − rc->rc_1_frame = 0; | |
| 2071 | − rc->rc_2_frame = 0; | |
| 2072 | − rc->bits_off_target = rc->optimal_buffer_level; | |
| 2073 | − rc->buffer_level = rc->optimal_buffer_level; | |
| 2074 | − } | |
| 2075 | − } | |
| 2076 | − } | |
| 2081 | + vp9_check_reset_rc_flag(cpi); | |
| 2077 | 2082 | |
| 2078 | 2083 | cpi->alt_ref_source = NULL; |
| 2079 | 2084 | rc->is_src_frame_alt_ref = 0; |
| @@ -1000,6 +1000,14 @@ int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width, · 999 unmodified lines | ||
| 1000 | 1000 | |
| 1001 | 1001 | void vp9_set_svc(VP9_COMP *cpi, int use_svc); |
| 1002 | 1002 | |
| 1003 | +// Check for resetting the rc flags (rc_1_frame, rc_2_frame) if the | |
| 1004 | +// configuration change has a large change in avg_frame_bandwidth. | |
| 1005 | +// For SVC check for resetting based on spatial layer average bandwidth. | |
| 1006 | +// Also reset buffer level to optimal level. | |
| 1007 | +void vp9_check_reset_rc_flag(VP9_COMP *cpi); | |
| 1008 | + | |
| 1009 | +void vp9_set_rc_buffer_sizes(VP9_COMP *cpi); | |
| 1010 | + | |
| 1003 | 1011 | static INLINE int stack_pop(int *stack, int stack_size) { |
| 1004 | 1012 | int idx; |
| 1005 | 1013 | const int r = stack[0]; |
| @@ -249,7 +249,7 @@ int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { · 248 unmodified lines | ||
| 249 | 249 | // way for CBR mode, for the buffering updates below. Look into removing one |
| 250 | 250 | // of these (i.e., bits_off_target). |
| 251 | 251 | // Update the buffer level before encoding with the per-frame-bandwidth, |
| 252 | −static void update_buffer_level_preencode(VP9_COMP *cpi) { | |
| 252 | +void vp9_update_buffer_level_preencode(VP9_COMP *cpi) { | |
| 253 | 253 | RATE_CONTROL *const rc = &cpi->rc; |
| 254 | 254 | rc->bits_off_target += rc->avg_frame_bandwidth; |
| 255 | 255 | // Clip the buffer level to the maximum specified buffer size. |
| @@ -2098,7 +2098,7 @@ void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { · 1842 unmodified lines | ||
| 2098 | 2098 | vp9_cyclic_refresh_update_parameters(cpi); |
| 2099 | 2099 | } |
| 2100 | 2100 | |
| 2101 | −static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { | |
| 2101 | +int vp9_calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { | |
| 2102 | 2102 | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
| 2103 | 2103 | const RATE_CONTROL *rc = &cpi->rc; |
| 2104 | 2104 | const SVC *const svc = &cpi->svc; |
| @@ -2147,7 +2147,7 @@ static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { · 42 unmodified lines | ||
| 2147 | 2147 | return VPXMAX(min_frame_target, target); |
| 2148 | 2148 | } |
| 2149 | 2149 | |
| 2150 | −static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { | |
| 2150 | +int vp9_calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { | |
| 2151 | 2151 | const RATE_CONTROL *rc = &cpi->rc; |
| 2152 | 2152 | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
| 2153 | 2153 | const SVC *const svc = &cpi->svc; |
| @@ -2253,7 +2253,7 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) { · 99 unmodified lines | ||
| 2253 | 2253 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2254 | 2254 | // Assumption here is that LAST_FRAME is being updated for a keyframe. |
| 2255 | 2255 | // Thus no change in update flags. |
| 2256 | − target = calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2256 | + target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2257 | 2257 | } |
| 2258 | 2258 | } else { |
| 2259 | 2259 | cm->frame_type = INTER_FRAME; |
| @@ -2266,7 +2266,7 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) { · 6 unmodified lines | ||
| 2266 | 2266 | (svc->spatial_layer_id == 0 && cm->current_video_frame > 0) |
| 2267 | 2267 | ? 0 |
| 2268 | 2268 | : svc->layer_context[svc->temporal_layer_id].is_key_frame; |
| 2269 | − target = calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2269 | + target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2270 | 2270 | } |
| 2271 | 2271 | } |
| 2272 | 2272 | |
| @@ -2275,7 +2275,7 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) { · 2 unmodified lines | ||
| 2275 | 2275 | svc->layer_context[layer].is_key_frame == 1) { |
| 2276 | 2276 | cm->frame_type = KEY_FRAME; |
| 2277 | 2277 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2278 | − target = calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2278 | + target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2279 | 2279 | } |
| 2280 | 2280 | // Set the buffer idx and refresh flags for key frames in simulcast mode. |
| 2281 | 2281 | // Note the buffer slot for long-term reference is set below (line 2255), |
| @@ -2360,7 +2360,7 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) { · 78 unmodified lines | ||
| 2360 | 2360 | } |
| 2361 | 2361 | if (svc->set_intra_only_frame) { |
| 2362 | 2362 | set_intra_only_frame(cpi); |
| 2363 | − target = calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2363 | + target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2364 | 2364 | } |
| 2365 | 2365 | // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
| 2366 | 2366 | // should be done here, before the frame qp is selected. |
| @@ -2433,13 +2433,13 @@ void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { · 66 unmodified lines | ||
| 2433 | 2433 | vp9_cyclic_refresh_update_parameters(cpi); |
| 2434 | 2434 | |
| 2435 | 2435 | if (frame_is_intra_only(cm)) |
| 2436 | − target = calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2436 | + target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); | |
| 2437 | 2437 | else |
| 2438 | − target = calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2438 | + target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2439 | 2439 | |
| 2440 | 2440 | vp9_rc_set_frame_target(cpi, target); |
| 2441 | 2441 | |
| 2442 | − if (cm->show_frame) update_buffer_level_preencode(cpi); | |
| 2442 | + if (cm->show_frame) vp9_update_buffer_level_preencode(cpi); | |
| 2443 | 2443 | |
| 2444 | 2444 | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC) |
| 2445 | 2445 | cpi->resize_pending = vp9_resize_one_pass_cbr(cpi); |
| @@ -2742,7 +2742,7 @@ int vp9_resize_one_pass_cbr(VP9_COMP *cpi) { · 296 unmodified lines | ||
| 2742 | 2742 | // Reset buffer level to optimal, update target size. |
| 2743 | 2743 | rc->buffer_level = rc->optimal_buffer_level; |
| 2744 | 2744 | rc->bits_off_target = rc->optimal_buffer_level; |
| 2745 | − rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2745 | + rc->this_frame_target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); | |
| 2746 | 2746 | // Get the projected qindex, based on the scaled target frame size (scaled |
| 2747 | 2747 | // so target_bits_per_mb in vp9_rc_regulate_q will be correct target). |
| 2748 | 2748 | target_bits_per_frame = (resize_action >= 0) |
| @@ -252,6 +252,9 @@ int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval); · 251 unmodified lines | ||
| 252 | 252 | // encode_frame_to_data_rate() function. |
| 253 | 253 | void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi); |
| 254 | 254 | void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi); |
| 255 | +int vp9_calc_pframe_target_size_one_pass_cbr(const struct VP9_COMP *cpi); | |
| 256 | +int vp9_calc_iframe_target_size_one_pass_cbr(const struct VP9_COMP *cpi); | |
| 257 | +void vp9_update_buffer_level_preencode(struct VP9_COMP *cpi); | |
| 255 | 258 | void vp9_rc_get_svc_params(struct VP9_COMP *cpi); |
| 256 | 259 | |
| 257 | 260 | // Post encode update of the rate control parameters based |
| @@ -0,0 +1,173 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2020 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 | +#include "vp9/ratectrl_rtc.h" | |
| 11 | + | |
| 12 | +#include <new> | |
| 13 | + | |
| 14 | +#include "vp9/encoder/vp9_encoder.h" | |
| 15 | +#include "vp9/encoder/vp9_picklpf.h" | |
| 16 | +#include "vpx/vp8cx.h" | |
| 17 | +#include "vpx/vpx_codec.h" | |
| 18 | + | |
| 19 | +namespace libvpx { | |
| 20 | + | |
| 21 | +std::unique_ptr<VP9RateControlRTC> VP9RateControlRTC::Create( | |
| 22 | + const VP9RateControlRtcConfig &cfg) { | |
| 23 | + std::unique_ptr<VP9RateControlRTC> rc_api(new (std::nothrow) | |
| 24 | + VP9RateControlRTC()); | |
| 25 | + if (!rc_api) return nullptr; | |
| 26 | + rc_api->cpi_ = static_cast<VP9_COMP *>(vpx_memalign(32, sizeof(*cpi_))); | |
| 27 | + if (rc_api->cpi_ == nullptr) { | |
| 28 | + return nullptr; | |
| 29 | + } | |
| 30 | + rc_api->InitRateControl(cfg); | |
| 31 | + return rc_api; | |
| 32 | +} | |
| 33 | + | |
| 34 | +void VP9RateControlRTC::InitRateControl(const VP9RateControlRtcConfig &rc_cfg) { | |
| 35 | + VP9_COMMON *cm = &cpi_->common; | |
| 36 | + VP9EncoderConfig *oxcf = &cpi_->oxcf; | |
| 37 | + RATE_CONTROL *const rc = &cpi_->rc; | |
| 38 | + cm->profile = PROFILE_0; | |
| 39 | + cm->bit_depth = VPX_BITS_8; | |
| 40 | + cm->show_frame = 1; | |
| 41 | + oxcf->rc_mode = VPX_CBR; | |
| 42 | + oxcf->pass = 0; | |
| 43 | + oxcf->aq_mode = NO_AQ; | |
| 44 | + oxcf->content = VP9E_CONTENT_DEFAULT; | |
| 45 | + oxcf->drop_frames_water_mark = 0; | |
| 46 | + | |
| 47 | + UpdateRateControl(rc_cfg); | |
| 48 | + | |
| 49 | + cpi_->use_svc = (cpi_->svc.number_spatial_layers > 1 || | |
| 50 | + cpi_->svc.number_temporal_layers > 1) | |
| 51 | + ? 1 | |
| 52 | + : 0; | |
| 53 | + | |
| 54 | + rc->rc_1_frame = 0; | |
| 55 | + rc->rc_2_frame = 0; | |
| 56 | + vp9_rc_init_minq_luts(); | |
| 57 | + vp9_rc_init(oxcf, 0, rc); | |
| 58 | + cpi_->sf.use_nonrd_pick_mode = 1; | |
| 59 | + cm->current_video_frame = 0; | |
| 60 | +} | |
| 61 | + | |
| 62 | +void VP9RateControlRTC::UpdateRateControl( | |
| 63 | + const VP9RateControlRtcConfig &rc_cfg) { | |
| 64 | + VP9_COMMON *cm = &cpi_->common; | |
| 65 | + VP9EncoderConfig *oxcf = &cpi_->oxcf; | |
| 66 | + RATE_CONTROL *const rc = &cpi_->rc; | |
| 67 | + | |
| 68 | + cm->width = rc_cfg.width; | |
| 69 | + cm->height = rc_cfg.height; | |
| 70 | + oxcf->width = rc_cfg.width; | |
| 71 | + oxcf->height = rc_cfg.height; | |
| 72 | + oxcf->worst_allowed_q = vp9_quantizer_to_qindex(rc_cfg.max_quantizer); | |
| 73 | + oxcf->best_allowed_q = vp9_quantizer_to_qindex(rc_cfg.min_quantizer); | |
| 74 | + rc->worst_quality = oxcf->worst_allowed_q; | |
| 75 | + rc->best_quality = oxcf->best_allowed_q; | |
| 76 | + oxcf->target_bandwidth = 1000 * rc_cfg.target_bandwidth; | |
| 77 | + oxcf->starting_buffer_level_ms = rc_cfg.buf_initial_sz; | |
| 78 | + oxcf->optimal_buffer_level_ms = rc_cfg.buf_optimal_sz; | |
| 79 | + oxcf->maximum_buffer_size_ms = rc_cfg.buf_sz; | |
| 80 | + oxcf->under_shoot_pct = rc_cfg.undershoot_pct; | |
| 81 | + oxcf->over_shoot_pct = rc_cfg.overshoot_pct; | |
| 82 | + oxcf->ss_number_layers = rc_cfg.ss_number_layers; | |
| 83 | + oxcf->ts_number_layers = rc_cfg.ts_number_layers; | |
| 84 | + oxcf->temporal_layering_mode = (VP9E_TEMPORAL_LAYERING_MODE)( | |
| 85 | + (rc_cfg.ts_number_layers > 1) ? rc_cfg.ts_number_layers : 0); | |
| 86 | + | |
| 87 | + cpi_->oxcf.rc_max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct; | |
| 88 | + cpi_->framerate = rc_cfg.framerate; | |
| 89 | + cpi_->svc.number_spatial_layers = rc_cfg.ss_number_layers; | |
| 90 | + cpi_->svc.number_temporal_layers = rc_cfg.ts_number_layers; | |
| 91 | + | |
| 92 | + for (int sl = 0; sl < cpi_->svc.number_spatial_layers; ++sl) { | |
| 93 | + for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) { | |
| 94 | + const int layer = | |
| 95 | + LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers); | |
| 96 | + LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer]; | |
| 97 | + RATE_CONTROL *const lrc = &lc->rc; | |
| 98 | + oxcf->layer_target_bitrate[layer] = | |
| 99 | + 1000 * rc_cfg.layer_target_bitrate[layer]; | |
| 100 | + lrc->worst_quality = | |
| 101 | + vp9_quantizer_to_qindex(rc_cfg.max_quantizers[layer]); | |
| 102 | + lrc->best_quality = vp9_quantizer_to_qindex(rc_cfg.min_quantizers[layer]); | |
| 103 | + lc->scaling_factor_num = rc_cfg.scaling_factor_num[sl]; | |
| 104 | + lc->scaling_factor_den = rc_cfg.scaling_factor_den[sl]; | |
| 105 | + oxcf->ts_rate_decimator[tl] = rc_cfg.ts_rate_decimator[tl]; | |
| 106 | + } | |
| 107 | + } | |
| 108 | + vp9_set_rc_buffer_sizes(cpi_); | |
| 109 | + vp9_new_framerate(cpi_, cpi_->framerate); | |
| 110 | + if (cpi_->svc.number_temporal_layers > 1) { | |
| 111 | + if (cm->current_video_frame == 0) vp9_init_layer_context(cpi_); | |
| 112 | + vp9_update_layer_context_change_config(cpi_, | |
| 113 | + (int)cpi_->oxcf.target_bandwidth); | |
| 114 | + } | |
| 115 | + vp9_check_reset_rc_flag(cpi_); | |
| 116 | +} | |
| 117 | + | |
| 118 | +void VP9RateControlRTC::ComputeQP(const VP9FrameParamsQpRTC &frame_params) { | |
| 119 | + VP9_COMMON *const cm = &cpi_->common; | |
| 120 | + int width, height; | |
| 121 | + cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id; | |
| 122 | + cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id; | |
| 123 | + if (cpi_->svc.number_spatial_layers > 1) { | |
| 124 | + const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id, | |
| 125 | + cpi_->svc.temporal_layer_id, | |
| 126 | + cpi_->svc.number_temporal_layers); | |
| 127 | + LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer]; | |
| 128 | + get_layer_resolution(cpi_->oxcf.width, cpi_->oxcf.height, | |
| 129 | + lc->scaling_factor_num, lc->scaling_factor_den, &width, | |
| 130 | + &height); | |
| 131 | + cm->width = width; | |
| 132 | + cm->height = height; | |
| 133 | + } | |
| 134 | + vp9_set_mb_mi(cm, cm->width, cm->height); | |
| 135 | + cm->frame_type = frame_params.frame_type; | |
| 136 | + cpi_->refresh_golden_frame = (cm->frame_type == KEY_FRAME) ? 1 : 0; | |
| 137 | + cpi_->sf.use_nonrd_pick_mode = 1; | |
| 138 | + if (cpi_->svc.number_spatial_layers == 1 && | |
| 139 | + cpi_->svc.number_temporal_layers == 1) { | |
| 140 | + int target; | |
| 141 | + if (frame_is_intra_only(cm)) | |
| 142 | + target = vp9_calc_iframe_target_size_one_pass_cbr(cpi_); | |
| 143 | + else | |
| 144 | + target = vp9_calc_pframe_target_size_one_pass_cbr(cpi_); | |
| 145 | + vp9_rc_set_frame_target(cpi_, target); | |
| 146 | + vp9_update_buffer_level_preencode(cpi_); | |
| 147 | + } else { | |
| 148 | + vp9_update_temporal_layer_framerate(cpi_); | |
| 149 | + vp9_restore_layer_context(cpi_); | |
| 150 | + vp9_rc_get_svc_params(cpi_); | |
| 151 | + } | |
| 152 | + int bottom_index, top_index; | |
| 153 | + cpi_->common.base_qindex = | |
| 154 | + vp9_rc_pick_q_and_bounds(cpi_, &bottom_index, &top_index); | |
| 155 | +} | |
| 156 | + | |
| 157 | +int VP9RateControlRTC::GetQP() const { return cpi_->common.base_qindex; } | |
| 158 | + | |
| 159 | +int VP9RateControlRTC::GetLoopfilterLevel() const { | |
| 160 | + struct loopfilter *const lf = &cpi_->common.lf; | |
| 161 | + vp9_pick_filter_level(NULL, cpi_, LPF_PICK_FROM_Q); | |
| 162 | + return lf->filter_level; | |
| 163 | +} | |
| 164 | + | |
| 165 | +void VP9RateControlRTC::PostEncodeUpdate(uint64_t encoded_frame_size) { | |
| 166 | + vp9_rc_postencode_update(cpi_, encoded_frame_size); | |
| 167 | + if (cpi_->svc.number_spatial_layers > 1 || | |
| 168 | + cpi_->svc.number_temporal_layers > 1) | |
| 169 | + vp9_save_layer_context(cpi_); | |
| 170 | + cpi_->common.current_video_frame++; | |
| 171 | +} | |
| 172 | + | |
| 173 | +} // namespace libvpx | |
| @@ -0,0 +1,116 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2020 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 | +#ifndef VPX_VP9_RATECTRL_RTC_H_ | |
| 12 | +#define VPX_VP9_RATECTRL_RTC_H_ | |
| 13 | + | |
| 14 | +#include <cstdint> | |
| 15 | +#include <memory> | |
| 16 | + | |
| 17 | +#include "vp9/common/vp9_entropymode.h" | |
| 18 | +#include "vp9/common/vp9_enums.h" | |
| 19 | +#include "vp9/common/vp9_onyxc_int.h" | |
| 20 | +#include "vp9/vp9_iface_common.h" | |
| 21 | +#include "vp9/encoder/vp9_encoder.h" | |
| 22 | +#include "vp9/encoder/vp9_firstpass.h" | |
| 23 | +#include "vp9/vp9_cx_iface.h" | |
| 24 | +#include "vpx_mem/vpx_mem.h" | |
| 25 | + | |
| 26 | +namespace libvpx { | |
| 27 | + | |
| 28 | +struct VP9RateControlRtcConfig { | |
| 29 | + int width; | |
| 30 | + int height; | |
| 31 | + // 0-63 | |
| 32 | + int max_quantizer; | |
| 33 | + int min_quantizer; | |
| 34 | + int64_t target_bandwidth; | |
| 35 | + int64_t buf_initial_sz; | |
| 36 | + int64_t buf_optimal_sz; | |
| 37 | + int64_t buf_sz; | |
| 38 | + int undershoot_pct; | |
| 39 | + int overshoot_pct; | |
| 40 | + int max_intra_bitrate_pct; | |
| 41 | + double framerate; | |
| 42 | + // Number of spatial layers | |
| 43 | + int ss_number_layers; | |
| 44 | + // Number of temporal layers | |
| 45 | + int ts_number_layers; | |
| 46 | + int max_quantizers[VPX_MAX_LAYERS]; | |
| 47 | + int min_quantizers[VPX_MAX_LAYERS]; | |
| 48 | + int scaling_factor_num[VPX_SS_MAX_LAYERS]; | |
| 49 | + int scaling_factor_den[VPX_SS_MAX_LAYERS]; | |
| 50 | + int layer_target_bitrate[VPX_MAX_LAYERS]; | |
| 51 | + int ts_rate_decimator[VPX_TS_MAX_LAYERS]; | |
| 52 | +}; | |
| 53 | + | |
| 54 | +struct VP9FrameParamsQpRTC { | |
| 55 | + FRAME_TYPE frame_type; | |
| 56 | + int spatial_layer_id; | |
| 57 | + int temporal_layer_id; | |
| 58 | +}; | |
| 59 | + | |
| 60 | +// This interface allows using VP9 real-time rate control without initializing | |
| 61 | +// the encoder. To use this interface, you need to link with libvp9rc.a. | |
| 62 | +// | |
| 63 | +// #include "vp9/ratectrl_rtc.h" | |
| 64 | +// VP9RateControlRTC rc_api; | |
| 65 | +// VP9RateControlRtcConfig cfg; | |
| 66 | +// VP9FrameParamsQpRTC frame_params; | |
| 67 | +// | |
| 68 | +// YourFunctionToInitializeConfig(cfg); | |
| 69 | +// rc_api.InitRateControl(cfg); | |
| 70 | +// // start encoding | |
| 71 | +// while (frame_to_encode) { | |
| 72 | +// if (config_changed) | |
| 73 | +// rc_api.UpdateRateControl(cfg); | |
| 74 | +// YourFunctionToFillFrameParams(frame_params); | |
| 75 | +// rc_api.ComputeQP(frame_params); | |
| 76 | +// YourFunctionToUseQP(rc_api.GetQP()); | |
| 77 | +// YourFunctionToUseLoopfilter(rc_api.GetLoopfilterLevel()); | |
| 78 | +// // After encoding | |
| 79 | +// rc_api.PostEncode(encoded_frame_size); | |
| 80 | +// } | |
| 81 | +class VP9RateControlRTC { | |
| 82 | + public: | |
| 83 | + static std::unique_ptr<VP9RateControlRTC> Create( | |
| 84 | + const VP9RateControlRtcConfig &cfg); | |
| 85 | + ~VP9RateControlRTC() { | |
| 86 | + if (cpi_) { | |
| 87 | + for (int sl = 0; sl < cpi_->svc.number_spatial_layers; sl++) { | |
| 88 | + for (int tl = 0; tl < cpi_->svc.number_temporal_layers; tl++) { | |
| 89 | + int layer = LAYER_IDS_TO_IDX(sl, tl, cpi_->oxcf.ts_number_layers); | |
| 90 | + LAYER_CONTEXT *const lc = &cpi_->svc.layer_context[layer]; | |
| 91 | + vpx_free(lc->map); | |
| 92 | + vpx_free(lc->last_coded_q_map); | |
| 93 | + vpx_free(lc->consec_zero_mv); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + vpx_free(cpi_); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + void UpdateRateControl(const VP9RateControlRtcConfig &rc_cfg); | |
| 101 | + // GetQP() needs to be called after ComputeQP() to get the latest QP | |
| 102 | + int GetQP() const; | |
| 103 | + int GetLoopfilterLevel() const; | |
| 104 | + void ComputeQP(const VP9FrameParamsQpRTC &frame_params); | |
| 105 | + // Feedback to rate control with the size of current encoded frame | |
| 106 | + void PostEncodeUpdate(uint64_t encoded_frame_size); | |
| 107 | + | |
| 108 | + private: | |
| 109 | + VP9RateControlRTC() {} | |
| 110 | + void InitRateControl(const VP9RateControlRtcConfig &cfg); | |
| 111 | + VP9_COMP *cpi_; | |
| 112 | +}; | |
| 113 | + | |
| 114 | +} // namespace libvpx | |
| 115 | + | |
| 116 | +#endif // VPX_VP9_RATECTRL_RTC_H_ | |