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