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/common/vp9_common.h"
15 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
16 #include "vp9/encoder/vp9_encoder.h"
17 #include "vp9/encoder/vp9_picklpf.h"
18 #include "vpx/vp8cx.h"
19 #include "vpx/vpx_codec.h"
20 #include "vpx_mem/vpx_mem.h"
21
22 namespace libvpx {
23
Create(const VP9RateControlRtcConfig & cfg)24 std::unique_ptr<VP9RateControlRTC> VP9RateControlRTC::Create(
25 const VP9RateControlRtcConfig &cfg) {
26 std::unique_ptr<VP9RateControlRTC> rc_api(new (std::nothrow)
27 VP9RateControlRTC());
28 if (!rc_api) return nullptr;
29 rc_api->cpi_ = static_cast<VP9_COMP *>(vpx_memalign(32, sizeof(*cpi_)));
30 if (!rc_api->cpi_) return nullptr;
31 vp9_zero(*rc_api->cpi_);
32
33 if (!rc_api->InitRateControl(cfg)) return nullptr;
34 if (cfg.aq_mode) {
35 VP9_COMP *const cpi = rc_api->cpi_;
36 cpi->segmentation_map = static_cast<uint8_t *>(
37 vpx_calloc(cpi->common.mi_rows * cpi->common.mi_cols,
38 sizeof(*cpi->segmentation_map)));
39 if (!cpi->segmentation_map) return nullptr;
40 cpi->cyclic_refresh =
41 vp9_cyclic_refresh_alloc(cpi->common.mi_rows, cpi->common.mi_cols);
42 cpi->cyclic_refresh->content_mode = 0;
43 }
44 return rc_api;
45 }
46
~VP9RateControlRTC()47 VP9RateControlRTC::~VP9RateControlRTC() {
48 if (cpi_) {
49 if (cpi_->svc.number_spatial_layers > 1 ||
50 cpi_->svc.number_temporal_layers > 1) {
51 for (int sl = 0; sl < cpi_->svc.number_spatial_layers; sl++) {
52 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; tl++) {
53 int layer = LAYER_IDS_TO_IDX(sl, tl, cpi_->oxcf.ts_number_layers);
54 LAYER_CONTEXT *const lc = &cpi_->svc.layer_context[layer];
55 vpx_free(lc->map);
56 vpx_free(lc->last_coded_q_map);
57 vpx_free(lc->consec_zero_mv);
58 }
59 }
60 }
61 if (cpi_->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
62 vpx_free(cpi_->segmentation_map);
63 cpi_->segmentation_map = NULL;
64 vp9_cyclic_refresh_free(cpi_->cyclic_refresh);
65 }
66 vpx_free(cpi_);
67 }
68 }
69
InitRateControl(const VP9RateControlRtcConfig & rc_cfg)70 bool VP9RateControlRTC::InitRateControl(const VP9RateControlRtcConfig &rc_cfg) {
71 VP9_COMMON *cm = &cpi_->common;
72 VP9EncoderConfig *oxcf = &cpi_->oxcf;
73 RATE_CONTROL *const rc = &cpi_->rc;
74 cm->profile = PROFILE_0;
75 cm->bit_depth = VPX_BITS_8;
76 cm->show_frame = 1;
77 oxcf->profile = cm->profile;
78 oxcf->bit_depth = cm->bit_depth;
79 oxcf->rc_mode = rc_cfg.rc_mode;
80 oxcf->pass = 0;
81 oxcf->aq_mode = rc_cfg.aq_mode ? CYCLIC_REFRESH_AQ : NO_AQ;
82 oxcf->content = VP9E_CONTENT_DEFAULT;
83 oxcf->drop_frames_water_mark = 0;
84 cm->current_video_frame = 0;
85 rc->kf_boost = DEFAULT_KF_BOOST;
86
87 if (!UpdateRateControl(rc_cfg)) return false;
88 vp9_set_mb_mi(cm, cm->width, cm->height);
89
90 cpi_->use_svc = (cpi_->svc.number_spatial_layers > 1 ||
91 cpi_->svc.number_temporal_layers > 1)
92 ? 1
93 : 0;
94
95 rc->rc_1_frame = 0;
96 rc->rc_2_frame = 0;
97 vp9_rc_init_minq_luts();
98 vp9_rc_init(oxcf, 0, rc);
99 rc->constrain_gf_key_freq_onepass_vbr = 0;
100 cpi_->sf.use_nonrd_pick_mode = 1;
101 return true;
102 }
103
UpdateRateControl(const VP9RateControlRtcConfig & rc_cfg)104 bool VP9RateControlRTC::UpdateRateControl(
105 const VP9RateControlRtcConfig &rc_cfg) {
106 // Since VPX_MAX_LAYERS (12) is less than the product of VPX_SS_MAX_LAYERS (5)
107 // and VPX_TS_MAX_LAYERS (5), check all three.
108 if (rc_cfg.ss_number_layers < 1 ||
109 rc_cfg.ss_number_layers > VPX_SS_MAX_LAYERS ||
110 rc_cfg.ts_number_layers < 1 ||
111 rc_cfg.ts_number_layers > VPX_TS_MAX_LAYERS ||
112 rc_cfg.ss_number_layers * rc_cfg.ts_number_layers > VPX_MAX_LAYERS) {
113 return false;
114 }
115
116 VP9_COMMON *cm = &cpi_->common;
117 VP9EncoderConfig *oxcf = &cpi_->oxcf;
118 RATE_CONTROL *const rc = &cpi_->rc;
119
120 cm->width = rc_cfg.width;
121 cm->height = rc_cfg.height;
122 oxcf->width = rc_cfg.width;
123 oxcf->height = rc_cfg.height;
124 oxcf->worst_allowed_q = vp9_quantizer_to_qindex(rc_cfg.max_quantizer);
125 oxcf->best_allowed_q = vp9_quantizer_to_qindex(rc_cfg.min_quantizer);
126 rc->worst_quality = oxcf->worst_allowed_q;
127 rc->best_quality = oxcf->best_allowed_q;
128 oxcf->init_framerate = rc_cfg.framerate;
129 oxcf->target_bandwidth = 1000 * rc_cfg.target_bandwidth;
130 oxcf->starting_buffer_level_ms = rc_cfg.buf_initial_sz;
131 oxcf->optimal_buffer_level_ms = rc_cfg.buf_optimal_sz;
132 oxcf->maximum_buffer_size_ms = rc_cfg.buf_sz;
133 oxcf->under_shoot_pct = rc_cfg.undershoot_pct;
134 oxcf->over_shoot_pct = rc_cfg.overshoot_pct;
135 oxcf->drop_frames_water_mark = rc_cfg.frame_drop_thresh;
136 oxcf->content = rc_cfg.is_screen ? VP9E_CONTENT_SCREEN : VP9E_CONTENT_DEFAULT;
137 oxcf->ss_number_layers = rc_cfg.ss_number_layers;
138 oxcf->ts_number_layers = rc_cfg.ts_number_layers;
139 oxcf->temporal_layering_mode =
140 (VP9E_TEMPORAL_LAYERING_MODE)((rc_cfg.ts_number_layers > 1)
141 ? rc_cfg.ts_number_layers
142 : 0);
143
144 cpi_->oxcf.rc_max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct;
145 cpi_->oxcf.rc_max_inter_bitrate_pct = rc_cfg.max_inter_bitrate_pct;
146 cpi_->framerate = rc_cfg.framerate;
147 cpi_->svc.number_spatial_layers = rc_cfg.ss_number_layers;
148 cpi_->svc.number_temporal_layers = rc_cfg.ts_number_layers;
149
150 vp9_set_mb_mi(cm, cm->width, cm->height);
151
152 if (setjmp(cpi_->common.error.jmp)) {
153 cpi_->common.error.setjmp = 0;
154 vpx_clear_system_state();
155 return false;
156 }
157 cpi_->common.error.setjmp = 1;
158
159 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) {
160 oxcf->ts_rate_decimator[tl] = rc_cfg.ts_rate_decimator[tl];
161 }
162 for (int sl = 0; sl < cpi_->svc.number_spatial_layers; ++sl) {
163 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) {
164 const int layer =
165 LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
166 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
167 RATE_CONTROL *const lrc = &lc->rc;
168 oxcf->layer_target_bitrate[layer] =
169 1000 * rc_cfg.layer_target_bitrate[layer];
170 lrc->worst_quality =
171 vp9_quantizer_to_qindex(rc_cfg.max_quantizers[layer]);
172 lrc->best_quality = vp9_quantizer_to_qindex(rc_cfg.min_quantizers[layer]);
173 lc->scaling_factor_num = rc_cfg.scaling_factor_num[sl];
174 lc->scaling_factor_den = rc_cfg.scaling_factor_den[sl];
175 }
176 }
177 vp9_set_rc_buffer_sizes(cpi_);
178 vp9_new_framerate(cpi_, cpi_->framerate);
179 if (cpi_->svc.number_temporal_layers > 1 ||
180 cpi_->svc.number_spatial_layers > 1) {
181 if (cm->current_video_frame == 0) {
182 vp9_init_layer_context(cpi_);
183 // svc->framedrop_mode is not currently exposed, so only allow for
184 // full superframe drop for now.
185 cpi_->svc.framedrop_mode = FULL_SUPERFRAME_DROP;
186 }
187 vp9_update_layer_context_change_config(cpi_,
188 (int)cpi_->oxcf.target_bandwidth);
189 cpi_->svc.max_consec_drop = rc_cfg.max_consec_drop;
190 }
191 vp9_check_reset_rc_flag(cpi_);
192
193 cpi_->common.error.setjmp = 0;
194 return true;
195 }
196
197 // Compute the QP for the frame. If the frame is dropped this function
198 // returns kDrop, and no QP is computed. If the frame is encoded (not dropped)
199 // the QP is computed and kOk is returned.
ComputeQP(const VP9FrameParamsQpRTC & frame_params)200 FrameDropDecision VP9RateControlRTC::ComputeQP(
201 const VP9FrameParamsQpRTC &frame_params) {
202 VP9_COMMON *const cm = &cpi_->common;
203 int width, height;
204 cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id;
205 cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id;
206 if (cpi_->svc.number_spatial_layers > 1) {
207 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
208 cpi_->svc.temporal_layer_id,
209 cpi_->svc.number_temporal_layers);
210 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
211 get_layer_resolution(cpi_->oxcf.width, cpi_->oxcf.height,
212 lc->scaling_factor_num, lc->scaling_factor_den, &width,
213 &height);
214 cm->width = width;
215 cm->height = height;
216 }
217 vp9_set_mb_mi(cm, cm->width, cm->height);
218 cm->frame_type = static_cast<FRAME_TYPE>(frame_params.frame_type);
219 // This is needed to ensure key frame does not get unset in rc_get_svc_params.
220 cpi_->frame_flags = (cm->frame_type == KEY_FRAME) ? FRAMEFLAGS_KEY : 0;
221 cpi_->refresh_golden_frame = (cm->frame_type == KEY_FRAME) ? 1 : 0;
222 cpi_->sf.use_nonrd_pick_mode = 1;
223 if (cpi_->svc.number_spatial_layers == 1 &&
224 cpi_->svc.number_temporal_layers == 1) {
225 int target = 0;
226 if (cpi_->oxcf.rc_mode == VPX_CBR) {
227 if (cpi_->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
228 vp9_cyclic_refresh_update_parameters(cpi_);
229 if (frame_is_intra_only(cm))
230 target = vp9_calc_iframe_target_size_one_pass_cbr(cpi_);
231 else
232 target = vp9_calc_pframe_target_size_one_pass_cbr(cpi_);
233 } else if (cpi_->oxcf.rc_mode == VPX_VBR) {
234 if (cm->frame_type == KEY_FRAME) {
235 cpi_->rc.this_key_frame_forced = cm->current_video_frame != 0;
236 cpi_->rc.frames_to_key = cpi_->oxcf.key_freq;
237 }
238 vp9_set_gf_update_one_pass_vbr(cpi_);
239 if (cpi_->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
240 vp9_cyclic_refresh_update_parameters(cpi_);
241 if (frame_is_intra_only(cm))
242 target = vp9_calc_iframe_target_size_one_pass_vbr(cpi_);
243 else
244 target = vp9_calc_pframe_target_size_one_pass_vbr(cpi_);
245 }
246 vp9_rc_set_frame_target(cpi_, target);
247 vp9_update_buffer_level_preencode(cpi_);
248 } else {
249 vp9_update_temporal_layer_framerate(cpi_);
250 vp9_restore_layer_context(cpi_);
251 vp9_rc_get_svc_params(cpi_);
252 }
253 if (cpi_->svc.spatial_layer_id == 0) vp9_zero(cpi_->svc.drop_spatial_layer);
254 // SVC: check for skip encoding of enhancement layer if the
255 // layer target bandwidth = 0.
256 if (vp9_svc_check_skip_enhancement_layer(cpi_))
257 return FrameDropDecision::kDrop;
258 // Check for dropping this frame based on buffer level.
259 // Never drop on key frame, or if base layer is key for svc,
260 if (!frame_is_intra_only(cm) &&
261 (!cpi_->use_svc ||
262 !cpi_->svc.layer_context[cpi_->svc.temporal_layer_id].is_key_frame)) {
263 if (vp9_rc_drop_frame(cpi_)) {
264 // For FULL_SUPERFRAME_DROP mode (the only mode considered here):
265 // if the superframe drop is decided we need to save the layer context for
266 // all spatial layers, and call update_buffer_level and postencode_drop
267 // for all spatial layers.
268 if (cpi_->svc.number_spatial_layers > 1 ||
269 cpi_->svc.number_temporal_layers > 1) {
270 vp9_save_layer_context(cpi_);
271 for (int sl = 1; sl < cpi_->svc.number_spatial_layers; sl++) {
272 cpi_->svc.spatial_layer_id = sl;
273 vp9_restore_layer_context(cpi_);
274 vp9_update_buffer_level_svc_preencode(cpi_);
275 vp9_rc_postencode_update_drop_frame(cpi_);
276 vp9_save_layer_context(cpi_);
277 }
278 }
279 return FrameDropDecision::kDrop;
280 }
281 }
282 // Compute the QP for the frame.
283 int bottom_index, top_index;
284 cpi_->common.base_qindex =
285 vp9_rc_pick_q_and_bounds(cpi_, &bottom_index, &top_index);
286
287 if (cpi_->oxcf.aq_mode == CYCLIC_REFRESH_AQ) vp9_cyclic_refresh_setup(cpi_);
288 if (cpi_->svc.number_spatial_layers > 1 ||
289 cpi_->svc.number_temporal_layers > 1)
290 vp9_save_layer_context(cpi_);
291
292 cpi_->last_frame_dropped = 0;
293 cpi_->svc.last_layer_dropped[cpi_->svc.spatial_layer_id] = 0;
294 if (cpi_->svc.spatial_layer_id == cpi_->svc.number_spatial_layers - 1)
295 cpi_->svc.num_encoded_top_layer++;
296
297 return FrameDropDecision::kOk;
298 }
299
GetQP() const300 int VP9RateControlRTC::GetQP() const { return cpi_->common.base_qindex; }
301
GetLoopfilterLevel() const302 int VP9RateControlRTC::GetLoopfilterLevel() const {
303 struct loopfilter *const lf = &cpi_->common.lf;
304 vp9_pick_filter_level(nullptr, cpi_, LPF_PICK_FROM_Q);
305 return lf->filter_level;
306 }
307
GetSegmentationData(VP9SegmentationData * segmentation_data) const308 bool VP9RateControlRTC::GetSegmentationData(
309 VP9SegmentationData *segmentation_data) const {
310 if (!cpi_->cyclic_refresh || !cpi_->cyclic_refresh->apply_cyclic_refresh) {
311 return false;
312 }
313
314 segmentation_data->segmentation_map = cpi_->segmentation_map;
315 segmentation_data->segmentation_map_size =
316 cpi_->common.mi_cols * cpi_->common.mi_rows;
317 segmentation_data->delta_q = cpi_->cyclic_refresh->qindex_delta;
318 segmentation_data->delta_q_size = 3u;
319 return true;
320 }
321
PostEncodeUpdate(uint64_t encoded_frame_size,const VP9FrameParamsQpRTC & frame_params)322 void VP9RateControlRTC::PostEncodeUpdate(
323 uint64_t encoded_frame_size, const VP9FrameParamsQpRTC &frame_params) {
324 cpi_->common.frame_type = static_cast<FRAME_TYPE>(frame_params.frame_type);
325 cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id;
326 cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id;
327 if (cpi_->svc.number_spatial_layers > 1 ||
328 cpi_->svc.number_temporal_layers > 1) {
329 vp9_restore_layer_context(cpi_);
330 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
331 cpi_->svc.temporal_layer_id,
332 cpi_->svc.number_temporal_layers);
333 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
334 cpi_->common.base_qindex = lc->frame_qp;
335 cpi_->common.MBs = lc->MBs;
336 // For spatial-svc, allow cyclic-refresh to be applied on the spatial
337 // layers, for the base temporal layer.
338 if (cpi_->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
339 cpi_->svc.number_spatial_layers > 1 &&
340 cpi_->svc.temporal_layer_id == 0) {
341 CYCLIC_REFRESH *const cr = cpi_->cyclic_refresh;
342 cr->qindex_delta[0] = lc->qindex_delta[0];
343 cr->qindex_delta[1] = lc->qindex_delta[1];
344 cr->qindex_delta[2] = lc->qindex_delta[2];
345 }
346 }
347 vp9_rc_postencode_update(cpi_, encoded_frame_size);
348 if (cpi_->svc.number_spatial_layers > 1 ||
349 cpi_->svc.number_temporal_layers > 1)
350 vp9_save_layer_context(cpi_);
351 cpi_->common.current_video_frame++;
352 }
353
354 } // namespace libvpx
355