• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ratectrl.h"
23 #include "av1/encoder/rc_utils.h"
24 #include "av1/encoder/svc_layercontext.h"
25 
26 namespace aom {
27 
AV1RateControlRtcConfig()28 AV1RateControlRtcConfig::AV1RateControlRtcConfig() {
29   width = 1280;
30   height = 720;
31   max_quantizer = 63;
32   min_quantizer = 2;
33   target_bandwidth = 1000;
34   buf_initial_sz = 600;
35   buf_optimal_sz = 600;
36   buf_sz = 1000;
37   undershoot_pct = overshoot_pct = 50;
38   max_intra_bitrate_pct = 50;
39   max_inter_bitrate_pct = 0;
40   framerate = 30.0;
41   ts_number_layers = 1;
42   aq_mode = 0;
43   layer_target_bitrate[0] = static_cast<int>(target_bandwidth);
44   ts_rate_decimator[0] = 1;
45   av1_zero(max_quantizers);
46   av1_zero(min_quantizers);
47   av1_zero(scaling_factor_den);
48   av1_zero(scaling_factor_num);
49   av1_zero(layer_target_bitrate);
50   av1_zero(ts_rate_decimator);
51   scaling_factor_num[0] = 1;
52   scaling_factor_den[0] = 1;
53   max_quantizers[0] = max_quantizer;
54   min_quantizers[0] = min_quantizer;
55 }
56 
Create(const AV1RateControlRtcConfig & cfg)57 std::unique_ptr<AV1RateControlRTC> AV1RateControlRTC::Create(
58     const AV1RateControlRtcConfig &cfg) {
59   std::unique_ptr<AV1RateControlRTC> rc_api(new (std::nothrow)
60                                                 AV1RateControlRTC());
61   if (!rc_api) return nullptr;
62   rc_api->cpi_ = static_cast<AV1_COMP *>(aom_memalign(32, sizeof(*cpi_)));
63   if (!rc_api->cpi_) return nullptr;
64   av1_zero(*rc_api->cpi_);
65   rc_api->cpi_->ppi =
66       static_cast<AV1_PRIMARY *>(aom_memalign(32, sizeof(AV1_PRIMARY)));
67   if (!rc_api->cpi_->ppi) return nullptr;
68   av1_zero(*rc_api->cpi_->ppi);
69   rc_api->cpi_->common.seq_params = &rc_api->cpi_->ppi->seq_params;
70   av1_zero(*rc_api->cpi_->common.seq_params);
71   const int num_layers = cfg.ss_number_layers * cfg.ts_number_layers;
72   if (!av1_alloc_layer_context(rc_api->cpi_, num_layers)) return nullptr;
73   rc_api->InitRateControl(cfg);
74   if (cfg.aq_mode) {
75     AV1_COMP *const cpi = rc_api->cpi_;
76     cpi->enc_seg.map = static_cast<uint8_t *>(aom_calloc(
77         cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols,
78         sizeof(*cpi->enc_seg.map)));
79     if (!cpi->enc_seg.map) return nullptr;
80     cpi->cyclic_refresh = av1_cyclic_refresh_alloc(
81         cpi->common.mi_params.mi_rows, cpi->common.mi_params.mi_cols);
82     if (!cpi->cyclic_refresh) return nullptr;
83   }
84   return rc_api;
85 }
86 
~AV1RateControlRTC()87 AV1RateControlRTC::~AV1RateControlRTC() {
88   if (cpi_) {
89     if (cpi_->svc.number_spatial_layers > 1 ||
90         cpi_->svc.number_temporal_layers > 1) {
91       for (int sl = 0; sl < cpi_->svc.number_spatial_layers; sl++) {
92         for (int tl = 0; tl < cpi_->svc.number_temporal_layers; tl++) {
93           int layer =
94               LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
95           LAYER_CONTEXT *const lc = &cpi_->svc.layer_context[layer];
96           aom_free(lc->map);
97         }
98       }
99     }
100     aom_free(cpi_->svc.layer_context);
101     cpi_->svc.layer_context = nullptr;
102 
103     if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
104       aom_free(cpi_->enc_seg.map);
105       cpi_->enc_seg.map = nullptr;
106       av1_cyclic_refresh_free(cpi_->cyclic_refresh);
107     }
108     aom_free(cpi_->ppi);
109     aom_free(cpi_);
110   }
111 }
112 
InitRateControl(const AV1RateControlRtcConfig & rc_cfg)113 void AV1RateControlRTC::InitRateControl(const AV1RateControlRtcConfig &rc_cfg) {
114   AV1_COMMON *cm = &cpi_->common;
115   AV1EncoderConfig *oxcf = &cpi_->oxcf;
116   RATE_CONTROL *const rc = &cpi_->rc;
117   cm->seq_params->profile = PROFILE_0;
118   cm->seq_params->bit_depth = AOM_BITS_8;
119   cm->show_frame = 1;
120   oxcf->profile = cm->seq_params->profile;
121   oxcf->mode = REALTIME;
122   oxcf->rc_cfg.mode = AOM_CBR;
123   oxcf->pass = AOM_RC_ONE_PASS;
124   oxcf->q_cfg.aq_mode = rc_cfg.aq_mode ? CYCLIC_REFRESH_AQ : NO_AQ;
125   oxcf->tune_cfg.content = AOM_CONTENT_DEFAULT;
126   oxcf->rc_cfg.drop_frames_water_mark = 0;
127   oxcf->tool_cfg.bit_depth = AOM_BITS_8;
128   oxcf->tool_cfg.superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
129   cm->current_frame.frame_number = 0;
130   cpi_->ppi->p_rc.kf_boost = DEFAULT_KF_BOOST_RT;
131   for (auto &lvl_idx : oxcf->target_seq_level_idx) lvl_idx = SEQ_LEVEL_MAX;
132 
133   memcpy(cpi_->ppi->level_params.target_seq_level_idx,
134          oxcf->target_seq_level_idx, sizeof(oxcf->target_seq_level_idx));
135   UpdateRateControl(rc_cfg);
136   set_sb_size(cm->seq_params,
137               av1_select_sb_size(oxcf, cm->width, cm->height,
138                                  cpi_->svc.number_spatial_layers));
139   cpi_->ppi->use_svc = cpi_->svc.number_spatial_layers > 1 ||
140                        cpi_->svc.number_temporal_layers > 1;
141   av1_primary_rc_init(oxcf, &cpi_->ppi->p_rc);
142   rc->rc_1_frame = 0;
143   rc->rc_2_frame = 0;
144   av1_rc_init_minq_luts();
145   av1_rc_init(oxcf, rc);
146   // Enable external rate control.
147   cpi_->rc.rtc_external_ratectrl = 1;
148   cpi_->sf.rt_sf.use_nonrd_pick_mode = 1;
149 }
150 
UpdateRateControl(const AV1RateControlRtcConfig & rc_cfg)151 void AV1RateControlRTC::UpdateRateControl(
152     const AV1RateControlRtcConfig &rc_cfg) {
153   AV1_COMMON *cm = &cpi_->common;
154   AV1EncoderConfig *oxcf = &cpi_->oxcf;
155   RATE_CONTROL *const rc = &cpi_->rc;
156 
157   initial_width_ = rc_cfg.width;
158   initial_height_ = rc_cfg.height;
159   cm->width = rc_cfg.width;
160   cm->height = rc_cfg.height;
161   oxcf->frm_dim_cfg.width = rc_cfg.width;
162   oxcf->frm_dim_cfg.height = rc_cfg.height;
163   oxcf->rc_cfg.worst_allowed_q = av1_quantizer_to_qindex(rc_cfg.max_quantizer);
164   oxcf->rc_cfg.best_allowed_q = av1_quantizer_to_qindex(rc_cfg.min_quantizer);
165   rc->worst_quality = oxcf->rc_cfg.worst_allowed_q;
166   rc->best_quality = oxcf->rc_cfg.best_allowed_q;
167   oxcf->input_cfg.init_framerate = rc_cfg.framerate;
168   oxcf->rc_cfg.target_bandwidth = rc_cfg.target_bandwidth > INT64_MAX / 1000
169                                       ? INT64_MAX
170                                       : 1000 * rc_cfg.target_bandwidth;
171   oxcf->rc_cfg.starting_buffer_level_ms = rc_cfg.buf_initial_sz;
172   oxcf->rc_cfg.optimal_buffer_level_ms = rc_cfg.buf_optimal_sz;
173   oxcf->rc_cfg.maximum_buffer_size_ms = rc_cfg.buf_sz;
174   oxcf->rc_cfg.under_shoot_pct = rc_cfg.undershoot_pct;
175   oxcf->rc_cfg.over_shoot_pct = rc_cfg.overshoot_pct;
176   oxcf->rc_cfg.max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct;
177   oxcf->rc_cfg.max_inter_bitrate_pct = rc_cfg.max_inter_bitrate_pct;
178   cpi_->framerate = rc_cfg.framerate;
179   cpi_->svc.number_spatial_layers = rc_cfg.ss_number_layers;
180   cpi_->svc.number_temporal_layers = rc_cfg.ts_number_layers;
181   set_primary_rc_buffer_sizes(oxcf, cpi_->ppi);
182   enc_set_mb_mi(&cm->mi_params, cm->width, cm->height, BLOCK_8X8);
183   int64_t target_bandwidth_svc = 0;
184   for (int sl = 0; sl < cpi_->svc.number_spatial_layers; ++sl) {
185     for (int tl = 0; tl < cpi_->svc.number_temporal_layers; ++tl) {
186       const int layer =
187           LAYER_IDS_TO_IDX(sl, tl, cpi_->svc.number_temporal_layers);
188       LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
189       RATE_CONTROL *const lrc = &lc->rc;
190       lc->layer_target_bitrate = 1000 * rc_cfg.layer_target_bitrate[layer];
191       lc->max_q = rc_cfg.max_quantizers[layer];
192       lc->min_q = rc_cfg.min_quantizers[layer];
193       lrc->worst_quality =
194           av1_quantizer_to_qindex(rc_cfg.max_quantizers[layer]);
195       lrc->best_quality = av1_quantizer_to_qindex(rc_cfg.min_quantizers[layer]);
196       lc->scaling_factor_num = rc_cfg.scaling_factor_num[sl];
197       lc->scaling_factor_den = rc_cfg.scaling_factor_den[sl];
198       lc->framerate_factor = rc_cfg.ts_rate_decimator[tl];
199       if (tl == cpi_->svc.number_temporal_layers - 1)
200         target_bandwidth_svc += lc->layer_target_bitrate;
201     }
202   }
203   av1_new_framerate(cpi_, cpi_->framerate);
204   if (cpi_->svc.number_temporal_layers > 1 ||
205       cpi_->svc.number_spatial_layers > 1) {
206     if (cm->current_frame.frame_number == 0) av1_init_layer_context(cpi_);
207     // This is needed to initialize external RC flag in layer context structure.
208     cpi_->rc.rtc_external_ratectrl = 1;
209     av1_update_layer_context_change_config(cpi_, target_bandwidth_svc);
210   }
211   check_reset_rc_flag(cpi_);
212 }
213 
ComputeQP(const AV1FrameParamsRTC & frame_params)214 void AV1RateControlRTC::ComputeQP(const AV1FrameParamsRTC &frame_params) {
215   AV1_COMMON *const cm = &cpi_->common;
216   int width, height;
217   GF_GROUP *const gf_group = &cpi_->ppi->gf_group;
218   cpi_->svc.spatial_layer_id = frame_params.spatial_layer_id;
219   cpi_->svc.temporal_layer_id = frame_params.temporal_layer_id;
220   if (cpi_->svc.number_spatial_layers > 1) {
221     const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
222                                        cpi_->svc.temporal_layer_id,
223                                        cpi_->svc.number_temporal_layers);
224     LAYER_CONTEXT *lc = &cpi_->svc.layer_context[layer];
225     av1_get_layer_resolution(initial_width_, initial_height_,
226                              lc->scaling_factor_num, lc->scaling_factor_den,
227                              &width, &height);
228     cm->width = width;
229     cm->height = height;
230   }
231   enc_set_mb_mi(&cm->mi_params, cm->width, cm->height, BLOCK_8X8);
232   cm->current_frame.frame_type = frame_params.frame_type;
233   cpi_->refresh_frame.golden_frame =
234       (cm->current_frame.frame_type == KEY_FRAME) ? 1 : 0;
235   cpi_->sf.rt_sf.use_nonrd_pick_mode = 1;
236 
237   if (frame_params.frame_type == kKeyFrame) {
238     gf_group->update_type[cpi_->gf_frame_index] = KF_UPDATE;
239     gf_group->frame_type[cpi_->gf_frame_index] = KEY_FRAME;
240     gf_group->refbuf_state[cpi_->gf_frame_index] = REFBUF_RESET;
241     if (cpi_->ppi->use_svc) {
242       const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
243                                          cpi_->svc.temporal_layer_id,
244                                          cpi_->svc.number_temporal_layers);
245       if (cm->current_frame.frame_number > 0)
246         av1_svc_reset_temporal_layers(cpi_, 1);
247       cpi_->svc.layer_context[layer].is_key_frame = 1;
248     }
249   } else {
250     gf_group->update_type[cpi_->gf_frame_index] = LF_UPDATE;
251     gf_group->frame_type[cpi_->gf_frame_index] = INTER_FRAME;
252     gf_group->refbuf_state[cpi_->gf_frame_index] = REFBUF_UPDATE;
253     if (cpi_->ppi->use_svc) {
254       const int layer = LAYER_IDS_TO_IDX(cpi_->svc.spatial_layer_id,
255                                          cpi_->svc.temporal_layer_id,
256                                          cpi_->svc.number_temporal_layers);
257       cpi_->svc.layer_context[layer].is_key_frame = 0;
258     }
259   }
260   if (cpi_->svc.spatial_layer_id == cpi_->svc.number_spatial_layers - 1)
261     cpi_->rc.frames_since_key++;
262   if (cpi_->svc.number_spatial_layers > 1 ||
263       cpi_->svc.number_temporal_layers > 1) {
264     av1_update_temporal_layer_framerate(cpi_);
265     av1_restore_layer_context(cpi_);
266   }
267   int target = 0;
268   if (cpi_->oxcf.rc_cfg.mode == AOM_CBR) {
269     if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
270       av1_cyclic_refresh_update_parameters(cpi_);
271     if (frame_is_intra_only(cm)) {
272       target = av1_calc_iframe_target_size_one_pass_cbr(cpi_);
273       cpi_->common.current_frame.frame_number = 0;
274     } else {
275       target = av1_calc_pframe_target_size_one_pass_cbr(
276           cpi_, gf_group->update_type[cpi_->gf_frame_index]);
277     }
278   }
279   av1_rc_set_frame_target(cpi_, target, cm->width, cm->height);
280 
281   int bottom_index, top_index;
282   cpi_->common.quant_params.base_qindex =
283       av1_rc_pick_q_and_bounds(cpi_, cm->width, cm->height,
284                                cpi_->gf_frame_index, &bottom_index, &top_index);
285 
286   if (cpi_->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
287     av1_cyclic_refresh_setup(cpi_);
288 }
289 
GetQP() const290 int AV1RateControlRTC::GetQP() const {
291   return cpi_->common.quant_params.base_qindex;
292 }
293 
GetCyclicRefreshMap() const294 signed char *AV1RateControlRTC::GetCyclicRefreshMap() const {
295   return cpi_->cyclic_refresh->map;
296 }
297 
GetDeltaQ() const298 int *AV1RateControlRTC::GetDeltaQ() const {
299   return cpi_->cyclic_refresh->qindex_delta;
300 }
301 
PostEncodeUpdate(uint64_t encoded_frame_size)302 void AV1RateControlRTC::PostEncodeUpdate(uint64_t encoded_frame_size) {
303   cpi_->common.current_frame.frame_number++;
304   av1_rc_postencode_update(cpi_, encoded_frame_size);
305   if (cpi_->svc.number_spatial_layers > 1 ||
306       cpi_->svc.number_temporal_layers > 1)
307     av1_save_layer_context(cpi_);
308 }
309 
310 }  // namespace aom
311