1 /*
2 * Copyright (c) 2021, 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 "av1/ratectrl_rtc.h"
13
14 #include <memory>
15 #include <new>
16
17 #include "aom/aomcx.h"
18 #include "aom/aom_encoder.h"
19 #include "aom_mem/aom_mem.h"
20 #include "av1/encoder/encoder.h"
21 #include "av1/encoder/encoder_utils.h"
22 #include "av1/encoder/pickcdef.h"
23 #include "av1/encoder/picklpf.h"
24 #include "av1/encoder/ratectrl.h"
25 #include "av1/encoder/rc_utils.h"
26 #include "av1/encoder/svc_layercontext.h"
27
28 namespace aom {
29
AV1RateControlRtcConfig()30 AV1RateControlRtcConfig::AV1RateControlRtcConfig() {
31 width = 1280;
32 height = 720;
33 max_quantizer = 63;
34 min_quantizer = 2;
35 target_bandwidth = 1000;
36 buf_initial_sz = 600;
37 buf_optimal_sz = 600;
38 buf_sz = 1000;
39 undershoot_pct = overshoot_pct = 50;
40 max_intra_bitrate_pct = 50;
41 max_inter_bitrate_pct = 0;
42 frame_drop_thresh = 0;
43 max_consec_drop = 0;
44 framerate = 30.0;
45 ss_number_layers = 1;
46 ts_number_layers = 1;
47 aq_mode = 0;
48 layer_target_bitrate[0] = static_cast<int>(target_bandwidth);
49 ts_rate_decimator[0] = 1;
50 av1_zero(max_quantizers);
51 av1_zero(min_quantizers);
52 av1_zero(scaling_factor_den);
53 av1_zero(scaling_factor_num);
54 av1_zero(layer_target_bitrate);
55 av1_zero(ts_rate_decimator);
56 scaling_factor_num[0] = 1;
57 scaling_factor_den[0] = 1;
58 max_quantizers[0] = max_quantizer;
59 min_quantizers[0] = min_quantizer;
60 }
61
Create(const AV1RateControlRtcConfig & cfg)62 std::unique_ptr<AV1RateControlRTC> AV1RateControlRTC::Create(
63 const AV1RateControlRtcConfig &cfg) {
64 std::unique_ptr<AV1RateControlRTC> rc_api(new (std::nothrow)
65 AV1RateControlRTC());
66 if (!rc_api) return nullptr;
67 rc_api->cpi_ = static_cast<AV1_COMP *>(aom_memalign(32, sizeof(*cpi_)));
68 if (!rc_api->cpi_) return nullptr;
69 av1_zero(*rc_api->cpi_);
70 rc_api->cpi_->ppi =
71 static_cast<AV1_PRIMARY *>(aom_memalign(32, sizeof(AV1_PRIMARY)));
72 if (!rc_api->cpi_->ppi) return nullptr;
73 av1_zero(*rc_api->cpi_->ppi);
74 rc_api->cpi_->common.seq_params = &rc_api->cpi_->ppi->seq_params;
75 av1_zero(*rc_api->cpi_->common.seq_params);
76 if (!rc_api->InitRateControl(cfg)) return nullptr;
77 if (cfg.aq_mode) {
78 AV1_COMP *const cpi = rc_api->cpi_;
79 cpi->enc_seg.map = static_cast<uint8_t *>(aom_calloc(
80 cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols,
81 sizeof(*cpi->enc_seg.map)));
82 if (!cpi->enc_seg.map) return nullptr;
83 cpi->cyclic_refresh = av1_cyclic_refresh_alloc(
84 cpi->common.mi_params.mi_rows, cpi->common.mi_params.mi_cols);
85 if (!cpi->cyclic_refresh) return nullptr;
86 }
87 return rc_api;
88 }
89
~AV1RateControlRTC()90 AV1RateControlRTC::~AV1RateControlRTC() {
91 if (cpi_) {
92 if (cpi_->svc.number_spatial_layers > 1 ||
93 cpi_->svc.number_temporal_layers > 1) {
94 for (int sl = 0; sl < cpi_->svc.number_spatial_layers; sl++) {
95 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; tl++) {
96 int layer =
97 LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
98 LAYER_CONTEXT *const lc = &cpi_->svc.layer_context[layer];
99 aom_free(lc->map);
100 }
101 }
102 }
103 aom_free(cpi_->svc.layer_context);
104 cpi_->svc.layer_context = nullptr;
105
106 if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
107 aom_free(cpi_->enc_seg.map);
108 cpi_->enc_seg.map = nullptr;
109 av1_cyclic_refresh_free(cpi_->cyclic_refresh);
110 }
111 aom_free(cpi_->ppi);
112 aom_free(cpi_);
113 }
114 }
115
InitRateControl(const AV1RateControlRtcConfig & rc_cfg)116 bool AV1RateControlRTC::InitRateControl(const AV1RateControlRtcConfig &rc_cfg) {
117 AV1_COMMON *cm = &cpi_->common;
118 AV1EncoderConfig *oxcf = &cpi_->oxcf;
119 RATE_CONTROL *const rc = &cpi_->rc;
120 cm->seq_params->profile = PROFILE_0;
121 cm->seq_params->bit_depth = AOM_BITS_8;
122 cm->show_frame = 1;
123 oxcf->profile = cm->seq_params->profile;
124 oxcf->mode = REALTIME;
125 oxcf->rc_cfg.mode = AOM_CBR;
126 oxcf->pass = AOM_RC_ONE_PASS;
127 oxcf->q_cfg.aq_mode = rc_cfg.aq_mode ? CYCLIC_REFRESH_AQ : NO_AQ;
128 oxcf->tune_cfg.content = AOM_CONTENT_DEFAULT;
129 oxcf->rc_cfg.drop_frames_water_mark = rc_cfg.frame_drop_thresh;
130 rc->max_consec_drop = rc_cfg.max_consec_drop;
131 cpi_->svc.framedrop_mode = AOM_FULL_SUPERFRAME_DROP;
132 oxcf->tool_cfg.bit_depth = AOM_BITS_8;
133 oxcf->tool_cfg.superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
134 oxcf->algo_cfg.loopfilter_control = LOOPFILTER_ALL;
135 cm->current_frame.frame_number = 0;
136 cpi_->ppi->p_rc.kf_boost = DEFAULT_KF_BOOST_RT;
137 for (auto &lvl_idx : oxcf->target_seq_level_idx) lvl_idx = SEQ_LEVEL_MAX;
138
139 memcpy(cpi_->ppi->level_params.target_seq_level_idx,
140 oxcf->target_seq_level_idx, sizeof(oxcf->target_seq_level_idx));
141 if (!UpdateRateControl(rc_cfg)) return false;
142 set_sb_size(cm->seq_params,
143 av1_select_sb_size(oxcf, cm->width, cm->height,
144 cpi_->svc.number_spatial_layers));
145 cpi_->ppi->use_svc = cpi_->svc.number_spatial_layers > 1 ||
146 cpi_->svc.number_temporal_layers > 1;
147 av1_primary_rc_init(oxcf, &cpi_->ppi->p_rc);
148 rc->rc_1_frame = 0;
149 rc->rc_2_frame = 0;
150 av1_rc_init_minq_luts();
151 av1_rc_init(oxcf, rc);
152 // Enable external rate control.
153 cpi_->rc.rtc_external_ratectrl = 1;
154 cpi_->sf.rt_sf.use_nonrd_pick_mode = 1;
155 return true;
156 }
157
UpdateRateControl(const AV1RateControlRtcConfig & rc_cfg)158 bool AV1RateControlRTC::UpdateRateControl(
159 const AV1RateControlRtcConfig &rc_cfg) {
160 if (rc_cfg.ss_number_layers < 1 ||
161 rc_cfg.ss_number_layers > AOM_MAX_SS_LAYERS ||
162 rc_cfg.ts_number_layers < 1 ||
163 rc_cfg.ts_number_layers > AOM_MAX_TS_LAYERS) {
164 return false;
165 }
166 const int num_layers = rc_cfg.ss_number_layers * rc_cfg.ts_number_layers;
167 if (num_layers > 1 && !av1_alloc_layer_context(cpi_, num_layers)) {
168 return false;
169 }
170 AV1_COMMON *cm = &cpi_->common;
171 AV1EncoderConfig *oxcf = &cpi_->oxcf;
172 RATE_CONTROL *const rc = &cpi_->rc;
173 initial_width_ = rc_cfg.width;
174 initial_height_ = rc_cfg.height;
175 cm->width = rc_cfg.width;
176 cm->height = rc_cfg.height;
177 oxcf->frm_dim_cfg.width = rc_cfg.width;
178 oxcf->frm_dim_cfg.height = rc_cfg.height;
179 oxcf->rc_cfg.worst_allowed_q = av1_quantizer_to_qindex(rc_cfg.max_quantizer);
180 oxcf->rc_cfg.best_allowed_q = av1_quantizer_to_qindex(rc_cfg.min_quantizer);
181 rc->worst_quality = oxcf->rc_cfg.worst_allowed_q;
182 rc->best_quality = oxcf->rc_cfg.best_allowed_q;
183 oxcf->input_cfg.init_framerate = rc_cfg.framerate;
184 oxcf->rc_cfg.target_bandwidth = rc_cfg.target_bandwidth > INT64_MAX / 1000
185 ? INT64_MAX
186 : 1000 * rc_cfg.target_bandwidth;
187 oxcf->rc_cfg.starting_buffer_level_ms = rc_cfg.buf_initial_sz;
188 oxcf->rc_cfg.optimal_buffer_level_ms = rc_cfg.buf_optimal_sz;
189 oxcf->rc_cfg.maximum_buffer_size_ms = rc_cfg.buf_sz;
190 oxcf->rc_cfg.under_shoot_pct = rc_cfg.undershoot_pct;
191 oxcf->rc_cfg.over_shoot_pct = rc_cfg.overshoot_pct;
192 oxcf->rc_cfg.drop_frames_water_mark = rc_cfg.frame_drop_thresh;
193 rc->max_consec_drop = rc_cfg.max_consec_drop;
194 oxcf->rc_cfg.max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct;
195 oxcf->rc_cfg.max_inter_bitrate_pct = rc_cfg.max_inter_bitrate_pct;
196 cpi_->framerate = rc_cfg.framerate;
197 if (rc_cfg.is_screen) {
198 cpi_->oxcf.tune_cfg.content = AOM_CONTENT_SCREEN;
199 cpi_->is_screen_content_type = 1;
200 }
201 cpi_->svc.number_spatial_layers = rc_cfg.ss_number_layers;
202 cpi_->svc.number_temporal_layers = rc_cfg.ts_number_layers;
203 set_primary_rc_buffer_sizes(oxcf, cpi_->ppi);
204 enc_set_mb_mi(&cm->mi_params, cm->width, cm->height, BLOCK_8X8);
205 av1_new_framerate(cpi_, cpi_->framerate);
206 if (cpi_->svc.number_temporal_layers > 1 ||
207 cpi_->svc.number_spatial_layers > 1) {
208 int64_t target_bandwidth_svc = 0;
209 for (int sl = 0; sl < cpi_->svc.number_spatial_layers; ++sl) {
210 for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) {
211 const int layer =
212 LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
213 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
214 RATE_CONTROL *const lrc = &lc->rc;
215 lc->layer_target_bitrate = 1000 * rc_cfg.layer_target_bitrate[layer];
216 lc->max_q = rc_cfg.max_quantizers[layer];
217 lc->min_q = rc_cfg.min_quantizers[layer];
218 lrc->worst_quality =
219 av1_quantizer_to_qindex(rc_cfg.max_quantizers[layer]);
220 lrc->best_quality =
221 av1_quantizer_to_qindex(rc_cfg.min_quantizers[layer]);
222 lc->scaling_factor_num = rc_cfg.scaling_factor_num[sl];
223 lc->scaling_factor_den = rc_cfg.scaling_factor_den[sl];
224 lc->framerate_factor = rc_cfg.ts_rate_decimator[tl];
225 if (tl == cpi_->svc.number_temporal_layers - 1)
226 target_bandwidth_svc += lc->layer_target_bitrate;
227 }
228 }
229
230 if (cm->current_frame.frame_number == 0) av1_init_layer_context(cpi_);
231 // This is needed to initialize external RC flag in layer context structure.
232 cpi_->rc.rtc_external_ratectrl = 1;
233 av1_update_layer_context_change_config(cpi_, target_bandwidth_svc);
234 }
235 check_reset_rc_flag(cpi_);
236 return true;
237 }
238
ComputeQP(const AV1FrameParamsRTC & frame_params)239 FrameDropDecision AV1RateControlRTC::ComputeQP(
240 const AV1FrameParamsRTC &frame_params) {
241 AV1_COMMON *const cm = &cpi_->common;
242 int width, height;
243 GF_GROUP *const gf_group = &cpi_->ppi->gf_group;
244 cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id;
245 cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id;
246 if (cpi_->svc.number_spatial_layers > 1) {
247 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
248 cpi_->svc.temporal_layer_id,
249 cpi_->svc.number_temporal_layers);
250 LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
251 av1_get_layer_resolution(initial_width_, initial_height_,
252 lc->scaling_factor_num, lc->scaling_factor_den,
253 &width, &height);
254 cm->width = width;
255 cm->height = height;
256 }
257 enc_set_mb_mi(&cm->mi_params, cm->width, cm->height, BLOCK_8X8);
258 cm->current_frame.frame_type = frame_params.frame_type;
259 cpi_->refresh_frame.golden_frame =
260 (cm->current_frame.frame_type == KEY_FRAME) ? 1 : 0;
261 cpi_->sf.rt_sf.use_nonrd_pick_mode = 1;
262
263 if (frame_params.frame_type == kKeyFrame) {
264 gf_group->update_type[cpi_->gf_frame_index] = KF_UPDATE;
265 gf_group->frame_type[cpi_->gf_frame_index] = KEY_FRAME;
266 gf_group->refbuf_state[cpi_->gf_frame_index] = REFBUF_RESET;
267 if (cpi_->ppi->use_svc) {
268 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
269 cpi_->svc.temporal_layer_id,
270 cpi_->svc.number_temporal_layers);
271 if (cm->current_frame.frame_number > 0)
272 av1_svc_reset_temporal_layers(cpi_, 1);
273 cpi_->svc.layer_context[layer].is_key_frame = 1;
274 }
275 } else {
276 gf_group->update_type[cpi_->gf_frame_index] = LF_UPDATE;
277 gf_group->frame_type[cpi_->gf_frame_index] = INTER_FRAME;
278 gf_group->refbuf_state[cpi_->gf_frame_index] = REFBUF_UPDATE;
279 if (cpi_->ppi->use_svc) {
280 const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
281 cpi_->svc.temporal_layer_id,
282 cpi_->svc.number_temporal_layers);
283 cpi_->svc.layer_context[layer].is_key_frame = 0;
284 }
285 }
286 if (cpi_->svc.spatial_layer_id == cpi_->svc.number_spatial_layers - 1)
287 cpi_->rc.frames_since_key++;
288 if (cpi_->svc.number_spatial_layers > 1 ||
289 cpi_->svc.number_temporal_layers > 1) {
290 av1_update_temporal_layer_framerate(cpi_);
291 av1_restore_layer_context(cpi_);
292 }
293 int target = 0;
294 if (cpi_->oxcf.rc_cfg.mode == AOM_CBR) {
295 if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
296 av1_cyclic_refresh_update_parameters(cpi_);
297 if (frame_is_intra_only(cm)) {
298 target = av1_calc_iframe_target_size_one_pass_cbr(cpi_);
299 cpi_->common.current_frame.frame_number = 0;
300 } else {
301 target = av1_calc_pframe_target_size_one_pass_cbr(
302 cpi_, gf_group->update_type[cpi_->gf_frame_index]);
303 }
304 }
305 av1_rc_set_frame_target(cpi_, target, cm->width, cm->height);
306 // Always drop for spatial enhancement layer if layer bandwidth is 0.
307 // Otherwise check for frame-dropping based on buffer level in
308 // av1_rc_drop_frame().
309 if ((cpi_->svc.spatial_layer_id > 0 &&
310 cpi_->oxcf.rc_cfg.target_bandwidth == 0) ||
311 av1_rc_drop_frame(cpi_)) {
312 cpi_->is_dropped_frame = true;
313 av1_rc_postencode_update_drop_frame(cpi_);
314 cpi_->frame_index_set.show_frame_count++;
315 cpi_->common.current_frame.frame_number++;
316 return FrameDropDecision::kDrop;
317 }
318 int bottom_index = 0, top_index = 0;
319 cpi_->common.quant_params.base_qindex =
320 av1_rc_pick_q_and_bounds(cpi_, cm->width, cm->height,
321 cpi_->gf_frame_index, &bottom_index, &top_index);
322 if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
323 av1_cyclic_refresh_setup(cpi_);
324 return FrameDropDecision::kOk;
325 }
326
GetQP() const327 int AV1RateControlRTC::GetQP() const {
328 return cpi_->common.quant_params.base_qindex;
329 }
330
GetLoopfilterLevel() const331 AV1LoopfilterLevel AV1RateControlRTC::GetLoopfilterLevel() const {
332 av1_pick_filter_level(nullptr, cpi_, LPF_PICK_FROM_Q);
333 AV1LoopfilterLevel lpf_level;
334 lpf_level.filter_level[0] = cpi_->common.lf.filter_level[0];
335 lpf_level.filter_level[1] = cpi_->common.lf.filter_level[1];
336 lpf_level.filter_level_u = cpi_->common.lf.filter_level_u;
337 lpf_level.filter_level_v = cpi_->common.lf.filter_level_v;
338
339 return lpf_level;
340 }
341
GetCdefInfo() const342 AV1CdefInfo AV1RateControlRTC::GetCdefInfo() const {
343 av1_pick_cdef_from_qp(&cpi_->common, 0, 0);
344 AV1CdefInfo cdef_level;
345 cdef_level.cdef_strength_y = cpi_->common.cdef_info.cdef_strengths[0];
346 cdef_level.cdef_strength_uv = cpi_->common.cdef_info.cdef_uv_strengths[0];
347 cdef_level.damping = cpi_->common.cdef_info.cdef_damping;
348
349 return cdef_level;
350 }
351
GetSegmentationData(AV1SegmentationData * segmentation_data) const352 bool AV1RateControlRTC::GetSegmentationData(
353 AV1SegmentationData *segmentation_data) const {
354 if (cpi_->oxcf.q_cfg.aq_mode == 0) {
355 return false;
356 }
357 segmentation_data->segmentation_map = cpi_->enc_seg.map;
358 segmentation_data->segmentation_map_size =
359 cpi_->common.mi_params.mi_rows * cpi_->common.mi_params.mi_cols;
360 segmentation_data->delta_q = cpi_->cyclic_refresh->qindex_delta;
361 segmentation_data->delta_q_size = 3u;
362 return true;
363 }
364
PostEncodeUpdate(uint64_t encoded_frame_size)365 void AV1RateControlRTC::PostEncodeUpdate(uint64_t encoded_frame_size) {
366 cpi_->common.current_frame.frame_number++;
367 if (cpi_->svc.spatial_layer_id == cpi_->svc.number_spatial_layers - 1)
368 cpi_->svc.prev_number_spatial_layers = cpi_->svc.number_spatial_layers;
369 av1_rc_postencode_update(cpi_, encoded_frame_size);
370 if (cpi_->svc.number_spatial_layers > 1 ||
371 cpi_->svc.number_temporal_layers > 1)
372 av1_save_layer_context(cpi_);
373 }
374
375 } // namespace aom
376