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