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