• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_mem/aom_mem.h"
21 #include "aom_ports/mem.h"
22 #include "aom_ports/system_state.h"
23 
24 #include "av1/common/alloccommon.h"
25 #include "av1/encoder/aq_cyclicrefresh.h"
26 #include "av1/common/common.h"
27 #include "av1/common/entropymode.h"
28 #include "av1/common/quant_common.h"
29 #include "av1/common/seg_common.h"
30 
31 #include "av1/encoder/encodemv.h"
32 #include "av1/encoder/encode_strategy.h"
33 #include "av1/encoder/gop_structure.h"
34 #include "av1/encoder/random.h"
35 #include "av1/encoder/ratectrl.h"
36 
37 #define USE_UNRESTRICTED_Q_IN_CQ_MODE 0
38 
39 // Max rate target for 1080P and below encodes under normal circumstances
40 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
41 #define MAX_MB_RATE 250
42 #define MAXRATE_1080P 2025000
43 
44 #define MIN_BPB_FACTOR 0.005
45 #define MAX_BPB_FACTOR 50
46 
47 #define SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO 0
48 #define SUPERRES_QADJ_PER_DENOM_KEYFRAME 2
49 #define SUPERRES_QADJ_PER_DENOM_ARFFRAME 0
50 
51 #define FRAME_OVERHEAD_BITS 200
52 #define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
53   do {                                                       \
54     switch (bit_depth) {                                     \
55       case AOM_BITS_8: name = name##_8; break;               \
56       case AOM_BITS_10: name = name##_10; break;             \
57       case AOM_BITS_12: name = name##_12; break;             \
58       default:                                               \
59         assert(0 &&                                          \
60                "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
61                " or AOM_BITS_12");                           \
62         name = NULL;                                         \
63     }                                                        \
64   } while (0)
65 
66 // Tables relating active max Q to active min Q
67 static int kf_low_motion_minq_8[QINDEX_RANGE];
68 static int kf_high_motion_minq_8[QINDEX_RANGE];
69 static int arfgf_low_motion_minq_8[QINDEX_RANGE];
70 static int arfgf_high_motion_minq_8[QINDEX_RANGE];
71 static int inter_minq_8[QINDEX_RANGE];
72 static int rtc_minq_8[QINDEX_RANGE];
73 
74 static int kf_low_motion_minq_10[QINDEX_RANGE];
75 static int kf_high_motion_minq_10[QINDEX_RANGE];
76 static int arfgf_low_motion_minq_10[QINDEX_RANGE];
77 static int arfgf_high_motion_minq_10[QINDEX_RANGE];
78 static int inter_minq_10[QINDEX_RANGE];
79 static int rtc_minq_10[QINDEX_RANGE];
80 static int kf_low_motion_minq_12[QINDEX_RANGE];
81 static int kf_high_motion_minq_12[QINDEX_RANGE];
82 static int arfgf_low_motion_minq_12[QINDEX_RANGE];
83 static int arfgf_high_motion_minq_12[QINDEX_RANGE];
84 static int inter_minq_12[QINDEX_RANGE];
85 static int rtc_minq_12[QINDEX_RANGE];
86 
87 static int gf_high = 2400;
88 static int gf_low = 300;
89 #ifdef STRICT_RC
90 static int kf_high = 3200;
91 #else
92 static int kf_high = 5000;
93 #endif
94 static int kf_low = 400;
95 
96 // How many times less pixels there are to encode given the current scaling.
97 // Temporary replacement for rcf_mult and rate_thresh_mult.
resize_rate_factor(const AV1_COMP * cpi,int width,int height)98 static double resize_rate_factor(const AV1_COMP *cpi, int width, int height) {
99   return (double)(cpi->oxcf.width * cpi->oxcf.height) / (width * height);
100 }
101 
102 // Functions to compute the active minq lookup table entries based on a
103 // formulaic approach to facilitate easier adjustment of the Q tables.
104 // The formulae were derived from computing a 3rd order polynomial best
105 // fit to the original data (after plotting real maxq vs minq (not q index))
get_minq_index(double maxq,double x3,double x2,double x1,aom_bit_depth_t bit_depth)106 static int get_minq_index(double maxq, double x3, double x2, double x1,
107                           aom_bit_depth_t bit_depth) {
108   const double minqtarget = AOMMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq);
109 
110   // Special case handling to deal with the step from q2.0
111   // down to lossless mode represented by q 1.0.
112   if (minqtarget <= 2.0) return 0;
113 
114   return av1_find_qindex(minqtarget, bit_depth, 0, QINDEX_RANGE - 1);
115 }
116 
init_minq_luts(int * kf_low_m,int * kf_high_m,int * arfgf_low,int * arfgf_high,int * inter,int * rtc,aom_bit_depth_t bit_depth)117 static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
118                            int *arfgf_high, int *inter, int *rtc,
119                            aom_bit_depth_t bit_depth) {
120   int i;
121   for (i = 0; i < QINDEX_RANGE; i++) {
122     const double maxq = av1_convert_qindex_to_q(i, bit_depth);
123     kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
124     kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);
125     arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
126     arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
127     inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
128     rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
129   }
130 }
131 
av1_rc_init_minq_luts(void)132 void av1_rc_init_minq_luts(void) {
133   init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
134                  arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
135                  inter_minq_8, rtc_minq_8, AOM_BITS_8);
136   init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
137                  arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
138                  inter_minq_10, rtc_minq_10, AOM_BITS_10);
139   init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
140                  arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
141                  inter_minq_12, rtc_minq_12, AOM_BITS_12);
142 }
143 
144 // These functions use formulaic calculations to make playing with the
145 // quantizer tables easier. If necessary they can be replaced by lookup
146 // tables if and when things settle down in the experimental bitstream
av1_convert_qindex_to_q(int qindex,aom_bit_depth_t bit_depth)147 double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) {
148   // Convert the index to a real Q value (scaled down to match old Q values)
149   switch (bit_depth) {
150     case AOM_BITS_8: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 4.0;
151     case AOM_BITS_10: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 16.0;
152     case AOM_BITS_12: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 64.0;
153     default:
154       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
155       return -1.0;
156   }
157 }
158 
av1_rc_bits_per_mb(FRAME_TYPE frame_type,int qindex,double correction_factor,aom_bit_depth_t bit_depth)159 int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
160                        double correction_factor, aom_bit_depth_t bit_depth) {
161   const double q = av1_convert_qindex_to_q(qindex, bit_depth);
162   int enumerator = frame_type == KEY_FRAME ? 2000000 : 1500000;
163 
164   assert(correction_factor <= MAX_BPB_FACTOR &&
165          correction_factor >= MIN_BPB_FACTOR);
166 
167   // q based adjustment to baseline enumerator
168   return (int)(enumerator * correction_factor / q);
169 }
170 
av1_estimate_bits_at_q(FRAME_TYPE frame_type,int q,int mbs,double correction_factor,aom_bit_depth_t bit_depth)171 int av1_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
172                            double correction_factor,
173                            aom_bit_depth_t bit_depth) {
174   const int bpm =
175       (int)(av1_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
176   return AOMMAX(FRAME_OVERHEAD_BITS,
177                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
178 }
179 
av1_rc_clamp_pframe_target_size(const AV1_COMP * const cpi,int target,FRAME_UPDATE_TYPE frame_update_type)180 int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target,
181                                     FRAME_UPDATE_TYPE frame_update_type) {
182   const RATE_CONTROL *rc = &cpi->rc;
183   const AV1EncoderConfig *oxcf = &cpi->oxcf;
184   const int min_frame_target =
185       AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
186   // Clip the frame target to the minimum setup value.
187   if (frame_update_type == OVERLAY_UPDATE ||
188       frame_update_type == INTNL_OVERLAY_UPDATE) {
189     // If there is an active ARF at this location use the minimum
190     // bits on this frame even if it is a constructed arf.
191     // The active maximum quantizer insures that an appropriate
192     // number of bits will be spent if needed for constructed ARFs.
193     target = min_frame_target;
194   } else if (target < min_frame_target) {
195     target = min_frame_target;
196   }
197 
198   // Clip the frame target to the maximum allowed value.
199   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
200   if (oxcf->rc_max_inter_bitrate_pct) {
201     const int max_rate =
202         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
203     target = AOMMIN(target, max_rate);
204   }
205 
206   return target;
207 }
208 
av1_rc_clamp_iframe_target_size(const AV1_COMP * const cpi,int target)209 int av1_rc_clamp_iframe_target_size(const AV1_COMP *const cpi, int target) {
210   const RATE_CONTROL *rc = &cpi->rc;
211   const AV1EncoderConfig *oxcf = &cpi->oxcf;
212   if (oxcf->rc_max_intra_bitrate_pct) {
213     const int max_rate =
214         rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
215     target = AOMMIN(target, max_rate);
216   }
217   if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
218   return target;
219 }
220 
221 // Update the buffer level for higher temporal layers, given the encoded current
222 // temporal layer.
update_layer_buffer_level(SVC * svc,int encoded_frame_size)223 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
224   const int current_temporal_layer = svc->temporal_layer_id;
225   for (int i = current_temporal_layer + 1; i < svc->number_temporal_layers;
226        ++i) {
227     const int layer =
228         LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
229     LAYER_CONTEXT *lc = &svc->layer_context[layer];
230     RATE_CONTROL *lrc = &lc->rc;
231     lrc->bits_off_target +=
232         (int)(lc->target_bandwidth / lc->framerate) - encoded_frame_size;
233     // Clip buffer level to maximum buffer size for the layer.
234     lrc->bits_off_target =
235         AOMMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
236     lrc->buffer_level = lrc->bits_off_target;
237   }
238 }
239 // Update the buffer level: leaky bucket model.
update_buffer_level(AV1_COMP * cpi,int encoded_frame_size)240 static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) {
241   const AV1_COMMON *const cm = &cpi->common;
242   RATE_CONTROL *const rc = &cpi->rc;
243 
244   // Non-viewable frames are a special case and are treated as pure overhead.
245   if (!cm->show_frame)
246     rc->bits_off_target -= encoded_frame_size;
247   else
248     rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
249 
250   // Clip the buffer level to the maximum specified buffer size.
251   rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
252   rc->buffer_level = rc->bits_off_target;
253 
254   if (cpi->use_svc) update_layer_buffer_level(&cpi->svc, encoded_frame_size);
255 }
256 
av1_rc_get_default_min_gf_interval(int width,int height,double framerate)257 int av1_rc_get_default_min_gf_interval(int width, int height,
258                                        double framerate) {
259   // Assume we do not need any constraint lower than 4K 20 fps
260   static const double factor_safe = 3840 * 2160 * 20.0;
261   const double factor = width * height * framerate;
262   const int default_interval =
263       clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
264 
265   if (factor <= factor_safe)
266     return default_interval;
267   else
268     return AOMMAX(default_interval,
269                   (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
270   // Note this logic makes:
271   // 4K24: 5
272   // 4K30: 6
273   // 4K60: 12
274 }
275 
av1_rc_get_default_max_gf_interval(double framerate,int min_gf_interval)276 int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
277   int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
278   interval += (interval & 0x01);  // Round to even value
279   interval = AOMMAX(MAX_GF_INTERVAL, interval);
280   return AOMMAX(interval, min_gf_interval);
281 }
282 
av1_rc_init(const AV1EncoderConfig * oxcf,int pass,RATE_CONTROL * rc)283 void av1_rc_init(const AV1EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
284   int i;
285 
286   if (pass == 0 && oxcf->rc_mode == AOM_CBR) {
287     rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
288     rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
289   } else {
290     rc->avg_frame_qindex[KEY_FRAME] =
291         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
292     rc->avg_frame_qindex[INTER_FRAME] =
293         (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
294   }
295 
296   rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
297   rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
298 
299   rc->buffer_level = rc->starting_buffer_level;
300   rc->bits_off_target = rc->starting_buffer_level;
301 
302   rc->rolling_target_bits = rc->avg_frame_bandwidth;
303   rc->rolling_actual_bits = rc->avg_frame_bandwidth;
304   rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
305   rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
306 
307   rc->total_actual_bits = 0;
308   rc->total_target_bits = 0;
309   rc->total_target_vs_actual = 0;
310 
311   rc->frames_since_key = 8;  // Sensible default for first frame.
312   rc->this_key_frame_forced = 0;
313   rc->next_key_frame_forced = 0;
314   rc->source_alt_ref_pending = 0;
315   rc->source_alt_ref_active = 0;
316 
317   rc->frames_till_gf_update_due = 0;
318   rc->ni_av_qi = oxcf->worst_allowed_q;
319   rc->ni_tot_qi = 0;
320   rc->ni_frames = 0;
321 
322   rc->tot_q = 0.0;
323   rc->avg_q = av1_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
324 
325   for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
326     rc->rate_correction_factors[i] = 0.7;
327   }
328   rc->rate_correction_factors[KF_STD] = 1.0;
329   rc->min_gf_interval = oxcf->min_gf_interval;
330   rc->max_gf_interval = oxcf->max_gf_interval;
331   if (rc->min_gf_interval == 0)
332     rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
333         oxcf->width, oxcf->height, oxcf->init_framerate);
334   if (rc->max_gf_interval == 0)
335     rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
336         oxcf->init_framerate, rc->min_gf_interval);
337   rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
338 }
339 
av1_rc_drop_frame(AV1_COMP * cpi)340 int av1_rc_drop_frame(AV1_COMP *cpi) {
341   const AV1EncoderConfig *oxcf = &cpi->oxcf;
342   RATE_CONTROL *const rc = &cpi->rc;
343 
344   if (!oxcf->drop_frames_water_mark) {
345     return 0;
346   } else {
347     if (rc->buffer_level < 0) {
348       // Always drop if buffer is below 0.
349       return 1;
350     } else {
351       // If buffer is below drop_mark, for now just drop every other frame
352       // (starting with the next frame) until it increases back over drop_mark.
353       int drop_mark =
354           (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
355       if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
356         --rc->decimation_factor;
357       } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
358         rc->decimation_factor = 1;
359       }
360       if (rc->decimation_factor > 0) {
361         if (rc->decimation_count > 0) {
362           --rc->decimation_count;
363           return 1;
364         } else {
365           rc->decimation_count = rc->decimation_factor;
366           return 0;
367         }
368       } else {
369         rc->decimation_count = 0;
370         return 0;
371       }
372     }
373   }
374 }
375 
adjust_q_cbr(const AV1_COMP * cpi,int q,int active_worst_quality)376 static int adjust_q_cbr(const AV1_COMP *cpi, int q, int active_worst_quality) {
377   const RATE_CONTROL *const rc = &cpi->rc;
378   const AV1_COMMON *const cm = &cpi->common;
379   const int max_delta = 16;
380   const int change_avg_frame_bandwidth =
381       abs(rc->avg_frame_bandwidth - rc->prev_avg_frame_bandwidth) >
382       0.1 * (rc->avg_frame_bandwidth);
383   // If resolution changes or avg_frame_bandwidth significantly changed,
384   // then set this flag to indicate change in target bits per macroblock.
385   const int change_target_bits_mb =
386       cm->prev_frame &&
387       (cm->width != cm->prev_frame->width ||
388        cm->height != cm->prev_frame->height || change_avg_frame_bandwidth);
389   // Apply some control/clamp to QP under certain conditions.
390   if (cm->current_frame.frame_type != KEY_FRAME && !cpi->use_svc &&
391       rc->frames_since_key > 1 && !change_target_bits_mb &&
392       (!cpi->oxcf.gf_cbr_boost_pct ||
393        !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame))) {
394     // Make sure q is between oscillating Qs to prevent resonance.
395     if (rc->rc_1_frame * rc->rc_2_frame == -1 &&
396         rc->q_1_frame != rc->q_2_frame) {
397       q = clamp(q, AOMMIN(rc->q_1_frame, rc->q_2_frame),
398                 AOMMAX(rc->q_1_frame, rc->q_2_frame));
399     }
400     // Limit the decrease in Q from previous frame.
401     if (rc->q_1_frame - q > max_delta) q = rc->q_1_frame - max_delta;
402   }
403   // For single spatial layer: if resolution has increased push q closer
404   // to the active_worst to avoid excess overshoot.
405   if (cpi->svc.number_spatial_layers <= 1 && cm->prev_frame &&
406       (cm->width * cm->height >
407        1.5 * cm->prev_frame->width * cm->prev_frame->height))
408     q = (q + active_worst_quality) >> 1;
409   return AOMMAX(AOMMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality);
410 }
411 
412 static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = {
413   KF_STD,        // KF_UPDATE
414   INTER_NORMAL,  // LF_UPDATE
415   GF_ARF_STD,    // GF_UPDATE
416   GF_ARF_STD,    // ARF_UPDATE
417   INTER_NORMAL,  // OVERLAY_UPDATE
418   INTER_NORMAL,  // INTNL_OVERLAY_UPDATE
419   GF_ARF_LOW,    // INTNL_ARF_UPDATE
420 };
421 
get_rate_factor_level(const GF_GROUP * const gf_group)422 static RATE_FACTOR_LEVEL get_rate_factor_level(const GF_GROUP *const gf_group) {
423   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
424   assert(update_type < FRAME_UPDATE_TYPES);
425   return rate_factor_levels[update_type];
426 }
427 
get_rate_correction_factor(const AV1_COMP * cpi,int width,int height)428 static double get_rate_correction_factor(const AV1_COMP *cpi, int width,
429                                          int height) {
430   const RATE_CONTROL *const rc = &cpi->rc;
431   double rcf;
432 
433   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
434     rcf = rc->rate_correction_factors[KF_STD];
435   } else if (is_stat_consumption_stage(cpi)) {
436     const RATE_FACTOR_LEVEL rf_lvl = get_rate_factor_level(&cpi->gf_group);
437     rcf = rc->rate_correction_factors[rf_lvl];
438   } else {
439     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
440         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
441         (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
442       rcf = rc->rate_correction_factors[GF_ARF_STD];
443     else
444       rcf = rc->rate_correction_factors[INTER_NORMAL];
445   }
446   rcf *= resize_rate_factor(cpi, width, height);
447   return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
448 }
449 
set_rate_correction_factor(AV1_COMP * cpi,double factor,int width,int height)450 static void set_rate_correction_factor(AV1_COMP *cpi, double factor, int width,
451                                        int height) {
452   RATE_CONTROL *const rc = &cpi->rc;
453 
454   // Normalize RCF to account for the size-dependent scaling factor.
455   factor /= resize_rate_factor(cpi, width, height);
456 
457   factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
458 
459   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
460     rc->rate_correction_factors[KF_STD] = factor;
461   } else if (is_stat_consumption_stage(cpi)) {
462     const RATE_FACTOR_LEVEL rf_lvl = get_rate_factor_level(&cpi->gf_group);
463     rc->rate_correction_factors[rf_lvl] = factor;
464   } else {
465     if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
466         !rc->is_src_frame_alt_ref && !cpi->use_svc &&
467         (cpi->oxcf.rc_mode != AOM_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
468       rc->rate_correction_factors[GF_ARF_STD] = factor;
469     else
470       rc->rate_correction_factors[INTER_NORMAL] = factor;
471   }
472 }
473 
av1_rc_update_rate_correction_factors(AV1_COMP * cpi,int width,int height)474 void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int width,
475                                            int height) {
476   const AV1_COMMON *const cm = &cpi->common;
477   int correction_factor = 100;
478   double rate_correction_factor =
479       get_rate_correction_factor(cpi, width, height);
480   double adjustment_limit;
481   const int MBs = av1_get_MBs(width, height);
482 
483   int projected_size_based_on_q = 0;
484 
485   // Do not update the rate factors for arf overlay frames.
486   if (cpi->rc.is_src_frame_alt_ref) return;
487 
488   // Clear down mmx registers to allow floating point in what follows
489   aom_clear_system_state();
490 
491   // Work out how big we would have expected the frame to be at this Q given
492   // the current correction factor.
493   // Stay in double to avoid int overflow when values are large
494   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
495     projected_size_based_on_q =
496         av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
497   } else {
498     projected_size_based_on_q = av1_estimate_bits_at_q(
499         cm->current_frame.frame_type, cm->quant_params.base_qindex, MBs,
500         rate_correction_factor, cm->seq_params.bit_depth);
501   }
502   // Work out a size correction factor.
503   if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
504     correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
505                               projected_size_based_on_q);
506 
507   // More heavily damped adjustment used if we have been oscillating either side
508   // of target.
509   if (correction_factor > 0) {
510     adjustment_limit =
511         0.25 + 0.5 * AOMMIN(1, fabs(log10(0.01 * correction_factor)));
512   } else {
513     adjustment_limit = 0.75;
514   }
515 
516   cpi->rc.q_2_frame = cpi->rc.q_1_frame;
517   cpi->rc.q_1_frame = cm->quant_params.base_qindex;
518   cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
519   if (correction_factor > 110)
520     cpi->rc.rc_1_frame = -1;
521   else if (correction_factor < 90)
522     cpi->rc.rc_1_frame = 1;
523   else
524     cpi->rc.rc_1_frame = 0;
525 
526   if (correction_factor > 102) {
527     // We are not already at the worst allowable quality
528     correction_factor =
529         (int)(100 + ((correction_factor - 100) * adjustment_limit));
530     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
531     // Keep rate_correction_factor within limits
532     if (rate_correction_factor > MAX_BPB_FACTOR)
533       rate_correction_factor = MAX_BPB_FACTOR;
534   } else if (correction_factor < 99) {
535     // We are not already at the best allowable quality
536     correction_factor =
537         (int)(100 - ((100 - correction_factor) * adjustment_limit));
538     rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
539 
540     // Keep rate_correction_factor within limits
541     if (rate_correction_factor < MIN_BPB_FACTOR)
542       rate_correction_factor = MIN_BPB_FACTOR;
543   }
544 
545   set_rate_correction_factor(cpi, rate_correction_factor, width, height);
546 }
547 
548 // Calculate rate for the given 'q'.
get_bits_per_mb(const AV1_COMP * cpi,int use_cyclic_refresh,double correction_factor,int q)549 static int get_bits_per_mb(const AV1_COMP *cpi, int use_cyclic_refresh,
550                            double correction_factor, int q) {
551   const AV1_COMMON *const cm = &cpi->common;
552   return use_cyclic_refresh
553              ? av1_cyclic_refresh_rc_bits_per_mb(cpi, q, correction_factor)
554              : av1_rc_bits_per_mb(cm->current_frame.frame_type, q,
555                                   correction_factor, cm->seq_params.bit_depth);
556 }
557 
558 // Similar to find_qindex_by_rate() function in ratectrl.c, but returns the q
559 // index with rate just above or below the desired rate, depending on which of
560 // the two rates is closer to the desired rate.
561 // Also, respects the selected aq_mode when computing the rate.
find_closest_qindex_by_rate(int desired_bits_per_mb,const AV1_COMP * cpi,double correction_factor,int best_qindex,int worst_qindex)562 static int find_closest_qindex_by_rate(int desired_bits_per_mb,
563                                        const AV1_COMP *cpi,
564                                        double correction_factor,
565                                        int best_qindex, int worst_qindex) {
566   const int use_cyclic_refresh = cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
567                                  cpi->cyclic_refresh->apply_cyclic_refresh;
568 
569   // Find 'qindex' based on 'desired_bits_per_mb'.
570   assert(best_qindex <= worst_qindex);
571   int low = best_qindex;
572   int high = worst_qindex;
573   while (low < high) {
574     const int mid = (low + high) >> 1;
575     const int mid_bits_per_mb =
576         get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, mid);
577     if (mid_bits_per_mb > desired_bits_per_mb) {
578       low = mid + 1;
579     } else {
580       high = mid;
581     }
582   }
583   assert(low == high);
584 
585   // Calculate rate difference of this q index from the desired rate.
586   const int curr_q = low;
587   const int curr_bits_per_mb =
588       get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, curr_q);
589   const int curr_bit_diff = (curr_bits_per_mb <= desired_bits_per_mb)
590                                 ? desired_bits_per_mb - curr_bits_per_mb
591                                 : INT_MAX;
592   assert((curr_bit_diff != INT_MAX && curr_bit_diff >= 0) ||
593          curr_q == worst_qindex);
594 
595   // Calculate rate difference for previous q index too.
596   const int prev_q = curr_q - 1;
597   int prev_bit_diff;
598   if (curr_bit_diff == INT_MAX || curr_q == best_qindex) {
599     prev_bit_diff = INT_MAX;
600   } else {
601     const int prev_bits_per_mb =
602         get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, prev_q);
603     assert(prev_bits_per_mb > desired_bits_per_mb);
604     prev_bit_diff = prev_bits_per_mb - desired_bits_per_mb;
605   }
606 
607   // Pick one of the two q indices, depending on which one has rate closer to
608   // the desired rate.
609   return (curr_bit_diff <= prev_bit_diff) ? curr_q : prev_q;
610 }
611 
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)612 int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
613                       int active_best_quality, int active_worst_quality,
614                       int width, int height) {
615   const int MBs = av1_get_MBs(width, height);
616   const double correction_factor =
617       get_rate_correction_factor(cpi, width, height);
618   const int target_bits_per_mb =
619       (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / MBs);
620 
621   int q =
622       find_closest_qindex_by_rate(target_bits_per_mb, cpi, correction_factor,
623                                   active_best_quality, active_worst_quality);
624   if (cpi->oxcf.rc_mode == AOM_CBR && has_no_stats_stage(cpi))
625     return adjust_q_cbr(cpi, q, active_worst_quality);
626 
627   return q;
628 }
629 
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)630 static int get_active_quality(int q, int gfu_boost, int low, int high,
631                               int *low_motion_minq, int *high_motion_minq) {
632   if (gfu_boost > high) {
633     return low_motion_minq[q];
634   } else if (gfu_boost < low) {
635     return high_motion_minq[q];
636   } else {
637     const int gap = high - low;
638     const int offset = high - gfu_boost;
639     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
640     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
641     return low_motion_minq[q] + adjustment;
642   }
643 }
644 
get_kf_active_quality(const RATE_CONTROL * const rc,int q,aom_bit_depth_t bit_depth)645 static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
646                                  aom_bit_depth_t bit_depth) {
647   int *kf_low_motion_minq;
648   int *kf_high_motion_minq;
649   ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
650   ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
651   return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
652                             kf_low_motion_minq, kf_high_motion_minq);
653 }
654 
get_gf_active_quality(const RATE_CONTROL * const rc,int q,aom_bit_depth_t bit_depth)655 static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
656                                  aom_bit_depth_t bit_depth) {
657   int *arfgf_low_motion_minq;
658   int *arfgf_high_motion_minq;
659   ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
660   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
661   return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
662                             arfgf_low_motion_minq, arfgf_high_motion_minq);
663 }
664 
get_gf_high_motion_quality(int q,aom_bit_depth_t bit_depth)665 static int get_gf_high_motion_quality(int q, aom_bit_depth_t bit_depth) {
666   int *arfgf_high_motion_minq;
667   ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
668   return arfgf_high_motion_minq[q];
669 }
670 
calc_active_worst_quality_one_pass_vbr(const AV1_COMP * cpi)671 static int calc_active_worst_quality_one_pass_vbr(const AV1_COMP *cpi) {
672   const RATE_CONTROL *const rc = &cpi->rc;
673   const unsigned int curr_frame = cpi->common.current_frame.frame_number;
674   int active_worst_quality;
675 
676   if (cpi->common.current_frame.frame_type == KEY_FRAME) {
677     active_worst_quality =
678         curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2;
679   } else {
680     if (!rc->is_src_frame_alt_ref &&
681         (cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
682          cpi->refresh_alt_ref_frame)) {
683       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
684                                              : rc->last_q[INTER_FRAME];
685     } else {
686       active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
687                                              : rc->last_q[INTER_FRAME] * 2;
688     }
689   }
690   return AOMMIN(active_worst_quality, rc->worst_quality);
691 }
692 
693 // Adjust active_worst_quality level based on buffer level.
calc_active_worst_quality_one_pass_cbr(const AV1_COMP * cpi)694 static int calc_active_worst_quality_one_pass_cbr(const AV1_COMP *cpi) {
695   // Adjust active_worst_quality: If buffer is above the optimal/target level,
696   // bring active_worst_quality down depending on fullness of buffer.
697   // If buffer is below the optimal level, let the active_worst_quality go from
698   // ambient Q (at buffer = optimal level) to worst_quality level
699   // (at buffer = critical level).
700   const AV1_COMMON *const cm = &cpi->common;
701   const RATE_CONTROL *rc = &cpi->rc;
702   // Buffer level below which we push active_worst to worst_quality.
703   int64_t critical_level = rc->optimal_buffer_level >> 3;
704   int64_t buff_lvl_step = 0;
705   int adjustment = 0;
706   int active_worst_quality;
707   int ambient_qp;
708   if (cm->current_frame.frame_type == KEY_FRAME) return rc->worst_quality;
709   // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
710   // for the first few frames following key frame. These are both initialized
711   // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
712   // So for first few frames following key, the qp of that key frame is weighted
713   // into the active_worst_quality setting.
714   ambient_qp = (cm->current_frame.frame_number < 5)
715                    ? AOMMIN(rc->avg_frame_qindex[INTER_FRAME],
716                             rc->avg_frame_qindex[KEY_FRAME])
717                    : rc->avg_frame_qindex[INTER_FRAME];
718   active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4);
719   if (rc->buffer_level > rc->optimal_buffer_level) {
720     // Adjust down.
721     // Maximum limit for down adjustment, ~30%.
722     int max_adjustment_down = active_worst_quality / 3;
723     if (max_adjustment_down) {
724       buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
725                        max_adjustment_down);
726       if (buff_lvl_step)
727         adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
728                            buff_lvl_step);
729       active_worst_quality -= adjustment;
730     }
731   } else if (rc->buffer_level > critical_level) {
732     // Adjust up from ambient Q.
733     if (critical_level) {
734       buff_lvl_step = (rc->optimal_buffer_level - critical_level);
735       if (buff_lvl_step) {
736         adjustment = (int)((rc->worst_quality - ambient_qp) *
737                            (rc->optimal_buffer_level - rc->buffer_level) /
738                            buff_lvl_step);
739       }
740       active_worst_quality = ambient_qp + adjustment;
741     }
742   } else {
743     // Set to worst_quality if buffer is below critical level.
744     active_worst_quality = rc->worst_quality;
745   }
746   return active_worst_quality;
747 }
748 
rc_pick_q_and_bounds_one_pass_cbr(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)749 static int rc_pick_q_and_bounds_one_pass_cbr(const AV1_COMP *cpi, int width,
750                                              int height, int *bottom_index,
751                                              int *top_index) {
752   const AV1_COMMON *const cm = &cpi->common;
753   const RATE_CONTROL *const rc = &cpi->rc;
754   const CurrentFrame *const current_frame = &cm->current_frame;
755   int active_best_quality;
756   int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
757   int q;
758   int *rtc_minq;
759   const int bit_depth = cm->seq_params.bit_depth;
760   ASSIGN_MINQ_TABLE(bit_depth, rtc_minq);
761 
762   if (frame_is_intra_only(cm)) {
763     active_best_quality = rc->best_quality;
764     // Handle the special case for key frames forced when we have reached
765     // the maximum key frame interval. Here force the Q to a range
766     // based on the ambient Q to reduce the risk of popping.
767     if (rc->this_key_frame_forced) {
768       int qindex = rc->last_boosted_qindex;
769       double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
770       int delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
771                                             (last_boosted_q * 0.75), bit_depth);
772       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
773     } else if (current_frame->frame_number > 0) {
774       // not first frame of one pass and kf_boost is set
775       double q_adj_factor = 1.0;
776       double q_val;
777 
778       active_best_quality =
779           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME], bit_depth);
780 
781       // Allow somewhat lower kf minq with small image formats.
782       if ((width * height) <= (352 * 288)) {
783         q_adj_factor -= 0.25;
784       }
785 
786       // Convert the adjustment factor to a qindex delta
787       // on active_best_quality.
788       q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
789       active_best_quality +=
790           av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
791     }
792   } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
793              cpi->oxcf.gf_cbr_boost_pct &&
794              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
795     // Use the lower of active_worst_quality and recent
796     // average Q as basis for GF/ARF best Q limit unless last frame was
797     // a key frame.
798     if (rc->frames_since_key > 1 &&
799         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
800       q = rc->avg_frame_qindex[INTER_FRAME];
801     } else {
802       q = active_worst_quality;
803     }
804     active_best_quality = get_gf_active_quality(rc, q, bit_depth);
805   } else {
806     // Use the lower of active_worst_quality and recent/average Q.
807     if (current_frame->frame_number > 1) {
808       if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
809         active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
810       else
811         active_best_quality = rtc_minq[active_worst_quality];
812     } else {
813       if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
814         active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
815       else
816         active_best_quality = rtc_minq[active_worst_quality];
817     }
818   }
819 
820   // Clip the active best and worst quality values to limits
821   active_best_quality =
822       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
823   active_worst_quality =
824       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
825 
826   *top_index = active_worst_quality;
827   *bottom_index = active_best_quality;
828 
829   // Limit Q range for the adaptive loop.
830   if (current_frame->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
831       !(current_frame->frame_number == 0)) {
832     int qdelta = 0;
833     aom_clear_system_state();
834     qdelta = av1_compute_qdelta_by_rate(&cpi->rc, current_frame->frame_type,
835                                         active_worst_quality, 2.0, bit_depth);
836     *top_index = active_worst_quality + qdelta;
837     *top_index = AOMMAX(*top_index, *bottom_index);
838   }
839 
840   // Special case code to try and match quality with forced key frames
841   if (current_frame->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
842     q = rc->last_boosted_qindex;
843   } else {
844     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
845                           active_worst_quality, width, height);
846     if (q > *top_index) {
847       // Special case when we are targeting the max allowed rate
848       if (rc->this_frame_target >= rc->max_frame_bandwidth)
849         *top_index = q;
850       else
851         q = *top_index;
852     }
853   }
854 
855   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
856   assert(*bottom_index <= rc->worst_quality &&
857          *bottom_index >= rc->best_quality);
858   assert(q <= rc->worst_quality && q >= rc->best_quality);
859   return q;
860 }
861 
gf_group_pyramid_level(const GF_GROUP * gf_group,int gf_index)862 static int gf_group_pyramid_level(const GF_GROUP *gf_group, int gf_index) {
863   return gf_group->layer_depth[gf_index];
864 }
865 
get_active_cq_level(const RATE_CONTROL * rc,const AV1EncoderConfig * const oxcf,int intra_only,SUPERRES_MODE superres_mode,int superres_denom)866 static int get_active_cq_level(const RATE_CONTROL *rc,
867                                const AV1EncoderConfig *const oxcf,
868                                int intra_only, SUPERRES_MODE superres_mode,
869                                int superres_denom) {
870   static const double cq_adjust_threshold = 0.1;
871   int active_cq_level = oxcf->cq_level;
872   (void)intra_only;
873   if (oxcf->rc_mode == AOM_CQ || oxcf->rc_mode == AOM_Q) {
874     // printf("Superres %d %d %d = %d\n", superres_denom, intra_only,
875     //        rc->frames_to_key, !(intra_only && rc->frames_to_key <= 1));
876     if ((superres_mode == SUPERRES_QTHRESH || superres_mode == SUPERRES_AUTO) &&
877         superres_denom != SCALE_NUMERATOR) {
878       int mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO;
879       if (intra_only && rc->frames_to_key <= 1) {
880         mult = 0;
881       } else if (intra_only) {
882         mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME;
883       } else {
884         mult = SUPERRES_QADJ_PER_DENOM_ARFFRAME;
885       }
886       active_cq_level = AOMMAX(
887           active_cq_level - ((superres_denom - SCALE_NUMERATOR) * mult), 0);
888     }
889   }
890   if (oxcf->rc_mode == AOM_CQ && rc->total_target_bits > 0) {
891     const double x = (double)rc->total_actual_bits / rc->total_target_bits;
892     if (x < cq_adjust_threshold) {
893       active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
894     }
895   }
896   return active_cq_level;
897 }
898 
get_q_using_fixed_offsets(const AV1EncoderConfig * const oxcf,const RATE_CONTROL * const rc,const GF_GROUP * const gf_group,int gf_index,int cq_level,int bit_depth)899 static int get_q_using_fixed_offsets(const AV1EncoderConfig *const oxcf,
900                                      const RATE_CONTROL *const rc,
901                                      const GF_GROUP *const gf_group,
902                                      int gf_index, int cq_level,
903                                      int bit_depth) {
904   assert(oxcf->use_fixed_qp_offsets);
905   assert(oxcf->rc_mode == AOM_Q);
906   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_index];
907 
908   int offset_idx = -1;
909   if (update_type == KF_UPDATE) {
910     if (rc->frames_to_key == 1) {
911       // Image / intra-only coding: ignore offsets.
912       return cq_level;
913     }
914     offset_idx = 0;
915   } else if (update_type == ARF_UPDATE || update_type == GF_UPDATE) {
916     offset_idx = 1;
917   } else if (update_type == INTNL_ARF_UPDATE) {
918     offset_idx =
919         AOMMIN(gf_group->layer_depth[gf_index], FIXED_QP_OFFSET_COUNT - 1);
920   } else {  // Leaf level / overlay frame.
921     assert(update_type == LF_UPDATE || update_type == OVERLAY_UPDATE ||
922            update_type == INTNL_OVERLAY_UPDATE);
923     return cq_level;  // Directly Return worst quality allowed.
924   }
925   assert(offset_idx >= 0 && offset_idx < FIXED_QP_OFFSET_COUNT);
926   assert(oxcf->fixed_qp_offsets[offset_idx] >= 0);
927 
928   // Get qindex offset, by first converting to 'q' and then back.
929   const double q_val_orig = av1_convert_qindex_to_q(cq_level, bit_depth);
930   const double q_val_target =
931       AOMMAX(q_val_orig - oxcf->fixed_qp_offsets[offset_idx], 0.0);
932   const int delta_qindex =
933       av1_compute_qdelta(rc, q_val_orig, q_val_target, bit_depth);
934   return AOMMAX(cq_level + delta_qindex, 0);
935 }
936 
rc_pick_q_and_bounds_one_pass_vbr(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)937 static int rc_pick_q_and_bounds_one_pass_vbr(const AV1_COMP *cpi, int width,
938                                              int height, int *bottom_index,
939                                              int *top_index) {
940   const AV1_COMMON *const cm = &cpi->common;
941   const RATE_CONTROL *const rc = &cpi->rc;
942   const CurrentFrame *const current_frame = &cm->current_frame;
943   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
944   const int cq_level =
945       get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode,
946                           cm->superres_scale_denominator);
947   const int bit_depth = cm->seq_params.bit_depth;
948 
949   if (oxcf->use_fixed_qp_offsets) {
950     return get_q_using_fixed_offsets(oxcf, rc, &cpi->gf_group,
951                                      cpi->gf_group.index, cq_level, bit_depth);
952   }
953 
954   int active_best_quality;
955   int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
956   int q;
957   int *inter_minq;
958   ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
959 
960   if (frame_is_intra_only(cm)) {
961     if (oxcf->rc_mode == AOM_Q) {
962       const int qindex = cq_level;
963       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
964       const int delta_qindex =
965           av1_compute_qdelta(rc, q_val, q_val * 0.25, bit_depth);
966       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
967     } else if (rc->this_key_frame_forced) {
968       const int qindex = rc->last_boosted_qindex;
969       const double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
970       const int delta_qindex = av1_compute_qdelta(
971           rc, last_boosted_q, last_boosted_q * 0.75, bit_depth);
972       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
973     } else {  // not first frame of one pass and kf_boost is set
974       double q_adj_factor = 1.0;
975 
976       active_best_quality =
977           get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME], bit_depth);
978 
979       // Allow somewhat lower kf minq with small image formats.
980       if ((width * height) <= (352 * 288)) {
981         q_adj_factor -= 0.25;
982       }
983 
984       // Convert the adjustment factor to a qindex delta on active_best_quality.
985       {
986         const double q_val =
987             av1_convert_qindex_to_q(active_best_quality, bit_depth);
988         active_best_quality +=
989             av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
990       }
991     }
992   } else if (!rc->is_src_frame_alt_ref &&
993              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
994     // Use the lower of active_worst_quality and recent
995     // average Q as basis for GF/ARF best Q limit unless last frame was
996     // a key frame.
997     q = (rc->frames_since_key > 1 &&
998          rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
999             ? rc->avg_frame_qindex[INTER_FRAME]
1000             : rc->avg_frame_qindex[KEY_FRAME];
1001     // For constrained quality dont allow Q less than the cq level
1002     if (oxcf->rc_mode == AOM_CQ) {
1003       if (q < cq_level) q = cq_level;
1004       active_best_quality = get_gf_active_quality(rc, q, bit_depth);
1005       // Constrained quality use slightly lower active best.
1006       active_best_quality = active_best_quality * 15 / 16;
1007     } else if (oxcf->rc_mode == AOM_Q) {
1008       const int qindex = cq_level;
1009       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1010       const int delta_qindex =
1011           (cpi->refresh_alt_ref_frame)
1012               ? av1_compute_qdelta(rc, q_val, q_val * 0.40, bit_depth)
1013               : av1_compute_qdelta(rc, q_val, q_val * 0.50, bit_depth);
1014       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1015     } else {
1016       active_best_quality = get_gf_active_quality(rc, q, bit_depth);
1017     }
1018   } else {
1019     if (oxcf->rc_mode == AOM_Q) {
1020       const int qindex = cq_level;
1021       const double q_val = av1_convert_qindex_to_q(qindex, bit_depth);
1022       const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
1023                                                      0.70, 1.0, 0.85, 1.0 };
1024       const int delta_qindex = av1_compute_qdelta(
1025           rc, q_val,
1026           q_val * delta_rate[current_frame->frame_number % FIXED_GF_INTERVAL],
1027           bit_depth);
1028       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1029     } else {
1030       // Use the lower of active_worst_quality and recent/average Q.
1031       active_best_quality = (current_frame->frame_number > 1)
1032                                 ? inter_minq[rc->avg_frame_qindex[INTER_FRAME]]
1033                                 : inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
1034       // For the constrained quality mode we don't want
1035       // q to fall below the cq level.
1036       if ((oxcf->rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1037         active_best_quality = cq_level;
1038       }
1039     }
1040   }
1041 
1042   // Clip the active best and worst quality values to limits
1043   active_best_quality =
1044       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1045   active_worst_quality =
1046       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1047 
1048   *top_index = active_worst_quality;
1049   *bottom_index = active_best_quality;
1050 
1051   // Limit Q range for the adaptive loop.
1052   {
1053     int qdelta = 0;
1054     aom_clear_system_state();
1055     if (current_frame->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
1056         !(current_frame->frame_number == 0)) {
1057       qdelta = av1_compute_qdelta_by_rate(&cpi->rc, current_frame->frame_type,
1058                                           active_worst_quality, 2.0, bit_depth);
1059     } else if (!rc->is_src_frame_alt_ref &&
1060                (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
1061       qdelta =
1062           av1_compute_qdelta_by_rate(&cpi->rc, current_frame->frame_type,
1063                                      active_worst_quality, 1.75, bit_depth);
1064     }
1065     *top_index = active_worst_quality + qdelta;
1066     *top_index = AOMMAX(*top_index, *bottom_index);
1067   }
1068 
1069   if (oxcf->rc_mode == AOM_Q) {
1070     q = active_best_quality;
1071     // Special case code to try and match quality with forced key frames
1072   } else if ((current_frame->frame_type == KEY_FRAME) &&
1073              rc->this_key_frame_forced) {
1074     q = rc->last_boosted_qindex;
1075   } else {
1076     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1077                           active_worst_quality, width, height);
1078     if (q > *top_index) {
1079       // Special case when we are targeting the max allowed rate
1080       if (rc->this_frame_target >= rc->max_frame_bandwidth)
1081         *top_index = q;
1082       else
1083         q = *top_index;
1084     }
1085   }
1086 
1087   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1088   assert(*bottom_index <= rc->worst_quality &&
1089          *bottom_index >= rc->best_quality);
1090   assert(q <= rc->worst_quality && q >= rc->best_quality);
1091   return q;
1092 }
1093 
1094 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
1095   1.00,  // INTER_NORMAL
1096   1.50,  // GF_ARF_LOW
1097   2.00,  // GF_ARF_STD
1098   2.00,  // KF_STD
1099 };
1100 
av1_frame_type_qdelta(const AV1_COMP * cpi,int q)1101 int av1_frame_type_qdelta(const AV1_COMP *cpi, int q) {
1102   const RATE_FACTOR_LEVEL rf_lvl = get_rate_factor_level(&cpi->gf_group);
1103   const FRAME_TYPE frame_type = (rf_lvl == KF_STD) ? KEY_FRAME : INTER_FRAME;
1104   double rate_factor;
1105 
1106   rate_factor = rate_factor_deltas[rf_lvl];
1107   if (rf_lvl == GF_ARF_LOW) {
1108     rate_factor -= (cpi->gf_group.layer_depth[cpi->gf_group.index] - 2) * 0.1;
1109     rate_factor = AOMMAX(rate_factor, 1.0);
1110   }
1111   return av1_compute_qdelta_by_rate(&cpi->rc, frame_type, q, rate_factor,
1112                                     cpi->common.seq_params.bit_depth);
1113 }
1114 
1115 // This unrestricted Q selection on CQ mode is useful when testing new features,
1116 // but may lead to Q being out of range on current RC restrictions
1117 #if USE_UNRESTRICTED_Q_IN_CQ_MODE
rc_pick_q_and_bounds_one_pass_cq(const AV1_COMP * cpi,int width,int height,int * bottom_index,int * top_index)1118 static int rc_pick_q_and_bounds_one_pass_cq(const AV1_COMP *cpi, int width,
1119                                             int height, int *bottom_index,
1120                                             int *top_index) {
1121   const AV1_COMMON *const cm = &cpi->common;
1122   const RATE_CONTROL *const rc = &cpi->rc;
1123   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1124   const int cq_level = get_active_cq_level(rc, oxcf, frame_is_intra_only(cm),
1125                                            cm->superres_scale_denominator);
1126   const int bit_depth = cm->seq_params.bit_depth;
1127   const int q = (int)av1_convert_qindex_to_q(cq_level, bit_depth);
1128   (void)width;
1129   (void)height;
1130   *top_index = q;
1131   *bottom_index = q;
1132 
1133   return q;
1134 }
1135 #endif  // USE_UNRESTRICTED_Q_IN_CQ_MODE
1136 
1137 #define STATIC_MOTION_THRESH 95
get_intra_q_and_bounds_two_pass(const AV1_COMP * cpi,int width,int height,int * active_best,int * active_worst,int cq_level,int is_fwd_kf)1138 static void get_intra_q_and_bounds_two_pass(const AV1_COMP *cpi, int width,
1139                                             int height, int *active_best,
1140                                             int *active_worst, int cq_level,
1141                                             int is_fwd_kf) {
1142   const AV1_COMMON *const cm = &cpi->common;
1143   const RATE_CONTROL *const rc = &cpi->rc;
1144   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1145   int active_best_quality;
1146   int active_worst_quality = *active_worst;
1147   const int bit_depth = cm->seq_params.bit_depth;
1148 
1149   if (rc->frames_to_key == 1 && oxcf->rc_mode == AOM_Q) {
1150     // If the next frame is also a key frame or the current frame is the
1151     // only frame in the sequence in AOM_Q mode, just use the cq_level
1152     // as q.
1153     active_best_quality = cq_level;
1154     active_worst_quality = cq_level;
1155   } else if (is_fwd_kf) {
1156     // Handle the special case for forward reference key frames.
1157     // Increase the boost because this keyframe is used as a forward and
1158     // backward reference.
1159     const int qindex = rc->last_boosted_qindex;
1160     const double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1161     const int delta_qindex = av1_compute_qdelta(
1162         rc, last_boosted_q, last_boosted_q * 0.25, bit_depth);
1163     active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1164   } else if (rc->this_key_frame_forced) {
1165     // Handle the special case for key frames forced when we have reached
1166     // the maximum key frame interval. Here force the Q to a range
1167     // based on the ambient Q to reduce the risk of popping.
1168     double last_boosted_q;
1169     int delta_qindex;
1170     int qindex;
1171 
1172     if (is_stat_consumption_stage_twopass(cpi) &&
1173         cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1174       qindex = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1175       active_best_quality = qindex;
1176       last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1177       delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1178                                         last_boosted_q * 1.25, bit_depth);
1179       active_worst_quality =
1180           AOMMIN(qindex + delta_qindex, active_worst_quality);
1181     } else {
1182       qindex = rc->last_boosted_qindex;
1183       last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth);
1184       delta_qindex = av1_compute_qdelta(rc, last_boosted_q,
1185                                         last_boosted_q * 0.50, bit_depth);
1186       active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1187     }
1188   } else {
1189     // Not forced keyframe.
1190     double q_adj_factor = 1.0;
1191     double q_val;
1192 
1193     // Baseline value derived from cpi->active_worst_quality and kf boost.
1194     active_best_quality =
1195         get_kf_active_quality(rc, active_worst_quality, bit_depth);
1196 
1197     if (is_stat_consumption_stage_twopass(cpi) &&
1198         cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1199       active_best_quality /= 3;
1200     }
1201 
1202     // Allow somewhat lower kf minq with small image formats.
1203     if ((width * height) <= (352 * 288)) {
1204       q_adj_factor -= 0.25;
1205     }
1206 
1207     // Make a further adjustment based on the kf zero motion measure.
1208     if (is_stat_consumption_stage_twopass(cpi))
1209       q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
1210 
1211     // Convert the adjustment factor to a qindex delta
1212     // on active_best_quality.
1213     q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth);
1214     active_best_quality +=
1215         av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth);
1216 
1217     // Tweak active_best_quality for AOM_Q mode when superres is on, as this
1218     // will be used directly as 'q' later.
1219     if (oxcf->rc_mode == AOM_Q &&
1220         (cpi->superres_mode == SUPERRES_QTHRESH ||
1221          cpi->superres_mode == SUPERRES_AUTO) &&
1222         cm->superres_scale_denominator != SCALE_NUMERATOR) {
1223       active_best_quality =
1224           AOMMAX(active_best_quality -
1225                      ((cm->superres_scale_denominator - SCALE_NUMERATOR) *
1226                       SUPERRES_QADJ_PER_DENOM_KEYFRAME),
1227                  0);
1228     }
1229   }
1230   *active_best = active_best_quality;
1231   *active_worst = active_worst_quality;
1232 }
1233 
adjust_active_best_and_worst_quality(const AV1_COMP * cpi,const int is_intrl_arf_boost,int * active_worst,int * active_best)1234 static void adjust_active_best_and_worst_quality(const AV1_COMP *cpi,
1235                                                  const int is_intrl_arf_boost,
1236                                                  int *active_worst,
1237                                                  int *active_best) {
1238   const AV1_COMMON *const cm = &cpi->common;
1239   const RATE_CONTROL *const rc = &cpi->rc;
1240   const int bit_depth = cpi->common.seq_params.bit_depth;
1241   int active_best_quality = *active_best;
1242   int active_worst_quality = *active_worst;
1243   // Extension to max or min Q if undershoot or overshoot is outside
1244   // the permitted range.
1245   if (cpi->oxcf.rc_mode != AOM_Q) {
1246     if (frame_is_intra_only(cm) ||
1247         (!rc->is_src_frame_alt_ref &&
1248          (cpi->refresh_golden_frame || is_intrl_arf_boost ||
1249           cpi->refresh_alt_ref_frame))) {
1250       active_best_quality -=
1251           (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
1252       active_worst_quality += (cpi->twopass.extend_maxq / 2);
1253     } else {
1254       active_best_quality -=
1255           (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
1256       active_worst_quality += cpi->twopass.extend_maxq;
1257     }
1258   }
1259 
1260   aom_clear_system_state();
1261 #ifndef STRICT_RC
1262   // Static forced key frames Q restrictions dealt with elsewhere.
1263   if (!(frame_is_intra_only(cm)) || !rc->this_key_frame_forced ||
1264       (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
1265     const int qdelta = av1_frame_type_qdelta(cpi, active_worst_quality);
1266     active_worst_quality =
1267         AOMMAX(active_worst_quality + qdelta, active_best_quality);
1268   }
1269 #endif
1270 
1271   // Modify active_best_quality for downscaled normal frames.
1272   if (av1_frame_scaled(cm) && !frame_is_kf_gf_arf(cpi)) {
1273     int qdelta = av1_compute_qdelta_by_rate(
1274         rc, cm->current_frame.frame_type, active_best_quality, 2.0, bit_depth);
1275     active_best_quality =
1276         AOMMAX(active_best_quality + qdelta, rc->best_quality);
1277   }
1278 
1279   active_best_quality =
1280       clamp(active_best_quality, rc->best_quality, rc->worst_quality);
1281   active_worst_quality =
1282       clamp(active_worst_quality, active_best_quality, rc->worst_quality);
1283 
1284   *active_best = active_best_quality;
1285   *active_worst = active_worst_quality;
1286 }
1287 
get_q(const AV1_COMP * cpi,const int width,const int height,const int active_worst_quality,const int active_best_quality)1288 static int get_q(const AV1_COMP *cpi, const int width, const int height,
1289                  const int active_worst_quality,
1290                  const int active_best_quality) {
1291   const AV1_COMMON *const cm = &cpi->common;
1292   const RATE_CONTROL *const rc = &cpi->rc;
1293   int q;
1294 
1295   if (cpi->oxcf.rc_mode == AOM_Q ||
1296       (frame_is_intra_only(cm) && !rc->this_key_frame_forced &&
1297        cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH &&
1298        rc->frames_to_key > 1)) {
1299     q = active_best_quality;
1300     // Special case code to try and match quality with forced key frames.
1301   } else if (frame_is_intra_only(cm) && rc->this_key_frame_forced) {
1302     // If static since last kf use better of last boosted and last kf q.
1303     if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1304       q = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1305     } else {
1306       q = AOMMIN(rc->last_boosted_qindex,
1307                  (active_best_quality + active_worst_quality) / 2);
1308     }
1309     q = clamp(q, active_best_quality, active_worst_quality);
1310   } else {
1311     q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
1312                           active_worst_quality, width, height);
1313     if (q > active_worst_quality) {
1314       // Special case when we are targeting the max allowed rate.
1315       if (rc->this_frame_target < rc->max_frame_bandwidth) {
1316         q = active_worst_quality;
1317       }
1318     }
1319     q = AOMMAX(q, active_best_quality);
1320   }
1321   return q;
1322 }
1323 
1324 // Returns |active_best_quality| for an inter frame.
1325 // The |active_best_quality| depends on different rate control modes:
1326 // VBR, Q, CQ, CBR.
1327 // The returning active_best_quality could further be adjusted in
1328 // 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)1329 static int get_active_best_quality(const AV1_COMP *const cpi,
1330                                    const int active_worst_quality,
1331                                    const int cq_level, const int gf_index) {
1332   const AV1_COMMON *const cm = &cpi->common;
1333   const int bit_depth = cm->seq_params.bit_depth;
1334   const RATE_CONTROL *const rc = &cpi->rc;
1335   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1336   const GF_GROUP *gf_group = &cpi->gf_group;
1337   const int rc_mode = oxcf->rc_mode;
1338   int *inter_minq;
1339   ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1340   int active_best_quality = 0;
1341   const int is_intrl_arf_boost =
1342       gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
1343   const int is_leaf_frame = !(cpi->refresh_golden_frame ||
1344                               cpi->refresh_alt_ref_frame || is_intrl_arf_boost);
1345   const int is_overlay_frame = rc->is_src_frame_alt_ref;
1346 
1347   if (is_leaf_frame || is_overlay_frame) {
1348     if (rc_mode == AOM_Q) return cq_level;
1349 
1350     active_best_quality = inter_minq[active_worst_quality];
1351     // For the constrained quality mode we don't want
1352     // q to fall below the cq level.
1353     if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1354       active_best_quality = cq_level;
1355     }
1356     return active_best_quality;
1357   }
1358 
1359   // TODO(chengchen): can we remove this condition?
1360   if (rc_mode == AOM_Q && !cpi->refresh_alt_ref_frame && !is_intrl_arf_boost) {
1361     return cq_level;
1362   }
1363 
1364   // Determine active_best_quality for frames that are not leaf or overlay.
1365   int q = active_worst_quality;
1366   // Use the lower of active_worst_quality and recent
1367   // average Q as basis for GF/ARF best Q limit unless last frame was
1368   // a key frame.
1369   if (rc->frames_since_key > 1 &&
1370       rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1371     q = rc->avg_frame_qindex[INTER_FRAME];
1372   }
1373   if (rc_mode == AOM_CQ && q < cq_level) q = cq_level;
1374   active_best_quality = get_gf_active_quality(rc, q, bit_depth);
1375   // Constrained quality use slightly lower active best.
1376   if (rc_mode == AOM_CQ) active_best_quality = active_best_quality * 15 / 16;
1377   const int min_boost = get_gf_high_motion_quality(q, bit_depth);
1378   const int boost = min_boost - active_best_quality;
1379   active_best_quality = min_boost - (int)(boost * rc->arf_boost_factor);
1380   if (!is_intrl_arf_boost) return active_best_quality;
1381 
1382   if (rc_mode == AOM_Q || rc_mode == AOM_CQ) active_best_quality = rc->arf_q;
1383   int this_height = gf_group_pyramid_level(gf_group, gf_index);
1384   while (this_height > 1) {
1385     active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
1386     --this_height;
1387   }
1388   return active_best_quality;
1389 }
1390 
rc_pick_q_and_bounds_two_pass(const AV1_COMP * cpi,int width,int height,int gf_index,int * bottom_index,int * top_index)1391 static int rc_pick_q_and_bounds_two_pass(const AV1_COMP *cpi, int width,
1392                                          int height, int gf_index,
1393                                          int *bottom_index, int *top_index) {
1394   const AV1_COMMON *const cm = &cpi->common;
1395   const RATE_CONTROL *const rc = &cpi->rc;
1396   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1397   const GF_GROUP *gf_group = &cpi->gf_group;
1398   const int cq_level =
1399       get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode,
1400                           cm->superres_scale_denominator);
1401   const int bit_depth = cm->seq_params.bit_depth;
1402 
1403   if (oxcf->use_fixed_qp_offsets) {
1404     return get_q_using_fixed_offsets(oxcf, rc, gf_group, gf_group->index,
1405                                      cq_level, bit_depth);
1406   }
1407 
1408   int active_best_quality = 0;
1409   int active_worst_quality = rc->active_worst_quality;
1410   int q;
1411 
1412   const int is_intrl_arf_boost =
1413       gf_group->update_type[gf_index] == INTNL_ARF_UPDATE;
1414 
1415   if (frame_is_intra_only(cm)) {
1416     const int is_fwd_kf =
1417         cm->current_frame.frame_type == KEY_FRAME && cm->show_frame == 0;
1418     get_intra_q_and_bounds_two_pass(cpi, width, height, &active_best_quality,
1419                                     &active_worst_quality, cq_level, is_fwd_kf);
1420 #ifdef STRICT_RC
1421     active_best_quality = 0;
1422 #endif
1423   } else {
1424 #ifdef STRICT_RC
1425     //  Active best quality limited by previous layer.
1426     const int pyramid_level = gf_group_pyramid_level(gf_group, gf_index);
1427     active_best_quality =
1428         rc->active_best_quality[pyramid_level - 1] +
1429         AOMMAX((rc->active_best_quality[pyramid_level - 1] / 10), 5);
1430 #else
1431     active_best_quality =
1432         get_active_best_quality(cpi, active_worst_quality, cq_level, gf_index);
1433 #endif
1434 
1435     // For alt_ref and GF frames (including internal arf frames) adjust the
1436     // worst allowed quality as well. This insures that even on hard
1437     // sections we dont clamp the Q at the same value for arf frames and
1438     // leaf (non arf) frames. This is important to the TPL model which assumes
1439     // Q drops with each arf level.
1440     if (!(rc->is_src_frame_alt_ref) &&
1441         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
1442          is_intrl_arf_boost)) {
1443       active_worst_quality =
1444           (active_best_quality + (3 * active_worst_quality) + 2) / 4;
1445     }
1446   }
1447 
1448   adjust_active_best_and_worst_quality(
1449       cpi, is_intrl_arf_boost, &active_worst_quality, &active_best_quality);
1450   q = get_q(cpi, width, height, active_worst_quality, active_best_quality);
1451 
1452   // Special case when we are targeting the max allowed rate.
1453   if (rc->this_frame_target >= rc->max_frame_bandwidth &&
1454       q > active_worst_quality) {
1455     active_worst_quality = q;
1456   }
1457 
1458 #ifdef STRICT_RC
1459   *top_index = rc->worst_quality;
1460 #else
1461   *top_index = active_worst_quality;
1462 #endif
1463   *bottom_index = active_best_quality;
1464 
1465   assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
1466   assert(*bottom_index <= rc->worst_quality &&
1467          *bottom_index >= rc->best_quality);
1468   assert(q <= rc->worst_quality && q >= rc->best_quality);
1469 
1470   return q;
1471 }
1472 
av1_rc_pick_q_and_bounds(const AV1_COMP * cpi,RATE_CONTROL * rc,int width,int height,int gf_index,int * bottom_index,int * top_index)1473 int av1_rc_pick_q_and_bounds(const AV1_COMP *cpi, RATE_CONTROL *rc, int width,
1474                              int height, int gf_index, int *bottom_index,
1475                              int *top_index) {
1476   int q;
1477   // TODO(sarahparker) merge onepass vbr and altref q computation
1478   // with two pass
1479   const GF_GROUP *gf_group = &cpi->gf_group;
1480   if ((cpi->oxcf.rc_mode != AOM_Q ||
1481        gf_group->update_type[gf_index] == ARF_UPDATE) &&
1482       has_no_stats_stage(cpi)) {
1483     if (cpi->oxcf.rc_mode == AOM_CBR)
1484       q = rc_pick_q_and_bounds_one_pass_cbr(cpi, width, height, bottom_index,
1485                                             top_index);
1486 #if USE_UNRESTRICTED_Q_IN_CQ_MODE
1487     else if (cpi->oxcf.rc_mode == AOM_CQ)
1488       q = rc_pick_q_and_bounds_one_pass_cq(cpi, width, height, bottom_index,
1489                                            top_index);
1490 #endif  // USE_UNRESTRICTED_Q_IN_CQ_MODE
1491     else
1492       q = rc_pick_q_and_bounds_one_pass_vbr(cpi, width, height, bottom_index,
1493                                             top_index);
1494   } else {
1495     q = rc_pick_q_and_bounds_two_pass(cpi, width, height, gf_index,
1496                                       bottom_index, top_index);
1497   }
1498   if (gf_group->update_type[gf_index] == ARF_UPDATE) rc->arf_q = q;
1499 
1500   return q;
1501 }
1502 
av1_rc_compute_frame_size_bounds(const AV1_COMP * cpi,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)1503 void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
1504                                       int *frame_under_shoot_limit,
1505                                       int *frame_over_shoot_limit) {
1506   if (cpi->oxcf.rc_mode == AOM_Q) {
1507     *frame_under_shoot_limit = 0;
1508     *frame_over_shoot_limit = INT_MAX;
1509   } else {
1510     // For very small rate targets where the fractional adjustment
1511     // may be tiny make sure there is at least a minimum range.
1512     const int tolerance =
1513         AOMMAX(100, (cpi->sf.hl_sf.recode_tolerance * frame_target) / 100);
1514     *frame_under_shoot_limit = AOMMAX(frame_target - tolerance, 0);
1515     *frame_over_shoot_limit =
1516         AOMMIN(frame_target + tolerance, cpi->rc.max_frame_bandwidth);
1517   }
1518 }
1519 
av1_rc_set_frame_target(AV1_COMP * cpi,int target,int width,int height)1520 void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) {
1521   const AV1_COMMON *const cm = &cpi->common;
1522   RATE_CONTROL *const rc = &cpi->rc;
1523 
1524   rc->this_frame_target = target;
1525 
1526   // Modify frame size target when down-scaled.
1527   if (av1_frame_scaled(cm))
1528     rc->this_frame_target =
1529         (int)(rc->this_frame_target * resize_rate_factor(cpi, width, height));
1530 
1531   // Target rate per SB64 (including partial SB64s.
1532   rc->sb64_target_rate =
1533       (int)(((int64_t)rc->this_frame_target << 12) / (width * height));
1534 }
1535 
update_alt_ref_frame_stats(AV1_COMP * cpi)1536 static void update_alt_ref_frame_stats(AV1_COMP *cpi) {
1537   // this frame refreshes means next frames don't unless specified by user
1538   RATE_CONTROL *const rc = &cpi->rc;
1539   rc->frames_since_golden = 0;
1540 
1541   // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1542   rc->source_alt_ref_pending = 0;
1543 
1544   // Set the alternate reference frame active flag
1545   rc->source_alt_ref_active = 1;
1546 }
1547 
update_golden_frame_stats(AV1_COMP * cpi)1548 static void update_golden_frame_stats(AV1_COMP *cpi) {
1549   RATE_CONTROL *const rc = &cpi->rc;
1550   const GF_GROUP *const gf_group = &cpi->gf_group;
1551 
1552   // Update the Golden frame usage counts.
1553   if (cpi->refresh_golden_frame || rc->is_src_frame_alt_ref) {
1554     rc->frames_since_golden = 0;
1555 
1556     // If we are not using alt ref in the up and coming group clear the arf
1557     // active flag. In multi arf group case, if the index is not 0 then
1558     // we are overlaying a mid group arf so should not reset the flag.
1559     if (!rc->source_alt_ref_pending && (gf_group->index == 0))
1560       rc->source_alt_ref_active = 0;
1561   } else if (cpi->common.show_frame) {
1562     rc->frames_since_golden++;
1563   }
1564 }
1565 
av1_rc_postencode_update(AV1_COMP * cpi,uint64_t bytes_used)1566 void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
1567   const AV1_COMMON *const cm = &cpi->common;
1568   const CurrentFrame *const current_frame = &cm->current_frame;
1569   RATE_CONTROL *const rc = &cpi->rc;
1570   const GF_GROUP *const gf_group = &cpi->gf_group;
1571 
1572   const int is_intrnl_arf =
1573       gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE;
1574 
1575   const int qindex = cm->quant_params.base_qindex;
1576 
1577   // Update rate control heuristics
1578   rc->projected_frame_size = (int)(bytes_used << 3);
1579 
1580   // Post encode loop adjustment of Q prediction.
1581   av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
1582 
1583   // Keep a record of last Q and ambient average Q.
1584   if (current_frame->frame_type == KEY_FRAME) {
1585     rc->last_q[KEY_FRAME] = qindex;
1586     rc->avg_frame_qindex[KEY_FRAME] =
1587         ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1588   } else {
1589     if ((cpi->use_svc && cpi->oxcf.rc_mode == AOM_CBR) ||
1590         (!rc->is_src_frame_alt_ref &&
1591          !(cpi->refresh_golden_frame || is_intrnl_arf ||
1592            cpi->refresh_alt_ref_frame))) {
1593       rc->last_q[INTER_FRAME] = qindex;
1594       rc->avg_frame_qindex[INTER_FRAME] =
1595           ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1596       rc->ni_frames++;
1597       rc->tot_q += av1_convert_qindex_to_q(qindex, cm->seq_params.bit_depth);
1598       rc->avg_q = rc->tot_q / rc->ni_frames;
1599       // Calculate the average Q for normal inter frames (not key or GFU
1600       // frames).
1601       rc->ni_tot_qi += qindex;
1602       rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1603     }
1604   }
1605 
1606   // Keep record of last boosted (KF/GF/ARF) Q value.
1607   // If the current frame is coded at a lower Q then we also update it.
1608   // If all mbs in this group are skipped only update if the Q value is
1609   // better than that already stored.
1610   // This is used to help set quality in forced key frames to reduce popping
1611   if ((qindex < rc->last_boosted_qindex) ||
1612       (current_frame->frame_type == KEY_FRAME) ||
1613       (!rc->constrained_gf_group &&
1614        (cpi->refresh_alt_ref_frame || is_intrnl_arf ||
1615         (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
1616     rc->last_boosted_qindex = qindex;
1617   }
1618   if (current_frame->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
1619 
1620   update_buffer_level(cpi, rc->projected_frame_size);
1621   rc->prev_avg_frame_bandwidth = rc->avg_frame_bandwidth;
1622 
1623   // Rolling monitors of whether we are over or underspending used to help
1624   // regulate min and Max Q in two pass.
1625   if (av1_frame_scaled(cm))
1626     rc->this_frame_target =
1627         (int)(rc->this_frame_target /
1628               resize_rate_factor(cpi, cm->width, cm->height));
1629   if (current_frame->frame_type != KEY_FRAME) {
1630     rc->rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
1631         rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
1632     rc->rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
1633         rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1634     rc->long_rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
1635         rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1636     rc->long_rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
1637         rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1638   }
1639 
1640   // Actual bits spent
1641   rc->total_actual_bits += rc->projected_frame_size;
1642   rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
1643 
1644   rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1645 
1646   if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
1647       (current_frame->frame_type != KEY_FRAME))
1648     // Update the alternate reference frame stats as appropriate.
1649     update_alt_ref_frame_stats(cpi);
1650   else
1651     // Update the Golden frame stats as appropriate.
1652     update_golden_frame_stats(cpi);
1653 
1654   if (current_frame->frame_type == KEY_FRAME) rc->frames_since_key = 0;
1655   // if (current_frame->frame_number == 1 && cm->show_frame)
1656   /*
1657   rc->this_frame_target =
1658       (int)(rc->this_frame_target / resize_rate_factor(cpi, cm->width,
1659   cm->height));
1660       */
1661 }
1662 
av1_rc_postencode_update_drop_frame(AV1_COMP * cpi)1663 void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
1664   // Update buffer level with zero size, update frame counters, and return.
1665   update_buffer_level(cpi, 0);
1666   cpi->rc.frames_since_key++;
1667   cpi->rc.frames_to_key--;
1668   cpi->rc.rc_2_frame = 0;
1669   cpi->rc.rc_1_frame = 0;
1670 }
1671 
av1_find_qindex(double desired_q,aom_bit_depth_t bit_depth,int best_qindex,int worst_qindex)1672 int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth,
1673                     int best_qindex, int worst_qindex) {
1674   assert(best_qindex <= worst_qindex);
1675   int low = best_qindex;
1676   int high = worst_qindex;
1677   while (low < high) {
1678     const int mid = (low + high) >> 1;
1679     const double mid_q = av1_convert_qindex_to_q(mid, bit_depth);
1680     if (mid_q < desired_q) {
1681       low = mid + 1;
1682     } else {
1683       high = mid;
1684     }
1685   }
1686   assert(low == high);
1687   assert(av1_convert_qindex_to_q(low, bit_depth) >= desired_q ||
1688          low == worst_qindex);
1689   return low;
1690 }
1691 
av1_compute_qdelta(const RATE_CONTROL * rc,double qstart,double qtarget,aom_bit_depth_t bit_depth)1692 int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1693                        aom_bit_depth_t bit_depth) {
1694   const int start_index =
1695       av1_find_qindex(qstart, bit_depth, rc->best_quality, rc->worst_quality);
1696   const int target_index =
1697       av1_find_qindex(qtarget, bit_depth, rc->best_quality, rc->worst_quality);
1698   return target_index - start_index;
1699 }
1700 
1701 // Find q_index for the desired_bits_per_mb, within [best_qindex, worst_qindex],
1702 // assuming 'correction_factor' is 1.0.
1703 // To be precise, 'q_index' is the smallest integer, for which the corresponding
1704 // bits per mb <= desired_bits_per_mb.
1705 // If no such q index is found, returns 'worst_qindex'.
find_qindex_by_rate(int desired_bits_per_mb,aom_bit_depth_t bit_depth,FRAME_TYPE frame_type,int best_qindex,int worst_qindex)1706 static int find_qindex_by_rate(int desired_bits_per_mb,
1707                                aom_bit_depth_t bit_depth, FRAME_TYPE frame_type,
1708                                int best_qindex, int worst_qindex) {
1709   assert(best_qindex <= worst_qindex);
1710   int low = best_qindex;
1711   int high = worst_qindex;
1712   while (low < high) {
1713     const int mid = (low + high) >> 1;
1714     const int mid_bits_per_mb =
1715         av1_rc_bits_per_mb(frame_type, mid, 1.0, bit_depth);
1716     if (mid_bits_per_mb > desired_bits_per_mb) {
1717       low = mid + 1;
1718     } else {
1719       high = mid;
1720     }
1721   }
1722   assert(low == high);
1723   assert(av1_rc_bits_per_mb(frame_type, low, 1.0, bit_depth) <=
1724              desired_bits_per_mb ||
1725          low == worst_qindex);
1726   return low;
1727 }
1728 
av1_compute_qdelta_by_rate(const RATE_CONTROL * rc,FRAME_TYPE frame_type,int qindex,double rate_target_ratio,aom_bit_depth_t bit_depth)1729 int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1730                                int qindex, double rate_target_ratio,
1731                                aom_bit_depth_t bit_depth) {
1732   // Look up the current projected bits per block for the base index
1733   const int base_bits_per_mb =
1734       av1_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
1735 
1736   // Find the target bits per mb based on the base value and given ratio.
1737   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1738 
1739   const int target_index =
1740       find_qindex_by_rate(target_bits_per_mb, bit_depth, frame_type,
1741                           rc->best_quality, rc->worst_quality);
1742   return target_index - qindex;
1743 }
1744 
av1_rc_set_gf_interval_range(const AV1_COMP * const cpi,RATE_CONTROL * const rc)1745 void av1_rc_set_gf_interval_range(const AV1_COMP *const cpi,
1746                                   RATE_CONTROL *const rc) {
1747   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1748 
1749   // Special case code for 1 pass fixed Q mode tests
1750   if ((has_no_stats_stage(cpi)) && (oxcf->rc_mode == AOM_Q)) {
1751     rc->max_gf_interval = FIXED_GF_INTERVAL;
1752     rc->min_gf_interval = FIXED_GF_INTERVAL;
1753     rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
1754   } else {
1755     // Set Maximum gf/arf interval
1756     rc->max_gf_interval = oxcf->max_gf_interval;
1757     rc->min_gf_interval = oxcf->min_gf_interval;
1758     if (rc->min_gf_interval == 0)
1759       rc->min_gf_interval = av1_rc_get_default_min_gf_interval(
1760           oxcf->width, oxcf->height, cpi->framerate);
1761     if (rc->max_gf_interval == 0)
1762       rc->max_gf_interval = av1_rc_get_default_max_gf_interval(
1763           cpi->framerate, rc->min_gf_interval);
1764     /*
1765      * Extended max interval for genuinely static scenes like slide shows.
1766      * The no.of.stats available in the case of LAP is limited,
1767      * hence setting to max_gf_interval.
1768      */
1769     if (cpi->lap_enabled)
1770       rc->static_scene_max_gf_interval = rc->max_gf_interval + 1;
1771     else
1772       rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH;
1773 
1774     if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1775       rc->max_gf_interval = rc->static_scene_max_gf_interval;
1776 
1777     // Clamp min to max
1778     rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval);
1779   }
1780 }
1781 
av1_rc_update_framerate(AV1_COMP * cpi,int width,int height)1782 void av1_rc_update_framerate(AV1_COMP *cpi, int width, int height) {
1783   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
1784   RATE_CONTROL *const rc = &cpi->rc;
1785   int vbr_max_bits;
1786   const int MBs = av1_get_MBs(width, height);
1787 
1788   rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
1789   rc->min_frame_bandwidth =
1790       (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
1791 
1792   rc->min_frame_bandwidth =
1793       AOMMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1794 
1795   // A maximum bitrate for a frame is defined.
1796   // The baseline for this aligns with HW implementations that
1797   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1798   // per 16x16 MB (averaged over a frame). However this limit is extended if
1799   // a very high rate is given on the command line or the the rate cannnot
1800   // be acheived because of a user specificed max q (e.g. when the user
1801   // specifies lossless encode.
1802   vbr_max_bits =
1803       (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
1804             100);
1805   rc->max_frame_bandwidth =
1806       AOMMAX(AOMMAX((MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1807 
1808   av1_rc_set_gf_interval_range(cpi, rc);
1809 }
1810 
1811 #define VBR_PCT_ADJUSTMENT_LIMIT 50
1812 // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(AV1_COMP * cpi,int * this_frame_target)1813 static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) {
1814   RATE_CONTROL *const rc = &cpi->rc;
1815   int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
1816   const int stats_count =
1817       cpi->twopass.stats_buf_ctx->total_stats != NULL
1818           ? (int)cpi->twopass.stats_buf_ctx->total_stats->count
1819           : 0;
1820   const int frame_window = AOMMIN(
1821       16, (int)(stats_count - (int)cpi->common.current_frame.frame_number));
1822 
1823   if (frame_window > 0) {
1824     const int max_delta =
1825         AOMMIN(abs((int)(vbr_bits_off_target / frame_window)),
1826                (*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100);
1827 
1828     // vbr_bits_off_target > 0 means we have extra bits to spend
1829     // vbr_bits_off_target < 0 we are currently overshooting
1830     *this_frame_target += (vbr_bits_off_target >= 0) ? max_delta : -max_delta;
1831   }
1832 
1833   // Fast redistribution of bits arising from massive local undershoot.
1834   // Dont do it for kf,arf,gf or overlay frames.
1835   if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
1836       rc->vbr_bits_off_target_fast) {
1837     int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target);
1838     int fast_extra_bits;
1839     fast_extra_bits = (int)AOMMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1840     fast_extra_bits = (int)AOMMIN(
1841         fast_extra_bits,
1842         AOMMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
1843     *this_frame_target += (int)fast_extra_bits;
1844     rc->vbr_bits_off_target_fast -= fast_extra_bits;
1845   }
1846 }
1847 
av1_set_target_rate(AV1_COMP * cpi,int width,int height)1848 void av1_set_target_rate(AV1_COMP *cpi, int width, int height) {
1849   RATE_CONTROL *const rc = &cpi->rc;
1850   int target_rate = rc->base_frame_target;
1851 
1852   // Correction to rate target based on prior over or under shoot.
1853   if (cpi->oxcf.rc_mode == AOM_VBR || cpi->oxcf.rc_mode == AOM_CQ)
1854     vbr_rate_correction(cpi, &target_rate);
1855   av1_rc_set_frame_target(cpi, target_rate, width, height);
1856 }
1857 
av1_calc_pframe_target_size_one_pass_vbr(const AV1_COMP * const cpi,FRAME_UPDATE_TYPE frame_update_type)1858 int av1_calc_pframe_target_size_one_pass_vbr(
1859     const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
1860   static const int af_ratio = 10;
1861   const RATE_CONTROL *const rc = &cpi->rc;
1862   int64_t target;
1863 #if USE_ALTREF_FOR_ONE_PASS
1864   if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
1865       frame_update_type == ARF_UPDATE) {
1866     target = ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval *
1867               af_ratio) /
1868              (rc->baseline_gf_interval + af_ratio - 1);
1869   } else {
1870     target = ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
1871              (rc->baseline_gf_interval + af_ratio - 1);
1872   }
1873   if (target > INT_MAX) target = INT_MAX;
1874 #else
1875   target = rc->avg_frame_bandwidth;
1876 #endif
1877   return av1_rc_clamp_pframe_target_size(cpi, (int)target, frame_update_type);
1878 }
1879 
av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP * const cpi)1880 int av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
1881   static const int kf_ratio = 25;
1882   const RATE_CONTROL *rc = &cpi->rc;
1883   const int target = rc->avg_frame_bandwidth * kf_ratio;
1884   return av1_rc_clamp_iframe_target_size(cpi, target);
1885 }
1886 
av1_calc_pframe_target_size_one_pass_cbr(const AV1_COMP * cpi,FRAME_UPDATE_TYPE frame_update_type)1887 int av1_calc_pframe_target_size_one_pass_cbr(
1888     const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
1889   const AV1EncoderConfig *oxcf = &cpi->oxcf;
1890   const RATE_CONTROL *rc = &cpi->rc;
1891   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1892   const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1893   int min_frame_target =
1894       AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1895   int target;
1896 
1897   if (oxcf->gf_cbr_boost_pct) {
1898     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1899     if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
1900       target =
1901           (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
1902           (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1903     } else {
1904       target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1905                (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1906     }
1907   } else {
1908     target = rc->avg_frame_bandwidth;
1909   }
1910   if (cpi->use_svc) {
1911     // Note that for layers, avg_frame_bandwidth is the cumulative
1912     // per-frame-bandwidth. For the target size of this frame, use the
1913     // layer average frame size (i.e., non-cumulative per-frame-bw).
1914     int layer =
1915         LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1916                          cpi->svc.number_temporal_layers);
1917     const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1918     target = lc->avg_frame_size;
1919     min_frame_target = AOMMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1920   }
1921   if (diff > 0) {
1922     // Lower the target bandwidth for this frame.
1923     const int pct_low = (int)AOMMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1924     target -= (target * pct_low) / 200;
1925   } else if (diff < 0) {
1926     // Increase the target bandwidth for this frame.
1927     const int pct_high =
1928         (int)AOMMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1929     target += (target * pct_high) / 200;
1930   }
1931   if (oxcf->rc_max_inter_bitrate_pct) {
1932     const int max_rate =
1933         rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
1934     target = AOMMIN(target, max_rate);
1935   }
1936   return AOMMAX(min_frame_target, target);
1937 }
1938 
av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP * cpi)1939 int av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
1940   const RATE_CONTROL *rc = &cpi->rc;
1941   int target;
1942   if (cpi->common.current_frame.frame_number == 0) {
1943     target = ((rc->starting_buffer_level / 2) > INT_MAX)
1944                  ? INT_MAX
1945                  : (int)(rc->starting_buffer_level / 2);
1946   } else {
1947     int kf_boost = 32;
1948     double framerate = cpi->framerate;
1949 
1950     kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
1951     if (rc->frames_since_key < framerate / 2) {
1952       kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
1953     }
1954     target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
1955   }
1956   return av1_rc_clamp_iframe_target_size(cpi, target);
1957 }
1958 
set_reference_structure_one_pass_rt(AV1_COMP * cpi,int gf_update)1959 static void set_reference_structure_one_pass_rt(AV1_COMP *cpi, int gf_update) {
1960   AV1_COMMON *const cm = &cpi->common;
1961   ExternalFlags *const ext_flags = &cpi->ext_flags;
1962   SVC *const svc = &cpi->svc;
1963   // Specify the reference prediction structure, for 1 layer nonrd mode.
1964   // Current structue is to use 3 references (LAST, GOLDEN, ALTREF),
1965   // where ALT_REF always behind current by lag_alt frames, and GOLDEN is
1966   // either updated on LAST with period baseline_gf_interval (fixed slot)
1967   // or always behind current by lag_gld (gld_fixed_slot = 0, lag_gld <= 7).
1968   const int gld_fixed_slot = 1;
1969   const unsigned int lag_alt = 4;
1970   int last_idx = 0;
1971   int last_idx_refresh = 0;
1972   int gld_idx = 0;
1973   int alt_ref_idx = 0;
1974   ext_flags->refresh_frame_flags_pending = 1;
1975   svc->external_ref_frame_config = 1;
1976   ext_flags->ref_frame_flags = 0;
1977   ext_flags->refresh_last_frame = 1;
1978   ext_flags->refresh_golden_frame = 0;
1979   ext_flags->refresh_alt_ref_frame = 0;
1980   for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) svc->ref_idx[i] = 7;
1981   for (int i = 0; i < REF_FRAMES; ++i) svc->refresh[i] = 0;
1982   // Always reference LAST, GOLDEN, ALTREF
1983   ext_flags->ref_frame_flags ^= AOM_LAST_FLAG;
1984   ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG;
1985   ext_flags->ref_frame_flags ^= AOM_ALT_FLAG;
1986   const int sh = 7 - gld_fixed_slot;
1987   // Moving index slot for last: 0 - (sh - 1).
1988   if (cm->current_frame.frame_number > 1)
1989     last_idx = ((cm->current_frame.frame_number - 1) % sh);
1990   // Moving index for refresh of last: one ahead for next frame.
1991   last_idx_refresh = (cm->current_frame.frame_number % sh);
1992   gld_idx = 6;
1993   if (!gld_fixed_slot) {
1994     gld_idx = 7;
1995     const unsigned int lag_gld = 7;  // Must be <= 7.
1996     // Moving index for gld_ref, lag behind current by gld_interval frames.
1997     if (cm->current_frame.frame_number > lag_gld)
1998       gld_idx = ((cm->current_frame.frame_number - lag_gld) % sh);
1999   }
2000   // Moving index for alt_ref, lag behind LAST by lag_alt frames.
2001   if (cm->current_frame.frame_number > lag_alt)
2002     alt_ref_idx = ((cm->current_frame.frame_number - lag_alt) % sh);
2003   svc->ref_idx[0] = last_idx;          // LAST
2004   svc->ref_idx[1] = last_idx_refresh;  // LAST2 (for refresh of last).
2005   svc->ref_idx[3] = gld_idx;           // GOLDEN
2006   svc->ref_idx[6] = alt_ref_idx;       // ALT_REF
2007   // Refresh this slot, which will become LAST on next frame.
2008   svc->refresh[last_idx_refresh] = 1;
2009   // Update GOLDEN on period for fixed slot case.
2010   if (gld_fixed_slot && gf_update) {
2011     ext_flags->refresh_golden_frame = 1;
2012     svc->refresh[gld_idx] = 1;
2013   }
2014 }
2015 
2016 #define DEFAULT_KF_BOOST_RT 2300
2017 #define DEFAULT_GF_BOOST_RT 2000
2018 
av1_get_one_pass_rt_params(AV1_COMP * cpi,EncodeFrameParams * const frame_params,unsigned int frame_flags)2019 void av1_get_one_pass_rt_params(AV1_COMP *cpi,
2020                                 EncodeFrameParams *const frame_params,
2021                                 unsigned int frame_flags) {
2022   RATE_CONTROL *const rc = &cpi->rc;
2023   AV1_COMMON *const cm = &cpi->common;
2024   GF_GROUP *const gf_group = &cpi->gf_group;
2025   ResizePendingParams *const resize_pending_params =
2026       &cpi->resize_pending_params;
2027   int gf_update = 0;
2028   int target;
2029   const int resize_pending =
2030       (resize_pending_params->width && resize_pending_params->height &&
2031        (cm->width != resize_pending_params->width ||
2032         cm->height != resize_pending_params->height));
2033   // Turn this on to explicitly set the reference structure rather than
2034   // relying on internal/default structure.
2035   const int set_reference_structure = 1;
2036   if (cpi->use_svc) {
2037     av1_update_temporal_layer_framerate(cpi);
2038     av1_restore_layer_context(cpi);
2039   }
2040   if ((!cpi->use_svc && rc->frames_to_key == 0) ||
2041       (cpi->use_svc && cpi->svc.spatial_layer_id == 0 &&
2042        cpi->svc.current_superframe % cpi->oxcf.key_freq == 0) ||
2043       (frame_flags & FRAMEFLAGS_KEY)) {
2044     frame_params->frame_type = KEY_FRAME;
2045     rc->this_key_frame_forced =
2046         cm->current_frame.frame_number != 0 && rc->frames_to_key == 0;
2047     rc->frames_to_key = cpi->oxcf.key_freq;
2048     rc->kf_boost = DEFAULT_KF_BOOST_RT;
2049     rc->source_alt_ref_active = 0;
2050     gf_group->update_type[gf_group->index] = KF_UPDATE;
2051     if (cpi->use_svc && cm->current_frame.frame_number > 0)
2052       av1_svc_reset_temporal_layers(cpi, 1);
2053   } else {
2054     frame_params->frame_type = INTER_FRAME;
2055     gf_group->update_type[gf_group->index] = LF_UPDATE;
2056   }
2057   // GF update based on frames_till_gf_update_due, also
2058   // force upddate on resize pending frame.
2059   if ((resize_pending || rc->frames_till_gf_update_due == 0) &&
2060       cpi->svc.temporal_layer_id == 0 && cpi->svc.spatial_layer_id == 0) {
2061     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2062       av1_cyclic_refresh_set_golden_update(cpi);
2063     else
2064       rc->baseline_gf_interval = MAX_GF_INTERVAL;
2065     if (rc->baseline_gf_interval > rc->frames_to_key)
2066       rc->baseline_gf_interval = rc->frames_to_key;
2067     rc->gfu_boost = DEFAULT_GF_BOOST_RT;
2068     rc->constrained_gf_group =
2069         (rc->baseline_gf_interval >= rc->frames_to_key) ? 1 : 0;
2070     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2071     gf_group->index = 0;
2072     // SVC does not use GF as periodid boost.
2073     // TODO(marpan): Find better way to disable this for SVC.
2074     if (cpi->use_svc) {
2075       SVC *const svc = &cpi->svc;
2076       rc->baseline_gf_interval = MAX_STATIC_GF_GROUP_LENGTH - 1;
2077       rc->gfu_boost = 1;
2078       rc->constrained_gf_group = 0;
2079       rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2080       for (int layer = 0;
2081            layer < svc->number_spatial_layers * svc->number_temporal_layers;
2082            ++layer) {
2083         LAYER_CONTEXT *const lc = &svc->layer_context[layer];
2084         lc->rc.baseline_gf_interval = rc->baseline_gf_interval;
2085         lc->rc.gfu_boost = rc->gfu_boost;
2086         lc->rc.constrained_gf_group = rc->constrained_gf_group;
2087         lc->rc.frames_till_gf_update_due = rc->frames_till_gf_update_due;
2088         lc->group_index = 0;
2089       }
2090     }
2091     gf_group->size = rc->baseline_gf_interval;
2092     gf_group->update_type[0] =
2093         (frame_params->frame_type == KEY_FRAME) ? KF_UPDATE : GF_UPDATE;
2094     gf_update = 1;
2095   }
2096   if (cpi->oxcf.rc_mode == AOM_CBR) {
2097     if (frame_params->frame_type == KEY_FRAME) {
2098       target = av1_calc_iframe_target_size_one_pass_cbr(cpi);
2099     } else {
2100       target = av1_calc_pframe_target_size_one_pass_cbr(
2101           cpi, gf_group->update_type[gf_group->index]);
2102     }
2103   } else {
2104     if (frame_params->frame_type == KEY_FRAME) {
2105       target = av1_calc_iframe_target_size_one_pass_vbr(cpi);
2106     } else {
2107       target = av1_calc_pframe_target_size_one_pass_vbr(
2108           cpi, gf_group->update_type[gf_group->index]);
2109     }
2110   }
2111   av1_rc_set_frame_target(cpi, target, cm->width, cm->height);
2112   rc->base_frame_target = target;
2113   if (set_reference_structure && cpi->oxcf.speed >= 6 &&
2114       cm->number_spatial_layers == 1 && cm->number_temporal_layers == 1)
2115     set_reference_structure_one_pass_rt(cpi, gf_update);
2116   cm->current_frame.frame_type = frame_params->frame_type;
2117 }
2118