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