1 /*
2 * Copyright (c) 2021 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 #include <math.h>
12 #include <new>
13 #include "vp8/vp8_ratectrl_rtc.h"
14 #include "vp8/encoder/ratectrl.h"
15 #include "vpx_ports/system_state.h"
16
17 namespace libvpx {
18 /* Quant MOD */
19 static const int kQTrans[] = {
20 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13, 15, 17, 18, 19,
21 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 37, 39, 41,
22 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 64, 67, 70, 73, 76, 79,
23 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124, 127,
24 };
25
26 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] = {
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
29 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5,
30 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10,
31 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16,
32 16, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
33 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30
34 };
35
36 static const unsigned char inter_minq[QINDEX_RANGE] = {
37 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11,
38 11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
39 24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
40 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
41 54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
42 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
43 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
44 };
45
rescale(int val,int num,int denom)46 static int rescale(int val, int num, int denom) {
47 int64_t llnum = num;
48 int64_t llden = denom;
49 int64_t llval = val;
50
51 return (int)(llval * llnum / llden);
52 }
53
Create(const VP8RateControlRtcConfig & cfg)54 std::unique_ptr<VP8RateControlRTC> VP8RateControlRTC::Create(
55 const VP8RateControlRtcConfig &cfg) {
56 std::unique_ptr<VP8RateControlRTC> rc_api(new (std::nothrow)
57 VP8RateControlRTC());
58 if (!rc_api) return nullptr;
59 rc_api->cpi_ = static_cast<VP8_COMP *>(vpx_memalign(32, sizeof(*cpi_)));
60 if (!rc_api->cpi_) return nullptr;
61 vp8_zero(*rc_api->cpi_);
62
63 rc_api->InitRateControl(cfg);
64
65 return rc_api;
66 }
67
InitRateControl(const VP8RateControlRtcConfig & rc_cfg)68 void VP8RateControlRTC::InitRateControl(const VP8RateControlRtcConfig &rc_cfg) {
69 VP8_COMMON *cm = &cpi_->common;
70 VP8_CONFIG *oxcf = &cpi_->oxcf;
71 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
72 cpi_->pass = 0;
73 cm->show_frame = 1;
74 oxcf->drop_frames_water_mark = 0;
75 cm->current_video_frame = 0;
76 cpi_->auto_gold = 1;
77 cpi_->key_frame_count = 1;
78 cpi_->rate_correction_factor = 1.0;
79 cpi_->key_frame_rate_correction_factor = 1.0;
80 cpi_->cyclic_refresh_mode_enabled = 0;
81 cpi_->auto_worst_q = 1;
82 cpi_->kf_overspend_bits = 0;
83 cpi_->kf_bitrate_adjustment = 0;
84 cpi_->gf_overspend_bits = 0;
85 cpi_->non_gf_bitrate_adjustment = 0;
86 UpdateRateControl(rc_cfg);
87 cpi_->buffer_level = oxcf->starting_buffer_level;
88 cpi_->bits_off_target = oxcf->starting_buffer_level;
89 }
90
UpdateRateControl(const VP8RateControlRtcConfig & rc_cfg)91 void VP8RateControlRTC::UpdateRateControl(
92 const VP8RateControlRtcConfig &rc_cfg) {
93 VP8_COMMON *cm = &cpi_->common;
94 VP8_CONFIG *oxcf = &cpi_->oxcf;
95 const unsigned int prev_number_of_layers = oxcf->number_of_layers;
96 vpx_clear_system_state();
97 cm->Width = rc_cfg.width;
98 cm->Height = rc_cfg.height;
99 oxcf->Width = rc_cfg.width;
100 oxcf->Height = rc_cfg.height;
101 oxcf->worst_allowed_q = kQTrans[rc_cfg.max_quantizer];
102 oxcf->best_allowed_q = kQTrans[rc_cfg.min_quantizer];
103 cpi_->worst_quality = oxcf->worst_allowed_q;
104 cpi_->best_quality = oxcf->best_allowed_q;
105 cpi_->output_framerate = rc_cfg.framerate;
106 oxcf->target_bandwidth =
107 static_cast<unsigned int>(1000 * rc_cfg.target_bandwidth);
108 cpi_->ref_framerate = cpi_->output_framerate;
109 oxcf->fixed_q = -1;
110 oxcf->error_resilient_mode = 1;
111 oxcf->starting_buffer_level_in_ms = rc_cfg.buf_initial_sz;
112 oxcf->optimal_buffer_level_in_ms = rc_cfg.buf_optimal_sz;
113 oxcf->maximum_buffer_size_in_ms = rc_cfg.buf_sz;
114 oxcf->starting_buffer_level = rc_cfg.buf_initial_sz;
115 oxcf->optimal_buffer_level = rc_cfg.buf_optimal_sz;
116 oxcf->maximum_buffer_size = rc_cfg.buf_sz;
117 oxcf->number_of_layers = rc_cfg.ts_number_layers;
118 cpi_->buffered_mode = oxcf->optimal_buffer_level > 0;
119 oxcf->under_shoot_pct = rc_cfg.undershoot_pct;
120 oxcf->over_shoot_pct = rc_cfg.overshoot_pct;
121 cpi_->oxcf.rc_max_intra_bitrate_pct = rc_cfg.max_intra_bitrate_pct;
122 cpi_->framerate = rc_cfg.framerate;
123 for (int i = 0; i < KEY_FRAME_CONTEXT; ++i) {
124 cpi_->prior_key_frame_distance[i] =
125 static_cast<int>(cpi_->output_framerate);
126 }
127
128 if (oxcf->number_of_layers > 1 || prev_number_of_layers > 1) {
129 memcpy(oxcf->target_bitrate, rc_cfg.layer_target_bitrate,
130 sizeof(rc_cfg.layer_target_bitrate));
131 memcpy(oxcf->rate_decimator, rc_cfg.ts_rate_decimator,
132 sizeof(rc_cfg.ts_rate_decimator));
133 if (cm->current_video_frame == 0) {
134 double prev_layer_framerate = 0;
135 for (unsigned int i = 0; i < oxcf->number_of_layers; ++i) {
136 vp8_init_temporal_layer_context(cpi_, oxcf, i, prev_layer_framerate);
137 prev_layer_framerate = cpi_->output_framerate / oxcf->rate_decimator[i];
138 }
139 } else if (oxcf->number_of_layers != prev_number_of_layers) {
140 // The number of temporal layers has changed, so reset/initialize the
141 // temporal layer context for the new layer configuration: this means
142 // calling vp8_reset_temporal_layer_change() below.
143
144 // Start at the base of the pattern cycle, so set the layer id to 0 and
145 // reset the temporal pattern counter.
146 // TODO(marpan/jianj): don't think lines 148-151 are needed (user controls
147 // the layer_id) so remove.
148 if (cpi_->temporal_layer_id > 0) {
149 cpi_->temporal_layer_id = 0;
150 }
151 cpi_->temporal_pattern_counter = 0;
152
153 vp8_reset_temporal_layer_change(cpi_, oxcf,
154 static_cast<int>(prev_number_of_layers));
155 }
156 }
157
158 cpi_->total_actual_bits = 0;
159 cpi_->total_target_vs_actual = 0;
160
161 cm->mb_rows = cm->Height >> 4;
162 cm->mb_cols = cm->Width >> 4;
163 cm->MBs = cm->mb_rows * cm->mb_cols;
164 cm->mode_info_stride = cm->mb_cols + 1;
165
166 // For temporal layers: starting/maximum/optimal_buffer_level is already set
167 // via vp8_init_temporal_layer_context() or vp8_reset_temporal_layer_change().
168 if (oxcf->number_of_layers <= 1 && prev_number_of_layers <= 1) {
169 oxcf->starting_buffer_level =
170 rescale((int)oxcf->starting_buffer_level, oxcf->target_bandwidth, 1000);
171 /* Set or reset optimal and maximum buffer levels. */
172 if (oxcf->optimal_buffer_level == 0) {
173 oxcf->optimal_buffer_level = oxcf->target_bandwidth / 8;
174 } else {
175 oxcf->optimal_buffer_level = rescale((int)oxcf->optimal_buffer_level,
176 oxcf->target_bandwidth, 1000);
177 }
178 if (oxcf->maximum_buffer_size == 0) {
179 oxcf->maximum_buffer_size = oxcf->target_bandwidth / 8;
180 } else {
181 oxcf->maximum_buffer_size =
182 rescale((int)oxcf->maximum_buffer_size, oxcf->target_bandwidth, 1000);
183 }
184 }
185
186 if (cpi_->bits_off_target > oxcf->maximum_buffer_size) {
187 cpi_->bits_off_target = oxcf->maximum_buffer_size;
188 cpi_->buffer_level = cpi_->bits_off_target;
189 }
190
191 vp8_new_framerate(cpi_, cpi_->framerate);
192 vpx_clear_system_state();
193 }
194
ComputeQP(const VP8FrameParamsQpRTC & frame_params)195 void VP8RateControlRTC::ComputeQP(const VP8FrameParamsQpRTC &frame_params) {
196 VP8_COMMON *const cm = &cpi_->common;
197 vpx_clear_system_state();
198 if (cpi_->oxcf.number_of_layers > 1) {
199 cpi_->temporal_layer_id = frame_params.temporal_layer_id;
200 const int layer = frame_params.temporal_layer_id;
201 vp8_update_layer_contexts(cpi_);
202 /* Restore layer specific context & set frame rate */
203 vp8_restore_layer_context(cpi_, layer);
204 vp8_new_framerate(cpi_, cpi_->layer_context[layer].framerate);
205 }
206 cm->frame_type = frame_params.frame_type;
207 cm->refresh_golden_frame = (cm->frame_type == KEY_FRAME) ? 1 : 0;
208 cm->refresh_alt_ref_frame = (cm->frame_type == KEY_FRAME) ? 1 : 0;
209 if (cm->frame_type == KEY_FRAME && cpi_->common.current_video_frame > 0) {
210 cpi_->common.frame_flags |= FRAMEFLAGS_KEY;
211 }
212
213 vp8_pick_frame_size(cpi_);
214
215 if (cpi_->buffer_level >= cpi_->oxcf.optimal_buffer_level &&
216 cpi_->buffered_mode) {
217 /* Max adjustment is 1/4 */
218 int Adjustment = cpi_->active_worst_quality / 4;
219 if (Adjustment) {
220 int buff_lvl_step;
221 if (cpi_->buffer_level < cpi_->oxcf.maximum_buffer_size) {
222 buff_lvl_step = (int)((cpi_->oxcf.maximum_buffer_size -
223 cpi_->oxcf.optimal_buffer_level) /
224 Adjustment);
225 if (buff_lvl_step) {
226 Adjustment =
227 (int)((cpi_->buffer_level - cpi_->oxcf.optimal_buffer_level) /
228 buff_lvl_step);
229 } else {
230 Adjustment = 0;
231 }
232 }
233 cpi_->active_worst_quality -= Adjustment;
234 if (cpi_->active_worst_quality < cpi_->active_best_quality) {
235 cpi_->active_worst_quality = cpi_->active_best_quality;
236 }
237 }
238 }
239
240 if (cpi_->ni_frames > 150) {
241 int q = cpi_->active_worst_quality;
242 if (cm->frame_type == KEY_FRAME) {
243 cpi_->active_best_quality = kf_high_motion_minq[q];
244 } else {
245 cpi_->active_best_quality = inter_minq[q];
246 }
247
248 if (cpi_->buffer_level >= cpi_->oxcf.maximum_buffer_size) {
249 cpi_->active_best_quality = cpi_->best_quality;
250
251 } else if (cpi_->buffer_level > cpi_->oxcf.optimal_buffer_level) {
252 int Fraction =
253 (int)(((cpi_->buffer_level - cpi_->oxcf.optimal_buffer_level) * 128) /
254 (cpi_->oxcf.maximum_buffer_size -
255 cpi_->oxcf.optimal_buffer_level));
256 int min_qadjustment =
257 ((cpi_->active_best_quality - cpi_->best_quality) * Fraction) / 128;
258
259 cpi_->active_best_quality -= min_qadjustment;
260 }
261 }
262
263 /* Clip the active best and worst quality values to limits */
264 if (cpi_->active_worst_quality > cpi_->worst_quality) {
265 cpi_->active_worst_quality = cpi_->worst_quality;
266 }
267 if (cpi_->active_best_quality < cpi_->best_quality) {
268 cpi_->active_best_quality = cpi_->best_quality;
269 }
270 if (cpi_->active_worst_quality < cpi_->active_best_quality) {
271 cpi_->active_worst_quality = cpi_->active_best_quality;
272 }
273
274 q_ = vp8_regulate_q(cpi_, cpi_->this_frame_target);
275 vp8_set_quantizer(cpi_, q_);
276 vpx_clear_system_state();
277 }
278
GetQP() const279 int VP8RateControlRTC::GetQP() const { return q_; }
280
PostEncodeUpdate(uint64_t encoded_frame_size)281 void VP8RateControlRTC::PostEncodeUpdate(uint64_t encoded_frame_size) {
282 VP8_COMMON *const cm = &cpi_->common;
283 vpx_clear_system_state();
284 cpi_->total_byte_count += encoded_frame_size;
285 cpi_->projected_frame_size = static_cast<int>(encoded_frame_size << 3);
286 if (cpi_->oxcf.number_of_layers > 1) {
287 for (unsigned int i = cpi_->current_layer + 1;
288 i < cpi_->oxcf.number_of_layers; ++i) {
289 cpi_->layer_context[i].total_byte_count += encoded_frame_size;
290 }
291 }
292
293 vp8_update_rate_correction_factors(cpi_, 2);
294
295 cpi_->last_q[cm->frame_type] = cm->base_qindex;
296
297 if (cm->frame_type == KEY_FRAME) {
298 vp8_adjust_key_frame_context(cpi_);
299 }
300
301 /* Keep a record of ambient average Q. */
302 if (cm->frame_type != KEY_FRAME) {
303 cpi_->avg_frame_qindex =
304 (2 + 3 * cpi_->avg_frame_qindex + cm->base_qindex) >> 2;
305 }
306 /* Keep a record from which we can calculate the average Q excluding
307 * key frames.
308 */
309 if (cm->frame_type != KEY_FRAME) {
310 cpi_->ni_frames++;
311 /* Damp value for first few frames */
312 if (cpi_->ni_frames > 150) {
313 cpi_->ni_tot_qi += q_;
314 cpi_->ni_av_qi = (cpi_->ni_tot_qi / cpi_->ni_frames);
315 } else {
316 cpi_->ni_tot_qi += q_;
317 cpi_->ni_av_qi =
318 ((cpi_->ni_tot_qi / cpi_->ni_frames) + cpi_->worst_quality + 1) / 2;
319 }
320
321 /* If the average Q is higher than what was used in the last
322 * frame (after going through the recode loop to keep the frame
323 * size within range) then use the last frame value - 1. The -1
324 * is designed to stop Q and hence the data rate, from
325 * progressively falling away during difficult sections, but at
326 * the same time reduce the number of itterations around the
327 * recode loop.
328 */
329 if (q_ > cpi_->ni_av_qi) cpi_->ni_av_qi = q_ - 1;
330 }
331
332 cpi_->bits_off_target +=
333 cpi_->av_per_frame_bandwidth - cpi_->projected_frame_size;
334 if (cpi_->bits_off_target > cpi_->oxcf.maximum_buffer_size) {
335 cpi_->bits_off_target = cpi_->oxcf.maximum_buffer_size;
336 }
337
338 cpi_->total_actual_bits += cpi_->projected_frame_size;
339 cpi_->buffer_level = cpi_->bits_off_target;
340
341 /* Propagate values to higher temporal layers */
342 if (cpi_->oxcf.number_of_layers > 1) {
343 for (unsigned int i = cpi_->current_layer + 1;
344 i < cpi_->oxcf.number_of_layers; ++i) {
345 LAYER_CONTEXT *lc = &cpi_->layer_context[i];
346 int bits_off_for_this_layer = (int)round(
347 lc->target_bandwidth / lc->framerate - cpi_->projected_frame_size);
348
349 lc->bits_off_target += bits_off_for_this_layer;
350
351 /* Clip buffer level to maximum buffer size for the layer */
352 if (lc->bits_off_target > lc->maximum_buffer_size) {
353 lc->bits_off_target = lc->maximum_buffer_size;
354 }
355
356 lc->total_actual_bits += cpi_->projected_frame_size;
357 lc->total_target_vs_actual += bits_off_for_this_layer;
358 lc->buffer_level = lc->bits_off_target;
359 }
360 }
361
362 cpi_->common.current_video_frame++;
363 cpi_->frames_since_key++;
364
365 if (cpi_->oxcf.number_of_layers > 1) vp8_save_layer_context(cpi_);
366 vpx_clear_system_state();
367 }
368 } // namespace libvpx
369