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