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