1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11 */
12 #include <stdlib.h>
13 
14 #include "EbDefinitions.h"
15 #include "EbEncHandle.h"
16 #include "EbRateControlProcess.h"
17 #include "EbSequenceControlSet.h"
18 #include "EbPictureControlSet.h"
19 #include "EbUtility.h"
20 #include "EbSvtAv1ErrorCodes.h"
21 #include "EbEntropyCoding.h"
22 
23 #include "EbRateControlResults.h"
24 #include "EbRateControlTasks.h"
25 
26 #include "EbSegmentation.h"
27 #include "EbLog.h"
28 #include "EbRateDistortionCost.h"
29 #include "EbLambdaRateTables.h"
30 #include "pass2_strategy.h"
31 
32 #include "EbTransforms.h"
33 #include "aom_dsp_rtcd.h"
34 #include "EbLog.h"
35 #include "EbIntraPrediction.h"
36 #include "EbMotionEstimation.h"
37 static const double tpl_hl_islice_div_factor[EB_MAX_TEMPORAL_LAYERS] = { 1, 1, 1, 2, 1, 0.8 };
38 static const double tpl_hl_base_frame_div_factor[EB_MAX_TEMPORAL_LAYERS] = { 1, 1, 1, 3, 1, 0.7 };
39 
40 
41 /**************************************
42  * Coded Frames Stats
43  **************************************/
44 typedef struct CodedFramesStatsEntry {
45     EbDctor  dctor;
46     uint64_t picture_number;
47     EbBool   end_of_sequence_flag;
48 } CodedFramesStatsEntry;
49 
50 typedef struct RateControlIntervalParamContext {
51     EbDctor                   dctor;
52     uint64_t                  first_poc;
53     uint64_t                  last_poc;
54 
55     // Projected total bits available for a key frame group of frames
56     int64_t kf_group_bits;
57     // Error score of frames still to be coded in kf group
58     int64_t kf_group_error_left;
59 } RateControlIntervalParamContext;
60 
61 typedef struct RateControlContext {
62     EbFifo *rate_control_input_tasks_fifo_ptr;
63     EbFifo *rate_control_output_results_fifo_ptr;
64 
65     RateControlIntervalParamContext **rate_control_param_queue;
66 } RateControlContext;
67 
rate_control_context_dctor(EbPtr p)68 static void rate_control_context_dctor(EbPtr p) {
69     EbThreadContext *   thread_context_ptr = (EbThreadContext *)p;
70     RateControlContext *obj                = (RateControlContext *)thread_context_ptr->priv;
71     if (obj->rate_control_param_queue)
72         EB_FREE_2D(obj->rate_control_param_queue);
73     EB_FREE_ARRAY(obj);
74 }
75 
rate_control_context_ctor(EbThreadContext * thread_context_ptr,const EbEncHandle * enc_handle_ptr)76 EbErrorType rate_control_context_ctor(EbThreadContext *  thread_context_ptr,
77                                       const EbEncHandle *enc_handle_ptr) {
78     uint32_t interval_index;
79 
80     int32_t intra_period = enc_handle_ptr->scs_instance_array[0]->scs_ptr->intra_period_length;
81 
82     RateControlContext *context_ptr;
83     EB_CALLOC_ARRAY(context_ptr, 1);
84     thread_context_ptr->priv  = context_ptr;
85     thread_context_ptr->dctor = rate_control_context_dctor;
86 
87     context_ptr->rate_control_input_tasks_fifo_ptr = svt_system_resource_get_consumer_fifo(
88         enc_handle_ptr->rate_control_tasks_resource_ptr, 0);
89     context_ptr->rate_control_output_results_fifo_ptr = svt_system_resource_get_producer_fifo(
90         enc_handle_ptr->rate_control_results_resource_ptr, 0);
91 
92     EB_MALLOC_2D(context_ptr->rate_control_param_queue, (int32_t)PARALLEL_GOP_MAX_NUMBER, 1);
93 
94     for (interval_index = 0; interval_index < PARALLEL_GOP_MAX_NUMBER; interval_index++) {
95         context_ptr->rate_control_param_queue[interval_index]->first_poc =
96             (interval_index * (uint32_t)(intra_period + 1));
97         context_ptr->rate_control_param_queue[interval_index]->last_poc =
98             ((interval_index + 1) * (uint32_t)(intra_period + 1)) - 1;
99     }
100 
101     return EB_ErrorNone;
102 }
103 
104 #define MAX_Q_INDEX 255
105 #define MIN_Q_INDEX 0
106 
107 extern int16_t svt_av1_ac_quant_q3(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
108 // These functions use formulaic calculations to make playing with the
109 // quantizer tables easier. If necessary they can be replaced by lookup
110 // tables if and when things settle down in the experimental Bitstream
111 
svt_av1_convert_qindex_to_q(int32_t qindex,AomBitDepth bit_depth)112 double svt_av1_convert_qindex_to_q(int32_t qindex, AomBitDepth bit_depth) {
113     // Convert the index to a real Q value (scaled down to match old Q values)
114     switch (bit_depth) {
115     case AOM_BITS_8: return svt_av1_ac_quant_q3(qindex, 0, bit_depth) / 4.0;
116     case AOM_BITS_10: return svt_av1_ac_quant_q3(qindex, 0, bit_depth) / 16.0;
117     case AOM_BITS_12: return svt_av1_ac_quant_q3(qindex, 0, bit_depth) / 64.0;
118     default: assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12"); return -1.0;
119     }
120 }
svt_av1_compute_qdelta(double qstart,double qtarget,AomBitDepth bit_depth)121 int32_t svt_av1_compute_qdelta(double qstart, double qtarget, AomBitDepth bit_depth) {
122     int32_t start_index  = MAX_Q_INDEX;
123     int32_t target_index = MAX_Q_INDEX;
124     int32_t i;
125 
126     // Convert the average q value to an index.
127     for (i = MIN_Q_INDEX; i < MAX_Q_INDEX; ++i) {
128         start_index = i;
129         if (svt_av1_convert_qindex_to_q(i, bit_depth) >= qstart)
130             break;
131     }
132 
133     // Convert the q target to an index
134     for (i = MIN_Q_INDEX; i < MAX_Q_INDEX; ++i) {
135         target_index = i;
136         if (svt_av1_convert_qindex_to_q(i, bit_depth) >= qtarget)
137             break;
138     }
139 
140     return target_index - start_index;
141 }
142 #define STATIC_MOTION_THRESH 95
143 
144 // that are not marked as coded with 0,0 motion in the first pass.
145 #define FAST_MOVING_KF_GROUP_THRESH 5
146 #define MEDIUM_MOVING_KF_GROUP_THRESH 30
147 #define MAX_QPS_COMP_I 150
148 #define MAX_QPS_COMP_I_LR 42
149 #define MAX_QPS_COMP_NONI 300
150 #define HIGH_QPS_COMP_THRESHOLD 80
151 #define LOW_QPS_COMP_THRESHOLD 40
152 #define HIGH_FILTERED_THRESHOLD (4 << 8) // 8 bit precision
153 #define LOW_FILTERED_THRESHOLD (2 << 8) // 8 bit precision
154 #define MAX_REF_AREA_I 50 // Max ref area for I slice
155 #define MAX_REF_AREA_NONI 50 // Max ref area for Non I slice
156 #define MAX_REF_AREA_NONI_LOW_RES 40 // Max ref area for Non I slice in low resolution
157 #define REF_AREA_DIF_THRESHOLD 10 // Difference threshold for ref area between two frames
158 #define REF_AREA_LOW_THRESHOLD 8 // Low threshold for ref area
159 #define REF_AREA_MED_THRESHOLD 16 // Medium threshold for ref area
160 #define ME_SAD_LOW_THRESHOLD1 15 // Low sad_ threshold1 for me distortion (very low)
161 #define ME_SAD_LOW_THRESHOLD2 25 // Low sad_ threshold2 for me distortion (low)
162 #define ME_SAD_HIGH_THRESHOLD 80 // High sad_ threshold2 for me distortion (high)
163 
164 #define ASSIGN_MINQ_TABLE(bit_depth, name)                   \
165     do {                                                     \
166         name = NULL;                                         \
167         switch (bit_depth) {                                 \
168         case AOM_BITS_10: name = name##_10; break;           \
169         case AOM_BITS_12: name = name##_12; break;           \
170         case AOM_BITS_8: name = name##_8; break;             \
171         }                                                    \
172         assert(name &&                                       \
173                "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
174                " or AOM_BITS_12");                           \
175         if (!name)                                           \
176             name = name##_8;                                 \
177     } while (0)
178 static int kf_low_motion_minq_cqp_8[QINDEX_RANGE] = {
179     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
180     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
181     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   2,   2,   2,   2,   2,   2,   2,   3,
182     3,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   4,   4,   4,   5,   5,   5,   5,   5,
183     5,   5,   6,   6,   6,   6,   6,   6,   6,   7,   7,   7,   7,   7,   7,   7,   7,   8,   8,
184     8,   8,   8,   9,   9,   9,   9,   10,  10,  10,  10,  11,  11,  11,  11,  12,  12,  12,  12,
185     13,  13,  13,  13,  14,  14,  14,  15,  15,  15,  16,  16,  16,  17,  17,  18,  18,  18,  19,
186     19,  19,  20,  20,  20,  21,  21,  22,  22,  23,  23,  24,  24,  24,  25,  25,  26,  26,  27,
187     27,  28,  28,  29,  30,  30,  31,  31,  32,  32,  33,  34,  34,  35,  36,  36,  37,  37,  38,
188     39,  39,  40,  41,  42,  42,  43,  44,  45,  45,  46,  47,  48,  49,  50,  51,  51,  52,  53,
189     54,  55,  56,  57,  58,  59,  60,  61,  62,  64,  65,  66,  67,  69,  70,  71,  72,  74,  75,
190     77,  78,  80,  82,  83,  85,  87,  89,  91,  93,  95,  96,  97,  99,  100, 101, 103, 104, 105,
191     107, 109, 110, 112, 114, 116, 118, 120, 122, 124, 125, 127, 129, 131, 134, 136, 138, 140, 142,
192     144, 147, 149, 151, 154, 156, 158, 161, 163};
193 static int kf_low_motion_minq_cqp_10[QINDEX_RANGE] = {
194     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
195     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
196     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   10,  10,  11,
197     11,  11,  11,  11,  11,  12,  12,  12,  12,  12,  13,  13,  13,  13,  13,  13,  13,  14,  14,
198     14,  14,  14,  14,  14,  15,  15,  15,  15,  15,  16,  16,  16,  16,  16,  16,  16,  17,  17,
199     17,  17,  17,  18,  18,  18,  18,  19,  19,  19,  19,  20,  20,  20,  21,  21,  21,  21,  22,
200     22,  22,  22,  23,  23,  23,  23,  24,  24,  24,  25,  25,  25,  26,  26,  26,  27,  27,  27,
201     28,  28,  28,  29,  29,  29,  30,  30,  31,  31,  32,  32,  32,  33,  33,  34,  34,  34,  35,
202     35,  36,  36,  37,  37,  38,  38,  39,  39,  40,  40,  41,  41,  42,  42,  43,  44,  44,  45,
203     46,  46,  47,  47,  48,  49,  49,  50,  51,  51,  52,  53,  54,  54,  55,  56,  57,  58,  58,
204     59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  76,  77,  78,
205     80,  81,  83,  84,  86,  87,  89,  91,  93,  95,  96,  97,  98,  100, 101, 102, 103, 105, 106,
206     108, 109, 111, 113, 115, 117, 119, 121, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142,
207     144, 147, 149, 151, 154, 156, 159, 161, 163};
208 static int kf_low_motion_minq_cqp_12[QINDEX_RANGE] = {
209     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
210     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
211     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   13,
212     13,  13,  13,  14,  14,  14,  14,  14,  14,  15,  15,  15,  15,  15,  16,  16,  16,  16,  16,
213     16,  16,  17,  17,  17,  17,  17,  17,  18,  18,  18,  18,  18,  18,  18,  19,  19,  19,  19,
214     19,  19,  20,  20,  20,  20,  21,  21,  21,  21,  22,  22,  22,  22,  23,  23,  23,  23,  24,
215     24,  24,  24,  25,  25,  25,  25,  26,  26,  26,  27,  27,  27,  28,  28,  28,  29,  29,  29,
216     30,  30,  30,  31,  31,  31,  32,  32,  33,  33,  33,  34,  34,  35,  35,  35,  36,  36,  37,
217     37,  38,  38,  39,  39,  39,  40,  40,  41,  41,  42,  42,  43,  44,  44,  45,  45,  46,  46,
218     47,  48,  48,  49,  49,  50,  51,  51,  52,  53,  53,  54,  55,  56,  56,  57,  58,  59,  59,
219     60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  78,  79,
220     80,  82,  83,  85,  86,  88,  90,  91,  93,  95,  96,  97,  99,  100, 101, 102, 104, 105, 106,
221     108, 110, 111, 113, 115, 117, 119, 121, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142,
222     144, 147, 149, 152, 154, 156, 159, 161, 163};
223 static int kf_high_motion_minq_8[QINDEX_RANGE] = {
224     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   2,   2,   3,   3,   4,   4,   5,
225     5,   5,   6,   6,   7,   7,   8,   8,   8,   9,   9,   10,  10,  11,  11,  11,  12,  12,  13,
226     13,  14,  14,  14,  15,  15,  16,  16,  16,  17,  17,  18,  18,  19,  19,  19,  20,  20,  21,
227     21,  21,  22,  22,  23,  23,  24,  24,  24,  25,  25,  26,  26,  26,  27,  27,  28,  28,  28,
228     29,  29,  30,  30,  30,  31,  31,  32,  32,  32,  33,  33,  34,  34,  34,  35,  35,  36,  36,
229     36,  37,  38,  39,  39,  40,  41,  42,  42,  43,  44,  45,  46,  46,  47,  48,  49,  49,  50,
230     51,  51,  52,  53,  54,  54,  55,  56,  57,  58,  59,  61,  62,  63,  64,  65,  66,  67,  68,
231     69,  70,  71,  72,  73,  74,  76,  77,  78,  80,  81,  82,  84,  85,  86,  88,  89,  90,  92,
232     93,  95,  96,  97,  97,  98,  99,  100, 100, 101, 102, 103, 104, 105, 106, 107, 107, 108, 109,
233     110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 121, 122, 123, 124, 124, 125, 126,
234     128, 128, 129, 130, 131, 131, 132, 134, 135, 136, 137, 138, 139, 140, 141, 143, 143, 144, 146,
235     146, 147, 149, 150, 151, 152, 153, 155, 156, 158, 158, 160, 161, 163, 164, 166, 166, 168, 170,
236     171, 173, 174, 176, 178, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 200, 201, 204, 206,
237     209, 212, 214, 216, 219, 222, 224, 227, 230};
238 
239 static int arfgf_low_motion_minq_8[QINDEX_RANGE] = {
240     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
241     0,   0,   2,   2,   2,   3,   3,   3,   3,   4,   4,   4,   5,   5,   5,   5,   6,   6,   6,
242     7,   7,   7,   7,   8,   8,   8,   9,   9,   9,   9,   10,  10,  10,  10,  11,  11,  11,  12,
243     12,  12,  12,  13,  13,  13,  13,  14,  14,  14,  15,  15,  15,  15,  16,  16,  16,  16,  17,
244     17,  17,  17,  18,  18,  18,  18,  19,  19,  19,  20,  20,  20,  20,  21,  21,  21,  21,  22,
245     22,  22,  23,  23,  24,  24,  25,  25,  26,  26,  27,  27,  28,  28,  29,  29,  30,  30,  31,
246     31,  32,  32,  33,  33,  34,  34,  35,  36,  36,  37,  38,  38,  39,  40,  41,  41,  42,  43,
247     43,  44,  45,  45,  46,  47,  48,  49,  49,  50,  51,  52,  53,  54,  54,  55,  56,  57,  58,
248     59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  77,  78,
249     79,  80,  81,  83,  84,  85,  86,  87,  89,  90,  91,  92,  94,  95,  96,  97,  97,  98,  100,
250     100, 101, 102, 102, 103, 105, 106, 106, 107, 109, 110, 110, 112, 113, 114, 116, 116, 118, 119,
251     120, 122, 123, 125, 125, 127, 128, 130, 132, 133, 134, 135, 137, 139, 140, 141, 143, 145, 146,
252     148, 150, 152, 154, 155, 158, 160, 162, 164, 166, 168, 171, 173, 176, 178, 181, 183, 186, 188,
253     191, 194, 197, 200, 203, 206, 210, 213, 216};
254 
255 static int arfgf_high_motion_minq_8[QINDEX_RANGE] = {
256     0,   0,   0,   0,   0,   0,   0,   0,   2,   2,   3,   3,   4,   4,   5,   5,   6,   7,   7,
257     8,   8,   9,   9,   10,  10,  11,  11,  12,  12,  13,  13,  14,  14,  15,  16,  16,  17,  17,
258     18,  18,  19,  19,  20,  20,  21,  21,  22,  22,  23,  23,  24,  24,  25,  25,  26,  26,  27,
259     27,  28,  28,  29,  29,  30,  31,  31,  32,  32,  33,  33,  34,  34,  35,  35,  36,  36,  37,
260     37,  38,  38,  39,  39,  40,  40,  41,  41,  42,  42,  43,  43,  44,  44,  45,  45,  46,  46,
261     46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
262     65,  66,  67,  68,  68,  69,  70,  72,  73,  74,  76,  77,  79,  80,  81,  83,  84,  85,  87,
263     88,  89,  91,  92,  93,  95,  96,  97,  98,  99,  100, 100, 101, 102, 103, 104, 105, 106, 107,
264     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 123, 124, 125,
265     126, 127, 128, 129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 139, 140, 141, 142,
266     144, 144, 145, 146, 147, 148, 149, 151, 151, 152, 153, 155, 156, 156, 157, 159, 160, 161, 162,
267     163, 164, 166, 167, 169, 169, 170, 172, 173, 175, 176, 178, 179, 180, 181, 183, 184, 186, 188,
268     189, 191, 192, 194, 196, 197, 199, 201, 202, 204, 206, 209, 210, 212, 214, 217, 218, 220, 223,
269     225, 228, 230, 232, 234, 237, 239, 242, 245};
270 static int inter_minq_8[QINDEX_RANGE] = {
271     0,   0,   2,   2,   3,   4,   5,   6,   7,   8,   9,   10,  10,  11,  12,  13,  14,  15,  16,
272     17,  18,  18,  19,  20,  21,  22,  23,  24,  25,  26,  26,  27,  28,  29,  30,  31,  32,  33,
273     33,  34,  35,  36,  37,  38,  39,  40,  40,  41,  42,  43,  44,  45,  46,  47,  47,  48,  49,
274     50,  51,  52,  53,  53,  54,  55,  56,  57,  58,  59,  59,  60,  61,  62,  63,  64,  65,  65,
275     66,  67,  68,  69,  70,  71,  71,  72,  73,  74,  75,  76,  77,  77,  78,  79,  80,  81,  82,
276     83,  84,  86,  88,  89,  91,  93,  94,  96,  97,  97,  98,  99,  100, 101, 102, 102, 103, 104,
277     105, 106, 107, 107, 108, 109, 110, 111, 112, 114, 115, 116, 117, 119, 120, 121, 122, 122, 123,
278     124, 125, 126, 127, 127, 128, 129, 131, 132, 133, 134, 135, 136, 137, 138, 139, 139, 140, 141,
279     142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 157, 158, 159,
280     161, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 176, 177,
281     178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
282     196, 197, 199, 199, 200, 201, 203, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215,
283     216, 217, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230, 231, 232, 234, 235, 236, 238, 239,
284     240, 242, 243, 245, 246, 248, 250, 251, 253};
285 /*
286 static int rtc_minq_8[QINDEX_RANGE] = {
287         0, 0, 0, 0, 0, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8,
288         9, 9, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18,
289         19, 20, 20, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 28, 28,
290         29, 30, 31, 31, 32, 33, 33, 34, 35, 35, 36, 37, 37, 38, 39,
291         39, 40, 41, 41, 42, 42, 43, 44, 44, 45, 46, 46, 47, 48, 48,
292         49, 50, 50, 51, 52, 52, 53, 54, 54, 55, 56, 56, 57, 58, 58,
293         59, 60, 60, 61, 61, 62, 63, 65, 66, 67, 69, 70, 71, 72, 74,
294         75, 76, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89, 90, 91, 93,
295         94, 96, 97, 98, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
296         109, 110, 110, 112, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123,
297         124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
298         139, 140, 141, 142, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
299         153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 162, 163, 164, 165, 166,
300         167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
301         182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
302         197, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
303         216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
304         241};
305 */
306 
307 static int kf_high_motion_minq_10[QINDEX_RANGE] = {
308     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
309     0,   0,   11,  11,  11,  12,  13,  13,  14,  14,  15,  15,  16,  16,  17,  17,  18,  18,  19,
310     19,  20,  20,  21,  21,  22,  22,  22,  23,  23,  24,  24,  25,  25,  26,  26,  27,  27,  27,
311     28,  28,  29,  29,  29,  30,  30,  31,  31,  32,  32,  32,  33,  33,  33,  34,  34,  35,  35,
312     35,  36,  36,  37,  37,  37,  38,  38,  39,  39,  39,  40,  40,  41,  41,  41,  42,  42,  42,
313     43,  43,  44,  45,  45,  46,  47,  48,  48,  49,  50,  50,  51,  52,  52,  53,  54,  54,  55,
314     56,  56,  57,  58,  58,  59,  60,  61,  62,  63,  64,  64,  66,  67,  67,  69,  69,  70,  71,
315     72,  73,  74,  75,  76,  77,  79,  80,  81,  82,  84,  85,  86,  87,  88,  90,  91,  92,  94,
316     95,  96,  97,  97,  98,  99,  100, 101, 101, 102, 103, 104, 105, 105, 106, 107, 108, 109, 110,
317     111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125, 126,
318     128, 129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 139, 139, 140, 141, 143, 143, 144, 146,
319     147, 147, 149, 150, 151, 152, 153, 155, 156, 158, 159, 160, 161, 163, 164, 166, 166, 168, 170,
320     171, 173, 174, 176, 178, 179, 181, 184, 185, 187, 189, 191, 193, 195, 197, 200, 201, 204, 206,
321     209, 212, 214, 216, 219, 222, 224, 227, 230};
322 
323 static int arfgf_low_motion_minq_10[QINDEX_RANGE] = {
324     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
325     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   10,  11,  11,  11,  12,  12,  12,  13,  13,
326     13,  14,  14,  14,  15,  15,  16,  16,  16,  17,  17,  17,  17,  18,  18,  18,  19,  19,  19,
327     20,  20,  20,  21,  21,  21,  21,  22,  22,  22,  23,  23,  23,  24,  24,  24,  24,  25,  25,
328     25,  25,  26,  26,  26,  26,  27,  27,  27,  28,  28,  28,  28,  28,  29,  29,  29,  30,  30,
329     30,  30,  31,  31,  32,  32,  33,  33,  34,  34,  35,  35,  36,  36,  37,  37,  37,  38,  38,
330     39,  39,  40,  40,  41,  41,  41,  42,  42,  43,  44,  44,  45,  46,  46,  47,  48,  48,  49,
331     49,  50,  50,  51,  52,  52,  53,  54,  55,  56,  56,  57,  58,  59,  59,  60,  61,  62,  62,
332     63,  64,  65,  66,  67,  68,  69,  69,  70,  72,  72,  73,  74,  75,  77,  77,  78,  79,  80,
333     82,  83,  84,  85,  86,  87,  88,  90,  91,  92,  93,  94,  95,  96,  97,  98,  98,  99,  101,
334     101, 102, 103, 103, 104, 106, 106, 107, 108, 110, 110, 111, 113, 114, 114, 116, 117, 119, 120,
335     121, 122, 123, 125, 126, 128, 129, 131, 132, 133, 135, 136, 137, 139, 140, 142, 144, 145, 146,
336     148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 169, 171, 173, 176, 178, 181, 184, 186, 189,
337     191, 194, 197, 200, 203, 206, 210, 213, 216};
338 
339 static int arfgf_high_motion_minq_10[QINDEX_RANGE] = {
340     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   10,  11,
341     11,  12,  13,  13,  14,  14,  15,  16,  16,  17,  18,  18,  19,  19,  20,  20,  21,  22,  22,
342     23,  23,  24,  24,  25,  26,  26,  27,  27,  28,  28,  29,  30,  30,  30,  31,  32,  32,  33,
343     33,  34,  34,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,  40,  40,  41,  41,  42,  42,
344     42,  43,  44,  44,  44,  45,  45,  46,  46,  47,  47,  48,  48,  49,  49,  50,  50,  51,  51,
345     52,  52,  53,  54,  55,  56,  57,  58,  59,  60,  60,  61,  62,  63,  64,  65,  66,  67,  67,
346     68,  69,  70,  71,  72,  72,  73,  75,  76,  77,  78,  80,  81,  82,  84,  85,  86,  87,  89,
347     90,  91,  92,  94,  95,  96,  97,  98,  99,  99,  100, 101, 102, 103, 104, 105, 105, 106, 107,
348     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121, 121, 122, 123, 124, 125, 125,
349     126, 127, 128, 129, 130, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 140, 141, 142,
350     144, 145, 145, 146, 147, 148, 149, 151, 152, 152, 153, 155, 156, 156, 157, 159, 160, 161, 163,
351     163, 164, 166, 167, 169, 170, 170, 172, 173, 175, 176, 178, 179, 181, 181, 183, 184, 186, 188,
352     189, 191, 192, 194, 196, 197, 199, 201, 202, 204, 206, 209, 210, 212, 214, 217, 218, 220, 223,
353     225, 228, 230, 232, 234, 237, 240, 242, 245};
354 
355 static int inter_minq_10[QINDEX_RANGE] = {
356     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   11,  11,  12,  13,  14,  15,  16,  17,
357     18,  19,  20,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  29,  30,  31,  32,  33,  34,
358     35,  36,  37,  37,  39,  39,  40,  41,  42,  43,  44,  44,  45,  46,  47,  48,  49,  50,  51,
359     51,  52,  53,  54,  55,  56,  57,  58,  58,  59,  60,  61,  62,  62,  63,  64,  65,  66,  67,
360     68,  69,  69,  70,  71,  72,  73,  73,  74,  75,  76,  77,  78,  79,  79,  80,  81,  82,  83,
361     84,  85,  87,  88,  90,  92,  93,  95,  96,  97,  98,  99,  99,  100, 101, 102, 103, 104, 104,
362     105, 106, 107, 108, 109, 109, 110, 111, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123,
363     124, 125, 126, 127, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 140, 141,
364     142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 158, 160,
365     161, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 177,
366     178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
367     196, 197, 199, 199, 200, 201, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215,
368     216, 218, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230, 231, 232, 234, 235, 236, 238, 239,
369     240, 242, 243, 245, 246, 248, 250, 251, 253};
370 /*
371 static int rtc_minq_10[QINDEX_RANGE] = {
372         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,
373         11, 12, 13, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22,
374         22, 23, 24, 24, 25, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32,
375         33, 34, 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 41, 42, 42,
376         43, 44, 44, 45, 46, 46, 47, 48, 48, 49, 50, 50, 51, 51, 52,
377         53, 53, 54, 55, 55, 56, 56, 57, 58, 58, 59, 60, 60, 61, 62,
378         62, 63, 63, 64, 64, 65, 67, 68, 69, 70, 71, 72, 74, 75, 76,
379         77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 93, 94,
380         95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 105, 106, 107, 108,
381         109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 123, 124,
382         124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
383         139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 151, 152,
384         153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 163, 164, 165, 166,
385         167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
386         182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
387         198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
388         216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
389         241};
390 */
391 
392 static int kf_high_motion_minq_12[QINDEX_RANGE] = {
393     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
394     0,   0,   0,   0,   13,  14,  14,  15,  15,  16,  16,  17,  17,  18,  18,  19,  19,  20,  20,
395     21,  21,  22,  22,  23,  23,  23,  24,  24,  25,  25,  26,  26,  27,  27,  28,  28,  28,  29,
396     29,  30,  30,  31,  31,  31,  32,  32,  33,  33,  33,  34,  34,  35,  35,  35,  36,  36,  37,
397     37,  37,  38,  38,  39,  39,  39,  40,  40,  40,  41,  41,  41,  42,  42,  43,  43,  43,  44,
398     44,  45,  45,  46,  47,  47,  48,  49,  49,  50,  51,  51,  52,  53,  53,  54,  55,  55,  56,
399     57,  57,  58,  59,  59,  60,  61,  62,  63,  64,  64,  65,  66,  67,  68,  69,  70,  71,  72,
400     73,  74,  75,  76,  77,  78,  79,  80,  82,  83,  84,  85,  86,  88,  89,  90,  91,  92,  94,
401     95,  96,  97,  98,  98,  99,  100, 101, 101, 102, 103, 104, 105, 106, 107, 107, 108, 109, 110,
402     111, 112, 113, 114, 115, 115, 116, 117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125, 126,
403     127, 128, 128, 129, 130, 131, 132, 132, 133, 134, 135, 136, 137, 137, 138, 139, 139, 140, 141,
404     142, 142, 143, 144, 145, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154, 155, 155, 156,
405     157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 170, 171, 172, 173, 175, 176, 178, 179,
406     181, 183, 184, 186, 188, 190, 191, 193, 195};
407 
408 static int arfgf_low_motion_minq_12[QINDEX_RANGE] = {
409     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
410     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   13,  13,  14,  14,  14,  15,  15,
411     15,  16,  16,  16,  17,  17,  17,  18,  18,  18,  19,  19,  19,  20,  20,  20,  21,  21,  21,
412     22,  22,  22,  22,  23,  23,  23,  24,  24,  24,  25,  25,  25,  25,  26,  26,  26,  26,  27,
413     27,  27,  28,  28,  28,  28,  29,  29,  29,  29,  30,  30,  30,  30,  31,  31,  31,  31,  32,
414     32,  32,  33,  33,  34,  34,  35,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,  39,  40,
415     40,  41,  41,  42,  42,  42,  43,  43,  44,  45,  45,  46,  46,  47,  48,  48,  49,  49,  50,
416     51,  51,  52,  52,  53,  54,  54,  55,  56,  57,  57,  58,  59,  60,  60,  61,  62,  63,  63,
417     64,  65,  66,  67,  68,  69,  70,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,
418     82,  83,  84,  86,  87,  88,  89,  90,  91,  92,  94,  95,  96,  96,  97,  98,  98,  99,  100,
419     100, 101, 102, 102, 103, 104, 105, 105, 106, 107, 108, 108, 109, 110, 111, 111, 112, 113, 114,
420     115, 115, 116, 117, 118, 119, 120, 121, 122, 122, 123, 124, 124, 125, 126, 127, 128, 129, 129,
421     130, 131, 132, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 146, 147, 149, 151, 152, 154,
422     155, 157, 159, 161, 163, 165, 167, 169, 171};
423 
424 static int arfgf_high_motion_minq_12[QINDEX_RANGE] = {
425     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
426     0,   13,  14,  14,  15,  16,  16,  17,  17,  18,  19,  19,  20,  20,  21,  22,  22,  23,  23,
427     24,  25,  25,  26,  26,  27,  27,  28,  28,  29,  30,  30,  31,  31,  32,  32,  33,  33,  34,
428     34,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,  40,  40,  41,  41,  42,  42,  43,  43,
429     44,  44,  45,  45,  46,  46,  47,  47,  48,  48,  49,  49,  49,  50,  50,  51,  51,  52,  52,
430     53,  53,  54,  55,  56,  57,  58,  59,  59,  60,  61,  62,  63,  64,  65,  65,  66,  67,  68,
431     69,  70,  71,  71,  72,  73,  74,  75,  77,  78,  79,  80,  82,  83,  84,  85,  87,  88,  89,
432     90,  92,  93,  94,  95,  96,  97,  98,  99,  100, 101, 101, 102, 103, 104, 105, 106, 106, 107,
433     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 119, 120, 121, 122, 122, 123, 124, 125, 125,
434     126, 127, 128, 129, 130, 131, 132, 132, 133, 134, 135, 136, 137, 138, 139, 140, 140, 141, 142,
435     143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 154, 155, 156, 157, 158,
436     158, 159, 160, 161, 162, 163, 163, 164, 165, 166, 167, 168, 169, 170, 170, 171, 172, 173, 174,
437     175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 187, 188, 189, 190, 192, 193, 194, 196,
438     197, 199, 200, 202, 203, 205, 207, 208, 210};
439 
440 static int inter_minq_12[QINDEX_RANGE] = {
441     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   13,  14,  15,  16,  17,
442     18,  19,  20,  21,  22,  23,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  32,  33,  34,
443     35,  36,  37,  38,  39,  40,  40,  41,  42,  43,  44,  45,  46,  47,  47,  48,  49,  50,  51,
444     52,  53,  53,  54,  55,  56,  57,  58,  59,  59,  60,  61,  62,  63,  64,  65,  65,  66,  67,
445     68,  69,  70,  70,  71,  72,  73,  74,  75,  76,  76,  77,  78,  79,  80,  80,  81,  82,  83,
446     84,  85,  87,  89,  90,  92,  93,  95,  96,  97,  98,  99,  99,  100, 101, 102, 103, 104, 104,
447     105, 106, 107, 108, 109, 109, 110, 111, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123,
448     124, 125, 126, 127, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 140, 141,
449     142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 158, 160,
450     161, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 177,
451     178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
452     196, 197, 199, 199, 200, 201, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215,
453     216, 217, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230, 231, 232, 234, 235, 236, 238, 239,
454     240, 242, 243, 245, 246, 248, 250, 251, 253};
455 /*
456 static int rtc_minq_12[QINDEX_RANGE] = {
457         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
458         0, 0, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22, 22,
459         23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 33,
460         34, 34, 35, 36, 37, 37, 38, 39, 39, 40, 41, 41, 42, 43, 43,
461         44, 45, 45, 46, 46, 47, 48, 48, 49, 50, 50, 51, 52, 52, 53,
462         54, 54, 55, 55, 56, 57, 57, 58, 58, 59, 60, 60, 61, 62, 62,
463         63, 63, 64, 65, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76,
464         78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94,
465         95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 108,
466         109, 110, 111, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
467         124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
468         139, 140, 141, 142, 143, 144, 145, 146, 146, 147, 148, 149, 150, 151, 152,
469         153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 163, 164, 165, 166,
470         167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
471         182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
472         197, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
473         216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
474         241};
475 */
476 
477 static int gf_high_tpl_la = 2400;
478 static int gf_low_tpl_la  = 300;
479 static int kf_high        = 5000;
480 static int kf_low         = 400;
481 
get_active_quality(int q,int gfu_boost,int low,int high,int * low_motion_minq,int * high_motion_minq)482 static int get_active_quality(int q, int gfu_boost, int low, int high, int *low_motion_minq,
483                               int *high_motion_minq) {
484     if (gfu_boost > high)
485         return low_motion_minq[q];
486     else if (gfu_boost < low)
487         return high_motion_minq[q];
488     else {
489         const int gap        = high - low;
490         const int offset     = high - gfu_boost;
491         const int qdiff      = high_motion_minq[q] - low_motion_minq[q];
492         const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
493         return low_motion_minq[q] + adjustment;
494     }
495 }
get_kf_active_quality_tpl(const RATE_CONTROL * const rc,int q,AomBitDepth bit_depth)496 static int get_kf_active_quality_tpl(const RATE_CONTROL *const rc, int q, AomBitDepth bit_depth) {
497     int *kf_low_motion_minq_cqp;
498     int *kf_high_motion_minq;
499     ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq_cqp);
500     ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
501     return get_active_quality(
502         q, rc->kf_boost, kf_low, kf_high, kf_low_motion_minq_cqp, kf_high_motion_minq);
503 }
get_gf_active_quality_tpl_la(const RATE_CONTROL * const rc,int q,AomBitDepth bit_depth)504 static int get_gf_active_quality_tpl_la(const RATE_CONTROL *const rc, int q,
505                                         AomBitDepth bit_depth) {
506     int *arfgf_low_motion_minq;
507     int *arfgf_high_motion_minq;
508     ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
509     ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
510     return get_active_quality(q,
511                               rc->gfu_boost,
512                               gf_low_tpl_la,
513                               gf_high_tpl_la,
514                               arfgf_low_motion_minq,
515                               arfgf_high_motion_minq);
516 }
get_gf_high_motion_quality(int q,AomBitDepth bit_depth)517 static int get_gf_high_motion_quality(int q, AomBitDepth bit_depth) {
518     int *arfgf_high_motion_minq;
519     ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
520     return arfgf_high_motion_minq[q];
521 }
522 int16_t svt_av1_dc_quant_qtx(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
523 
get_cqp_kf_boost_from_r0(double r0,int frames_to_key,EbInputResolution input_resolution)524 static int get_cqp_kf_boost_from_r0(double r0, int frames_to_key,
525                                     EbInputResolution input_resolution) {
526     double factor;
527     // when frames_to_key not available, it is set to -1. In this case the factor is set to average of min and max
528     if (frames_to_key == -1)
529         factor = (10.0 + 4.0) / 2;
530     else {
531         factor = sqrt((double)frames_to_key);
532         factor = AOMMIN(factor, 10.0);
533         factor = AOMMAX(factor, 4.0);
534     }
535     const int is_720p_or_smaller = input_resolution <= INPUT_SIZE_720p_RANGE;
536     const int boost = is_720p_or_smaller ? (int)rint(3 * (75.0 + 17.0 * factor) / 2 / r0)
537                                          : (int)rint(2 * (75.0 + 17.0 * factor) / r0);
538     return boost;
539 }
540 
svt_av1_get_gfu_boost_projection_factor(double min_factor,double max_factor,int frame_count)541 double svt_av1_get_gfu_boost_projection_factor(double min_factor, double max_factor,
542                                                int frame_count) {
543     double factor = sqrt((double)frame_count);
544     factor        = AOMMIN(factor, max_factor);
545     factor        = AOMMAX(factor, min_factor);
546     factor        = (200.0 + 10.0 * factor);
547     return factor;
548 }
549 
550 #define MAX_GFUBOOST_FACTOR 10.0
551 //#define MIN_GFUBOOST_FACTOR 4.0
get_gfu_boost_from_r0_lap(double min_factor,double max_factor,double r0,int frames_to_key)552 static int get_gfu_boost_from_r0_lap(double min_factor, double max_factor, double r0,
553                                      int frames_to_key) {
554     double factor = svt_av1_get_gfu_boost_projection_factor(min_factor, max_factor, frames_to_key);
555     const int boost = (int)rint(factor / r0);
556     return boost;
557 }
558 
svt_av1_get_deltaq_offset(AomBitDepth bit_depth,int qindex,double beta,EB_SLICE slice_type)559 int svt_av1_get_deltaq_offset(AomBitDepth bit_depth, int qindex, double beta, EB_SLICE slice_type) {
560     assert(beta > 0.0);
561     int q = svt_av1_dc_quant_qtx(qindex, 0, bit_depth);
562     int newq;
563     // use a less aggressive action when lowering the q for non I_slice
564     if (slice_type != I_SLICE && beta > 1)
565         newq = (int)rint(q / sqrt(sqrt(beta)));
566     else
567         newq = (int)rint(q / sqrt(beta));
568     int orig_qindex = qindex;
569     if (newq < q) {
570         do {
571             qindex--;
572             q = svt_av1_dc_quant_qtx(qindex, 0, bit_depth);
573         } while (newq < q && qindex > 0);
574     } else {
575         do {
576             qindex++;
577             q = svt_av1_dc_quant_qtx(qindex, 0, bit_depth);
578         } while (newq > q && qindex < MAXQ);
579     }
580     return qindex - orig_qindex;
581 }
582 
583 #define MIN_BPB_FACTOR 0.005
584 #define MAX_BPB_FACTOR 50
svt_av1_rc_bits_per_mb(FrameType frame_type,int qindex,double correction_factor,const int bit_depth,const int is_screen_content_type)585 int svt_av1_rc_bits_per_mb(FrameType frame_type, int qindex, double correction_factor,
586                            const int bit_depth, const int is_screen_content_type) {
587     const double q          = svt_av1_convert_qindex_to_q(qindex, bit_depth);
588     int          enumerator = frame_type == KEY_FRAME ? 1400000 : 1000000;
589     if (is_screen_content_type) {
590         enumerator = frame_type == KEY_FRAME ? 1000000 : 750000;
591     }
592     assert(correction_factor <= MAX_BPB_FACTOR && correction_factor >= MIN_BPB_FACTOR);
593 
594     // q based adjustment to baseline enumerator
595     return (int)(enumerator * correction_factor / q);
596 }
597 
find_qindex_by_rate(int desired_bits_per_mb,const int bit_depth,FrameType frame_type,const int is_screen_content_type,int best_qindex,int worst_qindex)598 static int find_qindex_by_rate(int desired_bits_per_mb, const int bit_depth, FrameType frame_type,
599                                const int is_screen_content_type, int best_qindex,
600                                int worst_qindex) {
601     assert(best_qindex <= worst_qindex);
602     int low  = best_qindex;
603     int high = worst_qindex;
604     while (low < high) {
605         const int mid             = (low + high) >> 1;
606         const int mid_bits_per_mb = svt_av1_rc_bits_per_mb(
607             frame_type, mid, 1.0, bit_depth, is_screen_content_type);
608         if (mid_bits_per_mb > desired_bits_per_mb) {
609             low = mid + 1;
610         } else {
611             high = mid;
612         }
613     }
614     assert(low == high);
615     assert(svt_av1_rc_bits_per_mb(frame_type, low, 1.0, bit_depth, is_screen_content_type) <=
616                desired_bits_per_mb ||
617            low == worst_qindex);
618     return low;
619 }
620 
svt_av1_compute_qdelta_by_rate(const RATE_CONTROL * rc,FrameType frame_type,int qindex,double rate_target_ratio,const int bit_depth,const int is_screen_content_type)621 int svt_av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FrameType frame_type, int qindex,
622                                    double rate_target_ratio, const int bit_depth,
623                                    const int is_screen_content_type) {
624     // Look up the current projected bits per block for the base index
625     const int base_bits_per_mb = svt_av1_rc_bits_per_mb(
626         frame_type, qindex, 1.0, bit_depth, is_screen_content_type);
627 
628     // Find the target bits per mb based on the base value and given ratio.
629     const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
630 
631     const int target_index = find_qindex_by_rate(target_bits_per_mb,
632                                                  bit_depth,
633                                                  frame_type,
634                                                  is_screen_content_type,
635                                                  rc->best_quality,
636                                                  rc->worst_quality);
637     return target_index - qindex;
638 }
639 
640 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
641     1.00, // INTER_NORMAL
642     1.00, // INTER_LOW
643     1.00, // INTER_HIGH
644     1.50, // GF_ARF_LOW
645     2.00, // GF_ARF_STD
646     2.00, // KF_STD
647 };
648 
svt_av1_frame_type_qdelta(RATE_CONTROL * rc,int rf_level,int q,const int bit_depth,const int sc_content_detected)649 int svt_av1_frame_type_qdelta(RATE_CONTROL *rc, int rf_level, int q, const int bit_depth,
650                               const int sc_content_detected) {
651     const int /*rate_factor_level*/ rf_lvl     = rf_level; //get_rate_factor_level(&cpi->gf_group);
652     const FrameType                 frame_type = (rf_lvl == KF_STD) ? KEY_FRAME : INTER_FRAME;
653     double                          rate_factor;
654 
655     rate_factor = rate_factor_deltas[rf_lvl];
656     if (rf_lvl == GF_ARF_LOW) {
657         rate_factor -= (0 /*cpi->gf_group.layer_depth[cpi->gf_group.index]*/ - 2) * 0.1;
658         rate_factor = AOMMAX(rate_factor, 1.0);
659     }
660     return svt_av1_compute_qdelta_by_rate(
661         rc, frame_type, q, rate_factor, bit_depth, sc_content_detected);
662 }
663 
664 static const rate_factor_level rate_factor_levels[FRAME_UPDATE_TYPES] = {
665     KF_STD, // KF_UPDATE
666     INTER_NORMAL, // LF_UPDATE
667     GF_ARF_STD, // GF_UPDATE
668     GF_ARF_STD, // ARF_UPDATE
669     INTER_NORMAL, // OVERLAY_UPDATE
670     INTER_NORMAL, // INTNL_OVERLAY_UPDATE
671     GF_ARF_LOW, // INTNL_ARF_UPDATE
672 };
673 
get_rate_factor_level(const GF_GROUP * const gf_group,unsigned char gf_group_index)674 static rate_factor_level get_rate_factor_level(const GF_GROUP *const gf_group,
675                                                unsigned char         gf_group_index) {
676     const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group_index];
677     assert(update_type < FRAME_UPDATE_TYPES);
678     return rate_factor_levels[update_type];
679 }
680 
av1_frame_type_qdelta_org(RATE_CONTROL * rc,GF_GROUP * gf_group,unsigned char gf_group_index,int q,const int bit_depth,uint8_t sc_content_detected)681 int av1_frame_type_qdelta_org(RATE_CONTROL *rc, GF_GROUP *gf_group, unsigned char gf_group_index,
682                               int q, const int bit_depth, uint8_t sc_content_detected) {
683     const rate_factor_level rf_lvl     = get_rate_factor_level(gf_group, gf_group_index);
684     const FrameType         frame_type = (rf_lvl == KF_STD) ? KEY_FRAME : INTER_FRAME;
685     double                  rate_factor;
686 
687     rate_factor = rate_factor_deltas[rf_lvl];
688     //anaghdin: to remove?
689     if (rf_lvl == GF_ARF_LOW) {
690         rate_factor -= (gf_group->layer_depth[gf_group_index] - 2) * 0.1;
691         rate_factor = AOMMAX(rate_factor, 1.0);
692     }
693     return svt_av1_compute_qdelta_by_rate(
694         rc, frame_type, q, rate_factor, bit_depth, sc_content_detected);
695 }
696 
adjust_active_best_and_worst_quality_org(PictureControlSet * pcs_ptr,RATE_CONTROL * rc,int * active_worst,int * active_best)697 static void adjust_active_best_and_worst_quality_org(PictureControlSet *pcs_ptr, RATE_CONTROL *rc,
698                                                      int *active_worst, int *active_best) {
699     int                 active_best_quality   = *active_best;
700     int                 active_worst_quality  = *active_worst;
701     int                 this_key_frame_forced = 0;
702     SequenceControlSet *scs_ptr               = pcs_ptr->parent_pcs_ptr->scs_ptr;
703     const int           bit_depth             = scs_ptr->static_config.encoder_bit_depth;
704 
705     EncodeContext *                    encode_context_ptr = scs_ptr->encode_context_ptr;
706     TWO_PASS *const                    twopass            = &scs_ptr->twopass;
707     const enum aom_rc_mode             rc_mode            = encode_context_ptr->rc_cfg.mode;
708     GF_GROUP *                         gf_group           = &encode_context_ptr->gf_group;
709     const RefreshFrameFlagsInfo *const refresh_frame_flags =
710         &pcs_ptr->parent_pcs_ptr->refresh_frame;
711     int is_intrl_arf_boost = gf_group->update_type[pcs_ptr->parent_pcs_ptr->gf_group_index] ==
712         INTNL_ARF_UPDATE;
713     this_key_frame_forced = rc->this_key_frame_forced;
714     // Extension to max or min Q if undershoot or overshoot is outside
715     // the permitted range.
716     if (rc_mode != AOM_Q) {
717         if (frame_is_intra_only(pcs_ptr->parent_pcs_ptr) ||
718             (1 /*!rc->is_src_frame_alt_ref*/ &&
719              (refresh_frame_flags->golden_frame || is_intrl_arf_boost ||
720               refresh_frame_flags->alt_ref_frame))) {
721             active_best_quality -= (twopass->extend_minq + twopass->extend_minq_fast);
722             active_worst_quality += (twopass->extend_maxq / 2);
723         } else {
724             active_best_quality -= (twopass->extend_minq + twopass->extend_minq_fast) / 2;
725             active_worst_quality += twopass->extend_maxq;
726         }
727     }
728 
729     // Static forced key frames Q restrictions dealt with elsewhere.
730     if (!frame_is_intra_only(pcs_ptr->parent_pcs_ptr) || !this_key_frame_forced ||
731         (twopass->last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
732         const int qdelta     = av1_frame_type_qdelta_org(rc,
733                                                      gf_group,
734                                                      pcs_ptr->parent_pcs_ptr->gf_group_index,
735                                                      active_worst_quality,
736                                                      bit_depth,
737                                                      pcs_ptr->parent_pcs_ptr->sc_class1);
738         active_worst_quality = AOMMAX(active_worst_quality + qdelta, active_best_quality);
739     }
740 
741     active_best_quality  = clamp(active_best_quality, rc->best_quality, rc->worst_quality);
742     active_worst_quality = clamp(active_worst_quality, active_best_quality, rc->worst_quality);
743 
744     *active_best  = active_best_quality;
745     *active_worst = active_worst_quality;
746 }
747 
adjust_active_best_and_worst_quality(PictureControlSet * pcs_ptr,RATE_CONTROL * rc,int rf_level,int * active_worst,int * active_best)748 static void adjust_active_best_and_worst_quality(PictureControlSet *pcs_ptr, RATE_CONTROL *rc,
749                                                  int rf_level, int *active_worst,
750                                                  int *active_best) {
751     int active_best_quality  = *active_best;
752     int active_worst_quality = *active_worst;
753     ;
754     SequenceControlSet *scs_ptr   = pcs_ptr->parent_pcs_ptr->scs_ptr;
755     const int           bit_depth = scs_ptr->static_config.encoder_bit_depth;
756 
757     // Static forced key frames Q restrictions dealt with elsewhere.
758     if (!frame_is_intra_only(pcs_ptr->parent_pcs_ptr)
759         /*|| (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)*/) {
760         const int qdelta     = svt_av1_frame_type_qdelta(rc,
761                                                      rf_level,
762                                                      active_worst_quality,
763                                                      bit_depth,
764                                                      pcs_ptr->parent_pcs_ptr->sc_class1);
765         active_worst_quality = AOMMAX(active_worst_quality + qdelta, active_best_quality);
766     }
767 
768     active_best_quality  = clamp(active_best_quality, rc->best_quality, rc->worst_quality);
769     active_worst_quality = clamp(active_worst_quality, active_best_quality, rc->worst_quality);
770 
771     *active_best  = active_best_quality;
772     *active_worst = active_worst_quality;
773 }
774 
775 /******************************************************
776  * cqp_qindex_calc_tpl_la
777  * Assign the q_index per frame.
778  * Used in the one pass encoding with tpl stats
779  ******************************************************/
cqp_qindex_calc_tpl_la(PictureControlSet * pcs_ptr,RATE_CONTROL * rc,int qindex)780 static int cqp_qindex_calc_tpl_la(PictureControlSet *pcs_ptr, RATE_CONTROL *rc, int qindex) {
781     SequenceControlSet *scs_ptr              = pcs_ptr->parent_pcs_ptr->scs_ptr;
782     const int           cq_level             = qindex;
783     int                 active_best_quality  = 0;
784     int                 active_worst_quality = qindex;
785     rc->arf_q                                = 0;
786     int q;
787     int refresh_golden_frame, refresh_alt_ref_frame, is_intrl_arf_boost, rf_level;
788     refresh_golden_frame  = frame_is_intra_only(pcs_ptr->parent_pcs_ptr) ? 1 : 0;
789     refresh_alt_ref_frame = (pcs_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? 1 : 0;
790     is_intrl_arf_boost    = (pcs_ptr->parent_pcs_ptr->temporal_layer_index > 0 &&
791                           pcs_ptr->parent_pcs_ptr->is_used_as_reference_flag)
792            ? 1
793            : 0;
794     rf_level              = (frame_is_intra_only(pcs_ptr->parent_pcs_ptr))  ? KF_STD
795                      : (pcs_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? GF_ARF_STD
796                      : pcs_ptr->parent_pcs_ptr->is_used_as_reference_flag   ? GF_ARF_LOW
797                                                                             : INTER_NORMAL;
798 
799     const int bit_depth = scs_ptr->static_config.encoder_bit_depth;
800     // Since many frames can be processed at the same time, storing/using arf_q in rc param is not sufficient and will create a run to run.
801     // So, for each frame, arf_q is updated based on the qp of its references.
802     rc->arf_q = MAX(rc->arf_q, ((pcs_ptr->ref_pic_qp_array[0][0] << 2) + 2));
803     if (pcs_ptr->slice_type == B_SLICE)
804         rc->arf_q = MAX(rc->arf_q, ((pcs_ptr->ref_pic_qp_array[1][0] << 2) + 2));
805 
806     if (frame_is_intra_only(pcs_ptr->parent_pcs_ptr)) {
807         // Not forced keyframe.
808         double q_adj_factor = 1.0;
809         double q_val;
810         rc->worst_quality = MAXQ;
811         rc->best_quality  = MINQ;
812         // The new tpl only looks at pictures in tpl group, which is fewer than before,
813         // As a results, we defined a factor to adjust r0
814         if (pcs_ptr->parent_pcs_ptr->frm_hdr.frame_type != KEY_FRAME) {
815             double factor;
816             if (!scs_ptr->lad_mg)
817                 factor = 2;
818             else
819                 factor = 1;
820             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / factor;
821         }
822         pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / tpl_hl_islice_div_factor[scs_ptr->static_config.hierarchical_levels];
823         if (pcs_ptr->parent_pcs_ptr->frm_hdr.frame_type == KEY_FRAME) {
824             if (scs_ptr->intra_period_length == -1 || scs_ptr->intra_period_length > KF_INTERVAL_TH) {
825                 double factor = 1.0;
826                 if (pcs_ptr->parent_pcs_ptr->r0 < 0.2){
827                     double mult = 1.0;
828                     factor = (double)(mult * 255.0) / qindex;
829                 }
830                 pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / factor;
831             }
832         }
833         // when frames_to_key not available, i.e. in 1 pass encoding
834         rc->kf_boost = get_cqp_kf_boost_from_r0(
835             pcs_ptr->parent_pcs_ptr->r0, -1, scs_ptr->input_resolution);
836         // NM: TODO: replaced by unitary number X * number of pictures in GOP // 93.75 * number of pictures in GOP
837         int max_boost = scs_ptr->intra_period_length < KF_INTERVAL_TH ? MAX_KF_BOOST_LOW_KI : MAX_KF_BOOST_HIGHT_KI;
838         rc->kf_boost = AOMMIN(rc->kf_boost, max_boost);
839         // Baseline value derived from cpi->active_worst_quality and kf boost.
840         active_best_quality = get_kf_active_quality_tpl(rc, active_worst_quality, bit_depth);
841         // Allow somewhat lower kf minq with small image formats.
842         if (pcs_ptr->parent_pcs_ptr->input_resolution == INPUT_SIZE_240p_RANGE)
843             q_adj_factor -= (pcs_ptr->parent_pcs_ptr->tune_tpl_for_chroma) ? 0.2 : 0.15;
844         // Make a further adjustment based on the kf zero motion measure.
845 
846         // Convert the adjustment factor to a qindex delta
847         // on active_best_quality.
848         q_val = svt_av1_convert_qindex_to_q(active_best_quality, bit_depth);
849         active_best_quality += svt_av1_compute_qdelta(q_val, q_val * q_adj_factor, bit_depth);
850     } else if (refresh_golden_frame || is_intrl_arf_boost || refresh_alt_ref_frame) {
851         double min_boost_factor = sqrt(1 << pcs_ptr->parent_pcs_ptr->hierarchical_levels);
852         // The new tpl only looks at pictures in tpl group, which is fewer than before,
853         // As a results, we defined a factor to adjust r0
854         if (pcs_ptr->parent_pcs_ptr->temporal_layer_index == 0) {
855             double div_factor = 1;
856             if (scs_ptr->lad_mg)
857             {
858                 div_factor = pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor ?
859                     pcs_ptr->parent_pcs_ptr->used_tpl_frame_num * pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor :
860                     div_factor;
861             }
862             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / div_factor;
863             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / tpl_hl_base_frame_div_factor[scs_ptr->static_config.hierarchical_levels];
864         }
865 
866         int num_stats_required_for_gfu_boost = pcs_ptr->parent_pcs_ptr->tpl_group_size +
867             (1 << pcs_ptr->parent_pcs_ptr->hierarchical_levels);
868 
869         rc->gfu_boost        = get_gfu_boost_from_r0_lap(min_boost_factor,
870                                                   MAX_GFUBOOST_FACTOR,
871                                                   pcs_ptr->parent_pcs_ptr->r0,
872                                                   num_stats_required_for_gfu_boost);
873         rc->arf_boost_factor = (pcs_ptr->ref_slice_type_array[0][0] == I_SLICE &&
874                                 pcs_ptr->ref_pic_r0[0][0] - pcs_ptr->parent_pcs_ptr->r0 >= 0.08)
875             ? (float_t)1.3
876             : (float_t)1;
877         q                    = active_worst_quality;
878 
879         // non ref frame or repeated frames with re-encode
880         if (!refresh_alt_ref_frame && !is_intrl_arf_boost)
881             active_best_quality = cq_level;
882         else {
883             if (!is_intrl_arf_boost) {
884                 active_best_quality = get_gf_active_quality_tpl_la(rc, q, bit_depth);
885                 rc->arf_q           = active_best_quality;
886                 const int min_boost = get_gf_high_motion_quality(q, bit_depth);
887                 const int boost     = min_boost - active_best_quality;
888                 active_best_quality = min_boost - (int)(boost * rc->arf_boost_factor);
889             } else {
890                 EbReferenceObject *ref_obj_l0 =
891                     (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[REF_LIST_0][0]->object_ptr;
892                 EbReferenceObject *ref_obj_l1 = NULL;
893                 if (pcs_ptr->slice_type == B_SLICE)
894                     ref_obj_l1 =
895                         (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[REF_LIST_1][0]->object_ptr;
896 
897                 uint8_t ref_tmp_layer = ref_obj_l0->tmp_layer_idx;
898                 if (pcs_ptr->slice_type == B_SLICE)
899                     ref_tmp_layer = MAX(ref_tmp_layer, ref_obj_l1->tmp_layer_idx);
900                 active_best_quality    = rc->arf_q;
901                 int8_t tmp_layer_delta = (int8_t)pcs_ptr->parent_pcs_ptr->temporal_layer_index -
902                     (int8_t)ref_tmp_layer;
903                 // active_best_quality is updated with the q index of the reference
904                 if (rf_level == GF_ARF_LOW) {
905                     while (tmp_layer_delta--)
906                         active_best_quality = (active_best_quality + cq_level + 1) / 2;
907                 }
908             }
909             // For alt_ref and GF frames (including internal arf frames) adjust the
910             // worst allowed quality as well. This insures that even on hard
911             // sections we dont clamp the Q at the same value for arf frames and
912             // leaf (non arf) frames. This is important to the TPL model which assumes
913             // Q drops with each arf level.
914             active_worst_quality = (active_best_quality + (3 * active_worst_quality) + 2) / 4;
915         }
916     } else
917         active_best_quality = cq_level;
918 
919     adjust_active_best_and_worst_quality(
920         pcs_ptr, rc, rf_level, &active_worst_quality, &active_best_quality);
921     q = active_best_quality;
922     clamp(q, active_best_quality, active_worst_quality);
923 
924     return q;
925 }
926 
927 #define DEFAULT_KF_BOOST 2700
928 #define DEFAULT_GF_BOOST 1350
929 #define FIXED_QP_OFFSET_COUNT 5
930 static const int percents[FIXED_QP_OFFSET_COUNT] = { 76, 60, 30, 15, 8 };
931 /******************************************************
932  * cqp_qindex_calc
933  * Assign the q_index per frame.
934  * Used in the one pass encoding with no look ahead
935  ******************************************************/
cqp_qindex_calc(PictureControlSet * pcs_ptr,RATE_CONTROL * rc,int qindex)936 static int cqp_qindex_calc(PictureControlSet *pcs_ptr, RATE_CONTROL *rc, int qindex) {
937     SequenceControlSet *scs_ptr = pcs_ptr->parent_pcs_ptr->scs_ptr;
938 
939     int       active_best_quality  = 0;
940     int       active_worst_quality = qindex;
941     int       q;
942     const int bit_depth = scs_ptr->static_config.encoder_bit_depth;
943     // Since many frames can be processed at the same time, storing/using arf_q in rc param is not sufficient and will create a run to run.
944     // So, for each frame, arf_q is updated based on the qp of its references.
945     rc->arf_q = 0;
946     if (pcs_ptr->ref_slice_type_array[0][0] != I_SLICE)
947         rc->arf_q = MAX(rc->arf_q, ((pcs_ptr->ref_pic_qp_array[0][0] << 2) + 2));
948     if ((pcs_ptr->slice_type == B_SLICE) && (pcs_ptr->ref_slice_type_array[1][0] != I_SLICE))
949         rc->arf_q = MAX(rc->arf_q, ((pcs_ptr->ref_pic_qp_array[1][0] << 2) + 2));
950     double q_val = svt_av1_convert_qindex_to_q(qindex, bit_depth);
951 
952     int offset_idx = -1;
953     if (!pcs_ptr->parent_pcs_ptr->is_used_as_reference_flag)
954         offset_idx = -1;
955     else if (pcs_ptr->slice_type == I_SLICE)
956         offset_idx = 0;
957     else
958         offset_idx = MIN(pcs_ptr->temporal_layer_index + 1, FIXED_QP_OFFSET_COUNT-1);
959 
960     const double q_val_target = (offset_idx == -1) ?
961         q_val :
962         MAX(q_val - (q_val * percents[offset_idx] / 100), 0.0);
963 
964     const int32_t delta_qindex = svt_av1_compute_qdelta(
965         q_val,
966         q_val_target,
967         bit_depth);
968 
969     active_best_quality = (int32_t)(qindex + delta_qindex);
970     q = active_best_quality;
971     clamp(q, active_best_quality, active_worst_quality);
972 
973     return q;
974 }
975 
976 // The table we use is modified from libaom; here is the original, from libaom:
977 // static const int rd_frame_type_factor[FRAME_UPDATE_TYPES] = { 128, 144, 128,
978 //                                                               128, 144, 144,
979 //                                                               128 };
980 static const int rd_frame_type_factor[FRAME_UPDATE_TYPES] = {128, 164, 128, 128, 164, 164, 128};
981 /*
982  * Set the sse lambda based on the bit_depth, then update based on frame position.
983  */
compute_rdmult_sse(PictureControlSet * pcs_ptr,uint8_t q_index,uint8_t bit_depth)984 int compute_rdmult_sse(PictureControlSet *pcs_ptr, uint8_t q_index, uint8_t bit_depth) {
985     FrameType frame_type = pcs_ptr->parent_pcs_ptr->frm_hdr.frame_type;
986     // To set gf_update_type based on current TL vs. the max TL (e.g. for 5L, max TL is 4)
987     uint8_t temporal_layer_index = pcs_ptr->temporal_layer_index;
988     uint8_t max_temporal_layer   = pcs_ptr->parent_pcs_ptr->hierarchical_levels;
989 
990     int64_t rdmult = bit_depth == 8 ? av1_lambda_mode_decision8_bit_sse[q_index]
991         : bit_depth == 10           ? av1lambda_mode_decision10_bit_sse[q_index]
992                                     : av1lambda_mode_decision12_bit_sse[q_index];
993 
994     // Update rdmult based on the frame's position in the miniGOP
995     if (frame_type != KEY_FRAME) {
996         uint8_t gf_update_type = temporal_layer_index == 0 ? ARF_UPDATE
997             : temporal_layer_index < max_temporal_layer    ? INTNL_ARF_UPDATE
998                                                            : LF_UPDATE;
999         rdmult                 = (rdmult * rd_frame_type_factor[gf_update_type]) >> 7;
1000     }
1001     return (int)rdmult;
1002 }
sb_setup_lambda(PictureControlSet * pcs_ptr,SuperBlock * sb_ptr)1003 static void sb_setup_lambda(PictureControlSet *pcs_ptr, SuperBlock *sb_ptr) {
1004     const Av1Common *const   cm         = pcs_ptr->parent_pcs_ptr->av1_cm;
1005     PictureParentControlSet *ppcs_ptr   = pcs_ptr->parent_pcs_ptr;
1006     SequenceControlSet *     scs_ptr    = ppcs_ptr->scs_ptr;
1007     const int                bsize_base = BLOCK_16X16;
1008     const int                num_mi_w   = mi_size_wide[bsize_base];
1009     const int                num_mi_h   = mi_size_high[bsize_base];
1010     const int                num_cols   = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
1011     const int                num_rows   = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
1012     const int num_bcols = (mi_size_wide[scs_ptr->seq_header.sb_size] + num_mi_w - 1) / num_mi_w;
1013     const int num_brows = (mi_size_high[scs_ptr->seq_header.sb_size] + num_mi_h - 1) / num_mi_h;
1014     int       mi_col    = sb_ptr->origin_x / 4;
1015     int       mi_row    = sb_ptr->origin_y / 4;
1016     int       row, col;
1017 
1018     double base_block_count = 0.0;
1019     double log_sum          = 0.0;
1020 
1021     for (row = mi_row / num_mi_w; row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
1022         for (col = mi_col / num_mi_h; col < num_cols && col < mi_col / num_mi_h + num_bcols;
1023              ++col) {
1024             const int index = row * num_cols + col;
1025             log_sum += log(ppcs_ptr->tpl_rdmult_scaling_factors[index]);
1026             base_block_count += 1.0;
1027         }
1028     }
1029     assert(base_block_count > 0);
1030 
1031     uint8_t   bit_depth   = pcs_ptr->hbd_mode_decision ? 10 : 8;
1032 
1033     const int orig_rdmult = compute_rdmult_sse(
1034         pcs_ptr, ppcs_ptr->frm_hdr.quantization_params.base_q_idx, bit_depth);
1035     const int    new_rdmult     = compute_rdmult_sse(pcs_ptr, sb_ptr->qindex, bit_depth);
1036     const double scaling_factor = (double)new_rdmult / (double)orig_rdmult;
1037     double       scale_adj      = log(scaling_factor) - log_sum / base_block_count;
1038     scale_adj                   = exp(scale_adj);
1039 
1040     for (row = mi_row / num_mi_w; row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
1041         for (col = mi_col / num_mi_h; col < num_cols && col < mi_col / num_mi_h + num_bcols;
1042              ++col) {
1043             const int index                                = row * num_cols + col;
1044             ppcs_ptr->tpl_sb_rdmult_scaling_factors[index] = scale_adj *
1045                 ppcs_ptr->tpl_rdmult_scaling_factors[index];
1046         }
1047     }
1048     ppcs_ptr->blk_lambda_tuning = EB_TRUE;
1049 }
1050 
1051 /******************************************************
1052  * sb_qp_derivation_tpl_la
1053  * Calculates the QP per SB based on the tpl statistics
1054  * used in one pass and second pass of two pass encoding
1055  ******************************************************/
sb_qp_derivation_tpl_la(PictureControlSet * pcs_ptr)1056 void sb_qp_derivation_tpl_la(PictureControlSet *pcs_ptr) {
1057     PictureParentControlSet *ppcs_ptr = pcs_ptr->parent_pcs_ptr;
1058     SequenceControlSet *     scs_ptr = pcs_ptr->parent_pcs_ptr->scs_ptr;
1059     SuperBlock *             sb_ptr;
1060     uint32_t                 sb_addr;
1061 
1062     pcs_ptr->parent_pcs_ptr->average_qp = 0;
1063     if (pcs_ptr->temporal_layer_index == 0)
1064         pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 1;
1065     else
1066         pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 0;
1067     if ((pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present) && (pcs_ptr->parent_pcs_ptr->tpl_is_valid == 1)) {
1068 
1069         for (sb_addr = 0; sb_addr < scs_ptr->sb_tot_cnt; ++sb_addr) {
1070             sb_ptr = pcs_ptr->sb_ptr_array[sb_addr];
1071             double beta = ppcs_ptr->tpl_beta[sb_addr];
1072             int    offset = svt_av1_get_deltaq_offset(scs_ptr->static_config.encoder_bit_depth,
1073                 ppcs_ptr->frm_hdr.quantization_params.base_q_idx,
1074                 beta,
1075                 pcs_ptr->parent_pcs_ptr->slice_type);
1076             offset = AOMMIN(
1077                 offset, pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_res * 9 * 4 - 1);
1078             offset = AOMMAX(
1079                 offset, -pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_res * 9 * 4 + 1);
1080             sb_ptr->qindex = CLIP3(
1081                 pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_res,
1082                 255 - pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_res,
1083                 ((int16_t)ppcs_ptr->frm_hdr.quantization_params.base_q_idx + (int16_t)offset));
1084 
1085             sb_setup_lambda(pcs_ptr, sb_ptr);
1086         }
1087 
1088     }
1089     else {
1090         for (sb_addr = 0; sb_addr < scs_ptr->sb_tot_cnt; ++sb_addr) {
1091             sb_ptr = pcs_ptr->sb_ptr_array[sb_addr];
1092             sb_ptr->qindex = quantizer_to_qindex[pcs_ptr->picture_qp];
1093             pcs_ptr->parent_pcs_ptr->average_qp += pcs_ptr->picture_qp;
1094         }
1095     }
1096 }
1097 
av1_find_qindex(double desired_q,aom_bit_depth_t bit_depth,int best_qindex,int worst_qindex)1098 static int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth, int best_qindex,
1099                            int worst_qindex) {
1100     assert(best_qindex <= worst_qindex);
1101     int low  = best_qindex;
1102     int high = worst_qindex;
1103     while (low < high) {
1104         const int    mid   = (low + high) >> 1;
1105         const double mid_q = svt_av1_convert_qindex_to_q(mid, bit_depth);
1106         if (mid_q < desired_q) {
1107             low = mid + 1;
1108         } else {
1109             high = mid;
1110         }
1111     }
1112     assert(low == high);
1113     assert(svt_av1_convert_qindex_to_q(low, bit_depth) >= desired_q || low == worst_qindex);
1114     return low;
1115 }
find_fp_qindex(aom_bit_depth_t bit_depth)1116 static int find_fp_qindex(aom_bit_depth_t bit_depth) {
1117 #ifdef ARCH_X86_64
1118     aom_clear_system_state();
1119 #endif
1120     return av1_find_qindex(FIRST_PASS_Q, bit_depth, 0, QINDEX_RANGE - 1);
1121 }
svt_av1_rc_get_default_min_gf_interval(int width,int height,double framerate)1122 int svt_av1_rc_get_default_min_gf_interval(int width, int height, double framerate) {
1123     // Assume we do not need any constraint lower than 4K 20 fps
1124     static const double factor_safe = 3840 * 2160 * 20.0;
1125     const double        factor      = width * height * framerate;
1126     const int default_interval = clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
1127 
1128     if (factor <= factor_safe)
1129         return default_interval;
1130     else
1131         return AOMMAX(default_interval, (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
1132     // Note this logic makes:
1133     // 4K24: 5
1134     // 4K30: 6
1135     // 4K60: 12
1136 }
set_rc_buffer_sizes(SequenceControlSet * scs_ptr)1137 void set_rc_buffer_sizes(SequenceControlSet *scs_ptr) {
1138     //const int64_t bandwidth = oxcf->target_bandwidth;
1139     //const int64_t starting = oxcf->rc_cfg.starting_buffer_level_ms;
1140     //const int64_t optimal = oxcf->rc_cfg.optimal_buffer_level_ms;
1141     //const int64_t maximum = oxcf->rc_cfg.maximum_buffer_size_ms;
1142 
1143     EncodeContext *       encode_context_ptr = scs_ptr->encode_context_ptr;
1144     RATE_CONTROL *        rc                 = &encode_context_ptr->rc;
1145     RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
1146     const int64_t         bandwidth          = scs_ptr->static_config.target_bit_rate;
1147     const int64_t         starting           = rc_cfg->starting_buffer_level_ms;
1148     const int64_t         optimal            = rc_cfg->optimal_buffer_level_ms;
1149     const int64_t         maximum            = rc_cfg->maximum_buffer_size_ms;
1150 
1151     rc->starting_buffer_level = starting * bandwidth / 1000;
1152     rc->optimal_buffer_level  = (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1153     rc->maximum_buffer_size   = (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1154 }
1155 
svt_av1_rc_get_default_max_gf_interval(double framerate,int min_gf_interval)1156 int svt_av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
1157     int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
1158     interval += (interval & 0x01); // Round to even value
1159     interval = AOMMAX(MAX_GF_INTERVAL, interval);
1160     return AOMMAX(interval, min_gf_interval);
1161 }
1162 
1163 //#define INT_MAX 0x7fffffff
1164 #define BPER_MB_NORMBITS 9
1165 #define FRAME_OVERHEAD_BITS 200
av1_rc_init(SequenceControlSet * scs_ptr)1166 static void av1_rc_init(SequenceControlSet *scs_ptr) {
1167     EncodeContext *             encode_context_ptr = scs_ptr->encode_context_ptr;
1168     RATE_CONTROL *              rc                 = &encode_context_ptr->rc;
1169     const RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
1170     const uint32_t              width              = scs_ptr->seq_header.max_frame_width;
1171     const uint32_t              height             = scs_ptr->seq_header.max_frame_height;
1172     int                         i;
1173 
1174     if (0 /*pass == 0*/ && rc_cfg->mode == AOM_CBR) {
1175         rc->avg_frame_qindex[KEY_FRAME]   = rc_cfg->worst_allowed_q;
1176         rc->avg_frame_qindex[INTER_FRAME] = rc_cfg->worst_allowed_q;
1177     } else {
1178         rc->avg_frame_qindex[KEY_FRAME]   = (rc_cfg->worst_allowed_q + rc_cfg->best_allowed_q) / 2;
1179         rc->avg_frame_qindex[INTER_FRAME] = (rc_cfg->worst_allowed_q + rc_cfg->best_allowed_q) / 2;
1180     }
1181 
1182     rc->last_q[KEY_FRAME]   = rc_cfg->best_allowed_q;
1183     rc->last_q[INTER_FRAME] = rc_cfg->worst_allowed_q;
1184 
1185     rc->buffer_level    = rc->starting_buffer_level;
1186     rc->bits_off_target = rc->starting_buffer_level;
1187 
1188     rc->rolling_target_bits      = rc->avg_frame_bandwidth;
1189     rc->rolling_actual_bits      = rc->avg_frame_bandwidth;
1190     rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
1191     rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
1192 
1193     rc->total_actual_bits      = 0;
1194     rc->total_target_bits      = 0;
1195     rc->total_target_vs_actual = 0;
1196 
1197     rc->frames_since_key       = 8; // Sensible default for first frame.
1198     rc->this_key_frame_forced  = 0;
1199     rc->next_key_frame_forced  = 0;
1200     rc->source_alt_ref_pending = 0;
1201     rc->source_alt_ref_active  = 0;
1202     rc->ni_av_qi                  = rc_cfg->worst_allowed_q;
1203     rc->ni_tot_qi                 = 0;
1204     rc->ni_frames                 = 0;
1205 
1206     rc->tot_q = 0.0;
1207     rc->avg_q = svt_av1_convert_qindex_to_q(rc_cfg->worst_allowed_q,
1208                                             scs_ptr->static_config.encoder_bit_depth);
1209 
1210     for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { rc->rate_correction_factors[i] = 0.7; }
1211     rc->rate_correction_factors[KF_STD] = 1.0;
1212     rc->min_gf_interval                 = encode_context_ptr->gf_cfg.min_gf_interval;
1213     rc->max_gf_interval                 = encode_context_ptr->gf_cfg.max_gf_interval;
1214     if (rc->min_gf_interval == 0)
1215         rc->min_gf_interval = svt_av1_rc_get_default_min_gf_interval(
1216             width,
1217             height, /*oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,*/
1218             scs_ptr->double_frame_rate /*oxcf->input_cfg.init_framerate*/);
1219     if (rc->max_gf_interval == 0)
1220         rc->max_gf_interval = svt_av1_rc_get_default_max_gf_interval(
1221             scs_ptr->double_frame_rate /*oxcf->input_cfg.init_framerate*/, rc->min_gf_interval);
1222     rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
1223     //rc->avg_frame_low_motion = 0;
1224 
1225     // Set absolute upper and lower quality limits
1226     rc->worst_quality = rc_cfg->worst_allowed_q;
1227     rc->best_quality  = rc_cfg->best_allowed_q;
1228     if (scs_ptr->lap_enabled) {
1229         double frame_rate = (double)scs_ptr->static_config.frame_rate_numerator /
1230             (double)scs_ptr->static_config.frame_rate_denominator;
1231         // Each frame can have a different duration, as the frame rate in the source
1232         // isn't guaranteed to be constant. The frame rate prior to the first frame
1233         // encoded in the second pass is a guess. However, the sum duration is not.
1234         // It is calculated based on the actual durations of all frames from the
1235         // first pass.
1236         svt_av1_new_framerate(scs_ptr, frame_rate);
1237     }
1238 }
1239 
combine_prior_with_tpl_boost_org(double min_factor,double max_factor,int prior_boost,int tpl_boost,int frames_to_key)1240 static AOM_INLINE int combine_prior_with_tpl_boost_org(double min_factor, double max_factor,
1241                                                        int prior_boost, int tpl_boost,
1242                                                        int frames_to_key) {
1243     double factor = sqrt((double)frames_to_key);
1244     double range  = max_factor - min_factor;
1245     factor        = AOMMIN(factor, max_factor);
1246     factor        = AOMMAX(factor, min_factor);
1247     factor -= min_factor;
1248     int boost = (int)((factor * prior_boost + (range - factor) * tpl_boost) / range);
1249     return boost;
1250 }
1251 
1252 #define MIN_BOOST_COMBINE_FACTOR 4.0
1253 #define MAX_BOOST_COMBINE_FACTOR 12.0
process_tpl_stats_frame_kf_gfu_boost(PictureControlSet * pcs_ptr)1254 void process_tpl_stats_frame_kf_gfu_boost(PictureControlSet *pcs_ptr) {
1255     SequenceControlSet *scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1256     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1257     RATE_CONTROL *const rc                 = &encode_context_ptr->rc;
1258 
1259     if (scs_ptr->lap_enabled) {
1260         double min_boost_factor = sqrt(rc->baseline_gf_interval);
1261         // The new tpl only looks at pictures in tpl group, which is fewer than before,
1262         // As a results, we defined a factor to adjust r0
1263         if (pcs_ptr->parent_pcs_ptr->slice_type != 2) {
1264             double div_factor = 1;
1265             if (scs_ptr->lad_mg)
1266             {
1267                 div_factor = pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor ?
1268                     pcs_ptr->parent_pcs_ptr->used_tpl_frame_num * pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor :
1269                     div_factor;
1270             }
1271             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / div_factor;
1272         }
1273         const int gfu_boost = get_gfu_boost_from_r0_lap(min_boost_factor,
1274                                                         MAX_GFUBOOST_FACTOR,
1275                                                         pcs_ptr->parent_pcs_ptr->r0,
1276                                                         rc->num_stats_required_for_gfu_boost);
1277         // printf("old boost %d new boost %d\n", rc->gfu_boost, gfu_boost);
1278         rc->gfu_boost = combine_prior_with_tpl_boost_org(min_boost_factor,
1279                                                          MAX_BOOST_COMBINE_FACTOR,
1280                                                          rc->gfu_boost,
1281                                                          gfu_boost,
1282                                                          rc->num_stats_used_for_gfu_boost);
1283     } else {
1284         // The new tpl only looks at pictures in tpl group, which is fewer than before,
1285         // As a results, we defined a factor to adjust r0
1286         if (pcs_ptr->parent_pcs_ptr->slice_type != 2) {
1287             double div_factor = 1;
1288             if (scs_ptr->lad_mg)
1289             {
1290                 div_factor = pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor ?
1291                     pcs_ptr->parent_pcs_ptr->used_tpl_frame_num * pcs_ptr->parent_pcs_ptr->tpl_ctrls.r0_adjust_factor :
1292                     div_factor;
1293             }
1294 
1295 
1296             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / div_factor;
1297         } else if (pcs_ptr->parent_pcs_ptr->frm_hdr.frame_type != KEY_FRAME) {
1298             double factor = 2;
1299             pcs_ptr->parent_pcs_ptr->r0 = pcs_ptr->parent_pcs_ptr->r0 / factor;
1300         }
1301         rc->gfu_boost = get_gfu_boost_from_r0_lap(MIN_BOOST_COMBINE_FACTOR,
1302                                                   MAX_GFUBOOST_FACTOR,
1303                                                   pcs_ptr->parent_pcs_ptr->r0,
1304                                                   rc->frames_to_key);
1305     }
1306     if (scs_ptr->static_config.rate_control_mode == 0)
1307         rc->kf_boost = get_cqp_kf_boost_from_r0(
1308             pcs_ptr->parent_pcs_ptr->r0, rc->frames_to_key, scs_ptr->input_resolution);
1309 }
1310 
get_intra_q_and_bounds(PictureControlSet * pcs_ptr,int * active_best,int * active_worst,int cq_level,int is_fwd_kf)1311 static void get_intra_q_and_bounds(PictureControlSet *pcs_ptr, int *active_best, int *active_worst,
1312                                    int cq_level, int is_fwd_kf) {
1313     SequenceControlSet *scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1314     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1315     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1316     TWO_PASS *const     twopass            = &scs_ptr->twopass;
1317     int                 active_best_quality;
1318     int                 active_worst_quality = *active_worst;
1319     const int           bit_depth            = scs_ptr->static_config.encoder_bit_depth;
1320 
1321     if (rc->frames_to_key <= 1 && encode_context_ptr->rc_cfg.mode == AOM_Q) {
1322         // If the next frame is also a key frame or the current frame is the
1323         // only frame in the sequence in AOM_Q mode, just use the cq_level
1324         // as q.
1325         active_best_quality  = cq_level;
1326         active_worst_quality = cq_level;
1327     } else if (is_fwd_kf) {
1328         // Handle the special case for forward reference key frames.
1329         // Increase the boost because this keyframe is used as a forward and
1330         // backward reference.
1331         const int    qindex         = rc->last_boosted_qindex;
1332         const double last_boosted_q = svt_av1_convert_qindex_to_q(qindex, bit_depth);
1333         const int    delta_qindex   = svt_av1_compute_qdelta(
1334             last_boosted_q, last_boosted_q * 0.25, bit_depth);
1335         active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1336     } else if (rc->this_key_frame_forced) {
1337         // Handle the special case for key frames forced when we have reached
1338         // the maximum key frame interval. Here force the Q to a range
1339         // based on the ambient Q to reduce the risk of popping.
1340         double last_boosted_q;
1341         int    delta_qindex;
1342         int    qindex;
1343 
1344         if (/*is_stat_consumption_stage_twopass(cpi) &&*/
1345             twopass->last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1346             qindex              = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1347             active_best_quality = qindex;
1348             last_boosted_q      = svt_av1_convert_qindex_to_q(qindex, bit_depth);
1349             delta_qindex = svt_av1_compute_qdelta(last_boosted_q, last_boosted_q * 1.25, bit_depth);
1350             active_worst_quality = AOMMIN(qindex + delta_qindex, active_worst_quality);
1351         } else {
1352             qindex         = rc->last_boosted_qindex;
1353             last_boosted_q = svt_av1_convert_qindex_to_q(qindex, bit_depth);
1354             delta_qindex = svt_av1_compute_qdelta(last_boosted_q, last_boosted_q * 0.50, bit_depth);
1355             active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality);
1356         }
1357     } else {
1358         // Not forced keyframe.
1359         double q_adj_factor = 1.0;
1360         double q_val;
1361         rc->worst_quality = MAXQ;
1362         rc->best_quality  = MINQ;
1363 
1364         // Baseline value derived from cpi->active_worst_quality and kf boost.
1365         active_best_quality = get_kf_active_quality_tpl(rc, active_worst_quality, bit_depth);
1366         if (/*is_stat_consumption_stage_twopass(cpi) &&*/
1367             twopass->kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) {
1368             active_best_quality /= 3;
1369         }
1370         if (pcs_ptr->parent_pcs_ptr->sc_class1 &&
1371             encode_context_ptr->rc_cfg.mode == AOM_VBR)
1372             active_best_quality /= 2;
1373         // Allow somewhat lower kf minq with small image formats.
1374         if (pcs_ptr->parent_pcs_ptr->input_resolution <= INPUT_SIZE_240p_RANGE)
1375             q_adj_factor -= (pcs_ptr->parent_pcs_ptr->tune_tpl_for_chroma) ? 0.2 : 0.15;
1376         // Make a further adjustment based on the kf zero motion measure.
1377 
1378         // Convert the adjustment factor to a qindex delta
1379         // on active_best_quality.
1380         q_val = svt_av1_convert_qindex_to_q(active_best_quality, bit_depth);
1381         active_best_quality += svt_av1_compute_qdelta(q_val, q_val * q_adj_factor, bit_depth);
1382     }
1383 
1384     *active_best  = active_best_quality;
1385     *active_worst = active_worst_quality;
1386     return;
1387 }
1388 
1389 // Returns |active_best_quality| for an inter frame.
1390 // The |active_best_quality| depends on different rate control modes:
1391 // VBR, Q, CQ, CBR.
1392 // The returning active_best_quality could further be adjusted in
1393 // adjust_active_best_and_worst_quality().
get_active_best_quality(PictureControlSet * pcs_ptr,const int active_worst_quality,const int cq_level)1394 static int get_active_best_quality(PictureControlSet *pcs_ptr, const int active_worst_quality,
1395                                    const int cq_level) {
1396     SequenceControlSet *   scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1397     EncodeContext *        encode_context_ptr = scs_ptr->encode_context_ptr;
1398     RATE_CONTROL *         rc                 = &encode_context_ptr->rc;
1399     const enum aom_rc_mode rc_mode            = encode_context_ptr->rc_cfg.mode;
1400     GF_GROUP *const        gf_group           = &encode_context_ptr->gf_group;
1401     const int              bit_depth          = scs_ptr->static_config.encoder_bit_depth;
1402     //int rf_level, update_type;
1403     const RefreshFrameFlagsInfo *const refresh_frame_flags =
1404         &pcs_ptr->parent_pcs_ptr->refresh_frame;
1405     const int is_intrl_arf_boost = gf_group->update_type[pcs_ptr->parent_pcs_ptr->gf_group_index] ==
1406         INTNL_ARF_UPDATE;
1407     int *inter_minq;
1408     ASSIGN_MINQ_TABLE(bit_depth, inter_minq);
1409     int       active_best_quality = 0;
1410     const int is_leaf_frame       = !(refresh_frame_flags->golden_frame ||
1411                                 refresh_frame_flags->alt_ref_frame || is_intrl_arf_boost);
1412     const int is_overlay_frame    = pcs_ptr->parent_pcs_ptr->is_overlay; //rc->is_src_frame_alt_ref;
1413 
1414     if (is_leaf_frame || is_overlay_frame) {
1415         if (rc_mode == AOM_Q)
1416             return cq_level;
1417 
1418         active_best_quality = inter_minq[active_worst_quality];
1419         // For the constrained quality mode we don't want
1420         // q to fall below the cq level.
1421         if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) {
1422             active_best_quality = cq_level;
1423         }
1424         return active_best_quality;
1425     }
1426 
1427     // TODO(chengchen): can we remove this condition?
1428     if (rc_mode == AOM_Q && !refresh_frame_flags->alt_ref_frame && !is_intrl_arf_boost) {
1429         return cq_level;
1430     }
1431 
1432     // Determine active_best_quality for frames that are not leaf or overlay.
1433     int q = active_worst_quality;
1434     // Use the lower of active_worst_quality and recent
1435     // average Q as basis for GF/ARF best Q limit unless last frame was
1436     // a key frame.
1437     if (rc_mode == AOM_VBR && rc->frames_since_key > 1 &&
1438         rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
1439         q = rc->avg_frame_qindex[INTER_FRAME];
1440     }
1441     if (rc_mode == AOM_CQ && q < cq_level)
1442         q = cq_level;
1443     active_best_quality = get_gf_active_quality_tpl_la(rc, q, bit_depth);
1444     // Constrained quality use slightly lower active best.
1445     if (rc_mode == AOM_CQ)
1446         active_best_quality = active_best_quality * 15 / 16;
1447     const int min_boost = get_gf_high_motion_quality(q, bit_depth);
1448     const int boost     = min_boost - active_best_quality;
1449 
1450     rc->arf_boost_factor = (pcs_ptr->ref_slice_type_array[0][0] == I_SLICE &&
1451                             pcs_ptr->ref_pic_r0[0][0] - pcs_ptr->parent_pcs_ptr->r0 >= 0.08)
1452         ? (float_t)1.3
1453         : (float_t)1;
1454     active_best_quality  = min_boost - (int)(boost * rc->arf_boost_factor);
1455     if (!is_intrl_arf_boost)
1456         return active_best_quality;
1457 
1458     if (rc_mode == AOM_Q || rc_mode == AOM_CQ)
1459         active_best_quality = rc->arf_q;
1460     int this_height =
1461         gf_group->layer_depth[pcs_ptr->parent_pcs_ptr
1462                                   ->gf_group_index]; //gf_group_pyramid_level(gf_group, gf_index);
1463     while (this_height > 1) {
1464         active_best_quality = (active_best_quality + active_worst_quality + 1) / 2;
1465         --this_height;
1466     }
1467     return active_best_quality;
1468 }
1469 
get_rate_correction_factor(PictureParentControlSet * ppcs_ptr)1470 static double get_rate_correction_factor(PictureParentControlSet *ppcs_ptr/*,
1471                                          int width, int height*/) {
1472     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1473     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1474     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1475     //const RefreshFrameFlagsInfo *const refresh_frame_flags = &ppcs_ptr->refresh_frame;
1476     double rcf;
1477 
1478     if (ppcs_ptr->frm_hdr.frame_type == KEY_FRAME) {
1479         rcf = rc->rate_correction_factors[KF_STD];
1480     } else {
1481         const rate_factor_level rf_lvl = get_rate_factor_level(&encode_context_ptr->gf_group,
1482                                                                ppcs_ptr->gf_group_index);
1483         rcf                            = rc->rate_correction_factors[rf_lvl];
1484     }
1485     //rcf *= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
1486     return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
1487 }
1488 
set_rate_correction_factor(PictureParentControlSet * ppcs_ptr,double factor)1489 static void set_rate_correction_factor(PictureParentControlSet *ppcs_ptr, double factor/*,
1490                                        int width, int height*/) {
1491     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1492     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1493     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1494     //const RefreshFrameFlagsInfo *const refresh_frame_flags = &ppcs_ptr->refresh_frame;
1495 
1496     // Normalize RCF to account for the size-dependent scaling factor.
1497     //factor /= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height);
1498 
1499     factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
1500 
1501     if (ppcs_ptr->frm_hdr.frame_type == KEY_FRAME) {
1502         rc->rate_correction_factors[KF_STD] = factor;
1503     } else {
1504         const rate_factor_level rf_lvl      = get_rate_factor_level(&encode_context_ptr->gf_group,
1505                                                                ppcs_ptr->gf_group_index);
1506         rc->rate_correction_factors[rf_lvl] = factor;
1507     }
1508 }
1509 
1510 // Calculate rate for the given 'q'.
get_bits_per_mb(PictureParentControlSet * ppcs_ptr,int use_cyclic_refresh,double correction_factor,int q)1511 static int get_bits_per_mb(PictureParentControlSet *ppcs_ptr, int use_cyclic_refresh,
1512                            double correction_factor, int q) {
1513     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
1514     return use_cyclic_refresh ? 0 /*av1_cyclic_refresh_rc_bits_per_mb(cpi, q, correction_factor)*/
1515                               : svt_av1_rc_bits_per_mb(ppcs_ptr->frm_hdr.frame_type,
1516                                                        q,
1517                                                        correction_factor,
1518                                                        scs_ptr->static_config.encoder_bit_depth,
1519                                                        ppcs_ptr->sc_class1);
1520 }
1521 
1522 // Similar to find_qindex_by_rate() function in ratectrl.c, but returns the q
1523 // index with rate just above or below the desired rate, depending on which of
1524 // the two rates is closer to the desired rate.
1525 // Also, respects the selected aq_mode when computing the rate.
find_closest_qindex_by_rate(int desired_bits_per_mb,PictureParentControlSet * ppcs_ptr,double correction_factor,int best_qindex,int worst_qindex)1526 static int find_closest_qindex_by_rate(int desired_bits_per_mb, PictureParentControlSet *ppcs_ptr,
1527                                        double correction_factor, int best_qindex,
1528                                        int worst_qindex) {
1529     const int use_cyclic_refresh = 0 /*cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
1530                                  cpi->cyclic_refresh->apply_cyclic_refresh*/
1531         ;
1532 
1533     // Find 'qindex' based on 'desired_bits_per_mb'.
1534     assert(best_qindex <= worst_qindex);
1535     int low  = best_qindex;
1536     int high = worst_qindex;
1537     while (low < high) {
1538         const int mid             = (low + high) >> 1;
1539         const int mid_bits_per_mb = get_bits_per_mb(
1540             ppcs_ptr, use_cyclic_refresh, correction_factor, mid);
1541         if (mid_bits_per_mb > desired_bits_per_mb) {
1542             low = mid + 1;
1543         } else {
1544             high = mid;
1545         }
1546     }
1547     assert(low == high);
1548 
1549     // Calculate rate difference of this q index from the desired rate.
1550     const int curr_q           = low;
1551     const int curr_bits_per_mb = get_bits_per_mb(
1552         ppcs_ptr, use_cyclic_refresh, correction_factor, curr_q);
1553     const int curr_bit_diff = (curr_bits_per_mb <= desired_bits_per_mb)
1554         ? desired_bits_per_mb - curr_bits_per_mb
1555         : INT_MAX;
1556     assert((curr_bit_diff != INT_MAX && curr_bit_diff >= 0) || curr_q == worst_qindex);
1557 
1558     // Calculate rate difference for previous q index too.
1559     const int prev_q = curr_q - 1;
1560     int       prev_bit_diff;
1561     if (curr_bit_diff == INT_MAX || curr_q == best_qindex) {
1562         prev_bit_diff = INT_MAX;
1563     } else {
1564         const int prev_bits_per_mb = get_bits_per_mb(
1565             ppcs_ptr, use_cyclic_refresh, correction_factor, prev_q);
1566         assert(prev_bits_per_mb > desired_bits_per_mb);
1567         prev_bit_diff = prev_bits_per_mb - desired_bits_per_mb;
1568     }
1569 
1570     // Pick one of the two q indices, depending on which one has rate closer to
1571     // the desired rate.
1572     return (curr_bit_diff <= prev_bit_diff) ? curr_q : prev_q;
1573 }
1574 
av1_rc_regulate_q(PictureParentControlSet * ppcs_ptr,int target_bits_per_frame,int active_best_quality,int active_worst_quality,int width,int height)1575 static int av1_rc_regulate_q(PictureParentControlSet *ppcs_ptr, int target_bits_per_frame,
1576                              int active_best_quality, int active_worst_quality, int width,
1577                              int height) {
1578     const int    MBs = ((width + 15) / 16) * ((height + 15) / 16); //av1_get_MBs(width, height);
1579     const double correction_factor  = get_rate_correction_factor(ppcs_ptr /*, width, height*/);
1580     const int    target_bits_per_mb = (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) /
1581                                          MBs);
1582 
1583     int q = find_closest_qindex_by_rate(
1584         target_bits_per_mb, ppcs_ptr, correction_factor, active_best_quality, active_worst_quality);
1585 
1586     return q;
1587 }
1588 
get_q(PictureControlSet * pcs_ptr,const int active_worst_quality,const int active_best_quality)1589 static int get_q(PictureControlSet *pcs_ptr, const int active_worst_quality,
1590                  const int active_best_quality) {
1591     SequenceControlSet *   scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1592     EncodeContext *        encode_context_ptr = scs_ptr->encode_context_ptr;
1593     RATE_CONTROL *         rc                 = &encode_context_ptr->rc;
1594     TWO_PASS *const        twopass            = &scs_ptr->twopass;
1595     const enum aom_rc_mode rc_mode            = encode_context_ptr->rc_cfg.mode;
1596     const int              width  = pcs_ptr->parent_pcs_ptr->av1_cm->frm_size.frame_width;
1597     const int              height = pcs_ptr->parent_pcs_ptr->av1_cm->frm_size.frame_height;
1598     int                    q;
1599 
1600     if (rc_mode == AOM_Q ||
1601         (frame_is_intra_only(pcs_ptr->parent_pcs_ptr) && !rc->this_key_frame_forced &&
1602          twopass->kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH && rc->frames_to_key > 1)) {
1603         q = active_best_quality;
1604         // Special case code to try and match quality with forced key frames.
1605     } else if (frame_is_intra_only(pcs_ptr->parent_pcs_ptr) && rc->this_key_frame_forced) {
1606         // If static since last kf use better of last boosted and last kf q.
1607         if (twopass->last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
1608             q = AOMMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
1609         } else {
1610             q = AOMMIN(rc->last_boosted_qindex, (active_best_quality + active_worst_quality) / 2);
1611         }
1612         q = clamp(q, active_best_quality, active_worst_quality);
1613     } else {
1614         q = av1_rc_regulate_q(pcs_ptr->parent_pcs_ptr,
1615                               pcs_ptr->parent_pcs_ptr->this_frame_target,
1616                               active_best_quality,
1617                               active_worst_quality,
1618                               width,
1619                               height);
1620         if (q > active_worst_quality) {
1621             // Special case when we are targeting the max allowed rate.
1622             if (pcs_ptr->parent_pcs_ptr->this_frame_target < rc->max_frame_bandwidth) {
1623                 q = active_worst_quality;
1624             }
1625         }
1626         q = AOMMAX(q, active_best_quality);
1627     }
1628     return q;
1629 }
1630 
1631 /******************************************************
1632  * rc_pick_q_and_bounds
1633  * assigns the q_index per frame using first pass statistics per frame.
1634  * used in the second pass of two pass encoding
1635  ******************************************************/
rc_pick_q_and_bounds(PictureControlSet * pcs_ptr)1636 static int rc_pick_q_and_bounds(PictureControlSet *pcs_ptr) {
1637     SequenceControlSet *               scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1638     EncodeContext *                    encode_context_ptr = scs_ptr->encode_context_ptr;
1639     RATE_CONTROL *                     rc                 = &encode_context_ptr->rc;
1640     GF_GROUP *const                    gf_group           = &encode_context_ptr->gf_group;
1641     const RefreshFrameFlagsInfo *const refresh_frame_flags =
1642         &pcs_ptr->parent_pcs_ptr->refresh_frame;
1643     const enum aom_rc_mode rc_mode              = encode_context_ptr->rc_cfg.mode;
1644     const int              cq_level             = encode_context_ptr->rc_cfg.cq_level;
1645     int                    active_best_quality  = 0;
1646     int                    active_worst_quality = rc->active_worst_quality;
1647     int                    q;
1648     int is_intrl_arf_boost = gf_group->update_type[pcs_ptr->parent_pcs_ptr->gf_group_index] ==
1649         INTNL_ARF_UPDATE;
1650 
1651     if (frame_is_intra_only(pcs_ptr->parent_pcs_ptr)) {
1652         const int is_fwd_kf = pcs_ptr->parent_pcs_ptr->frm_hdr.frame_type == KEY_FRAME &&
1653             pcs_ptr->parent_pcs_ptr->frm_hdr.show_frame == 0;
1654         get_intra_q_and_bounds(
1655             pcs_ptr, &active_best_quality, &active_worst_quality, cq_level, is_fwd_kf);
1656     } else {
1657         const int pyramid_level = gf_group->layer_depth[pcs_ptr->parent_pcs_ptr->gf_group_index];
1658         if ((pyramid_level <= 1) || (pyramid_level > MAX_ARF_LAYERS) || rc_mode == AOM_Q) {
1659             active_best_quality = get_active_best_quality(pcs_ptr, active_worst_quality, cq_level);
1660         } else {
1661             active_best_quality = rc->active_best_quality[pyramid_level - 1] + 1;
1662             active_best_quality = AOMMIN(active_best_quality, active_worst_quality);
1663             active_best_quality += (active_worst_quality - active_best_quality) / 2;
1664         }
1665         // For alt_ref and GF frames (including internal arf frames) adjust the
1666         // worst allowed quality as well. This insures that even on hard
1667         // sections we dont clamp the Q at the same value for arf frames and
1668         // leaf (non arf) frames. This is important to the TPL model which assumes
1669         // Q drops with each arf level.
1670         if (!(rc->is_src_frame_alt_ref) &&
1671             (refresh_frame_flags->golden_frame || refresh_frame_flags->alt_ref_frame ||
1672              is_intrl_arf_boost)) {
1673             active_worst_quality = (active_best_quality + (3 * active_worst_quality) + 2) / 4;
1674         }
1675     }
1676 
1677     adjust_active_best_and_worst_quality_org(
1678         pcs_ptr, rc, &active_worst_quality, &active_best_quality);
1679 
1680     q = get_q(pcs_ptr, active_worst_quality, active_best_quality);
1681     // Special case when we are targeting the max allowed rate.
1682     if (pcs_ptr->parent_pcs_ptr->this_frame_target >= rc->max_frame_bandwidth && q > active_worst_quality) {
1683         active_worst_quality = q;
1684     }
1685     pcs_ptr->parent_pcs_ptr->top_index = active_worst_quality;
1686     pcs_ptr->parent_pcs_ptr->bottom_index = active_best_quality;
1687     assert(pcs_ptr->parent_pcs_ptr->top_index <= rc->worst_quality && pcs_ptr->parent_pcs_ptr->top_index >= rc->best_quality);
1688     assert(pcs_ptr->parent_pcs_ptr->bottom_index <= rc->worst_quality && pcs_ptr->parent_pcs_ptr->bottom_index >= rc->best_quality);
1689 
1690     assert(q <= rc->worst_quality && q >= rc->best_quality);
1691 
1692     if (gf_group->update_type[pcs_ptr->parent_pcs_ptr->gf_group_index] == ARF_UPDATE)
1693         rc->arf_q = q;
1694 
1695     return q;
1696 }
1697 
av1_estimate_bits_at_q(FrameType frame_type,int q,int mbs,double correction_factor,AomBitDepth bit_depth,uint8_t sc_content_detected)1698 static int av1_estimate_bits_at_q(FrameType frame_type, int q, int mbs, double correction_factor,
1699                                   AomBitDepth bit_depth, uint8_t sc_content_detected) {
1700     const int bpm = (int)(svt_av1_rc_bits_per_mb(
1701         frame_type, q, correction_factor, bit_depth, sc_content_detected));
1702     return AOMMAX(FRAME_OVERHEAD_BITS, (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
1703 }
1704 
av1_rc_update_rate_correction_factors(PictureParentControlSet * ppcs_ptr,int width,int height)1705 static void av1_rc_update_rate_correction_factors(PictureParentControlSet *ppcs_ptr, int width,
1706                                                   int height) {
1707     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1708     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1709     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1710     int                 correction_factor  = 100;
1711     double rate_correction_factor = get_rate_correction_factor(ppcs_ptr /*, width, height*/);
1712     double adjustment_limit;
1713     //const int MBs = av1_get_MBs(width, height);
1714     const int MBs = ((width + 15) / 16) * ((height + 15) / 16); //av1_get_MBs(width, height);
1715 
1716     int projected_size_based_on_q = 0;
1717 
1718     // Do not update the rate factors for arf overlay frames.
1719     if (rc->is_src_frame_alt_ref)
1720         return;
1721 
1722     // Clear down mmx registers to allow floating point in what follows
1723     //aom_clear_system_state();
1724 
1725     // Work out how big we would have expected the frame to be at this Q given
1726     // the current correction factor.
1727     // Stay in double to avoid int overflow when values are large
1728 
1729     {
1730         projected_size_based_on_q = av1_estimate_bits_at_q(
1731             ppcs_ptr->frm_hdr.frame_type,
1732             ppcs_ptr->frm_hdr.quantization_params.base_q_idx /*cm->quant_params.base_qindex*/,
1733             MBs,
1734             rate_correction_factor,
1735             scs_ptr->static_config.encoder_bit_depth,
1736             ppcs_ptr->sc_class1);
1737     }
1738     // Work out a size correction factor.
1739     if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
1740         correction_factor = (int)((100 * (int64_t)ppcs_ptr->projected_frame_size) /
1741                                   projected_size_based_on_q);
1742 
1743     // More heavily damped adjustment used if we have been oscillating either side
1744     // of target.
1745     if (correction_factor > 0) {
1746         adjustment_limit = 0.25 + 0.5 * AOMMIN(1, fabs(log10(0.01 * correction_factor)));
1747     } else {
1748         adjustment_limit = 0.75;
1749     }
1750 
1751     rc->q_2_frame = rc->q_1_frame;
1752     rc->q_1_frame =
1753         ppcs_ptr->frm_hdr.quantization_params.base_q_idx; //cm->quant_params.base_qindex;
1754     rc->rc_2_frame = rc->rc_1_frame;
1755     if (correction_factor > 110)
1756         rc->rc_1_frame = -1;
1757     else if (correction_factor < 90)
1758         rc->rc_1_frame = 1;
1759     else
1760         rc->rc_1_frame = 0;
1761 
1762     if (correction_factor > 102) {
1763         // We are not already at the worst allowable quality
1764         correction_factor      = (int)(100 + ((correction_factor - 100) * adjustment_limit));
1765         rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
1766         // Keep rate_correction_factor within limits
1767         if (rate_correction_factor > MAX_BPB_FACTOR)
1768             rate_correction_factor = MAX_BPB_FACTOR;
1769     } else if (correction_factor < 99) {
1770         // We are not already at the best allowable quality
1771         correction_factor      = (int)(100 - ((100 - correction_factor) * adjustment_limit));
1772         rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
1773 
1774         // Keep rate_correction_factor within limits
1775         if (rate_correction_factor < MIN_BPB_FACTOR)
1776             rate_correction_factor = MIN_BPB_FACTOR;
1777     }
1778 
1779     set_rate_correction_factor(ppcs_ptr, rate_correction_factor /*, width, height*/);
1780 }
1781 
1782 // Update the buffer level: leaky bucket model.
update_buffer_level(PictureParentControlSet * ppcs_ptr,int encoded_frame_size)1783 static void update_buffer_level(PictureParentControlSet *ppcs_ptr, int encoded_frame_size) {
1784     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1785     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1786     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1787 
1788     // Non-viewable frames are a special case and are treated as pure overhead.
1789     if (!ppcs_ptr->frm_hdr.showable_frame)
1790         rc->bits_off_target -= encoded_frame_size;
1791     else
1792         rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
1793 
1794     // Clip the buffer level to the maximum specified buffer size.
1795     rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
1796     rc->buffer_level    = rc->bits_off_target;
1797 
1798     //if (cpi->use_svc) update_layer_buffer_level(&cpi->svc, encoded_frame_size);
1799 }
1800 
update_alt_ref_frame_stats(PictureParentControlSet * ppcs_ptr)1801 static void update_alt_ref_frame_stats(PictureParentControlSet *ppcs_ptr) {
1802     // this frame refreshes means next frames don't unless specified by user
1803     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1804     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1805     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1806     rc->frames_since_golden                = 0;
1807 
1808     // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1809     rc->source_alt_ref_pending = 0;
1810 
1811 }
1812 
update_golden_frame_stats(PictureParentControlSet * ppcs_ptr)1813 static void update_golden_frame_stats(PictureParentControlSet *ppcs_ptr) {
1814     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1815     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1816     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1817 
1818     // Update the Golden frame usage counts.
1819     if (/*cpi->refresh_frame.golden_frame*/ frame_is_intra_only(ppcs_ptr) ||
1820         rc->is_src_frame_alt_ref) {
1821         rc->frames_since_golden = 0;
1822 
1823         // If we are not using alt ref in the up and coming group clear the arf
1824         // active flag. In multi arf group case, if the index is not 0 then
1825         // we are overlaying a mid group arf so should not reset the flag.
1826         if (!rc->source_alt_ref_pending && (ppcs_ptr->gf_group_index == 0))
1827             rc->source_alt_ref_active = 0;
1828     } else if (ppcs_ptr->frm_hdr.show_frame) {
1829         rc->frames_since_golden++;
1830     }
1831 }
1832 
av1_rc_postencode_update(PictureParentControlSet * ppcs_ptr,uint64_t bytes_used)1833 static void av1_rc_postencode_update(PictureParentControlSet *ppcs_ptr, uint64_t bytes_used) {
1834     const RefreshFrameFlagsInfo *const refresh_frame_flags = &ppcs_ptr->refresh_frame;
1835     SequenceControlSet *               scs_ptr             = ppcs_ptr->scs_ptr;
1836     EncodeContext *                    encode_context_ptr  = scs_ptr->encode_context_ptr;
1837     RATE_CONTROL *                     rc                  = &encode_context_ptr->rc;
1838     GF_GROUP *const                    gf_group            = &encode_context_ptr->gf_group;
1839     const GFConfig *const              gf_cfg              = &encode_context_ptr->gf_cfg;
1840     CurrentFrame *const                current_frame       = &ppcs_ptr->av1_cm->current_frame;
1841     current_frame->frame_type                              = ppcs_ptr->frm_hdr.frame_type;
1842     FrameHeader *frm_hdr                                   = &ppcs_ptr->frm_hdr;
1843     const int    width                                     = ppcs_ptr->av1_cm->frm_size.frame_width;
1844     const int    height = ppcs_ptr->av1_cm->frm_size.frame_height;
1845 
1846     const int is_intrnl_arf = gf_group->update_type[ppcs_ptr->gf_group_index] == INTNL_ARF_UPDATE;
1847 
1848     const int qindex = frm_hdr->quantization_params.base_q_idx; //cm->quant_params.base_qindex;
1849 
1850     // Update rate control heuristics
1851     ppcs_ptr->projected_frame_size = (int)(bytes_used << 3);
1852     // Post encode loop adjustment of Q prediction.
1853     av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
1854 
1855     // Keep a record of last Q and ambient average Q.
1856     if (current_frame->frame_type == KEY_FRAME) {
1857         rc->last_q[KEY_FRAME]           = qindex;
1858         rc->avg_frame_qindex[KEY_FRAME] = ROUND_POWER_OF_TWO(
1859             3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1860     } else {
1861         if (/*(cpi->use_svc && cpi->oxcf.rc_cfg.mode == AOM_CBR) ||*/
1862             (!rc->is_src_frame_alt_ref &&
1863              !(refresh_frame_flags->golden_frame || is_intrnl_arf ||
1864                refresh_frame_flags->alt_ref_frame))) {
1865             rc->last_q[INTER_FRAME]           = qindex;
1866             rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO(
1867                 3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
1868             rc->ni_frames++;
1869             rc->tot_q += svt_av1_convert_qindex_to_q(qindex,
1870                                                      scs_ptr->static_config.encoder_bit_depth);
1871             rc->avg_q = rc->tot_q / rc->ni_frames;
1872             // Calculate the average Q for normal inter frames (not key or GFU
1873             // frames).
1874             rc->ni_tot_qi += qindex;
1875             rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
1876         }
1877     }
1878 
1879     // Keep record of last boosted (KF/GF/ARF) Q value.
1880     // If the current frame is coded at a lower Q then we also update it.
1881     // If all mbs in this group are skipped only update if the Q value is
1882     // better than that already stored.
1883     // This is used to help set quality in forced key frames to reduce popping
1884     if ((qindex < rc->last_boosted_qindex) || (current_frame->frame_type == KEY_FRAME) ||
1885         (!rc->constrained_gf_group &&
1886          (refresh_frame_flags->alt_ref_frame || is_intrnl_arf ||
1887           (refresh_frame_flags->golden_frame && !rc->is_src_frame_alt_ref)))) {
1888         rc->last_boosted_qindex = qindex;
1889     }
1890     if (current_frame->frame_type == KEY_FRAME)
1891         rc->last_kf_qindex = qindex;
1892     update_buffer_level(ppcs_ptr, ppcs_ptr->projected_frame_size);
1893     rc->prev_avg_frame_bandwidth = rc->avg_frame_bandwidth;
1894 
1895     // Rolling monitors of whether we are over or underspending used to help
1896     // regulate min and Max Q in two pass.
1897     if (current_frame->frame_type != KEY_FRAME) {
1898         rc->rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
1899             rc->rolling_target_bits * 3 + ppcs_ptr->this_frame_target, 2);
1900         rc->rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
1901             rc->rolling_actual_bits * 3 + ppcs_ptr->projected_frame_size, 2);
1902         rc->long_rolling_target_bits = (int)ROUND_POWER_OF_TWO_64(
1903             rc->long_rolling_target_bits * 31 + ppcs_ptr->this_frame_target, 5);
1904         rc->long_rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64(
1905             rc->long_rolling_actual_bits * 31 + ppcs_ptr->projected_frame_size, 5);
1906     }
1907 
1908     // Actual bits spent
1909     rc->total_actual_bits += ppcs_ptr->projected_frame_size;
1910     rc->total_target_bits += ppcs_ptr->frm_hdr.showable_frame ? rc->avg_frame_bandwidth : 0;
1911 
1912     rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1913 
1914     if (is_altref_enabled(gf_cfg->lag_in_frames, gf_cfg->enable_auto_arf) &&
1915         refresh_frame_flags->alt_ref_frame &&
1916         (current_frame->frame_type != KEY_FRAME &&
1917          current_frame->frame_type != S_FRAME /*!frame_is_sframe(cm)*/))
1918         // Update the alternate reference frame stats as appropriate.
1919         update_alt_ref_frame_stats(ppcs_ptr);
1920     else
1921         // Update the Golden frame stats as appropriate.
1922         update_golden_frame_stats(ppcs_ptr);
1923 
1924     if (current_frame->frame_type == KEY_FRAME)
1925         rc->frames_since_key = 0;
1926     // if (current_frame->frame_number == 1 && ppcs_ptr->frm_hdr.show_frame)
1927     /*
1928   rc->this_frame_target =
1929       (int)(rc->this_frame_target / resize_rate_factor(&cpi->oxcf.frm_dim_cfg,
1930   cm->width, cm->height));
1931       */
1932 }
update_rc_counts(PictureParentControlSet * ppcs_ptr)1933 void update_rc_counts(PictureParentControlSet *ppcs_ptr) {
1934     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
1935     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1936     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1937     GF_GROUP *const     gf_group           = &encode_context_ptr->gf_group;
1938 
1939     if (ppcs_ptr->frm_hdr.showable_frame) {
1940         // If this is a show_existing_frame with a source other than altref,
1941         // or if it is not a displayed forward keyframe, the keyframe update
1942         // counters were incremented when it was originally encoded.
1943         rc->frames_since_key++;
1944         rc->frames_to_key--;
1945     }
1946     //update_gf_group_index(cpi);
1947     // Increment the gf group index ready for the next frame. If this is
1948     // a show_existing_frame with a source other than altref, or if it is not
1949     // a displayed forward keyframe, the index was incremented when it was
1950     // originally encoded.
1951     ++gf_group->index;
1952 }
1953 
av1_rc_set_frame_target(PictureControlSet * pcs_ptr,int target,int width,int height)1954 static void av1_rc_set_frame_target(PictureControlSet *pcs_ptr, int target, int width, int height) {
1955     SequenceControlSet *scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
1956     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
1957     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
1958     pcs_ptr->parent_pcs_ptr->this_frame_target = target;
1959     // Modify frame size target when down-scaled.
1960     //if (av1_frame_scaled(cm))
1961     //    rc->this_frame_target =
1962     //    (int)(rc->this_frame_target *
1963     //        resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height));
1964 
1965     // Target rate per SB64 (including partial SB64s.
1966     rc->sb64_target_rate = (int)(((int64_t)pcs_ptr->parent_pcs_ptr->this_frame_target << 12) / (width * height));
1967 }
1968 #define VBR_PCT_ADJUSTMENT_LIMIT 50
1969 // For VBR...adjustment to the frame target based on error from previous frames
vbr_rate_correction(PictureControlSet * pcs_ptr,int * this_frame_target)1970 static void vbr_rate_correction(PictureControlSet *pcs_ptr, int *this_frame_target) {
1971     SequenceControlSet *scs_ptr             = pcs_ptr->parent_pcs_ptr->scs_ptr;
1972     EncodeContext *     encode_context_ptr  = scs_ptr->encode_context_ptr;
1973     RATE_CONTROL *      rc                  = &encode_context_ptr->rc;
1974     TWO_PASS *const     twopass             = &scs_ptr->twopass;
1975     int64_t             vbr_bits_off_target = rc->vbr_bits_off_target;
1976     const int           stats_count         = twopass->stats_buf_ctx->total_stats != NULL
1977                           ? (int)twopass->stats_buf_ctx->total_stats->count
1978                           : 0;
1979     const int frame_window = AOMMIN(16, (int)(stats_count - (int)pcs_ptr->picture_number));
1980     assert(VBR_PCT_ADJUSTMENT_LIMIT <= 100);
1981     if (frame_window > 0) {
1982         const int max_delta = (int)AOMMIN(
1983             abs((int)(vbr_bits_off_target / frame_window)),
1984             ((int64_t)(*this_frame_target) * VBR_PCT_ADJUSTMENT_LIMIT) / 100);
1985 
1986         // vbr_bits_off_target > 0 means we have extra bits to spend
1987         // vbr_bits_off_target < 0 we are currently overshooting
1988         *this_frame_target += (vbr_bits_off_target >= 0) ? max_delta : -max_delta;
1989     }
1990 
1991     // Fast redistribution of bits arising from massive local undershoot.
1992     // Dont do it for kf,arf,gf or overlay frames.
1993     if (!frame_is_kf_gf_arf(pcs_ptr->parent_pcs_ptr) && !rc->is_src_frame_alt_ref &&
1994         rc->vbr_bits_off_target_fast) {
1995         int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target);
1996         int fast_extra_bits;
1997         fast_extra_bits = (int)AOMMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
1998         fast_extra_bits = (int)AOMMIN(fast_extra_bits,
1999                                       AOMMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
2000         *this_frame_target += (int)fast_extra_bits;
2001         rc->vbr_bits_off_target_fast -= fast_extra_bits;
2002     }
2003 }
2004 
set_refresh_frame_flags(RefreshFrameFlagsInfo * const refresh_frame_flags,bool refresh_gf,bool refresh_bwdref,bool refresh_arf)2005 static INLINE void set_refresh_frame_flags(RefreshFrameFlagsInfo *const refresh_frame_flags,
2006                                            bool refresh_gf, bool refresh_bwdref, bool refresh_arf) {
2007     refresh_frame_flags->golden_frame  = refresh_gf;
2008     refresh_frame_flags->bwd_ref_frame = refresh_bwdref;
2009     refresh_frame_flags->alt_ref_frame = refresh_arf;
2010 }
2011 
av1_configure_buffer_updates(PictureControlSet * pcs_ptr,RefreshFrameFlagsInfo * const refresh_frame_flags,int force_refresh_all)2012 static void av1_configure_buffer_updates(PictureControlSet *          pcs_ptr,
2013                                          RefreshFrameFlagsInfo *const refresh_frame_flags,
2014                                          int                          force_refresh_all) {
2015     // NOTE(weitinglin): Should we define another function to take care of
2016     // cpi->rc.is_$Source_Type to make this function as it is in the comment?
2017     SequenceControlSet *    scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
2018     EncodeContext *         encode_context_ptr = scs_ptr->encode_context_ptr;
2019     RATE_CONTROL *          rc                 = &encode_context_ptr->rc;
2020     GF_GROUP *              gf_group           = &encode_context_ptr->gf_group;
2021     const FRAME_UPDATE_TYPE type = gf_group->update_type[pcs_ptr->parent_pcs_ptr->gf_group_index];
2022 
2023     rc->is_src_frame_alt_ref = 0;
2024 
2025     switch (type) {
2026     case KF_UPDATE: set_refresh_frame_flags(refresh_frame_flags, true, true, true); break;
2027 
2028     case LF_UPDATE: set_refresh_frame_flags(refresh_frame_flags, false, false, false); break;
2029 
2030     case GF_UPDATE: set_refresh_frame_flags(refresh_frame_flags, true, false, false); break;
2031 
2032     case OVERLAY_UPDATE:
2033         set_refresh_frame_flags(refresh_frame_flags, true, false, false);
2034         rc->is_src_frame_alt_ref = 1;
2035         break;
2036 
2037     case ARF_UPDATE:
2038         // NOTE: BWDREF does not get updated along with ALTREF_FRAME.
2039         set_refresh_frame_flags(refresh_frame_flags, false, false, true);
2040         break;
2041 
2042     case INTNL_OVERLAY_UPDATE:
2043         set_refresh_frame_flags(refresh_frame_flags, false, false, false);
2044         rc->is_src_frame_alt_ref = 1;
2045         break;
2046 
2047     case INTNL_ARF_UPDATE: set_refresh_frame_flags(refresh_frame_flags, false, true, false); break;
2048 
2049     default: assert(0); break;
2050     }
2051 
2052     if (force_refresh_all)
2053         set_refresh_frame_flags(refresh_frame_flags, true, true, true);
2054 }
2055 
av1_set_target_rate(PictureControlSet * pcs_ptr,int width,int height)2056 static void av1_set_target_rate(PictureControlSet *pcs_ptr, int width, int height) {
2057     SequenceControlSet *        scs_ptr            = pcs_ptr->parent_pcs_ptr->scs_ptr;
2058     EncodeContext *             encode_context_ptr = scs_ptr->encode_context_ptr;
2059     int                         target_rate        = pcs_ptr->parent_pcs_ptr->base_frame_target;
2060     const RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
2061     // Correction to rate target based on prior over or under shoot.
2062     if (rc_cfg->mode == AOM_VBR || rc_cfg->mode == AOM_CQ)
2063         vbr_rate_correction(pcs_ptr, &target_rate);
2064     av1_rc_set_frame_target(pcs_ptr, target_rate, width, height);
2065 }
2066 
av1_get_compression_ratio(PictureParentControlSet * ppcs_ptr,size_t encoded_frame_size)2067 static double av1_get_compression_ratio(PictureParentControlSet *ppcs_ptr,
2068                                         size_t                   encoded_frame_size) {
2069     const int upscaled_width = ppcs_ptr->av1_cm->frm_size.superres_upscaled_width;
2070     const int height         = ppcs_ptr->av1_cm->frm_size.frame_height; //cm->height;
2071     const int luma_pic_size  = upscaled_width * height;
2072     const /*BITSTREAM_PROFILE*/ EbAv1SeqProfile profile = ppcs_ptr->scs_ptr->seq_header.seq_profile;
2073     const int pic_size_profile_factor                   = profile == /*PROFILE_0*/ MAIN_PROFILE
2074                           ? 15
2075                           : (profile == /*PROFILE_1*/ HIGH_PROFILE ? 30 : 36);
2076     encoded_frame_size = (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
2077     const size_t uncompressed_frame_size = (luma_pic_size * pic_size_profile_factor) >> 3;
2078     return uncompressed_frame_size / (double)encoded_frame_size;
2079 }
2080 
av1_rc_compute_frame_size_bounds(PictureParentControlSet * ppcs_ptr,int frame_target,int * frame_under_shoot_limit,int * frame_over_shoot_limit)2081 static void av1_rc_compute_frame_size_bounds(PictureParentControlSet *ppcs_ptr, int frame_target,
2082                                              int *frame_under_shoot_limit,
2083                                              int *frame_over_shoot_limit) {
2084     EncodeContext *const        encode_context_ptr = ppcs_ptr->scs_ptr->encode_context_ptr;
2085     RATE_CONTROL *const         rc                 = &(encode_context_ptr->rc);
2086     const RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
2087     if (rc_cfg->mode == AOM_Q) {
2088         *frame_under_shoot_limit = 0;
2089         *frame_over_shoot_limit  = INT_MAX;
2090     } else {
2091         // For very small rate targets where the fractional adjustment
2092         // may be tiny make sure there is at least a minimum range.
2093         assert(encode_context_ptr->recode_tolerance <= 100);
2094         const int tolerance = (int)AOMMAX(
2095             100, ((int64_t)encode_context_ptr->recode_tolerance * frame_target) / 100);
2096         *frame_under_shoot_limit = AOMMAX(frame_target - tolerance, 0);
2097         *frame_over_shoot_limit  = AOMMIN(frame_target + tolerance, rc->max_frame_bandwidth);
2098     }
2099 }
2100 
2101 // Function to test for conditions that indicate we should loop
2102 // back and recode a frame.
recode_loop_test(PictureParentControlSet * ppcs_ptr,int high_limit,int low_limit,int q,int maxq,int minq)2103 static AOM_INLINE int recode_loop_test(PictureParentControlSet *ppcs_ptr, int high_limit,
2104                                        int low_limit, int q, int maxq, int minq) {
2105     EncodeContext *const        encode_context_ptr = ppcs_ptr->scs_ptr->encode_context_ptr;
2106     RATE_CONTROL *const         rc                 = &(encode_context_ptr->rc);
2107     const RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
2108     const int                   frame_is_kfgfarf   = frame_is_kf_gf_arf(ppcs_ptr);
2109     int                         force_recode       = 0;
2110     if ((ppcs_ptr->projected_frame_size >= rc->max_frame_bandwidth) ||
2111         (encode_context_ptr->recode_loop == ALLOW_RECODE) ||
2112         (frame_is_kfgfarf && (encode_context_ptr->recode_loop >= ALLOW_RECODE_KFMAXBW))) {
2113         // TODO(agrange) high_limit could be greater than the scale-down threshold.
2114         if ((ppcs_ptr->projected_frame_size > high_limit && q < maxq) ||
2115             (ppcs_ptr->projected_frame_size < low_limit && q > minq)) {
2116             force_recode = 1;
2117         }
2118         else if (rc_cfg->mode == AOM_CQ) {
2119             // Deal with frame undershoot and whether or not we are
2120             // below the automatically set cq level.
2121             if (q > rc_cfg->cq_level &&
2122                 ppcs_ptr->projected_frame_size < ((ppcs_ptr->this_frame_target * 7) >> 3)) {
2123                 force_recode = 1;
2124             }
2125         }
2126     }
2127     return force_recode;
2128 }
2129 
2130 // get overshoot regulated q based on q_low
get_regulated_q_overshoot(PictureParentControlSet * ppcs_ptr,int q_low,int q_high,int top_index,int bottom_index)2131 static int get_regulated_q_overshoot(PictureParentControlSet *ppcs_ptr, int q_low, int q_high,
2132                                      int top_index, int bottom_index) {
2133     const int            width              = ppcs_ptr->av1_cm->frm_size.frame_width;
2134     const int            height             = ppcs_ptr->av1_cm->frm_size.frame_height;
2135 
2136     av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2137 
2138     int q_regulated = av1_rc_regulate_q(
2139         ppcs_ptr, ppcs_ptr->this_frame_target, bottom_index, AOMMAX(q_high, top_index), width, height);
2140     int retries = 0;
2141     while (q_regulated < q_low && retries < 10) {
2142         av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2143         q_regulated = av1_rc_regulate_q(ppcs_ptr,
2144                                         ppcs_ptr->this_frame_target,
2145                                         bottom_index,
2146                                         AOMMAX(q_high, top_index),
2147                                         width,
2148                                         height);
2149         retries++;
2150     }
2151     return q_regulated;
2152 }
2153 
2154 // get undershoot regulated q based on q_high
get_regulated_q_undershoot(PictureParentControlSet * ppcs_ptr,int q_high,int top_index,int bottom_index)2155 static AOM_INLINE int get_regulated_q_undershoot(PictureParentControlSet *ppcs_ptr, int q_high,
2156                                                  int top_index, int bottom_index) {
2157     const int            width              = ppcs_ptr->av1_cm->frm_size.frame_width;
2158     const int            height             = ppcs_ptr->av1_cm->frm_size.frame_height;
2159 
2160     av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2161     int q_regulated = av1_rc_regulate_q(
2162         ppcs_ptr, ppcs_ptr->this_frame_target, bottom_index, top_index, width, height);
2163 
2164     int retries = 0;
2165     while (q_regulated > q_high && retries < 10) {
2166         av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2167         q_regulated = av1_rc_regulate_q(
2168             ppcs_ptr, ppcs_ptr->this_frame_target, bottom_index, top_index, width, height);
2169         retries++;
2170     }
2171     return q_regulated;
2172 }
2173 
2174 // This function works out whether we under- or over-shot
2175 // our bitrate target and adjusts q as appropriate.  Also decides whether
2176 // or not we should do another recode loop, indicated by *loop
recode_loop_update_q(PictureParentControlSet * ppcs_ptr,int * const loop,int * const q,int * const q_low,int * const q_high,const int top_index,const int bottom_index,int * const undershoot_seen,int * const overshoot_seen,int * const low_cr_seen,const int loop_count)2177 void recode_loop_update_q(PictureParentControlSet *ppcs_ptr, int *const loop, int *const q,
2178                           int *const q_low, int *const q_high, const int top_index,
2179                           const int bottom_index, int *const undershoot_seen,
2180                           int *const overshoot_seen, int *const low_cr_seen, const int loop_count) {
2181     SequenceControlSet *const   scs_ptr            = ppcs_ptr->scs_ptr;
2182     EncodeContext *const        encode_context_ptr = scs_ptr->encode_context_ptr;
2183     RATE_CONTROL *const         rc                 = &(encode_context_ptr->rc);
2184     const RateControlCfg *const rc_cfg             = &encode_context_ptr->rc_cfg;
2185     const int do_dummy_pack = (scs_ptr->encode_context_ptr->recode_loop >= ALLOW_RECODE_KFMAXBW &&
2186                                rc_cfg->mode != AOM_Q) ||
2187         rc_cfg->min_cr > 0;
2188     ppcs_ptr->projected_frame_size = do_dummy_pack
2189         ? (int)(((ppcs_ptr->pcs_total_rate + (1 << (AV1_PROB_COST_SHIFT - 1))) >>
2190             AV1_PROB_COST_SHIFT) +
2191             ((ppcs_ptr->frm_hdr.frame_type == KEY_FRAME) ? 13 : 0))
2192         : 0;
2193     if (ppcs_ptr->loop_count) {
2194         // scale rc->projected_frame_size with *0.8 for loop_count>=1
2195         ppcs_ptr->projected_frame_size = (ppcs_ptr->projected_frame_size * 8) / 10;
2196     }
2197     *loop = 0;
2198     if (scs_ptr->encode_context_ptr->recode_loop == ALLOW_RECODE_KFMAXBW &&
2199         ppcs_ptr->frm_hdr.frame_type != KEY_FRAME) {
2200         // skip re-encode for inter frame when setting -recode-loop 1
2201         return;
2202     }
2203 
2204     const int min_cr = rc_cfg->min_cr;
2205     if (min_cr > 0) {
2206         //aom_clear_system_state();
2207         const double compression_ratio = av1_get_compression_ratio(ppcs_ptr,
2208             ppcs_ptr->projected_frame_size >> 3);
2209         const double target_cr         = min_cr / 100.0;
2210         if (compression_ratio < target_cr) {
2211             *low_cr_seen = 1;
2212             if (*q < rc->worst_quality) {
2213                 const double cr_ratio    = target_cr / compression_ratio;
2214                 const int    projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
2215                 *q                       = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
2216                 *q_low                   = AOMMAX(*q, *q_low);
2217                 *q_high                  = AOMMAX(*q, *q_high);
2218                 *loop                    = 1;
2219             }
2220         }
2221         if (*low_cr_seen)
2222             return;
2223     }
2224 
2225     if (rc_cfg->mode == AOM_Q)
2226         return;
2227 
2228     const int last_q                 = *q;
2229     int       frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
2230     av1_rc_compute_frame_size_bounds(
2231         ppcs_ptr, ppcs_ptr->this_frame_target, &frame_under_shoot_limit, &frame_over_shoot_limit);
2232     if (frame_over_shoot_limit == 0)
2233         frame_over_shoot_limit = 1;
2234 
2235     if (recode_loop_test(ppcs_ptr,
2236                          frame_over_shoot_limit,
2237                          frame_under_shoot_limit,
2238                          *q,
2239                          AOMMAX(*q_high, top_index),
2240                          bottom_index)) {
2241         const int width  = ppcs_ptr->av1_cm->frm_size.frame_width;
2242         const int height = ppcs_ptr->av1_cm->frm_size.frame_height;
2243         // Is the projected frame size out of range and are we allowed
2244         // to attempt to recode.
2245 
2246         // Frame size out of permitted range:
2247         // Update correction factor & compute new Q to try...
2248         // Frame is too large
2249         if (ppcs_ptr->projected_frame_size > ppcs_ptr->this_frame_target) {
2250             // Special case if the projected size is > the max allowed.
2251             if (*q == *q_high && ppcs_ptr->projected_frame_size >= rc->max_frame_bandwidth) {
2252                 const double q_val_high_current = svt_av1_convert_qindex_to_q(
2253                     *q_high, scs_ptr->static_config.encoder_bit_depth);
2254                 const double q_val_high_new = q_val_high_current *
2255                     ((double)ppcs_ptr->projected_frame_size / rc->max_frame_bandwidth);
2256                 *q_high = av1_find_qindex(q_val_high_new,
2257                                           scs_ptr->static_config.encoder_bit_depth,
2258                                           rc->best_quality,
2259                                           rc->worst_quality);
2260             }
2261             // Raise Qlow as to at least the current value
2262             *q_low = AOMMIN(*q + 1, *q_high);
2263 
2264             if (*undershoot_seen || loop_count > 2 ||
2265                 (loop_count == 2 && !frame_is_intra_only(ppcs_ptr))) {
2266                 av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2267 
2268                 *q = (*q_high + *q_low + 1) / 2;
2269             } else if (loop_count == 2 && frame_is_intra_only(ppcs_ptr)) {
2270                 const int q_mid       = (*q_high + *q_low + 1) / 2;
2271                 const int q_regulated = get_regulated_q_overshoot(
2272                     ppcs_ptr, *q_low, *q_high, top_index, bottom_index);
2273                 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
2274                 // transition between loop_count < 2 and loop_count > 2.
2275                 *q = (q_mid + q_regulated + 1) / 2;
2276             } else {
2277                 *q = get_regulated_q_overshoot(ppcs_ptr, *q_low, *q_high, top_index, bottom_index);
2278             }
2279 
2280             *overshoot_seen = 1;
2281         } else {
2282             // Frame is too small
2283             *q_high = AOMMAX(*q - 1, *q_low);
2284 
2285             if (*overshoot_seen || loop_count > 2 ||
2286                 (loop_count == 2 && !frame_is_intra_only(ppcs_ptr))) {
2287                 av1_rc_update_rate_correction_factors(ppcs_ptr, width, height);
2288                 *q = (*q_high + *q_low) / 2;
2289             } else if (loop_count == 2 && frame_is_intra_only(ppcs_ptr)) {
2290                 const int q_mid       = (*q_high + *q_low) / 2;
2291                 const int q_regulated = get_regulated_q_undershoot(
2292                     ppcs_ptr, *q_high, top_index, bottom_index);
2293                 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
2294                 // transition between loop_count < 2 and loop_count > 2.
2295                 *q = (q_mid + q_regulated) / 2;
2296 
2297                 // Special case reset for qlow for constrained quality.
2298                 // This should only trigger where there is very substantial
2299                 // undershoot on a frame and the auto cq level is above
2300                 // the user passsed in value.
2301                 if (rc_cfg->mode == AOM_CQ && q_regulated < *q_low) {
2302                     *q_low = *q;
2303                 }
2304             } else {
2305                 *q = get_regulated_q_undershoot(ppcs_ptr, *q_high, top_index, bottom_index);
2306 
2307                 // Special case reset for qlow for constrained quality.
2308                 // This should only trigger where there is very substantial
2309                 // undershoot on a frame and the auto cq level is above
2310                 // the user passsed in value.
2311                 if (rc_cfg->mode == AOM_CQ && *q < *q_low) {
2312                     *q_low = *q;
2313                 }
2314             }
2315 
2316             *undershoot_seen = 1;
2317         }
2318 
2319         // Clamp Q to upper and lower limits:
2320         *q = clamp(*q, *q_low, *q_high);
2321     }
2322 
2323     *q    = (uint8_t)CLIP3((int32_t)quantizer_to_qindex[scs_ptr->static_config.min_qp_allowed],
2324                         (int32_t)quantizer_to_qindex[scs_ptr->static_config.max_qp_allowed],
2325                         *q);
2326     *loop = (*q != last_q);
2327 }
2328 /************************************************************************************************
2329 * Populate the required parameters in two_pass structure from other structures
2330 *************************************************************************************************/
restore_two_pass_param(PictureParentControlSet * ppcs_ptr,RateControlIntervalParamContext * rate_control_param_ptr)2331 static void restore_two_pass_param(PictureParentControlSet *        ppcs_ptr,
2332                                    RateControlIntervalParamContext *rate_control_param_ptr) {
2333     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
2334     TWO_PASS *const     twopass = &scs_ptr->twopass;
2335 
2336     twopass->stats_in = scs_ptr->twopass.stats_buf_ctx->stats_in_start + ppcs_ptr->stats_in_offset;
2337     twopass->stats_buf_ctx->stats_in_end = scs_ptr->twopass.stats_buf_ctx->stats_in_start +
2338         ppcs_ptr->stats_in_end_offset;
2339     twopass->kf_group_bits       = rate_control_param_ptr->kf_group_bits;
2340     twopass->kf_group_error_left = rate_control_param_ptr->kf_group_error_left;
2341 }
2342 /************************************************************************************************
2343 * Populate the required parameters in gf_group structure from other structures
2344 *************************************************************************************************/
restore_gf_group_param(PictureParentControlSet * ppcs_ptr)2345 static void restore_gf_group_param(PictureParentControlSet *ppcs_ptr) {
2346     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
2347     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
2348     GF_GROUP *const     gf_group = &encode_context_ptr->gf_group;
2349     gf_group->index = ppcs_ptr->gf_group_index;
2350     gf_group->size = ppcs_ptr->gf_group_size;
2351     gf_group->update_type[gf_group->index] = ppcs_ptr->update_type;
2352     gf_group->layer_depth[gf_group->index] = ppcs_ptr->layer_depth;
2353     gf_group->arf_boost[gf_group->index] = ppcs_ptr->arf_boost;
2354 }
2355 /************************************************************************************************
2356 * Populate the required parameters in rc, twopass and gf_group structures from other structures
2357 *************************************************************************************************/
restore_param(PictureParentControlSet * ppcs_ptr,RateControlIntervalParamContext * rate_control_param_ptr)2358 static void restore_param(PictureParentControlSet *ppcs_ptr,
2359     RateControlIntervalParamContext *rate_control_param_ptr) {
2360     restore_two_pass_param(ppcs_ptr, rate_control_param_ptr);
2361     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
2362     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
2363 
2364     const KeyFrameCfg *const kf_cfg = &encode_context_ptr->kf_cfg;
2365     ppcs_ptr->frames_since_key = (int)(ppcs_ptr->decode_order - ppcs_ptr->last_idr_picture);
2366     int key_max;
2367     if (scs_ptr->lap_enabled) {
2368         if (scs_ptr->static_config.hierarchical_levels != ppcs_ptr->hierarchical_levels)
2369             key_max = (int)MIN(
2370                 kf_cfg->key_freq_max,
2371                 (int)((int64_t)((scs_ptr->twopass.stats_buf_ctx->stats_in_end - 1)->frame) -
2372                       ppcs_ptr->last_idr_picture + 1));
2373         else
2374             key_max = kf_cfg->key_freq_max;
2375     } else {
2376         key_max = (int)MIN(
2377             kf_cfg->key_freq_max,
2378             (int)((int64_t)((scs_ptr->twopass.stats_buf_ctx->stats_in_end - 1)->frame) -
2379                   ppcs_ptr->last_idr_picture + 1));
2380     }
2381     ppcs_ptr->frames_to_key = key_max - ppcs_ptr->frames_since_key;
2382     restore_gf_group_param(ppcs_ptr);
2383 }
2384 /************************************************************************************************
2385 * Store the required parameters from rc structure to other structures
2386 *************************************************************************************************/
store_rc_param(PictureParentControlSet * ppcs_ptr)2387 static void store_rc_param(PictureParentControlSet *ppcs_ptr) {
2388     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
2389     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
2390     RATE_CONTROL *      rc                 = &encode_context_ptr->rc;
2391 
2392     ppcs_ptr->is_src_frame_alt_ref = ppcs_ptr->is_overlay;
2393     if (ppcs_ptr->is_new_gf_group) {
2394         for (uint8_t frame_idx = 0; frame_idx < (int32_t)ppcs_ptr->gf_interval; frame_idx++) {
2395             ppcs_ptr->gf_group[frame_idx]->num_stats_used_for_gfu_boost = rc->num_stats_used_for_gfu_boost;
2396             ppcs_ptr->gf_group[frame_idx]->num_stats_required_for_gfu_boost = rc->num_stats_required_for_gfu_boost;
2397         }
2398     }
2399 }
2400 /************************************************************************************************
2401 * Store the required parameters in two_pass structure from other structures
2402 *************************************************************************************************/
store_two_pass_param(PictureParentControlSet * ppcs_ptr,RateControlIntervalParamContext * rate_control_param_ptr)2403 static void store_two_pass_param(PictureParentControlSet *        ppcs_ptr,
2404                                  RateControlIntervalParamContext *rate_control_param_ptr) {
2405     SequenceControlSet *scs_ptr = ppcs_ptr->scs_ptr;
2406     TWO_PASS *const     twopass = &scs_ptr->twopass;
2407 
2408     rate_control_param_ptr->kf_group_bits       = twopass->kf_group_bits;
2409     rate_control_param_ptr->kf_group_error_left = twopass->kf_group_error_left;
2410 }
2411 /************************************************************************************************
2412 * Store the required parameters from gf_group structure to other structures
2413 *************************************************************************************************/
store_gf_group_param(PictureParentControlSet * ppcs_ptr)2414 static void store_gf_group_param(PictureParentControlSet *ppcs_ptr) {
2415     SequenceControlSet *scs_ptr            = ppcs_ptr->scs_ptr;
2416     EncodeContext *     encode_context_ptr = scs_ptr->encode_context_ptr;
2417     GF_GROUP *const     gf_group           = &encode_context_ptr->gf_group;
2418     if (ppcs_ptr->is_new_gf_group) {
2419         for (uint8_t frame_idx = 0; frame_idx < (int32_t)ppcs_ptr->gf_interval; frame_idx++) {
2420             uint8_t gf_group_index = ppcs_ptr->slice_type == I_SLICE ? frame_idx : frame_idx + 1;
2421             ppcs_ptr->gf_group[frame_idx]->gf_group_index = gf_group_index;
2422             ppcs_ptr->gf_group[frame_idx]->gf_group_size = MAX(gf_group->size, ppcs_ptr->gf_interval);
2423             ppcs_ptr->gf_group[frame_idx]->update_type    = gf_group->update_type[gf_group_index];
2424             ppcs_ptr->gf_group[frame_idx]->layer_depth    = gf_group->layer_depth[gf_group_index];
2425             ppcs_ptr->gf_group[frame_idx]->arf_boost      = gf_group->arf_boost[gf_group_index];
2426             ppcs_ptr->gf_group[frame_idx]->base_frame_target =
2427                 gf_group->bit_allocation[gf_group_index];
2428         }
2429     }
2430 }
2431 /************************************************************************************************
2432 * Store the required parameters from rc, twopass and gf_group structures to other structures
2433 *************************************************************************************************/
store_param(PictureParentControlSet * ppcs_ptr,RateControlIntervalParamContext * rate_control_param_ptr)2434 static void store_param(PictureParentControlSet *ppcs_ptr,
2435     RateControlIntervalParamContext *rate_control_param_ptr) {
2436 
2437     store_rc_param(ppcs_ptr);
2438     store_two_pass_param(ppcs_ptr, rate_control_param_ptr);
2439     store_gf_group_param(ppcs_ptr);
2440 }
rate_control_kernel(void * input_ptr)2441 void *rate_control_kernel(void *input_ptr) {
2442     // Context
2443     EbThreadContext *   thread_context_ptr = (EbThreadContext *)input_ptr;
2444     RateControlContext *context_ptr        = (RateControlContext *)thread_context_ptr->priv;
2445 
2446     RateControlIntervalParamContext *rate_control_param_ptr;
2447 
2448     PictureControlSet *      pcs_ptr;
2449     PictureParentControlSet *parentpicture_control_set_ptr;
2450 
2451     // Config
2452     SequenceControlSet *scs_ptr;
2453 
2454     // Input
2455     EbObjectWrapper * rate_control_tasks_wrapper_ptr;
2456     RateControlTasks *rate_control_tasks_ptr;
2457 
2458     // Output
2459     EbObjectWrapper *   rate_control_results_wrapper_ptr;
2460     RateControlResults *rate_control_results_ptr;
2461 
2462     uint64_t total_number_of_fb_frames = 0;
2463 
2464     RateControlTaskTypes task_type;
2465     RATE_CONTROL         rc;
2466 
2467     for (;;) {
2468         // Get RateControl Task
2469         EB_GET_FULL_OBJECT(context_ptr->rate_control_input_tasks_fifo_ptr,
2470                            &rate_control_tasks_wrapper_ptr);
2471 
2472         rate_control_tasks_ptr = (RateControlTasks *)rate_control_tasks_wrapper_ptr->object_ptr;
2473         task_type              = rate_control_tasks_ptr->task_type;
2474 
2475         // Modify these for different temporal layers later
2476         switch (task_type) {
2477         case RC_INPUT:
2478             pcs_ptr = (PictureControlSet *)rate_control_tasks_ptr->pcs_wrapper_ptr->object_ptr;
2479 
2480             scs_ptr = (SequenceControlSet *)pcs_ptr->scs_wrapper_ptr->object_ptr;
2481             FrameHeader *frm_hdr                       = &pcs_ptr->parent_pcs_ptr->frm_hdr;
2482             pcs_ptr->parent_pcs_ptr->blk_lambda_tuning = EB_FALSE;
2483 
2484             // SB Loop
2485             pcs_ptr->parent_pcs_ptr->sad_me = 0;
2486             if (pcs_ptr->slice_type != 2)
2487                 for (int sb_addr = 0; sb_addr < pcs_ptr->sb_total_count; ++sb_addr) {
2488                     pcs_ptr->parent_pcs_ptr->sad_me +=
2489                         pcs_ptr->parent_pcs_ptr->rc_me_distortion[sb_addr];
2490                 }
2491             // Frame level RC. Find the ParamPtr for the current GOP
2492             if (scs_ptr->intra_period_length == -1 ||
2493                 scs_ptr->static_config.rate_control_mode == 0) {
2494                 rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
2495             }
2496             else {
2497                 uint32_t interval_index_temp = 0;
2498                 EbBool   interval_found = EB_FALSE;
2499                 while ((interval_index_temp < PARALLEL_GOP_MAX_NUMBER) && !interval_found) {
2500                     if (pcs_ptr->picture_number >=
2501                         context_ptr->rate_control_param_queue[interval_index_temp]->first_poc &&
2502                         pcs_ptr->picture_number <=
2503                         context_ptr->rate_control_param_queue[interval_index_temp]->last_poc) {
2504                         interval_found = EB_TRUE;
2505                     }
2506                     else
2507                         interval_index_temp++;
2508                 }
2509                 CHECK_REPORT_ERROR(interval_index_temp != PARALLEL_GOP_MAX_NUMBER,
2510                     scs_ptr->encode_context_ptr->app_callback_ptr,
2511                     EB_ENC_RC_ERROR2);
2512 
2513                 rate_control_param_ptr = context_ptr->rate_control_param_queue[interval_index_temp];
2514 
2515             }
2516 
2517             if (use_input_stat(scs_ptr) || scs_ptr->lap_enabled) {
2518                 if (pcs_ptr->picture_number == 0) {
2519                     set_rc_buffer_sizes(scs_ptr);
2520                     av1_rc_init(scs_ptr);
2521                 }
2522                 restore_param(pcs_ptr->parent_pcs_ptr, rate_control_param_ptr);
2523                 svt_av1_get_second_pass_params(pcs_ptr->parent_pcs_ptr);
2524                 av1_configure_buffer_updates(pcs_ptr, &(pcs_ptr->parent_pcs_ptr->refresh_frame), 0);
2525                 av1_set_target_rate(pcs_ptr,
2526                                     pcs_ptr->parent_pcs_ptr->av1_cm->frm_size.frame_width,
2527                                     pcs_ptr->parent_pcs_ptr->av1_cm->frm_size.frame_height);
2528                 store_param(pcs_ptr->parent_pcs_ptr, rate_control_param_ptr);
2529             }
2530             if (scs_ptr->static_config.rate_control_mode == 0) {
2531                 // if RC mode is 0,  fixed QP is used
2532                 // QP scaling based on POC number for Flat IPPP structure
2533                 frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[pcs_ptr->picture_qp];
2534 
2535                 if (scs_ptr->static_config.use_fixed_qindex_offsets == 1) {
2536                     pcs_ptr->picture_qp = scs_ptr->static_config.qp;
2537                     int32_t qindex = quantizer_to_qindex[(uint8_t)scs_ptr->static_config.qp];
2538                     if (!frame_is_intra_only(pcs_ptr->parent_pcs_ptr)) {
2539                         qindex += scs_ptr->static_config.qindex_offsets[pcs_ptr->temporal_layer_index];
2540                     } else {
2541                         qindex += scs_ptr->static_config.key_frame_qindex_offset;
2542                     }
2543                     qindex = CLIP3(quantizer_to_qindex[scs_ptr->static_config.min_qp_allowed],
2544                         quantizer_to_qindex[scs_ptr->static_config.max_qp_allowed], qindex);
2545                     int32_t chroma_qindex = qindex;
2546                     if (frame_is_intra_only(pcs_ptr->parent_pcs_ptr)) {
2547                         chroma_qindex += scs_ptr->static_config.key_frame_chroma_qindex_offset;
2548                     } else {
2549                         chroma_qindex += scs_ptr->static_config.chroma_qindex_offsets[pcs_ptr->temporal_layer_index];
2550                     }
2551 
2552                     chroma_qindex = CLIP3(quantizer_to_qindex[scs_ptr->static_config.min_qp_allowed],
2553                         quantizer_to_qindex[scs_ptr->static_config.max_qp_allowed], chroma_qindex);
2554                     frm_hdr->quantization_params.base_q_idx = qindex;
2555                     frm_hdr->quantization_params.delta_q_dc[1] =
2556                     frm_hdr->quantization_params.delta_q_dc[2] =
2557                     frm_hdr->quantization_params.delta_q_ac[1] =
2558                     frm_hdr->quantization_params.delta_q_ac[2] = (chroma_qindex - qindex);
2559                     pcs_ptr->picture_qp =
2560                         (uint8_t)CLIP3((int32_t)scs_ptr->static_config.min_qp_allowed,
2561                         (int32_t)scs_ptr->static_config.max_qp_allowed,
2562                         (frm_hdr->quantization_params.base_q_idx + 2) >> 2);
2563 /*
2564                     printf("\nSVT: Frame Type = %s, PicNumber = %lld, DecoderOrder = %lld, Temp Layer "
2565                            "Index = %d, Picture QP = %d, Picture Qindex = %d, Picture Chroma Qindex = %d\n",
2566                            frame_is_intra_only(pcs_ptr->parent_pcs_ptr) ? "INTRA" : "INTER",
2567                            pcs_ptr->picture_number,
2568                            pcs_ptr->parent_pcs_ptr->decode_order,
2569                            pcs_ptr->temporal_layer_index,
2570                            pcs_ptr->picture_qp,
2571                            frm_hdr->quantization_params.base_q_idx,
2572                            chroma_qindex);
2573 */
2574                 }
2575                 else
2576 
2577                 if (scs_ptr->static_config.enable_qp_scaling_flag &&
2578                     pcs_ptr->parent_pcs_ptr->qp_on_the_fly == EB_FALSE) {
2579                     const int32_t qindex = quantizer_to_qindex[(uint8_t)scs_ptr->static_config.qp];
2580                     // if there are need enough pictures in the LAD/SlidingWindow, the adaptive QP scaling is not used
2581                     int32_t new_qindex;
2582                     if (!use_output_stat(scs_ptr)) {
2583                         // Content adaptive qp assignment
2584                         // 1pass QPS with tpl_la
2585                         if (!use_input_stat(scs_ptr) && scs_ptr->static_config.enable_tpl_la)
2586                             new_qindex = cqp_qindex_calc_tpl_la(pcs_ptr, &rc, qindex);
2587                         else if (use_input_stat(scs_ptr)) {
2588                             int32_t update_type =
2589                                 scs_ptr->encode_context_ptr->gf_group
2590                                     .update_type[pcs_ptr->parent_pcs_ptr->gf_group_index];
2591                             frm_hdr->quantization_params.base_q_idx =
2592                                 quantizer_to_qindex[pcs_ptr->picture_qp];
2593                             if (scs_ptr->static_config.enable_tpl_la &&
2594                                 pcs_ptr->parent_pcs_ptr->r0 != 0 &&
2595                                 (update_type == KF_UPDATE || update_type == GF_UPDATE ||
2596                                  update_type == ARF_UPDATE)) {
2597                                 process_tpl_stats_frame_kf_gfu_boost(pcs_ptr);
2598                             }
2599                             // VBR Qindex calculating
2600                             new_qindex = rc_pick_q_and_bounds(pcs_ptr);
2601                         } else
2602                             new_qindex = cqp_qindex_calc(pcs_ptr, &rc, qindex);
2603                     } else {
2604                         new_qindex = find_fp_qindex(
2605                             (AomBitDepth)scs_ptr->static_config.encoder_bit_depth);
2606                     }
2607                     frm_hdr->quantization_params.base_q_idx = (uint8_t)CLIP3(
2608                         (int32_t)quantizer_to_qindex[scs_ptr->static_config.min_qp_allowed],
2609                         (int32_t)quantizer_to_qindex[scs_ptr->static_config.max_qp_allowed],
2610                         (int32_t)(new_qindex));
2611 
2612                     pcs_ptr->picture_qp = (uint8_t)CLIP3(
2613                         (int32_t)scs_ptr->static_config.min_qp_allowed,
2614                         (int32_t)scs_ptr->static_config.max_qp_allowed,
2615                         (frm_hdr->quantization_params.base_q_idx + 2) >> 2);
2616                 }
2617                 else if (pcs_ptr->parent_pcs_ptr->qp_on_the_fly == EB_TRUE) {
2618                     pcs_ptr->picture_qp = (uint8_t)CLIP3(
2619                         (int32_t)scs_ptr->static_config.min_qp_allowed,
2620                         (int32_t)scs_ptr->static_config.max_qp_allowed,
2621                         pcs_ptr->parent_pcs_ptr->picture_qp);
2622                     frm_hdr->quantization_params.base_q_idx =
2623                         quantizer_to_qindex[pcs_ptr->picture_qp];
2624                 }
2625 
2626                 pcs_ptr->parent_pcs_ptr->picture_qp = pcs_ptr->picture_qp;
2627                 setup_segmentation(pcs_ptr, scs_ptr);
2628             } else {
2629                 // ***Rate Control***
2630                 if (scs_ptr->static_config.rate_control_mode == 1) {
2631                     if (use_input_stat(scs_ptr) || scs_ptr->lap_enabled) {
2632                         int32_t new_qindex =
2633                             quantizer_to_qindex[(uint8_t)scs_ptr->static_config.qp];
2634                         int32_t update_type =
2635                             scs_ptr->encode_context_ptr->gf_group
2636                                 .update_type[pcs_ptr->parent_pcs_ptr->gf_group_index];
2637                         frm_hdr->quantization_params.base_q_idx =
2638                             quantizer_to_qindex[pcs_ptr->picture_qp];
2639                         if (scs_ptr->static_config.enable_tpl_la &&
2640                             pcs_ptr->parent_pcs_ptr->r0 != 0 &&
2641                             (update_type == KF_UPDATE || update_type == GF_UPDATE ||
2642                              update_type == ARF_UPDATE)) {
2643                             process_tpl_stats_frame_kf_gfu_boost(pcs_ptr);
2644                         }
2645                         // VBR Qindex calculating
2646                         new_qindex                              = rc_pick_q_and_bounds(pcs_ptr);
2647                         frm_hdr->quantization_params.base_q_idx = (uint8_t)CLIP3(
2648                             (int32_t)quantizer_to_qindex[scs_ptr->static_config.min_qp_allowed],
2649                             (int32_t)quantizer_to_qindex[scs_ptr->static_config.max_qp_allowed],
2650                             (int32_t)(new_qindex));
2651 
2652                         pcs_ptr->picture_qp = (uint8_t)CLIP3(
2653                             (int32_t)scs_ptr->static_config.min_qp_allowed,
2654                             (int32_t)scs_ptr->static_config.max_qp_allowed,
2655                             (frm_hdr->quantization_params.base_q_idx + 2) >> 2);
2656 
2657                         //Limiting the QP based on the QP of the Reference frame
2658                         if ((int32_t)pcs_ptr->temporal_layer_index != 0) {
2659                             uint32_t ref_qp = 0;
2660                             if (pcs_ptr->ref_slice_type_array[0][0] != I_SLICE)
2661                                 ref_qp = pcs_ptr->ref_pic_qp_array[0][0];
2662                             if ((pcs_ptr->slice_type == B_SLICE) &&
2663                                 (pcs_ptr->ref_slice_type_array[1][0] != I_SLICE))
2664                                 ref_qp = MAX(ref_qp, pcs_ptr->ref_pic_qp_array[1][0]);
2665                             if (ref_qp > 0 && pcs_ptr->picture_qp < ref_qp ) {
2666                                 pcs_ptr->picture_qp = (uint8_t)CLIP3(scs_ptr->static_config.min_qp_allowed,
2667                                     scs_ptr->static_config.max_qp_allowed,
2668                                     (uint8_t)(ref_qp ));
2669 
2670                                 frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[pcs_ptr->picture_qp];
2671                             }
2672                         }
2673 
2674                     }
2675                 }
2676                 pcs_ptr->picture_qp = (uint8_t)CLIP3(scs_ptr->static_config.min_qp_allowed,
2677                                                      scs_ptr->static_config.max_qp_allowed,
2678                                                      pcs_ptr->picture_qp);
2679 
2680                 frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[pcs_ptr->picture_qp];
2681             }
2682 
2683             pcs_ptr->parent_pcs_ptr->picture_qp = pcs_ptr->picture_qp;
2684 
2685             // 2pass QPM with tpl_la
2686             if (scs_ptr->static_config.enable_adaptive_quantization == 2 &&
2687                 !use_output_stat(scs_ptr) && (use_input_stat(scs_ptr) || scs_ptr->lap_enabled) &&
2688                 scs_ptr->static_config.enable_tpl_la && pcs_ptr->parent_pcs_ptr->r0 != 0)
2689                 sb_qp_derivation_tpl_la(pcs_ptr);
2690             else
2691                 // 1pass QPM with tpl_la
2692                 if (scs_ptr->static_config.enable_adaptive_quantization == 2 &&
2693                     !use_output_stat(scs_ptr) && !use_input_stat(scs_ptr) &&
2694                     scs_ptr->static_config.enable_tpl_la && pcs_ptr->parent_pcs_ptr->r0 != 0)
2695                 sb_qp_derivation_tpl_la(pcs_ptr);
2696             else {
2697                 pcs_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 0;
2698                 pcs_ptr->parent_pcs_ptr->average_qp                             = 0;
2699                 for (int sb_addr = 0; sb_addr < pcs_ptr->sb_total_count_pix; ++sb_addr) {
2700                     SuperBlock *sb_ptr = pcs_ptr->sb_ptr_array[sb_addr];
2701                     sb_ptr->qindex     = quantizer_to_qindex[pcs_ptr->picture_qp];
2702                     pcs_ptr->parent_pcs_ptr->average_qp += pcs_ptr->picture_qp;
2703                 }
2704             }
2705             if (use_input_stat(scs_ptr) || scs_ptr->lap_enabled)
2706                 update_rc_counts(pcs_ptr->parent_pcs_ptr);
2707             // Get Empty Rate Control Results Buffer
2708             svt_get_empty_object(context_ptr->rate_control_output_results_fifo_ptr,
2709                                  &rate_control_results_wrapper_ptr);
2710             rate_control_results_ptr = (RateControlResults *)
2711                                            rate_control_results_wrapper_ptr->object_ptr;
2712             rate_control_results_ptr->pcs_wrapper_ptr = rate_control_tasks_ptr->pcs_wrapper_ptr;
2713 
2714             // Post Full Rate Control Results
2715             svt_post_full_object(rate_control_results_wrapper_ptr);
2716 
2717             // Release Rate Control Tasks
2718             svt_release_object(rate_control_tasks_wrapper_ptr);
2719 
2720             break;
2721 
2722         case RC_PACKETIZATION_FEEDBACK_RESULT:
2723 
2724             parentpicture_control_set_ptr = (PictureParentControlSet *)
2725                                                 rate_control_tasks_ptr->pcs_wrapper_ptr->object_ptr;
2726             scs_ptr = (SequenceControlSet *)
2727                 parentpicture_control_set_ptr->scs_wrapper_ptr->object_ptr;
2728             if (!use_output_stat(scs_ptr)) {
2729             restore_gf_group_param(parentpicture_control_set_ptr);
2730             if (scs_ptr->static_config.rate_control_mode == 0) {
2731                 av1_rc_postencode_update(parentpicture_control_set_ptr,
2732                                          (parentpicture_control_set_ptr->total_num_bits + 7) >> 3);
2733                 svt_av1_twopass_postencode_update(parentpicture_control_set_ptr);
2734             }
2735             if (scs_ptr->static_config.rate_control_mode != 0) {
2736                 if (scs_ptr->static_config.rate_control_mode == 1)
2737                     if (use_input_stat(scs_ptr) || scs_ptr->lap_enabled) {
2738                         av1_rc_postencode_update(
2739                             parentpicture_control_set_ptr,
2740                             (parentpicture_control_set_ptr->total_num_bits + 7) >> 3);
2741                         svt_av1_twopass_postencode_update(parentpicture_control_set_ptr);
2742                     }
2743             }
2744         }
2745             // Queue variables
2746             total_number_of_fb_frames++;
2747 
2748 
2749             // Release the SequenceControlSet
2750             svt_release_object(parentpicture_control_set_ptr->scs_wrapper_ptr);
2751             // Release the ParentPictureControlSet
2752             svt_release_object(parentpicture_control_set_ptr->input_picture_wrapper_ptr);
2753             svt_release_object(rate_control_tasks_ptr->pcs_wrapper_ptr);
2754 
2755             // Release Rate Control Tasks
2756             svt_release_object(rate_control_tasks_wrapper_ptr);
2757             break;
2758 
2759         case RC_ENTROPY_CODING_ROW_FEEDBACK_RESULT:
2760 
2761             // Extract bits-per-sb-row
2762 
2763             // Release Rate Control Tasks
2764             svt_release_object(rate_control_tasks_wrapper_ptr);
2765 
2766             break;
2767 
2768         default:
2769             pcs_ptr = (PictureControlSet *)rate_control_tasks_ptr->pcs_wrapper_ptr->object_ptr;
2770             scs_ptr = (SequenceControlSet *)pcs_ptr->scs_wrapper_ptr->object_ptr;
2771 
2772             break;
2773         }
2774     }
2775 
2776     return NULL;
2777 }
2778