• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 <assert.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_mem/aom_mem.h"
22 #include "aom_ports/mem.h"
23 #include "aom_ports/aom_once.h"
24 
25 #include "av1/common/alloccommon.h"
26 #include "av1/encoder/aq_cyclicrefresh.h"
27 #include "av1/common/common.h"
28 #include "av1/common/entropymode.h"
29 #include "av1/common/quant_common.h"
30 #include "av1/common/seg_common.h"
31 
32 #include "av1/encoder/encodemv.h"
33 #include "av1/encoder/encoder_utils.h"
34 #include "av1/encoder/encode_strategy.h"
35 #include "av1/encoder/gop_structure.h"
36 #include "av1/encoder/random.h"
37 #include "av1/encoder/ratectrl.h"
38 
39 #include "config/aom_dsp_rtcd.h"
40 
41 #define USE_UNRESTRICTED_Q_IN_CQ_MODE 0
42 
43 // Max rate target for 1080P and below encodes under normal circumstances
44 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
45 #define MAX_MB_RATE 250
46 #define MAXRATE_1080P 2025000
47 
48 #define MIN_BPB_FACTOR 0.005
49 #define MAX_BPB_FACTOR 50
50 
51 #define SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO 0
52 #define SUPERRES_QADJ_PER_DENOM_KEYFRAME 2
53 #define SUPERRES_QADJ_PER_DENOM_ARFFRAME 0
54 
55 #define FRAME_OVERHEAD_BITS 200
56 #define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
57   do {                                                       \
58     switch (bit_depth) {                                     \
59       case AOM_BITS_8: name = name##_8; break;               \
60       case AOM_BITS_10: name = name##_10; break;             \
61       case AOM_BITS_12: name = name##_12; break;             \
62       default:                                               \
63         assert(0 &&                                          \
64                "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
65                " or AOM_BITS_12");                           \
66         name = NULL;                                         \
67     }                                                        \
68   } while (0)
69 
70 // Tables relating active max Q to active min Q
71 static int kf_low_motion_minq_8[QINDEX_RANGE];
72 static int kf_high_motion_minq_8[QINDEX_RANGE];
73 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
74 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
75 static int inter_minq_8[QINDEX_RANGE];
76 static int rtc_minq_8[QINDEX_RANGE];
77 
78 static int kf_low_motion_minq_10[QINDEX_RANGE];
79 static int kf_high_motion_minq_10[QINDEX_RANGE];
80 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
81 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
82 static int inter_minq_10[QINDEX_RANGE];
83 static int rtc_minq_10[QINDEX_RANGE];
84 static int kf_low_motion_minq_12[QINDEX_RANGE];
85 static int kf_high_motion_minq_12[QINDEX_RANGE];
86 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
87 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
88 static int inter_minq_12[QINDEX_RANGE];
89 static int rtc_minq_12[QINDEX_RANGE];
90 
91 static int gf_high = 2400;
92 static int gf_low = 300;
93 #ifdef STRICT_RC
94 static int kf_high = 3200;
95 #else
96 static int kf_high = 5000;
97 #endif
98 static int kf_low = 400;
99 
100 // How many times less pixels there are to encode given the current scaling.
101 // Temporary replacement for rcf_mult and rate_thresh_mult.
resize_rate_factor(const FrameDimensionCfg * const frm_dim_cfg,int width,int height)102 static double resize_rate_factor(const FrameDimensionCfg *const frm_dim_cfg,
103                                  int width, int height) {
104   return (double)(frm_dim_cfg->width * frm_dim_cfg->height) / (width * height);
105 }
106 
107 // Functions to compute the active minq lookup table entries based on a
108 // formulaic approach to facilitate easier adjustment of the Q tables.
109 // The formulae were derived from computing a 3rd order polynomial best
110 // fit to the original data (after plotting real maxq vs minq (not q index))
get_minq_index(double maxq,double x3,double x2,double x1,aom_bit_depth_t bit_depth)111 static int get_minq_index(double maxq, double x3, double x2, double x1,
112                           aom_bit_depth_t bit_depth) {
113   const double minqtarget = AOMMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
114 
115   // Special case handling to deal with the step from q2.0
116   // down to lossless mode represented by q 1.0.
117   if (minqtarget <= 2.0) return 0;
118 
119   return av1_find_qindex(minqtarget, bit_depth, 0, QINDEX_RANGE - 1);
120 }
121 
init_minq_luts(int * kf_low_m,int * kf_high_m,int * arfgf_low,int * arfgf_high,int * inter,int * rtc,aom_bit_depth_t bit_depth)122 static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
123                            int *arfgf_high, int *inter, int *rtc,
124                            aom_bit_depth_t bit_depth) {
125   int i;
126   for (i = 0; i < QINDEX_RANGE; i++) {
127     const double maxq = av1_convert_qindex_to_q(i, bit_depth);
128     kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
129     kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
130     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
131     arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
132     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
133     rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
134   }
135 }
136 
rc_init_minq_luts(void)137 static void rc_init_minq_luts(void) {
138   init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
139                  arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
140                  inter_minq_8, rtc_minq_8, AOM_BITS_8);
141   init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
142                  arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
143                  inter_minq_10, rtc_minq_10, AOM_BITS_10);
144   init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
145                  arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
146                  inter_minq_12, rtc_minq_12, AOM_BITS_12);
147 }
148 
av1_rc_init_minq_luts(void)149 void av1_rc_init_minq_luts(void) { aom_once(rc_init_minq_luts); }
150 
151 // These functions use formulaic calculations to make playing with the
152 // quantizer tables easier. If necessary they can be replaced by lookup
153 // tables if and when things settle down in the experimental bitstream
av1_convert_qindex_to_q(int qindex,aom_bit_depth_t bit_depth)154 double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
155   // Convert the index to a real Q value (scaled down to match old Q values)
156   switch (bit_depth) {
157     case AOM_BITS_8: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 4.0;
158     case AOM_BITS_10: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 16.0;
159     case AOM_BITS_12: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 64.0;
160     default:
161       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
162       return -1.0;
163   }
164 }
165 
av1_get_bpmb_enumerator(FRAME_TYPE frame_type,const int is_screen_content_type)166 int av1_get_bpmb_enumerator(FRAME_TYPE frame_type,
167                             const int is_screen_content_type) {
168   int enumerator;
169 
170   if (is_screen_content_type) {
171     enumerator = (frame_type == KEY_FRAME) ? 1000000 : 750000;
172   } else {
173     enumerator = (frame_type == KEY_FRAME) ? 2000000 : 1500000;
174   }
175 
176   return enumerator;
177 }
178 
get_init_ratio(double sse)179 static int get_init_ratio(double sse) { return (int)(300000 / sse); }
180 
av1_rc_bits_per_mb(const AV1_COMP * cpi,FRAME_TYPE frame_type,int qindex,double correction_factor,int accurate_estimate)181 int av1_rc_bits_per_mb(const AV1_COMP *cpi, FRAME_TYPE frame_type, int qindex,
182                        double correction_factor, int accurate_estimate) {
183   const AV1_COMMON *const cm = &cpi->common;
184   const int is_screen_content_type = cpi->is_screen_content_type;
185   const aom_bit_depth_t bit_depth = cm->seq_params->bit_depth;
186   const double q = av1_convert_qindex_to_q(qindex, bit_depth);
187   int enumerator = av1_get_bpmb_enumerator(frame_type, is_screen_content_type);
188 
189   assert(correction_factor <= MAX_BPB_FACTOR &&
190          correction_factor >= MIN_BPB_FACTOR);
191 
192   if (cpi->oxcf.rc_cfg.mode == AOM_CBR && frame_type != KEY_FRAME &&
193       accurate_estimate && cpi->rec_sse != UINT64_MAX) {
194     const int mbs = cm->mi_params.MBs;
195     const double sse_sqrt =
196         (double)((int)sqrt((double)(cpi->rec_sse)) << BPER_MB_NORMBITS) /
197         (double)mbs;
198     const int ratio = (cpi->rc.bit_est_ratio == 0) ? get_init_ratio(sse_sqrt)
199                                                    : cpi->rc.bit_est_ratio;
200     // Clamp the enumerator to lower the q fluctuations.
201     enumerator = AOMMIN(AOMMAX((int)(ratio * sse_sqrt), 20000), 170000);
202   }
203 
204   // q based adjustment to baseline enumerator
205   return (int)(enumerator * correction_factor / q);
206 }
207 
av1_estimate_bits_at_q(const AV1_COMP * cpi,int q,double correction_factor)208 int av1_estimate_bits_at_q(const AV1_COMP *cpi, int q,
209                            double correction_factor) {
210   const AV1_COMMON *const cm = &cpi->common;
211   const FRAME_TYPE frame_type = cm->current_frame.frame_type;
212   const int mbs = cm->mi_params.MBs;
213   const int bpm =
214       (int)(av1_rc_bits_per_mb(cpi, frame_type, q, correction_factor,
215                                cpi->sf.hl_sf.accurate_bit_estimate));
216   return AOMMAX(FRAME_OVERHEAD_BITS,
217                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
218 }
219 
av1_rc_clamp_pframe_target_size(const AV1_COMP * const cpi,int target,FRAME_UPDATE_TYPE frame_update_type)220 int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target,
221                                     FRAME_UPDATE_TYPE frame_update_type) {
222   const RATE_CONTROL *rc = &cpi->rc;
223   const AV1EncoderConfig *oxcf = &cpi->oxcf;
224   const int min_frame_target =
225       AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
226   // Clip the frame target to the minimum setup value.
227   if (frame_update_type == OVERLAY_UPDATE ||
228       frame_update_type == INTNL_OVERLAY_UPDATE) {
229     // If there is an active ARF at this location use the minimum
230     // bits on this frame even if it is a constructed arf.
231     // The active maximum quantizer insures that an appropriate
232     // number of bits will be spent if needed for constructed ARFs.
233     target = min_frame_target;
234   } else if (target < min_frame_target) {
235     target = min_frame_target;
236   }
237 
238   // Clip the frame target to the maximum allowed value.
239   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
240   if (oxcf->rc_cfg.max_inter_bitrate_pct) {
241     const int max_rate =
242         rc->avg_frame_bandwidth * oxcf->rc_cfg.max_inter_bitrate_pct / 100;
243     target = AOMMIN(target, max_rate);
244   }
245 
246   return target;
247 }
248 
av1_rc_clamp_iframe_target_size(const AV1_COMP * const cpi,int64_t target)249 int av1_rc_clamp_iframe_target_size(const AV1_COMP *const cpi, int64_t target) {
250   const RATE_CONTROL *rc = &cpi->rc;
251   const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
252   if (rc_cfg->max_intra_bitrate_pct) {
253     const int64_t max_rate =
254         (int64_t)rc->avg_frame_bandwidth * rc_cfg->max_intra_bitrate_pct / 100;
255     target = AOMMIN(target, max_rate);
256   }
257   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
258   return (int)target;
259 }
260 
261 // Update the buffer level for higher temporal layers, given the encoded current
262 // temporal layer.
update_layer_buffer_level(SVC * svc,int encoded_frame_size,bool is_screen)263 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size,
264                                       bool is_screen) {
265   const int current_temporal_layer = svc->temporal_layer_id;
266   for (int i = current_temporal_layer + 1; i < svc->number_temporal_layers;
267        ++i) {
268     const int layer =
269         LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
270     LAYER_CONTEXT *lc = &svc->layer_context[layer];
271     PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc;
272     lp_rc->bits_off_target +=
273         (int)round(lc->target_bandwidth / lc->framerate) - encoded_frame_size;
274     // Clip buffer level to maximum buffer size for the layer.
275     lp_rc->bits_off_target =
276         AOMMIN(lp_rc->bits_off_target, lp_rc->maximum_buffer_size);
277     lp_rc->buffer_level = lp_rc->bits_off_target;
278 
279     // For screen-content mode: don't let buffer level go below threshold,
280     // given here as -rc->maximum_ buffer_size, to allow buffer to come back
281     // up sooner after slide change with big oveshoot.
282     if (is_screen) {
283       lp_rc->bits_off_target =
284           AOMMAX(lp_rc->bits_off_target, -lp_rc->maximum_buffer_size);
285       lp_rc->buffer_level = lp_rc->bits_off_target;
286     }
287   }
288 }
289 // Update the buffer level: leaky bucket model.
update_buffer_level(AV1_COMP * cpi,int encoded_frame_size)290 static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) {
291   const AV1_COMMON *const cm = &cpi->common;
292   RATE_CONTROL *const rc = &cpi->rc;
293   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
294 
295   // Non-viewable frames are a special case and are treated as pure overhead.
296   if (!cm->show_frame)
297     p_rc->bits_off_target -= encoded_frame_size;
298   else
299     p_rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
300 
301   // Clip the buffer level to the maximum specified buffer size.
302   p_rc->bits_off_target =
303       AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
304   // For screen-content mode: don't let buffel level go below threshold,
305   // given here as -rc->maximum_ buffer_size, to allow buffer to come back
306   // up sooner after slide change with big oveshoot.
307   if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN)
308     p_rc->bits_off_target =
309         AOMMAX(p_rc->bits_off_target, -p_rc->maximum_buffer_size);
310   p_rc->buffer_level = p_rc->bits_off_target;
311 
312   if (cpi->ppi->use_svc)
313     update_layer_buffer_level(&cpi->svc, encoded_frame_size,
314                               cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN);
315 
316 #if CONFIG_FPMT_TEST
317   /* The variable temp_buffer_level is introduced for quality
318    * simulation purpose, it retains the value previous to the parallel
319    * encode frames. The variable is updated based on the update flag.
320    *
321    * If there exist show_existing_frames between parallel frames, then to
322    * retain the temp state do not update it. */
323   int show_existing_between_parallel_frames =
324       (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
325            INTNL_OVERLAY_UPDATE &&
326        cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
327 
328   if (cpi->do_frame_data_update && !show_existing_between_parallel_frames &&
329       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
330     p_rc->temp_buffer_level = p_rc->buffer_level;
331   }
332 #endif
333 }
334 
av1_rc_get_default_min_gf_interval(int width,int height,double framerate)335 int av1_rc_get_default_min_gf_interval(int width, int height,
336                                        double framerate) {
337   // Assume we do not need any constraint lower than 4K 20 fps
338   static const double factor_safe = 3840 * 2160 * 20.0;
339   const double factor = (double)width * height * framerate;
340   const int default_interval =
341       clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
342 
343   if (factor <= factor_safe)
344     return default_interval;
345   else
346     return AOMMAX(default_interval,
347                   (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
348   // Note this logic makes:
349   // 4K24: 5
350   // 4K30: 6
351   // 4K60: 12
352 }
353 
av1_rc_get_default_max_gf_interval(double framerate,int min_gf_interval)354 int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
355   int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
356   interval += (interval & 0x01);  // Round to even value
357   interval = AOMMAX(MAX_GF_INTERVAL, interval);
358   return AOMMAX(interval, min_gf_interval);
359 }
360 
av1_primary_rc_init(const AV1EncoderConfig * oxcf,PRIMARY_RATE_CONTROL * p_rc)361 void av1_primary_rc_init(const AV1EncoderConfig *oxcf,
362                          PRIMARY_RATE_CONTROL *p_rc) {
363   const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
364 
365   int worst_allowed_q = rc_cfg->worst_allowed_q;
366 
367   int min_gf_interval = oxcf->gf_cfg.min_gf_interval;
368   int max_gf_interval = oxcf->gf_cfg.max_gf_interval;
369   if (min_gf_interval == 0)
370     min_gf_interval = av1_rc_get_default_min_gf_interval(
371         oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
372         oxcf->input_cfg.init_framerate);
373   if (max_gf_interval == 0)
374     max_gf_interval = av1_rc_get_default_max_gf_interval(
375         oxcf->input_cfg.init_framerate, min_gf_interval);
376   p_rc->baseline_gf_interval = (min_gf_interval + max_gf_interval) / 2;
377   p_rc->this_key_frame_forced = 0;
378   p_rc->next_key_frame_forced = 0;
379   p_rc->ni_frames = 0;
380 
381   p_rc->tot_q = 0.0;
382   p_rc->total_actual_bits = 0;
383   p_rc->total_target_bits = 0;
384   p_rc->buffer_level = p_rc->starting_buffer_level;
385 
386   if (oxcf->target_seq_level_idx[0] < SEQ_LEVELS) {
387     worst_allowed_q = 255;
388   }
389   if (oxcf->pass == AOM_RC_ONE_PASS && rc_cfg->mode == AOM_CBR) {
390     p_rc->avg_frame_qindex[KEY_FRAME] = worst_allowed_q;
391     p_rc->avg_frame_qindex[INTER_FRAME] = worst_allowed_q;
392   } else {
393     p_rc->avg_frame_qindex[KEY_FRAME] =
394         (worst_allowed_q + rc_cfg->best_allowed_q) / 2;
395     p_rc->avg_frame_qindex[INTER_FRAME] =
396         (worst_allowed_q + rc_cfg->best_allowed_q) / 2;
397   }
398   p_rc->avg_q = av1_convert_qindex_to_q(rc_cfg->worst_allowed_q,
399                                         oxcf->tool_cfg.bit_depth);
400   p_rc->last_q[KEY_FRAME] = rc_cfg->best_allowed_q;
401   p_rc->last_q[INTER_FRAME] = rc_cfg->worst_allowed_q;
402 
403   for (int i = 0; i < RATE_FACTOR_LEVELS; ++i) {
404     p_rc->rate_correction_factors[i] = 0.7;
405   }
406   p_rc->rate_correction_factors[KF_STD] = 1.0;
407   p_rc->bits_off_target = p_rc->starting_buffer_level;
408 
409   p_rc->rolling_target_bits = AOMMAX(
410       1, (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate));
411   p_rc->rolling_actual_bits = AOMMAX(
412       1, (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate));
413 }
414 
av1_rc_init(const AV1EncoderConfig * oxcf,RATE_CONTROL * rc)415 void av1_rc_init(const AV1EncoderConfig *oxcf, RATE_CONTROL *rc) {
416   const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
417 
418   rc->frames_since_key = 8;  // Sensible default for first frame.
419   rc->frames_to_fwd_kf = oxcf->kf_cfg.fwd_kf_dist;
420 
421   rc->frames_till_gf_update_due = 0;
422   rc->ni_av_qi = rc_cfg->worst_allowed_q;
423   rc->ni_tot_qi = 0;
424 
425   rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
426   rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
427   if (rc->min_gf_interval == 0)
428     rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
429         oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
430         oxcf->input_cfg.init_framerate);
431   if (rc->max_gf_interval == 0)
432     rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
433         oxcf->input_cfg.init_framerate, rc->min_gf_interval);
434   rc->avg_frame_low_motion = 0;
435 
436   rc->resize_state = ORIG;
437   rc->resize_avg_qp = 0;
438   rc->resize_buffer_underflow = 0;
439   rc->resize_count = 0;
440   rc->rtc_external_ratectrl = 0;
441   rc->frame_level_fast_extra_bits = 0;
442   rc->use_external_qp_one_pass = 0;
443   rc->percent_blocks_inactive = 0;
444 }
445 
check_buffer_below_thresh(AV1_COMP * cpi,int64_t buffer_level,int drop_mark)446 static bool check_buffer_below_thresh(AV1_COMP *cpi, int64_t buffer_level,
447                                       int drop_mark) {
448   SVC *svc = &cpi->svc;
449   if (!cpi->ppi->use_svc || cpi->svc.number_spatial_layers == 1 ||
450       cpi->svc.framedrop_mode == AOM_LAYER_DROP) {
451     return (buffer_level <= drop_mark);
452   } else {
453     // For SVC in the AOM_FULL_SUPERFRAME_DROP): the condition on
454     // buffer is checked on current and upper spatial layers.
455     for (int i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) {
456       const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id,
457                                          svc->number_temporal_layers);
458       LAYER_CONTEXT *lc = &svc->layer_context[layer];
459       PRIMARY_RATE_CONTROL *lrc = &lc->p_rc;
460       // Exclude check for layer whose bitrate is 0.
461       if (lc->target_bandwidth > 0) {
462         const int drop_thresh = cpi->oxcf.rc_cfg.drop_frames_water_mark;
463         const int drop_mark_layer =
464             (int)(drop_thresh * lrc->optimal_buffer_level / 100);
465         if (lrc->buffer_level <= drop_mark_layer) return true;
466       }
467     }
468     return false;
469   }
470 }
471 
av1_rc_drop_frame(AV1_COMP * cpi)472 int av1_rc_drop_frame(AV1_COMP *cpi) {
473   const AV1EncoderConfig *oxcf = &cpi->oxcf;
474   RATE_CONTROL *const rc = &cpi->rc;
475   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
476 #if CONFIG_FPMT_TEST
477   const int simulate_parallel_frame =
478       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
479       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
480   int64_t buffer_level =
481       simulate_parallel_frame ? p_rc->temp_buffer_level : p_rc->buffer_level;
482 #else
483   int64_t buffer_level = p_rc->buffer_level;
484 #endif
485   // Never drop on key frame, or for frame whose base layer is key.
486   // If drop_count_consec hits or exceeds max_consec_drop then don't drop.
487   if (cpi->common.current_frame.frame_type == KEY_FRAME ||
488       (cpi->ppi->use_svc &&
489        cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame) ||
490       !oxcf->rc_cfg.drop_frames_water_mark ||
491       (rc->max_consec_drop > 0 &&
492        rc->drop_count_consec >= rc->max_consec_drop)) {
493     return 0;
494   } else {
495     SVC *svc = &cpi->svc;
496     // In the full_superframe framedrop mode for svc, if the previous spatial
497     // layer was dropped, drop the current spatial layer.
498     if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 &&
499         svc->drop_spatial_layer[svc->spatial_layer_id - 1] &&
500         svc->framedrop_mode == AOM_FULL_SUPERFRAME_DROP)
501       return 1;
502     // -1 is passed here for drop_mark since we are checking if
503     // buffer goes below 0 (<= -1).
504     if (check_buffer_below_thresh(cpi, buffer_level, -1)) {
505       // Always drop if buffer is below 0.
506       rc->drop_count_consec++;
507       return 1;
508     } else {
509       // If buffer is below drop_mark, for now just drop every other frame
510       // (starting with the next frame) until it increases back over drop_mark.
511       const int drop_mark = (int)(oxcf->rc_cfg.drop_frames_water_mark *
512                                   p_rc->optimal_buffer_level / 100);
513       const bool buffer_below_thresh =
514           check_buffer_below_thresh(cpi, buffer_level, drop_mark);
515       if (!buffer_below_thresh && rc->decimation_factor > 0) {
516         --rc->decimation_factor;
517       } else if (buffer_below_thresh && rc->decimation_factor == 0) {
518         rc->decimation_factor = 1;
519       }
520       if (rc->decimation_factor > 0) {
521         if (rc->decimation_count > 0) {
522           --rc->decimation_count;
523           rc->drop_count_consec++;
524           return 1;
525         } else {
526           rc->decimation_count = rc->decimation_factor;
527           return 0;
528         }
529       } else {
530         rc->decimation_count = 0;
531         return 0;
532       }
533     }
534   }
535 }
536 
adjust_q_cbr(const AV1_COMP * cpi,int q,int active_worst_quality,int width,int height)537 static int adjust_q_cbr(const AV1_COMP *cpi, int q, int active_worst_quality,
538                         int width, int height) {
539   const RATE_CONTROL *const rc = &cpi->rc;
540   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
541   const AV1_COMMON *const cm = &cpi->common;
542   const SVC *const svc = &cpi->svc;
543   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
544   // Flag to indicate previous frame has overshoot, and buffer level
545   // for current frame is low (less than ~half of optimal). For such
546   // (inter) frames, if the source_sad is non-zero, relax the max_delta_up
547   // and clamp applied below.
548   const bool overshoot_buffer_low =
549       cpi->rc.rc_1_frame == -1 && rc->frame_source_sad > 1000 &&
550       p_rc->buffer_level < (p_rc->optimal_buffer_level >> 1) &&
551       rc->frames_since_key > 4;
552   int max_delta_down;
553   int max_delta_up = overshoot_buffer_low ? 60 : 20;
554   const int change_avg_frame_bandwidth =
555       abs(rc->avg_frame_bandwidth - rc->prev_avg_frame_bandwidth) >
556       0.1 * (rc->avg_frame_bandwidth);
557 
558   // Set the maximum adjustment down for Q for this frame.
559   if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
560       cpi->cyclic_refresh->apply_cyclic_refresh) {
561     // For static screen type content limit the Q drop till the start of the
562     // next refresh cycle.
563     if (cpi->is_screen_content_type &&
564         (cpi->cyclic_refresh->sb_index > cpi->cyclic_refresh->last_sb_index)) {
565       max_delta_down = AOMMIN(8, AOMMAX(1, rc->q_1_frame / 32));
566     } else {
567       max_delta_down = AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8));
568     }
569     if (!cpi->ppi->use_svc && cpi->is_screen_content_type) {
570       // Link max_delta_up to max_delta_down and buffer status.
571       if (p_rc->buffer_level > p_rc->optimal_buffer_level) {
572         max_delta_up = AOMMAX(4, max_delta_down);
573       } else {
574         max_delta_up = AOMMAX(8, max_delta_down);
575       }
576     }
577   } else {
578     max_delta_down = (cpi->is_screen_content_type)
579                          ? AOMMIN(8, AOMMAX(1, rc->q_1_frame / 16))
580                          : AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8));
581   }
582   // If resolution changes or avg_frame_bandwidth significantly changed,
583   // then set this flag to indicate change in target bits per macroblock.
584   const int change_target_bits_mb =
585       cm->prev_frame &&
586       (width != cm->prev_frame->width || height != cm->prev_frame->height ||
587        change_avg_frame_bandwidth);
588   // Apply some control/clamp to QP under certain conditions.
589   // Delay the use of the clamping for svc until after num_temporal_layers,
590   // to make they have been set for each temporal layer.
591   if (!frame_is_intra_only(cm) && rc->frames_since_key > 1 &&
592       (!cpi->ppi->use_svc ||
593        svc->current_superframe > (unsigned int)svc->number_temporal_layers) &&
594       !change_target_bits_mb && !cpi->rc.rtc_external_ratectrl &&
595       (!cpi->oxcf.rc_cfg.gf_cbr_boost_pct ||
596        !(refresh_frame->alt_ref_frame || refresh_frame->golden_frame))) {
597     // If in the previous two frames we have seen both overshoot and undershoot
598     // clamp Q between the two. Check for rc->q_1/2_frame > 0 in case they have
599     // not been set due to dropped frames.
600     if (rc->rc_1_frame * rc->rc_2_frame == -1 &&
601         rc->q_1_frame != rc->q_2_frame && rc->q_1_frame > 0 &&
602         rc->q_2_frame > 0 && !overshoot_buffer_low) {
603       int qclamp = clamp(q, AOMMIN(rc->q_1_frame, rc->q_2_frame),
604                          AOMMAX(rc->q_1_frame, rc->q_2_frame));
605       // If the previous frame had overshoot and the current q needs to
606       // increase above the clamped value, reduce the clamp for faster reaction
607       // to overshoot.
608       if (cpi->rc.rc_1_frame == -1 && q > qclamp && rc->frames_since_key > 10)
609         q = (q + qclamp) >> 1;
610       else
611         q = qclamp;
612     }
613     // Adjust Q base on source content change from scene detection.
614     if (cpi->sf.rt_sf.check_scene_detection && rc->prev_avg_source_sad > 0 &&
615         rc->frames_since_key > 10 && rc->frame_source_sad > 0 &&
616         !cpi->rc.rtc_external_ratectrl) {
617       const int bit_depth = cm->seq_params->bit_depth;
618       double delta =
619           (double)rc->avg_source_sad / (double)rc->prev_avg_source_sad - 1.0;
620       // Push Q downwards if content change is decreasing and buffer level
621       // is stable (at least 1/4-optimal level), so not overshooting. Do so
622       // only for high Q to avoid excess overshoot.
623       // Else reduce decrease in Q from previous frame if content change is
624       // increasing and buffer is below max (so not undershooting).
625       if (delta < 0.0 &&
626           p_rc->buffer_level > (p_rc->optimal_buffer_level >> 2) &&
627           q > (rc->worst_quality >> 1)) {
628         double q_adj_factor = 1.0 + 0.5 * tanh(4.0 * delta);
629         double q_val = av1_convert_qindex_to_q(q, bit_depth);
630         q += av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
631       } else if (rc->q_1_frame - q > 0 && delta > 0.1 &&
632                  p_rc->buffer_level < AOMMIN(p_rc->maximum_buffer_size,
633                                              p_rc->optimal_buffer_level << 1)) {
634         q = (3 * q + rc->q_1_frame) >> 2;
635       }
636     }
637     // Limit the decrease in Q from previous frame.
638     if (rc->q_1_frame - q > max_delta_down) q = rc->q_1_frame - max_delta_down;
639     // Limit the increase in Q from previous frame.
640     else if (q - rc->q_1_frame > max_delta_up)
641       q = rc->q_1_frame + max_delta_up;
642   }
643   // Adjustment for temporal layers.
644   if (svc->number_temporal_layers > 1 && svc->spatial_layer_id == 0 &&
645       !change_target_bits_mb && !cpi->rc.rtc_external_ratectrl &&
646       cpi->oxcf.resize_cfg.resize_mode != RESIZE_DYNAMIC) {
647     if (svc->temporal_layer_id > 0) {
648       // Constrain enhancement relative to the previous base TL0.
649       // Get base temporal layer TL0.
650       const int layer = LAYER_IDS_TO_IDX(0, 0, svc->number_temporal_layers);
651       LAYER_CONTEXT *lc = &svc->layer_context[layer];
652       // lc->rc.avg_frame_bandwidth and lc->p_rc.last_q correspond to the
653       // last TL0 frame.
654       if (rc->avg_frame_bandwidth < lc->rc.avg_frame_bandwidth &&
655           q < lc->p_rc.last_q[INTER_FRAME] - 4)
656         q = lc->p_rc.last_q[INTER_FRAME] - 4;
657     } else if (cpi->svc.temporal_layer_id == 0 &&
658                p_rc->buffer_level > (p_rc->optimal_buffer_level >> 2) &&
659                rc->frame_source_sad < 100000) {
660       // Push base TL0 Q down if buffer is stable and frame_source_sad
661       // is below threshold.
662       int delta = (svc->number_temporal_layers == 2) ? 4 : 10;
663       q = q - delta;
664     }
665   }
666   // For non-svc (single layer): if resolution has increased push q closer
667   // to the active_worst to avoid excess overshoot.
668   if (!cpi->ppi->use_svc && cm->prev_frame &&
669       (width * height > 1.5 * cm->prev_frame->width * cm->prev_frame->height))
670     q = (q + active_worst_quality) >> 1;
671   // For single layer RPS: Bias Q based on distance of closest reference.
672   if (cpi->ppi->rtc_ref.bias_recovery_frame) {
673     const int min_dist = av1_svc_get_min_ref_dist(cpi);
674     q = q - AOMMIN(min_dist, 20);
675   }
676   return AOMMAX(AOMMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
677 }
678 
679 static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = {
680   KF_STD,        // KF_UPDATE
681   INTER_NORMAL,  // LF_UPDATE
682   GF_ARF_STD,    // GF_UPDATE
683   GF_ARF_STD,    // ARF_UPDATE
684   INTER_NORMAL,  // OVERLAY_UPDATE
685   INTER_NORMAL,  // INTNL_OVERLAY_UPDATE
686   GF_ARF_LOW,    // INTNL_ARF_UPDATE
687 };
688 
get_rate_factor_level(const GF_GROUP * const gf_group,int gf_frame_index)689 static RATE_FACTOR_LEVEL get_rate_factor_level(const GF_GROUP *const gf_group,
690                                                int gf_frame_index) {
691   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_frame_index];
692   assert(update_type < FRAME_UPDATE_TYPES);
693   return rate_factor_levels[update_type];
694 }
695 
696 /*!\brief Gets a rate vs Q correction factor
697  *
698  * This function returns the current value of a correction factor used to
699  * dynamilcally adjust the relationship between Q and the expected number
700  * of bits for the frame.
701  *
702  * \ingroup rate_control
703  * \param[in]   cpi                   Top level encoder instance structure
704  * \param[in]   width                 Frame width
705  * \param[in]   height                Frame height
706  *
707  * \return Returns a correction factor for the current frame
708  */
get_rate_correction_factor(const AV1_COMP * cpi,int width,int height)709 static double get_rate_correction_factor(const AV1_COMP *cpi, int width,
710                                          int height) {
711   const RATE_CONTROL *const rc = &cpi->rc;
712   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
713   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
714   double rcf;
715   double rate_correction_factors_kfstd;
716   double rate_correction_factors_gfarfstd;
717   double rate_correction_factors_internormal;
718 
719   rate_correction_factors_kfstd =
720       (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
721           ? rc->frame_level_rate_correction_factors[KF_STD]
722           : p_rc->rate_correction_factors[KF_STD];
723   rate_correction_factors_gfarfstd =
724       (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
725           ? rc->frame_level_rate_correction_factors[GF_ARF_STD]
726           : p_rc->rate_correction_factors[GF_ARF_STD];
727   rate_correction_factors_internormal =
728       (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
729           ? rc->frame_level_rate_correction_factors[INTER_NORMAL]
730           : p_rc->rate_correction_factors[INTER_NORMAL];
731 
732   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
733     rcf = rate_correction_factors_kfstd;
734   } else if (is_stat_consumption_stage(cpi)) {
735     const RATE_FACTOR_LEVEL rf_lvl =
736         get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index);
737     double rate_correction_factors_rflvl =
738         (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
739             ? rc->frame_level_rate_correction_factors[rf_lvl]
740             : p_rc->rate_correction_factors[rf_lvl];
741     rcf = rate_correction_factors_rflvl;
742   } else {
743     if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) &&
744         !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
745         (cpi->oxcf.rc_cfg.mode != AOM_CBR ||
746          cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20))
747       rcf = rate_correction_factors_gfarfstd;
748     else
749       rcf = rate_correction_factors_internormal;
750   }
751   rcf *= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
752   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
753 }
754 
755 /*!\brief Sets a rate vs Q correction factor
756  *
757  * This function updates the current value of a correction factor used to
758  * dynamilcally adjust the relationship between Q and the expected number
759  * of bits for the frame.
760  *
761  * \ingroup rate_control
762  * \param[in]   cpi                   Top level encoder instance structure
763  * \param[in]   is_encode_stage       Indicates if recode loop or post-encode
764  * \param[in]   factor                New correction factor
765  * \param[in]   width                 Frame width
766  * \param[in]   height                Frame height
767  *
768  * \remark Updates the rate correction factor for the
769  *         current frame type in cpi->rc.
770  */
set_rate_correction_factor(AV1_COMP * cpi,int is_encode_stage,double factor,int width,int height)771 static void set_rate_correction_factor(AV1_COMP *cpi, int is_encode_stage,
772                                        double factor, int width, int height) {
773   RATE_CONTROL *const rc = &cpi->rc;
774   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
775   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
776   int update_default_rcf = 1;
777   // Normalize RCF to account for the size-dependent scaling factor.
778   factor /= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
779 
780   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
781 
782   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
783     p_rc->rate_correction_factors[KF_STD] = factor;
784   } else if (is_stat_consumption_stage(cpi)) {
785     const RATE_FACTOR_LEVEL rf_lvl =
786         get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index);
787     if (is_encode_stage &&
788         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
789       rc->frame_level_rate_correction_factors[rf_lvl] = factor;
790       update_default_rcf = 0;
791     }
792     if (update_default_rcf) p_rc->rate_correction_factors[rf_lvl] = factor;
793   } else {
794     if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) &&
795         !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
796         (cpi->oxcf.rc_cfg.mode != AOM_CBR ||
797          cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20)) {
798       p_rc->rate_correction_factors[GF_ARF_STD] = factor;
799     } else {
800       if (is_encode_stage &&
801           cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
802         rc->frame_level_rate_correction_factors[INTER_NORMAL] = factor;
803         update_default_rcf = 0;
804       }
805       if (update_default_rcf)
806         p_rc->rate_correction_factors[INTER_NORMAL] = factor;
807     }
808   }
809 }
810 
av1_rc_update_rate_correction_factors(AV1_COMP * cpi,int is_encode_stage,int width,int height)811 void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int is_encode_stage,
812                                            int width, int height) {
813   const AV1_COMMON *const cm = &cpi->common;
814   double correction_factor = 1.0;
815   double rate_correction_factor =
816       get_rate_correction_factor(cpi, width, height);
817   double adjustment_limit;
818   int projected_size_based_on_q = 0;
819   int cyclic_refresh_active =
820       cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled;
821 
822   // Do not update the rate factors for arf overlay frames.
823   if (cpi->rc.is_src_frame_alt_ref) return;
824 
825   // Don't update rate correction factors here on scene changes as
826   // it is already reset in av1_encodedframe_overshoot_cbr(),
827   // but reset variables related to previous frame q and size.
828   // Note that the counter of frames since the last scene change
829   // is only valid when cyclic refresh mode is enabled and that
830   // this break out only applies to scene changes that are not
831   // recorded as INTRA only key frames.
832   if ((cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) &&
833       (cpi->cyclic_refresh->counter_encode_maxq_scene_change == 0) &&
834       !frame_is_intra_only(cm) && !cpi->ppi->use_svc) {
835     cpi->rc.q_2_frame = cm->quant_params.base_qindex;
836     cpi->rc.q_1_frame = cm->quant_params.base_qindex;
837     cpi->rc.rc_2_frame = 0;
838     cpi->rc.rc_1_frame = 0;
839     return;
840   }
841 
842   // Clear down mmx registers to allow floating point in what follows
843 
844   // Work out how big we would have expected the frame to be at this Q given
845   // the current correction factor.
846   // Stay in double to avoid int overflow when values are large
847   if (cyclic_refresh_active) {
848     projected_size_based_on_q =
849         av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
850   } else {
851     projected_size_based_on_q = av1_estimate_bits_at_q(
852         cpi, cm->quant_params.base_qindex, rate_correction_factor);
853   }
854   // Work out a size correction factor.
855   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
856     correction_factor = (double)cpi->rc.projected_frame_size /
857                         (double)projected_size_based_on_q;
858 
859   // Clamp correction factor to prevent anything too extreme
860   correction_factor = AOMMAX(correction_factor, 0.25);
861 
862   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
863   cpi->rc.q_1_frame = cm->quant_params.base_qindex;
864   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
865   if (correction_factor > 1.1)
866     cpi->rc.rc_1_frame = -1;
867   else if (correction_factor < 0.9)
868     cpi->rc.rc_1_frame = 1;
869   else
870     cpi->rc.rc_1_frame = 0;
871 
872   // Decide how heavily to dampen the adjustment
873   if (correction_factor > 0.0) {
874     if (cpi->is_screen_content_type) {
875       adjustment_limit =
876           0.25 + 0.5 * AOMMIN(0.5, fabs(log10(correction_factor)));
877     } else {
878       adjustment_limit =
879           0.25 + 0.75 * AOMMIN(0.5, fabs(log10(correction_factor)));
880     }
881   } else {
882     adjustment_limit = 0.75;
883   }
884 
885   // Adjustment to delta Q and number of blocks updated in cyclic refressh
886   // based on over or under shoot of target in current frame.
887   if (cyclic_refresh_active && cpi->rc.this_frame_target > 0) {
888     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
889     if (correction_factor > 1.25) {
890       cr->percent_refresh_adjustment =
891           AOMMAX(cr->percent_refresh_adjustment - 1, -5);
892       cr->rate_ratio_qdelta_adjustment =
893           AOMMAX(cr->rate_ratio_qdelta_adjustment - 0.05, -0.0);
894     } else if (correction_factor < 0.5) {
895       cr->percent_refresh_adjustment =
896           AOMMIN(cr->percent_refresh_adjustment + 1, 5);
897       cr->rate_ratio_qdelta_adjustment =
898           AOMMIN(cr->rate_ratio_qdelta_adjustment + 0.05, 0.25);
899     }
900   }
901 
902   if (correction_factor > 1.01) {
903     // We are not already at the worst allowable quality
904     correction_factor = (1.0 + ((correction_factor - 1.0) * adjustment_limit));
905     rate_correction_factor = rate_correction_factor * correction_factor;
906     // Keep rate_correction_factor within limits
907     if (rate_correction_factor > MAX_BPB_FACTOR)
908       rate_correction_factor = MAX_BPB_FACTOR;
909   } else if (correction_factor < 0.99) {
910     // We are not already at the best allowable quality
911     correction_factor = 1.0 / correction_factor;
912     correction_factor = (1.0 + ((correction_factor - 1.0) * adjustment_limit));
913     correction_factor = 1.0 / correction_factor;
914 
915     rate_correction_factor = rate_correction_factor * correction_factor;
916 
917     // Keep rate_correction_factor within limits
918     if (rate_correction_factor < MIN_BPB_FACTOR)
919       rate_correction_factor = MIN_BPB_FACTOR;
920   }
921 
922   set_rate_correction_factor(cpi, is_encode_stage, rate_correction_factor,
923                              width, height);
924 }
925 
926 // Calculate rate for the given 'q'.
get_bits_per_mb(const AV1_COMP * cpi,int use_cyclic_refresh,double correction_factor,int q)927 static int get_bits_per_mb(const AV1_COMP *cpi, int use_cyclic_refresh,
928                            double correction_factor, int q) {
929   const AV1_COMMON *const cm = &cpi->common;
930   return use_cyclic_refresh
931              ? av1_cyclic_refresh_rc_bits_per_mb(cpi, q, correction_factor)
932              : av1_rc_bits_per_mb(cpi, cm->current_frame.frame_type, q,
933                                   correction_factor,
934                                   cpi->sf.hl_sf.accurate_bit_estimate);
935 }
936 
937 /*!\brief Searches for a Q index value predicted to give an average macro
938  * block rate closest to the target value.
939  *
940  * Similar to find_qindex_by_rate() function, but returns a q index with a
941  * rate just above or below the desired rate, depending on which of the two
942  * rates is closer to the desired rate.
943  * Also, respects the selected aq_mode when computing the rate.
944  *
945  * \ingroup rate_control
946  * \param[in]   desired_bits_per_mb   Target bits per mb
947  * \param[in]   cpi                   Top level encoder instance structure
948  * \param[in]   correction_factor     Current Q to rate correction factor
949  * \param[in]   best_qindex           Min allowed Q value.
950  * \param[in]   worst_qindex          Max allowed Q value.
951  *
952  * \return Returns a correction factor for the current frame
953  */
find_closest_qindex_by_rate(int desired_bits_per_mb,const AV1_COMP * cpi,double correction_factor,int best_qindex,int worst_qindex)954 static int find_closest_qindex_by_rate(int desired_bits_per_mb,
955                                        const AV1_COMP *cpi,
956                                        double correction_factor,
957                                        int best_qindex, int worst_qindex) {
958   const int use_cyclic_refresh = cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
959                                  cpi->cyclic_refresh->apply_cyclic_refresh;
960 
961   // Find 'qindex' based on 'desired_bits_per_mb'.
962   assert(best_qindex <= worst_qindex);
963   int low = best_qindex;
964   int high = worst_qindex;
965   while (low < high) {
966     const int mid = (low + high) >> 1;
967     const int mid_bits_per_mb =
968         get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, mid);
969     if (mid_bits_per_mb > desired_bits_per_mb) {
970       low = mid + 1;
971     } else {
972       high = mid;
973     }
974   }
975   assert(low == high);
976 
977   // Calculate rate difference of this q index from the desired rate.
978   const int curr_q = low;
979   const int curr_bits_per_mb =
980       get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, curr_q);
981   const int curr_bit_diff = (curr_bits_per_mb <= desired_bits_per_mb)
982                                 ? desired_bits_per_mb - curr_bits_per_mb
983                                 : INT_MAX;
984   assert((curr_bit_diff != INT_MAX && curr_bit_diff >= 0) ||
985          curr_q == worst_qindex);
986 
987   // Calculate rate difference for previous q index too.
988   const int prev_q = curr_q - 1;
989   int prev_bit_diff;
990   if (curr_bit_diff == INT_MAX || curr_q == best_qindex) {
991     prev_bit_diff = INT_MAX;
992   } else {
993     const int prev_bits_per_mb =
994         get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, prev_q);
995     assert(prev_bits_per_mb > desired_bits_per_mb);
996     prev_bit_diff = prev_bits_per_mb - desired_bits_per_mb;
997   }
998 
999   // Pick one of the two q indices, depending on which one has rate closer to
1000   // the desired rate.
1001   return (curr_bit_diff <= prev_bit_diff) ? curr_q : prev_q;
1002 }
1003 
av1_rc_regulate_q(const AV1_COMP * cpi,int target_bits_per_frame,int active_best_quality,int active_worst_quality,int width,int height)1004 int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
1005                       int active_best_quality, int active_worst_quality,
1006                       int width, int height) {
1007   const int MBs = av1_get_MBs(width, height);
1008   const double correction_factor =
1009       get_rate_correction_factor(cpi, width, height);
1010   const int target_bits_per_mb =
1011       (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / MBs);
1012 
1013   int q =
1014       find_closest_qindex_by_rate(target_bits_per_mb, cpi, correction_factor,
1015                                   active_best_quality, active_worst_quality);
1016   if (cpi->oxcf.rc_cfg.mode == AOM_CBR && has_no_stats_stage(cpi))
1017     return adjust_q_cbr(cpi, q, active_worst_quality, width, height);
1018 
1019   return q;
1020 }
1021 
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)1022 static int get_active_quality(int q, int gfu_boost, int low, int high,
1023                               int *low_motion_minq, int *high_motion_minq) {
1024   if (gfu_boost > high) {
1025     return low_motion_minq[q];
1026   } else if (gfu_boost < low) {
1027     return high_motion_minq[q];
1028   } else {
1029     const int gap = high - low;
1030     const int offset = high - gfu_boost;
1031     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
1032     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
1033     return low_motion_minq[q] + adjustment;
1034   }
1035 }
1036 
get_kf_active_quality(const PRIMARY_RATE_CONTROL * const p_rc,int q,aom_bit_depth_t bit_depth)1037 static int get_kf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
1038                                  aom_bit_depth_t bit_depth) {
1039   int *kf_low_motion_minq;
1040   int *kf_high_motion_minq;
1041   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
1042   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
1043   return get_active_quality(q, p_rc->kf_boost, kf_low, kf_high,
1044                             kf_low_motion_minq, kf_high_motion_minq);
1045 }
1046 
get_gf_active_quality_no_rc(int gfu_boost,int q,aom_bit_depth_t bit_depth)1047 static int get_gf_active_quality_no_rc(int gfu_boost, int q,
1048                                        aom_bit_depth_t bit_depth) {
1049   int *arfgf_low_motion_minq;
1050   int *arfgf_high_motion_minq;
1051   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
1052   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
1053   return get_active_quality(q, gfu_boost, gf_low, gf_high,
1054                             arfgf_low_motion_minq, arfgf_high_motion_minq);
1055 }
1056 
get_gf_active_quality(const PRIMARY_RATE_CONTROL * const p_rc,int q,aom_bit_depth_t bit_depth)1057 static int get_gf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
1058                                  aom_bit_depth_t bit_depth) {
1059   return get_gf_active_quality_no_rc(p_rc->gfu_boost, q, bit_depth);
1060 }
1061 
get_gf_high_motion_quality(int q,aom_bit_depth_t bit_depth)1062 static int get_gf_high_motion_quality(int q, aom_bit_depth_t bit_depth) {
1063   int *arfgf_high_motion_minq;
1064   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
1065   return arfgf_high_motion_minq[q];
1066 }
1067 
calc_active_worst_quality_no_stats_vbr(const AV1_COMP * cpi)1068 static int calc_active_worst_quality_no_stats_vbr(const AV1_COMP *cpi) {
1069   const RATE_CONTROL *const rc = &cpi->rc;
1070   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1071   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1072   const unsigned int curr_frame = cpi->common.current_frame.frame_number;
1073   int active_worst_quality;
1074   int last_q_key_frame;
1075   int last_q_inter_frame;
1076 #if CONFIG_FPMT_TEST
1077   const int simulate_parallel_frame =
1078       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1079       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1080   last_q_key_frame = simulate_parallel_frame ? p_rc->temp_last_q[KEY_FRAME]
1081                                              : p_rc->last_q[KEY_FRAME];
1082   last_q_inter_frame = simulate_parallel_frame ? p_rc->temp_last_q[INTER_FRAME]
1083                                                : p_rc->last_q[INTER_FRAME];
1084 #else
1085   last_q_key_frame = p_rc->last_q[KEY_FRAME];
1086   last_q_inter_frame = p_rc->last_q[INTER_FRAME];
1087 #endif
1088 
1089   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
1090     active_worst_quality =
1091         curr_frame == 0 ? rc->worst_quality : last_q_key_frame * 2;
1092   } else {
1093     if (!rc->is_src_frame_alt_ref &&
1094         (refresh_frame->golden_frame || refresh_frame->bwd_ref_frame ||
1095          refresh_frame->alt_ref_frame)) {
1096       active_worst_quality =
1097           curr_frame == 1 ? last_q_key_frame * 5 / 4 : last_q_inter_frame;
1098     } else {
1099       active_worst_quality =
1100           curr_frame == 1 ? last_q_key_frame * 2 : last_q_inter_frame * 2;
1101     }
1102   }
1103   return AOMMIN(active_worst_quality, rc->worst_quality);
1104 }
1105 
1106 // Adjust active_worst_quality level based on buffer level.
calc_active_worst_quality_no_stats_cbr(const AV1_COMP * cpi)1107 static int calc_active_worst_quality_no_stats_cbr(const AV1_COMP *cpi) {
1108   // Adjust active_worst_quality: If buffer is above the optimal/target level,
1109   // bring active_worst_quality down depending on fullness of buffer.
1110   // If buffer is below the optimal level, let the active_worst_quality go from
1111   // ambient Q (at buffer = optimal level) to worst_quality level
1112   // (at buffer = critical level).
1113   const AV1_COMMON *const cm = &cpi->common;
1114   const RATE_CONTROL *rc = &cpi->rc;
1115   const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
1116   const SVC *const svc = &cpi->svc;
1117   unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
1118   // Buffer level below which we push active_worst to worst_quality.
1119   int64_t critical_level = p_rc->optimal_buffer_level >> 3;
1120   int64_t buff_lvl_step = 0;
1121   int adjustment = 0;
1122   int active_worst_quality;
1123   int ambient_qp;
1124   if (cm->current_frame.frame_type == KEY_FRAME) return rc->worst_quality;
1125   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
1126   // for the first few frames following key frame. These are both initialized
1127   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
1128   // So for first few frames following key, the qp of that key frame is weighted
1129   // into the active_worst_quality setting. For SVC the key frame should
1130   // correspond to layer (0, 0), so use that for layer context.
1131   int avg_qindex_key = p_rc->avg_frame_qindex[KEY_FRAME];
1132   if (svc->number_temporal_layers > 1) {
1133     int layer = LAYER_IDS_TO_IDX(0, 0, svc->number_temporal_layers);
1134     const LAYER_CONTEXT *lc = &svc->layer_context[layer];
1135     const PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
1136     avg_qindex_key =
1137         AOMMIN(lp_rc->avg_frame_qindex[KEY_FRAME], lp_rc->last_q[KEY_FRAME]);
1138   }
1139   ambient_qp = (cm->current_frame.frame_number < num_frames_weight_key)
1140                    ? AOMMIN(p_rc->avg_frame_qindex[INTER_FRAME], avg_qindex_key)
1141                    : p_rc->avg_frame_qindex[INTER_FRAME];
1142   ambient_qp = AOMMIN(rc->worst_quality, ambient_qp);
1143 
1144   if (p_rc->buffer_level > p_rc->optimal_buffer_level) {
1145     // Adjust down.
1146     int max_adjustment_down;  // Maximum adjustment down for Q
1147 
1148     if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && !cpi->ppi->use_svc &&
1149         (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN)) {
1150       active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp);
1151       max_adjustment_down = AOMMIN(4, active_worst_quality / 16);
1152     } else {
1153       active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4);
1154       max_adjustment_down = active_worst_quality / 3;
1155     }
1156 
1157     if (max_adjustment_down) {
1158       buff_lvl_step =
1159           ((p_rc->maximum_buffer_size - p_rc->optimal_buffer_level) /
1160            max_adjustment_down);
1161       if (buff_lvl_step)
1162         adjustment = (int)((p_rc->buffer_level - p_rc->optimal_buffer_level) /
1163                            buff_lvl_step);
1164       active_worst_quality -= adjustment;
1165     }
1166   } else if (p_rc->buffer_level > critical_level) {
1167     // Adjust up from ambient Q.
1168     active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp);
1169     if (critical_level) {
1170       buff_lvl_step = (p_rc->optimal_buffer_level - critical_level);
1171       if (buff_lvl_step) {
1172         adjustment = (int)((rc->worst_quality - ambient_qp) *
1173                            (p_rc->optimal_buffer_level - p_rc->buffer_level) /
1174                            buff_lvl_step);
1175       }
1176       active_worst_quality += adjustment;
1177     }
1178   } else {
1179     // Set to worst_quality if buffer is below critical level.
1180     active_worst_quality = rc->worst_quality;
1181   }
1182   return active_worst_quality;
1183 }
1184 
1185 // Calculate the active_best_quality level.
calc_active_best_quality_no_stats_cbr(const AV1_COMP * cpi,int active_worst_quality,int width,int height)1186 static int calc_active_best_quality_no_stats_cbr(const AV1_COMP *cpi,
1187                                                  int active_worst_quality,
1188                                                  int width, int height) {
1189   const AV1_COMMON *const cm = &cpi->common;
1190   const RATE_CONTROL *const rc = &cpi->rc;
1191   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1192   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1193   const CurrentFrame *const current_frame = &cm->current_frame;
1194   int *rtc_minq;
1195   const int bit_depth = cm->seq_params->bit_depth;
1196   int active_best_quality = rc->best_quality;
1197   ASSIGN_MINQ_TABLE(bit_depth, rtc_minq);
1198 
1199   if (frame_is_intra_only(cm)) {
1200     // Handle the special case for key frames forced when we have reached
1201     // the maximum key frame interval. Here force the Q to a range
1202     // based on the ambient Q to reduce the risk of popping.
1203     if (p_rc->this_key_frame_forced) {
1204       int qindex = p_rc->last_boosted_qindex;
1205       double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1206       int delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1207                                             (last_boosted_q * 0.75), bit_depth);
1208       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1209     } else if (current_frame->frame_number > 0) {
1210       // not first frame of one pass and kf_boost is set
1211       double q_adj_factor = 1.0;
1212       double q_val;
1213       active_best_quality = get_kf_active_quality(
1214           p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth);
1215       // Allow somewhat lower kf minq with small image formats.
1216       if ((width * height) <= (352 * 288)) {
1217         q_adj_factor -= 0.25;
1218       }
1219       // Convert the adjustment factor to a qindex delta
1220       // on active_best_quality.
1221       q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
1222       active_best_quality +=
1223           av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1224     }
1225   } else if (!rc->is_src_frame_alt_ref && !cpi->ppi->use_svc &&
1226              cpi->oxcf.rc_cfg.gf_cbr_boost_pct &&
1227              (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1228     // Use the lower of active_worst_quality and recent
1229     // average Q as basis for GF/ARF best Q limit unless last frame was
1230     // a key frame.
1231     int q = active_worst_quality;
1232     if (rc->frames_since_key > 1 &&
1233         p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1234       q = p_rc->avg_frame_qindex[INTER_FRAME];
1235     }
1236     active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1237   } else {
1238     // Use the lower of active_worst_quality and recent/average Q.
1239     FRAME_TYPE frame_type =
1240         (current_frame->frame_number > 1) ? INTER_FRAME : KEY_FRAME;
1241     if (p_rc->avg_frame_qindex[frame_type] < active_worst_quality)
1242       active_best_quality = rtc_minq[p_rc->avg_frame_qindex[frame_type]];
1243     else
1244       active_best_quality = rtc_minq[active_worst_quality];
1245   }
1246   return active_best_quality;
1247 }
1248 
1249 #if RT_PASSIVE_STRATEGY
get_q_passive_strategy(const AV1_COMP * const cpi,const int q_candidate,const int threshold)1250 static int get_q_passive_strategy(const AV1_COMP *const cpi,
1251                                   const int q_candidate, const int threshold) {
1252   const AV1_COMMON *const cm = &cpi->common;
1253   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1254   const CurrentFrame *const current_frame = &cm->current_frame;
1255   int sum = 0;
1256   int count = 0;
1257   int i = 1;
1258   while (i < MAX_Q_HISTORY) {
1259     int frame_id = current_frame->frame_number - i;
1260     if (frame_id <= 0) break;
1261     sum += p_rc->q_history[frame_id % MAX_Q_HISTORY];
1262     ++count;
1263     ++i;
1264   }
1265   if (count > 0) {
1266     const int avg_q = sum / count;
1267     if (abs(avg_q - q_candidate) <= threshold) return avg_q;
1268   }
1269   return q_candidate;
1270 }
1271 #endif  // RT_PASSIVE_STRATEGY
1272 
1273 /*!\brief Picks q and q bounds given CBR rate control parameters in \c cpi->rc.
1274  *
1275  * Handles the special case when using:
1276  * - Constant bit-rate mode: \c cpi->oxcf.rc_cfg.mode == \ref AOM_CBR, and
1277  * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are
1278  * NOT available.
1279  *
1280  * \ingroup rate_control
1281  * \param[in]       cpi          Top level encoder structure
1282  * \param[in]       width        Coded frame width
1283  * \param[in]       height       Coded frame height
1284  * \param[out]      bottom_index Bottom bound for q index (best quality)
1285  * \param[out]      top_index    Top bound for q index (worst quality)
1286  * \return Returns selected q index to be used for encoding this frame.
1287  */
rc_pick_q_and_bounds_no_stats_cbr(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1288 static int rc_pick_q_and_bounds_no_stats_cbr(const AV1_COMP *cpi, int width,
1289                                              int height, int *bottom_index,
1290                                              int *top_index) {
1291   const AV1_COMMON *const cm = &cpi->common;
1292   const RATE_CONTROL *const rc = &cpi->rc;
1293   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1294   const CurrentFrame *const current_frame = &cm->current_frame;
1295   int q;
1296   int active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi);
1297   int active_best_quality = calc_active_best_quality_no_stats_cbr(
1298       cpi, active_worst_quality, width, height);
1299   assert(has_no_stats_stage(cpi));
1300   assert(cpi->oxcf.rc_cfg.mode == AOM_CBR);
1301 
1302   // Clip the active best and worst quality values to limits
1303   active_best_quality =
1304       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1305   active_worst_quality =
1306       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1307 
1308   *top_index = active_worst_quality;
1309   *bottom_index = active_best_quality;
1310 
1311   // Limit Q range for the adaptive loop.
1312   if (current_frame->frame_type == KEY_FRAME && !p_rc->this_key_frame_forced &&
1313       current_frame->frame_number != 0) {
1314     int qdelta = 0;
1315     qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1316                                         active_worst_quality, 2.0);
1317     *top_index = active_worst_quality + qdelta;
1318     *top_index = AOMMAX(*top_index, *bottom_index);
1319   }
1320 
1321   q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1322                         active_worst_quality, width, height);
1323 #if RT_PASSIVE_STRATEGY
1324   if (current_frame->frame_type != KEY_FRAME &&
1325       cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1326     q = get_q_passive_strategy(cpi, q, 50);
1327   }
1328 #endif  // RT_PASSIVE_STRATEGY
1329   if (q > *top_index) {
1330     // Special case when we are targeting the max allowed rate
1331     if (rc->this_frame_target >= rc->max_frame_bandwidth)
1332       *top_index = q;
1333     else
1334       q = *top_index;
1335   }
1336 
1337   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1338   assert(*bottom_index <= rc->worst_quality &&
1339          *bottom_index >= rc->best_quality);
1340   assert(q <= rc->worst_quality && q >= rc->best_quality);
1341   return q;
1342 }
1343 
gf_group_pyramid_level(const GF_GROUP * gf_group,int gf_index)1344 static int gf_group_pyramid_level(const GF_GROUP *gf_group, int gf_index) {
1345   return gf_group->layer_depth[gf_index];
1346 }
1347 
get_active_cq_level(const RATE_CONTROL * rc,const PRIMARY_RATE_CONTROL * p_rc,const AV1EncoderConfig * const oxcf,int intra_only,aom_superres_mode superres_mode,int superres_denom)1348 static int get_active_cq_level(const RATE_CONTROL *rc,
1349                                const PRIMARY_RATE_CONTROL *p_rc,
1350                                const AV1EncoderConfig *const oxcf,
1351                                int intra_only, aom_superres_mode superres_mode,
1352                                int superres_denom) {
1353   const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
1354   static const double cq_adjust_threshold = 0.1;
1355   int active_cq_level = rc_cfg->cq_level;
1356   if (rc_cfg->mode == AOM_CQ || rc_cfg->mode == AOM_Q) {
1357     // printf("Superres %d %d %d = %d\n", superres_denom, intra_only,
1358     //        rc->frames_to_key, !(intra_only && rc->frames_to_key <= 1));
1359     if ((superres_mode == AOM_SUPERRES_QTHRESH ||
1360          superres_mode == AOM_SUPERRES_AUTO) &&
1361         superres_denom != SCALE_NUMERATOR) {
1362       int mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO;
1363       if (intra_only && rc->frames_to_key <= 1) {
1364         mult = 0;
1365       } else if (intra_only) {
1366         mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME;
1367       } else {
1368         mult = SUPERRES_QADJ_PER_DENOM_ARFFRAME;
1369       }
1370       active_cq_level = AOMMAX(
1371           active_cq_level - ((superres_denom - SCALE_NUMERATOR) * mult), 0);
1372     }
1373   }
1374   if (rc_cfg->mode == AOM_CQ && p_rc->total_target_bits > 0) {
1375     const double x = (double)p_rc->total_actual_bits / p_rc->total_target_bits;
1376     if (x < cq_adjust_threshold) {
1377       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
1378     }
1379   }
1380   return active_cq_level;
1381 }
1382 
1383 /*!\brief Picks q and q bounds given non-CBR rate control params in \c cpi->rc.
1384  *
1385  * Handles the special case when using:
1386  * - Any rate control other than constant bit-rate mode:
1387  * \c cpi->oxcf.rc_cfg.mode != \ref AOM_CBR, and
1388  * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are
1389  * NOT available.
1390  *
1391  * \ingroup rate_control
1392  * \param[in]       cpi          Top level encoder structure
1393  * \param[in]       width        Coded frame width
1394  * \param[in]       height       Coded frame height
1395  * \param[out]      bottom_index Bottom bound for q index (best quality)
1396  * \param[out]      top_index    Top bound for q index (worst quality)
1397  * \return Returns selected q index to be used for encoding this frame.
1398  */
rc_pick_q_and_bounds_no_stats(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1399 static int rc_pick_q_and_bounds_no_stats(const AV1_COMP *cpi, int width,
1400                                          int height, int *bottom_index,
1401                                          int *top_index) {
1402   const AV1_COMMON *const cm = &cpi->common;
1403   const RATE_CONTROL *const rc = &cpi->rc;
1404   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1405   const CurrentFrame *const current_frame = &cm->current_frame;
1406   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1407   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1408   const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode;
1409 
1410   assert(has_no_stats_stage(cpi));
1411   assert(rc_mode == AOM_VBR ||
1412          (!USE_UNRESTRICTED_Q_IN_CQ_MODE && rc_mode == AOM_CQ) ||
1413          rc_mode == AOM_Q);
1414 
1415   const int cq_level =
1416       get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
1417                           cpi->superres_mode, cm->superres_scale_denominator);
1418   const int bit_depth = cm->seq_params->bit_depth;
1419 
1420   int active_best_quality;
1421   int active_worst_quality = calc_active_worst_quality_no_stats_vbr(cpi);
1422   int q;
1423   int *inter_minq;
1424   ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1425 
1426   if (frame_is_intra_only(cm)) {
1427     if (rc_mode == AOM_Q) {
1428       const int qindex = cq_level;
1429       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1430       const int delta_qindex =
1431           av1_compute_qdelta(rc, q_val, q_val * 0.25, bit_depth);
1432       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1433     } else if (p_rc->this_key_frame_forced) {
1434 #if CONFIG_FPMT_TEST
1435       const int simulate_parallel_frame =
1436           cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1437           cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1438       int qindex = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex
1439                                            : p_rc->last_boosted_qindex;
1440 #else
1441       int qindex = p_rc->last_boosted_qindex;
1442 #endif
1443       const double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1444       const int delta_qindex = av1_compute_qdelta(
1445           rc, last_boosted_q, last_boosted_q * 0.75, bit_depth);
1446       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1447     } else {  // not first frame of one pass and kf_boost is set
1448       double q_adj_factor = 1.0;
1449 
1450       active_best_quality = get_kf_active_quality(
1451           p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth);
1452 
1453       // Allow somewhat lower kf minq with small image formats.
1454       if ((width * height) <= (352 * 288)) {
1455         q_adj_factor -= 0.25;
1456       }
1457 
1458       // Convert the adjustment factor to a qindex delta on active_best_quality.
1459       {
1460         const double q_val =
1461             av1_convert_qindex_to_q(active_best_quality, bit_depth);
1462         active_best_quality +=
1463             av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1464       }
1465     }
1466   } else if (!rc->is_src_frame_alt_ref &&
1467              (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1468     // Use the lower of active_worst_quality and recent
1469     // average Q as basis for GF/ARF best Q limit unless last frame was
1470     // a key frame.
1471     q = (rc->frames_since_key > 1 &&
1472          p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
1473             ? p_rc->avg_frame_qindex[INTER_FRAME]
1474             : p_rc->avg_frame_qindex[KEY_FRAME];
1475     // For constrained quality dont allow Q less than the cq level
1476     if (rc_mode == AOM_CQ) {
1477       if (q < cq_level) q = cq_level;
1478       active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1479       // Constrained quality use slightly lower active best.
1480       active_best_quality = active_best_quality * 15 / 16;
1481     } else if (rc_mode == AOM_Q) {
1482       const int qindex = cq_level;
1483       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1484       const int delta_qindex =
1485           (refresh_frame->alt_ref_frame)
1486               ? av1_compute_qdelta(rc, q_val, q_val * 0.40, bit_depth)
1487               : av1_compute_qdelta(rc, q_val, q_val * 0.50, bit_depth);
1488       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1489     } else {
1490       active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1491     }
1492   } else {
1493     if (rc_mode == AOM_Q) {
1494       const int qindex = cq_level;
1495       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1496       const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
1497                                                      0.70, 1.0, 0.85, 1.0 };
1498       const int delta_qindex = av1_compute_qdelta(
1499           rc, q_val,
1500           q_val * delta_rate[current_frame->frame_number % FIXED_GF_INTERVAL],
1501           bit_depth);
1502       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1503     } else {
1504       // Use the lower of active_worst_quality and recent/average Q.
1505       active_best_quality =
1506           (current_frame->frame_number > 1)
1507               ? inter_minq[p_rc->avg_frame_qindex[INTER_FRAME]]
1508               : inter_minq[p_rc->avg_frame_qindex[KEY_FRAME]];
1509       // For the constrained quality mode we don't want
1510       // q to fall below the cq level.
1511       if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1512         active_best_quality = cq_level;
1513       }
1514     }
1515   }
1516 
1517   // Clip the active best and worst quality values to limits
1518   active_best_quality =
1519       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1520   active_worst_quality =
1521       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1522 
1523   *top_index = active_worst_quality;
1524   *bottom_index = active_best_quality;
1525 
1526   // Limit Q range for the adaptive loop.
1527   {
1528     int qdelta = 0;
1529     if (current_frame->frame_type == KEY_FRAME &&
1530         !p_rc->this_key_frame_forced && current_frame->frame_number != 0) {
1531       qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1532                                           active_worst_quality, 2.0);
1533     } else if (!rc->is_src_frame_alt_ref &&
1534                (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) {
1535       qdelta = av1_compute_qdelta_by_rate(cpi, current_frame->frame_type,
1536                                           active_worst_quality, 1.75);
1537     }
1538     *top_index = active_worst_quality + qdelta;
1539     *top_index = AOMMAX(*top_index, *bottom_index);
1540   }
1541 
1542   if (rc_mode == AOM_Q) {
1543     q = active_best_quality;
1544     // Special case code to try and match quality with forced key frames
1545   } else if ((current_frame->frame_type == KEY_FRAME) &&
1546              p_rc->this_key_frame_forced) {
1547 #if CONFIG_FPMT_TEST
1548     const int simulate_parallel_frame =
1549         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1550         cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1551     q = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex
1552                                 : p_rc->last_boosted_qindex;
1553 #else
1554     q = p_rc->last_boosted_qindex;
1555 #endif
1556   } else {
1557     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1558                           active_worst_quality, width, height);
1559     if (q > *top_index) {
1560       // Special case when we are targeting the max allowed rate
1561       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1562         *top_index = q;
1563       else
1564         q = *top_index;
1565     }
1566   }
1567 
1568   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1569   assert(*bottom_index <= rc->worst_quality &&
1570          *bottom_index >= rc->best_quality);
1571   assert(q <= rc->worst_quality && q >= rc->best_quality);
1572   return q;
1573 }
1574 
1575 static const double arf_layer_deltas[MAX_ARF_LAYERS + 1] = { 2.50, 2.00, 1.75,
1576                                                              1.50, 1.25, 1.15,
1577                                                              1.0 };
av1_frame_type_qdelta(const AV1_COMP * cpi,int q)1578 int av1_frame_type_qdelta(const AV1_COMP *cpi, int q) {
1579   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
1580   const RATE_FACTOR_LEVEL rf_lvl =
1581       get_rate_factor_level(gf_group, cpi->gf_frame_index);
1582   const FRAME_TYPE frame_type = gf_group->frame_type[cpi->gf_frame_index];
1583   const int arf_layer = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6);
1584   const double rate_factor =
1585       (rf_lvl == INTER_NORMAL) ? 1.0 : arf_layer_deltas[arf_layer];
1586 
1587   return av1_compute_qdelta_by_rate(cpi, frame_type, q, rate_factor);
1588 }
1589 
1590 // This unrestricted Q selection on CQ mode is useful when testing new features,
1591 // but may lead to Q being out of range on current RC restrictions
1592 #if USE_UNRESTRICTED_Q_IN_CQ_MODE
rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1593 static int rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP *cpi, int width,
1594                                             int height, int *bottom_index,
1595                                             int *top_index) {
1596   const AV1_COMMON *const cm = &cpi->common;
1597   const RATE_CONTROL *const rc = &cpi->rc;
1598   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1599   const int cq_level =
1600       get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode,
1601                           cm->superres_scale_denominator);
1602   const int bit_depth = cm->seq_params->bit_depth;
1603   const int q = (int)av1_convert_qindex_to_q(cq_level, bit_depth);
1604   (void)width;
1605   (void)height;
1606   assert(has_no_stats_stage(cpi));
1607   assert(cpi->oxcf.rc_cfg.mode == AOM_CQ);
1608 
1609   *top_index = q;
1610   *bottom_index = q;
1611 
1612   return q;
1613 }
1614 #endif  // USE_UNRESTRICTED_Q_IN_CQ_MODE
1615 
1616 #define STATIC_MOTION_THRESH 95
get_intra_q_and_bounds(const AV1_COMP * cpi,int width,int height,int * active_best,int * active_worst,int cq_level)1617 static void get_intra_q_and_bounds(const AV1_COMP *cpi, int width, int height,
1618                                    int *active_best, int *active_worst,
1619                                    int cq_level) {
1620   const AV1_COMMON *const cm = &cpi->common;
1621   const RATE_CONTROL *const rc = &cpi->rc;
1622   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1623   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1624   int active_best_quality;
1625   int active_worst_quality = *active_worst;
1626   const int bit_depth = cm->seq_params->bit_depth;
1627 
1628   if (rc->frames_to_key <= 1 && oxcf->rc_cfg.mode == AOM_Q) {
1629     // If the next frame is also a key frame or the current frame is the
1630     // only frame in the sequence in AOM_Q mode, just use the cq_level
1631     // as q.
1632     active_best_quality = cq_level;
1633     active_worst_quality = cq_level;
1634   } else if (p_rc->this_key_frame_forced) {
1635     // Handle the special case for key frames forced when we have reached
1636     // the maximum key frame interval. Here force the Q to a range
1637     // based on the ambient Q to reduce the risk of popping.
1638     double last_boosted_q;
1639     int delta_qindex;
1640     int qindex;
1641 #if CONFIG_FPMT_TEST
1642     const int simulate_parallel_frame =
1643         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1644         cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1645     int last_boosted_qindex = simulate_parallel_frame
1646                                   ? p_rc->temp_last_boosted_qindex
1647                                   : p_rc->last_boosted_qindex;
1648 #else
1649     int last_boosted_qindex = p_rc->last_boosted_qindex;
1650 #endif
1651     if (is_stat_consumption_stage_twopass(cpi) &&
1652         cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1653       qindex = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex);
1654       active_best_quality = qindex;
1655       last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1656       delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1657                                         last_boosted_q * 1.25, bit_depth);
1658       active_worst_quality =
1659           AOMMIN(qindex + delta_qindex, active_worst_quality);
1660     } else {
1661       qindex = last_boosted_qindex;
1662       last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1663       delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1664                                         last_boosted_q * 0.50, bit_depth);
1665       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1666     }
1667   } else {
1668     // Not forced keyframe.
1669     double q_adj_factor = 1.0;
1670     double q_val;
1671 
1672     // Baseline value derived from active_worst_quality and kf boost.
1673     active_best_quality =
1674         get_kf_active_quality(p_rc, active_worst_quality, bit_depth);
1675     if (cpi->is_screen_content_type) {
1676       active_best_quality /= 2;
1677     }
1678 
1679     if (is_stat_consumption_stage_twopass(cpi) &&
1680         cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1681       active_best_quality /= 3;
1682     }
1683 
1684     // Allow somewhat lower kf minq with small image formats.
1685     if ((width * height) <= (352 * 288)) {
1686       q_adj_factor -= 0.25;
1687     }
1688 
1689     // Make a further adjustment based on the kf zero motion measure.
1690     if (is_stat_consumption_stage_twopass(cpi))
1691       q_adj_factor +=
1692           0.05 - (0.001 * (double)cpi->ppi->twopass.kf_zeromotion_pct);
1693 
1694     // Convert the adjustment factor to a qindex delta
1695     // on active_best_quality.
1696     q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
1697     active_best_quality +=
1698         av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1699 
1700     // Tweak active_best_quality for AOM_Q mode when superres is on, as this
1701     // will be used directly as 'q' later.
1702     if (oxcf->rc_cfg.mode == AOM_Q &&
1703         (cpi->superres_mode == AOM_SUPERRES_QTHRESH ||
1704          cpi->superres_mode == AOM_SUPERRES_AUTO) &&
1705         cm->superres_scale_denominator != SCALE_NUMERATOR) {
1706       active_best_quality =
1707           AOMMAX(active_best_quality -
1708                      ((cm->superres_scale_denominator - SCALE_NUMERATOR) *
1709                       SUPERRES_QADJ_PER_DENOM_KEYFRAME),
1710                  0);
1711     }
1712   }
1713   *active_best = active_best_quality;
1714   *active_worst = active_worst_quality;
1715 }
1716 
adjust_active_best_and_worst_quality(const AV1_COMP * cpi,const int is_intrl_arf_boost,int * active_worst,int * active_best)1717 static void adjust_active_best_and_worst_quality(const AV1_COMP *cpi,
1718                                                  const int is_intrl_arf_boost,
1719                                                  int *active_worst,
1720                                                  int *active_best) {
1721   const AV1_COMMON *const cm = &cpi->common;
1722   const RATE_CONTROL *const rc = &cpi->rc;
1723   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1724   int active_best_quality = *active_best;
1725   int active_worst_quality = *active_worst;
1726 #if CONFIG_FPMT_TEST
1727 #endif
1728   // Extension to max or min Q if undershoot or overshoot is outside
1729   // the permitted range.
1730   if (cpi->oxcf.rc_cfg.mode != AOM_Q) {
1731 #if CONFIG_FPMT_TEST
1732     const int simulate_parallel_frame =
1733         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1734         cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
1735     const int extend_minq = simulate_parallel_frame
1736                                 ? p_rc->temp_extend_minq
1737                                 : cpi->ppi->twopass.extend_minq;
1738     const int extend_maxq = simulate_parallel_frame
1739                                 ? p_rc->temp_extend_maxq
1740                                 : cpi->ppi->twopass.extend_maxq;
1741     const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1742     if (frame_is_intra_only(cm) ||
1743         (!rc->is_src_frame_alt_ref &&
1744          (refresh_frame->golden_frame || is_intrl_arf_boost ||
1745           refresh_frame->alt_ref_frame))) {
1746       active_best_quality -= extend_minq;
1747       active_worst_quality += (extend_maxq / 2);
1748     } else {
1749       active_best_quality -= extend_minq / 2;
1750       active_worst_quality += extend_maxq;
1751     }
1752 #else
1753     (void)is_intrl_arf_boost;
1754     active_best_quality -= cpi->ppi->twopass.extend_minq / 8;
1755     active_worst_quality += cpi->ppi->twopass.extend_maxq / 4;
1756 #endif
1757   }
1758 
1759 #ifndef STRICT_RC
1760   // Static forced key frames Q restrictions dealt with elsewhere.
1761   if (!(frame_is_intra_only(cm)) || !p_rc->this_key_frame_forced ||
1762       (cpi->ppi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1763     const int qdelta = av1_frame_type_qdelta(cpi, active_worst_quality);
1764     active_worst_quality =
1765         AOMMAX(active_worst_quality + qdelta, active_best_quality);
1766   }
1767 #endif
1768 
1769   // Modify active_best_quality for downscaled normal frames.
1770   if (av1_frame_scaled(cm) && !frame_is_kf_gf_arf(cpi)) {
1771     int qdelta = av1_compute_qdelta_by_rate(cpi, cm->current_frame.frame_type,
1772                                             active_best_quality, 2.0);
1773     active_best_quality =
1774         AOMMAX(active_best_quality + qdelta, rc->best_quality);
1775   }
1776 
1777   active_best_quality =
1778       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1779   active_worst_quality =
1780       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1781 
1782   *active_best = active_best_quality;
1783   *active_worst = active_worst_quality;
1784 }
1785 
1786 /*!\brief Gets a Q value to use  for the current frame
1787  *
1788  *
1789  * Selects a Q value from a permitted range that we estimate
1790  * will result in approximately the target number of bits.
1791  *
1792  * \ingroup rate_control
1793  * \param[in]   cpi                   Top level encoder instance structure
1794  * \param[in]   width                 Width of frame
1795  * \param[in]   height                Height of frame
1796  * \param[in]   active_worst_quality  Max Q allowed
1797  * \param[in]   active_best_quality   Min Q allowed
1798  *
1799  * \return The suggested Q for this frame.
1800  */
get_q(const AV1_COMP * cpi,const int width,const int height,const int active_worst_quality,const int active_best_quality)1801 static int get_q(const AV1_COMP *cpi, const int width, const int height,
1802                  const int active_worst_quality,
1803                  const int active_best_quality) {
1804   const AV1_COMMON *const cm = &cpi->common;
1805   const RATE_CONTROL *const rc = &cpi->rc;
1806   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1807   int q;
1808 #if CONFIG_FPMT_TEST
1809   const int simulate_parallel_frame =
1810       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
1811       cpi->ppi->fpmt_unit_test_cfg;
1812   int last_boosted_qindex = simulate_parallel_frame
1813                                 ? p_rc->temp_last_boosted_qindex
1814                                 : p_rc->last_boosted_qindex;
1815 #else
1816   int last_boosted_qindex = p_rc->last_boosted_qindex;
1817 #endif
1818 
1819   if (cpi->oxcf.rc_cfg.mode == AOM_Q ||
1820       (frame_is_intra_only(cm) && !p_rc->this_key_frame_forced &&
1821        cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH &&
1822        rc->frames_to_key > 1)) {
1823     q = active_best_quality;
1824     // Special case code to try and match quality with forced key frames.
1825   } else if (frame_is_intra_only(cm) && p_rc->this_key_frame_forced) {
1826     // If static since last kf use better of last boosted and last kf q.
1827     if (cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1828       q = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex);
1829     } else {
1830       q = AOMMIN(last_boosted_qindex,
1831                  (active_best_quality + active_worst_quality) / 2);
1832     }
1833     q = clamp(q, active_best_quality, active_worst_quality);
1834   } else {
1835     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1836                           active_worst_quality, width, height);
1837     if (q > active_worst_quality) {
1838       // Special case when we are targeting the max allowed rate.
1839       if (rc->this_frame_target < rc->max_frame_bandwidth) {
1840         q = active_worst_quality;
1841       }
1842     }
1843     q = AOMMAX(q, active_best_quality);
1844   }
1845   return q;
1846 }
1847 
1848 // Returns |active_best_quality| for an inter frame.
1849 // The |active_best_quality| depends on different rate control modes:
1850 // VBR, Q, CQ, CBR.
1851 // The returning active_best_quality could further be adjusted in
1852 // adjust_active_best_and_worst_quality().
get_active_best_quality(const AV1_COMP * const cpi,const int active_worst_quality,const int cq_level,const int gf_index)1853 static int get_active_best_quality(const AV1_COMP *const cpi,
1854                                    const int active_worst_quality,
1855                                    const int cq_level, const int gf_index) {
1856   const AV1_COMMON *const cm = &cpi->common;
1857   const int bit_depth = cm->seq_params->bit_depth;
1858   const RATE_CONTROL *const rc = &cpi->rc;
1859   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1860   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1861   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
1862   const GF_GROUP *gf_group = &cpi->ppi->gf_group;
1863   const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode;
1864   int *inter_minq;
1865   ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1866   int active_best_quality = 0;
1867   const int is_intrl_arf_boost =
1868       gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
1869   int is_leaf_frame =
1870       !(gf_group->update_type[gf_index] == ARF_UPDATE ||
1871         gf_group->update_type[gf_index] == GF_UPDATE || is_intrl_arf_boost);
1872 
1873   // TODO(jingning): Consider to rework this hack that covers issues incurred
1874   // in lightfield setting.
1875   if (cm->tiles.large_scale) {
1876     is_leaf_frame = !(refresh_frame->golden_frame ||
1877                       refresh_frame->alt_ref_frame || is_intrl_arf_boost);
1878   }
1879   const int is_overlay_frame = rc->is_src_frame_alt_ref;
1880 
1881   if (is_leaf_frame || is_overlay_frame) {
1882     if (rc_mode == AOM_Q) return cq_level;
1883 
1884     active_best_quality = inter_minq[active_worst_quality];
1885     // For the constrained quality mode we don't want
1886     // q to fall below the cq level.
1887     if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1888       active_best_quality = cq_level;
1889     }
1890     return active_best_quality;
1891   }
1892 
1893   // Determine active_best_quality for frames that are not leaf or overlay.
1894   int q = active_worst_quality;
1895   // Use the lower of active_worst_quality and recent
1896   // average Q as basis for GF/ARF best Q limit unless last frame was
1897   // a key frame.
1898   if (rc->frames_since_key > 1 &&
1899       p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1900     q = p_rc->avg_frame_qindex[INTER_FRAME];
1901   }
1902   if (rc_mode == AOM_CQ && q < cq_level) q = cq_level;
1903   active_best_quality = get_gf_active_quality(p_rc, q, bit_depth);
1904   // Constrained quality use slightly lower active best.
1905   if (rc_mode == AOM_CQ) active_best_quality = active_best_quality * 15 / 16;
1906   const int min_boost = get_gf_high_motion_quality(q, bit_depth);
1907   const int boost = min_boost - active_best_quality;
1908   active_best_quality = min_boost - (int)(boost * p_rc->arf_boost_factor);
1909   if (!is_intrl_arf_boost) return active_best_quality;
1910 
1911   if (rc_mode == AOM_Q || rc_mode == AOM_CQ) active_best_quality = p_rc->arf_q;
1912   int this_height = gf_group_pyramid_level(gf_group, gf_index);
1913   while (this_height > 1) {
1914     active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
1915     --this_height;
1916   }
1917   return active_best_quality;
1918 }
1919 
1920 // Returns the q_index for a single frame in the GOP.
1921 // This function assumes that rc_mode == AOM_Q mode.
av1_q_mode_get_q_index(int base_q_index,int gf_update_type,int gf_pyramid_level,int arf_q)1922 int av1_q_mode_get_q_index(int base_q_index, int gf_update_type,
1923                            int gf_pyramid_level, int arf_q) {
1924   const int is_intrl_arf_boost = gf_update_type == INTNL_ARF_UPDATE;
1925   int is_leaf_or_overlay_frame = gf_update_type == LF_UPDATE ||
1926                                  gf_update_type == OVERLAY_UPDATE ||
1927                                  gf_update_type == INTNL_OVERLAY_UPDATE;
1928 
1929   if (is_leaf_or_overlay_frame) return base_q_index;
1930 
1931   if (!is_intrl_arf_boost) return arf_q;
1932 
1933   int active_best_quality = arf_q;
1934   int active_worst_quality = base_q_index;
1935 
1936   while (gf_pyramid_level > 1) {
1937     active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
1938     --gf_pyramid_level;
1939   }
1940   return active_best_quality;
1941 }
1942 
1943 // Returns the q_index for the ARF in the GOP.
av1_get_arf_q_index(int base_q_index,int gfu_boost,int bit_depth,double arf_boost_factor)1944 int av1_get_arf_q_index(int base_q_index, int gfu_boost, int bit_depth,
1945                         double arf_boost_factor) {
1946   int active_best_quality =
1947       get_gf_active_quality_no_rc(gfu_boost, base_q_index, bit_depth);
1948   const int min_boost = get_gf_high_motion_quality(base_q_index, bit_depth);
1949   const int boost = min_boost - active_best_quality;
1950   return min_boost - (int)(boost * arf_boost_factor);
1951 }
1952 
rc_pick_q_and_bounds_q_mode(const AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)1953 static int rc_pick_q_and_bounds_q_mode(const AV1_COMP *cpi, int width,
1954                                        int height, int gf_index,
1955                                        int *bottom_index, int *top_index) {
1956   const AV1_COMMON *const cm = &cpi->common;
1957   const RATE_CONTROL *const rc = &cpi->rc;
1958   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
1959   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1960   const int cq_level =
1961       get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
1962                           cpi->superres_mode, cm->superres_scale_denominator);
1963   int active_best_quality = 0;
1964   int active_worst_quality = rc->active_worst_quality;
1965   int q;
1966 
1967   if (frame_is_intra_only(cm)) {
1968     get_intra_q_and_bounds(cpi, width, height, &active_best_quality,
1969                            &active_worst_quality, cq_level);
1970   } else {
1971     //  Active best quality limited by previous layer.
1972     active_best_quality =
1973         get_active_best_quality(cpi, active_worst_quality, cq_level, gf_index);
1974   }
1975 
1976   if (cq_level > 0) active_best_quality = AOMMAX(1, active_best_quality);
1977 
1978   *top_index = active_worst_quality;
1979   *bottom_index = active_best_quality;
1980 
1981   *top_index = AOMMAX(*top_index, rc->best_quality);
1982   *top_index = AOMMIN(*top_index, rc->worst_quality);
1983 
1984   *bottom_index = AOMMAX(*bottom_index, rc->best_quality);
1985   *bottom_index = AOMMIN(*bottom_index, rc->worst_quality);
1986 
1987   q = active_best_quality;
1988 
1989   q = AOMMAX(q, rc->best_quality);
1990   q = AOMMIN(q, rc->worst_quality);
1991 
1992   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1993   assert(*bottom_index <= rc->worst_quality &&
1994          *bottom_index >= rc->best_quality);
1995   assert(q <= rc->worst_quality && q >= rc->best_quality);
1996 
1997   return q;
1998 }
1999 
2000 /*!\brief Picks q and q bounds given rate control parameters in \c cpi->rc.
2001  *
2002  * Handles the the general cases not covered by
2003  * \ref rc_pick_q_and_bounds_no_stats_cbr() and
2004  * \ref rc_pick_q_and_bounds_no_stats()
2005  *
2006  * \ingroup rate_control
2007  * \param[in]       cpi          Top level encoder structure
2008  * \param[in]       width        Coded frame width
2009  * \param[in]       height       Coded frame height
2010  * \param[in]       gf_index     Index of this frame in the golden frame group
2011  * \param[out]      bottom_index Bottom bound for q index (best quality)
2012  * \param[out]      top_index    Top bound for q index (worst quality)
2013  * \return Returns selected q index to be used for encoding this frame.
2014  */
rc_pick_q_and_bounds(const AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)2015 static int rc_pick_q_and_bounds(const AV1_COMP *cpi, int width, int height,
2016                                 int gf_index, int *bottom_index,
2017                                 int *top_index) {
2018   const AV1_COMMON *const cm = &cpi->common;
2019   const RATE_CONTROL *const rc = &cpi->rc;
2020   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2021   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2022   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
2023   const GF_GROUP *gf_group = &cpi->ppi->gf_group;
2024   assert(IMPLIES(has_no_stats_stage(cpi),
2025                  cpi->oxcf.rc_cfg.mode == AOM_Q &&
2026                      gf_group->update_type[gf_index] != ARF_UPDATE));
2027   const int cq_level =
2028       get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm),
2029                           cpi->superres_mode, cm->superres_scale_denominator);
2030 
2031   if (oxcf->rc_cfg.mode == AOM_Q) {
2032     return rc_pick_q_and_bounds_q_mode(cpi, width, height, gf_index,
2033                                        bottom_index, top_index);
2034   }
2035 
2036   int active_best_quality = 0;
2037   int active_worst_quality = rc->active_worst_quality;
2038   int q;
2039 
2040   const int is_intrl_arf_boost =
2041       gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
2042 
2043   if (frame_is_intra_only(cm)) {
2044     get_intra_q_and_bounds(cpi, width, height, &active_best_quality,
2045                            &active_worst_quality, cq_level);
2046 #ifdef STRICT_RC
2047     active_best_quality = 0;
2048 #endif
2049   } else {
2050     //  Active best quality limited by previous layer.
2051     const int pyramid_level = gf_group_pyramid_level(gf_group, gf_index);
2052 
2053     if ((pyramid_level <= 1) || (pyramid_level > MAX_ARF_LAYERS)) {
2054       active_best_quality = get_active_best_quality(cpi, active_worst_quality,
2055                                                     cq_level, gf_index);
2056     } else {
2057 #if CONFIG_FPMT_TEST
2058       const int simulate_parallel_frame =
2059           cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
2060           cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
2061       int local_active_best_quality =
2062           simulate_parallel_frame
2063               ? p_rc->temp_active_best_quality[pyramid_level - 1]
2064               : p_rc->active_best_quality[pyramid_level - 1];
2065       active_best_quality = local_active_best_quality + 1;
2066 #else
2067       active_best_quality = p_rc->active_best_quality[pyramid_level - 1] + 1;
2068 #endif
2069 
2070       active_best_quality = AOMMIN(active_best_quality, active_worst_quality);
2071 #ifdef STRICT_RC
2072       active_best_quality += (active_worst_quality - active_best_quality) / 16;
2073 #else
2074       active_best_quality += (active_worst_quality - active_best_quality) / 2;
2075 #endif
2076     }
2077 
2078     // For alt_ref and GF frames (including internal arf frames) adjust the
2079     // worst allowed quality as well. This insures that even on hard
2080     // sections we dont clamp the Q at the same value for arf frames and
2081     // leaf (non arf) frames. This is important to the TPL model which assumes
2082     // Q drops with each arf level.
2083     if (!(rc->is_src_frame_alt_ref) &&
2084         (refresh_frame->golden_frame || refresh_frame->alt_ref_frame ||
2085          is_intrl_arf_boost)) {
2086       active_worst_quality =
2087           (active_best_quality + (3 * active_worst_quality) + 2) / 4;
2088     }
2089   }
2090 
2091   adjust_active_best_and_worst_quality(
2092       cpi, is_intrl_arf_boost, &active_worst_quality, &active_best_quality);
2093   q = get_q(cpi, width, height, active_worst_quality, active_best_quality);
2094 
2095   // Special case when we are targeting the max allowed rate.
2096   if (rc->this_frame_target >= rc->max_frame_bandwidth &&
2097       q > active_worst_quality) {
2098     active_worst_quality = q;
2099   }
2100 
2101   *top_index = active_worst_quality;
2102   *bottom_index = active_best_quality;
2103 
2104   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
2105   assert(*bottom_index <= rc->worst_quality &&
2106          *bottom_index >= rc->best_quality);
2107   assert(q <= rc->worst_quality && q >= rc->best_quality);
2108 
2109   return q;
2110 }
2111 
rc_compute_variance_onepass_rt(AV1_COMP * cpi)2112 static void rc_compute_variance_onepass_rt(AV1_COMP *cpi) {
2113   AV1_COMMON *const cm = &cpi->common;
2114   YV12_BUFFER_CONFIG const *const unscaled_src = cpi->unscaled_source;
2115   if (unscaled_src == NULL) return;
2116 
2117   const uint8_t *src_y = unscaled_src->y_buffer;
2118   const int src_ystride = unscaled_src->y_stride;
2119   const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
2120   const uint8_t *pre_y = yv12->buffers[0];
2121   const int pre_ystride = yv12->strides[0];
2122 
2123   // TODO(yunqing): support scaled reference frames.
2124   if (cpi->scaled_ref_buf[LAST_FRAME - 1]) return;
2125 
2126   for (int i = 0; i < 2; ++i) {
2127     if (unscaled_src->widths[i] != yv12->widths[i] ||
2128         unscaled_src->heights[i] != yv12->heights[i]) {
2129       return;
2130     }
2131   }
2132 
2133   const int num_mi_cols = cm->mi_params.mi_cols;
2134   const int num_mi_rows = cm->mi_params.mi_rows;
2135   const BLOCK_SIZE bsize = BLOCK_64X64;
2136   int num_samples = 0;
2137   // sse is computed on 64x64 blocks
2138   const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
2139                                 ? (cm->seq_params->mib_size >> 1)
2140                                 : cm->seq_params->mib_size;
2141   const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
2142   const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb;
2143 
2144   uint64_t fsse = 0;
2145   cpi->rec_sse = 0;
2146 
2147   for (int sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
2148     for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
2149       unsigned int sse;
2150       uint8_t src[64 * 64] = { 0 };
2151       // Apply 4x4 block averaging/denoising on source frame.
2152       for (int i = 0; i < 64; i += 4) {
2153         for (int j = 0; j < 64; j += 4) {
2154           const unsigned int avg =
2155               aom_avg_4x4(src_y + i * src_ystride + j, src_ystride);
2156 
2157           for (int m = 0; m < 4; ++m) {
2158             for (int n = 0; n < 4; ++n) src[i * 64 + j + m * 64 + n] = avg;
2159           }
2160         }
2161       }
2162 
2163       cpi->ppi->fn_ptr[bsize].vf(src, 64, pre_y, pre_ystride, &sse);
2164       fsse += sse;
2165       num_samples++;
2166       src_y += 64;
2167       pre_y += 64;
2168     }
2169     src_y += (src_ystride << 6) - (sb_cols << 6);
2170     pre_y += (pre_ystride << 6) - (sb_cols << 6);
2171   }
2172   assert(num_samples > 0);
2173   // Ensure rec_sse > 0
2174   if (num_samples > 0) cpi->rec_sse = fsse > 0 ? fsse : 1;
2175 }
2176 
av1_rc_pick_q_and_bounds(AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)2177 int av1_rc_pick_q_and_bounds(AV1_COMP *cpi, int width, int height, int gf_index,
2178                              int *bottom_index, int *top_index) {
2179   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2180   int q;
2181   // TODO(sarahparker) merge no-stats vbr and altref q computation
2182   // with rc_pick_q_and_bounds().
2183   const GF_GROUP *gf_group = &cpi->ppi->gf_group;
2184   if ((cpi->oxcf.rc_cfg.mode != AOM_Q ||
2185        gf_group->update_type[gf_index] == ARF_UPDATE) &&
2186       has_no_stats_stage(cpi)) {
2187     if (cpi->oxcf.rc_cfg.mode == AOM_CBR) {
2188       // TODO(yunqing): the results could be used for encoder optimization.
2189       cpi->rec_sse = UINT64_MAX;
2190       if (cpi->sf.hl_sf.accurate_bit_estimate &&
2191           cpi->common.current_frame.frame_type != KEY_FRAME)
2192         rc_compute_variance_onepass_rt(cpi);
2193 
2194       q = rc_pick_q_and_bounds_no_stats_cbr(cpi, width, height, bottom_index,
2195                                             top_index);
2196       // preserve copy of active worst quality selected.
2197       cpi->rc.active_worst_quality = *top_index;
2198 
2199 #if USE_UNRESTRICTED_Q_IN_CQ_MODE
2200     } else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) {
2201       q = rc_pick_q_and_bounds_no_stats_cq(cpi, width, height, bottom_index,
2202                                            top_index);
2203 #endif  // USE_UNRESTRICTED_Q_IN_CQ_MODE
2204     } else {
2205       q = rc_pick_q_and_bounds_no_stats(cpi, width, height, bottom_index,
2206                                         top_index);
2207     }
2208   } else {
2209     q = rc_pick_q_and_bounds(cpi, width, height, gf_index, bottom_index,
2210                              top_index);
2211   }
2212   if (gf_group->update_type[gf_index] == ARF_UPDATE) p_rc->arf_q = q;
2213 
2214   return q;
2215 }
2216 
av1_rc_compute_frame_size_bounds(const AV1_COMP * cpi,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)2217 void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
2218                                       int *frame_under_shoot_limit,
2219                                       int *frame_over_shoot_limit) {
2220   if (cpi->oxcf.rc_cfg.mode == AOM_Q) {
2221     *frame_under_shoot_limit = 0;
2222     *frame_over_shoot_limit = INT_MAX;
2223   } else {
2224     // For very small rate targets where the fractional adjustment
2225     // may be tiny make sure there is at least a minimum range.
2226     assert(cpi->sf.hl_sf.recode_tolerance <= 100);
2227     const int tolerance = (int)AOMMAX(
2228         100, ((int64_t)cpi->sf.hl_sf.recode_tolerance * frame_target) / 100);
2229     *frame_under_shoot_limit = AOMMAX(frame_target - tolerance, 0);
2230     *frame_over_shoot_limit =
2231         AOMMIN(frame_target + tolerance, cpi->rc.max_frame_bandwidth);
2232   }
2233 }
2234 
av1_rc_set_frame_target(AV1_COMP * cpi,int target,int width,int height)2235 void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) {
2236   const AV1_COMMON *const cm = &cpi->common;
2237   RATE_CONTROL *const rc = &cpi->rc;
2238 
2239   rc->this_frame_target = target;
2240 
2241   // Modify frame size target when down-scaled.
2242   if (av1_frame_scaled(cm) && cpi->oxcf.rc_cfg.mode != AOM_CBR) {
2243     rc->this_frame_target =
2244         (int)(rc->this_frame_target *
2245               resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height));
2246   }
2247 
2248   // Target rate per SB64 (including partial SB64s.
2249   rc->sb64_target_rate =
2250       (int)(((int64_t)rc->this_frame_target << 12) / (width * height));
2251 }
2252 
update_alt_ref_frame_stats(AV1_COMP * cpi)2253 static void update_alt_ref_frame_stats(AV1_COMP *cpi) {
2254   // this frame refreshes means next frames don't unless specified by user
2255   RATE_CONTROL *const rc = &cpi->rc;
2256   rc->frames_since_golden = 0;
2257 }
2258 
update_golden_frame_stats(AV1_COMP * cpi)2259 static void update_golden_frame_stats(AV1_COMP *cpi) {
2260   RATE_CONTROL *const rc = &cpi->rc;
2261 
2262   // Update the Golden frame usage counts.
2263   if (cpi->refresh_frame.golden_frame || rc->is_src_frame_alt_ref) {
2264     rc->frames_since_golden = 0;
2265   } else if (cpi->common.show_frame) {
2266     rc->frames_since_golden++;
2267   }
2268 }
2269 
av1_rc_postencode_update(AV1_COMP * cpi,uint64_t bytes_used)2270 void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
2271   const AV1_COMMON *const cm = &cpi->common;
2272   const CurrentFrame *const current_frame = &cm->current_frame;
2273   RATE_CONTROL *const rc = &cpi->rc;
2274   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2275   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
2276   const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
2277 
2278   const int is_intrnl_arf =
2279       gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE;
2280 
2281   const int qindex = cm->quant_params.base_qindex;
2282 
2283 #if RT_PASSIVE_STRATEGY
2284   const int frame_number = current_frame->frame_number % MAX_Q_HISTORY;
2285   p_rc->q_history[frame_number] = qindex;
2286 #endif  // RT_PASSIVE_STRATEGY
2287 
2288   // Update rate control heuristics
2289   rc->projected_frame_size = (int)(bytes_used << 3);
2290 
2291   // Post encode loop adjustment of Q prediction.
2292   av1_rc_update_rate_correction_factors(cpi, 0, cm->width, cm->height);
2293 
2294   // Update bit estimation ratio.
2295   if (cpi->oxcf.rc_cfg.mode == AOM_CBR &&
2296       cm->current_frame.frame_type != KEY_FRAME &&
2297       cpi->sf.hl_sf.accurate_bit_estimate) {
2298     const double q = av1_convert_qindex_to_q(cm->quant_params.base_qindex,
2299                                              cm->seq_params->bit_depth);
2300     const int this_bit_est_ratio =
2301         (int)(rc->projected_frame_size * q / sqrt((double)cpi->rec_sse));
2302     cpi->rc.bit_est_ratio =
2303         cpi->rc.bit_est_ratio == 0
2304             ? this_bit_est_ratio
2305             : (7 * cpi->rc.bit_est_ratio + this_bit_est_ratio) / 8;
2306   }
2307 
2308   // Keep a record of last Q and ambient average Q.
2309   if (current_frame->frame_type == KEY_FRAME) {
2310     p_rc->last_q[KEY_FRAME] = qindex;
2311     p_rc->avg_frame_qindex[KEY_FRAME] =
2312         ROUND_POWER_OF_TWO(3 * p_rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
2313   } else {
2314     if ((cpi->ppi->use_svc && cpi->oxcf.rc_cfg.mode == AOM_CBR) ||
2315         cpi->rc.rtc_external_ratectrl ||
2316         (!rc->is_src_frame_alt_ref &&
2317          !(refresh_frame->golden_frame || is_intrnl_arf ||
2318            refresh_frame->alt_ref_frame))) {
2319       p_rc->last_q[INTER_FRAME] = qindex;
2320       p_rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO(
2321           3 * p_rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
2322       p_rc->ni_frames++;
2323       p_rc->tot_q += av1_convert_qindex_to_q(qindex, cm->seq_params->bit_depth);
2324       p_rc->avg_q = p_rc->tot_q / p_rc->ni_frames;
2325       // Calculate the average Q for normal inter frames (not key or GFU
2326       // frames).
2327       rc->ni_tot_qi += qindex;
2328       rc->ni_av_qi = rc->ni_tot_qi / p_rc->ni_frames;
2329     }
2330   }
2331   // Keep record of last boosted (KF/GF/ARF) Q value.
2332   // If the current frame is coded at a lower Q then we also update it.
2333   // If all mbs in this group are skipped only update if the Q value is
2334   // better than that already stored.
2335   // This is used to help set quality in forced key frames to reduce popping
2336   if ((qindex < p_rc->last_boosted_qindex) ||
2337       (current_frame->frame_type == KEY_FRAME) ||
2338       (!p_rc->constrained_gf_group &&
2339        (refresh_frame->alt_ref_frame || is_intrnl_arf ||
2340         (refresh_frame->golden_frame && !rc->is_src_frame_alt_ref)))) {
2341     p_rc->last_boosted_qindex = qindex;
2342   }
2343   if (current_frame->frame_type == KEY_FRAME) p_rc->last_kf_qindex = qindex;
2344 
2345   update_buffer_level(cpi, rc->projected_frame_size);
2346   rc->prev_avg_frame_bandwidth = rc->avg_frame_bandwidth;
2347 
2348   // Rolling monitors of whether we are over or underspending used to help
2349   // regulate min and Max Q in two pass.
2350   if (av1_frame_scaled(cm))
2351     rc->this_frame_target = (int)(rc->this_frame_target /
2352                                   resize_rate_factor(&cpi->oxcf.frm_dim_cfg,
2353                                                      cm->width, cm->height));
2354   if (current_frame->frame_type != KEY_FRAME) {
2355     p_rc->rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
2356         p_rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
2357     p_rc->rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
2358         p_rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
2359   }
2360 
2361   // Actual bits spent
2362   p_rc->total_actual_bits += rc->projected_frame_size;
2363   p_rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
2364 
2365   if (is_altref_enabled(cpi->oxcf.gf_cfg.lag_in_frames,
2366                         cpi->oxcf.gf_cfg.enable_auto_arf) &&
2367       refresh_frame->alt_ref_frame &&
2368       (current_frame->frame_type != KEY_FRAME && !frame_is_sframe(cm)))
2369     // Update the alternate reference frame stats as appropriate.
2370     update_alt_ref_frame_stats(cpi);
2371   else
2372     // Update the Golden frame stats as appropriate.
2373     update_golden_frame_stats(cpi);
2374 
2375 #if CONFIG_FPMT_TEST
2376   /*The variables temp_avg_frame_qindex, temp_last_q, temp_avg_q,
2377    * temp_last_boosted_qindex are introduced only for quality simulation
2378    * purpose, it retains the value previous to the parallel encode frames. The
2379    * variables are updated based on the update flag.
2380    *
2381    * If there exist show_existing_frames between parallel frames, then to
2382    * retain the temp state do not update it. */
2383   int show_existing_between_parallel_frames =
2384       (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
2385            INTNL_OVERLAY_UPDATE &&
2386        cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
2387 
2388   if (cpi->do_frame_data_update && !show_existing_between_parallel_frames &&
2389       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
2390     for (int i = 0; i < FRAME_TYPES; i++) {
2391       p_rc->temp_last_q[i] = p_rc->last_q[i];
2392     }
2393     p_rc->temp_avg_q = p_rc->avg_q;
2394     p_rc->temp_last_boosted_qindex = p_rc->last_boosted_qindex;
2395     p_rc->temp_total_actual_bits = p_rc->total_actual_bits;
2396     p_rc->temp_projected_frame_size = rc->projected_frame_size;
2397     for (int i = 0; i < RATE_FACTOR_LEVELS; i++)
2398       p_rc->temp_rate_correction_factors[i] = p_rc->rate_correction_factors[i];
2399   }
2400 #endif
2401   if (current_frame->frame_type == KEY_FRAME) rc->frames_since_key = 0;
2402   if (cpi->refresh_frame.golden_frame)
2403     rc->frame_num_last_gf_refresh = current_frame->frame_number;
2404   rc->prev_coded_width = cm->width;
2405   rc->prev_coded_height = cm->height;
2406   rc->frame_number_encoded++;
2407   rc->prev_frame_is_dropped = 0;
2408   rc->drop_count_consec = 0;
2409   // if (current_frame->frame_number == 1 && cm->show_frame)
2410   /*
2411   rc->this_frame_target =
2412       (int)(rc->this_frame_target / resize_rate_factor(&cpi->oxcf.frm_dim_cfg,
2413   cm->width, cm->height));
2414       */
2415 }
2416 
av1_rc_postencode_update_drop_frame(AV1_COMP * cpi)2417 void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
2418   // Update buffer level with zero size, update frame counters, and return.
2419   update_buffer_level(cpi, 0);
2420   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
2421     cpi->rc.frames_since_key++;
2422     cpi->rc.frames_to_key--;
2423   }
2424   cpi->rc.rc_2_frame = 0;
2425   cpi->rc.rc_1_frame = 0;
2426   cpi->rc.prev_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth;
2427   cpi->rc.prev_coded_width = cpi->common.width;
2428   cpi->rc.prev_coded_height = cpi->common.height;
2429   cpi->rc.prev_frame_is_dropped = 1;
2430   // On a scene/slide change for dropped frame: reset the avg_source_sad to 0,
2431   // otherwise the avg_source_sad can get too large and subsequent frames
2432   // may miss the scene/slide detection.
2433   if (cpi->rc.high_source_sad) cpi->rc.avg_source_sad = 0;
2434   if (cpi->ppi->use_svc && cpi->svc.number_spatial_layers > 1) {
2435     cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = true;
2436     cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = true;
2437   }
2438 }
2439 
av1_find_qindex(double desired_q,aom_bit_depth_t bit_depth,int best_qindex,int worst_qindex)2440 int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth,
2441                     int best_qindex, int worst_qindex) {
2442   assert(best_qindex <= worst_qindex);
2443   int low = best_qindex;
2444   int high = worst_qindex;
2445   while (low < high) {
2446     const int mid = (low + high) >> 1;
2447     const double mid_q = av1_convert_qindex_to_q(mid, bit_depth);
2448     if (mid_q < desired_q) {
2449       low = mid + 1;
2450     } else {
2451       high = mid;
2452     }
2453   }
2454   assert(low == high);
2455   assert(av1_convert_qindex_to_q(low, bit_depth) >= desired_q ||
2456          low == worst_qindex);
2457   return low;
2458 }
2459 
av1_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget,aom_bit_depth_t bit_depth)2460 int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
2461                        aom_bit_depth_t bit_depth) {
2462   const int start_index =
2463       av1_find_qindex(qstart, bit_depth, rc->best_quality, rc->worst_quality);
2464   const int target_index =
2465       av1_find_qindex(qtarget, bit_depth, rc->best_quality, rc->worst_quality);
2466   return target_index - start_index;
2467 }
2468 
2469 // Find q_index for the desired_bits_per_mb, within [best_qindex, worst_qindex],
2470 // assuming 'correction_factor' is 1.0.
2471 // To be precise, 'q_index' is the smallest integer, for which the corresponding
2472 // bits per mb <= desired_bits_per_mb.
2473 // If no such q index is found, returns 'worst_qindex'.
find_qindex_by_rate(const AV1_COMP * const cpi,int desired_bits_per_mb,FRAME_TYPE frame_type,int best_qindex,int worst_qindex)2474 static int find_qindex_by_rate(const AV1_COMP *const cpi,
2475                                int desired_bits_per_mb, FRAME_TYPE frame_type,
2476                                int best_qindex, int worst_qindex) {
2477   assert(best_qindex <= worst_qindex);
2478   int low = best_qindex;
2479   int high = worst_qindex;
2480   while (low < high) {
2481     const int mid = (low + high) >> 1;
2482     const int mid_bits_per_mb =
2483         av1_rc_bits_per_mb(cpi, frame_type, mid, 1.0, 0);
2484     if (mid_bits_per_mb > desired_bits_per_mb) {
2485       low = mid + 1;
2486     } else {
2487       high = mid;
2488     }
2489   }
2490   assert(low == high);
2491   assert(av1_rc_bits_per_mb(cpi, frame_type, low, 1.0, 0) <=
2492              desired_bits_per_mb ||
2493          low == worst_qindex);
2494   return low;
2495 }
2496 
av1_compute_qdelta_by_rate(const AV1_COMP * cpi,FRAME_TYPE frame_type,int qindex,double rate_target_ratio)2497 int av1_compute_qdelta_by_rate(const AV1_COMP *cpi, FRAME_TYPE frame_type,
2498                                int qindex, double rate_target_ratio) {
2499   const RATE_CONTROL *rc = &cpi->rc;
2500 
2501   // Look up the current projected bits per block for the base index
2502   const int base_bits_per_mb =
2503       av1_rc_bits_per_mb(cpi, frame_type, qindex, 1.0, 0);
2504 
2505   // Find the target bits per mb based on the base value and given ratio.
2506   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
2507 
2508   const int target_index = find_qindex_by_rate(
2509       cpi, target_bits_per_mb, frame_type, rc->best_quality, rc->worst_quality);
2510   return target_index - qindex;
2511 }
2512 
av1_rc_set_gf_interval_range(const AV1_COMP * const cpi,RATE_CONTROL * const rc)2513 void av1_rc_set_gf_interval_range(const AV1_COMP *const cpi,
2514                                   RATE_CONTROL *const rc) {
2515   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2516 
2517   // Special case code for 1 pass fixed Q mode tests
2518   if ((has_no_stats_stage(cpi)) && (oxcf->rc_cfg.mode == AOM_Q)) {
2519     rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
2520     rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
2521     rc->static_scene_max_gf_interval = rc->min_gf_interval + 1;
2522   } else {
2523     // Set Maximum gf/arf interval
2524     rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval;
2525     rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval;
2526     if (rc->min_gf_interval == 0)
2527       rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
2528           oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, cpi->framerate);
2529     if (rc->max_gf_interval == 0)
2530       rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
2531           cpi->framerate, rc->min_gf_interval);
2532     /*
2533      * Extended max interval for genuinely static scenes like slide shows.
2534      * The no.of.stats available in the case of LAP is limited,
2535      * hence setting to max_gf_interval.
2536      */
2537     if (cpi->ppi->lap_enabled)
2538       rc->static_scene_max_gf_interval = rc->max_gf_interval + 1;
2539     else
2540       rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
2541 
2542     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
2543       rc->max_gf_interval = rc->static_scene_max_gf_interval;
2544 
2545     // Clamp min to max
2546     rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval);
2547   }
2548 }
2549 
av1_rc_update_framerate(AV1_COMP * cpi,int width,int height)2550 void av1_rc_update_framerate(AV1_COMP *cpi, int width, int height) {
2551   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2552   RATE_CONTROL *const rc = &cpi->rc;
2553   int vbr_max_bits;
2554   const int MBs = av1_get_MBs(width, height);
2555 
2556   rc->avg_frame_bandwidth =
2557       (int)round(oxcf->rc_cfg.target_bandwidth / cpi->framerate);
2558   rc->min_frame_bandwidth =
2559       (int)(rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmin_section / 100);
2560 
2561   rc->min_frame_bandwidth =
2562       AOMMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
2563 
2564   // A maximum bitrate for a frame is defined.
2565   // The baseline for this aligns with HW implementations that
2566   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
2567   // per 16x16 MB (averaged over a frame). However this limit is extended if
2568   // a very high rate is given on the command line or the the rate cannnot
2569   // be acheived because of a user specificed max q (e.g. when the user
2570   // specifies lossless encode.
2571   vbr_max_bits =
2572       (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmax_section) /
2573             100);
2574   rc->max_frame_bandwidth =
2575       AOMMAX(AOMMAX((MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
2576 
2577   av1_rc_set_gf_interval_range(cpi, rc);
2578 }
2579 
2580 #define VBR_PCT_ADJUSTMENT_LIMIT 50
2581 // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(AV1_COMP * cpi,int * this_frame_target)2582 static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) {
2583   RATE_CONTROL *const rc = &cpi->rc;
2584   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2585 #if CONFIG_FPMT_TEST
2586   const int simulate_parallel_frame =
2587       cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 &&
2588       cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE;
2589   int64_t vbr_bits_off_target = simulate_parallel_frame
2590                                     ? cpi->ppi->p_rc.temp_vbr_bits_off_target
2591                                     : p_rc->vbr_bits_off_target;
2592 #else
2593   int64_t vbr_bits_off_target = p_rc->vbr_bits_off_target;
2594 #endif
2595   const int stats_count =
2596       cpi->ppi->twopass.stats_buf_ctx->total_stats != NULL
2597           ? (int)cpi->ppi->twopass.stats_buf_ctx->total_stats->count
2598           : 0;
2599   const int frame_window = AOMMIN(
2600       16, (int)(stats_count - (int)cpi->common.current_frame.frame_number));
2601   assert(VBR_PCT_ADJUSTMENT_LIMIT <= 100);
2602   if (frame_window > 0) {
2603     const int max_delta = (int)AOMMIN(
2604         abs((int)(vbr_bits_off_target / frame_window)),
2605         ((int64_t)(*this_frame_target) * VBR_PCT_ADJUSTMENT_LIMIT) / 100);
2606 
2607     // vbr_bits_off_target > 0 means we have extra bits to spend
2608     // vbr_bits_off_target < 0 we are currently overshooting
2609     *this_frame_target += (vbr_bits_off_target >= 0) ? max_delta : -max_delta;
2610   }
2611 
2612 #if CONFIG_FPMT_TEST
2613   int64_t vbr_bits_off_target_fast =
2614       simulate_parallel_frame ? cpi->ppi->p_rc.temp_vbr_bits_off_target_fast
2615                               : p_rc->vbr_bits_off_target_fast;
2616 #endif
2617   // Fast redistribution of bits arising from massive local undershoot.
2618   // Dont do it for kf,arf,gf or overlay frames.
2619   if (!frame_is_kf_gf_arf(cpi) &&
2620 #if CONFIG_FPMT_TEST
2621       vbr_bits_off_target_fast &&
2622 #else
2623       p_rc->vbr_bits_off_target_fast &&
2624 #endif
2625       !rc->is_src_frame_alt_ref) {
2626     int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target);
2627     int fast_extra_bits;
2628 #if CONFIG_FPMT_TEST
2629     fast_extra_bits = (int)AOMMIN(vbr_bits_off_target_fast, one_frame_bits);
2630     fast_extra_bits =
2631         (int)AOMMIN(fast_extra_bits,
2632                     AOMMAX(one_frame_bits / 8, vbr_bits_off_target_fast / 8));
2633 #else
2634     fast_extra_bits =
2635         (int)AOMMIN(p_rc->vbr_bits_off_target_fast, one_frame_bits);
2636     fast_extra_bits = (int)AOMMIN(
2637         fast_extra_bits,
2638         AOMMAX(one_frame_bits / 8, p_rc->vbr_bits_off_target_fast / 8));
2639 #endif
2640     if (fast_extra_bits > 0) {
2641       // Update this_frame_target only if additional bits are available from
2642       // local undershoot.
2643       *this_frame_target += (int)fast_extra_bits;
2644     }
2645     // Store the fast_extra_bits of the frame and reduce it from
2646     // vbr_bits_off_target_fast during postencode stage.
2647     rc->frame_level_fast_extra_bits = fast_extra_bits;
2648     // Retaining the condition to udpate during postencode stage since
2649     // fast_extra_bits are calculated based on vbr_bits_off_target_fast.
2650     cpi->do_update_vbr_bits_off_target_fast = 1;
2651   }
2652 }
2653 
av1_set_target_rate(AV1_COMP * cpi,int width,int height)2654 void av1_set_target_rate(AV1_COMP *cpi, int width, int height) {
2655   RATE_CONTROL *const rc = &cpi->rc;
2656   int target_rate = rc->base_frame_target;
2657 
2658   // Correction to rate target based on prior over or under shoot.
2659   if (cpi->oxcf.rc_cfg.mode == AOM_VBR || cpi->oxcf.rc_cfg.mode == AOM_CQ)
2660     vbr_rate_correction(cpi, &target_rate);
2661   av1_rc_set_frame_target(cpi, target_rate, width, height);
2662 }
2663 
av1_calc_pframe_target_size_one_pass_vbr(const AV1_COMP * const cpi,FRAME_UPDATE_TYPE frame_update_type)2664 int av1_calc_pframe_target_size_one_pass_vbr(
2665     const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
2666   static const int af_ratio = 10;
2667   const RATE_CONTROL *const rc = &cpi->rc;
2668   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2669   int64_t target;
2670 #if USE_ALTREF_FOR_ONE_PASS
2671   if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
2672       frame_update_type == ARF_UPDATE) {
2673     target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval *
2674               af_ratio) /
2675              (p_rc->baseline_gf_interval + af_ratio - 1);
2676   } else {
2677     target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval) /
2678              (p_rc->baseline_gf_interval + af_ratio - 1);
2679   }
2680   if (target > INT_MAX) target = INT_MAX;
2681 #else
2682   target = rc->avg_frame_bandwidth;
2683 #endif
2684   return av1_rc_clamp_pframe_target_size(cpi, (int)target, frame_update_type);
2685 }
2686 
av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP * const cpi)2687 int av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
2688   static const int kf_ratio = 25;
2689   const RATE_CONTROL *rc = &cpi->rc;
2690   const int64_t target = (int64_t)rc->avg_frame_bandwidth * kf_ratio;
2691   return av1_rc_clamp_iframe_target_size(cpi, target);
2692 }
2693 
av1_calc_pframe_target_size_one_pass_cbr(const AV1_COMP * cpi,FRAME_UPDATE_TYPE frame_update_type)2694 int av1_calc_pframe_target_size_one_pass_cbr(
2695     const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
2696   const AV1EncoderConfig *oxcf = &cpi->oxcf;
2697   const RATE_CONTROL *rc = &cpi->rc;
2698   const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2699   const RateControlCfg *rc_cfg = &oxcf->rc_cfg;
2700   const int64_t diff = p_rc->optimal_buffer_level - p_rc->buffer_level;
2701   const int64_t one_pct_bits = 1 + p_rc->optimal_buffer_level / 100;
2702   int min_frame_target =
2703       AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
2704   int target;
2705 
2706   if (rc_cfg->gf_cbr_boost_pct) {
2707     const int af_ratio_pct = rc_cfg->gf_cbr_boost_pct + 100;
2708     if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
2709       target = (rc->avg_frame_bandwidth * p_rc->baseline_gf_interval *
2710                 af_ratio_pct) /
2711                (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2712     } else {
2713       target = (rc->avg_frame_bandwidth * p_rc->baseline_gf_interval * 100) /
2714                (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
2715     }
2716   } else {
2717     target = rc->avg_frame_bandwidth;
2718   }
2719   if (cpi->ppi->use_svc) {
2720     // Note that for layers, avg_frame_bandwidth is the cumulative
2721     // per-frame-bandwidth. For the target size of this frame, use the
2722     // layer average frame size (i.e., non-cumulative per-frame-bw).
2723     int layer =
2724         LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
2725                          cpi->svc.number_temporal_layers);
2726     const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
2727     target = lc->avg_frame_size;
2728     min_frame_target = AOMMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
2729   }
2730   if (diff > 0) {
2731     // Lower the target bandwidth for this frame.
2732     const int pct_low =
2733         (int)AOMMIN(diff / one_pct_bits, rc_cfg->under_shoot_pct);
2734     target -= (target * pct_low) / 200;
2735   } else if (diff < 0) {
2736     // Increase the target bandwidth for this frame.
2737     const int pct_high =
2738         (int)AOMMIN(-diff / one_pct_bits, rc_cfg->over_shoot_pct);
2739     target += (target * pct_high) / 200;
2740   }
2741   if (rc_cfg->max_inter_bitrate_pct) {
2742     const int max_rate =
2743         rc->avg_frame_bandwidth * rc_cfg->max_inter_bitrate_pct / 100;
2744     target = AOMMIN(target, max_rate);
2745   }
2746   return AOMMAX(min_frame_target, target);
2747 }
2748 
av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP * cpi)2749 int av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
2750   const RATE_CONTROL *rc = &cpi->rc;
2751   const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2752   int64_t target;
2753   if (cpi->common.current_frame.frame_number == 0) {
2754     target = ((p_rc->starting_buffer_level / 2) > INT_MAX)
2755                  ? INT_MAX
2756                  : (int)(p_rc->starting_buffer_level / 2);
2757     if (cpi->svc.number_temporal_layers > 1 && target < (INT_MAX >> 2)) {
2758       target = target << AOMMIN(2, (cpi->svc.number_temporal_layers - 1));
2759     }
2760   } else {
2761     int kf_boost = 32;
2762     int framerate = (int)round(cpi->framerate);
2763 
2764     kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
2765     if (rc->frames_since_key < framerate / 2) {
2766       kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
2767     }
2768     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
2769   }
2770   return av1_rc_clamp_iframe_target_size(cpi, target);
2771 }
2772 
set_golden_update(AV1_COMP * const cpi)2773 static void set_golden_update(AV1_COMP *const cpi) {
2774   RATE_CONTROL *const rc = &cpi->rc;
2775   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2776   int divisor = 10;
2777   if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
2778     divisor = cpi->cyclic_refresh->percent_refresh;
2779 
2780   // Set minimum gf_interval for GF update to a multiple of the refresh period,
2781   // with some max limit. Depending on past encoding stats, GF flag may be
2782   // reset and update may not occur until next baseline_gf_interval.
2783   const int gf_length_mult[2] = { 8, 4 };
2784   if (divisor > 0)
2785     p_rc->baseline_gf_interval =
2786         AOMMIN(gf_length_mult[cpi->sf.rt_sf.gf_length_lvl] * (100 / divisor),
2787                MAX_GF_INTERVAL_RT);
2788   else
2789     p_rc->baseline_gf_interval = FIXED_GF_INTERVAL_RT;
2790   if (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 40)
2791     p_rc->baseline_gf_interval = 16;
2792 }
2793 
set_baseline_gf_interval(AV1_COMP * cpi,FRAME_TYPE frame_type)2794 static void set_baseline_gf_interval(AV1_COMP *cpi, FRAME_TYPE frame_type) {
2795   RATE_CONTROL *const rc = &cpi->rc;
2796   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
2797   GF_GROUP *const gf_group = &cpi->ppi->gf_group;
2798 
2799   set_golden_update(cpi);
2800 
2801   if (p_rc->baseline_gf_interval > rc->frames_to_key &&
2802       cpi->oxcf.kf_cfg.auto_key)
2803     p_rc->baseline_gf_interval = rc->frames_to_key;
2804   p_rc->gfu_boost = DEFAULT_GF_BOOST_RT;
2805   p_rc->constrained_gf_group =
2806       (p_rc->baseline_gf_interval >= rc->frames_to_key &&
2807        cpi->oxcf.kf_cfg.auto_key)
2808           ? 1
2809           : 0;
2810   rc->frames_till_gf_update_due = p_rc->baseline_gf_interval;
2811   cpi->gf_frame_index = 0;
2812   // SVC does not use GF as periodic boost.
2813   // TODO(marpan): Find better way to disable this for SVC.
2814   if (cpi->ppi->use_svc) {
2815     SVC *const svc = &cpi->svc;
2816     p_rc->baseline_gf_interval = MAX_STATIC_GF_GROUP_LENGTH - 1;
2817     p_rc->gfu_boost = 1;
2818     p_rc->constrained_gf_group = 0;
2819     rc->frames_till_gf_update_due = p_rc->baseline_gf_interval;
2820     for (int layer = 0;
2821          layer < svc->number_spatial_layers * svc->number_temporal_layers;
2822          ++layer) {
2823       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
2824       lc->p_rc.baseline_gf_interval = p_rc->baseline_gf_interval;
2825       lc->p_rc.gfu_boost = p_rc->gfu_boost;
2826       lc->p_rc.constrained_gf_group = p_rc->constrained_gf_group;
2827       lc->rc.frames_till_gf_update_due = rc->frames_till_gf_update_due;
2828       lc->group_index = 0;
2829     }
2830   }
2831   gf_group->size = p_rc->baseline_gf_interval;
2832   gf_group->update_type[0] = (frame_type == KEY_FRAME) ? KF_UPDATE : GF_UPDATE;
2833   gf_group->refbuf_state[cpi->gf_frame_index] =
2834       (frame_type == KEY_FRAME) ? REFBUF_RESET : REFBUF_UPDATE;
2835 }
2836 
av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP * cpi)2837 void av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP *cpi) {
2838   AV1_COMMON *const cm = &cpi->common;
2839   RATE_CONTROL *const rc = &cpi->rc;
2840   RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
2841   const int resize_pending = is_frame_resize_pending(cpi);
2842   if (!resize_pending && !rc->high_source_sad) {
2843     // Check if we should disable GF refresh (if period is up),
2844     // or force a GF refresh update (if we are at least halfway through
2845     // period) based on QP. Look into add info on segment deltaq.
2846     PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc;
2847     const int avg_qp = p_rc->avg_frame_qindex[INTER_FRAME];
2848     const int allow_gf_update =
2849         rc->frames_till_gf_update_due <= (p_rc->baseline_gf_interval - 10);
2850     int gf_update_changed = 0;
2851     int thresh = 87;
2852     if ((cm->current_frame.frame_number - cpi->rc.frame_num_last_gf_refresh) <
2853             FIXED_GF_INTERVAL_RT &&
2854         rc->frames_till_gf_update_due == 1 &&
2855         cm->quant_params.base_qindex > avg_qp) {
2856       // Disable GF refresh since QP is above the running average QP.
2857       rtc_ref->refresh[rtc_ref->gld_idx_1layer] = 0;
2858       gf_update_changed = 1;
2859       cpi->refresh_frame.golden_frame = 0;
2860     } else if (allow_gf_update &&
2861                ((cm->quant_params.base_qindex < thresh * avg_qp / 100) ||
2862                 (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 20))) {
2863       // Force refresh since QP is well below average QP or this is a high
2864       // motion frame.
2865       rtc_ref->refresh[rtc_ref->gld_idx_1layer] = 1;
2866       gf_update_changed = 1;
2867       cpi->refresh_frame.golden_frame = 1;
2868     }
2869     if (gf_update_changed) {
2870       set_baseline_gf_interval(cpi, INTER_FRAME);
2871       int refresh_mask = 0;
2872       for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
2873         int ref_frame_map_idx = rtc_ref->ref_idx[i];
2874         refresh_mask |= rtc_ref->refresh[ref_frame_map_idx]
2875                         << ref_frame_map_idx;
2876       }
2877       cm->current_frame.refresh_frame_flags = refresh_mask;
2878     }
2879   }
2880 }
2881 
2882 /*!\brief Setup the reference prediction structure for 1 pass real-time
2883  *
2884  * Set the reference prediction structure for 1 layer.
2885  * Current structue is to use 3 references (LAST, GOLDEN, ALTREF),
2886  * where ALT_REF always behind current by lag_alt frames, and GOLDEN is
2887  * either updated on LAST with period baseline_gf_interval (fixed slot)
2888  * or always behind current by lag_gld (gld_fixed_slot = 0, lag_gld <= 7).
2889  *
2890  * \ingroup rate_control
2891  * \param[in]       cpi          Top level encoder structure
2892  * \param[in]       gf_update    Flag to indicate if GF is updated
2893  *
2894  * \remark Nothing is returned. Instead the settings for the prediction
2895  * structure are set in \c cpi-ext_flags; and the buffer slot index
2896  * (for each of 7 references) and refresh flags (for each of the 8 slots)
2897  * are set in \c cpi->svc.ref_idx[] and \c cpi->svc.refresh[].
2898  */
av1_set_rtc_reference_structure_one_layer(AV1_COMP * cpi,int gf_update)2899 void av1_set_rtc_reference_structure_one_layer(AV1_COMP *cpi, int gf_update) {
2900   AV1_COMMON *const cm = &cpi->common;
2901   ExternalFlags *const ext_flags = &cpi->ext_flags;
2902   RATE_CONTROL *const rc = &cpi->rc;
2903   ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
2904       &ext_flags->refresh_frame;
2905   RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
2906   unsigned int frame_number = (cpi->oxcf.rc_cfg.drop_frames_water_mark)
2907                                   ? rc->frame_number_encoded
2908                                   : cm->current_frame.frame_number;
2909   unsigned int lag_alt = 4;
2910   int last_idx = 0;
2911   int last_idx_refresh = 0;
2912   int gld_idx = 0;
2913   int alt_ref_idx = 0;
2914   int last2_idx = 0;
2915   ext_refresh_frame_flags->update_pending = 1;
2916   ext_flags->ref_frame_flags = 0;
2917   ext_refresh_frame_flags->last_frame = 1;
2918   ext_refresh_frame_flags->golden_frame = 0;
2919   ext_refresh_frame_flags->alt_ref_frame = 0;
2920   // Decide altref lag adaptively for rt
2921   if (cpi->sf.rt_sf.sad_based_adp_altref_lag) {
2922     lag_alt = 6;
2923     const uint64_t th_frame_sad[4][3] = {
2924       { 18000, 18000, 18000 },  // HDRES CPU 9
2925       { 25000, 25000, 25000 },  // MIDRES CPU 9
2926       { 40000, 30000, 20000 },  // HDRES CPU10
2927       { 30000, 25000, 20000 }   // MIDRES CPU 10
2928     };
2929     int th_idx = cpi->sf.rt_sf.sad_based_adp_altref_lag - 1;
2930     assert(th_idx < 4);
2931     if (rc->avg_source_sad > th_frame_sad[th_idx][0])
2932       lag_alt = 3;
2933     else if (rc->avg_source_sad > th_frame_sad[th_idx][1])
2934       lag_alt = 4;
2935     else if (rc->avg_source_sad > th_frame_sad[th_idx][2])
2936       lag_alt = 5;
2937   }
2938   // This defines the reference structure for 1 layer (non-svc) RTC encoding.
2939   // To avoid the internal/default reference structure for non-realtime
2940   // overwriting this behavior, we use the "svc" ref parameters from the
2941   // external control SET_SVC_REF_FRAME_CONFIG.
2942   // TODO(marpan): rename that control and the related internal parameters
2943   // to rtc_ref.
2944   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) rtc_ref->ref_idx[i] = 7;
2945   for (int i = 0; i < REF_FRAMES; ++i) rtc_ref->refresh[i] = 0;
2946   // Set the reference frame flags.
2947   ext_flags->ref_frame_flags ^= AOM_LAST_FLAG;
2948   if (!cpi->sf.rt_sf.force_only_last_ref) {
2949     ext_flags->ref_frame_flags ^= AOM_ALT_FLAG;
2950     ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG;
2951     if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1])
2952       ext_flags->ref_frame_flags ^= AOM_LAST2_FLAG;
2953   }
2954   const int sh = 6;
2955   // Moving index slot for last: 0 - (sh - 1).
2956   if (frame_number > 1) last_idx = ((frame_number - 1) % sh);
2957   // Moving index for refresh of last: one ahead for next frame.
2958   last_idx_refresh = (frame_number % sh);
2959   gld_idx = 6;
2960 
2961   // Moving index for alt_ref, lag behind LAST by lag_alt frames.
2962   if (frame_number > lag_alt) alt_ref_idx = ((frame_number - lag_alt) % sh);
2963   if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) {
2964     // Moving index for LAST2, lag behind LAST by 2 frames.
2965     if (frame_number > 2) last2_idx = ((frame_number - 2) % sh);
2966   }
2967   rtc_ref->ref_idx[0] = last_idx;          // LAST
2968   rtc_ref->ref_idx[1] = last_idx_refresh;  // LAST2 (for refresh of last).
2969   if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) {
2970     rtc_ref->ref_idx[1] = last2_idx;         // LAST2
2971     rtc_ref->ref_idx[2] = last_idx_refresh;  // LAST3 (for refresh of last).
2972   }
2973   rtc_ref->ref_idx[3] = gld_idx;      // GOLDEN
2974   rtc_ref->ref_idx[6] = alt_ref_idx;  // ALT_REF
2975   // Refresh this slot, which will become LAST on next frame.
2976   rtc_ref->refresh[last_idx_refresh] = 1;
2977   // Update GOLDEN on period for fixed slot case.
2978   if (gf_update && cm->current_frame.frame_type != KEY_FRAME) {
2979     ext_refresh_frame_flags->golden_frame = 1;
2980     rtc_ref->refresh[gld_idx] = 1;
2981   }
2982   rtc_ref->gld_idx_1layer = gld_idx;
2983   // Set the flag to reduce the number of reference frame buffers used.
2984   // This assumes that slot 7 is never used.
2985   cpi->rt_reduce_num_ref_buffers = 1;
2986   cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[0] < 7);
2987   cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[1] < 7);
2988   cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[3] < 7);
2989   cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[6] < 7);
2990   if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1])
2991     cpi->rt_reduce_num_ref_buffers &= (rtc_ref->ref_idx[2] < 7);
2992 }
2993 
set_block_is_active(unsigned char * const active_map_4x4,int mi_cols,int mi_rows,int sbi_col,int sbi_row,int sh,int num_4x4)2994 static int set_block_is_active(unsigned char *const active_map_4x4, int mi_cols,
2995                                int mi_rows, int sbi_col, int sbi_row, int sh,
2996                                int num_4x4) {
2997   int r = sbi_row << sh;
2998   int c = sbi_col << sh;
2999   const int row_max = AOMMIN(num_4x4, mi_rows - r);
3000   const int col_max = AOMMIN(num_4x4, mi_cols - c);
3001   // Active map is set for 16x16 blocks, so only need to
3002   // check over16x16,
3003   for (int x = 0; x < row_max; x += 4) {
3004     for (int y = 0; y < col_max; y += 4) {
3005       if (active_map_4x4[(r + x) * mi_cols + (c + y)] == AM_SEGMENT_ID_ACTIVE)
3006         return 1;
3007     }
3008   }
3009   return 0;
3010 }
3011 
3012 /*!\brief Check for scene detection, for 1 pass real-time mode.
3013  *
3014  * Compute average source sad (temporal sad: between current source and
3015  * previous source) over a subset of superblocks. Use this is detect big changes
3016  * in content and set the \c cpi->rc.high_source_sad flag.
3017  *
3018  * \ingroup rate_control
3019  * \param[in]       cpi          Top level encoder structure
3020  * \param[in]       frame_input  Current and last input source frames
3021  *
3022  * \remark Nothing is returned. Instead the flag \c cpi->rc.high_source_sad
3023  * is set if scene change is detected, and \c cpi->rc.avg_source_sad is updated.
3024  */
rc_scene_detection_onepass_rt(AV1_COMP * cpi,const EncodeFrameInput * frame_input)3025 static void rc_scene_detection_onepass_rt(AV1_COMP *cpi,
3026                                           const EncodeFrameInput *frame_input) {
3027   AV1_COMMON *const cm = &cpi->common;
3028   RATE_CONTROL *const rc = &cpi->rc;
3029   YV12_BUFFER_CONFIG const *const unscaled_src = frame_input->source;
3030   YV12_BUFFER_CONFIG const *const unscaled_last_src = frame_input->last_source;
3031   uint8_t *src_y;
3032   int src_ystride;
3033   int src_width;
3034   int src_height;
3035   uint8_t *last_src_y;
3036   int last_src_ystride;
3037   int last_src_width;
3038   int last_src_height;
3039   int width = cm->width;
3040   int height = cm->height;
3041   if (cpi->svc.number_spatial_layers > 1) {
3042     width = cpi->oxcf.frm_dim_cfg.width;
3043     height = cpi->oxcf.frm_dim_cfg.height;
3044   }
3045   if (width != cm->render_width || height != cm->render_height ||
3046       unscaled_src == NULL || unscaled_last_src == NULL) {
3047     aom_free(cpi->src_sad_blk_64x64);
3048     cpi->src_sad_blk_64x64 = NULL;
3049   }
3050   if (unscaled_src == NULL || unscaled_last_src == NULL) return;
3051   src_y = unscaled_src->y_buffer;
3052   src_ystride = unscaled_src->y_stride;
3053   src_width = unscaled_src->y_width;
3054   src_height = unscaled_src->y_height;
3055   last_src_y = unscaled_last_src->y_buffer;
3056   last_src_ystride = unscaled_last_src->y_stride;
3057   last_src_width = unscaled_last_src->y_width;
3058   last_src_height = unscaled_last_src->y_height;
3059   if (src_width != last_src_width || src_height != last_src_height) {
3060     aom_free(cpi->src_sad_blk_64x64);
3061     cpi->src_sad_blk_64x64 = NULL;
3062     return;
3063   }
3064   rc->high_source_sad = 0;
3065   rc->percent_blocks_with_motion = 0;
3066   rc->max_block_source_sad = 0;
3067   rc->prev_avg_source_sad = rc->avg_source_sad;
3068   int num_mi_cols = cm->mi_params.mi_cols;
3069   int num_mi_rows = cm->mi_params.mi_rows;
3070   if (cpi->svc.number_spatial_layers > 1) {
3071     num_mi_cols = cpi->svc.mi_cols_full_resoln;
3072     num_mi_rows = cpi->svc.mi_rows_full_resoln;
3073   }
3074   int num_zero_temp_sad = 0;
3075   uint32_t min_thresh = 10000;
3076   if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN) {
3077     min_thresh = cm->width * cm->height <= 320 * 240 && cpi->framerate < 10.0
3078                      ? 50000
3079                      : 100000;
3080   }
3081   const BLOCK_SIZE bsize = BLOCK_64X64;
3082   // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
3083   uint64_t avg_sad = 0;
3084   uint64_t tmp_sad = 0;
3085   int num_samples = 0;
3086   const int thresh =
3087       cm->width * cm->height <= 320 * 240 && cpi->framerate < 10.0 ? 5 : 6;
3088   // SAD is computed on 64x64 blocks
3089   const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128)
3090                                 ? (cm->seq_params->mib_size >> 1)
3091                                 : cm->seq_params->mib_size;
3092   const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb;
3093   const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb;
3094   uint64_t sum_sq_thresh = 10000;  // sum = sqrt(thresh / 64*64)) ~1.5
3095   int num_low_var_high_sumdiff = 0;
3096   int light_change = 0;
3097   // Flag to check light change or not.
3098   const int check_light_change = 0;
3099   // TODO(marpan): There seems some difference along the bottom border when
3100   // using the source_last_tl0 for last_source (used for temporal layers or
3101   // when previous frame is dropped).
3102   // Remove this bord parameter when issue is resolved: difference is that
3103   // non-zero sad exists along bottom border even though source is static.
3104   const int border =
3105       rc->prev_frame_is_dropped || cpi->svc.number_temporal_layers > 1;
3106   // Store blkwise SAD for later use
3107   if (width == cm->render_width && height == cm->render_height) {
3108     if (cpi->src_sad_blk_64x64 == NULL) {
3109       CHECK_MEM_ERROR(cm, cpi->src_sad_blk_64x64,
3110                       (uint64_t *)aom_calloc(sb_cols * sb_rows,
3111                                              sizeof(*cpi->src_sad_blk_64x64)));
3112     }
3113   }
3114   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
3115   const int mi_cols = mi_params->mi_cols;
3116   const int mi_rows = mi_params->mi_rows;
3117   int sh = (cm->seq_params->sb_size == BLOCK_128X128) ? 5 : 4;
3118   int num_4x4 = (cm->seq_params->sb_size == BLOCK_128X128) ? 32 : 16;
3119   unsigned char *const active_map_4x4 = cpi->active_map.map;
3120   // Avoid bottom and right border.
3121   for (int sbi_row = 0; sbi_row < sb_rows - border; ++sbi_row) {
3122     for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
3123       int block_is_active = 1;
3124       if (cpi->active_map.enabled && rc->percent_blocks_inactive > 0) {
3125         block_is_active = set_block_is_active(active_map_4x4, mi_cols, mi_rows,
3126                                               sbi_col, sbi_row, sh, num_4x4);
3127       }
3128       if (block_is_active) {
3129         tmp_sad = cpi->ppi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
3130                                               last_src_ystride);
3131       } else {
3132         tmp_sad = 0;
3133       }
3134       if (cpi->src_sad_blk_64x64 != NULL)
3135         cpi->src_sad_blk_64x64[sbi_col + sbi_row * sb_cols] = tmp_sad;
3136       if (check_light_change) {
3137         unsigned int sse, variance;
3138         variance = cpi->ppi->fn_ptr[bsize].vf(src_y, src_ystride, last_src_y,
3139                                               last_src_ystride, &sse);
3140         // Note: sse - variance = ((sum * sum) >> 12)
3141         // Detect large lighting change.
3142         if (variance < (sse >> 1) && (sse - variance) > sum_sq_thresh) {
3143           num_low_var_high_sumdiff++;
3144         }
3145       }
3146       avg_sad += tmp_sad;
3147       num_samples++;
3148       if (tmp_sad == 0) num_zero_temp_sad++;
3149       if (tmp_sad > rc->max_block_source_sad)
3150         rc->max_block_source_sad = tmp_sad;
3151 
3152       src_y += 64;
3153       last_src_y += 64;
3154     }
3155     src_y += (src_ystride << 6) - (sb_cols << 6);
3156     last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
3157   }
3158   if (check_light_change && num_samples > 0 &&
3159       num_low_var_high_sumdiff > (num_samples >> 1))
3160     light_change = 1;
3161   if (num_samples > 0) avg_sad = avg_sad / num_samples;
3162   // Set high_source_sad flag if we detect very high increase in avg_sad
3163   // between current and previous frame value(s). Use minimum threshold
3164   // for cases where there is small change from content that is completely
3165   // static.
3166   if (!light_change &&
3167       avg_sad >
3168           AOMMAX(min_thresh, (unsigned int)(rc->avg_source_sad * thresh)) &&
3169       rc->frames_since_key > 1 + cpi->svc.number_spatial_layers &&
3170       num_zero_temp_sad < 3 * (num_samples >> 2))
3171     rc->high_source_sad = 1;
3172   else
3173     rc->high_source_sad = 0;
3174   rc->avg_source_sad = (3 * rc->avg_source_sad + avg_sad) >> 2;
3175   rc->frame_source_sad = avg_sad;
3176   if (num_samples > 0)
3177     rc->percent_blocks_with_motion =
3178         ((num_samples - num_zero_temp_sad) * 100) / num_samples;
3179   // Scene detection is only on base SLO, and using full/orignal resolution.
3180   // Pass the state to the upper spatial layers.
3181   if (cpi->svc.number_spatial_layers > 1) {
3182     SVC *svc = &cpi->svc;
3183     for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
3184       int tl = svc->temporal_layer_id;
3185       const int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3186       LAYER_CONTEXT *lc = &svc->layer_context[layer];
3187       RATE_CONTROL *lrc = &lc->rc;
3188       lrc->high_source_sad = rc->high_source_sad;
3189       lrc->frame_source_sad = rc->frame_source_sad;
3190       lrc->avg_source_sad = rc->avg_source_sad;
3191       lrc->percent_blocks_with_motion = rc->percent_blocks_with_motion;
3192       lrc->max_block_source_sad = rc->max_block_source_sad;
3193     }
3194   }
3195 }
3196 
3197 /*!\brief Set the GF baseline interval for 1 pass real-time mode.
3198  *
3199  *
3200  * \ingroup rate_control
3201  * \param[in]       cpi          Top level encoder structure
3202  * \param[in]       frame_type   frame type
3203  *
3204  * \return Return GF update flag, and update the \c cpi->rc with
3205  * the next GF interval settings.
3206  */
set_gf_interval_update_onepass_rt(AV1_COMP * cpi,FRAME_TYPE frame_type)3207 static int set_gf_interval_update_onepass_rt(AV1_COMP *cpi,
3208                                              FRAME_TYPE frame_type) {
3209   RATE_CONTROL *const rc = &cpi->rc;
3210   int gf_update = 0;
3211   const int resize_pending = is_frame_resize_pending(cpi);
3212   // GF update based on frames_till_gf_update_due, also
3213   // force upddate on resize pending frame or for scene change.
3214   if ((resize_pending || rc->high_source_sad ||
3215        rc->frames_till_gf_update_due == 0) &&
3216       cpi->svc.temporal_layer_id == 0 && cpi->svc.spatial_layer_id == 0) {
3217     set_baseline_gf_interval(cpi, frame_type);
3218     gf_update = 1;
3219   }
3220   return gf_update;
3221 }
3222 
resize_reset_rc(AV1_COMP * cpi,int resize_width,int resize_height,int prev_width,int prev_height)3223 static void resize_reset_rc(AV1_COMP *cpi, int resize_width, int resize_height,
3224                             int prev_width, int prev_height) {
3225   RATE_CONTROL *const rc = &cpi->rc;
3226   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3227   SVC *const svc = &cpi->svc;
3228   int target_bits_per_frame;
3229   int active_worst_quality;
3230   int qindex;
3231   double tot_scale_change = (double)(resize_width * resize_height) /
3232                             (double)(prev_width * prev_height);
3233   // Disable the skip mv search for svc on resize frame.
3234   svc->skip_mvsearch_last = 0;
3235   svc->skip_mvsearch_gf = 0;
3236   svc->skip_mvsearch_altref = 0;
3237   // Reset buffer level to optimal, update target size.
3238   p_rc->buffer_level = p_rc->optimal_buffer_level;
3239   p_rc->bits_off_target = p_rc->optimal_buffer_level;
3240   rc->this_frame_target =
3241       av1_calc_pframe_target_size_one_pass_cbr(cpi, INTER_FRAME);
3242   target_bits_per_frame = rc->this_frame_target;
3243   if (tot_scale_change > 4.0)
3244     p_rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
3245   else if (tot_scale_change > 1.0)
3246     p_rc->avg_frame_qindex[INTER_FRAME] =
3247         (p_rc->avg_frame_qindex[INTER_FRAME] + rc->worst_quality) >> 1;
3248   active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi);
3249   qindex = av1_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
3250                              active_worst_quality, resize_width, resize_height);
3251   // If resize is down, check if projected q index is close to worst_quality,
3252   // and if so, reduce the rate correction factor (since likely can afford
3253   // lower q for resized frame).
3254   if (tot_scale_change < 1.0 && qindex > 90 * rc->worst_quality / 100)
3255     p_rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
3256   // If resize is back up: check if projected q index is too much above the
3257   // previous index, and if so, reduce the rate correction factor
3258   // (since prefer to keep q for resized frame at least closet to previous q).
3259   // Also check if projected qindex is close to previous qindex, if so
3260   // increase correction factor (to push qindex higher and avoid overshoot).
3261   if (tot_scale_change >= 1.0) {
3262     if (tot_scale_change < 4.0 &&
3263         qindex > 130 * p_rc->last_q[INTER_FRAME] / 100)
3264       p_rc->rate_correction_factors[INTER_NORMAL] *= 0.8;
3265     if (qindex <= 120 * p_rc->last_q[INTER_FRAME] / 100)
3266       p_rc->rate_correction_factors[INTER_NORMAL] *= 1.5;
3267   }
3268   if (svc->number_temporal_layers > 1) {
3269     // Apply the same rate control reset to all temporal layers.
3270     for (int tl = 0; tl < svc->number_temporal_layers; tl++) {
3271       LAYER_CONTEXT *lc = NULL;
3272       lc = &svc->layer_context[svc->spatial_layer_id *
3273                                    svc->number_temporal_layers +
3274                                tl];
3275       lc->rc.resize_state = rc->resize_state;
3276       lc->p_rc.buffer_level = lc->p_rc.optimal_buffer_level;
3277       lc->p_rc.bits_off_target = lc->p_rc.optimal_buffer_level;
3278       lc->p_rc.rate_correction_factors[INTER_NORMAL] =
3279           p_rc->rate_correction_factors[INTER_NORMAL];
3280       lc->p_rc.avg_frame_qindex[INTER_FRAME] =
3281           p_rc->avg_frame_qindex[INTER_FRAME];
3282     }
3283   }
3284 }
3285 
3286 /*!\brief ChecK for resize based on Q, for 1 pass real-time mode.
3287  *
3288  * Check if we should resize, based on average QP from past x frames.
3289  * Only allow for resize at most 1/2 scale down for now, Scaling factor
3290  * for each step may be 3/4 or 1/2.
3291  *
3292  * \ingroup rate_control
3293  * \param[in]       cpi          Top level encoder structure
3294  *
3295  * \remark Return resized width/height in \c cpi->resize_pending_params,
3296  * and update some resize counters in \c rc.
3297  */
dynamic_resize_one_pass_cbr(AV1_COMP * cpi)3298 static void dynamic_resize_one_pass_cbr(AV1_COMP *cpi) {
3299   const AV1_COMMON *const cm = &cpi->common;
3300   RATE_CONTROL *const rc = &cpi->rc;
3301   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3302   RESIZE_ACTION resize_action = NO_RESIZE;
3303   const int avg_qp_thr1 = 70;
3304   const int avg_qp_thr2 = 50;
3305   // Don't allow for resized frame to go below 160x90, resize in steps of 3/4.
3306   const int min_width = (160 * 4) / 3;
3307   const int min_height = (90 * 4) / 3;
3308   int down_size_on = 1;
3309   // Don't resize on key frame; reset the counters on key frame.
3310   if (cm->current_frame.frame_type == KEY_FRAME) {
3311     rc->resize_avg_qp = 0;
3312     rc->resize_count = 0;
3313     rc->resize_buffer_underflow = 0;
3314     return;
3315   }
3316   // No resizing down if frame size is below some limit.
3317   if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0;
3318 
3319   // Resize based on average buffer underflow and QP over some window.
3320   // Ignore samples close to key frame, since QP is usually high after key.
3321   if (cpi->rc.frames_since_key > cpi->framerate) {
3322     const int window = AOMMIN(30, (int)(2 * cpi->framerate));
3323     rc->resize_avg_qp += p_rc->last_q[INTER_FRAME];
3324     if (cpi->ppi->p_rc.buffer_level <
3325         (int)(30 * p_rc->optimal_buffer_level / 100))
3326       ++rc->resize_buffer_underflow;
3327     ++rc->resize_count;
3328     // Check for resize action every "window" frames.
3329     if (rc->resize_count >= window) {
3330       int avg_qp = rc->resize_avg_qp / rc->resize_count;
3331       // Resize down if buffer level has underflowed sufficient amount in past
3332       // window, and we are at original or 3/4 of original resolution.
3333       // Resize back up if average QP is low, and we are currently in a resized
3334       // down state, i.e. 1/2 or 3/4 of original resolution.
3335       // Currently, use a flag to turn 3/4 resizing feature on/off.
3336       if (rc->resize_buffer_underflow > (rc->resize_count >> 2) &&
3337           down_size_on) {
3338         if (rc->resize_state == THREE_QUARTER) {
3339           resize_action = DOWN_ONEHALF;
3340           rc->resize_state = ONE_HALF;
3341         } else if (rc->resize_state == ORIG) {
3342           resize_action = DOWN_THREEFOUR;
3343           rc->resize_state = THREE_QUARTER;
3344         }
3345       } else if (rc->resize_state != ORIG &&
3346                  avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
3347         if (rc->resize_state == THREE_QUARTER ||
3348             avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100) {
3349           resize_action = UP_ORIG;
3350           rc->resize_state = ORIG;
3351         } else if (rc->resize_state == ONE_HALF) {
3352           resize_action = UP_THREEFOUR;
3353           rc->resize_state = THREE_QUARTER;
3354         }
3355       }
3356       // Reset for next window measurement.
3357       rc->resize_avg_qp = 0;
3358       rc->resize_count = 0;
3359       rc->resize_buffer_underflow = 0;
3360     }
3361   }
3362   // If decision is to resize, reset some quantities, and check is we should
3363   // reduce rate correction factor,
3364   if (resize_action != NO_RESIZE) {
3365     int resize_width = cpi->oxcf.frm_dim_cfg.width;
3366     int resize_height = cpi->oxcf.frm_dim_cfg.height;
3367     int resize_scale_num = 1;
3368     int resize_scale_den = 1;
3369     if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
3370       resize_scale_num = 3;
3371       resize_scale_den = 4;
3372     } else if (resize_action == DOWN_ONEHALF) {
3373       resize_scale_num = 1;
3374       resize_scale_den = 2;
3375     }
3376     resize_width = resize_width * resize_scale_num / resize_scale_den;
3377     resize_height = resize_height * resize_scale_num / resize_scale_den;
3378     resize_reset_rc(cpi, resize_width, resize_height, cm->width, cm->height);
3379   }
3380   return;
3381 }
3382 
set_key_frame(AV1_COMP * cpi,unsigned int frame_flags)3383 static INLINE int set_key_frame(AV1_COMP *cpi, unsigned int frame_flags) {
3384   RATE_CONTROL *const rc = &cpi->rc;
3385   AV1_COMMON *const cm = &cpi->common;
3386   SVC *const svc = &cpi->svc;
3387 
3388   // Very first frame has to be key frame.
3389   if (cm->current_frame.frame_number == 0) return 1;
3390   // Set key frame if forced by frame flags.
3391   if (frame_flags & FRAMEFLAGS_KEY) return 1;
3392   if (!cpi->ppi->use_svc) {
3393     // Non-SVC
3394     if (cpi->oxcf.kf_cfg.auto_key && rc->frames_to_key == 0) return 1;
3395   } else {
3396     // SVC
3397     if (svc->spatial_layer_id == 0 &&
3398         (cpi->oxcf.kf_cfg.auto_key &&
3399          (cpi->oxcf.kf_cfg.key_freq_max == 0 ||
3400           svc->current_superframe % cpi->oxcf.kf_cfg.key_freq_max == 0)))
3401       return 1;
3402   }
3403 
3404   return 0;
3405 }
3406 
3407 // Set to true if this frame is a recovery frame, for 1 layer RPS,
3408 // and whether we should apply some boost (QP, adjust speed features, etc).
3409 // Recovery frame here means frame whose closest reference suddenly
3410 // switched from previous frame to one much further away.
3411 // TODO(marpan): Consider adding on/off flag to SVC_REF_FRAME_CONFIG to
3412 // allow more control for applications.
set_flag_rps_bias_recovery_frame(const AV1_COMP * const cpi)3413 static bool set_flag_rps_bias_recovery_frame(const AV1_COMP *const cpi) {
3414   if (cpi->ppi->rtc_ref.set_ref_frame_config &&
3415       cpi->svc.number_temporal_layers == 1 &&
3416       cpi->svc.number_spatial_layers == 1 &&
3417       cpi->ppi->rtc_ref.reference_was_previous_frame) {
3418     int min_dist = av1_svc_get_min_ref_dist(cpi);
3419     // Only consider boost for this frame if its closest reference is further
3420     // than x frames away, using x = 4 for now.
3421     if (min_dist != INT_MAX && min_dist > 4) return true;
3422   }
3423   return false;
3424 }
3425 
av1_get_one_pass_rt_params(AV1_COMP * cpi,FRAME_TYPE * const frame_type,const EncodeFrameInput * frame_input,unsigned int frame_flags)3426 void av1_get_one_pass_rt_params(AV1_COMP *cpi, FRAME_TYPE *const frame_type,
3427                                 const EncodeFrameInput *frame_input,
3428                                 unsigned int frame_flags) {
3429   RATE_CONTROL *const rc = &cpi->rc;
3430   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3431   AV1_COMMON *const cm = &cpi->common;
3432   GF_GROUP *const gf_group = &cpi->ppi->gf_group;
3433   SVC *const svc = &cpi->svc;
3434   ResizePendingParams *const resize_pending_params =
3435       &cpi->resize_pending_params;
3436   int target;
3437   const int layer =
3438       LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
3439                        svc->number_temporal_layers);
3440   if (cpi->ppi->use_svc) {
3441     av1_update_temporal_layer_framerate(cpi);
3442     av1_restore_layer_context(cpi);
3443   }
3444   cpi->ppi->rtc_ref.bias_recovery_frame = set_flag_rps_bias_recovery_frame(cpi);
3445   // Set frame type.
3446   if (set_key_frame(cpi, frame_flags)) {
3447     *frame_type = KEY_FRAME;
3448     p_rc->this_key_frame_forced =
3449         cm->current_frame.frame_number != 0 && rc->frames_to_key == 0;
3450     rc->frames_to_key = cpi->oxcf.kf_cfg.key_freq_max;
3451     p_rc->kf_boost = DEFAULT_KF_BOOST_RT;
3452     gf_group->update_type[cpi->gf_frame_index] = KF_UPDATE;
3453     gf_group->frame_type[cpi->gf_frame_index] = KEY_FRAME;
3454     gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_RESET;
3455     if (cpi->ppi->use_svc) {
3456       if (cm->current_frame.frame_number > 0)
3457         av1_svc_reset_temporal_layers(cpi, 1);
3458       svc->layer_context[layer].is_key_frame = 1;
3459     }
3460     rc->frame_number_encoded = 0;
3461     cpi->ppi->rtc_ref.non_reference_frame = 0;
3462   } else {
3463     *frame_type = INTER_FRAME;
3464     gf_group->update_type[cpi->gf_frame_index] = LF_UPDATE;
3465     gf_group->frame_type[cpi->gf_frame_index] = INTER_FRAME;
3466     gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_UPDATE;
3467     if (cpi->ppi->use_svc) {
3468       LAYER_CONTEXT *lc = &svc->layer_context[layer];
3469       lc->is_key_frame =
3470           svc->spatial_layer_id == 0
3471               ? 0
3472               : svc->layer_context[svc->temporal_layer_id].is_key_frame;
3473       // If the user is setting the reference structure with
3474       // set_ref_frame_config and did not set any references, set the
3475       // frame type to Intra-only.
3476       if (cpi->ppi->rtc_ref.set_ref_frame_config) {
3477         int no_references_set = 1;
3478         for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
3479           if (cpi->ppi->rtc_ref.reference[i]) {
3480             no_references_set = 0;
3481             break;
3482           }
3483         }
3484         // Set to intra_only_frame if no references are set.
3485         // The stream can start decoding on INTRA_ONLY_FRAME so long as the
3486         // layer with the intra_only_frame doesn't signal a reference to a slot
3487         // that hasn't been set yet.
3488         if (no_references_set) *frame_type = INTRA_ONLY_FRAME;
3489       }
3490     }
3491   }
3492   if (cpi->active_map.enabled && cpi->rc.percent_blocks_inactive == 100) {
3493     rc->frame_source_sad = 0;
3494     rc->avg_source_sad = (3 * rc->avg_source_sad + rc->frame_source_sad) >> 2;
3495     rc->percent_blocks_with_motion = 0;
3496     rc->high_source_sad = 0;
3497   } else if (cpi->sf.rt_sf.check_scene_detection &&
3498              svc->spatial_layer_id == 0) {
3499     if (rc->prev_coded_width == cm->width &&
3500         rc->prev_coded_height == cm->height) {
3501       rc_scene_detection_onepass_rt(cpi, frame_input);
3502     } else {
3503       aom_free(cpi->src_sad_blk_64x64);
3504       cpi->src_sad_blk_64x64 = NULL;
3505     }
3506   }
3507   // Check for dynamic resize, for single spatial layer for now.
3508   // For temporal layers only check on base temporal layer.
3509   if (cpi->oxcf.resize_cfg.resize_mode == RESIZE_DYNAMIC) {
3510     if (svc->number_spatial_layers == 1 && svc->temporal_layer_id == 0)
3511       dynamic_resize_one_pass_cbr(cpi);
3512     if (rc->resize_state == THREE_QUARTER) {
3513       resize_pending_params->width = (3 + cpi->oxcf.frm_dim_cfg.width * 3) >> 2;
3514       resize_pending_params->height =
3515           (3 + cpi->oxcf.frm_dim_cfg.height * 3) >> 2;
3516     } else if (rc->resize_state == ONE_HALF) {
3517       resize_pending_params->width = (1 + cpi->oxcf.frm_dim_cfg.width) >> 1;
3518       resize_pending_params->height = (1 + cpi->oxcf.frm_dim_cfg.height) >> 1;
3519     } else {
3520       resize_pending_params->width = cpi->oxcf.frm_dim_cfg.width;
3521       resize_pending_params->height = cpi->oxcf.frm_dim_cfg.height;
3522     }
3523   } else if (is_frame_resize_pending(cpi)) {
3524     resize_reset_rc(cpi, resize_pending_params->width,
3525                     resize_pending_params->height, cm->width, cm->height);
3526   }
3527   // Set the GF interval and update flag.
3528   if (!rc->rtc_external_ratectrl)
3529     set_gf_interval_update_onepass_rt(cpi, *frame_type);
3530   // Set target size.
3531   if (cpi->oxcf.rc_cfg.mode == AOM_CBR) {
3532     if (*frame_type == KEY_FRAME || *frame_type == INTRA_ONLY_FRAME) {
3533       target = av1_calc_iframe_target_size_one_pass_cbr(cpi);
3534     } else {
3535       target = av1_calc_pframe_target_size_one_pass_cbr(
3536           cpi, gf_group->update_type[cpi->gf_frame_index]);
3537     }
3538   } else {
3539     if (*frame_type == KEY_FRAME || *frame_type == INTRA_ONLY_FRAME) {
3540       target = av1_calc_iframe_target_size_one_pass_vbr(cpi);
3541     } else {
3542       target = av1_calc_pframe_target_size_one_pass_vbr(
3543           cpi, gf_group->update_type[cpi->gf_frame_index]);
3544     }
3545   }
3546   if (cpi->oxcf.rc_cfg.mode == AOM_Q)
3547     rc->active_worst_quality = cpi->oxcf.rc_cfg.cq_level;
3548 
3549   av1_rc_set_frame_target(cpi, target, cm->width, cm->height);
3550   rc->base_frame_target = target;
3551   cm->current_frame.frame_type = *frame_type;
3552   // For fixed mode SVC: if KSVC is enabled remove inter layer
3553   // prediction on spatial enhancement layer frames for frames
3554   // whose base is not KEY frame.
3555   if (cpi->ppi->use_svc && !svc->use_flexible_mode && svc->ksvc_fixed_mode &&
3556       svc->number_spatial_layers > 1 &&
3557       !svc->layer_context[layer].is_key_frame) {
3558     ExternalFlags *const ext_flags = &cpi->ext_flags;
3559     ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG;
3560   }
3561 }
3562 
3563 #define CHECK_INTER_LAYER_PRED(ref_frame)                         \
3564   ((cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) && \
3565    (av1_check_ref_is_low_spatial_res_super_frame(cpi, ref_frame)))
3566 
av1_encodedframe_overshoot_cbr(AV1_COMP * cpi,int * q)3567 int av1_encodedframe_overshoot_cbr(AV1_COMP *cpi, int *q) {
3568   AV1_COMMON *const cm = &cpi->common;
3569   PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
3570   double rate_correction_factor =
3571       cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL];
3572   const int target_size = cpi->rc.avg_frame_bandwidth;
3573   double new_correction_factor;
3574   int target_bits_per_mb;
3575   double q2;
3576   int enumerator;
3577   int inter_layer_pred_on = 0;
3578   int is_screen_content = (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN);
3579   cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0;
3580   if (cpi->svc.spatial_layer_id > 0) {
3581     // For spatial layers: check if inter-layer (spatial) prediction is used
3582     // (check if any reference is being used that is the lower spatial layer),
3583     inter_layer_pred_on = CHECK_INTER_LAYER_PRED(LAST_FRAME) ||
3584                           CHECK_INTER_LAYER_PRED(GOLDEN_FRAME) ||
3585                           CHECK_INTER_LAYER_PRED(ALTREF_FRAME);
3586   }
3587   // If inter-layer prediction is on: we expect to pull up the quality from
3588   // the lower spatial layer, so we can use a lower q.
3589   if (cpi->svc.spatial_layer_id > 0 && inter_layer_pred_on) {
3590     *q = (cpi->rc.worst_quality + *q) >> 1;
3591   } else {
3592     *q = (3 * cpi->rc.worst_quality + *q) >> 2;
3593     // For screen content use the max-q set by the user to allow for less
3594     // overshoot on slide changes.
3595     if (is_screen_content) *q = cpi->rc.worst_quality;
3596   }
3597   // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
3598   // these parameters will affect QP selection for subsequent frames. If they
3599   // have settled down to a very different (low QP) state, then not adjusting
3600   // them may cause next frame to select low QP and overshoot again.
3601   p_rc->avg_frame_qindex[INTER_FRAME] = *q;
3602   p_rc->buffer_level = p_rc->optimal_buffer_level;
3603   p_rc->bits_off_target = p_rc->optimal_buffer_level;
3604   // Reset rate under/over-shoot flags.
3605   cpi->rc.rc_1_frame = 0;
3606   cpi->rc.rc_2_frame = 0;
3607   // Adjust rate correction factor.
3608   target_bits_per_mb =
3609       (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->mi_params.MBs);
3610   // Reset rate correction factor: for now base it on target_bits_per_mb
3611   // and qp (==max_QP). This comes from the inverse computation of
3612   // av1_rc_bits_per_mb().
3613   q2 = av1_convert_qindex_to_q(*q, cm->seq_params->bit_depth);
3614   enumerator = av1_get_bpmb_enumerator(INTER_NORMAL, is_screen_content);
3615   new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
3616   if (new_correction_factor > rate_correction_factor) {
3617     rate_correction_factor =
3618         (new_correction_factor + rate_correction_factor) / 2.0;
3619     if (rate_correction_factor > MAX_BPB_FACTOR)
3620       rate_correction_factor = MAX_BPB_FACTOR;
3621     cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL] =
3622         rate_correction_factor;
3623   }
3624   // For temporal layers: reset the rate control parameters across all
3625   // temporal layers. Only do it for spatial enhancement layers when
3626   // inter_layer_pred_on is not set (off).
3627   if (cpi->svc.number_temporal_layers > 1 &&
3628       (cpi->svc.spatial_layer_id == 0 || inter_layer_pred_on == 0)) {
3629     SVC *svc = &cpi->svc;
3630     for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
3631       int sl = svc->spatial_layer_id;
3632       const int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
3633       LAYER_CONTEXT *lc = &svc->layer_context[layer];
3634       RATE_CONTROL *lrc = &lc->rc;
3635       PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc;
3636       lp_rc->avg_frame_qindex[INTER_FRAME] = *q;
3637       lp_rc->buffer_level = lp_rc->optimal_buffer_level;
3638       lp_rc->bits_off_target = lp_rc->optimal_buffer_level;
3639       lrc->rc_1_frame = 0;
3640       lrc->rc_2_frame = 0;
3641       lp_rc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
3642     }
3643   }
3644   return 1;
3645 }
3646