2 * Copyright (c) 2020 The WebM project authors. All Rights Reserved.
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.
10 #include "vp9/ratectrl_rtc.h"
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"
21 std::unique_ptr<VP9RateControlRTC> VP9RateControlRTC::Create(
22 const VP9RateControlRtcConfig &cfg) {
23 std::unique_ptr<VP9RateControlRTC> rc_api(new (std::nothrow)
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) {
30 rc_api->InitRateControl(cfg);
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;
41 oxcf->profile = cm->profile;
42 oxcf->bit_depth = cm->bit_depth;
43 oxcf->rc_mode = rc_cfg.rc_mode;
45 oxcf->aq_mode = NO_AQ;
46 oxcf->content = VP9E_CONTENT_DEFAULT;
47 oxcf->drop_frames_water_mark = 0;
48 cm->current_video_frame = 0;
49 rc->kf_boost = DEFAULT_KF_BOOST;
51 UpdateRateControl(rc_cfg);
53 cpi_->use_svc = (cpi_->svc.number_spatial_layers > 1 ||
54 cpi_->svc.number_temporal_layers > 1)
60 vp9_rc_init_minq_luts();
61 vp9_rc_init(oxcf, 0, rc);
62 rc->constrain_gf_key_freq_onepass_vbr = 0;
63 cpi_->sf.use_nonrd_pick_mode = 1;
66 void VP9RateControlRTC::UpdateRateControl(
67 const VP9RateControlRtcConfig &rc_cfg) {
68 VP9_COMMON *cm = &cpi_->common;
69 VP9EncoderConfig *oxcf = &cpi_->oxcf;
70 RATE_CONTROL *const rc = &cpi_->rc;
72 cm->width = rc_cfg.width;
73 cm->height = rc_cfg.height;
74 oxcf->width = rc_cfg.width;
75 oxcf->height = rc_cfg.height;
76 oxcf->worst_allowed_q = vp9_quantizer_to_qindex(rc_cfg.max_quantizer);
77 oxcf->best_allowed_q = vp9_quantizer_to_qindex(rc_cfg.min_quantizer);
78 rc->worst_quality = oxcf->worst_allowed_q;
79 rc->best_quality = oxcf->best_allowed_q;
80 oxcf->init_framerate = rc_cfg.framerate;
81 oxcf->target_bandwidth = 1000 * rc_cfg.target_bandwidth;
82 oxcf->starting_buffer_level_ms = rc_cfg.buf_initial_sz;
83 oxcf->optimal_buffer_level_ms = rc_cfg.buf_optimal_sz;
84 oxcf->maximum_buffer_size_ms = rc_cfg.buf_sz;
85 oxcf->under_shoot_pct = rc_cfg.undershoot_pct;
86 oxcf->over_shoot_pct = rc_cfg.overshoot_pct;
87 oxcf->ss_number_layers = rc_cfg.ss_number_layers;
88 oxcf->ts_number_layers = rc_cfg.ts_number_layers;
89 oxcf->temporal_layering_mode = (VP9E_TEMPORAL_LAYERING_MODE)(
90 (rc_cfg.ts_number_layers > 1) ? rc_cfg.ts_number_layers : 0);
92 cpi_->oxcf.rc_max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct;
93 cpi_->framerate = rc_cfg.framerate;
94 cpi_->svc.number_spatial_layers = rc_cfg.ss_number_layers;
95 cpi_->svc.number_temporal_layers = rc_cfg.ts_number_layers;
97 for (int sl = 0; sl < cpi_->svc.number_spatial_layers; ++sl) {
98 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) {
100 LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
101 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
102 RATE_CONTROL *const lrc = &lc->rc;
103 oxcf->layer_target_bitrate[layer] =
104 1000 * rc_cfg.layer_target_bitrate[layer];
106 vp9_quantizer_to_qindex(rc_cfg.max_quantizers[layer]);
107 lrc->best_quality = vp9_quantizer_to_qindex(rc_cfg.min_quantizers[layer]);
108 lc->scaling_factor_num = rc_cfg.scaling_factor_num[sl];
109 lc->scaling_factor_den = rc_cfg.scaling_factor_den[sl];
110 oxcf->ts_rate_decimator[tl] = rc_cfg.ts_rate_decimator[tl];
113 vp9_set_rc_buffer_sizes(cpi_);
114 vp9_new_framerate(cpi_, cpi_->framerate);
115 if (cpi_->svc.number_temporal_layers > 1 ||
116 cpi_->svc.number_spatial_layers > 1) {
117 if (cm->current_video_frame == 0) vp9_init_layer_context(cpi_);
118 vp9_update_layer_context_change_config(cpi_,
119 (int)cpi_->oxcf.target_bandwidth);
121 vp9_check_reset_rc_flag(cpi_);
124 void VP9RateControlRTC::ComputeQP(const VP9FrameParamsQpRTC &frame_params) {
125 VP9_COMMON *const cm = &cpi_->common;
127 cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id;
128 cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id;
129 if (cpi_->svc.number_spatial_layers > 1) {
130 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
131 cpi_->svc.temporal_layer_id,
132 cpi_->svc.number_temporal_layers);
133 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
134 get_layer_resolution(cpi_->oxcf.width, cpi_->oxcf.height,
135 lc->scaling_factor_num, lc->scaling_factor_den, &width,
140 vp9_set_mb_mi(cm, cm->width, cm->height);
141 cm->frame_type = frame_params.frame_type;
142 cpi_->refresh_golden_frame = (cm->frame_type == KEY_FRAME) ? 1 : 0;
143 cpi_->sf.use_nonrd_pick_mode = 1;
144 if (cpi_->svc.number_spatial_layers == 1 &&
145 cpi_->svc.number_temporal_layers == 1) {
147 if (cpi_->oxcf.rc_mode == VPX_CBR) {
148 if (frame_is_intra_only(cm))
149 target = vp9_calc_iframe_target_size_one_pass_cbr(cpi_);
151 target = vp9_calc_pframe_target_size_one_pass_cbr(cpi_);
152 } else if (cpi_->oxcf.rc_mode == VPX_VBR) {
153 if (cm->frame_type == KEY_FRAME) {
154 cpi_->rc.this_key_frame_forced = cm->current_video_frame != 0;
155 cpi_->rc.frames_to_key = cpi_->oxcf.key_freq;
157 vp9_set_gf_update_one_pass_vbr(cpi_);
158 if (frame_is_intra_only(cm))
159 target = vp9_calc_iframe_target_size_one_pass_vbr(cpi_);
161 target = vp9_calc_pframe_target_size_one_pass_vbr(cpi_);
163 vp9_rc_set_frame_target(cpi_, target);
164 vp9_update_buffer_level_preencode(cpi_);
166 vp9_update_temporal_layer_framerate(cpi_);
167 vp9_restore_layer_context(cpi_);
168 vp9_rc_get_svc_params(cpi_);
170 int bottom_index, top_index;
171 cpi_->common.base_qindex =
172 vp9_rc_pick_q_and_bounds(cpi_, &bottom_index, &top_index);
175 int VP9RateControlRTC::GetQP() const { return cpi_->common.base_qindex; }
177 int VP9RateControlRTC::GetLoopfilterLevel() const {
178 struct loopfilter *const lf = &cpi_->common.lf;
179 vp9_pick_filter_level(nullptr, cpi_, LPF_PICK_FROM_Q);
180 return lf->filter_level;
183 void VP9RateControlRTC::PostEncodeUpdate(uint64_t encoded_frame_size) {
184 vp9_rc_postencode_update(cpi_, encoded_frame_size);
185 if (cpi_->svc.number_spatial_layers > 1 ||
186 cpi_->svc.number_temporal_layers > 1)
187 vp9_save_layer_context(cpi_);
188 cpi_->common.current_video_frame++;
191 } // namespace libvpx